FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/cpu.c
Date: 2024-02-16 17:37:06
Exec Total Coverage
Lines: 30 37 81.1%
Functions: 6 7 85.7%
Branches: 9 12 75.0%

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 #include "config.h"
20
21 #if HAVE_SCHED_GETAFFINITY
22 #ifndef _GNU_SOURCE
23 # define _GNU_SOURCE
24 #endif
25 #include <sched.h>
26 #endif
27
28 #include <stddef.h>
29 #include <stdint.h>
30 #include <stdatomic.h>
31
32 #include "attributes.h"
33 #include "cpu.h"
34 #include "cpu_internal.h"
35 #include "opt.h"
36 #include "common.h"
37
38 #if HAVE_GETPROCESSAFFINITYMASK || HAVE_WINRT
39 #include <windows.h>
40 #endif
41 #if HAVE_SYSCTL
42 #if HAVE_SYS_PARAM_H
43 #include <sys/param.h>
44 #endif
45 #include <sys/types.h>
46 #include <sys/sysctl.h>
47 #endif
48 #if HAVE_UNISTD_H
49 #include <unistd.h>
50 #endif
51
52 static atomic_int cpu_flags = ATOMIC_VAR_INIT(-1);
53 static atomic_int cpu_count = ATOMIC_VAR_INIT(-1);
54
55 8078 static int get_cpu_flags(void)
56 {
57 #if ARCH_MIPS
58 return ff_get_cpu_flags_mips();
59 #elif ARCH_AARCH64
60 return ff_get_cpu_flags_aarch64();
61 #elif ARCH_ARM
62 return ff_get_cpu_flags_arm();
63 #elif ARCH_PPC
64 return ff_get_cpu_flags_ppc();
65 #elif ARCH_RISCV
66 return ff_get_cpu_flags_riscv();
67 #elif ARCH_X86
68 8078 return ff_get_cpu_flags_x86();
69 #elif ARCH_LOONGARCH
70 return ff_get_cpu_flags_loongarch();
71 #endif
72 return 0;
73 }
74
75 8957 void av_force_cpu_flags(int arg){
76 8957 if (ARCH_X86 &&
77
2/2
✓ Branch 0 taken 2125 times.
✓ Branch 1 taken 6832 times.
8957 (arg & ( AV_CPU_FLAG_3DNOW |
78 AV_CPU_FLAG_3DNOWEXT |
79 AV_CPU_FLAG_MMXEXT |
80 AV_CPU_FLAG_SSE |
81 AV_CPU_FLAG_SSE2 |
82 AV_CPU_FLAG_SSE2SLOW |
83 AV_CPU_FLAG_SSE3 |
84 AV_CPU_FLAG_SSE3SLOW |
85 AV_CPU_FLAG_SSSE3 |
86 AV_CPU_FLAG_SSE4 |
87 AV_CPU_FLAG_SSE42 |
88 AV_CPU_FLAG_AVX |
89 AV_CPU_FLAG_AVXSLOW |
90 AV_CPU_FLAG_XOP |
91 AV_CPU_FLAG_FMA3 |
92 AV_CPU_FLAG_FMA4 |
93 AV_CPU_FLAG_AVX2 |
94 AV_CPU_FLAG_AVX512 ))
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2125 times.
2125 && !(arg & AV_CPU_FLAG_MMX)) {
96 av_log(NULL, AV_LOG_WARNING, "MMX implied by specified flags\n");
97 arg |= AV_CPU_FLAG_MMX;
98 }
99
100 8957 atomic_store_explicit(&cpu_flags, arg, memory_order_relaxed);
101 8957 }
102
103 512393 int av_get_cpu_flags(void)
104 {
105 512393 int flags = atomic_load_explicit(&cpu_flags, memory_order_relaxed);
106
2/2
✓ Branch 0 taken 8078 times.
✓ Branch 1 taken 504315 times.
512393 if (flags == -1) {
107 8078 flags = get_cpu_flags();
108 8078 atomic_store_explicit(&cpu_flags, flags, memory_order_relaxed);
109 }
110 512393 return flags;
111 }
112
113 6741 int av_parse_cpu_caps(unsigned *flags, const char *s)
114 {
115 static const AVOption cpuflags_opts[] = {
116 { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
117 #if ARCH_PPC
118 { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
119 #elif ARCH_X86
120 { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
121 { "mmx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2 }, .unit = "flags" },
122 { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2 }, .unit = "flags" },
123 { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE }, .unit = "flags" },
124 { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2 }, .unit = "flags" },
125 { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2SLOW }, .unit = "flags" },
126 { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3 }, .unit = "flags" },
127 { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3SLOW }, .unit = "flags" },
128 { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSSE3 }, .unit = "flags" },
129 { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
130 { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE4 }, .unit = "flags" },
131 { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE42 }, .unit = "flags" },
132 { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX }, .unit = "flags" },
133 { "avxslow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVXSLOW }, .unit = "flags" },
134 { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_XOP }, .unit = "flags" },
135 { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA3 }, .unit = "flags" },
136 { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA4 }, .unit = "flags" },
137 { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX2 }, .unit = "flags" },
138 { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI1 }, .unit = "flags" },
139 { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI2 }, .unit = "flags" },
140 { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOW }, .unit = "flags" },
141 { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOWEXT }, .unit = "flags" },
142 { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
143 { "aesni", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AESNI }, .unit = "flags" },
144 { "avx512" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX512 }, .unit = "flags" },
145 { "avx512icl", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX512ICL }, .unit = "flags" },
146 { "slowgather", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SLOW_GATHER }, .unit = "flags" },
147
148 #define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
149 #define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
150 #define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
151 { "pentium2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P2 }, .unit = "flags" },
152 { "pentium3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P3 }, .unit = "flags" },
153 { "pentium4", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P4 }, .unit = "flags" },
154
155 #define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
156 #define CPU_FLAG_ATHLON CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
157 #define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
158 #define CPU_FLAG_K8 CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
159 { "k6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
160 { "k62", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K62 }, .unit = "flags" },
161 { "athlon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLON }, .unit = "flags" },
162 { "athlonxp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLONXP }, .unit = "flags" },
163 { "k8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K8 }, .unit = "flags" },
164 #elif ARCH_ARM
165 { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
166 { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
167 { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
168 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
169 { "vfp_vm", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP_VM }, .unit = "flags" },
170 { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
171 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
172 { "setend", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SETEND }, .unit = "flags" },
173 #elif ARCH_AARCH64
174 { "armv8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV8 }, .unit = "flags" },
175 { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
176 { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
177 { "dotprod", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_DOTPROD }, .unit = "flags" },
178 { "i8mm", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_I8MM }, .unit = "flags" },
179 #elif ARCH_MIPS
180 { "mmi", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMI }, .unit = "flags" },
181 { "msa", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MSA }, .unit = "flags" },
182 #elif ARCH_LOONGARCH
183 { "lsx", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_LSX }, .unit = "flags" },
184 { "lasx", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_LASX }, .unit = "flags" },
185 #elif ARCH_RISCV
186 { "rvi", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVI }, .unit = "flags" },
187 { "rvf", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVF }, .unit = "flags" },
188 { "rvd", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVD }, .unit = "flags" },
189 { "zve32x", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVV_I32 }, .unit = "flags" },
190 { "zve32f", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVV_F32 }, .unit = "flags" },
191 { "zve64x", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVV_I64 }, .unit = "flags" },
192 { "zve64d", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVV_F64 }, .unit = "flags" },
193 { "zba", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVB_ADDR }, .unit = "flags" },
194 { "zbb", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_RVB_BASIC }, .unit = "flags" },
195 #endif
196 { NULL },
197 };
198 static const AVClass class = {
199 .class_name = "cpuflags",
200 .item_name = av_default_item_name,
201 .option = cpuflags_opts,
202 .version = LIBAVUTIL_VERSION_INT,
203 };
204 6741 const AVClass *pclass = &class;
205
206 6741 return av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, flags);
207 }
208
209 4276 int av_cpu_count(void)
210 {
211 static atomic_int printed = ATOMIC_VAR_INIT(0);
212
213 4276 int nb_cpus = 1;
214 4276 int count = 0;
215 #if HAVE_WINRT
216 SYSTEM_INFO sysinfo;
217 #endif
218 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
219 cpu_set_t cpuset;
220
221 4276 CPU_ZERO(&cpuset);
222
223
1/2
✓ Branch 1 taken 4276 times.
✗ Branch 2 not taken.
4276 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
224 4276 nb_cpus = CPU_COUNT(&cpuset);
225 #elif HAVE_GETPROCESSAFFINITYMASK
226 DWORD_PTR proc_aff, sys_aff;
227 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
228 nb_cpus = av_popcount64(proc_aff);
229 #elif HAVE_SYSCTL && defined(HW_NCPUONLINE)
230 int mib[2] = { CTL_HW, HW_NCPUONLINE };
231 size_t len = sizeof(nb_cpus);
232
233 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
234 nb_cpus = 0;
235 #elif HAVE_SYSCTL && defined(HW_NCPU)
236 int mib[2] = { CTL_HW, HW_NCPU };
237 size_t len = sizeof(nb_cpus);
238
239 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
240 nb_cpus = 0;
241 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
242 nb_cpus = sysconf(_SC_NPROC_ONLN);
243 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
244 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
245 #elif HAVE_WINRT
246 GetNativeSystemInfo(&sysinfo);
247 nb_cpus = sysinfo.dwNumberOfProcessors;
248 #endif
249
250
2/2
✓ Branch 0 taken 2655 times.
✓ Branch 1 taken 1621 times.
4276 if (!atomic_exchange_explicit(&printed, 1, memory_order_relaxed))
251 2655 av_log(NULL, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
252
253 4276 count = atomic_load_explicit(&cpu_count, memory_order_relaxed);
254
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4276 times.
4276 if (count > 0) {
256 nb_cpus = count;
257 av_log(NULL, AV_LOG_DEBUG, "overriding to %d logical cores\n", nb_cpus);
258 }
259
260 4276 return nb_cpus;
261 }
262
263 void av_cpu_force_count(int count)
264 {
265 atomic_store_explicit(&cpu_count, count, memory_order_relaxed);
266 }
267
268 323098 size_t av_cpu_max_align(void)
269 {
270 #if ARCH_MIPS
271 return ff_get_cpu_max_align_mips();
272 #elif ARCH_AARCH64
273 return ff_get_cpu_max_align_aarch64();
274 #elif ARCH_ARM
275 return ff_get_cpu_max_align_arm();
276 #elif ARCH_PPC
277 return ff_get_cpu_max_align_ppc();
278 #elif ARCH_X86
279 323098 return ff_get_cpu_max_align_x86();
280 #elif ARCH_LOONGARCH
281 return ff_get_cpu_max_align_loongarch();
282 #endif
283
284 return 8;
285 }
286