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 "hwconfig.h"
32 #include "internal.h"
33 #include "pthread_internal.h"
34 #include "thread.h"
35 #include "version.h"
36 
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/common.h"
40 #include "libavutil/cpu.h"
41 #include "libavutil/frame.h"
42 #include "libavutil/internal.h"
43 #include "libavutil/log.h"
44 #include "libavutil/mem.h"
45 #include "libavutil/opt.h"
46 #include "libavutil/thread.h"
47 
48 enum {
49  ///< Set when the thread is awaiting a packet.
51  ///< Set before the codec has called ff_thread_finish_setup().
53  /**
54  * Set when the codec calls get_buffer().
55  * State is returned to STATE_SETTING_UP afterwards.
56  */
58  /**
59  * Set when the codec calls get_format().
60  * State is returned to STATE_SETTING_UP afterwards.
61  */
63  ///< Set after the codec has called ff_thread_finish_setup().
65 };
66 
67 /**
68  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
69  */
70 typedef struct PerThreadContext {
72 
75  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
76  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
77  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
78 
79  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
80  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
81 
82  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
83 
84  AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
85 
86  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
87  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
88  int result; ///< The result of the last codec decode/encode() call.
89 
91 
92  /**
93  * Array of frames passed to ff_thread_release_buffer().
94  * Frames are released after all threads referencing them are finished.
95  */
99 
100  AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
101  int requested_flags; ///< flags passed to get_buffer() for requested_frame
102 
103  const enum AVPixelFormat *available_formats; ///< Format array for get_format()
104  enum AVPixelFormat result_format; ///< get_format() result
105 
106  int die; ///< Set when the thread should exit.
107 
110 
111  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
113 
114 /**
115  * Context stored in the client AVCodecInternal thread_ctx.
116  */
117 typedef struct FrameThreadContext {
118  PerThreadContext *threads; ///< The contexts for each thread.
119  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
120 
121  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
122  /**
123  * This lock is used for ensuring threads run in serial when hwaccel
124  * is used.
125  */
130 
131  int next_decoding; ///< The next context to submit a packet to.
132  int next_finished; ///< The next context to return output from.
133 
134  int delaying; /**<
135  * Set for the first N packets, where N is the number of threads.
136  * While it is set, ff_thread_en/decode_frame won't return any results.
137  */
139 
140 #define THREAD_SAFE_CALLBACKS(avctx) \
141 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
142 
143 static void async_lock(FrameThreadContext *fctx)
144 {
146  while (fctx->async_lock)
147  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
148  fctx->async_lock = 1;
150 }
151 
153 {
155  av_assert0(fctx->async_lock);
156  fctx->async_lock = 0;
159 }
160 
161 /**
162  * Codec worker thread.
163  *
164  * Automatically calls ff_thread_finish_setup() if the codec does
165  * not provide an update_thread_context method, or if the codec returns
166  * before calling it.
167  */
168 static attribute_align_arg void *frame_worker_thread(void *arg)
169 {
170  PerThreadContext *p = arg;
171  AVCodecContext *avctx = p->avctx;
172  const AVCodec *codec = avctx->codec;
173 
175  while (1) {
176  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
178 
179  if (p->die) break;
180 
181  if (!codec->update_thread_context && THREAD_SAFE_CALLBACKS(avctx))
182  ff_thread_finish_setup(avctx);
183 
184  /* If a decoder supports hwaccel, then it must call ff_get_format().
185  * Since that call must happen before ff_thread_finish_setup(), the
186  * decoder is required to implement update_thread_context() and call
187  * ff_thread_finish_setup() manually. Therefore the above
188  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
189  * cannot be true here. */
191 
192  /* if the previous thread uses hwaccel then we take the lock to ensure
193  * the threads don't run concurrently */
194  if (avctx->hwaccel) {
196  p->hwaccel_serializing = 1;
197  }
198 
199  av_frame_unref(p->frame);
200  p->got_frame = 0;
201  p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
202 
203  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
205  av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
206  "free the frame on failure. This is a bug, please report it.\n");
207  av_frame_unref(p->frame);
208  }
209 
210  if (atomic_load(&p->state) == STATE_SETTING_UP)
211  ff_thread_finish_setup(avctx);
212 
213  if (p->hwaccel_serializing) {
214  p->hwaccel_serializing = 0;
216  }
217 
218  if (p->async_serializing) {
219  p->async_serializing = 0;
220 
221  async_unlock(p->parent);
222  }
223 
225 
227 
231  }
233 
234  return NULL;
235 }
236 
237 /**
238  * Update the next thread's AVCodecContext with values from the reference thread's context.
239  *
240  * @param dst The destination context.
241  * @param src The source context.
242  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
243  * @return 0 on success, negative error code on failure
244  */
246 {
247  int err = 0;
248 
249  if (dst != src && (for_user || !(src->codec_descriptor->props & AV_CODEC_PROP_INTRA_ONLY))) {
250  dst->time_base = src->time_base;
251  dst->framerate = src->framerate;
252  dst->width = src->width;
253  dst->height = src->height;
254  dst->pix_fmt = src->pix_fmt;
255  dst->sw_pix_fmt = src->sw_pix_fmt;
256 
257  dst->coded_width = src->coded_width;
258  dst->coded_height = src->coded_height;
259 
260  dst->has_b_frames = src->has_b_frames;
261  dst->idct_algo = src->idct_algo;
262 
263  dst->bits_per_coded_sample = src->bits_per_coded_sample;
264  dst->sample_aspect_ratio = src->sample_aspect_ratio;
265 
266  dst->profile = src->profile;
267  dst->level = src->level;
268 
269  dst->bits_per_raw_sample = src->bits_per_raw_sample;
270  dst->ticks_per_frame = src->ticks_per_frame;
271  dst->color_primaries = src->color_primaries;
272 
273  dst->color_trc = src->color_trc;
274  dst->colorspace = src->colorspace;
275  dst->color_range = src->color_range;
276  dst->chroma_sample_location = src->chroma_sample_location;
277 
278  dst->hwaccel = src->hwaccel;
279  dst->hwaccel_context = src->hwaccel_context;
280 
281  dst->channels = src->channels;
282  dst->sample_rate = src->sample_rate;
283  dst->sample_fmt = src->sample_fmt;
284  dst->channel_layout = src->channel_layout;
285  dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
286 
287  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
288  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
290 
291  if (src->hw_frames_ctx) {
292  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
293  if (!dst->hw_frames_ctx)
294  return AVERROR(ENOMEM);
295  }
296  }
297 
298  dst->hwaccel_flags = src->hwaccel_flags;
299 
300  if (!!dst->internal->pool != !!src->internal->pool ||
301  (dst->internal->pool && dst->internal->pool->data != src->internal->pool->data)) {
302  av_buffer_unref(&dst->internal->pool);
303 
304  if (src->internal->pool) {
305  dst->internal->pool = av_buffer_ref(src->internal->pool);
306  if (!dst->internal->pool)
307  return AVERROR(ENOMEM);
308  }
309  }
310  }
311 
312  if (for_user) {
313 #if FF_API_CODED_FRAME
315  dst->coded_frame = src->coded_frame;
317 #endif
318  } else {
319  if (dst->codec->update_thread_context)
320  err = dst->codec->update_thread_context(dst, src);
321  }
322 
323  return err;
324 }
325 
326 /**
327  * Update the next thread's AVCodecContext with values set by the user.
328  *
329  * @param dst The destination context.
330  * @param src The source context.
331  * @return 0 on success, negative error code on failure
332  */
334 {
335  dst->flags = src->flags;
336 
337  dst->draw_horiz_band= src->draw_horiz_band;
338  dst->get_buffer2 = src->get_buffer2;
339 
340  dst->opaque = src->opaque;
341  dst->debug = src->debug;
342  dst->debug_mv = src->debug_mv;
343 
344  dst->slice_flags = src->slice_flags;
345  dst->flags2 = src->flags2;
346  dst->export_side_data = src->export_side_data;
347 
348  dst->skip_loop_filter = src->skip_loop_filter;
349  dst->skip_idct = src->skip_idct;
350  dst->skip_frame = src->skip_frame;
351 
352  dst->frame_number = src->frame_number;
353  dst->reordered_opaque = src->reordered_opaque;
354  dst->thread_safe_callbacks = src->thread_safe_callbacks;
355 
356  if (src->slice_count && src->slice_offset) {
357  if (dst->slice_count < src->slice_count) {
358  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
359  sizeof(*dst->slice_offset));
360  if (err < 0)
361  return err;
362  }
363  memcpy(dst->slice_offset, src->slice_offset,
364  src->slice_count * sizeof(*dst->slice_offset));
365  }
366  dst->slice_count = src->slice_count;
367  return 0;
368 }
369 
370 /// Releases the buffers that this decoding thread was the last user of.
372 {
373  FrameThreadContext *fctx = p->parent;
374 
375  while (p->num_released_buffers > 0) {
376  AVFrame *f;
377 
379 
380  // fix extended data in case the caller screwed it up
384  f->extended_data = f->data;
385  av_frame_unref(f);
386 
388  }
389 }
390 
391 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
392  AVPacket *avpkt)
393 {
394  FrameThreadContext *fctx = p->parent;
395  PerThreadContext *prev_thread = fctx->prev_thread;
396  const AVCodec *codec = p->avctx->codec;
397  int ret;
398 
399  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
400  return 0;
401 
403 
404  ret = update_context_from_user(p->avctx, user_avctx);
405  if (ret) {
407  return ret;
408  }
410  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
411  memory_order_relaxed);
412 
414 
415  if (prev_thread) {
416  int err;
417  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
418  pthread_mutex_lock(&prev_thread->progress_mutex);
419  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
420  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
421  pthread_mutex_unlock(&prev_thread->progress_mutex);
422  }
423 
424  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
425  if (err) {
427  return err;
428  }
429  }
430 
431  av_packet_unref(&p->avpkt);
432  ret = av_packet_ref(&p->avpkt, avpkt);
433  if (ret < 0) {
435  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
436  return ret;
437  }
438 
442 
443  /*
444  * If the client doesn't have a thread-safe get_buffer(),
445  * then decoding threads call back to the main thread,
446  * and it calls back to the client here.
447  */
448 
449  if (!p->avctx->thread_safe_callbacks && (
453  int call_done = 1;
455  while (atomic_load(&p->state) == STATE_SETTING_UP)
457 
458  switch (atomic_load_explicit(&p->state, memory_order_acquire)) {
459  case STATE_GET_BUFFER:
461  break;
462  case STATE_GET_FORMAT:
464  break;
465  default:
466  call_done = 0;
467  break;
468  }
469  if (call_done) {
472  }
474  }
475  }
476 
477  fctx->prev_thread = p;
478  fctx->next_decoding++;
479 
480  return 0;
481 }
482 
484  AVFrame *picture, int *got_picture_ptr,
485  AVPacket *avpkt)
486 {
487  FrameThreadContext *fctx = avctx->internal->thread_ctx;
488  int finished = fctx->next_finished;
489  PerThreadContext *p;
490  int err;
491 
492  /* release the async lock, permitting blocked hwaccel threads to
493  * go forward while we are in this function */
494  async_unlock(fctx);
495 
496  /*
497  * Submit a packet to the next decoding thread.
498  */
499 
500  p = &fctx->threads[fctx->next_decoding];
501  err = submit_packet(p, avctx, avpkt);
502  if (err)
503  goto finish;
504 
505  /*
506  * If we're still receiving the initial packets, don't return a frame.
507  */
508 
509  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
510  fctx->delaying = 0;
511 
512  if (fctx->delaying) {
513  *got_picture_ptr=0;
514  if (avpkt->size) {
515  err = avpkt->size;
516  goto finish;
517  }
518  }
519 
520  /*
521  * Return the next available frame from the oldest thread.
522  * If we're at the end of the stream, then we have to skip threads that
523  * didn't output a frame/error, because we don't want to accidentally signal
524  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
525  */
526 
527  do {
528  p = &fctx->threads[finished++];
529 
530  if (atomic_load(&p->state) != STATE_INPUT_READY) {
532  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
535  }
536 
537  av_frame_move_ref(picture, p->frame);
538  *got_picture_ptr = p->got_frame;
539  picture->pkt_dts = p->avpkt.dts;
540  err = p->result;
541 
542  /*
543  * A later call with avkpt->size == 0 may loop over all threads,
544  * including this one, searching for a frame/error to return before being
545  * stopped by the "finished != fctx->next_finished" condition.
546  * Make sure we don't mistakenly return the same frame/error again.
547  */
548  p->got_frame = 0;
549  p->result = 0;
550 
551  if (finished >= avctx->thread_count) finished = 0;
552  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
553 
554  update_context_from_thread(avctx, p->avctx, 1);
555 
556  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
557 
558  fctx->next_finished = finished;
559 
560  /* return the size of the consumed packet if no error occurred */
561  if (err >= 0)
562  err = avpkt->size;
563 finish:
564  async_lock(fctx);
565  return err;
566 }
567 
569 {
570  PerThreadContext *p;
571  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
572 
573  if (!progress ||
574  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
575  return;
576 
577  p = f->owner[field]->internal->thread_ctx;
578 
579  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
580  av_log(f->owner[field], AV_LOG_DEBUG,
581  "%p finished %d field %d\n", progress, n, field);
582 
584 
585  atomic_store_explicit(&progress[field], n, memory_order_release);
586 
589 }
590 
592 {
593  PerThreadContext *p;
594  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
595 
596  if (!progress ||
597  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
598  return;
599 
600  p = f->owner[field]->internal->thread_ctx;
601 
602  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
603  av_log(f->owner[field], AV_LOG_DEBUG,
604  "thread awaiting %d field %d from %p\n", n, field, progress);
605 
607  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
610 }
611 
613  PerThreadContext *p = avctx->internal->thread_ctx;
614 
615  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
616 
617  if (avctx->hwaccel && !p->hwaccel_serializing) {
619  p->hwaccel_serializing = 1;
620  }
621 
622  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
623  if (avctx->hwaccel &&
625  p->async_serializing = 1;
626 
627  async_lock(p->parent);
628  }
629 
632  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
633  }
634 
636 
639 }
640 
641 /// Waits for all threads to finish.
642 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
643 {
644  int i;
645 
646  async_unlock(fctx);
647 
648  for (i = 0; i < thread_count; i++) {
649  PerThreadContext *p = &fctx->threads[i];
650 
651  if (atomic_load(&p->state) != STATE_INPUT_READY) {
653  while (atomic_load(&p->state) != STATE_INPUT_READY)
656  }
657  p->got_frame = 0;
658  }
659 
660  async_lock(fctx);
661 }
662 
663 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
664 {
665  FrameThreadContext *fctx = avctx->internal->thread_ctx;
666  const AVCodec *codec = avctx->codec;
667  int i, j;
668 
669  park_frame_worker_threads(fctx, thread_count);
670 
671  if (fctx->prev_thread && avctx->internal->hwaccel_priv_data !=
673  if (update_context_from_thread(avctx, fctx->prev_thread->avctx, 1) < 0) {
674  av_log(avctx, AV_LOG_ERROR, "Failed to update user thread.\n");
675  }
676  }
677 
678  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
679  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
680  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
682  fctx->threads->avctx->internal->is_copy = 1;
683  }
684 
685  for (i = 0; i < thread_count; i++) {
686  PerThreadContext *p = &fctx->threads[i];
687 
689  p->die = 1;
692 
693  if (p->thread_init)
694  pthread_join(p->thread, NULL);
695  p->thread_init=0;
696 
697  if (codec->close && p->avctx)
698  codec->close(p->avctx);
699 
701  av_frame_free(&p->frame);
702  }
703 
704  for (i = 0; i < thread_count; i++) {
705  PerThreadContext *p = &fctx->threads[i];
706 
712  av_packet_unref(&p->avpkt);
713 
714  for (j = 0; j < p->released_buffers_allocated; j++)
717 
718  if (p->avctx) {
719  if (codec->priv_class)
721  av_freep(&p->avctx->priv_data);
722 
724  }
725 
726  if (p->avctx) {
728  av_freep(&p->avctx->internal);
730  }
731 
732  av_freep(&p->avctx);
733  }
734 
735  av_freep(&fctx->threads);
740 
741  av_freep(&avctx->internal->thread_ctx);
742 
743  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
744  av_opt_free(avctx->priv_data);
745  avctx->codec = NULL;
746 }
747 
749 {
750  int thread_count = avctx->thread_count;
751  const AVCodec *codec = avctx->codec;
752  AVCodecContext *src = avctx;
753  FrameThreadContext *fctx;
754  int i, err = 0;
755 
756  if (!thread_count) {
757  int nb_cpus = av_cpu_count();
758 #if FF_API_DEBUG_MV
759  if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
760  nb_cpus = 1;
761 #endif
762  // use number of cores + 1 as thread count if there is more than one
763  if (nb_cpus > 1)
764  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
765  else
766  thread_count = avctx->thread_count = 1;
767  }
768 
769  if (thread_count <= 1) {
770  avctx->active_thread_type = 0;
771  return 0;
772  }
773 
774  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
775  if (!fctx)
776  return AVERROR(ENOMEM);
777 
778  fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
779  if (!fctx->threads) {
780  av_freep(&avctx->internal->thread_ctx);
781  return AVERROR(ENOMEM);
782  }
783 
788 
789  fctx->async_lock = 1;
790  fctx->delaying = 1;
791 
792  if (codec->type == AVMEDIA_TYPE_VIDEO)
793  avctx->delay = src->thread_count - 1;
794 
795  for (i = 0; i < thread_count; i++) {
797  PerThreadContext *p = &fctx->threads[i];
798 
804 
805  p->frame = av_frame_alloc();
806  if (!p->frame) {
807  av_freep(&copy);
808  err = AVERROR(ENOMEM);
809  goto error;
810  }
811 
812  p->parent = fctx;
813  p->avctx = copy;
814 
815  if (!copy) {
816  err = AVERROR(ENOMEM);
817  goto error;
818  }
819 
820  *copy = *src;
821 
822  copy->internal = av_malloc(sizeof(AVCodecInternal));
823  if (!copy->internal) {
824  copy->priv_data = NULL;
825  err = AVERROR(ENOMEM);
826  goto error;
827  }
828  *copy->internal = *src->internal;
829  copy->internal->thread_ctx = p;
830  copy->internal->last_pkt_props = &p->avpkt;
831 
832  copy->delay = avctx->delay;
833 
834  if (codec->priv_data_size) {
835  copy->priv_data = av_mallocz(codec->priv_data_size);
836  if (!copy->priv_data) {
837  err = AVERROR(ENOMEM);
838  goto error;
839  }
840 
841  if (codec->priv_class) {
842  *(const AVClass **)copy->priv_data = codec->priv_class;
843  err = av_opt_copy(copy->priv_data, src->priv_data);
844  if (err < 0)
845  goto error;
846  }
847  }
848 
849  if (i)
850  copy->internal->is_copy = 1;
851 
852  if (codec->init)
853  err = codec->init(copy);
854 
855  if (err) goto error;
856 
857  if (!i)
858  update_context_from_thread(avctx, copy, 1);
859 
860  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
861 
863  p->thread_init= !err;
864  if(!p->thread_init)
865  goto error;
866  }
867 
868  return 0;
869 
870 error:
871  ff_frame_thread_free(avctx, i+1);
872 
873  return err;
874 }
875 
877 {
878  int i;
879  FrameThreadContext *fctx = avctx->internal->thread_ctx;
880 
881  if (!fctx) return;
882 
884  if (fctx->prev_thread) {
885  if (fctx->prev_thread != &fctx->threads[0])
887  }
888 
889  fctx->next_decoding = fctx->next_finished = 0;
890  fctx->delaying = 1;
891  fctx->prev_thread = NULL;
892  for (i = 0; i < avctx->thread_count; i++) {
893  PerThreadContext *p = &fctx->threads[i];
894  // Make sure decode flush calls with size=0 won't return old frames
895  p->got_frame = 0;
896  av_frame_unref(p->frame);
897  p->result = 0;
898 
900 
901  if (avctx->codec->flush)
902  avctx->codec->flush(p->avctx);
903  }
904 }
905 
907 {
908  PerThreadContext *p = avctx->internal->thread_ctx;
910  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
911  return 0;
912  }
913  return 1;
914 }
915 
917 {
918  PerThreadContext *p = avctx->internal->thread_ctx;
919  int err;
920 
921  f->owner[0] = f->owner[1] = avctx;
922 
923  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
924  return ff_get_buffer(avctx, f->f, flags);
925 
926  if (atomic_load(&p->state) != STATE_SETTING_UP &&
927  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
928  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
929  return -1;
930  }
931 
933  atomic_int *progress;
934  f->progress = av_buffer_alloc(2 * sizeof(*progress));
935  if (!f->progress) {
936  return AVERROR(ENOMEM);
937  }
938  progress = (atomic_int*)f->progress->data;
939 
940  atomic_init(&progress[0], -1);
941  atomic_init(&progress[1], -1);
942  }
943 
945  if (THREAD_SAFE_CALLBACKS(avctx)) {
946  err = ff_get_buffer(avctx, f->f, flags);
947  } else {
949  p->requested_frame = f->f;
950  p->requested_flags = flags;
951  atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
953 
954  while (atomic_load(&p->state) != STATE_SETTING_UP)
956 
957  err = p->result;
958 
960 
961  }
962  if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
963  ff_thread_finish_setup(avctx);
964  if (err)
965  av_buffer_unref(&f->progress);
966 
968 
969  return err;
970 }
971 
973 {
974  enum AVPixelFormat res;
975  PerThreadContext *p = avctx->internal->thread_ctx;
976  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
978  return ff_get_format(avctx, fmt);
979  if (atomic_load(&p->state) != STATE_SETTING_UP) {
980  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
981  return -1;
982  }
984  p->available_formats = fmt;
987 
988  while (atomic_load(&p->state) != STATE_SETTING_UP)
990 
991  res = p->result_format;
992 
994 
995  return res;
996 }
997 
999 {
1000  int ret = thread_get_buffer_internal(avctx, f, flags);
1001  if (ret < 0)
1002  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1003  return ret;
1004 }
1005 
1007 {
1008  PerThreadContext *p = avctx->internal->thread_ctx;
1009  FrameThreadContext *fctx;
1010  AVFrame *dst;
1011  int ret = 0;
1012  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1013  THREAD_SAFE_CALLBACKS(avctx);
1014 
1015  if (!f->f)
1016  return;
1017 
1018  if (avctx->debug & FF_DEBUG_BUFFERS)
1019  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1020 
1021  av_buffer_unref(&f->progress);
1022  f->owner[0] = f->owner[1] = NULL;
1023 
1024  // when the frame buffers are not allocated, just reset it to clean state
1025  if (can_direct_free || !f->f->buf[0]) {
1026  av_frame_unref(f->f);
1027  return;
1028  }
1029 
1030  fctx = p->parent;
1032 
1035  sizeof(*p->released_buffers));
1036  if (tmp) {
1038  p->released_buffers = tmp;
1039  }
1040 
1041  if (!tmp || !tmp[p->released_buffers_allocated]) {
1042  ret = AVERROR(ENOMEM);
1043  goto fail;
1044  }
1046  }
1047 
1049  av_frame_move_ref(dst, f->f);
1050 
1051  p->num_released_buffers++;
1052 
1053 fail:
1055 
1056  // make sure the frame is clean even if we fail to free it
1057  // this leaks, but it is better than crashing
1058  if (ret < 0) {
1059  av_log(avctx, AV_LOG_ERROR, "Could not queue a frame for freeing, this will leak\n");
1060  memset(f->f->buf, 0, sizeof(f->f->buf));
1061  if (f->f->extended_buf)
1062  memset(f->f->extended_buf, 0, f->f->nb_extended_buf * sizeof(*f->f->extended_buf));
1063  av_frame_unref(f->f);
1064  }
1065 }
error
static void error(const char *err)
Definition: target_bsf_fuzzer.c:29
pthread_mutex_t
_fmutex pthread_mutex_t
Definition: os2threads.h:53
hwconfig.h
av_packet_unref
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:605
AVCodecContext::hwaccel
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1690
AVCodec
AVCodec.
Definition: codec.h:190
FF_ENABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
AVCodecContext::hwaccel_context
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:1702
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:182
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:75
atomic_store
#define atomic_store(object, desired)
Definition: stdatomic.h:85
av_buffer_alloc
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
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 pixelFormat
Definition: avcodec.h:778
AVCodecContext::channel_layout
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1237
AVCodecContext::colorspace
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1154
ff_get_format
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1279
AVCodecContext::sample_rate
int sample_rate
samples per second
Definition: avcodec.h:1186
PerThreadContext::debug_threads
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
Definition: pthread_frame.c:111
AVCodecContext::thread_safe_callbacks
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
Definition: avcodec.h:1814
AVCodec::priv_class
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:216
AVCodec::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.h:251
FrameThreadContext::next_decoding
int next_decoding
The next context to submit a packet to.
Definition: pthread_frame.c:131
thread.h
AVBufferRef::data
uint8_t * data
The data buffer.
Definition: buffer.h:89
ff_thread_flush
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread_frame.c:876
pthread_mutex_init
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
AVHWAccel::caps_internal
int caps_internal
Internal hwaccel capabilities.
Definition: avcodec.h:2559
ff_thread_can_start_frame
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread_frame.c:906
MAX_AUTO_THREADS
#define MAX_AUTO_THREADS
Definition: pthread_internal.h:26
PerThreadContext::state
atomic_int state
Definition: pthread_frame.c:90
FrameThreadContext
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:117
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
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:642
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:300
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:26
THREAD_SAFE_CALLBACKS
#define THREAD_SAFE_CALLBACKS(avctx)
Definition: pthread_frame.c:140
AVCodecContext::color_trc
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1147
AVCodecContext::slice_offset
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:896
AVCodec::capabilities
int capabilities
Codec capabilities.
Definition: codec.h:209
internal.h
PerThreadContext::avpkt
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:84
atomic_int
intptr_t atomic_int
Definition: stdatomic.h:55
AVCodecContext::debug_mv
int debug_mv
debug motion vectors
Definition: avcodec.h:2157
AVCodec::decode
int(* decode)(struct AVCodecContext *, void *outdata, int *outdata_size, struct AVPacket *avpkt)
Definition: codec.h:282
version.h
av_mallocz_array
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:190
avcodec_default_get_format
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: decode.c:1067
AVCodec::flush
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec.h:305
AVFrame::buf
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:491
AVCodecContext::delay
int delay
Codec delay.
Definition: avcodec.h:682
PerThreadContext::die
int die
Set when the thread should exit.
Definition: pthread_frame.c:106
thread.h
FrameThreadContext::next_finished
int next_finished
The next context to return output from.
Definition: pthread_frame.c:132
AVCodecInternal::pool
AVBufferRef * pool
Definition: internal.h:133
AVCodecContext::slice_count
int slice_count
slice count
Definition: avcodec.h:880
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
FrameThreadContext::buffer_mutex
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread_frame.c:121
STATE_SETTING_UP
@ STATE_SETTING_UP
Definition: pthread_frame.c:52
AVCodecContext::framerate
AVRational framerate
Definition: avcodec.h:2069
AVCodecInternal::is_copy
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it.
Definition: internal.h:123
ff_frame_thread_free
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread_frame.c:663
AVCodecContext::skip_idct
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:1997
finish
static void finish(void)
Definition: movenc.c:345
AVCodecContext::codec
const struct AVCodec * codec
Definition: avcodec.h:535
AVCodecContext::skip_frame
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2004
fail
#define fail()
Definition: checkasm.h:123
PerThreadContext::available_formats
enum AVPixelFormat * available_formats
Format array for get_format()
Definition: pthread_frame.c:103
AVCodecContext::thread_count
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1785
AVCodecContext::flags
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:606
PerThreadContext
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:70
AVCodecContext::coded_height
int coded_height
Definition: avcodec.h:714
submit_packet
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
Definition: pthread_frame.c:391
av_frame_alloc
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
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:1341
avassert.h
AVCodecContext::color_primaries
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1140
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
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:591
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:568
STATE_SETUP_FINISHED
@ STATE_SETUP_FINISHED
Definition: pthread_frame.c:64
AVCodecContext::has_b_frames
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:816
FrameThreadContext::async_lock
int async_lock
Definition: pthread_frame.c:129
av_realloc_array
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Definition: mem.c:198
PerThreadContext::output_cond
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:77
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:658
PerThreadContext::requested_flags
int requested_flags
flags passed to get_buffer() for requested_frame
Definition: pthread_frame.c:101
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:1757
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
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:333
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
f
#define f(width, name)
Definition: cbs_vp9.c:255
FrameThreadContext::prev_thread
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
Definition: pthread_frame.c:119
AVCodecContext::codec_id
enum AVCodecID codec_id
Definition: avcodec.h:536
arg
const char * arg
Definition: jacosubdec.c:66
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:87
AV_CODEC_PROP_INTRA_ONLY
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition: codec_desc.h:72
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:67
NULL
#define NULL
Definition: coverity.c:32
AVCodecContext::color_range
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1161
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:125
AVCodecContext::slice_flags
int slice_flags
slice flags
Definition: avcodec.h:1004
AVCodec::type
enum AVMediaType type
Definition: codec.h:203
frame_worker_thread
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
Definition: pthread_frame.c:168
AVCodecContext::internal
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:561
PerThreadContext::released_buffers
AVFrame ** released_buffers
Array of frames passed to ff_thread_release_buffer().
Definition: pthread_frame.c:96
PerThreadContext::progress_mutex
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:80
src
#define src
Definition: vp8dsp.c:254
FrameThreadContext::async_mutex
pthread_mutex_t async_mutex
Definition: pthread_frame.c:127
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:612
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:126
PerThreadContext::avctx
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:82
pthread_internal.h
av_opt_free
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1610
AVFrame::pkt_dts
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:409
ff_thread_get_buffer
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread_frame.c:998
FF_DEBUG_BUFFERS
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1633
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:614
AVCodecContext::level
int level
level
Definition: avcodec.h:1982
atomic_load_explicit
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
FF_DEBUG_THREADS
#define FF_DEBUG_THREADS
Definition: avcodec.h:1634
pthread_mutex_unlock
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:66
async_lock
static void async_lock(FrameThreadContext *fctx)
Definition: pthread_frame.c:143
ff_thread_release_buffer
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread_frame.c:1006
PerThreadContext::thread
pthread_t thread
Definition: pthread_frame.c:73
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:267
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:649
PerThreadContext::result
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:88
AV_CODEC_ID_FFV1
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:82
AVCodecContext::flags2
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:613
ff_get_buffer
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
AVPacket::size
int size
Definition: packet.h:356
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:483
copy
static void copy(const float *p1, float *p2, const int length)
Definition: vf_vaguedenoiser.c:194
AVCodecInternal::hwaccel_priv_data
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:162
AVCodec::close
int(* close)(struct AVCodecContext *)
Definition: codec.h:283
cpu.h
async_unlock
static void async_unlock(FrameThreadContext *fctx)
Definition: pthread_frame.c:152
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
FrameThreadContext::async_cond
pthread_cond_t async_cond
Definition: pthread_frame.c:128
PerThreadContext::num_released_buffers
int num_released_buffers
Definition: pthread_frame.c:97
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:354
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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:206
PerThreadContext::parent
struct FrameThreadContext * parent
Definition: pthread_frame.c:71
AVCodecInternal
Definition: internal.h:116
AVCodecContext::skip_loop_filter
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1990
pthread_t
Definition: os2threads.h:44
pthread_cond_destroy
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
FF_THREAD_FRAME
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1796
AVCodecContext::channels
int channels
number of audio channels
Definition: avcodec.h:1187
pthread_mutex_destroy
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
AVCodecContext::bits_per_coded_sample
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1750
avcodec_default_get_buffer2
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: decode.c:1649
PerThreadContext::thread_init
int thread_init
Definition: pthread_frame.c:74
log.h
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
internal.h
STATE_GET_FORMAT
@ STATE_GET_FORMAT
Set when the codec calls get_format().
Definition: pthread_frame.c:62
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:2287
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:371
HWACCEL_CAP_ASYNC_SAFE
#define HWACCEL_CAP_ASYNC_SAFE
Definition: hwconfig.h:26
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:583
av_frame_unref
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:554
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:237
AVCodecContext::idct_algo
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1729
AVCodecContext::chroma_sample_location
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1168
pthread_cond_t
Definition: os2threads.h:58
AVCodecContext::height
int height
Definition: avcodec.h:699
AVCodecContext::pix_fmt
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:736
AVCodec::priv_data_size
int priv_data_size
Definition: codec.h:238
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:2226
avcodec.h
ret
ret
Definition: filter_design.txt:187
AVCodec::caps_internal
int caps_internal
Internal codec capabilities.
Definition: codec.h:310
ff_frame_thread_init
int ff_frame_thread_init(AVCodecContext *avctx)
Definition: pthread_frame.c:748
PerThreadContext::async_serializing
int async_serializing
Definition: pthread_frame.c:109
AVCodecContext::opaque
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:568
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:761
thread_get_buffer_internal
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
Definition: pthread_frame.c:916
AVCodecContext::coded_frame
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1776
AVCodecContext
main external API structure.
Definition: avcodec.h:526
AVCodecContext::active_thread_type
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1804
PerThreadContext::hwaccel_serializing
int hwaccel_serializing
Definition: pthread_frame.c:108
ThreadFrame
Definition: thread.h:34
STATE_INPUT_READY
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:50
PerThreadContext::frame
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:86
AVCodecContext::profile
int profile
profile
Definition: avcodec.h:1859
PerThreadContext::result_format
enum AVPixelFormat result_format
get_format() result
Definition: pthread_frame.c:104
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:2354
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:75
av_buffer_ref
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
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:1611
FF_DISABLE_DEPRECATION_WARNINGS
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
AVCodecContext::coded_width
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:714
AVCodecContext::codec_type
enum AVMediaType codec_type
Definition: avcodec.h:534
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:134
mem.h
FF_CODEC_CAP_ALLOCATE_PROGRESS
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: internal.h:75
AVCodecContext::frame_number
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1217
ff_thread_get_format
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
Definition: pthread_frame.c:972
av_opt_copy
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1768
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:553
AVPacket
This structure stores compressed data.
Definition: packet.h:332
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
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:1683
AVCodec::init
int(* init)(struct AVCodecContext *)
Definition: codec.h:267
AVCodecInternal::thread_ctx
void * thread_ctx
Definition: internal.h:135
AVCodecContext::width
int width
picture width / height.
Definition: avcodec.h:699
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:565
PerThreadContext::mutex
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:79
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
PerThreadContext::requested_frame
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
Definition: pthread_frame.c:100
pthread_cond_init
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:133
AVCodecContext::sw_pix_fmt
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:2076
atomic_init
#define atomic_init(obj, value)
Definition: stdatomic.h:33
STATE_GET_BUFFER
@ STATE_GET_BUFFER
Set when the codec calls get_buffer().
Definition: pthread_frame.c:57
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:905
PerThreadContext::released_buffers_allocated
int released_buffers_allocated
Definition: pthread_frame.c:98
PerThreadContext::progress_cond
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:76
FrameThreadContext::threads
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:118
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:245
pthread_mutex_lock
#define pthread_mutex_lock(a)
Definition: ffprobe.c:62