Browse code

Frame-based multithreading framework using pthreads

See doc/multithreading.txt for details on use in codecs.

Signed-off-by: Ronald S. Bultje <rsbultje@gmail.com>

Alexander Strange authored on 2011/02/08 11:15:44
Showing 11 changed files
... ...
@@ -13,6 +13,11 @@ libavutil:   2009-03-08
13 13
 
14 14
 API changes, most recent first:
15 15
 
16
+2011-02-09 - XXXXXXX - lavc 52.111.0 - threading API
17
+  Add CODEC_CAP_FRAME_THREADS with new restrictions on get_buffer()/
18
+  release_buffer()/draw_horiz_band() callbacks for appropriate codecs.
19
+  Add thread_type and active_thread_type fields to AVCodecContext.
20
+
16 21
 2011-02-08 - 3940caa - lavf 52.98.0 - av_probe_input_buffer
17 22
   Add av_probe_input_buffer() to avformat.h for probing format from a
18 23
   ByteIOContext.
19 24
new file mode 100644
... ...
@@ -0,0 +1,65 @@
0
+FFmpeg multithreading methods
1
+==============================================
2
+
3
+FFmpeg provides two methods for multithreading codecs.
4
+
5
+Slice threading decodes multiple parts of a frame at the same time, using
6
+AVCodecContext execute() and execute2().
7
+
8
+Frame threading decodes multiple frames at the same time.
9
+It accepts N future frames and delays decoded pictures by N-1 frames.
10
+The later frames are decoded in separate threads while the user is
11
+displaying the current one.
12
+
13
+Restrictions on clients
14
+==============================================
15
+
16
+Slice threading -
17
+* The client's draw_horiz_band() must be thread-safe according to the comment
18
+  in avcodec.h.
19
+
20
+Frame threading -
21
+* Restrictions with slice threading also apply.
22
+* For best performance, the client should set thread_safe_callbacks if it
23
+  provides a thread-safe get_buffer() callback.
24
+* There is one frame of delay added for every thread beyond the first one.
25
+  Clients must be able to handle this; the pkt_dts and pkt_pts fields in
26
+  AVFrame will work as usual.
27
+
28
+Restrictions on codec implementations
29
+==============================================
30
+
31
+Slice threading -
32
+ None except that there must be something worth executing in parallel.
33
+
34
+Frame threading -
35
+* Codecs can only accept entire pictures per packet.
36
+* Codecs similar to ffv1, whose streams don't reset across frames,
37
+  will not work because their bitstreams cannot be decoded in parallel.
38
+
39
+* The contents of buffers must not be read before ff_thread_await_progress()
40
+  has been called on them. reget_buffer() and buffer age optimizations no longer work.
41
+* The contents of buffers must not be written to after ff_thread_report_progress()
42
+  has been called on them. This includes draw_edges().
43
+
44
+Porting codecs to frame threading
45
+==============================================
46
+
47
+Find all context variables that are needed by the next frame. Move all
48
+code changing them, as well as code calling get_buffer(), up to before
49
+the decode process starts. Call ff_thread_finish_setup() afterwards. If
50
+some code can't be moved, have update_thread_context() run it in the next
51
+thread.
52
+
53
+If the codec allocates writable tables in its init(), add an init_thread_copy()
54
+which re-allocates them for other threads.
55
+
56
+Add CODEC_CAP_FRAME_THREADS to the codec capabilities. There will be very little
57
+speed gain at this point but it should work.
58
+
59
+Call ff_thread_report_progress() after some part of the current picture has decoded.
60
+A good place to put this is where draw_horiz_band() is called - add this if it isn't
61
+called anywhere, as it's useful too and the implementation is trivial when you're
62
+doing this. Note that draw_edges() needs to be called before reporting progress.
63
+
64
+Before accessing a reference frame or its MVs, call ff_thread_await_progress().
... ...
@@ -1694,6 +1694,7 @@ static int input_init(AVFilterContext *ctx, const char *args, void *opaque)
1694 1694
         codec->get_buffer     = input_get_buffer;
1695 1695
         codec->release_buffer = input_release_buffer;
1696 1696
         codec->reget_buffer   = input_reget_buffer;
1697
+        codec->thread_safe_callbacks = 1;
1697 1698
     }
1698 1699
 
1699 1700
     priv->frame = avcodec_alloc_frame();
... ...
@@ -32,7 +32,7 @@
32 32
 #include "libavutil/cpu.h"
33 33
 
34 34
 #define LIBAVCODEC_VERSION_MAJOR 52
35
-#define LIBAVCODEC_VERSION_MINOR 110
35
+#define LIBAVCODEC_VERSION_MINOR 111
36 36
 #define LIBAVCODEC_VERSION_MICRO  0
37 37
 
38 38
 #define LIBAVCODEC_VERSION_INT  AV_VERSION_INT(LIBAVCODEC_VERSION_MAJOR, \
