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