[FFmpeg-devel] [RFC] ffmpeg: default to single thread when hwaccel is active

Timo Rothenpieler timo at rothenpieler.org
Wed Sep 29 20:01:34 EEST 2021


---
 fftools/ffmpeg.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

Not sure if there is any downside to this.
Threading for hwaccel does not make a whole lot of sense, and at least
in case of nvdec wastes a lot of VRAM for no performance gain, and
specially on high core count systems by default can even exhaust the
maximum frame pool size.

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 98c2421938..d007d55173 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2995,12 +2995,6 @@ FF_ENABLE_DEPRECATION_WARNINGS
          * audio, and video decoders such as cuvid or mediacodec */
         ist->dec_ctx->pkt_timebase = ist->st->time_base;
 
-        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0))
-            av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
-        /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
-        if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
-            av_dict_set(&ist->decoder_opts, "threads", "1", 0);
-
         ret = hw_device_setup_for_decode(ist);
         if (ret < 0) {
             snprintf(error, error_len, "Device setup failed for "
@@ -3009,6 +3003,16 @@ FF_ENABLE_DEPRECATION_WARNINGS
             return ret;
         }
 
+        if (!av_dict_get(ist->decoder_opts, "threads", NULL, 0)) {
+            if (ist->dec_ctx->hw_device_ctx)
+                av_dict_set(&ist->decoder_opts, "threads", "1", 0);
+            else
+                av_dict_set(&ist->decoder_opts, "threads", "auto", 0);
+        }
+        /* Attached pics are sparse, therefore we would not want to delay their decoding till EOF. */
+        if (ist->st->disposition & AV_DISPOSITION_ATTACHED_PIC)
+            av_dict_set(&ist->decoder_opts, "threads", "1", 0);
+
         if ((ret = avcodec_open2(ist->dec_ctx, codec, &ist->decoder_opts)) < 0) {
             if (ret == AVERROR_EXPERIMENTAL)
                 abort_codec_experimental(codec, 0);
-- 
2.25.1



More information about the ffmpeg-devel mailing list