lavc: Drop deprecated get_buffer related functions
[libav.git] / libavcodec / pthread_frame.c
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
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 #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
50 /**
51 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
52 */
53 typedef struct PerThreadContext {
54 struct FrameThreadContext *parent;
55
56 pthread_t thread;
57 int thread_init;
58 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
59 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
60 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
61
62 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
63 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
64
65 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
66
67 AVPacket avpkt; ///< Input packet (for decoding) or output (for encoding).
68
69 AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
70 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
71 int result; ///< The result of the last codec decode/encode() call.
72
73 enum {
74 STATE_INPUT_READY, ///< Set when the thread is awaiting a packet.
75 STATE_SETTING_UP, ///< Set before the codec has called ff_thread_finish_setup().
76 STATE_GET_BUFFER, /**<
77 * Set when the codec calls get_buffer().
78 * State is returned to STATE_SETTING_UP afterwards.
79 */
80 STATE_SETUP_FINISHED ///< Set after the codec has called ff_thread_finish_setup().
81 } state;
82
83 /**
84 * Array of frames passed to ff_thread_release_buffer().
85 * Frames are released after all threads referencing them are finished.
86 */
87 AVFrame *released_buffers;
88 int num_released_buffers;
89 int released_buffers_allocated;
90
91 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
92 int requested_flags; ///< flags passed to get_buffer() for requested_frame
93 } PerThreadContext;
94
95 /**
96 * Context stored in the client AVCodecInternal thread_ctx.
97 */
98 typedef struct FrameThreadContext {
99 PerThreadContext *threads; ///< The contexts for each thread.
100 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
101
102 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
103
104 int next_decoding; ///< The next context to submit a packet to.
105 int next_finished; ///< The next context to return output from.
106
107 int delaying; /**<
108 * Set for the first N packets, where N is the number of threads.
109 * While it is set, ff_thread_en/decode_frame won't return any results.
110 */
111
112 int die; ///< Set when threads should exit.
113 } FrameThreadContext;
114
115 /**
116 * Codec worker thread.
117 *
118 * Automatically calls ff_thread_finish_setup() if the codec does
119 * not provide an update_thread_context method, or if the codec returns
120 * before calling it.
121 */
122 static attribute_align_arg void *frame_worker_thread(void *arg)
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) {
131 pthread_mutex_lock(&p->mutex);
132 while (p->state == STATE_INPUT_READY && !fctx->die)
133 pthread_cond_wait(&p->input_cond, &p->mutex);
134 pthread_mutex_unlock(&p->mutex);
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
142 pthread_mutex_lock(&p->mutex);
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
158 pthread_mutex_lock(&p->progress_mutex);
159 pthread_cond_signal(&p->output_cond);
160 pthread_mutex_unlock(&p->progress_mutex);
161
162 pthread_mutex_unlock(&p->mutex);
163 }
164
165 return NULL;
166 }
167
168 /**
169 * Update the next thread's AVCodecContext with values from the reference thread's context.
170 *
171 * @param dst The destination context.
172 * @param src The source context.
173 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
174 */
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->framerate = src->framerate;
182 dst->width = src->width;
183 dst->height = src->height;
184 dst->pix_fmt = src->pix_fmt;
185
186 dst->coded_width = src->coded_width;
187 dst->coded_height = src->coded_height;
188
189 dst->has_b_frames = src->has_b_frames;
190 dst->idct_algo = src->idct_algo;
191
192 dst->bits_per_coded_sample = src->bits_per_coded_sample;
193 dst->sample_aspect_ratio = src->sample_aspect_ratio;
194 #if FF_API_AFD
195 FF_DISABLE_DEPRECATION_WARNINGS
196 dst->dtg_active_format = src->dtg_active_format;
197 FF_ENABLE_DEPRECATION_WARNINGS
198 #endif /* FF_API_AFD */
199
200 dst->profile = src->profile;
201 dst->level = src->level;
202
203 dst->bits_per_raw_sample = src->bits_per_raw_sample;
204 dst->ticks_per_frame = src->ticks_per_frame;
205 dst->color_primaries = src->color_primaries;
206
207 dst->color_trc = src->color_trc;
208 dst->colorspace = src->colorspace;
209 dst->color_range = src->color_range;
210 dst->chroma_sample_location = src->chroma_sample_location;
211
212 dst->hwaccel = src->hwaccel;
213 dst->hwaccel_context = src->hwaccel_context;
214 dst->internal->hwaccel_priv_data = src->internal->hwaccel_priv_data;
215 }
216
217 if (for_user) {
218 #if FF_API_CODED_FRAME
219 FF_DISABLE_DEPRECATION_WARNINGS
220 dst->coded_frame = src->coded_frame;
221 FF_ENABLE_DEPRECATION_WARNINGS
222 #endif
223 } else {
224 if (dst->codec->update_thread_context)
225 err = dst->codec->update_thread_context(dst, src);
226 }
227
228 return err;
229 }
230
231 /**
232 * Update the next thread's AVCodecContext with values set by the user.
233 *
234 * @param dst The destination context.
235 * @param src The source context.
236 * @return 0 on success, negative error code on failure
237 */
238 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
239 {
240 #define copy_fields(s, e) memcpy(&dst->s, &src->s, (char*)&dst->e - (char*)&dst->s);
241 dst->flags = src->flags;
242
243 dst->draw_horiz_band= src->draw_horiz_band;
244 dst->get_buffer2 = src->get_buffer2;
245
246 dst->opaque = src->opaque;
247 dst->debug = src->debug;
248
249 dst->slice_flags = src->slice_flags;
250 dst->flags2 = src->flags2;
251
252 copy_fields(skip_loop_filter, subtitle_header);
253
254 dst->frame_number = src->frame_number;
255 dst->reordered_opaque = src->reordered_opaque;
256
257 if (src->slice_count && src->slice_offset) {
258 if (dst->slice_count < src->slice_count) {
259 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
260 sizeof(*dst->slice_offset));
261 if (!tmp) {
262 av_free(dst->slice_offset);
263 return AVERROR(ENOMEM);
264 }
265 dst->slice_offset = tmp;
266 }
267 memcpy(dst->slice_offset, src->slice_offset,
268 src->slice_count * sizeof(*dst->slice_offset));
269 }
270 dst->slice_count = src->slice_count;
271 return 0;
272 #undef copy_fields
273 }
274
275 /// Releases the buffers that this decoding thread was the last user of.
276 static void release_delayed_buffers(PerThreadContext *p)
277 {
278 FrameThreadContext *fctx = p->parent;
279
280 while (p->num_released_buffers > 0) {
281 AVFrame *f;
282
283 pthread_mutex_lock(&fctx->buffer_mutex);
284
285 // fix extended data in case the caller screwed it up
286 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO);
287 f = &p->released_buffers[--p->num_released_buffers];
288 f->extended_data = f->data;
289 av_frame_unref(f);
290
291 pthread_mutex_unlock(&fctx->buffer_mutex);
292 }
293 }
294
295 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
296 {
297 FrameThreadContext *fctx = p->parent;
298 PerThreadContext *prev_thread = fctx->prev_thread;
299 const AVCodec *codec = p->avctx->codec;
300
301 if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
302 return 0;
303
304 pthread_mutex_lock(&p->mutex);
305
306 release_delayed_buffers(p);
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) {
319 pthread_mutex_unlock(&p->mutex);
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;
328 pthread_cond_signal(&p->input_cond);
329 pthread_mutex_unlock(&p->mutex);
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
337 if (!p->avctx->thread_safe_callbacks &&
338 p->avctx->get_buffer2 != avcodec_default_get_buffer2) {
339 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
340 pthread_mutex_lock(&p->progress_mutex);
341 while (p->state == STATE_SETTING_UP)
342 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
343
344 if (p->state == STATE_GET_BUFFER) {
345 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
346 p->state = STATE_SETTING_UP;
347 pthread_cond_signal(&p->progress_cond);
348 }
349 pthread_mutex_unlock(&p->progress_mutex);
350 }
351 }
352
353 fctx->prev_thread = p;
354 fctx->next_decoding++;
355
356 return 0;
357 }
358
359 int ff_thread_decode_frame(AVCodecContext *avctx,
360 AVFrame *picture, int *got_picture_ptr,
361 AVPacket *avpkt)
362 {
363 FrameThreadContext *fctx = avctx->internal->thread_ctx;
364 int finished = fctx->next_finished;
365 PerThreadContext *p;
366 int err;
367
368 /*
369 * Submit a packet to the next decoding thread.
370 */
371
372 p = &fctx->threads[fctx->next_decoding];
373 err = update_context_from_user(p->avctx, avctx);
374 if (err) return err;
375 err = submit_packet(p, avpkt);
376 if (err) return err;
377
378 /*
379 * If we're still receiving the initial packets, don't return a frame.
380 */
381
382 if (fctx->delaying) {
383 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
384
385 *got_picture_ptr=0;
386 if (avpkt->size)
387 return avpkt->size;
388 }
389
390 /*
391 * Return the next available frame from the oldest thread.
392 * If we're at the end of the stream, then we have to skip threads that
393 * didn't output a frame, because we don't want to accidentally signal
394 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
395 */
396
397 do {
398 p = &fctx->threads[finished++];
399
400 if (p->state != STATE_INPUT_READY) {
401 pthread_mutex_lock(&p->progress_mutex);
402 while (p->state != STATE_INPUT_READY)
403 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
404 pthread_mutex_unlock(&p->progress_mutex);
405 }
406
407 av_frame_move_ref(picture, p->frame);
408 *got_picture_ptr = p->got_frame;
409 picture->pkt_dts = p->avpkt.dts;
410
411 /*
412 * A later call with avkpt->size == 0 may loop over all threads,
413 * including this one, searching for a frame to return before being
414 * stopped by the "finished != fctx->next_finished" condition.
415 * Make sure we don't mistakenly return the same frame again.
416 */
417 p->got_frame = 0;
418
419 if (finished >= avctx->thread_count) finished = 0;
420 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
421
422 update_context_from_thread(avctx, p->avctx, 1);
423
424 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
425
426 fctx->next_finished = finished;
427
428 /* return the size of the consumed packet if no error occurred */
429 return (p->result >= 0) ? avpkt->size : p->result;
430 }
431
432 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
433 {
434 PerThreadContext *p;
435 int *progress = f->progress ? (int*)f->progress->data : NULL;
436
437 if (!progress || progress[field] >= n) return;
438
439 p = f->owner->internal->thread_ctx;
440
441 if (f->owner->debug&FF_DEBUG_THREADS)
442 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
443
444 pthread_mutex_lock(&p->progress_mutex);
445 progress[field] = n;
446 pthread_cond_broadcast(&p->progress_cond);
447 pthread_mutex_unlock(&p->progress_mutex);
448 }
449
450 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
451 {
452 PerThreadContext *p;
453 int *progress = f->progress ? (int*)f->progress->data : NULL;
454
455 if (!progress || progress[field] >= n) return;
456
457 p = f->owner->internal->thread_ctx;
458
459 if (f->owner->debug&FF_DEBUG_THREADS)
460 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
461
462 pthread_mutex_lock(&p->progress_mutex);
463 while (progress[field] < n)
464 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
465 pthread_mutex_unlock(&p->progress_mutex);
466 }
467
468 void ff_thread_finish_setup(AVCodecContext *avctx) {
469 PerThreadContext *p = avctx->internal->thread_ctx;
470
471 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
472
473 pthread_mutex_lock(&p->progress_mutex);
474 p->state = STATE_SETUP_FINISHED;
475 pthread_cond_broadcast(&p->progress_cond);
476 pthread_mutex_unlock(&p->progress_mutex);
477 }
478
479 /// Waits for all threads to finish.
480 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
481 {
482 int i;
483
484 for (i = 0; i < thread_count; i++) {
485 PerThreadContext *p = &fctx->threads[i];
486
487 if (p->state != STATE_INPUT_READY) {
488 pthread_mutex_lock(&p->progress_mutex);
489 while (p->state != STATE_INPUT_READY)
490 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
491 pthread_mutex_unlock(&p->progress_mutex);
492 }
493 }
494 }
495
496 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
497 {
498 FrameThreadContext *fctx = avctx->internal->thread_ctx;
499 const AVCodec *codec = avctx->codec;
500 int i;
501
502 park_frame_worker_threads(fctx, thread_count);
503
504 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
505 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
506
507 fctx->die = 1;
508
509 for (i = 0; i < thread_count; i++) {
510 PerThreadContext *p = &fctx->threads[i];
511
512 pthread_mutex_lock(&p->mutex);
513 pthread_cond_signal(&p->input_cond);
514 pthread_mutex_unlock(&p->mutex);
515
516 if (p->thread_init)
517 pthread_join(p->thread, NULL);
518
519 if (codec->close)
520 codec->close(p->avctx);
521
522 avctx->codec = NULL;
523
524 release_delayed_buffers(p);
525 av_frame_free(&p->frame);
526 }
527
528 for (i = 0; i < thread_count; i++) {
529 PerThreadContext *p = &fctx->threads[i];
530
531 pthread_mutex_destroy(&p->mutex);
532 pthread_mutex_destroy(&p->progress_mutex);
533 pthread_cond_destroy(&p->input_cond);
534 pthread_cond_destroy(&p->progress_cond);
535 pthread_cond_destroy(&p->output_cond);
536 av_packet_unref(&p->avpkt);
537 av_freep(&p->released_buffers);
538
539 if (i) {
540 av_freep(&p->avctx->priv_data);
541 av_freep(&p->avctx->slice_offset);
542 }
543
544 av_freep(&p->avctx->internal);
545 av_freep(&p->avctx);
546 }
547
548 av_freep(&fctx->threads);
549 pthread_mutex_destroy(&fctx->buffer_mutex);
550 av_freep(&avctx->internal->thread_ctx);
551 }
552
553 int ff_frame_thread_init(AVCodecContext *avctx)
554 {
555 int thread_count = avctx->thread_count;
556 const AVCodec *codec = avctx->codec;
557 AVCodecContext *src = avctx;
558 FrameThreadContext *fctx;
559 int i, err = 0;
560
561 #if HAVE_W32THREADS
562 w32thread_init();
563 #endif
564
565 if (!thread_count) {
566 int nb_cpus = av_cpu_count();
567 av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
568 // use number of cores + 1 as thread count if there is more than one
569 if (nb_cpus > 1)
570 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
571 else
572 thread_count = avctx->thread_count = 1;
573 }
574
575 if (thread_count <= 1) {
576 avctx->active_thread_type = 0;
577 return 0;
578 }
579
580 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
581 if (!fctx)
582 return AVERROR(ENOMEM);
583
584 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
585 if (!fctx->threads) {
586 av_freep(&avctx->internal->thread_ctx);
587 return AVERROR(ENOMEM);
588 }
589
590 pthread_mutex_init(&fctx->buffer_mutex, NULL);
591 fctx->delaying = 1;
592
593 for (i = 0; i < thread_count; i++) {
594 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
595 PerThreadContext *p = &fctx->threads[i];
596
597 pthread_mutex_init(&p->mutex, NULL);
598 pthread_mutex_init(&p->progress_mutex, NULL);
599 pthread_cond_init(&p->input_cond, NULL);
600 pthread_cond_init(&p->progress_cond, NULL);
601 pthread_cond_init(&p->output_cond, NULL);
602
603 p->frame = av_frame_alloc();
604 if (!p->frame) {
605 av_freep(&copy);
606 err = AVERROR(ENOMEM);
607 goto error;
608 }
609
610 p->parent = fctx;
611 p->avctx = copy;
612
613 if (!copy) {
614 err = AVERROR(ENOMEM);
615 goto error;
616 }
617
618 *copy = *src;
619
620 copy->internal = av_malloc(sizeof(AVCodecInternal));
621 if (!copy->internal) {
622 err = AVERROR(ENOMEM);
623 goto error;
624 }
625 *copy->internal = *src->internal;
626 copy->internal->thread_ctx = p;
627 copy->internal->pkt = &p->avpkt;
628
629 if (!i) {
630 src = copy;
631
632 if (codec->init)
633 err = codec->init(copy);
634
635 update_context_from_thread(avctx, copy, 1);
636 } else {
637 copy->priv_data = av_malloc(codec->priv_data_size);
638 if (!copy->priv_data) {
639 err = AVERROR(ENOMEM);
640 goto error;
641 }
642 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
643 copy->internal->is_copy = 1;
644
645 if (codec->init_thread_copy)
646 err = codec->init_thread_copy(copy);
647 }
648
649 if (err) goto error;
650
651 if (!pthread_create(&p->thread, NULL, frame_worker_thread, p))
652 p->thread_init = 1;
653 }
654
655 return 0;
656
657 error:
658 ff_frame_thread_free(avctx, i+1);
659
660 return err;
661 }
662
663 void ff_thread_flush(AVCodecContext *avctx)
664 {
665 int i;
666 FrameThreadContext *fctx = avctx->internal->thread_ctx;
667
668 if (!fctx) return;
669
670 park_frame_worker_threads(fctx, avctx->thread_count);
671 if (fctx->prev_thread) {
672 if (fctx->prev_thread != &fctx->threads[0])
673 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
674 }
675
676 fctx->next_decoding = fctx->next_finished = 0;
677 fctx->delaying = 1;
678 fctx->prev_thread = NULL;
679 for (i = 0; i < avctx->thread_count; i++) {
680 PerThreadContext *p = &fctx->threads[i];
681 // Make sure decode flush calls with size=0 won't return old frames
682 p->got_frame = 0;
683 av_frame_unref(p->frame);
684
685 release_delayed_buffers(p);
686
687 if (avctx->codec->flush)
688 avctx->codec->flush(p->avctx);
689 }
690 }
691
692 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
693 {
694 PerThreadContext *p = avctx->internal->thread_ctx;
695 int err;
696
697 f->owner = avctx;
698
699 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
700 return ff_get_buffer(avctx, f->f, flags);
701
702 if (p->state != STATE_SETTING_UP &&
703 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
704 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
705 return -1;
706 }
707
708 if (avctx->internal->allocate_progress) {
709 int *progress;
710 f->progress = av_buffer_alloc(2 * sizeof(int));
711 if (!f->progress) {
712 return AVERROR(ENOMEM);
713 }
714 progress = (int*)f->progress->data;
715
716 progress[0] = progress[1] = -1;
717 }
718
719 pthread_mutex_lock(&p->parent->buffer_mutex);
720 if (avctx->thread_safe_callbacks ||
721 avctx->get_buffer2 == avcodec_default_get_buffer2) {
722 err = ff_get_buffer(avctx, f->f, flags);
723 } else {
724 p->requested_frame = f->f;
725 p->requested_flags = flags;
726 p->state = STATE_GET_BUFFER;
727 pthread_mutex_lock(&p->progress_mutex);
728 pthread_cond_signal(&p->progress_cond);
729
730 while (p->state != STATE_SETTING_UP)
731 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
732
733 err = p->result;
734
735 pthread_mutex_unlock(&p->progress_mutex);
736
737 }
738 if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
739 ff_thread_finish_setup(avctx);
740
741 if (err)
742 av_buffer_unref(&f->progress);
743
744 pthread_mutex_unlock(&p->parent->buffer_mutex);
745
746 return err;
747 }
748
749 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
750 {
751 PerThreadContext *p = avctx->internal->thread_ctx;
752 FrameThreadContext *fctx;
753 AVFrame *dst, *tmp;
754 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
755 avctx->thread_safe_callbacks ||
756 avctx->get_buffer2 == avcodec_default_get_buffer2;
757
758 if (!f->f || !f->f->buf[0])
759 return;
760
761 if (avctx->debug & FF_DEBUG_BUFFERS)
762 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
763
764 av_buffer_unref(&f->progress);
765 f->owner = NULL;
766
767 if (can_direct_free) {
768 av_frame_unref(f->f);
769 return;
770 }
771
772 fctx = p->parent;
773 pthread_mutex_lock(&fctx->buffer_mutex);
774
775 if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
776 goto fail;
777 tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
778 (p->num_released_buffers + 1) *
779 sizeof(*p->released_buffers));
780 if (!tmp)
781 goto fail;
782 p->released_buffers = tmp;
783
784 dst = &p->released_buffers[p->num_released_buffers];
785 av_frame_move_ref(dst, f->f);
786
787 p->num_released_buffers++;
788
789 fail:
790 pthread_mutex_unlock(&fctx->buffer_mutex);
791 }