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
37 typedef int (action_func)(AVCodecContext *c, void *arg);
38 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
40 typedef struct ThreadContext {
50 pthread_cond_t last_job_cond;
51 pthread_cond_t current_job_cond;
52 pthread_mutex_t current_job_lock;
57 /// Max number of frame buffers that can be allocated when using frame threads.
58 #define MAX_BUFFERS (32+1)
61 * Context used by codec threads and stored in their AVCodecContext thread_opaque.
63 typedef struct PerThreadContext {
64 struct FrameThreadContext *parent;
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.
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.
74 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
76 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
77 int allocated_buf_size; ///< Size allocated for avpkt.data
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.
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.
90 STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
94 * Array of frames passed to ff_thread_release_buffer().
95 * Frames are released after all threads referencing them are finished.
97 AVFrame released_buffers[MAX_BUFFERS];
98 int num_released_buffers;
101 * Array of progress values used by ff_thread_get_buffer().
103 int progress[MAX_BUFFERS][2];
104 uint8_t progress_used[MAX_BUFFERS];
106 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
110 * Context stored in the client AVCodecContext thread_opaque.
112 typedef struct FrameThreadContext {
113 PerThreadContext *threads; ///< The contexts for each thread.
114 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
116 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
118 int next_decoding; ///< The next context to submit a packet to.
119 int next_finished; ///< The next context to return output from.
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.
126 int die; ///< Set when threads should exit.
127 } FrameThreadContext;
129 static void* attribute_align_arg worker(void *v)
131 AVCodecContext *avctx = v;
132 ThreadContext *c = avctx->thread_opaque;
133 int our_job = c->job_count;
134 int thread_count = avctx->thread_count;
137 pthread_mutex_lock(&c->current_job_lock);
138 self_id = c->current_job++;
140 while (our_job >= c->job_count) {
141 if (c->current_job == thread_count + c->job_count)
142 pthread_cond_signal(&c->last_job_cond);
144 pthread_cond_wait(&c->current_job_cond, &c->current_job_lock);
148 pthread_mutex_unlock(&c->current_job_lock);
152 pthread_mutex_unlock(&c->current_job_lock);
154 c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
155 c->func2(avctx, c->args, our_job, self_id);
157 pthread_mutex_lock(&c->current_job_lock);
158 our_job = c->current_job++;
162 static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
164 pthread_cond_wait(&c->last_job_cond, &c->current_job_lock);
165 pthread_mutex_unlock(&c->current_job_lock);
168 static void thread_free(AVCodecContext *avctx)
170 ThreadContext *c = avctx->thread_opaque;
173 pthread_mutex_lock(&c->current_job_lock);
175 pthread_cond_broadcast(&c->current_job_cond);
176 pthread_mutex_unlock(&c->current_job_lock);
178 for (i=0; i<avctx->thread_count; i++)
179 pthread_join(c->workers[i], NULL);
181 pthread_mutex_destroy(&c->current_job_lock);
182 pthread_cond_destroy(&c->current_job_cond);
183 pthread_cond_destroy(&c->last_job_cond);
185 av_freep(&avctx->thread_opaque);
188 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
190 ThreadContext *c= avctx->thread_opaque;
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);
199 pthread_mutex_lock(&c->current_job_lock);
201 c->current_job = avctx->thread_count;
202 c->job_count = job_count;
203 c->job_size = job_size;
208 c->rets_count = job_count;
210 c->rets = &dummy_ret;
213 pthread_cond_broadcast(&c->current_job_cond);
215 avcodec_thread_park_workers(c, avctx->thread_count);
220 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
222 ThreadContext *c= avctx->thread_opaque;
224 return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
227 static int thread_init(AVCodecContext *avctx)
231 int thread_count = avctx->thread_count;
233 if (thread_count <= 1)
236 c = av_mallocz(sizeof(ThreadContext));
240 c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
246 avctx->thread_opaque = c;
251 pthread_cond_init(&c->current_job_cond, NULL);
252 pthread_cond_init(&c->last_job_cond, NULL);
253 pthread_mutex_init(&c->current_job_lock, NULL);
254 pthread_mutex_lock(&c->current_job_lock);
255 for (i=0; i<thread_count; i++) {
256 if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
257 avctx->thread_count = i;
258 pthread_mutex_unlock(&c->current_job_lock);
259 ff_thread_free(avctx);
264 avcodec_thread_park_workers(c, thread_count);
266 avctx->execute = avcodec_thread_execute;
267 avctx->execute2 = avcodec_thread_execute2;
272 * Codec worker thread.
274 * Automatically calls ff_thread_finish_setup() if the codec does
275 * not provide an update_thread_context method, or if the codec returns
278 static attribute_align_arg void *frame_worker_thread(void *arg)
280 PerThreadContext *p = arg;
281 FrameThreadContext *fctx = p->parent;
282 AVCodecContext *avctx = p->avctx;
283 AVCodec *codec = avctx->codec;
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);
293 if (fctx->die) break;
295 if (!codec->update_thread_context && avctx->thread_safe_callbacks)
296 ff_thread_finish_setup(avctx);
298 pthread_mutex_lock(&p->mutex);
299 avcodec_get_frame_defaults(&p->frame);
301 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
303 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
305 p->state = STATE_INPUT_READY;
307 pthread_mutex_lock(&p->progress_mutex);
308 pthread_cond_signal(&p->output_cond);
309 pthread_mutex_unlock(&p->progress_mutex);
311 pthread_mutex_unlock(&p->mutex);
318 * Updates the next thread's AVCodecContext with values from the reference thread's context.
320 * @param dst The destination context.
321 * @param src The source context.
322 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
324 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
329 dst->sub_id = src->sub_id;
330 dst->time_base = src->time_base;
331 dst->width = src->width;
332 dst->height = src->height;
333 dst->pix_fmt = src->pix_fmt;
335 dst->has_b_frames = src->has_b_frames;
336 dst->idct_algo = src->idct_algo;
337 dst->slice_count = src->slice_count;
339 dst->bits_per_coded_sample = src->bits_per_coded_sample;
340 dst->sample_aspect_ratio = src->sample_aspect_ratio;
341 dst->dtg_active_format = src->dtg_active_format;
343 dst->profile = src->profile;
344 dst->level = src->level;
346 dst->bits_per_raw_sample = src->bits_per_raw_sample;
347 dst->ticks_per_frame = src->ticks_per_frame;
348 dst->color_primaries = src->color_primaries;
350 dst->color_trc = src->color_trc;
351 dst->colorspace = src->colorspace;
352 dst->color_range = src->color_range;
353 dst->chroma_sample_location = src->chroma_sample_location;
357 dst->coded_frame = src->coded_frame;
358 dst->has_b_frames += src->thread_count - 1;
360 if (dst->codec->update_thread_context)
361 err = dst->codec->update_thread_context(dst, src);
368 * Update the next thread's AVCodecContext with values set by the user.
370 * @param dst The destination context.
371 * @param src The source context.
373 static void update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
375 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
376 dst->flags = src->flags;
378 dst->draw_horiz_band= src->draw_horiz_band;
379 dst->get_buffer = src->get_buffer;
380 dst->release_buffer = src->release_buffer;
382 dst->opaque = src->opaque;
383 dst->dsp_mask = src->dsp_mask;
384 dst->debug = src->debug;
385 dst->debug_mv = src->debug_mv;
387 dst->slice_flags = src->slice_flags;
388 dst->flags2 = src->flags2;
390 copy_fields(skip_loop_filter, bidir_refine);
392 dst->frame_number = src->frame_number;
393 dst->reordered_opaque = src->reordered_opaque;
397 static void free_progress(AVFrame *f)
399 PerThreadContext *p = f->owner->thread_opaque;
400 int *progress = f->thread_opaque;
402 p->progress_used[(progress - p->progress[0]) / 2] = 0;
405 /// Releases the buffers that this decoding thread was the last user of.
406 static void release_delayed_buffers(PerThreadContext *p)
408 FrameThreadContext *fctx = p->parent;
410 while (p->num_released_buffers > 0) {
413 pthread_mutex_lock(&fctx->buffer_mutex);
414 f = &p->released_buffers[--p->num_released_buffers];
416 f->thread_opaque = NULL;
418 f->owner->release_buffer(f->owner, f);
419 pthread_mutex_unlock(&fctx->buffer_mutex);
423 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
425 FrameThreadContext *fctx = p->parent;
426 PerThreadContext *prev_thread = fctx->prev_thread;
427 AVCodec *codec = p->avctx->codec;
428 uint8_t *buf = p->avpkt.data;
430 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
432 pthread_mutex_lock(&p->mutex);
434 release_delayed_buffers(p);
438 if (prev_thread->state == STATE_SETTING_UP) {
439 pthread_mutex_lock(&prev_thread->progress_mutex);
440 while (prev_thread->state == STATE_SETTING_UP)
441 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
442 pthread_mutex_unlock(&prev_thread->progress_mutex);
445 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
447 pthread_mutex_unlock(&p->mutex);
452 av_fast_malloc(&buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
455 memcpy(buf, avpkt->data, avpkt->size);
456 memset(buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
458 p->state = STATE_SETTING_UP;
459 pthread_cond_signal(&p->input_cond);
460 pthread_mutex_unlock(&p->mutex);
463 * If the client doesn't have a thread-safe get_buffer(),
464 * then decoding threads call back to the main thread,
465 * and it calls back to the client here.
468 if (!p->avctx->thread_safe_callbacks &&
469 p->avctx->get_buffer != avcodec_default_get_buffer) {
470 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
471 pthread_mutex_lock(&p->progress_mutex);
472 while (p->state == STATE_SETTING_UP)
473 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
475 if (p->state == STATE_GET_BUFFER) {
476 p->result = p->avctx->get_buffer(p->avctx, p->requested_frame);
477 p->state = STATE_SETTING_UP;
478 pthread_cond_signal(&p->progress_cond);
480 pthread_mutex_unlock(&p->progress_mutex);
484 fctx->prev_thread = p;
489 int ff_thread_decode_frame(AVCodecContext *avctx,
490 AVFrame *picture, int *got_picture_ptr,
493 FrameThreadContext *fctx = avctx->thread_opaque;
494 int finished = fctx->next_finished;
499 * Submit a packet to the next decoding thread.
502 p = &fctx->threads[fctx->next_decoding];
503 update_context_from_user(p->avctx, avctx);
504 err = submit_packet(p, avpkt);
507 fctx->next_decoding++;
510 * If we're still receiving the initial packets, don't return a frame.
513 if (fctx->delaying && avpkt->size) {
514 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
521 * Return the next available frame from the oldest thread.
522 * If we're at the end of the stream, then we have to skip threads that
523 * didn't output a frame, because we don't want to accidentally signal
524 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
528 p = &fctx->threads[finished++];
530 if (p->state != STATE_INPUT_READY) {
531 pthread_mutex_lock(&p->progress_mutex);
532 while (p->state != STATE_INPUT_READY)
533 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
534 pthread_mutex_unlock(&p->progress_mutex);
538 *got_picture_ptr = p->got_frame;
539 picture->pkt_dts = p->avpkt.dts;
542 * A later call with avkpt->size == 0 may loop over all threads,
543 * including this one, searching for a frame to return before being
544 * stopped by the "finished != fctx->next_finished" condition.
545 * Make sure we don't mistakenly return the same frame again.
549 if (finished >= avctx->thread_count) finished = 0;
550 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
552 update_context_from_thread(avctx, p->avctx, 1);
554 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
556 fctx->next_finished = finished;
561 void ff_thread_report_progress(AVFrame *f, int n, int field)
564 int *progress = f->thread_opaque;
566 if (!progress || progress[field] >= n) return;
568 p = f->owner->thread_opaque;
570 if (f->owner->debug&FF_DEBUG_THREADS)
571 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
573 pthread_mutex_lock(&p->progress_mutex);
575 pthread_cond_broadcast(&p->progress_cond);
576 pthread_mutex_unlock(&p->progress_mutex);
579 void ff_thread_await_progress(AVFrame *f, int n, int field)
582 int *progress = f->thread_opaque;
584 if (!progress || progress[field] >= n) return;
586 p = f->owner->thread_opaque;
588 if (f->owner->debug&FF_DEBUG_THREADS)
589 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
591 pthread_mutex_lock(&p->progress_mutex);
592 while (progress[field] < n)
593 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
594 pthread_mutex_unlock(&p->progress_mutex);
597 void ff_thread_finish_setup(AVCodecContext *avctx) {
598 PerThreadContext *p = avctx->thread_opaque;
600 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
602 if(p->state == STATE_SETUP_FINISHED){
603 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
606 pthread_mutex_lock(&p->progress_mutex);
607 p->state = STATE_SETUP_FINISHED;
608 pthread_cond_broadcast(&p->progress_cond);
609 pthread_mutex_unlock(&p->progress_mutex);
612 /// Waits for all threads to finish.
613 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
617 for (i = 0; i < thread_count; i++) {
618 PerThreadContext *p = &fctx->threads[i];
620 if (p->state != STATE_INPUT_READY) {
621 pthread_mutex_lock(&p->progress_mutex);
622 while (p->state != STATE_INPUT_READY)
623 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
624 pthread_mutex_unlock(&p->progress_mutex);
629 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
631 FrameThreadContext *fctx = avctx->thread_opaque;
632 AVCodec *codec = avctx->codec;
635 park_frame_worker_threads(fctx, thread_count);
637 if (fctx->prev_thread)
638 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
642 for (i = 0; i < thread_count; i++) {
643 PerThreadContext *p = &fctx->threads[i];
645 pthread_mutex_lock(&p->mutex);
646 pthread_cond_signal(&p->input_cond);
647 pthread_mutex_unlock(&p->mutex);
649 pthread_join(p->thread, NULL);
652 codec->close(p->avctx);
656 release_delayed_buffers(p);
659 for (i = 0; i < thread_count; i++) {
660 PerThreadContext *p = &fctx->threads[i];
662 avcodec_default_free_buffers(p->avctx);
664 pthread_mutex_destroy(&p->mutex);
665 pthread_mutex_destroy(&p->progress_mutex);
666 pthread_cond_destroy(&p->input_cond);
667 pthread_cond_destroy(&p->progress_cond);
668 pthread_cond_destroy(&p->output_cond);
669 av_freep(&p->avpkt.data);
672 av_freep(&p->avctx->priv_data);
677 av_freep(&fctx->threads);
678 pthread_mutex_destroy(&fctx->buffer_mutex);
679 av_freep(&avctx->thread_opaque);
682 static int frame_thread_init(AVCodecContext *avctx)
684 int thread_count = avctx->thread_count;
685 AVCodec *codec = avctx->codec;
686 AVCodecContext *src = avctx;
687 FrameThreadContext *fctx;
690 if (thread_count <= 1) {
691 avctx->active_thread_type = 0;
695 avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
697 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
698 pthread_mutex_init(&fctx->buffer_mutex, NULL);
701 for (i = 0; i < thread_count; i++) {
702 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
703 PerThreadContext *p = &fctx->threads[i];
705 pthread_mutex_init(&p->mutex, NULL);
706 pthread_mutex_init(&p->progress_mutex, NULL);
707 pthread_cond_init(&p->input_cond, NULL);
708 pthread_cond_init(&p->progress_cond, NULL);
709 pthread_cond_init(&p->output_cond, NULL);
715 copy->thread_opaque = p;
716 copy->pkt = &p->avpkt;
722 err = codec->init(copy);
724 update_context_from_thread(avctx, copy, 1);
727 copy->priv_data = av_malloc(codec->priv_data_size);
728 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
730 if (codec->init_thread_copy)
731 err = codec->init_thread_copy(copy);
736 pthread_create(&p->thread, NULL, frame_worker_thread, p);
742 frame_thread_free(avctx, i+1);
747 void ff_thread_flush(AVCodecContext *avctx)
749 FrameThreadContext *fctx = avctx->thread_opaque;
751 if (!avctx->thread_opaque) return;
753 park_frame_worker_threads(fctx, avctx->thread_count);
754 if (fctx->prev_thread) {
755 if (fctx->prev_thread != &fctx->threads[0])
756 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
757 if (avctx->codec->flush)
758 avctx->codec->flush(fctx->threads[0].avctx);
761 fctx->next_decoding = fctx->next_finished = 0;
763 fctx->prev_thread = NULL;
766 static int *allocate_progress(PerThreadContext *p)
770 for (i = 0; i < MAX_BUFFERS; i++)
771 if (!p->progress_used[i]) break;
773 if (i == MAX_BUFFERS) {
774 av_log(p->avctx, AV_LOG_ERROR, "allocate_progress() overflow\n");
778 p->progress_used[i] = 1;
780 return p->progress[i];
783 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f)
785 PerThreadContext *p = avctx->thread_opaque;
790 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
791 f->thread_opaque = NULL;
792 return avctx->get_buffer(avctx, f);
795 if (p->state != STATE_SETTING_UP &&
796 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
797 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
801 pthread_mutex_lock(&p->parent->buffer_mutex);
802 f->thread_opaque = progress = allocate_progress(p);
805 pthread_mutex_unlock(&p->parent->buffer_mutex);
812 if (avctx->thread_safe_callbacks ||
813 avctx->get_buffer == avcodec_default_get_buffer) {
814 err = avctx->get_buffer(avctx, f);
816 p->requested_frame = f;
817 p->state = STATE_GET_BUFFER;
818 pthread_mutex_lock(&p->progress_mutex);
819 pthread_cond_signal(&p->progress_cond);
821 while (p->state != STATE_SETTING_UP)
822 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
826 pthread_mutex_unlock(&p->progress_mutex);
828 if (!avctx->codec->update_thread_context)
829 ff_thread_finish_setup(avctx);
832 pthread_mutex_unlock(&p->parent->buffer_mutex);
835 * Buffer age is difficult to keep track of between
836 * multiple threads, and the optimizations it allows
837 * are not worth the effort. It is disabled for now.
844 void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
846 PerThreadContext *p = avctx->thread_opaque;
847 FrameThreadContext *fctx;
849 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) {
850 avctx->release_buffer(avctx, f);
854 if (p->num_released_buffers >= MAX_BUFFERS) {
855 av_log(p->avctx, AV_LOG_ERROR, "too many thread_release_buffer calls!\n");
859 if(avctx->debug & FF_DEBUG_BUFFERS)
860 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p, %d buffers used\n",
861 f, f->owner->internal_buffer_count);
864 pthread_mutex_lock(&fctx->buffer_mutex);
865 p->released_buffers[p->num_released_buffers++] = *f;
866 pthread_mutex_unlock(&fctx->buffer_mutex);
867 memset(f->data, 0, sizeof(f->data));
871 * Set the threading algorithms used.
873 * Threading requires more than one thread.
874 * Frame threading requires entire frames to be passed to the codec,
875 * and introduces extra decoding delay, so is incompatible with low_delay.
877 * @param avctx The context.
879 static void validate_thread_parameters(AVCodecContext *avctx)
881 int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
882 && !(avctx->flags & CODEC_FLAG_TRUNCATED)
883 && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
884 && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
885 if (avctx->thread_count == 1) {
886 avctx->active_thread_type = 0;
887 } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
888 avctx->active_thread_type = FF_THREAD_FRAME;
889 } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
890 avctx->thread_type & FF_THREAD_SLICE) {
891 avctx->active_thread_type = FF_THREAD_SLICE;
895 int ff_thread_init(AVCodecContext *avctx)
897 if (avctx->thread_opaque) {
898 av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
903 validate_thread_parameters(avctx);
905 if (avctx->active_thread_type&FF_THREAD_SLICE)
906 return thread_init(avctx);
907 else if (avctx->active_thread_type&FF_THREAD_FRAME)
908 return frame_thread_init(avctx);
914 void ff_thread_free(AVCodecContext *avctx)
916 if (avctx->active_thread_type&FF_THREAD_FRAME)
917 frame_thread_free(avctx, avctx->thread_count);