Add comments clarifying AVBufferRef usage and ownership

This commit is contained in:
Cameron Gutman 2023-10-04 19:45:35 -05:00
parent dcb719ee5f
commit a08e2b3de6

View File

@ -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;