av_tx.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 "libavutil/tx.h"
  20. #include "libavutil/error.h"
  21. #include "checkasm.h"
  22. #include <stdlib.h>
  23. #define EPS 0.0005
  24. #define SCALE_NOOP(x) (x)
  25. #define SCALE_INT20(x) (av_clip64(lrintf((x) * 2147483648.0), INT32_MIN, INT32_MAX) >> 12)
  26. #define randomize_complex(BUF, LEN, TYPE, SCALE) \
  27. do { \
  28. TYPE *buf = (TYPE *)BUF; \
  29. for (int i = 0; i < LEN; i++) { \
  30. double fre = (double)rnd() / UINT_MAX; \
  31. double fim = (double)rnd() / UINT_MAX; \
  32. buf[i] = (TYPE){ SCALE(fre), SCALE(fim) }; \
  33. } \
  34. } while (0)
  35. static const int check_lens[] = {
  36. 2, 4, 8, 16, 32, 64, 120, 960, 1024, 1920, 16384,
  37. };
  38. static AVTXContext *tx_refs[AV_TX_NB][2 /* Direction */][FF_ARRAY_ELEMS(check_lens)] = { 0 };
  39. static int init = 0;
  40. static void free_tx_refs(void)
  41. {
  42. for (int i = 0; i < FF_ARRAY_ELEMS(tx_refs); i++)
  43. for (int j = 0; j < FF_ARRAY_ELEMS(*tx_refs); j++)
  44. for (int k = 0; k < FF_ARRAY_ELEMS(**tx_refs); k++)
  45. av_tx_uninit(&tx_refs[i][j][k]);
  46. }
  47. #define CHECK_TEMPLATE(PREFIX, TYPE, DIR, DATA_TYPE, SCALE_TYPE, LENGTHS, CHECK_EXPRESSION) \
  48. do { \
  49. int err; \
  50. AVTXContext *tx; \
  51. av_tx_fn fn; \
  52. int num_checks = 0; \
  53. int last_check = 0; \
  54. \
  55. for (int i = 0; i < FF_ARRAY_ELEMS(LENGTHS); i++) { \
  56. int len = LENGTHS[i]; \
  57. const SCALE_TYPE scale = 1.0 / len; \
  58. \
  59. if ((err = av_tx_init(&tx, &fn, TYPE, DIR, len, &scale, 0x0)) < 0) { \
  60. fprintf(stderr, "av_tx: %s\n", av_err2str(err)); \
  61. return; \
  62. } \
  63. \
  64. if (check_func(fn, PREFIX "_%i", len)) { \
  65. AVTXContext *tx_ref = tx_refs[TYPE][DIR][i]; \
  66. if (!tx_ref) \
  67. tx_ref = tx; \
  68. num_checks++; \
  69. last_check = len; \
  70. call_ref(tx_ref, out_ref, in, sizeof(DATA_TYPE)); \
  71. call_new(tx, out_new, in, sizeof(DATA_TYPE)); \
  72. if (CHECK_EXPRESSION) { \
  73. fail(); \
  74. av_tx_uninit(&tx); \
  75. break; \
  76. } \
  77. bench_new(tx, out_new, in, sizeof(DATA_TYPE)); \
  78. av_tx_uninit(&tx_refs[TYPE][DIR][i]); \
  79. tx_refs[TYPE][DIR][i] = tx; \
  80. } else { \
  81. av_tx_uninit(&tx); \
  82. } \
  83. } \
  84. \
  85. if (num_checks == 1) \
  86. report(PREFIX "_%i", last_check); \
  87. else if (num_checks) \
  88. report(PREFIX); \
  89. } while (0)
  90. void checkasm_check_av_tx(void)
  91. {
  92. declare_func(void, AVTXContext *tx, void *out, void *in, ptrdiff_t stride);
  93. void *in = av_malloc(16384*2*8);
  94. void *out_ref = av_malloc(16384*2*8);
  95. void *out_new = av_malloc(16384*2*8);
  96. randomize_complex(in, 16384, AVComplexFloat, SCALE_NOOP);
  97. CHECK_TEMPLATE("float_fft", AV_TX_FLOAT_FFT, 0, AVComplexFloat, float, check_lens,
  98. !float_near_abs_eps_array(out_ref, out_new, EPS, len*2));
  99. CHECK_TEMPLATE("float_imdct", AV_TX_FLOAT_MDCT, 1, float, float, check_lens,
  100. !float_near_abs_eps_array(out_ref, out_new, EPS, len));
  101. randomize_complex(in, 16384, AVComplexDouble, SCALE_NOOP);
  102. CHECK_TEMPLATE("double_fft", AV_TX_DOUBLE_FFT, 0, AVComplexDouble, double, check_lens,
  103. !double_near_abs_eps_array(out_ref, out_new, EPS, len*2));
  104. av_free(in);
  105. av_free(out_ref);
  106. av_free(out_new);
  107. if (!init) {
  108. init = 1;
  109. atexit(free_tx_refs);
  110. }
  111. }