80 lines
2.1 KiB
Go
80 lines
2.1 KiB
Go
package frame
|
|
|
|
import (
|
|
"errors"
|
|
)
|
|
|
|
type FrameUint8 struct {
|
|
properties Properties
|
|
Pts int64
|
|
Y []uint8
|
|
Cb []uint8
|
|
Cr []uint8
|
|
}
|
|
|
|
func NewUint8FrameFromBytes(properties Properties, pts int64, data []byte) (*FrameUint8, error) {
|
|
if frameLength, _ := properties.ColorSpace.FrameSize(properties.Width, properties.Height); frameLength != len(data) {
|
|
return nil, errors.New("wrong length of data")
|
|
}
|
|
|
|
if properties.ColorSpace.BitDepth > 8 {
|
|
return nil, errors.New("wrong bit depth")
|
|
}
|
|
|
|
iY := properties.ColorSpace.ChromaSampling.PlaneLumaSamples(properties.Width, properties.Height)
|
|
iCb := properties.ColorSpace.ChromaSampling.PlaneCbSamples(properties.Width, properties.Height)
|
|
iCr := properties.ColorSpace.ChromaSampling.PlaneCrSamples(properties.Width, properties.Height)
|
|
|
|
return &FrameUint8{
|
|
properties: properties,
|
|
Y: data[:iY],
|
|
Cb: data[iY : iY+iCb],
|
|
Cr: data[iY+iCb : iY+iCb+iCr],
|
|
Pts: pts,
|
|
}, nil
|
|
}
|
|
|
|
func (i *FrameUint8) Get16(x, y int) (Y uint16, Cb uint16, Cr uint16) {
|
|
cy, cb, cr := i.GetNative(x, y)
|
|
|
|
return uint16(cy) << (16 - i.properties.ColorSpace.BitDepth), uint16(cb) << (16 - i.properties.ColorSpace.BitDepth), uint16(cr) << (16 - i.properties.ColorSpace.BitDepth)
|
|
}
|
|
|
|
func (i *FrameUint8) Get8(x, y int) (Y uint8, Cb uint8, Cr uint8) {
|
|
return i.GetNative(x, y)
|
|
}
|
|
|
|
func (i *FrameUint8) Properties() Properties {
|
|
return i.properties
|
|
}
|
|
|
|
func (i *FrameUint8) PTS() int64 {
|
|
return i.Pts
|
|
}
|
|
|
|
func (i *FrameUint8) GetNative(x, y int) (Y uint8, Cb uint8, Cr uint8) {
|
|
Yindex := x + y*i.properties.Width
|
|
|
|
Cwidth := (i.properties.Width * int(i.properties.ColorSpace.ChromaSampling.A)) / int(i.properties.ColorSpace.ChromaSampling.J)
|
|
if i.properties.ColorSpace.ChromaSampling.B == 0 {
|
|
y /= 2
|
|
}
|
|
Cindex := (x*int(i.properties.ColorSpace.ChromaSampling.A))/int(i.properties.ColorSpace.ChromaSampling.J) + y*Cwidth
|
|
Y = i.Y[Yindex]
|
|
Cb = i.Cb[Cindex]
|
|
Cr = i.Cr[Cindex]
|
|
return
|
|
}
|
|
|
|
func (i *FrameUint8) GetNativeLuma() []uint8 {
|
|
return i.Y
|
|
}
|
|
|
|
func (i *FrameUint8) GetNativeCb() []uint8 {
|
|
return i.Cb
|
|
}
|
|
|
|
func (i *FrameUint8) GetNativeCr() []uint8 {
|
|
return i.Cr
|
|
}
|