diff --git a/pkg/encoder/encoder.go b/pkg/encoder/encoder.go index 5378e47c8..b8ef829ae 100644 --- a/pkg/encoder/encoder.go +++ b/pkg/encoder/encoder.go @@ -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) @@ -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 diff --git a/pkg/encoder/h264/x264.go b/pkg/encoder/h264/x264.go index 6ca1f3aac..8e75f3ef2 100644 --- a/pkg/encoder/h264/x264.go +++ b/pkg/encoder/h264/x264.go @@ -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]) diff --git a/pkg/encoder/h264/x264_test.go b/pkg/encoder/h264/x264_test.go index 09f22ae32..0fe1bb43e 100644 --- a/pkg/encoder/h264/x264_test.go +++ b/pkg/encoder/h264/x264_test.go @@ -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) } @@ -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) } } diff --git a/pkg/encoder/vpx/libvpx.go b/pkg/encoder/vpx/libvpx.go index 81988b9c6..8de6ff409 100644 --- a/pkg/encoder/vpx/libvpx.go +++ b/pkg/encoder/vpx/libvpx.go @@ -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 { @@ -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{}