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 <stdatomic.h>
28 #include <stdint.h>
29
30 #include "avcodec.h"
31 #include "hwconfig.h"
32 #include "internal.h"
33 #include "pthread_internal.h"
34 #include "thread.h"
35 #include "version.h"
36
37 #include "libavutil/avassert.h"
38 #include "libavutil/buffer.h"
39 #include "libavutil/common.h"
40 #include "libavutil/cpu.h"
41 #include "libavutil/frame.h"
42 #include "libavutil/internal.h"
43 #include "libavutil/log.h"
44 #include "libavutil/mem.h"
45 #include "libavutil/opt.h"
46 #include "libavutil/thread.h"
47
48 enum {
49 ///< Set when the thread is awaiting a packet.
50 STATE_INPUT_READY,
51 ///< Set before the codec has called ff_thread_finish_setup().
52 STATE_SETTING_UP,
53 /**
54 * Set when the codec calls get_buffer().
55 * State is returned to STATE_SETTING_UP afterwards.
56 */
57 STATE_GET_BUFFER,
58 /**
59 * Set when the codec calls get_format().
60 * State is returned to STATE_SETTING_UP afterwards.
61 */
62 STATE_GET_FORMAT,
63 ///< Set after the codec has called ff_thread_finish_setup().
64 STATE_SETUP_FINISHED,
65 };
66
67 enum {
68 UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
69 NEEDS_CLOSE, ///< AVCodec->close needs to be called
70 INITIALIZED, ///< Thread has been properly set up
71 };
72
73 /**
74 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
75 */
76 typedef struct PerThreadContext {
77 struct FrameThreadContext *parent;
78
79 pthread_t thread;
80 int thread_init;
81 unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
82 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
83 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
84 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
85
86 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
87 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
88
89 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
90
91 AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
92
93 AVFrame *frame; ///< Output frame (for decoding) or input (for encoding).
94 int got_frame; ///< The output of got_picture_ptr from the last avcodec_decode_video() call.
95 int result; ///< The result of the last codec decode/encode() call.
96
97 atomic_int state;
98
99 #if FF_API_THREAD_SAFE_CALLBACKS
100 /**
101 * Array of frames passed to ff_thread_release_buffer().
102 * Frames are released after all threads referencing them are finished.
103 */
104 AVFrame **released_buffers;
105 int num_released_buffers;
106 int released_buffers_allocated;
107
108 AVFrame *requested_frame; ///< AVFrame the codec passed to get_buffer()
109 int requested_flags; ///< flags passed to get_buffer() for requested_frame
110
111 const enum AVPixelFormat *available_formats; ///< Format array for get_format()
112 enum AVPixelFormat result_format; ///< get_format() result
113 #endif
114
115 int die; ///< Set when the thread should exit.
116
117 int hwaccel_serializing;
118 int async_serializing;
119
120 atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
121 } PerThreadContext;
122
123 /**
124 * Context stored in the client AVCodecInternal thread_ctx.
125 */
126 typedef struct FrameThreadContext {
127 PerThreadContext *threads; ///< The contexts for each thread.
128 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
129
130 unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
131 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
132 /**
133 * This lock is used for ensuring threads run in serial when hwaccel
134 * is used.
135 */
136 pthread_mutex_t hwaccel_mutex;
137 pthread_mutex_t async_mutex;
138 pthread_cond_t async_cond;
139 int async_lock;
140
141 int next_decoding; ///< The next context to submit a packet to.
142 int next_finished; ///< The next context to return output from.
143
144 int delaying; /**<
145 * Set for the first N packets, where N is the number of threads.
146 * While it is set, ff_thread_en/decode_frame won't return any results.
147 */
148
149 /* hwaccel state is temporarily stored here in order to transfer its ownership
150 * to the next decoding thread without the need for extra synchronization */
151 const AVHWAccel *stash_hwaccel;
152 void *stash_hwaccel_context;
153 void *stash_hwaccel_priv;
154 } FrameThreadContext;
155
156 #if FF_API_THREAD_SAFE_CALLBACKS
157 #define THREAD_SAFE_CALLBACKS(avctx) \
158 ((avctx)->thread_safe_callbacks || (avctx)->get_buffer2 == avcodec_default_get_buffer2)
159 #endif
160
async_lock(FrameThreadContext * fctx)161 static void async_lock(FrameThreadContext *fctx)
162 {
163 pthread_mutex_lock(&fctx->async_mutex);
164 while (fctx->async_lock)
165 pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
166 fctx->async_lock = 1;
167 pthread_mutex_unlock(&fctx->async_mutex);
168 }
169
async_unlock(FrameThreadContext * fctx)170 static void async_unlock(FrameThreadContext *fctx)
171 {
172 pthread_mutex_lock(&fctx->async_mutex);
173 av_assert0(fctx->async_lock);
174 fctx->async_lock = 0;
175 pthread_cond_broadcast(&fctx->async_cond);
176 pthread_mutex_unlock(&fctx->async_mutex);
177 }
178
179 /**
180 * Codec worker thread.
181 *
182 * Automatically calls ff_thread_finish_setup() if the codec does
183 * not provide an update_thread_context method, or if the codec returns
184 * before calling it.
185 */
frame_worker_thread(void * arg)186 static attribute_align_arg void *frame_worker_thread(void *arg)
187 {
188 PerThreadContext *p = arg;
189 AVCodecContext *avctx = p->avctx;
190 const AVCodec *codec = avctx->codec;
191
192 pthread_mutex_lock(&p->mutex);
193 while (1) {
194 while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
195 pthread_cond_wait(&p->input_cond, &p->mutex);
196
197 if (p->die) break;
198
199 FF_DISABLE_DEPRECATION_WARNINGS
200 if (!codec->update_thread_context
201 #if FF_API_THREAD_SAFE_CALLBACKS
202 && THREAD_SAFE_CALLBACKS(avctx)
203 #endif
204 )
205 ff_thread_finish_setup(avctx);
206 FF_ENABLE_DEPRECATION_WARNINGS
207
208 /* If a decoder supports hwaccel, then it must call ff_get_format().
209 * Since that call must happen before ff_thread_finish_setup(), the
210 * decoder is required to implement update_thread_context() and call
211 * ff_thread_finish_setup() manually. Therefore the above
212 * ff_thread_finish_setup() call did not happen and hwaccel_serializing
213 * cannot be true here. */
214 av_assert0(!p->hwaccel_serializing);
215
216 /* if the previous thread uses hwaccel then we take the lock to ensure
217 * the threads don't run concurrently */
218 if (avctx->hwaccel) {
219 pthread_mutex_lock(&p->parent->hwaccel_mutex);
220 p->hwaccel_serializing = 1;
221 }
222
223 av_frame_unref(p->frame);
224 p->got_frame = 0;
225 p->result = codec->decode(avctx, p->frame, &p->got_frame, p->avpkt);
226
227 if ((p->result < 0 || !p->got_frame) && p->frame->buf[0]) {
228 if (avctx->codec->caps_internal & FF_CODEC_CAP_ALLOCATE_PROGRESS)
229 av_log(avctx, AV_LOG_ERROR, "A frame threaded decoder did not "
230 "free the frame on failure. This is a bug, please report it.\n");
231 av_frame_unref(p->frame);
232 }
233
234 if (atomic_load(&p->state) == STATE_SETTING_UP)
235 ff_thread_finish_setup(avctx);
236
237 if (p->hwaccel_serializing) {
238 /* wipe hwaccel state to avoid stale pointers lying around;
239 * the state was transferred to FrameThreadContext in
240 * ff_thread_finish_setup(), so nothing is leaked */
241 avctx->hwaccel = NULL;
242 avctx->hwaccel_context = NULL;
243 avctx->internal->hwaccel_priv_data = NULL;
244
245 p->hwaccel_serializing = 0;
246 pthread_mutex_unlock(&p->parent->hwaccel_mutex);
247 }
248 av_assert0(!avctx->hwaccel);
249
250 if (p->async_serializing) {
251 p->async_serializing = 0;
252
253 async_unlock(p->parent);
254 }
255
256 pthread_mutex_lock(&p->progress_mutex);
257
258 atomic_store(&p->state, STATE_INPUT_READY);
259
260 pthread_cond_broadcast(&p->progress_cond);
261 pthread_cond_signal(&p->output_cond);
262 pthread_mutex_unlock(&p->progress_mutex);
263 }
264 pthread_mutex_unlock(&p->mutex);
265
266 return NULL;
267 }
268
269 /**
270 * Update the next thread's AVCodecContext with values from the reference thread's context.
271 *
272 * @param dst The destination context.
273 * @param src The source context.
274 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
275 * @return 0 on success, negative error code on failure
276 */
update_context_from_thread(AVCodecContext * dst,AVCodecContext * src,int for_user)277 static int update_context_from_thread(AVCodecContext *dst, AVCodecContext *src, int for_user)
278 {
279 int err = 0;
280
281 if (dst != src && (for_user || src->codec->update_thread_context)) {
282 dst->time_base = src->time_base;
283 dst->framerate = src->framerate;
284 dst->width = src->width;
285 dst->height = src->height;
286 dst->pix_fmt = src->pix_fmt;
287 dst->sw_pix_fmt = src->sw_pix_fmt;
288
289 dst->coded_width = src->coded_width;
290 dst->coded_height = src->coded_height;
291
292 dst->has_b_frames = src->has_b_frames;
293 dst->idct_algo = src->idct_algo;
294
295 dst->bits_per_coded_sample = src->bits_per_coded_sample;
296 dst->sample_aspect_ratio = src->sample_aspect_ratio;
297
298 dst->profile = src->profile;
299 dst->level = src->level;
300
301 dst->bits_per_raw_sample = src->bits_per_raw_sample;
302 dst->ticks_per_frame = src->ticks_per_frame;
303 dst->color_primaries = src->color_primaries;
304
305 dst->color_trc = src->color_trc;
306 dst->colorspace = src->colorspace;
307 dst->color_range = src->color_range;
308 dst->chroma_sample_location = src->chroma_sample_location;
309
310 dst->channels = src->channels;
311 dst->sample_rate = src->sample_rate;
312 dst->sample_fmt = src->sample_fmt;
313 dst->channel_layout = src->channel_layout;
314
315 if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
316 (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
317 av_buffer_unref(&dst->hw_frames_ctx);
318
319 if (src->hw_frames_ctx) {
320 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
321 if (!dst->hw_frames_ctx)
322 return AVERROR(ENOMEM);
323 }
324 }
325
326 dst->hwaccel_flags = src->hwaccel_flags;
327
328 err = av_buffer_replace(&dst->internal->pool, src->internal->pool);
329 if (err < 0)
330 return err;
331 }
332
333 if (for_user) {
334 #if FF_API_CODED_FRAME
335 FF_DISABLE_DEPRECATION_WARNINGS
336 dst->coded_frame = src->coded_frame;
337 FF_ENABLE_DEPRECATION_WARNINGS
338 #endif
339 } else {
340 if (dst->codec->update_thread_context)
341 err = dst->codec->update_thread_context(dst, src);
342 }
343
344 return err;
345 }
346
347 /**
348 * Update the next thread's AVCodecContext with values set by the user.
349 *
350 * @param dst The destination context.
351 * @param src The source context.
352 * @return 0 on success, negative error code on failure
353 */
update_context_from_user(AVCodecContext * dst,AVCodecContext * src)354 static int update_context_from_user(AVCodecContext *dst, AVCodecContext *src)
355 {
356 dst->flags = src->flags;
357
358 dst->draw_horiz_band= src->draw_horiz_band;
359 dst->get_buffer2 = src->get_buffer2;
360
361 dst->opaque = src->opaque;
362 dst->debug = src->debug;
363
364 dst->slice_flags = src->slice_flags;
365 dst->flags2 = src->flags2;
366 dst->export_side_data = src->export_side_data;
367
368 dst->skip_loop_filter = src->skip_loop_filter;
369 dst->skip_idct = src->skip_idct;
370 dst->skip_frame = src->skip_frame;
371
372 dst->frame_number = src->frame_number;
373 dst->reordered_opaque = src->reordered_opaque;
374 #if FF_API_THREAD_SAFE_CALLBACKS
375 FF_DISABLE_DEPRECATION_WARNINGS
376 dst->thread_safe_callbacks = src->thread_safe_callbacks;
377 FF_ENABLE_DEPRECATION_WARNINGS
378 #endif
379
380 if (src->slice_count && src->slice_offset) {
381 if (dst->slice_count < src->slice_count) {
382 int err = av_reallocp_array(&dst->slice_offset, src->slice_count,
383 sizeof(*dst->slice_offset));
384 if (err < 0)
385 return err;
386 }
387 memcpy(dst->slice_offset, src->slice_offset,
388 src->slice_count * sizeof(*dst->slice_offset));
389 }
390 dst->slice_count = src->slice_count;
391 return 0;
392 }
393
394 #if FF_API_THREAD_SAFE_CALLBACKS
395 /// Releases the buffers that this decoding thread was the last user of.
release_delayed_buffers(PerThreadContext * p)396 static void release_delayed_buffers(PerThreadContext *p)
397 {
398 FrameThreadContext *fctx = p->parent;
399
400 while (p->num_released_buffers > 0) {
401 AVFrame *f;
402
403 pthread_mutex_lock(&fctx->buffer_mutex);
404
405 // fix extended data in case the caller screwed it up
406 av_assert0(p->avctx->codec_type == AVMEDIA_TYPE_VIDEO ||
407 p->avctx->codec_type == AVMEDIA_TYPE_AUDIO);
408 f = p->released_buffers[--p->num_released_buffers];
409 f->extended_data = f->data;
410 av_frame_unref(f);
411
412 pthread_mutex_unlock(&fctx->buffer_mutex);
413 }
414 }
415 #endif
416
submit_packet(PerThreadContext * p,AVCodecContext * user_avctx,AVPacket * avpkt)417 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
418 AVPacket *avpkt)
419 {
420 FrameThreadContext *fctx = p->parent;
421 PerThreadContext *prev_thread = fctx->prev_thread;
422 const AVCodec *codec = p->avctx->codec;
423 int ret;
424
425 if (!avpkt->size && !(codec->capabilities & AV_CODEC_CAP_DELAY))
426 return 0;
427
428 pthread_mutex_lock(&p->mutex);
429
430 ret = update_context_from_user(p->avctx, user_avctx);
431 if (ret) {
432 pthread_mutex_unlock(&p->mutex);
433 return ret;
434 }
435 atomic_store_explicit(&p->debug_threads,
436 (p->avctx->debug & FF_DEBUG_THREADS) != 0,
437 memory_order_relaxed);
438
439 #if FF_API_THREAD_SAFE_CALLBACKS
440 release_delayed_buffers(p);
441 #endif
442
443 if (prev_thread) {
444 int err;
445 if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
446 pthread_mutex_lock(&prev_thread->progress_mutex);
447 while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
448 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
449 pthread_mutex_unlock(&prev_thread->progress_mutex);
450 }
451
452 err = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
453 if (err) {
454 pthread_mutex_unlock(&p->mutex);
455 return err;
456 }
457
458 /* transfer hwaccel state stashed from previous thread, if any */
459 av_assert0(!p->avctx->hwaccel);
460 FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
461 FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
462 FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
463 }
464
465 av_packet_unref(p->avpkt);
466 ret = av_packet_ref(p->avpkt, avpkt);
467 if (ret < 0) {
468 pthread_mutex_unlock(&p->mutex);
469 av_log(p->avctx, AV_LOG_ERROR, "av_packet_ref() failed in submit_packet()\n");
470 return ret;
471 }
472
473 atomic_store(&p->state, STATE_SETTING_UP);
474 pthread_cond_signal(&p->input_cond);
475 pthread_mutex_unlock(&p->mutex);
476
477 #if FF_API_THREAD_SAFE_CALLBACKS
478 FF_DISABLE_DEPRECATION_WARNINGS
479 /*
480 * If the client doesn't have a thread-safe get_buffer(),
481 * then decoding threads call back to the main thread,
482 * and it calls back to the client here.
483 */
484
485 if (!p->avctx->thread_safe_callbacks && (
486 p->avctx->get_format != avcodec_default_get_format ||
487 p->avctx->get_buffer2 != avcodec_default_get_buffer2)) {
488 while (atomic_load(&p->state) != STATE_SETUP_FINISHED && atomic_load(&p->state) != STATE_INPUT_READY) {
489 int call_done = 1;
490 pthread_mutex_lock(&p->progress_mutex);
491 while (atomic_load(&p->state) == STATE_SETTING_UP)
492 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
493
494 switch (atomic_load_explicit(&p->state, memory_order_acquire)) {
495 case STATE_GET_BUFFER:
496 p->result = ff_get_buffer(p->avctx, p->requested_frame, p->requested_flags);
497 break;
498 case STATE_GET_FORMAT:
499 p->result_format = ff_get_format(p->avctx, p->available_formats);
500 break;
501 default:
502 call_done = 0;
503 break;
504 }
505 if (call_done) {
506 atomic_store(&p->state, STATE_SETTING_UP);
507 pthread_cond_signal(&p->progress_cond);
508 }
509 pthread_mutex_unlock(&p->progress_mutex);
510 }
511 }
512 FF_ENABLE_DEPRECATION_WARNINGS
513 #endif
514
515 fctx->prev_thread = p;
516 fctx->next_decoding++;
517
518 return 0;
519 }
520
ff_thread_decode_frame(AVCodecContext * avctx,AVFrame * picture,int * got_picture_ptr,AVPacket * avpkt)521 int ff_thread_decode_frame(AVCodecContext *avctx,
522 AVFrame *picture, int *got_picture_ptr,
523 AVPacket *avpkt)
524 {
525 FrameThreadContext *fctx = avctx->internal->thread_ctx;
526 int finished = fctx->next_finished;
527 PerThreadContext *p;
528 int err;
529
530 /* release the async lock, permitting blocked hwaccel threads to
531 * go forward while we are in this function */
532 async_unlock(fctx);
533
534 /*
535 * Submit a packet to the next decoding thread.
536 */
537
538 p = &fctx->threads[fctx->next_decoding];
539 err = submit_packet(p, avctx, avpkt);
540 if (err)
541 goto finish;
542
543 /*
544 * If we're still receiving the initial packets, don't return a frame.
545 */
546
547 if (fctx->next_decoding > (avctx->thread_count-1-(avctx->codec_id == AV_CODEC_ID_FFV1)))
548 fctx->delaying = 0;
549
550 if (fctx->delaying) {
551 *got_picture_ptr=0;
552 if (avpkt->size) {
553 err = avpkt->size;
554 goto finish;
555 }
556 }
557
558 /*
559 * Return the next available frame from the oldest thread.
560 * If we're at the end of the stream, then we have to skip threads that
561 * didn't output a frame/error, because we don't want to accidentally signal
562 * EOF (avpkt->size == 0 && *got_picture_ptr == 0 && err >= 0).
563 */
564
565 do {
566 p = &fctx->threads[finished++];
567
568 if (atomic_load(&p->state) != STATE_INPUT_READY) {
569 pthread_mutex_lock(&p->progress_mutex);
570 while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
571 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
572 pthread_mutex_unlock(&p->progress_mutex);
573 }
574
575 av_frame_move_ref(picture, p->frame);
576 *got_picture_ptr = p->got_frame;
577 picture->pkt_dts = p->avpkt->dts;
578 err = p->result;
579
580 /*
581 * A later call with avkpt->size == 0 may loop over all threads,
582 * including this one, searching for a frame/error to return before being
583 * stopped by the "finished != fctx->next_finished" condition.
584 * Make sure we don't mistakenly return the same frame/error again.
585 */
586 p->got_frame = 0;
587 p->result = 0;
588
589 if (finished >= avctx->thread_count) finished = 0;
590 } while (!avpkt->size && !*got_picture_ptr && err >= 0 && finished != fctx->next_finished);
591
592 update_context_from_thread(avctx, p->avctx, 1);
593
594 if (fctx->next_decoding >= avctx->thread_count) fctx->next_decoding = 0;
595
596 fctx->next_finished = finished;
597
598 /* return the size of the consumed packet if no error occurred */
599 if (err >= 0)
600 err = avpkt->size;
601 finish:
602 async_lock(fctx);
603 return err;
604 }
605
ff_thread_report_progress(ThreadFrame * f,int n,int field)606 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
607 {
608 PerThreadContext *p;
609 atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
610
611 if (!progress ||
612 atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
613 return;
614
615 p = f->owner[field]->internal->thread_ctx;
616
617 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
618 av_log(f->owner[field], AV_LOG_DEBUG,
619 "%p finished %d field %d\n", progress, n, field);
620
621 pthread_mutex_lock(&p->progress_mutex);
622
623 atomic_store_explicit(&progress[field], n, memory_order_release);
624
625 pthread_cond_broadcast(&p->progress_cond);
626 pthread_mutex_unlock(&p->progress_mutex);
627 }
628
ff_thread_await_progress(ThreadFrame * f,int n,int field)629 void ff_thread_await_progress(ThreadFrame *f, int n, int field)
630 {
631 PerThreadContext *p;
632 atomic_int *progress = f->progress ? (atomic_int*)f->progress->data : NULL;
633
634 if (!progress ||
635 atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
636 return;
637
638 p = f->owner[field]->internal->thread_ctx;
639
640 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
641 av_log(f->owner[field], AV_LOG_DEBUG,
642 "thread awaiting %d field %d from %p\n", n, field, progress);
643
644 pthread_mutex_lock(&p->progress_mutex);
645 while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
646 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
647 pthread_mutex_unlock(&p->progress_mutex);
648 }
649
ff_thread_finish_setup(AVCodecContext * avctx)650 void ff_thread_finish_setup(AVCodecContext *avctx) {
651 PerThreadContext *p = avctx->internal->thread_ctx;
652
653 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
654
655 if (avctx->hwaccel && !p->hwaccel_serializing) {
656 pthread_mutex_lock(&p->parent->hwaccel_mutex);
657 p->hwaccel_serializing = 1;
658 }
659
660 /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
661 if (avctx->hwaccel &&
662 !(avctx->hwaccel->caps_internal & HWACCEL_CAP_ASYNC_SAFE)) {
663 p->async_serializing = 1;
664
665 async_lock(p->parent);
666 }
667
668 /* save hwaccel state for passing to the next thread;
669 * this is done here so that this worker thread can wipe its own hwaccel
670 * state after decoding, without requiring synchronization */
671 av_assert0(!p->parent->stash_hwaccel);
672 p->parent->stash_hwaccel = avctx->hwaccel;
673 p->parent->stash_hwaccel_context = avctx->hwaccel_context;
674 p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
675
676 pthread_mutex_lock(&p->progress_mutex);
677 if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
678 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
679 }
680
681 atomic_store(&p->state, STATE_SETUP_FINISHED);
682
683 pthread_cond_broadcast(&p->progress_cond);
684 pthread_mutex_unlock(&p->progress_mutex);
685 }
686
687 /// Waits for all threads to finish.
park_frame_worker_threads(FrameThreadContext * fctx,int thread_count)688 static void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
689 {
690 int i;
691
692 async_unlock(fctx);
693
694 for (i = 0; i < thread_count; i++) {
695 PerThreadContext *p = &fctx->threads[i];
696
697 if (atomic_load(&p->state) != STATE_INPUT_READY) {
698 pthread_mutex_lock(&p->progress_mutex);
699 while (atomic_load(&p->state) != STATE_INPUT_READY)
700 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
701 pthread_mutex_unlock(&p->progress_mutex);
702 }
703 p->got_frame = 0;
704 }
705
706 async_lock(fctx);
707 }
708
709 #define SENTINEL 0 // This forbids putting a mutex/condition variable at the front.
710 #define OFFSET_ARRAY(...) __VA_ARGS__, SENTINEL
711 #define DEFINE_OFFSET_ARRAY(type, name, mutexes, conds) \
712 static const unsigned name ## _offsets[] = { offsetof(type, pthread_init_cnt),\
713 OFFSET_ARRAY mutexes, \
714 OFFSET_ARRAY conds }
715
716 #define OFF(member) offsetof(FrameThreadContext, member)
717 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx,
718 (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
719 (OFF(async_cond)));
720 #undef OFF
721
722 #define OFF(member) offsetof(PerThreadContext, member)
723 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread,
724 (OFF(progress_mutex), OFF(mutex)),
725 (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
726 #undef OFF
727
free_pthread(void * obj,const unsigned offsets[])728 static av_cold void free_pthread(void *obj, const unsigned offsets[])
729 {
730 unsigned cnt = *(unsigned*)((char*)obj + offsets[0]);
731 const unsigned *cur_offset = offsets;
732
733 for (; *(++cur_offset) != SENTINEL && cnt; cnt--)
734 pthread_mutex_destroy((pthread_mutex_t*)((char*)obj + *cur_offset));
735 for (; *(++cur_offset) != SENTINEL && cnt; cnt--)
736 pthread_cond_destroy ((pthread_cond_t *)((char*)obj + *cur_offset));
737 }
738
init_pthread(void * obj,const unsigned offsets[])739 static av_cold int init_pthread(void *obj, const unsigned offsets[])
740 {
741 const unsigned *cur_offset = offsets;
742 unsigned cnt = 0;
743 int err;
744
745 #define PTHREAD_INIT_LOOP(type) \
746 for (; *(++cur_offset) != SENTINEL; cnt++) { \
747 pthread_ ## type ## _t *dst = (void*)((char*)obj + *cur_offset); \
748 err = pthread_ ## type ## _init(dst, NULL); \
749 if (err) { \
750 err = AVERROR(err); \
751 goto fail; \
752 } \
753 }
754 PTHREAD_INIT_LOOP(mutex)
755 PTHREAD_INIT_LOOP(cond)
756
757 fail:
758 *(unsigned*)((char*)obj + offsets[0]) = cnt;
759 return err;
760 }
761
ff_frame_thread_free(AVCodecContext * avctx,int thread_count)762 void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
763 {
764 FrameThreadContext *fctx = avctx->internal->thread_ctx;
765 const AVCodec *codec = avctx->codec;
766 int i;
767
768 park_frame_worker_threads(fctx, thread_count);
769
770 if (fctx->prev_thread && fctx->prev_thread != fctx->threads)
771 if (update_context_from_thread(fctx->threads->avctx, fctx->prev_thread->avctx, 0) < 0) {
772 av_log(avctx, AV_LOG_ERROR, "Final thread update failed\n");
773 fctx->prev_thread->avctx->internal->is_copy = fctx->threads->avctx->internal->is_copy;
774 fctx->threads->avctx->internal->is_copy = 1;
775 }
776
777 for (i = 0; i < thread_count; i++) {
778 PerThreadContext *p = &fctx->threads[i];
779 AVCodecContext *ctx = p->avctx;
780
781 if (ctx->internal) {
782 if (p->thread_init == INITIALIZED) {
783 pthread_mutex_lock(&p->mutex);
784 p->die = 1;
785 pthread_cond_signal(&p->input_cond);
786 pthread_mutex_unlock(&p->mutex);
787
788 pthread_join(p->thread, NULL);
789 }
790 if (codec->close && p->thread_init != UNINITIALIZED)
791 codec->close(ctx);
792
793 #if FF_API_THREAD_SAFE_CALLBACKS
794 release_delayed_buffers(p);
795 for (int j = 0; j < p->released_buffers_allocated; j++)
796 av_frame_free(&p->released_buffers[j]);
797 av_freep(&p->released_buffers);
798 #endif
799 if (ctx->priv_data) {
800 if (codec->priv_class)
801 av_opt_free(ctx->priv_data);
802 av_freep(&ctx->priv_data);
803 }
804
805 av_freep(&ctx->slice_offset);
806
807 av_buffer_unref(&ctx->internal->pool);
808 av_freep(&ctx->internal);
809 av_buffer_unref(&ctx->hw_frames_ctx);
810 }
811
812 av_frame_free(&p->frame);
813
814 free_pthread(p, per_thread_offsets);
815 av_packet_free(&p->avpkt);
816
817 av_freep(&p->avctx);
818 }
819
820 av_freep(&fctx->threads);
821 free_pthread(fctx, thread_ctx_offsets);
822
823 /* if we have stashed hwaccel state, move it to the user-facing context,
824 * so it will be freed in avcodec_close() */
825 av_assert0(!avctx->hwaccel);
826 FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
827 FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
828 FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
829
830 av_freep(&avctx->internal->thread_ctx);
831
832 if (avctx->priv_data && avctx->codec && avctx->codec->priv_class)
833 av_opt_free(avctx->priv_data);
834 avctx->codec = NULL;
835 }
836
init_thread(PerThreadContext * p,int * threads_to_free,FrameThreadContext * fctx,AVCodecContext * avctx,AVCodecContext * src,const AVCodec * codec,int first)837 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
838 FrameThreadContext *fctx, AVCodecContext *avctx,
839 AVCodecContext *src, const AVCodec *codec, int first)
840 {
841 AVCodecContext *copy;
842 int err;
843
844 atomic_init(&p->state, STATE_INPUT_READY);
845
846 copy = av_memdup(src, sizeof(*src));
847 if (!copy)
848 return AVERROR(ENOMEM);
849 copy->priv_data = NULL;
850
851 /* From now on, this PerThreadContext will be cleaned up by
852 * ff_frame_thread_free in case of errors. */
853 (*threads_to_free)++;
854
855 p->parent = fctx;
856 p->avctx = copy;
857
858 copy->internal = av_memdup(src->internal, sizeof(*src->internal));
859 if (!copy->internal)
860 return AVERROR(ENOMEM);
861 copy->internal->thread_ctx = p;
862
863 copy->delay = avctx->delay;
864
865 if (codec->priv_data_size) {
866 copy->priv_data = av_mallocz(codec->priv_data_size);
867 if (!copy->priv_data)
868 return AVERROR(ENOMEM);
869
870 if (codec->priv_class) {
871 *(const AVClass **)copy->priv_data = codec->priv_class;
872 err = av_opt_copy(copy->priv_data, src->priv_data);
873 if (err < 0)
874 return err;
875 }
876 }
877
878 err = init_pthread(p, per_thread_offsets);
879 if (err < 0)
880 return err;
881
882 if (!(p->frame = av_frame_alloc()) ||
883 !(p->avpkt = av_packet_alloc()))
884 return AVERROR(ENOMEM);
885 copy->internal->last_pkt_props = p->avpkt;
886
887 if (!first)
888 copy->internal->is_copy = 1;
889
890 if (codec->init) {
891 err = codec->init(copy);
892 if (err < 0) {
893 if (codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)
894 p->thread_init = NEEDS_CLOSE;
895 return err;
896 }
897 }
898 p->thread_init = NEEDS_CLOSE;
899
900 if (first)
901 update_context_from_thread(avctx, copy, 1);
902
903 atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
904
905 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
906 if (err < 0)
907 return err;
908 p->thread_init = INITIALIZED;
909
910 return 0;
911 }
912
ff_frame_thread_init(AVCodecContext * avctx)913 int ff_frame_thread_init(AVCodecContext *avctx)
914 {
915 int thread_count = avctx->thread_count;
916 const AVCodec *codec = avctx->codec;
917 AVCodecContext *src = avctx;
918 FrameThreadContext *fctx;
919 int err, i = 0;
920
921 if (!thread_count) {
922 int nb_cpus = av_cpu_count();
923 // use number of cores + 1 as thread count if there is more than one
924 if (nb_cpus > 1)
925 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
926 else
927 thread_count = avctx->thread_count = 1;
928 }
929
930 if (thread_count <= 1) {
931 avctx->active_thread_type = 0;
932 return 0;
933 }
934
935 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
936 if (!fctx)
937 return AVERROR(ENOMEM);
938
939 err = init_pthread(fctx, thread_ctx_offsets);
940 if (err < 0) {
941 free_pthread(fctx, thread_ctx_offsets);
942 av_freep(&avctx->internal->thread_ctx);
943 return err;
944 }
945
946 fctx->async_lock = 1;
947 fctx->delaying = 1;
948
949 if (codec->type == AVMEDIA_TYPE_VIDEO)
950 avctx->delay = src->thread_count - 1;
951
952 fctx->threads = av_mallocz_array(thread_count, sizeof(PerThreadContext));
953 if (!fctx->threads) {
954 err = AVERROR(ENOMEM);
955 goto error;
956 }
957
958 for (; i < thread_count; ) {
959 PerThreadContext *p = &fctx->threads[i];
960 int first = !i;
961
962 err = init_thread(p, &i, fctx, avctx, src, codec, first);
963 if (err < 0)
964 goto error;
965 }
966
967 return 0;
968
969 error:
970 ff_frame_thread_free(avctx, i);
971 return err;
972 }
973
ff_thread_flush(AVCodecContext * avctx)974 void ff_thread_flush(AVCodecContext *avctx)
975 {
976 int i;
977 FrameThreadContext *fctx = avctx->internal->thread_ctx;
978
979 if (!fctx) return;
980
981 park_frame_worker_threads(fctx, avctx->thread_count);
982 if (fctx->prev_thread) {
983 if (fctx->prev_thread != &fctx->threads[0])
984 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
985 }
986
987 fctx->next_decoding = fctx->next_finished = 0;
988 fctx->delaying = 1;
989 fctx->prev_thread = NULL;
990 for (i = 0; i < avctx->thread_count; i++) {
991 PerThreadContext *p = &fctx->threads[i];
992 // Make sure decode flush calls with size=0 won't return old frames
993 p->got_frame = 0;
994 av_frame_unref(p->frame);
995 p->result = 0;
996
997 #if FF_API_THREAD_SAFE_CALLBACKS
998 release_delayed_buffers(p);
999 #endif
1000
1001 if (avctx->codec->flush)
1002 avctx->codec->flush(p->avctx);
1003 }
1004 }
1005
ff_thread_can_start_frame(AVCodecContext * avctx)1006 int ff_thread_can_start_frame(AVCodecContext *avctx)
1007 {
1008 PerThreadContext *p = avctx->internal->thread_ctx;
1009 FF_DISABLE_DEPRECATION_WARNINGS
1010 if ((avctx->active_thread_type&FF_THREAD_FRAME) && atomic_load(&p->state) != STATE_SETTING_UP &&
1011 (avctx->codec->update_thread_context
1012 #if FF_API_THREAD_SAFE_CALLBACKS
1013 || !THREAD_SAFE_CALLBACKS(avctx)
1014 #endif
1015 )) {
1016 return 0;
1017 }
1018 FF_ENABLE_DEPRECATION_WARNINGS
1019 return 1;
1020 }
1021
thread_get_buffer_internal(AVCodecContext * avctx,ThreadFrame * f,int flags)1022 static int thread_get_buffer_internal(AVCodecContext *avctx, ThreadFrame *f, int flags)
1023 {
1024 PerThreadContext *p = avctx->internal->thread_ctx;
1025 int err;
1026
1027 f->owner[0] = f->owner[1] = avctx;
1028
1029 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1030 return ff_get_buffer(avctx, f->f, flags);
1031
1032 FF_DISABLE_DEPRECATION_WARNINGS
1033 if (atomic_load(&p->state) != STATE_SETTING_UP &&
1034 (avctx->codec->update_thread_context
1035 #if FF_API_THREAD_SAFE_CALLBACKS
1036 || !THREAD_SAFE_CALLBACKS(avctx)
1037 #endif
1038 )) {
1039 FF_ENABLE_DEPRECATION_WARNINGS
1040 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
1041 return -1;
1042 }
1043
1044 if (avctx->codec->caps_internal & FF_CODEC_CAP_ALLOCATE_PROGRESS) {
1045 atomic_int *progress;
1046 f->progress = av_buffer_alloc(2 * sizeof(*progress));
1047 if (!f->progress) {
1048 return AVERROR(ENOMEM);
1049 }
1050 progress = (atomic_int*)f->progress->data;
1051
1052 atomic_init(&progress[0], -1);
1053 atomic_init(&progress[1], -1);
1054 }
1055
1056 pthread_mutex_lock(&p->parent->buffer_mutex);
1057 #if !FF_API_THREAD_SAFE_CALLBACKS
1058 err = ff_get_buffer(avctx, f->f, flags);
1059 #else
1060 FF_DISABLE_DEPRECATION_WARNINGS
1061 if (THREAD_SAFE_CALLBACKS(avctx)) {
1062 err = ff_get_buffer(avctx, f->f, flags);
1063 } else {
1064 pthread_mutex_lock(&p->progress_mutex);
1065 p->requested_frame = f->f;
1066 p->requested_flags = flags;
1067 atomic_store_explicit(&p->state, STATE_GET_BUFFER, memory_order_release);
1068 pthread_cond_broadcast(&p->progress_cond);
1069
1070 while (atomic_load(&p->state) != STATE_SETTING_UP)
1071 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
1072
1073 err = p->result;
1074
1075 pthread_mutex_unlock(&p->progress_mutex);
1076
1077 }
1078 if (!THREAD_SAFE_CALLBACKS(avctx) && !avctx->codec->update_thread_context)
1079 ff_thread_finish_setup(avctx);
1080 FF_ENABLE_DEPRECATION_WARNINGS
1081 #endif
1082 if (err)
1083 av_buffer_unref(&f->progress);
1084
1085 pthread_mutex_unlock(&p->parent->buffer_mutex);
1086
1087 return err;
1088 }
1089
1090 #if FF_API_THREAD_SAFE_CALLBACKS
1091 FF_DISABLE_DEPRECATION_WARNINGS
ff_thread_get_format(AVCodecContext * avctx,const enum AVPixelFormat * fmt)1092 enum AVPixelFormat ff_thread_get_format(AVCodecContext *avctx, const enum AVPixelFormat *fmt)
1093 {
1094 enum AVPixelFormat res;
1095 PerThreadContext *p = avctx->internal->thread_ctx;
1096 if (!(avctx->active_thread_type & FF_THREAD_FRAME) || avctx->thread_safe_callbacks ||
1097 avctx->get_format == avcodec_default_get_format)
1098 return ff_get_format(avctx, fmt);
1099 if (atomic_load(&p->state) != STATE_SETTING_UP) {
1100 av_log(avctx, AV_LOG_ERROR, "get_format() cannot be called after ff_thread_finish_setup()\n");
1101 return -1;
1102 }
1103 pthread_mutex_lock(&p->progress_mutex);
1104 p->available_formats = fmt;
1105 atomic_store(&p->state, STATE_GET_FORMAT);
1106 pthread_cond_broadcast(&p->progress_cond);
1107
1108 while (atomic_load(&p->state) != STATE_SETTING_UP)
1109 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
1110
1111 res = p->result_format;
1112
1113 pthread_mutex_unlock(&p->progress_mutex);
1114
1115 return res;
1116 }
1117 FF_ENABLE_DEPRECATION_WARNINGS
1118 #endif
1119
ff_thread_get_buffer(AVCodecContext * avctx,ThreadFrame * f,int flags)1120 int ff_thread_get_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
1121 {
1122 int ret = thread_get_buffer_internal(avctx, f, flags);
1123 if (ret < 0)
1124 av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1125 return ret;
1126 }
1127
ff_thread_release_buffer(AVCodecContext * avctx,ThreadFrame * f)1128 void ff_thread_release_buffer(AVCodecContext *avctx, ThreadFrame *f)
1129 {
1130 #if FF_API_THREAD_SAFE_CALLBACKS
1131 FF_DISABLE_DEPRECATION_WARNINGS
1132 PerThreadContext *p = avctx->internal->thread_ctx;
1133 FrameThreadContext *fctx;
1134 AVFrame *dst;
1135 int ret = 0;
1136 int can_direct_free = !(avctx->active_thread_type & FF_THREAD_FRAME) ||
1137 THREAD_SAFE_CALLBACKS(avctx);
1138 FF_ENABLE_DEPRECATION_WARNINGS
1139 #endif
1140
1141 if (!f->f)
1142 return;
1143
1144 if (avctx->debug & FF_DEBUG_BUFFERS)
1145 av_log(avctx, AV_LOG_DEBUG, "thread_release_buffer called on pic %p\n", f);
1146
1147 av_buffer_unref(&f->progress);
1148 f->owner[0] = f->owner[1] = NULL;
1149
1150 #if !FF_API_THREAD_SAFE_CALLBACKS
1151 av_frame_unref(f->f);
1152 #else
1153 // when the frame buffers are not allocated, just reset it to clean state
1154 if (can_direct_free || !f->f->buf[0]) {
1155 av_frame_unref(f->f);
1156 return;
1157 }
1158
1159 fctx = p->parent;
1160 pthread_mutex_lock(&fctx->buffer_mutex);
1161
1162 if (p->num_released_buffers == p->released_buffers_allocated) {
1163 AVFrame **tmp = av_realloc_array(p->released_buffers, p->released_buffers_allocated + 1,
1164 sizeof(*p->released_buffers));
1165 if (tmp) {
1166 tmp[p->released_buffers_allocated] = av_frame_alloc();
1167 p->released_buffers = tmp;
1168 }
1169
1170 if (!tmp || !tmp[p->released_buffers_allocated]) {
1171 ret = AVERROR(ENOMEM);
1172 goto fail;
1173 }
1174 p->released_buffers_allocated++;
1175 }
1176
1177 dst = p->released_buffers[p->num_released_buffers];
1178 av_frame_move_ref(dst, f->f);
1179
1180 p->num_released_buffers++;
1181
1182 fail:
1183 pthread_mutex_unlock(&fctx->buffer_mutex);
1184
1185 // make sure the frame is clean even if we fail to free it
1186 // this leaks, but it is better than crashing
1187 if (ret < 0) {
1188 av_log(avctx, AV_LOG_ERROR, "Could not queue a frame for freeing, this will leak\n");
1189 memset(f->f->buf, 0, sizeof(f->f->buf));
1190 if (f->f->extended_buf)
1191 memset(f->f->extended_buf, 0, f->f->nb_extended_buf * sizeof(*f->f->extended_buf));
1192 av_frame_unref(f->f);
1193 }
1194 #endif
1195 }
1196