libdav1d: optimize allocations with a sync.Pool
This commit is contained in:
parent
7d6f41da35
commit
322e4466d1
|
@ -13,6 +13,8 @@ import (
|
|||
"git.gammaspectra.live/S.O.N.G/Ignite/utilities"
|
||||
"git.gammaspectra.live/S.O.N.G/Ignite/utilities/ivfreader"
|
||||
"io"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"unsafe"
|
||||
)
|
||||
|
@ -29,6 +31,8 @@ type Decoder struct {
|
|||
ctx *C.Dav1dContext
|
||||
picture C.Dav1dPicture
|
||||
data C.Dav1dData
|
||||
|
||||
bufPool sync.Pool
|
||||
}
|
||||
|
||||
var dav1dVersion = fmt.Sprintf("dav1d %s", C.GoString(C.dav1d_version()))
|
||||
|
@ -208,12 +212,25 @@ func (d *Decoder) pictureToFrame() (frame.Frame, error) {
|
|||
|
||||
}
|
||||
|
||||
buf := make([]byte, len(yData)+len(uData)+len(vData))
|
||||
n := len(yData) + len(uData) + len(vData)
|
||||
|
||||
var buf []byte
|
||||
if b := d.bufPool.Get(); b != nil && len(b.([]byte)) == n {
|
||||
buf = b.([]byte)
|
||||
} else {
|
||||
buf = make([]byte, n)
|
||||
}
|
||||
|
||||
copy(buf, yData)
|
||||
copy(buf[len(yData):], uData)
|
||||
copy(buf[len(yData)+len(uData):], vData)
|
||||
|
||||
return frame.NewUint16FrameFromBytes(properties, int64(d.picture.m.timestamp), buf)
|
||||
if f, err := frame.NewUint16FrameFromBytes(properties, int64(d.picture.m.timestamp), buf); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
d.bufPool.Put(buf)
|
||||
return f, nil
|
||||
}
|
||||
} else {
|
||||
yData := unsafe.Slice((*byte)(d.picture.data[planeY]), properties.Height*properties.Width)
|
||||
var uData, vData []byte
|
||||
|
@ -223,12 +240,27 @@ func (d *Decoder) pictureToFrame() (frame.Frame, error) {
|
|||
vData = unsafe.Slice((*byte)(d.picture.data[planeV]), chromaHeight*chromaWidth)
|
||||
}
|
||||
|
||||
buf := make([]byte, len(yData)+len(uData)+len(vData))
|
||||
n := len(yData) + len(uData) + len(vData)
|
||||
|
||||
var buf []byte
|
||||
if b := d.bufPool.Get(); b != nil && len(b.([]byte)) == n {
|
||||
buf = b.([]byte)
|
||||
} else {
|
||||
buf = make([]byte, n)
|
||||
}
|
||||
|
||||
copy(buf, yData)
|
||||
copy(buf[len(yData):], uData)
|
||||
copy(buf[len(yData)+len(uData):], vData)
|
||||
|
||||
return frame.NewUint8FrameFromBytes(properties, int64(d.picture.m.timestamp), buf)
|
||||
if f, err := frame.NewUint8FrameFromBytes(properties, int64(d.picture.m.timestamp), buf); err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
runtime.SetFinalizer(f, func(frameUint8 *frame.FrameUint8) {
|
||||
d.bufPool.Put(buf)
|
||||
})
|
||||
return f, nil
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue