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 7EFF14D8A1 for ; Wed, 26 Feb 2025 02:42:27 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id ED4F468CAA5; Wed, 26 Feb 2025 04:42:23 +0200 (EET) Received: from mail-qv1-f53.google.com (mail-qv1-f53.google.com [209.85.219.53]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 2D97968C2C4 for ; Wed, 26 Feb 2025 04:42:17 +0200 (EET) Received: by mail-qv1-f53.google.com with SMTP id 6a1803df08f44-6e65a00556aso50760806d6.3 for ; Tue, 25 Feb 2025 18:42:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1740537736; x=1741142536; darn=ffmpeg.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id:from :to:cc:subject:date:message-id:reply-to; bh=msYgZPFTITWVwrX7vKDS2tuHSqeZsefIsaIs2RvKv6A=; b=RRNtw7KQMnItC+Go9DlXl+/RMxjLWBrbej0KwwYFhXFdlmQ//doqRa1x8TIH+UHKkT nr+x1Wgug4ZOOkzZK6PHyMlDh1IjHtgA+skqjCI+LL8wxL8J7ttFuh0FZHy6kTpRDa1C Strv/8tcaevQdjAYFzGdfpd4cfYLKM1UQmA+8KvKN3ENar5itPzYT+U4P7lBIQwibbRv 8moZgarymRe3QXhGl5CuI2/9dFyAh5i6pMTYt6NOVWa3jRxknZLwAWx7J917u3QkjMF1 9q+VSSDRSlcnzNRFrJ0rKPf3BfnqUyj54vRhMwr5kxlEY0wWhcYaA7p4thiBrtZwmYKW cYBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1740537736; x=1741142536; h=content-transfer-encoding:in-reply-to:from:content-language :references:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=msYgZPFTITWVwrX7vKDS2tuHSqeZsefIsaIs2RvKv6A=; b=gRWa2q8IN9jXV6bQPs+P5XeDIw5gyFQGEdavHUPwuyp/lPPLZUXAoaUnVmOdHfYKQy b3hjE3GaQGhtWuHcBIYiw4WBUvHvsfyiOLMsuri9CKSqY8zDGKmWSZbg+2DKD5QtusfU h8zaIrv+4eDLl9F+tM9zkCoE2SFN6ThoM6JnUxxle0Ca7Pn5rD4kFMFHRAAlty6o7d/m Q2OPwqVPHzHugBf7fhQo3SzqlLNdLjsyC9FulcSP7Worq2WQiuhNBLtQTzg+Dk7eWT5W ZX13Aw0PL9zx6nicQ2bVRjIGyaWYopxUVekbiNWUJoSjzG3BLpeDvuo3thD78vIumWvs 8orw== X-Gm-Message-State: AOJu0YxS4xbnjFjF8hwDPYI0oedVzmINVzA98dhSpczk0GHv0jGFVl2k XQEsl4pNyRmJtR8xU52Sbn0nwE/qF1hFGj4nO0ZMJzTAW0t1WTWX4rg6rA== X-Gm-Gg: ASbGncu46KDB9FbIjRqaB+eWS0lAzDR/Mz4Zir/fMfB5FRUGu0lBEgcnZl7DCTQt08p m5pYYjk58ZYtC7b1rlDTDqViSRFXCDQlIgFQbnsYNnKmr72Z8LbwT1r17hLxr6D/iK4vvj/OXzP hkeDDFe7ht8bXfG5UujTqt+CwB+yRSZDdNF7Y4GKGHBm5MJKTddQuSjKIBJD7vE1Qq1g5FelOcg 9+VoBFAruy3CH1BEocYGGtENdMx+WdWiJaHJ6zOXeYDwgymKWcLpE2+ngraZ3pTmu/9f1MZWSI4 X5ZDCbWaB8wv6dSn/ovAqDxyrarEzqj0Et0MQ3Zju9Lwvo72Pqa7NTmGyGLfArWg6mAV0tlWrv8 18KnAqw== X-Google-Smtp-Source: AGHT+IGFuZjJ4FTT3unPVBZw73/iAQYgvDxYABuYWYr4OkGIMG9cM/M35RjK0NkDdWjHlmjC2+ydRA== X-Received: by 2002:a05:6214:410:b0:6e4:3c52:d67e with SMTP id 6a1803df08f44-6e87ab47032mr77136416d6.18.1740537735689; Tue, 25 Feb 2025 18:42:15 -0800 (PST) Received: from [192.168.1.19] (pool-71-114-78-195.washdc.fios.verizon.net. [71.114.78.195]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-6e87b089bbfsm16975896d6.50.2025.02.25.18.42.13 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 25 Feb 2025 18:42:14 -0800 (PST) Message-ID: Date: Tue, 25 Feb 2025 21:42:13 -0500 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird To: ffmpeg-devel@ffmpeg.org References: <3bfc5f14-5661-4d30-8488-3138464fbd6e@gmail.com> <20250225184349.14058-1-timo@rothenpieler.org> Content-Language: en-US From: Scott Theisen 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 2/25/25 13: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; I think this is clearer: return ((av_fifo_can_read(ctx->frame_queue) + mult - 1) / mult) + ctx->cuparseinfo.ulMaxDisplayDelay >= ctx->nb_surfaces Integer ceiling division to get the number of referenced surfaces in frame_queue. However, when going from mult = 2 to 1, it thinks the buffer is more full than it really is, which probably isn't a problem. Unfortunately, when going from mult = 1 to 2, if there is more than one frame in frame_queue, it will think there are less surfaces referenced than there are, which may be a problem. To avoid that, on transitions you would have to drain frame_queue until it is empty or peek at what is in frame_queue to actually count the number of referenced surfaces. > } > > static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt) I'll apply this to MythTV for testing. _______________________________________________ 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".