... ...
@@ -728,6 +728,10 @@ typedef struct RcOverride{
728 728
  * Codec is able to deal with negative linesizes
729 729
  */
730 730
 #define CODEC_CAP_NEG_LINESIZES    0x0800
731
+/**
732
+ * Codec supports frame-level multithreading.
733
+ */
734
+#define CODEC_CAP_FRAME_THREADS    0x1000
731 735
 
732 736
 //The following defines may change, don't expect compatibility if you use them.
733 737
 #define MB_TYPE_INTRA4x4   0x0001
... ...
@@ -1027,7 +1031,20 @@ typedef struct AVPanScan{
1027 1027
      * - decoding: Read by user.\
1028 1028
      */\
1029 1029
     int64_t pkt_dts;\
1030
-
1030
+\
1031
+    /**\
1032
+     * the AVCodecContext which ff_thread_get_buffer() was last called on\
1033
+     * - encoding: Set by libavcodec.\
1034
+     * - decoding: Set by libavcodec.\
1035
+     */\
1036
+    struct AVCodecContext *owner;\
1037
+\
1038
+    /**\
1039
+     * used by multithreading to store frame-specific info\
1040
+     * - encoding: Set by libavcodec.\
1041
+     * - decoding: Set by libavcodec.\
1042
+     */\
1043
+    void *thread_opaque;
1031 1044
 
1032 1045
 #define FF_QSCALE_TYPE_MPEG1 0
1033 1046
 #define FF_QSCALE_TYPE_MPEG2 1
... ...
@@ -1239,6 +1256,10 @@ typedef struct AVCodecContext {
1239 1239
      * decoder to draw a horizontal band. It improves cache usage. Not
1240 1240
      * all codecs can do that. You must check the codec capabilities
1241 1241
      * beforehand.
1242
+     * When multithreading is used, it may be called from multiple threads
1243
+     * at the same time; threads might draw different parts of the same AVFrame,
1244
+     * or multiple AVFrames, and there is no guarantee that slices will be drawn
1245
+     * in order.
1242 1246
      * The function is also used by hardware acceleration APIs.
1243 1247
      * It is called at least once during frame decoding to pass
1244 1248
      * the data needed for hardware render.
... ...
@@ -1492,6 +1513,9 @@ typedef struct AVCodecContext {
1492 1492
      * if CODEC_CAP_DR1 is not set then get_buffer() must call
1493 1493
      * avcodec_default_get_buffer() instead of providing buffers allocated by
1494 1494
      * some other means.
1495
+     * If frame multithreading is used and thread_safe_callbacks is set,
1496
+     * it may be called from a different thread, but not from more than one at once.
1497
+     * Does not need to be reentrant.
1495 1498
      * - encoding: unused
1496 1499
      * - decoding: Set by libavcodec, user can override.
1497 1500
      */
... ...
@@ -1501,6 +1525,8 @@ typedef struct AVCodecContext {
1501 1501
      * Called to release buffers which were allocated with get_buffer.
1502 1502
      * A released buffer can be reused in get_buffer().
1503 1503
      * pic.data[*] must be set to NULL.
1504
+     * May be called from a different thread if frame multithreading is used,
1505
+     * but not by more than one thread at once, so does not need to be reentrant.
1504 1506
      * - encoding: unused
1505 1507
      * - decoding: Set by libavcodec, user can override.
1506 1508
      */
... ...
@@ -1804,6 +1830,7 @@ typedef struct AVCodecContext {
1804 1804
 #define FF_DEBUG_VIS_QP      0x00002000
1805 1805
 #define FF_DEBUG_VIS_MB_TYPE 0x00004000
1806 1806
 #define FF_DEBUG_BUFFERS     0x00008000
1807
+#define FF_DEBUG_THREADS     0x00010000
1807 1808
 
1808 1809
     /**
1809 1810
      * debug
... ...
@@ -2827,6 +2854,44 @@ typedef struct AVCodecContext {
2827 2827
      * - encoding: unused
2828 2828
      */
2829 2829
     AVPacket *pkt;
2830
+
2831
+    /**
2832
+     * Whether this is a copy of the context which had init() called on it.
2833
+     * This is used by multithreading - shared tables and picture pointers
2834
+     * should be freed from the original context only.
2835
+     * - encoding: Set by libavcodec.
2836
+     * - decoding: Set by libavcodec.
2837
+     */
2838
+    int is_copy;
2839
+
2840
+    /**
2841
+     * Which multithreading methods to use.
2842
+     * Use of FF_THREAD_FRAME will increase decoding delay by one frame per thread,
2843
+     * so clients which cannot provide future frames should not use it.
2844
+     *
2845
+     * - encoding: Set by user, otherwise the default is used.
2846
+     * - decoding: Set by user, otherwise the default is used.
2847
+     */
2848
+    int thread_type;
2849
+#define FF_THREAD_FRAME   1 //< Decode more than one frame at once
2850
+#define FF_THREAD_SLICE   2 //< Decode more than one part of a single frame at once
2851
+
2852
+    /**
2853
+     * Which multithreading methods are in use by the codec.
2854
+     * - encoding: Set by libavcodec.
2855
+     * - decoding: Set by libavcodec.
2856
+     */
2857
+    int active_thread_type;
2858
+
2859
+    /**
2860
+     * Set by the client if its custom get_buffer() callback can be called
2861
+     * from another thread, which allows faster multithreaded decoding.
2862
+     * draw_horiz_band() will be called from other threads regardless of this setting.
2863
+     * Ignored if the default get_buffer() is used.
2864
+     * - encoding: Set by user.
2865
+     * - decoding: Set by user.
2866
+     */
2867
+    int thread_safe_callbacks;
2830 2868
 } AVCodecContext;
2831 2869
 
2832 2870
 /**
... ...
@@ -2879,6 +2944,26 @@ typedef struct AVCodec {
2879 2879
     uint8_t max_lowres;                     ///< maximum value for lowres supported by the decoder
2880 2880
     AVClass *priv_class;                    ///< AVClass for the private context
2881 2881
     const AVProfile *profiles;              ///< array of recognized profiles, or NULL if unknown, array is terminated by {FF_PROFILE_UNKNOWN}
2882
+
2883
+    /**
2884
+     * @defgroup framethreading Frame-level threading support functions.
2885
+     * @{
2886
+     */
2887
+    /**
2888
+     * If defined, called on thread contexts when they are created.
2889
+     * If the codec allocates writable tables in init(), re-allocate them here.
2890
+     * priv_data will be set to a copy of the original.
2891
+     */
2892
+    int (*init_thread_copy)(AVCodecContext *);
2893
+    /**
2894
+     * Copy necessary context variables from a previous thread context to the current one.
2895
+     * If not defined, the next thread will start automatically; otherwise, the codec
2896
+     * must call ff_thread_finish_setup().
2897
+     *
2898
+     * dst and src will (rarely) point to the same context, in which case memcpy should be skipped.
2899
+     */
2900
+    int (*update_thread_context)(AVCodecContext *dst, const AVCodecContext *src);
2901
+    /** @} */
2882 2902
 } AVCodec;
2883 2903
 
2884 2904
 /**
... ...
@@ -250,6 +250,7 @@ static const AVOption options[]={
250 250
 {"vis_qp", "visualize quantization parameter (QP), lower QP are tinted greener", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_QP, INT_MIN, INT_MAX, V|D, "debug"},
251 251
 {"vis_mb_type", "visualize block types", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_MB_TYPE, INT_MIN, INT_MAX, V|D, "debug"},
252 252
 {"buffers", "picture buffer allocations", 0, FF_OPT_TYPE_CONST, FF_DEBUG_BUFFERS, INT_MIN, INT_MAX, V|D, "debug"},
253
+{"thread_ops", "threading operations", 0, FF_OPT_TYPE_CONST, FF_DEBUG_THREADS, INT_MIN, INT_MAX, V|D, "debug"},
253 254
 {"vismv", "visualize motion vectors (MVs)", OFFSET(debug_mv), FF_OPT_TYPE_INT, DEFAULT, 0, INT_MAX, V|D, "debug_mv"},
254 255
 {"pf", "forward predicted MVs of P-frames", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_MV_P_FOR, INT_MIN, INT_MAX, V|D, "debug_mv"},
255 256
 {"bf", "forward predicted MVs of B-frames", 0, FF_OPT_TYPE_CONST, FF_DEBUG_VIS_MV_B_FOR, INT_MIN, INT_MAX, V|D, "debug_mv"},
... ...
@@ -431,6 +432,9 @@ static const AVOption options[]={
431 431
 {"cholesky", NULL, 0, FF_OPT_TYPE_CONST, AV_LPC_TYPE_CHOLESKY, INT_MIN, INT_MAX, A|E, "lpc_type"},
432 432
 {"lpc_passes", "number of passes to use for Cholesky factorization during LPC analysis", OFFSET(lpc_passes), FF_OPT_TYPE_INT, -1, INT_MIN, INT_MAX, A|E},
433 433
 {"slices", "number of slices, used in parallelized decoding", OFFSET(slices), FF_OPT_TYPE_INT, 0, 0, INT_MAX, V|E},
434
+{"thread_type", "select multithreading type", OFFSET(thread_type), FF_OPT_TYPE_INT, FF_THREAD_SLICE|FF_THREAD_FRAME, 0, INT_MAX, V|E|D, "thread_type"},
435
+{"slice", NULL, 0, FF_OPT_TYPE_CONST, FF_THREAD_SLICE, INT_MIN, INT_MAX, V|E|D, "thread_type"},
436
+{"frame", NULL, 0, FF_OPT_TYPE_CONST, FF_THREAD_FRAME, INT_MIN, INT_MAX, V|E|D, "thread_type"},
434 437
 {NULL},
435 438
 };
436 439
 
... ...
@@ -1,5 +1,6 @@
1 1
 /*
2 2
  * Copyright (c) 2004 Roman Shaposhnik
3
+ * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
3 4
  *
4 5
  * Many thanks to Steven M. Schultz for providing clever ideas and
5 6
  * to Michael Niedermayer <michaelni@gmx.at> for writing initial
... ...
@@ -21,9 +22,17 @@
21 21
  * License along with FFmpeg; if not, write to the Free Software
22 22
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 23
  */
24
+
25
+/**
26
+ * @file
27
+ * Multithreading support functions
28
+ * @see doc/multithreading.txt
29
+ */
30
+
24 31
 #include <pthread.h>
25 32
 
26 33
 #include "avcodec.h"
34
+#include "thread.h"
27 35
 
28 36
 typedef int (action_func)(AVCodecContext *c, void *arg);
29 37
 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
... ...
@@ -45,6 +54,78 @@ typedef struct ThreadContext {
45 45
     int done;
46 46
 } ThreadContext;
47 47
 
48
+/// Max number of frame buffers that can be allocated when using frame threads.
49
+#define MAX_BUFFERS 32
50
+
51
+/**
52
+ * Context used by codec threads and stored in their AVCodecContext thread_opaque.
53
+ */
54
+typedef struct PerThreadContext {
55
+    struct FrameThreadContext *parent;
56
+
57
+    pthread_t      thread;
58
+    pthread_cond_t input_cond;      ///< Used to wait for a new packet from the main thread.
59
+    pthread_cond_t progress_cond;   ///< Used by child threads to wait for progress to change.
60
+    pthread_cond_t output_cond;     ///< Used by the main thread to wait for frames to finish.
61
+
62
+    pthread_mutex_t mutex;          ///< Mutex used to protect the contents of the PerThreadContext.
63
+    pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
64
+
65
+    AVCodecContext *avctx;          ///< Context used to decode packets passed to this thread.
66
+
67
+    AVPacket       avpkt;           ///< Input packet (for decoding) or output (for encoding).
68
+    int            allocated_buf_size; ///< Size allocated for avpkt.data
69
+
70
+    AVFrame frame;                  ///< Output frame (for decoding) or input (for encoding).
71
+    int     got_frame;              ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
72
+    int     result;                 ///< The result of the last codec decode/encode() call.
73
+
74
+    enum {
75
+        STATE_INPUT_READY,          ///< Set when the thread is awaiting a packet.
76
+        STATE_SETTING_UP,           ///< Set before the codec has called ff_thread_finish_setup().
77
+        STATE_GET_BUFFER,           /**<
78
+                                     * Set when the codec calls get_buffer().
79
+                                     * State is returned to STATE_SETTING_UP afterwards.
80
+                                     */
81
+        STATE_SETUP_FINISHED        ///< Set after the codec has called ff_thread_finish_setup().
82
+    } state;
83
+
84
+    /**
85
+     * Array of frames passed to ff_thread_release_buffer().
86
+     * Frames are released after all threads referencing them are finished.
87
+     */
88
+    AVFrame released_buffers[MAX_BUFFERS];
89
+    int     num_released_buffers;
90
+
91
+    /**
92
+     * Array of progress values used by ff_thread_get_buffer().
93
+     */
94
+    int     progress[MAX_BUFFERS][2];
95
+    uint8_t progress_used[MAX_BUFFERS];
96
+
97
+    AVFrame *requested_frame;       ///< AVFrame the codec passed to get_buffer()
98
+} PerThreadContext;
99
+
100
+/**
101
+ * Context stored in the client AVCodecContext thread_opaque.
102
+ */
103
+typedef struct FrameThreadContext {
104
+    PerThreadContext *threads;     ///< The contexts for each thread.
105
+    PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
106
+
107
+    pthread_mutex_t buffer_mutex;  ///< Mutex used to protect get/release_buffer().
108
+
109
+    int next_decoding;             ///< The next context to submit a packet to.
110
+    int next_finished;             ///< The next context to return output from.
111
+
112
+    int delaying;                  /**<
113
+                                    * Set for the first N packets, where N is the number of threads.
114
+                                    * While it is set, ff_thread_en/decode_frame won't return any results.
115
+                                    */
116
+
117
+    int die;                       ///< Set when threads should exit.
118
+} FrameThreadContext;
119
+
48 120
 static void* attribute_align_arg worker(void *v)
49 121
 {
50 122
     AVCodecContext *avctx = v;
... ...
@@ -84,7 +165,7 @@ static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int t
84 84
     pthread_mutex_unlock(&c->current_job_lock);
85 85
 }
86 86
 
87
-void avcodec_thread_free(AVCodecContext *avctx)
87
+static void thread_free(AVCodecContext *avctx)
88 88
 {
89 89
     ThreadContext *c = avctx->thread_opaque;
90 90
     int i;
... ...
@@ -109,6 +190,9 @@ static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void
109 109
     ThreadContext *c= avctx->thread_opaque;
110 110
     int dummy_ret;
111 111
 
112
+    if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
113
+        return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
114
+
112 115
     if (job_count <= 0)
113 116
         return 0;
114 117
 
... ...
@@ -140,12 +224,11 @@ static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, v
140 140
     return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
141 141
 }
142 142
 
143
-int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
143
+static int thread_init(AVCodecContext *avctx)
144 144
 {
145 145
     int i;
146 146
     ThreadContext *c;
147
-
148
-    avctx->thread_count = thread_count;
147
+    int thread_count = avctx->thread_count;
149 148
 
150 149
     if (thread_count <= 1)
151 150
         return 0;
... ...
@@ -184,3 +267,634 @@ int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
184 184
     avctx->execute2 = avcodec_thread_execute2;
185 185
     return 0;
186 186
 }
187
+
188
+/**
189
+ * Codec worker thread.
190
+ *
191
+ * Automatically calls ff_thread_finish_setup() if the codec does
192
+ * not provide an update_thread_context method, or if the codec returns
193
+ * before calling it.
194
+ */
195
+static attribute_align_arg void *frame_worker_thread(void *arg)
196
+{
197
+    PerThreadContext *p = arg;
198
+    FrameThreadContext *fctx = p->parent;
199
+    AVCodecContext *avctx = p->avctx;
200
+    AVCodec *codec = avctx->codec;
201
+
202
+    while (1) {
203
+        if (p->state == STATE_INPUT_READY && !fctx->die) {
204
+            pthread_mutex_lock(&p->mutex);
205
+            while (p->state == STATE_INPUT_READY && !fctx->die)
206
+                pthread_cond_wait(&p->input_cond, &p->mutex);
207
+            pthread_mutex_unlock(&p->mutex);
208
+        }
209
+
210
+        if (fctx->die) break;
211
+
212
+        if (!codec->update_thread_context) ff_thread_finish_setup(avctx);
213
+
214
+        pthread_mutex_lock(&p->mutex);
215
+        avcodec_get_frame_defaults(&p->frame);
216
+        p->got_frame = 0;
217
+        p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
218
+
219
+        if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
220
+
221
+        p->state = STATE_INPUT_READY;
222
+
223
+        pthread_mutex_lock(&p->progress_mutex);
224
+        pthread_cond_signal(&p->output_cond);
225
+        pthread_mutex_unlock(&p->progress_mutex);
226
+
227
+        pthread_mutex_unlock(&p->mutex);
228
+    }
229
+
230
+    return NULL;
231
+}
232
+
233
+/**
234
+ * Updates the next thread's AVCodecContext with values from the reference thread's context.
235
+ *
236
+ * @param dst The destination context.
237
+ * @param src The source context.
238
+ * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
239
+ */
240
+static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
241
+{
242
+    int err = 0;
243
+
244
+    if (dst != src) {
245
+        dst->sub_id    = src->sub_id;
246
+        dst->time_base = src->time_base;
247
+        dst->width     = src->width;
248
+        dst->height    = src->height;
249
+        dst->pix_fmt   = src->pix_fmt;
250
+
251
+        dst->has_b_frames = src->has_b_frames;
252
+        dst->idct_algo    = src->idct_algo;
253
+        dst->slice_count  = src->slice_count;
254
+
255
+        dst->bits_per_coded_sample = src->bits_per_coded_sample;
256
+        dst->sample_aspect_ratio   = src->sample_aspect_ratio;
257
+        dst->dtg_active_format     = src->dtg_active_format;
258
+
259
+        dst->profile = src->profile;
260
+        dst->level   = src->level;
261
+
262
+        dst->bits_per_raw_sample = src->bits_per_raw_sample;
263
+        dst->ticks_per_frame     = src->ticks_per_frame;
264
+        dst->color_primaries     = src->color_primaries;
265
+
266
+        dst->color_trc   = src->color_trc;
267
+        dst->colorspace  = src->colorspace;
268
+        dst->color_range = src->color_range;
269
+        dst->chroma_sample_location = src->chroma_sample_location;
270
+    }
271
+
272
+    if (for_user) {
273
+        dst->coded_frame   = src->coded_frame;
274
+        dst->has_b_frames += src->thread_count - 1;
275
+    } else {
276
+        if (dst->codec->update_thread_context)
277
+            err = dst->codec->update_thread_context(dst, src);
278
+    }
279
+
280
+    return err;
281
+}
282
+
283
+/**
284
+ * Update the next thread's AVCodecContext with values set by the user.
285
+ *
286
+ * @param dst The destination context.
287
+ * @param src The source context.
288
+ */
289
+static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
290
+{
291
+#define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
292
+    dst->flags          = src->flags;
293
+
294
+    dst->draw_horiz_band= src->draw_horiz_band;
295
+    dst->get_buffer     = src->get_buffer;
296
+    dst->release_buffer = src->release_buffer;
297
+
298
+    dst->opaque   = src->opaque;
299
+    dst->hurry_up = src->hurry_up;
300
+    dst->dsp_mask = src->dsp_mask;
301
+    dst->debug    = src->debug;
302
+    dst->debug_mv = src->debug_mv;
303
+
304
+    dst->slice_flags = src->slice_flags;
305
+    dst->flags2      = src->flags2;
306
+
307
+    copy_fields(skip_loop_filter, bidir_refine);
308
+
309
+    dst->frame_number     = src->frame_number;
310
+    dst->reordered_opaque = src->reordered_opaque;
311
+#undef copy_fields
312
+}
313
+
314
+static void free_progress(AVFrame *f)
315
+{
316
+    PerThreadContext *p = f->owner->thread_opaque;
317
+    int *progress = f->thread_opaque;
318
+
319
+    p->progress_used[(progress - p->progress[0]) / 2] = 0;
320
+}
321
+
322
+/// Releases the buffers that this decoding thread was the last user of.
323
+static void release_delayed_buffers(PerThreadContext *p)
324
+{
325
+    FrameThreadContext *fctx = p->parent;
326
+
327
+    while (p->num_released_buffers > 0) {
328
+        AVFrame *f = &p->released_buffers[--p->num_released_buffers];
329
+
330
+        pthread_mutex_lock(&fctx->buffer_mutex);
331
+        free_progress(f);
332
+        f->thread_opaque = NULL;
333
+
334
+        f->owner->release_buffer(f->owner, f);
335
+        pthread_mutex_unlock(&fctx->buffer_mutex);
336
+    }
337
+}
338
+
339
+static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
340
+{
341
+    FrameThreadContext *fctx = p->parent;
342
+    PerThreadContext *prev_thread = fctx->prev_thread;
343
+    AVCodec *codec = p->avctx->codec;
344
+    uint8_t *buf = p->avpkt.data;
345
+
346
+    if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
347
+
348
+    pthread_mutex_lock(&p->mutex);
349
+
350
+    release_delayed_buffers(p);
351
+
352
+    if (prev_thread) {
353
+        int err;
354
+        if (prev_thread->state == STATE_SETTING_UP) {
355
+            pthread_mutex_lock(&prev_thread->progress_mutex);
356
+            while (prev_thread->state == STATE_SETTING_UP)
357
+                pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
358
+            pthread_mutex_unlock(&prev_thread->progress_mutex);
359
+        }
360
+
361
+        err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
362
+        if (err) {
363
+            pthread_mutex_unlock(&p->mutex);
364
+            return err;
365
+        }
366
+    }
367
+
368
+    av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
369
+    p->avpkt = *avpkt;
370
+    p->avpkt.data = buf;
371
+    memcpy(buf, avpkt->data, avpkt->size);
372
+    memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
373
+
374
+    p->state = STATE_SETTING_UP;
375
+    pthread_cond_signal(&p->input_cond);
376
+    pthread_mutex_unlock(&p->mutex);
377
+
378
+    /*
379
+     * If the client doesn't have a thread-safe get_buffer(),
380
+     * then decoding threads call back to the main thread,
381
+     * and it calls back to the client here.
382
+     */
383
+
384
+    if (!p->avctx->thread_safe_callbacks &&
385
+         p->avctx->get_buffer != avcodec_default_get_buffer) {
386
+        while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
387
+            pthread_mutex_lock(&p->progress_mutex);
388
+            while (p->state == STATE_SETTING_UP)
389
+                pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
390
+
391
+            if (p->state == STATE_GET_BUFFER) {
392
+                p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
393
+                p->state  = STATE_SETTING_UP;
394
+                pthread_cond_signal(&p->progress_cond);
395
+            }
396
+            pthread_mutex_unlock(&p->progress_mutex);
397
+        }
398
+    }
399
+
400
+    fctx->prev_thread = p;
401
+
402
+    return 0;
403
+}
404
+
405
+int ff_thread_decode_frame(AVCodecContext *avctx,
406
+                           AVFrame *picture, int *got_picture_ptr,
407
+                           AVPacket *avpkt)
408
+{
409
+    FrameThreadContext *fctx = avctx->thread_opaque;
410
+    int finished = fctx->next_finished;
411
+    PerThreadContext *p;
412
+    int err;
413
+
414
+    /*
415
+     * Submit a packet to the next decoding thread.
416
+     */
417
+
418
+    p = &fctx->threads[fctx->next_decoding];
419
+    update_context_from_user(p->avctx, avctx);
420
+    err = submit_packet(p, avpkt);
421
+    if (err) return err;
422
+
423
+    fctx->next_decoding++;
424
+
425
+    /*
426
+     * If we're still receiving the initial packets, don't return a frame.
427
+     */
428
+
429
+    if (fctx->delaying && avpkt->size) {
430
+        if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
431
+
432
+        *got_picture_ptr=0;
433
+        return 0;
434
+    }
435
+
436
+    /*
437
+     * Return the next available frame from the oldest thread.
438
+     * If we're at the end of the stream, then we have to skip threads that
439
+     * didn't output a frame, because we don't want to accidentally signal
440
+     * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
441
+     */
442
+
443
+    do {
444
+        p = &fctx->threads[finished++];
445
+
446
+        if (p->state != STATE_INPUT_READY) {
447
+            pthread_mutex_lock(&p->progress_mutex);
448
+            while (p->state != STATE_INPUT_READY)
449
+                pthread_cond_wait(&p->output_cond, &p->progress_mutex);
450
+            pthread_mutex_unlock(&p->progress_mutex);
451
+        }
452
+
453
+        *picture = p->frame;
454
+        *got_picture_ptr = p->got_frame;
455
+        picture->pkt_dts = p->avpkt.dts;
456
+
457
+        /*
458
+         * A later call with avkpt->size == 0 may loop over all threads,
459
+         * including this one, searching for a frame to return before being
460
+         * stopped by the "finished != fctx->next_finished" condition.
461
+         * Make sure we don't mistakenly return the same frame again.
462
+         */
463
+        p->got_frame = 0;
464
+
465
+        if (finished >= avctx->thread_count) finished = 0;
466
+    } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
467
+
468
+    update_context_from_thread(avctx, p->avctx, 1);
469
+
470
+    if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
471
+
472
+    fctx->next_finished = finished;
473
+
474
+    return p->result;
475
+}
476
+
477
+void ff_thread_report_progress(AVFrame *f, int n, int field)
478
+{
479
+    PerThreadContext *p;
480
+    int *progress = f->thread_opaque;
481
+
482
+    if (!progress || progress[field] >= n) return;
483
+
484
+    p = f->owner->thread_opaque;
485
+
486
+    if (f->owner->debug&FF_DEBUG_THREADS)
487
+        av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
488
+
489
+    pthread_mutex_lock(&p->progress_mutex);
490
+    progress[field] = n;
491
+    pthread_cond_broadcast(&p->progress_cond);
492
+    pthread_mutex_unlock(&p->progress_mutex);
493
+}
494
+
495
+void ff_thread_await_progress(AVFrame *f, int n, int field)
496
+{
497
+    PerThreadContext *p;
498
+    int *progress = f->thread_opaque;
499
+
500
+    if (!progress || progress[field] >= n) return;
501
+
502
+    p = f->owner->thread_opaque;
503
+
504
+    if (f->owner->debug&FF_DEBUG_THREADS)
505
+        av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
506
+
507
+    pthread_mutex_lock(&p->progress_mutex);
508
+    while (progress[field] < n)
509
+        pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
510
+    pthread_mutex_unlock(&p->progress_mutex);
511
+}
512
+
513
+void ff_thread_finish_setup(AVCodecContext *avctx) {
514
+    PerThreadContext *p = avctx->thread_opaque;
515
+
516
+    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
517
+
518
+    pthread_mutex_lock(&p->progress_mutex);
519
+    p->state = STATE_SETUP_FINISHED;
520
+    pthread_cond_broadcast(&p->progress_cond);
521
+    pthread_mutex_unlock(&p->progress_mutex);
522
+}
523
+
524
+/// Waits for all threads to finish.
525
+static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
526
+{
527
+    int i;
528
+
529
+    for (i = 0; i < thread_count; i++) {
530
+        PerThreadContext *p = &fctx->threads[i];
531
+
532
+        if (p->state != STATE_INPUT_READY) {
533
+            pthread_mutex_lock(&p->progress_mutex);
534
+            while (p->state != STATE_INPUT_READY)
535
+                pthread_cond_wait(&p->output_cond, &p->progress_mutex);
536
+            pthread_mutex_unlock(&p->progress_mutex);
537
+        }
538
+    }
539
+}
540
+
541
+static void frame_thread_free(AVCodecContext *avctx, int thread_count)
542
+{
543
+    FrameThreadContext *fctx = avctx->thread_opaque;
544
+    AVCodec *codec = avctx->codec;
545
+    int i;
546
+
547
+    park_frame_worker_threads(fctx, thread_count);
548
+
549
+    if (fctx->prev_thread)
550
+        update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
551
+
552
+    fctx->die = 1;
553
+
554
+    for (i = 0; i < thread_count; i++) {
555
+        PerThreadContext *p = &fctx->threads[i];
556
+
557
+        pthread_mutex_lock(&p->mutex);
558
+        pthread_cond_signal(&p->input_cond);
559
+        pthread_mutex_unlock(&p->mutex);
560
+
561
+        pthread_join(p->thread, NULL);
562
+
563
+        if (codec->close)
564
+            codec->close(p->avctx);
565
+
566
+        avctx->codec = NULL;
567
+
568
+        release_delayed_buffers(p);
569
+    }
570
+
571
+    for (i = 0; i < thread_count; i++) {
572
+        PerThreadContext *p = &fctx->threads[i];
573
+
574
+        avcodec_default_free_buffers(p->avctx);
575
+
576
+        pthread_mutex_destroy(&p->mutex);
577
+        pthread_mutex_destroy(&p->progress_mutex);
578
+        pthread_cond_destroy(&p->input_cond);
579
+        pthread_cond_destroy(&p->progress_cond);
580
+        pthread_cond_destroy(&p->output_cond);
581
+        av_freep(&p->avpkt.data);
582
+
583
+        if (i)
584
+            av_freep(&p->avctx->priv_data);
585
+
586
+        av_freep(&p->avctx);
587
+    }
588
+
589
+    av_freep(&fctx->threads);
590
+    pthread_mutex_destroy(&fctx->buffer_mutex);
591
+    av_freep(&avctx->thread_opaque);
592
+}
593
+
594
+static int frame_thread_init(AVCodecContext *avctx)
595
+{
596
+    int thread_count = avctx->thread_count;
597
+    AVCodec *codec = avctx->codec;
598
+    AVCodecContext *src = avctx;
599
+    FrameThreadContext *fctx;
600
+    int i, err = 0;
601
+
602
+    avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
603
+
604
+    fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
605
+    pthread_mutex_init(&fctx->buffer_mutex, NULL);
606
+    fctx->delaying = 1;
607
+
608
+    for (i = 0; i < thread_count; i++) {
609
+        AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
610
+        PerThreadContext *p  = &fctx->threads[i];
611
+
612
+        pthread_mutex_init(&p->mutex, NULL);
613
+        pthread_mutex_init(&p->progress_mutex, NULL);
614
+        pthread_cond_init(&p->input_cond, NULL);
615
+        pthread_cond_init(&p->progress_cond, NULL);
616
+        pthread_cond_init(&p->output_cond, NULL);
617
+
618
+        p->parent = fctx;
619
+        p->avctx  = copy;
620
+
621
+        *copy = *src;
622
+        copy->thread_opaque = p;
623
+        copy->pkt = &p->avpkt;
624
+
625
+        if (!i) {
626
+            src = copy;
627
+
628
+            if (codec->init)
629
+                err = codec->init(copy);
630
+
631
+            update_context_from_thread(avctx, copy, 1);
632
+        } else {
633
+            copy->is_copy   = 1;
634
+            copy->priv_data = av_malloc(codec->priv_data_size);
635
+            memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
636
+
637
+            if (codec->init_thread_copy)
638
+                err = codec->init_thread_copy(copy);
639
+        }
640
+
641
+        if (err) goto error;
642
+
643
+        pthread_create(&p->thread, NULL, frame_worker_thread, p);
644
+    }
645
+
646
+    return 0;
647
+
648
+error:
649
+    frame_thread_free(avctx, i+1);
650
+
651
+    return err;
652
+}
653
+
654
+void ff_thread_flush(AVCodecContext *avctx)
655
+{
656
+    FrameThreadContext *fctx = avctx->thread_opaque;
657
+
658
+    if (!avctx->thread_opaque) return;
659
+
660
+    park_frame_worker_threads(fctx, avctx->thread_count);
661
+
662
+    if (fctx->prev_thread)
663
+        update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
664
+
665
+    fctx->next_decoding = fctx->next_finished = 0;
666
+    fctx->delaying = 1;
667
+    fctx->prev_thread = NULL;
668
+}
669
+
670
+static int *allocate_progress(PerThreadContext *p)
671
+{
672
+    int i;
673
+
674
+    for (i = 0; i < MAX_BUFFERS; i++)
675
+        if (!p->progress_used[i]) break;
676
+
677
+    if (i == MAX_BUFFERS) {
678
+        av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
679
+        return NULL;
680
+    }
681
+
682
+    p->progress_used[i] = 1;
683
+
684
+    return p->progress[i];
685
+}
686
+
687
+int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
688
+{
689
+    PerThreadContext *p = avctx->thread_opaque;
690
+    int *progress, err;
691
+
692
+    f->owner = avctx;
693
+
694
+    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
695
+        f->thread_opaque = NULL;
696
+        return avctx->get_buffer(avctx, f);
697
+    }
698
+
699
+    if (p->state != STATE_SETTING_UP) {
700
+        av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
701
+        return -1;
702
+    }
703
+
704
+    pthread_mutex_lock(&p->parent->buffer_mutex);
705
+    f->thread_opaque = progress = allocate_progress(p);
706
+
707
+    if (!progress) {
708
+        pthread_mutex_unlock(&p->parent->buffer_mutex);
709
+        return -1;
710
+    }
711
+
712
+    progress[0] =
713
+    progress[1] = -1;
714
+
715
+    if (avctx->thread_safe_callbacks ||
716
+        avctx->get_buffer == avcodec_default_get_buffer) {
717
+        err = avctx->get_buffer(avctx, f);
718
+    } else {
719
+        p->requested_frame = f;
720
+        p->state = STATE_GET_BUFFER;
721
+        pthread_mutex_lock(&p->progress_mutex);
722
+        pthread_cond_signal(&p->progress_cond);
723
+
724
+        while (p->state != STATE_SETTING_UP)
725
+            pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
726
+
727
+        err = p->result;
728
+
729
+        pthread_mutex_unlock(&p->progress_mutex);
730
+    }
731
+
732
+    pthread_mutex_unlock(&p->parent->buffer_mutex);
733
+
734
+    /*
735
+     * Buffer age is difficult to keep track of between
736
+     * multiple threads, and the optimizations it allows
737
+     * are not worth the effort. It is disabled for now.
738
+     */
739
+    f->age = INT_MAX;
740
+
741
+    return err;
742
+}
743
+
744
+void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
745
+{
746
+    PerThreadContext *p = avctx->thread_opaque;
747
+
748
+    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
749
+        avctx->release_buffer(avctx, f);
750
+        return;
751
+    }
752
+
753
+    if (p->num_released_buffers >= MAX_BUFFERS) {
754
+        av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
755
+        return;
756
+    }
757
+
758
+    if(avctx->debug & FF_DEBUG_BUFFERS)
759
+        av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
760
+                                    f, f->owner->internal_buffer_count);
761
+
762
+    p->released_buffers[p->num_released_buffers++] = *f;
763
+    memset(f->data, 0, sizeof(f->data));
764
+}
765
+
766
+/**
767
+ * Set the threading algorithms used.
768
+ *
769
+ * Threading requires more than one thread.
770
+ * Frame threading requires entire frames to be passed to the codec,
771
+ * and introduces extra decoding delay, so is incompatible with low_delay.
772
+ *
773
+ * @param avctx The context.
774
+ */
775
+static void validate_thread_parameters(AVCodecContext *avctx)
776
+{
777
+    int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
778
+                                && !(avctx->flags & CODEC_FLAG_TRUNCATED)
779
+                                && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
780
+                                && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
781
+    if (avctx->thread_count == 1) {
782
+        avctx->active_thread_type = 0;
783
+    } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
784
+        avctx->active_thread_type = FF_THREAD_FRAME;
785
+    } else {
786
+        avctx->active_thread_type = FF_THREAD_SLICE;
787
+    }
788
+}
789
+
790
+int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
791
+{
792
+    if (avctx->thread_opaque) {
793
+        av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
794
+        return -1;
795
+    }
796
+
797
+    avctx->thread_count = FFMAX(1, thread_count);
798
+
799
+    if (avctx->codec) {
800
+        validate_thread_parameters(avctx);
801
+
802
+        if (avctx->active_thread_type&FF_THREAD_SLICE)
803
+            return thread_init(avctx);
804
+        else if (avctx->active_thread_type&FF_THREAD_FRAME)
805
+            return frame_thread_init(avctx);
806
+    }
807
+
808
+    return 0;
809
+}
810
+
811
+void avcodec_thread_free(AVCodecContext *avctx)
812
+{
813
+    if (avctx->active_thread_type&FF_THREAD_FRAME)
814
+        frame_thread_free(avctx, avctx->thread_count);
815
+    else
816
+        thread_free(avctx);
817
+}
187 818
new file mode 100644
... ...
@@ -0,0 +1,111 @@
0
+/*
1
+ * Copyright (c) 2008 Alexander Strange <astrange@ithinksw.com>
2
+ *
3
+ * This file is part of FFmpeg.
4
+ *
5
+ * FFmpeg is free software; you can redistribute it and/or
6
+ * modify it under the terms of the GNU Lesser General Public
7
+ * License as published by the Free Software Foundation; either
8
+ * version 2.1 of the License, or (at your option) any later version.
9
+ *
10
+ * FFmpeg is distributed in the hope that it will be useful,
11
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
+ * Lesser General Public License for more details.
14
+ *
15
+ * You should have received a copy of the GNU Lesser General Public
16
+ * License along with FFmpeg; if not, write to the Free Software
17
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18
+ */
19
+
20
+/**
21
+ * @file
22
+ * Multithreading support functions
23
+ * @author Alexander Strange <astrange@ithinksw.com>
24
+ */
25
+
26
+#ifndef AVCODEC_THREAD_H
27
+#define AVCODEC_THREAD_H
28
+
29
+#include "config.h"
30
+#include "avcodec.h"
31
+
32
+/**
33
+ * Waits for decoding threads to finish and resets internal
34
+ * state. Called by avcodec_flush_buffers().
35
+ *
36
+ * @param avctx The context.
37
+ */
38
+void ff_thread_flush(AVCodecContext *avctx);
39
+
40
+/**
41
+ * Submits a new frame to a decoding thread.
42
+ * Returns the next available frame in picture. *got_picture_ptr
43
+ * will be 0 if none is available.
44
+ *
45
+ * Parameters are the same as avcodec_decode_video2().
46
+ */
47
+int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture,
48
+                           int *got_picture_ptr, AVPacket *avpkt);
49
+
50
+/**
51
+ * If the codec defines update_thread_context(), call this
52
+ * when they are ready for the next thread to start decoding
53
+ * the next frame. After calling it, do not change any variables
54
+ * read by the update_thread_context() method, or call ff_thread_get_buffer().
55
+ *
56
+ * @param avctx The context.
57
+ */
58
+void ff_thread_finish_setup(AVCodecContext *avctx);
59
+
60
+/**
61
+ * Notifies later decoding threads when part of their reference picture
62
+ * is ready.
63
+ * Call this when some part of the picture is finished decoding.
64
+ * Later calls with lower values of progress have no effect.
65
+ *
66
+ * @param f The picture being decoded.
67
+ * @param progress Value, in arbitrary units, of how much of the picture has decoded.
68
+ * @param field The field being decoded, for field-picture codecs.
69
+ * 0 for top field or frame pictures, 1 for bottom field.
70
+ */
71
+void ff_thread_report_progress(AVFrame *f, int progress, int field);
72
+
73
+/**
74
+ * Waits for earlier decoding threads to finish reference pictures
75
+ * Call this before accessing some part of a picture, with a given
76
+ * value for progress, and it will return after the responsible decoding
77
+ * thread calls ff_thread_report_progress() with the same or
78
+ * higher value for progress.
79
+ *
80
+ * @param f The picture being referenced.
81
+ * @param progress Value, in arbitrary units, to wait for.
82
+ * @param field The field being referenced, for field-picture codecs.
83
+ * 0 for top field or frame pictures, 1 for bottom field.
84
+ */
85
+void ff_thread_await_progress(AVFrame *f, int progress, int field);
86
+
87
+/**
88
+ * Wrapper around get_buffer() for frame-multithreaded codecs.
89
+ * Call this function instead of avctx->get_buffer(f).
90
+ * Cannot be called after the codec has called ff_thread_finish_setup().
91
+ *
92
+ * @param avctx The current context.
93
+ * @param f The frame to write into.
94
+ */
95
+int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f);
96
+
97
+/**
98
+ * Wrapper around release_buffer() frame-for multithreaded codecs.
99
+ * Call this function instead of avctx->release_buffer(f).
100
+ * The AVFrame will be copied and the actual release_buffer() call
101
+ * will be performed later. The contents of data pointed to by the
102
+ * AVFrame should not be changed until ff_thread_get_buffer() is called
103
+ * on it.
104
+ *
105
+ * @param avctx The current context.
106
+ * @param f The picture being released.
107
+ */
108
+void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f);
109
+
110
+#endif /* AVCODEC_THREAD_H */
... ...
@@ -37,6 +37,7 @@
37 37
 #include "dsputil.h"
