av_tx.c 6.5 KB

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