opusdsp.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/mem_internal.h"
  19. #include "libavcodec/opusdsp.h"
  20. #include "libavcodec/opustab.h"
  21. #include "checkasm.h"
  22. #define randomize_float(buf, len) \
  23. do { \
  24. for (int i = 0; i < len; i++) { \
  25. float f = (float)rnd() / (UINT_MAX >> 5) - 16.0f; \
  26. buf[i] = f; \
  27. } \
  28. } while (0)
  29. #define EPS 0.005
  30. #define MAX_SIZE (960)
  31. /* period is between 15 and 1022, inclusive */
  32. static void test_postfilter(int period)
  33. {
  34. LOCAL_ALIGNED(16, float, data0, [MAX_SIZE + 1024]);
  35. LOCAL_ALIGNED(16, float, data1, [MAX_SIZE + 1024]);
  36. /* This filter can explode very easily, so use a tapset from the codec.
  37. * In the codec these are usually multiplied by at least 0.09375f,
  38. * so its outside the largest filter value, but the filter is still stable
  39. * so use it. */
  40. float gains[3] = { 0.3066406250f, 0.2170410156f, 0.1296386719f };
  41. /* The codec will always call with an offset which is aligned once
  42. * (period + 2) is subtracted, but here we have to align it outselves. */
  43. int offset = FFALIGN(period + 2, 4);
  44. declare_func(void, float *data, int period, float *gains, int len);
  45. randomize_float(data0, MAX_SIZE + 1024);
  46. memcpy(data1, data0, (MAX_SIZE + 1024)*sizeof(float));
  47. call_ref(data0 + offset, period, gains, MAX_SIZE);
  48. call_new(data1 + offset, period, gains, MAX_SIZE);
  49. if (!float_near_abs_eps_array(data0 + offset, data1 + offset, EPS, MAX_SIZE))
  50. fail();
  51. bench_new(data1 + offset, period, gains, MAX_SIZE);
  52. }
  53. static void test_deemphasis(void)
  54. {
  55. LOCAL_ALIGNED(16, float, src, [FFALIGN(MAX_SIZE, 4)]);
  56. LOCAL_ALIGNED(16, float, dst0, [FFALIGN(MAX_SIZE, 4)]);
  57. LOCAL_ALIGNED(16, float, dst1, [FFALIGN(MAX_SIZE, 4)]);
  58. float coeff0 = (float)rnd() / (UINT_MAX >> 5) - 16.0f, coeff1 = coeff0;
  59. declare_func_float(float, float *out, float *in, float coeff, const float *weights, int len);
  60. randomize_float(src, MAX_SIZE);
  61. coeff0 = call_ref(dst0, src, coeff0, ff_opus_deemph_weights, MAX_SIZE);
  62. coeff1 = call_new(dst1, src, coeff1, ff_opus_deemph_weights, MAX_SIZE);
  63. if (!float_near_abs_eps(coeff0, coeff1, EPS) ||
  64. !float_near_abs_eps_array(dst0, dst1, EPS, MAX_SIZE))
  65. fail();
  66. bench_new(dst1, src, coeff1, ff_opus_deemph_weights, MAX_SIZE);
  67. }
  68. void checkasm_check_opusdsp(void)
  69. {
  70. OpusDSP ctx;
  71. ff_opus_dsp_init(&ctx);
  72. if (check_func(ctx.postfilter, "postfilter_15"))
  73. test_postfilter(15);
  74. report("postfilter_15");
  75. if (check_func(ctx.postfilter, "postfilter_512"))
  76. test_postfilter(512);
  77. report("postfilter_512");
  78. if (check_func(ctx.postfilter, "postfilter_1022"))
  79. test_postfilter(1022);
  80. report("postfilter_1022");
  81. if (check_func(ctx.deemphasis, "deemphasis"))
  82. test_deemphasis();
  83. report("deemphasis");
  84. }