38 38
 #include "libavutil/opt.h"
39 39
 #include "imgconvert.h"
40
+#include "thread.h"
40 41
 #include "audioconvert.h"
41 42
 #include "internal.h"
42 43
 #include <stdlib.h>
... ...
@@ -261,6 +262,11 @@ int avcodec_default_get_buffer(AVCodecContext *s, AVFrame *pic){
261 261
     (*picture_number)++;
262 262
 
263 263
     if(buf->base[0] && (buf->width != w || buf->height != h || buf->pix_fmt != s->pix_fmt)){
264
+        if(s->active_thread_type&FF_THREAD_FRAME) {
265
+            av_log_missing_feature(s, "Width/height changing with frame threads is", 0);
266
+            return -1;
267
+        }
268
+
264 269
         for(i=0; i<4; i++){
265 270
             av_freep(&buf->base[i]);
266 271
             buf->data[i]= NULL;
... ...
@@ -532,13 +538,21 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
532 532
         goto free_and_end;
533 533
     }
534 534
     avctx->frame_number = 0;
535
+
536
+    if (HAVE_THREADS && !avctx->thread_opaque) {
537
+        ret = avcodec_thread_init(avctx, avctx->thread_count);
538
+        if (ret < 0) {
539
+            goto free_and_end;
540
+        }
541
+    }
542
+
535 543
     if (avctx->codec->max_lowres < avctx->lowres) {
536 544
         av_log(avctx, AV_LOG_ERROR, "The maximum value for lowres supported by the decoder is %d\n",
537 545
                avctx->codec->max_lowres);
538 546
         goto free_and_end;
539 547
     }
540 548
 
541
-    if(avctx->codec->init){
549
+    if(avctx->codec->init && !(avctx->active_thread_type&FF_THREAD_FRAME)){
542 550
         ret = avctx->codec->init(avctx);
543 551
         if (ret < 0) {
544 552
             goto free_and_end;
... ...
@@ -636,14 +650,18 @@ int attribute_align_arg avcodec_decode_video2(AVCodecContext *avctx, AVFrame *pi
636 636
 
637 637
     avctx->pkt = avpkt;
638 638
 
639
-    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size){
640
-        ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
641
-                                avpkt);
639
+    if((avctx->codec->capabilities & CODEC_CAP_DELAY) || avpkt->size || (avctx->active_thread_type&FF_THREAD_FRAME)){
640
+        if (HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
641
+             ret = ff_thread_decode_frame(avctx, picture, got_picture_ptr,
642
+                                          avpkt);
643
+        else {
644
+            ret = avctx->codec->decode(avctx, picture, got_picture_ptr,
645
+                              avpkt);
646
+            picture->pkt_dts= avpkt->dts;
647
+        }
642 648
 
643 649
         emms_c(); //needed to avoid an emms_c() call before every return;
644 650
 
645
-        picture->pkt_dts= avpkt->dts;
646
-
647 651
         if (*got_picture_ptr)
648 652
             avctx->frame_number++;
649 653
     }else
... ...
@@ -768,6 +786,7 @@ av_cold int avcodec_close(AVCodecContext *avctx)
768 768
     if(avctx->codec && avctx->codec->encode)
769 769
         av_freep(&avctx->extradata);
770 770
     avctx->codec = NULL;
771
+    avctx->active_thread_type = 0;
771 772
     entangled_thread_counter--;
772 773
 
773 774
     /* Release any user-supplied mutex. */
... ...
@@ -1029,6 +1048,8 @@ void avcodec_init(void)
1029 1029
 
1030 1030
 void avcodec_flush_buffers(AVCodecContext *avctx)
1031 1031
 {
1032
+    if(HAVE_PTHREADS && avctx->active_thread_type&FF_THREAD_FRAME)
1033
+        ff_thread_flush(avctx);
1032 1034
     if(avctx->codec->flush)
1033 1035
         avctx->codec->flush(avctx);
1034 1036
 }
... ...
@@ -1229,3 +1250,30 @@ unsigned int ff_toupper4(unsigned int x)
1229 1229
             + (toupper((x>>16)&0xFF)<<16)
1230 1230
             + (toupper((x>>24)&0xFF)<<24);
1231 1231
 }
1232
+
1233
+#if !HAVE_PTHREADS
1234
+
1235
+int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
1236
+{
1237
+    f->owner = avctx;
1238
+    return avctx->get_buffer(avctx, f);
1239
+}
1240
+
1241
+void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
1242
+{
1243
+    f->owner->release_buffer(f->owner, f);
1244
+}
1245
+
1246
+void ff_thread_finish_setup(AVCodecContext *avctx)
1247
+{
1248
+}
1249
+
1250
+void ff_thread_report_progress(AVFrame *f, int progress, int field)
1251
+{
1252
+}
1253
+
1254
+void ff_thread_await_progress(AVFrame *f, int progress, int field)
1255
+{
1256
+}
1257
+
1258
+#endif
... ...
@@ -129,7 +129,13 @@ int avcodec_thread_init(AVCodecContext *s, int thread_count){
129 129
     ThreadContext *c;
130 130
     uint32_t threadid;
131 131
 
132
+    if(!(s->thread_type & FF_THREAD_SLICE)){
133
+        av_log(s, AV_LOG_WARNING, "The requested thread algorithm is not supported with this thread library.\n");
134
+        return 0;
135
+    }
136
+
132 137
     s->thread_count= thread_count;
138
+    s->active_thread_type= FF_THREAD_SLICE;
133 139
 
134 140
     if (thread_count <= 1)
135 141
         return 0;
... ...
@@ -930,6 +930,12 @@ static void compute_pkt_fields(AVFormatContext *s, AVStream *st,
930 930
     /* do we have a video B-frame ? */
931 931
     delay= st->codec->has_b_frames;
932 932
     presentation_delayed = 0;
933
+
934
+    // ignore delay caused by frame threading so that the mpeg2-without-dts
935
+    // warning will not trigger
936
+    if (delay && st->codec->active_thread_type&FF_THREAD_FRAME)
937
+        delay -= st->codec->thread_count-1;
938
+
933 939
     /* XXX: need has_b_frame, but cannot get it if the codec is
934 940
         not initialized */
935 941
     if (delay &&
... ...
@@ -210,4 +210,15 @@
210 210
     type ff_##name args
211 211
 #endif
212 212
 
213
+/**
214
+ * Returns NULL if a threading library has not been enabled.
215
+ * Used to disable threading functions in AVCodec definitions
216
+ * when not needed.
217
+ */
218
+#if HAVE_THREADS
219
+#   define ONLY_IF_THREADS_ENABLED(x) x
220
+#else
221
+#   define ONLY_IF_THREADS_ENABLED(x) NULL
222
+#endif
223
+
213 224
 #endif /* AVUTIL_INTERNAL_H */