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