FFmpeg  4.4.6
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 enum {
68  UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
69  NEEDS_CLOSE, ///< AVCodec->close needs to be called
70  INITIALIZED, ///< Thread has been properly set up
71 };
72 
73 /**
74  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
75  */
76 typedef struct PerThreadContext {
78 
81  unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
82  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
83  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
84  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
85 
86  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
87  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
88 
89  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
90 
91  AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
92 
93  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
94  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
95  int result; ///< The result of the last codec decode/encode() call.
96 
98 
99 #if FF_API_THREAD_SAFE_CALLBACKS
100  /**
101  * Array of frames passed to ff_thread_release_buffer().
102  * Frames are released after all threads referencing them are finished.
103  */
107 
108  AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
109  int requested_flags; ///< flags passed to get_buffer() for requested_frame
110 
111  const enum AVPixelFormat *available_formats; ///< Format array for get_format()
112  enum AVPixelFormat result_format; ///< get_format() result
113 #endif
114 
115  int die; ///< Set when the thread should exit.
116 
119 
120  atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
122 
123 /**
124  * Context stored in the client AVCodecInternal thread_ctx.
125  */
126 typedef struct FrameThreadContext {
127  PerThreadContext *threads; ///< The contexts for each thread.
128  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
129 
130  unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
131  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
132  /**
133  * This lock is used for ensuring threads run in serial when hwaccel
134  * is used.
135  */
140 
141  int next_decoding; ///< The next context to submit a packet to.
142  int next_finished; ///< The next context to return output from.
143 
144  int delaying; /**<
145  * Set for the first N packets, where N is the number of threads.
146  * While it is set, ff_thread_en/decode_frame won't return any results.
147  */
148 
149  /* hwaccel state is temporarily stored here in order to transfer its ownership
150  * to the next decoding thread without the need for extra synchronization */
155 
156 #if FF_API_THREAD_SAFE_CALLBACKS
157 #define THREAD_SAFE_CALLBACKS(avctx) \
158 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
159 #endif
160 
161 static void async_lock(FrameThreadContext *fctx)
162 {
164  while (fctx->async_lock)
165  pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
166  fctx->async_lock = 1;
168 }
169 
171 {
173  av_assert0(fctx->async_lock);
174  fctx->async_lock = 0;
177 }
178 
179 /**
180  * Codec worker thread.
181  *
182  * Automatically calls ff_thread_finish_setup() if the codec does
183  * not provide an update_thread_context method, or if the codec returns
184  * before calling it.
185  */
187 {
188  PerThreadContext *p = arg;
189  AVCodecContext *avctx = p->avctx;
190  const AVCodec *codec = avctx->codec;
191 
193  while (1) {
194  while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
196 
197  if (p->die) break;
198 
200  if (!codec->update_thread_context
202  && THREAD_SAFE_CALLBACKS(avctx)
203 #endif
204  )
205  ff_thread_finish_setup(avctx);
207 
208  /* If a decoder supports hwaccel, then it must call ff_get_format().
209  * Since that call must happen before ff_thread_finish_setup(), the
210  * decoder is required to implement update_thread_context() and call
211  * ff_thread_finish_setup() manually. Therefore the above
212  * ff_thread_finish_setup() call did not happen and hwaccel_serializing
213  * cannot be true here. */
215 
216  /* if the previous thread uses hwaccel then we take the lock to ensure
217  * the threads don't run concurrently */
218  if (avctx->hwaccel) {
220  p->hwaccel_serializing = 1;
221  }
222 
223  av_frame_unref(p->frame);
224  p->got_frame = 0;
225  p->result = codec->decode(avctx, p->frame, &p->got_frame, p->avpkt);
226 
227  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
229  av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
230  "free the frame on failure. This is a bug, please report it.\n");
231  av_frame_unref(p->frame);
232  }
233 
234  if (atomic_load(&p->state) == STATE_SETTING_UP)
235  ff_thread_finish_setup(avctx);
236 
237  if (p->hwaccel_serializing) {
238  /* wipe hwaccel state to avoid stale pointers lying around;
239  * the state was transferred to FrameThreadContext in
240  * ff_thread_finish_setup(), so nothing is leaked */
241  avctx->hwaccel = NULL;
242  avctx->hwaccel_context = NULL;
243  avctx->internal->hwaccel_priv_data = NULL;
244 
245  p->hwaccel_serializing = 0;
247  }
248  av_assert0(!avctx->hwaccel);
249 
250  if (p->async_serializing) {
251  p->async_serializing = 0;
252 
253  async_unlock(p->parent);
254  }
255 
257 
259 
263  }
265 
266  return NULL;
267 }
268 
269 /**
270  * Update the next thread's AVCodecContext with values from the reference thread's context.
271  *
272  * @param dst The destination context.
273  * @param src The source context.
274  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
275  * @return 0 on success, negative error code on failure
276  */
278 {
279  int err = 0;
280 
281  if (dst != src && (for_user || src->codec->update_thread_context)) {
282  dst->time_base = src->time_base;
283  dst->framerate = src->framerate;
284  dst->width = src->width;
285  dst->height = src->height;
286  dst->pix_fmt = src->pix_fmt;
287  dst->sw_pix_fmt = src->sw_pix_fmt;
288 
289  dst->coded_width = src->coded_width;
290  dst->coded_height = src->coded_height;
291 
292  dst->has_b_frames = src->has_b_frames;
293  dst->idct_algo = src->idct_algo;
294 
295  dst->bits_per_coded_sample = src->bits_per_coded_sample;
296  dst->sample_aspect_ratio = src->sample_aspect_ratio;
297 
298  dst->profile = src->profile;
299  dst->level = src->level;
300 
301  dst->bits_per_raw_sample = src->bits_per_raw_sample;
302  dst->ticks_per_frame = src->ticks_per_frame;
303  dst->color_primaries = src->color_primaries;
304 
305  dst->color_trc = src->color_trc;
306  dst->colorspace = src->colorspace;
307  dst->color_range = src->color_range;
308  dst->chroma_sample_location = src->chroma_sample_location;
309 
310  dst->channels = src->channels;
311  dst->sample_rate = src->sample_rate;
312  dst->sample_fmt = src->sample_fmt;
313  dst->channel_layout = src->channel_layout;
314 
315  if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
316  (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
318 
319  if (src->hw_frames_ctx) {
320  dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
321  if (!dst->hw_frames_ctx)
322  return AVERROR(ENOMEM);
323  }
324  }
325 
326  dst->hwaccel_flags = src->hwaccel_flags;
327 
328  err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
329  if (err < 0)
330  return err;
331  }
332 
333  if (for_user) {
334 #if FF_API_CODED_FRAME
336  dst->coded_frame = src->coded_frame;
338 #endif
339  } else {
340  if (dst->codec->update_thread_context)
341  err = dst->codec->update_thread_context(dst, src);
342  }
343 
344  return err;
345 }
346 
347 /**
348  * Update the next thread's AVCodecContext with values set by the user.
349  *
350  * @param dst The destination context.
351  * @param src The source context.
352  * @return 0 on success, negative error code on failure
353  */
355 {
356  dst->flags = src->flags;
357 
358  dst->draw_horiz_band= src->draw_horiz_band;
359  dst->get_buffer2 = src->get_buffer2;
360 
361  dst->opaque = src->opaque;
362  dst->debug = src->debug;
363 
364  dst->slice_flags = src->slice_flags;
365  dst->flags2 = src->flags2;
366  dst->export_side_data = src->export_side_data;
367 
368  dst->skip_loop_filter = src->skip_loop_filter;
369  dst->skip_idct = src->skip_idct;
370  dst->skip_frame = src->skip_frame;
371 
372  dst->frame_number = src->frame_number;
373  dst->reordered_opaque = src->reordered_opaque;
374 #if FF_API_THREAD_SAFE_CALLBACKS
376  dst->thread_safe_callbacks = src->thread_safe_callbacks;
378 #endif
379 
380  if (src->slice_count && src->slice_offset) {
381  if (dst->slice_count < src->slice_count) {
382  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
383  sizeof(*dst->slice_offset));
384  if (err < 0)
385  return err;
386  }
387  memcpy(dst->slice_offset, src->slice_offset,
388  src->slice_count * sizeof(*dst->slice_offset));
389  }
390  dst->slice_count = src->slice_count;
391  return 0;
392 }
393 
394 #if FF_API_THREAD_SAFE_CALLBACKS
395 /// Releases the buffers that this decoding thread was the last user of.
397 {
398  FrameThreadContext *fctx = p->parent;
399 
400  while (p->num_released_buffers > 0) {
401  AVFrame *f;
402 
404 
405  // fix extended data in case the caller screwed it up
409  f->extended_data = f->data;
410  av_frame_unref(f);
411 
413  }
414 }
415 #endif
416 
417 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
418  AVPacket *avpkt)
419 {
420  FrameThreadContext *fctx = p->parent;
421  PerThreadContext *prev_thread = fctx->prev_thread;
422  const AVCodec *codec = p->avctx->codec;
423  int ret;
424 
425  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
426  return 0;
427 
429 
430  ret = update_context_from_user(p->avctx, user_avctx);
431  if (ret) {
433  return ret;
434  }
436  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
437  memory_order_relaxed);
438 
439 #if FF_API_THREAD_SAFE_CALLBACKS
441 #endif
442 
443  if (prev_thread) {
444  int err;
445  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
446  pthread_mutex_lock(&prev_thread->progress_mutex);
447  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
448  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
449  pthread_mutex_unlock(&prev_thread->progress_mutex);
450  }
451 
452  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
453  if (err) {
455  return err;
456  }
457  }
458 
459  /* transfer the stashed hwaccel state, if any */
460  av_assert0(!p->avctx->hwaccel);
461  FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
464 
466  ret = av_packet_ref(p->avpkt, avpkt);
467  if (ret < 0) {
469  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
470  return ret;
471  }
472 
476 
477 #if FF_API_THREAD_SAFE_CALLBACKS
479  /*
480  * If the client doesn't have a thread-safe get_buffer(),
481  * then decoding threads call back to the main thread,
482  * and it calls back to the client here.
483  */
484 
485  if (!p->avctx->thread_safe_callbacks && (
489  int call_done = 1;
491  while (atomic_load(&p->state) == STATE_SETTING_UP)
493 
494  switch (atomic_load_explicit(&p->state, memory_order_acquire)) {
495  case STATE_GET_BUFFER:
497  break;
498  case STATE_GET_FORMAT:
500  break;
501  default:
502  call_done = 0;
503  break;
504  }
505  if (call_done) {
508  }
510  }
511  }
513 #endif
514 
515  fctx->prev_thread = p;
516  fctx->next_decoding++;
517 
518  return 0;
519 }
520 
522  AVFrame *picture, int *got_picture_ptr,
523  AVPacket *avpkt)
524 {
525  FrameThreadContext *fctx = avctx->internal->thread_ctx;
526  int finished = fctx->next_finished;
527  PerThreadContext *p;
528  int err;
529 
530  /* release the async lock, permitting blocked hwaccel threads to
531  * go forward while we are in this function */
532  async_unlock(fctx);
533 
534  /*
535  * Submit a packet to the next decoding thread.
536  */
537 
538  p = &fctx->threads[fctx->next_decoding];
539  err = submit_packet(p, avctx, avpkt);
540  if (err)
541  goto finish;
542 
543  /*
544  * If we're still receiving the initial packets, don't return a frame.
545  */
546 
547  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
548  fctx->delaying = 0;
549 
550  if (fctx->delaying) {
551  *got_picture_ptr=0;
552  if (avpkt->size) {
553  err = avpkt->size;
554  goto finish;
555  }
556  }
557 
558  /*
559  * Return the next available frame from the oldest thread.
560  * If we're at the end of the stream, then we have to skip threads that
561  * didn't output a frame/error, because we don't want to accidentally signal
562  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
563  */
564 
565  do {
566  p = &fctx->threads[finished++];
567 
568  if (atomic_load(&p->state) != STATE_INPUT_READY) {
570  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
573  }
574 
575  av_frame_move_ref(picture, p->frame);
576  *got_picture_ptr = p->got_frame;
577  picture->pkt_dts = p->avpkt->dts;
578  err = p->result;
579 
580  /*
581  * A later call with avkpt->size == 0 may loop over all threads,
582  * including this one, searching for a frame/error to return before being
583  * stopped by the "finished != fctx->next_finished" condition.
584  * Make sure we don't mistakenly return the same frame/error again.
585  */
586  p->got_frame = 0;
587  p->result = 0;
588 
589  if (finished >= avctx->thread_count) finished = 0;
590  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
591 
592  update_context_from_thread(avctx, p->avctx, 1);
593 
594  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
595 
596  fctx->next_finished = finished;
597 
598  /* return the size of the consumed packet if no error occurred */
599  if (err >= 0)
600  err = avpkt->size;
601 finish:
602  async_lock(fctx);
603  return err;
604 }
605 
606 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
607 {
608  PerThreadContext *p;
609  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
610 
611  if (!progress ||
612  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
613  return;
614 
615  p = f->owner[field]->internal->thread_ctx;
616 
617  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
618  av_log(f->owner[field], AV_LOG_DEBUG,
619  "%p finished %d field %d\n", progress, n, field);
620 
622 
623  atomic_store_explicit(&progress[field], n, memory_order_release);
624 
627 }
628 
629 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
630 {
631  PerThreadContext *p;
632  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
633 
634  if (!progress ||
635  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
636  return;
637 
638  p = f->owner[field]->internal->thread_ctx;
639 
640  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
641  av_log(f->owner[field], AV_LOG_DEBUG,
642  "thread awaiting %d field %d from %p\n", n, field, progress);
643 
645  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
648 }
649 
651  PerThreadContext *p = avctx->internal->thread_ctx;
652 
653  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
654 
655  if (avctx->hwaccel && !p->hwaccel_serializing) {
657  p->hwaccel_serializing = 1;
658  }
659 
660  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
661  if (avctx->hwaccel &&
663  p->async_serializing = 1;
664 
665  async_lock(p->parent);
666  }
667 
668  /* save hwaccel state for passing to the next thread;
669  * this is done here so that this worker thread can wipe its own hwaccel
670  * state after decoding, without requiring synchronization */
672  p->parent->stash_hwaccel = avctx->hwaccel;
675 
678  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
679  }
680 
682 
685 }
686 
687 /// Waits for all threads to finish.
688 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
689 {
690  int i;
691 
692  async_unlock(fctx);
693 
694  for (i = 0; i < thread_count; i++) {
695  PerThreadContext *p = &fctx->threads[i];
696 
697  if (atomic_load(&p->state) != STATE_INPUT_READY) {
699  while (atomic_load(&p->state) != STATE_INPUT_READY)
702  }
703  p->got_frame = 0;
704  }
705 
706  async_lock(fctx);
707 }
708 
709 #define SENTINEL 0 // This forbids putting a mutex/condition variable at the front.
710 #define OFFSET_ARRAY(...) __VA_ARGS__, SENTINEL
711 #define DEFINE_OFFSET_ARRAY(type, name, mutexes, conds) \
712 static const unsigned name ## _offsets[] = { offsetof(type, pthread_init_cnt),\
713  OFFSET_ARRAY mutexes, \
714  OFFSET_ARRAY conds }
715 
716 #define OFF(member) offsetof(FrameThreadContext, member)
718  (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
719  (OFF(async_cond)));
720 #undef OFF
721 
722 #define OFF(member) offsetof(PerThreadContext, member)
724  (OFF(progress_mutex), OFF(mutex)),
725  (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
726 #undef OFF
727 
728 static av_cold void free_pthread(void *obj, const unsigned offsets[])
729 {
730  unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
731  const unsigned *cur_offset = offsets;
732 
733  for (; *(++cur_offset) != SENTINEL && cnt; cnt--)
734  pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
735  for (; *(++cur_offset) != SENTINEL && cnt; cnt--)
736  pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
737 }
738 
739 static av_cold int init_pthread(void *obj, const unsigned offsets[])
740 {
741  const unsigned *cur_offset = offsets;
742  unsigned cnt = 0;
743  int err;
744 
745 #define PTHREAD_INIT_LOOP(type) \
746  for (; *(++cur_offset) != SENTINEL; cnt++) { \
747  pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \
748  err = pthread_ ## type ## _init(dst, NULL); \
749  if (err) { \
750  err = AVERROR(err); \
751  goto fail; \
752  } \
753  }
756 
757 fail:
758  *(unsigned*)((char*)obj + offsets[0]) = cnt;
759  return err;
760 }
761 
762 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
763 {
764  FrameThreadContext *fctx = avctx->internal->thread_ctx;
765  const AVCodec *codec = avctx->codec;
766  int i;
767 
768  park_frame_worker_threads(fctx, thread_count);
769 
770  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
771  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
772  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
774  fctx->threads->avctx->internal->is_copy = 1;
775  }
776 
777  for (i = 0; i < thread_count; i++) {
778  PerThreadContext *p = &fctx->threads[i];
779  AVCodecContext *ctx = p->avctx;
780 
781  if (ctx->internal) {
782  if (p->thread_init == INITIALIZED) {
784  p->die = 1;
787 
788  pthread_join(p->thread, NULL);
789  }
790  if (codec->close && p->thread_init != UNINITIALIZED)
791  codec->close(ctx);
792 
793 #if FF_API_THREAD_SAFE_CALLBACKS
795  for (int j = 0; j < p->released_buffers_allocated; j++)
798 #endif
799  if (ctx->priv_data) {
800  if (codec->priv_class)
803  }
804 
805  av_freep(&ctx->slice_offset);
806 
807  av_buffer_unref(&ctx->internal->pool);
808  av_freep(&ctx->internal);
809  av_buffer_unref(&ctx->hw_frames_ctx);
810  }
811 
812  av_frame_free(&p->frame);
813 
814  free_pthread(p, per_thread_offsets);
815  av_packet_free(&p->avpkt);
816 
817  av_freep(&p->avctx);
818  }
819 
820  av_freep(&fctx->threads);
821  free_pthread(fctx, thread_ctx_offsets);
822 
823  /* if we have stashed hwaccel state, move it to the user-facing context,
824  * so it will be freed in avcodec_close() */
825  av_assert0(!avctx->hwaccel);
826  FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
827  FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
828  FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
829 
830  av_freep(&avctx->internal->thread_ctx);
831 
832  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
833  av_opt_free(avctx->priv_data);
834  avctx->codec = NULL;
835 }
836 
837 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
838  FrameThreadContext *fctx, AVCodecContext *avctx,
839  AVCodecContext *src, const AVCodec *codec, int first)
840 {
842  int err;
843 
845 
846  copy = av_memdup(src, sizeof(*src));
847  if (!copy)
848  return AVERROR(ENOMEM);
849  copy->priv_data = NULL;
850 
851  /* From now on, this PerThreadContext will be cleaned up by
852  * ff_frame_thread_free in case of errors. */
853  (*threads_to_free)++;
854 
855  p->parent = fctx;
856  p->avctx = copy;
857 
858  copy->internal = av_memdup(src->internal, sizeof(*src->internal));
859  if (!copy->internal)
860  return AVERROR(ENOMEM);
861  copy->internal->thread_ctx = p;
862 
863  copy->delay = avctx->delay;
864 
865  if (codec->priv_data_size) {
866  copy->priv_data = av_mallocz(codec->priv_data_size);
867  if (!copy->priv_data)
868  return AVERROR(ENOMEM);
869 
870  if (codec->priv_class) {
871  *(const AVClass **)copy->priv_data = codec->priv_class;
872  err = av_opt_copy(copy->priv_data, src->priv_data);
873  if (err < 0)
874  return err;
875  }
876  }
877 
878  err = init_pthread(p, per_thread_offsets);
879  if (err < 0)
880  return err;
881 
882  if (!(p->frame = av_frame_alloc()) ||
883  !(p->avpkt = av_packet_alloc()))
884  return AVERROR(ENOMEM);
885  copy->internal->last_pkt_props = p->avpkt;
886 
887  if (!first)
888  copy->internal->is_copy = 1;
889 
890  if (codec->init) {
891  err = codec->init(copy);
892  if (err < 0) {
895  return err;
896  }
897  }
899 
900  if (first)
901  update_context_from_thread(avctx, copy, 1);
902 
903  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
904 
906  if (err < 0)
907  return err;
909 
910  return 0;
911 }
912 
914 {
915  int thread_count = avctx->thread_count;
916  const AVCodec *codec = avctx->codec;
917  AVCodecContext *src = avctx;
918  FrameThreadContext *fctx;
919  int err, i = 0;
920 
921  if (!thread_count) {
922  int nb_cpus = av_cpu_count();
923  // use number of cores + 1 as thread count if there is more than one
924  if (nb_cpus > 1)
925  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
926  else
927  thread_count = avctx->thread_count = 1;
928  }
929 
930  if (thread_count <= 1) {
931  avctx->active_thread_type = 0;
932  return 0;
933  }
934 
935  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
936  if (!fctx)
937  return AVERROR(ENOMEM);
938 
939  err = init_pthread(fctx, thread_ctx_offsets);
940  if (err < 0) {
941  free_pthread(fctx, thread_ctx_offsets);
942  av_freep(&avctx->internal->thread_ctx);
943  return err;
944  }
945 
946  fctx->async_lock = 1;
947  fctx->delaying = 1;
948 
949  if (codec->type == AVMEDIA_TYPE_VIDEO)
950  avctx->delay = src->thread_count - 1;
951 
952  fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
953  if (!fctx->threads) {
954  err = AVERROR(ENOMEM);
955  goto error;
956  }
957 
958  for (; i < thread_count; ) {
959  PerThreadContext *p = &fctx->threads[i];
960  int first = !i;
961 
962  err = init_thread(p, &i, fctx, avctx, src, codec, first);
963  if (err < 0)
964  goto error;
965  }
966 
967  return 0;
968 
969 error:
970  ff_frame_thread_free(avctx, i);
971  return err;
972 }
973 
975 {
976  int i;
977  FrameThreadContext *fctx = avctx->internal->thread_ctx;
978 
979  if (!fctx) return;
980 
982  if (fctx->prev_thread) {
983  if (fctx->prev_thread != &fctx->threads[0])
985  }
986 
987  fctx->next_decoding = fctx->next_finished = 0;
988  fctx->delaying = 1;
989  fctx->prev_thread = NULL;
990  for (i = 0; i < avctx->thread_count; i++) {
991  PerThreadContext *p = &fctx->threads[i];
992  // Make sure decode flush calls with size=0 won't return old frames
993  p->got_frame = 0;
994  av_frame_unref(p->frame);
995  p->result = 0;
996 
997 #if FF_API_THREAD_SAFE_CALLBACKS
999 #endif
1000 
1001  if (avctx->codec->flush)
1002  avctx->codec->flush(p->avctx);
1003  }
1004 }
1005 
1007 {
1008  PerThreadContext *p = avctx->internal->thread_ctx;
1011  (avctx->codec->update_thread_context
1013  || !THREAD_SAFE_CALLBACKS(avctx)
1014 #endif
1015  )) {
1016  return 0;
1017  }
1019  return 1;
1020 }
1021 
1023 {
1024  PerThreadContext *p = avctx->internal->thread_ctx;
1025  int err;
1026 
1027  f->owner[0] = f->owner[1] = avctx;
1028 
1029  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1030  return ff_get_buffer(avctx, f->f, flags);
1031 
1033  if (atomic_load(&p->state) != STATE_SETTING_UP &&
1034  (avctx->codec->update_thread_context
1036  || !THREAD_SAFE_CALLBACKS(avctx)
1037 #endif
1038  )) {
1040  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
1041  return -1;
1042  }
1043 
1045  atomic_int *progress;
1046  f->progress = av_buffer_alloc(2 * sizeof(*progress));
1047  if (!f->progress) {
1048  return AVERROR(ENOMEM);
1049  }
1050  progress = (atomic_int*)f->progress->data;
1051 
1052  atomic_init(&progress[0], -1);
1053  atomic_init(&progress[1], -1);
1054  }
1055 
1057 #if !FF_API_THREAD_SAFE_CALLBACKS
1058  err = ff_get_buffer(avctx, f->f, flags);
1059 #else
1061  if (THREAD_SAFE_CALLBACKS(avctx)) {
1062  err = ff_get_buffer(avctx, f->f, flags);
1063  } else {
1065  p->requested_frame = f->f;
1066  p->requested_flags = flags;
1067  atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
1069 
1070  while (atomic_load(&p->state) != STATE_SETTING_UP)
1072 
1073  err = p->result;
1074 
1076 
1077  }
1078  if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
1079  ff_thread_finish_setup(avctx);
1081 #endif
1082  if (err)
1083  av_buffer_unref(&f->progress);
1084 
1086 
1087  return err;
1088 }
1089 
1090 #if FF_API_THREAD_SAFE_CALLBACKS
1092 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1093 {
1094  enum AVPixelFormat res;
1095  PerThreadContext *p = avctx->internal->thread_ctx;
1096  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
1098  return ff_get_format(avctx, fmt);
1099  if (atomic_load(&p->state) != STATE_SETTING_UP) {
1100  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
1101  return -1;
1102  }
1104  p->available_formats = fmt;
1107 
1108  while (atomic_load(&p->state) != STATE_SETTING_UP)
1110 
1111  res = p->result_format;
1112 
1114 
1115  return res;
1116 }
1118 #endif
1119 
1121 {
1122  int ret = thread_get_buffer_internal(avctx, f, flags);
1123  if (ret < 0)
1124  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1125  return ret;
1126 }
1127 
1129 {
1130 #if FF_API_THREAD_SAFE_CALLBACKS
1132  PerThreadContext *p = avctx->internal->thread_ctx;
1133  FrameThreadContext *fctx;
1134  AVFrame *dst;
1135  int ret = 0;
1136  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1137  THREAD_SAFE_CALLBACKS(avctx);
1139 #endif
1140 
1141  if (!f->f)
1142  return;
1143 
1144  if (avctx->debug & FF_DEBUG_BUFFERS)
1145  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1146 
1147  av_buffer_unref(&f->progress);
1148  f->owner[0] = f->owner[1] = NULL;
1149 
1150 #if !FF_API_THREAD_SAFE_CALLBACKS
1151  av_frame_unref(f->f);
1152 #else
1153  // when the frame buffers are not allocated, just reset it to clean state
1154  if (can_direct_free || !f->f->buf[0]) {
1155  av_frame_unref(f->f);
1156  return;
1157  }
1158 
1159  fctx = p->parent;
1161 
1162  if (p->num_released_buffers == p->released_buffers_allocated) {
1163  AVFrame **tmp = av_realloc_array(p->released_buffers, p->released_buffers_allocated + 1,
1164  sizeof(*p->released_buffers));
1165  if (tmp) {
1166  tmp[p->released_buffers_allocated] = av_frame_alloc();
1167  p->released_buffers = tmp;
1168  }
1169 
1170  if (!tmp || !tmp[p->released_buffers_allocated]) {
1171  ret = AVERROR(ENOMEM);
1172  goto fail;
1173  }
1174  p->released_buffers_allocated++;
1175  }
1176 
1177  dst = p->released_buffers[p->num_released_buffers];
1178  av_frame_move_ref(dst, f->f);
1179 
1180  p->num_released_buffers++;
1181 
1182 fail:
1184 
1185  // make sure the frame is clean even if we fail to free it
1186  // this leaks, but it is better than crashing
1187  if (ret < 0) {
1188  av_log(avctx, AV_LOG_ERROR, "Could not queue a frame for freeing, this will leak\n");
1189  memset(f->f->buf, 0, sizeof(f->f->buf));
1190  if (f->f->extended_buf)
1191  memset(f->f->extended_buf, 0, f->f->nb_extended_buf * sizeof(*f->f->extended_buf));
1192  av_frame_unref(f->f);
1193  }
1194 #endif
1195 }
#define av_cold
Definition: attributes.h:88
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
Libavcodec external API header.
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:1788
#define FF_DEBUG_THREADS
Definition: avcodec.h:1640
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:1639
refcounted data buffer API
#define flags(name, subs,...)
Definition: cbs_av1.c:572
#define f(width, name)
Definition: cbs_vp9.c:255
#define fail()
Definition: checkasm.h:133
common internal and external API header
#define FFSWAP(type, a, b)
Definition: common.h:108
#define FFMIN(a, b)
Definition: common.h:105
#define NULL
Definition: coverity.c:32
int av_cpu_count(void)
Definition: cpu.c:275
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1900
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1326
#define atomic_store(object, desired)
Definition: stdatomic.h:85
intptr_t atomic_int
Definition: stdatomic.h:55
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
#define atomic_load(object)
Definition: stdatomic.h:93
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
#define atomic_init(obj, value)
Definition: stdatomic.h:33
#define pthread_mutex_lock(a)
Definition: ffprobe.c:63
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:67
reference-counted frame API
int av_opt_copy(void *dst, const void *src)
Copy options from src object into dest object.
Definition: opt.c:1789
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1611
#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:77
@ AV_CODEC_ID_FFV1
Definition: codec_id.h:82
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: decode.c:1695
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: decode.c:1114
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:75
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:634
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:64
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:641
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
AVBufferRef * av_buffer_alloc(buffer_size_t size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:67
int av_buffer_replace(AVBufferRef **pdst, AVBufferRef *src)
Ensure dst refers to the same data as src.
Definition: buffer.c:219
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:93
#define AVERROR(e)
Definition: error.h:43
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:553
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:582
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:215
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:200
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:285
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
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
Allocate, reallocate, or free an array.
Definition: mem.c:198
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
void * av_mallocz_array(size_t nmemb, size_t size)
Allocate a memory block for an array with av_mallocz().
Definition: mem.c:190
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
@ AVMEDIA_TYPE_VIDEO
Definition: avutil.h:201
static const int offsets[]
Definition: hevc_pel.c:34
#define HWACCEL_CAP_ASYNC_SAFE
Definition: hwconfig.h:26
int i
Definition: input.c:407
#define FF_CODEC_CAP_ALLOCATE_PROGRESS
Definition: internal.h:76
#define FF_CODEC_CAP_INIT_CLEANUP
The codec allows calling the close function for deallocation even if the init function returned a fai...
Definition: internal.h:49
const char * arg
Definition: jacosubdec.c:66
#define FF_API_THREAD_SAFE_CALLBACKS
Definition: version.h:157
common internal API header
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
#define attribute_align_arg
Definition: internal.h:61
swscale version macros
static AVMutex mutex
Definition: log.c:44
Memory handling functions.
AVOptions.
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:162
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:152
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
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
_fmutex pthread_mutex_t
Definition: os2threads.h:53
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:192
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
int(* cond)(enum AVPixelFormat pix_fmt)
Definition: pixdesc_query.c:28
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
static av_cold int init_pthread(void *obj, const unsigned offsets[])
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
@ NEEDS_CLOSE
AVCodec->close needs to be called.
Definition: pthread_frame.c:69
@ INITIALIZED
Thread has been properly set up.
Definition: pthread_frame.c:70
@ UNINITIALIZED
Thread has not been created, AVCodec->close mustn't be called.
Definition: pthread_frame.c:68
FF_ENABLE_DEPRECATION_WARNINGS int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
static void async_unlock(FrameThreadContext *fctx)
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
int ff_frame_thread_init(AVCodecContext *avctx)
static av_cold int init_thread(PerThreadContext *p, int *threads_to_free, FrameThreadContext *fctx, AVCodecContext *avctx, AVCodecContext *src, const AVCodec *codec, int first)
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
@ STATE_GET_FORMAT
Set when the codec calls get_format().
Definition: pthread_frame.c:62
@ STATE_SETTING_UP
Definition: pthread_frame.c:52
@ STATE_INPUT_READY
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:50
@ STATE_SETUP_FINISHED
Definition: pthread_frame.c:64
@ STATE_GET_BUFFER
Set when the codec calls get_buffer().
Definition: pthread_frame.c:57
#define DEFINE_OFFSET_ARRAY(type, name, mutexes, conds)
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...
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
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.
#define THREAD_SAFE_CALLBACKS(avctx)
#define PTHREAD_INIT_LOOP(type)
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
static void async_lock(FrameThreadContext *fctx)
#define OFF(member)
static av_cold void free_pthread(void *obj, const unsigned offsets[])
int ff_thread_can_start_frame(AVCodecContext *avctx)
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
#define SENTINEL
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
FF_DISABLE_DEPRECATION_WARNINGS enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
#define MAX_AUTO_THREADS
uint8_t * data
The data buffer.
Definition: buffer.h:92
Describe the class of an AVClass context structure.
Definition: log.h:67
main external API structure.
Definition: avcodec.h:536
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:746
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active).
Definition: avcodec.h:2283
int width
picture width / height.
Definition: avcodec.h:709
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:623
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1204
int debug
debug
Definition: avcodec.h:1627
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:2082
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1171
int slice_flags
slice flags
Definition: avcodec.h:1014
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:788
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1150
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames.
Definition: avcodec.h:2222
enum AVMediaType codec_type
Definition: avcodec.h:544
AVRational framerate
Definition: avcodec.h:2075
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:915
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:1744
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:1768
int slice_count
slice count
Definition: avcodec.h:890
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:668
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:1684
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:1796
int64_t reordered_opaque
opaque 64-bit number (generally a PTS) that will be reordered and output in AVFrame....
Definition: avcodec.h:1677
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:826
int level
level
Definition: avcodec.h:1988
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:771
enum AVDiscard skip_loop_filter
Skip loop filtering for selected frames.
Definition: avcodec.h:1996
const struct AVCodec * codec
Definition: avcodec.h:545
int profile
profile
Definition: avcodec.h:1862
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:1751
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:1723
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame,...
Definition: avcodec.h:2350
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1164
int sample_rate
samples per second
Definition: avcodec.h:1196
int delay
Codec delay.
Definition: avcodec.h:692
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1227
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:1816
int thread_count
thread count is used to decide how many independent tasks should be passed to execute()
Definition: avcodec.h:1777
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:578
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:1696
int coded_height
Definition: avcodec.h:724
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1157
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition: avcodec.h:659
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:616
int channels
number of audio channels
Definition: avcodec.h:1197
enum AVDiscard skip_idct
Skip IDCT/dequantization for selected frames.
Definition: avcodec.h:2003
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1178
enum AVCodecID codec_id
Definition: avcodec.h:546
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:724
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1247
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:906
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:1355
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:571
void * priv_data
Definition: avcodec.h:563
enum AVDiscard skip_frame
Skip decoding for selected frames.
Definition: avcodec.h:2010
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it.
Definition: internal.h:136
void * thread_ctx
Definition: internal.h:150
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:180
AVBufferRef * pool
Definition: internal.h:148
AVCodec.
Definition: codec.h:197
int caps_internal
Internal codec capabilities.
Definition: codec.h:328
const AVClass * priv_class
AVClass for the private context.
Definition: codec.h:223
int(* init)(struct AVCodecContext *)
Definition: codec.h:276
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:260
int(* decode)(struct AVCodecContext *avctx, void *outdata, int *got_frame_ptr, struct AVPacket *avpkt)
Decode picture or subtitle data.
Definition: codec.h:303
int priv_data_size
Definition: codec.h:245
void(* flush)(struct AVCodecContext *)
Flush buffers.
Definition: codec.h:323
enum AVMediaType type
Definition: codec.h:210
int(* close)(struct AVCodecContext *)
Definition: codec.h:305
int capabilities
Codec capabilities.
Definition: codec.h:216
AVFormatInternal * internal
An opaque field for libavformat internal usage.
Definition: avformat.h:1699
void * priv_data
Format private data.
Definition: avformat.h:1260
This structure describes decoded (raw) audio or video data.
Definition: frame.h:318
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:509
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:427
int caps_internal
Internal hwaccel capabilities.
Definition: avcodec.h:2591
This structure stores compressed data.
Definition: packet.h:346
int size
Definition: packet.h:370
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
Definition: packet.h:368
Context stored in the client AVCodecInternal thread_ctx.
pthread_mutex_t async_mutex
int next_decoding
The next context to submit a packet to.
PerThreadContext * threads
The contexts for each thread.
int delaying
Set for the first N packets, where N is the number of threads.
const AVHWAccel * stash_hwaccel
pthread_cond_t async_cond
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
int next_finished
The next context to return output from.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
void * stash_hwaccel_context
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when hwaccel is used.
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:76
pthread_t thread
Definition: pthread_frame.c:79
struct FrameThreadContext * parent
Definition: pthread_frame.c:77
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:89
enum AVPixelFormat * available_formats
Format array for get_format()
int die
Set when the thread should exit.
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:86
int released_buffers_allocated
AVFrame ** released_buffers
Array of frames passed to ff_thread_release_buffer().
int requested_flags
flags passed to get_buffer() for requested_frame
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
enum AVPixelFormat result_format
get_format() result
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:87
unsigned pthread_init_cnt
Number of successfully initialized mutexes/conditions.
Definition: pthread_frame.c:81
AVPacket * avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:91
atomic_int state
Definition: pthread_frame.c:97
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:94
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:95
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:82
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:93
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:84
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:83
#define av_freep(p)
#define av_log(a,...)
static void error(const char *err)
static uint8_t tmp[11]
Definition: aes_ctr.c:27
#define src
Definition: vp8dsp.c:255
AVFormatContext * ctx
Definition: movenc.c:48
static void finish(void)
Definition: movenc.c:342
if(ret< 0)
Definition: vf_mcdeint.c:282
static void copy(const float *p1, float *p2, const int length)