From: Zhao Zhili <quinkblack@foxmail.com> To: ffmpeg-devel@ffmpeg.org Cc: Zhao Zhili <zhilizhao@tencent.com> Subject: [FFmpeg-devel] [PATCH 4/5] avcodec/videotoolboxenc: Fix concurrent access to CVPixelBufferRef Date: Sun, 7 Jul 2024 18:21:33 +0800 Message-ID: <tencent_D6931D5C700BBB661750A3A6E727FEEA8006@qq.com> (raw) In-Reply-To: <20240707102134.93935-1-quinkblack@foxmail.com> From: Zhao Zhili <zhilizhao@tencent.com> For a frame comes from AV_HWDEVICE_TYPE_VIDEOTOOLBOX, it's CVPixelBufferRef is maintained by a pool. CVPixelBufferRef returned to the pool when frame buffer reference reached to zero. However, VTCompressionSessionEncodeFrame also hold a reference to the CVPixelBufferRef. So a new frame get from av_hwframe_get_buffer may access a CVPixelBufferRef which still used by the encoder. It's only after vtenc_output_callback that we can make sure CVPixelBufferRef has been released by the encoder. The issue can be tested with sample from trac #10884. ffmpeg -hwaccel videotoolbox \ -hwaccel_output_format videotoolbox_vld \ -i input.mp4 \ -c:v hevc_videotoolbox \ -profile:v main \ -b:v 3M \ -vf scale_vt=w=iw/2:h=ih/2:color_matrix=bt709:color_primaries=bt709:color_transfer=bt709 \ -c:a copy \ -tag:v hvc1 \ output.mp4 Withtout the patch, there are some out of order images in output.mp4. --- libavcodec/videotoolboxenc.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 213bfa8b49..9bb9b0b457 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -228,6 +228,7 @@ typedef struct ExtraSEI { typedef struct BufNode { CMSampleBufferRef cm_buffer; ExtraSEI *sei; + AVBufferRef *frame_buf; struct BufNode* next; } BufNode; @@ -727,6 +728,7 @@ static void vtenc_free_buf_node(BufNode *info) if (info->cm_buffer) CFRelease(info->cm_buffer); + av_buffer_unref(&info->frame_buf); av_free(info); } @@ -741,6 +743,7 @@ static void vtenc_output_callback( VTEncContext *vtctx = avctx->priv_data; BufNode *info = sourceFrameCtx; + av_buffer_unref(&info->frame_buf); if (vtctx->async_error) { vtenc_free_buf_node(info); return; @@ -2459,7 +2462,8 @@ static int copy_avframe_to_pixel_buffer(AVCodecContext *avctx, static int create_cv_pixel_buffer(AVCodecContext *avctx, const AVFrame *frame, - CVPixelBufferRef *cv_img) + CVPixelBufferRef *cv_img, + BufNode *node) { int plane_count; int color; @@ -2478,6 +2482,12 @@ static int create_cv_pixel_buffer(AVCodecContext *avctx, av_assert0(*cv_img); CFRetain(*cv_img); + if (frame->buf[0]) { + node->frame_buf = av_buffer_ref(frame->buf[0]); + if (!node->frame_buf) + return AVERROR(ENOMEM); + } + return 0; } @@ -2585,7 +2595,7 @@ static int vtenc_send_frame(AVCodecContext *avctx, if (!node) return AVERROR(ENOMEM); - status = create_cv_pixel_buffer(avctx, frame, &cv_img); + status = create_cv_pixel_buffer(avctx, frame, &cv_img, node); if (status) goto out; -- 2.42.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2024-07-07 10:21 UTC|newest] Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top [not found] <20240707102134.93935-1-quinkblack@foxmail.com> 2024-07-07 10:21 ` [FFmpeg-devel] [PATCH 2/5] avcodec/videotoolboxenc: Remove unused variable Zhao Zhili 2024-07-07 10:21 ` [FFmpeg-devel] [PATCH 3/5] avcodec/videotoolboxenc: Use BufNode as sourceFrameRefcon Zhao Zhili 2024-07-07 10:21 ` Zhao Zhili [this message] 2024-07-07 10:21 ` [FFmpeg-devel] [PATCH 5/5] avcodec/videotoolboxenc: Put ExtraSEI inside BufNode directly Zhao Zhili
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=tencent_D6931D5C700BBB661750A3A6E727FEEA8006@qq.com \ --to=quinkblack@foxmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ --cc=zhilizhao@tencent.com \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel This inbox may be cloned and mirrored by anyone: git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git # If you have public-inbox 1.1+ installed, you may # initialize and index your mirror using the following commands: public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \ ffmpegdev@gitmailbox.com public-inbox-index ffmpegdev Example config snippet for mirrors. AGPL code for this site: git clone https://public-inbox.org/public-inbox.git