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
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_UNISTD_H
44 #include <unistd.h>
45 #endif
46
48
70 }
71
74 }
75
77 {
80
81 if (ARCH_AARCH64)
83 if (ARCH_ARM)
85 if (ARCH_PPC)
87 if (ARCH_X86)
89
92 }
93
95 {
99 }
100
102 {
103 #define CPUFLAG_MMXEXT (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMXEXT | AV_CPU_FLAG_CMOV)
104 #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
105 #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
106 #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMXEXT)
107 #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
108 #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
109 #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
110 #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
111 #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
112 #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
113 #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
114 #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
115 #define CPUFLAG_AVXSLOW (AV_CPU_FLAG_AVXSLOW | CPUFLAG_AVX)
116 #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
117 #define CPUFLAG_FMA3 (AV_CPU_FLAG_FMA3 | CPUFLAG_AVX)
118 #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
119 #define CPUFLAG_AVX2 (AV_CPU_FLAG_AVX2 | CPUFLAG_AVX)
120 #define CPUFLAG_BMI2 (AV_CPU_FLAG_BMI2 | AV_CPU_FLAG_BMI1)
121 static const AVOption cpuflags_opts[] = {
123 #if ARCH_PPC
125 #elif ARCH_X86
148 #elif ARCH_ARM
155 #elif ARCH_AARCH64
159 #endif
161 };
165 .option = cpuflags_opts,
167 };
168
170 const AVClass *pclass = &
class;
171
174
175 return flags & INT_MAX;
176 }
177
179 {
180 static const AVOption cpuflags_opts[] = {
182 #if ARCH_PPC
184 #elif ARCH_X86
208
209 #define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
210 #define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
211 #define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
215
216 #define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
217 #define CPU_FLAG_ATHLON CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
218 #define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
219 #define CPU_FLAG_K8 CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
225 #elif ARCH_ARM
233 #elif ARCH_AARCH64
237 #endif
239 };
243 .option = cpuflags_opts,
245 };
246 const AVClass *pclass = &
class;
247
249 }
250
252 {
253 static volatile int printed;
254
255 int nb_cpus = 1;
256 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
257 cpu_set_t cpuset;
258
259 CPU_ZERO(&cpuset);
260
261 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
262 nb_cpus = CPU_COUNT(&cpuset);
263 #elif HAVE_GETPROCESSAFFINITYMASK
265 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
266 nb_cpus = av_popcount64(proc_aff);
267 #elif HAVE_SYSCTL && defined(HW_NCPU)
268 int mib[2] = { CTL_HW, HW_NCPU };
269 size_t len =
sizeof(nb_cpus);
270
271 if (sysctl(mib, 2, &nb_cpus, &len,
NULL, 0) == -1)
272 nb_cpus = 0;
273 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
274 nb_cpus = sysconf(_SC_NPROC_ONLN);
275 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
276 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
277 #endif
278
279 if (!printed) {
281 printed = 1;
282 }
283
284 return nb_cpus;
285 }
286
287 #ifdef TEST
288
289 #include <stdio.h>
291
292 #if !HAVE_GETOPT
294 #endif
295
296 static const struct {
297 int flag;
299 } cpu_flag_tab[] = {
300 #if ARCH_AARCH64
304 #elif ARCH_ARM
312 #elif ARCH_PPC
314 #elif ARCH_X86
337 #endif
338 { 0 }
339 };
340
341 static void print_cpu_flags(
int cpu_flags,
const char *
type)
342 {
343 int i;
344
345 printf("cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
346 printf("cpu_flags_str(%s) =", type);
347 for (i = 0; cpu_flag_tab[i].flag; i++)
348 if (cpu_flags & cpu_flag_tab[i].flag)
349 printf(
" %s", cpu_flag_tab[i].
name);
350 printf("\n");
351 }
352
353
354 int main(
int argc,
char **argv)
355 {
357 int cpu_flags_eff;
359 char threads[5] = "auto";
360 int i;
361
362 for(i = 0; cpu_flag_tab[i].flag; i++) {
363 unsigned tmp = 0;
365 fprintf(stderr, "Table missing %s\n", cpu_flag_tab[i].name);
366 return 4;
367 }
368 }
369
370 if (cpu_flags_raw < 0)
371 return 1;
372
373 for (;;) {
374 int c =
getopt(argc, argv,
"c:t:");
375 if (c == -1)
376 break;
377 switch (c) {
378 case 'c':
379 {
382 return 2;
383
385 break;
386 }
387 case 't':
388 {
390 if (len >= sizeof(threads)) {
391 fprintf(stderr,
"Invalid thread count '%s'\n",
optarg);
392 return 2;
393 }
394 }
395 }
396 }
397
399
400 if (cpu_flags_eff < 0)
401 return 3;
402
403 print_cpu_flags(cpu_flags_raw, "raw");
404 print_cpu_flags(cpu_flags_eff, "effective");
405 printf("threads = %s (cpu_count = %d)\n", threads, cpu_count);
406
407 return 0;
408 }
409
410 #endif
#define AV_CPU_FLAG_AVX
AVX functions: requires OS support even if YMM registers aren't used.
#define AV_CPU_FLAG_ALTIVEC
standard
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define AV_CPU_FLAG_SSE
SSE functions.
void av_set_cpu_flags_mask(int mask)
Set a mask on flags returned by av_get_cpu_flags().
#define AV_CPU_FLAG_MMX2
SSE integer functions or AMD MMX ext.
#define AV_CPU_FLAG_CMOV
supports cmov instruction
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define AV_CPU_FLAG_FMA3
Haswell FMA3 functions.
#define AV_CPU_FLAG_MMXEXT
SSE integer functions or AMD MMX ext.
int ff_get_cpu_flags_ppc(void)
This function MAY rely on signal() or fork() in order to make sure AltiVec is present.
int av_parse_cpu_flags(const char *s)
Parse CPU flags from a string.
#define AV_CPU_FLAG_ATOM
Atom processor, some SSSE3 instructions are slower.
#define AV_CPU_FLAG_AVX2
AVX2 functions: requires OS support even if YMM registers aren't used.
#define AV_CPU_FLAG_SSE2SLOW
SSE2 supported, but usually not faster.
#define AV_CPU_FLAG_XOP
Bulldozer XOP functions.
#define AV_CPU_FLAG_SSE42
Nehalem SSE4.2 functions.
#define AV_CPU_FLAG_SSSE3
Conroe SSSE3 functions.
static const uint16_t mask[17]
int av_opt_eval_flags(void *obj, const AVOption *o, const char *val, int *flags_out)
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_CPU_FLAG_ARMV6T2
size_t av_strlcpy(char *dst, const char *src, size_t size)
Copy the string src to dst, but no more than size - 1 bytes, and null-terminate dst.
int av_parse_cpu_caps(unsigned *flags, const char *s)
Parse CPU caps from a string and update the given AV_CPU_* flags based on that.
#define AV_CPU_FLAG_ARMV5TE
#define AV_CPU_FLAG_SSE3
Prescott SSE3 functions.
int ff_get_cpu_flags_x86(void)
#define AV_CPU_FLAG_BMI2
Bit Manipulation Instruction Set 2.
#define AV_CPU_FLAG_VFPV3
#define AV_CPU_FLAG_3DNOW
AMD 3DNOW.
#define AV_CPU_FLAG_ARMV6
int ff_get_cpu_flags_aarch64(void)
#define AV_CPU_FLAG_SSE3SLOW
SSE3 supported, but usually not faster.
#define AV_CPU_FLAG_AVXSLOW
AVX supported, but slow when using YMM registers (e.g. Bulldozer)
#define AV_CPU_FLAG_BMI1
Bit Manipulation Instruction Set 1.
#define AV_CPU_FLAG_MMX
standard MMX
static int getopt(int argc, char *argv[], char *opts)
Describe the class of an AVClass context structure.
#define AV_CPU_FLAG_FMA4
Bulldozer FMA4 functions.
#define AV_CPU_FLAG_SSE4
Penryn SSE4.1 functions.
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
#define AV_CPU_FLAG_3DNOWEXT
AMD 3DNowExt.
common internal and external API header
#define AV_CPU_FLAG_ARMV8
int ff_get_cpu_flags_arm(void)
#define AV_CPU_FLAG_SETEND
#define AV_CPU_FLAG_SSE2
PIV SSE2 functions.
void av_force_cpu_flags(int arg)
Disables cpu detection and forces the specified flags.
int main(int argc, char **argv)