Coverage Report

Created: 2026-01-25 07:18

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