flacdsp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2015 James Almer
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with FFmpeg; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. */
  20. #include <string.h>
  21. #include "checkasm.h"
  22. #include "libavcodec/flacdsp.h"
  23. #include "libavcodec/mathops.h"
  24. #include "libavutil/common.h"
  25. #include "libavutil/internal.h"
  26. #include "libavutil/intreadwrite.h"
  27. #include "libavutil/mem_internal.h"
  28. #define BUF_SIZE 256
  29. #define MAX_CHANNELS 8
  30. #define randomize_buffers() \
  31. do { \
  32. int i, j; \
  33. for (i = 0; i < BUF_SIZE; i += 4) { \
  34. for (j = 0; j < channels; j++) { \
  35. uint32_t r = rnd() & (1 << (bits - 2)) - 1; \
  36. AV_WN32A(ref_src[j] + i, r); \
  37. AV_WN32A(new_src[j] + i, r); \
  38. } \
  39. } \
  40. } while (0)
  41. static void check_decorrelate(uint8_t **ref_dst, uint8_t **ref_src, uint8_t **new_dst, uint8_t **new_src,
  42. int channels, int bits) {
  43. declare_func(void, uint8_t **out, int32_t **in, int channels, int len, int shift);
  44. randomize_buffers();
  45. call_ref(ref_dst, (int32_t **)ref_src, channels, BUF_SIZE / sizeof(int32_t), 8);
  46. call_new(new_dst, (int32_t **)new_src, channels, BUF_SIZE / sizeof(int32_t), 8);
  47. if (memcmp(*ref_dst, *new_dst, bits == 16 ? BUF_SIZE * (channels/2) : BUF_SIZE * channels) ||
  48. memcmp(*ref_src, *new_src, BUF_SIZE * channels))
  49. fail();
  50. bench_new(new_dst, (int32_t **)new_src, channels, BUF_SIZE / sizeof(int32_t), 8);
  51. }
  52. static void check_lpc(int pred_order, int bps)
  53. {
  54. int qlevel = rnd() % 16;
  55. int coeff_prec = (rnd() % 15) + 1;
  56. LOCAL_ALIGNED_16(int32_t, coeffs, [32]);
  57. LOCAL_ALIGNED_16(int32_t, dst, [BUF_SIZE]);
  58. LOCAL_ALIGNED_16(int32_t, dst0, [BUF_SIZE]);
  59. LOCAL_ALIGNED_16(int32_t, dst1, [BUF_SIZE]);
  60. declare_func(void, int32_t *, const int[32], int, int, int);
  61. if (bps <= 16)
  62. coeff_prec = av_clip(coeff_prec, 0, 32 - bps - av_log2(pred_order));
  63. for (int i = 0; i < 32; i++)
  64. coeffs[i] = sign_extend(rnd(), coeff_prec);
  65. for (int i = 0; i < BUF_SIZE; i++)
  66. dst[i] = sign_extend(rnd(), bps);
  67. memcpy(dst0, dst, BUF_SIZE * sizeof (int32_t));
  68. memcpy(dst1, dst, BUF_SIZE * sizeof (int32_t));
  69. call_ref(dst0, coeffs, pred_order, qlevel, BUF_SIZE);
  70. call_new(dst1, coeffs, pred_order, qlevel, BUF_SIZE);
  71. if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int32_t)) != 0)
  72. fail();
  73. bench_new(dst, coeffs, pred_order, qlevel, BUF_SIZE);
  74. }
  75. static void check_lpc33(int pred_order)
  76. {
  77. int qlevel = rnd() % 16;
  78. int coeff_prec = (rnd() % 15) + 1;
  79. LOCAL_ALIGNED_16(int64_t, dst, [BUF_SIZE]);
  80. LOCAL_ALIGNED_16(int64_t, dst0, [BUF_SIZE]);
  81. LOCAL_ALIGNED_16(int64_t, dst1, [BUF_SIZE]);
  82. LOCAL_ALIGNED_16(int32_t, residuals, [BUF_SIZE]);
  83. LOCAL_ALIGNED_16(int32_t, coeffs, [32]);
  84. declare_func(void, int64_t *, const int32_t *, const int[32], int, int, int);
  85. for (int i = 0; i < 32; i++)
  86. coeffs[i] = sign_extend(rnd(), coeff_prec);
  87. for (int i = 0; i < BUF_SIZE; i++) {
  88. residuals[i] = sign_extend(rnd(), pred_order);
  89. dst[i] = sign_extend64(((int64_t)rnd() << 1) | (rnd() & 1), 33);
  90. }
  91. memcpy(dst0, dst, BUF_SIZE * sizeof (int64_t));
  92. memcpy(dst1, dst, BUF_SIZE * sizeof (int64_t));
  93. call_ref(dst0, residuals, coeffs, pred_order, qlevel, BUF_SIZE);
  94. call_new(dst1, residuals, coeffs, pred_order, qlevel, BUF_SIZE);
  95. if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int64_t)) != 0)
  96. fail();
  97. bench_new(dst, residuals, coeffs, pred_order, qlevel, BUF_SIZE);
  98. }
  99. static void check_wasted32(void)
  100. {
  101. int wasted = rnd() % 32;
  102. LOCAL_ALIGNED_16(int32_t, dst, [BUF_SIZE]);
  103. LOCAL_ALIGNED_16(int32_t, dst0, [BUF_SIZE]);
  104. LOCAL_ALIGNED_16(int32_t, dst1, [BUF_SIZE]);
  105. declare_func(void, int32_t *, int, int);
  106. for (int i = 0; i < BUF_SIZE; i++)
  107. dst[i] = rnd();
  108. memcpy(dst0, dst, BUF_SIZE * sizeof (int32_t));
  109. memcpy(dst1, dst, BUF_SIZE * sizeof (int32_t));
  110. call_ref(dst0, wasted, BUF_SIZE);
  111. call_new(dst1, wasted, BUF_SIZE);
  112. if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int32_t)) != 0)
  113. fail();
  114. bench_new(dst, wasted, BUF_SIZE);
  115. }
  116. static void check_wasted33(void)
  117. {
  118. int wasted = rnd() % 33;
  119. LOCAL_ALIGNED_16(int64_t, dst0, [BUF_SIZE]);
  120. LOCAL_ALIGNED_16(int64_t, dst1, [BUF_SIZE]);
  121. LOCAL_ALIGNED_16(int32_t, residuals, [BUF_SIZE]);
  122. declare_func(void, int64_t *, const int32_t *, int, int);
  123. for (int i = 0; i < BUF_SIZE; i++)
  124. residuals[i] = rnd();
  125. call_ref(dst0, residuals, wasted, BUF_SIZE);
  126. call_new(dst1, residuals, wasted, BUF_SIZE);
  127. if (memcmp(dst0, dst1, BUF_SIZE * sizeof (int64_t)) != 0)
  128. fail();
  129. bench_new(dst0, residuals, wasted, BUF_SIZE);
  130. }
  131. void checkasm_check_flacdsp(void)
  132. {
  133. LOCAL_ALIGNED_16(uint8_t, ref_dst, [BUF_SIZE*MAX_CHANNELS]);
  134. LOCAL_ALIGNED_16(uint8_t, ref_buf, [BUF_SIZE*MAX_CHANNELS]);
  135. LOCAL_ALIGNED_16(uint8_t, new_dst, [BUF_SIZE*MAX_CHANNELS]);
  136. LOCAL_ALIGNED_16(uint8_t, new_buf, [BUF_SIZE*MAX_CHANNELS]);
  137. uint8_t *ref_src[] = { &ref_buf[BUF_SIZE*0], &ref_buf[BUF_SIZE*1], &ref_buf[BUF_SIZE*2], &ref_buf[BUF_SIZE*3],
  138. &ref_buf[BUF_SIZE*4], &ref_buf[BUF_SIZE*5], &ref_buf[BUF_SIZE*6], &ref_buf[BUF_SIZE*7] };
  139. uint8_t *new_src[] = { &new_buf[BUF_SIZE*0], &new_buf[BUF_SIZE*1], &new_buf[BUF_SIZE*2], &new_buf[BUF_SIZE*3],
  140. &new_buf[BUF_SIZE*4], &new_buf[BUF_SIZE*5], &new_buf[BUF_SIZE*6], &new_buf[BUF_SIZE*7] };
  141. static const char * const names[3] = { "ls", "rs", "ms" };
  142. static const struct {
  143. enum AVSampleFormat fmt;
  144. int bits;
  145. } fmts[] = {
  146. { AV_SAMPLE_FMT_S16, 16 },
  147. { AV_SAMPLE_FMT_S32, 32 },
  148. };
  149. static const signed char pred_orders[] = { 13, 16, 29, 32 };
  150. FLACDSPContext h;
  151. int i, j;
  152. for (i = 0; i < 2; i++) {
  153. ff_flacdsp_init(&h, fmts[i].fmt, 2);
  154. for (j = 0; j < 3; j++)
  155. if (check_func(h.decorrelate[j + 1], "flac_decorrelate_%s_%d", names[j], fmts[i].bits))
  156. check_decorrelate(&ref_dst, ref_src, &new_dst, new_src, 2, fmts[i].bits);
  157. for (j = 2; j <= MAX_CHANNELS; j += 2) {
  158. ff_flacdsp_init(&h, fmts[i].fmt, j);
  159. if (check_func(h.decorrelate[0], "flac_decorrelate_indep%d_%d", j, fmts[i].bits))
  160. check_decorrelate(&ref_dst, ref_src, &new_dst, new_src, j, fmts[i].bits);
  161. }
  162. }
  163. report("decorrelate");
  164. for (i = 0; i < FF_ARRAY_ELEMS(pred_orders); i++)
  165. if (check_func(h.lpc16, "flac_lpc_16_%d", pred_orders[i]))
  166. check_lpc(pred_orders[i], 16);
  167. for (i = 0; i < FF_ARRAY_ELEMS(pred_orders); i++)
  168. if (check_func(h.lpc32, "flac_lpc_32_%d", pred_orders[i]))
  169. check_lpc(pred_orders[i], 32);
  170. for (i = 0; i < FF_ARRAY_ELEMS(pred_orders); i++)
  171. if (check_func(h.lpc33, "flac_lpc_33_%d", pred_orders[i]))
  172. check_lpc33(pred_orders[i]);
  173. report("lpc");
  174. if (check_func(h.wasted32, "flac_wasted_32"))
  175. check_wasted32();
  176. if (check_func(h.wasted33, "flac_wasted_33"))
  177. check_wasted33();
  178. report("wasted");
  179. }