FFmpeg
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 "config.h"
26 
27 #include <stdatomic.h>
28 #include <stdint.h>
29 
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "decode.h"
33 #include "hwconfig.h"
34 #include "internal.h"
35 #include "pthread_internal.h"
36 #include "thread.h"
37 #include "threadframe.h"
38 #include "version_major.h"
39 
40 #include "libavutil/avassert.h"
41 #include "libavutil/buffer.h"
42 #include "libavutil/common.h"
43 #include "libavutil/cpu.h"
44 #include "libavutil/frame.h"
45 #include "libavutil/internal.h"
46 #include "libavutil/log.h"
47 #include "libavutil/mem.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/thread.h"
50 
51 enum {
52  ///< Set when the thread is awaiting a packet.
54  ///< Set before the codec has called ff_thread_finish_setup().
56  /**
57  * Set when the codec calls get_buffer().
58  * State is returned to STATE_SETTING_UP afterwards.
59  */
61  /**
62  * Set when the codec calls get_format().
63  * State is returned to STATE_SETTING_UP afterwards.
64  */
66  ///< Set after the codec has called ff_thread_finish_setup().
68 };
69 
70 enum {
71  UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
72  NEEDS_CLOSE, ///< FFCodec->close needs to be called
73  INITIALIZED, ///< Thread has been properly set up
74 };
75 
76 /**
77  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
78  */
79 typedef struct PerThreadContext {
81 
84  unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
85  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
86  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
87  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
88 
89  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
90  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
91 
92  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
93 
94  AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
95 
96  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
97  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
98  int result; ///< The result of the last codec decode/encode() call.
99 
101 
102  int die; ///< Set when the thread should exit.
103 
106 
107  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
109 
110 /**
111  * Context stored in the client AVCodecInternal thread_ctx.
112  */
113 typedef struct FrameThreadContext {
114  PerThreadContext *threads; ///< The contexts for each thread.
115  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
116 
117  unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
118  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
119  /**
120  * This lock is used for ensuring threads run in serial when hwaccel
121  * is used.
122  */
127 
128  int next_decoding; ///< The next context to submit a packet to.
129  int next_finished; ///< The next context to return output from.
130 
131  int delaying; /**<
132  * Set for the first N packets, where N is the number of threads.
133  * While it is set, ff_thread_en/decode_frame won't return any results.
134  */
135 
136  /* hwaccel state is temporarily stored here in order to transfer its ownership
137  * to the next decoding thread without the need for extra synchronization */
142 
143 static void async_lock(FrameThreadContext *fctx)
144 {
146  while (fctx->async_lock)
147  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
148  fctx->async_lock = 1;
150 }
151 
153 {
155  av_assert0(fctx->async_lock);
156  fctx->async_lock = 0;
159 }
160 
162 {
163  AVCodecContext *avctx = p->avctx;
164  int idx = p - p->parent->threads;
165  char name[16];
166 
167  snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
168 
170 }
171 
172 /**
173  * Codec worker thread.
174  *
175  * Automatically calls ff_thread_finish_setup() if the codec does
176  * not provide an update_thread_context method, or if the codec returns
177  * before calling it.
178  */
179 static attribute_align_arg void *frame_worker_thread(void *arg)
180 {
181  PerThreadContext *p = arg;
182  AVCodecContext *avctx = p->avctx;
183  const FFCodec *codec = ffcodec(avctx->codec);
184 
185  thread_set_name(p);
186 
188  while (1) {
189  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
191 
192  if (p->die) break;
193 
194  if (!codec->update_thread_context)
195  ff_thread_finish_setup(avctx);
196 
197  /* If a decoder supports hwaccel, then it must call ff_get_format().
198  * Since that call must happen before ff_thread_finish_setup(), the
199  * decoder is required to implement update_thread_context() and call
200  * ff_thread_finish_setup() manually. Therefore the above
201  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
202  * cannot be true here. */
204 
205  /* if the previous thread uses hwaccel then we take the lock to ensure
206  * the threads don't run concurrently */
207  if (avctx->hwaccel) {
209  p->hwaccel_serializing = 1;
210  }
211 
212  av_frame_unref(p->frame);
213  p->got_frame = 0;
214  p->result = codec->cb.decode(avctx, p->frame, &p->got_frame, p->avpkt);
215 
216  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0])
217  ff_thread_release_buffer(avctx, p->frame);
218 
219  if (atomic_load(&p->state) == STATE_SETTING_UP)
220  ff_thread_finish_setup(avctx);
221 
222  if (p->hwaccel_serializing) {
223  /* wipe hwaccel state to avoid stale pointers lying around;
224  * the state was transferred to FrameThreadContext in
225  * ff_thread_finish_setup(), so nothing is leaked */
226  avctx->hwaccel = NULL;
227  avctx->hwaccel_context = NULL;
228  avctx->internal->hwaccel_priv_data = NULL;
229 
230  p->hwaccel_serializing = 0;
232  }
233  av_assert0(!avctx->hwaccel);
234 
235  if (p->async_serializing) {
236  p->async_serializing = 0;
237 
238  async_unlock(p->parent);
239  }
240 
242 
244 
248  }
250 
251  return NULL;
252 }
253 
254 /**
255  * Update the next thread's AVCodecContext with values from the reference thread's context.
256  *
257  * @param dst The destination context.
258  * @param src The source context.
259  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
260  * @return 0 on success, negative error code on failure
261  */
263 {
264  const FFCodec *const codec = ffcodec(dst->codec);
265  int err = 0;
266 
267  if (dst != src && (for_user || codec->update_thread_context)) {
268  dst->time_base = src->time_base;
269  dst->framerate = src->framerate;
270  dst->width = src->width;
271  dst->height = src->height;
272  dst->pix_fmt = src->pix_fmt;
273  dst->sw_pix_fmt = src->sw_pix_fmt;
274 
275  dst->coded_width = src->coded_width;
276  dst->coded_height = src->coded_height;
277 
278  dst->has_b_frames = src->has_b_frames;
279  dst->idct_algo = src->idct_algo;
280  dst->properties = src->properties;
281 
282  dst->bits_per_coded_sample = src->bits_per_coded_sample;
283  dst->sample_aspect_ratio = src->sample_aspect_ratio;
284 
285  dst->profile = src->profile;
286  dst->level = src->level;
287 
288  dst->bits_per_raw_sample = src->bits_per_raw_sample;
289  dst->ticks_per_frame = src->ticks_per_frame;
290  dst->color_primaries = src->color_primaries;
291 
292  dst->color_trc = src->color_trc;
293  dst->colorspace = src->colorspace;
294  dst->color_range = src->color_range;
295  dst->chroma_sample_location = src->chroma_sample_location;
296 
297  dst->sample_rate = src->sample_rate;
298  dst->sample_fmt = src->sample_fmt;
299 #if FF_API_OLD_CHANNEL_LAYOUT
301  dst->channels = src->channels;
302  dst->channel_layout = src->channel_layout;
304 #endif
305  err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
306  if (err < 0)
307  return err;
308 
309  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
310  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
312 
313  if (src->hw_frames_ctx) {
314  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
315  if (!dst->hw_frames_ctx)
316  return AVERROR(ENOMEM);
317  }
318  }
319 
320  dst->hwaccel_flags = src->hwaccel_flags;
321 
322  err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
323  if (err < 0)
324  return err;
325  }
326 
327  if (for_user) {
329  err = codec->update_thread_context_for_user(dst, src);
330  } else {
331  if (codec->update_thread_context)
332  err = codec->update_thread_context(dst, src);
333  }
334 
335  return err;
336 }
337 
338 /**
339  * Update the next thread's AVCodecContext with values set by the user.
340  *
341  * @param dst The destination context.
342  * @param src The source context.
343  * @return 0 on success, negative error code on failure
344  */
346 {
347  int err;
348 
349  dst->flags = src->flags;
350 
351  dst->draw_horiz_band= src->draw_horiz_band;
352  dst->get_buffer2 = src->get_buffer2;
353 
354  dst->opaque = src->opaque;
355  dst->debug = src->debug;
356 
357  dst->slice_flags = src->slice_flags;
358  dst->flags2 = src->flags2;
359  dst->export_side_data = src->export_side_data;
360 
361  dst->skip_loop_filter = src->skip_loop_filter;
362  dst->skip_idct = src->skip_idct;
363  dst->skip_frame = src->skip_frame;
364 
365  dst->frame_num = src->frame_num;
366 #if FF_API_AVCTX_FRAME_NUMBER
368  dst->frame_number = src->frame_number;
370 #endif
371 #if FF_API_REORDERED_OPAQUE
373  dst->reordered_opaque = src->reordered_opaque;
375 #endif
376 
377 #if FF_API_SLICE_OFFSET
379  if (src->slice_count && src->slice_offset) {
380  if (dst->slice_count < src->slice_count) {
381  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
382  sizeof(*dst->slice_offset));
383  if (err < 0)
384  return err;
385  }
386  memcpy(dst->slice_offset, src->slice_offset,
387  src->slice_count * sizeof(*dst->slice_offset));
388  }
389  dst->slice_count = src->slice_count;
391 #endif
392 
394  err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
395  if (err < 0)
396  return err;
397 
398  return 0;
399 }
400 
401 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
402  AVPacket *avpkt)
403 {
404  FrameThreadContext *fctx = p->parent;
405  PerThreadContext *prev_thread = fctx->prev_thread;
406  const AVCodec *codec = p->avctx->codec;
407  int ret;
408 
409  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
410  return 0;
411 
413 
414  ret = update_context_from_user(p->avctx, user_avctx);
415  if (ret) {
417  return ret;
418  }
420  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
421  memory_order_relaxed);
422 
423  if (prev_thread) {
424  int err;
425  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
426  pthread_mutex_lock(&prev_thread->progress_mutex);
427  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
428  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
429  pthread_mutex_unlock(&prev_thread->progress_mutex);
430  }
431 
432  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
433  if (err) {
435  return err;
436  }
437  }
438 
439  /* transfer the stashed hwaccel state, if any */
440  av_assert0(!p->avctx->hwaccel);
441  FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
444 
446  ret = av_packet_ref(p->avpkt, avpkt);
447  if (ret < 0) {
449  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
450  return ret;
451  }
452 
456 
457  fctx->prev_thread = p;
458  fctx->next_decoding++;
459 
460  return 0;
461 }
462 
464  AVFrame *picture, int *got_picture_ptr,
465  AVPacket *avpkt)
466 {
467  FrameThreadContext *fctx = avctx->internal->thread_ctx;
468  int finished = fctx->next_finished;
469  PerThreadContext *p;
470  int err;
471 
472  /* release the async lock, permitting blocked hwaccel threads to
473  * go forward while we are in this function */
474  async_unlock(fctx);
475 
476  /*
477  * Submit a packet to the next decoding thread.
478  */
479 
480  p = &fctx->threads[fctx->next_decoding];
481  err = submit_packet(p, avctx, avpkt);
482  if (err)
483  goto finish;
484 
485  /*
486  * If we're still receiving the initial packets, don't return a frame.
487  */
488 
489  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
490  fctx->delaying = 0;
491 
492  if (fctx->delaying) {
493  *got_picture_ptr=0;
494  if (avpkt->size) {
495  err = avpkt->size;
496  goto finish;
497  }
498  }
499 
500  /*
501  * Return the next available frame from the oldest thread.
502  * If we're at the end of the stream, then we have to skip threads that
503  * didn't output a frame/error, because we don't want to accidentally signal
504  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
505  */
506 
507  do {
508  p = &fctx->threads[finished++];
509 
510  if (atomic_load(&p->state) != STATE_INPUT_READY) {
512  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
515  }
516 
517  av_frame_move_ref(picture, p->frame);
518  *got_picture_ptr = p->got_frame;
519  picture->pkt_dts = p->avpkt->dts;
520  err = p->result;
521 
522  /*
523  * A later call with avkpt->size == 0 may loop over all threads,
524  * including this one, searching for a frame/error to return before being
525  * stopped by the "finished != fctx->next_finished" condition.
526  * Make sure we don't mistakenly return the same frame/error again.
527  */
528  p->got_frame = 0;
529  p->result = 0;
530 
531  if (finished >= avctx->thread_count) finished = 0;
532  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
533 
534  update_context_from_thread(avctx, p->avctx, 1);
535 
536  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
537 
538  fctx->next_finished = finished;
539 
540  /* return the size of the consumed packet if no error occurred */
541  if (err >= 0)
542  err = avpkt->size;
543 finish:
544  async_lock(fctx);
545  return err;
546 }
547 
549 {
550  PerThreadContext *p;
551  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
552 
553  if (!progress ||
554  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
555  return;
556 
557  p = f->owner[field]->internal->thread_ctx;
558 
559  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
560  av_log(f->owner[field], AV_LOG_DEBUG,
561  "%p finished %d field %d\n", progress, n, field);
562 
564 
565  atomic_store_explicit(&progress[field], n, memory_order_release);
566 
569 }
570 
571 void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
572 {
573  PerThreadContext *p;
574  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
575 
576  if (!progress ||
577  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
578  return;
579 
580  p = f->owner[field]->internal->thread_ctx;
581 
582  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
583  av_log(f->owner[field], AV_LOG_DEBUG,
584  "thread awaiting %d field %d from %p\n", n, field, progress);
585 
587  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
590 }
591 
593  PerThreadContext *p = avctx->internal->thread_ctx;
594 
595  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
596 
597  if (avctx->hwaccel && !p->hwaccel_serializing) {
599  p->hwaccel_serializing = 1;
600  }
601 
602  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
603  if (avctx->hwaccel &&
605  p->async_serializing = 1;
606 
607  async_lock(p->parent);
608  }
609 
610  /* save hwaccel state for passing to the next thread;
611  * this is done here so that this worker thread can wipe its own hwaccel
612  * state after decoding, without requiring synchronization */
614  p->parent->stash_hwaccel = avctx->hwaccel;
617 
620  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
621  }
622 
624 
627 }
628 
629 /// Waits for all threads to finish.
630 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
631 {
632  int i;
633 
634  async_unlock(fctx);
635 
636  for (i = 0; i < thread_count; i++) {
637  PerThreadContext *p = &fctx->threads[i];
638 
639  if (atomic_load(&p->state) != STATE_INPUT_READY) {
641  while (atomic_load(&p->state) != STATE_INPUT_READY)
644  }
645  p->got_frame = 0;
646  }
647 
648  async_lock(fctx);
649 }
650 
651 #define OFF(member) offsetof(FrameThreadContext, member)
652 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
653  (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
654  (OFF(async_cond)));
655 #undef OFF
656 
657 #define OFF(member) offsetof(PerThreadContext, member)
658 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
659  (OFF(progress_mutex), OFF(mutex)),
660  (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
661 #undef OFF
662 
663 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
664 {
665  FrameThreadContext *fctx = avctx->internal->thread_ctx;
666  const FFCodec *codec = ffcodec(avctx->codec);
667  int i;
668 
669  park_frame_worker_threads(fctx, thread_count);
670 
671  for (i = 0; i < thread_count; i++) {
672  PerThreadContext *p = &fctx->threads[i];
673  AVCodecContext *ctx = p->avctx;
674 
675  if (ctx->internal) {
676  if (p->thread_init == INITIALIZED) {
678  p->die = 1;
681 
682  pthread_join(p->thread, NULL);
683  }
684  if (codec->close && p->thread_init != UNINITIALIZED)
685  codec->close(ctx);
686 
687  if (ctx->priv_data) {
688  if (codec->p.priv_class)
691  }
692 
693 #if FF_API_SLICE_OFFSET
695  av_freep(&ctx->slice_offset);
697 #endif
698 
699  av_buffer_unref(&ctx->internal->pool);
700  av_packet_free(&ctx->internal->last_pkt_props);
701  av_freep(&ctx->internal);
702  av_buffer_unref(&ctx->hw_frames_ctx);
703  }
704 
705  av_frame_free(&p->frame);
706 
707  ff_pthread_free(p, per_thread_offsets);
708  av_packet_free(&p->avpkt);
709 
710  av_freep(&p->avctx);
711  }
712 
713  av_freep(&fctx->threads);
714  ff_pthread_free(fctx, thread_ctx_offsets);
715 
716  /* if we have stashed hwaccel state, move it to the user-facing context,
717  * so it will be freed in avcodec_close() */
718  av_assert0(!avctx->hwaccel);
719  FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
720  FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
721  FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
722 
723  av_freep(&avctx->internal->thread_ctx);
724 }
725 
726 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
727  FrameThreadContext *fctx, AVCodecContext *avctx,
728  const FFCodec *codec, int first)
729 {
731  int err;
732 
734 
735  copy = av_memdup(avctx, sizeof(*avctx));
736  if (!copy)
737  return AVERROR(ENOMEM);
738  copy->priv_data = NULL;
739 
740  /* From now on, this PerThreadContext will be cleaned up by
741  * ff_frame_thread_free in case of errors. */
742  (*threads_to_free)++;
743 
744  p->parent = fctx;
745  p->avctx = copy;
746 
747  copy->internal = av_mallocz(sizeof(*copy->internal));
748  if (!copy->internal)
749  return AVERROR(ENOMEM);
750  copy->internal->thread_ctx = p;
751 
752  copy->delay = avctx->delay;
753 
754  if (codec->priv_data_size) {
755  copy->priv_data = av_mallocz(codec->priv_data_size);
756  if (!copy->priv_data)
757  return AVERROR(ENOMEM);
758 
759  if (codec->p.priv_class) {
760  *(const AVClass **)copy->priv_data = codec->p.priv_class;
761  err = av_opt_copy(copy->priv_data, avctx->priv_data);
762  if (err < 0)
763  return err;
764  }
765  }
766 
767  err = ff_pthread_init(p, per_thread_offsets);
768  if (err < 0)
769  return err;
770 
771  if (!(p->frame = av_frame_alloc()) ||
772  !(p->avpkt = av_packet_alloc()))
773  return AVERROR(ENOMEM);
774 
775  if (!first)
776  copy->internal->is_copy = 1;
777 
778  copy->internal->last_pkt_props = av_packet_alloc();
779  if (!copy->internal->last_pkt_props)
780  return AVERROR(ENOMEM);
781 
782  if (codec->init) {
783  err = codec->init(copy);
784  if (err < 0) {
787  return err;
788  }
789  }
791 
792  if (first)
793  update_context_from_thread(avctx, copy, 1);
794 
795  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
796 
798  if (err < 0)
799  return err;
801 
802  return 0;
803 }
804 
806 {
807  int thread_count = avctx->thread_count;
808  const FFCodec *codec = ffcodec(avctx->codec);
809  FrameThreadContext *fctx;
810  int err, i = 0;
811 
812  if (!thread_count) {
813  int nb_cpus = av_cpu_count();
814  // use number of cores + 1 as thread count if there is more than one
815  if (nb_cpus > 1)
816  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
817  else
818  thread_count = avctx->thread_count = 1;
819  }
820 
821  if (thread_count <= 1) {
822  avctx->active_thread_type = 0;
823  return 0;
824  }
825 
826  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
827  if (!fctx)
828  return AVERROR(ENOMEM);
829 
830  err = ff_pthread_init(fctx, thread_ctx_offsets);
831  if (err < 0) {
832  ff_pthread_free(fctx, thread_ctx_offsets);
833  av_freep(&avctx->internal->thread_ctx);
834  return err;
835  }
836 
837  fctx->async_lock = 1;
838  fctx->delaying = 1;
839 
840  if (codec->p.type == AVMEDIA_TYPE_VIDEO)
841  avctx->delay = avctx->thread_count - 1;
842 
843  fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
844  if (!fctx->threads) {
845  err = AVERROR(ENOMEM);
846  goto error;
847  }
848 
849  for (; i < thread_count; ) {
850  PerThreadContext *p = &fctx->threads[i];
851  int first = !i;
852 
853  err = init_thread(p, &i, fctx, avctx, codec, first);
854  if (err < 0)
855  goto error;
856  }
857 
858  return 0;
859 
860 error:
861  ff_frame_thread_free(avctx, i);
862  return err;
863 }
864 
866 {
867  int i;
868  FrameThreadContext *fctx = avctx->internal->thread_ctx;
869 
870  if (!fctx) return;
871 
873  if (fctx->prev_thread) {
874  if (fctx->prev_thread != &fctx->threads[0])
876  }
877 
878  fctx->next_decoding = fctx->next_finished = 0;
879  fctx->delaying = 1;
880  fctx->prev_thread = NULL;
881  for (i = 0; i < avctx->thread_count; i++) {
882  PerThreadContext *p = &fctx->threads[i];
883  // Make sure decode flush calls with size=0 won't return old frames
884  p->got_frame = 0;
885  av_frame_unref(p->frame);
886  p->result = 0;
887 
888  if (ffcodec(avctx->codec)->flush)
889  ffcodec(avctx->codec)->flush(p->avctx);
890  }
891 }
892 
894 {
895  PerThreadContext *p = avctx->internal->thread_ctx;
896 
899  return 0;
900  }
901 
902  return 1;
903 }
904 
906 {
907  PerThreadContext *p;
908  int err;
909 
910  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
911  return ff_get_buffer(avctx, f, flags);
912 
913  p = avctx->internal->thread_ctx;
914  if (atomic_load(&p->state) != STATE_SETTING_UP &&
916  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
917  return -1;
918  }
919 
921  err = ff_get_buffer(avctx, f, flags);
922 
924 
925  return err;
926 }
927 
929 {
930  int ret = thread_get_buffer_internal(avctx, f, flags);
931  if (ret < 0)
932  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
933  return ret;
934 }
935 
937 {
938  int ret;
939 
940  f->owner[0] = f->owner[1] = avctx;
941  /* Hint: It is possible for this function to be called with codecs
942  * that don't support frame threading at all, namely in case
943  * a frame-threaded decoder shares code with codecs that are not.
944  * This currently affects non-MPEG-4 mpegvideo codecs and and VP7.
945  * The following check will always be true for them. */
946  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
947  return ff_get_buffer(avctx, f->f, flags);
948 
950  atomic_int *progress;
951  f->progress = av_buffer_alloc(2 * sizeof(*progress));
952  if (!f->progress) {
953  return AVERROR(ENOMEM);
954  }
955  progress = (atomic_int*)f->progress->data;
956 
957  atomic_init(&progress[0], -1);
958  atomic_init(&progress[1], -1);
959  }
960 
961  ret = ff_thread_get_buffer(avctx, f->f, flags);
962  if (ret)
963  av_buffer_unref(&f->progress);
964  return ret;
965 }
966 
968 {
969  if (!f)
970  return;
971 
972  if (avctx->debug & FF_DEBUG_BUFFERS)
973  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
974 
975  av_frame_unref(f);
976 }
977 
979 {
980  av_buffer_unref(&f->progress);
981  f->owner[0] = f->owner[1] = NULL;
982  ff_thread_release_buffer(avctx, f->f);
983 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
hwconfig.h
FFCodec::update_thread_context
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: codec_internal.h:157
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1409
AVCodec
AVCodec.
Definition: codec.h:184
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:82
thread_set_name
static void thread_set_name(PerThreadContext *p)
Definition: pthread_frame.c:161
AVCodecContext::hwaccel_context
void * hwaccel_context
Legacy hardware accelerator context.
Definition: avcodec.h:1433
pthread_join
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
PerThreadContext::input_cond
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:85
atomic_store
#define atomic_store(object, desired)
Definition: stdatomic.h:85
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1006
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:978
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1038
PerThreadContext::debug_threads
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
Definition: pthread_frame.c:107
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:216
FrameThreadContext::next_decoding
int next_decoding
The next context to submit a packet to.
Definition: pthread_frame.c:128
thread.h
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_thread_flush
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread_frame.c:865
init_thread
static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, const FFCodec *codec, int first)
Definition: pthread_frame.c:726
AVHWAccel::caps_internal
int caps_internal
Internal hwaccel capabilities.
Definition: avcodec.h:2225
ff_thread_can_start_frame
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread_frame.c:893
STATE_INPUT_READY
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:53
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
PerThreadContext::state
atomic_int state
Definition: pthread_frame.c:100
FrameThreadContext
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:113
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:101
park_frame_worker_threads
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
Definition: pthread_frame.c:630
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:999
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:203
internal.h
STATE_GET_FORMAT
@ STATE_GET_FORMAT
Set when the codec calls get_format().
Definition: pthread_frame.c:65
version_major.h
atomic_int
intptr_t atomic_int
Definition: stdatomic.h:55
FFCodec
Definition: codec_internal.h:127
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVCodecContext::slice_offset
attribute_deprecated int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:789
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:550
FrameThreadContext::stash_hwaccel_context
void * stash_hwaccel_context
Definition: pthread_frame.c:139
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:581
PerThreadContext::die
int die
Set when the thread should exit.
Definition: pthread_frame.c:102
thread.h
ff_pthread_free
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition: pthread.c:91
FrameThreadContext::next_finished
int next_finished
The next context to return output from.
Definition: pthread_frame.c:129
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:73
AVCodecInternal::pool
AVBufferRef * pool
Definition: internal.h:71
UNINITIALIZED
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
Definition: pthread_frame.c:71
FrameThreadContext::buffer_mutex
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread_frame.c:118
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
AVCodecContext::slice_count
attribute_deprecated int slice_count
slice count
Definition: avcodec.h:781
FFCodec::priv_data_size
int priv_data_size
Definition: codec_internal.h:145
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1762
NEEDS_CLOSE
@ NEEDS_CLOSE
FFCodec->close needs to be called.
Definition: pthread_frame.c:72
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:663
AVHWAccel
Definition: avcodec.h:2088
AVCodecContext::skip_idct
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:1718
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
finish
static void finish(void)
Definition: movenc.c:342
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:435
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2066
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1725
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1506
FrameThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:117
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:506
PerThreadContext
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:79
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:613
submit_packet
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
Definition: pthread_frame.c:401
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:89
AVCodecContext::get_buffer2
int(* get_buffer2)(struct AVCodecContext *s, AVFrame *frame, int flags)
This callback is called at the beginning of each frame to get data buffer(s) for it.
Definition: avcodec.h:1211
first
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But first
Definition: rate_distortion.txt:12
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:992
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:548
FFCodec::update_thread_context_for_user
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
Definition: codec_internal.h:162
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:721
FrameThreadContext::async_lock
int async_lock
Definition: pthread_frame.c:126
PerThreadContext::output_cond
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:87
AVCodecContext::ticks_per_frame
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:557
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:928
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1491
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFCodec::flush
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec_internal.h:246
decode.h
update_context_from_user
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
Definition: pthread_frame.c:345
field
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this field
Definition: writing_filters.txt:78
atomic_load
#define atomic_load(object)
Definition: stdatomic.h:93
pthread_cond_broadcast
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:162
FrameThreadContext::prev_thread
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
Definition: pthread_frame.c:115
FFCodec::decode
int(* decode)(struct AVCodecContext *avctx, struct AVFrame *frame, int *got_frame_ptr, struct AVPacket *avpkt)
Decode to an AVFrame.
Definition: codec_internal.h:193
FFCodec::init
int(* init)(struct AVCodecContext *)
Definition: codec_internal.h:178
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:436
arg
const char * arg
Definition: jacosubdec.c:67
pthread_create
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
if
if(ret)
Definition: filter_design.txt:179
PerThreadContext::got_frame
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:97
threadframe.h
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1013
av_buffer_unref
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
ff_thread_await_progress
void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread_frame.c:571
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:892
thread_get_buffer_internal
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
Definition: pthread_frame.c:905
AVCodec::type
enum AVMediaType type
Definition: codec.h:197
frame_worker_thread
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
Definition: pthread_frame.c:179
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:461
PerThreadContext::progress_mutex
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:90
FrameThreadContext::async_mutex
pthread_mutex_t async_mutex
Definition: pthread_frame.c:124
ff_thread_finish_setup
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...
Definition: pthread_frame.c:592
FrameThreadContext::hwaccel_mutex
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when hwaccel is used.
Definition: pthread_frame.c:123
PerThreadContext::avctx
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:92
pthread_internal.h
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1719
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:444
FF_DEBUG_BUFFERS
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1374
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:430
AVCodecContext::level
int level
level
Definition: avcodec.h:1703
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
FF_DEBUG_THREADS
#define FF_DEBUG_THREADS
Definition: avcodec.h:1375
OFF
#define OFF(member)
Definition: pthread_frame.c:657
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:79
AVCodecInternal::last_pkt_props
AVPacket * last_pkt_props
Properties (timestamps+side data) extracted from the last packet passed for decoding.
Definition: internal.h:90
async_lock
static void async_lock(FrameThreadContext *fctx)
Definition: pthread_frame.c:143
PerThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:84
FFCodec::cb
union FFCodec::@50 cb
PerThreadContext::thread
pthread_t thread
Definition: pthread_frame.c:82
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:206
AVCodecContext::time_base
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:548
PerThreadContext::result
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:98
FrameThreadContext::stash_hwaccel
const AVHWAccel * stash_hwaccel
Definition: pthread_frame.c:138
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:85
f
f
Definition: af_crystalizer.c:122
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:513
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1490
INITIALIZED
@ INITIALIZED
Thread has been properly set up.
Definition: pthread_frame.c:73
AVPacket::size
int size
Definition: packet.h:375
ff_thread_decode_frame
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
Definition: pthread_frame.c:463
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:187
codec_internal.h
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:137
cpu.h
PerThreadContext::avpkt
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:94
async_unlock
static void async_unlock(FrameThreadContext *fctx)
Definition: pthread_frame.c:152
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1054
FrameThreadContext::async_cond
pthread_cond_t async_cond
Definition: pthread_frame.c:125
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:325
frame.h
buffer.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:223
PerThreadContext::parent
struct FrameThreadContext * parent
Definition: pthread_frame.c:80
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:62
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
AVCodecContext::skip_loop_filter
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1711
pthread_t
Definition: os2threads.h:44
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1517
ff_thread_release_buffer
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread_frame.c:967
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1484
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:385
PerThreadContext::thread_init
int thread_init
Definition: pthread_frame.c:83
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:1863
internal.h
common.h
AVCodecContext::hwaccel_flags
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition: avcodec.h:1949
atomic_store_explicit
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
HWACCEL_CAP_ASYNC_SAFE
#define HWACCEL_CAP_ASYNC_SAFE
Definition: hwconfig.h:26
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:511
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:484
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1460
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:191
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1020
pthread_cond_t
Definition: os2threads.h:58
AVCodecContext::height
int height
Definition: avcodec.h:598
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:635
ff_thread_get_ext_buffer
int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around ff_get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:936
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1899
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2077
DEFINE_OFFSET_ARRAY
DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),(OFF(async_cond)))
ret
ret
Definition: filter_design.txt:187
FFSWAP
#define FFSWAP(type, a, b)
Definition: macros.h:52
ff_frame_thread_init
int ff_frame_thread_init(AVCodecContext *avctx)
Definition: pthread_frame.c:805
PerThreadContext::async_serializing
int async_serializing
Definition: pthread_frame.c:105
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:468
pthread_cond_signal
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:152
AVCodecContext::draw_horiz_band
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band.
Definition: avcodec.h:660
AVCodecContext
main external API structure.
Definition: avcodec.h:426
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1525
PerThreadContext::hwaccel_serializing
int hwaccel_serializing
Definition: pthread_frame.c:104
ThreadFrame
Definition: threadframe.h:27
PerThreadContext::frame
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:96
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1569
AVCodecContext::export_side_data
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition: avcodec.h:2016
AV_CODEC_CAP_DELAY
#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:76
FFCodec::close
int(* close)(struct AVCodecContext *)
Definition: codec_internal.h:240
ff_pthread_init
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:104
pthread_cond_wait
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:192
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1362
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:639
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:613
AVMEDIA_TYPE_VIDEO
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
FrameThreadContext::delaying
int delaying
Set for the first N packets, where N is the number of threads.
Definition: pthread_frame.c:131
mem.h
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1885
STATE_SETUP_FINISHED
@ STATE_SETUP_FINISHED
Definition: pthread_frame.c:67
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:453
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecInternal::thread_ctx
void * thread_ctx
Definition: internal.h:73
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:598
AVCodecContext::frame_number
attribute_deprecated int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1080
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
PerThreadContext::mutex
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1769
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
STATE_SETTING_UP
@ STATE_SETTING_UP
Definition: pthread_frame.c:55
snprintf
#define snprintf
Definition: snprintf.h:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1132
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:799
FrameThreadContext::stash_hwaccel_priv
void * stash_hwaccel_priv
Definition: pthread_frame.c:140
mutex
static AVMutex mutex
Definition: log.c:46
PerThreadContext::progress_cond
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:86
STATE_GET_BUFFER
@ STATE_GET_BUFFER
Set when the codec calls get_buffer().
Definition: pthread_frame.c:60
FrameThreadContext::threads
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:114
update_context_from_thread
static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
Update the next thread's AVCodecContext with values from the reference thread's context.
Definition: pthread_frame.c:262
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:75
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:195