Revision b38f008e libavcodec/pthread.c

View differences:

libavcodec/pthread.c
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 22
 * License along with FFmpeg; if not, write to the Free Software
22 23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 24
 */
25

  
26
/**
27
 * @file
28
 * Multithreading support functions
29
 * @see doc/multithreading.txt
30
 */
31

  
24 32
#include <pthread.h>
25 33

  
26 34
#include "avcodec.h"
35
#include "thread.h"
27 36

  
28 37
typedef int (action_func)(AVCodecContext *c, void *arg);
29 38
typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
......
45 54
    int done;
46 55
} ThreadContext;
47 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

  
48 129
static void* attribute_align_arg worker(void *v)
49 130
{
50 131
    AVCodecContext *avctx = v;
......
84 165
    pthread_mutex_unlock(&c->current_job_lock);
85 166
}
86 167

  
87
void avcodec_thread_free(AVCodecContext *avctx)
168
static void thread_free(AVCodecContext *avctx)
88 169
{
89 170
    ThreadContext *c = avctx->thread_opaque;
90 171
    int i;
......
109 190
    ThreadContext *c= avctx->thread_opaque;
110 191
    int dummy_ret;
111 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

  
112 196
    if (job_count <= 0)
113 197
        return 0;
114 198

  
......
140 224
    return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
141 225
}
142 226

  
143
int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
227
static int thread_init(AVCodecContext *avctx)
144 228
{
145 229
    int i;
146 230
    ThreadContext *c;
147

  
148
    avctx->thread_count = thread_count;
231
    int thread_count = avctx->thread_count;
149 232

  
150 233
    if (thread_count <= 1)
151 234
        return 0;
......
184 267
    avctx->execute2 = avcodec_thread_execute2;
185 268
    return 0;
186 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) ff_thread_finish_setup(avctx);
296

  
297
        pthread_mutex_lock(&p->mutex);
298
        avcodec_get_frame_defaults(&p->frame);
299
        p->got_frame = 0;
300
        p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
301

  
302
        if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
303

  
304
        p->state = STATE_INPUT_READY;
305

  
306
        pthread_mutex_lock(&p->progress_mutex);
307
        pthread_cond_signal(&p->output_cond);
308
        pthread_mutex_unlock(&p->progress_mutex);
309

  
310
        pthread_mutex_unlock(&p->mutex);
311
    }
312

  
313
    return NULL;
314
}
315

  
316
/**
317
 * Updates the next thread's AVCodecContext with values from the reference thread's context.
318
 *
319
 * @param dst The destination context.
320
 * @param src The source context.
321
 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
322
 */
323
static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
324
{
325
    int err = 0;
326

  
327
    if (dst != src) {
328
        dst->sub_id    = src->sub_id;
329
        dst->time_base = src->time_base;
330
        dst->width     = src->width;
331
        dst->height    = src->height;
332
        dst->pix_fmt   = src->pix_fmt;
333

  
334
        dst->has_b_frames = src->has_b_frames;
335
        dst->idct_algo    = src->idct_algo;
336
        dst->slice_count  = src->slice_count;
337

  
338
        dst->bits_per_coded_sample = src->bits_per_coded_sample;
339
        dst->sample_aspect_ratio   = src->sample_aspect_ratio;
340
        dst->dtg_active_format     = src->dtg_active_format;
341

  
342
        dst->profile = src->profile;
343
        dst->level   = src->level;
344

  
345
        dst->bits_per_raw_sample = src->bits_per_raw_sample;
346
        dst->ticks_per_frame     = src->ticks_per_frame;
347
        dst->color_primaries     = src->color_primaries;
348

  
349
        dst->color_trc   = src->color_trc;
350
        dst->colorspace  = src->colorspace;
351
        dst->color_range = src->color_range;
352
        dst->chroma_sample_location = src->chroma_sample_location;
353
    }
354

  
355
    if (for_user) {
356
        dst->coded_frame   = src->coded_frame;
357
        dst->has_b_frames += src->thread_count - 1;
358
    } else {
359
        if (dst->codec->update_thread_context)
360
            err = dst->codec->update_thread_context(dst, src);
361
    }
362

  
363
    return err;
364
}
365

  
366
/**
367
 * Update the next thread's AVCodecContext with values set by the user.
368
 *
369
 * @param dst The destination context.
370
 * @param src The source context.
371
 */
