[FFmpeg-cvslog] threads: default to automatic thread count detection

Janne Grunau git at videolan.org
Sun Jan 1 02:49:36 CET 2012


ffmpeg | branch: master | Janne Grunau <janne-libav at jannau.net> | Sun Dec 25 11:45:05 2011 +0100| [e4e30256f87f177decf59b59e923d05ef64147df] | committer: Janne Grunau

threads: default to automatic thread count detection

> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=e4e30256f87f177decf59b59e923d05ef64147df
---

 Changelog            |    1 +
 libavcodec/options.c |    2 +-
 libavcodec/version.h |    2 +-
 libavformat/utils.c  |   19 +++++++++++++++----
 4 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/Changelog b/Changelog
index 0cbc20a..c305406 100644
--- a/Changelog
+++ b/Changelog
@@ -6,6 +6,7 @@ version <next>:
 
 - Indeo 4 decoder
 - SMJPEG demuxer
+- Automatic thread count based on detection number of (available) CPU cores
 
 
 version 0.8_beta1:
diff --git a/libavcodec/options.c b/libavcodec/options.c
index 4ac9313..4ac92fd 100644
--- a/libavcodec/options.c
+++ b/libavcodec/options.c
@@ -370,7 +370,7 @@ static const AVOption options[]={
 {"float", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_AA_FLOAT }, INT_MIN, INT_MAX, V|D, "aa"},
 #endif
 {"qns", "quantizer noise shaping", OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
-{"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.dbl = 1 }, 0, INT_MAX, V|E|D, "threads"},
+{"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, V|E|D, "threads"},
 {"auto", "detect a good number of threads", 0, AV_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, V|E|D, "threads"},
 {"me_threshold", "motion estimaton threshold", OFFSET(me_threshold), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
 {"mb_threshold", "macroblock threshold", OFFSET(mb_threshold), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
diff --git a/libavcodec/version.h b/libavcodec/version.h
index 9b4f6d7..e6226cb 100644
--- a/libavcodec/version.h
+++ b/libavcodec/version.h
@@ -22,7 +22,7 @@
 
 #define LIBAVCODEC_VERSION_MAJOR 53
 #define LIBAVCODEC_VERSION_MINOR 32
-#define LIBAVCODEC_VERSION_MICRO  0
+#define LIBAVCODEC_VERSION_MICRO  1
 
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
                                                LIBAVCODEC_VERSION_MINOR, \
diff --git a/libavformat/utils.c b/libavformat/utils.c
index aa2c276..163d602 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2268,9 +2268,14 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
     int i, count, ret, read_size, j;
     AVStream *st;
     AVPacket pkt1, *pkt;
+    AVDictionary *one_thread_opt = NULL;
     int64_t old_offset = avio_tell(ic->pb);
     int orig_nb_streams = ic->nb_streams;        // new streams might appear, no options for those
 
+    /* this function doesn't flush the decoders, so force thread count
+     * to 1 to fix behavior when thread count > number of frames in the file */
+    av_dict_set(&one_thread_opt, "threads", "1", 0);
+
     for(i=0;i<ic->nb_streams;i++) {
         AVCodec *codec;
         st = ic->streams[i];
@@ -2300,12 +2305,13 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
         /* Ensure that subtitle_header is properly set. */
         if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
             && codec && !st->codec->codec)
-            avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
+            avcodec_open2(st->codec, codec, options ? &options[i] : &one_thread_opt);
 
         //try to just open decoders, in case this is enough to get parameters
         if(!has_codec_parameters(st->codec)){
             if (codec && !st->codec->codec)
-                avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
+                avcodec_open2(st->codec, codec, options ? &options[i]
+                              : &one_thread_opt);
         }
     }
 
@@ -2444,7 +2450,8 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
            least one frame of codec data, this makes sure the codec initializes
            the channel configuration and does not only trust the values from the container.
         */
-        try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i] : NULL);
+        try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i]
+                         : &one_thread_opt);
 
         st->codec_info_nb_frames++;
         count++;
@@ -2549,8 +2556,12 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
 #endif
 
  find_stream_info_err:
-    for (i=0; i < ic->nb_streams; i++)
+    for (i=0; i < ic->nb_streams; i++) {
+        if (ic->streams[i]->codec)
+            ic->streams[i]->codec->thread_count = 0;
         av_freep(&ic->streams[i]->info);
+    }
+    av_dict_free(&one_thread_opt);
     return ret;
 }
 



More information about the ffmpeg-cvslog mailing list