]> git.sesse.net Git - ffmpeg/blob - libavcodec/pthread.c
0311dcd7e9fb98a4c00ad31d11de66cb53e4dcc8
[ffmpeg] / libavcodec / pthread.c
1 /*
2  * Copyright (c) 2004 Roman Shaposhnik
3  * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
4  *
5  * Many thanks to Steven M. Schultz for providing clever ideas and
6  * to Michael Niedermayer <michaelni@gmx.at> for writing initial
7  * implementation.
8  *
9  * This file is part of Libav.
10  *
11  * Libav is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * Libav is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with Libav; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 /**
27  * @file
28  * Multithreading support functions
29  * @see doc/multithreading.txt
30  */
31
32 #include <pthread.h>
33
34 #include "avcodec.h"
35 #include "thread.h"
36
37 typedef int (action_func)(AVCodecContext *c, void *arg);
38 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
39
40 typedef struct ThreadContext {
41     pthread_t *workers;
42     action_func *func;
43     action_func2 *func2;
44     void *args;
45     int *rets;
46     int rets_count;
47     int job_count;
48     int job_size;
49
50     pthread_cond_t last_job_cond;
51     pthread_cond_t current_job_cond;
52     pthread_mutex_t current_job_lock;
53     int current_job;
54     int done;
55 } ThreadContext;
56
57 /// Max number of frame buffers that can be allocated when using frame threads.
58 #define MAX_BUFFERS 32
59
60 /**
61  * Context used by codec threads and stored in their AVCodecContext thread_opaque.
62  */
63 typedef struct PerThreadContext {
64     struct FrameThreadContext *parent;
65
66     pthread_t      thread;
67     pthread_cond_t input_cond;      ///< Used to wait for a new packet from the main thread.
68     pthread_cond_t progress_cond;   ///< Used by child threads to wait for progress to change.
69     pthread_cond_t output_cond;     ///< Used by the main thread to wait for frames to finish.
70
71     pthread_mutex_t mutex;          ///< Mutex used to protect the contents of the PerThreadContext.
72     pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
73
74     AVCodecContext *avctx;          ///< Context used to decode packets passed to this thread.
75
76     AVPacket       avpkt;           ///< Input packet (for decoding) or output (for encoding).
77     int            allocated_buf_size; ///< Size allocated for avpkt.data
78
79     AVFrame frame;                  ///< Output frame (for decoding) or input (for encoding).
80     int     got_frame;              ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
81     int     result;                 ///< The result of the last codec decode/encode() call.
82
83     enum {
84         STATE_INPUT_READY,          ///< Set when the thread is awaiting a packet.
85         STATE_SETTING_UP,           ///< Set before the codec has called ff_thread_finish_setup().
86         STATE_GET_BUFFER,           /**<
87                                      * Set when the codec calls get_buffer().
88                                      * State is returned to STATE_SETTING_UP afterwards.
89                                      */
90         STATE_SETUP_FINISHED        ///< Set after the codec has called ff_thread_finish_setup().
91     } state;
92
93     /**
94      * Array of frames passed to ff_thread_release_buffer().
95      * Frames are released after all threads referencing them are finished.
96      */
97     AVFrame released_buffers[MAX_BUFFERS];
98     int     num_released_buffers;
99
100     /**
101      * Array of progress values used by ff_thread_get_buffer().
102      */
103     int     progress[MAX_BUFFERS][2];
104     uint8_t progress_used[MAX_BUFFERS];
105
106     AVFrame *requested_frame;       ///< AVFrame the codec passed to get_buffer()
107 } PerThreadContext;
108
109 /**
110  * Context stored in the client AVCodecContext thread_opaque.
111  */
112 typedef struct FrameThreadContext {
113     PerThreadContext *threads;     ///< The contexts for each thread.
114     PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
115
116     pthread_mutex_t buffer_mutex;  ///< Mutex used to protect get/release_buffer().
117
118     int next_decoding;             ///< The next context to submit a packet to.
119     int next_finished;             ///< The next context to return output from.
120
121     int delaying;                  /**<
122                                     * Set for the first N packets, where N is the number of threads.
123                                     * While it is set, ff_thread_en/decode_frame won't return any results.
124                                     */
125
126     int die;                       ///< Set when threads should exit.
127 } FrameThreadContext;
128
129 static void* attribute_align_arg worker(void *v)
130 {
131     AVCodecContext *avctx = v;
132     ThreadContext *c = avctx->thread_opaque;
133     int our_job = c->job_count;
134     int thread_count = avctx->thread_count;
135     int self_id;
136
137     pthread_mutex_lock(&c->current_job_lock);
138     self_id = c->current_job++;
139     for (;;){
140         while (our_job >= c->job_count) {
141             if (c->current_job == thread_count + c->job_count)
142                 pthread_cond_signal(&c->last_job_cond);
143
144             pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
145             our_job = self_id;
146
147             if (c->done) {
148                 pthread_mutex_unlock(&c->current_job_lock);
149                 return NULL;
150             }
151         }
152         pthread_mutex_unlock(&c->current_job_lock);
153
154         c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
155                                                    c->func2(avctx, c->args, our_job, self_id);
156
157         pthread_mutex_lock(&c->current_job_lock);
158         our_job = c->current_job++;
159     }
160 }
161
162 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
163 {
164     pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
165     pthread_mutex_unlock(&c->current_job_lock);
166 }
167
168 static void thread_free(AVCodecContext *avctx)
169 {
170     ThreadContext *c = avctx->thread_opaque;
171     int i;
172
173     pthread_mutex_lock(&c->current_job_lock);
174     c->done = 1;
175     pthread_cond_broadcast(&c->current_job_cond);
176     pthread_mutex_unlock(&c->current_job_lock);
177
178     for (i=0; i<avctx->thread_count; i++)
179          pthread_join(c->workers[i], NULL);
180
181     pthread_mutex_destroy(&c->current_job_lock);
182     pthread_cond_destroy(&c->current_job_cond);
183     pthread_cond_destroy(&c->last_job_cond);
184     av_free(c->workers);
185     av_freep(&avctx->thread_opaque);
186 }
187
188 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
189 {
190     ThreadContext *c= avctx->thread_opaque;
191     int dummy_ret;
192
193     if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
194         return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
195
196     if (job_count <= 0)
197         return 0;
198
199     pthread_mutex_lock(&c->current_job_lock);
200
201     c->current_job = avctx->thread_count;
202     c->job_count = job_count;
203     c->job_size = job_size;
204     c->args = arg;
205     c->func = func;
206     if (ret) {
207         c->rets = ret;
208         c->rets_count = job_count;
209     } else {
210         c->rets = &dummy_ret;
211         c->rets_count = 1;
212     }
213     pthread_cond_broadcast(&c->current_job_cond);
214
215     avcodec_thread_park_workers(c, avctx->thread_count);
216
217     return 0;
218 }
219
220 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
221 {
222     ThreadContext *c= avctx->thread_opaque;
223     c->func2 = func2;
224     return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
225 }
226
227 static int thread_init(AVCodecContext *avctx)
228 {
229     int i;
230     ThreadContext *c;
231     int thread_count = avctx->thread_count;
232
233     if (thread_count <= 1)
234         return 0;
235
236     c = av_mallocz(sizeof(ThreadContext));
237     if (!c)
238         return -1;
239
240     c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
241     if (!c->workers) {
242         av_free(c);
243         return -1;
244     }
245
246     avctx->thread_opaque = c;
247     c->current_job = 0;
248     c->job_count = 0;
249     c->job_size = 0;
250     c->done = 0;
251     pthread_cond_init(&c->current_job_cond, NULL);
252     pthread_cond_init(&c->last_job_cond, NULL);
253     pthread_mutex_init(&c->current_job_lock, NULL);
254     pthread_mutex_lock(&c->current_job_lock);
255     for (i=0; i<thread_count; i++) {
256         if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
257            avctx->thread_count = i;
258            pthread_mutex_unlock(&c->current_job_lock);
259            ff_thread_free(avctx);
260            return -1;
261         }
262     }
263
264     avcodec_thread_park_workers(c, thread_count);
265
266     avctx->execute = avcodec_thread_execute;
267     avctx->execute2 = avcodec_thread_execute2;
268     return 0;
269 }
270
271 /**
272  * Codec worker thread.
273  *
274  * Automatically calls ff_thread_finish_setup() if the codec does
275  * not provide an update_thread_context method, or if the codec returns
276  * before calling it.
277  */
278 static attribute_align_arg void *frame_worker_thread(void *arg)
279 {
280     PerThreadContext *p = arg;
281     FrameThreadContext *fctx = p->parent;
282     AVCodecContext *avctx = p->avctx;
283     AVCodec *codec = avctx->codec;
284
285     while (1) {
286         if (p->state == STATE_INPUT_READY && !fctx->die) {
287             pthread_mutex_lock(&p->mutex);
288             while (p->state == STATE_INPUT_READY && !fctx->die)
289                 pthread_cond_wait(&p->input_cond, &p->mutex);
290             pthread_mutex_unlock(&p->mutex);
291         }
292
293         if (fctx->die) break;
294
295         if (!codec->update_thread_context && avctx->thread_safe_callbacks)
296             ff_thread_finish_setup(avctx);
297
298         pthread_mutex_lock(&p->mutex);
299         avcodec_get_frame_defaults(&p->frame);
300         p->got_frame = 0;
301         p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
302
303         if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
304
305         p->state = STATE_INPUT_READY;
306
307         pthread_mutex_lock(&p->progress_mutex);
308         pthread_cond_signal(&p->output_cond);
309         pthread_mutex_unlock(&p->progress_mutex);
310
311         pthread_mutex_unlock(&p->mutex);
312     }
313
314     return NULL;
315 }
316
317 /**
318  * Updates the next thread's AVCodecContext with values from the reference thread's context.
319  *
320  * @param dst The destination context.
321  * @param src The source context.
322  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
323  */
324 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
325 {
326     int err = 0;
327
328     if (dst != src) {
329         dst->sub_id    = src->sub_id;
330         dst->time_base = src->time_base;
331         dst->width     = src->width;
332         dst->height    = src->height;
333         dst->pix_fmt   = src->pix_fmt;
334
335         dst->has_b_frames = src->has_b_frames;
336         dst->idct_algo    = src->idct_algo;
337         dst->slice_count  = src->slice_count;
338
339         dst->bits_per_coded_sample = src->bits_per_coded_sample;
340         dst->sample_aspect_ratio   = src->sample_aspect_ratio;
341         dst->dtg_active_format     = src->dtg_active_format;
342
343         dst->profile = src->profile;
344         dst->level   = src->level;
345
346         dst->bits_per_raw_sample = src->bits_per_raw_sample;
347         dst->ticks_per_frame     = src->ticks_per_frame;
348         dst->color_primaries     = src->color_primaries;
349
350         dst->color_trc   = src->color_trc;
351         dst->colorspace  = src->colorspace;
352         dst->color_range = src->color_range;
353         dst->chroma_sample_location = src->chroma_sample_location;
354     }
355
356     if (for_user) {
357         dst->coded_frame   = src->coded_frame;
358         dst->has_b_frames += src->thread_count - 1;
359     } else {
360         if (dst->codec->update_thread_context)
361             err = dst->codec->update_thread_context(dst, src);
362     }
363
364     return err;
365 }
366
367 /**
368  * Update the next thread's AVCodecContext with values set by the user.
369  *
370  * @param dst The destination context.
371  * @param src The source context.
372  */
373 static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
374 {
375 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
376     dst->flags          = src->flags;
377
378     dst->draw_horiz_band= src->draw_horiz_band;
379     dst->get_buffer     = src->get_buffer;
380     dst->release_buffer = src->release_buffer;
381
382     dst->opaque   = src->opaque;
383 #if FF_API_HURRY_UP
384     dst->hurry_up = src->hurry_up;
385 #endif
386     dst->dsp_mask = src->dsp_mask;
387     dst->debug    = src->debug;
388     dst->debug_mv = src->debug_mv;
389
390     dst->slice_flags = src->slice_flags;
391     dst->flags2      = src->flags2;
392
393     copy_fields(skip_loop_filter, bidir_refine);
394
395     dst->frame_number     = src->frame_number;
396     dst->reordered_opaque = src->reordered_opaque;
397 #undef copy_fields
398 }
399
400 static void free_progress(AVFrame *f)
401 {
402     PerThreadContext *p = f->owner->thread_opaque;
403     int *progress = f->thread_opaque;
404
405     p->progress_used[(progress - p->progress[0]) / 2] = 0;
406 }
407
408 /// Releases the buffers that this decoding thread was the last user of.
409 static void release_delayed_buffers(PerThreadContext *p)
410 {
411     FrameThreadContext *fctx = p->parent;
412
413     while (p->num_released_buffers > 0) {
414         AVFrame *f = &p->released_buffers[--p->num_released_buffers];
415
416         pthread_mutex_lock(&fctx->buffer_mutex);
417         free_progress(f);
418         f->thread_opaque = NULL;
419
420         f->owner->release_buffer(f->owner, f);
421         pthread_mutex_unlock(&fctx->buffer_mutex);
422     }
423 }
424
425 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
426 {
427     FrameThreadContext *fctx = p->parent;
428     PerThreadContext *prev_thread = fctx->prev_thread;
429     AVCodec *codec = p->avctx->codec;
430     uint8_t *buf = p->avpkt.data;
431
432     if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
433
434     pthread_mutex_lock(&p->mutex);
435
436     release_delayed_buffers(p);
437
438     if (prev_thread) {
439         int err;
440         if (prev_thread->state == STATE_SETTING_UP) {
441             pthread_mutex_lock(&prev_thread->progress_mutex);
442             while (prev_thread->state == STATE_SETTING_UP)
443                 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
444             pthread_mutex_unlock(&prev_thread->progress_mutex);
445         }
446
447         err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
448         if (err) {
449             pthread_mutex_unlock(&p->mutex);
450             return err;
451         }
452     }
453
454     av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
455     p->avpkt = *avpkt;
456     p->avpkt.data = buf;
457     memcpy(buf, avpkt->data, avpkt->size);
458     memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
459
460     p->state = STATE_SETTING_UP;
461     pthread_cond_signal(&p->input_cond);
462     pthread_mutex_unlock(&p->mutex);
463
464     /*
465      * If the client doesn't have a thread-safe get_buffer(),
466      * then decoding threads call back to the main thread,
467      * and it calls back to the client here.
468      */
469
470     if (!p->avctx->thread_safe_callbacks &&
471          p->avctx->get_buffer != avcodec_default_get_buffer) {
472         while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
473             pthread_mutex_lock(&p->progress_mutex);
474             while (p->state == STATE_SETTING_UP)
475                 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
476
477             if (p->state == STATE_GET_BUFFER) {
478                 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
479                 p->state  = STATE_SETTING_UP;
480                 pthread_cond_signal(&p->progress_cond);
481             }
482             pthread_mutex_unlock(&p->progress_mutex);
483         }
484     }
485
486     fctx->prev_thread = p;
487
488     return 0;
489 }
490
491 int ff_thread_decode_frame(AVCodecContext *avctx,
492                            AVFrame *picture, int *got_picture_ptr,
493                            AVPacket *avpkt)
494 {
495     FrameThreadContext *fctx = avctx->thread_opaque;
496     int finished = fctx->next_finished;
497     PerThreadContext *p;
498     int err;
499
500     /*
501      * Submit a packet to the next decoding thread.
502      */
503
504     p = &fctx->threads[fctx->next_decoding];
505     update_context_from_user(p->avctx, avctx);
506     err = submit_packet(p, avpkt);
507     if (err) return err;
508
509     fctx->next_decoding++;
510
511     /*
512      * If we're still receiving the initial packets, don't return a frame.
513      */
514
515     if (fctx->delaying && avpkt->size) {
516         if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
517
518         *got_picture_ptr=0;
519         return 0;
520     }
521
522     /*
523      * Return the next available frame from the oldest thread.
524      * If we're at the end of the stream, then we have to skip threads that
525      * didn't output a frame, because we don't want to accidentally signal
526      * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
527      */
528
529     do {
530         p = &fctx->threads[finished++];
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         *picture = p->frame;
540         *got_picture_ptr = p->got_frame;
541         picture->pkt_dts = p->avpkt.dts;
542
543         /*
544          * A later call with avkpt->size == 0 may loop over all threads,
545          * including this one, searching for a frame to return before being
546          * stopped by the "finished != fctx->next_finished" condition.
547          * Make sure we don't mistakenly return the same frame again.
548          */
549         p->got_frame = 0;
550
551         if (finished >= avctx->thread_count) finished = 0;
552     } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
553
554     update_context_from_thread(avctx, p->avctx, 1);
555
556     if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
557
558     fctx->next_finished = finished;
559
560     return p->result;
561 }
562
563 void ff_thread_report_progress(AVFrame *f, int n, int field)
564 {
565     PerThreadContext *p;
566     int *progress = f->thread_opaque;
567
568     if (!progress || progress[field] >= n) return;
569
570     p = f->owner->thread_opaque;
571
572     if (f->owner->debug&FF_DEBUG_THREADS)
573         av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
574
575     pthread_mutex_lock(&p->progress_mutex);
576     progress[field] = n;
577     pthread_cond_broadcast(&p->progress_cond);
578     pthread_mutex_unlock(&p->progress_mutex);
579 }
580
581 void ff_thread_await_progress(AVFrame *f, int n, int field)
582 {
583     PerThreadContext *p;
584     int *progress = f->thread_opaque;
585
586     if (!progress || progress[field] >= n) return;
587
588     p = f->owner->thread_opaque;
589
590     if (f->owner->debug&FF_DEBUG_THREADS)
591         av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
592
593     pthread_mutex_lock(&p->progress_mutex);
594     while (progress[field] < n)
595         pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
596     pthread_mutex_unlock(&p->progress_mutex);
597 }
598
599 void ff_thread_finish_setup(AVCodecContext *avctx) {
600     PerThreadContext *p = avctx->thread_opaque;
601
602     if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
603
604     pthread_mutex_lock(&p->progress_mutex);
605     p->state = STATE_SETUP_FINISHED;
606     pthread_cond_broadcast(&p->progress_cond);
607     pthread_mutex_unlock(&p->progress_mutex);
608 }
609
610 /// Waits for all threads to finish.
611 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
612 {
613     int i;
614
615     for (i = 0; i < thread_count; i++) {
616         PerThreadContext *p = &fctx->threads[i];
617
618         if (p->state != STATE_INPUT_READY) {
619             pthread_mutex_lock(&p->progress_mutex);
620             while (p->state != STATE_INPUT_READY)
621                 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
622             pthread_mutex_unlock(&p->progress_mutex);
623         }
624     }
625 }
626
627 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
628 {
629     FrameThreadContext *fctx = avctx->thread_opaque;
630     AVCodec *codec = avctx->codec;
631     int i;
632
633     park_frame_worker_threads(fctx, thread_count);
634
635     if (fctx->prev_thread)
636         update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
637
638     fctx->die = 1;
639
640     for (i = 0; i < thread_count; i++) {
641         PerThreadContext *p = &fctx->threads[i];
642
643         pthread_mutex_lock(&p->mutex);
644         pthread_cond_signal(&p->input_cond);
645         pthread_mutex_unlock(&p->mutex);
646
647         pthread_join(p->thread, NULL);
648
649         if (codec->close)
650             codec->close(p->avctx);
651
652         avctx->codec = NULL;
653
654         release_delayed_buffers(p);
655     }
656
657     for (i = 0; i < thread_count; i++) {
658         PerThreadContext *p = &fctx->threads[i];
659
660         avcodec_default_free_buffers(p->avctx);
661
662         pthread_mutex_destroy(&p->mutex);
663         pthread_mutex_destroy(&p->progress_mutex);
664         pthread_cond_destroy(&p->input_cond);
665         pthread_cond_destroy(&p->progress_cond);
666         pthread_cond_destroy(&p->output_cond);
667         av_freep(&p->avpkt.data);
668
669         if (i)
670             av_freep(&p->avctx->priv_data);
671
672         av_freep(&p->avctx);
673     }
674
675     av_freep(&fctx->threads);
676     pthread_mutex_destroy(&fctx->buffer_mutex);
677     av_freep(&avctx->thread_opaque);
678 }
679
680 static int frame_thread_init(AVCodecContext *avctx)
681 {
682     int thread_count = avctx->thread_count;
683     AVCodec *codec = avctx->codec;
684     AVCodecContext *src = avctx;
685     FrameThreadContext *fctx;
686     int i, err = 0;
687
688     if (thread_count <= 1) {
689         avctx->active_thread_type = 0;
690         return 0;
691     }
692
693     avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
694
695     fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
696     pthread_mutex_init(&fctx->buffer_mutex, NULL);
697     fctx->delaying = 1;
698
699     for (i = 0; i < thread_count; i++) {
700         AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
701         PerThreadContext *p  = &fctx->threads[i];
702
703         pthread_mutex_init(&p->mutex, NULL);
704         pthread_mutex_init(&p->progress_mutex, NULL);
705         pthread_cond_init(&p->input_cond, NULL);
706         pthread_cond_init(&p->progress_cond, NULL);
707         pthread_cond_init(&p->output_cond, NULL);
708
709         p->parent = fctx;
710         p->avctx  = copy;
711
712         *copy = *src;
713         copy->thread_opaque = p;
714         copy->pkt = &p->avpkt;
715
716         if (!i) {
717             src = copy;
718
719             if (codec->init)
720                 err = codec->init(copy);
721
722             update_context_from_thread(avctx, copy, 1);
723         } else {
724             copy->is_copy   = 1;
725             copy->priv_data = av_malloc(codec->priv_data_size);
726             memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
727
728             if (codec->init_thread_copy)
729                 err = codec->init_thread_copy(copy);
730         }
731
732         if (err) goto error;
733
734         pthread_create(&p->thread, NULL, frame_worker_thread, p);
735     }
736
737     return 0;
738
739 error:
740     frame_thread_free(avctx, i+1);
741
742     return err;
743 }
744
745 void ff_thread_flush(AVCodecContext *avctx)
746 {
747     FrameThreadContext *fctx = avctx->thread_opaque;
748
749     if (!avctx->thread_opaque) return;
750
751     park_frame_worker_threads(fctx, avctx->thread_count);
752
753     if (fctx->prev_thread)
754         update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
755
756     fctx->next_decoding = fctx->next_finished = 0;
757     fctx->delaying = 1;
758     fctx->prev_thread = NULL;
759 }
760
761 static int *allocate_progress(PerThreadContext *p)
762 {
763     int i;
764
765     for (i = 0; i < MAX_BUFFERS; i++)
766         if (!p->progress_used[i]) break;
767
768     if (i == MAX_BUFFERS) {
769         av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
770         return NULL;
771     }
772
773     p->progress_used[i] = 1;
774
775     return p->progress[i];
776 }
777
778 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
779 {
780     PerThreadContext *p = avctx->thread_opaque;
781     int *progress, err;
782
783     f->owner = avctx;
784
785     if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
786         f->thread_opaque = NULL;
787         return avctx->get_buffer(avctx, f);
788     }
789
790     if (p->state != STATE_SETTING_UP &&
791         (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
792         av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
793         return -1;
794     }
795
796     pthread_mutex_lock(&p->parent->buffer_mutex);
797     f->thread_opaque = progress = allocate_progress(p);
798
799     if (!progress) {
800         pthread_mutex_unlock(&p->parent->buffer_mutex);
801         return -1;
802     }
803
804     progress[0] =
805     progress[1] = -1;
806
807     if (avctx->thread_safe_callbacks ||
808         avctx->get_buffer == avcodec_default_get_buffer) {
809         err = avctx->get_buffer(avctx, f);
810     } else {
811         p->requested_frame = f;
812         p->state = STATE_GET_BUFFER;
813         pthread_mutex_lock(&p->progress_mutex);
814         pthread_cond_signal(&p->progress_cond);
815
816         while (p->state != STATE_SETTING_UP)
817             pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
818
819         err = p->result;
820
821         pthread_mutex_unlock(&p->progress_mutex);
822
823         if (!avctx->codec->update_thread_context)
824             ff_thread_finish_setup(avctx);
825     }
826
827     pthread_mutex_unlock(&p->parent->buffer_mutex);
828
829     /*
830      * Buffer age is difficult to keep track of between
831      * multiple threads, and the optimizations it allows
832      * are not worth the effort. It is disabled for now.
833      */
834     f->age = INT_MAX;
835
836     return err;
837 }
838
839 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
840 {
841     PerThreadContext *p = avctx->thread_opaque;
842
843     if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
844         avctx->release_buffer(avctx, f);
845         return;
846     }
847
848     if (p->num_released_buffers >= MAX_BUFFERS) {
849         av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
850         return;
851     }
852
853     if(avctx->debug & FF_DEBUG_BUFFERS)
854         av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
855                                     f, f->owner->internal_buffer_count);
856
857     p->released_buffers[p->num_released_buffers++] = *f;
858     memset(f->data, 0, sizeof(f->data));
859 }
860
861 /**
862  * Set the threading algorithms used.
863  *
864  * Threading requires more than one thread.
865  * Frame threading requires entire frames to be passed to the codec,
866  * and introduces extra decoding delay, so is incompatible with low_delay.
867  *
868  * @param avctx The context.
869  */
870 static void validate_thread_parameters(AVCodecContext *avctx)
871 {
872     int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
873                                 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
874                                 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
875                                 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
876     if (avctx->thread_count == 1) {
877         avctx->active_thread_type = 0;
878     } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
879         avctx->active_thread_type = FF_THREAD_FRAME;
880     } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
881                avctx->thread_type & FF_THREAD_SLICE) {
882         avctx->active_thread_type = FF_THREAD_SLICE;
883     }
884 }
885
886 int ff_thread_init(AVCodecContext *avctx)
887 {
888     if (avctx->thread_opaque) {
889         av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
890         return -1;
891     }
892
893     if (avctx->codec) {
894         validate_thread_parameters(avctx);
895
896         if (avctx->active_thread_type&FF_THREAD_SLICE)
897             return thread_init(avctx);
898         else if (avctx->active_thread_type&FF_THREAD_FRAME)
899             return frame_thread_init(avctx);
900     }
901
902     return 0;
903 }
904
905 void ff_thread_free(AVCodecContext *avctx)
906 {
907     if (avctx->active_thread_type&FF_THREAD_FRAME)
908         frame_thread_free(avctx, avctx->thread_count);
909     else
910         thread_free(avctx);
911 }