FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cpu.c
Go to the documentation of this file.
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 <stdint.h>
20 
21 #include "cpu.h"
22 #include "cpu_internal.h"
23 #include "config.h"
24 #include "opt.h"
25 #include "common.h"
26 
27 #if HAVE_SCHED_GETAFFINITY
28 #ifndef _GNU_SOURCE
29 # define _GNU_SOURCE
30 #endif
31 #include <sched.h>
32 #endif
33 #if HAVE_GETPROCESSAFFINITYMASK
34 #include <windows.h>
35 #endif
36 #if HAVE_SYSCTL
37 #if HAVE_SYS_PARAM_H
38 #include <sys/param.h>
39 #endif
40 #include <sys/types.h>
41 #include <sys/sysctl.h>
42 #endif
43 #if HAVE_SYSCONF
44 #include <unistd.h>
45 #endif
46 
47 static int flags, checked;
48 
50  flags = arg;
51  checked = arg != -1;
52 }
53 
55 {
56  if (checked)
57  return flags;
58 
59  if (ARCH_AARCH64)
61  if (ARCH_ARM)
63  if (ARCH_PPC)
65  if (ARCH_X86)
67 
68  checked = 1;
69  return flags;
70 }
71 
73 {
74  checked = 0;
76  checked = 1;
77 }
78 
79 int av_parse_cpu_flags(const char *s)
80 {
81 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
82 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
83 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
84 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
85 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
86 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
87 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
88 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
89 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
90 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
91 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
92 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
93 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
94 #define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
95 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
96 #define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
97 #define CPUFLAG_BMI1 (AV_CPU_FLAG_BMI1)
98 #define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | CPUFLAG_BMI1)
99  static const AVOption cpuflags_opts[] = {
100  { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
101 #if ARCH_PPC
102  { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
103 #elif ARCH_X86
104  { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
105  { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
106  { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
107  { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
108  { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
109  { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
110  { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
111  { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
112  { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
113  { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
114  { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
115  { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
116  { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
117  { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
118  { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
119  { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
120  { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI1 }, .unit = "flags" },
121  { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI2 }, .unit = "flags" },
122  { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
123  { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
124  { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
125 #elif ARCH_ARM
126  { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
127  { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
128  { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
129  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
130  { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
131  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
132 #elif ARCH_AARCH64
133  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
134  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
135 #endif
136  { NULL },
137  };
138  static const AVClass class = {
139  .class_name = "cpuflags",
140  .item_name = av_default_item_name,
141  .option = cpuflags_opts,
142  .version = LIBAVUTIL_VERSION_INT,
143  };
144 
145  int flags = 0, ret;
146  const AVClass *pclass = &class;
147 
148  if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
149  return ret;
150 
151  return flags & INT_MAX;
152 }
153 
154 int av_parse_cpu_caps(unsigned *flags, const char *s)
155 {
156  static const AVOption cpuflags_opts[] = {
157  { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
158 #if ARCH_PPC
159  { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
160 #elif ARCH_X86
161  { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
162  { "mmx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2 }, .unit = "flags" },
163  { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX2 }, .unit = "flags" },
164  { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE }, .unit = "flags" },
165  { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2 }, .unit = "flags" },
166  { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE2SLOW }, .unit = "flags" },
167  { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3 }, .unit = "flags" },
168  { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE3SLOW }, .unit = "flags" },
169  { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSSE3 }, .unit = "flags" },
170  { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
171  { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE4 }, .unit = "flags" },
172  { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_SSE42 }, .unit = "flags" },
173  { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX }, .unit = "flags" },
174  { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_XOP }, .unit = "flags" },
175  { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA3 }, .unit = "flags" },
176  { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_FMA4 }, .unit = "flags" },
177  { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_AVX2 }, .unit = "flags" },
178  { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI1 }, .unit = "flags" },
179  { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI2 }, .unit = "flags" },
180  { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOW }, .unit = "flags" },
181  { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_3DNOWEXT }, .unit = "flags" },
182  { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
183 
184 #define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
185 #define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
186 #define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
187  { "pentium2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P2 }, .unit = "flags" },
188  { "pentium3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P3 }, .unit = "flags" },
189  { "pentium4", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P4 }, .unit = "flags" },
190 
191 #define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
192 #define CPU_FLAG_ATHLON CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
193 #define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
194 #define CPU_FLAG_K8 CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
195  { "k6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
196  { "k62", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K62 }, .unit = "flags" },
197  { "athlon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLON }, .unit = "flags" },
198  { "athlonxp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLONXP }, .unit = "flags" },
199  { "k8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_K8 }, .unit = "flags" },
200 #elif ARCH_ARM
201  { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
202  { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
203  { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
204  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
205  { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
206  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
207 #endif
208  { NULL },
209  };
210  static const AVClass class = {
211  .class_name = "cpuflags",
212  .item_name = av_default_item_name,
213  .option = cpuflags_opts,
214  .version = LIBAVUTIL_VERSION_INT,
215  };
216  const AVClass *pclass = &class;
217 
218  return av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, flags);
219 }
220 
221 int av_cpu_count(void)
222 {
223  static volatile int printed;
224 
225  int nb_cpus = 1;
226 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
227  cpu_set_t cpuset;
228 
229  CPU_ZERO(&cpuset);
230 
231  if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
232  nb_cpus = CPU_COUNT(&cpuset);
233 #elif HAVE_GETPROCESSAFFINITYMASK
234  DWORD_PTR proc_aff, sys_aff;
235  if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
236  nb_cpus = av_popcount64(proc_aff);
237 #elif HAVE_SYSCTL && defined(HW_NCPU)
238  int mib[2] = { CTL_HW, HW_NCPU };
239  size_t len = sizeof(nb_cpus);
240 
241  if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
242  nb_cpus = 0;
243 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
244  nb_cpus = sysconf(_SC_NPROC_ONLN);
245 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
246  nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
247 #endif
248 
249  if (!printed) {
250  av_log(NULL, AV_LOG_DEBUG, "detected %d logical cores\n", nb_cpus);
251  printed = 1;
252  }
253 
254  return nb_cpus;
255 }
256 
257 #ifdef TEST
258 
259 #include <stdio.h>
260 
261 static const struct {
262  int flag;
263  const char *name;
264 } cpu_flag_tab[] = {
265 #if ARCH_AARCH64
266  { AV_CPU_FLAG_NEON, "neon" },
267  { AV_CPU_FLAG_VFP, "vfp" },
268 #elif ARCH_ARM
269  { AV_CPU_FLAG_ARMV5TE, "armv5te" },
270  { AV_CPU_FLAG_ARMV6, "armv6" },
271  { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
272  { AV_CPU_FLAG_VFP, "vfp" },
273  { AV_CPU_FLAG_VFPV3, "vfpv3" },
274  { AV_CPU_FLAG_NEON, "neon" },
275 #elif ARCH_PPC
276  { AV_CPU_FLAG_ALTIVEC, "altivec" },
277 #elif ARCH_X86
278  { AV_CPU_FLAG_MMX, "mmx" },
279  { AV_CPU_FLAG_MMXEXT, "mmxext" },
280  { AV_CPU_FLAG_SSE, "sse" },
281  { AV_CPU_FLAG_SSE2, "sse2" },
282  { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
283  { AV_CPU_FLAG_SSE3, "sse3" },
284  { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
285  { AV_CPU_FLAG_SSSE3, "ssse3" },
286  { AV_CPU_FLAG_ATOM, "atom" },
287  { AV_CPU_FLAG_SSE4, "sse4.1" },
288  { AV_CPU_FLAG_SSE42, "sse4.2" },
289  { AV_CPU_FLAG_AVX, "avx" },
290  { AV_CPU_FLAG_XOP, "xop" },
291  { AV_CPU_FLAG_FMA3, "fma3" },
292  { AV_CPU_FLAG_FMA4, "fma4" },
293  { AV_CPU_FLAG_3DNOW, "3dnow" },
294  { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
295  { AV_CPU_FLAG_CMOV, "cmov" },
296  { AV_CPU_FLAG_AVX2, "avx2" },
297  { AV_CPU_FLAG_BMI1, "bmi1" },
298  { AV_CPU_FLAG_BMI2, "bmi2" },
299 #endif
300  { 0 }
301 };
302 
303 int main(void)
304 {
305  int cpu_flags = av_get_cpu_flags();
306  int i;
307 
308  printf("cpu_flags = 0x%08X\n", cpu_flags);
309  printf("cpu_flags =");
310  for (i = 0; cpu_flag_tab[i].flag; i++)
311  if (cpu_flags & cpu_flag_tab[i].flag)
312  printf(" %s", cpu_flag_tab[i].name);
313  printf("\n");
314 
315  return 0;
316 }
317 
318 #endif