aacpsdsp.c 9.0 KB

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