FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread_frame.c
Date: 2026-01-14 03:33:33
Exec Total Coverage
Lines: 422 542 77.9%
Functions: 26 28 92.9%
Branches: 190 316 60.1%

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