Skip to content

Commit

Permalink
Try YUV without the mem pool
Browse files Browse the repository at this point in the history
  • Loading branch information
sergystepanov committed Dec 4, 2024
1 parent 954bb23 commit a7acebc
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
2 changes: 1 addition & 1 deletion pkg/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (v *Video) Encode(frame InFrame) OutFrame {
}

yCbCr := v.y.Process(yuv.RawFrame(frame), v.rot, v.pf)
defer v.y.Put(&yCbCr)
//defer v.y.Put(&yCbCr)
if bytes := v.codec.Encode(yCbCr); len(bytes) > 0 {
return bytes
}
Expand Down
20 changes: 12 additions & 8 deletions pkg/encoder/yuv/yuv.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
)

type Conv struct {
w, h int
sw, sh int
scale float64
pool sync.Pool
w, h int
sw, sh int
scale float64
pool sync.Pool
frame []byte
frameSc []byte
}

type RawFrame struct {
Expand All @@ -35,7 +37,9 @@ func NewYuvConv(w, h int, scale float64) Conv {
bufSize := int(float64(sw) * float64(sh) * 1.5)
return Conv{
w: w, h: h, sw: sw, sh: sh, scale: scale,
pool: sync.Pool{New: func() any { b := make([]byte, bufSize); return &b }},
pool: sync.Pool{New: func() any { b := make([]byte, bufSize); return &b }},
frame: make([]byte, bufSize),
frameSc: make([]byte, bufSize),
}
}

Expand All @@ -52,13 +56,13 @@ func (c *Conv) Process(frame RawFrame, rot uint, pf PixFmt) []byte {
stride = frame.Stride >> 1
}

buf := *c.pool.Get().(*[]byte)
buf := c.frame //*c.pool.Get().(*[]byte)
libyuv.Y420(frame.Data, buf, frame.W, frame.H, stride, dx, dy, rot, uint32(pf), cx, cy)

if c.scale > 1 {
dstBuf := *c.pool.Get().(*[]byte)
dstBuf := c.frameSc //*c.pool.Get().(*[]byte)
libyuv.Y420Scale(buf, dstBuf, dx, dy, c.sw, c.sh)
c.pool.Put(&buf)
//c.pool.Put(&buf)
return dstBuf
}
return buf
Expand Down

0 comments on commit a7acebc

Please sign in to comment.