diff --git a/src/platform/macos/nv12_zero_device.cpp b/src/platform/macos/nv12_zero_device.cpp index 881c86aa..f376bdcd 100644 --- a/src/platform/macos/nv12_zero_device.cpp +++ b/src/platform/macos/nv12_zero_device.cpp @@ -26,9 +26,17 @@ namespace platf { nv12_zero_device::convert(platf::img_t &img) { av_img_t *av_img = (av_img_t *) &img; + // Release any existing CVPixelBuffer previously retained for encoding av_buffer_unref(&av_frame->buf[0]); + // Attach an AVBufferRef to this frame which will retain ownership of the CVPixelBuffer + // until av_buffer_unref() is called (above) or the frame is freed with av_frame_free(). + // + // The presence of the AVBufferRef allows FFmpeg to simply add a reference to the buffer + // rather than having to perform a deep copy of the data buffers in avcodec_send_frame(). av_frame->buf[0] = av_buffer_create((uint8_t *) CFRetain(av_img->pixel_buffer->buf), 0, free_buffer, NULL, 0); + + // Place a CVPixelBufferRef at data[3] as required by AV_PIX_FMT_VIDEOTOOLBOX av_frame->data[3] = (uint8_t *) av_img->pixel_buffer->buf; return 0;