Browse code

threads: default to automatic thread count detection

Janne Grunau authored on 2011/12/25 19:45:05
Showing 4 changed files
... ...
@@ -6,6 +6,7 @@ version <next>:
6 6
 
7 7
 - Indeo 4 decoder
8 8
 - SMJPEG demuxer
9
+- Automatic thread count based on detection number of (available) CPU cores
9 10
 
10 11
 
11 12
 version 0.8_beta1:
... ...
@@ -370,7 +370,7 @@ static const AVOption options[]={
370 370
 {"float", NULL, 0, AV_OPT_TYPE_CONST, {.dbl = FF_AA_FLOAT }, INT_MIN, INT_MAX, V|D, "aa"},
371 371
 #endif
372 372
 {"qns", "quantizer noise shaping", OFFSET(quantizer_noise_shaping), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
373
-{"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.dbl = 1 }, 0, INT_MAX, V|E|D, "threads"},
373
+{"threads", NULL, OFFSET(thread_count), AV_OPT_TYPE_INT, {.dbl = 0 }, 0, INT_MAX, V|E|D, "threads"},
374 374
 {"auto", "detect a good number of threads", 0, AV_OPT_TYPE_CONST, {.dbl = 0 }, INT_MIN, INT_MAX, V|E|D, "threads"},
375 375
 {"me_threshold", "motion estimaton threshold", OFFSET(me_threshold), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
376 376
 {"mb_threshold", "macroblock threshold", OFFSET(mb_threshold), AV_OPT_TYPE_INT, {.dbl = DEFAULT }, INT_MIN, INT_MAX, V|E},
... ...
@@ -22,7 +22,7 @@
22 22
 
23 23
 #define LIBAVCODEC_VERSION_MAJOR 53
24 24
 #define LIBAVCODEC_VERSION_MINOR 32
25
-#define LIBAVCODEC_VERSION_MICRO  0
25
+#define LIBAVCODEC_VERSION_MICRO  1
26 26
 
27 27
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
28 28
                                                LIBAVCODEC_VERSION_MINOR, \
... ...
@@ -2268,9 +2268,14 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2268 2268
     int i, count, ret, read_size, j;
2269 2269
     AVStream *st;
2270 2270
     AVPacket pkt1, *pkt;
2271
+    AVDictionary *one_thread_opt = NULL;
2271 2272
     int64_t old_offset = avio_tell(ic->pb);
2272 2273
     int orig_nb_streams = ic->nb_streams;        // new streams might appear, no options for those
2273 2274
 
2275
+    /* this function doesn't flush the decoders, so force thread count
2276
+     * to 1 to fix behavior when thread count > number of frames in the file */
2277
+    av_dict_set(&one_thread_opt, "threads", "1", 0);
2278
+
2274 2279
     for(i=0;i<ic->nb_streams;i++) {
2275 2280
         AVCodec *codec;
2276 2281
         st = ic->streams[i];
... ...
@@ -2300,12 +2305,13 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2300 2300
         /* Ensure that subtitle_header is properly set. */
2301 2301
         if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE
2302 2302
             && codec && !st->codec->codec)
2303
-            avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
2303
+            avcodec_open2(st->codec, codec, options ? &options[i] : &one_thread_opt);
2304 2304
 
2305 2305
         //try to just open decoders, in case this is enough to get parameters
2306 2306
         if(!has_codec_parameters(st->codec)){
2307 2307
             if (codec && !st->codec->codec)
2308
-                avcodec_open2(st->codec, codec, options ? &options[i] : NULL);
2308
+                avcodec_open2(st->codec, codec, options ? &options[i]
2309
+                              : &one_thread_opt);
2309 2310
         }
2310 2311
     }
2311 2312
 
... ...
@@ -2444,7 +2450,8 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2444 2444
            least one frame of codec data, this makes sure the codec initializes
2445 2445
            the channel configuration and does not only trust the values from the container.
2446 2446
         */
2447
-        try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i] : NULL);
2447
+        try_decode_frame(st, pkt, (options && i < orig_nb_streams )? &options[i]
2448
+                         : &one_thread_opt);
2448 2449
 
2449 2450
         st->codec_info_nb_frames++;
2450 2451
         count++;
... ...
@@ -2549,8 +2556,12 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
2549 2549
 #endif
2550 2550
 
2551 2551
  find_stream_info_err:
2552
-    for (i=0; i < ic->nb_streams; i++)
2552
+    for (i=0; i < ic->nb_streams; i++) {
2553
+        if (ic->streams[i]->codec)
2554
+            ic->streams[i]->codec->thread_count = 0;
2553 2555
         av_freep(&ic->streams[i]->info);
2556
+    }
2557
+    av_dict_free(&one_thread_opt);
2554 2558
     return ret;
2555 2559
 }
2556 2560