FFmpeg  4.0
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__
35 
36 #include <stdint.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include "libavutil/avstring.h"
40 
41 #define AT_HWCAP 16
42 
43 /* Relevant HWCAP values from kernel headers */
44 #define HWCAP_VFP (1 << 6)
45 #define HWCAP_EDSP (1 << 7)
46 #define HWCAP_THUMBEE (1 << 11)
47 #define HWCAP_NEON (1 << 12)
48 #define HWCAP_VFPv3 (1 << 13)
49 #define HWCAP_TLS (1 << 15)
50 
51 static int get_hwcap(uint32_t *hwcap)
52 {
53  struct { uint32_t a_type; uint32_t a_val; } auxv;
54  FILE *f = fopen("/proc/self/auxv", "r");
55  int err = -1;
56 
57  if (!f)
58  return -1;
59 
60  while (fread(&auxv, sizeof(auxv), 1, f) > 0) {
61  if (auxv.a_type == AT_HWCAP) {
62  *hwcap = auxv.a_val;
63  err = 0;
64  break;
65  }
66  }
67 
68  fclose(f);
69  return err;
70 }
71 
72 static int get_cpuinfo(uint32_t *hwcap)
73 {
74  FILE *f = fopen("/proc/cpuinfo", "r");
75  char buf[200];
76 
77  if (!f)
78  return -1;
79 
80  *hwcap = 0;
81  while (fgets(buf, sizeof(buf), f)) {
82  if (av_strstart(buf, "Features", NULL)) {
83  if (strstr(buf, " edsp "))
84  *hwcap |= HWCAP_EDSP;
85  if (strstr(buf, " tls "))
86  *hwcap |= HWCAP_TLS;
87  if (strstr(buf, " thumbee "))
88  *hwcap |= HWCAP_THUMBEE;
89  if (strstr(buf, " vfp "))
90  *hwcap |= HWCAP_VFP;
91  if (strstr(buf, " vfpv3 "))
92  *hwcap |= HWCAP_VFPv3;
93  if (strstr(buf, " neon ") || strstr(buf, " asimd "))
94  *hwcap |= HWCAP_NEON;
95  if (strstr(buf, " fp ")) // Listed on 64 bit ARMv8 kernels
96  *hwcap |= HWCAP_VFP | HWCAP_VFPv3;
97  break;
98  }
99  }
100  fclose(f);
101  return 0;
102 }
103 
104 int ff_get_cpu_flags_arm(void)
105 {
106  int flags = CORE_CPU_FLAGS;
107  uint32_t hwcap;
108 
109  if (get_hwcap(&hwcap) < 0)
110  if (get_cpuinfo(&hwcap) < 0)
111  return flags;
112 
113 #define check_cap(cap, flag) do { \
114  if (hwcap & HWCAP_ ## cap) \
115  flags |= AV_CPU_FLAG_ ## flag; \
116  } while (0)
117 
118  /* No flags explicitly indicate v6 or v6T2 so check others which
119  imply support. */
120  check_cap(EDSP, ARMV5TE);
121  check_cap(TLS, ARMV6);
122  check_cap(THUMBEE, ARMV6T2);
123  check_cap(VFP, VFP);
124  check_cap(VFPv3, VFPV3);
125  check_cap(NEON, NEON);
126 
127  /* The v6 checks above are not reliable so let higher flags
128  trickle down. */
129  if (flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON))
130  flags |= AV_CPU_FLAG_ARMV6T2;
131  else if (flags & (AV_CPU_FLAG_ARMV6T2 | AV_CPU_FLAG_ARMV6))
132  /* Some functions use the 'setend' instruction which is deprecated on ARMv8
133  * and serializing on some ARMv7 cores. This ensures such functions
134  * are only enabled on ARMv6. */
135  flags |= AV_CPU_FLAG_SETEND;
136 
137  if (flags & AV_CPU_FLAG_ARMV6T2)
138  flags |= AV_CPU_FLAG_ARMV6;
139 
140  /* set the virtual VFPv2 vector mode flag */
141  if ((flags & AV_CPU_FLAG_VFP) && !(flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON)))
142  flags |= AV_CPU_FLAG_VFP_VM;
143 
144  return flags;
145 }
146 
147 #else
148 
150 {
158 }
159 
160 #endif
161 
163 {
164  int flags = av_get_cpu_flags();
165 
166  if (flags & AV_CPU_FLAG_NEON)
167  return 16;
168 
169  return 8;
170 }
#define NULL
Definition: coverity.c:32
#define HAVE_NEON
Definition: config.h:45
#define AV_CPU_FLAG_VFP
Definition: cpu.h:67
#define HAVE_ARMV6T2
Definition: config.h:43
#define AV_CPU_FLAG_NEON
Definition: cpu.h:69
#define HAVE_ARMV5TE
Definition: config.h:41
static int flags
Definition: log.c:55
#define HAVE_VFP
Definition: config.h:46
#define CORE_CPU_FLAGS
Definition: cpu.c:26
#define AV_CPU_FLAG_ARMV6T2
Definition: cpu.h:66
#define AV_CPU_FLAG_VFP_VM
VFPv2 vector mode, deprecated in ARMv7-A and unavailable in various CPUs implementations.
Definition: cpu.h:71
#define AV_CPU_FLAG_ARMV5TE
Definition: cpu.h:64
#define AV_CPU_FLAG_VFPV3
Definition: cpu.h:68
#define HAVE_VFPV3
Definition: config.h:47
#define AV_CPU_FLAG_ARMV6
Definition: cpu.h:65
void * buf
Definition: avisynth_c.h:690
int av_get_cpu_flags(void)
Return the flags which specify extensions supported by the CPU.
Definition: cpu.c:93
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:34
int ff_get_cpu_flags_arm(void)
Definition: cpu.c:149
#define AV_CPU_FLAG_SETEND
Definition: cpu.h:72
#define HAVE_ARMV6
Definition: config.h:42
size_t ff_get_cpu_max_align_arm(void)
Definition: cpu.c:162