FFmpeg  2.6.9
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
pthread_frame.c
Go to the documentation of this file.
1 /*
2  * This file is part of FFmpeg.
3  *
4  * FFmpeg is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * FFmpeg is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with FFmpeg; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /**
20  * @file
21  * Frame multithreading support functions
22  * @see doc/multithreading.txt
23  */
24 
25 #include "config.h"
26 
27 #include <stdint.h>
28 
29 #if HAVE_PTHREADS
30 #include <pthread.h>
31 #elif HAVE_W32THREADS
32 #include "compat/w32pthreads.h"
33 #elif HAVE_OS2THREADS
34 #include "compat/os2threads.h"
35 #endif
36 
37 #include "avcodec.h"
38 #include "internal.h"
39 #include "pthread_internal.h"
40 #include "thread.h"
41 #include "version.h"
42 
43 #include "libavutil/avassert.h"
44 #include "libavutil/buffer.h"
45 #include "libavutil/common.h"
46 #include "libavutil/cpu.h"
47 #include "libavutil/frame.h"
48 #include "libavutil/internal.h"
49 #include "libavutil/log.h"
50 #include "libavutil/mem.h"
51 
52 /**
53  * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
54  */
55 typedef struct PerThreadContext {
57 
60  pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
61  pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
62  pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
63 
64  pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
65  pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
66 
67  AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
68 
69  AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
70 
71  AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
72  int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
73  int result; ///< The result of the last codec decode/encode() call.
74 
75  enum {
76  STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
77  STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
79  * Set when the codec calls get_buffer().
80  * State is returned to STATE_SETTING_UP afterwards.
81  */
83  * Set when the codec calls get_format().
84  * State is returned to STATE_SETTING_UP afterwards.
85  */
86  STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
87  } state;
88 
89  /**
90  * Array of frames passed to ff_thread_release_buffer().
91  * Frames are released after all threads referencing them are finished.
92  */
96 
97  AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
98  int requested_flags; ///< flags passed to get_buffer() for requested_frame
99 
100  const enum AVPixelFormat *available_formats; ///< Format array for get_format()
101  enum AVPixelFormat result_format; ///< get_format() result
103 
104 /**
105  * Context stored in the client AVCodecInternal thread_ctx.
106  */
107 typedef struct FrameThreadContext {
108  PerThreadContext *threads; ///< The contexts for each thread.
109  PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
110 
111  pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
112 
113  int next_decoding; ///< The next context to submit a packet to.
114  int next_finished; ///< The next context to return output from.
115 
116  int delaying; /**<
117  * Set for the first N packets, where N is the number of threads.
118  * While it is set, ff_thread_en/decode_frame won't return any results.
119  */
120 
121  int die; ///< Set when threads should exit.
123 
124 #if FF_API_GET_BUFFER
125 #define THREAD_SAFE_CALLBACKS(avctx) \
126 ((avctx)->thread_safe_callbacks || (!(avctx)->get_buffer && (avctx)->get_buffer2 == avcodec_default_get_buffer2))
127 #else
128 #define THREAD_SAFE_CALLBACKS(avctx) \
129 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
130 #endif
131 
132 /**
133  * Codec worker thread.
134  *
135  * Automatically calls ff_thread_finish_setup() if the codec does
136  * not provide an update_thread_context method, or if the codec returns
137  * before calling it.
138  */
140 {
141  PerThreadContext *p = arg;
142  FrameThreadContext *fctx = p->parent;
143  AVCodecContext *avctx = p->avctx;
144  const AVCodec *codec = avctx->codec;
145 
147  while (1) {
148  while (p->state == STATE_INPUT_READY && !fctx->die)
150 
151  if (fctx->die) break;
152 
153  if (!codec->update_thread_context && THREAD_SAFE_CALLBACKS(avctx))
154  ff_thread_finish_setup(avctx);
155 
156  av_frame_unref(p->frame);
157  p->got_frame = 0;
158  p->result = codec->decode(avctx, p->frame, &p->got_frame, &p->avpkt);
159 
160  if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
161  if (avctx->internal->allocate_progress)
162  av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
163  "free the frame on failure. This is a bug, please report it.\n");
164  av_frame_unref(p->frame);
165  }
166 
167  if (p->state == STATE_SETTING_UP) ff_thread_finish_setup(avctx);
168 
170 #if 0 //BUFREF-FIXME
171  for (i = 0; i < MAX_BUFFERS; i++)
172  if (p->progress_used[i] && (p->got_frame || p->result<0 || avctx->codec_id != AV_CODEC_ID_H264)) {
173  p->progress[i][0] = INT_MAX;
174  p->progress[i][1] = INT_MAX;
175  }
176 #endif
177  p->state = STATE_INPUT_READY;
178 
182  }
184 
185  return NULL;
186 }
187 
188 /**
189  * Update the next thread's AVCodecContext with values from the reference thread's context.
190  *
191  * @param dst The destination context.
192  * @param src The source context.
193  * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
194  */
196 {
197  int err = 0;
198 
199  if (dst != src) {
200  dst->time_base = src->time_base;
201  dst->framerate = src->framerate;
202  dst->width = src->width;
203  dst->height = src->height;
204  dst->pix_fmt = src->pix_fmt;
205 
206  dst->coded_width = src->coded_width;
207  dst->coded_height = src->coded_height;
208 
209  dst->has_b_frames = src->has_b_frames;
210  dst->idct_algo = src->idct_algo;
211 
214 #if FF_API_AFD
218 #endif /* FF_API_AFD */
219 
220  dst->profile = src->profile;
221  dst->level = src->level;
222 
224  dst->ticks_per_frame = src->ticks_per_frame;
225  dst->color_primaries = src->color_primaries;
226 
227  dst->color_trc = src->color_trc;
228  dst->colorspace = src->colorspace;
229  dst->color_range = src->color_range;
231 
232  dst->hwaccel = src->hwaccel;
233  dst->hwaccel_context = src->hwaccel_context;
234 
235  dst->channels = src->channels;
236  dst->sample_rate = src->sample_rate;
237  dst->sample_fmt = src->sample_fmt;
238  dst->channel_layout = src->channel_layout;
240  }
241 
242  if (for_user) {
243  dst->delay = src->thread_count - 1;
244  dst->coded_frame = src->coded_frame;
245  } else {
246  if (dst->codec->update_thread_context)
247  err = dst->codec->update_thread_context(dst, src);
248  }
249 
250  return err;
251 }
252 
253 /**
254  * Update the next thread's AVCodecContext with values set by the user.
255  *
256  * @param dst The destination context.
257  * @param src The source context.
258  * @return 0 on success, negative error code on failure
259  */
261 {
262 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
263  dst->flags = src->flags;
264 
265  dst->draw_horiz_band= src->draw_horiz_band;
266  dst->get_buffer2 = src->get_buffer2;
267 #if FF_API_GET_BUFFER
269  dst->get_buffer = src->get_buffer;
270  dst->release_buffer = src->release_buffer;
272 #endif
273 
274  dst->opaque = src->opaque;
275  dst->debug = src->debug;
276  dst->debug_mv = src->debug_mv;
277 
278  dst->slice_flags = src->slice_flags;
279  dst->flags2 = src->flags2;
280 
281  copy_fields(skip_loop_filter, subtitle_header);
282 
283  dst->frame_number = src->frame_number;
286 
287  if (src->slice_count && src->slice_offset) {
288  if (dst->slice_count < src->slice_count) {
289  int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
290  sizeof(*dst->slice_offset));
291  if (err < 0)
292  return err;
293  }
294  memcpy(dst->slice_offset, src->slice_offset,
295  src->slice_count * sizeof(*dst->slice_offset));
296  }
297  dst->slice_count = src->slice_count;
298  return 0;
299 #undef copy_fields
300 }
301 
302 /// Releases the buffers that this decoding thread was the last user of.
304 {
305  FrameThreadContext *fctx = p->parent;
306 
307  while (p->num_released_buffers > 0) {
308  AVFrame *f;
309 
311 
312  // fix extended data in case the caller screwed it up
316  f->extended_data = f->data;
317  av_frame_unref(f);
318 
320  }
321 }
322 
324 {
325  FrameThreadContext *fctx = p->parent;
326  PerThreadContext *prev_thread = fctx->prev_thread;
327  const AVCodec *codec = p->avctx->codec;
328 
329  if (!avpkt->size && !(codec->capabilities & CODEC_CAP_DELAY)) return 0;
330 
332 
334 
335  if (prev_thread) {
336  int err;
337  if (prev_thread->state == STATE_SETTING_UP) {
338  pthread_mutex_lock(&prev_thread->progress_mutex);
339  while (prev_thread->state == STATE_SETTING_UP)
340  pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
341  pthread_mutex_unlock(&prev_thread->progress_mutex);
342  }
343 
344  err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
345  if (err) {
347  return err;
348  }
349  }
350 
351  av_packet_unref(&p->avpkt);
352  av_packet_ref(&p->avpkt, avpkt);
353 
354  p->state = STATE_SETTING_UP;
357 
358  /*
359  * If the client doesn't have a thread-safe get_buffer(),
360  * then decoding threads call back to the main thread,
361  * and it calls back to the client here.
362  */
363 
365  if (!p->avctx->thread_safe_callbacks && (
368  p->avctx->get_buffer ||
369 #endif
372  while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
373  int call_done = 1;
375  while (p->state == STATE_SETTING_UP)
377 
378  switch (p->state) {
379  case STATE_GET_BUFFER:
381  break;
382  case STATE_GET_FORMAT:
384  break;
385  default:
386  call_done = 0;
387  break;
388  }
389  if (call_done) {
390  p->state = STATE_SETTING_UP;
392  }
394  }
395  }
396 
397  fctx->prev_thread = p;
398  fctx->next_decoding++;
399 
400  return 0;
401 }
402 
404  AVFrame *picture, int *got_picture_ptr,
405  AVPacket *avpkt)
406 {
407  FrameThreadContext *fctx = avctx->internal->thread_ctx;
408  int finished = fctx->next_finished;
409  PerThreadContext *p;
410  int err;
411 
412  /*
413  * Submit a packet to the next decoding thread.
414  */
415 
416  p = &fctx->threads[fctx->next_decoding];
417  err = update_context_from_user(p->avctx, avctx);
418  if (err) return err;
419  err = submit_packet(p, avpkt);
420  if (err) return err;
421 
422  /*
423  * If we're still receiving the initial packets, don't return a frame.
424  */
425 
426  if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
427  fctx->delaying = 0;
428 
429  if (fctx->delaying) {
430  *got_picture_ptr=0;
431  if (avpkt->size)
432  return avpkt->size;
433  }
434 
435  /*
436  * Return the next available frame from the oldest thread.
437  * If we're at the end of the stream, then we have to skip threads that
438  * didn't output a frame, because we don't want to accidentally signal
439  * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
440  */
441 
442  do {
443  p = &fctx->threads[finished++];
444 
445  if (p->state != STATE_INPUT_READY) {
447  while (p->state != STATE_INPUT_READY)
450  }
451 
452  av_frame_move_ref(picture, p->frame);
453  *got_picture_ptr = p->got_frame;
454  picture->pkt_dts = p->avpkt.dts;
455 
456  if (p->result < 0)
457  err = p->result;
458 
459  /*
460  * A later call with avkpt->size == 0 may loop over all threads,
461  * including this one, searching for a frame to return before being
462  * stopped by the "finished != fctx->next_finished" condition.
463  * Make sure we don't mistakenly return the same frame again.
464  */
465  p->got_frame = 0;
466 
467  if (finished >= avctx->thread_count) finished = 0;
468  } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
469 
470  update_context_from_thread(avctx, p->avctx, 1);
471 
472  if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
473 
474  fctx->next_finished = finished;
475 
476  /*
477  * When no frame was found while flushing, but an error occured in
478  * any thread, return it instead of 0.
479  * Otherwise the error can get lost.
480  */
481  if (!avpkt->size && !*got_picture_ptr)
482  return err;
483 
484  /* return the size of the consumed packet if no error occurred */
485  return (p->result >= 0) ? avpkt->size : p->result;
486 }
487 
488 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
489 {
490  PerThreadContext *p;
491  volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
492 
493  if (!progress || progress[field] >= n) return;
494 
495  p = f->owner->internal->thread_ctx;
496 
497  if (f->owner->debug&FF_DEBUG_THREADS)
498  av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
499 
501  progress[field] = n;
504 }
505 
506 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
507 {
508  PerThreadContext *p;
509  volatile int *progress = f->progress ? (int*)f->progress->data : NULL;
510 
511  if (!progress || progress[field] >= n) return;
512 
513  p = f->owner->internal->thread_ctx;
514 
515  if (f->owner->debug&FF_DEBUG_THREADS)
516  av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
517 
519  while (progress[field] < n)
522 }
523 
525  PerThreadContext *p = avctx->internal->thread_ctx;
526 
527  if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
528 
529  if(p->state == STATE_SETUP_FINISHED){
530  av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
531  }
532 
534  p->state = STATE_SETUP_FINISHED;
537 }
538 
539 /// Waits for all threads to finish.
540 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
541 {
542  int i;
543 
544  for (i = 0; i < thread_count; i++) {
545  PerThreadContext *p = &fctx->threads[i];
546 
547  if (p->state != STATE_INPUT_READY) {
549  while (p->state != STATE_INPUT_READY)
552  }
553  p->got_frame = 0;
554  }
555 }
556 
557 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
558 {
559  FrameThreadContext *fctx = avctx->internal->thread_ctx;
560  const AVCodec *codec = avctx->codec;
561  int i;
562 
563  park_frame_worker_threads(fctx, thread_count);
564 
565  if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
566  if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
567  av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
569  fctx->threads->avctx->internal->is_copy = 1;
570  }
571 
572  fctx->die = 1;
573 
574  for (i = 0; i < thread_count; i++) {
575  PerThreadContext *p = &fctx->threads[i];
576 
580 
581  if (p->thread_init)
582  pthread_join(p->thread, NULL);
583  p->thread_init=0;
584 
585  if (codec->close && p->avctx)
586  codec->close(p->avctx);
587 
588  avctx->codec = NULL;
589 
591  av_frame_free(&p->frame);
592  }
593 
594  for (i = 0; i < thread_count; i++) {
595  PerThreadContext *p = &fctx->threads[i];
596 
602  av_packet_unref(&p->avpkt);
604 
605  if (i && p->avctx) {
606  av_freep(&p->avctx->priv_data);
608  }
609 
610  if (p->avctx)
611  av_freep(&p->avctx->internal);
612  av_freep(&p->avctx);
613  }
614 
615  av_freep(&fctx->threads);
617  av_freep(&avctx->internal->thread_ctx);
618 }
619 
621 {
622  int thread_count = avctx->thread_count;
623  const AVCodec *codec = avctx->codec;
624  AVCodecContext *src = avctx;
625  FrameThreadContext *fctx;
626  int i, err = 0;
627 
628 #if HAVE_W32THREADS
629  w32thread_init();
630 #endif
631 
632  if (!thread_count) {
633  int nb_cpus = av_cpu_count();
634  if ((avctx->debug & (FF_DEBUG_VIS_QP | FF_DEBUG_VIS_MB_TYPE)) || avctx->debug_mv)
635  nb_cpus = 1;
636  // use number of cores + 1 as thread count if there is more than one
637  if (nb_cpus > 1)
638  thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
639  else
640  thread_count = avctx->thread_count = 1;
641  }
642 
643  if (thread_count <= 1) {
644  avctx->active_thread_type = 0;
645  return 0;
646  }
647 
648  avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
649 
650  fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
652  fctx->delaying = 1;
653 
654  for (i = 0; i < thread_count; i++) {
656  PerThreadContext *p = &fctx->threads[i];
657 
663 
664  p->frame = av_frame_alloc();
665  if (!p->frame) {
666  av_freep(&copy);
667  err = AVERROR(ENOMEM);
668  goto error;
669  }
670 
671  p->parent = fctx;
672  p->avctx = copy;
673 
674  if (!copy) {
675  err = AVERROR(ENOMEM);
676  goto error;
677  }
678 
679  *copy = *src;
680 
681  copy->internal = av_malloc(sizeof(AVCodecInternal));
682  if (!copy->internal) {
683  copy->priv_data = NULL;
684  err = AVERROR(ENOMEM);
685  goto error;
686  }
687  *copy->internal = *src->internal;
688  copy->internal->thread_ctx = p;
689  copy->internal->pkt = &p->avpkt;
690 
691  if (!i) {
692  src = copy;
693 
694  if (codec->init)
695  err = codec->init(copy);
696 
697  update_context_from_thread(avctx, copy, 1);
698  } else {
699  copy->priv_data = av_malloc(codec->priv_data_size);
700  if (!copy->priv_data) {
701  err = AVERROR(ENOMEM);
702  goto error;
703  }
704  memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
705  copy->internal->is_copy = 1;
706 
707  if (codec->init_thread_copy)
708  err = codec->init_thread_copy(copy);
709  }
710 
711  if (err) goto error;
712 
714  p->thread_init= !err;
715  if(!p->thread_init)
716  goto error;
717  }
718 
719  return 0;
720 
721 error:
722  ff_frame_thread_free(avctx, i+1);
723 
724  return err;
725 }
726 
728 {
729  int i;
730  FrameThreadContext *fctx = avctx->internal->thread_ctx;
731 
732  if (!fctx) return;
733 
735  if (fctx->prev_thread) {
736  if (fctx->prev_thread != &fctx->threads[0])
738  }
739 
740  fctx->next_decoding = fctx->next_finished = 0;
741  fctx->delaying = 1;
742  fctx->prev_thread = NULL;
743  for (i = 0; i < avctx->thread_count; i++) {
744  PerThreadContext *p = &fctx->threads[i];
745  // Make sure decode flush calls with size=0 won't return old frames
746  p->got_frame = 0;
747  av_frame_unref(p->frame);
748 
750 
751  if (avctx->codec->flush)
752  avctx->codec->flush(p->avctx);
753  }
754 }
755 
757 {
758  PerThreadContext *p = avctx->internal->thread_ctx;
759  if ((avctx->active_thread_type&FF_THREAD_FRAME) && p->state != STATE_SETTING_UP &&
760  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
761  return 0;
762  }
763  return 1;
764 }
765 
767 {
768  PerThreadContext *p = avctx->internal->thread_ctx;
769  int err;
770 
771  f->owner = avctx;
772 
773  ff_init_buffer_info(avctx, f->f);
774 
775  if (!(avctx->active_thread_type & FF_THREAD_FRAME))
776  return ff_get_buffer(avctx, f->f, flags);
777 
778  if (p->state != STATE_SETTING_UP &&
779  (avctx->codec->update_thread_context || !THREAD_SAFE_CALLBACKS(avctx))) {
780  av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
781  return -1;
782  }
783 
784  if (avctx->internal->allocate_progress) {
785  int *progress;
786  f->progress = av_buffer_alloc(2 * sizeof(int));
787  if (!f->progress) {
788  return AVERROR(ENOMEM);
789  }
790  progress = (int*)f->progress->data;
791 
792  progress[0] = progress[1] = -1;
793  }
794 
797  if (avctx->thread_safe_callbacks || (
799  !avctx->get_buffer &&
800 #endif
803  err = ff_get_buffer(avctx, f->f, flags);
804  } else {
806  p->requested_frame = f->f;
807  p->requested_flags = flags;
808  p->state = STATE_GET_BUFFER;
810 
811  while (p->state != STATE_SETTING_UP)
813 
814  err = p->result;
815 
817 
818  }
819  if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
820  ff_thread_finish_setup(avctx);
821 
822  if (err)
824 
826 
827  return err;
828 }
829 
831 {
832  enum AVPixelFormat res;
833  PerThreadContext *p = avctx->internal->thread_ctx;
834  if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
836  return ff_get_format(avctx, fmt);
837  if (p->state != STATE_SETTING_UP) {
838  av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
839  return -1;
840  }
842  p->available_formats = fmt;
843  p->state = STATE_GET_FORMAT;
845 
846  while (p->state != STATE_SETTING_UP)
848 
849  res = p->result_format;
850 
852 
853  return res;
854 }
855 
857 {
858  int ret = thread_get_buffer_internal(avctx, f, flags);
859  if (ret < 0)
860  av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
861  return ret;
862 }
863 
865 {
866  PerThreadContext *p = avctx->internal->thread_ctx;
867  FrameThreadContext *fctx;
868  AVFrame *dst, *tmp;
870  int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
871  avctx->thread_safe_callbacks ||
872  (
874  !avctx->get_buffer &&
875 #endif
878 
879  if (!f->f || !f->f->buf[0])
880  return;
881 
882  if (avctx->debug & FF_DEBUG_BUFFERS)
883  av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
884 
886  f->owner = NULL;
887 
888  if (can_direct_free) {
889  av_frame_unref(f->f);
890  return;
891  }
892 
893  fctx = p->parent;
895 
896  if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
897  goto fail;
899  (p->num_released_buffers + 1) *
900  sizeof(*p->released_buffers));
901  if (!tmp)
902  goto fail;
903  p->released_buffers = tmp;
904 
906  av_frame_move_ref(dst, f->f);
907 
909 
910 fail:
912 }