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 <stddef.h>
20 #include <stdint.h>
21 #include <stdatomic.h>
22
26 #include "config.h"
29
30 #if HAVE_SCHED_GETAFFINITY
31 #ifndef _GNU_SOURCE
32 # define _GNU_SOURCE
33 #endif
34 #include <sched.h>
35 #endif
36 #if HAVE_GETPROCESSAFFINITYMASK || HAVE_WINRT
37 #include <windows.h>
38 #endif
39 #if HAVE_SYSCTL
40 #if HAVE_SYS_PARAM_H
41 #include <sys/param.h>
42 #endif
43 #include <sys/types.h>
44 #include <sys/sysctl.h>
45 #endif
46 #if HAVE_UNISTD_H
47 #include <unistd.h>
48 #endif
49
52
54 {
55 if (ARCH_MIPS)
57 if (ARCH_AARCH64)
59 if (ARCH_ARM)
61 if (ARCH_PPC)
63 if (ARCH_X86)
65 if (ARCH_LOONGARCH)
67 return 0;
68 }
69
71 if (ARCH_X86 &&
93 }
94
96 }
97
99 {
104 }
106 }
107
109 {
110 static const AVOption cpuflags_opts[] = {
112 #if ARCH_PPC
114 #elif ARCH_X86
141
142 #define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
143 #define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
144 #define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
148
149 #define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
150 #define CPU_FLAG_ATHLON CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
151 #define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
152 #define CPU_FLAG_K8 CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
158 #elif ARCH_ARM
167 #elif ARCH_AARCH64
171 #elif ARCH_MIPS
174 #elif ARCH_LOONGARCH
177 #endif
179 };
183 .option = cpuflags_opts,
185 };
186 const AVClass *pclass = &
class;
187
189 }
190
192 {
194
195 int nb_cpus = 1;
196 int count = 0;
197 #if HAVE_WINRT
198 SYSTEM_INFO sysinfo;
199 #endif
200 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
201 cpu_set_t cpuset;
202
203 CPU_ZERO(&cpuset);
204
205 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
206 nb_cpus = CPU_COUNT(&cpuset);
207 #elif HAVE_GETPROCESSAFFINITYMASK
208 DWORD_PTR proc_aff, sys_aff;
209 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
211 #elif HAVE_SYSCTL && defined(HW_NCPUONLINE)
212 int mib[2] = { CTL_HW, HW_NCPUONLINE };
213 size_t len =
sizeof(nb_cpus);
214
215 if (sysctl(mib, 2, &nb_cpus, &
len,
NULL, 0) == -1)
216 nb_cpus = 0;
217 #elif HAVE_SYSCTL && defined(HW_NCPU)
218 int mib[2] = { CTL_HW, HW_NCPU };
219 size_t len =
sizeof(nb_cpus);
220
221 if (sysctl(mib, 2, &nb_cpus, &
len,
NULL, 0) == -1)
222 nb_cpus = 0;
223 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
224 nb_cpus = sysconf(_SC_NPROC_ONLN);
225 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
226 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
227 #elif HAVE_WINRT
228 GetNativeSystemInfo(&sysinfo);
229 nb_cpus = sysinfo.dwNumberOfProcessors;
230 #endif
231
234
236
237 if (count > 0) {
238 nb_cpus = count;
240 }
241
242 return nb_cpus;
243 }
244
246 {
248 }
249
251 {
252 if (ARCH_MIPS)
254 if (ARCH_AARCH64)
256 if (ARCH_ARM)
258 if (ARCH_PPC)
260 if (ARCH_X86)
262 if (ARCH_LOONGARCH)
264
265 return 8;
266 }