FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread_frame.c
Date: 2026-09-12 04:54:02
Exec Total Coverage
Lines: 425 545 78.0%
Functions: 26 28 92.9%
Branches: 194 320 60.6%

Line Branch Exec Source
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 <stdatomic.h>
26
27 #include "avcodec.h"
28 #include "avcodec_internal.h"
29 #include "codec_internal.h"
30 #include "decode.h"
31 #include "hwaccel_internal.h"
32 #include "hwconfig.h"
33 #include "internal.h"
34 #include "packet_internal.h"
35 #include "pthread_internal.h"
36 #include "libavutil/refstruct.h"
37 #include "thread.h"
38 #include "threadframe.h"
39
40 #include "libavutil/avassert.h"
41 #include "libavutil/buffer.h"
42 #include "libavutil/cpu.h"
43 #include "libavutil/frame.h"
44 #include "libavutil/internal.h"
45 #include "libavutil/log.h"
46 #include "libavutil/mem.h"
47 #include "libavutil/opt.h"
48 #include "libavutil/thread.h"
49
50 enum {
51 /// Set when the thread is awaiting a packet.
52 STATE_INPUT_READY,
53 /// Set before the codec has called ff_thread_finish_setup().
54 STATE_SETTING_UP,
55 /// Set after the codec has called ff_thread_finish_setup().
56 STATE_SETUP_FINISHED,
57 };
58
59 enum {
60 UNINITIALIZED, ///< Thread has not been created, AVCodec->close mustn't be called
61 NEEDS_CLOSE, ///< FFCodec->close needs to be called
62 INITIALIZED, ///< Thread has been properly set up
63 };
64
65 typedef struct DecodedFrames {
66 AVFrame **f;
67 size_t nb_f;
68 size_t nb_f_allocated;
69 } DecodedFrames;
70
71 typedef struct ThreadFrameProgress {
72 atomic_int progress[2];
73 } ThreadFrameProgress;
74
75 /**
76 * Context used by codec threads and stored in their AVCodecInternal thread_ctx.
77 */
78 typedef struct PerThreadContext {
79 struct FrameThreadContext *parent;
80
81 pthread_t thread;
82 int thread_init;
83 unsigned pthread_init_cnt;///< Number of successfully initialized mutexes/conditions
84 pthread_cond_t input_cond; ///< Used to wait for a new packet from the main thread.
85 pthread_cond_t progress_cond; ///< Used by child threads to wait for progress to change.
86 pthread_cond_t output_cond; ///< Used by the main thread to wait for frames to finish.
87
88 pthread_mutex_t mutex; ///< Mutex used to protect the contents of the PerThreadContext.
89 pthread_mutex_t progress_mutex; ///< Mutex used to protect frame progress values and progress_cond.
90
91 AVCodecContext *avctx; ///< Context used to decode packets passed to this thread.
92
93 AVPacket *avpkt; ///< Input packet (for decoding) or output (for encoding).
94
95 /**
96 * Decoded frames from a single decode iteration.
97 */
98 DecodedFrames df;
99 int result; ///< The result of the last codec decode/encode() call.
100
101 atomic_int state;
102
103 int die; ///< Set when the thread should exit.
104
105 int hwaccel_serializing;
106 int async_serializing;
107
108 // set to 1 in ff_thread_finish_setup() when a threadsafe hwaccel is used;
109 // cannot check hwaccel caps directly, because
110 // worked threads clear hwaccel state for thread-unsafe hwaccels
111 // after each decode call
112 int hwaccel_threadsafe;
113
114 atomic_int debug_threads; ///< Set if the FF_DEBUG_THREADS option is set.
115 } PerThreadContext;
116
117 /**
118 * Context stored in the client AVCodecInternal thread_ctx.
119 */
120 typedef struct FrameThreadContext {
121 PerThreadContext *threads; ///< The contexts for each thread.
122 PerThreadContext *prev_thread; ///< The last thread submit_packet() was called on.
123
124 unsigned pthread_init_cnt; ///< Number of successfully initialized mutexes/conditions
125 pthread_mutex_t buffer_mutex; ///< Mutex used to protect get/release_buffer().
126 /**
127 * This lock is used for ensuring threads run in serial when thread-unsafe
128 * hwaccel is used.
129 */
130 pthread_mutex_t hwaccel_mutex;
131 pthread_mutex_t async_mutex;
132 pthread_cond_t async_cond;
133 int async_lock;
134
135 DecodedFrames df;
136 int result;
137
138 /**
139 * Packet to be submitted to the next thread for decoding.
140 */
141 AVPacket *next_pkt;
142
143 int next_decoding; ///< The next context to submit a packet to.
144 int next_finished; ///< The next context to return output from.
145
146 /* hwaccel state for thread-unsafe hwaccels is temporarily stored here in
147 * order to transfer its ownership to the next decoding thread without the
148 * need for extra synchronization */
149 const AVHWAccel *stash_hwaccel;
150 void *stash_hwaccel_context;
151 void *stash_hwaccel_priv;
152 } FrameThreadContext;
153
154 1818 static int hwaccel_serial(const AVCodecContext *avctx)
155 {
156
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1818 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
1818 return avctx->hwaccel && !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
157 }
158
159 1221 static void async_lock(FrameThreadContext *fctx)
160 {
161 1221 pthread_mutex_lock(&fctx->async_mutex);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1221 times.
1221 while (fctx->async_lock)
163 pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
164 1221 fctx->async_lock = 1;
165 1221 pthread_mutex_unlock(&fctx->async_mutex);
166 1221 }
167
168 1221 static void async_unlock(FrameThreadContext *fctx)
169 {
170 1221 pthread_mutex_lock(&fctx->async_mutex);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1221 times.
1221 av_assert0(fctx->async_lock);
172 1221 fctx->async_lock = 0;
173 1221 pthread_cond_broadcast(&fctx->async_cond);
174 1221 pthread_mutex_unlock(&fctx->async_mutex);
175 1221 }
176
177 76 static void thread_set_name(PerThreadContext *p)
178 {
179 76 AVCodecContext *avctx = p->avctx;
180 76 int idx = p - p->parent->threads;
181 char name[16];
182
183 76 snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
184
185 76 ff_thread_setname(name);
186 76 }
187
188 // get a free frame to decode into
189 1144 static AVFrame *decoded_frames_get_free(DecodedFrames *df)
190 {
191
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 981 times.
1144 if (df->nb_f == df->nb_f_allocated) {
192 163 AVFrame **tmp = av_realloc_array(df->f, df->nb_f + 1,
193 sizeof(*df->f));
194
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (!tmp)
195 return NULL;
196 163 df->f = tmp;
197
198 163 df->f[df->nb_f] = av_frame_alloc();
199
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 163 times.
163 if (!df->f[df->nb_f])
200 return NULL;
201
202 163 df->nb_f_allocated++;
203 }
204
205
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1144 times.
1144 av_assert0(!df->f[df->nb_f]->buf[0]);
206
207 1144 return df->f[df->nb_f];
208 }
209
210 537 static void decoded_frames_pop(DecodedFrames *df, AVFrame *dst)
211 {
212 537 AVFrame *tmp_frame = df->f[0];
213 537 av_frame_move_ref(dst, tmp_frame);
214 537 memmove(df->f, df->f + 1, (df->nb_f - 1) * sizeof(*df->f));
215 537 df->f[--df->nb_f] = tmp_frame;
216 537 }
217
218 static void decoded_frames_flush(DecodedFrames *df)
219 {
220 for (size_t i = 0; i < df->nb_f; i++)
221 av_frame_unref(df->f[i]);
222 df->nb_f = 0;
223 }
224
225 86 static void decoded_frames_free(DecodedFrames *df)
226 {
227
2/2
✓ Branch 0 taken 163 times.
✓ Branch 1 taken 86 times.
249 for (size_t i = 0; i < df->nb_f_allocated; i++)
228 163 av_frame_free(&df->f[i]);
229 86 av_freep(&df->f);
230 86 df->nb_f = 0;
231 86 df->nb_f_allocated = 0;
232 86 }
233
234 /**
235 * Codec worker thread.
236 *
237 * Automatically calls ff_thread_finish_setup() if the codec does
238 * not provide an update_thread_context method, or if the codec returns
239 * before calling it.
240 */
241 76 static attribute_align_arg void *frame_worker_thread(void *arg)
242 {
243 76 PerThreadContext *p = arg;
244 76 AVCodecContext *avctx = p->avctx;
245 76 const FFCodec *codec = ffcodec(avctx->codec);
246
247 76 thread_set_name(p);
248
249 76 pthread_mutex_lock(&p->mutex);
250 606 while (1) {
251 int ret;
252
253
4/4
✓ Branch 0 taken 754 times.
✓ Branch 1 taken 606 times.
✓ Branch 2 taken 678 times.
✓ Branch 3 taken 76 times.
1360 while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
254 678 pthread_cond_wait(&p->input_cond, &p->mutex);
255
256
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 606 times.
682 if (p->die) break;
257
258
2/2
✓ Branch 0 taken 494 times.
✓ Branch 1 taken 112 times.
606 if (!codec->update_thread_context)
259 494 ff_thread_finish_setup(avctx);
260
261 /* If a decoder supports hwaccel, then it must call ff_get_format().
262 * Since that call must happen before ff_thread_finish_setup(), the
263 * decoder is required to implement update_thread_context() and call
264 * ff_thread_finish_setup() manually. Therefore the above
265 * ff_thread_finish_setup() call did not happen and hwaccel_serializing
266 * cannot be true here. */
267
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 av_assert0(!p->hwaccel_serializing);
268
269 /* if the previous thread uses thread-unsafe hwaccel then we take the
270 * lock to ensure the threads don't run concurrently */
271
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 606 times.
606 if (hwaccel_serial(avctx)) {
272 pthread_mutex_lock(&p->parent->hwaccel_mutex);
273 p->hwaccel_serializing = 1;
274 }
275
276 606 ret = 0;
277
2/2
✓ Branch 0 taken 1144 times.
✓ Branch 1 taken 606 times.
1750 while (ret >= 0) {
278 AVFrame *frame;
279
280 /* get the frame which will store the output */
281 1144 frame = decoded_frames_get_free(&p->df);
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1144 times.
1144 if (!frame) {
283 p->result = AVERROR(ENOMEM);
284 goto alloc_fail;
285 }
286
287 /* do the actual decoding */
288 1144 ret = ff_decode_receive_frame_internal(avctx, frame);
289
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 606 times.
1144 if (ret == 0)
290 538 p->df.nb_f++;
291
2/4
✓ Branch 0 taken 606 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 606 times.
606 else if (ret < 0 && frame->buf[0])
292 av_frame_unref(frame);
293
294
2/2
✓ Branch 0 taken 606 times.
✓ Branch 1 taken 538 times.
1144 p->result = (ret == AVERROR(EAGAIN)) ? 0 : ret;
295 }
296
297
2/2
✓ Branch 0 taken 578 times.
✓ Branch 1 taken 28 times.
606 if (atomic_load(&p->state) == STATE_SETTING_UP)
298 28 ff_thread_finish_setup(avctx);
299
300 578 alloc_fail:
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (p->hwaccel_serializing) {
302 /* wipe hwaccel state for thread-unsafe hwaccels to avoid stale
303 * pointers lying around;
304 * the state was transferred to FrameThreadContext in
305 * ff_thread_finish_setup(), so nothing is leaked */
306 avctx->hwaccel = NULL;
307 avctx->hwaccel_context = NULL;
308 avctx->internal->hwaccel_priv_data = NULL;
309
310 p->hwaccel_serializing = 0;
311 pthread_mutex_unlock(&p->parent->hwaccel_mutex);
312 }
313
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
606 av_assert0(!avctx->hwaccel ||
314 (ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE));
315
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (p->async_serializing) {
317 p->async_serializing = 0;
318
319 async_unlock(p->parent);
320 }
321
322 606 pthread_mutex_lock(&p->progress_mutex);
323
324 606 atomic_store(&p->state, STATE_INPUT_READY);
325
326 606 pthread_cond_broadcast(&p->progress_cond);
327 606 pthread_cond_signal(&p->output_cond);
328 606 pthread_mutex_unlock(&p->progress_mutex);
329 }
330 76 pthread_mutex_unlock(&p->mutex);
331
332 76 return NULL;
333 }
334
335 /**
336 * Update the next thread's AVCodecContext with values from the reference thread's context.
337 *
338 * @param dst The destination context.
339 * @param src The source context.
340 * @param for_user 0 if the destination is a codec thread, 1 if the destination is the user's thread
341 * @return 0 on success, negative error code on failure
342 */
343 1111 static int update_context_from_thread(AVCodecContext *dst, const AVCodecContext *src, int for_user)
344 {
345 1111 const FFCodec *const codec = ffcodec(dst->codec);
346 1111 int err = 0;
347
348
5/6
✓ Branch 0 taken 1111 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 555 times.
✓ Branch 3 taken 556 times.
✓ Branch 4 taken 108 times.
✓ Branch 5 taken 447 times.
1111 if (dst != src && (for_user || codec->update_thread_context)) {
349 664 dst->time_base = src->time_base;
350 664 dst->framerate = src->framerate;
351 664 dst->width = src->width;
352 664 dst->height = src->height;
353 664 dst->pix_fmt = src->pix_fmt;
354 664 dst->sw_pix_fmt = src->sw_pix_fmt;
355
356 664 dst->coded_width = src->coded_width;
357 664 dst->coded_height = src->coded_height;
358
359 664 dst->has_b_frames = src->has_b_frames;
360 664 dst->idct_algo = src->idct_algo;
361
362 664 dst->bits_per_coded_sample = src->bits_per_coded_sample;
363 664 dst->sample_aspect_ratio = src->sample_aspect_ratio;
364
365 664 dst->profile = src->profile;
366 664 dst->level = src->level;
367
368 664 dst->bits_per_raw_sample = src->bits_per_raw_sample;
369 664 dst->color_primaries = src->color_primaries;
370
371 664 dst->alpha_mode = src->alpha_mode;
372
373 664 dst->color_trc = src->color_trc;
374 664 dst->colorspace = src->colorspace;
375 664 dst->color_range = src->color_range;
376 664 dst->chroma_sample_location = src->chroma_sample_location;
377
378 664 dst->sample_rate = src->sample_rate;
379 664 dst->sample_fmt = src->sample_fmt;
380 664 err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
381
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 664 times.
664 if (err < 0)
382 return err;
383
384
1/2
✓ Branch 0 taken 664 times.
✗ Branch 1 not taken.
664 if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
385
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 664 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
664 (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
386 av_buffer_unref(&dst->hw_frames_ctx);
387
388 if (src->hw_frames_ctx) {
389 dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
390 if (!dst->hw_frames_ctx)
391 return AVERROR(ENOMEM);
392 }
393 }
394
395 664 dst->hwaccel_flags = src->hwaccel_flags;
396
397 664 av_refstruct_replace(&dst->internal->pool, src->internal->pool);
398 664 ff_decode_internal_sync(dst, src);
399 }
400
401
2/2
✓ Branch 0 taken 556 times.
✓ Branch 1 taken 555 times.
1111 if (for_user) {
402
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 498 times.
556 if (codec->update_thread_context_for_user)
403 58 err = codec->update_thread_context_for_user(dst, src);
404 } else {
405 555 const PerThreadContext *p_src = src->internal->thread_ctx;
406 555 PerThreadContext *p_dst = dst->internal->thread_ctx;
407
408
2/2
✓ Branch 0 taken 108 times.
✓ Branch 1 taken 447 times.
555 if (codec->update_thread_context) {
409 108 err = codec->update_thread_context(dst, src);
410
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 108 times.
108 if (err < 0)
411 return err;
412 }
413
414 // reset dst hwaccel state if needed
415
3/6
✓ Branch 0 taken 555 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 555 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 555 times.
555 av_assert0(p_dst->hwaccel_threadsafe ||
416 (!dst->hwaccel && !dst->internal->hwaccel_priv_data));
417
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 555 times.
555 if (p_dst->hwaccel_threadsafe &&
418 (!p_src->hwaccel_threadsafe || dst->hwaccel != src->hwaccel)) {
419 ff_hwaccel_uninit(dst);
420 p_dst->hwaccel_threadsafe = 0;
421 }
422
423 // propagate hwaccel state for threadsafe hwaccels
424
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 555 times.
555 if (p_src->hwaccel_threadsafe) {
425 const FFHWAccel *hwaccel = ffhwaccel(src->hwaccel);
426 if (!dst->hwaccel) {
427 if (hwaccel->priv_data_size) {
428 av_assert0(hwaccel->update_thread_context);
429
430 dst->internal->hwaccel_priv_data =
431 av_mallocz(hwaccel->priv_data_size);
432 if (!dst->internal->hwaccel_priv_data)
433 return AVERROR(ENOMEM);
434 }
435 dst->hwaccel = src->hwaccel;
436 }
437 av_assert0(dst->hwaccel == src->hwaccel);
438
439 if (hwaccel->update_thread_context) {
440 err = hwaccel->update_thread_context(dst, src);
441 if (err < 0) {
442 av_log(dst, AV_LOG_ERROR, "Error propagating hwaccel state\n");
443 ff_hwaccel_uninit(dst);
444 return err;
445 }
446 }
447 p_dst->hwaccel_threadsafe = 1;
448 }
449 }
450
451 1111 return err;
452 }
453
454 /**
455 * Update the next thread's AVCodecContext with values set by the user.
456 *
457 * @param dst The destination context.
458 * @param src The source context.
459 * @return 0 on success, negative error code on failure
460 */
461 606 static int update_context_from_user(AVCodecContext *dst, const AVCodecContext *src)
462 {
463 int err;
464
465 606 dst->flags = src->flags;
466
467 606 dst->draw_horiz_band= src->draw_horiz_band;
468 606 dst->get_buffer2 = src->get_buffer2;
469
470 606 dst->opaque = src->opaque;
471 606 dst->debug = src->debug;
472
473 606 dst->slice_flags = src->slice_flags;
474 606 dst->flags2 = src->flags2;
475 606 dst->export_side_data = src->export_side_data;
476
477 606 dst->skip_loop_filter = src->skip_loop_filter;
478 606 dst->skip_idct = src->skip_idct;
479 606 dst->skip_pred = src->skip_pred;
480 606 dst->skip_frame = src->skip_frame;
481
482 606 dst->frame_num = src->frame_num;
483
484 606 av_packet_unref(dst->internal->last_pkt_props);
485 606 err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
486
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (err < 0)
487 return err;
488
489 606 return 0;
490 }
491
492 606 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
493 AVPacket *in_pkt)
494 {
495 606 FrameThreadContext *fctx = p->parent;
496 606 PerThreadContext *prev_thread = fctx->prev_thread;
497 606 const AVCodec *codec = p->avctx->codec;
498 int ret;
499
500 606 pthread_mutex_lock(&p->mutex);
501
502 606 av_packet_unref(p->avpkt);
503 606 av_packet_move_ref(p->avpkt, in_pkt);
504
505
3/4
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 538 times.
✓ Branch 2 taken 68 times.
✗ Branch 3 not taken.
606 if (AVPACKET_IS_EMPTY(p->avpkt))
506 68 p->avctx->internal->draining = 1;
507
508 606 ret = update_context_from_user(p->avctx, user_avctx);
509
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (ret) {
510 pthread_mutex_unlock(&p->mutex);
511 return ret;
512 }
513 606 atomic_store_explicit(&p->debug_threads,
514 (p->avctx->debug & FF_DEBUG_THREADS) != 0,
515 memory_order_relaxed);
516
517
2/2
✓ Branch 0 taken 596 times.
✓ Branch 1 taken 10 times.
606 if (prev_thread) {
518
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 272 times.
596 if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
519 324 pthread_mutex_lock(&prev_thread->progress_mutex);
520
2/2
✓ Branch 0 taken 324 times.
✓ Branch 1 taken 324 times.
648 while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
521 324 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
522 324 pthread_mutex_unlock(&prev_thread->progress_mutex);
523 }
524
525 /* codecs without delay might not be prepared to be called repeatedly here during
526 * flushing (vp3/theora), and also don't need to be, since from this point on, they
527 * will always return EOF anyway */
528
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 528 times.
596 if (!p->avctx->internal->draining ||
529
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 41 times.
68 (codec->capabilities & AV_CODEC_CAP_DELAY)) {
530 555 ret = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
531
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 555 times.
555 if (ret) {
532 pthread_mutex_unlock(&p->mutex);
533 return ret;
534 }
535 }
536 }
537
538 /* transfer the stashed hwaccel state, if any */
539
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
606 av_assert0(!p->avctx->hwaccel || p->hwaccel_threadsafe);
540
1/2
✓ Branch 0 taken 606 times.
✗ Branch 1 not taken.
606 if (!p->hwaccel_threadsafe) {
541 606 FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
542 606 FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
543 606 FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
544 }
545
546 606 atomic_store(&p->state, STATE_SETTING_UP);
547 606 pthread_cond_signal(&p->input_cond);
548 606 pthread_mutex_unlock(&p->mutex);
549
550 606 fctx->prev_thread = p;
551 606 fctx->next_decoding = (fctx->next_decoding + 1) % p->avctx->thread_count;
552
553 606 return 0;
554 }
555
556 1211 int ff_thread_receive_frame(AVCodecContext *avctx, AVFrame *frame, unsigned flags)
557 {
558 1211 FrameThreadContext *fctx = avctx->internal->thread_ctx;
559 1211 int ret = 0;
560
561 /* release the async lock, permitting blocked hwaccel threads to
562 * go forward while we are in this function */
563 1211 async_unlock(fctx);
564
565 /* submit packets to threads while there are no buffered results to return */
566
4/4
✓ Branch 0 taken 1282 times.
✓ Branch 1 taken 537 times.
✓ Branch 2 taken 1273 times.
✓ Branch 3 taken 9 times.
1819 while (!fctx->df.nb_f && !fctx->result) {
567 PerThreadContext *p;
568
569
2/2
✓ Branch 0 taken 1251 times.
✓ Branch 1 taken 22 times.
1273 if (fctx->next_decoding != fctx->next_finished &&
570
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1249 times.
1251 (flags & AV_CODEC_RECEIVE_FRAME_FLAG_SYNCHRONOUS))
571 2 goto wait_for_result;
572
573 /* get a packet to be submitted to the next thread */
574 1271 av_packet_unref(fctx->next_pkt);
575 1271 ret = ff_decode_get_packet(avctx, fctx->next_pkt);
576
4/4
✓ Branch 0 taken 733 times.
✓ Branch 1 taken 538 times.
✓ Branch 2 taken 665 times.
✓ Branch 3 taken 68 times.
1271 if (ret < 0 && ret != AVERROR_EOF)
577 665 goto finish;
578
579 606 ret = submit_packet(&fctx->threads[fctx->next_decoding], avctx,
580 fctx->next_pkt);
581
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (ret < 0)
582 goto finish;
583
584 /* do not return any frames until all threads have something to do */
585
2/2
✓ Branch 0 taken 74 times.
✓ Branch 1 taken 532 times.
606 if (fctx->next_decoding != fctx->next_finished &&
586
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 62 times.
74 !avctx->internal->draining)
587 62 continue;
588
589 544 wait_for_result:
590 546 p = &fctx->threads[fctx->next_finished];
591 546 fctx->next_finished = (fctx->next_finished + 1) % avctx->thread_count;
592
593
2/2
✓ Branch 0 taken 129 times.
✓ Branch 1 taken 417 times.
546 if (atomic_load(&p->state) != STATE_INPUT_READY) {
594 129 pthread_mutex_lock(&p->progress_mutex);
595
2/2
✓ Branch 0 taken 128 times.
✓ Branch 1 taken 129 times.
257 while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
596 128 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
597 129 pthread_mutex_unlock(&p->progress_mutex);
598 }
599
600 546 update_context_from_thread(avctx, p->avctx, 1);
601 546 fctx->result = p->result;
602 546 p->result = 0;
603
2/2
✓ Branch 0 taken 535 times.
✓ Branch 1 taken 11 times.
546 if (p->df.nb_f)
604 535 FFSWAP(DecodedFrames, fctx->df, p->df);
605 }
606
607 /* a thread may return multiple frames AND an error
608 * we first return all the frames, then the error */
609
2/2
✓ Branch 0 taken 537 times.
✓ Branch 1 taken 9 times.
546 if (fctx->df.nb_f) {
610 537 decoded_frames_pop(&fctx->df, frame);
611 537 ret = 0;
612 } else {
613 9 ret = fctx->result;
614 9 fctx->result = 0;
615 }
616
617 1211 finish:
618 1211 async_lock(fctx);
619 1211 return ret;
620 }
621
622 296547 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
623 {
624 PerThreadContext *p;
625
2/2
✓ Branch 0 taken 1240 times.
✓ Branch 1 taken 295307 times.
296547 atomic_int *progress = f->progress ? f->progress->progress : NULL;
626
627
2/2
✓ Branch 0 taken 1240 times.
✓ Branch 1 taken 295307 times.
296547 if (!progress ||
628
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 1213 times.
1240 atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
629 295334 return;
630
631 1213 p = f->owner[field]->internal->thread_ctx;
632
633
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1213 times.
1213 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
634 av_log(f->owner[field], AV_LOG_DEBUG,
635 "%p finished %d field %d\n", progress, n, field);
636
637 1213 pthread_mutex_lock(&p->progress_mutex);
638
639 1213 atomic_store_explicit(&progress[field], n, memory_order_release);
640
641 1213 pthread_cond_broadcast(&p->progress_cond);
642 1213 pthread_mutex_unlock(&p->progress_mutex);
643 }
644
645 150891 void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
646 {
647 PerThreadContext *p;
648
2/2
✓ Branch 0 taken 150193 times.
✓ Branch 1 taken 698 times.
150891 atomic_int *progress = f->progress ? f->progress->progress : NULL;
649
650
2/2
✓ Branch 0 taken 150193 times.
✓ Branch 1 taken 698 times.
150891 if (!progress ||
651
2/2
✓ Branch 0 taken 149667 times.
✓ Branch 1 taken 526 times.
150193 atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
652 150365 return;
653
654 526 p = f->owner[field]->internal->thread_ctx;
655
656
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 526 times.
526 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
657 av_log(f->owner[field], AV_LOG_DEBUG,
658 "thread awaiting %d field %d from %p\n", n, field, progress);
659
660 526 pthread_mutex_lock(&p->progress_mutex);
661
2/2
✓ Branch 0 taken 528 times.
✓ Branch 1 taken 526 times.
1054 while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
662 528 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
663 526 pthread_mutex_unlock(&p->progress_mutex);
664 }
665
666 24245 void ff_thread_finish_setup(AVCodecContext *avctx) {
667 PerThreadContext *p;
668
669
2/2
✓ Branch 0 taken 23639 times.
✓ Branch 1 taken 606 times.
24245 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
670
671 606 p = avctx->internal->thread_ctx;
672
673
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 p->hwaccel_threadsafe = avctx->hwaccel &&
674 (ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
675
676
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 606 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
606 if (hwaccel_serial(avctx) && !p->hwaccel_serializing) {
677 pthread_mutex_lock(&p->parent->hwaccel_mutex);
678 p->hwaccel_serializing = 1;
679 }
680
681 /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if (avctx->hwaccel &&
683 !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_ASYNC_SAFE)) {
684 p->async_serializing = 1;
685
686 async_lock(p->parent);
687 }
688
689 /* thread-unsafe hwaccels share a single private data instance, so we
690 * save hwaccel state for passing to the next thread;
691 * this is done here so that this worker thread can wipe its own hwaccel
692 * state after decoding, without requiring synchronization */
693
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 av_assert0(!p->parent->stash_hwaccel);
694
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 606 times.
606 if (hwaccel_serial(avctx)) {
695 p->parent->stash_hwaccel = avctx->hwaccel;
696 p->parent->stash_hwaccel_context = avctx->hwaccel_context;
697 p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
698 }
699
700 606 pthread_mutex_lock(&p->progress_mutex);
701
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 606 times.
606 if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
702 av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
703 }
704
705 606 atomic_store(&p->state, STATE_SETUP_FINISHED);
706
707 606 pthread_cond_broadcast(&p->progress_cond);
708 606 pthread_mutex_unlock(&p->progress_mutex);
709 }
710
711 /// Waits for all threads to finish.
712 10 static av_cold void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
713 {
714 int i;
715
716 10 async_unlock(fctx);
717
718
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 10 times.
86 for (i = 0; i < thread_count; i++) {
719 76 PerThreadContext *p = &fctx->threads[i];
720
721
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 71 times.
76 if (atomic_load(&p->state) != STATE_INPUT_READY) {
722 5 pthread_mutex_lock(&p->progress_mutex);
723
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 while (atomic_load(&p->state) != STATE_INPUT_READY)
724 5 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
725 5 pthread_mutex_unlock(&p->progress_mutex);
726 }
727 }
728
729 10 async_lock(fctx);
730 10 }
731
732 #define OFF(member) offsetof(FrameThreadContext, member)
733 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
734 (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
735 (OFF(async_cond)));
736 #undef OFF
737
738 #define OFF(member) offsetof(PerThreadContext, member)
739 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
740 (OFF(progress_mutex), OFF(mutex)),
741 (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
742 #undef OFF
743
744 10 av_cold void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
745 {
746 10 FrameThreadContext *fctx = avctx->internal->thread_ctx;
747 10 const FFCodec *codec = ffcodec(avctx->codec);
748 int i;
749
750 10 park_frame_worker_threads(fctx, thread_count);
751
752
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 10 times.
86 for (i = 0; i < thread_count; i++) {
753 76 PerThreadContext *p = &fctx->threads[i];
754 76 AVCodecContext *ctx = p->avctx;
755
756
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (ctx->internal) {
757
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (p->thread_init == INITIALIZED) {
758 76 pthread_mutex_lock(&p->mutex);
759 76 p->die = 1;
760 76 pthread_cond_signal(&p->input_cond);
761 76 pthread_mutex_unlock(&p->mutex);
762
763 76 pthread_join(p->thread, NULL);
764 }
765
2/4
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
76 if (codec->close && p->thread_init != UNINITIALIZED)
766 76 codec->close(ctx);
767
768 /* When using a threadsafe hwaccel, this is where
769 * each thread's context is uninit'd and freed. */
770 76 ff_hwaccel_uninit(ctx);
771
772
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (ctx->priv_data) {
773
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 18 times.
76 if (codec->p.priv_class)
774 58 av_opt_free(ctx->priv_data);
775 76 av_freep(&ctx->priv_data);
776 }
777
778 76 av_refstruct_unref(&ctx->internal->pool);
779 76 av_packet_free(&ctx->internal->in_pkt);
780 76 av_packet_free(&ctx->internal->last_pkt_props);
781 76 ff_decode_internal_uninit(ctx);
782 76 av_freep(&ctx->internal);
783 76 av_buffer_unref(&ctx->hw_frames_ctx);
784 76 av_frame_side_data_free(&ctx->decoded_side_data,
785 &ctx->nb_decoded_side_data);
786 }
787
788 76 decoded_frames_free(&p->df);
789
790 76 ff_pthread_free(p, per_thread_offsets);
791 76 av_packet_free(&p->avpkt);
792
793 76 av_freep(&p->avctx);
794 }
795
796 10 decoded_frames_free(&fctx->df);
797 10 av_packet_free(&fctx->next_pkt);
798
799 10 av_freep(&fctx->threads);
800 10 ff_pthread_free(fctx, thread_ctx_offsets);
801
802 /* if we have stashed hwaccel state, move it to the user-facing context,
803 * so it will be freed in ff_codec_close() */
804
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 av_assert0(!avctx->hwaccel);
805 10 FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
806 10 FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
807 10 FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
808
809 10 av_freep(&avctx->internal->thread_ctx);
810 10 }
811
812 76 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
813 FrameThreadContext *fctx, AVCodecContext *avctx,
814 const FFCodec *codec, int first)
815 {
816 AVCodecContext *copy;
817 int err;
818
819 76 atomic_init(&p->state, STATE_INPUT_READY);
820
821 76 copy = av_memdup(avctx, sizeof(*avctx));
822
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!copy)
823 return AVERROR(ENOMEM);
824 76 copy->priv_data = NULL;
825 76 copy->decoded_side_data = NULL;
826 76 copy->nb_decoded_side_data = 0;
827
828 /* From now on, this PerThreadContext will be cleaned up by
829 * ff_frame_thread_free in case of errors. */
830 76 (*threads_to_free)++;
831
832 76 p->parent = fctx;
833 76 p->avctx = copy;
834
835 76 copy->internal = ff_decode_internal_alloc();
836
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!copy->internal)
837 return AVERROR(ENOMEM);
838 76 ff_decode_internal_sync(copy, avctx);
839 76 copy->internal->thread_ctx = p;
840 76 copy->internal->progress_frame_pool = avctx->internal->progress_frame_pool;
841
842 76 copy->delay = avctx->delay;
843
844
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (codec->priv_data_size) {
845 76 copy->priv_data = av_mallocz(codec->priv_data_size);
846
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!copy->priv_data)
847 return AVERROR(ENOMEM);
848
849
2/2
✓ Branch 0 taken 58 times.
✓ Branch 1 taken 18 times.
76 if (codec->p.priv_class) {
850 58 *(const AVClass **)copy->priv_data = codec->p.priv_class;
851 58 err = av_opt_copy(copy->priv_data, avctx->priv_data);
852
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 58 times.
58 if (err < 0)
853 return err;
854 }
855 }
856
857 76 err = ff_pthread_init(p, per_thread_offsets);
858
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (err < 0)
859 return err;
860
861
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 76 times.
76 if (!(p->avpkt = av_packet_alloc()))
862 return AVERROR(ENOMEM);
863
864 76 copy->internal->is_frame_mt = 1;
865
2/2
✓ Branch 0 taken 66 times.
✓ Branch 1 taken 10 times.
76 if (!first)
866 66 copy->internal->is_copy = 1;
867
868 76 copy->internal->in_pkt = av_packet_alloc();
869
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!copy->internal->in_pkt)
870 return AVERROR(ENOMEM);
871
872 76 copy->internal->last_pkt_props = av_packet_alloc();
873
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (!copy->internal->last_pkt_props)
874 return AVERROR(ENOMEM);
875
876
1/2
✓ Branch 0 taken 76 times.
✗ Branch 1 not taken.
76 if (codec->init) {
877 76 err = codec->init(copy);
878
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (err < 0) {
879 if (codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)
880 p->thread_init = NEEDS_CLOSE;
881 return err;
882 }
883 }
884 76 p->thread_init = NEEDS_CLOSE;
885
886
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 66 times.
76 if (first)
887 10 update_context_from_thread(avctx, copy, 1);
888
889
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 66 times.
76 const AVCodecContext *src = first ? copy : avctx;
890
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 66 times.
76 AVCodecContext *dst = first ? avctx : copy;
891 76 av_frame_side_data_free(&dst->decoded_side_data, &dst->nb_decoded_side_data);
892
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 for (int i = 0; i < src->nb_decoded_side_data; i++) {
893 err = av_frame_side_data_clone(&dst->decoded_side_data,
894 &dst->nb_decoded_side_data,
895 src->decoded_side_data[i], 0);
896 if (err < 0)
897 return err;
898 }
899
900 76 atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
901
902 76 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
903
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (err < 0)
904 return err;
905 76 p->thread_init = INITIALIZED;
906
907 76 return 0;
908 }
909
910 10 av_cold int ff_frame_thread_init(AVCodecContext *avctx)
911 {
912 10 int thread_count = avctx->thread_count;
913 10 const FFCodec *codec = ffcodec(avctx->codec);
914 FrameThreadContext *fctx;
915 10 int err, i = 0;
916
917
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!thread_count) {
918 int nb_cpus = av_cpu_count();
919 // use number of cores + 1 as thread count if there is more than one
920 if (nb_cpus > 1)
921 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
922 else
923 thread_count = avctx->thread_count = 1;
924 }
925
926
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (thread_count <= 1) {
927 avctx->active_thread_type = 0;
928 return 0;
929 }
930
931 10 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
932
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!fctx)
933 return AVERROR(ENOMEM);
934
935 10 err = ff_pthread_init(fctx, thread_ctx_offsets);
936
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (err < 0) {
937 ff_pthread_free(fctx, thread_ctx_offsets);
938 av_freep(&avctx->internal->thread_ctx);
939 return err;
940 }
941
942 10 fctx->next_pkt = av_packet_alloc();
943
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!fctx->next_pkt)
944 return AVERROR(ENOMEM);
945
946 10 fctx->async_lock = 1;
947
948
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
10 if (codec->p.type == AVMEDIA_TYPE_VIDEO)
949 6 avctx->delay = avctx->thread_count - 1;
950
951 10 fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
952
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!fctx->threads) {
953 err = AVERROR(ENOMEM);
954 goto error;
955 }
956
957
2/2
✓ Branch 0 taken 76 times.
✓ Branch 1 taken 10 times.
86 for (; i < thread_count; ) {
958 76 PerThreadContext *p = &fctx->threads[i];
959 76 int first = !i;
960
961 76 err = init_thread(p, &i, fctx, avctx, codec, first);
962
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 76 times.
76 if (err < 0)
963 goto error;
964 }
965
966 10 return 0;
967
968 error:
969 ff_frame_thread_free(avctx, i);
970 return err;
971 }
972
973 av_cold void ff_thread_flush(AVCodecContext *avctx)
974 {
975 int i;
976 FrameThreadContext *fctx = avctx->internal->thread_ctx;
977
978 if (!fctx) return;
979
980 park_frame_worker_threads(fctx, avctx->thread_count);
981 if (fctx->prev_thread) {
982 if (fctx->prev_thread != &fctx->threads[0])
983 update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
984 }
985
986 fctx->next_decoding = fctx->next_finished = 0;
987 fctx->prev_thread = NULL;
988
989 decoded_frames_flush(&fctx->df);
990 fctx->result = 0;
991
992 for (i = 0; i < avctx->thread_count; i++) {
993 PerThreadContext *p = &fctx->threads[i];
994
995 decoded_frames_flush(&p->df);
996 p->result = 0;
997
998 avcodec_flush_buffers(p->avctx);
999 }
1000 }
1001
1002 38016 int ff_thread_can_start_frame(AVCodecContext *avctx)
1003 {
1004
2/2
✓ Branch 0 taken 52 times.
✓ Branch 1 taken 37964 times.
38016 if ((avctx->active_thread_type & FF_THREAD_FRAME) &&
1005
1/2
✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
52 ffcodec(avctx->codec)->update_thread_context) {
1006 52 PerThreadContext *p = avctx->internal->thread_ctx;
1007
1008
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 52 times.
52 if (atomic_load(&p->state) != STATE_SETTING_UP)
1009 return 0;
1010 }
1011
1012 38016 return 1;
1013 }
1014
1015 53473 static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
1016 {
1017 PerThreadContext *p;
1018 int err;
1019
1020
2/2
✓ Branch 0 taken 52935 times.
✓ Branch 1 taken 538 times.
53473 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1021 52935 return ff_get_buffer(avctx, f, flags);
1022
1023 538 p = avctx->internal->thread_ctx;
1024
2/2
✓ Branch 0 taken 453 times.
✓ Branch 1 taken 85 times.
538 if (atomic_load(&p->state) != STATE_SETTING_UP &&
1025
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 453 times.
453 ffcodec(avctx->codec)->update_thread_context) {
1026 av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
1027 return -1;
1028 }
1029
1030 538 pthread_mutex_lock(&p->parent->buffer_mutex);
1031 538 err = ff_get_buffer(avctx, f, flags);
1032
1033 538 pthread_mutex_unlock(&p->parent->buffer_mutex);
1034
1035 538 return err;
1036 }
1037
1038 53473 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
1039 {
1040 53473 int ret = thread_get_buffer_internal(avctx, f, flags);
1041
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 53473 times.
53473 if (ret < 0)
1042 av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1043 53473 return ret;
1044 }
1045
1046 26570 int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
1047 {
1048 int ret;
1049
1050 26570 f->owner[0] = f->owner[1] = avctx;
1051
2/2
✓ Branch 0 taken 26528 times.
✓ Branch 1 taken 42 times.
26570 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1052 26528 return ff_get_buffer(avctx, f->f, flags);
1053
1054 42 f->progress = av_refstruct_allocz(sizeof(*f->progress));
1055
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (!f->progress)
1056 return AVERROR(ENOMEM);
1057
1058 42 atomic_init(&f->progress->progress[0], -1);
1059 42 atomic_init(&f->progress->progress[1], -1);
1060
1061 42 ret = ff_thread_get_buffer(avctx, f->f, flags);
1062
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 42 times.
42 if (ret)
1063 av_refstruct_unref(&f->progress);
1064 42 return ret;
1065 }
1066
1067 55137 void ff_thread_release_ext_buffer(ThreadFrame *f)
1068 {
1069 55137 av_refstruct_unref(&f->progress);
1070 55137 f->owner[0] = f->owner[1] = NULL;
1071
1/2
✓ Branch 0 taken 55137 times.
✗ Branch 1 not taken.
55137 if (f->f)
1072 55137 av_frame_unref(f->f);
1073 55137 }
1074
1075 811 av_cold enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1076 {
1077 PerThreadContext *p;
1078 const void *ref;
1079
1080
2/2
✓ Branch 0 taken 803 times.
✓ Branch 1 taken 8 times.
811 if (!avctx->internal->is_copy)
1081 803 return avctx->active_thread_type & FF_THREAD_FRAME ?
1082
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 802 times.
803 FF_THREAD_IS_FIRST_THREAD : FF_THREAD_NO_FRAME_THREADING;
1083
1084 8 p = avctx->internal->thread_ctx;
1085
1086 av_assert1(memcpy(&ref, (char*)avctx->priv_data + offset, sizeof(ref)) && ref == NULL);
1087
1088 8 memcpy(&ref, (const char*)p->parent->threads[0].avctx->priv_data + offset, sizeof(ref));
1089 av_assert1(ref);
1090 8 av_refstruct_replace((char*)avctx->priv_data + offset, ref);
1091
1092 8 return FF_THREAD_IS_COPY;
1093 }
1094
1095 1076 int ff_thread_get_packet(AVCodecContext *avctx, AVPacket *pkt)
1096 {
1097 1076 PerThreadContext *p = avctx->internal->thread_ctx;
1098
1099
3/4
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 538 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 538 times.
1076 if (!AVPACKET_IS_EMPTY(p->avpkt)) {
1100 538 av_packet_move_ref(pkt, p->avpkt);
1101 538 return 0;
1102 }
1103
1104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 538 times.
538 return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
1105 }
1106