You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.9KB

  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (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 GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "tx_priv.h"
  19. /* Calculates the modular multiplicative inverse, not fast, replace */
  20. static av_always_inline int mulinv(int n, int m)
  21. {
  22. n = n % m;
  23. for (int x = 1; x < m; x++)
  24. if (((n * x) % m) == 1)
  25. return x;
  26. av_assert0(0); /* Never reached */
  27. }
  28. /* Guaranteed to work for any n, m where gcd(n, m) == 1 */
  29. int ff_tx_gen_compound_mapping(AVTXContext *s)
  30. {
  31. int *in_map, *out_map;
  32. const int n = s->n;
  33. const int m = s->m;
  34. const int inv = s->inv;
  35. const int type = s->type;
  36. const int len = n*m;
  37. const int m_inv = mulinv(m, n);
  38. const int n_inv = mulinv(n, m);
  39. const int mdct = type == AV_TX_FLOAT_MDCT || type == AV_TX_DOUBLE_MDCT;
  40. if (!(s->pfatab = av_malloc(2*len*sizeof(*s->pfatab))))
  41. return AVERROR(ENOMEM);
  42. in_map = s->pfatab;
  43. out_map = s->pfatab + n*m;
  44. /* Ruritanian map for input, CRT map for output, can be swapped */
  45. for (int j = 0; j < m; j++) {
  46. for (int i = 0; i < n; i++) {
  47. /* Shifted by 1 to simplify MDCTs */
  48. in_map[j*n + i] = ((i*m + j*n) % len) << mdct;
  49. out_map[(i*m*m_inv + j*n*n_inv) % len] = i*m + j;
  50. }
  51. }
  52. /* Change transform direction by reversing all ACs */
  53. if (inv) {
  54. for (int i = 0; i < m; i++) {
  55. int *in = &in_map[i*n + 1]; /* Skip the DC */
  56. for (int j = 0; j < ((n - 1) >> 1); j++)
  57. FFSWAP(int, in[j], in[n - j - 2]);
  58. }
  59. }
  60. /* Our 15-point transform is also a compound one, so embed its input map */
  61. if (n == 15) {
  62. for (int k = 0; k < m; k++) {
  63. int tmp[15];
  64. memcpy(tmp, &in_map[k*15], 15*sizeof(*tmp));
  65. for (int i = 0; i < 5; i++) {
  66. for (int j = 0; j < 3; j++)
  67. in_map[k*15 + i*3 + j] = tmp[(i*3 + j*5) % 15];
  68. }
  69. }
  70. }
  71. return 0;
  72. }
  73. int ff_tx_gen_ptwo_revtab(AVTXContext *s)
  74. {
  75. const int m = s->m, inv = s->inv;
  76. if (!(s->revtab = av_malloc(m*sizeof(*s->revtab))))
  77. return AVERROR(ENOMEM);
  78. /* Default */
  79. for (int i = 0; i < m; i++) {
  80. int k = -split_radix_permutation(i, m, inv) & (m - 1);
  81. s->revtab[k] = i;
  82. }
  83. return 0;
  84. }
  85. av_cold void av_tx_uninit(AVTXContext **ctx)
  86. {
  87. if (!(*ctx))
  88. return;
  89. av_free((*ctx)->pfatab);
  90. av_free((*ctx)->exptab);
  91. av_free((*ctx)->revtab);
  92. av_free((*ctx)->tmp);
  93. av_freep(ctx);
  94. }
  95. av_cold int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type,
  96. int inv, int len, const void *scale, uint64_t flags)
  97. {
  98. int err;
  99. AVTXContext *s = av_mallocz(sizeof(*s));
  100. if (!s)
  101. return AVERROR(ENOMEM);
  102. switch (type) {
  103. case AV_TX_FLOAT_FFT:
  104. case AV_TX_FLOAT_MDCT:
  105. if ((err = ff_tx_init_mdct_fft_float(s, tx, type, inv, len, scale, flags)))
  106. goto fail;
  107. break;
  108. case AV_TX_DOUBLE_FFT:
  109. case AV_TX_DOUBLE_MDCT:
  110. if ((err = ff_tx_init_mdct_fft_double(s, tx, type, inv, len, scale, flags)))
  111. goto fail;
  112. break;
  113. default:
  114. err = AVERROR(EINVAL);
  115. goto fail;
  116. }
  117. *ctx = s;
  118. return 0;
  119. fail:
  120. av_tx_uninit(&s);
  121. *tx = NULL;
  122. return err;
  123. }