From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id BDEF7411D3 for ; Sun, 7 Jul 2024 10:22:16 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 30ABE68DADB; Sun, 7 Jul 2024 13:22:01 +0300 (EEST) Received: from out162-62-58-216.mail.qq.com (out162-62-58-216.mail.qq.com [162.62.58.216]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 37E5F68DA5C for ; Sun, 7 Jul 2024 13:21:45 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foxmail.com; s=s201512; t=1720347697; bh=J+H6R9iG5ROOJS/kL0K56cUnaOs3Pmi00FtqlA5Hu6k=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=qk5ueFY4aNpOhqUxfBude+c7ZdQW7I2CC6gLD8lJNhW4Qgfewj6Mm7jRAB03vGUTb c8DhQVfS/OIV3/E+P0VpkvpRH8STCYEgylodHtIgud4TuH/BQdzpaFiMGkMIO5otm4 7hKcARsoe9gMVgh52ApEW+PecC+rOz63p8Cxx2YM= Received: from ZHILIZHAO-MB1.tencent.com ([113.116.28.29]) by newxmesmtplogicsvrsza10-0.qq.com (NewEsmtp) with SMTP id 5633A86C; Sun, 07 Jul 2024 18:21:35 +0800 X-QQ-mid: xmsmtpt1720347696tdirvyhvk Message-ID: X-QQ-XMAILINFO: NvKyM24IHTKS1fop19uWb4kYzGGXwNsEg0SyhhV5xwJuDI7Lks0hoIiLkxfN8c NhLB/eWts+jDMdysZUuzcAdsHgZ2Fcnd9dJbn95LW7jP34Lnsc7XspGFHxco1zP7qeKxtcXpfRC2 syAzReEBiK5K9h7qAdnidKapYzuwTxRpairKlqNqce1CAfOqAUy3vaTF5p5GtQaxcCytCk8kBtPL 3uhT8qzc1BLOZS5KuK2Aek1iE8zr5ARp0eI2RqGfF0dCjq/rqa53HLkPeVfpZyvWHkM8Lggt77yw PZGHoxZJ7HemZsOHI/WuW3Qj/bGmKzQn/+y9c2iV21IaHPkqt7sBq8LVevx1n9gmOa5t+nviQmUj JIcL85IeskaqtzDW0AfI8J35+2AH/d5+V7H++VWD2fyD3czU+VtNHc8cK1YXfYAazR7+n2PyWck/ 2n0QshguP0uQxTBXmbSomHUFjm5tOqJVoAGVAvzqYo8Asla0IbAk4mUYmNd4r7XRepCvilOAe5p4 jr2cexFHyRgi+7J/0lJZcicg5XWC5649fH7eEuy082S3EqjSE73/u836hVq3sHaYT2SX+z3rdlH8 dbVLLyD9heSJ2uRgjDRToUbJliYUEFirfW+B33ZNOF6gAPYOaW7Itk2SHS8ACYBQE24Z4eEIRD3s 6MWs3ujqSJHN5j28EZYuNa7BTFVIjD0T0EbhzGYxJNgml45GlfPZYRurljPS058cnWfrHdnJUp22 bJ826ZMOGZTB+JanKlpQ4OTf4LfysojYg5Pr+lAnfy80ahvU+sxQo8Sv4qFJGD/RGCW7IWyDQTeU O48Fj6rKO5C3RjwxC1+l9tWDY8vBmfUqVKnNmKicBPxBEpYgb3sn2QNIZojvNt16SQSH9S6Sqkoj eypQ5YBGIS+9VyNn0xFODAlLDQhSE8zJMkFdHdLAeusMHigwOcABUmnAv6MEmPMUOxh+rUZ0SOwu FrmCUzzf16SrraaLaLHgslIUxlLx6ASDfPB+BH7KY+OkW1Sl4dd+EX4nxsJCotVwDY5rh9A4g= X-QQ-XMRINFO: Nq+8W0+stu50PRdwbJxPCL0= From: Zhao Zhili To: ffmpeg-devel@ffmpeg.org Date: Sun, 7 Jul 2024 18:21:32 +0800 X-OQ-MSGID: <20240707102134.93935-3-quinkblack@foxmail.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20240707102134.93935-1-quinkblack@foxmail.com> References: <20240707102134.93935-1-quinkblack@foxmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 3/5] avcodec/videotoolboxenc: Use BufNode as sourceFrameRefcon X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Zhao Zhili Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: From: Zhao Zhili ExtraSEI is used as the sourceFrameRefcon of VTCompressionSessionEncodeFrame. It cannot hold other information which is necessary to fix another issue in the following patch. This patch also fixed leak of ExtraSEI on the error path of vtenc_output_callback. --- libavcodec/videotoolboxenc.c | 77 ++++++++++++++++++++++++------------ 1 file changed, 51 insertions(+), 26 deletions(-) diff --git a/libavcodec/videotoolboxenc.c b/libavcodec/videotoolboxenc.c index 03e3c9ad42..213bfa8b49 100644 --- a/libavcodec/videotoolboxenc.c +++ b/libavcodec/videotoolboxenc.c @@ -347,8 +347,7 @@ static void set_async_error(VTEncContext *vtctx, int err) while (info) { BufNode *next = info->next; - CFRelease(info->cm_buffer); - av_free(info); + vtenc_free_buf_node(info); info = next; } @@ -438,19 +437,8 @@ static int vtenc_q_pop(VTEncContext *vtctx, bool wait, CMSampleBufferRef *buf, E return 0; } -static void vtenc_q_push(VTEncContext *vtctx, CMSampleBufferRef buffer, ExtraSEI *sei) +static void vtenc_q_push(VTEncContext *vtctx, BufNode *info) { - BufNode *info = av_malloc(sizeof(BufNode)); - if (!info) { - set_async_error(vtctx, AVERROR(ENOMEM)); - return; - } - - CFRetain(buffer); - info->cm_buffer = buffer; - info->sei = sei; - info->next = NULL; - pthread_mutex_lock(&vtctx->lock); if (!vtctx->q_head) { @@ -726,6 +714,22 @@ static int set_extradata(AVCodecContext *avctx, CMSampleBufferRef sample_buffer) return 0; } +static void vtenc_free_buf_node(BufNode *info) +{ + if (!info) + return; + + if (info->sei) { + av_free(info->sei->data); + av_free(info->sei); + } + + if (info->cm_buffer) + CFRelease(info->cm_buffer); + + av_free(info); +} + static void vtenc_output_callback( void *ctx, void *sourceFrameCtx, @@ -735,13 +739,15 @@ static void vtenc_output_callback( { AVCodecContext *avctx = ctx; VTEncContext *vtctx = avctx->priv_data; - ExtraSEI *sei = sourceFrameCtx; + BufNode *info = sourceFrameCtx; if (vtctx->async_error) { + vtenc_free_buf_node(info); return; } if (status) { + vtenc_free_buf_node(info); av_log(avctx, AV_LOG_ERROR, "Error encoding frame: %d\n", (int)status); set_async_error(vtctx, AVERROR_EXTERNAL); return; @@ -751,15 +757,19 @@ static void vtenc_output_callback( return; } + CFRetain(sample_buffer); + info->cm_buffer = sample_buffer; + if (!avctx->extradata && (avctx->flags & AV_CODEC_FLAG_GLOBAL_HEADER)) { int set_status = set_extradata(avctx, sample_buffer); if (set_status) { + vtenc_free_buf_node(info); set_async_error(vtctx, set_status); return; } } - vtenc_q_push(vtctx, sample_buffer, sei); + vtenc_q_push(vtctx, info); } static int get_length_code_size( @@ -2565,19 +2575,23 @@ static int vtenc_send_frame(AVCodecContext *avctx, const AVFrame *frame) { CMTime time; - CFDictionaryRef frame_dict; + CFDictionaryRef frame_dict = NULL; CVPixelBufferRef cv_img = NULL; AVFrameSideData *side_data = NULL; + BufNode *node = av_mallocz(sizeof(*node)); ExtraSEI *sei = NULL; - int status = create_cv_pixel_buffer(avctx, frame, &cv_img); + int status; - if (status) return status; + if (!node) + return AVERROR(ENOMEM); + + status = create_cv_pixel_buffer(avctx, frame, &cv_img); + if (status) + goto out; status = create_encoder_dict_h264(frame, &frame_dict); - if (status) { - CFRelease(cv_img); - return status; - } + if (status) + goto out; #if CONFIG_ATSC_A53 side_data = av_frame_get_side_data(frame, AV_FRAME_DATA_A53_CC); @@ -2587,6 +2601,7 @@ static int vtenc_send_frame(AVCodecContext *avctx, status = AVERROR(ENOMEM); goto out; } + node->sei = sei; status = ff_alloc_a53_sei(frame, 0, &sei->data, &sei->size); if (status < 0) { av_free(sei); @@ -2602,7 +2617,7 @@ static int vtenc_send_frame(AVCodecContext *avctx, time, kCMTimeInvalid, frame_dict, - sei, + node, NULL ); @@ -2616,7 +2631,10 @@ static int vtenc_send_frame(AVCodecContext *avctx, out: if (frame_dict) CFRelease(frame_dict); - CFRelease(cv_img); + if (cv_img) + CFRelease(cv_img); + if (status) + vtenc_free_buf_node(node); return status; } @@ -2701,6 +2719,10 @@ static int vtenc_populate_extradata(AVCodecContext *avctx, CVPixelBufferRef pix_buf = NULL; CMTime time; CMSampleBufferRef buf = NULL; + BufNode *node = av_mallocz(sizeof(*node)); + + if (!node) + return AVERROR(ENOMEM); status = vtenc_create_encoder(avctx, codec_type, @@ -2736,7 +2758,7 @@ static int vtenc_populate_extradata(AVCodecContext *avctx, time, kCMTimeInvalid, NULL, - NULL, + node, NULL); if (status) { @@ -2747,6 +2769,7 @@ static int vtenc_populate_extradata(AVCodecContext *avctx, status = AVERROR_EXTERNAL; goto pe_cleanup; } + node = NULL; //Populates extradata - output frames are flushed and param sets are available. status = VTCompressionSessionCompleteFrames(vtctx->session, @@ -2773,6 +2796,8 @@ pe_cleanup: vtctx->frame_ct_out = 0; av_assert0(status != 0 || (avctx->extradata && avctx->extradata_size > 0)); + if (!status) + vtenc_free_buf_node(node); return status; } -- 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".