Remove pools from YUV conv

This commit is contained in:
Sergey Stepanov 2024-12-05 01:11:02 +03:00
parent db32479c4e
commit 5649d4410a
No known key found for this signature in database
GPG key ID: A56B4929BAA8556B

View file

@ -2,7 +2,6 @@ package yuv
import (
"image"
"sync"
"github.com/giongto35/cloud-game/v3/pkg/encoder/yuv/libyuv"
)
@ -11,7 +10,6 @@ type Conv struct {
w, h int
sw, sh int
scale float64
pool sync.Pool
frame []byte
frameSc []byte
}
@ -33,42 +31,49 @@ func NewYuvConv(w, h int, scale float64) Conv {
if scale < 1 {
scale = 1
}
sw, sh := round(w, scale), round(h, scale)
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 }},
frame: make([]byte, bufSize),
frameSc: make([]byte, bufSize),
conv := Conv{w: w, h: h, sw: sw, sh: sh, scale: scale}
bufSize := int(float64(w) * float64(h) * 1.5)
if scale == 1 {
conv.frame = make([]byte, bufSize)
} else {
bufSizeSc := int(float64(sw) * float64(sh) * 1.5)
// [original frame][scaled frame ]
frames := make([]byte, bufSize+bufSizeSc)
conv.frame = frames[:bufSize]
conv.frameSc = frames[bufSize:]
}
return conv
}
// Process converts an image to YUV I420 format inside the internal buffer.
func (c *Conv) Process(frame RawFrame, rot uint, pf PixFmt) []byte {
dx, dy := c.w, c.h // dest
cx, cy := c.w, c.h // crop
if rot == 90 || rot == 270 {
cx, cy = cy, cx
}
stride := frame.Stride >> 2
if pf == PixFmt(libyuv.FourccRgbp) || pf == PixFmt(libyuv.FourccRgb0) {
var stride int
switch pf {
case PixFmt(libyuv.FourccRgbp), PixFmt(libyuv.FourccRgb0):
stride = frame.Stride >> 1
default:
stride = frame.Stride >> 2
}
buf := c.frame //*c.pool.Get().(*[]byte)
libyuv.Y420(frame.Data, buf, frame.W, frame.H, stride, dx, dy, rot, uint32(pf), cx, cy)
libyuv.Y420(frame.Data, c.frame, frame.W, frame.H, stride, c.w, c.h, rot, uint32(pf), cx, cy)
if c.scale > 1 {
dstBuf := c.frameSc //*c.pool.Get().(*[]byte)
libyuv.Y420Scale(buf, dstBuf, dx, dy, c.sw, c.sh)
//c.pool.Put(&buf)
return dstBuf
libyuv.Y420Scale(c.frame, c.frameSc, c.w, c.h, c.sw, c.sh)
return c.frameSc
}
return buf
return c.frame
}
func (c *Conv) Put(x *[]byte) { c.pool.Put(x) }
func (c *Conv) Version() string { return libyuv.Version() }
func round(x int, scale float64) int { return (int(float64(x)*scale) + 1) & ^1 }