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"
43 #include "libavutil/avassert.h"
44 #include "libavutil/buffer.h"
45 #include "libavutil/common.h"
46 #include "libavutil/cpu.h"
47 #include "libavutil/frame.h"
48 #include "libavutil/internal.h"
49 #include "libavutil/log.h"
50 #include "libavutil/mem.h"
51 #include "libavutil/opt.h"
54 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
56 typedef struct PerThreadContext {
57 struct FrameThreadContext *parent;
61 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
62 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
63 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
65 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
66 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
68 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
70 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
72 AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
73 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
74 int result; ///< The result of the last codec decode/encode() call.
77 STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
78 STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
79 STATE_GET_BUFFER, /**<
80 * Set when the codec calls get_buffer().
81 * State is returned to STATE_SETTING_UP afterwards.
83 STATE_GET_FORMAT, /**<
84 * Set when the codec calls get_format().
85 * State is returned to STATE_SETTING_UP afterwards.
87 STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
91 * Array of frames passed to ff_thread_release_buffer().
92 * Frames are released after all threads referencing them are finished.
94 AVFrame *released_buffers;
95 int num_released_buffers;
96 int released_buffers_allocated;
98 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
99 int requested_flags; ///< flags passed to get_buffer() for requested_frame
101 const enum AVPixelFormat *available_formats; ///< Format array for get_format()
102 enum AVPixelFormat result_format; ///< get_format() result
106 * Context stored in the client AVCodecInternal thread_ctx.
108 typedef struct FrameThreadContext {
109 PerThreadContext *threads; ///< The contexts for each thread.
110 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
112 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
114 int next_decoding; ///< The next context to submit a packet to.
115 int next_finished; ///< The next context to return output from.
118 * Set for the first N packets, where N is the number of threads.
119 * While it is set, ff_thread_en/decode_frame won't return any results.
122 int die; ///< Set when threads should exit.
123 } FrameThreadContext;
125 #if FF_API_GET_BUFFER
126 #define THREAD_SAFE_CALLBACKS(avctx) \
127 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
129 #define THREAD_SAFE_CALLBACKS(avctx) \
130 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
134 * Codec worker thread.
136 * Automatically calls ff_thread_finish_setup() if the codec does
137 * not provide an update_thread_context method, or if the codec returns
140 static attribute_align_arg void *frame_worker_thread(void *arg)
142 PerThreadContext *p = arg;
143 FrameThreadContext *fctx = p->parent;
144 AVCodecContext *avctx = p->avctx;
145 const AVCodec *codec = avctx->codec;
147 pthread_mutex_lock(&p->mutex);
149 while (p->state == STATE_INPUT_READY && !fctx->die)
150 pthread_cond_wait(&p->input_cond, &p->mutex);
152 if (fctx->die) break;
154 if (!codec->update_thread_context && THREAD_SAFE_CALLBACKS(avctx))
155 ff_thread_finish_setup(avctx);
157 av_frame_unref(p->frame);
159 p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
161 if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
162 if (avctx->internal->allocate_progress)
163 av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
164 "free the frame on failure. This is a bug, please report it.\n");
165 av_frame_unref(p->frame);
168 if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
170 pthread_mutex_lock(&p->progress_mutex);
172 for (i = 0; i < MAX_BUFFERS; i++)
173 if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
174 p->progress[i][0] = INT_MAX;
175 p->progress[i][1] = INT_MAX;
178 p->state = STATE_INPUT_READY;
180 pthread_cond_broadcast(&p->progress_cond);
181 pthread_cond_signal(&p->output_cond);
182 pthread_mutex_unlock(&p->progress_mutex);
184 pthread_mutex_unlock(&p->mutex);
190 * Update the next thread's AVCodecContext with values from the reference thread's context.
192 * @param dst The destination context.
193 * @param src The source context.
194 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
196 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
201 dst->time_base = src->time_base;
202 dst->framerate = src->framerate;
203 dst->width = src->width;
204 dst->height = src->height;
205 dst->pix_fmt = src->pix_fmt;
207 dst->coded_width = src->coded_width;
208 dst->coded_height = src->coded_height;
210 dst->has_b_frames = src->has_b_frames;
211 dst->idct_algo = src->idct_algo;
213 dst->bits_per_coded_sample = src->bits_per_coded_sample;
214 dst->sample_aspect_ratio = src->sample_aspect_ratio;
216 FF_DISABLE_DEPRECATION_WARNINGS
217 dst->dtg_active_format = src->dtg_active_format;
218 FF_ENABLE_DEPRECATION_WARNINGS
219 #endif /* FF_API_AFD */
221 dst->profile = src->profile;
222 dst->level = src->level;
224 dst->bits_per_raw_sample = src->bits_per_raw_sample;
225 dst->ticks_per_frame = src->ticks_per_frame;
226 dst->color_primaries = src->color_primaries;
228 dst->color_trc = src->color_trc;
229 dst->colorspace = src->colorspace;
230 dst->color_range = src->color_range;
231 dst->chroma_sample_location = src->chroma_sample_location;
233 dst->hwaccel = src->hwaccel;
234 dst->hwaccel_context = src->hwaccel_context;
236 dst->channels = src->channels;
237 dst->sample_rate = src->sample_rate;
238 dst->sample_fmt = src->sample_fmt;
239 dst->channel_layout = src->channel_layout;
240 dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
244 dst->delay = src->thread_count - 1;
245 #if FF_API_CODED_FRAME
246 FF_DISABLE_DEPRECATION_WARNINGS
247 dst->coded_frame = src->coded_frame;
248 FF_ENABLE_DEPRECATION_WARNINGS
251 if (dst->codec->update_thread_context)
252 err = dst->codec->update_thread_context(dst, src);
259 * Update the next thread's AVCodecContext with values set by the user.
261 * @param dst The destination context.
262 * @param src The source context.
263 * @return 0 on success, negative error code on failure
265 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
267 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
268 dst->flags = src->flags;
270 dst->draw_horiz_band= src->draw_horiz_band;
271 dst->get_buffer2 = src->get_buffer2;
272 #if FF_API_GET_BUFFER
273 FF_DISABLE_DEPRECATION_WARNINGS
274 dst->get_buffer = src->get_buffer;
275 dst->release_buffer = src->release_buffer;
276 FF_ENABLE_DEPRECATION_WARNINGS
279 dst->opaque = src->opaque;
280 dst->debug = src->debug;
281 dst->debug_mv = src->debug_mv;
283 dst->slice_flags = src->slice_flags;
284 dst->flags2 = src->flags2;
286 copy_fields(skip_loop_filter, subtitle_header);
288 dst->frame_number = src->frame_number;
289 dst->reordered_opaque = src->reordered_opaque;
290 dst->thread_safe_callbacks = src->thread_safe_callbacks;
292 if (src->slice_count && src->slice_offset) {
293 if (dst->slice_count < src->slice_count) {
294 int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
295 sizeof(*dst->slice_offset));
299 memcpy(dst->slice_offset, src->slice_offset,
300 src->slice_count * sizeof(*dst->slice_offset));
302 dst->slice_count = src->slice_count;
307 /// Releases the buffers that this decoding thread was the last user of.
308 static void release_delayed_buffers(PerThreadContext *p)
310 FrameThreadContext *fctx = p->parent;
312 while (p->num_released_buffers > 0) {
315 pthread_mutex_lock(&fctx->buffer_mutex);
317 // fix extended data in case the caller screwed it up
318 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
319 p->avctx->codec_type == AVMEDIA_TYPE_AUDIO);
320 f = &p->released_buffers[--p->num_released_buffers];
321 f->extended_data = f->data;
324 pthread_mutex_unlock(&fctx->buffer_mutex);
328 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
330 FrameThreadContext *fctx = p->parent;
331 PerThreadContext *prev_thread = fctx->prev_thread;
332 const AVCodec *codec = p->avctx->codec;
334 if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
337 pthread_mutex_lock(&p->mutex);
339 release_delayed_buffers(p);
343 if (prev_thread->state == STATE_SETTING_UP) {
344 pthread_mutex_lock(&prev_thread->progress_mutex);
345 while (prev_thread->state == STATE_SETTING_UP)
346 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
347 pthread_mutex_unlock(&prev_thread->progress_mutex);
350 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
352 pthread_mutex_unlock(&p->mutex);
357 av_packet_unref(&p->avpkt);
358 av_packet_ref(&p->avpkt, avpkt);
360 p->state = STATE_SETTING_UP;
361 pthread_cond_signal(&p->input_cond);
362 pthread_mutex_unlock(&p->mutex);
365 * If the client doesn't have a thread-safe get_buffer(),
366 * then decoding threads call back to the main thread,
367 * and it calls back to the client here.
370 FF_DISABLE_DEPRECATION_WARNINGS
371 if (!p->avctx->thread_safe_callbacks && (
372 p->avctx->get_format != avcodec_default_get_format ||
373 #if FF_API_GET_BUFFER
374 p->avctx->get_buffer ||
376 p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
377 FF_ENABLE_DEPRECATION_WARNINGS
378 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
380 pthread_mutex_lock(&p->progress_mutex);
381 while (p->state == STATE_SETTING_UP)
382 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
385 case STATE_GET_BUFFER:
386 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
388 case STATE_GET_FORMAT:
389 p->result_format = ff_get_format(p->avctx, p->available_formats);
396 p->state = STATE_SETTING_UP;
397 pthread_cond_signal(&p->progress_cond);
399 pthread_mutex_unlock(&p->progress_mutex);
403 fctx->prev_thread = p;
404 fctx->next_decoding++;
409 int ff_thread_decode_frame(AVCodecContext *avctx,
410 AVFrame *picture, int *got_picture_ptr,
413 FrameThreadContext *fctx = avctx->internal->thread_ctx;
414 int finished = fctx->next_finished;
419 * Submit a packet to the next decoding thread.
422 p = &fctx->threads[fctx->next_decoding];
423 err = update_context_from_user(p->avctx, avctx);
425 err = submit_packet(p, avpkt);
429 * If we're still receiving the initial packets, don't return a frame.
432 if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
435 if (fctx->delaying) {
442 * Return the next available frame from the oldest thread.
443 * If we're at the end of the stream, then we have to skip threads that
444 * didn't output a frame, because we don't want to accidentally signal
445 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
449 p = &fctx->threads[finished++];
451 if (p->state != STATE_INPUT_READY) {
452 pthread_mutex_lock(&p->progress_mutex);
453 while (p->state != STATE_INPUT_READY)
454 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
455 pthread_mutex_unlock(&p->progress_mutex);
458 av_frame_move_ref(picture, p->frame);
459 *got_picture_ptr = p->got_frame;
460 picture->pkt_dts = p->avpkt.dts;
466 * A later call with avkpt->size == 0 may loop over all threads,
467 * including this one, searching for a frame to return before being
468 * stopped by the "finished != fctx->next_finished" condition.
469 * Make sure we don't mistakenly return the same frame again.
473 if (finished >= avctx->thread_count) finished = 0;
474 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
476 update_context_from_thread(avctx, p->avctx, 1);
478 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
480 fctx->next_finished = finished;
483 * When no frame was found while flushing, but an error occurred in
484 * any thread, return it instead of 0.
485 * Otherwise the error can get lost.
487 if (!avpkt->size && !*got_picture_ptr)
490 /* return the size of the consumed packet if no error occurred */
491 return (p->result >= 0) ? avpkt->size : p->result;
494 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
497 volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
499 if (!progress || progress[field] >= n) return;
501 p = f->owner->internal->thread_ctx;
503 if (f->owner->debug&FF_DEBUG_THREADS)
504 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
506 pthread_mutex_lock(&p->progress_mutex);
508 pthread_cond_broadcast(&p->progress_cond);
509 pthread_mutex_unlock(&p->progress_mutex);
512 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
515 volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
517 if (!progress || progress[field] >= n) return;
519 p = f->owner->internal->thread_ctx;
521 if (f->owner->debug&FF_DEBUG_THREADS)
522 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
524 pthread_mutex_lock(&p->progress_mutex);
525 while (progress[field] < n)
526 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
527 pthread_mutex_unlock(&p->progress_mutex);
530 void ff_thread_finish_setup(AVCodecContext *avctx) {
531 PerThreadContext *p = avctx->internal->thread_ctx;
533 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
535 if(p->state == STATE_SETUP_FINISHED){
536 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
539 pthread_mutex_lock(&p->progress_mutex);
540 p->state = STATE_SETUP_FINISHED;
541 pthread_cond_broadcast(&p->progress_cond);
542 pthread_mutex_unlock(&p->progress_mutex);
545 /// Waits for all threads to finish.
546 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
550 for (i = 0; i < thread_count; i++) {
551 PerThreadContext *p = &fctx->threads[i];
553 if (p->state != STATE_INPUT_READY) {
554 pthread_mutex_lock(&p->progress_mutex);
555 while (p->state != STATE_INPUT_READY)
556 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
557 pthread_mutex_unlock(&p->progress_mutex);
563 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
565 FrameThreadContext *fctx = avctx->internal->thread_ctx;
566 const AVCodec *codec = avctx->codec;
569 park_frame_worker_threads(fctx, thread_count);
571 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
572 if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
573 av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
574 fctx->prev_thread->avctx->internal->is_copy = fctx->threads->avctx->internal->is_copy;
575 fctx->threads->avctx->internal->is_copy = 1;
580 for (i = 0; i < thread_count; i++) {
581 PerThreadContext *p = &fctx->threads[i];
583 pthread_mutex_lock(&p->mutex);
584 pthread_cond_signal(&p->input_cond);
585 pthread_mutex_unlock(&p->mutex);
588 pthread_join(p->thread, NULL);
591 if (codec->close && p->avctx)
592 codec->close(p->avctx);
594 release_delayed_buffers(p);
595 av_frame_free(&p->frame);
598 for (i = 0; i < thread_count; i++) {
599 PerThreadContext *p = &fctx->threads[i];
601 pthread_mutex_destroy(&p->mutex);
602 pthread_mutex_destroy(&p->progress_mutex);
603 pthread_cond_destroy(&p->input_cond);
604 pthread_cond_destroy(&p->progress_cond);
605 pthread_cond_destroy(&p->output_cond);
606 av_packet_unref(&p->avpkt);
607 av_freep(&p->released_buffers);
610 av_freep(&p->avctx->priv_data);
611 av_freep(&p->avctx->slice_offset);
615 av_freep(&p->avctx->internal);
619 av_freep(&fctx->threads);
620 pthread_mutex_destroy(&fctx->buffer_mutex);
621 av_freep(&avctx->internal->thread_ctx);
623 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
624 av_opt_free(avctx->priv_data);
628 int ff_frame_thread_init(AVCodecContext *avctx)
630 int thread_count = avctx->thread_count;
631 const AVCodec *codec = avctx->codec;
632 AVCodecContext *src = avctx;
633 FrameThreadContext *fctx;
641 int nb_cpus = av_cpu_count();
642 if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
644 // use number of cores + 1 as thread count if there is more than one
646 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
648 thread_count = avctx->thread_count = 1;
651 if (thread_count <= 1) {
652 avctx->active_thread_type = 0;
656 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
658 return AVERROR(ENOMEM);
660 fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
661 if (!fctx->threads) {
662 av_freep(&avctx->internal->thread_ctx);
663 return AVERROR(ENOMEM);
666 pthread_mutex_init(&fctx->buffer_mutex, NULL);
669 for (i = 0; i < thread_count; i++) {
670 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
671 PerThreadContext *p = &fctx->threads[i];
673 pthread_mutex_init(&p->mutex, NULL);
674 pthread_mutex_init(&p->progress_mutex, NULL);
675 pthread_cond_init(&p->input_cond, NULL);
676 pthread_cond_init(&p->progress_cond, NULL);
677 pthread_cond_init(&p->output_cond, NULL);
679 p->frame = av_frame_alloc();
682 err = AVERROR(ENOMEM);
690 err = AVERROR(ENOMEM);
696 copy->internal = av_malloc(sizeof(AVCodecInternal));
697 if (!copy->internal) {
698 copy->priv_data = NULL;
699 err = AVERROR(ENOMEM);
702 *copy->internal = *src->internal;
703 copy->internal->thread_ctx = p;
704 copy->internal->pkt = &p->avpkt;
710 err = codec->init(copy);
712 update_context_from_thread(avctx, copy, 1);
714 copy->priv_data = av_malloc(codec->priv_data_size);
715 if (!copy->priv_data) {
716 err = AVERROR(ENOMEM);
719 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
720 copy->internal->is_copy = 1;
722 if (codec->init_thread_copy)
723 err = codec->init_thread_copy(copy);
728 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
729 p->thread_init= !err;
737 ff_frame_thread_free(avctx, i+1);
742 void ff_thread_flush(AVCodecContext *avctx)
745 FrameThreadContext *fctx = avctx->internal->thread_ctx;
749 park_frame_worker_threads(fctx, avctx->thread_count);
750 if (fctx->prev_thread) {
751 if (fctx->prev_thread != &fctx->threads[0])
752 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
755 fctx->next_decoding = fctx->next_finished = 0;
757 fctx->prev_thread = NULL;
758 for (i = 0; i < avctx->thread_count; i++) {
759 PerThreadContext *p = &fctx->threads[i];
760 // Make sure decode flush calls with size=0 won't return old frames
762 av_frame_unref(p->frame);
764 release_delayed_buffers(p);
766 if (avctx->codec->flush)
767 avctx->codec->flush(p->avctx);
771 int ff_thread_can_start_frame(AVCodecContext *avctx)
773 PerThreadContext *p = avctx->internal->thread_ctx;
774 if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
775 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
781 static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
783 PerThreadContext *p = avctx->internal->thread_ctx;
788 ff_init_buffer_info(avctx, f->f);
790 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
791 return ff_get_buffer(avctx, f->f, flags);
793 if (p->state != STATE_SETTING_UP &&
794 (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
795 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
799 if (avctx->internal->allocate_progress) {
801 f->progress = av_buffer_alloc(2 * sizeof(int));
803 return AVERROR(ENOMEM);
805 progress = (int*)f->progress->data;
807 progress[0] = progress[1] = -1;
810 pthread_mutex_lock(&p->parent->buffer_mutex);
811 FF_DISABLE_DEPRECATION_WARNINGS
812 if (avctx->thread_safe_callbacks || (
813 #if FF_API_GET_BUFFER
814 !avctx->get_buffer &&
816 avctx->get_buffer2 == avcodec_default_get_buffer2)) {
817 FF_ENABLE_DEPRECATION_WARNINGS
818 err = ff_get_buffer(avctx, f->f, flags);
820 pthread_mutex_lock(&p->progress_mutex);
821 p->requested_frame = f->f;
822 p->requested_flags = flags;
823 p->state = STATE_GET_BUFFER;
824 pthread_cond_broadcast(&p->progress_cond);
826 while (p->state != STATE_SETTING_UP)
827 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
831 pthread_mutex_unlock(&p->progress_mutex);
834 if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
835 ff_thread_finish_setup(avctx);
838 av_buffer_unref(&f->progress);
840 pthread_mutex_unlock(&p->parent->buffer_mutex);
845 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
847 enum AVPixelFormat res;
848 PerThreadContext *p = avctx->internal->thread_ctx;
849 if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
850 avctx->get_format == avcodec_default_get_format)
851 return ff_get_format(avctx, fmt);
852 if (p->state != STATE_SETTING_UP) {
853 av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
856 pthread_mutex_lock(&p->progress_mutex);
857 p->available_formats = fmt;
858 p->state = STATE_GET_FORMAT;
859 pthread_cond_broadcast(&p->progress_cond);
861 while (p->state != STATE_SETTING_UP)
862 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
864 res = p->result_format;
866 pthread_mutex_unlock(&p->progress_mutex);
871 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
873 int ret = thread_get_buffer_internal(avctx, f, flags);
875 av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
879 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
881 PerThreadContext *p = avctx->internal->thread_ctx;
882 FrameThreadContext *fctx;
884 FF_DISABLE_DEPRECATION_WARNINGS
885 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
886 avctx->thread_safe_callbacks ||
888 #if FF_API_GET_BUFFER
889 !avctx->get_buffer &&
891 avctx->get_buffer2 == avcodec_default_get_buffer2);
892 FF_ENABLE_DEPRECATION_WARNINGS
894 if (!f->f || !f->f->buf[0])
897 if (avctx->debug & FF_DEBUG_BUFFERS)
898 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
900 av_buffer_unref(&f->progress);
903 if (can_direct_free) {
904 av_frame_unref(f->f);
909 pthread_mutex_lock(&fctx->buffer_mutex);
911 if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
913 tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
914 (p->num_released_buffers + 1) *
915 sizeof(*p->released_buffers));
918 p->released_buffers = tmp;
920 dst = &p->released_buffers[p->num_released_buffers];
921 av_frame_move_ref(dst, f->f);
923 p->num_released_buffers++;
926 pthread_mutex_unlock(&fctx->buffer_mutex);