FFmpeg
pthread_frame.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Frame multithreading support functions
22  * @see doc/multithreading.txt
23  */
24 
25 #include "config.h"
26 
27 #include <stdatomic.h>
28 #include <stdint.h>
29 
30 #include "avcodec.h"
31 #include "avcodec_internal.h"
32 #include "codec_internal.h"
33 #include "decode.h"
34 #include "hwaccel_internal.h"
35 #include "hwconfig.h"
36 #include "internal.h"
37 #include "pthread_internal.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 /**
69  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
70  */
71 typedef struct PerThreadContext {
73 
76  unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
77  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
78  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
79  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
80 
81  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
82  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
83 
84  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
85 
86  AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
87 
88  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
89  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
90  int result; ///< The result of the last codec decode/encode() call.
91 
93 
94  int die; ///< Set when the thread should exit.
95 
98 
99  // set to 1 in ff_thread_finish_setup() when a threadsafe hwaccel is used;
100  // cannot check hwaccel caps directly, because
101  // worked threads clear hwaccel state for thread-unsafe hwaccels
102  // after each decode call
104 
105  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
107 
108 /**
109  * Context stored in the client AVCodecInternal thread_ctx.
110  */
111 typedef struct FrameThreadContext {
112  PerThreadContext *threads; ///< The contexts for each thread.
113  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
114 
115  unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
116  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
117  /**
118  * This lock is used for ensuring threads run in serial when thread-unsafe
119  * hwaccel is used.
120  */
125 
126  int next_decoding; ///< The next context to submit a packet to.
127  int next_finished; ///< The next context to return output from.
128 
129  int delaying; /**<
130  * Set for the first N packets, where N is the number of threads.
131  * While it is set, ff_thread_en/decode_frame won't return any results.
132  */
133 
134  /* hwaccel state for thread-unsafe hwaccels is temporarily stored here in
135  * order to transfer its ownership to the next decoding thread without the
136  * need for extra synchronization */
141 
142 static int hwaccel_serial(const AVCodecContext *avctx)
143 {
144  return avctx->hwaccel && !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
145 }
146 
147 static void async_lock(FrameThreadContext *fctx)
148 {
150  while (fctx->async_lock)
151  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
152  fctx->async_lock = 1;
154 }
155 
157 {
159  av_assert0(fctx->async_lock);
160  fctx->async_lock = 0;
163 }
164 
166 {
167  AVCodecContext *avctx = p->avctx;
168  int idx = p - p->parent->threads;
169  char name[16];
170 
171  snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
172 
174 }
175 
176 /**
177  * Codec worker thread.
178  *
179  * Automatically calls ff_thread_finish_setup() if the codec does
180  * not provide an update_thread_context method, or if the codec returns
181  * before calling it.
182  */
184 {
185  PerThreadContext *p = arg;
186  AVCodecContext *avctx = p->avctx;
187  const FFCodec *codec = ffcodec(avctx->codec);
188 
189  thread_set_name(p);
190 
192  while (1) {
193  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
195 
196  if (p->die) break;
197 
198  if (!codec->update_thread_context)
199  ff_thread_finish_setup(avctx);
200 
201  /* If a decoder supports hwaccel, then it must call ff_get_format().
202  * Since that call must happen before ff_thread_finish_setup(), the
203  * decoder is required to implement update_thread_context() and call
204  * ff_thread_finish_setup() manually. Therefore the above
205  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
206  * cannot be true here. */
208 
209  /* if the previous thread uses thread-unsafe hwaccel then we take the
210  * lock to ensure the threads don't run concurrently */
211  if (hwaccel_serial(avctx)) {
213  p->hwaccel_serializing = 1;
214  }
215 
216  av_frame_unref(p->frame);
217  p->got_frame = 0;
218  p->result = codec->cb.decode(avctx, p->frame, &p->got_frame, p->avpkt);
219 
220  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0])
221  ff_thread_release_buffer(avctx, p->frame);
222 
223  if (atomic_load(&p->state) == STATE_SETTING_UP)
224  ff_thread_finish_setup(avctx);
225 
226  if (p->hwaccel_serializing) {
227  /* wipe hwaccel state for thread-unsafe hwaccels to avoid stale
228  * pointers lying around;
229  * the state was transferred to FrameThreadContext in
230  * ff_thread_finish_setup(), so nothing is leaked */
231  avctx->hwaccel = NULL;
232  avctx->hwaccel_context = NULL;
233  avctx->internal->hwaccel_priv_data = NULL;
234 
235  p->hwaccel_serializing = 0;
237  }
238  av_assert0(!avctx->hwaccel ||
240 
241  if (p->async_serializing) {
242  p->async_serializing = 0;
243 
244  async_unlock(p->parent);
245  }
246 
248 
250 
254  }
256 
257  return NULL;
258 }
259 
260 /**
261  * Update the next thread's AVCodecContext with values from the reference thread's context.
262  *
263  * @param dst The destination context.
264  * @param src The source context.
265  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
266  * @return 0 on success, negative error code on failure
267  */
268 static int update_context_from_thread(AVCodecContext *dst, const AVCodecContext *src, int for_user)
269 {
270  const FFCodec *const codec = ffcodec(dst->codec);
271  int err = 0;
272 
273  if (dst != src && (for_user || codec->update_thread_context)) {
274  dst->time_base = src->time_base;
275  dst->framerate = src->framerate;
276  dst->width = src->width;
277  dst->height = src->height;
278  dst->pix_fmt = src->pix_fmt;
279  dst->sw_pix_fmt = src->sw_pix_fmt;
280 
281  dst->coded_width = src->coded_width;
282  dst->coded_height = src->coded_height;
283 
284  dst->has_b_frames = src->has_b_frames;
285  dst->idct_algo = src->idct_algo;
286  dst->properties = src->properties;
287 
288  dst->bits_per_coded_sample = src->bits_per_coded_sample;
289  dst->sample_aspect_ratio = src->sample_aspect_ratio;
290 
291  dst->profile = src->profile;
292  dst->level = src->level;
293 
294  dst->bits_per_raw_sample = src->bits_per_raw_sample;
295 #if FF_API_TICKS_PER_FRAME
297  dst->ticks_per_frame = src->ticks_per_frame;
299 #endif
300  dst->color_primaries = src->color_primaries;
301 
302  dst->color_trc = src->color_trc;
303  dst->colorspace = src->colorspace;
304  dst->color_range = src->color_range;
305  dst->chroma_sample_location = src->chroma_sample_location;
306 
307  dst->sample_rate = src->sample_rate;
308  dst->sample_fmt = src->sample_fmt;
309 #if FF_API_OLD_CHANNEL_LAYOUT
311  dst->channels = src->channels;
312  dst->channel_layout = src->channel_layout;
314 #endif
315  err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
316  if (err < 0)
317  return err;
318 
319  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
320  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
322 
323  if (src->hw_frames_ctx) {
324  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
325  if (!dst->hw_frames_ctx)
326  return AVERROR(ENOMEM);
327  }
328  }
329 
330  dst->hwaccel_flags = src->hwaccel_flags;
331 
332  err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
333  if (err < 0)
334  return err;
335  }
336 
337  if (for_user) {
339  err = codec->update_thread_context_for_user(dst, src);
340  } else {
341  const PerThreadContext *p_src = src->internal->thread_ctx;
342  PerThreadContext *p_dst = dst->internal->thread_ctx;
343 
344  if (codec->update_thread_context) {
345  err = codec->update_thread_context(dst, src);
346  if (err < 0)
347  return err;
348  }
349 
350  // reset dst hwaccel state if needed
352  (!dst->hwaccel && !dst->internal->hwaccel_priv_data));
353  if (p_dst->hwaccel_threadsafe &&
354  (!p_src->hwaccel_threadsafe || dst->hwaccel != src->hwaccel)) {
355  ff_hwaccel_uninit(dst);
356  p_dst->hwaccel_threadsafe = 0;
357  }
358 
359  // propagate hwaccel state for threadsafe hwaccels
360  if (p_src->hwaccel_threadsafe) {
361  const FFHWAccel *hwaccel = ffhwaccel(src->hwaccel);
362  if (!dst->hwaccel) {
363  if (hwaccel->priv_data_size) {
365 
367  av_mallocz(hwaccel->priv_data_size);
368  if (!dst->internal->hwaccel_priv_data)
369  return AVERROR(ENOMEM);
370  }
371  dst->hwaccel = src->hwaccel;
372  }
373  av_assert0(dst->hwaccel == src->hwaccel);
374 
375  if (hwaccel->update_thread_context) {
376  err = hwaccel->update_thread_context(dst, src);
377  if (err < 0) {
378  av_log(dst, AV_LOG_ERROR, "Error propagating hwaccel state\n");
379  ff_hwaccel_uninit(dst);
380  return err;
381  }
382  }
383  p_dst->hwaccel_threadsafe = 1;
384  }
385  }
386 
387  return err;
388 }
389 
390 /**
391  * Update the next thread's AVCodecContext with values set by the user.
392  *
393  * @param dst The destination context.
394  * @param src The source context.
395  * @return 0 on success, negative error code on failure
396  */
398 {
399  int err;
400 
401  dst->flags = src->flags;
402 
403  dst->draw_horiz_band= src->draw_horiz_band;
404  dst->get_buffer2 = src->get_buffer2;
405 
406  dst->opaque = src->opaque;
407  dst->debug = src->debug;
408 
409  dst->slice_flags = src->slice_flags;
410  dst->flags2 = src->flags2;
411  dst->export_side_data = src->export_side_data;
412 
413  dst->skip_loop_filter = src->skip_loop_filter;
414  dst->skip_idct = src->skip_idct;
415  dst->skip_frame = src->skip_frame;
416 
417  dst->frame_num = src->frame_num;
418 #if FF_API_AVCTX_FRAME_NUMBER
420  dst->frame_number = src->frame_number;
422 #endif
423 #if FF_API_REORDERED_OPAQUE
425  dst->reordered_opaque = src->reordered_opaque;
427 #endif
428 
430  err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
431  if (err < 0)
432  return err;
433 
434  return 0;
435 }
436 
437 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
438  AVPacket *avpkt)
439 {
440  FrameThreadContext *fctx = p->parent;
441  PerThreadContext *prev_thread = fctx->prev_thread;
442  const AVCodec *codec = p->avctx->codec;
443  int ret;
444 
445  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
446  return 0;
447 
449 
450  ret = update_context_from_user(p->avctx, user_avctx);
451  if (ret) {
453  return ret;
454  }
456  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
457  memory_order_relaxed);
458 
459  if (prev_thread) {
460  int err;
461  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
462  pthread_mutex_lock(&prev_thread->progress_mutex);
463  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
464  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
465  pthread_mutex_unlock(&prev_thread->progress_mutex);
466  }
467 
468  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
469  if (err) {
471  return err;
472  }
473  }
474 
475  /* transfer the stashed hwaccel state, if any */
477  if (!p->hwaccel_threadsafe) {
478  FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
481  }
482 
484  ret = av_packet_ref(p->avpkt, avpkt);
485  if (ret < 0) {
487  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
488  return ret;
489  }
490 
494 
495  fctx->prev_thread = p;
496  fctx->next_decoding++;
497 
498  return 0;
499 }
500 
502  AVFrame *picture, int *got_picture_ptr,
503  AVPacket *avpkt)
504 {
505  FrameThreadContext *fctx = avctx->internal->thread_ctx;
506  int finished = fctx->next_finished;
507  PerThreadContext *p;
508  int err;
509 
510  /* release the async lock, permitting blocked hwaccel threads to
511  * go forward while we are in this function */
512  async_unlock(fctx);
513 
514  /*
515  * Submit a packet to the next decoding thread.
516  */
517 
518  p = &fctx->threads[fctx->next_decoding];
519  err = submit_packet(p, avctx, avpkt);
520  if (err)
521  goto finish;
522 
523  /*
524  * If we're still receiving the initial packets, don't return a frame.
525  */
526 
527  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
528  fctx->delaying = 0;
529 
530  if (fctx->delaying) {
531  *got_picture_ptr=0;
532  if (avpkt->size) {
533  err = avpkt->size;
534  goto finish;
535  }
536  }
537 
538  /*
539  * Return the next available frame from the oldest thread.
540  * If we're at the end of the stream, then we have to skip threads that
541  * didn't output a frame/error, because we don't want to accidentally signal
542  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
543  */
544 
545  do {
546  p = &fctx->threads[finished++];
547 
548  if (atomic_load(&p->state) != STATE_INPUT_READY) {
550  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
553  }
554 
555  av_frame_move_ref(picture, p->frame);
556  *got_picture_ptr = p->got_frame;
557  picture->pkt_dts = p->avpkt->dts;
558  err = p->result;
559 
560  /*
561  * A later call with avkpt->size == 0 may loop over all threads,
562  * including this one, searching for a frame/error to return before being
563  * stopped by the "finished != fctx->next_finished" condition.
564  * Make sure we don't mistakenly return the same frame/error again.
565  */
566  p->got_frame = 0;
567  p->result = 0;
568 
569  if (finished >= avctx->thread_count) finished = 0;
570  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
571 
572  update_context_from_thread(avctx, p->avctx, 1);
573 
574  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
575 
576  fctx->next_finished = finished;
577 
578  /* return the size of the consumed packet if no error occurred */
579  if (err >= 0)
580  err = avpkt->size;
581 finish:
582  async_lock(fctx);
583  return err;
584 }
585 
587 {
588  PerThreadContext *p;
589  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
590 
591  if (!progress ||
592  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
593  return;
594 
595  p = f->owner[field]->internal->thread_ctx;
596 
597  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
598  av_log(f->owner[field], AV_LOG_DEBUG,
599  "%p finished %d field %d\n", progress, n, field);
600 
602 
603  atomic_store_explicit(&progress[field], n, memory_order_release);
604 
607 }
608 
609 void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
610 {
611  PerThreadContext *p;
612  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
613 
614  if (!progress ||
615  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
616  return;
617 
618  p = f->owner[field]->internal->thread_ctx;
619 
620  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
621  av_log(f->owner[field], AV_LOG_DEBUG,
622  "thread awaiting %d field %d from %p\n", n, field, progress);
623 
625  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
628 }
629 
631  PerThreadContext *p;
632 
633  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
634 
635  p = avctx->internal->thread_ctx;
636 
637  p->hwaccel_threadsafe = avctx->hwaccel &&
639 
640  if (hwaccel_serial(avctx) && !p->hwaccel_serializing) {
642  p->hwaccel_serializing = 1;
643  }
644 
645  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
646  if (avctx->hwaccel &&
648  p->async_serializing = 1;
649 
650  async_lock(p->parent);
651  }
652 
653  /* thread-unsafe hwaccels share a single private data instance, so we
654  * save hwaccel state for passing to the next thread;
655  * this is done here so that this worker thread can wipe its own hwaccel
656  * state after decoding, without requiring synchronization */
658  if (hwaccel_serial(avctx)) {
659  p->parent->stash_hwaccel = avctx->hwaccel;
662  }
663 
666  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
667  }
668 
670 
673 }
674 
675 /// Waits for all threads to finish.
676 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
677 {
678  int i;
679 
680  async_unlock(fctx);
681 
682  for (i = 0; i < thread_count; i++) {
683  PerThreadContext *p = &fctx->threads[i];
684 
685  if (atomic_load(&p->state) != STATE_INPUT_READY) {
687  while (atomic_load(&p->state) != STATE_INPUT_READY)
690  }
691  p->got_frame = 0;
692  }
693 
694  async_lock(fctx);
695 }
696 
697 #define OFF(member) offsetof(FrameThreadContext, member)
698 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
699  (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
700  (OFF(async_cond)));
701 #undef OFF
702 
703 #define OFF(member) offsetof(PerThreadContext, member)
704 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
705  (OFF(progress_mutex), OFF(mutex)),
706  (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
707 #undef OFF
708 
709 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
710 {
711  FrameThreadContext *fctx = avctx->internal->thread_ctx;
712  const FFCodec *codec = ffcodec(avctx->codec);
713  int i;
714 
715  park_frame_worker_threads(fctx, thread_count);
716 
717  for (i = 0; i < thread_count; i++) {
718  PerThreadContext *p = &fctx->threads[i];
719  AVCodecContext *ctx = p->avctx;
720 
721  if (ctx->internal) {
722  if (p->thread_init == INITIALIZED) {
724  p->die = 1;
727 
728  pthread_join(p->thread, NULL);
729  }
730  if (codec->close && p->thread_init != UNINITIALIZED)
731  codec->close(ctx);
732 
733  /* When using a threadsafe hwaccel, this is where
734  * each thread's context is uninit'd and freed. */
736 
737  if (ctx->priv_data) {
738  if (codec->p.priv_class)
741  }
742 
743  av_buffer_unref(&ctx->internal->pool);
744  av_packet_free(&ctx->internal->last_pkt_props);
745  av_freep(&ctx->internal);
746  av_buffer_unref(&ctx->hw_frames_ctx);
747  }
748 
749  av_frame_free(&p->frame);
750 
751  ff_pthread_free(p, per_thread_offsets);
752  av_packet_free(&p->avpkt);
753 
754  av_freep(&p->avctx);
755  }
756 
757  av_freep(&fctx->threads);
758  ff_pthread_free(fctx, thread_ctx_offsets);
759 
760  /* if we have stashed hwaccel state, move it to the user-facing context,
761  * so it will be freed in avcodec_close() */
762  av_assert0(!avctx->hwaccel);
763  FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
764  FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
765  FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
766 
767  av_freep(&avctx->internal->thread_ctx);
768 }
769 
770 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
771  FrameThreadContext *fctx, AVCodecContext *avctx,
772  const FFCodec *codec, int first)
773 {
775  int err;
776 
778 
779  copy = av_memdup(avctx, sizeof(*avctx));
780  if (!copy)
781  return AVERROR(ENOMEM);
782  copy->priv_data = NULL;
783 
784  /* From now on, this PerThreadContext will be cleaned up by
785  * ff_frame_thread_free in case of errors. */
786  (*threads_to_free)++;
787 
788  p->parent = fctx;
789  p->avctx = copy;
790 
791  copy->internal = ff_decode_internal_alloc();
792  if (!copy->internal)
793  return AVERROR(ENOMEM);
794  copy->internal->thread_ctx = p;
795 
796  copy->delay = avctx->delay;
797 
798  if (codec->priv_data_size) {
799  copy->priv_data = av_mallocz(codec->priv_data_size);
800  if (!copy->priv_data)
801  return AVERROR(ENOMEM);
802 
803  if (codec->p.priv_class) {
804  *(const AVClass **)copy->priv_data = codec->p.priv_class;
805  err = av_opt_copy(copy->priv_data, avctx->priv_data);
806  if (err < 0)
807  return err;
808  }
809  }
810 
811  err = ff_pthread_init(p, per_thread_offsets);
812  if (err < 0)
813  return err;
814 
815  if (!(p->frame = av_frame_alloc()) ||
816  !(p->avpkt = av_packet_alloc()))
817  return AVERROR(ENOMEM);
818 
819  if (!first)
820  copy->internal->is_copy = 1;
821 
822  copy->internal->last_pkt_props = av_packet_alloc();
823  if (!copy->internal->last_pkt_props)
824  return AVERROR(ENOMEM);
825 
826  if (codec->init) {
827  err = codec->init(copy);
828  if (err < 0) {
831  return err;
832  }
833  }
835 
836  if (first)
837  update_context_from_thread(avctx, copy, 1);
838 
839  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
840 
842  if (err < 0)
843  return err;
845 
846  return 0;
847 }
848 
850 {
851  int thread_count = avctx->thread_count;
852  const FFCodec *codec = ffcodec(avctx->codec);
853  FrameThreadContext *fctx;
854  int err, i = 0;
855 
856  if (!thread_count) {
857  int nb_cpus = av_cpu_count();
858  // use number of cores + 1 as thread count if there is more than one
859  if (nb_cpus > 1)
860  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
861  else
862  thread_count = avctx->thread_count = 1;
863  }
864 
865  if (thread_count <= 1) {
866  avctx->active_thread_type = 0;
867  return 0;
868  }
869 
870  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
871  if (!fctx)
872  return AVERROR(ENOMEM);
873 
874  err = ff_pthread_init(fctx, thread_ctx_offsets);
875  if (err < 0) {
876  ff_pthread_free(fctx, thread_ctx_offsets);
877  av_freep(&avctx->internal->thread_ctx);
878  return err;
879  }
880 
881  fctx->async_lock = 1;
882  fctx->delaying = 1;
883 
884  if (codec->p.type == AVMEDIA_TYPE_VIDEO)
885  avctx->delay = avctx->thread_count - 1;
886 
887  fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
888  if (!fctx->threads) {
889  err = AVERROR(ENOMEM);
890  goto error;
891  }
892 
893  for (; i < thread_count; ) {
894  PerThreadContext *p = &fctx->threads[i];
895  int first = !i;
896 
897  err = init_thread(p, &i, fctx, avctx, codec, first);
898  if (err < 0)
899  goto error;
900  }
901 
902  return 0;
903 
904 error:
905  ff_frame_thread_free(avctx, i);
906  return err;
907 }
908 
910 {
911  int i;
912  FrameThreadContext *fctx = avctx->internal->thread_ctx;
913 
914  if (!fctx) return;
915 
917  if (fctx->prev_thread) {
918  if (fctx->prev_thread != &fctx->threads[0])
920  }
921 
922  fctx->next_decoding = fctx->next_finished = 0;
923  fctx->delaying = 1;
924  fctx->prev_thread = NULL;
925  for (i = 0; i < avctx->thread_count; i++) {
926  PerThreadContext *p = &fctx->threads[i];
927  // Make sure decode flush calls with size=0 won't return old frames
928  p->got_frame = 0;
929  av_frame_unref(p->frame);
930  p->result = 0;
931 
932  if (ffcodec(avctx->codec)->flush)
933  ffcodec(avctx->codec)->flush(p->avctx);
934  }
935 }
936 
938 {
939  if ((avctx->active_thread_type & FF_THREAD_FRAME) &&
941  PerThreadContext *p = avctx->internal->thread_ctx;
942 
943  if (atomic_load(&p->state) != STATE_SETTING_UP)
944  return 0;
945  }
946 
947  return 1;
948 }
949 
951 {
952  PerThreadContext *p;
953  int err;
954 
955  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
956  return ff_get_buffer(avctx, f, flags);
957 
958  p = avctx->internal->thread_ctx;
959  if (atomic_load(&p->state) != STATE_SETTING_UP &&
961  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
962  return -1;
963  }
964 
966  err = ff_get_buffer(avctx, f, flags);
967 
969 
970  return err;
971 }
972 
974 {
975  int ret = thread_get_buffer_internal(avctx, f, flags);
976  if (ret < 0)
977  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
978  return ret;
979 }
980 
982 {
983  int ret;
984 
985  f->owner[0] = f->owner[1] = avctx;
986  /* Hint: It is possible for this function to be called with codecs
987  * that don't support frame threading at all, namely in case
988  * a frame-threaded decoder shares code with codecs that are not.
989  * This currently affects non-MPEG-4 mpegvideo codecs and and VP7.
990  * The following check will always be true for them. */
991  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
992  return ff_get_buffer(avctx, f->f, flags);
993 
995  atomic_int *progress;
996  f->progress = av_buffer_alloc(2 * sizeof(*progress));
997  if (!f->progress) {
998  return AVERROR(ENOMEM);
999  }
1000  progress = (atomic_int*)f->progress->data;
1001 
1002  atomic_init(&progress[0], -1);
1003  atomic_init(&progress[1], -1);
1004  }
1005 
1006  ret = ff_thread_get_buffer(avctx, f->f, flags);
1007  if (ret)
1008  av_buffer_unref(&f->progress);
1009  return ret;
1010 }
1011 
1013 {
1014  if (!f)
1015  return;
1016 
1017  if (avctx->debug & FF_DEBUG_BUFFERS)
1018  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1019 
1020  av_frame_unref(f);
1021 }
1022 
1024 {
1025  av_buffer_unref(&f->progress);
1026  f->owner[0] = f->owner[1] = NULL;
1027  ff_thread_release_buffer(avctx, f->f);
1028 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
hwconfig.h
FFCodec::update_thread_context
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: codec_internal.h:157
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:423
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1431
AVCodec
AVCodec.
Definition: codec.h:187
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:165
AVCodecContext::hwaccel_context
void * hwaccel_context
Legacy hardware accelerator context.
Definition: avcodec.h:1455
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:77
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:142
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1025
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:1023
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1060
PerThreadContext::debug_threads
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
Definition: pthread_frame.c:105
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:219
FrameThreadContext::next_decoding
int next_decoding
The next context to submit a packet to.
Definition: pthread_frame.c:126
thread.h
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:90
ff_thread_flush
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread_frame.c:909
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:770
ff_thread_can_start_frame
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread_frame.c:937
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
UNINITIALIZED
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
Definition: pthread_frame.c:63
PerThreadContext::state
atomic_int state
Definition: pthread_frame.c:92
FrameThreadContext
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:111
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:100
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:676
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1018
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:206
internal.h
version_major.h
atomic_int
intptr_t atomic_int
Definition: stdatomic.h:55
FFCodec
Definition: codec_internal.h:127
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:590
FrameThreadContext::stash_hwaccel_context
void * stash_hwaccel_context
Definition: pthread_frame.c:138
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:600
PerThreadContext::die
int die
Set when the thread should exit.
Definition: pthread_frame.c:94
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:127
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:74
AVCodecInternal::pool
AVBufferRef * pool
Definition: internal.h:65
FrameThreadContext::buffer_mutex
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread_frame.c:116
ff_hwaccel_uninit
void ff_hwaccel_uninit(AVCodecContext *avctx)
Definition: decode.c:1253
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
FFCodec::priv_data_size
int priv_data_size
Definition: codec_internal.h:145
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1797
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:709
AVHWAccel
Definition: avcodec.h:2123
AVCodecContext::skip_idct
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:1753
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
finish
static void finish(void)
Definition: movenc.c:342
FFHWAccel
Definition: hwaccel_internal.h:33
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:446
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2101
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1760
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1528
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:397
FrameThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:115
HWACCEL_CAP_THREAD_SAFE
#define HWACCEL_CAP_THREAD_SAFE
Definition: hwaccel_internal.h:31
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:517
PerThreadContext
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:71
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:632
submit_packet
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
Definition: pthread_frame.c:437
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:88
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:1233
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:1011
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
ff_thread_report_progress
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread_frame.c:586
FFCodec::update_thread_context_for_user
int(* update_thread_context_for_user)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy variables back to the user-facing context.
Definition: codec_internal.h:162
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:740
INITIALIZED
@ INITIALIZED
Thread has been properly set up.
Definition: pthread_frame.c:65
FrameThreadContext::async_lock
int async_lock
Definition: pthread_frame.c:124
STATE_SETUP_FINISHED
@ STATE_SETUP_FINISHED
Set after the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:59
STATE_INPUT_READY
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:55
FFHWAccel::priv_data_size
int priv_data_size
Size of the private data to allocate in AVCodecInternal.hwaccel_priv_data.
Definition: hwaccel_internal.h:111
PerThreadContext::output_cond
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:79
NEEDS_CLOSE
@ NEEDS_CLOSE
FFCodec->close needs to be called.
Definition: pthread_frame.c:64
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:973
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:1513
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFCodec::flush
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec_internal.h:246
decode.h
PerThreadContext::hwaccel_threadsafe
int hwaccel_threadsafe
Definition: pthread_frame.c:103
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:113
FFCodec::decode
int(* decode)(struct AVCodecContext *avctx, struct AVFrame *frame, int *got_frame_ptr, struct AVPacket *avpkt)
Decode to an AVFrame.
Definition: codec_internal.h:193
FFCodec::init
int(* init)(struct AVCodecContext *)
Definition: codec_internal.h:178
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:447
arg
const char * arg
Definition: jacosubdec.c:67
pthread_create
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:80
if
if(ret)
Definition: filter_design.txt:179
PerThreadContext::got_frame
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:89
threadframe.h
HWACCEL_CAP_ASYNC_SAFE
#define HWACCEL_CAP_ASYNC_SAFE
Header providing the internals of AVHWAccel.
Definition: hwaccel_internal.h:30
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:1035
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:609
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:911
thread_get_buffer_internal
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
Definition: pthread_frame.c:950
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:183
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:472
PerThreadContext::progress_mutex
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:82
FrameThreadContext::async_mutex
pthread_mutex_t async_mutex
Definition: pthread_frame.c:122
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:630
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:121
PerThreadContext::avctx
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:84
pthread_internal.h
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1719
FFCodec::cb
union FFCodec::@53 cb
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:459
FF_DEBUG_BUFFERS
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1396
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:431
AVCodecContext::level
int level
level
Definition: avcodec.h:1734
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
FF_DEBUG_THREADS
#define FF_DEBUG_THREADS
Definition: avcodec.h:1397
OFF
#define OFF(member)
Definition: pthread_frame.c:703
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:79
AVCodecInternal::last_pkt_props
AVPacket * last_pkt_props
Properties (timestamps+side data) extracted from the last packet passed for decoding.
Definition: internal.h:84
async_lock
static void async_lock(FrameThreadContext *fctx)
Definition: pthread_frame.c:147
PerThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:76
PerThreadContext::thread
pthread_t thread
Definition: pthread_frame.c:74
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:209
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:559
STATE_SETTING_UP
@ STATE_SETTING_UP
Set before the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:57
PerThreadContext::result
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:90
FrameThreadContext::stash_hwaccel
const AVHWAccel * stash_hwaccel
Definition: pthread_frame.c:137
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:524
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1575
AVPacket::size
int size
Definition: packet.h:375
ff_thread_decode_frame
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
Definition: pthread_frame.c:501
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:185
codec_internal.h
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:124
cpu.h
PerThreadContext::avpkt
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:86
async_unlock
static void async_unlock(FrameThreadContext *fctx)
Definition: pthread_frame.c:156
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1076
FrameThreadContext::async_cond
pthread_cond_t async_cond
Definition: pthread_frame.c:123
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:325
frame.h
buffer.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
PerThreadContext::parent
struct FrameThreadContext * parent
Definition: pthread_frame.c:72
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:63
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
AVCodecContext::skip_loop_filter
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1746
pthread_t
Definition: os2threads.h:44
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1539
ff_thread_release_buffer
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around av_frame_unref() for frame-threaded codecs.
Definition: pthread_frame.c:1012
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1506
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:386
PerThreadContext::thread_init
int thread_init
Definition: pthread_frame.c:75
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:244
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:1898
internal.h
common.h
AVCodecContext::hwaccel_flags
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition: avcodec.h:1984
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:649
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:622
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:254
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1482
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
av_buffer_replace
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:233
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1042
pthread_cond_t
Definition: os2threads.h:58
AVCodecContext::height
int height
Definition: avcodec.h:617
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:654
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:981
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:262
AVCodecContext::hw_frames_ctx
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:1934
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2112
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:849
ff_decode_internal_alloc
struct AVCodecInternal * ff_decode_internal_alloc(void)
Definition: decode.c:1845
PerThreadContext::async_serializing
int async_serializing
Definition: pthread_frame.c:97
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:479
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:679
AVCodecContext
main external API structure.
Definition: avcodec.h:437
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1547
PerThreadContext::hwaccel_serializing
int hwaccel_serializing
Definition: pthread_frame.c:96
ThreadFrame
Definition: threadframe.h:27
avcodec_internal.h
PerThreadContext::frame
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:88
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1592
ffhwaccel
static const FFHWAccel * ffhwaccel(const AVHWAccel *codec)
Definition: hwaccel_internal.h:165
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:575
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:2051
AV_CODEC_CAP_DELAY
#define AV_CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
Definition: codec.h:76
FFCodec::close
int(* close)(struct AVCodecContext *)
Definition: codec_internal.h:240
ff_pthread_init
av_cold int ff_pthread_init(void *obj, const unsigned offsets[])
Initialize/destroy a list of mutexes/conditions contained in a structure.
Definition: pthread.c:104
pthread_cond_wait
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:192
AVCodecContext::debug
int debug
debug
Definition: avcodec.h:1384
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:643
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:632
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:129
mem.h
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1885
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:464
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecInternal::thread_ctx
void * thread_ctx
Definition: internal.h:67
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:617
AVCodecContext::frame_number
attribute_deprecated int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1102
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:467
PerThreadContext::mutex
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:81
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:268
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1804
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
FFHWAccel::caps_internal
int caps_internal
Internal hwaccel capabilities.
Definition: hwaccel_internal.h:116
snprintf
#define snprintf
Definition: snprintf.h:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1133
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:818
FrameThreadContext::stash_hwaccel_priv
void * stash_hwaccel_priv
Definition: pthread_frame.c:139
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:78
FFHWAccel::update_thread_context
int(* update_thread_context)(AVCodecContext *dst, const AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: hwaccel_internal.h:149
FrameThreadContext::threads
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:112
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:75
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:214