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 "hwconfig.h"
33 #include "internal.h"
34 #include "pthread_internal.h"
35 #include "thread.h"
36 #include "threadframe.h"
37 #include "version_major.h"
38 
39 #include "libavutil/avassert.h"
40 #include "libavutil/buffer.h"
41 #include "libavutil/common.h"
42 #include "libavutil/cpu.h"
43 #include "libavutil/frame.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/log.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/thread.h"
49 
50 enum {
51  ///< Set when the thread is awaiting a packet.
53  ///< Set before the codec has called ff_thread_finish_setup().
55  /**
56  * Set when the codec calls get_buffer().
57  * State is returned to STATE_SETTING_UP afterwards.
58  */
60  /**
61  * Set when the codec calls get_format().
62  * State is returned to STATE_SETTING_UP afterwards.
63  */
65  ///< Set after the codec has called ff_thread_finish_setup().
67 };
68 
69 enum {
70  UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
71  NEEDS_CLOSE, ///< FFCodec->close needs to be called
72  INITIALIZED, ///< Thread has been properly set up
73 };
74 
75 /**
76  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
77  */
78 typedef struct PerThreadContext {
80 
83  unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
84  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
85  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
86  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
87 
88  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
89  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
90 
91  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
92 
93  AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
94 
95  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
96  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
97  int result; ///< The result of the last codec decode/encode() call.
98 
100 
101 #if FF_API_THREAD_SAFE_CALLBACKS
102  /**
103  * Array of frames passed to ff_thread_release_buffer().
104  * Frames are released after all threads referencing them are finished.
105  */
109 
110  AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
111  int requested_flags; ///< flags passed to get_buffer() for requested_frame
112 
113  const enum AVPixelFormat *available_formats; ///< Format array for get_format()
114  enum AVPixelFormat result_format; ///< get_format() result
115 #endif
116 
117  int die; ///< Set when the thread should exit.
118 
121 
122  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
124 
125 /**
126  * Context stored in the client AVCodecInternal thread_ctx.
127  */
128 typedef struct FrameThreadContext {
129  PerThreadContext *threads; ///< The contexts for each thread.
130  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
131 
132  unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
133  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
134  /**
135  * This lock is used for ensuring threads run in serial when hwaccel
136  * is used.
137  */
142 
143  int next_decoding; ///< The next context to submit a packet to.
144  int next_finished; ///< The next context to return output from.
145 
146  int delaying; /**<
147  * Set for the first N packets, where N is the number of threads.
148  * While it is set, ff_thread_en/decode_frame won't return any results.
149  */
151 
152 #if FF_API_THREAD_SAFE_CALLBACKS
153 #define THREAD_SAFE_CALLBACKS(avctx) \
154 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
155 #endif
156 
157 static void async_lock(FrameThreadContext *fctx)
158 {
160  while (fctx->async_lock)
161  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
162  fctx->async_lock = 1;
164 }
165 
167 {
169  av_assert0(fctx->async_lock);
170  fctx->async_lock = 0;
173 }
174 
175 /**
176  * Codec worker thread.
177  *
178  * Automatically calls ff_thread_finish_setup() if the codec does
179  * not provide an update_thread_context method, or if the codec returns
180  * before calling it.
181  */
182 static attribute_align_arg void *frame_worker_thread(void *arg)
183 {
184  PerThreadContext *p = arg;
185  AVCodecContext *avctx = p->avctx;
186  const FFCodec *codec = ffcodec(avctx->codec);
187 
189  while (1) {
190  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
192 
193  if (p->die) break;
194 
196  if (!codec->update_thread_context
198  && THREAD_SAFE_CALLBACKS(avctx)
199 #endif
200  )
201  ff_thread_finish_setup(avctx);
203 
204  /* If a decoder supports hwaccel, then it must call ff_get_format().
205  * Since that call must happen before ff_thread_finish_setup(), the
206  * decoder is required to implement update_thread_context() and call
207  * ff_thread_finish_setup() manually. Therefore the above
208  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
209  * cannot be true here. */
211 
212  /* if the previous thread uses hwaccel then we take the lock to ensure
213  * the threads don't run concurrently */
214  if (avctx->hwaccel) {
216  p->hwaccel_serializing = 1;
217  }
218 
219  av_frame_unref(p->frame);
220  p->got_frame = 0;
221  p->result = codec->cb.decode(avctx, p->frame, &p->got_frame, p->avpkt);
222 
223  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0])
224  ff_thread_release_buffer(avctx, p->frame);
225 
226  if (atomic_load(&p->state) == STATE_SETTING_UP)
227  ff_thread_finish_setup(avctx);
228 
229  if (p->hwaccel_serializing) {
230  p->hwaccel_serializing = 0;
232  }
233 
234  if (p->async_serializing) {
235  p->async_serializing = 0;
236 
237  async_unlock(p->parent);
238  }
239 
241 
243 
247  }
249 
250  return NULL;
251 }
252 
253 /**
254  * Update the next thread's AVCodecContext with values from the reference thread's context.
255  *
256  * @param dst The destination context.
257  * @param src The source context.
258  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
259  * @return 0 on success, negative error code on failure
260  */
262 {
263  const FFCodec *const codec = ffcodec(dst->codec);
264  int err = 0;
265 
266  if (dst != src && (for_user || codec->update_thread_context)) {
267  dst->time_base = src->time_base;
268  dst->framerate = src->framerate;
269  dst->width = src->width;
270  dst->height = src->height;
271  dst->pix_fmt = src->pix_fmt;
272  dst->sw_pix_fmt = src->sw_pix_fmt;
273 
274  dst->coded_width = src->coded_width;
275  dst->coded_height = src->coded_height;
276 
277  dst->has_b_frames = src->has_b_frames;
278  dst->idct_algo = src->idct_algo;
279  dst->properties = src->properties;
280 
281  dst->bits_per_coded_sample = src->bits_per_coded_sample;
282  dst->sample_aspect_ratio = src->sample_aspect_ratio;
283 
284  dst->profile = src->profile;
285  dst->level = src->level;
286 
287  dst->bits_per_raw_sample = src->bits_per_raw_sample;
288  dst->ticks_per_frame = src->ticks_per_frame;
289  dst->color_primaries = src->color_primaries;
290 
291  dst->color_trc = src->color_trc;
292  dst->colorspace = src->colorspace;
293  dst->color_range = src->color_range;
294  dst->chroma_sample_location = src->chroma_sample_location;
295 
296  dst->hwaccel = src->hwaccel;
297  dst->hwaccel_context = src->hwaccel_context;
298 
299  dst->sample_rate = src->sample_rate;
300  dst->sample_fmt = src->sample_fmt;
301 #if FF_API_OLD_CHANNEL_LAYOUT
303  dst->channels = src->channels;
304  dst->channel_layout = src->channel_layout;
306 #endif
307  err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
308  if (err < 0)
309  return err;
310 
311  dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
312 
313  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
314  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
316 
317  if (src->hw_frames_ctx) {
318  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
319  if (!dst->hw_frames_ctx)
320  return AVERROR(ENOMEM);
321  }
322  }
323 
324  dst->hwaccel_flags = src->hwaccel_flags;
325 
326  err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
327  if (err < 0)
328  return err;
329  }
330 
331  if (for_user) {
333  err = codec->update_thread_context_for_user(dst, src);
334  } else {
335  if (codec->update_thread_context)
336  err = codec->update_thread_context(dst, src);
337  }
338 
339  return err;
340 }
341 
342 /**
343  * Update the next thread's AVCodecContext with values set by the user.
344  *
345  * @param dst The destination context.
346  * @param src The source context.
347  * @return 0 on success, negative error code on failure
348  */
350 {
351  dst->flags = src->flags;
352 
353  dst->draw_horiz_band= src->draw_horiz_band;
354  dst->get_buffer2 = src->get_buffer2;
355 
356  dst->opaque = src->opaque;
357  dst->debug = src->debug;
358 
359  dst->slice_flags = src->slice_flags;
360  dst->flags2 = src->flags2;
361  dst->export_side_data = src->export_side_data;
362 
363  dst->skip_loop_filter = src->skip_loop_filter;
364  dst->skip_idct = src->skip_idct;
365  dst->skip_frame = src->skip_frame;
366 
367  dst->frame_number = src->frame_number;
368  dst->reordered_opaque = src->reordered_opaque;
369 #if FF_API_THREAD_SAFE_CALLBACKS
371  dst->thread_safe_callbacks = src->thread_safe_callbacks;
373 #endif
374 
375  if (src->slice_count && src->slice_offset) {
376  if (dst->slice_count < src->slice_count) {
377  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
378  sizeof(*dst->slice_offset));
379  if (err < 0)
380  return err;
381  }
382  memcpy(dst->slice_offset, src->slice_offset,
383  src->slice_count * sizeof(*dst->slice_offset));
384  }
385  dst->slice_count = src->slice_count;
386  return 0;
387 }
388 
389 #if FF_API_THREAD_SAFE_CALLBACKS
390 /// Releases the buffers that this decoding thread was the last user of.
392 {
393  FrameThreadContext *fctx = p->parent;
394 
395  while (p->num_released_buffers > 0) {
396  AVFrame *f;
397 
399 
400  // fix extended data in case the caller screwed it up
404  f->extended_data = f->data;
405  av_frame_unref(f);
406 
408  }
409 }
410 #endif
411 
412 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
413  AVPacket *avpkt)
414 {
415  FrameThreadContext *fctx = p->parent;
416  PerThreadContext *prev_thread = fctx->prev_thread;
417  const AVCodec *codec = p->avctx->codec;
418  int ret;
419 
420  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
421  return 0;
422 
424 
425  ret = update_context_from_user(p->avctx, user_avctx);
426  if (ret) {
428  return ret;
429  }
431  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
432  memory_order_relaxed);
433 
434 #if FF_API_THREAD_SAFE_CALLBACKS
436 #endif
437 
438  if (prev_thread) {
439  int err;
440  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
441  pthread_mutex_lock(&prev_thread->progress_mutex);
442  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
443  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
444  pthread_mutex_unlock(&prev_thread->progress_mutex);
445  }
446 
447  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
448  if (err) {
450  return err;
451  }
452  }
453 
455  ret = av_packet_ref(p->avpkt, avpkt);
456  if (ret < 0) {
458  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
459  return ret;
460  }
461 
465 
466 #if FF_API_THREAD_SAFE_CALLBACKS
468  /*
469  * If the client doesn't have a thread-safe get_buffer(),
470  * then decoding threads call back to the main thread,
471  * and it calls back to the client here.
472  */
473 
474  if (!p->avctx->thread_safe_callbacks && (
478  int call_done = 1;
480  while (atomic_load(&p->state) == STATE_SETTING_UP)
482 
483  switch (atomic_load_explicit(&p->state, memory_order_acquire)) {
484  case STATE_GET_BUFFER:
486  break;
487  case STATE_GET_FORMAT:
489  break;
490  default:
491  call_done = 0;
492  break;
493  }
494  if (call_done) {
497  }
499  }
500  }
502 #endif
503 
504  fctx->prev_thread = p;
505  fctx->next_decoding++;
506 
507  return 0;
508 }
509 
511  AVFrame *picture, int *got_picture_ptr,
512  AVPacket *avpkt)
513 {
514  FrameThreadContext *fctx = avctx->internal->thread_ctx;
515  int finished = fctx->next_finished;
516  PerThreadContext *p;
517  int err;
518 
519  /* release the async lock, permitting blocked hwaccel threads to
520  * go forward while we are in this function */
521  async_unlock(fctx);
522 
523  /*
524  * Submit a packet to the next decoding thread.
525  */
526 
527  p = &fctx->threads[fctx->next_decoding];
528  err = submit_packet(p, avctx, avpkt);
529  if (err)
530  goto finish;
531 
532  /*
533  * If we're still receiving the initial packets, don't return a frame.
534  */
535 
536  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
537  fctx->delaying = 0;
538 
539  if (fctx->delaying) {
540  *got_picture_ptr=0;
541  if (avpkt->size) {
542  err = avpkt->size;
543  goto finish;
544  }
545  }
546 
547  /*
548  * Return the next available frame from the oldest thread.
549  * If we're at the end of the stream, then we have to skip threads that
550  * didn't output a frame/error, because we don't want to accidentally signal
551  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
552  */
553 
554  do {
555  p = &fctx->threads[finished++];
556 
557  if (atomic_load(&p->state) != STATE_INPUT_READY) {
559  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
562  }
563 
564  av_frame_move_ref(picture, p->frame);
565  *got_picture_ptr = p->got_frame;
566  picture->pkt_dts = p->avpkt->dts;
567  err = p->result;
568 
569  /*
570  * A later call with avkpt->size == 0 may loop over all threads,
571  * including this one, searching for a frame/error to return before being
572  * stopped by the "finished != fctx->next_finished" condition.
573  * Make sure we don't mistakenly return the same frame/error again.
574  */
575  p->got_frame = 0;
576  p->result = 0;
577 
578  if (finished >= avctx->thread_count) finished = 0;
579  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
580 
581  update_context_from_thread(avctx, p->avctx, 1);
582 
583  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
584 
585  fctx->next_finished = finished;
586 
587  /* return the size of the consumed packet if no error occurred */
588  if (err >= 0)
589  err = avpkt->size;
590 finish:
591  async_lock(fctx);
592  return err;
593 }
594 
596 {
597  PerThreadContext *p;
598  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
599 
600  if (!progress ||
601  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
602  return;
603 
604  p = f->owner[field]->internal->thread_ctx;
605 
606  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
607  av_log(f->owner[field], AV_LOG_DEBUG,
608  "%p finished %d field %d\n", progress, n, field);
609 
611 
612  atomic_store_explicit(&progress[field], n, memory_order_release);
613 
616 }
617 
619 {
620  PerThreadContext *p;
621  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
622 
623  if (!progress ||
624  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
625  return;
626 
627  p = f->owner[field]->internal->thread_ctx;
628 
629  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
630  av_log(f->owner[field], AV_LOG_DEBUG,
631  "thread awaiting %d field %d from %p\n", n, field, progress);
632 
634  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
637 }
638 
640  PerThreadContext *p = avctx->internal->thread_ctx;
641 
642  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
643 
644  if (avctx->hwaccel && !p->hwaccel_serializing) {
646  p->hwaccel_serializing = 1;
647  }
648 
649  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
650  if (avctx->hwaccel &&
652  p->async_serializing = 1;
653 
654  async_lock(p->parent);
655  }
656 
659  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
660  }
661 
663 
666 }
667 
668 /// Waits for all threads to finish.
669 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
670 {
671  int i;
672 
673  async_unlock(fctx);
674 
675  for (i = 0; i < thread_count; i++) {
676  PerThreadContext *p = &fctx->threads[i];
677 
678  if (atomic_load(&p->state) != STATE_INPUT_READY) {
680  while (atomic_load(&p->state) != STATE_INPUT_READY)
683  }
684  p->got_frame = 0;
685  }
686 
687  async_lock(fctx);
688 }
689 
690 #define OFF(member) offsetof(FrameThreadContext, member)
691 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
692  (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
693  (OFF(async_cond)));
694 #undef OFF
695 
696 #define OFF(member) offsetof(PerThreadContext, member)
697 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
698  (OFF(progress_mutex), OFF(mutex)),
699  (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
700 #undef OFF
701 
702 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
703 {
704  FrameThreadContext *fctx = avctx->internal->thread_ctx;
705  const FFCodec *codec = ffcodec(avctx->codec);
706  int i;
707 
708  park_frame_worker_threads(fctx, thread_count);
709 
710  if (fctx->prev_thread && avctx->internal->hwaccel_priv_data !=
712  if (update_context_from_thread(avctx, fctx->prev_thread->avctx, 1) < 0) {
713  av_log(avctx, AV_LOG_ERROR, "Failed to update user thread.\n");
714  }
715  }
716 
717  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
718  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
719  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
721  fctx->threads->avctx->internal->is_copy = 1;
722  }
723 
724  for (i = 0; i < thread_count; i++) {
725  PerThreadContext *p = &fctx->threads[i];
726  AVCodecContext *ctx = p->avctx;
727 
728  if (ctx->internal) {
729  if (p->thread_init == INITIALIZED) {
731  p->die = 1;
734 
735  pthread_join(p->thread, NULL);
736  }
737  if (codec->close && p->thread_init != UNINITIALIZED)
738  codec->close(ctx);
739 
740 #if FF_API_THREAD_SAFE_CALLBACKS
742  for (int j = 0; j < p->released_buffers_allocated; j++)
745 #endif
746  if (ctx->priv_data) {
747  if (codec->p.priv_class)
750  }
751 
752  av_freep(&ctx->slice_offset);
753 
754  av_buffer_unref(&ctx->internal->pool);
755  av_freep(&ctx->internal);
756  av_buffer_unref(&ctx->hw_frames_ctx);
757  }
758 
759  av_frame_free(&p->frame);
760 
761  ff_pthread_free(p, per_thread_offsets);
762  av_packet_free(&p->avpkt);
763 
764  av_freep(&p->avctx);
765  }
766 
767  av_freep(&fctx->threads);
768  ff_pthread_free(fctx, thread_ctx_offsets);
769 
770  av_freep(&avctx->internal->thread_ctx);
771 }
772 
773 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
774  FrameThreadContext *fctx, AVCodecContext *avctx,
775  const FFCodec *codec, int first)
776 {
778  int err;
779 
781 
782  copy = av_memdup(avctx, sizeof(*avctx));
783  if (!copy)
784  return AVERROR(ENOMEM);
785  copy->priv_data = NULL;
786 
787  /* From now on, this PerThreadContext will be cleaned up by
788  * ff_frame_thread_free in case of errors. */
789  (*threads_to_free)++;
790 
791  p->parent = fctx;
792  p->avctx = copy;
793 
794  copy->internal = av_mallocz(sizeof(*copy->internal));
795  if (!copy->internal)
796  return AVERROR(ENOMEM);
797  copy->internal->thread_ctx = p;
798 
799  copy->delay = avctx->delay;
800 
801  if (codec->priv_data_size) {
802  copy->priv_data = av_mallocz(codec->priv_data_size);
803  if (!copy->priv_data)
804  return AVERROR(ENOMEM);
805 
806  if (codec->p.priv_class) {
807  *(const AVClass **)copy->priv_data = codec->p.priv_class;
808  err = av_opt_copy(copy->priv_data, avctx->priv_data);
809  if (err < 0)
810  return err;
811  }
812  }
813 
814  err = ff_pthread_init(p, per_thread_offsets);
815  if (err < 0)
816  return err;
817 
818  if (!(p->frame = av_frame_alloc()) ||
819  !(p->avpkt = av_packet_alloc()))
820  return AVERROR(ENOMEM);
821  copy->internal->last_pkt_props = p->avpkt;
822 
823  if (!first)
824  copy->internal->is_copy = 1;
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 FF_API_THREAD_SAFE_CALLBACKS
934 #endif
935 
936  if (ffcodec(avctx->codec)->flush)
937  ffcodec(avctx->codec)->flush(p->avctx);
938  }
939 }
940 
942 {
943  PerThreadContext *p = avctx->internal->thread_ctx;
948  || !THREAD_SAFE_CALLBACKS(avctx)
949 #endif
950  )) {
951  return 0;
952  }
954  return 1;
955 }
956 
958 {
959  PerThreadContext *p;
960  int err;
961 
962  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
963  return ff_get_buffer(avctx, f, flags);
964 
965  p = avctx->internal->thread_ctx;
967  if (atomic_load(&p->state) != STATE_SETTING_UP &&
970  || !THREAD_SAFE_CALLBACKS(avctx)
971 #endif
972  )) {
974  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
975  return -1;
976  }
977 
979 #if !FF_API_THREAD_SAFE_CALLBACKS
980  err = ff_get_buffer(avctx, f->f, flags);
981 #else
983  if (THREAD_SAFE_CALLBACKS(avctx)) {
984  err = ff_get_buffer(avctx, f, flags);
985  } else {
987  p->requested_frame = f;
988  p->requested_flags = flags;
989  atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
991 
992  while (atomic_load(&p->state) != STATE_SETTING_UP)
994 
995  err = p->result;
996 
998 
999  }
1000  if (!THREAD_SAFE_CALLBACKS(avctx) && !ffcodec(avctx->codec)->update_thread_context)
1001  ff_thread_finish_setup(avctx);
1003 #endif
1004 
1006 
1007  return err;
1008 }
1009 
1010 #if FF_API_THREAD_SAFE_CALLBACKS
1013 {
1014  enum AVPixelFormat res;
1015  PerThreadContext *p;
1016  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
1018  return ff_get_format(avctx, fmt);
1019 
1020  p = avctx->internal->thread_ctx;
1021  if (atomic_load(&p->state) != STATE_SETTING_UP) {
1022  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
1023  return -1;
1024  }
1026  p->available_formats = fmt;
1029 
1030  while (atomic_load(&p->state) != STATE_SETTING_UP)
1032 
1033  res = p->result_format;
1034 
1036 
1037  return res;
1038 }
1040 #endif
1041 
1043 {
1044  int ret = thread_get_buffer_internal(avctx, f, flags);
1045  if (ret < 0)
1046  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1047  return ret;
1048 }
1049 
1051 {
1052  int ret;
1053 
1054  f->owner[0] = f->owner[1] = avctx;
1055  /* Hint: It is possible for this function to be called with codecs
1056  * that don't support frame threading at all, namely in case
1057  * a frame-threaded decoder shares code with codecs that are not.
1058  * This currently affects non-MPEG-4 mpegvideo codecs and and VP7.
1059  * The following check will always be true for them. */
1060  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1061  return ff_get_buffer(avctx, f->f, flags);
1062 
1064  atomic_int *progress;
1065  f->progress = av_buffer_alloc(2 * sizeof(*progress));
1066  if (!f->progress) {
1067  return AVERROR(ENOMEM);
1068  }
1069  progress = (atomic_int*)f->progress->data;
1070 
1071  atomic_init(&progress[0], -1);
1072  atomic_init(&progress[1], -1);
1073  }
1074 
1075  ret = ff_thread_get_buffer(avctx, f->f, flags);
1076  if (ret)
1077  av_buffer_unref(&f->progress);
1078  return ret;
1079 }
1080 
1082 {
1083 #if FF_API_THREAD_SAFE_CALLBACKS
1085  PerThreadContext *p;
1086  FrameThreadContext *fctx;
1087  AVFrame *dst;
1088  int ret = 0;
1089  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1090  THREAD_SAFE_CALLBACKS(avctx);
1092 #endif
1093 
1094  if (!f)
1095  return;
1096 
1097  if (avctx->debug & FF_DEBUG_BUFFERS)
1098  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1099 
1100 #if !FF_API_THREAD_SAFE_CALLBACKS
1101  av_frame_unref(f->f);
1102 #else
1103  // when the frame buffers are not allocated, just reset it to clean state
1104  if (can_direct_free || !f->buf[0]) {
1105  av_frame_unref(f);
1106  return;
1107  }
1108 
1109  p = avctx->internal->thread_ctx;
1110  fctx = p->parent;
1112 
1113  if (p->num_released_buffers == p->released_buffers_allocated) {
1114  AVFrame **tmp = av_realloc_array(p->released_buffers, p->released_buffers_allocated + 1,
1115  sizeof(*p->released_buffers));
1116  if (tmp) {
1117  tmp[p->released_buffers_allocated] = av_frame_alloc();
1118  p->released_buffers = tmp;
1119  }
1120 
1121  if (!tmp || !tmp[p->released_buffers_allocated]) {
1122  ret = AVERROR(ENOMEM);
1123  goto fail;
1124  }
1125  p->released_buffers_allocated++;
1126  }
1127 
1128  dst = p->released_buffers[p->num_released_buffers];
1129  av_frame_move_ref(dst, f);
1130 
1131  p->num_released_buffers++;
1132 
1133 fail:
1135 
1136  // make sure the frame is clean even if we fail to free it
1137  // this leaks, but it is better than crashing
1138  if (ret < 0) {
1139  av_log(avctx, AV_LOG_ERROR, "Could not queue a frame for freeing, this will leak\n");
1140  memset(f->buf, 0, sizeof(f->buf));
1141  if (f->extended_buf)
1142  memset(f->extended_buf, 0, f->nb_extended_buf * sizeof(*f->extended_buf));
1143  av_frame_unref(f);
1144  }
1145 #endif
1146 }
1147 
1149 {
1150  av_buffer_unref(&f->progress);
1151  f->owner[0] = f->owner[1] = NULL;
1152  ff_thread_release_buffer(avctx, f->f);
1153 }
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:142
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:1379
AVCodec
AVCodec.
Definition: codec.h:196
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
AVCodecContext::hwaccel_context
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:1390
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
AVPixelFormat
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
PerThreadContext::input_cond
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:84
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:39
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
AVCodecContext::get_format
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Callback to negotiate the pixel format.
Definition: avcodec.h:653
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:966
ff_thread_release_ext_buffer
void ff_thread_release_ext_buffer(AVCodecContext *avctx, ThreadFrame *f)
Unref a ThreadFrame.
Definition: pthread_frame.c:1148
ff_get_format
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1100
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:998
STATE_GET_BUFFER
@ STATE_GET_BUFFER
Set when the codec calls get_buffer().
Definition: pthread_frame.c:59
PerThreadContext::debug_threads
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
Definition: pthread_frame.c:122
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:228
FrameThreadContext::next_decoding
int next_decoding
The next context to submit a packet to.
Definition: pthread_frame.c:143
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:773
AVHWAccel::caps_internal
int caps_internal
Internal hwaccel capabilities.
Definition: avcodec.h:2205
ff_thread_can_start_frame
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread_frame.c:941
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
PerThreadContext::state
atomic_int state
Definition: pthread_frame.c:99
STATE_GET_FORMAT
@ STATE_GET_FORMAT
Set when the codec calls get_format().
Definition: pthread_frame.c:64
FrameThreadContext
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:128
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:111
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:669
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:325
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
THREAD_SAFE_CALLBACKS
#define THREAD_SAFE_CALLBACKS(avctx)
Definition: pthread_frame.c:153
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:959
AVCodecContext::slice_offset
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:750
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:215
UNINITIALIZED
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
Definition: pthread_frame.c:70
internal.h
version_major.h
atomic_int
intptr_t atomic_int
Definition: stdatomic.h:55
FFCodec
Definition: codec_internal.h:112
av_buffer_ref
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:103
avcodec_default_get_format
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: decode.c:888
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:525
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:545
PerThreadContext::die
int die
Set when the thread should exit.
Definition: pthread_frame.c:117
thread.h
ff_pthread_free
av_cold void ff_pthread_free(void *obj, const unsigned offsets[])
Definition: pthread.c:94
FrameThreadContext::next_finished
int next_finished
The next context to return output from.
Definition: pthread_frame.c:144
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
ff_thread_get_buffer
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:1042
AVCodecInternal::pool
AVBufferRef * pool
Definition: internal.h:61
AVCodecContext::slice_count
int slice_count
slice count
Definition: avcodec.h:743
FrameThreadContext::buffer_mutex
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread_frame.c:133
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:637
FFCodec::cb
union FFCodec::@43 cb
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:312
FFCodec::priv_data_size
int priv_data_size
Definition: codec_internal.h:130
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:1733
AVCodecInternal::is_copy
int is_copy
When using frame-threaded decoding, this field is set for the first worker thread (e....
Definition: internal.h:53
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:702
AVCodecContext::skip_idct
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:1689
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:116
finish
static void finish(void)
Definition: movenc.c:342
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:398
AVCodecContext::ch_layout
AVChannelLayout ch_layout
Audio channel layout.
Definition: avcodec.h:2057
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:1696
fail
#define fail()
Definition: checkasm.h:130
PerThreadContext::available_formats
enum AVPixelFormat * available_formats
Format array for get_format()
Definition: pthread_frame.c:113
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1463
FrameThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:132
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:469
PerThreadContext
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:78
FF_API_THREAD_SAFE_CALLBACKS
#define FF_API_THREAD_SAFE_CALLBACKS
Definition: version_major.h:43
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:577
submit_packet
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
Definition: pthread_frame.c:412
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:99
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:1167
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:952
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_await_progress
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread_frame.c:618
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:595
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:147
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:685
FrameThreadContext::async_lock
int async_lock
Definition: pthread_frame.c:141
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:225
PerThreadContext::output_cond
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:86
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
AVCodecContext::ticks_per_frame
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:521
PerThreadContext::requested_flags
int requested_flags
flags passed to get_buffer() for requested_frame
Definition: pthread_frame.c:111
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:1448
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:231
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:349
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:130
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:178
FFCodec::init
int(* init)(struct AVCodecContext *)
Definition: codec_internal.h:163
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:399
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:96
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:973
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
STATE_INPUT_READY
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:52
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:852
thread_get_buffer_internal
static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
Definition: pthread_frame.c:957
AVCodec::type
enum AVMediaType type
Definition: codec.h:209
frame_worker_thread
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
Definition: pthread_frame.c:182
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:424
PerThreadContext::released_buffers
AVFrame ** released_buffers
Array of frames passed to ff_thread_release_buffer().
Definition: pthread_frame.c:106
PerThreadContext::progress_mutex
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:89
FrameThreadContext::async_mutex
pthread_mutex_t async_mutex
Definition: pthread_frame.c:139
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:639
FrameThreadContext::hwaccel_mutex
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when hwaccel is used.
Definition: pthread_frame.c:138
PerThreadContext::avctx
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:91
pthread_internal.h
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1718
AVCodecContext::thread_safe_callbacks
attribute_deprecated int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
Definition: avcodec.h:1502
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:439
FF_DEBUG_BUFFERS
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1334
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:1674
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
FF_DEBUG_THREADS
#define FF_DEBUG_THREADS
Definition: avcodec.h:1335
OFF
#define OFF(member)
Definition: pthread_frame.c:696
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:77
async_lock
static void async_lock(FrameThreadContext *fctx)
Definition: pthread_frame.c:157
PerThreadContext::pthread_init_cnt
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:83
PerThreadContext::thread
pthread_t thread
Definition: pthread_frame.c:81
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:194
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:512
PerThreadContext::result
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:97
STATE_SETUP_FINISHED
@ STATE_SETUP_FINISHED
Definition: pthread_frame.c:66
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:83
f
f
Definition: af_crystalizer.c:122
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:476
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1403
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:510
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:120
cpu.h
PerThreadContext::avpkt
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:93
async_unlock
static void async_unlock(FrameThreadContext *fctx)
Definition: pthread_frame.c:166
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1014
FrameThreadContext::async_cond
pthread_cond_t async_cond
Definition: pthread_frame.c:140
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: codec_internal.h:66
PerThreadContext::num_released_buffers
int num_released_buffers
Definition: pthread_frame.c:107
ffcodec
static const av_always_inline FFCodec * ffcodec(const AVCodec *codec)
Definition: codec_internal.h:273
STATE_SETTING_UP
@ STATE_SETTING_UP
Definition: pthread_frame.c:54
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, or free an array through a pointer to a pointer.
Definition: mem.c:233
PerThreadContext::parent
struct FrameThreadContext * parent
Definition: pthread_frame.c:79
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:1682
pthread_t
Definition: os2threads.h:44
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1474
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:1081
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1441
avcodec_default_get_buffer2
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: get_buffer.c:290
FFCodec::caps_internal
unsigned caps_internal
Internal codec capabilities FF_CODEC_CAP_*.
Definition: codec_internal.h:121
PerThreadContext::thread_init
int thread_init
Definition: pthread_frame.c:82
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:271
AVCodecContext::properties
unsigned properties
Properties of the stream that gets decoded.
Definition: avcodec.h:1845
internal.h
INITIALIZED
@ INITIALIZED
Thread has been properly set up.
Definition: pthread_frame.c:72
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:1940
atomic_store_explicit
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
release_delayed_buffers
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
Definition: pthread_frame.c:391
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:506
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:477
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:264
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1417
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:980
NEEDS_CLOSE
@ NEEDS_CLOSE
FFCodec->close needs to be called.
Definition: pthread_frame.c:71
pthread_cond_t
Definition: os2threads.h:58
AVCodecContext::height
int height
Definition: avcodec.h:562
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:599
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:1050
av_calloc
void * av_calloc(size_t nmemb, size_t size)
Definition: mem.c:272
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:1881
avcodec.h
DEFINE_OFFSET_ARRAY
DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,(OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),(OFF(async_cond)))
ret
ret
Definition: filter_design.txt:187
ff_frame_thread_init
int ff_frame_thread_init(AVCodecContext *avctx)
Definition: pthread_frame.c:849
PerThreadContext::async_serializing
int async_serializing
Definition: pthread_frame.c:120
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:431
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:624
AVCodecContext
main external API structure.
Definition: avcodec.h:389
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1482
PerThreadContext::hwaccel_serializing
int hwaccel_serializing
Definition: pthread_frame.c:119
ThreadFrame
Definition: threadframe.h:27
PerThreadContext::frame
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:95
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1548
ff_thread_get_format
FF_DISABLE_DEPRECATION_WARNINGS enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
Definition: pthread_frame.c:1012
PerThreadContext::result_format
enum AVPixelFormat result_format
get_format() result
Definition: pthread_frame.c:114
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:2007
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:82
FFCodec::close
int(* close)(struct AVCodecContext *)
Definition: codec_internal.h:225
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:107
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:1322
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:82
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:577
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:397
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:146
mem.h
AVCodecContext::frame_number
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1037
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1884
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:416
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
AVCodecContext::reordered_opaque
int64_t reordered_opaque
opaque 64-bit number (generally a PTS) that will be reordered and output in AVFrame....
Definition: avcodec.h:1372
AVCodecInternal::thread_ctx
void * thread_ctx
Definition: internal.h:63
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:562
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:88
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
PerThreadContext::requested_frame
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
Definition: pthread_frame.c:110
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1740
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1241
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:759
mutex
static AVMutex mutex
Definition: log.c:46
PerThreadContext::released_buffers_allocated
int released_buffers_allocated
Definition: pthread_frame.c:108
PerThreadContext::progress_cond
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:85
FrameThreadContext::threads
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:129
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:261
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:73