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 av_frame_unref(p->frame);
153 p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
155 if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
156 if (avctx->internal->allocate_progress)
157 av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
158 "free the frame on failure. This is a bug, please report it.\n");
159 av_frame_unref(p->frame);
162 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
164 pthread_mutex_lock(&p->progress_mutex);
166 for (i = 0; i < MAX_BUFFERS; i++)
167 if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
168 p->progress[i][0] = INT_MAX;
169 p->progress[i][1] = INT_MAX;
172 p->state = STATE_INPUT_READY;
174 pthread_cond_broadcast(&p->progress_cond);
175 pthread_cond_signal(&p->output_cond);
176 pthread_mutex_unlock(&p->progress_mutex);
178 pthread_mutex_unlock(&p->mutex);
184 * Update the next thread's AVCodecContext with values from the reference thread's context.
186 * @param dst The destination context.
187 * @param src The source context.
188 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
190 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
195 dst->time_base = src->time_base;
196 dst->width = src->width;
197 dst->height = src->height;
198 dst->pix_fmt = src->pix_fmt;
200 dst->coded_width = src->coded_width;
201 dst->coded_height = src->coded_height;
203 dst->has_b_frames = src->has_b_frames;
204 dst->idct_algo = src->idct_algo;
206 dst->bits_per_coded_sample = src->bits_per_coded_sample;
207 dst->sample_aspect_ratio = src->sample_aspect_ratio;
208 dst->dtg_active_format = src->dtg_active_format;
210 dst->profile = src->profile;
211 dst->level = src->level;
213 dst->bits_per_raw_sample = src->bits_per_raw_sample;
214 dst->ticks_per_frame = src->ticks_per_frame;
215 dst->color_primaries = src->color_primaries;
217 dst->color_trc = src->color_trc;
218 dst->colorspace = src->colorspace;
219 dst->color_range = src->color_range;
220 dst->chroma_sample_location = src->chroma_sample_location;
222 dst->hwaccel = src->hwaccel;
223 dst->hwaccel_context = src->hwaccel_context;
225 dst->channels = src->channels;
226 dst->sample_rate = src->sample_rate;
227 dst->sample_fmt = src->sample_fmt;
228 dst->channel_layout = src->channel_layout;
229 dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
233 dst->delay = src->thread_count - 1;
234 dst->coded_frame = src->coded_frame;
236 if (dst->codec->update_thread_context)
237 err = dst->codec->update_thread_context(dst, src);
244 * Update the next thread's AVCodecContext with values set by the user.
246 * @param dst The destination context.
247 * @param src The source context.
248 * @return 0 on success, negative error code on failure
250 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
252 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
253 dst->flags = src->flags;
255 dst->draw_horiz_band= src->draw_horiz_band;
256 dst->get_buffer2 = src->get_buffer2;
257 #if FF_API_GET_BUFFER
258 FF_DISABLE_DEPRECATION_WARNINGS
259 dst->get_buffer = src->get_buffer;
260 dst->release_buffer = src->release_buffer;
261 FF_ENABLE_DEPRECATION_WARNINGS
264 dst->opaque = src->opaque;
265 dst->debug = src->debug;
266 dst->debug_mv = src->debug_mv;
268 dst->slice_flags = src->slice_flags;
269 dst->flags2 = src->flags2;
271 copy_fields(skip_loop_filter, subtitle_header);
273 dst->frame_number = src->frame_number;
274 dst->reordered_opaque = src->reordered_opaque;
275 dst->thread_safe_callbacks = src->thread_safe_callbacks;
277 if (src->slice_count && src->slice_offset) {
278 if (dst->slice_count < src->slice_count) {
279 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
280 sizeof(*dst->slice_offset));
282 av_free(dst->slice_offset);
283 return AVERROR(ENOMEM);
285 dst->slice_offset = tmp;
287 memcpy(dst->slice_offset, src->slice_offset,
288 src->slice_count * sizeof(*dst->slice_offset));
290 dst->slice_count = src->slice_count;
295 /// Releases the buffers that this decoding thread was the last user of.
296 static void release_delayed_buffers(PerThreadContext *p)
298 FrameThreadContext *fctx = p->parent;
300 while (p->num_released_buffers > 0) {
303 pthread_mutex_lock(&fctx->buffer_mutex);
305 // fix extended data in case the caller screwed it up
306 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
307 p->avctx->codec_type == AVMEDIA_TYPE_AUDIO);
308 f = &p->released_buffers[--p->num_released_buffers];
309 f->extended_data = f->data;
312 pthread_mutex_unlock(&fctx->buffer_mutex);
316 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
318 FrameThreadContext *fctx = p->parent;
319 PerThreadContext *prev_thread = fctx->prev_thread;
320 const AVCodec *codec = p->avctx->codec;
323 if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
325 pthread_mutex_lock(&p->mutex);
327 release_delayed_buffers(p);
331 if (prev_thread->state == STATE_SETTING_UP) {
332 pthread_mutex_lock(&prev_thread->progress_mutex);
333 while (prev_thread->state == STATE_SETTING_UP)
334 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
335 pthread_mutex_unlock(&prev_thread->progress_mutex);
338 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
340 pthread_mutex_unlock(&p->mutex);
345 av_packet_free_side_data(&p->avpkt);
346 av_buffer_unref(&p->avpkt.buf);
349 p->avpkt.buf = av_buffer_ref(avpkt->buf);
351 av_fast_malloc(&p->buf, &p->allocated_buf_size, avpkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
353 pthread_mutex_unlock(&p->mutex);
354 return AVERROR(ENOMEM);
356 p->avpkt.data = p->buf;
357 memcpy(p->buf, avpkt->data, avpkt->size);
358 memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
360 if ((ret = av_copy_packet_side_data(&p->avpkt, avpkt)) < 0) {
361 pthread_mutex_unlock(&p->mutex);
365 p->state = STATE_SETTING_UP;
366 pthread_cond_signal(&p->input_cond);
367 pthread_mutex_unlock(&p->mutex);
370 * If the client doesn't have a thread-safe get_buffer(),
371 * then decoding threads call back to the main thread,
372 * and it calls back to the client here.
375 FF_DISABLE_DEPRECATION_WARNINGS
376 if (!p->avctx->thread_safe_callbacks && (
377 p->avctx->get_format != avcodec_default_get_format ||
378 #if FF_API_GET_BUFFER
379 p->avctx->get_buffer ||
381 p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
382 FF_ENABLE_DEPRECATION_WARNINGS
383 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
385 pthread_mutex_lock(&p->progress_mutex);
386 while (p->state == STATE_SETTING_UP)
387 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
390 case STATE_GET_BUFFER:
391 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
393 case STATE_GET_FORMAT:
394 p->result_format = p->avctx->get_format(p->avctx, p->available_formats);
401 p->state = STATE_SETTING_UP;
402 pthread_cond_signal(&p->progress_cond);
404 pthread_mutex_unlock(&p->progress_mutex);
408 fctx->prev_thread = p;
409 fctx->next_decoding++;
414 int ff_thread_decode_frame(AVCodecContext *avctx,
415 AVFrame *picture, int *got_picture_ptr,
418 FrameThreadContext *fctx = avctx->internal->thread_ctx;
419 int finished = fctx->next_finished;
424 * Submit a packet to the next decoding thread.
427 p = &fctx->threads[fctx->next_decoding];
428 err = update_context_from_user(p->avctx, avctx);
430 err = submit_packet(p, avpkt);
434 * If we're still receiving the initial packets, don't return a frame.
437 if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
440 if (fctx->delaying) {
447 * Return the next available frame from the oldest thread.
448 * If we're at the end of the stream, then we have to skip threads that
449 * didn't output a frame, because we don't want to accidentally signal
450 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
454 p = &fctx->threads[finished++];
456 if (p->state != STATE_INPUT_READY) {
457 pthread_mutex_lock(&p->progress_mutex);
458 while (p->state != STATE_INPUT_READY)
459 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
460 pthread_mutex_unlock(&p->progress_mutex);
463 av_frame_move_ref(picture, p->frame);
464 *got_picture_ptr = p->got_frame;
465 picture->pkt_dts = p->avpkt.dts;
468 * A later call with avkpt->size == 0 may loop over all threads,
469 * including this one, searching for a frame to return before being
470 * stopped by the "finished != fctx->next_finished" condition.
471 * Make sure we don't mistakenly return the same frame again.
475 if (finished >= avctx->thread_count) finished = 0;
476 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
478 update_context_from_thread(avctx, p->avctx, 1);
480 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
482 fctx->next_finished = finished;
484 /* return the size of the consumed packet if no error occurred */
485 return (p->result >= 0) ? avpkt->size : p->result;
488 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
491 volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
493 if (!progress || progress[field] >= n) return;
495 p = f->owner->internal->thread_ctx;
497 if (f->owner->debug&FF_DEBUG_THREADS)
498 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
500 pthread_mutex_lock(&p->progress_mutex);
502 pthread_cond_broadcast(&p->progress_cond);
503 pthread_mutex_unlock(&p->progress_mutex);
506 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
509 volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
511 if (!progress || progress[field] >= n) return;
513 p = f->owner->internal->thread_ctx;
515 if (f->owner->debug&FF_DEBUG_THREADS)
516 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
518 pthread_mutex_lock(&p->progress_mutex);
519 while (progress[field] < n)
520 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
521 pthread_mutex_unlock(&p->progress_mutex);
524 void ff_thread_finish_setup(AVCodecContext *avctx) {
525 PerThreadContext *p = avctx->internal->thread_ctx;
527 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
529 if(p->state == STATE_SETUP_FINISHED){
530 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
533 pthread_mutex_lock(&p->progress_mutex);
534 p->state = STATE_SETUP_FINISHED;
535 pthread_cond_broadcast(&p->progress_cond);
536 pthread_mutex_unlock(&p->progress_mutex);
539 /// Waits for all threads to finish.
540 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
544 for (i = 0; i < thread_count; i++) {
545 PerThreadContext *p = &fctx->threads[i];
547 if (p->state != STATE_INPUT_READY) {
548 pthread_mutex_lock(&p->progress_mutex);
549 while (p->state != STATE_INPUT_READY)
550 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
551 pthread_mutex_unlock(&p->progress_mutex);
557 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
559 FrameThreadContext *fctx = avctx->internal->thread_ctx;
560 const AVCodec *codec = avctx->codec;
563 park_frame_worker_threads(fctx, thread_count);
565 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
566 if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
567 av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
568 fctx->prev_thread->avctx->internal->is_copy = fctx->threads->avctx->internal->is_copy;
569 fctx->threads->avctx->internal->is_copy = 1;
574 for (i = 0; i < thread_count; i++) {
575 PerThreadContext *p = &fctx->threads[i];
577 pthread_mutex_lock(&p->mutex);
578 pthread_cond_signal(&p->input_cond);
579 pthread_mutex_unlock(&p->mutex);
582 pthread_join(p->thread, NULL);
586 codec->close(p->avctx);
590 release_delayed_buffers(p);
591 av_frame_free(&p->frame);
594 for (i = 0; i < thread_count; i++) {
595 PerThreadContext *p = &fctx->threads[i];
597 pthread_mutex_destroy(&p->mutex);
598 pthread_mutex_destroy(&p->progress_mutex);
599 pthread_cond_destroy(&p->input_cond);
600 pthread_cond_destroy(&p->progress_cond);
601 pthread_cond_destroy(&p->output_cond);
602 av_packet_free_side_data(&p->avpkt);
603 av_buffer_unref(&p->avpkt.buf);
605 av_freep(&p->released_buffers);
608 av_freep(&p->avctx->priv_data);
609 av_freep(&p->avctx->slice_offset);
612 av_freep(&p->avctx->internal);
616 av_freep(&fctx->threads);
617 pthread_mutex_destroy(&fctx->buffer_mutex);
618 av_freep(&avctx->internal->thread_ctx);
621 int ff_frame_thread_init(AVCodecContext *avctx)
623 int thread_count = avctx->thread_count;
624 const AVCodec *codec = avctx->codec;
625 AVCodecContext *src = avctx;
626 FrameThreadContext *fctx;
634 int nb_cpus = av_cpu_count();
635 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
637 // use number of cores + 1 as thread count if there is more than one
639 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
641 thread_count = avctx->thread_count = 1;
644 if (thread_count <= 1) {
645 avctx->active_thread_type = 0;
649 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
651 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
652 pthread_mutex_init(&fctx->buffer_mutex, NULL);
655 for (i = 0; i < thread_count; i++) {
656 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
657 PerThreadContext *p = &fctx->threads[i];
659 pthread_mutex_init(&p->mutex, NULL);
660 pthread_mutex_init(&p->progress_mutex, NULL);
661 pthread_cond_init(&p->input_cond, NULL);
662 pthread_cond_init(&p->progress_cond, NULL);
663 pthread_cond_init(&p->output_cond, NULL);
665 p->frame = av_frame_alloc();
667 err = AVERROR(ENOMEM);
676 err = AVERROR(ENOMEM);
682 copy->internal = av_malloc(sizeof(AVCodecInternal));
683 if (!copy->internal) {
684 err = AVERROR(ENOMEM);
687 *copy->internal = *src->internal;
688 copy->internal->thread_ctx = p;
689 copy->internal->pkt = &p->avpkt;
695 err = codec->init(copy);
697 update_context_from_thread(avctx, copy, 1);
699 copy->priv_data = av_malloc(codec->priv_data_size);
700 if (!copy->priv_data) {
701 err = AVERROR(ENOMEM);
704 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
705 copy->internal->is_copy = 1;
707 if (codec->init_thread_copy)
708 err = codec->init_thread_copy(copy);
713 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
714 p->thread_init= !err;
722 ff_frame_thread_free(avctx, i+1);
727 void ff_thread_flush(AVCodecContext *avctx)
730 FrameThreadContext *fctx = avctx->internal->thread_ctx;
734 park_frame_worker_threads(fctx, avctx->thread_count);
735 if (fctx->prev_thread) {
736 if (fctx->prev_thread != &fctx->threads[0])
737 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
740 fctx->next_decoding = fctx->next_finished = 0;
742 fctx->prev_thread = NULL;
743 for (i = 0; i < avctx->thread_count; i++) {
744 PerThreadContext *p = &fctx->threads[i];
745 // Make sure decode flush calls with size=0 won't return old frames
747 av_frame_unref(p->frame);
749 release_delayed_buffers(p);
751 if (avctx->codec->flush)
752 avctx->codec->flush(p->avctx);
756 int ff_thread_can_start_frame(AVCodecContext *avctx)
758 PerThreadContext *p = avctx->internal->thread_ctx;
759 if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
760 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
766 static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
768 PerThreadContext *p = avctx->internal->thread_ctx;
773 ff_init_buffer_info(avctx, f->f);
775 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
776 return ff_get_buffer(avctx, f->f, flags);
778 if (p->state != STATE_SETTING_UP &&
779 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
780 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
784 if (avctx->internal->allocate_progress) {
786 f->progress = av_buffer_alloc(2 * sizeof(int));
788 return AVERROR(ENOMEM);
790 progress = (int*)f->progress->data;
792 progress[0] = progress[1] = -1;
795 pthread_mutex_lock(&p->parent->buffer_mutex);
796 FF_DISABLE_DEPRECATION_WARNINGS
797 if (avctx->thread_safe_callbacks || (
798 #if FF_API_GET_BUFFER
799 !avctx->get_buffer &&
801 avctx->get_buffer2 == avcodec_default_get_buffer2)) {
802 FF_ENABLE_DEPRECATION_WARNINGS
803 err = ff_get_buffer(avctx, f->f, flags);
805 pthread_mutex_lock(&p->progress_mutex);
806 p->requested_frame = f->f;
807 p->requested_flags = flags;
808 p->state = STATE_GET_BUFFER;
809 pthread_cond_broadcast(&p->progress_cond);
811 while (p->state != STATE_SETTING_UP)
812 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
816 pthread_mutex_unlock(&p->progress_mutex);
819 if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
820 ff_thread_finish_setup(avctx);
823 av_buffer_unref(&f->progress);
825 pthread_mutex_unlock(&p->parent->buffer_mutex);
830 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
832 enum AVPixelFormat res;
833 PerThreadContext *p = avctx->internal->thread_ctx;
834 if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
835 avctx->get_format == avcodec_default_get_format)
836 return ff_get_format(avctx, fmt);
837 if (p->state != STATE_SETTING_UP) {
838 av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
841 pthread_mutex_lock(&p->progress_mutex);
842 p->available_formats = fmt;
843 p->state = STATE_GET_FORMAT;
844 pthread_cond_broadcast(&p->progress_cond);
846 while (p->state != STATE_SETTING_UP)
847 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
849 res = p->result_format;
851 pthread_mutex_unlock(&p->progress_mutex);
856 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
858 int ret = thread_get_buffer_internal(avctx, f, flags);
860 av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
864 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
866 PerThreadContext *p = avctx->internal->thread_ctx;
867 FrameThreadContext *fctx;
869 FF_DISABLE_DEPRECATION_WARNINGS
870 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
871 avctx->thread_safe_callbacks ||
873 #if FF_API_GET_BUFFER
874 !avctx->get_buffer &&
876 avctx->get_buffer2 == avcodec_default_get_buffer2);
877 FF_ENABLE_DEPRECATION_WARNINGS
879 if (!f->f || !f->f->buf[0])
882 if (avctx->debug & FF_DEBUG_BUFFERS)
883 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
885 av_buffer_unref(&f->progress);
888 if (can_direct_free) {
889 av_frame_unref(f->f);
894 pthread_mutex_lock(&fctx->buffer_mutex);
896 if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
898 tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
899 (p->num_released_buffers + 1) *
900 sizeof(*p->released_buffers));
903 p->released_buffers = tmp;
905 dst = &p->released_buffers[p->num_released_buffers];
906 av_frame_move_ref(dst, f->f);
908 p->num_released_buffers++;
911 pthread_mutex_unlock(&fctx->buffer_mutex);