FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread_frame.c
Date: 2021-09-23 20:34:37
Exec Total Coverage
Lines: 362 529 68.4%
Branches: 185 329 56.2%

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