2 * Copyright (c) 2004 Roman Shaposhnik
3 * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
5 * Many thanks to Steven M. Schultz for providing clever ideas and
6 * to Michael Niedermayer <michaelni@gmx.at> for writing initial
9 * This file is part of FFmpeg.
11 * FFmpeg 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.
16 * FFmpeg 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.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with FFmpeg; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
28 * Multithreading support functions
29 * @see doc/multithreading.txt
40 #include "w32pthreads.h"
42 #include "os2threads.h"
45 typedef int (action_func)(AVCodecContext *c, void *arg);
46 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
48 typedef struct ThreadContext {
58 pthread_cond_t last_job_cond;
59 pthread_cond_t current_job_cond;
60 pthread_mutex_t current_job_lock;
65 /// Max number of frame buffers that can be allocated when using frame threads.
66 #define MAX_BUFFERS (32+1)
69 * Context used by codec threads and stored in their AVCodecContext thread_opaque.
71 typedef struct PerThreadContext {
72 struct FrameThreadContext *parent;
75 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
76 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
77 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
79 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
80 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
82 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
84 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
85 int allocated_buf_size; ///< Size allocated for avpkt.data
87 AVFrame frame; ///< Output frame (for decoding) or input (for encoding).
88 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
89 int result; ///< The result of the last codec decode/encode() call.
92 STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
93 STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
94 STATE_GET_BUFFER, /**<
95 * Set when the codec calls get_buffer().
96 * State is returned to STATE_SETTING_UP afterwards.
98 STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
102 * Array of frames passed to ff_thread_release_buffer().
103 * Frames are released after all threads referencing them are finished.
105 AVFrame released_buffers[MAX_BUFFERS];
106 int num_released_buffers;
109 * Array of progress values used by ff_thread_get_buffer().
111 int progress[MAX_BUFFERS][2];
112 uint8_t progress_used[MAX_BUFFERS];
114 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
118 * Context stored in the client AVCodecContext thread_opaque.
120 typedef struct FrameThreadContext {
121 PerThreadContext *threads; ///< The contexts for each thread.
122 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
124 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
126 int next_decoding; ///< The next context to submit a packet to.
127 int next_finished; ///< The next context to return output from.
130 * Set for the first N packets, where N is the number of threads.
131 * While it is set, ff_thread_en/decode_frame won't return any results.
134 int die; ///< Set when threads should exit.
135 } FrameThreadContext;
137 static void* attribute_align_arg worker(void *v)
139 AVCodecContext *avctx = v;
140 ThreadContext *c = avctx->thread_opaque;
141 int our_job = c->job_count;
142 int thread_count = avctx->thread_count;
145 pthread_mutex_lock(&c->current_job_lock);
146 self_id = c->current_job++;
148 while (our_job >= c->job_count) {
149 if (c->current_job == thread_count + c->job_count)
150 pthread_cond_signal(&c->last_job_cond);
152 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
156 pthread_mutex_unlock(&c->current_job_lock);
160 pthread_mutex_unlock(&c->current_job_lock);
162 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
163 c->func2(avctx, c->args, our_job, self_id);
165 pthread_mutex_lock(&c->current_job_lock);
166 our_job = c->current_job++;
170 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
172 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
173 pthread_mutex_unlock(&c->current_job_lock);
176 static void thread_free(AVCodecContext *avctx)
178 ThreadContext *c = avctx->thread_opaque;
181 pthread_mutex_lock(&c->current_job_lock);
183 pthread_cond_broadcast(&c->current_job_cond);
184 pthread_mutex_unlock(&c->current_job_lock);
186 for (i=0; i<avctx->thread_count; i++)
187 pthread_join(c->workers[i], NULL);
189 pthread_mutex_destroy(&c->current_job_lock);
190 pthread_cond_destroy(&c->current_job_cond);
191 pthread_cond_destroy(&c->last_job_cond);
193 av_freep(&avctx->thread_opaque);
196 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
198 ThreadContext *c= avctx->thread_opaque;
201 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
202 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
207 pthread_mutex_lock(&c->current_job_lock);
209 c->current_job = avctx->thread_count;
210 c->job_count = job_count;
211 c->job_size = job_size;
216 c->rets_count = job_count;
218 c->rets = &dummy_ret;
221 pthread_cond_broadcast(&c->current_job_cond);
223 avcodec_thread_park_workers(c, avctx->thread_count);
228 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
230 ThreadContext *c= avctx->thread_opaque;
232 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
235 static int thread_init(AVCodecContext *avctx)
239 int thread_count = avctx->thread_count;
241 if (thread_count <= 1)
244 c = av_mallocz(sizeof(ThreadContext));
248 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
254 avctx->thread_opaque = c;
259 pthread_cond_init(&c->current_job_cond, NULL);
260 pthread_cond_init(&c->last_job_cond, NULL);
261 pthread_mutex_init(&c->current_job_lock, NULL);
262 pthread_mutex_lock(&c->current_job_lock);
263 for (i=0; i<thread_count; i++) {
264 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
265 avctx->thread_count = i;
266 pthread_mutex_unlock(&c->current_job_lock);
267 ff_thread_free(avctx);
272 avcodec_thread_park_workers(c, thread_count);
274 avctx->execute = avcodec_thread_execute;
275 avctx->execute2 = avcodec_thread_execute2;
280 * Codec worker thread.
282 * Automatically calls ff_thread_finish_setup() if the codec does
283 * not provide an update_thread_context method, or if the codec returns
286 static attribute_align_arg void *frame_worker_thread(void *arg)
288 PerThreadContext *p = arg;
289 FrameThreadContext *fctx = p->parent;
290 AVCodecContext *avctx = p->avctx;
291 AVCodec *codec = avctx->codec;
294 if (p->state == STATE_INPUT_READY && !fctx->die) {
295 pthread_mutex_lock(&p->mutex);
296 while (p->state == STATE_INPUT_READY && !fctx->die)
297 pthread_cond_wait(&p->input_cond, &p->mutex);
298 pthread_mutex_unlock(&p->mutex);
301 if (fctx->die) break;
303 if (!codec->update_thread_context && avctx->thread_safe_callbacks)
304 ff_thread_finish_setup(avctx);
306 pthread_mutex_lock(&p->mutex);
307 avcodec_get_frame_defaults(&p->frame);
309 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
311 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
313 p->state = STATE_INPUT_READY;
315 pthread_mutex_lock(&p->progress_mutex);
316 pthread_cond_signal(&p->output_cond);
317 pthread_mutex_unlock(&p->progress_mutex);
319 pthread_mutex_unlock(&p->mutex);
326 * Updates the next thread's AVCodecContext with values from the reference thread's context.
328 * @param dst The destination context.
329 * @param src The source context.
330 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
332 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
337 dst->sub_id = src->sub_id;
338 dst->time_base = src->time_base;
339 dst->width = src->width;
340 dst->height = src->height;
341 dst->pix_fmt = src->pix_fmt;
343 dst->coded_width = src->coded_width;
344 dst->coded_height = src->coded_height;
346 dst->has_b_frames = src->has_b_frames;
347 dst->idct_algo = src->idct_algo;
348 dst->slice_count = src->slice_count;
350 dst->bits_per_coded_sample = src->bits_per_coded_sample;
351 dst->sample_aspect_ratio = src->sample_aspect_ratio;
352 dst->dtg_active_format = src->dtg_active_format;
354 dst->profile = src->profile;
355 dst->level = src->level;
357 dst->bits_per_raw_sample = src->bits_per_raw_sample;
358 dst->ticks_per_frame = src->ticks_per_frame;
359 dst->color_primaries = src->color_primaries;
361 dst->color_trc = src->color_trc;
362 dst->colorspace = src->colorspace;
363 dst->color_range = src->color_range;
364 dst->chroma_sample_location = src->chroma_sample_location;
368 dst->delay = src->thread_count - 1;
369 dst->coded_frame = src->coded_frame;
371 if (dst->codec->update_thread_context)
372 err = dst->codec->update_thread_context(dst, src);
379 * Update the next thread's AVCodecContext with values set by the user.
381 * @param dst The destination context.
382 * @param src The source context.
384 static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
386 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
387 dst->flags = src->flags;
389 dst->draw_horiz_band= src->draw_horiz_band;
390 dst->get_buffer = src->get_buffer;
391 dst->release_buffer = src->release_buffer;
393 dst->opaque = src->opaque;
394 dst->dsp_mask = src->dsp_mask;
395 dst->debug = src->debug;
396 dst->debug_mv = src->debug_mv;
398 dst->slice_flags = src->slice_flags;
399 dst->flags2 = src->flags2;
401 copy_fields(skip_loop_filter, bidir_refine);
403 dst->frame_number = src->frame_number;
404 dst->reordered_opaque = src->reordered_opaque;
408 static void free_progress(AVFrame *f)
410 PerThreadContext *p = f->owner->thread_opaque;
411 int *progress = f->thread_opaque;
413 p->progress_used[(progress - p->progress[0]) / 2] = 0;
416 /// Releases the buffers that this decoding thread was the last user of.
417 static void release_delayed_buffers(PerThreadContext *p)
419 FrameThreadContext *fctx = p->parent;
421 while (p->num_released_buffers > 0) {
424 pthread_mutex_lock(&fctx->buffer_mutex);
425 f = &p->released_buffers[--p->num_released_buffers];
427 f->thread_opaque = NULL;
429 f->owner->release_buffer(f->owner, f);
430 pthread_mutex_unlock(&fctx->buffer_mutex);
434 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
436 FrameThreadContext *fctx = p->parent;
437 PerThreadContext *prev_thread = fctx->prev_thread;
438 AVCodec *codec = p->avctx->codec;
439 uint8_t *buf = p->avpkt.data;
441 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
443 pthread_mutex_lock(&p->mutex);
445 release_delayed_buffers(p);
449 if (prev_thread->state == STATE_SETTING_UP) {
450 pthread_mutex_lock(&prev_thread->progress_mutex);
451 while (prev_thread->state == STATE_SETTING_UP)
452 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
453 pthread_mutex_unlock(&prev_thread->progress_mutex);
456 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
458 pthread_mutex_unlock(&p->mutex);
463 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
466 memcpy(buf, avpkt->data, avpkt->size);
467 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
469 p->state = STATE_SETTING_UP;
470 pthread_cond_signal(&p->input_cond);
471 pthread_mutex_unlock(&p->mutex);
474 * If the client doesn't have a thread-safe get_buffer(),
475 * then decoding threads call back to the main thread,
476 * and it calls back to the client here.
479 if (!p->avctx->thread_safe_callbacks &&
480 p->avctx->get_buffer != avcodec_default_get_buffer) {
481 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
482 pthread_mutex_lock(&p->progress_mutex);
483 while (p->state == STATE_SETTING_UP)
484 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
486 if (p->state == STATE_GET_BUFFER) {
487 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
488 p->state = STATE_SETTING_UP;
489 pthread_cond_signal(&p->progress_cond);
491 pthread_mutex_unlock(&p->progress_mutex);
495 fctx->prev_thread = p;
500 int ff_thread_decode_frame(AVCodecContext *avctx,
501 AVFrame *picture, int *got_picture_ptr,
504 FrameThreadContext *fctx = avctx->thread_opaque;
505 int finished = fctx->next_finished;
510 * Submit a packet to the next decoding thread.
513 p = &fctx->threads[fctx->next_decoding];
514 update_context_from_user(p->avctx, avctx);
515 err = submit_packet(p, avpkt);
518 fctx->next_decoding++;
521 * If we're still receiving the initial packets, don't return a frame.
524 if (fctx->delaying && avpkt->size) {
525 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
532 * Return the next available frame from the oldest thread.
533 * If we're at the end of the stream, then we have to skip threads that
534 * didn't output a frame, because we don't want to accidentally signal
535 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
539 p = &fctx->threads[finished++];
541 if (p->state != STATE_INPUT_READY) {
542 pthread_mutex_lock(&p->progress_mutex);
543 while (p->state != STATE_INPUT_READY)
544 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
545 pthread_mutex_unlock(&p->progress_mutex);
549 *got_picture_ptr = p->got_frame;
550 picture->pkt_dts = p->avpkt.dts;
553 * A later call with avkpt->size == 0 may loop over all threads,
554 * including this one, searching for a frame to return before being
555 * stopped by the "finished != fctx->next_finished" condition.
556 * Make sure we don't mistakenly return the same frame again.
560 if (finished >= avctx->thread_count) finished = 0;
561 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
563 update_context_from_thread(avctx, p->avctx, 1);
565 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
567 fctx->next_finished = finished;
572 void ff_thread_report_progress(AVFrame *f, int n, int field)
575 int *progress = f->thread_opaque;
577 if (!progress || progress[field] >= n) return;
579 p = f->owner->thread_opaque;
581 if (f->owner->debug&FF_DEBUG_THREADS)
582 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
584 pthread_mutex_lock(&p->progress_mutex);
586 pthread_cond_broadcast(&p->progress_cond);
587 pthread_mutex_unlock(&p->progress_mutex);
590 void ff_thread_await_progress(AVFrame *f, int n, int field)
593 int *progress = f->thread_opaque;
595 if (!progress || progress[field] >= n) return;
597 p = f->owner->thread_opaque;
599 if (f->owner->debug&FF_DEBUG_THREADS)
600 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
602 pthread_mutex_lock(&p->progress_mutex);
603 while (progress[field] < n)
604 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
605 pthread_mutex_unlock(&p->progress_mutex);
608 void ff_thread_finish_setup(AVCodecContext *avctx) {
609 PerThreadContext *p = avctx->thread_opaque;
611 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
613 if(p->state == STATE_SETUP_FINISHED){
614 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
617 pthread_mutex_lock(&p->progress_mutex);
618 p->state = STATE_SETUP_FINISHED;
619 pthread_cond_broadcast(&p->progress_cond);
620 pthread_mutex_unlock(&p->progress_mutex);
623 /// Waits for all threads to finish.
624 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
628 for (i = 0; i < thread_count; i++) {
629 PerThreadContext *p = &fctx->threads[i];
631 if (p->state != STATE_INPUT_READY) {
632 pthread_mutex_lock(&p->progress_mutex);
633 while (p->state != STATE_INPUT_READY)
634 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
635 pthread_mutex_unlock(&p->progress_mutex);
640 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
642 FrameThreadContext *fctx = avctx->thread_opaque;
643 AVCodec *codec = avctx->codec;
646 park_frame_worker_threads(fctx, thread_count);
648 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
649 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
653 for (i = 0; i < thread_count; i++) {
654 PerThreadContext *p = &fctx->threads[i];
656 pthread_mutex_lock(&p->mutex);
657 pthread_cond_signal(&p->input_cond);
658 pthread_mutex_unlock(&p->mutex);
660 pthread_join(p->thread, NULL);
663 codec->close(p->avctx);
667 release_delayed_buffers(p);
670 for (i = 0; i < thread_count; i++) {
671 PerThreadContext *p = &fctx->threads[i];
673 avcodec_default_free_buffers(p->avctx);
675 pthread_mutex_destroy(&p->mutex);
676 pthread_mutex_destroy(&p->progress_mutex);
677 pthread_cond_destroy(&p->input_cond);
678 pthread_cond_destroy(&p->progress_cond);
679 pthread_cond_destroy(&p->output_cond);
680 av_freep(&p->avpkt.data);
683 av_freep(&p->avctx->priv_data);
688 av_freep(&fctx->threads);
689 pthread_mutex_destroy(&fctx->buffer_mutex);
690 av_freep(&avctx->thread_opaque);
693 static int frame_thread_init(AVCodecContext *avctx)
695 int thread_count = avctx->thread_count;
696 AVCodec *codec = avctx->codec;
697 AVCodecContext *src = avctx;
698 FrameThreadContext *fctx;
701 if (thread_count <= 1) {
702 avctx->active_thread_type = 0;
706 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
708 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
709 pthread_mutex_init(&fctx->buffer_mutex, NULL);
712 for (i = 0; i < thread_count; i++) {
713 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
714 PerThreadContext *p = &fctx->threads[i];
716 pthread_mutex_init(&p->mutex, NULL);
717 pthread_mutex_init(&p->progress_mutex, NULL);
718 pthread_cond_init(&p->input_cond, NULL);
719 pthread_cond_init(&p->progress_cond, NULL);
720 pthread_cond_init(&p->output_cond, NULL);
726 copy->thread_opaque = p;
727 copy->pkt = &p->avpkt;
733 err = codec->init(copy);
735 update_context_from_thread(avctx, copy, 1);
738 copy->priv_data = av_malloc(codec->priv_data_size);
739 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
741 if (codec->init_thread_copy)
742 err = codec->init_thread_copy(copy);
747 pthread_create(&p->thread, NULL, frame_worker_thread, p);
753 frame_thread_free(avctx, i+1);
758 void ff_thread_flush(AVCodecContext *avctx)
760 FrameThreadContext *fctx = avctx->thread_opaque;
762 if (!avctx->thread_opaque) return;
764 park_frame_worker_threads(fctx, avctx->thread_count);
765 if (fctx->prev_thread) {
766 if (fctx->prev_thread != &fctx->threads[0])
767 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
768 if (avctx->codec->flush)
769 avctx->codec->flush(fctx->threads[0].avctx);
772 fctx->next_decoding = fctx->next_finished = 0;
774 fctx->prev_thread = NULL;
777 static int *allocate_progress(PerThreadContext *p)
781 for (i = 0; i < MAX_BUFFERS; i++)
782 if (!p->progress_used[i]) break;
784 if (i == MAX_BUFFERS) {
785 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
789 p->progress_used[i] = 1;
791 return p->progress[i];
794 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
796 PerThreadContext *p = avctx->thread_opaque;
801 ff_init_buffer_info(avctx, f);
803 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
804 f->thread_opaque = NULL;
805 return avctx->get_buffer(avctx, f);
808 if (p->state != STATE_SETTING_UP &&
809 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
810 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
814 pthread_mutex_lock(&p->parent->buffer_mutex);
815 f->thread_opaque = progress = allocate_progress(p);
818 pthread_mutex_unlock(&p->parent->buffer_mutex);
825 if (avctx->thread_safe_callbacks ||
826 avctx->get_buffer == avcodec_default_get_buffer) {
827 err = avctx->get_buffer(avctx, f);
829 p->requested_frame = f;
830 p->state = STATE_GET_BUFFER;
831 pthread_mutex_lock(&p->progress_mutex);
832 pthread_cond_signal(&p->progress_cond);
834 while (p->state != STATE_SETTING_UP)
835 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
839 pthread_mutex_unlock(&p->progress_mutex);
841 if (!avctx->codec->update_thread_context)
842 ff_thread_finish_setup(avctx);
845 pthread_mutex_unlock(&p->parent->buffer_mutex);
848 * Buffer age is difficult to keep track of between
849 * multiple threads, and the optimizations it allows
850 * are not worth the effort. It is disabled for now.
857 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
859 PerThreadContext *p = avctx->thread_opaque;
860 FrameThreadContext *fctx;
862 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
863 avctx->release_buffer(avctx, f);
867 if (p->num_released_buffers >= MAX_BUFFERS) {
868 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
872 if(avctx->debug & FF_DEBUG_BUFFERS)
873 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
874 f, f->owner->internal_buffer_count);
877 pthread_mutex_lock(&fctx->buffer_mutex);
878 p->released_buffers[p->num_released_buffers++] = *f;
879 pthread_mutex_unlock(&fctx->buffer_mutex);
880 memset(f->data, 0, sizeof(f->data));
884 * Set the threading algorithms used.
886 * Threading requires more than one thread.
887 * Frame threading requires entire frames to be passed to the codec,
888 * and introduces extra decoding delay, so is incompatible with low_delay.
890 * @param avctx The context.
892 static void validate_thread_parameters(AVCodecContext *avctx)
894 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
895 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
896 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
897 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
898 if (avctx->thread_count == 1) {
899 avctx->active_thread_type = 0;
900 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
901 avctx->active_thread_type = FF_THREAD_FRAME;
902 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
903 avctx->thread_type & FF_THREAD_SLICE) {
904 avctx->active_thread_type = FF_THREAD_SLICE;
908 int ff_thread_init(AVCodecContext *avctx)
910 if (avctx->thread_opaque) {
911 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
920 validate_thread_parameters(avctx);
922 if (avctx->active_thread_type&FF_THREAD_SLICE)
923 return thread_init(avctx);
924 else if (avctx->active_thread_type&FF_THREAD_FRAME)
925 return frame_thread_init(avctx);
931 void ff_thread_free(AVCodecContext *avctx)
933 if (avctx->active_thread_type&FF_THREAD_FRAME)
934 frame_thread_free(avctx, avctx->thread_count);