cpu.c 9.3 KB

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