Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag
@ 2022-12-05 22:25 Timo Rothenpieler
  2022-12-05 22:25 ` [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
                   ` (3 more replies)
  0 siblings, 4 replies; 10+ messages in thread
From: Timo Rothenpieler @ 2022-12-05 22:25 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Timo Rothenpieler

---
 doc/APIchanges             |  3 +++
 libavcodec/avcodec.h       | 11 +++++++++++
 libavcodec/options_table.h |  1 +
 libavcodec/version.h       |  2 +-
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index ab7ce15fae..9c1f905a61 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-12-xx - xxxxxxxxxx - lavc 59.54.101 - avcodec.h
+  Add AV_HWACCEL_FLAG_MINIMIZE_COPIES.
+
 2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h
   Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT.
 
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 3edd8e2636..a72551bb5e 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2253,6 +2253,17 @@ typedef struct AVHWAccel {
  */
 #define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH (1 << 2)
 
+/**
+ * Hardware acceleration can have a limited number of direct output surfaces.
+ * For some processing chains, this can be okay, but others will run into the
+ * limit and in turn produce very confusing errors.
+ *
+ * Thus, the hwaccel will by default make a safety copy. If a users really
+ * wants to minimize the amount of copies, they can set this flag and ensure
+ * their chain does not exhaust the surface pool.
+ */
+#define AV_HWACCEL_FLAG_MINIMIZE_COPIES (1 << 3)
+
 /**
  * @}
  */
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index cd02f5096f..05e8948bc2 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -399,6 +399,7 @@ static const AVOption avcodec_options[] = {
 {"ignore_level", "ignore level even if the codec level used is unknown or higher than the maximum supported level reported by the hardware driver", 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWACCEL_FLAG_IGNORE_LEVEL }, INT_MIN, INT_MAX, V | D, "hwaccel_flags" },
 {"allow_high_depth", "allow to output YUV pixel formats with a different chroma sampling than 4:2:0 and/or other than 8 bits per component", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
 {"allow_profile_mismatch", "attempt to decode anyway if HW accelerated decoder's supported profiles do not exactly match the stream", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
+{"minimize_copies", "minimize number of frame copies at cost of risking surface pool exhaustion", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_MINIMIZE_COPIES }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
 {"extra_hw_frames", "Number of extra hardware frames to allocate for the user", OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, V|D },
 {"discard_damaged_percentage", "Percentage of damaged samples to discard a frame", OFFSET(discard_damaged_percentage), AV_OPT_TYPE_INT, {.i64 = 95 }, 0, 100, V|D },
 {NULL},
diff --git a/libavcodec/version.h b/libavcodec/version.h
index d149bc6c46..9e66920593 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -30,7 +30,7 @@
 #include "version_major.h"
 
 #define LIBAVCODEC_VERSION_MINOR  54
-#define LIBAVCODEC_VERSION_MICRO 100
+#define LIBAVCODEC_VERSION_MICRO 101
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise
  2022-12-05 22:25 [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Timo Rothenpieler
@ 2022-12-05 22:25 ` Timo Rothenpieler
  2022-12-06 23:42   ` Philip Langdale
  2022-12-06 23:42 ` [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Philip Langdale
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 10+ messages in thread
From: Timo Rothenpieler @ 2022-12-05 22:25 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Timo Rothenpieler

---
 libavcodec/nvdec.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/libavcodec/nvdec.c b/libavcodec/nvdec.c
index fbaedf0b6b..76ee395734 100644
--- a/libavcodec/nvdec.c
+++ b/libavcodec/nvdec.c
@@ -51,6 +51,8 @@ typedef struct NVDECDecoder {
 
     CudaFunctions *cudl;
     CuvidFunctions *cvdl;
+
+    int minimize_copies;
 } 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 minimize_copies = !!(avctx->hwaccel_flags & AV_HWACCEL_FLAG_MINIMIZE_COPIES);
+
     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 = minimize_copies ? 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->minimize_copies = minimize_copies;
     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->minimize_copies)
+        return ret;
+
+    return av_frame_make_writable(frame);
 }
 
 int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag
  2022-12-05 22:25 [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Timo Rothenpieler
  2022-12-05 22:25 ` [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
@ 2022-12-06 23:42 ` Philip Langdale
  2022-12-07  3:05 ` Andreas Rheinhardt
  2022-12-09 14:16 ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Timo Rothenpieler
  3 siblings, 0 replies; 10+ messages in thread
From: Philip Langdale @ 2022-12-06 23:42 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

On Mon,  5 Dec 2022 23:25:58 +0100
Timo Rothenpieler <timo@rothenpieler.org> wrote:

> ---
>  doc/APIchanges             |  3 +++
>  libavcodec/avcodec.h       | 11 +++++++++++
>  libavcodec/options_table.h |  1 +
>  libavcodec/version.h       |  2 +-
>  4 files changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index ab7ce15fae..9c1f905a61 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,9 @@ libavutil:     2021-04-27
>  
>  API changes, most recent first:
>  
> +2022-12-xx - xxxxxxxxxx - lavc 59.54.101 - avcodec.h
> +  Add AV_HWACCEL_FLAG_MINIMIZE_COPIES.
> +
>  2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h
>    Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT.
>  
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 3edd8e2636..a72551bb5e 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -2253,6 +2253,17 @@ typedef struct AVHWAccel {
>   */
>  #define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH (1 << 2)
>  
> +/**
> + * Hardware acceleration can have a limited number of direct output
> surfaces.
> + * For some processing chains, this can be okay, but others will run
> into the
> + * limit and in turn produce very confusing errors.
> + *
> + * Thus, the hwaccel will by default make a safety copy. If a users
> really
> + * wants to minimize the amount of copies, they can set this flag
> and ensure
> + * their chain does not exhaust the surface pool.
> + */
> +#define AV_HWACCEL_FLAG_MINIMIZE_COPIES (1 << 3)

I would soften the statement about making a safety copy to make it
clear that only hwaccels where limits are relevant will make safety
copies. Otherwise people will think every other hwaccel has suddenly
started doing it.

Otherwise, LGTM.

--phil
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise
  2022-12-05 22:25 ` [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
@ 2022-12-06 23:42   ` Philip Langdale
  0 siblings, 0 replies; 10+ messages in thread
From: Philip Langdale @ 2022-12-06 23:42 UTC (permalink / raw)
  To: ffmpeg-devel

On Mon,  5 Dec 2022 23:25:59 +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..76ee395734 100644
> --- a/libavcodec/nvdec.c
> +++ b/libavcodec/nvdec.c
> @@ -51,6 +51,8 @@ typedef struct NVDECDecoder {
>  
>      CudaFunctions *cudl;
>      CuvidFunctions *cvdl;
> +
> +    int minimize_copies;
>  } 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 minimize_copies = !!(avctx->hwaccel_flags &
> AV_HWACCEL_FLAG_MINIMIZE_COPIES); +
>      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 = minimize_copies ?
> 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->minimize_copies = minimize_copies;
>      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->minimize_copies)
> +        return ret;
> +
> +    return av_frame_make_writable(frame);
>  }
>  
>  int ff_nvdec_start_frame(AVCodecContext *avctx, AVFrame *frame)

LGTM.


--phil
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag
  2022-12-05 22:25 [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Timo Rothenpieler
  2022-12-05 22:25 ` [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
  2022-12-06 23:42 ` [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Philip Langdale
@ 2022-12-07  3:05 ` Andreas Rheinhardt
  2022-12-09 14:16 ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Timo Rothenpieler
  3 siblings, 0 replies; 10+ messages in thread
From: Andreas Rheinhardt @ 2022-12-07  3:05 UTC (permalink / raw)
  To: ffmpeg-devel

Timo Rothenpieler:
> ---
>  doc/APIchanges             |  3 +++
>  libavcodec/avcodec.h       | 11 +++++++++++
>  libavcodec/options_table.h |  1 +
>  libavcodec/version.h       |  2 +-
>  4 files changed, 16 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index ab7ce15fae..9c1f905a61 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,9 @@ libavutil:     2021-04-27
>  
>  API changes, most recent first:
>  
> +2022-12-xx - xxxxxxxxxx - lavc 59.54.101 - avcodec.h
> +  Add AV_HWACCEL_FLAG_MINIMIZE_COPIES.
> +
>  2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h
>    Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT.
>  
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 3edd8e2636..a72551bb5e 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -2253,6 +2253,17 @@ typedef struct AVHWAccel {
>   */
>  #define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH (1 << 2)
>  
> +/**
> + * Hardware acceleration can have a limited number of direct output surfaces.
> + * For some processing chains, this can be okay, but others will run into the
> + * limit and in turn produce very confusing errors.
> + *
> + * Thus, the hwaccel will by default make a safety copy. If a users really
> + * wants to minimize the amount of copies, they can set this flag and ensure
> + * their chain does not exhaust the surface pool.
> + */
> +#define AV_HWACCEL_FLAG_MINIMIZE_COPIES (1 << 3)
> +
>  /**
>   * @}
>   */
> diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
> index cd02f5096f..05e8948bc2 100644
> --- a/libavcodec/options_table.h
> +++ b/libavcodec/options_table.h
> @@ -399,6 +399,7 @@ static const AVOption avcodec_options[] = {
>  {"ignore_level", "ignore level even if the codec level used is unknown or higher than the maximum supported level reported by the hardware driver", 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWACCEL_FLAG_IGNORE_LEVEL }, INT_MIN, INT_MAX, V | D, "hwaccel_flags" },
>  {"allow_high_depth", "allow to output YUV pixel formats with a different chroma sampling than 4:2:0 and/or other than 8 bits per component", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
>  {"allow_profile_mismatch", "attempt to decode anyway if HW accelerated decoder's supported profiles do not exactly match the stream", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
> +{"minimize_copies", "minimize number of frame copies at cost of risking surface pool exhaustion", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_MINIMIZE_COPIES }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
>  {"extra_hw_frames", "Number of extra hardware frames to allocate for the user", OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, V|D },
>  {"discard_damaged_percentage", "Percentage of damaged samples to discard a frame", OFFSET(discard_damaged_percentage), AV_OPT_TYPE_INT, {.i64 = 95 }, 0, 100, V|D },
>  {NULL},
> diff --git a/libavcodec/version.h b/libavcodec/version.h
> index d149bc6c46..9e66920593 100644
> --- a/libavcodec/version.h
> +++ b/libavcodec/version.h
> @@ -30,7 +30,7 @@
>  #include "version_major.h"
>  
>  #define LIBAVCODEC_VERSION_MINOR  54
> -#define LIBAVCODEC_VERSION_MICRO 100
> +#define LIBAVCODEC_VERSION_MICRO 101

A new public define needs a minor bump.

>  
>  #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
>                                                 LIBAVCODEC_VERSION_MINOR, \

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag
  2022-12-05 22:25 [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Timo Rothenpieler
                   ` (2 preceding siblings ...)
  2022-12-07  3:05 ` Andreas Rheinhardt
@ 2022-12-09 14:16 ` Timo Rothenpieler
  2022-12-09 14:16   ` [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
  2022-12-09 19:20   ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Philip Langdale
  3 siblings, 2 replies; 10+ messages in thread
From: Timo Rothenpieler @ 2022-12-09 14:16 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Timo Rothenpieler

---
 doc/APIchanges             |  3 +++
 libavcodec/avcodec.h       | 16 ++++++++++++++++
 libavcodec/options_table.h |  1 +
 libavcodec/version.h       |  4 ++--
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index ab7ce15fae..328028f293 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-12-xx - xxxxxxxxxx - lavc 59.55.100 - avcodec.h
+  Add AV_HWACCEL_FLAG_UNSAFE_OUTPUT.
+
 2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h
   Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT.
 
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 3edd8e2636..0ac581d660 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -2253,6 +2253,22 @@ typedef struct AVHWAccel {
  */
 #define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH (1 << 2)
 
+/**
+ * Some hardware decoders (namely nvdec) can either output direct decoder
+ * surfaces, or make an on-device copy and return said copy.
+ * There is a hard limit on how many decoder surfaces there can be, and it
+ * cannot be accurately guessed ahead of time.
+ * For some processing chains, this can be okay, but others will run into the
+ * limit and in turn produce very confusing errors that require fine tuning of
+ * more or less obscure options by the user, or in extreme cases cannot be
+ * resolved at all without inserting an avfilter that forces a copy.
+ *
+ * Thus, the hwaccel will by default make a copy for safety and resilience.
+ * If a users really wants to minimize the amount of copies, they can set this
+ * flag and ensure their processing chain does not exhaust the surface pool.
+ */
+#define AV_HWACCEL_FLAG_UNSAFE_OUTPUT (1 << 3)
+
 /**
  * @}
  */
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index cd02f5096f..7924ca6144 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -399,6 +399,7 @@ static const AVOption avcodec_options[] = {
 {"ignore_level", "ignore level even if the codec level used is unknown or higher than the maximum supported level reported by the hardware driver", 0, AV_OPT_TYPE_CONST, { .i64 = AV_HWACCEL_FLAG_IGNORE_LEVEL }, INT_MIN, INT_MAX, V | D, "hwaccel_flags" },
 {"allow_high_depth", "allow to output YUV pixel formats with a different chroma sampling than 4:2:0 and/or other than 8 bits per component", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
 {"allow_profile_mismatch", "attempt to decode anyway if HW accelerated decoder's supported profiles do not exactly match the stream", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
+{"unsafe_output", "allow potentially unsafe hwaccel frame output that might require special care to process successfully", 0, AV_OPT_TYPE_CONST, {.i64 = AV_HWACCEL_FLAG_UNSAFE_OUTPUT }, INT_MIN, INT_MAX, V | D, "hwaccel_flags"},
 {"extra_hw_frames", "Number of extra hardware frames to allocate for the user", OFFSET(extra_hw_frames), AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, V|D },
 {"discard_damaged_percentage", "Percentage of damaged samples to discard a frame", OFFSET(discard_damaged_percentage), AV_OPT_TYPE_INT, {.i64 = 95 }, 0, 100, V|D },
 {NULL},
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 9e66920593..9f42f09f4e 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -29,8 +29,8 @@
 
 #include "version_major.h"
 
-#define LIBAVCODEC_VERSION_MINOR  54
-#define LIBAVCODEC_VERSION_MICRO 101
+#define LIBAVCODEC_VERSION_MINOR  55
+#define LIBAVCODEC_VERSION_MICRO 100
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise
  2022-12-09 14:16 ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Timo Rothenpieler
@ 2022-12-09 14:16   ` Timo Rothenpieler
  2022-12-09 19:20     ` Philip Langdale
  2022-12-09 19:20   ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Philip Langdale
  1 sibling, 1 reply; 10+ messages in thread
From: Timo Rothenpieler @ 2022-12-09 14:16 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Timo Rothenpieler

---
 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)
-- 
2.34.1

_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag
  2022-12-09 14:16 ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Timo Rothenpieler
  2022-12-09 14:16   ` [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
@ 2022-12-09 19:20   ` Philip Langdale
  1 sibling, 0 replies; 10+ messages in thread
From: Philip Langdale @ 2022-12-09 19:20 UTC (permalink / raw)
  To: ffmpeg-devel

On Fri,  9 Dec 2022 15:16:16 +0100
Timo Rothenpieler <timo@rothenpieler.org> wrote:

> ---
>  doc/APIchanges             |  3 +++
>  libavcodec/avcodec.h       | 16 ++++++++++++++++
>  libavcodec/options_table.h |  1 +
>  libavcodec/version.h       |  4 ++--
>  4 files changed, 22 insertions(+), 2 deletions(-)
> 
> diff --git a/doc/APIchanges b/doc/APIchanges
> index ab7ce15fae..328028f293 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,9 @@ libavutil:     2021-04-27
>  
>  API changes, most recent first:
>  
> +2022-12-xx - xxxxxxxxxx - lavc 59.55.100 - avcodec.h
> +  Add AV_HWACCEL_FLAG_UNSAFE_OUTPUT.
> +
>  2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h
>    Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT.
>  
> diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
> index 3edd8e2636..0ac581d660 100644
> --- a/libavcodec/avcodec.h
> +++ b/libavcodec/avcodec.h
> @@ -2253,6 +2253,22 @@ typedef struct AVHWAccel {
>   */
>  #define AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH (1 << 2)
>  
> +/**
> + * Some hardware decoders (namely nvdec) can either output direct
> decoder
> + * surfaces, or make an on-device copy and return said copy.
> + * There is a hard limit on how many decoder surfaces there can be,
> and it
> + * cannot be accurately guessed ahead of time.
> + * For some processing chains, this can be okay, but others will run
> into the
> + * limit and in turn produce very confusing errors that require fine
> tuning of
> + * more or less obscure options by the user, or in extreme cases
> cannot be
> + * resolved at all without inserting an avfilter that forces a copy.
> + *
> + * Thus, the hwaccel will by default make a copy for safety and
> resilience.
> + * If a users really wants to minimize the amount of copies, they
> can set this
> + * flag and ensure their processing chain does not exhaust the
> surface pool.
> + */
> +#define AV_HWACCEL_FLAG_UNSAFE_OUTPUT (1 << 3)
> +
>  /**
>   * @}
>   */
> diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
> index cd02f5096f..7924ca6144 100644
> --- a/libavcodec/options_table.h
> +++ b/libavcodec/options_table.h
> @@ -399,6 +399,7 @@ static const AVOption avcodec_options[] = {
>  {"ignore_level", "ignore level even if the codec level used is
> unknown or higher than the maximum supported level reported by the
> hardware driver", 0, AV_OPT_TYPE_CONST, { .i64 =
> AV_HWACCEL_FLAG_IGNORE_LEVEL }, INT_MIN, INT_MAX, V | D,
> "hwaccel_flags" }, {"allow_high_depth", "allow to output YUV pixel
> formats with a different chroma sampling than 4:2:0 and/or other than
> 8 bits per component", 0, AV_OPT_TYPE_CONST, {.i64 =
> AV_HWACCEL_FLAG_ALLOW_HIGH_DEPTH }, INT_MIN, INT_MAX, V | D,
> "hwaccel_flags"}, {"allow_profile_mismatch", "attempt to decode
> anyway if HW accelerated decoder's supported profiles do not exactly
> match the stream", 0, AV_OPT_TYPE_CONST, {.i64 =
> AV_HWACCEL_FLAG_ALLOW_PROFILE_MISMATCH }, INT_MIN, INT_MAX, V | D,
> "hwaccel_flags"}, +{"unsafe_output", "allow potentially unsafe
> hwaccel frame output that might require special care to process
> successfully", 0, AV_OPT_TYPE_CONST, {.i64 =
> AV_HWACCEL_FLAG_UNSAFE_OUTPUT }, INT_MIN, INT_MAX, V | D,
> "hwaccel_flags"}, {"extra_hw_frames", "Number of extra hardware
> frames to allocate for the user", OFFSET(extra_hw_frames),
> AV_OPT_TYPE_INT, { .i64 = -1 }, -1, INT_MAX, V|D },
> {"discard_damaged_percentage", "Percentage of damaged samples to
> discard a frame", OFFSET(discard_damaged_percentage),
> AV_OPT_TYPE_INT, {.i64 = 95 }, 0, 100, V|D }, {NULL}, diff --git
> a/libavcodec/version.h b/libavcodec/version.h index
> 9e66920593..9f42f09f4e 100644 --- a/libavcodec/version.h +++
> b/libavcodec/version.h @@ -29,8 +29,8 @@ #include "version_major.h"
> -#define LIBAVCODEC_VERSION_MINOR  54 -#define
> LIBAVCODEC_VERSION_MICRO 101 +#define LIBAVCODEC_VERSION_MINOR  55
> +#define LIBAVCODEC_VERSION_MICRO 100 #define LIBAVCODEC_VERSION_INT
> AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \ LIBAVCODEC_VERSION_MINOR, \

LGTM


--phil
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise
  2022-12-09 14:16   ` [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
@ 2022-12-09 19:20     ` Philip Langdale
  2022-12-10  0:01       ` Timo Rothenpieler
  0 siblings, 1 reply; 10+ messages in thread
From: Philip Langdale @ 2022-12-09 19:20 UTC (permalink / raw)
  To: ffmpeg-devel

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
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise
  2022-12-09 19:20     ` Philip Langdale
@ 2022-12-10  0:01       ` Timo Rothenpieler
  0 siblings, 0 replies; 10+ messages in thread
From: Timo Rothenpieler @ 2022-12-10  0:01 UTC (permalink / raw)
  To: ffmpeg-devel

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
_______________________________________________
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".

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2022-12-10  0:01 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-12-05 22:25 [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Timo Rothenpieler
2022-12-05 22:25 ` [FFmpeg-devel] [PATCH 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
2022-12-06 23:42   ` Philip Langdale
2022-12-06 23:42 ` [FFmpeg-devel] [PATCH 1/2] lavc: add new minimize_copies hwaccel_flag Philip Langdale
2022-12-07  3:05 ` Andreas Rheinhardt
2022-12-09 14:16 ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Timo Rothenpieler
2022-12-09 14:16   ` [FFmpeg-devel] [PATCH v2 2/2] avcodec/nvdec: make explicit copy of frames unless user requested otherwise Timo Rothenpieler
2022-12-09 19:20     ` Philip Langdale
2022-12-10  0:01       ` Timo Rothenpieler
2022-12-09 19:20   ` [FFmpeg-devel] [PATCH v2 1/2] lavc: add new unsafe_output hwaccel_flag Philip Langdale

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git