Coverage Report

Created: 2026-08-25 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/pthread_slice.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
 * Slice multithreading support functions
22
 * @see doc/multithreading.txt
23
 */
24
25
#include "avcodec.h"
26
#include "codec_internal.h"
27
#include "internal.h"
28
#include "pthread_internal.h"
29
#include "thread.h"
30
31
#include "libavutil/attributes.h"
32
#include "libavutil/cpu.h"
33
#include "libavutil/macros.h"
34
#include "libavutil/mem.h"
35
#include "libavutil/slicethread.h"
36
37
typedef int (action_func)(AVCodecContext *c, void *arg);
38
typedef int (action_func2)(AVCodecContext *c, void *arg, int jobnr, int threadnr);
39
typedef int (main_func)(AVCodecContext *c);
40
41
typedef struct SliceThreadContext {
42
    AVSliceThread *thread;
43
    action_func *func;
44
    action_func2 *func2;
45
    main_func *mainfunc;
46
    void *args;
47
    int *rets;
48
    int job_size;
49
} SliceThreadContext;
50
51
0
static int main_function(void *priv) {
52
0
    AVCodecContext *avctx = priv;
53
0
    SliceThreadContext *c = avctx->internal->thread_ctx;
54
0
    c->mainfunc(avctx);
55
0
    return 0;
56
0
}
57
58
static int worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
59
0
{
60
0
    AVCodecContext *avctx = priv;
61
0
    SliceThreadContext *c = avctx->internal->thread_ctx;
62
0
    int ret;
63
64
0
    ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
65
0
                  : c->func2(avctx, c->args, jobnr, threadnr);
66
0
    if (c->rets)
67
0
        c->rets[jobnr] = ret;
68
69
0
    return 0;
70
0
}
71
72
av_cold void ff_slice_thread_free(AVCodecContext *avctx)
73
0
{
74
0
    SliceThreadContext *c = avctx->internal->thread_ctx;
75
76
0
    avpriv_slicethread_free(&c->thread);
77
78
0
    av_freep(&avctx->internal->thread_ctx);
79
0
}
80
81
static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
82
0
{
83
0
    SliceThreadContext *c = avctx->internal->thread_ctx;
84
85
0
    if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
86
0
        return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
87
88
0
    if (job_count <= 0)
89
0
        return 0;
90
91
0
    c->job_size = job_size;
92
0
    c->args = arg;
93
0
    c->func = func;
94
0
    c->rets = ret;
95
96
0
    avpriv_slicethread_execute2(c->thread, job_count, !!c->mainfunc  );
97
0
    return 0;
98
0
}
99
100
static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
101
0
{
102
0
    SliceThreadContext *c = avctx->internal->thread_ctx;
103
0
    c->func2 = func2;
104
0
    return thread_execute(avctx, NULL, arg, ret, job_count, 0);
105
0
}
106
107
int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count)
108
0
{
109
0
    SliceThreadContext *c = avctx->internal->thread_ctx;
110
0
    c->func2 = func2;
111
0
    c->mainfunc = mainfunc;
112
0
    return thread_execute(avctx, NULL, arg, ret, job_count, 0);
113
0
}
114
115
av_cold int ff_slice_thread_init(AVCodecContext *avctx)
116
0
{
117
0
    SliceThreadContext *c;
118
0
    int thread_count = avctx->thread_count;
119
0
    int (*mainfunc)(void *);
120
121
0
    if (!thread_count) {
122
0
        int nb_cpus = av_cpu_count();
123
0
        if  (avctx->height)
124
0
            nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
125
        // use number of cores + 1 as thread count if there is more than one
126
0
        if (nb_cpus > 1)
127
0
            thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
128
0
        else
129
0
            thread_count = avctx->thread_count = 1;
130
0
    }
131
132
0
    if (thread_count <= 1) {
133
0
        avctx->active_thread_type = 0;
134
0
        return 0;
135
0
    }
136
137
0
    avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
138
0
    if (!c)
139
0
        return AVERROR(ENOMEM);
140
0
    mainfunc = ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF ? &main_function : NULL;
141
0
    thread_count = avpriv_slicethread_create2(&c->thread, avctx, worker_func,
142
0
                                              mainfunc, thread_count);
143
0
    if (thread_count <= 1) {
144
0
        ff_slice_thread_free(avctx);
145
0
        avctx->thread_count = 1;
146
0
        avctx->active_thread_type = 0;
147
0
        return thread_count < 0 ? thread_count : 0;
148
0
    }
149
0
    avctx->thread_count = thread_count;
150
151
0
    avctx->execute = thread_execute;
152
0
    avctx->execute2 = thread_execute2;
153
0
    return 0;
154
0
}