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
38 typedef int (action_func)(AVCodecContext *c, void *arg);
39 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
41 typedef struct ThreadContext {
51 pthread_cond_t last_job_cond;
52 pthread_cond_t current_job_cond;
53 pthread_mutex_t current_job_lock;
58 /// Max number of frame buffers that can be allocated when using frame threads.
59 #define MAX_BUFFERS (32+1)
62 * Context used by codec threads and stored in their AVCodecContext thread_opaque.
64 typedef struct PerThreadContext {
65 struct FrameThreadContext *parent;
68 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
69 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
70 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
72 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
73 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
75 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
77 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
78 int allocated_buf_size; ///< Size allocated for avpkt.data
80 AVFrame frame; ///< Output frame (for decoding) or input (for encoding).
81 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
82 int result; ///< The result of the last codec decode/encode() call.
85 STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
86 STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
87 STATE_GET_BUFFER, /**<
88 * Set when the codec calls get_buffer().
89 * State is returned to STATE_SETTING_UP afterwards.
91 STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
95 * Array of frames passed to ff_thread_release_buffer().
96 * Frames are released after all threads referencing them are finished.
98 AVFrame released_buffers[MAX_BUFFERS];
99 int num_released_buffers;
102 * Array of progress values used by ff_thread_get_buffer().
104 int progress[MAX_BUFFERS][2];
105 uint8_t progress_used[MAX_BUFFERS];
107 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
111 * Context stored in the client AVCodecContext thread_opaque.
113 typedef struct FrameThreadContext {
114 PerThreadContext *threads; ///< The contexts for each thread.
115 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
117 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
119 int next_decoding; ///< The next context to submit a packet to.
120 int next_finished; ///< The next context to return output from.
123 * Set for the first N packets, where N is the number of threads.
124 * While it is set, ff_thread_en/decode_frame won't return any results.
127 int die; ///< Set when threads should exit.
128 } FrameThreadContext;
130 static void* attribute_align_arg worker(void *v)
132 AVCodecContext *avctx = v;
133 ThreadContext *c = avctx->thread_opaque;
134 int our_job = c->job_count;
135 int thread_count = avctx->thread_count;
138 pthread_mutex_lock(&c->current_job_lock);
139 self_id = c->current_job++;
141 while (our_job >= c->job_count) {
142 if (c->current_job == thread_count + c->job_count)
143 pthread_cond_signal(&c->last_job_cond);
145 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
149 pthread_mutex_unlock(&c->current_job_lock);
153 pthread_mutex_unlock(&c->current_job_lock);
155 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
156 c->func2(avctx, c->args, our_job, self_id);
158 pthread_mutex_lock(&c->current_job_lock);
159 our_job = c->current_job++;
163 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
165 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
166 pthread_mutex_unlock(&c->current_job_lock);
169 static void thread_free(AVCodecContext *avctx)
171 ThreadContext *c = avctx->thread_opaque;
174 pthread_mutex_lock(&c->current_job_lock);
176 pthread_cond_broadcast(&c->current_job_cond);
177 pthread_mutex_unlock(&c->current_job_lock);
179 for (i=0; i<avctx->thread_count; i++)
180 pthread_join(c->workers[i], NULL);
182 pthread_mutex_destroy(&c->current_job_lock);
183 pthread_cond_destroy(&c->current_job_cond);
184 pthread_cond_destroy(&c->last_job_cond);
186 av_freep(&avctx->thread_opaque);
189 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
191 ThreadContext *c= avctx->thread_opaque;
194 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
195 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
200 pthread_mutex_lock(&c->current_job_lock);
202 c->current_job = avctx->thread_count;
203 c->job_count = job_count;
204 c->job_size = job_size;
209 c->rets_count = job_count;
211 c->rets = &dummy_ret;
214 pthread_cond_broadcast(&c->current_job_cond);
216 avcodec_thread_park_workers(c, avctx->thread_count);
221 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
223 ThreadContext *c= avctx->thread_opaque;
225 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
228 static int thread_init(AVCodecContext *avctx)
232 int thread_count = avctx->thread_count;
234 if (thread_count <= 1)
237 c = av_mallocz(sizeof(ThreadContext));
241 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
247 avctx->thread_opaque = c;
252 pthread_cond_init(&c->current_job_cond, NULL);
253 pthread_cond_init(&c->last_job_cond, NULL);
254 pthread_mutex_init(&c->current_job_lock, NULL);
255 pthread_mutex_lock(&c->current_job_lock);
256 for (i=0; i<thread_count; i++) {
257 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
258 avctx->thread_count = i;
259 pthread_mutex_unlock(&c->current_job_lock);
260 ff_thread_free(avctx);
265 avcodec_thread_park_workers(c, thread_count);
267 avctx->execute = avcodec_thread_execute;
268 avctx->execute2 = avcodec_thread_execute2;
273 * Codec worker thread.
275 * Automatically calls ff_thread_finish_setup() if the codec does
276 * not provide an update_thread_context method, or if the codec returns
279 static attribute_align_arg void *frame_worker_thread(void *arg)
281 PerThreadContext *p = arg;
282 FrameThreadContext *fctx = p->parent;
283 AVCodecContext *avctx = p->avctx;
284 AVCodec *codec = avctx->codec;
287 if (p->state == STATE_INPUT_READY && !fctx->die) {
288 pthread_mutex_lock(&p->mutex);
289 while (p->state == STATE_INPUT_READY && !fctx->die)
290 pthread_cond_wait(&p->input_cond, &p->mutex);
291 pthread_mutex_unlock(&p->mutex);
294 if (fctx->die) break;
296 if (!codec->update_thread_context && avctx->thread_safe_callbacks)
297 ff_thread_finish_setup(avctx);
299 pthread_mutex_lock(&p->mutex);
300 avcodec_get_frame_defaults(&p->frame);
302 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
304 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
306 p->state = STATE_INPUT_READY;
308 pthread_mutex_lock(&p->progress_mutex);
309 pthread_cond_signal(&p->output_cond);
310 pthread_mutex_unlock(&p->progress_mutex);
312 pthread_mutex_unlock(&p->mutex);
319 * Updates the next thread's AVCodecContext with values from the reference thread's context.
321 * @param dst The destination context.
322 * @param src The source context.
323 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
325 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
330 dst->sub_id = src->sub_id;
331 dst->time_base = src->time_base;
332 dst->width = src->width;
333 dst->height = src->height;
334 dst->pix_fmt = src->pix_fmt;
336 dst->has_b_frames = src->has_b_frames;
337 dst->idct_algo = src->idct_algo;
338 dst->slice_count = src->slice_count;
340 dst->bits_per_coded_sample = src->bits_per_coded_sample;
341 dst->sample_aspect_ratio = src->sample_aspect_ratio;
342 dst->dtg_active_format = src->dtg_active_format;
344 dst->profile = src->profile;
345 dst->level = src->level;
347 dst->bits_per_raw_sample = src->bits_per_raw_sample;
348 dst->ticks_per_frame = src->ticks_per_frame;
349 dst->color_primaries = src->color_primaries;
351 dst->color_trc = src->color_trc;
352 dst->colorspace = src->colorspace;
353 dst->color_range = src->color_range;
354 dst->chroma_sample_location = src->chroma_sample_location;
358 dst->coded_frame = src->coded_frame;
359 dst->has_b_frames += src->thread_count - 1;
361 if (dst->codec->update_thread_context)
362 err = dst->codec->update_thread_context(dst, src);
369 * Update the next thread's AVCodecContext with values set by the user.
371 * @param dst The destination context.
372 * @param src The source context.
374 static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
376 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
377 dst->flags = src->flags;
379 dst->draw_horiz_band= src->draw_horiz_band;
380 dst->get_buffer = src->get_buffer;
381 dst->release_buffer = src->release_buffer;
383 dst->opaque = src->opaque;
384 dst->dsp_mask = src->dsp_mask;
385 dst->debug = src->debug;
386 dst->debug_mv = src->debug_mv;
388 dst->slice_flags = src->slice_flags;
389 dst->flags2 = src->flags2;
391 copy_fields(skip_loop_filter, bidir_refine);
393 dst->frame_number = src->frame_number;
394 dst->reordered_opaque = src->reordered_opaque;
398 static void free_progress(AVFrame *f)
400 PerThreadContext *p = f->owner->thread_opaque;
401 int *progress = f->thread_opaque;
403 p->progress_used[(progress - p->progress[0]) / 2] = 0;
406 /// Releases the buffers that this decoding thread was the last user of.
407 static void release_delayed_buffers(PerThreadContext *p)
409 FrameThreadContext *fctx = p->parent;
411 while (p->num_released_buffers > 0) {
414 pthread_mutex_lock(&fctx->buffer_mutex);
415 f = &p->released_buffers[--p->num_released_buffers];
417 f->thread_opaque = NULL;
419 f->owner->release_buffer(f->owner, f);
420 pthread_mutex_unlock(&fctx->buffer_mutex);
424 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
426 FrameThreadContext *fctx = p->parent;
427 PerThreadContext *prev_thread = fctx->prev_thread;
428 AVCodec *codec = p->avctx->codec;
429 uint8_t *buf = p->avpkt.data;
431 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
433 pthread_mutex_lock(&p->mutex);
435 release_delayed_buffers(p);
439 if (prev_thread->state == STATE_SETTING_UP) {
440 pthread_mutex_lock(&prev_thread->progress_mutex);
441 while (prev_thread->state == STATE_SETTING_UP)
442 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
443 pthread_mutex_unlock(&prev_thread->progress_mutex);
446 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
448 pthread_mutex_unlock(&p->mutex);
453 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
456 memcpy(buf, avpkt->data, avpkt->size);
457 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
459 p->state = STATE_SETTING_UP;
460 pthread_cond_signal(&p->input_cond);
461 pthread_mutex_unlock(&p->mutex);
464 * If the client doesn't have a thread-safe get_buffer(),
465 * then decoding threads call back to the main thread,
466 * and it calls back to the client here.
469 if (!p->avctx->thread_safe_callbacks &&
470 p->avctx->get_buffer != avcodec_default_get_buffer) {
471 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
472 pthread_mutex_lock(&p->progress_mutex);
473 while (p->state == STATE_SETTING_UP)
474 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
476 if (p->state == STATE_GET_BUFFER) {
477 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
478 p->state = STATE_SETTING_UP;
479 pthread_cond_signal(&p->progress_cond);
481 pthread_mutex_unlock(&p->progress_mutex);
485 fctx->prev_thread = p;
490 int ff_thread_decode_frame(AVCodecContext *avctx,
491 AVFrame *picture, int *got_picture_ptr,
494 FrameThreadContext *fctx = avctx->thread_opaque;
495 int finished = fctx->next_finished;
500 * Submit a packet to the next decoding thread.
503 p = &fctx->threads[fctx->next_decoding];
504 update_context_from_user(p->avctx, avctx);
505 err = submit_packet(p, avpkt);
508 fctx->next_decoding++;
511 * If we're still receiving the initial packets, don't return a frame.
514 if (fctx->delaying && avpkt->size) {
515 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
522 * Return the next available frame from the oldest thread.
523 * If we're at the end of the stream, then we have to skip threads that
524 * didn't output a frame, because we don't want to accidentally signal
525 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
529 p = &fctx->threads[finished++];
531 if (p->state != STATE_INPUT_READY) {
532 pthread_mutex_lock(&p->progress_mutex);
533 while (p->state != STATE_INPUT_READY)
534 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
535 pthread_mutex_unlock(&p->progress_mutex);
539 *got_picture_ptr = p->got_frame;
540 picture->pkt_dts = p->avpkt.dts;
543 * A later call with avkpt->size == 0 may loop over all threads,
544 * including this one, searching for a frame to return before being
545 * stopped by the "finished != fctx->next_finished" condition.
546 * Make sure we don't mistakenly return the same frame again.
550 if (finished >= avctx->thread_count) finished = 0;
551 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
553 update_context_from_thread(avctx, p->avctx, 1);
555 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
557 fctx->next_finished = finished;
562 void ff_thread_report_progress(AVFrame *f, int n, int field)
565 int *progress = f->thread_opaque;
567 if (!progress || progress[field] >= n) return;
569 p = f->owner->thread_opaque;
571 if (f->owner->debug&FF_DEBUG_THREADS)
572 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
574 pthread_mutex_lock(&p->progress_mutex);
576 pthread_cond_broadcast(&p->progress_cond);
577 pthread_mutex_unlock(&p->progress_mutex);
580 void ff_thread_await_progress(AVFrame *f, int n, int field)
583 int *progress = f->thread_opaque;
585 if (!progress || progress[field] >= n) return;
587 p = f->owner->thread_opaque;
589 if (f->owner->debug&FF_DEBUG_THREADS)
590 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
592 pthread_mutex_lock(&p->progress_mutex);
593 while (progress[field] < n)
594 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
595 pthread_mutex_unlock(&p->progress_mutex);
598 void ff_thread_finish_setup(AVCodecContext *avctx) {
599 PerThreadContext *p = avctx->thread_opaque;
601 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
603 if(p->state == STATE_SETUP_FINISHED){
604 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
607 pthread_mutex_lock(&p->progress_mutex);
608 p->state = STATE_SETUP_FINISHED;
609 pthread_cond_broadcast(&p->progress_cond);
610 pthread_mutex_unlock(&p->progress_mutex);
613 /// Waits for all threads to finish.
614 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
618 for (i = 0; i < thread_count; i++) {
619 PerThreadContext *p = &fctx->threads[i];
621 if (p->state != STATE_INPUT_READY) {
622 pthread_mutex_lock(&p->progress_mutex);
623 while (p->state != STATE_INPUT_READY)
624 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
625 pthread_mutex_unlock(&p->progress_mutex);
630 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
632 FrameThreadContext *fctx = avctx->thread_opaque;
633 AVCodec *codec = avctx->codec;
636 park_frame_worker_threads(fctx, thread_count);
638 if (fctx->prev_thread)
639 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
643 for (i = 0; i < thread_count; i++) {
644 PerThreadContext *p = &fctx->threads[i];
646 pthread_mutex_lock(&p->mutex);
647 pthread_cond_signal(&p->input_cond);
648 pthread_mutex_unlock(&p->mutex);
650 pthread_join(p->thread, NULL);
653 codec->close(p->avctx);
657 release_delayed_buffers(p);
660 for (i = 0; i < thread_count; i++) {
661 PerThreadContext *p = &fctx->threads[i];
663 avcodec_default_free_buffers(p->avctx);
665 pthread_mutex_destroy(&p->mutex);
666 pthread_mutex_destroy(&p->progress_mutex);
667 pthread_cond_destroy(&p->input_cond);
668 pthread_cond_destroy(&p->progress_cond);
669 pthread_cond_destroy(&p->output_cond);
670 av_freep(&p->avpkt.data);
673 av_freep(&p->avctx->priv_data);
678 av_freep(&fctx->threads);
679 pthread_mutex_destroy(&fctx->buffer_mutex);
680 av_freep(&avctx->thread_opaque);
683 static int frame_thread_init(AVCodecContext *avctx)
685 int thread_count = avctx->thread_count;
686 AVCodec *codec = avctx->codec;
687 AVCodecContext *src = avctx;
688 FrameThreadContext *fctx;
691 if (thread_count <= 1) {
692 avctx->active_thread_type = 0;
696 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
698 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
699 pthread_mutex_init(&fctx->buffer_mutex, NULL);
702 for (i = 0; i < thread_count; i++) {
703 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
704 PerThreadContext *p = &fctx->threads[i];
706 pthread_mutex_init(&p->mutex, NULL);
707 pthread_mutex_init(&p->progress_mutex, NULL);
708 pthread_cond_init(&p->input_cond, NULL);
709 pthread_cond_init(&p->progress_cond, NULL);
710 pthread_cond_init(&p->output_cond, NULL);
716 copy->thread_opaque = p;
717 copy->pkt = &p->avpkt;
723 err = codec->init(copy);
725 update_context_from_thread(avctx, copy, 1);
728 copy->priv_data = av_malloc(codec->priv_data_size);
729 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
731 if (codec->init_thread_copy)
732 err = codec->init_thread_copy(copy);
737 pthread_create(&p->thread, NULL, frame_worker_thread, p);
743 frame_thread_free(avctx, i+1);
748 void ff_thread_flush(AVCodecContext *avctx)
750 FrameThreadContext *fctx = avctx->thread_opaque;
752 if (!avctx->thread_opaque) return;
754 park_frame_worker_threads(fctx, avctx->thread_count);
755 if (fctx->prev_thread) {
756 if (fctx->prev_thread != &fctx->threads[0])
757 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
758 if (avctx->codec->flush)
759 avctx->codec->flush(fctx->threads[0].avctx);
762 fctx->next_decoding = fctx->next_finished = 0;
764 fctx->prev_thread = NULL;
767 static int *allocate_progress(PerThreadContext *p)
771 for (i = 0; i < MAX_BUFFERS; i++)
772 if (!p->progress_used[i]) break;
774 if (i == MAX_BUFFERS) {
775 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
779 p->progress_used[i] = 1;
781 return p->progress[i];
784 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
786 PerThreadContext *p = avctx->thread_opaque;
791 ff_init_buffer_info(avctx, f);
793 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
794 f->thread_opaque = NULL;
795 return avctx->get_buffer(avctx, f);
798 if (p->state != STATE_SETTING_UP &&
799 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
800 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
804 pthread_mutex_lock(&p->parent->buffer_mutex);
805 f->thread_opaque = progress = allocate_progress(p);
808 pthread_mutex_unlock(&p->parent->buffer_mutex);
815 if (avctx->thread_safe_callbacks ||
816 avctx->get_buffer == avcodec_default_get_buffer) {
817 err = avctx->get_buffer(avctx, f);
819 p->requested_frame = f;
820 p->state = STATE_GET_BUFFER;
821 pthread_mutex_lock(&p->progress_mutex);
822 pthread_cond_signal(&p->progress_cond);
824 while (p->state != STATE_SETTING_UP)
825 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
829 pthread_mutex_unlock(&p->progress_mutex);
831 if (!avctx->codec->update_thread_context)
832 ff_thread_finish_setup(avctx);
835 pthread_mutex_unlock(&p->parent->buffer_mutex);
838 * Buffer age is difficult to keep track of between
839 * multiple threads, and the optimizations it allows
840 * are not worth the effort. It is disabled for now.
847 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
849 PerThreadContext *p = avctx->thread_opaque;
850 FrameThreadContext *fctx;
852 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
853 avctx->release_buffer(avctx, f);
857 if (p->num_released_buffers >= MAX_BUFFERS) {
858 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
862 if(avctx->debug & FF_DEBUG_BUFFERS)
863 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
864 f, f->owner->internal_buffer_count);
867 pthread_mutex_lock(&fctx->buffer_mutex);
868 p->released_buffers[p->num_released_buffers++] = *f;
869 pthread_mutex_unlock(&fctx->buffer_mutex);
870 memset(f->data, 0, sizeof(f->data));
874 * Set the threading algorithms used.
876 * Threading requires more than one thread.
877 * Frame threading requires entire frames to be passed to the codec,
878 * and introduces extra decoding delay, so is incompatible with low_delay.
880 * @param avctx The context.
882 static void validate_thread_parameters(AVCodecContext *avctx)
884 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
885 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
886 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
887 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
888 if (avctx->thread_count == 1) {
889 avctx->active_thread_type = 0;
890 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
891 avctx->active_thread_type = FF_THREAD_FRAME;
892 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
893 avctx->thread_type & FF_THREAD_SLICE) {
894 avctx->active_thread_type = FF_THREAD_SLICE;
898 int ff_thread_init(AVCodecContext *avctx)
900 if (avctx->thread_opaque) {
901 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
906 validate_thread_parameters(avctx);
908 if (avctx->active_thread_type&FF_THREAD_SLICE)
909 return thread_init(avctx);
910 else if (avctx->active_thread_type&FF_THREAD_FRAME)
911 return frame_thread_init(avctx);
917 void ff_thread_free(AVCodecContext *avctx)
919 if (avctx->active_thread_type&FF_THREAD_FRAME)
920 frame_thread_free(avctx, avctx->thread_count);