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 "hwaccel.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]) {
204  if (avctx->internal->allocate_progress)
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 
265 
266  dst->profile = src->profile;
267  dst->level = src->level;
268 
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;
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;
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) {
293  if (!dst->hw_frames_ctx)
294  return AVERROR(ENOMEM);
295  }
296  }
297 
298  dst->hwaccel_flags = src->hwaccel_flags;
299  }
300 
301  if (for_user) {
302  dst->delay = src->thread_count - 1;
303 #if FF_API_CODED_FRAME
305  dst->coded_frame = src->coded_frame;
307 #endif
308  } else {
309  if (dst->codec->update_thread_context)
310  err = dst->codec->update_thread_context(dst, src);
311  }
312 
313  return err;
314 }
315 
316 /**
317  * Update the next thread's AVCodecContext with values set by the user.
318  *
319  * @param dst The destination context.
320  * @param src The source context.
321  * @return 0 on success, negative error code on failure
322  */
324 {
325 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
326  dst->flags = src->flags;
327 
328  dst->draw_horiz_band= src->draw_horiz_band;
329  dst->get_buffer2 = src->get_buffer2;
330 
331  dst->opaque = src->opaque;
332  dst->debug = src->debug;
333  dst->debug_mv = src->debug_mv;
334 
335  dst->slice_flags = src->slice_flags;
336  dst->flags2 = src->flags2;
338 
339  copy_fields(skip_loop_filter, subtitle_header);
340 
341  dst->frame_number = src->frame_number;
344 
345  if (src->slice_count && src->slice_offset) {
346  if (dst->slice_count < src->slice_count) {
347  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
348  sizeof(*dst->slice_offset));
349  if (err < 0)
350  return err;
351  }
352  memcpy(dst->slice_offset, src->slice_offset,
353  src->slice_count * sizeof(*dst->slice_offset));
354  }
355  dst->slice_count = src->slice_count;
356  return 0;
357 #undef copy_fields
358 }
359 
360 /// Releases the buffers that this decoding thread was the last user of.
362 {
363  FrameThreadContext *fctx = p->parent;
364 
365  while (p->num_released_buffers > 0) {
366  AVFrame *f;
367 
369 
370  // fix extended data in case the caller screwed it up
374  f->extended_data = f->data;
375  av_frame_unref(f);
376 
378  }
379 }
380 
381 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
382  AVPacket *avpkt)
383 {
384  FrameThreadContext *fctx = p->parent;
385  PerThreadContext *prev_thread = fctx->prev_thread;
386  const AVCodec *codec = p->avctx->codec;
387  int ret;
388 
389  if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
390  return 0;
391 
393 
394  ret = update_context_from_user(p->avctx, user_avctx);
395  if (ret) {
397  return ret;
398  }
400  (p->avctx->debug & FF_DEBUG_THREADS) != 0,
401  memory_order_relaxed);
402 
404 
405  if (prev_thread) {
406  int err;
407  if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
408  pthread_mutex_lock(&prev_thread->progress_mutex);
409  while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
410  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
411  pthread_mutex_unlock(&prev_thread->progress_mutex);
412  }
413 
414  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
415  if (err) {
417  return err;
418  }
419  }
420 
421  av_packet_unref(&p->avpkt);
422  ret = av_packet_ref(&p->avpkt, avpkt);
423  if (ret < 0) {
425  av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
426  return ret;
427  }
428 
432 
433  /*
434  * If the client doesn't have a thread-safe get_buffer(),
435  * then decoding threads call back to the main thread,
436  * and it calls back to the client here.
437  */
438 
439  if (!p->avctx->thread_safe_callbacks && (
443  int call_done = 1;
445  while (atomic_load(&p->state) == STATE_SETTING_UP)
447 
448  switch (atomic_load_explicit(&p->state, memory_order_acquire)) {
449  case STATE_GET_BUFFER:
451  break;
452  case STATE_GET_FORMAT:
454  break;
455  default:
456  call_done = 0;
457  break;
458  }
459  if (call_done) {
462  }
464  }
465  }
466 
467  fctx->prev_thread = p;
468  fctx->next_decoding++;
469 
470  return 0;
471 }
472 
474  AVFrame *picture, int *got_picture_ptr,
475  AVPacket *avpkt)
476 {
477  FrameThreadContext *fctx = avctx->internal->thread_ctx;
478  int finished = fctx->next_finished;
479  PerThreadContext *p;
480  int err;
481 
482  /* release the async lock, permitting blocked hwaccel threads to
483  * go forward while we are in this function */
484  async_unlock(fctx);
485 
486  /*
487  * Submit a packet to the next decoding thread.
488  */
489 
490  p = &fctx->threads[fctx->next_decoding];
491  err = submit_packet(p, avctx, avpkt);
492  if (err)
493  goto finish;
494 
495  /*
496  * If we're still receiving the initial packets, don't return a frame.
497  */
498 
499  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
500  fctx->delaying = 0;
501 
502  if (fctx->delaying) {
503  *got_picture_ptr=0;
504  if (avpkt->size) {
505  err = avpkt->size;
506  goto finish;
507  }
508  }
509 
510  /*
511  * Return the next available frame from the oldest thread.
512  * If we're at the end of the stream, then we have to skip threads that
513  * didn't output a frame/error, because we don't want to accidentally signal
514  * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
515  */
516 
517  do {
518  p = &fctx->threads[finished++];
519 
520  if (atomic_load(&p->state) != STATE_INPUT_READY) {
522  while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
525  }
526 
527  av_frame_move_ref(picture, p->frame);
528  *got_picture_ptr = p->got_frame;
529  picture->pkt_dts = p->avpkt.dts;
530  err = p->result;
531 
532  /*
533  * A later call with avkpt->size == 0 may loop over all threads,
534  * including this one, searching for a frame/error to return before being
535  * stopped by the "finished != fctx->next_finished" condition.
536  * Make sure we don't mistakenly return the same frame/error again.
537  */
538  p->got_frame = 0;
539  p->result = 0;
540 
541  if (finished >= avctx->thread_count) finished = 0;
542  } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
543 
544  update_context_from_thread(avctx, p->avctx, 1);
545 
546  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
547 
548  fctx->next_finished = finished;
549 
550  /* return the size of the consumed packet if no error occurred */
551  if (err >= 0)
552  err = avpkt->size;
553 finish:
554  async_lock(fctx);
555  return err;
556 }
557 
559 {
560  PerThreadContext *p;
561  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
562 
563  if (!progress ||
564  atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
565  return;
566 
567  p = f->owner[field]->internal->thread_ctx;
568 
569  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
570  av_log(f->owner[field], AV_LOG_DEBUG,
571  "%p finished %d field %d\n", progress, n, field);
572 
574 
575  atomic_store_explicit(&progress[field], n, memory_order_release);
576 
579 }
580 
582 {
583  PerThreadContext *p;
584  atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
585 
586  if (!progress ||
587  atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
588  return;
589 
590  p = f->owner[field]->internal->thread_ctx;
591 
592  if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
593  av_log(f->owner[field], AV_LOG_DEBUG,
594  "thread awaiting %d field %d from %p\n", n, field, progress);
595 
597  while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
600 }
601 
603  PerThreadContext *p = avctx->internal->thread_ctx;
604 
605  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
606 
607  if (avctx->hwaccel && !p->hwaccel_serializing) {
609  p->hwaccel_serializing = 1;
610  }
611 
612  /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
613  if (avctx->hwaccel &&
615  p->async_serializing = 1;
616 
617  async_lock(p->parent);
618  }
619 
622  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
623  }
624 
626 
629 }
630 
631 /// Waits for all threads to finish.
632 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
633 {
634  int i;
635 
636  async_unlock(fctx);
637 
638  for (i = 0; i < thread_count; i++) {
639  PerThreadContext *p = &fctx->threads[i];
640 
641  if (atomic_load(&p->state) != STATE_INPUT_READY) {
643  while (atomic_load(&p->state) != STATE_INPUT_READY)
646  }
647  p->got_frame = 0;
648  }
649 
650  async_lock(fctx);
651 }
652 
653 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
654 {
655  FrameThreadContext *fctx = avctx->internal->thread_ctx;
656  const AVCodec *codec = avctx->codec;
657  int i;
658 
659  park_frame_worker_threads(fctx, thread_count);
660 
661  if (fctx->prev_thread && avctx->internal->hwaccel_priv_data !=
663  if (update_context_from_thread(avctx, fctx->prev_thread->avctx, 1) < 0) {
664  av_log(avctx, AV_LOG_ERROR, "Failed to update user thread.\n");
665  }
666  }
667 
668  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
669  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
670  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
672  fctx->threads->avctx->internal->is_copy = 1;
673  }
674 
675  for (i = 0; i < thread_count; i++) {
676  PerThreadContext *p = &fctx->threads[i];
677 
679  p->die = 1;
682 
683  if (p->thread_init)
684  pthread_join(p->thread, NULL);
685  p->thread_init=0;
686 
687  if (codec->close && p->avctx)
688  codec->close(p->avctx);
689 
691  av_frame_free(&p->frame);
692  }
693 
694  for (i = 0; i < thread_count; i++) {
695  PerThreadContext *p = &fctx->threads[i];
696 
702  av_packet_unref(&p->avpkt);
704 
705  if (i && p->avctx) {
706  av_freep(&p->avctx->priv_data);
708  }
709 
710  if (p->avctx) {
711  av_freep(&p->avctx->internal);
713  }
714 
715  av_freep(&p->avctx);
716  }
717 
718  av_freep(&fctx->threads);
723 
724  av_freep(&avctx->internal->thread_ctx);
725 
726  if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
727  av_opt_free(avctx->priv_data);
728  avctx->codec = NULL;
729 }
730 
732 {
733  int thread_count = avctx->thread_count;
734  const AVCodec *codec = avctx->codec;
736  FrameThreadContext *fctx;
737  int i, err = 0;
738 
739  if (!thread_count) {
740  int nb_cpus = av_cpu_count();
741 #if FF_API_DEBUG_MV
742  if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
743  nb_cpus = 1;
744 #endif
745  // use number of cores + 1 as thread count if there is more than one
746  if (nb_cpus > 1)
747  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
748  else
749  thread_count = avctx->thread_count = 1;
750  }
751 
752  if (thread_count <= 1) {
753  avctx->active_thread_type = 0;
754  return 0;
755  }
756 
757  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
758  if (!fctx)
759  return AVERROR(ENOMEM);
760 
761  fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
762  if (!fctx->threads) {
763  av_freep(&avctx->internal->thread_ctx);
764  return AVERROR(ENOMEM);
765  }
766 
771 
772  fctx->async_lock = 1;
773  fctx->delaying = 1;
774 
775  for (i = 0; i < thread_count; i++) {
777  PerThreadContext *p = &fctx->threads[i];
778 
784 
785  p->frame = av_frame_alloc();
786  if (!p->frame) {
787  av_freep(&copy);
788  err = AVERROR(ENOMEM);
789  goto error;
790  }
791 
792  p->parent = fctx;
793  p->avctx = copy;
794 
795  if (!copy) {
796  err = AVERROR(ENOMEM);
797  goto error;
798  }
799 
800  *copy = *src;
801 
802  copy->internal = av_malloc(sizeof(AVCodecInternal));
803  if (!copy->internal) {
804  copy->priv_data = NULL;
805  err = AVERROR(ENOMEM);
806  goto error;
807  }
808  *copy->internal = *src->internal;
809  copy->internal->thread_ctx = p;
810  copy->internal->last_pkt_props = &p->avpkt;
811 
812  if (!i) {
813  src = copy;
814 
815  if (codec->init)
816  err = codec->init(copy);
817 
818  update_context_from_thread(avctx, copy, 1);
819  } else {
820  copy->priv_data = av_malloc(codec->priv_data_size);
821  if (!copy->priv_data) {
822  err = AVERROR(ENOMEM);
823  goto error;
824  }
825  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
826  copy->internal->is_copy = 1;
827 
828  if (codec->init_thread_copy)
829  err = codec->init_thread_copy(copy);
830  }
831 
832  if (err) goto error;
833 
834  atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
835 
837  p->thread_init= !err;
838  if(!p->thread_init)
839  goto error;
840  }
841 
842  return 0;
843 
844 error:
845  ff_frame_thread_free(avctx, i+1);
846 
847  return err;
848 }
849 
851 {
852  int i;
853  FrameThreadContext *fctx = avctx->internal->thread_ctx;
854 
855  if (!fctx) return;
856 
858  if (fctx->prev_thread) {
859  if (fctx->prev_thread != &fctx->threads[0])
861  }
862 
863  fctx->next_decoding = fctx->next_finished = 0;
864  fctx->delaying = 1;
865  fctx->prev_thread = NULL;
866  for (i = 0; i < avctx->thread_count; i++) {
867  PerThreadContext *p = &fctx->threads[i];
868  // Make sure decode flush calls with size=0 won't return old frames
869  p->got_frame = 0;
870  av_frame_unref(p->frame);
871  p->result = 0;
872 
874 
875  if (avctx->codec->flush)
876  avctx->codec->flush(p->avctx);
877  }
878 }
879 
881 {
882  PerThreadContext *p = avctx->internal->thread_ctx;
884  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
885  return 0;
886  }
887  return 1;
888 }
889 
891 {
892  PerThreadContext *p = avctx->internal->thread_ctx;
893  int err;
894 
895  f->owner[0] = f->owner[1] = avctx;
896 
897  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
898  return ff_get_buffer(avctx, f->f, flags);
899 
900  if (atomic_load(&p->state) != STATE_SETTING_UP &&
901  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
902  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
903  return -1;
904  }
905 
906  if (avctx->internal->allocate_progress) {
907  atomic_int *progress;
908  f->progress = av_buffer_alloc(2 * sizeof(*progress));
909  if (!f->progress) {
910  return AVERROR(ENOMEM);
911  }
912  progress = (atomic_int*)f->progress->data;
913 
914  atomic_init(&progress[0], -1);
915  atomic_init(&progress[1], -1);
916  }
917 
919  if (THREAD_SAFE_CALLBACKS(avctx)) {
920  err = ff_get_buffer(avctx, f->f, flags);
921  } else {
923  p->requested_frame = f->f;
924  p->requested_flags = flags;
925  atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
927 
928  while (atomic_load(&p->state) != STATE_SETTING_UP)
930 
931  err = p->result;
932 
934 
935  }
936  if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
937  ff_thread_finish_setup(avctx);
938  if (err)
940 
942 
943  return err;
944 }
945 
947 {
948  enum AVPixelFormat res;
949  PerThreadContext *p = avctx->internal->thread_ctx;
950  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
952  return ff_get_format(avctx, fmt);
953  if (atomic_load(&p->state) != STATE_SETTING_UP) {
954  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
955  return -1;
956  }
958  p->available_formats = fmt;
961 
962  while (atomic_load(&p->state) != STATE_SETTING_UP)
964 
965  res = p->result_format;
966 
968 
969  return res;
970 }
971 
973 {
974  int ret = thread_get_buffer_internal(avctx, f, flags);
975  if (ret < 0)
976  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
977  return ret;
978 }
979 
981 {
982  PerThreadContext *p = avctx->internal->thread_ctx;
983  FrameThreadContext *fctx;
984  AVFrame *dst, *tmp;
985  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
986  THREAD_SAFE_CALLBACKS(avctx);
987 
988  if (!f->f || !f->f->buf[0])
989  return;
990 
991  if (avctx->debug & FF_DEBUG_BUFFERS)
992  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
993 
995  f->owner[0] = f->owner[1] = NULL;
996 
997  if (can_direct_free) {
998  av_frame_unref(f->f);
999  return;
1000  }
1001 
1002  fctx = p->parent;
1004 
1005  if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
1006  goto fail;
1008  (p->num_released_buffers + 1) *
1009  sizeof(*p->released_buffers));
1010  if (!tmp)
1011  goto fail;
1012  p->released_buffers = tmp;
1013 
1014  dst = &p->released_buffers[p->num_released_buffers];
1015  av_frame_move_ref(dst, f->f);
1016 
1017  p->num_released_buffers++;
1018 
1019 fail:
1021 }
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1873
int caps_internal
Internal hwaccel capabilities.
Definition: avcodec.h:3877
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:76
#define NULL
Definition: coverity.c:32
int ff_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Select the (possibly hardware accelerated) pixel format.
Definition: decode.c:1370
const struct AVCodec * codec
Definition: avcodec.h:1630
AVRational framerate
Definition: avcodec.h:3161
#define AV_CODEC_PROP_INTRA_ONLY
Codec uses only intra compression.
Definition: avcodec.h:768
const AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition: avcodec.h:3182
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:112
static void copy(const float *p1, float *p2, const int length)
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:126
#define copy_fields(s, e)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
#define pthread_mutex_lock(a)
Definition: ffprobe.c:61
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:192
#define atomic_store(object, desired)
Definition: stdatomic.h:85
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:70
int av_cpu_count(void)
Definition: cpu.c:267
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1809
const char * fmt
Definition: avisynth_c.h:861
atomic_int state
Definition: pthread_frame.c:90
AVPacket * last_pkt_props
Properties (timestamps+side data) extracted from the last packet passed for decoding.
Definition: internal.h:178
static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx, AVPacket *avpkt)
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
AVFrame * f
Definition: thread.h:35
Memory handling functions.
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:486
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:2256
int size
Definition: avcodec.h:1534
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:2000
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1831
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everything contained in src to dst and reset src.
Definition: frame.c:583
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2852
static void error(const char *err)
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:75
intptr_t atomic_int
Definition: stdatomic.h:55
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
const struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2785
#define src
Definition: vp8dsp.c:254
int profile
profile
Definition: avcodec.h:2954
enum AVPixelFormat * available_formats
Format array for get_format()
AVCodec.
Definition: avcodec.h:3555
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:144
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:84
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, &#39;draw_horiz_band&#39; is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1856
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1744
#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: avcodec.h:1024
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int export_side_data
Bit set of AV_CODEC_EXPORT_DATA_* flags, which affects the kind of metadata exported in frame...
Definition: avcodec.h:3446
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: decode.c:1158
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2289
#define av_malloc(s)
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:190
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2797
AVOptions.
#define f(width, name)
Definition: cbs_vp9.c:255
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
void * thread_ctx
Definition: internal.h:169
Multithreading support functions.
#define THREAD_SAFE_CALLBACKS(avctx)
pthread_mutex_t hwaccel_mutex
This lock is used for ensuring threads run in serial when hwaccel is used.
static void finish(void)
Definition: movenc.c:345
int requested_flags
flags passed to get_buffer() for requested_frame
int next_decoding
The next context to submit a packet to.
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:152
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2845
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...
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:2263
Context stored in the client AVCodecInternal thread_ctx.
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:82
#define av_log(a,...)
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:609
int die
Set when the thread should exit.
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
int slice_count
slice count
Definition: avcodec.h:1975
Libavcodec version macros.
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:259
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1911
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
#define atomic_load(object)
Definition: stdatomic.h:93
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:142
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:203
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
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2899
int capabilities
Codec capabilities.
Definition: avcodec.h:3574
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:88
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
const char * arg
Definition: jacosubdec.c:66
int flags
AV_CODEC_FLAG_*.
Definition: avcodec.h:1701
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:50
simple assert() macros that are a bit more flexible than ISO C assert().
#define fail()
Definition: checkasm.h:122
reference-counted frame API
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:2332
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:205
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition: avcodec.h:750
common internal API header
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:77
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2891
#define FFMIN(a, b)
Definition: common.h:96
int width
picture width / height.
Definition: avcodec.h:1794
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2824
AVBufferRef * hw_frames_ctx
A reference to the AVHWFramesContext describing the input (for encoding) or output (decoding) frames...
Definition: avcodec.h:3318
Set when the codec calls get_format().
Definition: pthread_frame.c:62
int priv_data_size
Definition: avcodec.h:3603
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
#define atomic_load_explicit(object, order)
Definition: stdatomic.h:96
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:2235
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:94
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:104
int level
level
Definition: avcodec.h:3074
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:2728
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
Definition: mem.c:476
int64_t reordered_opaque
opaque 64-bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
Definition: avcodec.h:2778
int n
Definition: avisynth_c.h:760
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1753
pthread_t thread
Definition: pthread_frame.c:73
#define pthread_mutex_unlock(a)
Definition: ffprobe.c:65
#define FF_DEBUG_THREADS
Definition: avcodec.h:2729
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2880
if(ret)
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:87
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread&#39;s AVCodecContext with values set by the user.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
AVBufferRef * progress
Definition: thread.h:39
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:80
int(* update_thread_context)(AVCodecContext *dst, const AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
Definition: avcodec.h:3622
Set when the codec calls get_buffer().
Definition: pthread_frame.c:57
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
pthread_cond_t async_cond
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: decode.c:1701
Libavcodec external API header.
enum AVMediaType codec_type
Definition: avcodec.h:1629
enum AVCodecID codec_id
Definition: avcodec.h:1631
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:68
int sample_rate
samples per second
Definition: avcodec.h:2281
int debug
debug
Definition: avcodec.h:2706
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1621
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:600
uint8_t * data
The data buffer.
Definition: buffer.h:89
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1969
int(* close)(AVCodecContext *)
Definition: avcodec.h:3654
int slice_flags
slice flags
Definition: avcodec.h:2099
AVCodecContext * owner[2]
Definition: thread.h:36
int coded_height
Definition: avcodec.h:1809
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:2436
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:2249
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:2242
enum AVPixelFormat result_format
get_format() result
int delaying
Set for the first N packets, where N is the number of threads.
int(* init_thread_copy)(AVCodecContext *)
If defined, called on thread contexts when they are created.
Definition: avcodec.h:3614
refcounted data buffer API
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
#define atomic_store_explicit(object, desired, order)
Definition: stdatomic.h:90
static void async_unlock(FrameThreadContext *fctx)
PerThreadContext * threads
The contexts for each thread.
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:157
static void async_lock(FrameThreadContext *fctx)
#define MAX_AUTO_THREADS
AVFrame * released_buffers
Array of frames passed to ff_thread_release_buffer().
Definition: pthread_frame.c:96
struct FrameThreadContext * parent
Definition: pthread_frame.c:71
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:554
#define flags(name, subs,...)
Definition: cbs_av1.c:564
const AVClass * priv_class
AVClass for the private context.
Definition: avcodec.h:3581
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:404
void av_opt_free(void *obj)
Free all allocated objects in obj.
Definition: opt.c:1607
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
Definition: avcodec.h:3653
common internal api header.
_fmutex pthread_mutex_t
Definition: os2threads.h:53
common internal and external API header
void(* flush)(AVCodecContext *)
Flush buffers.
Definition: avcodec.h:3676
int(* init)(AVCodecContext *)
Definition: avcodec.h:3638
int released_buffers_allocated
Definition: pthread_frame.c:98
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:196
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:94
static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
Update the next thread&#39;s AVCodecContext with values from the reference thread&#39;s context.
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:133
attribute_deprecated AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2871
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
Definition: avcodec.h:2909
#define HWACCEL_CAP_ASYNC_SAFE
Definition: hwaccel.h:26
void * priv_data
Definition: avcodec.h:1648
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:86
int ff_thread_can_start_frame(AVCodecContext *avctx)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:162
int channels
number of audio channels
Definition: avcodec.h:2282
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1656
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:79
int flags2
AV_CODEC_FLAG2_*.
Definition: avcodec.h:1708
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
pthread_mutex_t async_mutex
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1532
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:1991
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:2312
atomic_int debug_threads
Set if the FF_DEBUG_THREADS option is set.
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
#define atomic_init(obj, value)
Definition: stdatomic.h:33
#define av_freep(p)
int hwaccel_flags
Bit set of AV_HWACCEL_FLAG_* flags, which affect hardware accelerated decoding (if active)...
Definition: avcodec.h:3379
int debug_mv
debug motion vectors
Definition: avcodec.h:3249
int next_finished
The next context to return output from.
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
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:342
AVPixelFormat
Pixel format.
Definition: pixfmt.h:64
This structure stores compressed data.
Definition: avcodec.h:1510
int delay
Codec delay.
Definition: avcodec.h:1777
int ff_frame_thread_init(AVCodecContext *avctx)
enum AVPixelFormat sw_pix_fmt
Nominal unaccelerated pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:3168
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1663
void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.c:191
static uint8_t tmp[11]
Definition: aes_ctr.c:26