FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread_frame.c
Date: 2022-07-04 00:18:54
Exec Total Coverage
Lines: 364 533 68.3%
Branches: 180 325 55.4%

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