372
static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
373
{
374
#define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
375
    dst->flags          = src->flags;
376

  
377
    dst->draw_horiz_band= src->draw_horiz_band;
378
    dst->get_buffer     = src->get_buffer;
379
    dst->release_buffer = src->release_buffer;
380

  
381
    dst->opaque   = src->opaque;
382
    dst->hurry_up = src->hurry_up;
383
    dst->dsp_mask = src->dsp_mask;
384
    dst->debug    = src->debug;
385
    dst->debug_mv = src->debug_mv;
386

  
387
    dst->slice_flags = src->slice_flags;
388
    dst->flags2      = src->flags2;
389

  
390
    copy_fields(skip_loop_filter, bidir_refine);
391

  
392
    dst->frame_number     = src->frame_number;
393
    dst->reordered_opaque = src->reordered_opaque;
394
#undef copy_fields
395
}
396

  
397
static void free_progress(AVFrame *f)
398
{
399
    PerThreadContext *p = f->owner->thread_opaque;
400
    int *progress = f->thread_opaque;
401

  
402
    p->progress_used[(progress - p->progress[0]) / 2] = 0;
403
}
404

  
405
/// Releases the buffers that this decoding thread was the last user of.
406
static void release_delayed_buffers(PerThreadContext *p)
407
{
408
    FrameThreadContext *fctx = p->parent;
409

  
410
    while (p->num_released_buffers > 0) {
411
        AVFrame *f = &p->released_buffers[--p->num_released_buffers];
412

  
413
        pthread_mutex_lock(&fctx->buffer_mutex);
414
        free_progress(f);
415
        f->thread_opaque = NULL;
416

  
417
        f->owner->release_buffer(f->owner, f);
418
        pthread_mutex_unlock(&fctx->buffer_mutex);
419
    }
420
}
421

  
422
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
423
{
424
    FrameThreadContext *fctx = p->parent;
425
    PerThreadContext *prev_thread = fctx->prev_thread;
426
    AVCodec *codec = p->avctx->codec;
427
    uint8_t *buf = p->avpkt.data;
428

  
429
    if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
430

  
431
    pthread_mutex_lock(&p->mutex);
432

  
433
    release_delayed_buffers(p);
434

  
435
    if (prev_thread) {
436
        int err;
437
        if (prev_thread->state == STATE_SETTING_UP) {
438
            pthread_mutex_lock(&prev_thread->progress_mutex);
439
            while (prev_thread->state == STATE_SETTING_UP)
440
                pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
441
            pthread_mutex_unlock(&prev_thread->progress_mutex);
442
        }
443

  
444
        err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
445
        if (err) {
446
            pthread_mutex_unlock(&p->mutex);
447
            return err;
448
        }
449
    }
450

  
451
    av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
452
    p->avpkt = *avpkt;
453
    p->avpkt.data = buf;
454
    memcpy(buf, avpkt->data, avpkt->size);
455
    memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
456

  
457
    p->state = STATE_SETTING_UP;
458
    pthread_cond_signal(&p->input_cond);
459
    pthread_mutex_unlock(&p->mutex);
460

  
461
    /*
462
     * If the client doesn't have a thread-safe get_buffer(),
463
     * then decoding threads call back to the main thread,
464
     * and it calls back to the client here.
465
     */
466

  
467
    if (!p->avctx->thread_safe_callbacks &&
468
         p->avctx->get_buffer != avcodec_default_get_buffer) {
469
        while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
470
            pthread_mutex_lock(&p->progress_mutex);
471
            while (p->state == STATE_SETTING_UP)
472
                pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
473

  
474
            if (p->state == STATE_GET_BUFFER) {
475
                p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
476
                p->state  = STATE_SETTING_UP;
477
                pthread_cond_signal(&p->progress_cond);
478
            }
479
            pthread_mutex_unlock(&p->progress_mutex);
480
        }
481
    }
482

  
483
    fctx->prev_thread = p;
484

  
485
    return 0;
486
}
487

  
488
int ff_thread_decode_frame(AVCodecContext *avctx,
489
                           AVFrame *picture, int *got_picture_ptr,
490
                           AVPacket *avpkt)
