2 * This file is part of FFmpeg.
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 * Frame multithreading support functions
22 * @see doc/multithreading.txt
32 #include "compat/w32pthreads.h"
34 #include "compat/os2threads.h"
39 #include "pthread_internal.h"
42 #include "libavutil/avassert.h"
43 #include "libavutil/buffer.h"
44 #include "libavutil/common.h"
45 #include "libavutil/cpu.h"
46 #include "libavutil/frame.h"
47 #include "libavutil/log.h"
48 #include "libavutil/mem.h"
51 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
53 typedef struct PerThreadContext {
54 struct FrameThreadContext *parent;
58 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
59 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
60 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
62 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
63 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
65 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
67 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
68 uint8_t *buf; ///< backup storage for packet data when the input packet is not refcounted
69 int allocated_buf_size; ///< Size allocated for buf
71 AVFrame frame; ///< Output frame (for decoding) or input (for encoding).
72 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
73 int result; ///< The result of the last codec decode/encode() call.
76 STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
77 STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
78 STATE_GET_BUFFER, /**<
79 * Set when the codec calls get_buffer().
80 * State is returned to STATE_SETTING_UP afterwards.
82 STATE_GET_FORMAT, /**<
83 * Set when the codec calls get_format().
84 * State is returned to STATE_SETTING_UP afterwards.
86 STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
90 * Array of frames passed to ff_thread_release_buffer().
91 * Frames are released after all threads referencing them are finished.
93 AVFrame *released_buffers;
94 int num_released_buffers;
95 int released_buffers_allocated;
97 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
98 int requested_flags; ///< flags passed to get_buffer() for requested_frame
100 const enum AVPixelFormat *available_formats; ///< Format array for get_format()
101 enum AVPixelFormat result_format; ///< get_format() result
105 * Context stored in the client AVCodecInternal thread_ctx.
107 typedef struct FrameThreadContext {
108 PerThreadContext *threads; ///< The contexts for each thread.
109 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
111 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
113 int next_decoding; ///< The next context to submit a packet to.
114 int next_finished; ///< The next context to return output from.
117 * Set for the first N packets, where N is the number of threads.
118 * While it is set, ff_thread_en/decode_frame won't return any results.
121 int die; ///< Set when threads should exit.
122 } FrameThreadContext;
124 #define THREAD_SAFE_CALLBACKS(avctx) \
125 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
128 * Codec worker thread.
130 * Automatically calls ff_thread_finish_setup() if the codec does
131 * not provide an update_thread_context method, or if the codec returns
134 static attribute_align_arg void *frame_worker_thread(void *arg)
136 PerThreadContext *p = arg;
137 FrameThreadContext *fctx = p->parent;
138 AVCodecContext *avctx = p->avctx;
139 const AVCodec *codec = avctx->codec;
141 pthread_mutex_lock(&p->mutex);
143 while (p->state == STATE_INPUT_READY && !fctx->die)
144 pthread_cond_wait(&p->input_cond, &p->mutex);
146 if (fctx->die) break;
148 if (!codec->update_thread_context && THREAD_SAFE_CALLBACKS(avctx))
149 ff_thread_finish_setup(avctx);
151 avcodec_get_frame_defaults(&p->frame);
153 p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
155 /* many decoders assign whole AVFrames, thus overwriting extended_data;
156 * make sure it's set correctly */
157 p->frame.extended_data = p->frame.data;
159 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
161 pthread_mutex_lock(&p->progress_mutex);
163 for (i = 0; i < MAX_BUFFERS; i++)
164 if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
165 p->progress[i][0] = INT_MAX;
166 p->progress[i][1] = INT_MAX;
169 p->state = STATE_INPUT_READY;
171 pthread_cond_broadcast(&p->progress_cond);
172 pthread_cond_signal(&p->output_cond);
173 pthread_mutex_unlock(&p->progress_mutex);
175 pthread_mutex_unlock(&p->mutex);
181 * Update the next thread's AVCodecContext with values from the reference thread's context.
183 * @param dst The destination context.
184 * @param src The source context.
185 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
187 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
192 dst->time_base = src->time_base;
193 dst->width = src->width;
194 dst->height = src->height;
195 dst->pix_fmt = src->pix_fmt;
197 dst->coded_width = src->coded_width;
198 dst->coded_height = src->coded_height;
200 dst->has_b_frames = src->has_b_frames;
201 dst->idct_algo = src->idct_algo;
203 dst->bits_per_coded_sample = src->bits_per_coded_sample;
204 dst->sample_aspect_ratio = src->sample_aspect_ratio;
205 dst->dtg_active_format = src->dtg_active_format;
207 dst->profile = src->profile;
208 dst->level = src->level;
210 dst->bits_per_raw_sample = src->bits_per_raw_sample;
211 dst->ticks_per_frame = src->ticks_per_frame;
212 dst->color_primaries = src->color_primaries;
214 dst->color_trc = src->color_trc;
215 dst->colorspace = src->colorspace;
216 dst->color_range = src->color_range;
217 dst->chroma_sample_location = src->chroma_sample_location;
219 dst->hwaccel = src->hwaccel;
220 dst->hwaccel_context = src->hwaccel_context;
222 dst->channels = src->channels;
223 dst->sample_rate = src->sample_rate;
224 dst->sample_fmt = src->sample_fmt;
225 dst->channel_layout = src->channel_layout;
229 dst->delay = src->thread_count - 1;
230 dst->coded_frame = src->coded_frame;
232 if (dst->codec->update_thread_context)
233 err = dst->codec->update_thread_context(dst, src);
240 * Update the next thread's AVCodecContext with values set by the user.
242 * @param dst The destination context.
243 * @param src The source context.
244 * @return 0 on success, negative error code on failure
246 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
248 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
249 dst->flags = src->flags;
251 dst->draw_horiz_band= src->draw_horiz_band;
252 dst->get_buffer2 = src->get_buffer2;
253 #if FF_API_GET_BUFFER
254 FF_DISABLE_DEPRECATION_WARNINGS
255 dst->get_buffer = src->get_buffer;
256 dst->release_buffer = src->release_buffer;
257 FF_ENABLE_DEPRECATION_WARNINGS
260 dst->opaque = src->opaque;
261 dst->debug = src->debug;
262 dst->debug_mv = src->debug_mv;
264 dst->slice_flags = src->slice_flags;
265 dst->flags2 = src->flags2;
267 copy_fields(skip_loop_filter, subtitle_header);
269 dst->frame_number = src->frame_number;
270 dst->reordered_opaque = src->reordered_opaque;
271 dst->thread_safe_callbacks = src->thread_safe_callbacks;
273 if (src->slice_count && src->slice_offset) {
274 if (dst->slice_count < src->slice_count) {
275 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
276 sizeof(*dst->slice_offset));
278 av_free(dst->slice_offset);
279 return AVERROR(ENOMEM);
281 dst->slice_offset = tmp;
283 memcpy(dst->slice_offset, src->slice_offset,
284 src->slice_count * sizeof(*dst->slice_offset));
286 dst->slice_count = src->slice_count;
291 /// Releases the buffers that this decoding thread was the last user of.
292 static void release_delayed_buffers(PerThreadContext *p)
294 FrameThreadContext *fctx = p->parent;
296 while (p->num_released_buffers > 0) {
299 pthread_mutex_lock(&fctx->buffer_mutex);
301 // fix extended data in case the caller screwed it up
302 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
303 p->avctx->codec_type == AVMEDIA_TYPE_AUDIO);
304 f = &p->released_buffers[--p->num_released_buffers];
305 f->extended_data = f->data;
308 pthread_mutex_unlock(&fctx->buffer_mutex);
312 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
314 FrameThreadContext *fctx = p->parent;
315 PerThreadContext *prev_thread = fctx->prev_thread;
316 const AVCodec *codec = p->avctx->codec;
318 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
320 pthread_mutex_lock(&p->mutex);
322 release_delayed_buffers(p);
326 if (prev_thread->state == STATE_SETTING_UP) {
327 pthread_mutex_lock(&prev_thread->progress_mutex);
328 while (prev_thread->state == STATE_SETTING_UP)
329 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
330 pthread_mutex_unlock(&prev_thread->progress_mutex);
333 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
335 pthread_mutex_unlock(&p->mutex);
340 av_buffer_unref(&p->avpkt.buf);
343 p->avpkt.buf = av_buffer_ref(avpkt->buf);
345 av_fast_malloc(&p->buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
347 pthread_mutex_unlock(&p->mutex);
348 return AVERROR(ENOMEM);
350 p->avpkt.data = p->buf;
351 memcpy(p->buf, avpkt->data, avpkt->size);
352 memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
355 p->state = STATE_SETTING_UP;
356 pthread_cond_signal(&p->input_cond);
357 pthread_mutex_unlock(&p->mutex);
360 * If the client doesn't have a thread-safe get_buffer(),
361 * then decoding threads call back to the main thread,
362 * and it calls back to the client here.
365 FF_DISABLE_DEPRECATION_WARNINGS
366 if (!p->avctx->thread_safe_callbacks && (
367 p->avctx->get_format != avcodec_default_get_format ||
368 #if FF_API_GET_BUFFER
369 p->avctx->get_buffer ||
371 p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
372 FF_ENABLE_DEPRECATION_WARNINGS
373 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
375 pthread_mutex_lock(&p->progress_mutex);
376 while (p->state == STATE_SETTING_UP)
377 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
380 case STATE_GET_BUFFER:
381 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
383 case STATE_GET_FORMAT:
384 p->result_format = p->avctx->get_format(p->avctx, p->available_formats);
391 p->state = STATE_SETTING_UP;
392 pthread_cond_signal(&p->progress_cond);
394 pthread_mutex_unlock(&p->progress_mutex);
398 fctx->prev_thread = p;
399 fctx->next_decoding++;
404 int ff_thread_decode_frame(AVCodecContext *avctx,
405 AVFrame *picture, int *got_picture_ptr,
408 FrameThreadContext *fctx = avctx->internal->thread_ctx;
409 int finished = fctx->next_finished;
414 * Submit a packet to the next decoding thread.
417 p = &fctx->threads[fctx->next_decoding];
418 err = update_context_from_user(p->avctx, avctx);
420 err = submit_packet(p, avpkt);
424 * If we're still receiving the initial packets, don't return a frame.
427 if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
430 if (fctx->delaying) {
437 * Return the next available frame from the oldest thread.
438 * If we're at the end of the stream, then we have to skip threads that
439 * didn't output a frame, because we don't want to accidentally signal
440 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
444 p = &fctx->threads[finished++];
446 if (p->state != STATE_INPUT_READY) {
447 pthread_mutex_lock(&p->progress_mutex);
448 while (p->state != STATE_INPUT_READY)
449 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
450 pthread_mutex_unlock(&p->progress_mutex);
453 av_frame_move_ref(picture, &p->frame);
454 *got_picture_ptr = p->got_frame;
455 picture->pkt_dts = p->avpkt.dts;
458 * A later call with avkpt->size == 0 may loop over all threads,
459 * including this one, searching for a frame to return before being
460 * stopped by the "finished != fctx->next_finished" condition.
461 * Make sure we don't mistakenly return the same frame again.
465 if (finished >= avctx->thread_count) finished = 0;
466 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
468 update_context_from_thread(avctx, p->avctx, 1);
470 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
472 fctx->next_finished = finished;
474 /* return the size of the consumed packet if no error occurred */
475 return (p->result >= 0) ? avpkt->size : p->result;
478 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
481 volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
483 if (!progress || progress[field] >= n) return;
485 p = f->owner->internal->thread_ctx;
487 if (f->owner->debug&FF_DEBUG_THREADS)
488 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
490 pthread_mutex_lock(&p->progress_mutex);
492 pthread_cond_broadcast(&p->progress_cond);
493 pthread_mutex_unlock(&p->progress_mutex);
496 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
499 volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
501 if (!progress || progress[field] >= n) return;
503 p = f->owner->internal->thread_ctx;
505 if (f->owner->debug&FF_DEBUG_THREADS)
506 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
508 pthread_mutex_lock(&p->progress_mutex);
509 while (progress[field] < n)
510 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
511 pthread_mutex_unlock(&p->progress_mutex);
514 void ff_thread_finish_setup(AVCodecContext *avctx) {
515 PerThreadContext *p = avctx->internal->thread_ctx;
517 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
519 if(p->state == STATE_SETUP_FINISHED){
520 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
523 pthread_mutex_lock(&p->progress_mutex);
524 p->state = STATE_SETUP_FINISHED;
525 pthread_cond_broadcast(&p->progress_cond);
526 pthread_mutex_unlock(&p->progress_mutex);
529 /// Waits for all threads to finish.
530 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
534 for (i = 0; i < thread_count; i++) {
535 PerThreadContext *p = &fctx->threads[i];
537 if (p->state != STATE_INPUT_READY) {
538 pthread_mutex_lock(&p->progress_mutex);
539 while (p->state != STATE_INPUT_READY)
540 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
541 pthread_mutex_unlock(&p->progress_mutex);
547 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
549 FrameThreadContext *fctx = avctx->internal->thread_ctx;
550 const AVCodec *codec = avctx->codec;
553 park_frame_worker_threads(fctx, thread_count);
555 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
556 if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
557 av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
558 fctx->prev_thread->avctx->internal->is_copy = fctx->threads->avctx->internal->is_copy;
559 fctx->threads->avctx->internal->is_copy = 1;
564 for (i = 0; i < thread_count; i++) {
565 PerThreadContext *p = &fctx->threads[i];
567 pthread_mutex_lock(&p->mutex);
568 pthread_cond_signal(&p->input_cond);
569 pthread_mutex_unlock(&p->mutex);
572 pthread_join(p->thread, NULL);
576 codec->close(p->avctx);
580 release_delayed_buffers(p);
581 av_frame_unref(&p->frame);
584 for (i = 0; i < thread_count; i++) {
585 PerThreadContext *p = &fctx->threads[i];
587 pthread_mutex_destroy(&p->mutex);
588 pthread_mutex_destroy(&p->progress_mutex);
589 pthread_cond_destroy(&p->input_cond);
590 pthread_cond_destroy(&p->progress_cond);
591 pthread_cond_destroy(&p->output_cond);
592 av_buffer_unref(&p->avpkt.buf);
594 av_freep(&p->released_buffers);
597 av_freep(&p->avctx->priv_data);
598 av_freep(&p->avctx->slice_offset);
601 av_freep(&p->avctx->internal);
605 av_freep(&fctx->threads);
606 pthread_mutex_destroy(&fctx->buffer_mutex);
607 av_freep(&avctx->internal->thread_ctx);
610 int ff_frame_thread_init(AVCodecContext *avctx)
612 int thread_count = avctx->thread_count;
613 const AVCodec *codec = avctx->codec;
614 AVCodecContext *src = avctx;
615 FrameThreadContext *fctx;
623 int nb_cpus = av_cpu_count();
624 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
626 // use number of cores + 1 as thread count if there is more than one
628 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
630 thread_count = avctx->thread_count = 1;
633 if (thread_count <= 1) {
634 avctx->active_thread_type = 0;
638 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
640 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
641 pthread_mutex_init(&fctx->buffer_mutex, NULL);
644 for (i = 0; i < thread_count; i++) {
645 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
646 PerThreadContext *p = &fctx->threads[i];
648 pthread_mutex_init(&p->mutex, NULL);
649 pthread_mutex_init(&p->progress_mutex, NULL);
650 pthread_cond_init(&p->input_cond, NULL);
651 pthread_cond_init(&p->progress_cond, NULL);
652 pthread_cond_init(&p->output_cond, NULL);
658 err = AVERROR(ENOMEM);
664 copy->internal = av_malloc(sizeof(AVCodecInternal));
665 if (!copy->internal) {
666 err = AVERROR(ENOMEM);
669 *copy->internal = *src->internal;
670 copy->internal->thread_ctx = p;
671 copy->internal->pkt = &p->avpkt;
677 err = codec->init(copy);
679 update_context_from_thread(avctx, copy, 1);
681 copy->priv_data = av_malloc(codec->priv_data_size);
682 if (!copy->priv_data) {
683 err = AVERROR(ENOMEM);
686 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
687 copy->internal->is_copy = 1;
689 if (codec->init_thread_copy)
690 err = codec->init_thread_copy(copy);
695 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
696 p->thread_init= !err;
704 ff_frame_thread_free(avctx, i+1);
709 void ff_thread_flush(AVCodecContext *avctx)
712 FrameThreadContext *fctx = avctx->internal->thread_ctx;
716 park_frame_worker_threads(fctx, avctx->thread_count);
717 if (fctx->prev_thread) {
718 if (fctx->prev_thread != &fctx->threads[0])
719 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
720 if (avctx->codec->flush)
721 avctx->codec->flush(fctx->threads[0].avctx);
724 fctx->next_decoding = fctx->next_finished = 0;
726 fctx->prev_thread = NULL;
727 for (i = 0; i < avctx->thread_count; i++) {
728 PerThreadContext *p = &fctx->threads[i];
729 // Make sure decode flush calls with size=0 won't return old frames
731 av_frame_unref(&p->frame);
733 release_delayed_buffers(p);
737 int ff_thread_can_start_frame(AVCodecContext *avctx)
739 PerThreadContext *p = avctx->internal->thread_ctx;
740 if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
741 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
747 static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
749 PerThreadContext *p = avctx->internal->thread_ctx;
754 ff_init_buffer_info(avctx, f->f);
756 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
757 return ff_get_buffer(avctx, f->f, flags);
759 if (p->state != STATE_SETTING_UP &&
760 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
761 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
765 if (avctx->internal->allocate_progress) {
767 f->progress = av_buffer_alloc(2 * sizeof(int));
769 return AVERROR(ENOMEM);
771 progress = (int*)f->progress->data;
773 progress[0] = progress[1] = -1;
776 pthread_mutex_lock(&p->parent->buffer_mutex);
777 FF_DISABLE_DEPRECATION_WARNINGS
778 if (avctx->thread_safe_callbacks || (
779 #if FF_API_GET_BUFFER
780 !avctx->get_buffer &&
782 avctx->get_buffer2 == avcodec_default_get_buffer2)) {
783 FF_ENABLE_DEPRECATION_WARNINGS
784 err = ff_get_buffer(avctx, f->f, flags);
786 pthread_mutex_lock(&p->progress_mutex);
787 p->requested_frame = f->f;
788 p->requested_flags = flags;
789 p->state = STATE_GET_BUFFER;
790 pthread_cond_broadcast(&p->progress_cond);
792 while (p->state != STATE_SETTING_UP)
793 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
797 pthread_mutex_unlock(&p->progress_mutex);
800 if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
801 ff_thread_finish_setup(avctx);
804 av_buffer_unref(&f->progress);
806 pthread_mutex_unlock(&p->parent->buffer_mutex);
811 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
813 enum AVPixelFormat res;
814 PerThreadContext *p = avctx->internal->thread_ctx;
815 if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
816 avctx->get_format == avcodec_default_get_format)
817 return avctx->get_format(avctx, fmt);
818 if (p->state != STATE_SETTING_UP) {
819 av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
822 pthread_mutex_lock(&p->progress_mutex);
823 p->available_formats = fmt;
824 p->state = STATE_GET_FORMAT;
825 pthread_cond_broadcast(&p->progress_cond);
827 while (p->state != STATE_SETTING_UP)
828 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
830 res = p->result_format;
832 pthread_mutex_unlock(&p->progress_mutex);
837 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
839 int ret = thread_get_buffer_internal(avctx, f, flags);
841 av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
845 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
847 PerThreadContext *p = avctx->internal->thread_ctx;
848 FrameThreadContext *fctx;
850 FF_DISABLE_DEPRECATION_WARNINGS
851 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
852 avctx->thread_safe_callbacks ||
854 #if FF_API_GET_BUFFER
855 !avctx->get_buffer &&
857 avctx->get_buffer2 == avcodec_default_get_buffer2);
858 FF_ENABLE_DEPRECATION_WARNINGS
863 if (avctx->debug & FF_DEBUG_BUFFERS)
864 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
866 av_buffer_unref(&f->progress);
869 if (can_direct_free) {
870 av_frame_unref(f->f);
875 pthread_mutex_lock(&fctx->buffer_mutex);
877 if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
879 tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
880 (p->num_released_buffers + 1) *
881 sizeof(*p->released_buffers));
884 p->released_buffers = tmp;
886 dst = &p->released_buffers[p->num_released_buffers];
887 av_frame_move_ref(dst, f->f);
889 p->num_released_buffers++;
892 pthread_mutex_unlock(&fctx->buffer_mutex);