FFmpeg coverage


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