audiodsp.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 "libavutil/mem_internal.h"
  26. #include "checkasm.h"
  27. #define MAX_SIZE (32 * 128)
  28. #define randomize_float(buf, len) \
  29. do { \
  30. int i; \
  31. for (i = 0; i < len; i++) { \
  32. float f = (float)rnd() / (UINT_MAX >> 5) - 16.0f; \
  33. buf[i] = f; \
  34. } \
  35. } while (0)
  36. #define randomize_int(buf, len, size, bits) \
  37. do { \
  38. int i; \
  39. for (i = 0; i < len; i++) { \
  40. uint ## size ## _t r = rnd() & ((1LL << bits) - 1); \
  41. AV_WN ## size ## A(buf + i, -(1LL << (bits - 1)) + r); \
  42. } \
  43. } while (0)
  44. void checkasm_check_audiodsp(void)
  45. {
  46. AudioDSPContext adsp;
  47. ff_audiodsp_init(&adsp);
  48. if (check_func(adsp.scalarproduct_int16, "audiodsp.scalarproduct_int16")) {
  49. LOCAL_ALIGNED(32, int16_t, v1, [MAX_SIZE]);
  50. LOCAL_ALIGNED(32, int16_t, v2, [MAX_SIZE]);
  51. unsigned int len_bits_minus4, v1_bits, v2_bits, len;
  52. int32_t res0, res1;
  53. declare_func(int32_t, const int16_t *v1, const int16_t *v2, int len);
  54. // generate random 5-12bit vector length
  55. len_bits_minus4 = rnd() % 8;
  56. len = rnd() & ((1 << len_bits_minus4) - 1);
  57. len = 16 * FFMAX(len, 1);
  58. // generate the bit counts for each of the vectors such that the result
  59. // fits into int32
  60. v1_bits = 1 + rnd() % 15;
  61. v2_bits = FFMIN(32 - (len_bits_minus4 + 4) - v1_bits - 1, 15);
  62. randomize_int(v1, MAX_SIZE, 16, v1_bits + 1);
  63. randomize_int(v2, MAX_SIZE, 16, v2_bits + 1);
  64. res0 = call_ref(v1, v2, len);
  65. res1 = call_new(v1, v2, len);
  66. if (res0 != res1)
  67. fail();
  68. bench_new(v1, v2, MAX_SIZE);
  69. }
  70. if (check_func(adsp.vector_clip_int32, "audiodsp.vector_clip_int32")) {
  71. LOCAL_ALIGNED(32, int32_t, src, [MAX_SIZE]);
  72. LOCAL_ALIGNED(32, int32_t, dst0, [MAX_SIZE]);
  73. LOCAL_ALIGNED(32, int32_t, dst1, [MAX_SIZE]);
  74. int32_t val1, val2, min, max;
  75. int len;
  76. declare_func(void, int32_t *dst, const int32_t *src,
  77. int32_t min, int32_t max, unsigned int len);
  78. val1 = ((int32_t)rnd());
  79. val1 = FFSIGN(val1) * (val1 & ((1 << 24) - 1));
  80. val2 = ((int32_t)rnd());
  81. val2 = FFSIGN(val2) * (val2 & ((1 << 24) - 1));
  82. min = FFMIN(val1, val2);
  83. max = FFMAX(val1, val2);
  84. randomize_int(src, MAX_SIZE, 32, 32);
  85. len = rnd() % 128;
  86. len = 32 * FFMAX(len, 1);
  87. call_ref(dst0, src, min, max, len);
  88. call_new(dst1, src, min, max, len);
  89. if (memcmp(dst0, dst1, len * sizeof(*dst0)))
  90. fail();
  91. bench_new(dst1, src, min, max, MAX_SIZE);
  92. }
  93. if (check_func(adsp.vector_clipf, "audiodsp.vector_clipf")) {
  94. LOCAL_ALIGNED(32, float, src, [MAX_SIZE]);
  95. LOCAL_ALIGNED(32, float, dst0, [MAX_SIZE]);
  96. LOCAL_ALIGNED(32, float, dst1, [MAX_SIZE]);
  97. float val1, val2, min, max;
  98. int i, len;
  99. declare_func(void, float *dst, const float *src,
  100. int len, float min, float max);
  101. val1 = (float)rnd() / (UINT_MAX >> 1) - 1.0f;
  102. val2 = (float)rnd() / (UINT_MAX >> 1) - 1.0f;
  103. min = FFMIN(val1, val2);
  104. max = FFMAX(val1, val2);
  105. randomize_float(src, MAX_SIZE);
  106. len = rnd() % 128;
  107. len = 16 * FFMAX(len, 1);
  108. call_ref(dst0, src, len, min, max);
  109. call_new(dst1, src, len, min, max);
  110. for (i = 0; i < len; i++) {
  111. if (!float_near_ulp_array(dst0, dst1, 3, len))
  112. fail();
  113. }
  114. bench_new(dst1, src, MAX_SIZE, min, max);
  115. }
  116. report("audiodsp");
  117. }