FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 "libavutil/cpu.h"
20 #include "config.h"
21 
22 #define CORE_FLAG(f) \
23  (AV_CPU_FLAG_ ## f * (HAVE_ ## f ## _EXTERNAL || HAVE_ ## f ## _INLINE))
24 
25 #define CORE_CPU_FLAGS \
26  (CORE_FLAG(ARMV5TE) | \
27  CORE_FLAG(ARMV6) | \
28  CORE_FLAG(ARMV6T2) | \
29  CORE_FLAG(VFP) | \
30  CORE_FLAG(VFPV3) | \
31  CORE_FLAG(NEON))
32 
33 #if defined __linux__ || defined __ANDROID__
34 
35 #include <stdint.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include "libavutil/avstring.h"
39 
40 #define AT_HWCAP 16
41 
42 /* Relevant HWCAP values from kernel headers */
43 #define HWCAP_VFP (1 << 6)
44 #define HWCAP_EDSP (1 << 7)
45 #define HWCAP_THUMBEE (1 << 11)
46 #define HWCAP_NEON (1 << 12)
47 #define HWCAP_VFPv3 (1 << 13)
48 #define HWCAP_TLS (1 << 15)
49 
50 static int get_hwcap(uint32_t *hwcap)
51 {
52  struct { uint32_t a_type; uint32_t a_val; } auxv;
53  FILE *f = fopen("/proc/self/auxv", "r");
54  int err = -1;
55 
56  if (!f)
57  return -1;
58 
59  while (fread(&auxv, sizeof(auxv), 1, f) > 0) {
60  if (auxv.a_type == AT_HWCAP) {
61  *hwcap = auxv.a_val;
62  err = 0;
63  break;
64  }
65  }
66 
67  fclose(f);
68  return err;
69 }
70 
71 static int get_cpuinfo(uint32_t *hwcap)
72 {
73  FILE *f = fopen("/proc/cpuinfo", "r");
74  char buf[200];
75 
76  if (!f)
77  return -1;
78 
79  *hwcap = 0;
80  while (fgets(buf, sizeof(buf), f)) {
81  if (av_strstart(buf, "Features", NULL)) {
82  if (strstr(buf, " edsp "))
83  *hwcap |= HWCAP_EDSP;
84  if (strstr(buf, " tls "))
85  *hwcap |= HWCAP_TLS;
86  if (strstr(buf, " thumbee "))
87  *hwcap |= HWCAP_THUMBEE;
88  if (strstr(buf, " vfp "))
89  *hwcap |= HWCAP_VFP;
90  if (strstr(buf, " vfpv3 "))
91  *hwcap |= HWCAP_VFPv3;
92  if (strstr(buf, " neon "))
93  *hwcap |= HWCAP_NEON;
94  break;
95  }
96  }
97  fclose(f);
98  return 0;
99 }
100 
101 int ff_get_cpu_flags_arm(void)
102 {
103  int flags = CORE_CPU_FLAGS;
104  uint32_t hwcap;
105 
106  if (get_hwcap(&hwcap) < 0)
107  if (get_cpuinfo(&hwcap) < 0)
108  return flags;
109 
110 #define check_cap(cap, flag) do { \
111  if (hwcap & HWCAP_ ## cap) \
112  flags |= AV_CPU_FLAG_ ## flag; \
113  } while (0)
114 
115  /* No flags explicitly indicate v6 or v6T2 so check others which
116  imply support. */
117  check_cap(EDSP, ARMV5TE);
118  check_cap(TLS, ARMV6);
119  check_cap(THUMBEE, ARMV6T2);
120  check_cap(VFP, VFP);
121  check_cap(VFPv3, VFPV3);
122  check_cap(NEON, NEON);
123 
124  /* The v6 checks above are not reliable so let higher flags
125  trickle down. */
126  if (flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON))
127  flags |= AV_CPU_FLAG_ARMV6T2;
128  if (flags & AV_CPU_FLAG_ARMV6T2)
129  flags |= AV_CPU_FLAG_ARMV6;
130 
131  return flags;
132 }
133 
134 #else
135 
137 {
138  return AV_CPU_FLAG_ARMV5TE * HAVE_ARMV5TE |
139  AV_CPU_FLAG_ARMV6 * HAVE_ARMV6 |
140  AV_CPU_FLAG_ARMV6T2 * HAVE_ARMV6T2 |
141  AV_CPU_FLAG_VFP * HAVE_VFP |
142  AV_CPU_FLAG_VFPV3 * HAVE_VFPV3 |
143  AV_CPU_FLAG_NEON * HAVE_NEON;
144 }
145 
146 #endif