tx.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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. #ifndef AVUTIL_TX_H
  19. #define AVUTIL_TX_H
  20. #include <stdint.h>
  21. #include <stddef.h>
  22. typedef struct AVTXContext AVTXContext;
  23. typedef struct AVComplexFloat {
  24. float re, im;
  25. } AVComplexFloat;
  26. typedef struct AVComplexDouble {
  27. double re, im;
  28. } AVComplexDouble;
  29. typedef struct AVComplexInt32 {
  30. int32_t re, im;
  31. } AVComplexInt32;
  32. enum AVTXType {
  33. /**
  34. * Standard complex to complex FFT with sample data type of AVComplexFloat,
  35. * AVComplexDouble or AVComplexInt32, for each respective variant.
  36. *
  37. * Output is not 1/len normalized. Scaling currently unsupported.
  38. * The stride parameter must be set to the size of a single sample in bytes.
  39. */
  40. AV_TX_FLOAT_FFT = 0,
  41. AV_TX_DOUBLE_FFT = 2,
  42. AV_TX_INT32_FFT = 4,
  43. /**
  44. * Standard MDCT with a sample data type of float, double or int32_t,
  45. * respecively. For the float and int32 variants, the scale type is
  46. * 'float', while for the double variant, it's 'double'.
  47. * If scale is NULL, 1.0 will be used as a default.
  48. *
  49. * Length is the frame size, not the window size (which is 2x frame).
  50. * For forward transforms, the stride specifies the spacing between each
  51. * sample in the output array in bytes. The input must be a flat array.
  52. *
  53. * For inverse transforms, the stride specifies the spacing between each
  54. * sample in the input array in bytes. The output must be a flat array.
  55. *
  56. * NOTE: the inverse transform is half-length, meaning the output will not
  57. * contain redundant data. This is what most codecs work with. To do a full
  58. * inverse transform, set the AV_TX_FULL_IMDCT flag on init.
  59. */
  60. AV_TX_FLOAT_MDCT = 1,
  61. AV_TX_DOUBLE_MDCT = 3,
  62. AV_TX_INT32_MDCT = 5,
  63. /**
  64. * Real to complex and complex to real DFTs.
  65. * For the float and int32 variants, the scale type is 'float', while for
  66. * the double variant, it's a 'double'. If scale is NULL, 1.0 will be used
  67. * as a default.
  68. *
  69. * For forward transforms (R2C), stride must be the spacing between two
  70. * samples in bytes. For inverse transforms, the stride must be set
  71. * to the spacing between two complex values in bytes.
  72. *
  73. * The forward transform performs a real-to-complex DFT of N samples to
  74. * N/2+1 complex values.
  75. *
  76. * The inverse transform performs a complex-to-real DFT of N/2+1 complex
  77. * values to N real samples. The output is not normalized, but can be
  78. * made so by setting the scale value to 1.0/len.
  79. * NOTE: the inverse transform always overwrites the input.
  80. */
  81. AV_TX_FLOAT_RDFT = 6,
  82. AV_TX_DOUBLE_RDFT = 7,
  83. AV_TX_INT32_RDFT = 8,
  84. /**
  85. * Real to real (DCT) transforms.
  86. *
  87. * The forward transform is a DCT-II.
  88. * The inverse transform is a DCT-III.
  89. *
  90. * The input array is always overwritten. DCT-III requires that the
  91. * input be padded with 2 extra samples. Stride must be set to the
  92. * spacing between two samples in bytes.
  93. */
  94. AV_TX_FLOAT_DCT = 9,
  95. AV_TX_DOUBLE_DCT = 10,
  96. AV_TX_INT32_DCT = 11,
  97. /**
  98. * Discrete Cosine Transform I
  99. *
  100. * The forward transform is a DCT-I.
  101. * The inverse transform is a DCT-I multiplied by 2/(N + 1).
  102. *
  103. * The input array is always overwritten.
  104. */
  105. AV_TX_FLOAT_DCT_I = 12,
  106. AV_TX_DOUBLE_DCT_I = 13,
  107. AV_TX_INT32_DCT_I = 14,
  108. /**
  109. * Discrete Sine Transform I
  110. *
  111. * The forward transform is a DST-I.
  112. * The inverse transform is a DST-I multiplied by 2/(N + 1).
  113. *
  114. * The input array is always overwritten.
  115. */
  116. AV_TX_FLOAT_DST_I = 15,
  117. AV_TX_DOUBLE_DST_I = 16,
  118. AV_TX_INT32_DST_I = 17,
  119. /* Not part of the API, do not use */
  120. AV_TX_NB,
  121. };
  122. /**
  123. * Function pointer to a function to perform the transform.
  124. *
  125. * @note Using a different context than the one allocated during av_tx_init()
  126. * is not allowed.
  127. *
  128. * @param s the transform context
  129. * @param out the output array
  130. * @param in the input array
  131. * @param stride the input or output stride in bytes
  132. *
  133. * The out and in arrays must be aligned to the maximum required by the CPU
  134. * architecture unless the AV_TX_UNALIGNED flag was set in av_tx_init().
  135. * The stride must follow the constraints the transform type has specified.
  136. */
  137. typedef void (*av_tx_fn)(AVTXContext *s, void *out, void *in, ptrdiff_t stride);
  138. /**
  139. * Flags for av_tx_init()
  140. */
  141. enum AVTXFlags {
  142. /**
  143. * Allows for in-place transformations, where input == output.
  144. * May be unsupported or slower for some transform types.
  145. */
  146. AV_TX_INPLACE = 1ULL << 0,
  147. /**
  148. * Relaxes alignment requirement for the in and out arrays of av_tx_fn().
  149. * May be slower with certain transform types.
  150. */
  151. AV_TX_UNALIGNED = 1ULL << 1,
  152. /**
  153. * Performs a full inverse MDCT rather than leaving out samples that can be
  154. * derived through symmetry. Requires an output array of 'len' floats,
  155. * rather than the usual 'len/2' floats.
  156. * Ignored for all transforms but inverse MDCTs.
  157. */
  158. AV_TX_FULL_IMDCT = 1ULL << 2,
  159. /**
  160. * Perform a real to half-complex RDFT.
  161. * Only the real, or imaginary coefficients will
  162. * be output, depending on the flag used. Only available for forward RDFTs.
  163. * Output array must have enough space to hold N complex values
  164. * (regular size for a real to complex transform).
  165. */
  166. AV_TX_REAL_TO_REAL = 1ULL << 3,
  167. AV_TX_REAL_TO_IMAGINARY = 1ULL << 4,
  168. };
  169. /**
  170. * Initialize a transform context with the given configuration
  171. * (i)MDCTs with an odd length are currently not supported.
  172. *
  173. * @param ctx the context to allocate, will be NULL on error
  174. * @param tx pointer to the transform function pointer to set
  175. * @param type type the type of transform
  176. * @param inv whether to do an inverse or a forward transform
  177. * @param len the size of the transform in samples
  178. * @param scale pointer to the value to scale the output if supported by type
  179. * @param flags a bitmask of AVTXFlags or 0
  180. *
  181. * @return 0 on success, negative error code on failure
  182. */
  183. int av_tx_init(AVTXContext **ctx, av_tx_fn *tx, enum AVTXType type,
  184. int inv, int len, const void *scale, uint64_t flags);
  185. /**
  186. * Frees a context and sets *ctx to NULL, does nothing when *ctx == NULL.
  187. */
  188. void av_tx_uninit(AVTXContext **ctx);
  189. #endif /* AVUTIL_TX_H */