avresample.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. * @return 0 on success, negative AVERROR code on failure
  119. */
  120. int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
  121. double center_mix_level, double surround_mix_level,
  122. double lfe_mix_level, int normalize, double *matrix,
  123. int stride);
  124. /**
  125. * Get the current channel mixing matrix.
  126. *
  127. * @param avr audio resample context
  128. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  129. * input channel i in output channel o.
  130. * @param stride distance between adjacent input channels in the matrix array
  131. * @return 0 on success, negative AVERROR code on failure
  132. */
  133. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  134. int stride);
  135. /**
  136. * Set channel mixing matrix.
  137. *
  138. * Allows for setting a custom mixing matrix, overriding the default matrix
  139. * generated internally during avresample_open(). This function can be called
  140. * anytime on an allocated context, either before or after calling
  141. * avresample_open(). avresample_convert() always uses the current matrix.
  142. * Calling avresample_close() on the context will clear the current matrix.
  143. *
  144. * @see avresample_close()
  145. *
  146. * @param avr audio resample context
  147. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  148. * input channel i in output channel o.
  149. * @param stride distance between adjacent input channels in the matrix array
  150. * @return 0 on success, negative AVERROR code on failure
  151. */
  152. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  153. int stride);
  154. /**
  155. * Set compensation for resampling.
  156. *
  157. * This can be called anytime after avresample_open(). If resampling was not
  158. * being done previously, the AVAudioResampleContext is closed and reopened
  159. * with resampling enabled. In this case, any samples remaining in the output
  160. * FIFO and the current channel mixing matrix will be restored after reopening
  161. * the context.
  162. *
  163. * @param avr audio resample context
  164. * @param sample_delta compensation delta, in samples
  165. * @param compensation_distance compensation distance, in samples
  166. * @return 0 on success, negative AVERROR code on failure
  167. */
  168. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  169. int compensation_distance);
  170. /**
  171. * Convert input samples and write them to the output FIFO.
  172. *
  173. * The output data can be NULL or have fewer allocated samples than required.
  174. * In this case, any remaining samples not written to the output will be added
  175. * to an internal FIFO buffer, to be returned at the next call to this function
  176. * or to avresample_read().
  177. *
  178. * If converting sample rate, there may be data remaining in the internal
  179. * resampling delay buffer. avresample_get_delay() tells the number of remaining
  180. * samples. To get this data as output, call avresample_convert() with NULL
  181. * input.
  182. *
  183. * At the end of the conversion process, there may be data remaining in the
  184. * internal FIFO buffer. avresample_available() tells the number of remaining
  185. * samples. To get this data as output, either call avresample_convert() with
  186. * NULL input or call avresample_read().
  187. *
  188. * @see avresample_available()
  189. * @see avresample_read()
  190. * @see avresample_get_delay()
  191. *
  192. * @param avr audio resample context
  193. * @param output output data pointers
  194. * @param out_plane_size output plane size, in bytes.
  195. * This can be 0 if unknown, but that will lead to
  196. * optimized functions not being used directly on the
  197. * output, which could slow down some conversions.
  198. * @param out_samples maximum number of samples that the output buffer can hold
  199. * @param input input data pointers
  200. * @param in_plane_size input plane size, in bytes
  201. * This can be 0 if unknown, but that will lead to
  202. * optimized functions not being used directly on the
  203. * input, which could slow down some conversions.
  204. * @param in_samples number of input samples to convert
  205. * @return number of samples written to the output buffer,
  206. * not including converted samples added to the internal
  207. * output FIFO
  208. */
  209. int avresample_convert(AVAudioResampleContext *avr, void **output,
  210. int out_plane_size, int out_samples, void **input,
  211. int in_plane_size, int in_samples);
  212. /**
  213. * Return the number of samples currently in the resampling delay buffer.
  214. *
  215. * When resampling, there may be a delay between the input and output. Any
  216. * unconverted samples in each call are stored internally in a delay buffer.
  217. * This function allows the user to determine the current number of samples in
  218. * the delay buffer, which can be useful for synchronization.
  219. *
  220. * @see avresample_convert()
  221. *
  222. * @param avr audio resample context
  223. * @return number of samples currently in the resampling delay buffer
  224. */
  225. int avresample_get_delay(AVAudioResampleContext *avr);
  226. /**
  227. * Return the number of available samples in the output FIFO.
  228. *
  229. * During conversion, if the user does not specify an output buffer or
  230. * specifies an output buffer that is smaller than what is needed, remaining
  231. * samples that are not written to the output are stored to an internal FIFO
  232. * buffer. The samples in the FIFO can be read with avresample_read() or
  233. * avresample_convert().
  234. *
  235. * @see avresample_read()
  236. * @see avresample_convert()
  237. *
  238. * @param avr audio resample context
  239. * @return number of samples available for reading
  240. */
  241. int avresample_available(AVAudioResampleContext *avr);
  242. /**
  243. * Read samples from the output FIFO.
  244. *
  245. * During conversion, if the user does not specify an output buffer or
  246. * specifies an output buffer that is smaller than what is needed, remaining
  247. * samples that are not written to the output are stored to an internal FIFO
  248. * buffer. This function can be used to read samples from that internal FIFO.
  249. *
  250. * @see avresample_available()
  251. * @see avresample_convert()
  252. *
  253. * @param avr audio resample context
  254. * @param output output data pointers. May be NULL, in which case
  255. * nb_samples of data is discarded from output FIFO.
  256. * @param nb_samples number of samples to read from the FIFO
  257. * @return the number of samples written to output
  258. */
  259. int avresample_read(AVAudioResampleContext *avr, void **output, int nb_samples);
  260. #endif /* AVRESAMPLE_AVRESAMPLE_H */