lpc.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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 "libavutil/avassert.h"
  19. #include "libavutil/mem_internal.h"
  20. #include "libavcodec/lpc.h"
  21. #include "checkasm.h"
  22. #define randomize_int32(buf, len) \
  23. do { \
  24. for (int i = 0; i < len; i++) { \
  25. int32_t f = ((int)(UINT32_MAX >> 17)) - ((int)(rnd() >> 16)); \
  26. buf[i] = f; \
  27. } \
  28. } while (0)
  29. #define EPS 0.005
  30. static void test_window(int len)
  31. {
  32. LOCAL_ALIGNED(16, int32_t, src, [5000]);
  33. LOCAL_ALIGNED(16, double, dst0, [5000]);
  34. LOCAL_ALIGNED(16, double, dst1, [5000]);
  35. declare_func(void, const int32_t *in, ptrdiff_t len, double *out);
  36. randomize_int32(src, len);
  37. call_ref(src, len, dst0);
  38. call_new(src, len, dst1);
  39. for (int i = 0; i < len; i++) {
  40. if (!double_near_abs_eps(dst0[i], dst1[i], EPS)) {
  41. fprintf(stderr, "%d: %- .12f - %- .12f = % .12g\n",
  42. i, dst0[i], dst1[i], dst0[i] - dst1[i]);
  43. fail();
  44. break;
  45. }
  46. }
  47. bench_new(src, 4608 + (len & 1), dst1);
  48. }
  49. static void test_compute_autocorr(ptrdiff_t len, int lag)
  50. {
  51. const double eps = EPS * (double)len;
  52. LOCAL_ALIGNED(32, double, src, [5000 + 2 + MAX_LPC_ORDER]);
  53. LOCAL_ALIGNED(16, double, dst0, [MAX_LPC_ORDER + 1]);
  54. LOCAL_ALIGNED(16, double, dst1, [MAX_LPC_ORDER + 1]);
  55. declare_func(void, const double *in, ptrdiff_t len, int lag, double *out);
  56. av_assert0(lag >= 0 && lag <= MAX_LPC_ORDER);
  57. for (int i = 0; i < MAX_LPC_ORDER; i++)
  58. src[i] = 0.;
  59. src += MAX_LPC_ORDER;
  60. for (int i = 0; i < 5000 + 2; i++) {
  61. src[i] = (double)rnd() / (double)UINT_MAX;
  62. }
  63. call_ref(src, len, lag, dst0);
  64. call_new(src, len, lag, dst1);
  65. for (size_t i = 0; i <= lag; i++) {
  66. if (!double_near_abs_eps(dst0[i], dst1[i], eps)) {
  67. fprintf(stderr, "%zu: %- .12f - %- .12f = % .12g\n",
  68. i, dst0[i], dst1[i], dst0[i] - dst1[i]);
  69. fail();
  70. break;
  71. }
  72. }
  73. bench_new(src, 4608 + (len & 1), lag, dst1);
  74. }
  75. void checkasm_check_lpc(void)
  76. {
  77. LPCContext ctx;
  78. int len = 2000 + rnd() % 3000;
  79. static const int lags[] = { 8, 12, };
  80. ff_lpc_init(&ctx, 32, 16, FF_LPC_TYPE_DEFAULT);
  81. if (check_func(ctx.lpc_apply_welch_window, "apply_welch_window_even")) {
  82. test_window(len & ~1);
  83. }
  84. report("apply_welch_window_even");
  85. if (check_func(ctx.lpc_apply_welch_window, "apply_welch_window_odd")) {
  86. test_window(len | 1);
  87. }
  88. report("apply_welch_window_odd");
  89. ff_lpc_end(&ctx);
  90. for (size_t i = 0; i < FF_ARRAY_ELEMS(lags); i++) {
  91. ff_lpc_init(&ctx, len, lags[i], FF_LPC_TYPE_DEFAULT);
  92. if (check_func(ctx.lpc_compute_autocorr, "autocorr_%d_even", lags[i]))
  93. test_compute_autocorr(len & ~1, lags[i]);
  94. #if !ARCH_X86
  95. if (check_func(ctx.lpc_compute_autocorr, "autocorr_%d_odd", lags[i]))
  96. test_compute_autocorr(len | 1, lags[i]);
  97. #endif
  98. ff_lpc_end(&ctx);
  99. }
  100. report("compute_autocorr");
  101. }