diff mbox series

[FFmpeg-devel,v2,2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise

Message ID 20221209141617.13641-2-timo@rothenpieler.org
State Accepted
Commit 7e8b5393895f6a21df501b047e5f987f456b9701
Headers show
Series [FFmpeg-devel,v2,1/2] lavc: add new unsafe_output hwaccel_flag | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Timo Rothenpieler Dec. 9, 2022, 2:16 p.m. UTC
---
 libavcodec/nvdec.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

Comments

Philip Langdale Dec. 9, 2022, 7:20 p.m. UTC | #1
On Fri,  9 Dec 2022 15:16:17 +0100
Timo Rothenpieler <timo@rothenpieler.org> wrote:

> ---
>  libavcodec/nvdec.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
> index fbaedf0b6b..a477449d14 100644
> --- a/libavcodec/nvdec.c
> +++ b/libavcodec/nvdec.c
> @@ -51,6 +51,8 @@ typedef struct NVDECDecoder {
>  
>      CudaFunctions *cudl;
>      CuvidFunctions *cvdl;
> +
> +    int unsafe_output;
>  } NVDECDecoder;
>  
>  typedef struct NVDECFramePool {
> @@ -344,6 +346,8 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
>      int cuvid_codec_type, cuvid_chroma_format, chroma_444;
>      int ret = 0;
>  
> +    int unsafe_output = !!(avctx->hwaccel_flags &
> AV_HWACCEL_FLAG_UNSAFE_OUTPUT); +
>      sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
>      if (!sw_desc)
>          return AVERROR_BUG;
> @@ -402,7 +406,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
>      params.CodecType           = cuvid_codec_type;
>      params.ChromaFormat        = cuvid_chroma_format;
>      params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
> -    params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
> +    params.ulNumOutputSurfaces = unsafe_output ?
> frames_ctx->initial_pool_size : 1; 
>      ret = nvdec_decoder_create(&ctx->decoder_ref,
> frames_ctx->device_ref, &params, avctx); if (ret < 0) {
> @@ -417,6 +421,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
>      }
>  
>      decoder = (NVDECDecoder*)ctx->decoder_ref->data;
> +    decoder->unsafe_output = unsafe_output;
>      decoder->real_hw_frames_ref = real_hw_frames_ref;
>      real_hw_frames_ref = NULL;
>  
> @@ -554,7 +559,11 @@ copy_fail:
>  
>  finish:
>      CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
> -    return ret;
> +
> +    if (ret < 0 || decoder->unsafe_output)
> +        return ret;
> +
> +    return av_frame_make_writable(frame);
>  }
>  
>  int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)

LGTM


--phil
Timo Rothenpieler Dec. 10, 2022, 12:01 a.m. UTC | #2
On 09.12.2022 20:20, Philip Langdale wrote:
> On Fri,  9 Dec 2022 15:16:17 +0100
> Timo Rothenpieler <timo@rothenpieler.org> wrote:
> 
>> ---
>>   libavcodec/nvdec.c | 13 +++++++++++--
>>   1 file changed, 11 insertions(+), 2 deletions(-)
>>
>> diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
>> index fbaedf0b6b..a477449d14 100644
>> --- a/libavcodec/nvdec.c
>> +++ b/libavcodec/nvdec.c
>> @@ -51,6 +51,8 @@ typedef struct NVDECDecoder {
>>   
>>       CudaFunctions *cudl;
>>       CuvidFunctions *cvdl;
>> +
>> +    int unsafe_output;
>>   } NVDECDecoder;
>>   
>>   typedef struct NVDECFramePool {
>> @@ -344,6 +346,8 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
>>       int cuvid_codec_type, cuvid_chroma_format, chroma_444;
>>       int ret = 0;
>>   
>> +    int unsafe_output = !!(avctx->hwaccel_flags &
>> AV_HWACCEL_FLAG_UNSAFE_OUTPUT); +
>>       sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
>>       if (!sw_desc)
>>           return AVERROR_BUG;
>> @@ -402,7 +406,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
>>       params.CodecType           = cuvid_codec_type;
>>       params.ChromaFormat        = cuvid_chroma_format;
>>       params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
>> -    params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
>> +    params.ulNumOutputSurfaces = unsafe_output ?
>> frames_ctx->initial_pool_size : 1;
>>       ret = nvdec_decoder_create(&ctx->decoder_ref,
>> frames_ctx->device_ref, &params, avctx); if (ret < 0) {
>> @@ -417,6 +421,7 @@ int ff_nvdec_decode_init(AVCodecContext *avctx)
>>       }
>>   
>>       decoder = (NVDECDecoder*)ctx->decoder_ref->data;
>> +    decoder->unsafe_output = unsafe_output;
>>       decoder->real_hw_frames_ref = real_hw_frames_ref;
>>       real_hw_frames_ref = NULL;
>>   
>> @@ -554,7 +559,11 @@ copy_fail:
>>   
>>   finish:
>>       CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
>> -    return ret;
>> +
>> +    if (ret < 0 || decoder->unsafe_output)
>> +        return ret;
>> +
>> +    return av_frame_make_writable(frame);
>>   }
>>   
>>   int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
> 
> LGTM
> 
> 

applied
diff mbox series

Patch

diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index fbaedf0b6b..a477449d14 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -51,6 +51,8 @@  typedef struct NVDECDecoder {
 
     CudaFunctions *cudl;
     CuvidFunctions *cvdl;
+
+    int unsafe_output;
 } NVDECDecoder;
 
 typedef struct NVDECFramePool {
@@ -344,6 +346,8 @@  int ff_nvdec_decode_init(AVCodecContext *avctx)
     int cuvid_codec_type, cuvid_chroma_format, chroma_444;
     int ret = 0;
 
+    int unsafe_output = !!(avctx->hwaccel_flags & AV_HWACCEL_FLAG_UNSAFE_OUTPUT);
+
     sw_desc = av_pix_fmt_desc_get(avctx->sw_pix_fmt);
     if (!sw_desc)
         return AVERROR_BUG;
@@ -402,7 +406,7 @@  int ff_nvdec_decode_init(AVCodecContext *avctx)
     params.CodecType           = cuvid_codec_type;
     params.ChromaFormat        = cuvid_chroma_format;
     params.ulNumDecodeSurfaces = frames_ctx->initial_pool_size;
-    params.ulNumOutputSurfaces = frames_ctx->initial_pool_size;
+    params.ulNumOutputSurfaces = unsafe_output ? frames_ctx->initial_pool_size : 1;
 
     ret = nvdec_decoder_create(&ctx->decoder_ref, frames_ctx->device_ref, &params, avctx);
     if (ret < 0) {
@@ -417,6 +421,7 @@  int ff_nvdec_decode_init(AVCodecContext *avctx)
     }
 
     decoder = (NVDECDecoder*)ctx->decoder_ref->data;
+    decoder->unsafe_output = unsafe_output;
     decoder->real_hw_frames_ref = real_hw_frames_ref;
     real_hw_frames_ref = NULL;
 
@@ -554,7 +559,11 @@  copy_fail:
 
 finish:
     CHECK_CU(decoder->cudl->cuCtxPopCurrent(&dummy));
-    return ret;
+
+    if (ret < 0 || decoder->unsafe_output)
+        return ret;
+
+    return av_frame_make_writable(frame);
 }
 
 int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)