64b8aa552f6c041003df4f943c92bc7f73f441e6
[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 #if FF_API_GET_BUFFER
246 FF_DISABLE_DEPRECATION_WARNINGS
247 dst->get_buffer = src->get_buffer;
248 dst->release_buffer = src->release_buffer;
249 FF_ENABLE_DEPRECATION_WARNINGS
250 #endif
251
252 dst->opaque = src->opaque;
253 dst->debug = src->debug;
254
255 dst->slice_flags = src->slice_flags;
256 dst->flags2 = src->flags2;
257
258 copy_fields(skip_loop_filter, subtitle_header);
259
260 dst->frame_number = src->frame_number;
261 dst->reordered_opaque = src->reordered_opaque;
262
263 if (src->slice_count && src->slice_offset) {
264 if (dst->slice_count < src->slice_count) {
265 int *tmp = av_realloc(dst->slice_offset, src->slice_count *
266 sizeof(*dst->slice_offset));
267 if (!tmp) {
268 av_free(dst->slice_offset);
269 return AVERROR(ENOMEM);
270 }
271 dst->slice_offset = tmp;
272 }
273 memcpy(dst->slice_offset, src->slice_offset,
274 src->slice_count * sizeof(*dst->slice_offset));
275 }
276 dst->slice_count = src->slice_count;
277 return 0;
278 #undef copy_fields
279 }
280
281 /// Releases the buffers that this decoding thread was the last user of.
282 static void release_delayed_buffers(PerThreadContext *p)
283 {
284 FrameThreadContext *fctx = p->parent;
285
286 while (p->num_released_buffers > 0) {
287 AVFrame *f;
288
289 pthread_mutex_lock(&fctx->buffer_mutex);
290
291 // fix extended data in case the caller screwed it up
292 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO);
293 f = &p->released_buffers[--p->num_released_buffers];
294 f->extended_data = f->data;
295 av_frame_unref(f);
296
297 pthread_mutex_unlock(&fctx->buffer_mutex);
298 }
299 }
300
301 static int submit_packet(PerThreadContext *p, AVPacket *avpkt)
302 {
303 FrameThreadContext *fctx = p->parent;
304 PerThreadContext *prev_thread = fctx->prev_thread;
305 const AVCodec *codec = p->avctx->codec;
306
307 if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
308 return 0;
309
310 pthread_mutex_lock(&p->mutex);
311
312 release_delayed_buffers(p);
313
314 if (prev_thread) {
315 int err;
316 if (prev_thread->state == STATE_SETTING_UP) {
317 pthread_mutex_lock(&prev_thread->progress_mutex);
318 while (prev_thread->state == STATE_SETTING_UP)
319 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
320 pthread_mutex_unlock(&prev_thread->progress_mutex);
321 }
322
323 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
324 if (err) {
325 pthread_mutex_unlock(&p->mutex);
326 return err;
327 }
328 }
329
330 av_packet_unref(&p->avpkt);
331 av_packet_ref(&p->avpkt, avpkt);
332
333 p->state = STATE_SETTING_UP;
334 pthread_cond_signal(&p->input_cond);
335 pthread_mutex_unlock(&p->mutex);
336
337 /*
338 * If the client doesn't have a thread-safe get_buffer(),
339 * then decoding threads call back to the main thread,
340 * and it calls back to the client here.
341 */
342
343 FF_DISABLE_DEPRECATION_WARNINGS
344 if (!p->avctx->thread_safe_callbacks && (
345 #if FF_API_GET_BUFFER
346 p->avctx->get_buffer ||
347 #endif
348 p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
349 FF_ENABLE_DEPRECATION_WARNINGS
350 while (p->state != STATE_SETUP_FINISHED && p->state != STATE_INPUT_READY) {
351 pthread_mutex_lock(&p->progress_mutex);
352 while (p->state == STATE_SETTING_UP)
353 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
354
355 if (p->state == STATE_GET_BUFFER) {
356 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
357 p->state = STATE_SETTING_UP;
358 pthread_cond_signal(&p->progress_cond);
359 }
360 pthread_mutex_unlock(&p->progress_mutex);
361 }
362 }
363
364 fctx->prev_thread = p;
365 fctx->next_decoding++;
366
367 return 0;
368 }
369
370 int ff_thread_decode_frame(AVCodecContext *avctx,
371 AVFrame *picture, int *got_picture_ptr,
372 AVPacket *avpkt)
373 {
374 FrameThreadContext *fctx = avctx->internal->thread_ctx;
375 int finished = fctx->next_finished;
376 PerThreadContext *p;
377 int err;
378
379 /*
380 * Submit a packet to the next decoding thread.
381 */
382
383 p = &fctx->threads[fctx->next_decoding];
384 err = update_context_from_user(p->avctx, avctx);
385 if (err) return err;
386 err = submit_packet(p, avpkt);
387 if (err) return err;
388
389 /*
390 * If we're still receiving the initial packets, don't return a frame.
391 */
392
393 if (fctx->delaying) {
394 if (fctx->next_decoding >= (avctx->thread_count-1)) fctx->delaying = 0;
395
396 *got_picture_ptr=0;
397 if (avpkt->size)
398 return avpkt->size;
399 }
400
401 /*
402 * Return the next available frame from the oldest thread.
403 * If we're at the end of the stream, then we have to skip threads that
404 * didn't output a frame, because we don't want to accidentally signal
405 * EOF (avpkt->size == 0 && *got_picture_ptr == 0).
406 */
407
408 do {
409 p = &fctx->threads[finished++];
410
411 if (p->state != STATE_INPUT_READY) {
412 pthread_mutex_lock(&p->progress_mutex);
413 while (p->state != STATE_INPUT_READY)
414 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
415 pthread_mutex_unlock(&p->progress_mutex);
416 }
417
418 av_frame_move_ref(picture, p->frame);
419 *got_picture_ptr = p->got_frame;
420 picture->pkt_dts = p->avpkt.dts;
421
422 /*
423 * A later call with avkpt->size == 0 may loop over all threads,
424 * including this one, searching for a frame to return before being
425 * stopped by the "finished != fctx->next_finished" condition.
426 * Make sure we don't mistakenly return the same frame again.
427 */
428 p->got_frame = 0;
429
430 if (finished >= avctx->thread_count) finished = 0;
431 } while (!avpkt->size && !*got_picture_ptr && finished != fctx->next_finished);
432
433 update_context_from_thread(avctx, p->avctx, 1);
434
435 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
436
437 fctx->next_finished = finished;
438
439 /* return the size of the consumed packet if no error occurred */
440 return (p->result >= 0) ? avpkt->size : p->result;
441 }
442
443 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
444 {
445 PerThreadContext *p;
446 int *progress = f->progress ? (int*)f->progress->data : NULL;
447
448 if (!progress || progress[field] >= n) return;
449
450 p = f->owner->internal->thread_ctx;
451
452 if (f->owner->debug&FF_DEBUG_THREADS)
453 av_log(f->owner, AV_LOG_DEBUG, "%p finished %d field %d\n", progress, n, field);
454
455 pthread_mutex_lock(&p->progress_mutex);
456 progress[field] = n;
457 pthread_cond_broadcast(&p->progress_cond);
458 pthread_mutex_unlock(&p->progress_mutex);
459 }
460
461 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
462 {
463 PerThreadContext *p;
464 int *progress = f->progress ? (int*)f->progress->data : NULL;
465
466 if (!progress || progress[field] >= n) return;
467
468 p = f->owner->internal->thread_ctx;
469
470 if (f->owner->debug&FF_DEBUG_THREADS)
471 av_log(f->owner, AV_LOG_DEBUG, "thread awaiting %d field %d from %p\n", n, field, progress);
472
473 pthread_mutex_lock(&p->progress_mutex);
474 while (progress[field] < n)
475 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
476 pthread_mutex_unlock(&p->progress_mutex);
477 }
478
479 void ff_thread_finish_setup(AVCodecContext *avctx) {
480 PerThreadContext *p = avctx->internal->thread_ctx;
481
482 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
483
484 pthread_mutex_lock(&p->progress_mutex);
485 p->state = STATE_SETUP_FINISHED;
486 pthread_cond_broadcast(&p->progress_cond);
487 pthread_mutex_unlock(&p->progress_mutex);
488 }
489
490 /// Waits for all threads to finish.
491 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
492 {
493 int i;
494
495 for (i = 0; i < thread_count; i++) {
496 PerThreadContext *p = &fctx->threads[i];
497
498 if (p->state != STATE_INPUT_READY) {
499 pthread_mutex_lock(&p->progress_mutex);
500 while (p->state != STATE_INPUT_READY)
501 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
502 pthread_mutex_unlock(&p->progress_mutex);
503 }
504 }
505 }
506
507 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
508 {
509 FrameThreadContext *fctx = avctx->internal->thread_ctx;
510 const AVCodec *codec = avctx->codec;
511 int i;
512
513 park_frame_worker_threads(fctx, thread_count);
514
515 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
516 update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0);
517
518 fctx->die = 1;
519
520 for (i = 0; i < thread_count; i++) {
521 PerThreadContext *p = &fctx->threads[i];
522
523 pthread_mutex_lock(&p->mutex);
524 pthread_cond_signal(&p->input_cond);
525 pthread_mutex_unlock(&p->mutex);
526
527 if (p->thread_init)
528 pthread_join(p->thread, NULL);
529
530 if (codec->close)
531 codec->close(p->avctx);
532
533 avctx->codec = NULL;
534
535 release_delayed_buffers(p);
536 av_frame_free(&p->frame);
537 }
538
539 for (i = 0; i < thread_count; i++) {
540 PerThreadContext *p = &fctx->threads[i];
541
542 pthread_mutex_destroy(&p->mutex);
543 pthread_mutex_destroy(&p->progress_mutex);
544 pthread_cond_destroy(&p->input_cond);
545 pthread_cond_destroy(&p->progress_cond);
546 pthread_cond_destroy(&p->output_cond);
547 av_packet_unref(&p->avpkt);
548 av_freep(&p->released_buffers);
549
550 if (i) {
551 av_freep(&p->avctx->priv_data);
552 av_freep(&p->avctx->slice_offset);
553 }
554
555 av_freep(&p->avctx->internal);
556 av_freep(&p->avctx);
557 }
558
559 av_freep(&fctx->threads);
560 pthread_mutex_destroy(&fctx->buffer_mutex);
561 av_freep(&avctx->internal->thread_ctx);
562 }
563
564 int ff_frame_thread_init(AVCodecContext *avctx)
565 {
566 int thread_count = avctx->thread_count;
567 const AVCodec *codec = avctx->codec;
568 AVCodecContext *src = avctx;
569 FrameThreadContext *fctx;
570 int i, err = 0;
571
572 #if HAVE_W32THREADS
573 w32thread_init();
574 #endif
575
576 if (!thread_count) {
577 int nb_cpus = av_cpu_count();
578 av_log(avctx, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
579 // use number of cores + 1 as thread count if there is more than one
580 if (nb_cpus > 1)
581 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
582 else
583 thread_count = avctx->thread_count = 1;
584 }
585
586 if (thread_count <= 1) {
587 avctx->active_thread_type = 0;
588 return 0;
589 }
590
591 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
592 if (!fctx)
593 return AVERROR(ENOMEM);
594
595 fctx->threads = av_mallocz(sizeof(PerThreadContext) * thread_count);
596 if (!fctx->threads) {
597 av_freep(&avctx->internal->thread_ctx);
598 return AVERROR(ENOMEM);
599 }
600
601 pthread_mutex_init(&fctx->buffer_mutex, NULL);
602 fctx->delaying = 1;
603
604 for (i = 0; i < thread_count; i++) {
605 AVCodecContext *copy = av_malloc(sizeof(AVCodecContext));
606 PerThreadContext *p = &fctx->threads[i];
607
608 pthread_mutex_init(&p->mutex, NULL);
609 pthread_mutex_init(&p->progress_mutex, NULL);
610 pthread_cond_init(&p->input_cond, NULL);
611 pthread_cond_init(&p->progress_cond, NULL);
612 pthread_cond_init(&p->output_cond, NULL);
613
614 p->frame = av_frame_alloc();
615 if (!p->frame) {
616 av_freep(&copy);
617 err = AVERROR(ENOMEM);
618 goto error;
619 }
620
621 p->parent = fctx;
622 p->avctx = copy;
623
624 if (!copy) {
625 err = AVERROR(ENOMEM);
626 goto error;
627 }
628
629 *copy = *src;
630
631 copy->internal = av_malloc(sizeof(AVCodecInternal));
632 if (!copy->internal) {
633 err = AVERROR(ENOMEM);
634 goto error;
635 }
636 *copy->internal = *src->internal;
637 copy->internal->thread_ctx = p;
638 copy->internal->pkt = &p->avpkt;
639
640 if (!i) {
641 src = copy;
642
643 if (codec->init)
644 err = codec->init(copy);
645
646 update_context_from_thread(avctx, copy, 1);
647 } else {
648 copy->priv_data = av_malloc(codec->priv_data_size);
649 if (!copy->priv_data) {
650 err = AVERROR(ENOMEM);
651 goto error;
652 }
653 memcpy(copy->priv_data, src->priv_data, codec->priv_data_size);
654 copy->internal->is_copy = 1;
655
656 if (codec->init_thread_copy)
657 err = codec->init_thread_copy(copy);
658 }
659
660 if (err) goto error;
661
662 if (!pthread_create(&p->thread, NULL, frame_worker_thread, p))
663 p->thread_init = 1;
664 }
665
666 return 0;
667
668 error:
669 ff_frame_thread_free(avctx, i+1);
670
671 return err;
672 }
673
674 void ff_thread_flush(AVCodecContext *avctx)
675 {
676 int i;
677 FrameThreadContext *fctx = avctx->internal->thread_ctx;
678
679 if (!fctx) return;
680
681 park_frame_worker_threads(fctx, avctx->thread_count);
682 if (fctx->prev_thread) {
683 if (fctx->prev_thread != &fctx->threads[0])
684 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
685 }
686
687 fctx->next_decoding = fctx->next_finished = 0;
688 fctx->delaying = 1;
689 fctx->prev_thread = NULL;
690 for (i = 0; i < avctx->thread_count; i++) {
691 PerThreadContext *p = &fctx->threads[i];
692 // Make sure decode flush calls with size=0 won't return old frames
693 p->got_frame = 0;
694 av_frame_unref(p->frame);
695
696 release_delayed_buffers(p);
697
698 if (avctx->codec->flush)
699 avctx->codec->flush(p->avctx);
700 }
701 }
702
703 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
704 {
705 PerThreadContext *p = avctx->internal->thread_ctx;
706 int err;
707
708 f->owner = avctx;
709
710 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
711 return ff_get_buffer(avctx, f->f, flags);
712
713 if (p->state != STATE_SETTING_UP &&
714 (avctx->codec->update_thread_context || !avctx->thread_safe_callbacks)) {
715 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
716 return -1;
717 }
718
719 if (avctx->internal->allocate_progress) {
720 int *progress;
721 f->progress = av_buffer_alloc(2 * sizeof(int));
722 if (!f->progress) {
723 return AVERROR(ENOMEM);
724 }
725 progress = (int*)f->progress->data;
726
727 progress[0] = progress[1] = -1;
728 }
729
730 pthread_mutex_lock(&p->parent->buffer_mutex);
731 FF_DISABLE_DEPRECATION_WARNINGS
732 if (avctx->thread_safe_callbacks || (
733 #if FF_API_GET_BUFFER
734 !avctx->get_buffer &&
735 #endif
736 avctx->get_buffer2 == avcodec_default_get_buffer2)) {
737 FF_ENABLE_DEPRECATION_WARNINGS
738 err = ff_get_buffer(avctx, f->f, flags);
739 } else {
740 p->requested_frame = f->f;
741 p->requested_flags = flags;
742 p->state = STATE_GET_BUFFER;
743 pthread_mutex_lock(&p->progress_mutex);
744 pthread_cond_signal(&p->progress_cond);
745
746 while (p->state != STATE_SETTING_UP)
747 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
748
749 err = p->result;
750
751 pthread_mutex_unlock(&p->progress_mutex);
752
753 }
754 if (!avctx->thread_safe_callbacks && !avctx->codec->update_thread_context)
755 ff_thread_finish_setup(avctx);
756
757 if (err)
758 av_buffer_unref(&f->progress);
759
760 pthread_mutex_unlock(&p->parent->buffer_mutex);
761
762 return err;
763 }
764
765 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
766 {
767 PerThreadContext *p = avctx->internal->thread_ctx;
768 FrameThreadContext *fctx;
769 AVFrame *dst, *tmp;
770 FF_DISABLE_DEPRECATION_WARNINGS
771 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
772 avctx->thread_safe_callbacks ||
773 (
774 #if FF_API_GET_BUFFER
775 !avctx->get_buffer &&
776 #endif
777 avctx->get_buffer2 == avcodec_default_get_buffer2);
778 FF_ENABLE_DEPRECATION_WARNINGS
779
780 if (!f->f || !f->f->buf[0])
781 return;
782
783 if (avctx->debug & FF_DEBUG_BUFFERS)
784 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
785
786 av_buffer_unref(&f->progress);
787 f->owner = NULL;
788
789 if (can_direct_free) {
790 av_frame_unref(f->f);
791 return;
792 }
793
794 fctx = p->parent;
795 pthread_mutex_lock(&fctx->buffer_mutex);
796
797 if (p->num_released_buffers + 1 >= INT_MAX / sizeof(*p->released_buffers))
798 goto fail;
799 tmp = av_fast_realloc(p->released_buffers, &p->released_buffers_allocated,
800 (p->num_released_buffers + 1) *
801 sizeof(*p->released_buffers));
802 if (!tmp)
803 goto fail;
804 p->released_buffers = tmp;
805
806 dst = &p->released_buffers[p->num_released_buffers];
807 av_frame_move_ref(dst, f->f);
808
809 p->num_released_buffers++;
810
811 fail:
812 pthread_mutex_unlock(&fctx->buffer_mutex);
813 }