Skip to content

Commit

Permalink
Remove Encoder.LoadBuf interface method
Browse files Browse the repository at this point in the history
There is no point in keeping it only for early YUV image pooling.
  • Loading branch information
sergystepanov committed Feb 12, 2024
1 parent b903700 commit e2226e7
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 20 deletions.
9 changes: 3 additions & 6 deletions pkg/encoder/encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ type (
InFrame yuv.RawFrame
OutFrame []byte
Encoder interface {
LoadBuf(input []byte)
Encode() []byte
Encode([]byte) []byte
IntraRefresh()
Info() string
SetFlip(bool)
Expand Down Expand Up @@ -75,10 +74,8 @@ func (v *Video) Encode(frame InFrame) OutFrame {
}

yCbCr := v.y.Process(yuv.RawFrame(frame), v.rot, v.pf)
v.codec.LoadBuf(yCbCr)
v.y.Put(&yCbCr)

if bytes := v.codec.Encode(); len(bytes) > 0 {
defer v.y.Put(&yCbCr)
if bytes := v.codec.Encode(yCbCr); len(bytes) > 0 {
return bytes
}
return nil
Expand Down
4 changes: 1 addition & 3 deletions pkg/encoder/h264/x264.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,13 +122,11 @@ func NewEncoder(w, h int, th int, opts *Options) (encoder *H264, err error) {
return
}

func (e *H264) LoadBuf(yuv []byte) {
func (e *H264) Encode(yuv []byte) []byte {
e.in.img.plane[0] = (*C.uchar)(unsafe.Pointer(&yuv[0]))
e.in.img.plane[1] = (*C.uchar)(unsafe.Pointer(&yuv[e.y]))
e.in.img.plane[2] = (*C.uchar)(unsafe.Pointer(&yuv[e.y+e.uv]))
}

func (e *H264) Encode() []byte {
e.in.i_pts += 1

e.p.Pin(e.in.img.plane[0])
Expand Down
6 changes: 2 additions & 4 deletions pkg/encoder/h264/x264_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ func TestH264Encode(t *testing.T) {
return
}
data := make([]byte, 120*120*1.5)
h264.LoadBuf(data)
h264.Encode()
h264.Encode(data)
if err := h264.Shutdown(); err != nil {
t.Error(err)
}
Expand All @@ -25,7 +24,6 @@ func Benchmark(b *testing.B) {
}
data := make([]byte, int(float64(w)*float64(h)*1.5))
for i := 0; i < b.N; i++ {
h264.LoadBuf(data)
h264.Encode()
h264.Encode(data)
}
}
11 changes: 4 additions & 7 deletions pkg/encoder/vpx/libvpx.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,13 @@ func NewEncoder(w, h int, opts *Options) (*Vpx, error) {
return &vpx, nil
}

func (vpx *Vpx) LoadBuf(yuv []byte) {
// Encode encodes yuv image with the VPX8 encoder.
// see: https://chromium.googlesource.com/webm/libvpx/+/master/examples/simple_encoder.c
func (vpx *Vpx) Encode(yuv []byte) []byte {
C.vpx_img_read(&vpx.image, unsafe.Pointer(&yuv[0]))
if vpx.flipped {
C.vpx_img_flip(&vpx.image)
}
}

// Encode encodes yuv image with the VPX8 encoder.
// see: https://chromium.googlesource.com/webm/libvpx/+/master/examples/simple_encoder.c
func (vpx *Vpx) Encode() []byte {
var iter C.vpx_codec_iter_t

var flags C.int
if vpx.kfi > 0 && vpx.frameCount%vpx.kfi == 0 {
Expand All @@ -156,6 +152,7 @@ func (vpx *Vpx) Encode() []byte {
}
vpx.frameCount++

var iter C.vpx_codec_iter_t
fb := C.get_frame_buffer(&vpx.codecCtx, &iter)
if fb.ptr == nil {
return []byte{}
Expand Down

0 comments on commit e2226e7

Please sign in to comment.