audiodsp.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (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
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along
  15. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  17. */
  18. #include <math.h>
  19. #include <string.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include "libavcodec/audiodsp.h"
  23. #include "libavutil/common.h"
  24. #include "libavutil/intreadwrite.h"
  25. #include "checkasm.h"
  26. #define MAX_SIZE (32 * 128)
  27. #define randomize_float(buf, len) \
  28. do { \
  29. int i; \
  30. for (i = 0; i < len; i++) { \
  31. float f = (float)rnd() / (UINT_MAX >> 5) - 16.0f; \
  32. buf[i] = f; \
  33. } \
  34. } while (0)
  35. #define randomize_int(buf, len, size, bits) \
  36. do { \
  37. int i; \
  38. for (i = 0; i < len; i++) { \
  39. uint ## size ## _t r = rnd() & ((1LL << bits) - 1); \
  40. AV_WN ## size ## A(buf + i, -(1LL << (bits - 1)) + r); \
  41. } \
  42. } while (0)
  43. void checkasm_check_audiodsp(void)
  44. {
  45. AudioDSPContext adsp;
  46. ff_audiodsp_init(&adsp);
  47. if (check_func(adsp.scalarproduct_int16, "audiodsp.scalarproduct_int16")) {
  48. LOCAL_ALIGNED(32, int16_t, v1, [MAX_SIZE]);
  49. LOCAL_ALIGNED(32, int16_t, v2, [MAX_SIZE]);
  50. unsigned int len_bits_minus4, v1_bits, v2_bits, len;
  51. int32_t res0, res1;
  52. declare_func_emms(AV_CPU_FLAG_MMX, int32_t, const int16_t *v1, const int16_t *v2, int len);
  53. // generate random 5-12bit vector length
  54. len_bits_minus4 = rnd() % 8;
  55. len = rnd() & ((1 << len_bits_minus4) - 1);
  56. len = 16 * FFMAX(len, 1);
  57. // generate the bit counts for each of the vectors such that the result
  58. // fits into int32
  59. v1_bits = 1 + rnd() % 15;
  60. v2_bits = FFMIN(32 - (len_bits_minus4 + 4) - v1_bits - 1, 15);
  61. randomize_int(v1, MAX_SIZE, 16, v1_bits + 1);
  62. randomize_int(v2, MAX_SIZE, 16, v2_bits + 1);
  63. res0 = call_ref(v1, v2, len);
  64. res1 = call_new(v1, v2, len);
  65. if (res0 != res1)
  66. fail();
  67. bench_new(v1, v2, MAX_SIZE);
  68. }
  69. if (check_func(adsp.vector_clip_int32, "audiodsp.vector_clip_int32")) {
  70. LOCAL_ALIGNED(32, int32_t, src, [MAX_SIZE]);
  71. LOCAL_ALIGNED(32, int32_t, dst0, [MAX_SIZE]);
  72. LOCAL_ALIGNED(32, int32_t, dst1, [MAX_SIZE]);
  73. int32_t val1, val2, min, max;
  74. int len;
  75. declare_func_emms(AV_CPU_FLAG_MMX, void, int32_t *dst, const int32_t *src,
  76. int32_t min, int32_t max, unsigned int len);
  77. val1 = ((int32_t)rnd());
  78. val1 = FFSIGN(val1) * (val1 & ((1 << 24) - 1));
  79. val2 = ((int32_t)rnd());
  80. val2 = FFSIGN(val2) * (val2 & ((1 << 24) - 1));
  81. min = FFMIN(val1, val2);
  82. max = FFMAX(val1, val2);
  83. randomize_int(src, MAX_SIZE, 32, 32);
  84. len = rnd() % 128;
  85. len = 32 * FFMAX(len, 1);
  86. call_ref(dst0, src, min, max, len);
  87. call_new(dst1, src, min, max, len);
  88. if (memcmp(dst0, dst1, len * sizeof(*dst0)))
  89. fail();
  90. bench_new(dst1, src, min, max, MAX_SIZE);
  91. }
  92. if (check_func(adsp.vector_clipf, "audiodsp.vector_clipf")) {
  93. LOCAL_ALIGNED(32, float, src, [MAX_SIZE]);
  94. LOCAL_ALIGNED(32, float, dst0, [MAX_SIZE]);
  95. LOCAL_ALIGNED(32, float, dst1, [MAX_SIZE]);
  96. float val1, val2, min, max;
  97. int i, len;
  98. declare_func_emms(AV_CPU_FLAG_MMX, void, float *dst, const float *src,
  99. int len, float min, float max);
  100. val1 = (float)rnd() / (UINT_MAX >> 1) - 1.0f;
  101. val2 = (float)rnd() / (UINT_MAX >> 1) - 1.0f;
  102. min = FFMIN(val1, val2);
  103. max = FFMAX(val1, val2);
  104. randomize_float(src, MAX_SIZE);
  105. len = rnd() % 128;
  106. len = 16 * FFMAX(len, 1);
  107. call_ref(dst0, src, len, min, max);
  108. call_new(dst1, src, len, min, max);
  109. for (i = 0; i < len; i++) {
  110. if (!float_near_ulp_array(dst0, dst1, 3, len))
  111. fail();
  112. }
  113. bench_new(dst1, src, MAX_SIZE, min, max);
  114. }
  115. report("audiodsp");
  116. }