Libav
cpu.c
Go to the documentation of this file.
1 /*
2  * This file is part of Libav.
3  *
4  * Libav 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  * Libav 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 Libav; 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 #define _GNU_SOURCE
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 cpuflags_mask = -1, checked;
46 
48 {
49  static int flags;
50 
51  if (checked)
52  return flags;
53 
54  if (ARCH_AARCH64)
55  flags = ff_get_cpu_flags_aarch64();
56  if (ARCH_ARM)
57  flags = ff_get_cpu_flags_arm();
58  if (ARCH_PPC)
59  flags = ff_get_cpu_flags_ppc();
60  if (ARCH_X86)
61  flags = ff_get_cpu_flags_x86();
62 
63  flags &= cpuflags_mask;
64  checked = 1;
65 
66  return flags;
67 }
68 
70 {
72  checked = 0;
73 }
74 
75 int av_parse_cpu_flags(const char *s)
76 {
77 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
78 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
79 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
80 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
81 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
82 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
83 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
84 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
85 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
86 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
87 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
88 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
89 #define CPUFLAG_AVXSLOW (AV_CPU_FLAG_AVXSLOW | CPUFLAG_AVX)
90 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
91 #define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
92 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
93 #define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
94 #define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | AV_CPU_FLAG_BMI1)
95  static const AVOption cpuflags_opts[] = {
96  { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
97 #if ARCH_PPC
98  { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
99 #elif ARCH_X86
100  { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_MMX }, .unit = "flags" },
101  { "mmxext" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_MMXEXT }, .unit = "flags" },
102  { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE }, .unit = "flags" },
103  { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2 }, .unit = "flags" },
104  { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE2SLOW }, .unit = "flags" },
105  { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3 }, .unit = "flags" },
106  { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE3SLOW }, .unit = "flags" },
107  { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSSE3 }, .unit = "flags" },
108  { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ATOM }, .unit = "flags" },
109  { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE4 }, .unit = "flags" },
110  { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_SSE42 }, .unit = "flags" },
111  { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX }, .unit = "flags" },
112  { "avxslow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVXSLOW }, .unit = "flags" },
113  { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_XOP }, .unit = "flags" },
114  { "fma3" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA3 }, .unit = "flags" },
115  { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_FMA4 }, .unit = "flags" },
116  { "avx2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_AVX2 }, .unit = "flags" },
117  { "bmi1" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_BMI1 }, .unit = "flags" },
118  { "bmi2" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_BMI2 }, .unit = "flags" },
119  { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOW }, .unit = "flags" },
120  { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = CPUFLAG_3DNOWEXT }, .unit = "flags" },
121  { "cmov", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_CMOV }, .unit = "flags" },
122 #elif ARCH_ARM
123  { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
124  { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
125  { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
126  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
127  { "vfp_vm", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP_VM }, .unit = "flags" },
128  { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
129  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
130 #elif ARCH_AARCH64
131  { "armv8", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_ARMV8 }, .unit = "flags" },
132  { "neon", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_NEON }, .unit = "flags" },
133  { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = AV_CPU_FLAG_VFP }, .unit = "flags" },
134 #endif
135  { NULL },
136  };
137  static const AVClass class = {
138  .class_name = "cpuflags",
139  .item_name = av_default_item_name,
140  .option = cpuflags_opts,
141  .version = LIBAVUTIL_VERSION_INT,
142  };
143 
144  int flags = 0, ret;
145  const AVClass *pclass = &class;
146 
147  if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
148  return ret;
149 
150  return flags & INT_MAX;
151 }
152 
153 int av_cpu_count(void)
154 {
155  int nb_cpus = 1;
156 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
157  cpu_set_t cpuset;
158 
159  CPU_ZERO(&cpuset);
160 
161  if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
162  nb_cpus = CPU_COUNT(&cpuset);
163 #elif HAVE_GETPROCESSAFFINITYMASK
164  DWORD_PTR proc_aff, sys_aff;
165  if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
166  nb_cpus = av_popcount64(proc_aff);
167 #elif HAVE_SYSCTL && defined(HW_NCPU)
168  int mib[2] = { CTL_HW, HW_NCPU };
169  size_t len = sizeof(nb_cpus);
170 
171  if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
172  nb_cpus = 0;
173 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
174  nb_cpus = sysconf(_SC_NPROC_ONLN);
175 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
176  nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
177 #endif
178 
179  return nb_cpus;
180 }
#define CPUFLAG_3DNOW
#define AV_CPU_FLAG_ALTIVEC
standard
Definition: cpu.h:55
AVOption.
Definition: opt.h:234
int av_cpu_count(void)
Definition: cpu.c:153
#define ARCH_PPC
Definition: config.h:24
#define CPUFLAG_SSE3
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
Definition: cpu.c:69
#define CPUFLAG_SSE4
#define AV_CPU_FLAG_CMOV
i686 cmov
Definition: cpu.h:49
#define AV_CPU_FLAG_VFP
Definition: cpu.h:62
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:39
static int checked
Definition: cpu.c:45
AVOptions.
#define CPUFLAG_SSE3SLOW
#define AV_CPU_FLAG_NEON
Definition: cpu.h:64
int ff_get_cpu_flags_ppc(void)
This function MAY rely on signal() or fork() in order to make sure AltiVec is present.
Definition: cpu.c:44
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
Definition: cpu.c:75
#define AV_CPU_FLAG_ATOM
Atom processor, some SSSE3 instructions are slower.
Definition: cpu.h:42
static int flags
Definition: log.c:50
static int cpuflags_mask
Definition: cpu.c:45
static const uint16_t mask[17]
Definition: lzw.c:38
#define ARCH_X86
Definition: config.h:33
int av_opt_eval_flags(void *obj, const AVOption *o, const char *val, int *flags_out)
#define AV_CPU_FLAG_ARMV6T2
Definition: cpu.h:61
#define AV_CPU_FLAG_VFP_VM
VFPv2 vector mode, deprecated in ARMv7-A and unavailable in various CPUs implementations.
Definition: cpu.h:66
#define CPUFLAG_SSE
#define AV_CPU_FLAG_ARMV5TE
Definition: cpu.h:59
#define CPUFLAG_SSE2
#define ARCH_ARM
Definition: config.h:14
int ff_get_cpu_flags_x86(void)
Definition: cpu.c:90
#define AV_CPU_FLAG_VFPV3
Definition: cpu.h:63
#define CPUFLAG_BMI2
LIBAVUTIL_VERSION_INT
Definition: eval.c:55
NULL
Definition: eval.c:55
#define AV_CPU_FLAG_ARMV6
Definition: cpu.h:60
int ff_get_cpu_flags_aarch64(void)
Definition: cpu.c:23
#define CPUFLAG_FMA4
av_default_item_name
Definition: dnxhdenc.c:55
#define AV_CPU_FLAG_BMI1
Bit Manipulation Instruction Set 1.
Definition: cpu.h:52
#define AV_CPU_FLAG_MMX
standard MMX
Definition: cpu.h:29
#define CPUFLAG_FMA3
Describe the class of an AVClass context structure.
Definition: log.h:34
#define CPUFLAG_AVXSLOW
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:47
#define CPUFLAG_SSE2SLOW
#define CPUFLAG_XOP
common internal and external API header
#define AV_CPU_FLAG_ARMV8
Definition: cpu.h:65
#define CPUFLAG_AVX2
int len
int ff_get_cpu_flags_arm(void)
Definition: cpu.c:143
#define ARCH_AARCH64
Definition: config.h:12
#define CPUFLAG_3DNOWEXT
#define CPUFLAG_MMXEXT
#define CPUFLAG_AVX
#define CPUFLAG_SSE42
#define CPUFLAG_SSSE3