FFmpeg
pthread_frame.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Frame multithreading support functions
22  * @see doc/multithreading.txt
23  */
24 
25 #include "config.h"
26 
27 #include <stdatomic.h>
28 #include <stdint.h>
29 
30 #include "avcodec.h"
31 #include "codec_internal.h"
32 #include "decode.h"
33 #include "hwconfig.h"
34 #include "internal.h"
35 #include "pthread_internal.h"
36 #include "thread.h"
37 #include "threadframe.h"
38 #include "version_major.h"
39 
40 #include "libavutil/avassert.h"
41 #include "libavutil/buffer.h"
42 #include "libavutil/common.h"
43 #include "libavutil/cpu.h"
44 #include "libavutil/frame.h"
45 #include "libavutil/internal.h"
46 #include "libavutil/log.h"
47 #include "libavutil/mem.h"
48 #include "libavutil/opt.h"
49 #include "libavutil/thread.h"
50 
51 enum {
52  ///< Set when the thread is awaiting a packet.
54  ///< Set before the codec has called ff_thread_finish_setup().
56  /**
57  * Set when the codec calls get_buffer().
58  * State is returned to STATE_SETTING_UP afterwards.
59  */
61  /**
62  * Set when the codec calls get_format().
63  * State is returned to STATE_SETTING_UP afterwards.
64  */
66  ///< Set after the codec has called ff_thread_finish_setup().
68 };
69 
70 enum {
71  UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
72  NEEDS_CLOSE, ///< FFCodec->close needs to be called
73  INITIALIZED, ///< Thread has been properly set up
74 };
75 
76 /**
77  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
78  */
79 typedef struct PerThreadContext {
81 
84  unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
85  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
86  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
87  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
88 
89  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
90  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
91 
92  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
93 
94  AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
95 
96  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
97  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
98  int result; ///< The result of the last codec decode/encode() call.
99 
101 
102  int die; ///< Set when the thread should exit.
103 
106 
107  // set to 1 in ff_thread_finish_setup() when a threadsafe hwaccel is used;
108  // cannot check hwaccel caps directly, because
109  // worked threads clear hwaccel state for thread-unsafe hwaccels
110  // after each decode call
112 
113  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
115 
116 /**
117  * Context stored in the client AVCodecInternal thread_ctx.
118  */
119 typedef struct FrameThreadContext {
120  PerThreadContext *threads; ///< The contexts for each thread.
121  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
122 
123  unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
124  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
125  /**
126  * This lock is used for ensuring threads run in serial when thread-unsafe
127  * hwaccel is used.
128  */
133 
134  int next_decoding; ///< The next context to submit a packet to.
135  int next_finished; ///< The next context to return output from.
136 
137  int delaying; /**<
138  * Set for the first N packets, where N is the number of threads.
139  * While it is set, ff_thread_en/decode_frame won't return any results.
140  */
141 
142  /* hwaccel state for thread-unsafe hwaccels is temporarily stored here in
143  * order to transfer its ownership to the next decoding thread without the
144  * need for extra synchronization */
149 
150 static int hwaccel_serial(const AVCodecContext *avctx)
151 {
152  return avctx->hwaccel && !(avctx->hwaccel->caps_internal & HWACCEL_CAP_THREAD_SAFE);
153 }
154 
155 static void async_lock(FrameThreadContext *fctx)
156 {
158  while (fctx->async_lock)
159  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
160  fctx->async_lock = 1;
162 }
163 
165 {
167  av_assert0(fctx->async_lock);
168  fctx->async_lock = 0;
171 }
172 
174 {
175  AVCodecContext *avctx = p->avctx;
176  int idx = p - p->parent->threads;
177  char name[16];
178 
179  snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
180 
182 }
183 
184 /**
185  * Codec worker thread.
186  *
187  * Automatically calls ff_thread_finish_setup() if the codec does
188  * not provide an update_thread_context method, or if the codec returns
189  * before calling it.
190  */
192 {
193  PerThreadContext *p = arg;
194  AVCodecContext *avctx = p->avctx;
195  const FFCodec *codec = ffcodec(avctx->codec);
196 
197  thread_set_name(p);
198 
200  while (1) {
201  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
203 
204  if (p->die) break;
205 
206  if (!codec->update_thread_context)
207  ff_thread_finish_setup(avctx);
208 
209  /* If a decoder supports hwaccel, then it must call ff_get_format().
210  * Since that call must happen before ff_thread_finish_setup(), the
211  * decoder is required to implement update_thread_context() and call
212  * ff_thread_finish_setup() manually. Therefore the above
213  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
214  * cannot be true here. */
216 
217  /* if the previous thread uses thread-unsafe hwaccel then we take the
218  * lock to ensure the threads don't run concurrently */
219  if (hwaccel_serial(avctx)) {
221  p->hwaccel_serializing = 1;
222  }
223 
224  av_frame_unref(p->frame);
225  p->got_frame = 0;
226  p->result = codec->cb.decode(avctx, p->frame, &p->got_frame, p->avpkt);
227 
228  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0])
229  ff_thread_release_buffer(avctx, p->frame);
230 
231  if (atomic_load(&p->state) == STATE_SETTING_UP)
232  ff_thread_finish_setup(avctx);
233 
234  if (p->hwaccel_serializing) {
235  /* wipe hwaccel state for thread-unsafe hwaccels to avoid stale
236  * pointers lying around;
237  * the state was transferred to FrameThreadContext in
238  * ff_thread_finish_setup(), so nothing is leaked */
239  avctx->hwaccel = NULL;
240  avctx->hwaccel_context = NULL;
241  avctx->internal->hwaccel_priv_data = NULL;
242 
243  p->hwaccel_serializing = 0;
245  }
246  av_assert0(!avctx->hwaccel ||
248 
249  if (p->async_serializing) {
250  p->async_serializing = 0;
251 
252  async_unlock(p->parent);
253  }
254 
256 
258 
262  }
264 
265  return NULL;
266 }
267 
268 /**
269  * Update the next thread's AVCodecContext with values from the reference thread's context.
270  *
271  * @param dst The destination context.
272  * @param src The source context.
273  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
274  * @return 0 on success, negative error code on failure
275  */
277 {
278  const FFCodec *const codec = ffcodec(dst->codec);
279  int err = 0;
280 
281  if (dst != src && (for_user || codec->update_thread_context)) {
282  dst->time_base = src->time_base;
283  dst->framerate = src->framerate;
284  dst->width = src->width;
285  dst->height = src->height;
286  dst->pix_fmt = src->pix_fmt;
287  dst->sw_pix_fmt = src->sw_pix_fmt;
288 
289  dst->coded_width = src->coded_width;
290  dst->coded_height = src->coded_height;
291 
292  dst->has_b_frames = src->has_b_frames;
293  dst->idct_algo = src->idct_algo;
294  dst->properties = src->properties;
295 
296  dst->bits_per_coded_sample = src->bits_per_coded_sample;
297  dst->sample_aspect_ratio = src->sample_aspect_ratio;
298 
299  dst->profile = src->profile;
300  dst->level = src->level;
301 
302  dst->bits_per_raw_sample = src->bits_per_raw_sample;
303 #if FF_API_TICKS_PER_FRAME
305  dst->ticks_per_frame = src->ticks_per_frame;
307 #endif
308  dst->color_primaries = src->color_primaries;
309 
310  dst->color_trc = src->color_trc;
311  dst->colorspace = src->colorspace;
312  dst->color_range = src->color_range;
313  dst->chroma_sample_location = src->chroma_sample_location;
314 
315  dst->sample_rate = src->sample_rate;
316  dst->sample_fmt = src->sample_fmt;
317 #if FF_API_OLD_CHANNEL_LAYOUT
319  dst->channels = src->channels;
320  dst->channel_layout = src->channel_layout;
322 #endif
323  err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
324  if (err < 0)
325  return err;
326 
327  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
328  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
330 
331  if (src->hw_frames_ctx) {
332  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
333  if (!dst->hw_frames_ctx)
334  return AVERROR(ENOMEM);
335  }
336  }
337 
338  dst->hwaccel_flags = src->hwaccel_flags;
339 
340  err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
341  if (err < 0)
342  return err;
343  }
344 
345  if (for_user) {
347  err = codec->update_thread_context_for_user(dst, src);
348  } else {
349  const PerThreadContext *p_src = src->internal->thread_ctx;
350  PerThreadContext *p_dst = dst->internal->thread_ctx;
351 
352  if (codec->update_thread_context) {
353  err = codec->update_thread_context(dst, src);
354  if (err < 0)
355  return err;
356  }
357 
358  // reset dst hwaccel state if needed
360  (!dst->hwaccel && !dst->internal->hwaccel_priv_data));
361  if (p_dst->hwaccel_threadsafe &&
362  (!p_src->hwaccel_threadsafe || dst->hwaccel != src->hwaccel)) {
363  ff_hwaccel_uninit(dst);
364  p_dst->hwaccel_threadsafe = 0;
365  }
366 
367  // propagate hwaccel state for threadsafe hwaccels
368  if (p_src->hwaccel_threadsafe) {
369  if (!dst->hwaccel) {
370  if (src->hwaccel->priv_data_size) {
371  av_assert0(src->hwaccel->update_thread_context);
372 
374  av_mallocz(src->hwaccel->priv_data_size);
375  if (!dst->internal->hwaccel_priv_data)
376  return AVERROR(ENOMEM);
377  }
378  dst->hwaccel = src->hwaccel;
379  }
380  av_assert0(dst->hwaccel == src->hwaccel);
381 
382  if (src->hwaccel->update_thread_context) {
383  err = src->hwaccel->update_thread_context(dst, src);
384  if (err < 0) {
385  av_log(dst, AV_LOG_ERROR, "Error propagating hwaccel state\n");
386  ff_hwaccel_uninit(dst);
387  return err;
388  }
389  }
390  p_dst->hwaccel_threadsafe = 1;
391  }
392  }
393 
394  return err;
395 }
396 
397 /**
398  * Update the next thread's AVCodecContext with values set by the user.
399  *
400  * @param dst The destination context.
401  * @param src The source context.
402  * @return 0 on success, negative error code on failure
403  */
405 {
406  int err;
407 
408  dst->flags = src->flags;
409 
410  dst->draw_horiz_band= src->draw_horiz_band;
411  dst->get_buffer2 = src->get_buffer2;
412 
413  dst->opaque = src->opaque;
414  dst->debug = src->debug;
415 
416  dst->slice_flags = src->slice_flags;
417  dst->flags2 = src->flags2;
418  dst->export_side_data = src->export_side_data;
419 
420  dst->skip_loop_filter = src->skip_loop_filter;
421  dst->skip_idct = src->skip_idct;
422  dst->skip_frame = src->skip_frame;
423 
424  dst->frame_num = src->frame_num;
425 #if FF_API_AVCTX_FRAME_NUMBER
427  dst->frame_number = src->frame_number;
429 #endif
430 #if FF_API_REORDERED_OPAQUE
432  dst->reordered_opaque = src->reordered_opaque;
434 #endif
435 
436 #if FF_API_SLICE_OFFSET
438  if (src->slice_count && src->slice_offset) {
439  if (dst->slice_count < src->slice_count) {
440  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
441  sizeof(*dst->slice_offset));
442  if (err < 0)
443  return err;
444  }
445  memcpy(dst->slice_offset, src->slice_offset,
446  src->slice_count * sizeof(*dst->slice_offset));
447  }
448  dst->slice_count = src->slice_count;
450 #endif
451 
453  err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
454  if (err < 0)
455  return err;
456 
457  return 0;
458 }
459 
460 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
461  AVPacket *avpkt)
462 {
463  FrameThreadContext *fctx = p->parent;
464  PerThreadContext *prev_thread = fctx->prev_thread;
465  const AVCodec *codec = p->avctx->codec;
466  int ret;
467 
468  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
469  return 0;
470 
472 
473  ret = update_context_from_user(p->avctx, user_avctx);
474  if (ret) {
476  return ret;
477  }
479  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
480  memory_order_relaxed);
481 
482  if (prev_thread) {
483  int err;
484  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
485  pthread_mutex_lock(&prev_thread->progress_mutex);
486  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
487  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
488  pthread_mutex_unlock(&prev_thread->progress_mutex);
489  }
490 
491  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
492  if (err) {
494  return err;
495  }
496  }
497 
498  /* transfer the stashed hwaccel state, if any */
500  if (!p->hwaccel_threadsafe) {
501  FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
504  }
505 
507  ret = av_packet_ref(p->avpkt, avpkt);
508  if (ret < 0) {
510  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
511  return ret;
512  }
513 
517 
518  fctx->prev_thread = p;
519  fctx->next_decoding++;
520 
521  return 0;
522 }
523 
525  AVFrame *picture, int *got_picture_ptr,
526  AVPacket *avpkt)
527 {
528  FrameThreadContext *fctx = avctx->internal->thread_ctx;
529  int finished = fctx->next_finished;
530  PerThreadContext *p;
531  int err;
532 
533  /* release the async lock, permitting blocked hwaccel threads to
534  * go forward while we are in this function */
535  async_unlock(fctx);
536 
537  /*
538  * Submit a packet to the next decoding thread.
539  */
540 
541  p = &fctx->threads[fctx->next_decoding];
542  err = submit_packet(p, avctx, avpkt);
543  if (err)
544  goto finish;
545 
546  /*
547  * If we're still receiving the initial packets, don't return a frame.
548  */
549 
550  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
551  fctx->delaying = 0;
552 
553  if (fctx->delaying) {
554  *got_picture_ptr=0;
555  if (avpkt->size) {
556  err = avpkt->size;
557  goto finish;
558  }
559  }
560 
561  /*
562  * Return the next available frame from the oldest thread.
563  * If we're at the end of the stream, then we have to skip threads that
564  * didn't output a frame/error, because we don't want to accidentally signal
565  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
566  */
567 
568  do {
569  p = &fctx->threads[finished++];
570 
571  if (atomic_load(&p->state) != STATE_INPUT_READY) {
573  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
576  }
577 
578  av_frame_move_ref(picture, p->frame);
579  *got_picture_ptr = p->got_frame;
580  picture->pkt_dts = p->avpkt->dts;
581  err = p->result;
582 
583  /*
584  * A later call with avkpt->size == 0 may loop over all threads,
585  * including this one, searching for a frame/error to return before being
586  * stopped by the "finished != fctx->next_finished" condition.
587  * Make sure we don't mistakenly return the same frame/error again.
588  */
589  p->got_frame = 0;
590  p->result = 0;
591 
592  if (finished >= avctx->thread_count) finished = 0;
593  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
594 
595  update_context_from_thread(avctx, p->avctx, 1);
596 
597  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
598 
599  fctx->next_finished = finished;
600 
601  /* return the size of the consumed packet if no error occurred */
602  if (err >= 0)
603  err = avpkt->size;
604 finish:
605  async_lock(fctx);
606  return err;
607 }
608 
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_relaxed) >= 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  "%p finished %d field %d\n", progress, n, field);
623 
625 
626  atomic_store_explicit(&progress[field], n, memory_order_release);
627 
630 }
631 
632 void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
633 {
634  PerThreadContext *p;
635  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
636 
637  if (!progress ||
638  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
639  return;
640 
641  p = f->owner[field]->internal->thread_ctx;
642 
643  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
644  av_log(f->owner[field], AV_LOG_DEBUG,
645  "thread awaiting %d field %d from %p\n", n, field, progress);
646 
648  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
651 }
652 
654  PerThreadContext *p = avctx->internal->thread_ctx;
655 
656  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
657 
658  p->hwaccel_threadsafe = avctx->hwaccel &&
660 
661  if (hwaccel_serial(avctx) && !p->hwaccel_serializing) {
663  p->hwaccel_serializing = 1;
664  }
665 
666  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
667  if (avctx->hwaccel &&
669  p->async_serializing = 1;
670 
671  async_lock(p->parent);
672  }
673 
674  /* thread-unsafe hwaccels share a single private data instance, so we
675  * save hwaccel state for passing to the next thread;
676  * this is done here so that this worker thread can wipe its own hwaccel
677  * state after decoding, without requiring synchronization */
679  if (hwaccel_serial(avctx)) {
680  p->parent->stash_hwaccel = avctx->hwaccel;
683  }
684 
687  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
688  }
689 
691 
694 }
695 
696 /// Waits for all threads to finish.
697 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
698 {
699  int i;
700 
701  async_unlock(fctx);
702 
703  for (i = 0; i < thread_count; i++) {
704  PerThreadContext *p = &fctx->threads[i];
705 
706  if (atomic_load(&p->state) != STATE_INPUT_READY) {
708  while (atomic_load(&p->state) != STATE_INPUT_READY)
711  }
712  p->got_frame = 0;
713  }
714 
715  async_lock(fctx);
716 }
717 
718 #define OFF(member) offsetof(FrameThreadContext, member)
719 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
720  (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
721  (OFF(async_cond)));
722 #undef OFF
723 
724 #define OFF(member) offsetof(PerThreadContext, member)
725 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
726  (OFF(progress_mutex), OFF(mutex)),
727  (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
728 #undef OFF
729 
730 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
731 {
732  FrameThreadContext *fctx = avctx->internal->thread_ctx;
733  const FFCodec *codec = ffcodec(avctx->codec);
734  int i;
735 
736  park_frame_worker_threads(fctx, thread_count);
737 
738  for (i = 0; i < thread_count; i++) {
739  PerThreadContext *p = &fctx->threads[i];
740  AVCodecContext *ctx = p->avctx;
741 
742  if (ctx->internal) {
743  if (p->thread_init == INITIALIZED) {
745  p->die = 1;
748 
749  pthread_join(p->thread, NULL);
750  }
751  if (codec->close && p->thread_init != UNINITIALIZED)
752  codec->close(ctx);
753 
754  if (ctx->priv_data) {
755  if (codec->p.priv_class)
758  }
759 
760 #if FF_API_SLICE_OFFSET
762  av_freep(&ctx->slice_offset);
764 #endif
765 
766  av_buffer_unref(&ctx->internal->pool);
767  av_packet_free(&ctx->internal->last_pkt_props);
768  av_freep(&ctx->internal);
769  av_buffer_unref(&ctx->hw_frames_ctx);
770  }
771 
772  av_frame_free(&p->frame);
773 
774  ff_pthread_free(p, per_thread_offsets);
775  av_packet_free(&p->avpkt);
776 
777  av_freep(&p->avctx);
778  }
779 
780  av_freep(&fctx->threads);
781  ff_pthread_free(fctx, thread_ctx_offsets);
782 
783  /* if we have stashed hwaccel state, move it to the user-facing context,
784  * so it will be freed in avcodec_close() */
785  av_assert0(!avctx->hwaccel);
786  FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
787  FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
788  FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
789 
790  av_freep(&avctx->internal->thread_ctx);
791 }
792 
793 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
794  FrameThreadContext *fctx, AVCodecContext *avctx,
795  const FFCodec *codec, int first)
796 {
798  int err;
799 
801 
802  copy = av_memdup(avctx, sizeof(*avctx));
803  if (!copy)
804  return AVERROR(ENOMEM);
805  copy->priv_data = NULL;
806 
807  /* From now on, this PerThreadContext will be cleaned up by
808  * ff_frame_thread_free in case of errors. */
809  (*threads_to_free)++;
810 
811  p->parent = fctx;
812  p->avctx = copy;
813 
814  copy->internal = av_mallocz(sizeof(*copy->internal));
815  if (!copy->internal)
816  return AVERROR(ENOMEM);
817  copy->internal->thread_ctx = p;
818 
819  copy->delay = avctx->delay;
820 
821  if (codec->priv_data_size) {
822  copy->priv_data = av_mallocz(codec->priv_data_size);
823  if (!copy->priv_data)
824  return AVERROR(ENOMEM);
825 
826  if (codec->p.priv_class) {
827  *(const AVClass **)copy->priv_data = codec->p.priv_class;
828  err = av_opt_copy(copy->priv_data, avctx->priv_data);
829  if (err < 0)
830  return err;
831  }
832  }
833 
834  err = ff_pthread_init(p, per_thread_offsets);
835  if (err < 0)
836  return err;
837 
838  if (!(p->frame = av_frame_alloc()) ||
839  !(p->avpkt = av_packet_alloc()))
840  return AVERROR(ENOMEM);
841 
842  if (!first)
843  copy->internal->is_copy = 1;
844 
845  copy->internal->last_pkt_props = av_packet_alloc();
846  if (!copy->internal->last_pkt_props)
847  return AVERROR(ENOMEM);
848 
849  if (codec->init) {
850  err = codec->init(copy);
851  if (err < 0) {
854  return err;
855  }
856  }
858 
859  if (first)
860  update_context_from_thread(avctx, copy, 1);
861 
862  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
863 
865  if (err < 0)
866  return err;
868 
869  return 0;
870 }
871 
873 {
874  int thread_count = avctx->thread_count;
875  const FFCodec *codec = ffcodec(avctx->codec);
876  FrameThreadContext *fctx;
877  int err, i = 0;
878 
879  if (!thread_count) {
880  int nb_cpus = av_cpu_count();
881  // use number of cores + 1 as thread count if there is more than one
882  if (nb_cpus > 1)
883  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
884  else
885  thread_count = avctx->thread_count = 1;
886  }
887 
888  if (thread_count <= 1) {
889  avctx->active_thread_type = 0;
890  return 0;
891  }
892 
893  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
894  if (!fctx)
895  return AVERROR(ENOMEM);
896 
897  err = ff_pthread_init(fctx, thread_ctx_offsets);
898  if (err < 0) {
899  ff_pthread_free(fctx, thread_ctx_offsets);
900  av_freep(&avctx->internal->thread_ctx);
901  return err;
902  }
903 
904  fctx->async_lock = 1;
905  fctx->delaying = 1;
906 
907  if (codec->p.type == AVMEDIA_TYPE_VIDEO)
908  avctx->delay = avctx->thread_count - 1;
909 
910  fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
911  if (!fctx->threads) {
912  err = AVERROR(ENOMEM);
913  goto error;
914  }
915 
916  for (; i < thread_count; ) {
917  PerThreadContext *p = &fctx->threads[i];
918  int first = !i;
919 
920  err = init_thread(p, &i, fctx, avctx, codec, first);
921  if (err < 0)
922  goto error;
923  }
924 
925  return 0;
926 
927 error:
928  ff_frame_thread_free(avctx, i);
929  return err;
930 }
931 
933 {
934  int i;
935  FrameThreadContext *fctx = avctx->internal->thread_ctx;
936 
937  if (!fctx) return;
938 
940  if (fctx->prev_thread) {
941  if (fctx->prev_thread != &fctx->threads[0])
943  }
944 
945  fctx->next_decoding = fctx->next_finished = 0;
946  fctx->delaying = 1;
947  fctx->prev_thread = NULL;
948  for (i = 0; i < avctx->thread_count; i++) {
949  PerThreadContext *p = &fctx->threads[i];
950  // Make sure decode flush calls with size=0 won't return old frames
951  p->got_frame = 0;
952  av_frame_unref(p->frame);
953  p->result = 0;
954 
955  if (ffcodec(avctx->codec)->flush)
956  ffcodec(avctx->codec)->flush(p->avctx);
957  }
958 }
959 
961 {
962  PerThreadContext *p = avctx->internal->thread_ctx;
963 
966  return 0;
967  }
968 
969  return 1;
970 }
971 
973 {
974  PerThreadContext *p;
975  int err;
976 
977  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
978  return ff_get_buffer(avctx, f, flags);
979 
980  p = avctx->internal->thread_ctx;
981  if (atomic_load(&p->state) != STATE_SETTING_UP &&
983  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
984  return -1;
985  }
986 
988  err = ff_get_buffer(avctx, f, flags);
989 
991 
992  return err;
993 }
994 
996 {
997  int ret = thread_get_buffer_internal(avctx, f, flags);
998  if (ret < 0)
999  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1000  return ret;
1001 }
1002 
1004 {
1005  int ret;
1006 
1007  f->owner[0] = f->owner[1] = avctx;
1008  /* Hint: It is possible for this function to be called with codecs
1009  * that don't support frame threading at all, namely in case
1010  * a frame-threaded decoder shares code with codecs that are not.
1011  * This currently affects non-MPEG-4 mpegvideo codecs and and VP7.
1012  * The following check will always be true for them. */
1013  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1014  return ff_get_buffer(avctx, f->f, flags);
1015 
1017  atomic_int *progress;
1018  f->progress = av_buffer_alloc(2 * sizeof(*progress));
1019  if (!f->progress) {
1020  return AVERROR(ENOMEM);
1021  }
1022  progress = (atomic_int*)f->progress->data;
1023 
1024  atomic_init(&progress[0], -1);
1025  atomic_init(&progress[1], -1);
1026  }
1027 
1028  ret = ff_thread_get_buffer(avctx, f->f, flags);
1029  if (ret)
1030  av_buffer_unref(&f->progress);
1031  return ret;
1032 }
1033 
1035 {
1036  if (!f)
1037  return;
1038 
1039  if (avctx->debug & FF_DEBUG_BUFFERS)
1040  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1041 
1042  av_frame_unref(f);
1043 }
1044 
1046 {
1047  av_buffer_unref(&f->progress);
1048  f->owner[0] = f->owner[1] = NULL;
1049  ff_thread_release_buffer(avctx, f->f);
1050 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:31
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
hwconfig.h
FFCodec::update_thread_context
int(* update_thread_context)(struct AVCodecContext *dst, const struct AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: codec_internal.h:157
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:422
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1429
AVCodec
AVCodec.
Definition: codec.h:187
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:81
thread_set_name
static void thread_set_name(PerThreadContext *p)
Definition: pthread_frame.c:173
AVCodecContext::hwaccel_context
void * hwaccel_context
Legacy hardware accelerator context.
Definition: avcodec.h:1453
pthread_join
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
name
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default minimum maximum flags name is the option name
Definition: writing_filters.txt:88
PerThreadContext::input_cond
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:85
atomic_store
#define atomic_store(object, desired)
Definition: stdatomic.h:85
FF_CODEC_CAP_INIT_CLEANUP
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: codec_internal.h:42
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
hwaccel_serial
static int hwaccel_serial(const AVCodecContext *avctx)
Definition: pthread_frame.c:150
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1023
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:1045
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1058
PerThreadContext::debug_threads
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
Definition: pthread_frame.c:113
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:134
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:932
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:793
AVHWAccel::caps_internal
int caps_internal
Internal hwaccel capabilities.
Definition: avcodec.h:2248
ff_thread_can_start_frame
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread_frame.c:960
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
PerThreadContext::state
atomic_int state
Definition: pthread_frame.c:100
FrameThreadContext
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:119
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:697
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:330
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1016
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
STATE_GET_BUFFER
@ STATE_GET_BUFFER
Set when the codec calls get_buffer().
Definition: pthread_frame.c:60
FFCodec
Definition: codec_internal.h:127
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
AVCodecContext::slice_offset
attribute_deprecated int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:806
HWACCEL_CAP_THREAD_SAFE
#define HWACCEL_CAP_THREAD_SAFE
Definition: hwconfig.h:27
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:580
FrameThreadContext::stash_hwaccel_context
void * stash_hwaccel_context
Definition: pthread_frame.c:146
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:598
PerThreadContext::die
int die
Set when the thread should exit.
Definition: pthread_frame.c:102
thread.h
ff_pthread_free
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition: pthread.c:91
FrameThreadContext::next_finished
int next_finished
The next context to return output from.
Definition: pthread_frame.c:135
av_packet_free
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:73
AVCodecInternal::pool
AVBufferRef * pool
Definition: internal.h:71
FrameThreadContext::buffer_mutex
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread_frame.c:124
ff_hwaccel_uninit
void ff_hwaccel_uninit(AVCodecContext *avctx)
Definition: decode.c:1157
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:302
AVCodecContext::slice_count
attribute_deprecated int slice_count
slice count
Definition: avcodec.h:798
FFCodec::priv_data_size
int priv_data_size
Definition: codec_internal.h:145
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1785
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:730
AVHWAccel
Definition: avcodec.h:2111
AVCodecContext::skip_idct
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:1741
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
finish
static void finish(void)
Definition: movenc.c:342
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:444
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2089
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1748
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1526
NEEDS_CLOSE
@ NEEDS_CLOSE
FFCodec->close needs to be called.
Definition: pthread_frame.c:72
FrameThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:123
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:515
PerThreadContext
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:79
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:630
submit_packet
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
Definition: pthread_frame.c:460
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:1231
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:1009
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:609
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:738
FrameThreadContext::async_lock
int async_lock
Definition: pthread_frame.c:132
PerThreadContext::output_cond
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:87
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:995
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
AVCodecContext::bits_per_raw_sample
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1511
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFCodec::flush
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec_internal.h:246
decode.h
update_context_from_user
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
Definition: pthread_frame.c:404
PerThreadContext::hwaccel_threadsafe
int hwaccel_threadsafe
Definition: pthread_frame.c:111
INITIALIZED
@ INITIALIZED
Thread has been properly set up.
Definition: pthread_frame.c:73
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:121
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:445
arg
const char * arg
Definition: jacosubdec.c:67
pthread_create
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:80
if
if(ret)
Definition: filter_design.txt:179
PerThreadContext::got_frame
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:97
STATE_INPUT_READY
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:53
threadframe.h
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1033
av_buffer_unref
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
Definition: buffer.c:139
ff_thread_await_progress
void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread_frame.c:632
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:909
thread_get_buffer_internal
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
Definition: pthread_frame.c:972
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:191
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:470
PerThreadContext::progress_mutex
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:90
FrameThreadContext::async_mutex
pthread_mutex_t async_mutex
Definition: pthread_frame.c:130
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:653
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:129
PerThreadContext::avctx
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:92
pthread_internal.h
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1719
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:449
FF_DEBUG_BUFFERS
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1394
av_packet_ref
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:430
AVCodecContext::level
int level
level
Definition: avcodec.h:1726
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
FF_DEBUG_THREADS
#define FF_DEBUG_THREADS
Definition: avcodec.h:1395
OFF
#define OFF(member)
Definition: pthread_frame.c:724
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:79
AVCodecInternal::last_pkt_props
AVPacket * last_pkt_props
Properties (timestamps+side data) extracted from the last packet passed for decoding.
Definition: internal.h:90
async_lock
static void async_lock(FrameThreadContext *fctx)
Definition: pthread_frame.c:155
PerThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:84
PerThreadContext::thread
pthread_t thread
Definition: pthread_frame.c:82
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:206
attribute_align_arg
#define attribute_align_arg
Definition: internal.h:58
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:557
PerThreadContext::result
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:98
FrameThreadContext::stash_hwaccel
const AVHWAccel * stash_hwaccel
Definition: pthread_frame.c:145
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:85
f
f
Definition: af_crystalizer.c:122
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:522
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1506
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:524
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:187
codec_internal.h
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:137
FFCodec::cb
union FFCodec::@51 cb
cpu.h
PerThreadContext::avpkt
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:94
async_unlock
static void async_unlock(FrameThreadContext *fctx)
Definition: pthread_frame.c:164
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1074
FrameThreadContext::async_cond
pthread_cond_t async_cond
Definition: pthread_frame.c:131
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:69
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:325
frame.h
buffer.h
AVPacket::dts
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:373
av_reallocp_array
int av_reallocp_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate an array through a pointer to a pointer.
Definition: mem.c:223
PerThreadContext::parent
struct FrameThreadContext * parent
Definition: pthread_frame.c:80
av_packet_alloc
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:62
av_buffer_alloc
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:77
AVCodecContext::skip_loop_filter
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1734
pthread_t
Definition: os2threads.h:44
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1537
ff_thread_release_buffer
void ff_thread_release_buffer(AVCodecContext *avctx, AVFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread_frame.c:1034
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1504
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:136
av_packet_copy_props
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:385
PerThreadContext::thread_init
int thread_init
Definition: pthread_frame.c:83
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:1886
STATE_SETTING_UP
@ STATE_SETTING_UP
Definition: pthread_frame.c:55
internal.h
STATE_SETUP_FINISHED
@ STATE_SETUP_FINISHED
Definition: pthread_frame.c:67
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:1972
atomic_store_explicit
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
HWACCEL_CAP_ASYNC_SAFE
#define HWACCEL_CAP_ASYNC_SAFE
Definition: hwconfig.h:26
FFMIN
#define FFMIN(a, b)
Definition: macros.h:49
av_frame_move_ref
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c: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
STATE_GET_FORMAT
@ STATE_GET_FORMAT
Set when the codec calls get_format().
Definition: pthread_frame.c:65
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:1480
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:1040
pthread_cond_t
Definition: os2threads.h:58
AVCodecContext::height
int height
Definition: avcodec.h:615
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:652
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:1003
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:1922
avcodec.h
AVCodecContext::frame_num
int64_t frame_num
Frame counter, set by libavcodec.
Definition: avcodec.h:2100
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:872
PerThreadContext::async_serializing
int async_serializing
Definition: pthread_frame.c:105
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:477
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:677
UNINITIALIZED
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
Definition: pthread_frame.c:71
AVCodecContext
main external API structure.
Definition: avcodec.h:435
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1545
PerThreadContext::hwaccel_serializing
int hwaccel_serializing
Definition: pthread_frame.c:104
ThreadFrame
Definition: threadframe.h:27
PerThreadContext::frame
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:96
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1589
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:573
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:2039
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:1382
av_channel_layout_copy
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
Definition: channel_layout.c:639
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:80
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:630
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:137
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:462
AVPacket
This structure stores compressed data.
Definition: packet.h:351
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
AVCodecInternal::thread_ctx
void * thread_ctx
Definition: internal.h:73
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:615
AVCodecContext::frame_number
attribute_deprecated int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1100
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:561
PerThreadContext::mutex
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:89
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1792
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
snprintf
#define snprintf
Definition: snprintf.h:34
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1132
AVCodecContext::sample_aspect_ratio
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel.
Definition: avcodec.h:816
FrameThreadContext::stash_hwaccel_priv
void * stash_hwaccel_priv
Definition: pthread_frame.c:147
mutex
static AVMutex mutex
Definition: log.c:46
PerThreadContext::progress_cond
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:86
FrameThreadContext::threads
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:120
update_context_from_thread
static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
Update the next thread's AVCodecContext with values from the reference thread's context.
Definition: pthread_frame.c:276
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:75
ff_thread_setname
static int ff_thread_setname(const char *name)
Definition: thread.h:195