mirror of
https://github.com/FFmpeg/FFmpeg.git
synced 2026-11-06 13:28:17 +00:00
Before ced4a6ebc9, we used constants
in the form HWCAP_NEON. In that commit, they were renamed to
HWCAP_ARM_NEON, to match the pattern used for aarch64 and PPC.
On PPC our naming is entirely custom to us, upstream headers
(from e.g. glibc) use an entirely different naming.
On aarch64, we use names similar to the system headers, where they
are named e.g. HWCAP_CRC32, but we add an extra prefix, naming them
HWCAP_AARCH64_CRC32, to avoid any potential conflict and redefinition
issues. (In practice, this isn't needed - both glibc, musl and
BSDs provide the aarch64 HWCAP_ defines with the same names and
same literal expansion.)
However, on ARM, the landscape of the HWCAP defines is much less
consistent. On glibc, <bits/hwcap.h> (which gets included by
<sys/auxv.h>) provides most constants with an HWCAP_ARM_ prefix.
(The couple newest constants lack that prefix though.)
The kernel headers, in <asm/hwcap.h>) define them with just a
HWCAP_ prefix, i.e. HWCAP_ARM_NEON vs HWCAP_NEON.
For the actual expansion, <asm/hwcap.h> defines HWCAP_NEON as
(1 << 12), while glibc <bits/hwcap.h> defines HWCAP_ARM_NEON
as 4096. Musl's <bits/hwcap.h> defines both HWCAP_NEON and
HWCAP_ARM_NEON, as (1 << 12).
The BSDs define HWCAP_NEON in <elf.h>, defined as 0x00001000.
Thus, all system headers define the constant with differing
expansions. And they differ in which name they expose for the
constants. But we provide our definitions in any case.
Due to all this, wrap our definitions in #ifndefs, to avoid warnings
about redefining the constants on glibc.
207 lines
5.5 KiB
C
207 lines
5.5 KiB
C
/*
|
|
* This file is part of FFmpeg.
|
|
*
|
|
* FFmpeg is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
* License as published by the Free Software Foundation; either
|
|
* version 2.1 of the License, or (at your option) any later version.
|
|
*
|
|
* FFmpeg is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
* Lesser General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
* License along with FFmpeg; if not, write to the Free Software
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
*/
|
|
|
|
#include "libavutil/cpu.h"
|
|
#include "libavutil/cpu_internal.h"
|
|
#include "config.h"
|
|
|
|
#define CORE_FLAG(f) \
|
|
(AV_CPU_FLAG_ ## f * (HAVE_ ## f ## _EXTERNAL || HAVE_ ## f ## _INLINE))
|
|
|
|
#define CORE_CPU_FLAGS \
|
|
(CORE_FLAG(ARMV5TE) | \
|
|
CORE_FLAG(ARMV6) | \
|
|
CORE_FLAG(ARMV6T2) | \
|
|
CORE_FLAG(VFP) | \
|
|
CORE_FLAG(VFPV3) | \
|
|
CORE_FLAG(NEON))
|
|
|
|
#if defined __linux__ || defined __ANDROID__ || HAVE_ELF_AUX_INFO
|
|
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include "libavutil/avstring.h"
|
|
|
|
#if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
|
|
#include <sys/auxv.h>
|
|
#endif
|
|
|
|
#ifndef AT_HWCAP
|
|
#define AT_HWCAP 16
|
|
#endif
|
|
|
|
/* Relevant HWCAP values from <sys/auxv.h> that includes <bits/hwcap.h> */
|
|
#ifndef HWCAP_ARM_VFP
|
|
#define HWCAP_ARM_VFP (1 << 6)
|
|
#endif
|
|
#ifndef HWCAP_ARM_EDSP
|
|
#define HWCAP_ARM_EDSP (1 << 7)
|
|
#endif
|
|
#ifndef HWCAP_ARM_THUMBEE
|
|
#define HWCAP_ARM_THUMBEE (1 << 11)
|
|
#endif
|
|
#ifndef HWCAP_ARM_NEON
|
|
#define HWCAP_ARM_NEON (1 << 12)
|
|
#endif
|
|
#ifndef HWCAP_ARM_VFPv3
|
|
#define HWCAP_ARM_VFPv3 (1 << 13)
|
|
#endif
|
|
#ifndef HWCAP_ARM_TLS
|
|
#define HWCAP_ARM_TLS (1 << 15)
|
|
#endif
|
|
|
|
static int get_auxval(uint32_t *hwcap)
|
|
{
|
|
#if HAVE_GETAUXVAL || HAVE_ELF_AUX_INFO
|
|
unsigned long ret = ff_getauxval(AT_HWCAP);
|
|
if (ret == 0)
|
|
return -1;
|
|
*hwcap = ret;
|
|
return 0;
|
|
#else
|
|
return -1;
|
|
#endif
|
|
}
|
|
|
|
#if defined __linux__ || defined __ANDROID__
|
|
static int get_hwcap(uint32_t *hwcap)
|
|
{
|
|
struct { uint32_t a_type; uint32_t a_val; } auxv;
|
|
FILE *f = fopen("/proc/self/auxv", "r");
|
|
int err = -1;
|
|
|
|
if (!f)
|
|
return -1;
|
|
|
|
while (fread(&auxv, sizeof(auxv), 1, f) > 0) {
|
|
if (auxv.a_type == AT_HWCAP) {
|
|
*hwcap = auxv.a_val;
|
|
err = 0;
|
|
break;
|
|
}
|
|
}
|
|
|
|
fclose(f);
|
|
return err;
|
|
}
|
|
|
|
static int get_cpuinfo(uint32_t *hwcap)
|
|
{
|
|
FILE *f = fopen("/proc/cpuinfo", "r");
|
|
char buf[200];
|
|
|
|
if (!f)
|
|
return -1;
|
|
|
|
*hwcap = 0;
|
|
while (fgets(buf, sizeof(buf), f)) {
|
|
if (av_strstart(buf, "Features", NULL)) {
|
|
if (strstr(buf, " edsp "))
|
|
*hwcap |= HWCAP_ARM_EDSP;
|
|
if (strstr(buf, " tls "))
|
|
*hwcap |= HWCAP_ARM_TLS;
|
|
if (strstr(buf, " thumbee "))
|
|
*hwcap |= HWCAP_ARM_THUMBEE;
|
|
if (strstr(buf, " vfp "))
|
|
*hwcap |= HWCAP_ARM_VFP;
|
|
if (strstr(buf, " vfpv3 "))
|
|
*hwcap |= HWCAP_ARM_VFPv3;
|
|
if (strstr(buf, " neon ") || strstr(buf, " asimd "))
|
|
*hwcap |= HWCAP_ARM_NEON;
|
|
if (strstr(buf, " fp ")) // Listed on 64 bit ARMv8 kernels
|
|
*hwcap |= HWCAP_ARM_VFP | HWCAP_ARM_VFPv3;
|
|
break;
|
|
}
|
|
}
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
#endif
|
|
|
|
int ff_get_cpu_flags_arm(void)
|
|
{
|
|
int flags = CORE_CPU_FLAGS;
|
|
uint32_t hwcap;
|
|
|
|
if (get_auxval(&hwcap) < 0)
|
|
#if defined __linux__ || defined __ANDROID__
|
|
if (get_hwcap(&hwcap) < 0)
|
|
if (get_cpuinfo(&hwcap) < 0)
|
|
#endif
|
|
return flags;
|
|
|
|
#define check_cap(cap, flag) do { \
|
|
if (hwcap & HWCAP_ARM_ ## cap) \
|
|
flags |= AV_CPU_FLAG_ ## flag; \
|
|
} while (0)
|
|
|
|
/* No flags explicitly indicate v6 or v6T2 so check others which
|
|
imply support. */
|
|
check_cap(EDSP, ARMV5TE);
|
|
check_cap(TLS, ARMV6);
|
|
check_cap(THUMBEE, ARMV6T2);
|
|
check_cap(VFP, VFP);
|
|
check_cap(VFPv3, VFPV3);
|
|
check_cap(NEON, NEON);
|
|
|
|
/* The v6 checks above are not reliable so let higher flags
|
|
trickle down. */
|
|
if (flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON))
|
|
flags |= AV_CPU_FLAG_ARMV6T2;
|
|
else if (flags & (AV_CPU_FLAG_ARMV6T2 | AV_CPU_FLAG_ARMV6))
|
|
/* Some functions use the 'setend' instruction which is deprecated on ARMv8
|
|
* and serializing on some ARMv7 cores. This ensures such functions
|
|
* are only enabled on ARMv6. */
|
|
flags |= AV_CPU_FLAG_SETEND;
|
|
|
|
if (flags & AV_CPU_FLAG_ARMV6T2)
|
|
flags |= AV_CPU_FLAG_ARMV6;
|
|
|
|
/* set the virtual VFPv2 vector mode flag */
|
|
if ((flags & AV_CPU_FLAG_VFP) && !(flags & (AV_CPU_FLAG_VFPV3 | AV_CPU_FLAG_NEON)))
|
|
flags |= AV_CPU_FLAG_VFP_VM;
|
|
|
|
return flags;
|
|
}
|
|
|
|
#else
|
|
|
|
int ff_get_cpu_flags_arm(void)
|
|
{
|
|
return AV_CPU_FLAG_ARMV5TE * HAVE_ARMV5TE |
|
|
AV_CPU_FLAG_ARMV6 * HAVE_ARMV6 |
|
|
AV_CPU_FLAG_ARMV6T2 * HAVE_ARMV6T2 |
|
|
AV_CPU_FLAG_VFP * HAVE_VFP |
|
|
AV_CPU_FLAG_VFPV3 * HAVE_VFPV3 |
|
|
AV_CPU_FLAG_NEON * HAVE_NEON |
|
|
AV_CPU_FLAG_SETEND * !(HAVE_NEON | HAVE_VFPV3);
|
|
}
|
|
|
|
#endif
|
|
|
|
size_t ff_get_cpu_max_align_arm(void)
|
|
{
|
|
int flags = av_get_cpu_flags();
|
|
|
|
if (flags & AV_CPU_FLAG_NEON)
|
|
return 16;
|
|
|
|
return 8;
|
|
}
|