aacpsdsp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 "libavcodec/aacpsdsp.h"
  19. #include "libavutil/intfloat.h"
  20. #include "libavutil/mem_internal.h"
  21. #include "checkasm.h"
  22. #define N 32
  23. #define STRIDE 128
  24. #define BUF_SIZE (N * STRIDE)
  25. #define randomize(buf, len) do { \
  26. int i; \
  27. for (i = 0; i < len; i++) { \
  28. const INTFLOAT f = (INTFLOAT)rnd() / UINT_MAX; \
  29. (buf)[i] = f; \
  30. } \
  31. } while (0)
  32. #define EPS 0.005
  33. static void clear_less_significant_bits(INTFLOAT *buf, int len, int bits)
  34. {
  35. int i;
  36. for (i = 0; i < len; i++) {
  37. union av_intfloat32 u = { .f = buf[i] };
  38. u.i &= (0xffffffff << bits);
  39. buf[i] = u.f;
  40. }
  41. }
  42. static void test_add_squares(void)
  43. {
  44. LOCAL_ALIGNED_16(INTFLOAT, dst0, [BUF_SIZE]);
  45. LOCAL_ALIGNED_16(INTFLOAT, dst1, [BUF_SIZE]);
  46. LOCAL_ALIGNED_16(INTFLOAT, src, [BUF_SIZE], [2]);
  47. declare_func(void, INTFLOAT *dst,
  48. const INTFLOAT (*src)[2], int n);
  49. randomize((INTFLOAT *)src, BUF_SIZE * 2);
  50. randomize(dst0, BUF_SIZE);
  51. memcpy(dst1, dst0, BUF_SIZE * sizeof(INTFLOAT));
  52. call_ref(dst0, src, BUF_SIZE);
  53. call_new(dst1, src, BUF_SIZE);
  54. if (!float_near_abs_eps_array(dst0, dst1, EPS, BUF_SIZE))
  55. fail();
  56. bench_new(dst1, src, BUF_SIZE);
  57. }
  58. static void test_mul_pair_single(void)
  59. {
  60. LOCAL_ALIGNED_16(INTFLOAT, dst0, [BUF_SIZE], [2]);
  61. LOCAL_ALIGNED_16(INTFLOAT, dst1, [BUF_SIZE], [2]);
  62. LOCAL_ALIGNED_16(INTFLOAT, src0, [BUF_SIZE], [2]);
  63. LOCAL_ALIGNED_16(INTFLOAT, src1, [BUF_SIZE]);
  64. declare_func(void, INTFLOAT (*dst)[2],
  65. INTFLOAT (*src0)[2], INTFLOAT *src1, int n);
  66. randomize((INTFLOAT *)src0, BUF_SIZE * 2);
  67. randomize(src1, BUF_SIZE);
  68. call_ref(dst0, src0, src1, BUF_SIZE);
  69. call_new(dst1, src0, src1, BUF_SIZE);
  70. if (!float_near_abs_eps_array((float *)dst0, (float *)dst1, EPS, BUF_SIZE * 2))
  71. fail();
  72. bench_new(dst1, src0, src1, BUF_SIZE);
  73. }
  74. static void test_hybrid_analysis(void)
  75. {
  76. LOCAL_ALIGNED_16(INTFLOAT, dst0, [BUF_SIZE], [2]);
  77. LOCAL_ALIGNED_16(INTFLOAT, dst1, [BUF_SIZE], [2]);
  78. LOCAL_ALIGNED_16(INTFLOAT, in, [13], [2]);
  79. LOCAL_ALIGNED_16(INTFLOAT, filter, [N], [8][2]);
  80. declare_func(void, INTFLOAT (*out)[2], INTFLOAT (*in)[2],
  81. const INTFLOAT (*filter)[8][2],
  82. ptrdiff_t stride, int n);
  83. randomize((INTFLOAT *)in, 13 * 2);
  84. randomize((INTFLOAT *)filter, N * 8 * 2);
  85. randomize((INTFLOAT *)dst0, BUF_SIZE * 2);
  86. memcpy(dst1, dst0, BUF_SIZE * 2 * sizeof(INTFLOAT));
  87. call_ref(dst0, in, filter, STRIDE, N);
  88. call_new(dst1, in, filter, STRIDE, N);
  89. if (!float_near_abs_eps_array((float *)dst0, (float *)dst1, EPS, BUF_SIZE * 2))
  90. fail();
  91. bench_new(dst1, in, filter, STRIDE, N);
  92. }
  93. static void test_hybrid_analysis_ileave(void)
  94. {
  95. LOCAL_ALIGNED_16(INTFLOAT, in, [2], [38][64]);
  96. LOCAL_ALIGNED_16(INTFLOAT, out0, [91], [32][2]);
  97. LOCAL_ALIGNED_16(INTFLOAT, out1, [91], [32][2]);
  98. declare_func(void, INTFLOAT (*out)[32][2], INTFLOAT L[2][38][64],
  99. int i, int len);
  100. randomize((INTFLOAT *)out0, 91 * 32 * 2);
  101. randomize((INTFLOAT *)in, 2 * 38 * 64);
  102. memcpy(out1, out0, 91 * 32 * 2 * sizeof(INTFLOAT));
  103. /* len is hardcoded to 32 as that's the only value used in
  104. libavcodec. asm functions are likely to be optimized
  105. hardcoding this value in their loops and could fail with
  106. anything else.
  107. i is hardcoded to the two values currently used by the
  108. aac decoder because the arm neon implementation is
  109. micro-optimized for them and will fail for almost every
  110. other value. */
  111. call_ref(out0, in, 3, 32);
  112. call_new(out1, in, 3, 32);
  113. /* the function just moves data around, so memcmp is enough */
  114. if (memcmp(out0, out1, 91 * 32 * 2 * sizeof(INTFLOAT)))
  115. fail();
  116. call_ref(out0, in, 5, 32);
  117. call_new(out1, in, 5, 32);
  118. if (memcmp(out0, out1, 91 * 32 * 2 * sizeof(INTFLOAT)))
  119. fail();
  120. bench_new(out1, in, 3, 32);
  121. }
  122. static void test_hybrid_synthesis_deint(void)
  123. {
  124. LOCAL_ALIGNED_16(INTFLOAT, out0, [2], [38][64]);
  125. LOCAL_ALIGNED_16(INTFLOAT, out1, [2], [38][64]);
  126. LOCAL_ALIGNED_16(INTFLOAT, in, [91], [32][2]);
  127. declare_func(void, INTFLOAT out[2][38][64], INTFLOAT (*in)[32][2],
  128. int i, int len);
  129. randomize((INTFLOAT *)in, 91 * 32 * 2);
  130. randomize((INTFLOAT *)out0, 2 * 38 * 64);
  131. memcpy(out1, out0, 2 * 38 * 64 * sizeof(INTFLOAT));
  132. /* len is hardcoded to 32 as that's the only value used in
  133. libavcodec. asm functions are likely to be optimized
  134. hardcoding this value in their loops and could fail with
  135. anything else.
  136. i is hardcoded to the two values currently used by the
  137. aac decoder because the arm neon implementation is
  138. micro-optimized for them and will fail for almost every
  139. other value. */
  140. call_ref(out0, in, 3, 32);
  141. call_new(out1, in, 3, 32);
  142. /* the function just moves data around, so memcmp is enough */
  143. if (memcmp(out0, out1, 2 * 38 * 64 * sizeof(INTFLOAT)))
  144. fail();
  145. call_ref(out0, in, 5, 32);
  146. call_new(out1, in, 5, 32);
  147. if (memcmp(out0, out1, 2 * 38 * 64 * sizeof(INTFLOAT)))
  148. fail();
  149. bench_new(out1, in, 3, 32);
  150. }
  151. static void test_stereo_interpolate(PSDSPContext *psdsp)
  152. {
  153. int i;
  154. LOCAL_ALIGNED_16(INTFLOAT, l, [BUF_SIZE], [2]);
  155. LOCAL_ALIGNED_16(INTFLOAT, r, [BUF_SIZE], [2]);
  156. LOCAL_ALIGNED_16(INTFLOAT, l0, [BUF_SIZE], [2]);
  157. LOCAL_ALIGNED_16(INTFLOAT, r0, [BUF_SIZE], [2]);
  158. LOCAL_ALIGNED_16(INTFLOAT, l1, [BUF_SIZE], [2]);
  159. LOCAL_ALIGNED_16(INTFLOAT, r1, [BUF_SIZE], [2]);
  160. LOCAL_ALIGNED_16(INTFLOAT, h, [2], [4]);
  161. LOCAL_ALIGNED_16(INTFLOAT, h_step, [2], [4]);
  162. declare_func(void, INTFLOAT (*l)[2], INTFLOAT (*r)[2],
  163. INTFLOAT h[2][4], INTFLOAT h_step[2][4], int len);
  164. randomize((INTFLOAT *)l, BUF_SIZE * 2);
  165. randomize((INTFLOAT *)r, BUF_SIZE * 2);
  166. for (i = 0; i < 2; i++) {
  167. if (check_func(psdsp->stereo_interpolate[i], "ps_stereo_interpolate%s", i ? "_ipdopd" : "")) {
  168. memcpy(l0, l, BUF_SIZE * 2 * sizeof(INTFLOAT));
  169. memcpy(l1, l, BUF_SIZE * 2 * sizeof(INTFLOAT));
  170. memcpy(r0, r, BUF_SIZE * 2 * sizeof(INTFLOAT));
  171. memcpy(r1, r, BUF_SIZE * 2 * sizeof(INTFLOAT));
  172. randomize((INTFLOAT *)h, 2 * 4);
  173. randomize((INTFLOAT *)h_step, 2 * 4);
  174. // Clear the least significant 14 bits of h_step, to avoid
  175. // divergence when accumulating h_step BUF_SIZE times into
  176. // a float variable which may or may not have extra intermediate
  177. // precision. Therefore clear roughly log2(BUF_SIZE) less
  178. // significant bits, to get the same result regardless of any
  179. // extra precision in the accumulator.
  180. clear_less_significant_bits((INTFLOAT *)h_step, 2 * 4, 14);
  181. call_ref(l0, r0, h, h_step, BUF_SIZE);
  182. call_new(l1, r1, h, h_step, BUF_SIZE);
  183. if (!float_near_abs_eps_array((float *)l0, (float *)l1, EPS, BUF_SIZE * 2) ||
  184. !float_near_abs_eps_array((float *)r0, (float *)r1, EPS, BUF_SIZE * 2))
  185. fail();
  186. memcpy(l1, l, BUF_SIZE * 2 * sizeof(INTFLOAT));
  187. memcpy(r1, r, BUF_SIZE * 2 * sizeof(INTFLOAT));
  188. bench_new(l1, r1, h, h_step, BUF_SIZE);
  189. }
  190. }
  191. }
  192. void checkasm_check_aacpsdsp(void)
  193. {
  194. PSDSPContext psdsp;
  195. ff_psdsp_init(&psdsp);
  196. if (check_func(psdsp.add_squares, "ps_add_squares"))
  197. test_add_squares();
  198. report("add_squares");
  199. if (check_func(psdsp.mul_pair_single, "ps_mul_pair_single"))
  200. test_mul_pair_single();
  201. report("mul_pair_single");
  202. if (check_func(psdsp.hybrid_analysis, "ps_hybrid_analysis"))
  203. test_hybrid_analysis();
  204. report("hybrid_analysis");
  205. if (check_func(psdsp.hybrid_analysis_ileave, "ps_hybrid_analysis_ileave"))
  206. test_hybrid_analysis_ileave();
  207. report("hybrid_analysis_ileave");
  208. if (check_func(psdsp.hybrid_synthesis_deint, "ps_hybrid_synthesis_deint"))
  209. test_hybrid_synthesis_deint();
  210. report("hybrid_synthesis_deint");
  211. test_stereo_interpolate(&psdsp);
  212. report("stereo_interpolate");
  213. }