Optimize decoding slices

This commit is contained in:
DataHoarder 2022-11-10 11:45:37 +01:00
parent 6722babcac
commit 49f274e0f9
Signed by: DataHoarder
SSH key fingerprint: SHA256:OLTRf6Fl87G52SiR7sWLGNzlJt4WOX+tfI2yxo0z7xk

View file

@ -160,57 +160,35 @@ func (d *Decoder) pictureToFrame() (frame.Frame, error) {
if bitDepth > 8 { //16-bit
//TODO: check reserve
buf := make([]byte, 0, height*width*2+chromaHeight*chromaWidth*2*2)
yData := unsafe.Slice((*byte)(d.picture.data[planeY]), height*width*2)
for y := 0; y < height; y++ {
buf = append(buf, yData[:width*2]...)
yData = yData[d.picture.stride[0]:]
}
var uData, vData []byte
if d.picture.p.layout != C.DAV1D_PIXEL_LAYOUT_I400 {
uData := unsafe.Slice((*byte)(d.picture.data[planeU]), chromaHeight*chromaWidth*2)
vData := unsafe.Slice((*byte)(d.picture.data[planeV]), chromaHeight*chromaWidth*2)
uData = unsafe.Slice((*byte)(d.picture.data[planeU]), chromaHeight*chromaWidth*2)
vData = unsafe.Slice((*byte)(d.picture.data[planeV]), chromaHeight*chromaWidth*2)
//TODO: check sizes!
for y := 0; y < chromaHeight; y++ {
buf = append(buf, uData[:chromaWidth*2]...)
uData = uData[d.picture.stride[1]:]
}
for y := 0; y < chromaHeight; y++ {
buf = append(buf, vData[:chromaWidth*2]...)
vData = vData[d.picture.stride[1]:]
}
}
buf := make([]byte, len(yData)+len(uData)+len(vData))
copy(buf, yData)
copy(buf[len(yData):], uData)
copy(buf[len(yData)+len(uData):], vData)
return frame.NewUint16FrameFromBytes(colorFormat, width, height, buf)
} else {
//TODO: check reserve
buf := make([]byte, 0, height*width+chromaHeight*chromaWidth*2)
yData := unsafe.Slice((*byte)(d.picture.data[planeY]), height*width)
for y := 0; y < height; y++ {
buf = append(buf, yData[:width]...)
yData = yData[d.picture.stride[0]:]
}
var uData, vData []byte
if d.picture.p.layout != C.DAV1D_PIXEL_LAYOUT_I400 {
uData := unsafe.Slice((*byte)(d.picture.data[planeU]), chromaHeight*chromaWidth)
vData := unsafe.Slice((*byte)(d.picture.data[planeV]), chromaHeight*chromaWidth)
//TODO: check sizes!
for y := 0; y < chromaHeight; y++ {
buf = append(buf, uData[:chromaWidth]...)
uData = uData[d.picture.stride[1]:]
}
for y := 0; y < chromaHeight; y++ {
buf = append(buf, vData[:chromaWidth]...)
vData = vData[d.picture.stride[1]:]
}
uData = unsafe.Slice((*byte)(d.picture.data[planeU]), chromaHeight*chromaWidth)
vData = unsafe.Slice((*byte)(d.picture.data[planeV]), chromaHeight*chromaWidth)
}
buf := make([]byte, len(yData)+len(uData)+len(vData))
copy(buf, yData)
copy(buf[len(yData):], uData)
copy(buf[len(yData)+len(uData):], vData)
return frame.NewUint8FrameFromBytes(colorFormat, width, height, buf)
}
}