pthread.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2004 Roman Shaposhnik
3  * Copyright (c) 2008 Alexander Strange (astrange@ithinksw.com)
4  *
5  * Many thanks to Steven M. Schultz for providing clever ideas and
6  * to Michael Niedermayer <michaelni@gmx.at> for writing initial
7  * implementation.
8  *
9  * This file is part of FFmpeg.
10  *
11  * FFmpeg is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * FFmpeg is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with FFmpeg; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25 
26 /**
27  * @file
28  * Multithreading support functions
29  * @see doc/multithreading.txt
30  */
31 
32 #include "config.h"
33 
34 #if HAVE_SCHED_GETAFFINITY
35 #ifndef _GNU_SOURCE
36 # define _GNU_SOURCE
37 #endif
38 #include <sched.h>
39 #endif
40 #if HAVE_GETPROCESSAFFINITYMASK
41 #include <windows.h>
42 #endif
43 #if HAVE_SYSCTL
44 #if HAVE_SYS_PARAM_H
45 #include <sys/param.h>
46 #endif
47 #include <sys/types.h>
48 #include <sys/param.h>
49 #include <sys/sysctl.h>
50 #endif
51 #if HAVE_SYSCONF
52 #include <unistd.h>
53 #endif
54 
55 #include "avcodec.h"
56 #include "internal.h"
57 #include "thread.h"
58 #include "libavutil/avassert.h"
59 #include "libavutil/common.h"
60 
61 #if HAVE_PTHREADS
62 #include <pthread.h>
63 #elif HAVE_W32THREADS
64 #include "w32pthreads.h"
65 #elif HAVE_OS2THREADS
66 #include "os2threads.h"
67 #endif
68 
69 typedef int (action_func)(AVCodecContext *c, void *arg);
70 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
71 
72 typedef struct ThreadContext {
76  void *args;
77  int *rets;
79  int job_count;
80  int job_size;
81 
86  unsigned int current_execute;
87  int done;
89 
90 /**
91  * Context used by codec threads and stored in their AVCodecContext thread_opaque.
92  */
93 typedef struct PerThreadContext {
95 
98  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
99  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
100  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
101 
102  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
103  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
104 
105  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
106 
107  AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
108  uint8_t *buf; ///< backup storage for packet data when the input packet is not refcounted
109  int allocated_buf_size; ///< Size allocated for buf
110 
111  AVFrame frame; ///< Output frame (for decoding) or input (for encoding).
112  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
113  int result; ///< The result of the last codec decode/encode() call.
114 
115  enum {
116  STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
117  STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
118  STATE_GET_BUFFER, /**<
119  * Set when the codec calls get_buffer().
120  * State is returned to STATE_SETTING_UP afterwards.
121  */
122  STATE_GET_FORMAT, /**<
123  * Set when the codec calls get_format().
124  * State is returned to STATE_SETTING_UP afterwards.
125  */
126  STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
127  } state;
128 
129  /**
130  * Array of frames passed to ff_thread_release_buffer().
131  * Frames are released after all threads referencing them are finished.
132  */
136 
137  AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
138  int requested_flags; ///< flags passed to get_buffer() for requested_frame
139 
140  const enum AVPixelFormat *available_formats; ///< Format array for get_format()
141  enum AVPixelFormat result_format; ///< get_format() result
143 
144 /**
145  * Context stored in the client AVCodecContext thread_opaque.
146  */
147 typedef struct FrameThreadContext {
148  PerThreadContext *threads; ///< The contexts for each thread.
149  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
150 
151  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
152 
153  int next_decoding; ///< The next context to submit a packet to.
154  int next_finished; ///< The next context to return output from.
155 
156  int delaying; /**<
157  * Set for the first N packets, where N is the number of threads.
158  * While it is set, ff_thread_en/decode_frame won't return any results.
159  */
160 
161  int die; ///< Set when threads should exit.
163 
164 
165 /* H264 slice threading seems to be buggy with more than 16 threads,
166  * limit the number of threads to 16 for automatic detection */
167 #define MAX_AUTO_THREADS 16
168 
170 {
171  int ret, nb_cpus = 1;
172 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
173  cpu_set_t cpuset;
174 
175  CPU_ZERO(&cpuset);
176 
177  ret = sched_getaffinity(0, sizeof(cpuset), &cpuset);
178  if (!ret) {
179  nb_cpus = CPU_COUNT(&cpuset);
180  }
181 #elif HAVE_GETPROCESSAFFINITYMASK
182  DWORD_PTR proc_aff, sys_aff;
183  ret = GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff);
184  if (ret)
185  nb_cpus = av_popcount64(proc_aff);
186 #elif HAVE_SYSCTL && defined(HW_NCPU)
187  int mib[2] = { CTL_HW, HW_NCPU };
188  size_t len = sizeof(nb_cpus);
189 
190  ret = sysctl(mib, 2, &nb_cpus, &len, NULL, 0);
191  if (ret == -1)
192  nb_cpus = 0;
193 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
194  nb_cpus = sysconf(_SC_NPROC_ONLN);
195 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
196  nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
197 #endif
198  av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
199 
200  if (avctx->height)
201  nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
202 
203  return nb_cpus;
204 }
205 
206 
207 static void* attribute_align_arg worker(void *v)
208 {
209  AVCodecContext *avctx = v;
210  ThreadContext *c = avctx->thread_opaque;
211  int our_job = c->job_count;
212  int last_execute = 0;
213  int thread_count = avctx->thread_count;
214  int self_id;
215 
217  self_id = c->current_job++;
218  for (;;){
219  while (our_job >= c->job_count) {
220  if (c->current_job == thread_count + c->job_count)
222 
223  while (last_execute == c->current_execute && !c->done)
225  last_execute = c->current_execute;
226  our_job = self_id;
227 
228  if (c->done) {
230  return NULL;
231  }
232  }
234 
235  c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
236  c->func2(avctx, c->args, our_job, self_id);
237 
239  our_job = c->current_job++;
240  }
241 }
242 
244 {
245  while (c->current_job != thread_count + c->job_count)
248 }
249 
250 static void thread_free(AVCodecContext *avctx)
251 {
252  ThreadContext *c = avctx->thread_opaque;
253  int i;
254 
256  c->done = 1;
259 
260  for (i=0; i<avctx->thread_count; i++)
261  pthread_join(c->workers[i], NULL);
262 
266  av_free(c->workers);
267  av_freep(&avctx->thread_opaque);
268 }
269 
270 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
271 {
272  ThreadContext *c= avctx->thread_opaque;
273  int dummy_ret;
274 
275  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
276  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
277 
278  if (job_count <= 0)
279  return 0;
280 
282 
283  c->current_job = avctx->thread_count;
284  c->job_count = job_count;
285  c->job_size = job_size;
286  c->args = arg;
287  c->func = func;
288  if (ret) {
289  c->rets = ret;
290  c->rets_count = job_count;
291  } else {
292  c->rets = &dummy_ret;
293  c->rets_count = 1;
294  }
295  c->current_execute++;
297 
299 
300  return 0;
301 }
302 
304 {
305  ThreadContext *c= avctx->thread_opaque;
306  c->func2 = func2;
307  return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
308 }
309 
310 static int thread_init(AVCodecContext *avctx)
311 {
312  int i;
313  ThreadContext *c;
314  int thread_count = avctx->thread_count;
315 
316  if (!thread_count) {
317  int nb_cpus = ff_get_logical_cpus(avctx);
318  // use number of cores + 1 as thread count if there is more than one
319  if (nb_cpus > 1)
320  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
321  else
322  thread_count = avctx->thread_count = 1;
323  }
324 
325  if (thread_count <= 1) {
326  avctx->active_thread_type = 0;
327  return 0;
328  }
329 
330  c = av_mallocz(sizeof(ThreadContext));
331  if (!c)
332  return -1;
333 
334  c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
335  if (!c->workers) {
336  av_free(c);
337  return -1;
338  }
339 
340  avctx->thread_opaque = c;
341  c->current_job = 0;
342  c->job_count = 0;
343  c->job_size = 0;
344  c->done = 0;
349  for (i=0; i<thread_count; i++) {
350  if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
351  avctx->thread_count = i;
353  ff_thread_free(avctx);
354  return -1;
355  }
356  }
357 
358  avcodec_thread_park_workers(c, thread_count);
359 
362  return 0;
363 }
364 
365 /**
366  * Codec worker thread.
367  *
368  * Automatically calls ff_thread_finish_setup() if the codec does
369  * not provide an update_thread_context method, or if the codec returns
370  * before calling it.
371  */
373 {
374  PerThreadContext *p = arg;
375  FrameThreadContext *fctx = p->parent;
376  AVCodecContext *avctx = p->avctx;
377  const AVCodec *codec = avctx->codec;
378 
380  while (1) {
381  while (p->state == STATE_INPUT_READY && !fctx->die)
383 
384  if (fctx->die) break;
385 
386  if (!codec->update_thread_context && (avctx->thread_safe_callbacks || (
388  !avctx->get_buffer &&
389 #endif
391  ff_thread_finish_setup(avctx);
392 
394  p->got_frame = 0;
395  p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
396 
397  /* many decoders assign whole AVFrames, thus overwriting extended_data;
398  * make sure it's set correctly */
399  p->frame.extended_data = p->frame.data;
400 
401  if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
402 
404 #if 0 //BUFREF-FIXME
405  for (i = 0; i < MAX_BUFFERS; i++)
406  if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
407  p->progress[i][0] = INT_MAX;
408  p->progress[i][1] = INT_MAX;
409  }
410 #endif
411  p->state = STATE_INPUT_READY;
412 
416  }
418 
419  return NULL;
420 }
421 
422 /**
423  * Update the next thread's AVCodecContext with values from the reference thread's context.
424  *
425  * @param dst The destination context.
426  * @param src The source context.
427  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
428  */
430 {
431  int err = 0;
432 
433  if (dst != src) {
434  dst->time_base = src->time_base;
435  dst->width = src->width;
436  dst->height = src->height;
437  dst->pix_fmt = src->pix_fmt;
438 
439  dst->coded_width = src->coded_width;
440  dst->coded_height = src->coded_height;
441 
442  dst->has_b_frames = src->has_b_frames;
443  dst->idct_algo = src->idct_algo;
444 
448 
449  dst->profile = src->profile;
450  dst->level = src->level;
451 
453  dst->ticks_per_frame = src->ticks_per_frame;
454  dst->color_primaries = src->color_primaries;
455 
456  dst->color_trc = src->color_trc;
457  dst->colorspace = src->colorspace;
458  dst->color_range = src->color_range;
460 
461  dst->hwaccel = src->hwaccel;
462  dst->hwaccel_context = src->hwaccel_context;
463  }
464 
465  if (for_user) {
466  dst->delay = src->thread_count - 1;
467  dst->coded_frame = src->coded_frame;
468  } else {
469  if (dst->codec->update_thread_context)
470  err = dst->codec->update_thread_context(dst, src);
471  }
472 
473  return err;
474 }
475 
476 /**
477  * Update the next thread's AVCodecContext with values set by the user.
478  *
479  * @param dst The destination context.
480  * @param src The source context.
481  * @return 0 on success, negative error code on failure
482  */
484 {
485 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
486  dst->flags = src->flags;
487 
488  dst->draw_horiz_band= src->draw_horiz_band;
489  dst->get_buffer2 = src->get_buffer2;
490 #if FF_API_GET_BUFFER
491  dst->get_buffer = src->get_buffer;
492  dst->release_buffer = src->release_buffer;
493 #endif
494 
495  dst->opaque = src->opaque;
496  dst->debug = src->debug;
497  dst->debug_mv = src->debug_mv;
498 
499  dst->slice_flags = src->slice_flags;
500  dst->flags2 = src->flags2;
501 
502  copy_fields(skip_loop_filter, subtitle_header);
503 
504  dst->frame_number = src->frame_number;
507 
508  if (src->slice_count && src->slice_offset) {
509  if (dst->slice_count < src->slice_count) {
510  int *tmp = av_realloc(dst->slice_offset, src->slice_count *
511  sizeof(*dst->slice_offset));
512  if (!tmp) {
513  av_free(dst->slice_offset);
514  return AVERROR(ENOMEM);
515  }
516  dst->slice_offset = tmp;
517  }
518  memcpy(dst->slice_offset, src->slice_offset,
519  src->slice_count * sizeof(*dst->slice_offset));
520  }
521  dst->slice_count = src->slice_count;
522  return 0;
523 #undef copy_fields
524 }
525 
526 /// Releases the buffers that this decoding thread was the last user of.
528 {
529  FrameThreadContext *fctx = p->parent;
530 
531  while (p->num_released_buffers > 0) {
532  AVFrame *f;
533 
535 
536  // fix extended data in case the caller screwed it up
539  f->extended_data = f->data;
540  av_frame_unref(f);
541 
543  }
544 }
545 
547 {
548  FrameThreadContext *fctx = p->parent;
549  PerThreadContext *prev_thread = fctx->prev_thread;
550  const AVCodec *codec = p->avctx->codec;
551 
552  if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
553 
555 
557 
558  if (prev_thread) {
559  int err;
560  if (prev_thread->state == STATE_SETTING_UP) {
561  pthread_mutex_lock(&prev_thread->progress_mutex);
562  while (prev_thread->state == STATE_SETTING_UP)
563  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
564  pthread_mutex_unlock(&prev_thread->progress_mutex);
565  }
566 
567  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
568  if (err) {
570  return err;
571  }
572  }
573 
575  p->avpkt = *avpkt;
576  if (avpkt->buf)
577  p->avpkt.buf = av_buffer_ref(avpkt->buf);
578  else {
580  p->avpkt.data = p->buf;
581  memcpy(p->buf, avpkt->data, avpkt->size);
582  memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
583  }
584 
585  p->state = STATE_SETTING_UP;
588 
589  /*
590  * If the client doesn't have a thread-safe get_buffer(),
591  * then decoding threads call back to the main thread,
592  * and it calls back to the client here.
593  */
594 
595  if (!p->avctx->thread_safe_callbacks && (
598  p->avctx->get_buffer ||
599 #endif
601  while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
602  int call_done = 1;
604  while (p->state == STATE_SETTING_UP)
606 
607  switch (p->state) {
608  case STATE_GET_BUFFER:
610  break;
611  case STATE_GET_FORMAT:
613  break;
614  default:
615  call_done = 0;
616  break;
617  }
618  if (call_done) {
619  p->state = STATE_SETTING_UP;
621  }
623  }
624  }
625 
626  fctx->prev_thread = p;
627  fctx->next_decoding++;
628 
629  return 0;
630 }
631 
633  AVFrame *picture, int *got_picture_ptr,
634  AVPacket *avpkt)
635 {
636  FrameThreadContext *fctx = avctx->thread_opaque;
637  int finished = fctx->next_finished;
638  PerThreadContext *p;
639  int err;
640 
641  /*
642  * Submit a packet to the next decoding thread.
643  */
644 
645  p = &fctx->threads[fctx->next_decoding];
646  err = update_context_from_user(p->avctx, avctx);
647  if (err) return err;
648  err = submit_packet(p, avpkt);
649  if (err) return err;
650 
651  /*
652  * If we're still receiving the initial packets, don't return a frame.
653  */
654 
655  if (fctx->delaying) {
656  if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
657 
658  *got_picture_ptr=0;
659  if (avpkt->size)
660  return avpkt->size;
661  }
662 
663  /*
664  * Return the next available frame from the oldest thread.
665  * If we're at the end of the stream, then we have to skip threads that
666  * didn't output a frame, because we don't want to accidentally signal
667  * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
668  */
669 
670  do {
671  p = &fctx->threads[finished++];
672 
673  if (p->state != STATE_INPUT_READY) {
675  while (p->state != STATE_INPUT_READY)
678  }
679 
680  av_frame_move_ref(picture, &p->frame);
681  *got_picture_ptr = p->got_frame;
682  picture->pkt_dts = p->avpkt.dts;
683 
684  /*
685  * A later call with avkpt->size == 0 may loop over all threads,
686  * including this one, searching for a frame to return before being
687  * stopped by the "finished != fctx->next_finished" condition.
688  * Make sure we don't mistakenly return the same frame again.
689  */
690  p->got_frame = 0;
691 
692  if (finished >= avctx->thread_count) finished = 0;
693  } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
694 
695  update_context_from_thread(avctx, p->avctx, 1);
696 
697  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
698 
699  fctx->next_finished = finished;
700 
701  /* return the size of the consumed packet if no error occurred */
702  return (p->result >= 0) ? avpkt->size : p->result;
703 }
704 
705 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
706 {
707  PerThreadContext *p;
708  volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
709 
710  if (!progress || progress[field] >= n) return;
711 
712  p = f->owner->thread_opaque;
713 
714  if (f->owner->debug&FF_DEBUG_THREADS)
715  av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
716 
718  progress[field] = n;
721 }
722 
723 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
724 {
725  PerThreadContext *p;
726  volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
727 
728  if (!progress || progress[field] >= n) return;
729 
730  p = f->owner->thread_opaque;
731 
732  if (f->owner->debug&FF_DEBUG_THREADS)
733  av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
734 
736  while (progress[field] < n)
739 }
740 
742  PerThreadContext *p = avctx->thread_opaque;
743 
744  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
745 
746  if(p->state == STATE_SETUP_FINISHED){
747  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
748  }
749 
751  p->state = STATE_SETUP_FINISHED;
754 }
755 
756 /// Waits for all threads to finish.
757 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
758 {
759  int i;
760 
761  for (i = 0; i < thread_count; i++) {
762  PerThreadContext *p = &fctx->threads[i];
763 
764  if (p->state != STATE_INPUT_READY) {
766  while (p->state != STATE_INPUT_READY)
769  }
770  p->got_frame = 0;
771  }
772 }
773 
774 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
775 {
776  FrameThreadContext *fctx = avctx->thread_opaque;
777  const AVCodec *codec = avctx->codec;
778  int i;
779 
780  park_frame_worker_threads(fctx, thread_count);
781 
782  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
783  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
784  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
786  fctx->threads->avctx->internal->is_copy = 1;
787  }
788 
789  fctx->die = 1;
790 
791  for (i = 0; i < thread_count; i++) {
792  PerThreadContext *p = &fctx->threads[i];
793 
797 
798  if (p->thread_init)
799  pthread_join(p->thread, NULL);
800  p->thread_init=0;
801 
802  if (codec->close)
803  codec->close(p->avctx);
804 
805  avctx->codec = NULL;
806 
808  av_frame_unref(&p->frame);
809  }
810 
811  for (i = 0; i < thread_count; i++) {
812  PerThreadContext *p = &fctx->threads[i];
813 
820  av_freep(&p->buf);
822 
823  if (i) {
824  av_freep(&p->avctx->priv_data);
825  av_freep(&p->avctx->internal);
827  }
828 
829  av_freep(&p->avctx);
830  }
831 
832  av_freep(&fctx->threads);
834  av_freep(&avctx->thread_opaque);
835 }
836 
838 {
839  int thread_count = avctx->thread_count;
840  const AVCodec *codec = avctx->codec;
841  AVCodecContext *src = avctx;
842  FrameThreadContext *fctx;
843  int i, err = 0;
844 
845  if (!thread_count) {
846  int nb_cpus = ff_get_logical_cpus(avctx);
847  if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
848  nb_cpus = 1;
849  // use number of cores + 1 as thread count if there is more than one
850  if (nb_cpus > 1)
851  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
852  else
853  thread_count = avctx->thread_count = 1;
854  }
855 
856  if (thread_count <= 1) {
857  avctx->active_thread_type = 0;
858  return 0;
859  }
860 
861  avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
862 
863  fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
865  fctx->delaying = 1;
866 
867  for (i = 0; i < thread_count; i++) {
869  PerThreadContext *p = &fctx->threads[i];
870 
876 
877  p->parent = fctx;
878  p->avctx = copy;
879 
880  if (!copy) {
881  err = AVERROR(ENOMEM);
882  goto error;
883  }
884 
885  *copy = *src;
886  copy->thread_opaque = p;
887  copy->pkt = &p->avpkt;
888 
889  if (!i) {
890  src = copy;
891 
892  if (codec->init)
893  err = codec->init(copy);
894 
895  update_context_from_thread(avctx, copy, 1);
896  } else {
897  copy->priv_data = av_malloc(codec->priv_data_size);
898  if (!copy->priv_data) {
899  err = AVERROR(ENOMEM);
900  goto error;
901  }
902  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
903  copy->internal = av_malloc(sizeof(AVCodecInternal));
904  if (!copy->internal) {
905  err = AVERROR(ENOMEM);
906  goto error;
907  }
908  *copy->internal = *src->internal;
909  copy->internal->is_copy = 1;
910 
911  if (codec->init_thread_copy)
912  err = codec->init_thread_copy(copy);
913  }
914 
915  if (err) goto error;
916 
918  p->thread_init= !err;
919  if(!p->thread_init)
920  goto error;
921  }
922 
923  return 0;
924 
925 error:
926  frame_thread_free(avctx, i+1);
927 
928  return err;
929 }
930 
932 {
933  int i;
934  FrameThreadContext *fctx = avctx->thread_opaque;
935 
936  if (!avctx->thread_opaque) return;
937 
939  if (fctx->prev_thread) {
940  if (fctx->prev_thread != &fctx->threads[0])
942  if (avctx->codec->flush)
943  avctx->codec->flush(fctx->threads[0].avctx);
944  }
945 
946  fctx->next_decoding = fctx->next_finished = 0;
947  fctx->delaying = 1;
948  fctx->prev_thread = NULL;
949  for (i = 0; i < avctx->thread_count; i++) {
950  PerThreadContext *p = &fctx->threads[i];
951  // Make sure decode flush calls with size=0 won't return old frames
952  p->got_frame = 0;
953  av_frame_unref(&p->frame);
954 
956  }
957 }
958 
960 {
961  PerThreadContext *p = avctx->thread_opaque;
962  if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
963  (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && (
965  avctx->get_buffer ||
966 #endif
968  return 0;
969  }
970  return 1;
971 }
972 
974 {
975  PerThreadContext *p = avctx->thread_opaque;
976  int err;
977 
978  f->owner = avctx;
979 
980  ff_init_buffer_info(avctx, f->f);
981 
982  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
983  return ff_get_buffer(avctx, f->f, flags);
984 
985  if (p->state != STATE_SETTING_UP &&
986  (avctx->codec->update_thread_context || (!avctx->thread_safe_callbacks && (
988  avctx->get_buffer ||
989 #endif
991  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
992  return -1;
993  }
994 
995  if (avctx->internal->allocate_progress) {
996  int *progress;
997  f->progress = av_buffer_alloc(2 * sizeof(int));
998  if (!f->progress) {
999  return AVERROR(ENOMEM);
1000  }
1001  progress = (int*)f->progress->data;
1002 
1003  progress[0] = progress[1] = -1;
1004  }
1005 
1007 
1008  if (avctx->thread_safe_callbacks || (
1010  !avctx->get_buffer &&
1011 #endif
1013  err = ff_get_buffer(avctx, f->f, flags);
1014  } else {
1016  p->requested_frame = f->f;
1017  p->requested_flags = flags;
1018  p->state = STATE_GET_BUFFER;
1020 
1021  while (p->state != STATE_SETTING_UP)
1023 
1024  err = p->result;
1025 
1027 
1028  if (!avctx->codec->update_thread_context)
1029  ff_thread_finish_setup(avctx);
1030  }
1031 
1032  if (err)
1034 
1036 
1037  return err;
1038 }
1039 
1041 {
1042  enum AVPixelFormat res;
1043  PerThreadContext *p = avctx->thread_opaque;
1044  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
1046  return avctx->get_format(avctx, fmt);
1047  if (p->state != STATE_SETTING_UP) {
1048  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
1049  return -1;
1050  }
1052  p->available_formats = fmt;
1053  p->state = STATE_GET_FORMAT;
1055 
1056  while (p->state != STATE_SETTING_UP)
1058 
1059  res = p->result_format;
1060 
1062 
1063  return res;
1064 }
1065 
1067 {
1068  int ret = thread_get_buffer_internal(avctx, f, flags);
1069  if (ret < 0)
1070  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1071  return ret;
1072 }
1073 
1075 {
1076  PerThreadContext *p = avctx->thread_opaque;
1077  FrameThreadContext *fctx;
1078  AVFrame *dst, *tmp;
1079  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1080  avctx->thread_safe_callbacks ||
1081  (
1083  !avctx->get_buffer &&
1084 #endif
1086 
1087  if (!f->f->data[0])
1088  return;
1089 
1090  if (avctx->debug & FF_DEBUG_BUFFERS)
1091  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1092 
1094  f->owner = NULL;
1095 
1096  if (can_direct_free) {
1097  av_frame_unref(f->f);
1098  return;
1099  }
1100 
1101  fctx = p->parent;
1103 
1104  if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
1105  goto fail;
1107  (p->num_released_buffers + 1) *
1108  sizeof(*p->released_buffers));
1109  if (!tmp)
1110  goto fail;
1111  p->released_buffers = tmp;
1112 
1113  dst = &p->released_buffers[p->num_released_buffers];
1114  av_frame_move_ref(dst, f->f);
1115 
1116  p->num_released_buffers++;
1117 
1118 fail:
1120 }
1121 
1122 /**
1123  * Set the threading algorithms used.
1124  *
1125  * Threading requires more than one thread.
1126  * Frame threading requires entire frames to be passed to the codec,
1127  * and introduces extra decoding delay, so is incompatible with low_delay.
1128  *
1129  * @param avctx The context.
1130  */
1132 {
1133  int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
1134  && !(avctx->flags & CODEC_FLAG_TRUNCATED)
1135  && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
1136  && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
1137  if (avctx->thread_count == 1) {
1138  avctx->active_thread_type = 0;
1139  } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
1141  } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
1142  avctx->thread_type & FF_THREAD_SLICE) {
1144  } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) {
1145  avctx->thread_count = 1;
1146  avctx->active_thread_type = 0;
1147  }
1148 
1149  if (avctx->thread_count > MAX_AUTO_THREADS)
1150  av_log(avctx, AV_LOG_WARNING,
1151  "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
1152  avctx->thread_count, MAX_AUTO_THREADS);
1153 }
1154 
1156 {
1157  if (avctx->thread_opaque) {
1158  av_log(avctx, AV_LOG_ERROR, "avcodec_thread_init is ignored after avcodec_open\n");
1159  return -1;
1160  }
1161 
1162 #if HAVE_W32THREADS
1163  w32thread_init();
1164 #endif
1165 
1166  if (avctx->codec) {
1168 
1170  return thread_init(avctx);
1171  else if (avctx->active_thread_type&FF_THREAD_FRAME)
1172  return frame_thread_init(avctx);
1173  }
1174 
1175  return 0;
1176 }
1177 
1179 {
1181  frame_thread_free(avctx, avctx->thread_count);
1182  else
1183  thread_free(avctx);
1184 }
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread.c:99
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:205
const struct AVCodec * codec
float v
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:90
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
struct ThreadContext ThreadContext
This structure describes decoded (raw) audio or video data.
Definition: frame.h:76
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread.c:723
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:149
#define FF_DEBUG_VIS_QP
pthread_t * workers
Definition: pthread.c:73
Context used by codec threads and stored in their AVCodecContext thread_opaque.
Definition: pthread.c:93
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
Definition: pthread.c:372
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
Definition: pthread.c:137
int coded_width
Bitstream width / height, may be different from width/height e.g.
const char * fmt
Definition: avisynth_c.h:669
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.
Definition: pthread.c:429
AVFrame * f
Definition: thread.h:36
static int avcodec_thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
Definition: pthread.c:270
AVFrame * coded_frame
the picture in the bitstream
Set before the codec has called ff_thread_finish_setup().
Definition: pthread.c:117
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:154
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Sinusoidal phase f
AVRational sample_aspect_ratio
sample aspect ratio (0 if unknown) That is the width of a pixel divided by the height of the pixel...
os2threads to pthreads wrapper
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
void ff_thread_free(AVCodecContext *avctx)
Definition: pthread.c:1178
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:141
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
Definition: pthread.c:973
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
#define FF_DEBUG_VIS_MB_TYPE
pthread_cond_t last_job_cond
Definition: pthread.c:82
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread.c:98
action_func2 * func2
Definition: pthread.c:75
int( action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr)
Definition: pthread.c:70
enum AVPixelFormat * available_formats
Format array for get_format()
Definition: pthread.c:140
static av_always_inline int pthread_cond_destroy(pthread_cond_t *cond)
Definition: os2threads.h:120
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread.c:107
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...
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:198
void * args
Definition: pthread.c:76
initialize output if(nPeaks >3)%at least 3 peaks in spectrum for trying to find f0 nf0peaks
int job_count
Definition: pthread.c:79
struct AVHWAccel * hwaccel
Hardware accelerator in use.
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
Definition: pthread.c:303
#define CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
HMTX pthread_mutex_t
Definition: os2threads.h:38
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
uint8_t
void * hwaccel_context
Hardware accelerator context.
#define FF_DEBUG_THREADS
action_func * func
Definition: pthread.c:74
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
Definition: pthread.c:632
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:352
uint8_t * data
int requested_flags
flags passed to get_buffer() for requested_frame
Definition: pthread.c:138
int next_decoding
The next context to submit a packet to.
Definition: pthread.c:153
AVFrame frame
Output frame (for decoding) or input (for encoding).
Definition: pthread.c:111
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:127
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
Definition: pthread.c:1040
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
Definition: pthread.c:243
int( action_func)(AVCodecContext *c, void *arg)
Definition: pthread.c:69
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
static void validate_thread_parameters(AVCodecContext *avctx)
Set the threading algorithms used.
Definition: pthread.c:1131
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Context stored in the client AVCodecContext thread_opaque.
Definition: pthread.c:147
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread.c:105
AVCodecContext * owner
Definition: thread.h:37
int slice_count
slice count
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
Definition: pthread.c:741
#define CODEC_FLAG_TRUNCATED
int has_b_frames
Size of the frame reordering buffer in the decoder.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:183
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
Definition: pthread.c:149
uint8_t * buf
backup storage for packet data when the input packet is not refcounted
Definition: pthread.c:108
Multithreading support functions.
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given block if it is not large enough, otherwise do nothing.
#define CODEC_CAP_DELAY
Encoder or decoder requires flushing with NULL input at the end in order to give the complete and cor...
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
static void frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread.c:774
#define MAX_AUTO_THREADS
Definition: pthread.c:167
int active_thread_type
Which multithreading methods are in use by the codec.
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
Definition: pthread.c:757
int capabilities
Codec capabilities.
int ff_thread_init(AVCodecContext *avctx)
Definition: pthread.c:1155
int result
The result of the last codec decode/encode() call.
Definition: pthread.c:113
int rets_count
Definition: pthread.c:78
int current_job
Definition: pthread.c:85
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
const char * arg
int flags
CODEC_FLAG_*.
simple assert() macros that are a bit more flexible than ISO C assert().
void av_log(void *avcl, int level, const char *fmt,...)
Definition: log.c:246
int die
Set when threads should exit.
Definition: pthread.c:161
void * thread_opaque
thread opaque Can be used by execute() to store some per AVCodecContext stuff.
external API header
#define CODEC_FLAG_LOW_DELAY
Force low delay.
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread.c:100
int job_size
Definition: pthread.c:80
#define FF_API_GET_BUFFER
#define FFMIN(a, b)
Definition: common.h:58
ret
Definition: avfilter.c:821
int width
picture width / height.
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
pthread_mutex_t current_job_lock
Definition: pthread.c:84
#define FF_DEBUG_BUFFERS
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx->thread_count == 0 (auto).
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
static av_always_inline int pthread_join(pthread_t thread, void **value_ptr)
Definition: os2threads.h:76
static av_always_inline int pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *attr)
Definition: os2threads.h:83
int num_released_buffers
Definition: pthread.c:134
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread&#39;s AVCodecContext with values set by the user.
Definition: pthread.c:483
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
pthread_t thread
Definition: pthread.c:96
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread.c:112
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread.c:151
pthread_cond_t current_job_cond
Definition: pthread.c:83
AVBufferRef * progress
Definition: thread.h:40
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread.c:103
int(* update_thread_context)(AVCodecContext *dst, const AVCodecContext *src)
Copy necessary context variables from a previous thread context to the current one.
#define attribute_align_arg
static av_always_inline int pthread_create(pthread_t *thread, const pthread_attr_t *attr, void *(*start_routine)(void *), void *arg)
Definition: os2threads.h:62
int(* execute2)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg, int jobnr, int threadnr), void *arg2, int *ret, int count)
The codec may call this to execute several independent things.
NULL
Definition: eval.c:55
struct PerThreadContext PerThreadContext
Context used by codec threads and stored in their AVCodecContext thread_opaque.
AVS_Value src
Definition: avisynth_c.h:523
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
enum AVMediaType codec_type
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
enum AVCodecID codec_id
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
main external API structure.
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.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:148
int(* close)(AVCodecContext *)
int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
does needed setup of pkt_pts/pos and such for (re)get_buffer();
#define FF_THREAD_FRAME
Decode more than one frame at once.
int slice_flags
slice flags
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
pthread_t worker[MAX_THREADS]
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.
static void thread_free(AVCodecContext *avctx)
Definition: pthread.c:250
synthesis window for stochastic i
enum AVColorSpace colorspace
YUV colorspace type.
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
enum AVPixelFormat result_format
get_format() result
Definition: pthread.c:141
int delaying
Set for the first N packets, where N is the number of threads.
Definition: pthread.c:156
int(* init_thread_copy)(AVCodecContext *)
If defined, called on thread contexts when they are created.
enum PerThreadContext::@83 state
#define copy_fields(s, e)
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:330
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread.c:931
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread.c:1066
PerThreadContext * threads
The contexts for each thread.
Definition: pthread.c:148
int allocate_progress
Whether to allocate progress for frame threading.
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFilterBuffer 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.Buffer references ownership and permissions
AVFrame * released_buffers
Array of frames passed to ff_thread_release_buffer().
Definition: pthread.c:133
struct FrameThreadContext * parent
Definition: pthread.c:94
static uint32_t state
Definition: trasher.c:27
static int flags
Definition: cpu.c:23
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:87
Set when the thread is awaiting a packet.
Definition: pthread.c:116
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:171
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
int ff_get_logical_cpus(AVCodecContext *avctx)
Definition: pthread.c:169
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
common internal api header.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:162
common internal and external API header
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
static int thread_init(AVCodecContext *avctx)
Definition: pthread.c:310
unsigned int current_execute
Definition: pthread.c:86
void(* flush)(AVCodecContext *)
Flush buffers.
int(* init)(AVCodecContext *)
static double c[64]
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
Definition: pthread.c:546
int released_buffers_allocated
Definition: pthread.c:135
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
static av_always_inline int pthread_cond_init(pthread_cond_t *cond, const pthread_condattr_t *attr)
Definition: os2threads.h:111
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
static int frame_thread_init(AVCodecContext *avctx)
Definition: pthread.c:837
struct FrameThreadContext FrameThreadContext
Context stored in the client AVCodecContext thread_opaque.
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
Definition: pthread.c:705
int len
static void w32thread_init(void)
Definition: w32pthreads.h:258
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:138
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:104
struct AVCodecInternal * internal
Private context used for internal data.
else dst[i][x+y *dst_stride[i]]
Definition: vf_mcdeint.c:160
DWORD * DWORD_PTR
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread.c:102
w32threads to pthreads wrapper
int flags2
CODEC_FLAG2_*.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
Definition: pthread.c:527
int * slice_offset
slice offsets in the frame in bytes
int frame_number
Frame counter, set by libavcodec.
int * rets
Definition: pthread.c:77
#define av_always_inline
Definition: attributes.h:41
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:97
int next_finished
The next context to return output from.
Definition: pthread.c:154
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread.c:1074
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:117
int(* execute)(struct AVCodecContext *c, int(*func)(struct AVCodecContext *c2, void *arg), void *arg2, int *ret, int count, int size)
The codec may call this to execute several independent things.
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
This structure stores compressed data.
int allocated_buf_size
Size allocated for buf.
Definition: pthread.c:109
int delay
Codec delay.
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread.c:959
void * opaque
Private data of the user, can be used to carry app specific stuff.
int thread_type
Which multithreading methods to use.
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)