PR #25866 opened by Marcos Ashton (MarcosAsh) URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25866 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/25866.patch
NVDEC scales every mapped picture to the target size, which nvdec sets to the coded size when it creates the decoder. av1dec keeps the hwaccel for frames below the sequence maximum, so with `-hwaccel cuda` frames 10-19 of av1-1-b8-03-sizedown come out scaled up on master. The last commit reconfigures the target to the picture size before mapping. NVIDIA only documents cuvidReconfigureDecoder for the parser's sequence callback, and FFmpeg hasn't used it before. In my testing, nothing may be decoded between a target change and the next unmap, so the reconfigured picture is copied even with unsafe_output. On master only AV1 reaches this, and av1dec has no frame threads. Recreating the decoder the way cuviddec does would drop the references the smaller frames predict from. Pictures larger than the decoder or below the NVDEC minimum behave as before. The first two commits fix odd sizes, which the last one relies on. Tested on an RTX 5070 Ti (driver 615.78.08) against software decoding, single-threaded and three times with frame threads, with and without unsafe_output: the 226 VP9 vectors in fate-suite, the 24 libvpx resize_inter vectors, av1-1-b8-03-sizedown and sizeup, and 14 generated streams with odd sizes, repeated resizes and frames below the NVDEC minimum. av1-1-b8-03-sizedown and a 282x173 VP9 stream are now bit-exact, a generated VP9 resize stream goes from 9 wrong frames to 3, and nothing else changes. Frames held mapped with unsafe_output keep their content across a reconfigure; I downloaded them again after the whole stream had decoded. Decoding speed at 1080p is the same. The CUARRAY output path is unchanged and I couldn't build it (no SDK 13 headers). With #25360, VP9 could keep NVDEC on non-keyframe size changes too. That needs this series plus a lock, because vp9 calls the hwaccel before ff_thread_finish_setup(), so I'll send it once #25360 is in. >From 9a4d502c4cc98c5ec0fa112423bff176f48830b1 Mon Sep 17 00:00:00 2001 From: marcos ashton <[email protected]> Date: Sun, 8 Nov 2026 15:54:56 +0000 Subject: [PATCH 1/3] avcodec/nvdec: keep the picture size when copying the mapped frame av_frame_make_writable() gives the copy the frames context size, so a 282x173 4:2:0 picture came out as 282x174. --- libavcodec/nvdec.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index d928e3c7a1a..8e830798850 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -962,7 +962,14 @@ finish: if (ret < 0 || decoder->unsafe_output) return ret; - return av_frame_make_writable(frame); + /* The copy has the frames context size */ + int width = frame->width; + int height = frame->height; + + ret = av_frame_make_writable(frame); + frame->width = width; + frame->height = height; + return ret; } int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame) -- 2.54.0 >From 8d9dfafd54eff857819a93e27dcb865ca689f9cc Mon Sep 17 00:00:00 2001 From: marcos ashton <[email protected]> Date: Sun, 8 Nov 2026 15:54:56 +0000 Subject: [PATCH 2/3] avcodec/nvdec: use an even target size dynlink_cuviddec.h says the target should be aligned to 2, and with an odd one, pictures such as 282x173 came out wrong. --- libavcodec/nvdec.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index 8e830798850..e3fb519d9be 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -60,6 +60,8 @@ typedef struct NVDECDecoder { int unsafe_output; int opaque_output; + + unsigned int target_width, target_height; #ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT atomic_int *surface_in_use; int num_surfaces; @@ -619,8 +621,8 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) params.ulWidth = avctx->coded_width; params.ulHeight = avctx->coded_height; - params.ulTargetWidth = avctx->coded_width; - params.ulTargetHeight = avctx->coded_height; + params.ulTargetWidth = FFALIGN(avctx->coded_width, 2); + params.ulTargetHeight = FFALIGN(avctx->coded_height, 2); params.bitDepthMinus8 = sw_desc->comp[0].depth - 8; params.OutputFormat = output_format; params.CodecType = cuvid_codec_type; @@ -668,6 +670,8 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) decoder = ctx->decoder; decoder->unsafe_output = unsafe_output; decoder->opaque_output = opaque_output; + decoder->target_width = params.ulTargetWidth; + decoder->target_height = params.ulTargetHeight; #ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT if (opaque_output) { @@ -947,7 +951,7 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) for (i = 0; frame->linesize[i]; i++) { frame->data[i] = (uint8_t*)(devptr + offset); frame->linesize[i] = pitch; - offset += pitch * (frame->height >> (i ? shift_v : 0)); + offset += pitch * (decoder->target_height >> (i ? shift_v : 0)); } goto finish; -- 2.54.0 >From b683bb5edcd50ea2380a49fd3f6b677286112ae2 Mon Sep 17 00:00:00 2001 From: marcos ashton <[email protected]> Date: Sun, 8 Nov 2026 15:54:56 +0000 Subject: [PATCH 3/3] avcodec/nvdec: output pictures smaller than the decoder at their own size NVDEC scales pictures to the target size, so AV1 frames below the sequence maximum, as in av1-1-b8-03-sizedown, came out scaled up. --- libavcodec/nvdec.c | 54 +++++++++++++++++++++++++++++++++++++++++++++- libavcodec/nvdec.h | 1 + 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c index e3fb519d9be..ec85d941dde 100644 --- a/libavcodec/nvdec.c +++ b/libavcodec/nvdec.c @@ -62,6 +62,8 @@ typedef struct NVDECDecoder { int opaque_output; unsigned int target_width, target_height; + unsigned int min_width, min_height; + CUVIDRECONFIGUREDECODERINFO reconfig; #ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT atomic_int *surface_in_use; int num_surfaces; @@ -148,6 +150,9 @@ static int nvdec_test_capabilities(NVDECDecoder *decoder, av_log(logctx, AV_LOG_VERBOSE, "min_height: %d, max_height: %d\n", caps.nMinHeight, caps.nMaxHeight); + decoder->min_width = caps.nMinWidth; + decoder->min_height = caps.nMinHeight; + if (!caps.bIsSupported) { av_log(logctx, AV_LOG_ERROR, "Hardware is lacking required capabilities\n"); return AVERROR(EINVAL); @@ -621,6 +626,8 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) params.ulWidth = avctx->coded_width; params.ulHeight = avctx->coded_height; + params.ulMaxWidth = avctx->coded_width; + params.ulMaxHeight = avctx->coded_height; params.ulTargetWidth = FFALIGN(avctx->coded_width, 2); params.ulTargetHeight = FFALIGN(avctx->coded_height, 2); params.bitDepthMinus8 = sw_desc->comp[0].depth - 8; @@ -673,6 +680,10 @@ int ff_nvdec_decode_init(AVCodecContext *avctx) decoder->target_width = params.ulTargetWidth; decoder->target_height = params.ulTargetHeight; + decoder->reconfig.ulWidth = params.ulWidth; + decoder->reconfig.ulHeight = params.ulHeight; + decoder->reconfig.ulNumDecodeSurfaces = params.ulNumDecodeSurfaces; + #ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT if (opaque_output) { void *logctx = avctx; @@ -798,6 +809,24 @@ static void nvdec_opaque_release_slot(void *opaque, uint8_t *data) } #endif +static int nvdec_set_target(NVDECDecoder *decoder, unsigned int width, + unsigned int height, void *logctx) +{ + CUVIDRECONFIGUREDECODERINFO reconfig = decoder->reconfig; + int ret; + + reconfig.ulTargetWidth = width; + reconfig.ulTargetHeight = height; + ret = CHECK_CU(decoder->cvdl->cuvidReconfigureDecoder(decoder->decoder, + &reconfig)); + if (ret < 0) + return ret; + + decoder->target_width = width; + decoder->target_height = height; + return 0; +} + static int nvdec_retrieve_data(void *logctx, AVFrame *frame) { FrameDecodeData *fdd = frame->private_ref; @@ -814,7 +843,10 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) unsigned int pitch, i; unsigned int offset = 0; + unsigned int target_width = FFALIGN(decoder->reconfig.ulWidth, 2); + unsigned int target_height = FFALIGN(decoder->reconfig.ulHeight, 2); int shift_h = 0, shift_v = 0; + int reconfigured = 0; int ret = 0; #ifdef NVDEC_HAVE_OPAQUE_OUTPUT_SUPPORT @@ -920,6 +952,22 @@ static int nvdec_retrieve_data(void *logctx, AVFrame *frame) if (ret < 0) return ret; + /* NVDEC scales every picture to the target size */ + if (cf->width >= decoder->min_width && + cf->height >= decoder->min_height && + cf->width <= decoder->reconfig.ulWidth && + cf->height <= decoder->reconfig.ulHeight) { + target_width = FFALIGN(cf->width, 2); + target_height = FFALIGN(cf->height, 2); + } + if (target_width != decoder->target_width || + target_height != decoder->target_height) { + ret = nvdec_set_target(decoder, target_width, target_height, logctx); + if (ret < 0) + goto finish; + reconfigured = 1; + } + ret = CHECK_CU(decoder->cvdl->cuvidMapVideoFrame(decoder->decoder, cf->idx, &devptr, &pitch, &vpp)); @@ -963,7 +1011,9 @@ copy_fail: finish: CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy)); - if (ret < 0 || decoder->unsafe_output) + /* Decoding before the unmap breaks a new target (found by testing), so + * copy now even with unsafe output */ + if (ret < 0 || (decoder->unsafe_output && !reconfigured)) return ret; /* The copy has the frames context size */ @@ -1003,6 +1053,8 @@ int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame) goto fail; } cf->ref_idx = cf->idx = *cf->idx_ref; + cf->width = avctx->coded_width; + cf->height = avctx->coded_height; fdd->hwaccel_priv = cf; fdd->hwaccel_priv_free = nvdec_fdd_priv_free; diff --git a/libavcodec/nvdec.h b/libavcodec/nvdec.h index b2145e6fc22..b1c4281291a 100644 --- a/libavcodec/nvdec.h +++ b/libavcodec/nvdec.h @@ -63,6 +63,7 @@ typedef struct NVDECFrame { unsigned int *idx_ref; ///< RefStruct reference unsigned int *ref_idx_ref; ///< RefStruct reference struct NVDECDecoder *decoder; ///< RefStruct reference + unsigned int width, height; ///< coded size of the picture } NVDECFrame; typedef struct NVDECContext { -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