491
{
492
    FrameThreadContext *fctx = avctx->thread_opaque;
493
    int finished = fctx->next_finished;
494
    PerThreadContext *p;
495
    int err;
496

  
497
    /*
498
     * Submit a packet to the next decoding thread.
499
     */
500

  
501
    p = &fctx->threads[fctx->next_decoding];
502
    update_context_from_user(p->avctx, avctx);
503
    err = submit_packet(p, avpkt);
504
    if (err) return err;
505

  
506
    fctx->next_decoding++;
507

  
508
    /*
509
     * If we're still receiving the initial packets, don't return a frame.
510
     */
511

  
512
    if (fctx->delaying && avpkt->size) {
513
        if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
514

  
515
        *got_picture_ptr=0;
516
        return 0;
517
    }
518

  
519
    /*
520
     * Return the next available frame from the oldest thread.
521
     * If we're at the end of the stream, then we have to skip threads that
522
     * didn't output a frame, because we don't want to accidentally signal
523
     * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
524
     */
525

  
526
    do {
527
        p = &fctx->threads[finished++];
528

  
529
        if (p->state != STATE_INPUT_READY) {
530
            pthread_mutex_lock(&p->progress_mutex);
531
            while (p->state != STATE_INPUT_READY)
532
                pthread_cond_wait(&p->output_cond, &p->progress_mutex);
533
            pthread_mutex_unlock(&p->progress_mutex);
534
        }
535

  
536
        *picture = p->frame;
537
        *got_picture_ptr = p->got_frame;
538
        picture->pkt_dts = p->avpkt.dts;
539

  
540
        /*
541
         * A later call with avkpt->size == 0 may loop over all threads,
542
         * including this one, searching for a frame to return before being
543
         * stopped by the "finished != fctx->next_finished" condition.
544
         * Make sure we don't mistakenly return the same frame again.
545
         */
546
        p->got_frame = 0;
547

  
548
        if (finished >= avctx->thread_count) finished = 0;
549
    } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
550

  
551
    update_context_from_thread(avctx, p->avctx, 1);
552

  
553
    if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
554

  
555
    fctx->next_finished = finished;
556

  
557
    return p->result;
558
}
559

  
560
void ff_thread_report_progress(AVFrame *f, int n, int field)
561
{
562
    PerThreadContext *p;
563
    int *progress = f->thread_opaque;
564

  
565
    if (!progress || progress[field] >= n) return;
566

  
567
    p = f->owner->thread_opaque;
568

  
569
    if (f->owner->debug&FF_DEBUG_THREADS)
570
        av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
571

  
572
    pthread_mutex_lock(&p->progress_mutex);
573
    progress[field] = n;
574
    pthread_cond_broadcast(&p->progress_cond);
575
    pthread_mutex_unlock(&p->progress_mutex);
576
}
577

  
578
void ff_thread_await_progress(AVFrame *f, int n, int field)
579
{
580
    PerThreadContext *p;
581
    int *progress = f->thread_opaque;
582

  
583
    if (!progress || progress[field] >= n) return;
584

  
585
    p = f->owner->thread_opaque;
586

  
587
    if (f->owner->debug&FF_DEBUG_THREADS)
588
        av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
589

  
590
    pthread_mutex_lock(&p->progress_mutex);
591
    while (progress[field] < n)
592
        pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
593
    pthread_mutex_unlock(&p->progress_mutex);
594
}
595

  
596
void ff_thread_finish_setup(AVCodecContext *avctx) {
597
    PerThreadContext *p = avctx->thread_opaque;
598

  
599
    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
600

  
601
    pthread_mutex_lock(&p->progress_mutex);
602
    p->state = STATE_SETUP_FINISHED;
603
    pthread_cond_broadcast(&p->progress_cond);
604
    pthread_mutex_unlock(&p->progress_mutex);
605
}
606

  
607
/// Waits for all threads to finish.
608
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
609
{
610
    int i;
611

  
612
    for (i = 0; i < thread_count; i++) {
613
        PerThreadContext *p = &fctx->threads[i];
614

  
615
        if (p->state != STATE_INPUT_READY) {
616
            pthread_mutex_lock(&p->progress_mutex);
617
            while (p->state != STATE_INPUT_READY)
618
                pthread_cond_wait(&p->output_cond, &p->progress_mutex);
619
            pthread_mutex_unlock(&p->progress_mutex);
620
        }
621
    }
622
}
623

  
624
static void frame_thread_free(AVCodecContext *avctx, int thread_count)
625
{
626
    FrameThreadContext *fctx = avctx->thread_opaque;
627
    AVCodec *codec = avctx->codec;
628
    int i;
629

  
630
    park_frame_worker_threads(fctx, thread_count);
631

  
632
    if (fctx->prev_thread)
633
        update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
634

  
635
    fctx->die = 1;
636

  
637
    for (i = 0; i < thread_count; i++) {
638
        PerThreadContext *p = &fctx->threads[i];
639

  
640
        pthread_mutex_lock(&p->mutex);
641
        pthread_cond_signal(&p->input_cond);
642
        pthread_mutex_unlock(&p->mutex);
643

  
644
        pthread_join(p->thread, NULL);
645

  
646
        if (codec->close)
647
            codec->close(p->avctx);
648

  
649
        avctx->codec = NULL;
650

  
651
        release_delayed_buffers(p);
652
    }
653

  
654
    for (i = 0; i < thread_count; i++) {
655
        PerThreadContext *p = &fctx->threads[i];
656

  
657
        avcodec_default_free_buffers(p->avctx);
658

  
659
        pthread_mutex_destroy(&p->mutex);
660
        pthread_mutex_destroy(&p->progress_mutex);
661
        pthread_cond_destroy(&p->input_cond);
662
        pthread_cond_destroy(&p->progress_cond);
663
        pthread_cond_destroy(&p->output_cond);
664
        av_freep(&p->avpkt.data);
665

  
666
        if (i)
667
            av_freep(&p->avctx->priv_data);
668

  
669
        av_freep(&p->avctx);
670
    }
671

  
672
    av_freep(&fctx->threads);
673
    pthread_mutex_destroy(&fctx->buffer_mutex);
674
    av_freep(&avctx->thread_opaque);
675
}
676

  
677
static int frame_thread_init(AVCodecContext *avctx)
678
{
679
    int thread_count = avctx->thread_count;
680
    AVCodec *codec = avctx->codec;
681
    AVCodecContext *src = avctx;
682
    FrameThreadContext *fctx;
683
    int i, err = 0;
684

  
685
    avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
686

  
687
    fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
688
    pthread_mutex_init(&fctx->buffer_mutex, NULL);
689
    fctx->delaying = 1;
690

  
691
    for (i = 0; i < thread_count; i++) {
692
        AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
693
        PerThreadContext *p  = &fctx->threads[i];
694

  
695
        pthread_mutex_init(&p->mutex, NULL);
696
        pthread_mutex_init(&p->progress_mutex, NULL);
697
        pthread_cond_init(&p->input_cond, NULL);
698
        pthread_cond_init(&p->progress_cond, NULL);
699
        pthread_cond_init(&p->output_cond, NULL);
700

  
701
        p->parent = fctx;
702
        p->avctx  = copy;
703

  
704
        *copy = *src;
705
        copy->thread_opaque = p;
706
        copy->pkt = &p->avpkt;
707

  
708
        if (!i) {
709
            src = copy;
710

  
711
            if (codec->init)
712
                err = codec->init(copy);
713

  
714
            update_context_from_thread(avctx, copy, 1);
715
        } else {
716
            copy->is_copy   = 1;
717
            copy->priv_data = av_malloc(codec->priv_data_size);
718
            memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
719

  
720
            if (codec->init_thread_copy)
721
                err = codec->init_thread_copy(copy);
722
        }
723

  
724
        if (err) goto error;
725

  
726
        pthread_create(&p->thread, NULL, frame_worker_thread, p);
727
    }
728

  
729
    return 0;
730

  
731
error:
732
    frame_thread_free(avctx, i+1);
733

  
734
    return err;
735
}
736

  
737
void ff_thread_flush(AVCodecContext *avctx)
738
{
739
    FrameThreadContext *fctx = avctx->thread_opaque;
740

  
741
    if (!avctx->thread_opaque) return;
742

  
743
    park_frame_worker_threads(fctx, avctx->thread_count);
744

  
745
    if (fctx->prev_thread)
746
        update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
747

  
748
    fctx->next_decoding = fctx->next_finished = 0;
749
    fctx->delaying = 1;
750
    fctx->prev_thread = NULL;
751
}
752

  
753
static int *allocate_progress(PerThreadContext *p)
754
{
755
    int i;
756

  
757
    for (i = 0; i < MAX_BUFFERS; i++)
758
        if (!p->progress_used[i]) break;
759

  
760
    if (i == MAX_BUFFERS) {
761
        av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
762
        return NULL;
763
    }
764

  
765
    p->progress_used[i] = 1;
766

  
767
    return p->progress[i];
768
}
769

  
770
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
771
{
772
    PerThreadContext *p = avctx->thread_opaque;
773
    int *progress, err;
774

  
775
    f->owner = avctx;
776

  
777
    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
778
        f->thread_opaque = NULL;
779
        return avctx->get_buffer(avctx, f);
780
    }
