FFmpeg
Loading...
Searching...
No Matches
pthread_frame.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
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.
8 *
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.
13 *
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
17 */
18
19/**
20 * @file
21 * Frame multithreading support functions
22 * @see doc/multithreading.txt
23 */
24
25#include <stdatomic.h>
26
27#include "avcodec.h"
28#include "avcodec_internal.h"
29#include "codec_internal.h"
30#include "decode.h"
31#include "hwaccel_internal.h"
32#include "hwconfig.h"
33#include "internal.h"
34#include "packet_internal.h"
35#include "pthread_internal.h"
36#include "libavutil/refstruct.h"
37#include "thread.h"
38#include "threadframe.h"
39
40#include "libavutil/avassert.h"
41#include "libavutil/buffer.h"
42#include "libavutil/cpu.h"
43#include "libavutil/frame.h"
44#include "libavutil/internal.h"
45#include "libavutil/log.h"
46#include "libavutil/mem.h"
47#include "libavutil/opt.h"
48#include "libavutil/thread.h"
49
50enum {
51 /// Set when the thread is awaiting a packet.
53 /// Set before the codec has called ff_thread_finish_setup().
55 /// Set after the codec has called ff_thread_finish_setup().
57};
58
59enum {
60 UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
61 NEEDS_CLOSE, ///< FFCodec->close needs to be called
62 INITIALIZED, ///< Thread has been properly set up
63};
64
65typedef struct DecodedFrames {
67 size_t nb_f;
70
74
75/**
76 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
77 */
78typedef struct PerThreadContext {
80
83 unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
84 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
85 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
86 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
87
88 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
89 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
90
91 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
92
93 AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
94
95 /**
96 * Decoded frames from a single decode iteration.
97 */
99 int result; ///< The result of the last codec decode/encode() call.
100
102
103 int die; ///< Set when the thread should exit.
104
107
108 // set to 1 in ff_thread_finish_setup() when a threadsafe hwaccel is used;
109 // cannot check hwaccel caps directly, because
110 // worked threads clear hwaccel state for thread-unsafe hwaccels
111 // after each decode call
113
114 atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
116
117/**
118 * Context stored in the client AVCodecInternal thread_ctx.
119 */
120typedef struct FrameThreadContext {
121 PerThreadContext *threads; ///< The contexts for each thread.
122 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
123
124 unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
125 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
126 /**
127 * This lock is used for ensuring threads run in serial when thread-unsafe
128 * hwaccel is used.
129 */
134
137
138 /**
139 * Packet to be submitted to the next thread for decoding.
140 */
142
143 int next_decoding; ///< The next context to submit a packet to.
144 int next_finished; ///< The next context to return output from.
145
146 /* hwaccel state for thread-unsafe hwaccels is temporarily stored here in
147 * order to transfer its ownership to the next decoding thread without the
148 * need for extra synchronization */
153
154static int hwaccel_serial(const AVCodecContext *avctx)
155{
156 return avctx->hwaccel && !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
157}
158
160{
162 while (fctx->async_lock)
164 fctx->async_lock = 1;
166}
167
169{
171 av_assert0(fctx->async_lock);
172 fctx->async_lock = 0;
175}
176
178{
179 AVCodecContext *avctx = p->avctx;
180 int idx = p - p->parent->threads;
181 char name[16];
182
183 snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
184
186}
187
188// get a free frame to decode into
190{
191 if (df->nb_f == df->nb_f_allocated) {
192 AVFrame **tmp = av_realloc_array(df->f, df->nb_f + 1,
193 sizeof(*df->f));
194 if (!tmp)
195 return NULL;
196 df->f = tmp;
197
198 df->f[df->nb_f] = av_frame_alloc();
199 if (!df->f[df->nb_f])
200 return NULL;
201
202 df->nb_f_allocated++;
203 }
204
205 av_assert0(!df->f[df->nb_f]->buf[0]);
206
207 return df->f[df->nb_f];
208}
209
211{
212 AVFrame *tmp_frame = df->f[0];
213 av_frame_move_ref(dst, tmp_frame);
214 memmove(df->f, df->f + 1, (df->nb_f - 1) * sizeof(*df->f));
215 df->f[--df->nb_f] = tmp_frame;
216}
217
219{
220 for (size_t i = 0; i < df->nb_f; i++)
221 av_frame_unref(df->f[i]);
222 df->nb_f = 0;
223}
224
226{
227 for (size_t i = 0; i < df->nb_f_allocated; i++)
228 av_frame_free(&df->f[i]);
229 av_freep(&df->f);
230 df->nb_f = 0;
231 df->nb_f_allocated = 0;
232}
233
234/**
235 * Codec worker thread.
236 *
237 * Automatically calls ff_thread_finish_setup() if the codec does
238 * not provide an update_thread_context method, or if the codec returns
239 * before calling it.
240 */
242{
244 AVCodecContext *avctx = p->avctx;
245 const FFCodec *codec = ffcodec(avctx->codec);
246
248
249 pthread_mutex_lock(&p->mutex);
250 while (1) {
251 int ret;
252
253 while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
254 pthread_cond_wait(&p->input_cond, &p->mutex);
255
256 if (p->die) break;
257
258 if (!codec->update_thread_context)
260
261 /* If a decoder supports hwaccel, then it must call ff_get_format().
262 * Since that call must happen before ff_thread_finish_setup(), the
263 * decoder is required to implement update_thread_context() and call
264 * ff_thread_finish_setup() manually. Therefore the above
265 * ff_thread_finish_setup() call did not happen and hwaccel_serializing
266 * cannot be true here. */
267 av_assert0(!p->hwaccel_serializing);
268
269 /* if the previous thread uses thread-unsafe hwaccel then we take the
270 * lock to ensure the threads don't run concurrently */
271 if (hwaccel_serial(avctx)) {
272 pthread_mutex_lock(&p->parent->hwaccel_mutex);
273 p->hwaccel_serializing = 1;
274 }
275
276 ret = 0;
277 while (ret >= 0) {
278 AVFrame *frame;
279
280 /* get the frame which will store the output */
282 if (!frame) {
283 p->result = AVERROR(ENOMEM);
284 goto alloc_fail;
285 }
286
287 /* do the actual decoding */
289 if (ret == 0)
290 p->df.nb_f++;
291 else if (ret < 0 && frame->buf[0])
293
294 p->result = (ret == AVERROR(EAGAIN)) ? 0 : ret;
295 }
296
297 if (atomic_load(&p->state) == STATE_SETTING_UP)
299
300alloc_fail:
301 if (p->hwaccel_serializing) {
302 /* wipe hwaccel state for thread-unsafe hwaccels to avoid stale
303 * pointers lying around;
304 * the state was transferred to FrameThreadContext in
305 * ff_thread_finish_setup(), so nothing is leaked */
306 avctx->hwaccel = NULL;
307 avctx->hwaccel_context = NULL;
309
310 p->hwaccel_serializing = 0;
311 pthread_mutex_unlock(&p->parent->hwaccel_mutex);
312 }
313 av_assert0(!avctx->hwaccel ||
314 (ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE));
315
316 if (p->async_serializing) {
317 p->async_serializing = 0;
318
319 async_unlock(p->parent);
320 }
321
322 pthread_mutex_lock(&p->progress_mutex);
323
325
326 pthread_cond_broadcast(&p->progress_cond);
327 pthread_cond_signal(&p->output_cond);
328 pthread_mutex_unlock(&p->progress_mutex);
329 }
330 pthread_mutex_unlock(&p->mutex);
331
332 return NULL;
333}
334
335/**
336 * Update the next thread's AVCodecContext with values from the reference thread's context.
337 *
338 * @param dst The destination context.
339 * @param src The source context.
340 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
341 * @return 0 on success, negative error code on failure
342 */
344{
345 const FFCodec *const codec = ffcodec(dst->codec);
346 int err = 0;
347
348 if (dst != src && (for_user || codec->update_thread_context)) {
349 dst->time_base = src->time_base;
350 dst->framerate = src->framerate;
351 dst->width = src->width;
352 dst->height = src->height;
353 dst->pix_fmt = src->pix_fmt;
354 dst->sw_pix_fmt = src->sw_pix_fmt;
355
356 dst->coded_width = src->coded_width;
357 dst->coded_height = src->coded_height;
358
359 dst->has_b_frames = src->has_b_frames;
360 dst->idct_algo = src->idct_algo;
361
362 dst->bits_per_coded_sample = src->bits_per_coded_sample;
363 dst->sample_aspect_ratio = src->sample_aspect_ratio;
364
365 dst->profile = src->profile;
366 dst->level = src->level;
367
368 dst->bits_per_raw_sample = src->bits_per_raw_sample;
369 dst->color_primaries = src->color_primaries;
370
371 dst->alpha_mode = src->alpha_mode;
372
373 dst->color_trc = src->color_trc;
374 dst->colorspace = src->colorspace;
375 dst->color_range = src->color_range;
376 dst->chroma_sample_location = src->chroma_sample_location;
377
378 dst->sample_rate = src->sample_rate;
379 dst->sample_fmt = src->sample_fmt;
380 err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
381 if (err < 0)
382 return err;
383
384 if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
385 (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
386 av_buffer_unref(&dst->hw_frames_ctx);
387
388 if (src->hw_frames_ctx) {
389 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
390 if (!dst->hw_frames_ctx)
391 return AVERROR(ENOMEM);
392 }
393 }
394
395 dst->hwaccel_flags = src->hwaccel_flags;
396
397 av_refstruct_replace(&dst->internal->pool, src->internal->pool);
399 }
400
401 if (for_user) {
404 } else {
405 const PerThreadContext *p_src = src->internal->thread_ctx;
406 PerThreadContext *p_dst = dst->internal->thread_ctx;
407
408 if (codec->update_thread_context) {
409 err = codec->update_thread_context(dst, src);
410 if (err < 0)
411 return err;
412 }
413
414 // reset dst hwaccel state if needed
416 (!dst->hwaccel && !dst->internal->hwaccel_priv_data));
417 if (p_dst->hwaccel_threadsafe &&
418 (!p_src->hwaccel_threadsafe || dst->hwaccel != src->hwaccel)) {
420 p_dst->hwaccel_threadsafe = 0;
421 }
422
423 // propagate hwaccel state for threadsafe hwaccels
424 if (p_src->hwaccel_threadsafe) {
425 const FFHWAccel *hwaccel = ffhwaccel(src->hwaccel);
426 if (!dst->hwaccel) {
427 if (hwaccel->priv_data_size) {
428 av_assert0(hwaccel->update_thread_context);
429
430 dst->internal->hwaccel_priv_data =
431 av_mallocz(hwaccel->priv_data_size);
432 if (!dst->internal->hwaccel_priv_data)
433 return AVERROR(ENOMEM);
434 }
435 dst->hwaccel = src->hwaccel;
436 }
437 av_assert0(dst->hwaccel == src->hwaccel);
438
439 if (hwaccel->update_thread_context) {
440 err = hwaccel->update_thread_context(dst, src);
441 if (err < 0) {
442 av_log(dst, AV_LOG_ERROR, "Error propagating hwaccel state\n");
444 return err;
445 }
446 }
447 p_dst->hwaccel_threadsafe = 1;
448 }
449 }
450
451 return err;
452}
453
454/**
455 * Update the next thread's AVCodecContext with values set by the user.
456 *
457 * @param dst The destination context.
458 * @param src The source context.
459 * @return 0 on success, negative error code on failure
460 */
462{
463 int err;
464
465 dst->flags = src->flags;
466
467 dst->draw_horiz_band= src->draw_horiz_band;
468 dst->get_buffer2 = src->get_buffer2;
469
470 dst->opaque = src->opaque;
471 dst->debug = src->debug;
472
473 dst->slice_flags = src->slice_flags;
474 dst->flags2 = src->flags2;
475 dst->export_side_data = src->export_side_data;
476
477 dst->skip_loop_filter = src->skip_loop_filter;
478 dst->skip_idct = src->skip_idct;
479 dst->skip_frame = src->skip_frame;
480
481 dst->frame_num = src->frame_num;
482
483 av_packet_unref(dst->internal->last_pkt_props);
484 err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
485 if (err < 0)
486 return err;
487
488 return 0;
489}
490
492 AVPacket *in_pkt)
493{
494 FrameThreadContext *fctx = p->parent;
495 PerThreadContext *prev_thread = fctx->prev_thread;
496 const AVCodec *codec = p->avctx->codec;
497 int ret;
498
499 pthread_mutex_lock(&p->mutex);
500
501 av_packet_unref(p->avpkt);
502 av_packet_move_ref(p->avpkt, in_pkt);
503
504 if (AVPACKET_IS_EMPTY(p->avpkt))
505 p->avctx->internal->draining = 1;
506
507 ret = update_context_from_user(p->avctx, user_avctx);
508 if (ret) {
509 pthread_mutex_unlock(&p->mutex);
510 return ret;
511 }
512 atomic_store_explicit(&p->debug_threads,
513 (p->avctx->debug & FF_DEBUG_THREADS) != 0,
514 memory_order_relaxed);
515
516 if (prev_thread) {
517 if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
518 pthread_mutex_lock(&prev_thread->progress_mutex);
519 while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
520 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
522 }
523
524 /* codecs without delay might not be prepared to be called repeatedly here during
525 * flushing (vp3/theora), and also don't need to be, since from this point on, they
526 * will always return EOF anyway */
527 if (!p->avctx->internal->draining ||
528 (codec->capabilities & AV_CODEC_CAP_DELAY)) {
529 ret = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
530 if (ret) {
531 pthread_mutex_unlock(&p->mutex);
532 return ret;
533 }
534 }
535 }
536
537 /* transfer the stashed hwaccel state, if any */
538 av_assert0(!p->avctx->hwaccel || p->hwaccel_threadsafe);
539 if (!p->hwaccel_threadsafe) {
540 FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
541 FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
542 FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
543 }
544
545 atomic_store(&p->state, STATE_SETTING_UP);
546 pthread_cond_signal(&p->input_cond);
547 pthread_mutex_unlock(&p->mutex);
548
549 fctx->prev_thread = p;
550 fctx->next_decoding = (fctx->next_decoding + 1) % p->avctx->thread_count;
551
552 return 0;
553}
554
556{
557 FrameThreadContext *fctx = avctx->internal->thread_ctx;
558 int ret = 0;
559
560 /* release the async lock, permitting blocked hwaccel threads to
561 * go forward while we are in this function */
562 async_unlock(fctx);
563
564 /* submit packets to threads while there are no buffered results to return */
565 while (!fctx->df.nb_f && !fctx->result) {
567
568 if (fctx->next_decoding != fctx->next_finished &&
570 goto wait_for_result;
571
572 /* get a packet to be submitted to the next thread */
574 ret = ff_decode_get_packet(avctx, fctx->next_pkt);
575 if (ret < 0 && ret != AVERROR_EOF)
576 goto finish;
577
578 ret = submit_packet(&fctx->threads[fctx->next_decoding], avctx,
579 fctx->next_pkt);
580 if (ret < 0)
581 goto finish;
582
583 /* do not return any frames until all threads have something to do */
584 if (fctx->next_decoding != fctx->next_finished &&
585 !avctx->internal->draining)
586 continue;
587
588 wait_for_result:
589 p = &fctx->threads[fctx->next_finished];
590 fctx->next_finished = (fctx->next_finished + 1) % avctx->thread_count;
591
592 if (atomic_load(&p->state) != STATE_INPUT_READY) {
593 pthread_mutex_lock(&p->progress_mutex);
594 while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
595 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
596 pthread_mutex_unlock(&p->progress_mutex);
597 }
598
599 update_context_from_thread(avctx, p->avctx, 1);
600 fctx->result = p->result;
601 p->result = 0;
602 if (p->df.nb_f)
603 FFSWAP(DecodedFrames, fctx->df, p->df);
604 }
605
606 /* a thread may return multiple frames AND an error
607 * we first return all the frames, then the error */
608 if (fctx->df.nb_f) {
609 decoded_frames_pop(&fctx->df, frame);
610 ret = 0;
611 } else {
612 ret = fctx->result;
613 fctx->result = 0;
614 }
615
616finish:
617 async_lock(fctx);
618 return ret;
619}
620
622{
624 atomic_int *progress = f->progress ? f->progress->progress : NULL;
625
626 if (!progress ||
627 atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
628 return;
629
630 p = f->owner[field]->internal->thread_ctx;
631
632 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
633 av_log(f->owner[field], AV_LOG_DEBUG,
634 "%p finished %d field %d\n", progress, n, field);
635
636 pthread_mutex_lock(&p->progress_mutex);
637
638 atomic_store_explicit(&progress[field], n, memory_order_release);
639
640 pthread_cond_broadcast(&p->progress_cond);
641 pthread_mutex_unlock(&p->progress_mutex);
642}
643
644void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
645{
647 atomic_int *progress = f->progress ? f->progress->progress : NULL;
648
649 if (!progress ||
650 atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
651 return;
652
653 p = f->owner[field]->internal->thread_ctx;
654
655 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
656 av_log(f->owner[field], AV_LOG_DEBUG,
657 "thread awaiting %d field %d from %p\n", n, field, progress);
658
659 pthread_mutex_lock(&p->progress_mutex);
660 while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
661 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
662 pthread_mutex_unlock(&p->progress_mutex);
663}
664
667
668 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
669
670 p = avctx->internal->thread_ctx;
671
672 p->hwaccel_threadsafe = avctx->hwaccel &&
674
675 if (hwaccel_serial(avctx) && !p->hwaccel_serializing) {
676 pthread_mutex_lock(&p->parent->hwaccel_mutex);
677 p->hwaccel_serializing = 1;
678 }
679
680 /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
681 if (avctx->hwaccel &&
682 !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_ASYNC_SAFE)) {
683 p->async_serializing = 1;
684
685 async_lock(p->parent);
686 }
687
688 /* thread-unsafe hwaccels share a single private data instance, so we
689 * save hwaccel state for passing to the next thread;
690 * this is done here so that this worker thread can wipe its own hwaccel
691 * state after decoding, without requiring synchronization */
692 av_assert0(!p->parent->stash_hwaccel);
693 if (hwaccel_serial(avctx)) {
694 p->parent->stash_hwaccel = avctx->hwaccel;
695 p->parent->stash_hwaccel_context = avctx->hwaccel_context;
696 p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
697 }
698
699 pthread_mutex_lock(&p->progress_mutex);
700 if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
701 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
702 }
703
705
706 pthread_cond_broadcast(&p->progress_cond);
707 pthread_mutex_unlock(&p->progress_mutex);
708}
709
710/// Waits for all threads to finish.
711static av_cold void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
712{
713 int i;
714
715 async_unlock(fctx);
716
717 for (i = 0; i < thread_count; i++) {
718 PerThreadContext *p = &fctx->threads[i];
719
720 if (atomic_load(&p->state) != STATE_INPUT_READY) {
721 pthread_mutex_lock(&p->progress_mutex);
722 while (atomic_load(&p->state) != STATE_INPUT_READY)
723 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
724 pthread_mutex_unlock(&p->progress_mutex);
725 }
726 }
727
728 async_lock(fctx);
729}
730
731#define OFF(member) offsetof(FrameThreadContext, member)
732DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
733 (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
734 (OFF(async_cond)));
735#undef OFF
736
737#define OFF(member) offsetof(PerThreadContext, member)
738DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
739 (OFF(progress_mutex), OFF(mutex)),
740 (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
741#undef OFF
742
743av_cold void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
744{
745 FrameThreadContext *fctx = avctx->internal->thread_ctx;
746 const FFCodec *codec = ffcodec(avctx->codec);
747 int i;
748
749 park_frame_worker_threads(fctx, thread_count);
750
751 for (i = 0; i < thread_count; i++) {
752 PerThreadContext *p = &fctx->threads[i];
753 AVCodecContext *ctx = p->avctx;
754
755 if (ctx->internal) {
756 if (p->thread_init == INITIALIZED) {
757 pthread_mutex_lock(&p->mutex);
758 p->die = 1;
759 pthread_cond_signal(&p->input_cond);
760 pthread_mutex_unlock(&p->mutex);
761
762 pthread_join(p->thread, NULL);
763 }
764 if (codec->close && p->thread_init != UNINITIALIZED)
765 codec->close(ctx);
766
767 /* When using a threadsafe hwaccel, this is where
768 * each thread's context is uninit'd and freed. */
770
771 if (ctx->priv_data) {
772 if (codec->p.priv_class)
773 av_opt_free(ctx->priv_data);
774 av_freep(&ctx->priv_data);
775 }
776
777 av_refstruct_unref(&ctx->internal->pool);
778 av_packet_free(&ctx->internal->in_pkt);
779 av_packet_free(&ctx->internal->last_pkt_props);
781 av_freep(&ctx->internal);
782 av_buffer_unref(&ctx->hw_frames_ctx);
783 av_frame_side_data_free(&ctx->decoded_side_data,
784 &ctx->nb_decoded_side_data);
785 }
786
787 decoded_frames_free(&p->df);
788
789 ff_pthread_free(p, per_thread_offsets);
790 av_packet_free(&p->avpkt);
791
792 av_freep(&p->avctx);
793 }
794
795 decoded_frames_free(&fctx->df);
796 av_packet_free(&fctx->next_pkt);
797
798 av_freep(&fctx->threads);
799 ff_pthread_free(fctx, thread_ctx_offsets);
800
801 /* if we have stashed hwaccel state, move it to the user-facing context,
802 * so it will be freed in ff_codec_close() */
803 av_assert0(!avctx->hwaccel);
804 FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
805 FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
807
808 av_freep(&avctx->internal->thread_ctx);
809}
810
811static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
813 const FFCodec *codec, int first)
814{
816 int err;
817
818 atomic_init(&p->state, STATE_INPUT_READY);
819
820 copy = av_memdup(avctx, sizeof(*avctx));
821 if (!copy)
822 return AVERROR(ENOMEM);
823 copy->priv_data = NULL;
824 copy->decoded_side_data = NULL;
825 copy->nb_decoded_side_data = 0;
826
827 /* From now on, this PerThreadContext will be cleaned up by
828 * ff_frame_thread_free in case of errors. */
829 (*threads_to_free)++;
830
831 p->parent = fctx;
832 p->avctx = copy;
833
834 copy->internal = ff_decode_internal_alloc();
835 if (!copy->internal)
836 return AVERROR(ENOMEM);
838 copy->internal->thread_ctx = p;
839 copy->internal->progress_frame_pool = avctx->internal->progress_frame_pool;
840
841 copy->delay = avctx->delay;
842
843 if (codec->priv_data_size) {
844 copy->priv_data = av_mallocz(codec->priv_data_size);
845 if (!copy->priv_data)
846 return AVERROR(ENOMEM);
847
848 if (codec->p.priv_class) {
849 *(const AVClass **)copy->priv_data = codec->p.priv_class;
850 err = av_opt_copy(copy->priv_data, avctx->priv_data);
851 if (err < 0)
852 return err;
853 }
854 }
855
856 err = ff_pthread_init(p, per_thread_offsets);
857 if (err < 0)
858 return err;
859
860 if (!(p->avpkt = av_packet_alloc()))
861 return AVERROR(ENOMEM);
862
863 copy->internal->is_frame_mt = 1;
864 if (!first)
865 copy->internal->is_copy = 1;
866
867 copy->internal->in_pkt = av_packet_alloc();
868 if (!copy->internal->in_pkt)
869 return AVERROR(ENOMEM);
870
871 copy->internal->last_pkt_props = av_packet_alloc();
872 if (!copy->internal->last_pkt_props)
873 return AVERROR(ENOMEM);
874
875 if (codec->init) {
876 err = codec->init(copy);
877 if (err < 0) {
879 p->thread_init = NEEDS_CLOSE;
880 return err;
881 }
882 }
883 p->thread_init = NEEDS_CLOSE;
884
885 if (first)
887
888 const AVCodecContext *src = first ? copy : avctx;
889 AVCodecContext *dst = first ? avctx : copy;
890 av_frame_side_data_free(&dst->decoded_side_data, &dst->nb_decoded_side_data);
891 for (int i = 0; i < src->nb_decoded_side_data; i++) {
892 err = av_frame_side_data_clone(&dst->decoded_side_data,
893 &dst->nb_decoded_side_data,
894 src->decoded_side_data[i], 0);
895 if (err < 0)
896 return err;
897 }
898
899 atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
900
901 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
902 if (err < 0)
903 return err;
904 p->thread_init = INITIALIZED;
905
906 return 0;
907}
908
910{
911 int thread_count = avctx->thread_count;
912 const FFCodec *codec = ffcodec(avctx->codec);
913 FrameThreadContext *fctx;
914 int err, i = 0;
915
916 if (!thread_count) {
917 int nb_cpus = av_cpu_count();
918 // use number of cores + 1 as thread count if there is more than one
919 if (nb_cpus > 1)
920 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
921 else
922 thread_count = avctx->thread_count = 1;
923 }
924
925 if (thread_count <= 1) {
926 avctx->active_thread_type = 0;
927 return 0;
928 }
929
930 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
931 if (!fctx)
932 return AVERROR(ENOMEM);
933
934 err = ff_pthread_init(fctx, thread_ctx_offsets);
935 if (err < 0) {
936 ff_pthread_free(fctx, thread_ctx_offsets);
937 av_freep(&avctx->internal->thread_ctx);
938 return err;
939 }
940
941 fctx->next_pkt = av_packet_alloc();
942 if (!fctx->next_pkt)
943 return AVERROR(ENOMEM);
944
945 fctx->async_lock = 1;
946
947 if (codec->p.type == AVMEDIA_TYPE_VIDEO)
948 avctx->delay = avctx->thread_count - 1;
949
950 fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
951 if (!fctx->threads) {
952 err = AVERROR(ENOMEM);
953 goto error;
954 }
955
956 for (; i < thread_count; ) {
957 PerThreadContext *p = &fctx->threads[i];
958 int first = !i;
959
960 err = init_thread(p, &i, fctx, avctx, codec, first);
961 if (err < 0)
962 goto error;
963 }
964
965 return 0;
966
967error:
968 ff_frame_thread_free(avctx, i);
969 return err;
970}
971
973{
974 int i;
975 FrameThreadContext *fctx = avctx->internal->thread_ctx;
976
977 if (!fctx) return;
978
980 if (fctx->prev_thread) {
981 if (fctx->prev_thread != &fctx->threads[0])
983 }
984
985 fctx->next_decoding = fctx->next_finished = 0;
986 fctx->prev_thread = NULL;
987
988 decoded_frames_flush(&fctx->df);
989 fctx->result = 0;
990
991 for (i = 0; i < avctx->thread_count; i++) {
992 PerThreadContext *p = &fctx->threads[i];
993
994 decoded_frames_flush(&p->df);
995 p->result = 0;
996
997 avcodec_flush_buffers(p->avctx);
998 }
999}
1000
1002{
1003 if ((avctx->active_thread_type & FF_THREAD_FRAME) &&
1004 ffcodec(avctx->codec)->update_thread_context) {
1006
1007 if (atomic_load(&p->state) != STATE_SETTING_UP)
1008 return 0;
1009 }
1010
1011 return 1;
1012}
1013
1015{
1017 int err;
1018
1019 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1020 return ff_get_buffer(avctx, f, flags);
1021
1022 p = avctx->internal->thread_ctx;
1023 if (atomic_load(&p->state) != STATE_SETTING_UP &&
1024 ffcodec(avctx->codec)->update_thread_context) {
1025 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
1026 return -1;
1027 }
1028
1029 pthread_mutex_lock(&p->parent->buffer_mutex);
1030 err = ff_get_buffer(avctx, f, flags);
1031
1032 pthread_mutex_unlock(&p->parent->buffer_mutex);
1033
1034 return err;
1035}
1036
1038{
1039 int ret = thread_get_buffer_internal(avctx, f, flags);
1040 if (ret < 0)
1041 av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1042 return ret;
1043}
1044
1046{
1047 int ret;
1048
1049 f->owner[0] = f->owner[1] = avctx;
1050 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1051 return ff_get_buffer(avctx, f->f, flags);
1052
1053 f->progress = av_refstruct_allocz(sizeof(*f->progress));
1054 if (!f->progress)
1055 return AVERROR(ENOMEM);
1056
1057 atomic_init(&f->progress->progress[0], -1);
1058 atomic_init(&f->progress->progress[1], -1);
1059
1060 ret = ff_thread_get_buffer(avctx, f->f, flags);
1061 if (ret)
1062 av_refstruct_unref(&f->progress);
1063 return ret;
1064}
1065
1067{
1068 av_refstruct_unref(&f->progress);
1069 f->owner[0] = f->owner[1] = NULL;
1070 if (f->f)
1071 av_frame_unref(f->f);
1072}
1073
1075{
1077 const void *ref;
1078
1079 if (!avctx->internal->is_copy)
1080 return avctx->active_thread_type & FF_THREAD_FRAME ?
1082
1083 p = avctx->internal->thread_ctx;
1084
1085 av_assert1(memcpy(&ref, (char*)avctx->priv_data + offset, sizeof(ref)) && ref == NULL);
1086
1087 memcpy(&ref, (const char*)p->parent->threads[0].avctx->priv_data + offset, sizeof(ref));
1088 av_assert1(ref);
1089 av_refstruct_replace((char*)avctx->priv_data + offset, ref);
1090
1091 return FF_THREAD_IS_COPY;
1092}
1093
1095{
1097
1098 if (!AVPACKET_IS_EMPTY(p->avpkt)) {
1099 av_packet_move_ref(pkt, p->avpkt);
1100 return 0;
1101 }
1102
1103 return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
1104}
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition dsp.h:87
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition avassert.h:58
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition avcodec.h:1590
#define FF_DEBUG_THREADS
Definition avcodec.h:1405
void ff_decode_internal_uninit(struct AVCodecContext *avctx)
Definition decode.c:2406
int ff_decode_receive_frame_internal(struct AVCodecContext *avctx, AVFrame *frame)
Do the actual decoding and obtain a decoded frame from the decoder, if available.
Definition decode.c:625
void ff_decode_internal_sync(struct AVCodecContext *dst, const struct AVCodecContext *src)
struct AVCodecInternal * ff_decode_internal_alloc(void)
Definition decode.c:2385
refcounted data buffer API
#define flags(name, subs,...)
Definition cbs_h264.c:74
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define f(width, name)
Definition cbs_vp8.c:236
static av_always_inline const FFCodec * ffcodec(const AVCodec *codec)
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
#define NULL
Definition coverity.c:32
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition decode.c:1777
int ff_decode_get_packet(AVCodecContext *avctx, AVPacket *pkt)
Called by decoders to get the next packet for decoding.
Definition decode.c:254
void ff_hwaccel_uninit(AVCodecContext *avctx)
Definition decode.c:1217
#define ff_thread_get_packet(avctx, pkt)
Definition decode.c:225
#define ff_thread_receive_frame(avctx, frame, flags)
Definition decode.c:226
static AVPacket * pkt
static AVFrame * frame
#define atomic_store(object, desired)
Definition stdatomic.h:85
intptr_t atomic_int
Definition stdatomic.h:55
#define atomic_load_explicit(object, order)
Definition stdatomic.h:96
#define atomic_load(object)
Definition stdatomic.h:93
#define atomic_store_explicit(object, desired, order)
Definition stdatomic.h:90
#define atomic_init(obj, value)
Definition stdatomic.h:33
static const char * hwaccel
Definition ffplay.c:357
reference-counted frame API
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition codec.h:79
#define AV_CODEC_RECEIVE_FRAME_FLAG_SYNCHRONOUS
The decoder will bypass frame threading and return the next frame as soon as possible.
Definition avcodec.h:428
void avcodec_flush_buffers(AVCodecContext *avctx)
Reset the internal codec state / flush internal buffers.
Definition avcodec.c:389
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition packet.c:74
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition packet.c:434
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition packet.c:491
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition packet.c:63
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition packet.c:397
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition buffer.c:139
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition buffer.c:103
#define AVERROR_EOF
End of file.
Definition error.h:57
#define AVERROR(e)
Definition error.h:45
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition frame.c:496
void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd)
Free all side data entries and their contents, then zeroes out the values which the pointers are poin...
Definition side_data.c:139
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition frame.c:523
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition frame.c:64
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition frame.c:52
int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd, const AVFrameSideData *src, unsigned int flags)
Add a new side data entry to an array based on existing side data, taking a reference towards the con...
Definition side_data.c:254
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition mem.c:217
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition mem.c:302
@ AVMEDIA_TYPE_VIDEO
Definition avutil.h:200
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition opt.c:2025
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition opt.c:2215
#define HWACCEL_CAP_ASYNC_SAFE
Header providing the internals of AVHWAccel.
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
#define HWACCEL_CAP_THREAD_SAFE
unsigned offset
Definition libaomenc.c:763
common internal api header.
const char * arg
Definition jacosubdec.c:65
#define AVPACKET_IS_EMPTY(pkt)
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition pthread.c:92
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
Definition pthread.c:105
Multithreading API for decoders.
ThreadingStatus
Definition thread.h:60
@ FF_THREAD_IS_COPY
Definition thread.h:61
@ FF_THREAD_IS_FIRST_THREAD
Definition thread.h:62
@ FF_THREAD_NO_FRAME_THREADING
Definition thread.h:63
#define av_cold
Definition attributes.h:117
int av_cpu_count(void)
Definition cpu.c:228
common internal API header
#define attribute_align_arg
Definition internal.h:50
static int ff_thread_setname(const char *name)
Definition thread.h:216
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
AVOptions.
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition os2threads.h:162
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition os2threads.h:152
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition os2threads.h:119
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition os2threads.h:94
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition os2threads.h:80
_fmutex pthread_mutex_t
Definition os2threads.h:53
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition os2threads.h:126
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition os2threads.h:192
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static void decoded_frames_flush(DecodedFrames *df)
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
static void async_unlock(FrameThreadContext *fctx)
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
av_cold int ff_frame_thread_init(AVCodecContext *avctx)
static av_cold void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
void ff_thread_release_ext_buffer(ThreadFrame *f)
Unref a ThreadFrame.
av_cold enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
Allows to synchronize objects whose lifetime is the whole decoding process among all frame threads.
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
static void decoded_frames_pop(DecodedFrames *df, AVFrame *dst)
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around ff_get_buffer() for frame-multithreaded codecs.
@ NEEDS_CLOSE
FFCodec->close needs to be called.
@ INITIALIZED
Thread has been properly set up.
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
static int update_context_from_user(AVCodecContext *dst, const AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
static int update_context_from_thread(AVCodecContext *dst, const AVCodecContext *src, int for_user)
Update the next thread's AVCodecContext with values from the reference thread's context.
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *in_pkt)
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
@ STATE_SETTING_UP
Set before the codec has called ff_thread_finish_setup().
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
@ STATE_SETUP_FINISHED
Set after the codec has called ff_thread_finish_setup().
static AVFrame * decoded_frames_get_free(DecodedFrames *df)
static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, const FFCodec *codec, int first)
static void async_lock(FrameThreadContext *fctx)
av_cold void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
#define OFF(member)
static void thread_set_name(PerThreadContext *p)
static int hwaccel_serial(const AVCodecContext *avctx)
int ff_thread_can_start_frame(AVCodecContext *avctx)
static void decoded_frames_free(DecodedFrames *df)
av_cold void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
#define DEFINE_OFFSET_ARRAY(type, name, cnt_variable, mutexes, conds)
#define MAX_AUTO_THREADS
const char * name
Definition qsvenc.c:142
void av_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition refstruct.c:120
void av_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition refstruct.c:160
static void * av_refstruct_allocz(size_t size)
Equivalent to av_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition refstruct.h:105
static AVMutex mutex
Definition resman.c:61
#define snprintf
Definition snprintf.h:34
Describe the class of an AVClass context structure.
Definition log.h:76
main external API structure.
Definition avcodec.h:443
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition avcodec.h:1423
int active_thread_type
Which multithreading methods are in use by the codec.
Definition avcodec.h:1598
const struct AVCodec * codec
Definition avcodec.h:452
int delay
Codec delay.
Definition avcodec.h:587
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition avcodec.h:1579
void * hwaccel_context
Legacy hardware accelerator context.
Definition avcodec.h:1447
struct AVCodecInternal * internal
Private context used for internal data.
Definition avcodec.h:478
void * priv_data
Definition avcodec.h:470
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
Definition internal.h:54
void * thread_ctx
Definition internal.h:73
void * hwaccel_priv_data
hwaccel-specific private data
Definition internal.h:130
int draining
decoding: AVERROR_EOF has been returned from ff_decode_get_packet(); must not be used by decoders tha...
Definition internal.h:139
struct AVRefStructPool * progress_frame_pool
Definition internal.h:71
AVCodec.
Definition codec.h:175
const AVClass * priv_class
AVClass for the private context.
Definition codec.h:197
enum AVMediaType type
Definition codec.h:188
const char * name
Name of the codec implementation.
Definition codec.h:182
int capabilities
Codec capabilities.
Definition codec.h:194
This structure describes decoded (raw) audio or video data.
Definition frame.h:472
This structure stores compressed data.
Definition packet.h:580
AVFrame ** f
size_t nb_f_allocated
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
int priv_data_size
AVCodec p
The public AVCodec.
int(* init)(struct AVCodecContext *)
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
int(* close)(struct AVCodecContext *)
int caps_internal
Internal hwaccel capabilities.
Context stored in the client AVCodecInternal thread_ctx.
pthread_mutex_t async_mutex
int next_decoding
The next context to submit a packet to.
PerThreadContext * threads
The contexts for each thread.
const AVHWAccel * stash_hwaccel
pthread_cond_t async_cond
AVPacket * next_pkt
Packet to be submitted to the next thread for decoding.
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
int next_finished
The next context to return output from.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when thread-unsafe hwaccel is used.
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
struct FrameThreadContext * parent
AVCodecContext * avctx
Context used to decode packets passed to this thread.
DecodedFrames df
Decoded frames from a single decode iteration.
int die
Set when the thread should exit.
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
int result
The result of the last codec decode/encode() call.
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
atomic_int progress[2]
#define av_mallocz(s)
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[40]
Definition aes_ctr.c:52
#define src
Definition vp8dsp.c:248
static int ref[MAX_W *MAX_W]
static AVFormatContext * ctx
Definition movenc.c:49
static void finish(void)
Definition movenc.c:374
static void copy(const float *p1, float *p2, const int length)
#define df(A, B)
Definition vf_xbr.c:91