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 "pthread_internal.h"
36 #include "refstruct.h"
37 #include "thread.h"
38 #include "threadframe.h"
39 #include "version_major.h"
40 
41 #include "libavutil/avassert.h"
42 #include "libavutil/buffer.h"
43 #include "libavutil/common.h"
44 #include "libavutil/cpu.h"
45 #include "libavutil/frame.h"
46 #include "libavutil/internal.h"
47 #include "libavutil/log.h"
48 #include "libavutil/mem.h"
49 #include "libavutil/opt.h"
50 #include "libavutil/thread.h"
51 
52 enum {
53  /// Set when the thread is awaiting a packet.
55  /// Set before the codec has called ff_thread_finish_setup().
57  /// Set after the codec has called ff_thread_finish_setup().
59 };
60 
61 enum {
62  UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
63  NEEDS_CLOSE, ///< FFCodec->close needs to be called
64  INITIALIZED, ///< Thread has been properly set up
65 };
66 
67 typedef struct ThreadFrameProgress {
70 
71 /**
72  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
73  */
74 typedef struct PerThreadContext {
76 
79  unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
80  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
81  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
82  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
83 
84  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
85  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
86 
87  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
88 
89  AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
90 
91  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
92  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
93  int result; ///< The result of the last codec decode/encode() call.
94 
96 
97  int die; ///< Set when the thread should exit.
98 
101 
102  // set to 1 in ff_thread_finish_setup() when a threadsafe hwaccel is used;
103  // cannot check hwaccel caps directly, because
104  // worked threads clear hwaccel state for thread-unsafe hwaccels
105  // after each decode call
107 
108  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
109 
110  /// The following two fields have the same semantics as the DecodeContext field
114 
115 /**
116  * Context stored in the client AVCodecInternal thread_ctx.
117  */
118 typedef struct FrameThreadContext {
119  PerThreadContext *threads; ///< The contexts for each thread.
120  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
121 
122  unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
123  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
124  /**
125  * This lock is used for ensuring threads run in serial when thread-unsafe
126  * hwaccel is used.
127  */
132 
133  int next_decoding; ///< The next context to submit a packet to.
134  int next_finished; ///< The next context to return output from.
135 
136  int delaying; /**<
137  * Set for the first N packets, where N is the number of threads.
138  * While it is set, ff_thread_en/decode_frame won't return any results.
139  */
140 
141  /* hwaccel state for thread-unsafe hwaccels is temporarily stored here in
142  * order to transfer its ownership to the next decoding thread without the
143  * need for extra synchronization */
148 
149 static int hwaccel_serial(const AVCodecContext *avctx)
150 {
151  return avctx->hwaccel && !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
152 }
153 
154 static void async_lock(FrameThreadContext *fctx)
155 {
157  while (fctx->async_lock)
158  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
159  fctx->async_lock = 1;
161 }
162 
164 {
166  av_assert0(fctx->async_lock);
167  fctx->async_lock = 0;
170 }
171 
173 {
174  AVCodecContext *avctx = p->avctx;
175  int idx = p - p->parent->threads;
176  char name[16];
177 
178  snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
179 
181 }
182 
183 /**
184  * Codec worker thread.
185  *
186  * Automatically calls ff_thread_finish_setup() if the codec does
187  * not provide an update_thread_context method, or if the codec returns
188  * before calling it.
189  */
191 {
192  PerThreadContext *p = arg;
193  AVCodecContext *avctx = p->avctx;
194  const FFCodec *codec = ffcodec(avctx->codec);
195 
196  thread_set_name(p);
197 
199  while (1) {
200  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
202 
203  if (p->die) break;
204 
205  if (!codec->update_thread_context)
206  ff_thread_finish_setup(avctx);
207 
208  /* If a decoder supports hwaccel, then it must call ff_get_format().
209  * Since that call must happen before ff_thread_finish_setup(), the
210  * decoder is required to implement update_thread_context() and call
211  * ff_thread_finish_setup() manually. Therefore the above
212  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
213  * cannot be true here. */
215 
216  /* if the previous thread uses thread-unsafe hwaccel then we take the
217  * lock to ensure the threads don't run concurrently */
218  if (hwaccel_serial(avctx)) {
220  p->hwaccel_serializing = 1;
221  }
222 
223  av_frame_unref(p->frame);
224  p->got_frame = 0;
226  p->frame->flags |= p->intra_only_flag;
227  p->result = codec->cb.decode(avctx, p->frame, &p->got_frame, p->avpkt);
228 
229  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0])
230  av_frame_unref(p->frame);
231 
232  if (atomic_load(&p->state) == STATE_SETTING_UP)
233  ff_thread_finish_setup(avctx);
234 
235  if (p->hwaccel_serializing) {
236  /* wipe hwaccel state for thread-unsafe hwaccels to avoid stale
237  * pointers lying around;
238  * the state was transferred to FrameThreadContext in
239  * ff_thread_finish_setup(), so nothing is leaked */
240  avctx->hwaccel = NULL;
241  avctx->hwaccel_context = NULL;
242  avctx->internal->hwaccel_priv_data = NULL;
243 
244  p->hwaccel_serializing = 0;
246  }
247  av_assert0(!avctx->hwaccel ||
249 
250  if (p->async_serializing) {
251  p->async_serializing = 0;
252 
253  async_unlock(p->parent);
254  }
255 
257 
259 
263  }
265 
266  return NULL;
267 }
268 
269 /**
270  * Update the next thread's AVCodecContext with values from the reference thread's context.
271  *
272  * @param dst The destination context.
273  * @param src The source context.
274  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
275  * @return 0 on success, negative error code on failure
276  */
277 static int update_context_from_thread(AVCodecContext *dst, const AVCodecContext *src, int for_user)
278 {
279  const FFCodec *const codec = ffcodec(dst->codec);
280  int err = 0;
281 
282  if (dst != src && (for_user || codec->update_thread_context)) {
283  dst->time_base = src->time_base;
284  dst->framerate = src->framerate;
285  dst->width = src->width;
286  dst->height = src->height;
287  dst->pix_fmt = src->pix_fmt;
288  dst->sw_pix_fmt = src->sw_pix_fmt;
289 
290  dst->coded_width = src->coded_width;
291  dst->coded_height = src->coded_height;
292 
293  dst->has_b_frames = src->has_b_frames;
294  dst->idct_algo = src->idct_algo;
295  dst->properties = src->properties;
296 
297  dst->bits_per_coded_sample = src->bits_per_coded_sample;
298  dst->sample_aspect_ratio = src->sample_aspect_ratio;
299 
300  dst->profile = src->profile;
301  dst->level = src->level;
302 
303  dst->bits_per_raw_sample = src->bits_per_raw_sample;
304 #if FF_API_TICKS_PER_FRAME
306  dst->ticks_per_frame = src->ticks_per_frame;
308 #endif
309  dst->color_primaries = src->color_primaries;
310 
311  dst->color_trc = src->color_trc;
312  dst->colorspace = src->colorspace;
313  dst->color_range = src->color_range;
314  dst->chroma_sample_location = src->chroma_sample_location;
315 
316  dst->sample_rate = src->sample_rate;
317  dst->sample_fmt = src->sample_fmt;
318  err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
319  if (err < 0)
320  return err;
321 
322  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
323  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
325 
326  if (src->hw_frames_ctx) {
327  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
328  if (!dst->hw_frames_ctx)
329  return AVERROR(ENOMEM);
330  }
331  }
332 
333  dst->hwaccel_flags = src->hwaccel_flags;
334 
335  ff_refstruct_replace(&dst->internal->pool, src->internal->pool);
336  }
337 
338  if (for_user) {
340  err = codec->update_thread_context_for_user(dst, src);
341  } else {
342  const PerThreadContext *p_src = src->internal->thread_ctx;
343  PerThreadContext *p_dst = dst->internal->thread_ctx;
344 
345  if (codec->update_thread_context) {
346  err = codec->update_thread_context(dst, src);
347  if (err < 0)
348  return err;
349  }
350 
351  // reset dst hwaccel state if needed
353  (!dst->hwaccel && !dst->internal->hwaccel_priv_data));
354  if (p_dst->hwaccel_threadsafe &&
355  (!p_src->hwaccel_threadsafe || dst->hwaccel != src->hwaccel)) {
356  ff_hwaccel_uninit(dst);
357  p_dst->hwaccel_threadsafe = 0;
358  }
359 
360  // propagate hwaccel state for threadsafe hwaccels
361  if (p_src->hwaccel_threadsafe) {
362  const FFHWAccel *hwaccel = ffhwaccel(src->hwaccel);
363  if (!dst->hwaccel) {
364  if (hwaccel->priv_data_size) {
365  av_assert0(hwaccel->update_thread_context);
366 
368  av_mallocz(hwaccel->priv_data_size);
369  if (!dst->internal->hwaccel_priv_data)
370  return AVERROR(ENOMEM);
371  }
372  dst->hwaccel = src->hwaccel;
373  }
374  av_assert0(dst->hwaccel == src->hwaccel);
375 
376  if (hwaccel->update_thread_context) {
377  err = hwaccel->update_thread_context(dst, src);
378  if (err < 0) {
379  av_log(dst, AV_LOG_ERROR, "Error propagating hwaccel state\n");
380  ff_hwaccel_uninit(dst);
381  return err;
382  }
383  }
384  p_dst->hwaccel_threadsafe = 1;
385  }
386  }
387 
388  return err;
389 }
390 
391 /**
392  * Update the next thread's AVCodecContext with values set by the user.
393  *
394  * @param dst The destination context.
395  * @param src The source context.
396  * @return 0 on success, negative error code on failure
397  */
399 {
400  int err;
401 
402  dst->flags = src->flags;
403 
404  dst->draw_horiz_band= src->draw_horiz_band;
405  dst->get_buffer2 = src->get_buffer2;
406 
407  dst->opaque = src->opaque;
408  dst->debug = src->debug;
409 
410  dst->slice_flags = src->slice_flags;
411  dst->flags2 = src->flags2;
412  dst->export_side_data = src->export_side_data;
413 
414  dst->skip_loop_filter = src->skip_loop_filter;
415  dst->skip_idct = src->skip_idct;
416  dst->skip_frame = src->skip_frame;
417 
418  dst->frame_num = src->frame_num;
419 
421  err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
422  if (err < 0)
423  return err;
424 
425  return 0;
426 }
427 
428 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
429  AVPacket *avpkt)
430 {
431  FrameThreadContext *fctx = p->parent;
432  PerThreadContext *prev_thread = fctx->prev_thread;
433  const AVCodec *codec = p->avctx->codec;
434  int ret;
435 
436  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
437  return 0;
438 
440 
441  ret = update_context_from_user(p->avctx, user_avctx);
442  if (ret) {
444  return ret;
445  }
447  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
448  memory_order_relaxed);
449 
450  if (prev_thread) {
451  int err;
452  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
453  pthread_mutex_lock(&prev_thread->progress_mutex);
454  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
455  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
456  pthread_mutex_unlock(&prev_thread->progress_mutex);
457  }
458 
459  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
460  if (err) {
462  return err;
463  }
464  }
465 
466  /* transfer the stashed hwaccel state, if any */
468  if (!p->hwaccel_threadsafe) {
469  FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
472  }
473 
475  ret = av_packet_ref(p->avpkt, avpkt);
476  if (ret < 0) {
478  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
479  return ret;
480  }
481 
485 
486  fctx->prev_thread = p;
487  fctx->next_decoding++;
488 
489  return 0;
490 }
491 
493  AVFrame *picture, int *got_picture_ptr,
494  AVPacket *avpkt)
495 {
496  FrameThreadContext *fctx = avctx->internal->thread_ctx;
497  int finished = fctx->next_finished;
498  PerThreadContext *p;
499  int err;
500 
501  /* release the async lock, permitting blocked hwaccel threads to
502  * go forward while we are in this function */
503  async_unlock(fctx);
504 
505  /*
506  * Submit a packet to the next decoding thread.
507  */
508 
509  p = &fctx->threads[fctx->next_decoding];
510  err = submit_packet(p, avctx, avpkt);
511  if (err)
512  goto finish;
513 
514  /*
515  * If we're still receiving the initial packets, don't return a frame.
516  */
517 
518  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
519  fctx->delaying = 0;
520 
521  if (fctx->delaying) {
522  *got_picture_ptr=0;
523  if (avpkt->size) {
524  err = avpkt->size;
525  goto finish;
526  }
527  }
528 
529  /*
530  * Return the next available frame from the oldest thread.
531  * If we're at the end of the stream, then we have to skip threads that
532  * didn't output a frame/error, because we don't want to accidentally signal
533  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
534  */
535 
536  do {
537  p = &fctx->threads[finished++];
538 
539  if (atomic_load(&p->state) != STATE_INPUT_READY) {
541  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
544  }
545 
546  av_frame_move_ref(picture, p->frame);
547  *got_picture_ptr = p->got_frame;
548  picture->pkt_dts = p->avpkt->dts;
549  err = p->result;
550 
551  /*
552  * A later call with avkpt->size == 0 may loop over all threads,
553  * including this one, searching for a frame/error to return before being
554  * stopped by the "finished != fctx->next_finished" condition.
555  * Make sure we don't mistakenly return the same frame/error again.
556  */
557  p->got_frame = 0;
558  p->result = 0;
559 
560  if (finished >= avctx->thread_count) finished = 0;
561  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
562 
563  update_context_from_thread(avctx, p->avctx, 1);
564 
565  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
566 
567  fctx->next_finished = finished;
568 
569  /* return the size of the consumed packet if no error occurred */
570  if (err >= 0)
571  err = avpkt->size;
572 finish:
573  async_lock(fctx);
574  return err;
575 }
576 
578 {
579  PerThreadContext *p;
580  atomic_int *progress = f->progress ? f->progress->progress : NULL;
581 
582  if (!progress ||
583  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
584  return;
585 
586  p = f->owner[field]->internal->thread_ctx;
587 
588  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
589  av_log(f->owner[field], AV_LOG_DEBUG,
590  "%p finished %d field %d\n", progress, n, field);
591 
593 
594  atomic_store_explicit(&progress[field], n, memory_order_release);
595 
598 }
599 
600 void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
601 {
602  PerThreadContext *p;
603  atomic_int *progress = f->progress ? f->progress->progress : NULL;
604 
605  if (!progress ||
606  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
607  return;
608 
609  p = f->owner[field]->internal->thread_ctx;
610 
611  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
612  av_log(f->owner[field], AV_LOG_DEBUG,
613  "thread awaiting %d field %d from %p\n", n, field, progress);
614 
616  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
619 }
620 
622  PerThreadContext *p;
623 
624  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
625 
626  p = avctx->internal->thread_ctx;
627 
628  p->hwaccel_threadsafe = avctx->hwaccel &&
630 
631  if (hwaccel_serial(avctx) && !p->hwaccel_serializing) {
633  p->hwaccel_serializing = 1;
634  }
635 
636  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
637  if (avctx->hwaccel &&
639  p->async_serializing = 1;
640 
641  async_lock(p->parent);
642  }
643 
644  /* thread-unsafe hwaccels share a single private data instance, so we
645  * save hwaccel state for passing to the next thread;
646  * this is done here so that this worker thread can wipe its own hwaccel
647  * state after decoding, without requiring synchronization */
649  if (hwaccel_serial(avctx)) {
650  p->parent->stash_hwaccel = avctx->hwaccel;
653  }
654 
657  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
658  }
659 
661 
664 }
665 
666 /// Waits for all threads to finish.
667 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
668 {
669  int i;
670 
671  async_unlock(fctx);
672 
673  for (i = 0; i < thread_count; i++) {
674  PerThreadContext *p = &fctx->threads[i];
675 
676  if (atomic_load(&p->state) != STATE_INPUT_READY) {
678  while (atomic_load(&p->state) != STATE_INPUT_READY)
681  }
682  p->got_frame = 0;
683  }
684 
685  async_lock(fctx);
686 }
687 
688 #define OFF(member) offsetof(FrameThreadContext, member)
689 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
690  (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
691  (OFF(async_cond)));
692 #undef OFF
693 
694 #define OFF(member) offsetof(PerThreadContext, member)
695 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
696  (OFF(progress_mutex), OFF(mutex)),
697  (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
698 #undef OFF
699 
700 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
701 {
702  FrameThreadContext *fctx = avctx->internal->thread_ctx;
703  const FFCodec *codec = ffcodec(avctx->codec);
704  int i;
705 
706  park_frame_worker_threads(fctx, thread_count);
707 
708  for (i = 0; i < thread_count; i++) {
709  PerThreadContext *p = &fctx->threads[i];
710  AVCodecContext *ctx = p->avctx;
711 
712  if (ctx->internal) {
713  if (p->thread_init == INITIALIZED) {
715  p->die = 1;
718 
719  pthread_join(p->thread, NULL);
720  }
721  if (codec->close && p->thread_init != UNINITIALIZED)
722  codec->close(ctx);
723 
724  /* When using a threadsafe hwaccel, this is where
725  * each thread's context is uninit'd and freed. */
727 
728  if (ctx->priv_data) {
729  if (codec->p.priv_class)
732  }
733 
734  ff_refstruct_unref(&ctx->internal->pool);
735  av_packet_free(&ctx->internal->last_pkt_props);
736  av_freep(&ctx->internal);
737  av_buffer_unref(&ctx->hw_frames_ctx);
738  av_frame_side_data_free(&ctx->decoded_side_data,
739  &ctx->nb_decoded_side_data);
740  }
741 
742  av_frame_free(&p->frame);
743 
744  ff_pthread_free(p, per_thread_offsets);
745  av_packet_free(&p->avpkt);
746 
747  av_freep(&p->avctx);
748  }
749 
750  av_freep(&fctx->threads);
751  ff_pthread_free(fctx, thread_ctx_offsets);
752 
753  /* if we have stashed hwaccel state, move it to the user-facing context,
754  * so it will be freed in ff_codec_close() */
755  av_assert0(!avctx->hwaccel);
756  FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
757  FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
758  FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
759 
760  av_freep(&avctx->internal->thread_ctx);
761 }
762 
763 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
764  FrameThreadContext *fctx, AVCodecContext *avctx,
765  const FFCodec *codec, int first)
766 {
768  int err;
769 
773  if (avctx->codec_type == AVMEDIA_TYPE_VIDEO)
775  }
776 
778 
779  copy = av_memdup(avctx, sizeof(*avctx));
780  if (!copy)
781  return AVERROR(ENOMEM);
782  copy->priv_data = NULL;
783  copy->decoded_side_data = NULL;
784  copy->nb_decoded_side_data = 0;
785 
786  /* From now on, this PerThreadContext will be cleaned up by
787  * ff_frame_thread_free in case of errors. */
788  (*threads_to_free)++;
789 
790  p->parent = fctx;
791  p->avctx = copy;
792 
793  copy->internal = ff_decode_internal_alloc();
794  if (!copy->internal)
795  return AVERROR(ENOMEM);
796  copy->internal->thread_ctx = p;
797  copy->internal->progress_frame_pool = avctx->internal->progress_frame_pool;
798 
799  copy->delay = avctx->delay;
800 
801  if (codec->priv_data_size) {
802  copy->priv_data = av_mallocz(codec->priv_data_size);
803  if (!copy->priv_data)
804  return AVERROR(ENOMEM);
805 
806  if (codec->p.priv_class) {
807  *(const AVClass **)copy->priv_data = codec->p.priv_class;
808  err = av_opt_copy(copy->priv_data, avctx->priv_data);
809  if (err < 0)
810  return err;
811  }
812  }
813 
814  err = ff_pthread_init(p, per_thread_offsets);
815  if (err < 0)
816  return err;
817 
818  if (!(p->frame = av_frame_alloc()) ||
819  !(p->avpkt = av_packet_alloc()))
820  return AVERROR(ENOMEM);
821 
822  if (!first)
823  copy->internal->is_copy = 1;
824 
825  copy->internal->last_pkt_props = av_packet_alloc();
826  if (!copy->internal->last_pkt_props)
827  return AVERROR(ENOMEM);
828 
829  if (codec->init) {
830  err = codec->init(copy);
831  if (err < 0) {
834  return err;
835  }
836  }
838 
839  if (first) {
840  update_context_from_thread(avctx, copy, 1);
841 
843  for (int i = 0; i < copy->nb_decoded_side_data; i++) {
845  &avctx->nb_decoded_side_data,
846  copy->decoded_side_data[i], 0);
847  if (err < 0)
848  return err;
849  }
850  }
851 
852  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
853 
855  if (err < 0)
856  return err;
858 
859  return 0;
860 }
861 
863 {
864  int thread_count = avctx->thread_count;
865  const FFCodec *codec = ffcodec(avctx->codec);
866  FrameThreadContext *fctx;
867  int err, i = 0;
868 
869  if (!thread_count) {
870  int nb_cpus = av_cpu_count();
871  // use number of cores + 1 as thread count if there is more than one
872  if (nb_cpus > 1)
873  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
874  else
875  thread_count = avctx->thread_count = 1;
876  }
877 
878  if (thread_count <= 1) {
879  avctx->active_thread_type = 0;
880  return 0;
881  }
882 
883  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
884  if (!fctx)
885  return AVERROR(ENOMEM);
886 
887  err = ff_pthread_init(fctx, thread_ctx_offsets);
888  if (err < 0) {
889  ff_pthread_free(fctx, thread_ctx_offsets);
890  av_freep(&avctx->internal->thread_ctx);
891  return err;
892  }
893 
894  fctx->async_lock = 1;
895  fctx->delaying = 1;
896 
897  if (codec->p.type == AVMEDIA_TYPE_VIDEO)
898  avctx->delay = avctx->thread_count - 1;
899 
900  fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
901  if (!fctx->threads) {
902  err = AVERROR(ENOMEM);
903  goto error;
904  }
905 
906  for (; i < thread_count; ) {
907  PerThreadContext *p = &fctx->threads[i];
908  int first = !i;
909 
910  err = init_thread(p, &i, fctx, avctx, codec, first);
911  if (err < 0)
912  goto error;
913  }
914 
915  return 0;
916 
917 error:
918  ff_frame_thread_free(avctx, i);
919  return err;
920 }
921 
923 {
924  int i;
925  FrameThreadContext *fctx = avctx->internal->thread_ctx;
926 
927  if (!fctx) return;
928 
930  if (fctx->prev_thread) {
931  if (fctx->prev_thread != &fctx->threads[0])
933  }
934 
935  fctx->next_decoding = fctx->next_finished = 0;
936  fctx->delaying = 1;
937  fctx->prev_thread = NULL;
938  for (i = 0; i < avctx->thread_count; i++) {
939  PerThreadContext *p = &fctx->threads[i];
940  // Make sure decode flush calls with size=0 won't return old frames
941  p->got_frame = 0;
942  av_frame_unref(p->frame);
943  p->result = 0;
944 
945  if (ffcodec(avctx->codec)->flush)
946  ffcodec(avctx->codec)->flush(p->avctx);
947  }
948 }
949 
951 {
952  if ((avctx->active_thread_type & FF_THREAD_FRAME) &&
954  PerThreadContext *p = avctx->internal->thread_ctx;
955 
956  if (atomic_load(&p->state) != STATE_SETTING_UP)
957  return 0;
958  }
959 
960  return 1;
961 }
962 
964 {
965  PerThreadContext *p;
966  int err;
967 
968  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
969  return ff_get_buffer(avctx, f, flags);
970 
971  p = avctx->internal->thread_ctx;
972  if (atomic_load(&p->state) != STATE_SETTING_UP &&
974  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
975  return -1;
976  }
977 
979  err = ff_get_buffer(avctx, f, flags);
980 
982 
983  return err;
984 }
985 
987 {
988  int ret = thread_get_buffer_internal(avctx, f, flags);
989  if (ret < 0)
990  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
991  return ret;
992 }
993 
995 {
996  int ret;
997 
998  f->owner[0] = f->owner[1] = avctx;
999  /* Hint: It is possible for this function to be called with codecs
1000  * that don't support frame threading at all, namely in case
1001  * a frame-threaded decoder shares code with codecs that are not.
1002  * This currently affects non-MPEG-4 mpegvideo codecs.
1003  * The following check will always be true for them. */
1004  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1005  return ff_get_buffer(avctx, f->f, flags);
1006 
1007  f->progress = ff_refstruct_allocz(sizeof(*f->progress));
1008  if (!f->progress)
1009  return AVERROR(ENOMEM);
1010 
1011  atomic_init(&f->progress->progress[0], -1);
1012  atomic_init(&f->progress->progress[1], -1);
1013 
1014  ret = ff_thread_get_buffer(avctx, f->f, flags);
1015  if (ret)
1016  ff_refstruct_unref(&f->progress);
1017  return ret;
1018 }
1019 
1021 {
1022  ff_refstruct_unref(&f->progress);
1023  f->owner[0] = f->owner[1] = NULL;
1024  if (f->f)
1025  av_frame_unref(f->f);
1026 }
1027 
1029 {
1030  PerThreadContext *p;
1031  const void *ref;
1032 
1033  if (!avctx->internal->is_copy)
1034  return avctx->active_thread_type & FF_THREAD_FRAME ?
1036 
1037  p = avctx->internal->thread_ctx;
1038 
1039  av_assert1(memcpy(&ref, (char*)avctx->priv_data + offset, sizeof(ref)) && ref == NULL);
1040 
1041  memcpy(&ref, (const char*)p->parent->threads[0].avctx->priv_data + offset, sizeof(ref));
1042  av_assert1(ref);
1043  ff_refstruct_replace((char*)avctx->priv_data + offset, ref);
1044 
1045  return FF_THREAD_IS_COPY;
1046 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:32
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:156
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: packet.c:427
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1427
AVCodec
AVCodec.
Definition: codec.h:187
UNINITIALIZED
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
Definition: pthread_frame.c:62
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:172
AVCodecContext::hwaccel_context
void * hwaccel_context
Legacy hardware accelerator context.
Definition: avcodec.h:1451
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:80
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
PerThreadContext::initial_pict_type
enum AVPictureType initial_pict_type
Definition: pthread_frame.c:112
hwaccel_serial
static int hwaccel_serial(const AVCodecContext *avctx)
Definition: pthread_frame.c:149
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:685
AVCodecContext::decoded_side_data
AVFrameSideData ** decoded_side_data
Array containing static side data, such as HDR10 CLL / MDCV structures.
Definition: avcodec.h:2077
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1050
PerThreadContext::debug_threads
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
Definition: pthread_frame.c:108
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:212
FrameThreadContext::next_decoding
int next_decoding
The next context to submit a packet to.
Definition: pthread_frame.c:133
thread.h
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ThreadFrameProgress
Definition: pthread_frame.c:67
ff_thread_flush
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread_frame.c:922
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:763
ff_thread_can_start_frame
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread_frame.c:950
AVPictureType
AVPictureType
Definition: avutil.h:277
AVCodecContext::codec_descriptor
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition: avcodec.h:1862
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
PerThreadContext::state
atomic_int state
Definition: pthread_frame.c:95
FF_THREAD_IS_FIRST_THREAD
@ FF_THREAD_IS_FIRST_THREAD
Definition: thread.h:89
FrameThreadContext
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:118
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:160
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:667
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:374
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:678
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
internal.h
ff_thread_sync_ref
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:1028
STATE_SETUP_FINISHED
@ STATE_SETUP_FINISHED
Set after the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:58
version_major.h
atomic_int
intptr_t atomic_int
Definition: stdatomic.h:55
FFCodec
Definition: codec_internal.h:126
AVFrame::flags
int flags
Frame flags, a combination of AV_FRAME_FLAGS.
Definition: frame.h:646
INITIALIZED
@ INITIALIZED
Thread has been properly set up.
Definition: pthread_frame.c:64
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: frame.c:872
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:587
FrameThreadContext::stash_hwaccel_context
void * stash_hwaccel_context
Definition: pthread_frame.c:145
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:601
PerThreadContext::die
int die
Set when the thread should exit.
Definition: pthread_frame.c:97
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:134
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:74
FrameThreadContext::buffer_mutex
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread_frame.c:123
ff_hwaccel_uninit
void ff_hwaccel_uninit(AVCodecContext *avctx)
Definition: decode.c:1229
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:144
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:560
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
AVCodecInternal::progress_frame_pool
struct FFRefStructPool * progress_frame_pool
Definition: internal.h:64
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:700
AVHWAccel
Definition: avcodec.h:2089
AVCodecContext::skip_idct
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:1813
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:130
finish
static void finish(void)
Definition: movenc.c:373
FFHWAccel
Definition: hwaccel_internal.h:34
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:454
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:1065
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1820
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1583
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:398
FrameThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:122
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1925
HWACCEL_CAP_THREAD_SAFE
#define HWACCEL_CAP_THREAD_SAFE
Definition: hwaccel_internal.h:32
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:502
PerThreadContext
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:74
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:633
submit_packet
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
Definition: pthread_frame.c:428
refstruct.h
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:148
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:1222
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:671
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
AV_FRAME_FLAG_KEY
#define AV_FRAME_FLAG_KEY
A flag to mark frames that are keyframes.
Definition: frame.h:625
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:577
NEEDS_CLOSE
@ NEEDS_CLOSE
FFCodec->close needs to be called.
Definition: pthread_frame.c:63
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:161
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:723
FrameThreadContext::async_lock
int async_lock
Definition: pthread_frame.c:131
AVCodecInternal::pool
struct FramePool * pool
Definition: internal.h:62
AVCodecContext::nb_decoded_side_data
int nb_decoded_side_data
Definition: avcodec.h:2078
PerThreadContext::output_cond
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:82
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:986
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1575
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:49
FFCodec::flush
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec_internal.h:245
decode.h
PerThreadContext::hwaccel_threadsafe
int hwaccel_threadsafe
Definition: pthread_frame.c:106
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:120
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:192
FFCodec::init
int(* init)(struct AVCodecContext *)
Definition: codec_internal.h:177
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:455
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
AVCodecDescriptor::props
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: codec_desc.h:54
PerThreadContext::got_frame
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:92
threadframe.h
AV_CODEC_PROP_INTRA_ONLY
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition: codec_desc.h:72
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:66
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:695
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:600
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:730
thread_get_buffer_internal
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
Definition: pthread_frame.c:963
AVCodec::type
enum AVMediaType type
Definition: codec.h:200
frame_worker_thread
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
Definition: pthread_frame.c:190
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:480
PerThreadContext::progress_mutex
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:85
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:279
FrameThreadContext::async_mutex
pthread_mutex_t async_mutex
Definition: pthread_frame.c:129
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:621
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:1020
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:128
PerThreadContext::avctx
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:87
ff_refstruct_allocz
static void * ff_refstruct_allocz(size_t size)
Equivalent to ff_refstruct_alloc_ext(size, 0, NULL, NULL)
Definition: refstruct.h:105
pthread_internal.h
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:493
FF_THREAD_IS_COPY
@ FF_THREAD_IS_COPY
Definition: thread.h:88
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: packet.c:435
AVCodecContext::level
int level
Encoding level descriptor.
Definition: avcodec.h:1784
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
FF_DEBUG_THREADS
#define FF_DEBUG_THREADS
Definition: avcodec.h:1409
OFF
#define OFF(member)
Definition: pthread_frame.c:694
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:82
AVCodecInternal::last_pkt_props
AVPacket * last_pkt_props
Properties (timestamps+side data) extracted from the last packet passed for decoding.
Definition: internal.h:83
async_lock
static void async_lock(FrameThreadContext *fctx)
Definition: pthread_frame.c:154
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:2125
PerThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:79
PerThreadContext::thread
pthread_t thread
Definition: pthread_frame.c:77
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:211
attribute_align_arg
#define attribute_align_arg
Definition: internal.h:50
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:544
PerThreadContext::result
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:93
FrameThreadContext::stash_hwaccel
const AVHWAccel * stash_hwaccel
Definition: pthread_frame.c:144
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:85
f
f
Definition: af_crystalizer.c:121
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:509
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
AVFrame::pict_type
enum AVPictureType pict_type
Picture type of the frame.
Definition: frame.h:476
AVPacket::size
int size
Definition: packet.h:525
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:492
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:123
cpu.h
PerThreadContext::intra_only_flag
int intra_only_flag
The following two fields have the same semantics as the DecodeContext field.
Definition: pthread_frame.c:111
PerThreadContext::avpkt
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:89
async_unlock
static void async_unlock(FrameThreadContext *fctx)
Definition: pthread_frame.c:163
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
FrameThreadContext::async_cond
pthread_cond_t async_cond
Definition: pthread_frame.c:130
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:305
AV_PICTURE_TYPE_NONE
@ AV_PICTURE_TYPE_NONE
Undefined.
Definition: avutil.h:278
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:523
PerThreadContext::parent
struct FrameThreadContext * parent
Definition: pthread_frame.c:75
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
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: packet.c:63
AVCodecContext::skip_loop_filter
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1806
FF_THREAD_NO_FRAME_THREADING
@ FF_THREAD_NO_FRAME_THREADING
Definition: thread.h:90
pthread_t
Definition: os2threads.h:44
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1594
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1568
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:135
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:390
PerThreadContext::thread_init
int thread_init
Definition: pthread_frame.c:78
FFCodec::cb
union FFCodec::@79 cb
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:1796
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: frame.c:111
internal.h
STATE_SETTING_UP
@ STATE_SETTING_UP
Set before the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:56
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:1506
av_assert1
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
Definition: avassert.h:56
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:633
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:606
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
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1548
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:702
pthread_cond_t
Definition: os2threads.h:58
AVCodecContext::height
int height
Definition: avcodec.h:618
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:657
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:994
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:264
STATE_INPUT_READY
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:54
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:1475
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2031
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:862
ff_decode_internal_alloc
struct AVCodecInternal * ff_decode_internal_alloc(void)
Definition: decode.c:2157
PerThreadContext::async_serializing
int async_serializing
Definition: pthread_frame.c:100
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:487
hwaccel
static const char * hwaccel
Definition: ffplay.c:353
ff_refstruct_replace
void ff_refstruct_replace(void *dstp, const void *src)
Ensure *dstp refers to the same object as src.
Definition: refstruct.c:160
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:758
AVCodecContext
main external API structure.
Definition: avcodec.h:445
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1602
PerThreadContext::hwaccel_serializing
int hwaccel_serializing
Definition: pthread_frame.c:99
ThreadFrame
Definition: threadframe.h:27
avcodec_internal.h
PerThreadContext::frame
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:91
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1640
ffhwaccel
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
Definition: hwaccel_internal.h:166
ref
static int ref[MAX_W *MAX_W]
Definition: jpeg2000dwt.c:112
AVCodecContext::ticks_per_frame
attribute_deprecated int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:576
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:1927
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:239
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:1396
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:440
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:72
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:633
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:453
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:136
mem.h
ThreadingStatus
ThreadingStatus
Definition: thread.h:87
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AVPacket
This structure stores compressed data.
Definition: packet.h:501
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:66
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:618
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
PerThreadContext::mutex
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:84
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
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:277
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:664
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:117
snprintf
#define snprintf
Definition: snprintf.h:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1283
ff_refstruct_unref
void ff_refstruct_unref(void *objp)
Decrement the reference count of the underlying object and automatically free the object if there are...
Definition: refstruct.c:120
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:642
ThreadFrameProgress::progress
atomic_int progress[2]
Definition: pthread_frame.c:68
FrameThreadContext::stash_hwaccel_priv
void * stash_hwaccel_priv
Definition: pthread_frame.c:146
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:81
FrameThreadContext::threads
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:119
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:78
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:216