781

  
782
    if (p->state != STATE_SETTING_UP) {
783
        av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
784
        return -1;
785
    }
786

  
787
    pthread_mutex_lock(&p->parent->buffer_mutex);
788
    f->thread_opaque = progress = allocate_progress(p);
789

  
790
    if (!progress) {
791
        pthread_mutex_unlock(&p->parent->buffer_mutex);
792
        return -1;
793
    }
794

  
795
    progress[0] =
796
    progress[1] = -1;
797

  
798
    if (avctx->thread_safe_callbacks ||
799
        avctx->get_buffer == avcodec_default_get_buffer) {
800
        err = avctx->get_buffer(avctx, f);
801
    } else {
802
        p->requested_frame = f;
803
        p->state = STATE_GET_BUFFER;
804
        pthread_mutex_lock(&p->progress_mutex);
805
        pthread_cond_signal(&p->progress_cond);
806

  
807
        while (p->state != STATE_SETTING_UP)
808
            pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
809

  
810
        err = p->result;
811

  
812
        pthread_mutex_unlock(&p->progress_mutex);
813
    }
814

  
815
    pthread_mutex_unlock(&p->parent->buffer_mutex);
816

  
817
    /*
818
     * Buffer age is difficult to keep track of between
819
     * multiple threads, and the optimizations it allows
820
     * are not worth the effort. It is disabled for now.
821
     */
