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