cpu.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 <stdio.h>
  19. #include "config.h"
  20. #include "libavutil/cpu.h"
  21. #include "libavutil/avstring.h"
  22. #if HAVE_UNISTD_H
  23. #include <unistd.h>
  24. #endif
  25. #if !HAVE_GETOPT
  26. #include "compat/getopt.c"
  27. #endif
  28. static const struct {
  29. int flag;
  30. const char *name;
  31. } cpu_flag_tab[] = {
  32. #if ARCH_AARCH64
  33. { AV_CPU_FLAG_ARMV8, "armv8" },
  34. { AV_CPU_FLAG_NEON, "neon" },
  35. { AV_CPU_FLAG_VFP, "vfp" },
  36. #elif ARCH_ARM
  37. { AV_CPU_FLAG_ARMV5TE, "armv5te" },
  38. { AV_CPU_FLAG_ARMV6, "armv6" },
  39. { AV_CPU_FLAG_ARMV6T2, "armv6t2" },
  40. { AV_CPU_FLAG_VFP, "vfp" },
  41. { AV_CPU_FLAG_VFP_VM, "vfp_vm" },
  42. { AV_CPU_FLAG_VFPV3, "vfpv3" },
  43. { AV_CPU_FLAG_NEON, "neon" },
  44. { AV_CPU_FLAG_SETEND, "setend" },
  45. #elif ARCH_PPC
  46. { AV_CPU_FLAG_ALTIVEC, "altivec" },
  47. #elif ARCH_X86
  48. { AV_CPU_FLAG_MMX, "mmx" },
  49. { AV_CPU_FLAG_MMXEXT, "mmxext" },
  50. { AV_CPU_FLAG_SSE, "sse" },
  51. { AV_CPU_FLAG_SSE2, "sse2" },
  52. { AV_CPU_FLAG_SSE2SLOW, "sse2slow" },
  53. { AV_CPU_FLAG_SSE3, "sse3" },
  54. { AV_CPU_FLAG_SSE3SLOW, "sse3slow" },
  55. { AV_CPU_FLAG_SSSE3, "ssse3" },
  56. { AV_CPU_FLAG_ATOM, "atom" },
  57. { AV_CPU_FLAG_SSE4, "sse4.1" },
  58. { AV_CPU_FLAG_SSE42, "sse4.2" },
  59. { AV_CPU_FLAG_AVX, "avx" },
  60. { AV_CPU_FLAG_AVXSLOW, "avxslow" },
  61. { AV_CPU_FLAG_XOP, "xop" },
  62. { AV_CPU_FLAG_FMA3, "fma3" },
  63. { AV_CPU_FLAG_FMA4, "fma4" },
  64. { AV_CPU_FLAG_3DNOW, "3dnow" },
  65. { AV_CPU_FLAG_3DNOWEXT, "3dnowext" },
  66. { AV_CPU_FLAG_CMOV, "cmov" },
  67. { AV_CPU_FLAG_AVX2, "avx2" },
  68. { AV_CPU_FLAG_BMI1, "bmi1" },
  69. { AV_CPU_FLAG_BMI2, "bmi2" },
  70. { AV_CPU_FLAG_AESNI, "aesni" },
  71. { AV_CPU_FLAG_AVX512, "avx512" },
  72. #endif
  73. { 0 }
  74. };
  75. static void print_cpu_flags(int cpu_flags, const char *type)
  76. {
  77. int i;
  78. printf("cpu_flags(%s) = 0x%08X\n", type, cpu_flags);
  79. printf("cpu_flags_str(%s) =", type);
  80. for (i = 0; cpu_flag_tab[i].flag; i++)
  81. if (cpu_flags & cpu_flag_tab[i].flag)
  82. printf(" %s", cpu_flag_tab[i].name);
  83. printf("\n");
  84. }
  85. int main(int argc, char **argv)
  86. {
  87. int cpu_flags_raw = av_get_cpu_flags();
  88. int cpu_flags_eff;
  89. int cpu_count = av_cpu_count();
  90. char threads[5] = "auto";
  91. int i;
  92. for(i = 0; cpu_flag_tab[i].flag; i++) {
  93. unsigned tmp = 0;
  94. if (av_parse_cpu_caps(&tmp, cpu_flag_tab[i].name) < 0) {
  95. fprintf(stderr, "Table missing %s\n", cpu_flag_tab[i].name);
  96. return 4;
  97. }
  98. }
  99. if (cpu_flags_raw < 0)
  100. return 1;
  101. for (;;) {
  102. int c = getopt(argc, argv, "c:t:");
  103. if (c == -1)
  104. break;
  105. switch (c) {
  106. case 'c':
  107. {
  108. unsigned flags = av_get_cpu_flags();
  109. if (av_parse_cpu_caps(&flags, optarg) < 0)
  110. return 2;
  111. av_force_cpu_flags(flags);
  112. break;
  113. }
  114. case 't':
  115. {
  116. int len = av_strlcpy(threads, optarg, sizeof(threads));
  117. if (len >= sizeof(threads)) {
  118. fprintf(stderr, "Invalid thread count '%s'\n", optarg);
  119. return 2;
  120. }
  121. }
  122. }
  123. }
  124. cpu_flags_eff = av_get_cpu_flags();
  125. if (cpu_flags_eff < 0)
  126. return 3;
  127. print_cpu_flags(cpu_flags_raw, "raw");
  128. print_cpu_flags(cpu_flags_eff, "effective");
  129. printf("threads = %s (cpu_count = %d)\n", threads, cpu_count);
  130. return 0;
  131. }