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
20 #include "config.h"
23
24 #if HAVE_SCHED_GETAFFINITY
25 #ifndef _GNU_SOURCE
26 # define _GNU_SOURCE
27 #endif
28 #include <sched.h>
29 #endif
30 #if HAVE_GETPROCESSAFFINITYMASK
31 #include <windows.h>
32 #endif
33 #if HAVE_SYSCTL
34 #if HAVE_SYS_PARAM_H
35 #include <sys/param.h>
36 #endif
37 #include <sys/types.h>
38 #include <sys/param.h>
39 #include <sys/sysctl.h>
40 #endif
41 #if HAVE_SYSCONF
42 #include <unistd.h>
43 #endif
44
46
50 }
51
53 {
56
60
63 }
64
66 {
70 }
71
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 static const AVOption cpuflags_opts[] = {
89 {
"flags" , NULL, 0,
AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit =
"flags" },
90 #if ARCH_PPC
92 #elif ARCH_X86
110 #elif ARCH_ARM
117 #endif
118 { NULL },
119 };
123 .option = cpuflags_opts,
125 };
126
128 const AVClass *pclass = &
class;
129
132
133 return flags & INT_MAX;
134 }
135
137 {
138 static const AVOption cpuflags_opts[] = {
139 {
"flags" , NULL, 0,
AV_OPT_TYPE_FLAGS, { .i64 = 0 }, INT64_MIN, INT64_MAX, .unit =
"flags" },
140 #if ARCH_PPC
142 #elif ARCH_X86
161
162 #define CPU_FLAG_P2 AV_CPU_FLAG_CMOV | AV_CPU_FLAG_MMX
163 #define CPU_FLAG_P3 CPU_FLAG_P2 | AV_CPU_FLAG_MMX2 | AV_CPU_FLAG_SSE
164 #define CPU_FLAG_P4 CPU_FLAG_P3| AV_CPU_FLAG_SSE2
165 {
"pentium2", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P2 }, .unit =
"flags" },
166 {
"pentium3", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P3 }, .unit =
"flags" },
167 {
"pentium4", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_P4 }, .unit =
"flags" },
168
169 #define CPU_FLAG_K62 AV_CPU_FLAG_MMX | AV_CPU_FLAG_3DNOW
170 #define CPU_FLAG_ATHLON CPU_FLAG_K62 | AV_CPU_FLAG_CMOV | AV_CPU_FLAG_3DNOWEXT | AV_CPU_FLAG_MMX2
171 #define CPU_FLAG_ATHLONXP CPU_FLAG_ATHLON | AV_CPU_FLAG_SSE
172 #define CPU_FLAG_K8 CPU_FLAG_ATHLONXP | AV_CPU_FLAG_SSE2
175 {
"athlon", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLON }, .unit =
"flags" },
176 {
"athlonxp", NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = CPU_FLAG_ATHLONXP }, .unit =
"flags" },
178 #elif ARCH_ARM
185 #endif
186 { NULL },
187 };
191 .option = cpuflags_opts,
193 };
194 const AVClass *pclass = &
class;
195
197 }
198
200 {
201 static volatile int printed;
202
203 int nb_cpus = 1;
204 #if HAVE_SCHED_GETAFFINITY && defined(CPU_COUNT)
205 cpu_set_t cpuset;
206
207 CPU_ZERO(&cpuset);
208
209 if (!sched_getaffinity(0, sizeof(cpuset), &cpuset))
210 nb_cpus = CPU_COUNT(&cpuset);
211 #elif HAVE_GETPROCESSAFFINITYMASK
213 if (GetProcessAffinityMask(GetCurrentProcess(), &proc_aff, &sys_aff))
214 nb_cpus = av_popcount64(proc_aff);
215 #elif HAVE_SYSCTL && defined(HW_NCPU)
216 int mib[2] = { CTL_HW, HW_NCPU };
217 size_t len =
sizeof(nb_cpus);
218
219 if (sysctl(mib, 2, &nb_cpus, &len, NULL, 0) == -1)
220 nb_cpus = 0;
221 #elif HAVE_SYSCONF && defined(_SC_NPROC_ONLN)
222 nb_cpus = sysconf(_SC_NPROC_ONLN);
223 #elif HAVE_SYSCONF && defined(_SC_NPROCESSORS_ONLN)
224 nb_cpus = sysconf(_SC_NPROCESSORS_ONLN);
225 #endif
226
227 if (!printed) {
229 printed = 1;
230 }
231
232 return nb_cpus;
233 }
234
235 #ifdef TEST
236
237 #include <stdio.h>
238
239 static const struct {
240 int flag;
242 } cpu_flag_tab[] = {
243 #if ARCH_ARM
250 #elif ARCH_PPC
252 #elif ARCH_X86
270 #endif
271 { 0 }
272 };
273
275 {
277 int i;
278
279 printf("cpu_flags = 0x%08X\n", cpu_flags);
280 printf("cpu_flags =");
281 for (i = 0; cpu_flag_tab[i].flag; i++)
282 if (cpu_flags & cpu_flag_tab[i].flag)
283 printf(
" %s", cpu_flag_tab[i].
name);
284 printf("\n");
285
286 return 0;
287 }
288
289 #endif