diff mbox

[FFmpeg-devel] avcodec/nvdec: Round up odd width/height values

Message ID 20171124182413.20535-1-philipl@overt.org
State Accepted
Commit 4186a77f26a82b4e15109d3286d6de98c2cfe7b6
Headers show

Commit Message

Philip Langdale Nov. 24, 2017, 6:24 p.m. UTC
nvdec will not produce odd width/height output, and while this is
basically never an issue with most codecs, due to internal alignment
requirements, you can get odd sized jpegs.

If an odd-sized jpb is encountered, nvdec will actually round down
internally and produce output that is slightly smaller. This isn't
the end of the world, as long as you know the output size doesn't
match the original image resolution.

However, with an hwaccel, we don't know. The decoder controls
the reported output size and the hwaccel cannot change it. I was
able to trigger an error in mpv where it tries to copy the output
surface as part of rendering and triggers a cuda error because
cuda knows the output frame is smaller than expected.

To fix this, we can round up the configured width/height passed
to nvdec so that the frames are always at least as large as the
decoder's reported size, and data can be copied out safely.

In this particular jpeg case, you end up with a blank (green) line
at the bottom due to nvdec refusing to decode the last line, but
the behaviour matches cuviddec, so it's as good as you're going to
get.

Signed-off-by: Philip Langdale <philipl@overt.org>
---
 libavcodec/nvdec.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

Comments

Timo Rothenpieler Nov. 24, 2017, 7:05 p.m. UTC | #1
Am 24.11.2017 um 19:24 schrieb Philip Langdale:
> nvdec will not produce odd width/height output, and while this is
> basically never an issue with most codecs, due to internal alignment
> requirements, you can get odd sized jpegs.
> 
> If an odd-sized jpb is encountered, nvdec will actually round down

jpg

> internally and produce output that is slightly smaller. This isn't
> the end of the world, as long as you know the output size doesn't
> match the original image resolution.
> 
> However, with an hwaccel, we don't know. The decoder controls
> the reported output size and the hwaccel cannot change it. I was
> able to trigger an error in mpv where it tries to copy the output
> surface as part of rendering and triggers a cuda error because
> cuda knows the output frame is smaller than expected.
> 
> To fix this, we can round up the configured width/height passed
> to nvdec so that the frames are always at least as large as the
> decoder's reported size, and data can be copied out safely.
> 
> In this particular jpeg case, you end up with a blank (green) line
> at the bottom due to nvdec refusing to decode the last line, but
> the behaviour matches cuviddec, so it's as good as you're going to
> get.
> 
> Signed-off-by: Philip Langdale <philipl@overt.org>
> ---
>   libavcodec/nvdec.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
> index 206429c603..dc7badc4d9 100644
> --- a/libavcodec/nvdec.c
> +++ b/libavcodec/nvdec.c
> @@ -534,8 +534,8 @@ int ff_nvdec_frame_params(AVCodecContext *avctx,
>       }
>   
>       frames_ctx->format            = AV_PIX_FMT_CUDA;
> -    frames_ctx->width             = avctx->coded_width;
> -    frames_ctx->height            = avctx->coded_height;
> +    frames_ctx->width             = (avctx->coded_width + 1) & ~1;
> +    frames_ctx->height            = (avctx->coded_height + 1) & ~1;
>       frames_ctx->initial_pool_size = dpb_size;
>   
>       switch (sw_desc->comp[0].depth) {
> 

lgtm
diff mbox

Patch

diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index 206429c603..dc7badc4d9 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -534,8 +534,8 @@  int ff_nvdec_frame_params(AVCodecContext *avctx,
     }
 
     frames_ctx->format            = AV_PIX_FMT_CUDA;
-    frames_ctx->width             = avctx->coded_width;
-    frames_ctx->height            = avctx->coded_height;
+    frames_ctx->width             = (avctx->coded_width + 1) & ~1;
+    frames_ctx->height            = (avctx->coded_height + 1) & ~1;
     frames_ctx->initial_pool_size = dpb_size;
 
     switch (sw_desc->comp[0].depth) {