../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
435 / 558
78.0%
Branches:
199 / 328
60.7%
Functions:
26 / 28
92.9%

libavcodec/pthread_frame.c
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 7794 static int hwaccel_serial(const AVCodecContext *avctx)
155 {
156
1/4
✗ Branch 2 → 3 not taken.
✓ Branch 2 → 6 taken 7794 times.
✗ Branch 4 → 5 not taken.
✗ Branch 4 → 6 not taken.
7794 return avctx->hwaccel && !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
157 }
158
159 5196 static void async_lock(FrameThreadContext *fctx)
160 {
161 5196 pthread_mutex_lock(&fctx->async_mutex);
162
1/2
✗ Branch 5 → 4 not taken.
✓ Branch 5 → 6 taken 5196 times.
5196 while (fctx->async_lock)
163 ✗ pthread_cond_wait(&fctx->async_cond, &fctx->async_mutex);
164 5196 fctx->async_lock = 1;
165 5196 pthread_mutex_unlock(&fctx->async_mutex);
166 5196 }
167
168 5196 static void async_unlock(FrameThreadContext *fctx)
169 {
170 5196 pthread_mutex_lock(&fctx->async_mutex);
171
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 5196 times.
5196 av_assert0(fctx->async_lock);
172 5196 fctx->async_lock = 0;
173 5196 pthread_cond_broadcast(&fctx->async_cond);
174 5196 pthread_mutex_unlock(&fctx->async_mutex);
175 5196 }
176
177 97 static void thread_set_name(PerThreadContext *p)
178 {
179 97 AVCodecContext *avctx = p->avctx;
180 97 int idx = p - p->parent->threads;
181 char name[16];
182
183 97 snprintf(name, sizeof(name), "av:%.7s:df%d", avctx->codec->name, idx);
184
185 97 ff_thread_setname(name);
186 97 }
187
188 // get a free frame to decode into
189 5079 static AVFrame *decoded_frames_get_free(DecodedFrames *df)
190 {
191
2/2
✓ Branch 2 → 3 taken 217 times.
✓ Branch 2 → 10 taken 4862 times.
5079 if (df->nb_f == df->nb_f_allocated) {
192 217 AVFrame **tmp = av_realloc_array(df->f, df->nb_f + 1,
193 sizeof(*df->f));
194
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 217 times.
217 if (!tmp)
195 ✗ return NULL;
196 217 df->f = tmp;
197
198 217 df->f[df->nb_f] = av_frame_alloc();
199
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 217 times.
217 if (!df->f[df->nb_f])
200 ✗ return NULL;
201
202 217 df->nb_f_allocated++;
203 }
204
205
1/2
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 13 taken 5079 times.
5079 av_assert0(!df->f[df->nb_f]->buf[0]);
206
207 5079 return df->f[df->nb_f];
208 }
209
210 2480 static void decoded_frames_pop(DecodedFrames *df, AVFrame *dst)
211 {
212 2480 AVFrame *tmp_frame = df->f[0];
213 2480 av_frame_move_ref(dst, tmp_frame);
214 2480 memmove(df->f, df->f + 1, (df->nb_f - 1) * sizeof(*df->f));
215 2480 df->f[--df->nb_f] = tmp_frame;
216 2480 }
217
218 ✗ static void decoded_frames_flush(DecodedFrames *df)
219 {
220
0/2
✗ Branch 5 → 3 not taken.
✗ Branch 5 → 6 not taken.
✗ 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 113 static void decoded_frames_free(DecodedFrames *df)
226 {
227
2/2
✓ Branch 5 → 3 taken 217 times.
✓ Branch 5 → 6 taken 113 times.
330 for (size_t i = 0; i < df->nb_f_allocated; i++)
228 217 av_frame_free(&df->f[i]);
229 113 av_freep(&df->f);
230 113 df->nb_f = 0;
231 113 df->nb_f_allocated = 0;
232 113 }
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 97 static attribute_align_arg void *frame_worker_thread(void *arg)
242 {
243 97 PerThreadContext *p = arg;
244 97 AVCodecContext *avctx = p->avctx;
245 97 const FFCodec *codec = ffcodec(avctx->codec);
246
247 97 thread_set_name(p);
248
249 97 pthread_mutex_lock(&p->mutex);
250 2598 while (1) {
251 int ret;
252
253
4/4
✓ Branch 7 → 8 taken 2792 times.
✓ Branch 7 → 9 taken 2598 times.
✓ Branch 8 → 6 taken 2695 times.
✓ Branch 8 → 9 taken 97 times.
5390 while (atomic_load(&p->state) == STATE_INPUT_READY && !p->die)
254 2695 pthread_cond_wait(&p->input_cond, &p->mutex);
255
256
2/2
✓ Branch 9 → 10 taken 97 times.
✓ Branch 9 → 11 taken 2598 times.
2695 if (p->die) break;
257
258
2/2
✓ Branch 11 → 12 taken 173 times.
✓ Branch 11 → 13 taken 2425 times.
2598 if (!codec->update_thread_context)
259 173 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 13 → 14 not taken.
✓ Branch 13 → 16 taken 2598 times.
2598 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 17 → 18 not taken.
✓ Branch 17 → 20 taken 2598 times.
2598 if (hwaccel_serial(avctx)) {
272 ✗ pthread_mutex_lock(&p->parent->hwaccel_mutex);
273 ✗ p->hwaccel_serializing = 1;
274 }
275
276 2598 ret = 0;
277
2/2
✓ Branch 34 → 21 taken 5079 times.
✓ Branch 34 → 35 taken 2598 times.
7677 while (ret >= 0) {
278 AVFrame *frame;
279
280 /* get the frame which will store the output */
281 5079 frame = decoded_frames_get_free(&p->df);
282
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 5079 times.
5079 if (!frame) {
283 ✗ p->result = AVERROR(ENOMEM);
284 ✗ goto alloc_fail;
285 }
286
287 /* do the actual decoding */
288 5079 ret = ff_decode_receive_frame_internal(avctx, frame);
289
2/2
✓ Branch 25 → 26 taken 2481 times.
✓ Branch 25 → 27 taken 2598 times.
5079 if (ret == 0)
290 2481 p->df.nb_f++;
291
2/4
✓ Branch 27 → 28 taken 2598 times.
✗ Branch 27 → 30 not taken.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 2598 times.
2598 else if (ret < 0 && frame->buf[0])
292 ✗ av_frame_unref(frame);
293
294
2/2
✓ Branch 30 → 31 taken 2598 times.
✓ Branch 30 → 32 taken 2481 times.
5079 p->result = (ret == AVERROR(EAGAIN)) ? 0 : ret;
295 }
296
297
2/2
✓ Branch 35 → 36 taken 2503 times.
✓ Branch 35 → 37 taken 95 times.
2598 if (atomic_load(&p->state) == STATE_SETTING_UP)
298 95 ff_thread_finish_setup(avctx);
299
300 2503 alloc_fail:
301
1/2
✗ Branch 38 → 39 not taken.
✓ Branch 38 → 40 taken 2598 times.
2598 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 40 → 41 not taken.
✓ Branch 40 → 45 taken 2598 times.
✗ Branch 42 → 43 not taken.
✗ Branch 42 → 45 not taken.
2598 av_assert0(!avctx->hwaccel ||
314 (ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE));
315
316
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 47 taken 2598 times.
2598 if (p->async_serializing) {
317 ✗ p->async_serializing = 0;
318
319 ✗ async_unlock(p->parent);
320 }
321
322 2598 pthread_mutex_lock(&p->progress_mutex);
323
324 2598 atomic_store(&p->state, STATE_INPUT_READY);
325
326 2598 pthread_cond_broadcast(&p->progress_cond);
327 2598 pthread_cond_signal(&p->output_cond);
328 2598 pthread_mutex_unlock(&p->progress_mutex);
329 }
330 97 pthread_mutex_unlock(&p->mutex);
331
332 97 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 5068 static int update_context_from_thread(AVCodecContext *dst, const AVCodecContext *src, int for_user)
344 {
345 5068 const FFCodec *const codec = ffcodec(dst->codec);
346 5068 int err = 0;
347
348
5/6
✓ Branch 3 → 4 taken 5068 times.
✗ Branch 3 → 19 not taken.
✓ Branch 4 → 5 taken 2529 times.
✓ Branch 4 → 6 taken 2539 times.
✓ Branch 5 → 6 taken 2383 times.
✓ Branch 5 → 19 taken 146 times.
5068 if (dst != src && (for_user || codec->update_thread_context)) {
349 4922 dst->time_base = src->time_base;
350 4922 dst->framerate = src->framerate;
351 4922 dst->width = src->width;
352 4922 dst->height = src->height;
353 4922 dst->pix_fmt = src->pix_fmt;
354 4922 dst->sw_pix_fmt = src->sw_pix_fmt;
355
356 4922 dst->coded_width = src->coded_width;
357 4922 dst->coded_height = src->coded_height;
358
359 4922 dst->has_b_frames = src->has_b_frames;
360 4922 dst->idct_algo = src->idct_algo;
361
362 4922 dst->bits_per_coded_sample = src->bits_per_coded_sample;
363 4922 dst->sample_aspect_ratio = src->sample_aspect_ratio;
364
365 4922 dst->profile = src->profile;
366 4922 dst->level = src->level;
367
368 4922 dst->bits_per_raw_sample = src->bits_per_raw_sample;
369 4922 dst->color_primaries = src->color_primaries;
370
371 4922 dst->alpha_mode = src->alpha_mode;
372
373 4922 dst->color_trc = src->color_trc;
374 4922 dst->colorspace = src->colorspace;
375 4922 dst->color_range = src->color_range;
376 4922 dst->chroma_sample_location = src->chroma_sample_location;
377
378 4922 dst->sample_rate = src->sample_rate;
379 4922 dst->sample_fmt = src->sample_fmt;
380 4922 err = av_channel_layout_copy(&dst->ch_layout, &src->ch_layout);
381
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 9 taken 4922 times.
4922 if (err < 0)
382 ✗ return err;
383
384
1/2
✓ Branch 9 → 10 taken 4922 times.
✗ Branch 9 → 12 not taken.
4922 if (!!dst->hw_frames_ctx != !!src->hw_frames_ctx ||
385
1/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 17 taken 4922 times.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 17 not taken.
4922 (dst->hw_frames_ctx && dst->hw_frames_ctx->data != src->hw_frames_ctx->data)) {
386 ✗ av_buffer_unref(&dst->hw_frames_ctx);
387
388
0/2
✗ Branch 13 → 14 not taken.
✗ Branch 13 → 17 not taken.
✗ if (src->hw_frames_ctx) {
389 ✗ dst->hw_frames_ctx = av_buffer_ref(src->hw_frames_ctx);
390
0/2
✗ Branch 15 → 16 not taken.
✗ Branch 15 → 17 not taken.
✗ if (!dst->hw_frames_ctx)
391 ✗ return AVERROR(ENOMEM);
392 }
393 }
394
395 4922 dst->hwaccel_flags = src->hwaccel_flags;
396
397 4922 av_refstruct_replace(&dst->internal->pool, src->internal->pool);
398 4922 ff_decode_internal_sync(dst, src);
399 }
400
401
2/2
✓ Branch 19 → 20 taken 2539 times.
✓ Branch 19 → 22 taken 2529 times.
5068 if (for_user) {
402
2/2
✓ Branch 20 → 21 taken 1880 times.
✓ Branch 20 → 57 taken 659 times.
2539 if (codec->update_thread_context_for_user)
403 1880 err = codec->update_thread_context_for_user(dst, src);
404 } else {
405 2529 const PerThreadContext *p_src = src->internal->thread_ctx;
406 2529 PerThreadContext *p_dst = dst->internal->thread_ctx;
407
408
2/2
✓ Branch 22 → 23 taken 2383 times.
✓ Branch 22 → 26 taken 146 times.
2529 if (codec->update_thread_context) {
409 2383 err = codec->update_thread_context(dst, src);
410
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 2383 times.
2383 if (err < 0)
411 ✗ return err;
412 }
413
414 // reset dst hwaccel state if needed
415
3/6
✓ Branch 26 → 27 taken 2529 times.
✗ Branch 26 → 31 not taken.
✓ Branch 27 → 28 taken 2529 times.
✗ Branch 27 → 29 not taken.
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 2529 times.
2529 av_assert0(p_dst->hwaccel_threadsafe ||
416 (!dst->hwaccel && !dst->internal->hwaccel_priv_data));
417
1/2
✗ Branch 31 → 32 not taken.
✓ Branch 31 → 36 taken 2529 times.
2529 if (p_dst->hwaccel_threadsafe &&
418
0/4
✗ Branch 32 → 33 not taken.
✗ Branch 32 → 34 not taken.
✗ Branch 33 → 34 not taken.
✗ Branch 33 → 36 not taken.
✗ (!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 36 → 37 not taken.
✓ Branch 36 → 57 taken 2529 times.
2529 if (p_src->hwaccel_threadsafe) {
425 ✗ const FFHWAccel *hwaccel = ffhwaccel(src->hwaccel);
426
0/2
✗ Branch 38 → 39 not taken.
✗ Branch 38 → 47 not taken.
✗ if (!dst->hwaccel) {
427
0/2
✗ Branch 39 → 40 not taken.
✗ Branch 39 → 46 not taken.
✗ if (hwaccel->priv_data_size) {
428
0/2
✗ Branch 40 → 41 not taken.
✗ Branch 40 → 43 not taken.
✗ av_assert0(hwaccel->update_thread_context);
429
430 ✗ dst->internal->hwaccel_priv_data =
431 ✗ av_mallocz(hwaccel->priv_data_size);
432
0/2
✗ Branch 44 → 45 not taken.
✗ Branch 44 → 46 not taken.
✗ if (!dst->internal->hwaccel_priv_data)
433 ✗ return AVERROR(ENOMEM);
434 }
435 ✗ dst->hwaccel = src->hwaccel;
436 }
437
0/2
✗ Branch 47 → 48 not taken.
✗ Branch 47 → 50 not taken.
✗ av_assert0(dst->hwaccel == src->hwaccel);
438
439
0/2
✗ Branch 50 → 51 not taken.
✗ Branch 50 → 56 not taken.
✗ if (hwaccel->update_thread_context) {
440 ✗ err = hwaccel->update_thread_context(dst, src);
441
0/2
✗ Branch 52 → 53 not taken.
✗ Branch 52 → 56 not taken.
✗ 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 5068 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 2598 static int update_context_from_user(AVCodecContext *dst, const AVCodecContext *src)
462 {
463 int err;
464
465 2598 dst->flags = src->flags;
466
467 2598 dst->draw_horiz_band= src->draw_horiz_band;
468 2598 dst->get_buffer2 = src->get_buffer2;
469
470 2598 dst->opaque = src->opaque;
471 2598 dst->debug = src->debug;
472
473 2598 dst->slice_flags = src->slice_flags;
474 2598 dst->flags2 = src->flags2;
475 2598 dst->export_side_data = src->export_side_data;
476
477 2598 dst->skip_loop_filter = src->skip_loop_filter;
478 2598 dst->skip_idct = src->skip_idct;
479 2598 dst->skip_pred = src->skip_pred;
480 2598 dst->skip_frame = src->skip_frame;
481
482 2598 dst->frame_num = src->frame_num;
483
484 2598 av_packet_unref(dst->internal->last_pkt_props);
485 2598 err = av_packet_copy_props(dst->internal->last_pkt_props, src->internal->last_pkt_props);
486
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 6 taken 2598 times.
2598 if (err < 0)
487 ✗ return err;
488
489 2598 return 0;
490 }
491
492 2598 static int submit_packet(PerThreadContext *p, AVCodecContext *user_avctx,
493 AVPacket *in_pkt)
494 {
495 2598 FrameThreadContext *fctx = p->parent;
496 2598 PerThreadContext *prev_thread = fctx->prev_thread;
497 2598 const AVCodec *codec = p->avctx->codec;
498 int ret;
499
500 2598 pthread_mutex_lock(&p->mutex);
501
502 2598 av_packet_unref(p->avpkt);
503 2598 av_packet_move_ref(p->avpkt, in_pkt);
504
505
3/4
✓ Branch 5 → 6 taken 89 times.
✓ Branch 5 → 8 taken 2509 times.
✓ Branch 6 → 7 taken 89 times.
✗ Branch 6 → 8 not taken.
2598 if (AVPACKET_IS_EMPTY(p->avpkt))
506 89 p->avctx->internal->draining = 1;
507
508 2598 ret = update_context_from_user(p->avctx, user_avctx);
509
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 2598 times.
2598 if (ret) {
510 ✗ pthread_mutex_unlock(&p->mutex);
511 ✗ return ret;
512 }
513 2598 atomic_store_explicit(&p->debug_threads,
514 (p->avctx->debug & FF_DEBUG_THREADS) != 0,
515 memory_order_relaxed);
516
517
2/2
✓ Branch 12 → 13 taken 2582 times.
✓ Branch 12 → 27 taken 16 times.
2598 if (prev_thread) {
518
2/2
✓ Branch 13 → 14 taken 853 times.
✓ Branch 13 → 19 taken 1729 times.
2582 if (atomic_load(&prev_thread->state) == STATE_SETTING_UP) {
519 853 pthread_mutex_lock(&prev_thread->progress_mutex);
520
2/2
✓ Branch 17 → 16 taken 853 times.
✓ Branch 17 → 18 taken 853 times.
1706 while (atomic_load(&prev_thread->state) == STATE_SETTING_UP)
521 853 pthread_cond_wait(&prev_thread->progress_cond, &prev_thread->progress_mutex);
522 853 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 19 → 20 taken 89 times.
✓ Branch 19 → 21 taken 2493 times.
2582 if (!p->avctx->internal->draining ||
529
2/2
✓ Branch 20 → 21 taken 36 times.
✓ Branch 20 → 27 taken 53 times.
89 (codec->capabilities & AV_CODEC_CAP_DELAY)) {
530 2529 ret = update_context_from_thread(p->avctx, prev_thread->avctx, 0);
531
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 27 taken 2529 times.
2529 if (ret) {
532 /* No packet was submitted and the thread indices did not advance,
533 * so retrying the drain packet can repeat this error forever. */
534
0/2
✗ Branch 23 → 24 not taken.
✗ Branch 23 → 25 not taken.
✗ if (user_avctx->internal->draining)
535 ✗ user_avctx->internal->draining_done = 1;
536 ✗ pthread_mutex_unlock(&p->mutex);
537 ✗ return ret;
538 }
539 }
540 }
541
542 /* transfer the stashed hwaccel state, if any */
543
1/4
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 31 taken 2598 times.
✗ Branch 28 → 29 not taken.
✗ Branch 28 → 31 not taken.
2598 av_assert0(!p->avctx->hwaccel || p->hwaccel_threadsafe);
544
1/2
✓ Branch 31 → 32 taken 2598 times.
✗ Branch 31 → 33 not taken.
2598 if (!p->hwaccel_threadsafe) {
545 2598 FFSWAP(const AVHWAccel*, p->avctx->hwaccel, fctx->stash_hwaccel);
546 2598 FFSWAP(void*, p->avctx->hwaccel_context, fctx->stash_hwaccel_context);
547 2598 FFSWAP(void*, p->avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
548 }
549
550 2598 atomic_store(&p->state, STATE_SETTING_UP);
551 2598 pthread_cond_signal(&p->input_cond);
552 2598 pthread_mutex_unlock(&p->mutex);
553
554 2598 fctx->prev_thread = p;
555 2598 fctx->next_decoding = (fctx->next_decoding + 1) % p->avctx->thread_count;
556
557 2598 return 0;
558 }
559
560 5180 int ff_thread_receive_frame(AVCodecContext *avctx, AVFrame *frame, unsigned flags)
561 {
562 5180 FrameThreadContext *fctx = avctx->internal->thread_ctx;
563 5180 int ret = 0;
564
565 /* release the async lock, permitting blocked hwaccel threads to
566 * go forward while we are in this function */
567 5180 async_unlock(fctx);
568
569
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 5180 times.
5180 if (avctx->internal->draining_done) {
570 ✗ ret = AVERROR_EOF;
571 ✗ goto finish;
572 }
573
574 /* submit packets to threads while there are no buffered results to return */
575
4/4
✓ Branch 32 → 33 taken 5306 times.
✓ Branch 32 → 34 taken 2480 times.
✓ Branch 33 → 6 taken 5263 times.
✓ Branch 33 → 34 taken 43 times.
7786 while (!fctx->df.nb_f && !fctx->result) {
576 PerThreadContext *p;
577
578
2/2
✓ Branch 6 → 7 taken 5223 times.
✓ Branch 6 → 9 taken 40 times.
5263 if (fctx->next_decoding != fctx->next_finished &&
579
2/2
✓ Branch 7 → 8 taken 8 times.
✓ Branch 7 → 9 taken 5215 times.
5223 (flags & AV_CODEC_RECEIVE_FRAME_FLAG_SYNCHRONOUS))
580 8 goto wait_for_result;
581
582 /* get a packet to be submitted to the next thread */
583 5255 av_packet_unref(fctx->next_pkt);
584 5255 ret = ff_decode_get_packet(avctx, fctx->next_pkt);
585
4/4
✓ Branch 11 → 12 taken 2746 times.
✓ Branch 11 → 14 taken 2509 times.
✓ Branch 12 → 13 taken 2657 times.
✓ Branch 12 → 14 taken 89 times.
5255 if (ret < 0 && ret != AVERROR_EOF)
586 2657 goto finish;
587
588 2598 ret = submit_packet(&fctx->threads[fctx->next_decoding], avctx,
589 fctx->next_pkt);
590
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 2598 times.
2598 if (ret < 0)
591 ✗ goto finish;
592
593 /* do not return any frames until all threads have something to do */
594
2/2
✓ Branch 17 → 18 taken 95 times.
✓ Branch 17 → 21 taken 2503 times.
2598 if (fctx->next_decoding != fctx->next_finished &&
595
2/2
✓ Branch 18 → 19 taken 12 times.
✓ Branch 18 → 20 taken 83 times.
95 !avctx->internal->draining)
596 83 continue;
597
598 2515 wait_for_result:
599 2523 p = &fctx->threads[fctx->next_finished];
600 2523 fctx->next_finished = (fctx->next_finished + 1) % avctx->thread_count;
601
602
2/2
✓ Branch 22 → 23 taken 243 times.
✓ Branch 22 → 28 taken 2280 times.
2523 if (atomic_load(&p->state) != STATE_INPUT_READY) {
603 243 pthread_mutex_lock(&p->progress_mutex);
604
2/2
✓ Branch 26 → 25 taken 243 times.
✓ Branch 26 → 27 taken 243 times.
486 while (atomic_load_explicit(&p->state, memory_order_relaxed) != STATE_INPUT_READY)
605 243 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
606 243 pthread_mutex_unlock(&p->progress_mutex);
607 }
608
609 2523 update_context_from_thread(avctx, p->avctx, 1);
610 2523 fctx->result = p->result;
611 2523 p->result = 0;
612
2/2
✓ Branch 29 → 30 taken 2478 times.
✓ Branch 29 → 31 taken 45 times.
2523 if (p->df.nb_f)
613 2478 FFSWAP(DecodedFrames, fctx->df, p->df);
614 }
615
616 /* a thread may return multiple frames AND an error
617 * we first return all the frames, then the error */
618
2/2
✓ Branch 34 → 35 taken 2480 times.
✓ Branch 34 → 37 taken 43 times.
2523 if (fctx->df.nb_f) {
619 2480 decoded_frames_pop(&fctx->df, frame);
620 2480 ret = 0;
621 } else {
622 43 ret = fctx->result;
623 43 fctx->result = 0;
624 }
625
626 5180 finish:
627 5180 async_lock(fctx);
628 5180 return ret;
629 }
630
631 299826 void ff_thread_report_progress(ThreadFrame *f, int n, int field)
632 {
633 PerThreadContext *p;
634
2/2
✓ Branch 2 → 3 taken 1240 times.
✓ Branch 2 → 4 taken 298586 times.
299826 atomic_int *progress = f->progress ? f->progress->progress : NULL;
635
636
2/2
✓ Branch 5 → 6 taken 1240 times.
✓ Branch 5 → 7 taken 298586 times.
299826 if (!progress ||
637
2/2
✓ Branch 6 → 7 taken 27 times.
✓ Branch 6 → 8 taken 1213 times.
1240 atomic_load_explicit(&progress[field], memory_order_relaxed) >= n)
638 298613 return;
639
640 1213 p = f->owner[field]->internal->thread_ctx;
641
642
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 1213 times.
1213 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
643 ✗ av_log(f->owner[field], AV_LOG_DEBUG,
644 "%p finished %d field %d\n", progress, n, field);
645
646 1213 pthread_mutex_lock(&p->progress_mutex);
647
648 1213 atomic_store_explicit(&progress[field], n, memory_order_release);
649
650 1213 pthread_cond_broadcast(&p->progress_cond);
651 1213 pthread_mutex_unlock(&p->progress_mutex);
652 }
653
654 150891 void ff_thread_await_progress(const ThreadFrame *f, int n, int field)
655 {
656 PerThreadContext *p;
657
2/2
✓ Branch 2 → 3 taken 150193 times.
✓ Branch 2 → 4 taken 698 times.
150891 atomic_int *progress = f->progress ? f->progress->progress : NULL;
658
659
2/2
✓ Branch 5 → 6 taken 150193 times.
✓ Branch 5 → 7 taken 698 times.
150891 if (!progress ||
660
2/2
✓ Branch 6 → 7 taken 149500 times.
✓ Branch 6 → 8 taken 693 times.
150193 atomic_load_explicit(&progress[field], memory_order_acquire) >= n)
661 150198 return;
662
663 693 p = f->owner[field]->internal->thread_ctx;
664
665
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 693 times.
693 if (atomic_load_explicit(&p->debug_threads, memory_order_relaxed))
666 ✗ av_log(f->owner[field], AV_LOG_DEBUG,
667 "thread awaiting %d field %d from %p\n", n, field, progress);
668
669 693 pthread_mutex_lock(&p->progress_mutex);
670
2/2
✓ Branch 13 → 12 taken 698 times.
✓ Branch 13 → 14 taken 693 times.
1391 while (atomic_load_explicit(&progress[field], memory_order_relaxed) < n)
671 698 pthread_cond_wait(&p->progress_cond, &p->progress_mutex);
672 693 pthread_mutex_unlock(&p->progress_mutex);
673 }
674
675 32879 void ff_thread_finish_setup(AVCodecContext *avctx) {
676 PerThreadContext *p;
677
678
2/2
✓ Branch 2 → 3 taken 30281 times.
✓ Branch 2 → 4 taken 2598 times.
32879 if (!(avctx->active_thread_type&FF_THREAD_FRAME)) return;
679
680 2598 p = avctx->internal->thread_ctx;
681
682
1/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 8 taken 2598 times.
2598 p->hwaccel_threadsafe = avctx->hwaccel &&
683
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ (ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_THREAD_SAFE);
684
685
1/4
✗ Branch 10 → 11 not taken.
✓ Branch 10 → 14 taken 2598 times.
✗ Branch 11 → 12 not taken.
✗ Branch 11 → 14 not taken.
2598 if (hwaccel_serial(avctx) && !p->hwaccel_serializing) {
686 ✗ pthread_mutex_lock(&p->parent->hwaccel_mutex);
687 ✗ p->hwaccel_serializing = 1;
688 }
689
690 /* this assumes that no hwaccel calls happen before ff_thread_finish_setup() */
691
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 18 taken 2598 times.
2598 if (avctx->hwaccel &&
692
0/2
✗ Branch 16 → 17 not taken.
✗ Branch 16 → 18 not taken.
✗ !(ffhwaccel(avctx->hwaccel)->caps_internal & HWACCEL_CAP_ASYNC_SAFE)) {
693 ✗ p->async_serializing = 1;
694
695 ✗ async_lock(p->parent);
696 }
697
698 /* thread-unsafe hwaccels share a single private data instance, so we
699 * save hwaccel state for passing to the next thread;
700 * this is done here so that this worker thread can wipe its own hwaccel
701 * state after decoding, without requiring synchronization */
702
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 2598 times.
2598 av_assert0(!p->parent->stash_hwaccel);
703
1/2
✗ Branch 22 → 23 not taken.
✓ Branch 22 → 24 taken 2598 times.
2598 if (hwaccel_serial(avctx)) {
704 ✗ p->parent->stash_hwaccel = avctx->hwaccel;
705 ✗ p->parent->stash_hwaccel_context = avctx->hwaccel_context;
706 ✗ p->parent->stash_hwaccel_priv = avctx->internal->hwaccel_priv_data;
707 }
708
709 2598 pthread_mutex_lock(&p->progress_mutex);
710
1/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 27 taken 2598 times.
2598 if(atomic_load(&p->state) == STATE_SETUP_FINISHED){
711 ✗ av_log(avctx, AV_LOG_WARNING, "Multiple ff_thread_finish_setup() calls\n");
712 }
713
714 2598 atomic_store(&p->state, STATE_SETUP_FINISHED);
715
716 2598 pthread_cond_broadcast(&p->progress_cond);
717 2598 pthread_mutex_unlock(&p->progress_mutex);
718 }
719
720 /// Waits for all threads to finish.
721 16 static av_cold void park_frame_worker_threads(FrameThreadContext *fctx, int thread_count)
722 {
723 int i;
724
725 16 async_unlock(fctx);
726
727
2/2
✓ Branch 11 → 4 taken 97 times.
✓ Branch 11 → 12 taken 16 times.
113 for (i = 0; i < thread_count; i++) {
728 97 PerThreadContext *p = &fctx->threads[i];
729
730
2/2
✓ Branch 4 → 5 taken 11 times.
✓ Branch 4 → 10 taken 86 times.
97 if (atomic_load(&p->state) != STATE_INPUT_READY) {
731 11 pthread_mutex_lock(&p->progress_mutex);
732
2/2
✓ Branch 8 → 7 taken 11 times.
✓ Branch 8 → 9 taken 11 times.
22 while (atomic_load(&p->state) != STATE_INPUT_READY)
733 11 pthread_cond_wait(&p->output_cond, &p->progress_mutex);
734 11 pthread_mutex_unlock(&p->progress_mutex);
735 }
736 }
737
738 16 async_lock(fctx);
739 16 }
740
741 #define OFF(member) offsetof(FrameThreadContext, member)
742 DEFINE_OFFSET_ARRAY(FrameThreadContext, thread_ctx, pthread_init_cnt,
743 (OFF(buffer_mutex), OFF(hwaccel_mutex), OFF(async_mutex)),
744 (OFF(async_cond)));
745 #undef OFF
746
747 #define OFF(member) offsetof(PerThreadContext, member)
748 DEFINE_OFFSET_ARRAY(PerThreadContext, per_thread, pthread_init_cnt,
749 (OFF(progress_mutex), OFF(mutex)),
750 (OFF(input_cond), OFF(progress_cond), OFF(output_cond)));
751 #undef OFF
752
753 16 av_cold void ff_frame_thread_free(AVCodecContext *avctx, int thread_count)
754 {
755 16 FrameThreadContext *fctx = avctx->internal->thread_ctx;
756 16 const FFCodec *codec = ffcodec(avctx->codec);
757 int i;
758
759 16 park_frame_worker_threads(fctx, thread_count);
760
761
2/2
✓ Branch 32 → 5 taken 97 times.
✓ Branch 32 → 33 taken 16 times.
113 for (i = 0; i < thread_count; i++) {
762 97 PerThreadContext *p = &fctx->threads[i];
763 97 AVCodecContext *ctx = p->avctx;
764
765
1/2
✓ Branch 5 → 6 taken 97 times.
✗ Branch 5 → 26 not taken.
97 if (ctx->internal) {
766
1/2
✓ Branch 6 → 7 taken 97 times.
✗ Branch 6 → 11 not taken.
97 if (p->thread_init == INITIALIZED) {
767 97 pthread_mutex_lock(&p->mutex);
768 97 p->die = 1;
769 97 pthread_cond_signal(&p->input_cond);
770 97 pthread_mutex_unlock(&p->mutex);
771
772 97 pthread_join(p->thread, NULL);
773 }
774
2/4
✓ Branch 11 → 12 taken 97 times.
✗ Branch 11 → 14 not taken.
✓ Branch 12 → 13 taken 97 times.
✗ Branch 12 → 14 not taken.
97 if (codec->close && p->thread_init != UNINITIALIZED)
775 97 codec->close(ctx);
776
777 /* When using a threadsafe hwaccel, this is where
778 * each thread's context is uninit'd and freed. */
779 97 ff_hwaccel_uninit(ctx);
780
781
1/2
✓ Branch 15 → 16 taken 97 times.
✗ Branch 15 → 19 not taken.
97 if (ctx->priv_data) {
782
2/2
✓ Branch 16 → 17 taken 75 times.
✓ Branch 16 → 18 taken 22 times.
97 if (codec->p.priv_class)
783 75 av_opt_free(ctx->priv_data);
784 97 av_freep(&ctx->priv_data);
785 }
786
787 97 av_refstruct_unref(&ctx->internal->pool);
788 97 av_packet_free(&ctx->internal->in_pkt);
789 97 av_packet_free(&ctx->internal->last_pkt_props);
790 97 ff_decode_internal_uninit(ctx);
791 97 av_freep(&ctx->internal);
792 97 av_buffer_unref(&ctx->hw_frames_ctx);
793 97 av_frame_side_data_free(&ctx->decoded_side_data,
794 &ctx->nb_decoded_side_data);
795 }
796
797 97 decoded_frames_free(&p->df);
798
799 97 ff_pthread_free(p, per_thread_offsets);
800 97 av_packet_free(&p->avpkt);
801
802 97 av_channel_layout_uninit(&ctx->ch_layout);
803 97 av_freep(&p->avctx);
804 }
805
806 16 decoded_frames_free(&fctx->df);
807 16 av_packet_free(&fctx->next_pkt);
808
809 16 av_freep(&fctx->threads);
810 16 ff_pthread_free(fctx, thread_ctx_offsets);
811
812 /* if we have stashed hwaccel state, move it to the user-facing context,
813 * so it will be freed in ff_codec_close() */
814
1/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 16 times.
16 av_assert0(!avctx->hwaccel);
815 16 FFSWAP(const AVHWAccel*, avctx->hwaccel, fctx->stash_hwaccel);
816 16 FFSWAP(void*, avctx->hwaccel_context, fctx->stash_hwaccel_context);
817 16 FFSWAP(void*, avctx->internal->hwaccel_priv_data, fctx->stash_hwaccel_priv);
818
819 16 av_freep(&avctx->internal->thread_ctx);
820 16 }
821
822 97 static av_cold int init_thread(PerThreadContext *p, int *threads_to_free,
823 FrameThreadContext *fctx, AVCodecContext *avctx,
824 const FFCodec *codec, int first)
825 {
826 AVCodecContext *copy;
827 int err;
828
829 97 atomic_init(&p->state, STATE_INPUT_READY);
830
831 97 copy = av_memdup(avctx, sizeof(*avctx));
832
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 97 times.
97 if (!copy)
833 ✗ return AVERROR(ENOMEM);
834 97 copy->internal = NULL;
835 97 copy->priv_data = NULL;
836 97 copy->decoded_side_data = NULL;
837 97 copy->nb_decoded_side_data = 0;
838 97 copy->ch_layout = (AVChannelLayout){ 0 };
839
840 /* From now on, this PerThreadContext will be cleaned up by
841 * ff_frame_thread_free in case of errors. */
842 97 (*threads_to_free)++;
843
844 97 p->parent = fctx;
845 97 p->avctx = copy;
846
847 97 err = av_channel_layout_copy(&copy->ch_layout, &avctx->ch_layout);
848
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 97 times.
97 if (err < 0)
849 ✗ return err;
850
851 97 copy->internal = ff_decode_internal_alloc();
852
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 97 times.
97 if (!copy->internal)
853 ✗ return AVERROR(ENOMEM);
854 97 ff_decode_internal_sync(copy, avctx);
855 97 copy->internal->thread_ctx = p;
856 97 copy->internal->progress_frame_pool = avctx->internal->progress_frame_pool;
857
858 97 copy->delay = avctx->delay;
859
860
1/2
✓ Branch 12 → 13 taken 97 times.
✗ Branch 12 → 20 not taken.
97 if (codec->priv_data_size) {
861 97 copy->priv_data = av_mallocz(codec->priv_data_size);
862
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 16 taken 97 times.
97 if (!copy->priv_data)
863 ✗ return AVERROR(ENOMEM);
864
865
2/2
✓ Branch 16 → 17 taken 75 times.
✓ Branch 16 → 20 taken 22 times.
97 if (codec->p.priv_class) {
866 75 *(const AVClass **)copy->priv_data = codec->p.priv_class;
867 75 err = av_opt_copy(copy->priv_data, avctx->priv_data);
868
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 20 taken 75 times.
75 if (err < 0)
869 ✗ return err;
870 }
871 }
872
873 97 err = ff_pthread_init(p, per_thread_offsets);
874
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 23 taken 97 times.
97 if (err < 0)
875 ✗ return err;
876
877
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 97 times.
97 if (!(p->avpkt = av_packet_alloc()))
878 ✗ return AVERROR(ENOMEM);
879
880 97 copy->internal->is_frame_mt = 1;
881
2/2
✓ Branch 26 → 27 taken 81 times.
✓ Branch 26 → 28 taken 16 times.
97 if (!first)
882 81 copy->internal->is_copy = 1;
883
884 97 copy->internal->in_pkt = av_packet_alloc();
885
1/2
✗ Branch 29 → 30 not taken.
✓ Branch 29 → 31 taken 97 times.
97 if (!copy->internal->in_pkt)
886 ✗ return AVERROR(ENOMEM);
887
888 97 copy->internal->last_pkt_props = av_packet_alloc();
889
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 34 taken 97 times.
97 if (!copy->internal->last_pkt_props)
890 ✗ return AVERROR(ENOMEM);
891
892
1/2
✓ Branch 34 → 35 taken 97 times.
✗ Branch 34 → 40 not taken.
97 if (codec->init) {
893 97 err = codec->init(copy);
894
1/2
✗ Branch 36 → 37 not taken.
✓ Branch 36 → 40 taken 97 times.
97 if (err < 0) {
895
0/2
✗ Branch 37 → 38 not taken.
✗ Branch 37 → 39 not taken.
✗ if (codec->caps_internal & FF_CODEC_CAP_INIT_CLEANUP)
896 ✗ p->thread_init = NEEDS_CLOSE;
897 ✗ return err;
898 }
899 }
900 97 p->thread_init = NEEDS_CLOSE;
901
902
2/2
✓ Branch 40 → 41 taken 16 times.
✓ Branch 40 → 44 taken 81 times.
97 if (first) {
903 16 err = update_context_from_thread(avctx, copy, 1);
904
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 44 taken 16 times.
16 if (err < 0)
905 ✗ return err;
906 }
907
908
2/2
✓ Branch 44 → 45 taken 16 times.
✓ Branch 44 → 46 taken 81 times.
97 const AVCodecContext *src = first ? copy : avctx;
909
2/2
✓ Branch 47 → 48 taken 16 times.
✓ Branch 47 → 49 taken 81 times.
97 AVCodecContext *dst = first ? avctx : copy;
910 97 av_frame_side_data_free(&dst->decoded_side_data, &dst->nb_decoded_side_data);
911
1/2
✗ Branch 56 → 52 not taken.
✓ Branch 56 → 57 taken 97 times.
97 for (int i = 0; i < src->nb_decoded_side_data; i++) {
912 ✗ err = av_frame_side_data_clone(&dst->decoded_side_data,
913 &dst->nb_decoded_side_data,
914 ✗ src->decoded_side_data[i], 0);
915
0/2
✗ Branch 53 → 54 not taken.
✗ Branch 53 → 55 not taken.
✗ if (err < 0)
916 ✗ return err;
917 }
918
919 97 atomic_init(&p->debug_threads, (copy->debug & FF_DEBUG_THREADS) != 0);
920
921 97 err = AVERROR(pthread_create(&p->thread, NULL, frame_worker_thread, p));
922
1/2
✗ Branch 58 → 59 not taken.
✓ Branch 58 → 60 taken 97 times.
97 if (err < 0)
923 ✗ return err;
924 97 p->thread_init = INITIALIZED;
925
926 97 return 0;
927 }
928
929 16 av_cold int ff_frame_thread_init(AVCodecContext *avctx)
930 {
931 16 int thread_count = avctx->thread_count;
932 16 const FFCodec *codec = ffcodec(avctx->codec);
933 FrameThreadContext *fctx;
934 16 int err, i = 0;
935
936
2/2
✓ Branch 3 → 4 taken 1 time.
✓ Branch 3 → 8 taken 15 times.
16 if (!thread_count) {
937 1 int nb_cpus = av_cpu_count();
938 // use number of cores + 1 as thread count if there is more than one
939
1/2
✓ Branch 5 → 6 taken 1 time.
✗ Branch 5 → 7 not taken.
1 if (nb_cpus > 1)
940 1 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
941 else
942 ✗ thread_count = avctx->thread_count = 1;
943 }
944
945
1/2
✗ Branch 8 → 9 not taken.
✓ Branch 8 → 10 taken 16 times.
16 if (thread_count <= 1) {
946 ✗ avctx->active_thread_type = 0;
947 ✗ return 0;
948 }
949
950 16 avctx->internal->thread_ctx = fctx = av_mallocz(sizeof(FrameThreadContext));
951
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 13 taken 16 times.
16 if (!fctx)
952 ✗ return AVERROR(ENOMEM);
953
954 16 err = ff_pthread_init(fctx, thread_ctx_offsets);
955
1/2
✗ Branch 14 → 15 not taken.
✓ Branch 14 → 18 taken 16 times.
16 if (err < 0) {
956 ✗ ff_pthread_free(fctx, thread_ctx_offsets);
957 ✗ av_freep(&avctx->internal->thread_ctx);
958 ✗ return err;
959 }
960
961 16 fctx->next_pkt = av_packet_alloc();
962
1/2
✗ Branch 19 → 20 not taken.
✓ Branch 19 → 21 taken 16 times.
16 if (!fctx->next_pkt)
963 ✗ return AVERROR(ENOMEM);
964
965 16 fctx->async_lock = 1;
966
967
2/2
✓ Branch 21 → 22 taken 8 times.
✓ Branch 21 → 23 taken 8 times.
16 if (codec->p.type == AVMEDIA_TYPE_VIDEO)
968 8 avctx->delay = avctx->thread_count - 1;
969
970 16 fctx->threads = av_calloc(thread_count, sizeof(*fctx->threads));
971
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 26 taken 16 times.
16 if (!fctx->threads) {
972 ✗ err = AVERROR(ENOMEM);
973 ✗ goto error;
974 }
975
976
2/2
✓ Branch 31 → 27 taken 97 times.
✓ Branch 31 → 32 taken 16 times.
113 for (; i < thread_count; ) {
977 97 PerThreadContext *p = &fctx->threads[i];
978 97 int first = !i;
979
980 97 err = init_thread(p, &i, fctx, avctx, codec, first);
981
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 30 taken 97 times.
97 if (err < 0)
982 ✗ goto error;
983 }
984
985 16 return 0;
986
987 ✗ error:
988 ✗ ff_frame_thread_free(avctx, i);
989 ✗ return err;
990 }
991
992 ✗ av_cold void ff_thread_flush(AVCodecContext *avctx)
993 {
994 int i;
995 ✗ FrameThreadContext *fctx = avctx->internal->thread_ctx;
996
997
0/2
✗ Branch 2 → 3 not taken.
✗ Branch 2 → 4 not taken.
✗ if (!fctx) return;
998
999 ✗ park_frame_worker_threads(fctx, avctx->thread_count);
1000
0/2
✗ Branch 5 → 6 not taken.
✗ Branch 5 → 8 not taken.
✗ if (fctx->prev_thread) {
1001
0/2
✗ Branch 6 → 7 not taken.
✗ Branch 6 → 8 not taken.
✗ if (fctx->prev_thread != &fctx->threads[0])
1002 ✗ update_context_from_thread(fctx->threads[0].avctx, fctx->prev_thread->avctx, 0);
1003 }
1004
1005 ✗ fctx->next_decoding = fctx->next_finished = 0;
1006 ✗ fctx->prev_thread = NULL;
1007
1008 ✗ decoded_frames_flush(&fctx->df);
1009 ✗ fctx->result = 0;
1010
1011
0/2
✗ Branch 13 → 10 not taken.
✗ Branch 13 → 14 not taken.
✗ for (i = 0; i < avctx->thread_count; i++) {
1012 ✗ PerThreadContext *p = &fctx->threads[i];
1013
1014 ✗ decoded_frames_flush(&p->df);
1015 ✗ p->result = 0;
1016
1017 ✗ avcodec_flush_buffers(p->avctx);
1018 }
1019 }
1020
1021 46769 int ff_thread_can_start_frame(AVCodecContext *avctx)
1022 {
1023
2/2
✓ Branch 2 → 3 taken 2317 times.
✓ Branch 2 → 7 taken 44452 times.
46769 if ((avctx->active_thread_type & FF_THREAD_FRAME) &&
1024
1/2
✓ Branch 4 → 5 taken 2317 times.
✗ Branch 4 → 7 not taken.
2317 ffcodec(avctx->codec)->update_thread_context) {
1025 2317 PerThreadContext *p = avctx->internal->thread_ctx;
1026
1027
1/2
✗ Branch 5 → 6 not taken.
✓ Branch 5 → 7 taken 2317 times.
2317 if (atomic_load(&p->state) != STATE_SETTING_UP)
1028 ✗ return 0;
1029 }
1030
1031 46769 return 1;
1032 }
1033
1034 57687 static int thread_get_buffer_internal(AVCodecContext *avctx, AVFrame *f, int flags)
1035 {
1036 PerThreadContext *p;
1037 int err;
1038
1039
2/2
✓ Branch 2 → 3 taken 55206 times.
✓ Branch 2 → 5 taken 2481 times.
57687 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1040 55206 return ff_get_buffer(avctx, f, flags);
1041
1042 2481 p = avctx->internal->thread_ctx;
1043
2/2
✓ Branch 5 → 6 taken 150 times.
✓ Branch 5 → 10 taken 2331 times.
2481 if (atomic_load(&p->state) != STATE_SETTING_UP &&
1044
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 150 times.
150 ffcodec(avctx->codec)->update_thread_context) {
1045 ✗ av_log(avctx, AV_LOG_ERROR, "get_buffer() cannot be called after ff_thread_finish_setup()\n");
1046 ✗ return -1;
1047 }
1048
1049 2481 pthread_mutex_lock(&p->parent->buffer_mutex);
1050 2481 err = ff_get_buffer(avctx, f, flags);
1051
1052 2481 pthread_mutex_unlock(&p->parent->buffer_mutex);
1053
1054 2481 return err;
1055 }
1056
1057 57687 int ff_thread_get_buffer(AVCodecContext *avctx, AVFrame *f, int flags)
1058 {
1059 57687 int ret = thread_get_buffer_internal(avctx, f, flags);
1060
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 57687 times.
57687 if (ret < 0)
1061 ✗ av_log(avctx, AV_LOG_ERROR, "thread_get_buffer() failed\n");
1062 57687 return ret;
1063 }
1064
1065 26770 int ff_thread_get_ext_buffer(AVCodecContext *avctx, ThreadFrame *f, int flags)
1066 {
1067 int ret;
1068
1069 26770 f->owner[0] = f->owner[1] = avctx;
1070
2/2
✓ Branch 2 → 3 taken 26728 times.
✓ Branch 2 → 5 taken 42 times.
26770 if (!(avctx->active_thread_type & FF_THREAD_FRAME))
1071 26728 return ff_get_buffer(avctx, f->f, flags);
1072
1073 42 f->progress = av_refstruct_allocz(sizeof(*f->progress));
1074
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 42 times.
42 if (!f->progress)
1075 ✗ return AVERROR(ENOMEM);
1076
1077 42 atomic_init(&f->progress->progress[0], -1);
1078 42 atomic_init(&f->progress->progress[1], -1);
1079
1080 42 ret = ff_thread_get_buffer(avctx, f->f, flags);
1081
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 42 times.
42 if (ret)
1082 ✗ av_refstruct_unref(&f->progress);
1083 42 return ret;
1084 }
1085
1086 55575 void ff_thread_release_ext_buffer(ThreadFrame *f)
1087 {
1088 55575 av_refstruct_unref(&f->progress);
1089 55575 f->owner[0] = f->owner[1] = NULL;
1090
1/2
✓ Branch 3 → 4 taken 55575 times.
✗ Branch 3 → 5 not taken.
55575 if (f->f)
1091 55575 av_frame_unref(f->f);
1092 55575 }
1093
1094 854 av_cold enum ThreadingStatus ff_thread_sync_ref(AVCodecContext *avctx, size_t offset)
1095 {
1096 PerThreadContext *p;
1097 const void *ref;
1098
1099
2/2
✓ Branch 2 → 3 taken 835 times.
✓ Branch 2 → 7 taken 19 times.
854 if (!avctx->internal->is_copy)
1100 835 return avctx->active_thread_type & FF_THREAD_FRAME ?
1101
2/2
✓ Branch 3 → 4 taken 3 times.
✓ Branch 3 → 5 taken 832 times.
835 FF_THREAD_IS_FIRST_THREAD : FF_THREAD_NO_FRAME_THREADING;
1102
1103 19 p = avctx->internal->thread_ctx;
1104
1105 av_assert1(memcpy(&ref, (char*)avctx->priv_data + offset, sizeof(ref)) && ref == NULL);
1106
1107 19 memcpy(&ref, (const char*)p->parent->threads[0].avctx->priv_data + offset, sizeof(ref));
1108 av_assert1(ref);
1109 19 av_refstruct_replace((char*)avctx->priv_data + offset, ref);
1110
1111 19 return FF_THREAD_IS_COPY;
1112 }
1113
1114 4990 int ff_thread_get_packet(AVCodecContext *avctx, AVPacket *pkt)
1115 {
1116 4990 PerThreadContext *p = avctx->internal->thread_ctx;
1117
1118
3/4
✓ Branch 2 → 3 taken 2481 times.
✓ Branch 2 → 4 taken 2509 times.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 2481 times.
4990 if (!AVPACKET_IS_EMPTY(p->avpkt)) {
1119 2509 av_packet_move_ref(pkt, p->avpkt);
1120 2509 return 0;
1121 }
1122
1123
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 8 taken 2481 times.
2481 return avctx->internal->draining ? AVERROR_EOF : AVERROR(EAGAIN);
1124 }
1125