FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavcodec/pthread_slice.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 48 65 73.8%
Functions: 5 7 71.4%
Branches: 17 24 70.8%

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 * 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 static void main_function(void *priv) {
52 AVCodecContext *avctx = priv;
53 SliceThreadContext *c = avctx->internal->thread_ctx;
54 c->mainfunc(avctx);
55 }
56
57 14132 static void worker_func(void *priv, int jobnr, int threadnr, int nb_jobs, int nb_threads)
58 {
59 14132 AVCodecContext *avctx = priv;
60 14132 SliceThreadContext *c = avctx->internal->thread_ctx;
61 int ret;
62
63 13432 ret = c->func ? c->func(avctx, (char *)c->args + c->job_size * jobnr)
64
2/2
✓ Branch 0 taken 13432 times.
✓ Branch 1 taken 700 times.
14132 : c->func2(avctx, c->args, jobnr, threadnr);
65
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14132 times.
14132 if (c->rets)
66 c->rets[jobnr] = ret;
67 14132 }
68
69 25 av_cold void ff_slice_thread_free(AVCodecContext *avctx)
70 {
71 25 SliceThreadContext *c = avctx->internal->thread_ctx;
72
73 25 avpriv_slicethread_free(&c->thread);
74
75 25 av_freep(&avctx->internal->thread_ctx);
76 25 }
77
78 2979 static int thread_execute(AVCodecContext *avctx, action_func* func, void *arg, int *ret, int job_count, int job_size)
79 {
80 2979 SliceThreadContext *c = avctx->internal->thread_ctx;
81
82
2/4
✓ Branch 0 taken 2979 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2979 times.
2979 if (!(avctx->active_thread_type&FF_THREAD_SLICE) || avctx->thread_count <= 1)
83 return avcodec_default_execute(avctx, func, arg, ret, job_count, job_size);
84
85
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2974 times.
2979 if (job_count <= 0)
86 5 return 0;
87
88 2974 c->job_size = job_size;
89 2974 c->args = arg;
90 2974 c->func = func;
91 2974 c->rets = ret;
92
93 2974 avpriv_slicethread_execute(c->thread, job_count, !!c->mainfunc );
94 2974 return 0;
95 }
96
97 100 static int thread_execute2(AVCodecContext *avctx, action_func2* func2, void *arg, int *ret, int job_count)
98 {
99 100 SliceThreadContext *c = avctx->internal->thread_ctx;
100 100 c->func2 = func2;
101 100 return thread_execute(avctx, NULL, arg, ret, job_count, 0);
102 }
103
104 int ff_slice_thread_execute_with_mainfunc(AVCodecContext *avctx, action_func2* func2, main_func *mainfunc, void *arg, int *ret, int job_count)
105 {
106 SliceThreadContext *c = avctx->internal->thread_ctx;
107 c->func2 = func2;
108 c->mainfunc = mainfunc;
109 return thread_execute(avctx, NULL, arg, ret, job_count, 0);
110 }
111
112 28 av_cold int ff_slice_thread_init(AVCodecContext *avctx)
113 {
114 SliceThreadContext *c;
115 28 int thread_count = avctx->thread_count;
116 void (*mainfunc)(void *);
117
118
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 23 times.
28 if (!thread_count) {
119 5 int nb_cpus = av_cpu_count();
120
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 if (avctx->height)
121 5 nb_cpus = FFMIN(nb_cpus, (avctx->height+15)/16);
122 // use number of cores + 1 as thread count if there is more than one
123
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 3 times.
5 if (nb_cpus > 1)
124 2 thread_count = avctx->thread_count = FFMIN(nb_cpus + 1, MAX_AUTO_THREADS);
125 else
126 3 thread_count = avctx->thread_count = 1;
127 }
128
129
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 25 times.
28 if (thread_count <= 1) {
130 3 avctx->active_thread_type = 0;
131 3 return 0;
132 }
133
134 25 avctx->internal->thread_ctx = c = av_mallocz(sizeof(*c));
135
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (!c)
136 return AVERROR(ENOMEM);
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 25 times.
25 mainfunc = ffcodec(avctx->codec)->caps_internal & FF_CODEC_CAP_SLICE_THREAD_HAS_MF ? &main_function : NULL;
138 25 thread_count = avpriv_slicethread_create(&c->thread, avctx, worker_func,
139 mainfunc, thread_count);
140
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 25 times.
25 if (thread_count <= 1) {
141 ff_slice_thread_free(avctx);
142 avctx->thread_count = 1;
143 avctx->active_thread_type = 0;
144 return thread_count < 0 ? thread_count : 0;
145 }
146 25 avctx->thread_count = thread_count;
147
148 25 avctx->execute = thread_execute;
149 25 avctx->execute2 = thread_execute2;
150 25 return 0;
151 }
152