You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

166 lines
6.5KB

  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. #include "cpu.h"
  19. #include "config.h"
  20. #include "opt.h"
  21. static int cpuflags_mask = -1, checked;
  22. int av_get_cpu_flags(void)
  23. {
  24. static int flags;
  25. if (checked)
  26. return flags;
  27. if (ARCH_ARM) flags = ff_get_cpu_flags_arm();
  28. if (ARCH_PPC) flags = ff_get_cpu_flags_ppc();
  29. if (ARCH_X86) flags = ff_get_cpu_flags_x86();
  30. flags &= cpuflags_mask;
  31. checked = 1;
  32. return flags;
  33. }
  34. void av_set_cpu_flags_mask(int mask)
  35. {
  36. cpuflags_mask = mask;
  37. checked = 0;
  38. }
  39. int av_parse_cpu_flags(const char *s)
  40. {
  41. #define CPUFLAG_MMX2 (AV_CPU_FLAG_MMX | AV_CPU_FLAG_MMX2)
  42. #define CPUFLAG_3DNOW (AV_CPU_FLAG_3DNOW | AV_CPU_FLAG_MMX)
  43. #define CPUFLAG_3DNOWEXT (AV_CPU_FLAG_3DNOWEXT | CPUFLAG_3DNOW)
  44. #define CPUFLAG_SSE (AV_CPU_FLAG_SSE | CPUFLAG_MMX2)
  45. #define CPUFLAG_SSE2 (AV_CPU_FLAG_SSE2 | CPUFLAG_SSE)
  46. #define CPUFLAG_SSE2SLOW (AV_CPU_FLAG_SSE2SLOW | CPUFLAG_SSE2)
  47. #define CPUFLAG_SSE3 (AV_CPU_FLAG_SSE3 | CPUFLAG_SSE2)
  48. #define CPUFLAG_SSE3SLOW (AV_CPU_FLAG_SSE3SLOW | CPUFLAG_SSE3)
  49. #define CPUFLAG_SSSE3 (AV_CPU_FLAG_SSSE3 | CPUFLAG_SSE3)
  50. #define CPUFLAG_SSE4 (AV_CPU_FLAG_SSE4 | CPUFLAG_SSSE3)
  51. #define CPUFLAG_SSE42 (AV_CPU_FLAG_SSE42 | CPUFLAG_SSE4)
  52. #define CPUFLAG_AVX (AV_CPU_FLAG_AVX | CPUFLAG_SSE42)
  53. #define CPUFLAG_XOP (AV_CPU_FLAG_XOP | CPUFLAG_AVX)
  54. #define CPUFLAG_FMA4 (AV_CPU_FLAG_FMA4 | CPUFLAG_AVX)
  55. static const AVOption cpuflags_opts[] = {
  56. { "flags" , NULL, 0, AV_OPT_TYPE_FLAGS, { 0 }, INT64_MIN, INT64_MAX, .unit = "flags" },
  57. #if ARCH_PPC
  58. { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
  59. #elif ARCH_X86
  60. { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_MMX }, .unit = "flags" },
  61. { "mmx2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_MMX2 }, .unit = "flags" },
  62. { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE }, .unit = "flags" },
  63. { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE2 }, .unit = "flags" },
  64. { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE2SLOW }, .unit = "flags" },
  65. { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE3 }, .unit = "flags" },
  66. { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE3SLOW }, .unit = "flags" },
  67. { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSSE3 }, .unit = "flags" },
  68. { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ATOM }, .unit = "flags" },
  69. { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE4 }, .unit = "flags" },
  70. { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE42 }, .unit = "flags" },
  71. { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_AVX }, .unit = "flags" },
  72. { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_XOP }, .unit = "flags" },
  73. { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_FMA4 }, .unit = "flags" },
  74. { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_3DNOW }, .unit = "flags" },
  75. { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_3DNOWEXT }, .unit = "flags" },
  76. #elif ARCH_ARM
  77. { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
  78. { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
  79. { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
  80. { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_VFP }, .unit = "flags" },
  81. { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
  82. { "neon", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_NEON }, .unit = "flags" },
  83. #endif
  84. { NULL },
  85. };
  86. static const AVClass class = {
  87. .class_name = "cpuflags",
  88. .item_name = av_default_item_name,
  89. .option = cpuflags_opts,
  90. .version = LIBAVUTIL_VERSION_INT,
  91. };
  92. int flags = 0, ret;
  93. const AVClass *pclass = &class;
  94. if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
  95. return ret;
  96. return flags & INT_MAX;
  97. }
  98. #ifdef TEST
  99. #undef printf
  100. #include <stdio.h>
  101. static const struct {
  102. int flag;
  103. const char *name;
  104. } cpu_flag_tab[] = {
  105. #if ARCH_ARM
  106. { AV_CPU_FLAG_ARMV5TE, "armv5te" },
  107. { AV_CPU_FLAG_ARMV6, "armv6" },
  108. { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
  109. { AV_CPU_FLAG_VFP, "vfp" },
  110. { AV_CPU_FLAG_VFPV3, "vfpv3" },
  111. { AV_CPU_FLAG_NEON, "neon" },
  112. #elif ARCH_PPC
  113. { AV_CPU_FLAG_ALTIVEC, "altivec" },
  114. #elif ARCH_X86
  115. { AV_CPU_FLAG_MMX, "mmx" },
  116. { AV_CPU_FLAG_MMX2, "mmx2" },
  117. { AV_CPU_FLAG_SSE, "sse" },
  118. { AV_CPU_FLAG_SSE2, "sse2" },
  119. { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
  120. { AV_CPU_FLAG_SSE3, "sse3" },
  121. { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
  122. { AV_CPU_FLAG_SSSE3, "ssse3" },
  123. { AV_CPU_FLAG_ATOM, "atom" },
  124. { AV_CPU_FLAG_SSE4, "sse4.1" },
  125. { AV_CPU_FLAG_SSE42, "sse4.2" },
  126. { AV_CPU_FLAG_AVX, "avx" },
  127. { AV_CPU_FLAG_XOP, "xop" },
  128. { AV_CPU_FLAG_FMA4, "fma4" },
  129. { AV_CPU_FLAG_3DNOW, "3dnow" },
  130. { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
  131. #endif
  132. { 0 }
  133. };
  134. int main(void)
  135. {
  136. int cpu_flags = av_get_cpu_flags();
  137. int i;
  138. printf("cpu_flags = 0x%08X\n", cpu_flags);
  139. printf("cpu_flags =");
  140. for (i = 0; cpu_flag_tab[i].flag; i++)
  141. if (cpu_flags & cpu_flag_tab[i].flag)
  142. printf(" %s", cpu_flag_tab[i].name);
  143. printf("\n");
  144. return 0;
  145. }
  146. #endif