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 ESMTPS id 874C74D88D for ; Tue, 25 Feb 2025 23:15:36 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id E570368CDCE; Wed, 26 Feb 2025 01:15:31 +0200 (EET) Received: from btbn.de (btbn.de [144.76.60.213]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 8CB6C68CADF for ; Wed, 26 Feb 2025 01:15:24 +0200 (EET) Received: from [authenticated] by btbn.de (Postfix) with ESMTPSA id F03E12819FD57; Wed, 26 Feb 2025 00:15:23 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rothenpieler.org; s=mail; t=1740525324; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=W3iQZgmJ1HtlvAhnWYmqaY+GHafd3e2w5adu3EL4t6Y=; b=n+MwfxWRBLIJfat+JI44l+HR9XJyNnP9UsC+eBMLU1AwQuR2uoGOu0CnLD2dhNEDdkAP6H /Q4f2ppVriJxKUvq8MTyLK29Ya90FXPOy6DDpCpK71H1EOokFM6IMewIWwX2s+llI23/D4 3JRbGQvEamBCN8gLHzUdN1jA3T1RTJCVgNMjOHPGhtZ9Wyl2iwTAAU4rjglZqouUHcpH/l UiCgk8fzAtX1b0Cw8PfqStJNKpwEoBt+uFLfLBIVQeIt5gXvQwoNB6V2SKe9tQRjjNMwtY 4qMe0Y2GDoJC1dCguBVUsuxTk9/ZNDWcyLBEvpAmluZeYfYbi+CgpbA7K/xK1w== Message-ID: <4bbe5cf1-0e7b-4459-b965-031c4f203eb1@rothenpieler.org> Date: Wed, 26 Feb 2025 00:15:23 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: ffmpeg-devel@ffmpeg.org, Scott Theisen References: <3bfc5f14-5661-4d30-8488-3138464fbd6e@gmail.com> <20250225184349.14058-1-timo@rothenpieler.org> Content-Language: en-US From: Timo Rothenpieler In-Reply-To: <20250225184349.14058-1-timo@rothenpieler.org> Subject: Re: [FFmpeg-devel] [PATCH] avcodec/cuviddec: correctly handle buffer size and status when deinterlacing 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 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="us-ascii"; Format="flowed" Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: On 25.02.2025 19:43, Timo Rothenpieler wrote: > --- > libavcodec/cuviddec.c | 24 +++++++++++++----------- > 1 file changed, 13 insertions(+), 11 deletions(-) > > diff --git a/libavcodec/cuviddec.c b/libavcodec/cuviddec.c > index 67076a1752..312742fb8c 100644 > --- a/libavcodec/cuviddec.c > +++ b/libavcodec/cuviddec.c > @@ -131,7 +131,7 @@ static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* form > CUVIDDECODECREATEINFO cuinfo; > int surface_fmt; > int chroma_444; > - int fifo_size_inc; > + int old_nb_surfaces, fifo_size_inc, fifo_size_mul = 1; > > int old_width = avctx->width; > int old_height = avctx->height; > @@ -349,20 +349,24 @@ static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* form > return 0; > } > > - fifo_size_inc = ctx->nb_surfaces; > - ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3); > + if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) { > + avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1}); > + fifo_size_mul = 2; > + } > > + old_nb_surfaces = ctx->nb_surfaces; > + ctx->nb_surfaces = FFMAX(ctx->nb_surfaces, format->min_num_decode_surfaces + 3); > if (avctx->extra_hw_frames > 0) > ctx->nb_surfaces += avctx->extra_hw_frames; > > - fifo_size_inc = ctx->nb_surfaces - fifo_size_inc; > + fifo_size_inc = ctx->nb_surfaces * fifo_size_mul - av_fifo_can_read(ctx->frame_queue) - av_fifo_can_write(ctx->frame_queue); > if (fifo_size_inc > 0 && av_fifo_grow2(ctx->frame_queue, fifo_size_inc) < 0) { > av_log(avctx, AV_LOG_ERROR, "Failed to grow frame queue on video sequence callback\n"); > ctx->internal_error = AVERROR(ENOMEM); > return 0; > } > > - if (fifo_size_inc > 0 && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) { > + if (ctx->nb_surfaces > old_nb_surfaces && av_reallocp_array(&ctx->key_frame, ctx->nb_surfaces, sizeof(int)) < 0) { > av_log(avctx, AV_LOG_ERROR, "Failed to grow key frame array on video sequence callback\n"); > ctx->internal_error = AVERROR(ENOMEM); > return 0; > @@ -374,9 +378,6 @@ static int CUDAAPI cuvid_handle_video_sequence(void *opaque, CUVIDEOFORMAT* form > cuinfo.bitDepthMinus8 = format->bit_depth_luma_minus8; > cuinfo.DeinterlaceMode = ctx->deint_mode_current; > > - if (ctx->deint_mode_current != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) > - avctx->framerate = av_mul_q(avctx->framerate, (AVRational){2, 1}); > - > ctx->internal_error = CHECK_CU(ctx->cvdl->cuvidCreateDecoder(&ctx->cudecoder, &cuinfo)); > if (ctx->internal_error < 0) > return 0; > @@ -448,11 +449,12 @@ static int cuvid_is_buffer_full(AVCodecContext *avctx) > { > CuvidContext *ctx = avctx->priv_data; > > - int delay = ctx->cuparseinfo.ulMaxDisplayDelay; > + int mult = 1; > if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field) > - delay *= 2; > + mult = 2; > > - return av_fifo_can_read(ctx->frame_queue) + delay >= ctx->nb_surfaces; > + // "- mult + 1" ensures that the buffer is still signalled full if one half-frame has already been returned when deinterlacing. > + return av_fifo_can_read(ctx->frame_queue) + (ctx->cuparseinfo.ulMaxDisplayDelay * mult) >= ctx->nb_surfaces * mult - mult + 1; > } > > static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt) This fixes decoding+deinterlacing with the ffmpeg CLI for me. Can you verify if it also fixes it in your case? _______________________________________________ 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".