822
    f->age = INT_MAX;
823

  
824
    return err;
825
}
826

  
827
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
828
{
829
    PerThreadContext *p = avctx->thread_opaque;
830

  
831
    if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
832
        avctx->release_buffer(avctx, f);
833
        return;
834
    }
835

  
836
    if (p->num_released_buffers >= MAX_BUFFERS) {
837
        av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
838
        return;
839
    }
840

  
841
    if(avctx->debug & FF_DEBUG_BUFFERS)
842
        av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
843
                                    f, f->owner->internal_buffer_count);
844

  
845
    p->released_buffers[p->num_released_buffers++] = *f;
846
    memset(f->data, 0, sizeof(f->data));
847
}
848

  
849
/**
850
 * Set the threading algorithms used.
851
 *
852
 * Threading requires more than one thread.
853
 * Frame threading requires entire frames to be passed to the codec,
854
 * and introduces extra decoding delay, so is incompatible with low_delay.
855
 *
856
 * @param avctx The context.
857
 */
858
static void validate_thread_parameters(AVCodecContext *avctx)
859
{
860
    int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
861
                                && !(avctx->flags & CODEC_FLAG_TRUNCATED)
862
                                && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
863
                                && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
864
    if (avctx->thread_count == 1) {
865
        avctx->active_thread_type = 0;
866
    } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
867
        avctx->active_thread_type = FF_THREAD_FRAME;
868
    } else {
869
        avctx->active_thread_type = FF_THREAD_SLICE;
870
    }
871
}
872

  
873
int avcodec_thread_init(AVCodecContext *avctx, int thread_count)
874
{
875
    if (avctx->thread_opaque) {
876
        av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
877
        return -1;
878
    }
879

  
880
    avctx->thread_count = FFMAX(1, thread_count);
881

  
882
    if (avctx->codec) {
883
        validate_thread_parameters(avctx);
884

  
885
        if (avctx->active_thread_type&FF_THREAD_SLICE)
886
            return thread_init(avctx);
887
        else if (avctx->active_thread_type&FF_THREAD_FRAME)
888
            return frame_thread_init(avctx);
889
    }
890

  
891
    return 0;
892
}
893

  
894
void avcodec_thread_free(AVCodecContext *avctx)
895
{
896
    if (avctx->active_thread_type&FF_THREAD_FRAME)
897
        frame_thread_free(avctx, avctx->thread_count);
898
    else
899
        thread_free(avctx);
900
}

Also available in: Unified diff