From a7acebc5d0ccce454329e988729d13ca93720c6e Mon Sep 17 00:00:00 2001 From: Sergey Stepanov Date: Wed, 4 Dec 2024 22:09:51 +0300 Subject: [PATCH] Try YUV without the mem pool --- pkg/encoder/encoder.go | 2 +- pkg/encoder/yuv/yuv.go | 20 ++++++++++++-------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/pkg/encoder/encoder.go b/pkg/encoder/encoder.go index 550f75d1a..0372c2c59 100644 --- a/pkg/encoder/encoder.go +++ b/pkg/encoder/encoder.go @@ -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 } diff --git a/pkg/encoder/yuv/yuv.go b/pkg/encoder/yuv/yuv.go index 25d591d18..69bc89c8c 100644 --- a/pkg/encoder/yuv/yuv.go +++ b/pkg/encoder/yuv/yuv.go @@ -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 { @@ -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), } } @@ -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