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.

286 lines
11KB

  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of Libav.
  5. *
  6. * Libav is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * Libav is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with Libav; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef AVRESAMPLE_AVRESAMPLE_H
  21. #define AVRESAMPLE_AVRESAMPLE_H
  22. /**
  23. * @file
  24. * external API header
  25. */
  26. #include "libavutil/audioconvert.h"
  27. #include "libavutil/avutil.h"
  28. #include "libavutil/dict.h"
  29. #include "libavutil/log.h"
  30. #include "libavresample/version.h"
  31. #define AVRESAMPLE_MAX_CHANNELS 32
  32. typedef struct AVAudioResampleContext AVAudioResampleContext;
  33. /** Mixing Coefficient Types */
  34. enum AVMixCoeffType {
  35. AV_MIX_COEFF_TYPE_Q8, /** 16-bit 8.8 fixed-point */
  36. AV_MIX_COEFF_TYPE_Q15, /** 32-bit 17.15 fixed-point */
  37. AV_MIX_COEFF_TYPE_FLT, /** floating-point */
  38. AV_MIX_COEFF_TYPE_NB, /** Number of coeff types. Not part of ABI */
  39. };
  40. /**
  41. * Return the LIBAVRESAMPLE_VERSION_INT constant.
  42. */
  43. unsigned avresample_version(void);
  44. /**
  45. * Return the libavresample build-time configuration.
  46. * @return configure string
  47. */
  48. const char *avresample_configuration(void);
  49. /**
  50. * Return the libavresample license.
  51. */
  52. const char *avresample_license(void);
  53. /**
  54. * Get the AVClass for AVAudioResampleContext.
  55. *
  56. * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
  57. * without allocating a context.
  58. *
  59. * @see av_opt_find().
  60. *
  61. * @return AVClass for AVAudioResampleContext
  62. */
  63. const AVClass *avresample_get_class(void);
  64. /**
  65. * Allocate AVAudioResampleContext and set options.
  66. *
  67. * @return allocated audio resample context, or NULL on failure
  68. */
  69. AVAudioResampleContext *avresample_alloc_context(void);
  70. /**
  71. * Initialize AVAudioResampleContext.
  72. *
  73. * @param avr audio resample context
  74. * @return 0 on success, negative AVERROR code on failure
  75. */
  76. int avresample_open(AVAudioResampleContext *avr);
  77. /**
  78. * Close AVAudioResampleContext.
  79. *
  80. * This closes the context, but it does not change the parameters. The context
  81. * can be reopened with avresample_open(). It does, however, clear the output
  82. * FIFO and any remaining leftover samples in the resampling delay buffer. If
  83. * there was a custom matrix being used, that is also cleared.
  84. *
  85. * @see avresample_convert()
  86. * @see avresample_set_matrix()
  87. *
  88. * @param avr audio resample context
  89. */
  90. void avresample_close(AVAudioResampleContext *avr);
  91. /**
  92. * Free AVAudioResampleContext and associated AVOption values.
  93. *
  94. * This also calls avresample_close() before freeing.
  95. *
  96. * @param avr audio resample context
  97. */
  98. void avresample_free(AVAudioResampleContext **avr);
  99. /**
  100. * Generate a channel mixing matrix.
  101. *
  102. * This function is the one used internally by libavresample for building the
  103. * default mixing matrix. It is made public just as a utility function for
  104. * building custom matrices.
  105. *
  106. * @param in_layout input channel layout
  107. * @param out_layout output channel layout
  108. * @param center_mix_level mix level for the center channel
  109. * @param surround_mix_level mix level for the surround channel(s)
  110. * @param lfe_mix_level mix level for the low-frequency effects channel
  111. * @param normalize if 1, coefficients will be normalized to prevent
  112. * overflow. if 0, coefficients will not be
  113. * normalized.
  114. * @param[out] matrix mixing coefficients; matrix[i + stride * o] is
  115. * the weight of input channel i in output channel o.
  116. * @param stride distance between adjacent input channels in the
  117. * matrix array
  118. * @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)
  119. * @return 0 on success, negative AVERROR code on failure
  120. */
  121. int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
  122. double center_mix_level, double surround_mix_level,
  123. double lfe_mix_level, int normalize, double *matrix,
  124. int stride, enum AVMatrixEncoding matrix_encoding);
  125. /**
  126. * Get the current channel mixing matrix.
  127. *
  128. * @param avr audio resample context
  129. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  130. * input channel i in output channel o.
  131. * @param stride distance between adjacent input channels in the matrix array
  132. * @return 0 on success, negative AVERROR code on failure
  133. */
  134. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  135. int stride);
  136. /**
  137. * Set channel mixing matrix.
  138. *
  139. * Allows for setting a custom mixing matrix, overriding the default matrix
  140. * generated internally during avresample_open(). This function can be called
  141. * anytime on an allocated context, either before or after calling
  142. * avresample_open(). avresample_convert() always uses the current matrix.
  143. * Calling avresample_close() on the context will clear the current matrix.
  144. *
  145. * @see avresample_close()
  146. *
  147. * @param avr audio resample context
  148. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  149. * input channel i in output channel o.
  150. * @param stride distance between adjacent input channels in the matrix array
  151. * @return 0 on success, negative AVERROR code on failure
  152. */
  153. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  154. int stride);
  155. /**
  156. * Set compensation for resampling.
  157. *
  158. * This can be called anytime after avresample_open(). If resampling was not
  159. * being done previously, the AVAudioResampleContext is closed and reopened
  160. * with resampling enabled. In this case, any samples remaining in the output
  161. * FIFO and the current channel mixing matrix will be restored after reopening
  162. * the context.
  163. *
  164. * @param avr audio resample context
  165. * @param sample_delta compensation delta, in samples
  166. * @param compensation_distance compensation distance, in samples
  167. * @return 0 on success, negative AVERROR code on failure
  168. */
  169. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  170. int compensation_distance);
  171. /**
  172. * Convert input samples and write them to the output FIFO.
  173. *
  174. * The output data can be NULL or have fewer allocated samples than required.
  175. * In this case, any remaining samples not written to the output will be added
  176. * to an internal FIFO buffer, to be returned at the next call to this function
  177. * or to avresample_read().
  178. *
  179. * If converting sample rate, there may be data remaining in the internal
  180. * resampling delay buffer. avresample_get_delay() tells the number of remaining
  181. * samples. To get this data as output, call avresample_convert() with NULL
  182. * input.
  183. *
  184. * At the end of the conversion process, there may be data remaining in the
  185. * internal FIFO buffer. avresample_available() tells the number of remaining
  186. * samples. To get this data as output, either call avresample_convert() with
  187. * NULL input or call avresample_read().
  188. *
  189. * @see avresample_available()
  190. * @see avresample_read()
  191. * @see avresample_get_delay()
  192. *
  193. * @param avr audio resample context
  194. * @param output output data pointers
  195. * @param out_plane_size output plane size, in bytes.
  196. * This can be 0 if unknown, but that will lead to
  197. * optimized functions not being used directly on the
  198. * output, which could slow down some conversions.
  199. * @param out_samples maximum number of samples that the output buffer can hold
  200. * @param input input data pointers
  201. * @param in_plane_size input plane size, in bytes
  202. * This can be 0 if unknown, but that will lead to
  203. * optimized functions not being used directly on the
  204. * input, which could slow down some conversions.
  205. * @param in_samples number of input samples to convert
  206. * @return number of samples written to the output buffer,
  207. * not including converted samples added to the internal
  208. * output FIFO
  209. */
  210. int avresample_convert(AVAudioResampleContext *avr, void **output,
  211. int out_plane_size, int out_samples, void **input,
  212. int in_plane_size, int in_samples);
  213. /**
  214. * Return the number of samples currently in the resampling delay buffer.
  215. *
  216. * When resampling, there may be a delay between the input and output. Any
  217. * unconverted samples in each call are stored internally in a delay buffer.
  218. * This function allows the user to determine the current number of samples in
  219. * the delay buffer, which can be useful for synchronization.
  220. *
  221. * @see avresample_convert()
  222. *
  223. * @param avr audio resample context
  224. * @return number of samples currently in the resampling delay buffer
  225. */
  226. int avresample_get_delay(AVAudioResampleContext *avr);
  227. /**
  228. * Return the number of available samples in the output FIFO.
  229. *
  230. * During conversion, if the user does not specify an output buffer or
  231. * specifies an output buffer that is smaller than what is needed, remaining
  232. * samples that are not written to the output are stored to an internal FIFO
  233. * buffer. The samples in the FIFO can be read with avresample_read() or
  234. * avresample_convert().
  235. *
  236. * @see avresample_read()
  237. * @see avresample_convert()
  238. *
  239. * @param avr audio resample context
  240. * @return number of samples available for reading
  241. */
  242. int avresample_available(AVAudioResampleContext *avr);
  243. /**
  244. * Read samples from the output FIFO.
  245. *
  246. * During conversion, if the user does not specify an output buffer or
  247. * specifies an output buffer that is smaller than what is needed, remaining
  248. * samples that are not written to the output are stored to an internal FIFO
  249. * buffer. This function can be used to read samples from that internal FIFO.
  250. *
  251. * @see avresample_available()
  252. * @see avresample_convert()
  253. *
  254. * @param avr audio resample context
  255. * @param output output data pointers. May be NULL, in which case
  256. * nb_samples of data is discarded from output FIFO.
  257. * @param nb_samples number of samples to read from the FIFO
  258. * @return the number of samples written to output
  259. */
  260. int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples);
  261. #endif /* AVRESAMPLE_AVRESAMPLE_H */