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 #include "version.h"
40 
41 #include "libavutil/avassert.h"
42 #include "libavutil/buffer.h"
43 #include "libavutil/common.h"
44 #include "libavutil/cpu.h"
45 #include "libavutil/frame.h"
46 #include "libavutil/internal.h"
47 #include "libavutil/log.h"
48 #include "libavutil/mem.h"
49 
53 typedef struct PerThreadContext {
55 
61 
64 
66 
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 
193 #if FF_API_AFD
197 #endif /* FF_API_AFD */
198 
199  dst->profile = src->profile;
200  dst->level = src->level;
201 
203  dst->ticks_per_frame = src->ticks_per_frame;
204  dst->color_primaries = src->color_primaries;
205 
206  dst->color_trc = src->color_trc;
207  dst->colorspace = src->colorspace;
208  dst->color_range = src->color_range;
210 
211  dst->hwaccel = src->hwaccel;
212  dst->hwaccel_context = src->hwaccel_context;
214  }
215 
216  if (for_user) {
217  dst->coded_frame = src->coded_frame;
218  } else {
219  if (dst->codec->update_thread_context)
220  err = dst->codec->update_thread_context(dst, src);
221  }
222 
223  return err;
224 }
225 
234 {
235 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
236  dst->flags = src->flags;
237 
238  dst->draw_horiz_band= src->draw_horiz_band;
239  dst->get_buffer2 = src->get_buffer2;
240 #if FF_API_GET_BUFFER
242  dst->get_buffer = src->get_buffer;
243  dst->release_buffer = src->release_buffer;
245 #endif
246 
247  dst->opaque = src->opaque;
248  dst->debug = src->debug;
249 
250  dst->slice_flags = src->slice_flags;
251  dst->flags2 = src->flags2;
252 
253  copy_fields(skip_loop_filter, subtitle_header);
254 
255  dst->frame_number = src->frame_number;
257 
258  if (src->slice_count && src->slice_offset) {
259  if (dst->slice_count < src->slice_count) {
260  int *tmp = av_realloc(dst->slice_offset, src->slice_count *
261  sizeof(*dst->slice_offset));
262  if (!tmp) {
263  av_free(dst->slice_offset);
264  return AVERROR(ENOMEM);
265  }
266  dst->slice_offset = tmp;
267  }
268  memcpy(dst->slice_offset, src->slice_offset,
269  src->slice_count * sizeof(*dst->slice_offset));
270  }
271  dst->slice_count = src->slice_count;
272  return 0;
273 #undef copy_fields
274 }
275 
278 {
279  FrameThreadContext *fctx = p->parent;
280 
281  while (p->num_released_buffers > 0) {
282  AVFrame *f;
283 
285 
286  // fix extended data in case the caller screwed it up
289  f->extended_data = f->data;
290  av_frame_unref(f);
291 
293  }
294 }
295 
297 {
298  FrameThreadContext *fctx = p->parent;
299  PerThreadContext *prev_thread = fctx->prev_thread;
300  const AVCodec *codec = p->avctx->codec;
301 
302  if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
303 
305 
307 
308  if (prev_thread) {
309  int err;
310  if (prev_thread->state == STATE_SETTING_UP) {
311  pthread_mutex_lock(&prev_thread->progress_mutex);
312  while (prev_thread->state == STATE_SETTING_UP)
313  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
314  pthread_mutex_unlock(&prev_thread->progress_mutex);
315  }
316 
317  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
318  if (err) {
320  return err;
321  }
322  }
323 
324  av_packet_unref(&p->avpkt);
325  av_packet_ref(&p->avpkt, avpkt);
326 
327  p->state = STATE_SETTING_UP;
330 
331  /*
332  * If the client doesn't have a thread-safe get_buffer(),
333  * then decoding threads call back to the main thread,
334  * and it calls back to the client here.
335  */
336 
338  if (!p->avctx->thread_safe_callbacks && (
340  p->avctx->get_buffer ||
341 #endif
344  while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
346  while (p->state == STATE_SETTING_UP)
348 
349  if (p->state == STATE_GET_BUFFER) {
351  p->state = STATE_SETTING_UP;
353  }
355  }
356  }
357 
358  fctx->prev_thread = p;
359  fctx->next_decoding++;
360 
361  return 0;
362 }
363 
365  AVFrame *picture, int *got_picture_ptr,
366  AVPacket *avpkt)
367 {
368  FrameThreadContext *fctx = avctx->internal->thread_ctx;
369  int finished = fctx->next_finished;
370  PerThreadContext *p;
371  int err;
372 
373  /*
374  * Submit a packet to the next decoding thread.
375  */
376 
377  p = &fctx->threads[fctx->next_decoding];
378  err = update_context_from_user(p->avctx, avctx);
379  if (err) return err;
380  err = submit_packet(p, avpkt);
381  if (err) return err;
382 
383  /*
384  * If we're still receiving the initial packets, don't return a frame.
385  */
386 
387  if (fctx->delaying) {
388  if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
389 
390  *got_picture_ptr=0;
391  if (avpkt->size)
392  return avpkt->size;
393  }
394 
395  /*
396  * Return the next available frame from the oldest thread.
397  * If we're at the end of the stream, then we have to skip threads that
398  * didn't output a frame, because we don't want to accidentally signal
399  * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
400  */
401 
402  do {
403  p = &fctx->threads[finished++];
404 
405  if (p->state != STATE_INPUT_READY) {
407  while (p->state != STATE_INPUT_READY)
410  }
411 
412  av_frame_move_ref(picture, p->frame);
413  *got_picture_ptr = p->got_frame;
414  picture->pkt_dts = p->avpkt.dts;
415 
416  /*
417  * A later call with avkpt->size == 0 may loop over all threads,
418  * including this one, searching for a frame to return before being
419  * stopped by the "finished != fctx->next_finished" condition.
420  * Make sure we don't mistakenly return the same frame again.
421  */
422  p->got_frame = 0;
423 
424  if (finished >= avctx->thread_count) finished = 0;
425  } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
426 
427  update_context_from_thread(avctx, p->avctx, 1);
428 
429  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
430 
431  fctx->next_finished = finished;
432 
433  /* return the size of the consumed packet if no error occurred */
434  return (p->result >= 0) ? avpkt->size : p->result;
435 }
436 
437 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
438 {
439  PerThreadContext *p;
440  int *progress = f->progress ? (int*)f->progress->data : NULL;
441 
442  if (!progress || progress[field] >= n) return;
443 
444  p = f->owner->internal->thread_ctx;
445 
446  if (f->owner->debug&FF_DEBUG_THREADS)
447  av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
448 
450  progress[field] = n;
453 }
454 
455 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
456 {
457  PerThreadContext *p;
458  int *progress = f->progress ? (int*)f->progress->data : NULL;
459 
460  if (!progress || progress[field] >= n) return;
461 
462  p = f->owner->internal->thread_ctx;
463 
464  if (f->owner->debug&FF_DEBUG_THREADS)
465  av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
466 
468  while (progress[field] < n)
471 }
472 
474  PerThreadContext *p = avctx->internal->thread_ctx;
475 
476  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
477 
479  p->state = STATE_SETUP_FINISHED;
482 }
483 
485 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
486 {
487  int i;
488 
489  for (i = 0; i < thread_count; i++) {
490  PerThreadContext *p = &fctx->threads[i];
491 
492  if (p->state != STATE_INPUT_READY) {
494  while (p->state != STATE_INPUT_READY)
497  }
498  }
499 }
500 
501 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
502 {
503  FrameThreadContext *fctx = avctx->internal->thread_ctx;
504  const AVCodec *codec = avctx->codec;
505  int i;
506 
507  park_frame_worker_threads(fctx, thread_count);
508 
509  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
511 
512  fctx->die = 1;
513 
514  for (i = 0; i < thread_count; i++) {
515  PerThreadContext *p = &fctx->threads[i];
516 
520 
521  if (p->thread_init)
522  pthread_join(p->thread, NULL);
523 
524  if (codec->close)
525  codec->close(p->avctx);
526 
527  avctx->codec = NULL;
528 
530  av_frame_free(&p->frame);
531  }
532 
533  for (i = 0; i < thread_count; i++) {
534  PerThreadContext *p = &fctx->threads[i];
535 
541  av_packet_unref(&p->avpkt);
543 
544  if (i) {
545  av_freep(&p->avctx->priv_data);
547  }
548 
549  av_freep(&p->avctx->internal);
550  av_freep(&p->avctx);
551  }
552 
553  av_freep(&fctx->threads);
555  av_freep(&avctx->internal->thread_ctx);
556 }
557 
559 {
560  int thread_count = avctx->thread_count;
561  const AVCodec *codec = avctx->codec;
562  AVCodecContext *src = avctx;
563  FrameThreadContext *fctx;
564  int i, err = 0;
565 
566 #if HAVE_W32THREADS
567  w32thread_init();
568 #endif
569 
570  if (!thread_count) {
571  int nb_cpus = av_cpu_count();
572  av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
573  // use number of cores + 1 as thread count if there is more than one
574  if (nb_cpus > 1)
575  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
576  else
577  thread_count = avctx->thread_count = 1;
578  }
579 
580  if (thread_count <= 1) {
581  avctx->active_thread_type = 0;
582  return 0;
583  }
584 
585  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
586 
587  fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
589  fctx->delaying = 1;
590 
591  for (i = 0; i < thread_count; i++) {
593  PerThreadContext *p = &fctx->threads[i];
594 
600 
601  p->frame = av_frame_alloc();
602  if (!p->frame) {
603  err = AVERROR(ENOMEM);
604  goto error;
605  }
606 
607  p->parent = fctx;
608  p->avctx = copy;
609 
610  if (!copy) {
611  err = AVERROR(ENOMEM);
612  goto error;
613  }
614 
615  *copy = *src;
616 
617  copy->internal = av_malloc(sizeof(AVCodecInternal));
618  if (!copy->internal) {
619  copy->priv_data = NULL;
620  err = AVERROR(ENOMEM);
621  goto error;
622  }
623  *copy->internal = *src->internal;
624  copy->internal->thread_ctx = p;
625  copy->internal->pkt = &p->avpkt;
626 
627  if (!i) {
628  src = copy;
629 
630  if (codec->init)
631  err = codec->init(copy);
632 
633  update_context_from_thread(avctx, copy, 1);
634  } else {
635  copy->priv_data = av_malloc(codec->priv_data_size);
636  if (!copy->priv_data) {
637  err = AVERROR(ENOMEM);
638  goto error;
639  }
640  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
641  copy->internal->is_copy = 1;
642 
643  if (codec->init_thread_copy)
644  err = codec->init_thread_copy(copy);
645  }
646 
647  if (err) goto error;
648 
650  p->thread_init = 1;
651  }
652 
653  return 0;
654 
655 error:
656  ff_frame_thread_free(avctx, i+1);
657 
658  return err;
659 }
660 
662 {
663  int i;
664  FrameThreadContext *fctx = avctx->internal->thread_ctx;
665 
666  if (!fctx) return;
667 
669  if (fctx->prev_thread) {
670  if (fctx->prev_thread != &fctx->threads[0])
672  }
673 
674  fctx->next_decoding = fctx->next_finished = 0;
675  fctx->delaying = 1;
676  fctx->prev_thread = NULL;
677  for (i = 0; i < avctx->thread_count; i++) {
678  PerThreadContext *p = &fctx->threads[i];
679  // Make sure decode flush calls with size=0 won't return old frames
680  p->got_frame = 0;
681  av_frame_unref(p->frame);
682 
684 
685  if (avctx->codec->flush)
686  avctx->codec->flush(p->avctx);
687  }
688 }
689 
691 {
692  PerThreadContext *p = avctx->internal->thread_ctx;
693  int err;
694 
695  f->owner = avctx;
696 
697  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
698  return ff_get_buffer(avctx, f->f, flags);
699 
700  if (p->state != STATE_SETTING_UP &&
701  (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
702  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
703  return -1;
704  }
705 
706  if (avctx->internal->allocate_progress) {
707  int *progress;
708  f->progress = av_buffer_alloc(2 * sizeof(int));
709  if (!f->progress) {
710  return AVERROR(ENOMEM);
711  }
712  progress = (int*)f->progress->data;
713 
714  progress[0] = progress[1] = -1;
715  }
716 
719  if (avctx->thread_safe_callbacks || (
721  !avctx->get_buffer &&
722 #endif
725  err = ff_get_buffer(avctx, f->f, flags);
726  } else {
727  p->requested_frame = f->f;
728  p->requested_flags = flags;
729  p->state = STATE_GET_BUFFER;
732 
733  while (p->state != STATE_SETTING_UP)
735 
736  err = p->result;
737 
739 
740  }
741  if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
742  ff_thread_finish_setup(avctx);
743 
744  if (err)
746 
748 
749  return err;
750 }
751 
753 {
754  PerThreadContext *p = avctx->internal->thread_ctx;
755  FrameThreadContext *fctx;
756  AVFrame *dst, *tmp;
758  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
759  avctx->thread_safe_callbacks ||
760  (
762  !avctx->get_buffer &&
763 #endif
766 
767  if (!f->f || !f->f->buf[0])
768  return;
769 
770  if (avctx->debug & FF_DEBUG_BUFFERS)
771  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
772 
774  f->owner = NULL;
775 
776  if (can_direct_free) {
777  av_frame_unref(f->f);
778  return;
779  }
780 
781  fctx = p->parent;
783 
784  if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
785  goto fail;
787  (p->num_released_buffers + 1) *
788  sizeof(*p->released_buffers));
789  if (!tmp)
790  goto fail;
791  p->released_buffers = tmp;
792 
794  av_frame_move_ref(dst, f->f);
795 
797 
798 fail:
800 }
static av_unused void pthread_cond_signal(pthread_cond_t *cond)
Definition: w32pthreads.h:252
static av_unused void w32thread_init(void)
Definition: w32pthreads.h:277
pthread_cond_t progress_cond
Used by child threads to wait for progress to change.
Definition: pthread_frame.c:59
const struct AVCodec * codec
Definition: avcodec.h:1059
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
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:135
Context used by codec threads and stored in their AVCodecInternal thread_ctx.
Definition: pthread_frame.c:53
int av_cpu_count(void)
Definition: cpu.c:151
AVFrame * requested_frame
AVFrame the codec passed to get_buffer()
Definition: pthread_frame.c:91
static av_unused void pthread_cond_init(pthread_cond_t *cond, const void *unused_attr)
Definition: w32pthreads.h:149
int coded_width
Bitstream width / height, may be different from width/height e.g.
Definition: avcodec.h:1244
void(* flush)(AVCodecContext *)
Flush buffers.
Definition: avcodec.h:2903
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:393
AVFrame * coded_frame
the picture in the bitstream
Definition: avcodec.h:2548
enum AVColorRange color_range
MPEG vs JPEG YUV range.
Definition: avcodec.h:1782
static av_unused void pthread_cond_broadcast(pthread_cond_t *cond)
Definition: w32pthreads.h:189
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:1445
AVPacket * pkt
Current packet as passed into the decoder, to avoid having to pass the packet into every function...
Definition: internal.h:99
enum AVPixelFormat pix_fmt
Pixel format, see AV_PIX_FMT_xxx.
Definition: avcodec.h:1270
enum PerThreadContext::@57 state
void av_frame_move_ref(AVFrame *dst, AVFrame *src)
Move everythnig contained in src to dst and reset src.
Definition: frame.c:300
int(* decode)(AVCodecContext *, void *outdata, int *outdata_size, AVPacket *avpkt)
Definition: avcodec.h:2897
int bits_per_raw_sample
Bits per sample/pixel of internal libavcodec pixel/sample format.
Definition: avcodec.h:2530
pthread_cond_t input_cond
Used to wait for a new packet from the main thread.
Definition: pthread_frame.c:58
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:1955
int profile
profile
Definition: avcodec.h:2638
AVCodec.
Definition: avcodec.h:2812
AVPacket avpkt
Input packet (for decoding) or output (for encoding).
Definition: pthread_frame.c:67
attribute_deprecated void(* release_buffer)(struct AVCodecContext *c, AVFrame *pic)
Called to release buffers which were allocated with get_buffer.
Definition: avcodec.h:1969
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented...
Definition: avcodec.h:1175
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:2448
#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:2861
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
Definition: frame.c:57
void * hwaccel_context
Hardware accelerator context.
Definition: avcodec.h:2460
static attribute_align_arg void * frame_worker_thread(void *arg)
Codec worker thread.
void * thread_ctx
Definition: internal.h:93
Multithreading support functions.
CRITICAL_SECTION pthread_mutex_t
Definition: w32pthreads.h:55
static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
static av_unused int pthread_create(pthread_t *thread, const void *unused_attr, void *(*start_routine)(void *), void *arg)
Definition: w32pthreads.h:97
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.
Set before the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:75
static int flags
Definition: log.c:44
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:2523
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:1789
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:65
AVCodecContext * owner
Definition: thread.h:37
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:1420
Libavcodec version macros.
int(* close)(AVCodecContext *)
Definition: avcodec.h:2898
#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:1355
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:369
void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
Wrapper around release_buffer() frame-for multithreaded codecs.
#define 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:713
int is_copy
Whether the parent AVCodecContext is a copy of the context which had init() called on it...
Definition: internal.h:66
#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:69
static int pthread_mutex_init(pthread_mutex_t *m, void *attr)
Definition: w32pthreads.h:117
int active_thread_type
Which multithreading methods are in use by the codec.
Definition: avcodec.h:2575
static int pthread_mutex_unlock(pthread_mutex_t *m)
Definition: w32pthreads.h:132
int capabilities
Codec capabilities.
Definition: avcodec.h:2831
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
int flags
CODEC_FLAG_*.
Definition: avcodec.h:1144
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:169
int die
Set when threads should exit.
reference-counted frame API
static av_unused void pthread_join(pthread_t thread, void **value_ptr)
Definition: w32pthreads.h:107
common internal API header
static int pthread_mutex_destroy(pthread_mutex_t *m)
Definition: w32pthreads.h:122
static av_unused void pthread_cond_destroy(pthread_cond_t *cond)
Definition: w32pthreads.h:173
pthread_cond_t output_cond
Used by the main thread to wait for frames to finish.
Definition: pthread_frame.c:60
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:1304
#define FF_THREAD_FRAME
Decode more than one frame at once.
Definition: avcodec.h:2567
#define FF_API_GET_BUFFER
Definition: version.h:61
#define FFMIN(a, b)
Definition: common.h:57
static av_unused int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
Definition: w32pthreads.h:220
Set after the codec has called ff_thread_finish_setup().
Definition: pthread_frame.c:80
int width
picture width / height.
Definition: avcodec.h:1229
int idct_algo
IDCT algorithm, see FF_IDCT_* below.
Definition: avcodec.h:2489
int priv_data_size
Definition: avcodec.h:2850
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:1761
int level
level
Definition: avcodec.h:2721
#define FF_DEBUG_BUFFERS
Definition: avcodec.h:2403
int64_t reordered_opaque
opaque 64bit number (generally a PTS) that will be reordered and output in AVFrame.reordered_opaque
Definition: avcodec.h:2441
int ticks_per_frame
For some codecs, the time base is closer to the field rate than the frame rate.
Definition: avcodec.h:1184
pthread_t thread
Definition: pthread_frame.c:56
#define FF_DEBUG_THREADS
Definition: avcodec.h:2404
int thread_count
thread count is used to decide how many independent tasks should be passed to execute() ...
Definition: avcodec.h:2556
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:63
#define attribute_align_arg
Definition: internal.h:54
NULL
Definition: eval.c:55
static int pthread_mutex_lock(pthread_mutex_t *m)
Definition: w32pthreads.h:127
int avcodec_default_get_buffer2(AVCodecContext *s, AVFrame *frame, int flags)
The default callback for AVCodecContext.get_buffer2().
Definition: utils.c:518
Libavcodec external API header.
enum AVMediaType codec_type
Definition: avcodec.h:1058
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
int debug
debug
Definition: avcodec.h:2378
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:1050
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:346
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:621
Set when the codec calls get_buffer().
Definition: pthread_frame.c:76
int slice_flags
slice flags
Definition: avcodec.h:1577
int coded_height
Definition: avcodec.h:1244
enum AVColorSpace colorspace
YUV colorspace type.
Definition: avcodec.h:1775
enum AVColorTransferCharacteristic color_trc
Color Transfer Characteristic.
Definition: avcodec.h:1768
int delaying
Set for the first N packets, where N is the number of threads.
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:2065
attribute_deprecated int dtg_active_format
DTG active format information (additional aspect ratio information only used in DVB MPEG-2 transport ...
Definition: avcodec.h:1538
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:81
#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:54
void av_frame_unref(AVFrame *frame)
Unreference all the buffers referenced by frame and reset the frame fields.
Definition: frame.c:283
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:141
int64_t pkt_dts
DTS copied from the AVPacket that triggered returning this frame.
Definition: frame.h:221
Set when the thread is awaiting a packet.
Definition: pthread_frame.c:74
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
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
void * hwaccel_priv_data
hwaccel-specific private data
Definition: internal.h:104
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:2585
int av_packet_ref(AVPacket *dst, AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:355
void * priv_data
Definition: avcodec.h:1092
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:2869
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:77
struct AVCodecInternal * internal
Private context used for internal data.
Definition: avcodec.h:1100
pthread_mutex_t mutex
Mutex used to protect the contents of the PerThreadContext.
Definition: pthread_frame.c:62
w32threads to pthreads wrapper
int flags2
CODEC_FLAG2_*.
Definition: avcodec.h:1151
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:1436
int frame_number
Frame counter, set by libavcodec.
Definition: avcodec.h:1838
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:2882
uint8_t ** extended_data
pointers to the data planes/channels.
Definition: frame.h:169
This structure stores compressed data.
Definition: avcodec.h:950
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:1107