FFmpeg  2.1.1
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 #include "avcodec.h"
35 #include "internal.h"
36 #include "thread.h"
37 #include "libavutil/avassert.h"
38 #include "libavutil/common.h"
39 #include "libavutil/cpu.h"
40 #include "libavutil/internal.h"
41 
42 #if HAVE_PTHREADS
43 #include <pthread.h>
44 #elif HAVE_W32THREADS
45 #include "compat/w32pthreads.h"
46 #elif HAVE_OS2THREADS
47 #include "compat/os2threads.h"
48 #endif
49 
50 typedef int (action_func)(AVCodecContext *c, void *arg);
51 typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
52 
53 typedef struct ThreadContext {
57  void *args;
58  int *rets;
60  int job_count;
61  int job_size;
62 
66  unsigned current_execute;
68  int done;
69 
70  int *entries;
76 
77 /**
78  * Context used by codec threads and stored in their AVCodecContext thread_opaque.
79  */
80 typedef struct PerThreadContext {
82 
85  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
86  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
87  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
88 
89  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
90  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
91 
92  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
93 
94  AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
95  uint8_t *buf; ///< backup storage for packet data when the input packet is not refcounted
96  int allocated_buf_size; ///< Size allocated for buf
97 
98  AVFrame frame; ///< Output frame (for decoding) or input (for encoding).
99  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
100  int result; ///< The result of the last codec decode/encode() call.
101 
102  enum {
103  STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
104  STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
106  * Set when the codec calls get_buffer().
107  * State is returned to STATE_SETTING_UP afterwards.
108  */
110  * Set when the codec calls get_format().
111  * State is returned to STATE_SETTING_UP afterwards.
112  */
113  STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
114  } state;
115 
116  /**
117  * Array of frames passed to ff_thread_release_buffer().
118  * Frames are released after all threads referencing them are finished.
119  */
123 
124  AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
125  int requested_flags; ///< flags passed to get_buffer() for requested_frame
126 
127  const enum AVPixelFormat *available_formats; ///< Format array for get_format()
128  enum AVPixelFormat result_format; ///< get_format() result
130 
131 /**
132  * Context stored in the client AVCodecContext thread_opaque.
133  */
134 typedef struct FrameThreadContext {
135  PerThreadContext *threads; ///< The contexts for each thread.
136  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
137 
138  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
139 
140  int next_decoding; ///< The next context to submit a packet to.
141  int next_finished; ///< The next context to return output from.
142 
143  int delaying; /**<
144  * Set for the first N packets, where N is the number of threads.
145  * While it is set, ff_thread_en/decode_frame won't return any results.
146  */
147 
148  int die; ///< Set when threads should exit.
150 
151 
152 /* H264 slice threading seems to be buggy with more than 16 threads,
153  * limit the number of threads to 16 for automatic detection */
154 #define MAX_AUTO_THREADS 16
155 
156 static void* attribute_align_arg worker(void *v)
157 {
158  AVCodecContext *avctx = v;
159  ThreadContext *c = avctx->thread_opaque;
160  unsigned last_execute = 0;
161  int our_job = c->job_count;
162  int thread_count = avctx->thread_count;
163  int self_id;
164 
166  self_id = c->current_job++;
167  for (;;){
168  while (our_job >= c->job_count) {
169  if (c->current_job == thread_count + c->job_count)
171 
172  while (last_execute == c->current_execute && !c->done)
174  last_execute = c->current_execute;
175  our_job = self_id;
176 
177  if (c->done) {
179  return NULL;
180  }
181  }
183 
184  c->rets[our_job%c->rets_count] = c->func ? c->func(avctx, (char*)c->args + our_job*c->job_size):
185  c->func2(avctx, c->args, our_job, self_id);
186 
188  our_job = c->current_job++;
189  }
190 }
191 
193 {
194  while (c->current_job != thread_count + c->job_count)
197 }
198 
199 static void thread_free(AVCodecContext *avctx)
200 {
201  ThreadContext *c = avctx->thread_opaque;
202  int i;
203 
205  c->done = 1;
208 
209  for (i=0; i<avctx->thread_count; i++)
210  pthread_join(c->workers[i], NULL);
211 
215  av_free(c->workers);
216  av_freep(&avctx->thread_opaque);
217 }
218 
219 static int avcodec_thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
220 {
221  ThreadContext *c= avctx->thread_opaque;
222  int dummy_ret;
223 
224  if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
225  return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
226 
227  if (job_count <= 0)
228  return 0;
229 
231 
232  c->current_job = avctx->thread_count;
233  c->job_count = job_count;
234  c->job_size = job_size;
235  c->args = arg;
236  c->func = func;
237  if (ret) {
238  c->rets = ret;
239  c->rets_count = job_count;
240  } else {
241  c->rets = &dummy_ret;
242  c->rets_count = 1;
243  }
244  c->current_execute++;
246 
248 
249  return 0;
250 }
251 
252 static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
253 {
254  ThreadContext *c= avctx->thread_opaque;
255  c->func2 = func2;
256  return avcodec_thread_execute(avctx, NULL, arg, ret, job_count, 0);
257 }
258 
260 {
261  int i;
262  ThreadContext *c;
263  int thread_count = avctx->thread_count;
264 
265  if (!thread_count) {
266  int nb_cpus = av_cpu_count();
267  if (avctx->height)
268  nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
269  // use number of cores + 1 as thread count if there is more than one
270  if (nb_cpus > 1)
271  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
272  else
273  thread_count = avctx->thread_count = 1;
274  }
275 
276  if (thread_count <= 1) {
277  avctx->active_thread_type = 0;
278  return 0;
279  }
280 
281  c = av_mallocz(sizeof(ThreadContext));
282  if (!c)
283  return -1;
284 
285  c->workers = av_mallocz(sizeof(pthread_t)*thread_count);
286  if (!c->workers) {
287  av_free(c);
288  return -1;
289  }
290 
291  avctx->thread_opaque = c;
292  c->current_job = 0;
293  c->job_count = 0;
294  c->job_size = 0;
295  c->done = 0;
297  pthread_cond_init(&c->last_job_cond, NULL);
300  for (i=0; i<thread_count; i++) {
301  if(pthread_create(&c->workers[i], NULL, worker, avctx)) {
302  avctx->thread_count = i;
304  ff_thread_free(avctx);
305  return -1;
306  }
307  }
308 
309  avcodec_thread_park_workers(c, thread_count);
310 
313  return 0;
314 }
315 
316 #define THREAD_SAFE_CALLBACKS(avctx) \
317 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
318 
319 /**
320  * Codec worker thread.
321  *
322  * Automatically calls ff_thread_finish_setup() if the codec does
323  * not provide an update_thread_context method, or if the codec returns
324  * before calling it.
325  */
327 {
328  PerThreadContext *p = arg;
329  FrameThreadContext *fctx = p->parent;
330  AVCodecContext *avctx = p->avctx;
331  const AVCodec *codec = avctx->codec;
332 
334  while (1) {
335  while (p->state == STATE_INPUT_READY && !fctx->die)
337 
338  if (fctx->die) break;
339 
340  if (!codec->update_thread_context && THREAD_SAFE_CALLBACKS(avctx))
341  ff_thread_finish_setup(avctx);
342 
344  p->got_frame = 0;
345  p->result = codec->decode(avctx, &p->frame, &p->got_frame, &p->avpkt);
346 
347  /* many decoders assign whole AVFrames, thus overwriting extended_data;
348  * make sure it's set correctly */
349  p->frame.extended_data = p->frame.data;
350 
351  if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
352 
354 #if 0 //BUFREF-FIXME
355  for (i = 0; i < MAX_BUFFERS; i++)
356  if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
357  p->progress[i][0] = INT_MAX;
358  p->progress[i][1] = INT_MAX;
359  }
360 #endif
361  p->state = STATE_INPUT_READY;
362 
366  }
368 
369  return NULL;
370 }
371 
372 /**
373  * Update the next thread's AVCodecContext with values from the reference thread's context.
374  *
375  * @param dst The destination context.
376  * @param src The source context.
377  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
378  */
380 {
381  int err = 0;
382 
383  if (dst != src) {
384  dst->time_base = src->time_base;
385  dst->width = src->width;
386  dst->height = src->height;
387  dst->pix_fmt = src->pix_fmt;
388 
389  dst->coded_width = src->coded_width;
390  dst->coded_height = src->coded_height;
391 
392  dst->has_b_frames = src->has_b_frames;
393  dst->idct_algo = src->idct_algo;
394 
398 
399  dst->profile = src->profile;
400  dst->level = src->level;
401 
403  dst->ticks_per_frame = src->ticks_per_frame;
404  dst->color_primaries = src->color_primaries;
405 
406  dst->color_trc = src->color_trc;
407  dst->colorspace = src->colorspace;
408  dst->color_range = src->color_range;
410 
411  dst->hwaccel = src->hwaccel;
412  dst->hwaccel_context = src->hwaccel_context;
413 
414  dst->channels = src->channels;
415  dst->sample_rate = src->sample_rate;
416  dst->sample_fmt = src->sample_fmt;
417  dst->channel_layout = src->channel_layout;
418  }
419 
420  if (for_user) {
421  dst->delay = src->thread_count - 1;
422  dst->coded_frame = src->coded_frame;
423  } else {
424  if (dst->codec->update_thread_context)
425  err = dst->codec->update_thread_context(dst, src);
426  }
427 
428  return err;
429 }
430 
431 /**
432  * Update the next thread's AVCodecContext with values set by the user.
433  *
434  * @param dst The destination context.
435  * @param src The source context.
436  * @return 0 on success, negative error code on failure
437  */
439 {
440 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
441  dst->flags = src->flags;
442 
443  dst->draw_horiz_band= src->draw_horiz_band;
444  dst->get_buffer2 = src->get_buffer2;
445 #if FF_API_GET_BUFFER
447  dst->get_buffer = src->get_buffer;
448  dst->release_buffer = src->release_buffer;
450 #endif
451 
452  dst->opaque = src->opaque;
453  dst->debug = src->debug;
454  dst->debug_mv = src->debug_mv;
455 
456  dst->slice_flags = src->slice_flags;
457  dst->flags2 = src->flags2;
458 
459  copy_fields(skip_loop_filter, subtitle_header);
460 
461  dst->frame_number = src->frame_number;
464 
465  if (src->slice_count && src->slice_offset) {
466  if (dst->slice_count < src->slice_count) {
467  int *tmp = av_realloc(dst->slice_offset, src->slice_count *
468  sizeof(*dst->slice_offset));
469  if (!tmp) {
470  av_free(dst->slice_offset);
471  return AVERROR(ENOMEM);
472  }
473  dst->slice_offset = tmp;
474  }
475  memcpy(dst->slice_offset, src->slice_offset,
476  src->slice_count * sizeof(*dst->slice_offset));
477  }
478  dst->slice_count = src->slice_count;
479  return 0;
480 #undef copy_fields
481 }
482 
483 /// Releases the buffers that this decoding thread was the last user of.
485 {
486  FrameThreadContext *fctx = p->parent;
487 
488  while (p->num_released_buffers > 0) {
489  AVFrame *f;
490 
492 
493  // fix extended data in case the caller screwed it up
497  f->extended_data = f->data;
498  av_frame_unref(f);
499 
501  }
502 }
503 
505 {
506  FrameThreadContext *fctx = p->parent;
507  PerThreadContext *prev_thread = fctx->prev_thread;
508  const AVCodec *codec = p->avctx->codec;
509 
510  if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
511 
513 
515 
516  if (prev_thread) {
517  int err;
518  if (prev_thread->state == STATE_SETTING_UP) {
519  pthread_mutex_lock(&prev_thread->progress_mutex);
520  while (prev_thread->state == STATE_SETTING_UP)
521  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
522  pthread_mutex_unlock(&prev_thread->progress_mutex);
523  }
524 
525  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
526  if (err) {
528  return err;
529  }
530  }
531 
533  p->avpkt = *avpkt;
534  if (avpkt->buf)
535  p->avpkt.buf = av_buffer_ref(avpkt->buf);
536  else {
538  p->avpkt.data = p->buf;
539  memcpy(p->buf, avpkt->data, avpkt->size);
540  memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
541  }
542 
543  p->state = STATE_SETTING_UP;
546 
547  /*
548  * If the client doesn't have a thread-safe get_buffer(),
549  * then decoding threads call back to the main thread,
550  * and it calls back to the client here.
551  */
552 
554  if (!p->avctx->thread_safe_callbacks && (
557  p->avctx->get_buffer ||
558 #endif
561  while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
562  int call_done = 1;
564  while (p->state == STATE_SETTING_UP)
566 
567  switch (p->state) {
568  case STATE_GET_BUFFER:
570  break;
571  case STATE_GET_FORMAT:
573  break;
574  default:
575  call_done = 0;
576  break;
577  }
578  if (call_done) {
579  p->state = STATE_SETTING_UP;
581  }
583  }
584  }
585 
586  fctx->prev_thread = p;
587  fctx->next_decoding++;
588 
589  return 0;
590 }
591 
593  AVFrame *picture, int *got_picture_ptr,
594  AVPacket *avpkt)
595 {
596  FrameThreadContext *fctx = avctx->thread_opaque;
597  int finished = fctx->next_finished;
598  PerThreadContext *p;
599  int err;
600 
601  /*
602  * Submit a packet to the next decoding thread.
603  */
604 
605  p = &fctx->threads[fctx->next_decoding];
606  err = update_context_from_user(p->avctx, avctx);
607  if (err) return err;
608  err = submit_packet(p, avpkt);
609  if (err) return err;
610 
611  /*
612  * If we're still receiving the initial packets, don't return a frame.
613  */
614 
615  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
616  fctx->delaying = 0;
617 
618  if (fctx->delaying) {
619  *got_picture_ptr=0;
620  if (avpkt->size)
621  return avpkt->size;
622  }
623 
624  /*
625  * Return the next available frame from the oldest thread.
626  * If we're at the end of the stream, then we have to skip threads that
627  * didn't output a frame, because we don't want to accidentally signal
628  * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
629  */
630 
631  do {
632  p = &fctx->threads[finished++];
633 
634  if (p->state != STATE_INPUT_READY) {
636  while (p->state != STATE_INPUT_READY)
639  }
640 
641  av_frame_move_ref(picture, &p->frame);
642  *got_picture_ptr = p->got_frame;
643  picture->pkt_dts = p->avpkt.dts;
644 
645  /*
646  * A later call with avkpt->size == 0 may loop over all threads,
647  * including this one, searching for a frame to return before being
648  * stopped by the "finished != fctx->next_finished" condition.
649  * Make sure we don't mistakenly return the same frame again.
650  */
651  p->got_frame = 0;
652 
653  if (finished >= avctx->thread_count) finished = 0;
654  } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
655 
656  update_context_from_thread(avctx, p->avctx, 1);
657 
658  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
659 
660  fctx->next_finished = finished;
661 
662  /* return the size of the consumed packet if no error occurred */
663  return (p->result >= 0) ? avpkt->size : p->result;
664 }
665 
666 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
667 {
668  PerThreadContext *p;
669  volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
670 
671  if (!progress || progress[field] >= n) return;
672 
673  p = f->owner->thread_opaque;
674 
675  if (f->owner->debug&FF_DEBUG_THREADS)
676  av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
677 
679  progress[field] = n;
682 }
683 
684 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
685 {
686  PerThreadContext *p;
687  volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
688 
689  if (!progress || progress[field] >= n) return;
690 
691  p = f->owner->thread_opaque;
692 
693  if (f->owner->debug&FF_DEBUG_THREADS)
694  av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
695 
697  while (progress[field] < n)
700 }
701 
703  PerThreadContext *p = avctx->thread_opaque;
704 
705  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
706 
707  if(p->state == STATE_SETUP_FINISHED){
708  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
709  }
710 
712  p->state = STATE_SETUP_FINISHED;
715 }
716 
717 /// Waits for all threads to finish.
718 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
719 {
720  int i;
721 
722  for (i = 0; i < thread_count; i++) {
723  PerThreadContext *p = &fctx->threads[i];
724 
725  if (p->state != STATE_INPUT_READY) {
727  while (p->state != STATE_INPUT_READY)
730  }
731  p->got_frame = 0;
732  }
733 }
734 
735 static void frame_thread_free(AVCodecContext *avctx, int thread_count)
736 {
737  FrameThreadContext *fctx = avctx->thread_opaque;
738  const AVCodec *codec = avctx->codec;
739  int i;
740 
741  park_frame_worker_threads(fctx, thread_count);
742 
743  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
744  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
745  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
747  fctx->threads->avctx->internal->is_copy = 1;
748  }
749 
750  fctx->die = 1;
751 
752  for (i = 0; i < thread_count; i++) {
753  PerThreadContext *p = &fctx->threads[i];
754 
758 
759  if (p->thread_init)
760  pthread_join(p->thread, NULL);
761  p->thread_init=0;
762 
763  if (codec->close)
764  codec->close(p->avctx);
765 
766  avctx->codec = NULL;
767 
769  av_frame_unref(&p->frame);
770  }
771 
772  for (i = 0; i < thread_count; i++) {
773  PerThreadContext *p = &fctx->threads[i];
774 
781  av_freep(&p->buf);
783 
784  if (i) {
785  av_freep(&p->avctx->priv_data);
786  av_freep(&p->avctx->internal);
788  }
789 
790  av_freep(&p->avctx);
791  }
792 
793  av_freep(&fctx->threads);
795  av_freep(&avctx->thread_opaque);
796 }
797 
799 {
800  int thread_count = avctx->thread_count;
801  const AVCodec *codec = avctx->codec;
802  AVCodecContext *src = avctx;
803  FrameThreadContext *fctx;
804  int i, err = 0;
805 
806  if (!thread_count) {
807  int nb_cpus = av_cpu_count();
808  if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
809  nb_cpus = 1;
810  // use number of cores + 1 as thread count if there is more than one
811  if (nb_cpus > 1)
812  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
813  else
814  thread_count = avctx->thread_count = 1;
815  }
816 
817  if (thread_count <= 1) {
818  avctx->active_thread_type = 0;
819  return 0;
820  }
821 
822  avctx->thread_opaque = fctx = av_mallocz(sizeof(FrameThreadContext));
823 
824  fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
825  pthread_mutex_init(&fctx->buffer_mutex, NULL);
826  fctx->delaying = 1;
827 
828  for (i = 0; i < thread_count; i++) {
830  PerThreadContext *p = &fctx->threads[i];
831 
832  pthread_mutex_init(&p->mutex, NULL);
834  pthread_cond_init(&p->input_cond, NULL);
835  pthread_cond_init(&p->progress_cond, NULL);
836  pthread_cond_init(&p->output_cond, NULL);
837 
838  p->parent = fctx;
839  p->avctx = copy;
840 
841  if (!copy) {
842  err = AVERROR(ENOMEM);
843  goto error;
844  }
845 
846  *copy = *src;
847  copy->thread_opaque = p;
848  copy->pkt = &p->avpkt;
849 
850  if (!i) {
851  src = copy;
852 
853  if (codec->init)
854  err = codec->init(copy);
855 
856  update_context_from_thread(avctx, copy, 1);
857  } else {
858  copy->priv_data = av_malloc(codec->priv_data_size);
859  if (!copy->priv_data) {
860  err = AVERROR(ENOMEM);
861  goto error;
862  }
863  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
864  copy->internal = av_malloc(sizeof(AVCodecInternal));
865  if (!copy->internal) {
866  err = AVERROR(ENOMEM);
867  goto error;
868  }
869  *copy->internal = *src->internal;
870  copy->internal->is_copy = 1;
871 
872  if (codec->init_thread_copy)
873  err = codec->init_thread_copy(copy);
874  }
875 
876  if (err) goto error;
877 
878  err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
879  p->thread_init= !err;
880  if(!p->thread_init)
881  goto error;
882  }
883 
884  return 0;
885 
886 error:
887  frame_thread_free(avctx, i+1);
888 
889  return err;
890 }
891 
893 {
894  int i;
895  FrameThreadContext *fctx = avctx->thread_opaque;
896 
897  if (!avctx->thread_opaque) return;
898 
900  if (fctx->prev_thread) {
901  if (fctx->prev_thread != &fctx->threads[0])
903  if (avctx->codec->flush)
904  avctx->codec->flush(fctx->threads[0].avctx);
905  }
906 
907  fctx->next_decoding = fctx->next_finished = 0;
908  fctx->delaying = 1;
909  fctx->prev_thread = NULL;
910  for (i = 0; i < avctx->thread_count; i++) {
911  PerThreadContext *p = &fctx->threads[i];
912  // Make sure decode flush calls with size=0 won't return old frames
913  p->got_frame = 0;
914  av_frame_unref(&p->frame);
915 
917  }
918 }
919 
921 {
922  PerThreadContext *p = avctx->thread_opaque;
923  if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
924  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
925  return 0;
926  }
927  return 1;
928 }
929 
931 {
932  PerThreadContext *p = avctx->thread_opaque;
933  int err;
934 
935  f->owner = avctx;
936 
937  ff_init_buffer_info(avctx, f->f);
938 
939  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
940  return ff_get_buffer(avctx, f->f, flags);
941 
942  if (p->state != STATE_SETTING_UP &&
943  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
944  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
945  return -1;
946  }
947 
948  if (avctx->internal->allocate_progress) {
949  int *progress;
950  f->progress = av_buffer_alloc(2 * sizeof(int));
951  if (!f->progress) {
952  return AVERROR(ENOMEM);
953  }
954  progress = (int*)f->progress->data;
955 
956  progress[0] = progress[1] = -1;
957  }
958 
960 
962  if (avctx->thread_safe_callbacks || (
964  !avctx->get_buffer &&
965 #endif
968  err = ff_get_buffer(avctx, f->f, flags);
969  } else {
971  p->requested_frame = f->f;
972  p->requested_flags = flags;
973  p->state = STATE_GET_BUFFER;
975 
976  while (p->state != STATE_SETTING_UP)
978 
979  err = p->result;
980 
982 
983  }
984  if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
985  ff_thread_finish_setup(avctx);
986 
987  if (err)
989 
991 
992  return err;
993 }
994 
996 {
997  enum AVPixelFormat res;
998  PerThreadContext *p = avctx->thread_opaque;
999  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
1001  return avctx->get_format(avctx, fmt);
1002  if (p->state != STATE_SETTING_UP) {
1003  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
1004  return -1;
1005  }
1007  p->available_formats = fmt;
1008  p->state = STATE_GET_FORMAT;
1010 
1011  while (p->state != STATE_SETTING_UP)
1013 
1014  res = p->result_format;
1015 
1017 
1018  return res;
1019 }
1020 
1022 {
1023  int ret = thread_get_buffer_internal(avctx, f, flags);
1024  if (ret < 0)
1025  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1026  return ret;
1027 }
1028 
1030 {
1031  PerThreadContext *p = avctx->thread_opaque;
1032  FrameThreadContext *fctx;
1033  AVFrame *dst, *tmp;
1035  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1036  avctx->thread_safe_callbacks ||
1037  (
1039  !avctx->get_buffer &&
1040 #endif
1043 
1044  if (!f->f->data[0])
1045  return;
1046 
1047  if (avctx->debug & FF_DEBUG_BUFFERS)
1048  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1049 
1051  f->owner = NULL;
1052 
1053  if (can_direct_free) {
1054  av_frame_unref(f->f);
1055  return;
1056  }
1057 
1058  fctx = p->parent;
1060 
1061  if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
1062  goto fail;
1064  (p->num_released_buffers + 1) *
1065  sizeof(*p->released_buffers));
1066  if (!tmp)
1067  goto fail;
1068  p->released_buffers = tmp;
1069 
1070  dst = &p->released_buffers[p->num_released_buffers];
1071  av_frame_move_ref(dst, f->f);
1072 
1073  p->num_released_buffers++;
1074 
1075 fail:
1077 }
1078 
1079 /**
1080  * Set the threading algorithms used.
1081  *
1082  * Threading requires more than one thread.
1083  * Frame threading requires entire frames to be passed to the codec,
1084  * and introduces extra decoding delay, so is incompatible with low_delay.
1085  *
1086  * @param avctx The context.
1087  */
1089 {
1090  int frame_threading_supported = (avctx->codec->capabilities & CODEC_CAP_FRAME_THREADS)
1091  && !(avctx->flags & CODEC_FLAG_TRUNCATED)
1092  && !(avctx->flags & CODEC_FLAG_LOW_DELAY)
1093  && !(avctx->flags2 & CODEC_FLAG2_CHUNKS);
1094  if (avctx->thread_count == 1) {
1095  avctx->active_thread_type = 0;
1096  } else if (frame_threading_supported && (avctx->thread_type & FF_THREAD_FRAME)) {
1098  } else if (avctx->codec->capabilities & CODEC_CAP_SLICE_THREADS &&
1099  avctx->thread_type & FF_THREAD_SLICE) {
1101  } else if (!(avctx->codec->capabilities & CODEC_CAP_AUTO_THREADS)) {
1102  avctx->thread_count = 1;
1103  avctx->active_thread_type = 0;
1104  }
1105 
1106  if (avctx->thread_count > MAX_AUTO_THREADS)
1107  av_log(avctx, AV_LOG_WARNING,
1108  "Application has requested %d threads. Using a thread count greater than %d is not recommended.\n",
1109  avctx->thread_count, MAX_AUTO_THREADS);
1110 }
1111 
1113 {
1114 #if HAVE_W32THREADS
1115  w32thread_init();
1116 #endif
1117 
1119 
1121  return thread_init_internal(avctx);
1122  else if (avctx->active_thread_type&FF_THREAD_FRAME)
1123  return frame_thread_init(avctx);
1124 
1125  return 0;
1126 }
1127 
1129 {
1131  frame_thread_free(avctx, avctx->thread_count);
1132  else
1133  thread_free(avctx);
1134 }
1135 
1136 void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
1137 {
1138  ThreadContext *p = avctx->thread_opaque;
1139  int *entries = p->entries;
1140 
1141  pthread_mutex_lock(&p->progress_mutex[thread]);
1142  entries[field] +=n;
1143  pthread_cond_signal(&p->progress_cond[thread]);
1144  pthread_mutex_unlock(&p->progress_mutex[thread]);
1145 }
1146 
1147 void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
1148 {
1149  ThreadContext *p = avctx->thread_opaque;
1150  int *entries = p->entries;
1151 
1152  if (!entries || !field) return;
1153 
1154  thread = thread ? thread - 1 : p->thread_count - 1;
1155 
1156  pthread_mutex_lock(&p->progress_mutex[thread]);
1157  while ((entries[field - 1] - entries[field]) < shift){
1158  pthread_cond_wait(&p->progress_cond[thread], &p->progress_mutex[thread]);
1159  }
1160  pthread_mutex_unlock(&p->progress_mutex[thread]);
1161 }
1162 
1164 {
1165  int i;
1166 
1167  if (avctx->active_thread_type & FF_THREAD_SLICE) {
1168  ThreadContext *p = avctx->thread_opaque;
1169  p->thread_count = avctx->thread_count;
1170  p->entries = av_mallocz(count * sizeof(int));
1171 
1172  if (!p->entries) {
1173  return AVERROR(ENOMEM);
1174  }
1175 
1176  p->entries_count = count;
1179 
1180  for (i = 0; i < p->thread_count; i++) {
1181  pthread_mutex_init(&p->progress_mutex[i], NULL);
1182  pthread_cond_init(&p->progress_cond[i], NULL);
1183  }
1184  }
1185 
1186  return 0;
1187 }
1188 
1190 {
1191  ThreadContext *p = avctx->thread_opaque;
1192  memset(p->entries, 0, p->entries_count * sizeof(int));
1193 }
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread.c:86
int ff_thread_can_start_frame(AVCodecContext *avctx)
Definition: pthread.c:920
float v
static int shift(int a, int b)
Definition: sonic.c:78
static av_always_inline int pthread_mutex_destroy(pthread_mutex_t *mutex)
Definition: os2threads.h:90
Set after the codec has called ff_thread_finish_setup().
Definition: pthread.c:113
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:105
This structure describes decoded (raw) audio or video data.
Definition: frame.h:96
Set when the codec calls get_buffer().
Definition: pthread.c:105
static av_always_inline int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: os2threads.h:149
int av_cpu_count(void)
Definition: cpu.c:201
Context used by codec threads and stored in their AVCodecContext thread_opaque.
Definition: pthread.c:80
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
Definition: pthread.c:124
#define av_always_inline
Definition: attributes.h:41
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1324
const char * fmt
Definition: avisynth_c.h:669
void(* flush)(AVCodecContext *)
Flush buffers.
Definition: avcodec.h:3013
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: avcodec.h:4153
AVFrame * f
Definition: thread.h:36
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:592
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
Definition: pthread.c:718
enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
Wrapper around get_format() for frame-multithreaded codecs.
Definition: pthread.c:995
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1848
int size
Definition: avcodec.h:1064
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:140
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:1517
os2threads to pthreads wrapper
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1342
void ff_thread_await_progress2(AVCodecContext *avctx, int field, int thread, int shift)
Definition: pthread.c:1147
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
Definition: avcodec.h:3007
static void *attribute_align_arg worker(void *v)
Definition: pthread.c:156
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2570
void ff_thread_report_progress2(AVCodecContext *avctx, int field, int thread, int n)
Definition: pthread.c:1136
pthread_cond_t last_job_cond
Definition: pthread.c:63
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread.c:85
attribute_deprecated int(* get_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called at the beginning of each frame to get a buffer for it.
Definition: avcodec.h:2022
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:1508
action_func2 * func2
Definition: pthread.c:56
int profile
profile
Definition: avcodec.h:2678
enum AVPixelFormat * available_formats
Format array for get_format()
Definition: pthread.c:127
AVCodec.
Definition: avcodec.h:2922
Set when the thread is awaiting a packet.
Definition: pthread.c:103
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:94
attribute_deprecated void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:2036
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1046
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1265
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:234
void * args
Definition: pthread.c:57
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
Definition: avfilter.c:965
int job_count
Definition: pthread.c:60
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:438
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: utils.c:153
static uint8_t * res
Definition: ffhash.c:43
int(* init_thread_copy)(AVCodecContext *)
If defined, called on thread contexts when they are created.
Definition: avcodec.h:2971
static void validate_thread_parameters(AVCodecContext *avctx)
Set the threading algorithms used.
Definition: pthread.c:1088
#define CODEC_FLAG2_CHUNKS
Input bitstream might be truncated at a packet boundaries instead of only at frame boundaries...
Definition: avcodec.h:726
HMTX pthread_mutex_t
Definition: os2threads.h:38
enum AVPixelFormat avcodec_default_get_format(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
Definition: utils.c:1039
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1881
uint8_t
Multithreading support functions.
#define THREAD_SAFE_CALLBACKS(avctx)
Definition: pthread.c:316
#define FF_DEBUG_THREADS
Definition: avcodec.h:2459
action_func * func
Definition: pthread.c:55
int requested_flags
flags passed to get_buffer() for requested_frame
Definition: pthread.c:125
int next_decoding
The next context to submit a packet to.
Definition: pthread.c:140
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1190
AVFrame frame
Output frame (for decoding) or input (for encoding).
Definition: pthread.c:98
int ff_thread_init(AVCodecContext *avctx)
Definition: pthread.c:1112
static av_always_inline int pthread_cond_signal(pthread_cond_t *cond)
Definition: os2threads.h:127
static void copy(LZOContext *c, int cnt)
Copies bytes from input to output buffer with checking.
Definition: lzo.c:79
int bits_per_coded_sample
bits per sample/pixel from the demuxer (needed for huffyuv).
Definition: avcodec.h:2563
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1855
Context stored in the client AVCodecContext thread_opaque.
Definition: pthread.c:134
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread.c:92
AVCodecContext * owner
Definition: thread.h:37
int slice_count
slice count
Definition: avcodec.h:1492
int(* close)(AVCodecContext *)
Definition: avcodec.h:3008
static int avcodec_thread_execute(AVCodecContext *avctx, action_func *func, void *arg, int *ret, int job_count, int job_size)
Definition: pthread.c:219
#define CODEC_FLAG_TRUNCATED
Definition: avcodec.h:708
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
Definition: pthread.c:684
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: avcodec.h:4147
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1427
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:219
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
Definition: pthread.c:136
uint8_t * buf
backup storage for packet data when the input packet is not refcounted
Definition: pthread.c:95
#define 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:769
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:63
#define FF_THREAD_SLICE
Decode more than one part of a single frame at once.
Definition: avcodec.h:2608
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2615
#define FF_API_GET_BUFFER
Definition: version.h:86
int capabilities
Codec capabilities.
Definition: avcodec.h:2941
int result
The result of the last codec decode/encode() call.
Definition: pthread.c:100
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: avcodec.h:4168
int rets_count
Definition: pthread.c:59
int current_job
Definition: pthread.c:67
Set when the codec calls get_format().
Definition: pthread.c:109
struct AVCodec * codec
Definition: avcodec.h:1155
const char * arg
Definition: jacosubdec.c:69
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1234
static av_always_inline void avcodec_thread_park_workers(ThreadContext *c, int thread_count)
Definition: pthread.c:192
int die
Set when threads should exit.
Definition: pthread.c:148
static int avcodec_thread_execute2(AVCodecContext *avctx, action_func2 *func2, void *arg, int *ret, int job_count)
Definition: pthread.c:252
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
Definition: pthread.c:1029
static void thread_free(AVCodecContext *avctx)
Definition: pthread.c:199
void ff_thread_free(AVCodecContext *avctx)
Definition: pthread.c:1128
Libavcodec external API header.
void * thread_opaque
thread opaque Can be used by execute() to store some per AVCodecContext stuff.
Definition: avcodec.h:2664
AVPixelFormat
Pixel format.
Definition: pixfmt.h:66
#define CODEC_FLAG_LOW_DELAY
Force low delay.
Definition: avcodec.h:712
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
Definition: pthread.c:326
uint64_t channel_layout
Audio channel layout.
Definition: avcodec.h:1934
goto fail
Definition: avfilter.c:963
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:580
int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
Definition: avcodec.h:1610
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread.c:87
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:1376
int * rets
Definition: pthread.c:58
int job_size
Definition: pthread.c:61
uint8_t * data
The data buffer.
Definition: buffer.h:89
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:351
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
Definition: mem.c:141
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
Definition: pthread.c:1021
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1197
ret
Definition: avfilter.c:961
int width
picture width / height.
Definition: avcodec.h:1314
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2540
pthread_mutex_t current_job_lock
Definition: pthread.c:65
int priv_data_size
Definition: avcodec.h:2960
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:73
#define CODEC_CAP_AUTO_THREADS
Codec supports avctx-&gt;thread_count == 0 (auto).
Definition: avcodec.h:823
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1827
#define FFMIN(a, b)
Definition: avcodec.h:925
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 thread_count
Definition: pthread.c:72
int level
level
Definition: avcodec.h:2756
int num_released_buffers
Definition: pthread.c:121
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
Definition: avcodec.h:2494
int n
Definition: avisynth_c.h:588
int ff_alloc_entries(AVCodecContext *avctx, int count)
Definition: pthread.c:1163
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1274
pthread_t thread
Definition: pthread.c:83
void ff_reset_entries(AVCodecContext *avctx)
Definition: pthread.c:1189
#define FF_DEBUG_VIS_MB_TYPE
Definition: avcodec.h:2457
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2596
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.
Definition: utils.c:120
enum PerThreadContext::@81 state
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread.c:99
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
Definition: pthread.c:138
pthread_cond_t current_job_cond
Definition: pthread.c:64
AVBufferRef * progress
Definition: thread.h:40
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread.c:90
#define attribute_align_arg
Definition: internal.h:56
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
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:65
AVS_Value src
Definition: avisynth_c.h:523
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2513
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:693
enum AVMediaType codec_type
Definition: avcodec.h:1154
static int thread_init_internal(AVCodecContext *avctx)
Definition: pthread.c:259
enum AVCodecID codec_id
Definition: avcodec.h:1157
int sample_rate
samples per second
Definition: avcodec.h:1873
int debug
debug
Definition: avcodec.h:2442
main external API structure.
Definition: avcodec.h:1146
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2607
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:373
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:941
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
Definition: pthread.c:892
int ff_init_buffer_info(AVCodecContext *s, AVFrame *frame)
does needed setup of pkt_pts/pos and such for (re)get_buffer();
Definition: utils.c:716
int slice_flags
slice flags
Definition: avcodec.h:1648
int coded_height
Definition: avcodec.h:1324
void avcodec_get_frame_defaults(AVFrame *frame)
Set the fields of the given AVFrame to default values.
Definition: utils.c:1046
enum AVPixelFormat(* get_format)(struct AVCodecContext *s, const enum AVPixelFormat *fmt)
callback to negotiate the pixelFormat
Definition: avcodec.h:1389
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
Definition: pthread.c:484
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:379
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1841
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1834
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2588
enum AVPixelFormat result_format
get_format() result
Definition: pthread.c:128
uint8_t * data
Definition: avcodec.h:1063
int delaying
Set for the first N packets, where N is the number of threads.
Definition: pthread.c:143
static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
Definition: pthread.c:930
int(* func)(AVBPrint *dst, const char *in, const char *arg)
Definition: jacosubdec.c:70
#define MAX_AUTO_THREADS
Definition: pthread.c:154
unsigned current_execute
Definition: pthread.c:66
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:2458
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:2135
PerThreadContext * threads
The contexts for each thread.
Definition: pthread.c:135
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:78
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:666
AVFrame * released_buffers
Array of frames passed to ff_thread_release_buffer().
Definition: pthread.c:120
void * priv_data
Definition: avcodec.h:1182
struct FrameThreadContext * parent
Definition: pthread.c:81
static int flags
Definition: cpu.c:45
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:194
pthread_cond_t * progress_cond
Definition: pthread.c:73
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
Definition: pthread.c:504
#define CODEC_CAP_SLICE_THREADS
Codec supports slice-based (or partition-based) multithreading.
Definition: avcodec.h:815
common internal api header.
#define CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: avcodec.h:811
static double c[64]
int released_buffers_allocated
Definition: pthread.c:122
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...
Definition: avcodec.h:2625
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:2979
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:91
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.
Definition: avcodec.h:2636
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:79
pthread_mutex_t * progress_mutex
Definition: pthread.c:74
static void w32thread_init(void)
Definition: w32pthreads.h:265
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.
Definition: avcodec.h:2656
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2501
static av_always_inline int pthread_cond_broadcast(pthread_cond_t *cond)
Definition: os2threads.h:138
int channels
number of audio channels
Definition: avcodec.h:1874
static av_always_inline int pthread_mutex_unlock(pthread_mutex_t *mutex)
Definition: os2threads.h:104
int( action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr)
Definition: pthread.c:51
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread.c:89
w32threads to pthreads wrapper
#define AVERROR(e)
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1241
int64_t dts
Decompression timestamp in AVStream-&gt;time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1062
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1904
void INT64 INT64 count
Definition: avisynth_c.h:594
static av_always_inline int pthread_mutex_lock(pthread_mutex_t *mutex)
Definition: os2threads.h:97
int entries_count
Definition: pthread.c:71
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
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:702
pthread_t * workers
Definition: pthread.c:54
int debug_mv
debug
Definition: avcodec.h:2466
int next_finished
The next context to return output from.
Definition: pthread.c:141
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
Definition: avcodec.h:2805
int( action_func)(AVCodecContext *c, void *arg)
Definition: pthread.c:50
int(* init)(AVCodecContext *)
Definition: avcodec.h:2992
#define copy_fields(s, e)
This structure stores compressed data.
Definition: avcodec.h:1040
int allocated_buf_size
Size allocated for buf.
Definition: pthread.c:96
int delay
Codec delay.
Definition: avcodec.h:1302
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:107
static int frame_thread_init(AVCodecContext *avctx)
Definition: pthread.c:798
Set before the codec has called ff_thread_finish_setup().
Definition: pthread.c:104
int thread_type
Which multithreading methods to use.
Definition: avcodec.h:2606
#define FF_DEBUG_VIS_QP
Definition: avcodec.h:2456
static void frame_thread_free(AVCodecContext *avctx, int thread_count)
Definition: pthread.c:735
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:241
int * entries
Definition: pthread.c:70
int avcodec_default_execute(AVCodecContext *c, int(*func)(AVCodecContext *c2, void *arg2), void *arg, int *ret, int count, int size)
Definition: utils.c:1009