Libav
pthread_frame.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * Libav is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with Libav; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
25 #include "config.h"
26 
27 #include <stdint.h>
28 
29 #if HAVE_PTHREADS
30 #include <pthread.h>
31 #elif HAVE_W32THREADS
32 #include "compat/w32pthreads.h"
33 #endif
34 
35 #include "avcodec.h"
36 #include "internal.h"
37 #include "pthread_internal.h"
38 #include "thread.h"
39 
40 #include "libavutil/avassert.h"
41 #include "libavutil/buffer.h"
42 #include "libavutil/common.h"
43 #include "libavutil/cpu.h"
44 #include "libavutil/frame.h"
45 #include "libavutil/log.h"
46 #include "libavutil/mem.h"
47 
51 typedef struct PerThreadContext {
53 
59 
62 
64 
68 
70  int got_frame;
71  int result;
72 
73  enum {
81  } state;
82 
90 
94 
98 typedef struct FrameThreadContext {
101 
103 
106 
107  int delaying;
112  int die;
114 
123 {
124  PerThreadContext *p = arg;
125  FrameThreadContext *fctx = p->parent;
126  AVCodecContext *avctx = p->avctx;
127  const AVCodec *codec = avctx->codec;
128 
129  while (1) {
130  if (p->state == STATE_INPUT_READY && !fctx->die) {
132  while (p->state == STATE_INPUT_READY && !fctx->die)
135  }
136 
137  if (fctx->die) break;
138 
139  if (!codec->update_thread_context && avctx->thread_safe_callbacks)
140  ff_thread_finish_setup(avctx);
141 
143  av_frame_unref(p->frame);
144  p->got_frame = 0;
145  p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
146 
147  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
148  if (avctx->internal->allocate_progress)
149  av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
150  "free the frame on failure. This is a bug, please report it.\n");
151  av_frame_unref(p->frame);
152  }
153 
154  if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
155 
156  p->state = STATE_INPUT_READY;
157 
161 
163  }
164 
165  return NULL;
166 }
167 
175 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
176 {
177  int err = 0;
178 
179  if (dst != src) {
180  dst->time_base = src->time_base;
181  dst->width = src->width;
182  dst->height = src->height;
183  dst->pix_fmt = src->pix_fmt;
184 
185  dst->coded_width = src->coded_width;
186  dst->coded_height = src->coded_height;
187 
188  dst->has_b_frames = src->has_b_frames;
189  dst->idct_algo = src->idct_algo;
190 
194 
195  dst->profile = src->profile;
196  dst->level = src->level;
197 
199  dst->ticks_per_frame = src->ticks_per_frame;
200  dst->color_primaries = src->color_primaries;
201 
202  dst->color_trc = src->color_trc;
203  dst->colorspace = src->colorspace;
204  dst->color_range = src->color_range;
206 
207  dst->hwaccel = src->hwaccel;
208  dst->hwaccel_context = src->hwaccel_context;
209  }
210 
211  if (for_user) {
212  dst->coded_frame = src->coded_frame;
213  } else {
214  if (dst->codec->update_thread_context)
215  err = dst->codec->update_thread_context(dst, src);
216  }
217 
218  return err;
219 }
220 
229 {
230 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
231  dst->flags = src->flags;
232 
233  dst->draw_horiz_band= src->draw_horiz_band;
234  dst->get_buffer2 = src->get_buffer2;
235 #if FF_API_GET_BUFFER
237  dst->get_buffer = src->get_buffer;
238  dst->release_buffer = src->release_buffer;
240 #endif
241 
242  dst->opaque = src->opaque;
243  dst->debug = src->debug;
244 
245  dst->slice_flags = src->slice_flags;
246  dst->flags2 = src->flags2;
247 
248  copy_fields(skip_loop_filter, subtitle_header);
249 
250  dst->frame_number = src->frame_number;
252 
253  if (src->slice_count && src->slice_offset) {
254  if (dst->slice_count < src->slice_count) {
255  int *tmp = av_realloc(dst->slice_offset, src->slice_count *
256  sizeof(*dst->slice_offset));
257  if (!tmp) {
258  av_free(dst->slice_offset);
259  return AVERROR(ENOMEM);
260  }
261  dst->slice_offset = tmp;
262  }
263  memcpy(dst->slice_offset, src->slice_offset,
264  src->slice_count * sizeof(*dst->slice_offset));
265  }
266  dst->slice_count = src->slice_count;
267  return 0;
268 #undef copy_fields
269 }
270 
273 {
274  FrameThreadContext *fctx = p->parent;
275 
276  while (p->num_released_buffers > 0) {
277  AVFrame *f;
278 
280 
281  // fix extended data in case the caller screwed it up
284  f->extended_data = f->data;
285  av_frame_unref(f);
286 
288  }
289 }
290 
292 {
293  FrameThreadContext *fctx = p->parent;
294  PerThreadContext *prev_thread = fctx->prev_thread;
295  const AVCodec *codec = p->avctx->codec;
296 
297  if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
298 
300 
302 
303  if (prev_thread) {
304  int err;
305  if (prev_thread->state == STATE_SETTING_UP) {
306  pthread_mutex_lock(&prev_thread->progress_mutex);
307  while (prev_thread->state == STATE_SETTING_UP)
308  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
309  pthread_mutex_unlock(&prev_thread->progress_mutex);
310  }
311 
312  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
313  if (err) {
315  return err;
316  }
317  }
318 
320  p->avpkt = *avpkt;
321  if (avpkt->buf)
322  p->avpkt.buf = av_buffer_ref(avpkt->buf);
323  else {
325  p->avpkt.data = p->buf;
326  memcpy(p->buf, avpkt->data, avpkt->size);
327  memset(p->buf + avpkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
328  }
329 
330  p->state = STATE_SETTING_UP;
333 
334  /*
335  * If the client doesn't have a thread-safe get_buffer(),
336  * then decoding threads call back to the main thread,
337  * and it calls back to the client here.
338  */
339 
341  if (!p->avctx->thread_safe_callbacks && (
343  p->avctx->get_buffer ||
344 #endif
347  while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
349  while (p->state == STATE_SETTING_UP)
351 
352  if (p->state == STATE_GET_BUFFER) {
354  p->state = STATE_SETTING_UP;
356  }
358  }
359  }
360 
361  fctx->prev_thread = p;
362  fctx->next_decoding++;
363 
364  return 0;
365 }
366 
368  AVFrame *picture, int *got_picture_ptr,
369  AVPacket *avpkt)
370 {
371  FrameThreadContext *fctx = avctx->internal->thread_ctx;
372  int finished = fctx->next_finished;
373  PerThreadContext *p;
374  int err;
375 
376  /*
377  * Submit a packet to the next decoding thread.
378  */
379 
380  p = &fctx->threads[fctx->next_decoding];
381  err = update_context_from_user(p->avctx, avctx);
382  if (err) return err;
383  err = submit_packet(p, avpkt);
384  if (err) return err;
385 
386  /*
387  * If we're still receiving the initial packets, don't return a frame.
388  */
389 
390  if (fctx->delaying) {
391  if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
392 
393  *got_picture_ptr=0;
394  if (avpkt->size)
395  return avpkt->size;
396  }
397 
398  /*
399  * Return the next available frame from the oldest thread.
400  * If we're at the end of the stream, then we have to skip threads that
401  * didn't output a frame, because we don't want to accidentally signal
402  * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
403  */
404 
405  do {
406  p = &fctx->threads[finished++];
407 
408  if (p->state != STATE_INPUT_READY) {
410  while (p->state != STATE_INPUT_READY)
413  }
414 
415  av_frame_move_ref(picture, p->frame);
416  *got_picture_ptr = p->got_frame;
417  picture->pkt_dts = p->avpkt.dts;
418 
419  /*
420  * A later call with avkpt->size == 0 may loop over all threads,
421  * including this one, searching for a frame to return before being
422  * stopped by the "finished != fctx->next_finished" condition.
423  * Make sure we don't mistakenly return the same frame again.
424  */
425  p->got_frame = 0;
426 
427  if (finished >= avctx->thread_count) finished = 0;
428  } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
429 
430  update_context_from_thread(avctx, p->avctx, 1);
431 
432  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
433 
434  fctx->next_finished = finished;
435 
436  /* return the size of the consumed packet if no error occurred */
437  return (p->result >= 0) ? avpkt->size : p->result;
438 }
439 
440 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
441 {
442  PerThreadContext *p;
443  int *progress = f->progress ? (int*)f->progress->data : NULL;
444 
445  if (!progress || progress[field] >= n) return;
446 
447  p = f->owner->internal->thread_ctx;
448 
449  if (f->owner->debug&FF_DEBUG_THREADS)
450  av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
451 
453  progress[field] = n;
456 }
457 
458 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
459 {
460  PerThreadContext *p;
461  int *progress = f->progress ? (int*)f->progress->data : NULL;
462 
463  if (!progress || progress[field] >= n) return;
464 
465  p = f->owner->internal->thread_ctx;
466 
467  if (f->owner->debug&FF_DEBUG_THREADS)
468  av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
469 
471  while (progress[field] < n)
474 }
475 
477  PerThreadContext *p = avctx->internal->thread_ctx;
478 
479  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
480 
482  p->state = STATE_SETUP_FINISHED;
485 }
486 
488 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
489 {
490  int i;
491 
492  for (i = 0; i < thread_count; i++) {
493  PerThreadContext *p = &fctx->threads[i];
494 
495  if (p->state != STATE_INPUT_READY) {
497  while (p->state != STATE_INPUT_READY)
500  }
501  }
502 }
503 
504 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
505 {
506  FrameThreadContext *fctx = avctx->internal->thread_ctx;
507  const AVCodec *codec = avctx->codec;
508  int i;
509 
510  park_frame_worker_threads(fctx, thread_count);
511 
512  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
514 
515  fctx->die = 1;
516 
517  for (i = 0; i < thread_count; i++) {
518  PerThreadContext *p = &fctx->threads[i];
519 
523 
524  if (p->thread_init)
525  pthread_join(p->thread, NULL);
526 
527  if (codec->close)
528  codec->close(p->avctx);
529 
530  avctx->codec = NULL;
531 
533  av_frame_free(&p->frame);
534  }
535 
536  for (i = 0; i < thread_count; i++) {
537  PerThreadContext *p = &fctx->threads[i];
538 
545  av_freep(&p->buf);
547 
548  if (i) {
549  av_freep(&p->avctx->priv_data);
551  }
552 
553  av_freep(&p->avctx->internal);
554  av_freep(&p->avctx);
555  }
556 
557  av_freep(&fctx->threads);
559  av_freep(&avctx->internal->thread_ctx);
560 }
561 
563 {
564  int thread_count = avctx->thread_count;
565  const AVCodec *codec = avctx->codec;
566  AVCodecContext *src = avctx;
567  FrameThreadContext *fctx;
568  int i, err = 0;
569 
570 #if HAVE_W32THREADS
571  w32thread_init();
572 #endif
573 
574  if (!thread_count) {
575  int nb_cpus = av_cpu_count();
576  av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
577  // use number of cores + 1 as thread count if there is more than one
578  if (nb_cpus > 1)
579  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
580  else
581  thread_count = avctx->thread_count = 1;
582  }
583 
584  if (thread_count <= 1) {
585  avctx->active_thread_type = 0;
586  return 0;
587  }
588 
589  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
590 
591  fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
593  fctx->delaying = 1;
594 
595  for (i = 0; i < thread_count; i++) {
597  PerThreadContext *p = &fctx->threads[i];
598 
604 
605  p->frame = av_frame_alloc();
606  if (!p->frame) {
607  err = AVERROR(ENOMEM);
608  goto error;
609  }
610 
611  p->parent = fctx;
612  p->avctx = copy;
613 
614  if (!copy) {
615  err = AVERROR(ENOMEM);
616  goto error;
617  }
618 
619  *copy = *src;
620 
621  copy->internal = av_malloc(sizeof(AVCodecInternal));
622  if (!copy->internal) {
623  err = AVERROR(ENOMEM);
624  goto error;
625  }
626  *copy->internal = *src->internal;
627  copy->internal->thread_ctx = p;
628  copy->internal->pkt = &p->avpkt;
629 
630  if (!i) {
631  src = copy;
632 
633  if (codec->init)
634  err = codec->init(copy);
635 
636  update_context_from_thread(avctx, copy, 1);
637  } else {
638  copy->priv_data = av_malloc(codec->priv_data_size);
639  if (!copy->priv_data) {
640  err = AVERROR(ENOMEM);
641  goto error;
642  }
643  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
644  copy->internal->is_copy = 1;
645 
646  if (codec->init_thread_copy)
647  err = codec->init_thread_copy(copy);
648  }
649 
650  if (err) goto error;
651 
653  p->thread_init = 1;
654  }
655 
656  return 0;
657 
658 error:
659  ff_frame_thread_free(avctx, i+1);
660 
661  return err;
662 }
663 
665 {
666  int i;
667  FrameThreadContext *fctx = avctx->internal->thread_ctx;
668 
669  if (!fctx) return;
670 
672  if (fctx->prev_thread) {
673  if (fctx->prev_thread != &fctx->threads[0])
675  }
676 
677  fctx->next_decoding = fctx->next_finished = 0;
678  fctx->delaying = 1;
679  fctx->prev_thread = NULL;
680  for (i = 0; i < avctx->thread_count; i++) {
681  PerThreadContext *p = &fctx->threads[i];
682  // Make sure decode flush calls with size=0 won't return old frames
683  p->got_frame = 0;
684  av_frame_unref(p->frame);
685 
687 
688  if (avctx->codec->flush)
689  avctx->codec->flush(p->avctx);
690  }
691 }
692 
694 {
695  PerThreadContext *p = avctx->internal->thread_ctx;
696  int err;
697 
698  f->owner = avctx;
699 
700  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
701  return ff_get_buffer(avctx, f->f, flags);
702 
703  if (p->state != STATE_SETTING_UP &&
704  (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
705  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
706  return -1;
707  }
708 
709  if (avctx->internal->allocate_progress) {
710  int *progress;
711  f->progress = av_buffer_alloc(2 * sizeof(int));
712  if (!f->progress) {
713  return AVERROR(ENOMEM);
714  }
715  progress = (int*)f->progress->data;
716 
717  progress[0] = progress[1] = -1;
718  }
719 
722  if (avctx->thread_safe_callbacks || (
724  !avctx->get_buffer &&
725 #endif
728  err = ff_get_buffer(avctx, f->f, flags);
729  } else {
730  p->requested_frame = f->f;
731  p->requested_flags = flags;
732  p->state = STATE_GET_BUFFER;
735 
736  while (p->state != STATE_SETTING_UP)
738 
739  err = p->result;
740 
742 
743  }
744  if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
745  ff_thread_finish_setup(avctx);
746 
747  if (err)
749 
751 
752  return err;
753 }
754 
756 {
757  PerThreadContext *p = avctx->internal->thread_ctx;
758  FrameThreadContext *fctx;
759  AVFrame *dst, *tmp;
761  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
762  avctx->thread_safe_callbacks ||
763  (
765  !avctx->get_buffer &&
766 #endif
769 
770  if (!f->f->buf[0])
771  return;
772 
773  if (avctx->debug & FF_DEBUG_BUFFERS)
774  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
775 
777  f->owner = NULL;
778 
779  if (can_direct_free) {
780  av_frame_unref(f->f);
781  return;
782  }
783 
784  fctx = p->parent;
786 
787  if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
788  goto fail;
790  (p->num_released_buffers + 1) *
791  sizeof(*p->released_buffers));
792  if (!tmp)
793  goto fail;
794  p->released_buffers = tmp;
795 
797  av_frame_move_ref(dst, f->f);
798 
800 
801 fail:
803 }
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:57
const struct AVCodec * codec
Definition: avcodec.h:1063
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:62
static int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:207
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:106
#define copy_fields(s, e)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:107
static void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:94
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:51
int av_cpu_count(void)
Definition: cpu.c:144
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
Definition: pthread_frame.c:91
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1227
void(* flush)(AVCodecContext *)
Flush buffers.
Definition: avcodec.h:2846
AVFrame * f
Definition: thread.h:36
memory handling functions
AVBufferRef * buf[AV_NUM_DATA_POINTERS]
AVBuffer references backing the data for this frame.
Definition: frame.h:365
static AVFrame * picture
Definition: output.c:179
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2506
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1754
int size
Definition: avcodec.h:974
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:1422
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
Definition: internal.h:97
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1247
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:292
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
Definition: avcodec.h:2840
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2488
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:56
void ff_thread_await_progress(ThreadFrame *f, int n, int field)
Wait for earlier decoding threads to finish reference pictures.
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:1927
int profile
profile
Definition: avcodec.h:2596
AVCodec.
Definition: avcodec.h:2755
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:65
attribute_deprecated void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1941
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1173
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
struct AVHWAccel * hwaccel
Hardware accelerator in use.
Definition: avcodec.h:2417
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
int(* init_thread_copy)(AVCodecContext *)
If defined, called on thread contexts when they are created.
Definition: avcodec.h:2804
Set before the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:75
uint8_t
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:43
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2429
static void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:136
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
void * thread_ctx
Definition: internal.h:91
Multithreading support functions.
CRITICAL_SECTION pthread_mutex_t
Definition: w32pthreads.h:54
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
uint8_t * data
Definition: avcodec.h:973
int requested_flags
flags passed to get_buffer() for requested_frame
Definition: pthread_frame.c:92
int next_decoding
The next context to submit a packet to.
static int flags
Definition: log.c:44
static int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:84
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:2481
Set when the codec calls get_buffer().
Definition: pthread_frame.c:76
void ff_thread_finish_setup(AVCodecContext *avctx)
If the codec defines update_thread_context(), call this when they are ready for the next thread to st...
enum AVChromaLocation chroma_sample_location
This defines the location of chroma samples.
Definition: avcodec.h:1761
Context stored in the client AVCodecInternal thread_ctx.
Definition: pthread_frame.c:98
AVCodecContext * avctx
Context used to decode packets passed to this thread.
Definition: pthread_frame.c:63
AVCodecContext * owner
Definition: thread.h:37
static void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:239
int ff_thread_decode_frame(AVCodecContext *avctx, AVFrame *picture, int *got_picture_ptr, AVPacket *avpkt)
Submit a new frame to a decoding thread.
int slice_count
slice count
Definition: avcodec.h:1397
int(* close)(AVCodecContext *)
Definition: avcodec.h:2841
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:123
int has_b_frames
Size of the frame reordering buffer in the decoder.
Definition: avcodec.h:1332
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
PerThreadContext * prev_thread
The last thread submit_packet() was called on.
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: mem.c:349
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
uint8_t * buf
backup storage for packet data when the input packet is not refcounted
Definition: pthread_frame.c:66
#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:740
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:64
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:55
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:104
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2533
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:119
int capabilities
Codec capabilities.
Definition: avcodec.h:2774
int result
The result of the last codec decode/encode() call.
Definition: pthread_frame.c:71
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:144
enum PerThreadContext::@56 state
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1142
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:148
int die
Set when threads should exit.
reference-counted frame API
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:509
int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
Definition: avcodec.h:1513
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:109
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:58
void(* draw_horiz_band)(struct AVCodecContext *s, const AVFrame *src, int offset[AV_NUM_DATA_POINTERS], int y, int type, int height)
If non NULL, 'draw_horiz_band' is called by the libavcodec decoder to draw a horizontal band...
Definition: avcodec.h:1281
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2525
#define FF_API_GET_BUFFER
Definition: version.h:61
#define FFMIN(a, b)
Definition: common.h:57
int width
picture width / height.
Definition: avcodec.h:1217
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2456
int priv_data_size
Definition: avcodec.h:2793
void ff_thread_report_progress(ThreadFrame *f, int n, int field)
Notify later decoding threads when part of their reference picture is ready.
enum AVColorPrimaries color_primaries
Chromaticity coordinates of the source primaries.
Definition: avcodec.h:1733
int level
level
Definition: avcodec.h:2679
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:2371
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
Definition: avcodec.h:2410
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1182
pthread_t thread
Definition: pthread_frame.c:54
#define FF_DEBUG_THREADS
Definition: avcodec.h:2372
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2514
if(ac->has_optimized_func)
int got_frame
The output of got_picture_ptr from the last avcodec_decode_video() call.
Definition: pthread_frame.c:70
static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
Update the next thread's AVCodecContext with values set by the user.
pthread_mutex_t buffer_mutex
Mutex used to protect get/release_buffer().
AVBufferRef * progress
Definition: thread.h:40
pthread_mutex_t progress_mutex
Mutex used to protect frame progress values and progress_cond.
Definition: pthread_frame.c:61
#define attribute_align_arg
Definition: internal.h:53
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:114
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:524
Libavcodec external API header.
enum AVMediaType codec_type
Definition: avcodec.h:1062
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
static void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:160
int debug
debug
Definition: avcodec.h:2348
int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
Wrapper around get_buffer() for frame-multithreaded codecs.
main external API structure.
Definition: avcodec.h:1054
uint8_t * data
The data buffer.
Definition: buffer.h:89
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: utils.c:575
int slice_flags
slice flags
Definition: avcodec.h:1551
int coded_height
Definition: avcodec.h:1227
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1747
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1740
int delaying
Set for the first N packets, where N is the number of threads.
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:74
refcounted data buffer API
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:2037
PerThreadContext * threads
The contexts for each thread.
Definition: pthread_frame.c:99
int allocate_progress
Whether to allocate progress for frame threading.
Definition: internal.h:79
#define MAX_AUTO_THREADS
AVFrame * released_buffers
Array of frames passed to ff_thread_release_buffer().
Definition: pthread_frame.c:87
struct FrameThreadContext * parent
Definition: pthread_frame.c:52
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:273
static void pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:176
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:113
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:193
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:368
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:75
common internal api header.
common internal and external API header
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
int released_buffers_allocated
Definition: pthread_frame.c:89
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
Update the next thread's AVCodecContext with values from the reference thread's context.
int thread_safe_callbacks
Set by the client if its custom get_buffer() callback can be called synchronously from another thread...
Definition: avcodec.h:2543
void * priv_data
Definition: avcodec.h:1090
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:2812
void ff_thread_flush(AVCodecContext *avctx)
Wait for decoding threads to finish and reset internal state.
AVFrame * frame
Output frame (for decoding) or input (for encoding).
Definition: pthread_frame.c:69
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
static void w32thread_init(void)
Definition: w32pthreads.h:264
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1098
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:60
w32threads to pthreads wrapper
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1149
static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
Waits for all threads to finish.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
int * slice_offset
slice offsets in the frame in bytes
Definition: avcodec.h:1413
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1810
static void release_delayed_buffers(PerThreadContext *p)
Releases the buffers that this decoding thread was the last user of.
int next_finished
The next context to return output from.
static enum AVDiscard skip_loop_filter
Definition: avplay.c:254
int(* init)(AVCodecContext *)
Definition: avcodec.h:2825
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:141
This structure stores compressed data.
Definition: avcodec.h:950
int allocated_buf_size
Size allocated for buf.
Definition: pthread_frame.c:67
Set after the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:80
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
int ff_frame_thread_init(AVCodecContext *avctx)
void * opaque
Private data of the user, can be used to carry app specific stuff.
Definition: avcodec.h:1105