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.

164 lines
6.4KB

  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. { "altivec" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ALTIVEC }, .unit = "flags" },
  58. { "mmx" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_MMX }, .unit = "flags" },
  59. { "mmx2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_MMX2 }, .unit = "flags" },
  60. { "sse" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE }, .unit = "flags" },
  61. { "sse2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE2 }, .unit = "flags" },
  62. { "sse2slow", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE2SLOW }, .unit = "flags" },
  63. { "sse3" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE3 }, .unit = "flags" },
  64. { "sse3slow", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE3SLOW }, .unit = "flags" },
  65. { "ssse3" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSSE3 }, .unit = "flags" },
  66. { "atom" , NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ATOM }, .unit = "flags" },
  67. { "sse4.1" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE4 }, .unit = "flags" },
  68. { "sse4.2" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_SSE42 }, .unit = "flags" },
  69. { "avx" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_AVX }, .unit = "flags" },
  70. { "xop" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_XOP }, .unit = "flags" },
  71. { "fma4" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_FMA4 }, .unit = "flags" },
  72. { "3dnow" , NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_3DNOW }, .unit = "flags" },
  73. { "3dnowext", NULL, 0, AV_OPT_TYPE_CONST, { CPUFLAG_3DNOWEXT }, .unit = "flags" },
  74. { "armv5te", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV5TE }, .unit = "flags" },
  75. { "armv6", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV6 }, .unit = "flags" },
  76. { "armv6t2", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_ARMV6T2 }, .unit = "flags" },
  77. { "vfp", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_VFP }, .unit = "flags" },
  78. { "vfpv3", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_VFPV3 }, .unit = "flags" },
  79. { "neon", NULL, 0, AV_OPT_TYPE_CONST, { AV_CPU_FLAG_NEON }, .unit = "flags" },
  80. { NULL },
  81. };
  82. static const AVClass class = {
  83. .class_name = "cpuflags",
  84. .item_name = av_default_item_name,
  85. .option = cpuflags_opts,
  86. .version = LIBAVUTIL_VERSION_INT,
  87. };
  88. int flags = 0, ret;
  89. const AVClass *pclass = &class;
  90. if ((ret = av_opt_eval_flags(&pclass, &cpuflags_opts[0], s, &flags)) < 0)
  91. return ret;
  92. return flags & INT_MAX;
  93. }
  94. #ifdef TEST
  95. #undef printf
  96. #include <stdio.h>
  97. static const struct {
  98. int flag;
  99. const char *name;
  100. } cpu_flag_tab[] = {
  101. #if ARCH_ARM
  102. { AV_CPU_FLAG_ARMV5TE, "armv5te" },
  103. { AV_CPU_FLAG_ARMV6, "armv6" },
  104. { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
  105. { AV_CPU_FLAG_VFP, "vfp" },
  106. { AV_CPU_FLAG_VFPV3, "vfpv3" },
  107. { AV_CPU_FLAG_NEON, "neon" },
  108. #elif ARCH_PPC
  109. { AV_CPU_FLAG_ALTIVEC, "altivec" },
  110. #elif ARCH_X86
  111. { AV_CPU_FLAG_MMX, "mmx" },
  112. { AV_CPU_FLAG_MMX2, "mmx2" },
  113. { AV_CPU_FLAG_SSE, "sse" },
  114. { AV_CPU_FLAG_SSE2, "sse2" },
  115. { AV_CPU_FLAG_SSE2SLOW, "sse2(slow)" },
  116. { AV_CPU_FLAG_SSE3, "sse3" },
  117. { AV_CPU_FLAG_SSE3SLOW, "sse3(slow)" },
  118. { AV_CPU_FLAG_SSSE3, "ssse3" },
  119. { AV_CPU_FLAG_ATOM, "atom" },
  120. { AV_CPU_FLAG_SSE4, "sse4.1" },
  121. { AV_CPU_FLAG_SSE42, "sse4.2" },
  122. { AV_CPU_FLAG_AVX, "avx" },
  123. { AV_CPU_FLAG_XOP, "xop" },
  124. { AV_CPU_FLAG_FMA4, "fma4" },
  125. { AV_CPU_FLAG_3DNOW, "3dnow" },
  126. { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
  127. #endif
  128. { 0 }
  129. };
  130. int main(void)
  131. {
  132. int cpu_flags = av_get_cpu_flags();
  133. int i;
  134. printf("cpu_flags = 0x%08X\n", cpu_flags);
  135. printf("cpu_flags =");
  136. for (i = 0; cpu_flag_tab[i].flag; i++)
  137. if (cpu_flags & cpu_flag_tab[i].flag)
  138. printf(" %s", cpu_flag_tab[i].name);
  139. printf("\n");
  140. return 0;
  141. }
  142. #endif