cpu.c 4.8 KB

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