2 * This file is part of Libav.
4 * Libav 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 * Libav 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 Libav; 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
27 #include <stdatomic.h>
33 #include "compat/w32pthreads.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"
53 ///< Set when the thread is awaiting a packet.
55 ///< Set before the codec has called ff_thread_finish_setup().
58 * Set when the codec calls get_buffer().
59 * State is returned to STATE_SETTING_UP afterwards.
62 ///< Set after the codec has called ff_thread_finish_setup().
67 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
69 typedef struct PerThreadContext {
70 struct FrameThreadContext *parent;
74 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
75 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
76 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
78 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
79 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
81 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
83 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
85 AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
86 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
87 int result; ///< The result of the last codec decode/encode() call.
92 * Array of frames passed to ff_thread_release_buffer().
93 * Frames are released after all threads referencing them are finished.
95 AVFrame *released_buffers;
96 int num_released_buffers;
97 int released_buffers_allocated;
99 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
100 int requested_flags; ///< flags passed to get_buffer() for requested_frame
102 int die; ///< Set when the thread should exit.
104 int hwaccel_serializing;
105 int async_serializing;
109 * Context stored in the client AVCodecInternal thread_ctx.
111 typedef struct FrameThreadContext {
112 PerThreadContext *threads; ///< The contexts for each thread.
113 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
115 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
117 * This lock is used for ensuring threads run in serial when hwaccel
120 pthread_mutex_t hwaccel_mutex;
121 pthread_mutex_t async_mutex;
123 int next_decoding; ///< The next context to submit a packet to.
124 int next_finished; ///< The next context to return output from.
127 * Set for the first N packets, where N is the number of threads.
128 * While it is set, ff_thread_en/decode_frame won't return any results.
130 } FrameThreadContext;
133 * Codec worker thread.
135 * Automatically calls ff_thread_finish_setup() if the codec does
136 * not provide an update_thread_context method, or if the codec returns
139 static attribute_align_arg void *frame_worker_thread(void *arg)
141 PerThreadContext *p = arg;
142 AVCodecContext *avctx = p->avctx;
143 const AVCodec *codec = avctx->codec;
146 if (atomic_load(&p->state) == STATE_INPUT_READY) {
147 pthread_mutex_lock(&p->mutex);
148 while (atomic_load(&p->state) == STATE_INPUT_READY) {
150 pthread_mutex_unlock(&p->mutex);
153 pthread_cond_wait(&p->input_cond, &p->mutex);
155 pthread_mutex_unlock(&p->mutex);
158 if (!codec->update_thread_context && avctx->thread_safe_callbacks)
159 ff_thread_finish_setup(avctx);
161 pthread_mutex_lock(&p->mutex);
163 /* If a decoder supports hwaccel, then it must call ff_get_format().
164 * Since that call must happen before ff_thread_finish_setup(), the
165 * decoder is required to implement update_thread_context() and call
166 * ff_thread_finish_setup() manually. Therefore the above
167 * ff_thread_finish_setup() call did not happen and hwaccel_serializing
168 * cannot be true here. */
169 av_assert0(!p->hwaccel_serializing);
171 /* if the previous thread uses hwaccel then we take the lock to ensure
172 * the threads don't run concurrently */
173 if (avctx->hwaccel) {
174 pthread_mutex_lock(&p->parent->hwaccel_mutex);
175 p->hwaccel_serializing = 1;
178 av_frame_unref(p->frame);
180 p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
182 if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
183 if (avctx->internal->allocate_progress)
184 av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
185 "free the frame on failure. This is a bug, please report it.\n");
186 av_frame_unref(p->frame);
189 if (atomic_load(&p->state) == STATE_SETTING_UP)
190 ff_thread_finish_setup(avctx);
192 if (p->hwaccel_serializing) {
193 p->hwaccel_serializing = 0;
194 pthread_mutex_unlock(&p->parent->hwaccel_mutex);
197 if (p->async_serializing) {
198 p->async_serializing = 0;
199 pthread_mutex_unlock(&p->parent->async_mutex);
202 atomic_store(&p->state, STATE_INPUT_READY);
204 pthread_mutex_lock(&p->progress_mutex);
205 pthread_cond_signal(&p->output_cond);
206 pthread_mutex_unlock(&p->progress_mutex);
208 pthread_mutex_unlock(&p->mutex);
216 * Update the next thread's AVCodecContext with values from the reference thread's context.
218 * @param dst The destination context.
219 * @param src The source context.
220 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
222 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
227 dst->time_base = src->time_base;
228 dst->framerate = src->framerate;
229 dst->width = src->width;
230 dst->height = src->height;
231 dst->pix_fmt = src->pix_fmt;
233 dst->coded_width = src->coded_width;
234 dst->coded_height = src->coded_height;
236 dst->has_b_frames = src->has_b_frames;
237 dst->idct_algo = src->idct_algo;
239 dst->bits_per_coded_sample = src->bits_per_coded_sample;
240 dst->sample_aspect_ratio = src->sample_aspect_ratio;
242 FF_DISABLE_DEPRECATION_WARNINGS
243 dst->dtg_active_format = src->dtg_active_format;
244 FF_ENABLE_DEPRECATION_WARNINGS
245 #endif /* FF_API_AFD */
247 dst->profile = src->profile;
248 dst->level = src->level;
250 dst->bits_per_raw_sample = src->bits_per_raw_sample;
251 dst->ticks_per_frame = src->ticks_per_frame;
252 dst->color_primaries = src->color_primaries;
254 dst->color_trc = src->color_trc;
255 dst->colorspace = src->colorspace;
256 dst->color_range = src->color_range;
257 dst->chroma_sample_location = src->chroma_sample_location;
259 dst->hwaccel = src->hwaccel;
260 dst->hwaccel_context = src->hwaccel_context;
261 dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
263 if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
264 (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
265 av_buffer_unref(&dst->hw_frames_ctx);
267 if (src->hw_frames_ctx) {
268 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
269 if (!dst->hw_frames_ctx)
270 return AVERROR(ENOMEM);
276 #if FF_API_CODED_FRAME
277 FF_DISABLE_DEPRECATION_WARNINGS
278 dst->coded_frame = src->coded_frame;
279 FF_ENABLE_DEPRECATION_WARNINGS
282 if (dst->codec->update_thread_context)
283 err = dst->codec->update_thread_context(dst, src);
290 * Update the next thread's AVCodecContext with values set by the user.
292 * @param dst The destination context.
293 * @param src The source context.
294 * @return 0 on success, negative error code on failure
296 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
298 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
299 dst->flags = src->flags;
301 dst->draw_horiz_band= src->draw_horiz_band;
302 dst->get_buffer2 = src->get_buffer2;
304 dst->opaque = src->opaque;
305 dst->debug = src->debug;
307 dst->slice_flags = src->slice_flags;
308 dst->flags2 = src->flags2;
310 copy_fields(skip_loop_filter, subtitle_header);
312 dst->frame_number = src->frame_number;
313 dst->reordered_opaque = src->reordered_opaque;
315 if (src->slice_count && src->slice_offset) {
316 if (dst->slice_count < src->slice_count) {
317 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
318 sizeof(*dst->slice_offset));
320 av_free(dst->slice_offset);
321 return AVERROR(ENOMEM);
323 dst->slice_offset = tmp;
325 memcpy(dst->slice_offset, src->slice_offset,
326 src->slice_count * sizeof(*dst->slice_offset));
328 dst->slice_count = src->slice_count;
333 /// Releases the buffers that this decoding thread was the last user of.
334 static void release_delayed_buffers(PerThreadContext *p)
336 FrameThreadContext *fctx = p->parent;
338 while (p->num_released_buffers > 0) {
341 pthread_mutex_lock(&fctx->buffer_mutex);
343 // fix extended data in case the caller screwed it up
344 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO);
345 f = &p->released_buffers[--p->num_released_buffers];
346 f->extended_data = f->data;
349 pthread_mutex_unlock(&fctx->buffer_mutex);
353 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
355 FrameThreadContext *fctx = p->parent;
356 PerThreadContext *prev_thread = fctx->prev_thread;
357 const AVCodec *codec = p->avctx->codec;
359 if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
362 pthread_mutex_lock(&p->mutex);
364 release_delayed_buffers(p);
368 if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
369 pthread_mutex_lock(&prev_thread->progress_mutex);
370 while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
371 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
372 pthread_mutex_unlock(&prev_thread->progress_mutex);
375 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
377 pthread_mutex_unlock(&p->mutex);
382 av_packet_unref(&p->avpkt);
383 av_packet_ref(&p->avpkt, avpkt);
385 atomic_store(&p->state, STATE_SETTING_UP);
386 pthread_cond_signal(&p->input_cond);
387 pthread_mutex_unlock(&p->mutex);
390 * If the client doesn't have a thread-safe get_buffer(),
391 * then decoding threads call back to the main thread,
392 * and it calls back to the client here.
395 if (!p->avctx->thread_safe_callbacks &&
396 p->avctx->get_buffer2 != avcodec_default_get_buffer2) {
397 while (atomic_load(&p->state) != STATE_SETUP_FINISHED &&
398 atomic_load(&p->state) != STATE_INPUT_READY) {
399 pthread_mutex_lock(&p->progress_mutex);
400 while (atomic_load(&p->state) == STATE_SETTING_UP)
401 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
403 if (atomic_load_explicit(&p->state, memory_order_acquire) == STATE_GET_BUFFER) {
404 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
405 atomic_store(&p->state, STATE_SETTING_UP);
406 pthread_cond_signal(&p->progress_cond);
408 pthread_mutex_unlock(&p->progress_mutex);
412 fctx->prev_thread = p;
413 fctx->next_decoding++;
418 int ff_thread_decode_frame(AVCodecContext *avctx,
419 AVFrame *picture, int *got_picture_ptr,
422 FrameThreadContext *fctx = avctx->internal->thread_ctx;
423 int finished = fctx->next_finished;
427 /* release the async lock, permitting blocked hwaccel threads to
428 * go forward while we are in this function */
429 pthread_mutex_unlock(&fctx->async_mutex);
432 * Submit a packet to the next decoding thread.
435 p = &fctx->threads[fctx->next_decoding];
436 err = update_context_from_user(p->avctx, avctx);
439 err = submit_packet(p, avpkt);
444 * If we're still receiving the initial packets, don't return a frame.
447 if (fctx->delaying) {
448 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
458 * Return the next available frame from the oldest thread.
459 * If we're at the end of the stream, then we have to skip threads that
460 * didn't output a frame, because we don't want to accidentally signal
461 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
465 p = &fctx->threads[finished++];
467 if (atomic_load(&p->state) != STATE_INPUT_READY) {
468 pthread_mutex_lock(&p->progress_mutex);
469 while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
470 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
471 pthread_mutex_unlock(&p->progress_mutex);
474 av_frame_move_ref(picture, p->frame);
475 *got_picture_ptr = p->got_frame;
476 picture->pkt_dts = p->avpkt.dts;
479 * A later call with avkpt->size == 0 may loop over all threads,
480 * including this one, searching for a frame to return before being
481 * stopped by the "finished != fctx->next_finished" condition.
482 * Make sure we don't mistakenly return the same frame again.
486 if (finished >= avctx->thread_count) finished = 0;
487 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
489 update_context_from_thread(avctx, p->avctx, 1);
491 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
493 fctx->next_finished = finished;
495 /* return the size of the consumed packet if no error occurred */
496 ret = (p->result >= 0) ? avpkt->size : p->result;
498 pthread_mutex_lock(&fctx->async_mutex);
504 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
507 atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
510 atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
513 p = f->owner->internal->thread_ctx;
515 if (f->owner->debug&FF_DEBUG_THREADS)
516 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
518 pthread_mutex_lock(&p->progress_mutex);
520 atomic_store_explicit(&progress[field], n, memory_order_release);
522 pthread_cond_broadcast(&p->progress_cond);
523 pthread_mutex_unlock(&p->progress_mutex);
526 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
529 atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
532 atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
535 p = f->owner->internal->thread_ctx;
537 if (f->owner->debug&FF_DEBUG_THREADS)
538 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
540 pthread_mutex_lock(&p->progress_mutex);
541 while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
542 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
543 pthread_mutex_unlock(&p->progress_mutex);
546 void ff_thread_finish_setup(AVCodecContext *avctx) {
547 PerThreadContext *p = avctx->internal->thread_ctx;
549 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
551 if (avctx->hwaccel && !p->hwaccel_serializing) {
552 pthread_mutex_lock(&p->parent->hwaccel_mutex);
553 p->hwaccel_serializing = 1;
556 /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
557 if (avctx->hwaccel &&
558 !(avctx->hwaccel->caps_internal & HWACCEL_CAP_ASYNC_SAFE)) {
559 p->async_serializing = 1;
560 pthread_mutex_lock(&p->parent->async_mutex);
563 pthread_mutex_lock(&p->progress_mutex);
565 atomic_store(&p->state, STATE_SETUP_FINISHED);
567 pthread_cond_broadcast(&p->progress_cond);
568 pthread_mutex_unlock(&p->progress_mutex);
571 /// Waits for all threads to finish.
572 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
576 pthread_mutex_unlock(&fctx->async_mutex);
578 for (i = 0; i < thread_count; i++) {
579 PerThreadContext *p = &fctx->threads[i];
581 if (atomic_load(&p->state) != STATE_INPUT_READY) {
582 pthread_mutex_lock(&p->progress_mutex);
583 while (atomic_load(&p->state) != STATE_INPUT_READY)
584 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
585 pthread_mutex_unlock(&p->progress_mutex);
589 pthread_mutex_lock(&fctx->async_mutex);
592 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
594 FrameThreadContext *fctx = avctx->internal->thread_ctx;
595 const AVCodec *codec = avctx->codec;
598 park_frame_worker_threads(fctx, thread_count);
600 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
601 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
603 for (i = 0; i < thread_count; i++) {
604 PerThreadContext *p = &fctx->threads[i];
606 pthread_mutex_lock(&p->mutex);
608 pthread_cond_signal(&p->input_cond);
609 pthread_mutex_unlock(&p->mutex);
612 pthread_join(p->thread, NULL);
615 codec->close(p->avctx);
619 release_delayed_buffers(p);
620 av_frame_free(&p->frame);
623 for (i = 0; i < thread_count; i++) {
624 PerThreadContext *p = &fctx->threads[i];
626 pthread_mutex_destroy(&p->mutex);
627 pthread_mutex_destroy(&p->progress_mutex);
628 pthread_cond_destroy(&p->input_cond);
629 pthread_cond_destroy(&p->progress_cond);
630 pthread_cond_destroy(&p->output_cond);
631 av_packet_unref(&p->avpkt);
632 av_freep(&p->released_buffers);
635 av_freep(&p->avctx->priv_data);
636 av_freep(&p->avctx->slice_offset);
639 av_buffer_unref(&p->avctx->hw_frames_ctx);
641 av_freep(&p->avctx->internal);
645 av_freep(&fctx->threads);
646 pthread_mutex_destroy(&fctx->buffer_mutex);
647 pthread_mutex_destroy(&fctx->hwaccel_mutex);
649 pthread_mutex_unlock(&fctx->async_mutex);
650 pthread_mutex_destroy(&fctx->async_mutex);
652 av_freep(&avctx->internal->thread_ctx);
655 int ff_frame_thread_init(AVCodecContext *avctx)
657 int thread_count = avctx->thread_count;
658 const AVCodec *codec = avctx->codec;
659 AVCodecContext *src = avctx;
660 FrameThreadContext *fctx;
668 int nb_cpus = av_cpu_count();
669 av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
670 // use number of cores + 1 as thread count if there is more than one
672 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
674 thread_count = avctx->thread_count = 1;
677 if (thread_count <= 1) {
678 avctx->active_thread_type = 0;
682 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
684 return AVERROR(ENOMEM);
686 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
687 if (!fctx->threads) {
688 av_freep(&avctx->internal->thread_ctx);
689 return AVERROR(ENOMEM);
692 pthread_mutex_init(&fctx->buffer_mutex, NULL);
693 pthread_mutex_init(&fctx->hwaccel_mutex, NULL);
695 pthread_mutex_init(&fctx->async_mutex, NULL);
696 pthread_mutex_lock(&fctx->async_mutex);
700 for (i = 0; i < thread_count; i++) {
701 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
702 PerThreadContext *p = &fctx->threads[i];
704 pthread_mutex_init(&p->mutex, NULL);
705 pthread_mutex_init(&p->progress_mutex, NULL);
706 pthread_cond_init(&p->input_cond, NULL);
707 pthread_cond_init(&p->progress_cond, NULL);
708 pthread_cond_init(&p->output_cond, NULL);
710 p->frame = av_frame_alloc();
713 err = AVERROR(ENOMEM);
721 err = AVERROR(ENOMEM);
727 copy->internal = av_malloc(sizeof(AVCodecInternal));
728 if (!copy->internal) {
729 err = AVERROR(ENOMEM);
732 *copy->internal = *src->internal;
733 copy->internal->thread_ctx = p;
734 copy->internal->last_pkt_props = &p->avpkt;
740 err = codec->init(copy);
742 update_context_from_thread(avctx, copy, 1);
744 copy->priv_data = av_malloc(codec->priv_data_size);
745 if (!copy->priv_data) {
746 err = AVERROR(ENOMEM);
749 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
750 copy->internal->is_copy = 1;
752 if (codec->init_thread_copy)
753 err = codec->init_thread_copy(copy);
758 if (!pthread_create(&p->thread, NULL, frame_worker_thread, p))
765 ff_frame_thread_free(avctx, i+1);
770 void ff_thread_flush(AVCodecContext *avctx)
773 FrameThreadContext *fctx = avctx->internal->thread_ctx;
777 park_frame_worker_threads(fctx, avctx->thread_count);
778 if (fctx->prev_thread) {
779 if (fctx->prev_thread != &fctx->threads[0])
780 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
783 fctx->next_decoding = fctx->next_finished = 0;
785 fctx->prev_thread = NULL;
786 for (i = 0; i < avctx->thread_count; i++) {
787 PerThreadContext *p = &fctx->threads[i];
788 // Make sure decode flush calls with size=0 won't return old frames
790 av_frame_unref(p->frame);
792 release_delayed_buffers(p);
794 if (avctx->codec->flush)
795 avctx->codec->flush(p->avctx);
799 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
801 PerThreadContext *p = avctx->internal->thread_ctx;
806 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
807 return ff_get_buffer(avctx, f->f, flags);
809 if (atomic_load(&p->state) != STATE_SETTING_UP &&
810 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
811 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
815 if (avctx->internal->allocate_progress) {
816 atomic_int *progress;
817 f->progress = av_buffer_alloc(2 * sizeof(*progress));
819 return AVERROR(ENOMEM);
821 progress = (atomic_int*)f->progress->data;
823 atomic_init(&progress[0], -1);
824 atomic_init(&progress[1], -1);
827 pthread_mutex_lock(&p->parent->buffer_mutex);
828 if (avctx->thread_safe_callbacks ||
829 avctx->get_buffer2 == avcodec_default_get_buffer2) {
830 err = ff_get_buffer(avctx, f->f, flags);
832 p->requested_frame = f->f;
833 p->requested_flags = flags;
834 atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
835 pthread_mutex_lock(&p->progress_mutex);
836 pthread_cond_signal(&p->progress_cond);
838 while (atomic_load(&p->state) != STATE_SETTING_UP)
839 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
843 pthread_mutex_unlock(&p->progress_mutex);
846 if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
847 ff_thread_finish_setup(avctx);
850 av_buffer_unref(&f->progress);
852 pthread_mutex_unlock(&p->parent->buffer_mutex);
857 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
859 PerThreadContext *p = avctx->internal->thread_ctx;
860 FrameThreadContext *fctx;
862 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
863 avctx->thread_safe_callbacks ||
864 avctx->get_buffer2 == avcodec_default_get_buffer2;
866 if (!f->f || !f->f->buf[0])
869 if (avctx->debug & FF_DEBUG_BUFFERS)
870 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
872 av_buffer_unref(&f->progress);
875 if (can_direct_free) {
876 av_frame_unref(f->f);
881 pthread_mutex_lock(&fctx->buffer_mutex);
883 if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
885 tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
886 (p->num_released_buffers + 1) *
887 sizeof(*p->released_buffers));
890 p->released_buffers = tmp;
892 dst = &p->released_buffers[p->num_released_buffers];
893 av_frame_move_ref(dst, f->f);
895 p->num_released_buffers++;
898 pthread_mutex_unlock(&fctx->buffer_mutex);