FFmpeg
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 "libavutil/cpu.h"
20 #include "libavutil/cpu_internal.h"
21 #include "config.h"
22 
23 #define CORE_FLAG(f) \
24  (AV_CPU_FLAG_ ## f * (HAVE_ ## f ## _EXTERNAL || HAVE_ ## f ## _INLINE))
25 
26 #define CORE_CPU_FLAGS \
27  (CORE_FLAG(ARMV5TE) | \
28  CORE_FLAG(ARMV6) | \
29  CORE_FLAG(ARMV6T2) | \
30  CORE_FLAG(VFP) | \
31  CORE_FLAG(VFPV3) | \
32  CORE_FLAG(NEON))
33 
34 #if defined __linux__ || defined __ANDROID__ || HAVE_ELF_AUX_INFO
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "libavutil/avstring.h"
40 
41 #if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
42 #include <sys/auxv.h>
43 #endif
44 
45 #ifndef AT_HWCAP
46 #define AT_HWCAP 16
47 #endif
48 
49 /* Relevant HWCAP values from kernel headers */
50 #define HWCAP_ARM_VFP (1 << 6)
51 #define HWCAP_ARM_EDSP (1 << 7)
52 #define HWCAP_ARM_THUMBEE (1 << 11)
53 #define HWCAP_ARM_NEON (1 << 12)
54 #define HWCAP_ARM_VFPv3 (1 << 13)
55 #define HWCAP_ARM_TLS (1 << 15)
56 
57 static int get_auxval(uint32_t *hwcap)
58 {
59 #if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
60  unsigned long ret = ff_getauxval(AT_HWCAP);
61  if (ret == 0)
62  return -1;
63  *hwcap = ret;
64  return 0;
65 #else
66  return -1;
67 #endif
68 }
69 
70 #if defined __linux__ || defined __ANDROID__
71 static int get_hwcap(uint32_t *hwcap)
72 {
73  struct { uint32_t a_type; uint32_t a_val; } auxv;
74  FILE *f = fopen("/proc/self/auxv", "r");
75  int err = -1;
76 
77  if (!f)
78  return -1;
79 
80  while (fread(&auxv, sizeof(auxv), 1, f) > 0) {
81  if (auxv.a_type == AT_HWCAP) {
82  *hwcap = auxv.a_val;
83  err = 0;
84  break;
85  }
86  }
87 
88  fclose(f);
89  return err;
90 }
91 
92 static int get_cpuinfo(uint32_t *hwcap)
93 {
94  FILE *f = fopen("/proc/cpuinfo", "r");
95  char buf[200];
96 
97  if (!f)
98  return -1;
99 
100  *hwcap = 0;
101  while (fgets(buf, sizeof(buf), f)) {
102  if (av_strstart(buf, "Features", NULL)) {
103  if (strstr(buf, " edsp "))
104  *hwcap |= HWCAP_ARM_EDSP;
105  if (strstr(buf, " tls "))
106  *hwcap |= HWCAP_ARM_TLS;
107  if (strstr(buf, " thumbee "))
108  *hwcap |= HWCAP_ARM_THUMBEE;
109  if (strstr(buf, " vfp "))
110  *hwcap |= HWCAP_ARM_VFP;
111  if (strstr(buf, " vfpv3 "))
112  *hwcap |= HWCAP_ARM_VFPv3;
113  if (strstr(buf, " neon ") || strstr(buf, " asimd "))
114  *hwcap |= HWCAP_ARM_NEON;
115  if (strstr(buf, " fp ")) // Listed on 64 bit ARMv8 kernels
116  *hwcap |= HWCAP_ARM_VFP | HWCAP_ARM_VFPv3;
117  break;
118  }
119  }
120  fclose(f);
121  return 0;
122 }
123 #endif
124 
125 int ff_get_cpu_flags_arm(void)
126 {
127  int flags = CORE_CPU_FLAGS;
128  uint32_t hwcap;
129 
130  if (get_auxval(&hwcap) < 0)
131 #if defined __linux__ || defined __ANDROID__
132  if (get_hwcap(&hwcap) < 0)
133  if (get_cpuinfo(&hwcap) < 0)
134 #endif
135  return flags;
136 
137 #define check_cap(cap, flag) do { \
138  if (hwcap & HWCAP_ARM_ ## cap) \
139  flags |= AV_CPU_FLAG_ ## flag; \
140  } while (0)
141 
142  /* No flags explicitly indicate v6 or v6T2 so check others which
143  imply support. */
144  check_cap(EDSP, ARMV5TE);
145  check_cap(TLS, ARMV6);
146  check_cap(THUMBEE, ARMV6T2);
147  check_cap(VFP, VFP);
148  check_cap(VFPv3, VFPV3);
149  check_cap(NEON, NEON);
150 
151  /* The v6 checks above are not reliable so let higher flags
152  trickle down. */
156  /* Some functions use the 'setend' instruction which is deprecated on ARMv8
157  * and serializing on some ARMv7 cores. This ensures such functions
158  * are only enabled on ARMv6. */
160 
163 
164  /* set the virtual VFPv2 vector mode flag */
167 
168  return flags;
169 }
170 
171 #else
172 
174 {
175  return AV_CPU_FLAG_ARMV5TE * HAVE_ARMV5TE |
176  AV_CPU_FLAG_ARMV6 * HAVE_ARMV6 |
177  AV_CPU_FLAG_ARMV6T2 * HAVE_ARMV6T2 |
178  AV_CPU_FLAG_VFP * HAVE_VFP |
179  AV_CPU_FLAG_VFPV3 * HAVE_VFPV3 |
180  AV_CPU_FLAG_NEON * HAVE_NEON |
181  AV_CPU_FLAG_SETEND * !(HAVE_NEON | HAVE_VFPV3);
182 }
183 
184 #endif
185 
187 {
188  int flags = av_get_cpu_flags();
189 
190  if (flags & AV_CPU_FLAG_NEON)
191  return 16;
192 
193  return 8;
194 }
flags
const SwsFlags flags[]
Definition: swscale.c:61
AV_CPU_FLAG_VFP
#define AV_CPU_FLAG_VFP
Definition: cpu.h:68
AT_HWCAP
#define AT_HWCAP
Definition: cpu.c:50
ff_get_cpu_flags_arm
int ff_get_cpu_flags_arm(void)
Definition: cpu.c:173
av_get_cpu_flags
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:109
cpu_internal.h
AV_CPU_FLAG_ARMV6
#define AV_CPU_FLAG_ARMV6
Definition: cpu.h:66
ff_getauxval
unsigned long ff_getauxval(unsigned long type)
Definition: cpu.c:302
NULL
#define NULL
Definition: coverity.c:32
f
f
Definition: af_crystalizer.c:122
AV_CPU_FLAG_SETEND
#define AV_CPU_FLAG_SETEND
Definition: cpu.h:77
cpu.h
AV_CPU_FLAG_NEON
#define AV_CPU_FLAG_NEON
Definition: cpu.h:70
av_strstart
int av_strstart(const char *str, const char *pfx, const char **ptr)
Return non-zero if pfx is a prefix of str.
Definition: avstring.c:36
ret
ret
Definition: filter_design.txt:187
AV_CPU_FLAG_VFPV3
#define AV_CPU_FLAG_VFPV3
Definition: cpu.h:69
ff_get_cpu_max_align_arm
size_t ff_get_cpu_max_align_arm(void)
Definition: cpu.c:186
AV_CPU_FLAG_ARMV5TE
#define AV_CPU_FLAG_ARMV5TE
Definition: cpu.h:65
AV_CPU_FLAG_VFP_VM
#define AV_CPU_FLAG_VFP_VM
VFPv2 vector mode, deprecated in ARMv7-A and unavailable in various CPUs implementations.
Definition: cpu.h:72
avstring.h
CORE_CPU_FLAGS
#define CORE_CPU_FLAGS
Definition: cpu.c:26
AV_CPU_FLAG_ARMV6T2
#define AV_CPU_FLAG_ARMV6T2
Definition: cpu.h:67