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.

368 lines
14KB

  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. * @ingroup lavr
  25. * external API header
  26. */
  27. /**
  28. * @defgroup lavr Libavresample
  29. * @{
  30. *
  31. * Libavresample (lavr) is a library that handles audio resampling, sample
  32. * format conversion and mixing.
  33. *
  34. * Interaction with lavr is done through AVAudioResampleContext, which is
  35. * allocated with avresample_alloc_context(). It is opaque, so all parameters
  36. * must be set with the @ref avoptions API.
  37. *
  38. * For example the following code will setup conversion from planar float sample
  39. * format to interleaved signed 16-bit integer, downsampling from 48kHz to
  40. * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
  41. * matrix):
  42. * @code
  43. * AVAudioResampleContext *avr = avresample_alloc_context();
  44. * av_opt_set_int(avr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);
  45. * av_opt_set_int(avr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
  46. * av_opt_set_int(avr, "in_sample_rate", 48000, 0);
  47. * av_opt_set_int(avr, "out_sample_rate", 44100, 0);
  48. * av_opt_set_int(avr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
  49. * av_opt_set_int(avr, "out_sample_fmt, AV_SAMPLE_FMT_S16, 0);
  50. * @endcode
  51. *
  52. * Once the context is initialized, it must be opened with avresample_open(). If
  53. * you need to change the conversion parameters, you must close the context with
  54. * avresample_close(), change the parameters as described above, then reopen it
  55. * again.
  56. *
  57. * The conversion itself is done by repeatedly calling avresample_convert().
  58. * Note that the samples may get buffered in two places in lavr. The first one
  59. * is the output FIFO, where the samples end up if the output buffer is not
  60. * large enough. The data stored in there may be retrieved at any time with
  61. * avresample_read(). The second place is the resampling delay buffer,
  62. * applicable only when resampling is done. The samples in it require more input
  63. * before they can be processed. Their current amount is returned by
  64. * avresample_get_delay(). At the end of conversion the resampling buffer can be
  65. * flushed by calling avresample_convert() with NULL input.
  66. *
  67. * The following code demonstrates the conversion loop assuming the parameters
  68. * from above and caller-defined functions get_input() and handle_output():
  69. * @code
  70. * uint8_t **input;
  71. * int in_linesize, in_samples;
  72. *
  73. * while (get_input(&input, &in_linesize, &in_samples)) {
  74. * uint8_t *output
  75. * int out_linesize;
  76. * int out_samples = avresample_available(avr) +
  77. * av_rescale_rnd(avresample_get_delay(avr) +
  78. * in_samples, 44100, 48000, AV_ROUND_UP);
  79. * av_samples_alloc(&output, &out_linesize, 2, out_samples,
  80. * AV_SAMPLE_FMT_S16, 0);
  81. * out_samples = avresample_convert(avr, &output, out_linesize, out_samples,
  82. * input, in_linesize, in_samples);
  83. * handle_output(output, out_linesize, out_samples);
  84. * av_freep(&output);
  85. * }
  86. * @endcode
  87. *
  88. * When the conversion is finished and the FIFOs are flushed if required, the
  89. * conversion context and everything associated with it must be freed with
  90. * avresample_free().
  91. */
  92. #include "libavutil/avutil.h"
  93. #include "libavutil/channel_layout.h"
  94. #include "libavutil/dict.h"
  95. #include "libavutil/log.h"
  96. #include "libavresample/version.h"
  97. #define AVRESAMPLE_MAX_CHANNELS 32
  98. typedef struct AVAudioResampleContext AVAudioResampleContext;
  99. /** Mixing Coefficient Types */
  100. enum AVMixCoeffType {
  101. AV_MIX_COEFF_TYPE_Q8, /** 16-bit 8.8 fixed-point */
  102. AV_MIX_COEFF_TYPE_Q15, /** 32-bit 17.15 fixed-point */
  103. AV_MIX_COEFF_TYPE_FLT, /** floating-point */
  104. AV_MIX_COEFF_TYPE_NB, /** Number of coeff types. Not part of ABI */
  105. };
  106. /** Resampling Filter Types */
  107. enum AVResampleFilterType {
  108. AV_RESAMPLE_FILTER_TYPE_CUBIC, /**< Cubic */
  109. AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */
  110. AV_RESAMPLE_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */
  111. };
  112. /**
  113. * Return the LIBAVRESAMPLE_VERSION_INT constant.
  114. */
  115. unsigned avresample_version(void);
  116. /**
  117. * Return the libavresample build-time configuration.
  118. * @return configure string
  119. */
  120. const char *avresample_configuration(void);
  121. /**
  122. * Return the libavresample license.
  123. */
  124. const char *avresample_license(void);
  125. /**
  126. * Get the AVClass for AVAudioResampleContext.
  127. *
  128. * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
  129. * without allocating a context.
  130. *
  131. * @see av_opt_find().
  132. *
  133. * @return AVClass for AVAudioResampleContext
  134. */
  135. const AVClass *avresample_get_class(void);
  136. /**
  137. * Allocate AVAudioResampleContext and set options.
  138. *
  139. * @return allocated audio resample context, or NULL on failure
  140. */
  141. AVAudioResampleContext *avresample_alloc_context(void);
  142. /**
  143. * Initialize AVAudioResampleContext.
  144. *
  145. * @param avr audio resample context
  146. * @return 0 on success, negative AVERROR code on failure
  147. */
  148. int avresample_open(AVAudioResampleContext *avr);
  149. /**
  150. * Close AVAudioResampleContext.
  151. *
  152. * This closes the context, but it does not change the parameters. The context
  153. * can be reopened with avresample_open(). It does, however, clear the output
  154. * FIFO and any remaining leftover samples in the resampling delay buffer. If
  155. * there was a custom matrix being used, that is also cleared.
  156. *
  157. * @see avresample_convert()
  158. * @see avresample_set_matrix()
  159. *
  160. * @param avr audio resample context
  161. */
  162. void avresample_close(AVAudioResampleContext *avr);
  163. /**
  164. * Free AVAudioResampleContext and associated AVOption values.
  165. *
  166. * This also calls avresample_close() before freeing.
  167. *
  168. * @param avr audio resample context
  169. */
  170. void avresample_free(AVAudioResampleContext **avr);
  171. /**
  172. * Generate a channel mixing matrix.
  173. *
  174. * This function is the one used internally by libavresample for building the
  175. * default mixing matrix. It is made public just as a utility function for
  176. * building custom matrices.
  177. *
  178. * @param in_layout input channel layout
  179. * @param out_layout output channel layout
  180. * @param center_mix_level mix level for the center channel
  181. * @param surround_mix_level mix level for the surround channel(s)
  182. * @param lfe_mix_level mix level for the low-frequency effects channel
  183. * @param normalize if 1, coefficients will be normalized to prevent
  184. * overflow. if 0, coefficients will not be
  185. * normalized.
  186. * @param[out] matrix mixing coefficients; matrix[i + stride * o] is
  187. * the weight of input channel i in output channel o.
  188. * @param stride distance between adjacent input channels in the
  189. * matrix array
  190. * @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)
  191. * @return 0 on success, negative AVERROR code on failure
  192. */
  193. int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
  194. double center_mix_level, double surround_mix_level,
  195. double lfe_mix_level, int normalize, double *matrix,
  196. int stride, enum AVMatrixEncoding matrix_encoding);
  197. /**
  198. * Get the current channel mixing matrix.
  199. *
  200. * @param avr audio resample context
  201. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  202. * input channel i in output channel o.
  203. * @param stride distance between adjacent input channels in the matrix array
  204. * @return 0 on success, negative AVERROR code on failure
  205. */
  206. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  207. int stride);
  208. /**
  209. * Set channel mixing matrix.
  210. *
  211. * Allows for setting a custom mixing matrix, overriding the default matrix
  212. * generated internally during avresample_open(). This function can be called
  213. * anytime on an allocated context, either before or after calling
  214. * avresample_open(). avresample_convert() always uses the current matrix.
  215. * Calling avresample_close() on the context will clear the current matrix.
  216. *
  217. * @see avresample_close()
  218. *
  219. * @param avr audio resample context
  220. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  221. * input channel i in output channel o.
  222. * @param stride distance between adjacent input channels in the matrix array
  223. * @return 0 on success, negative AVERROR code on failure
  224. */
  225. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  226. int stride);
  227. /**
  228. * Set compensation for resampling.
  229. *
  230. * This can be called anytime after avresample_open(). If resampling was not
  231. * being done previously, the AVAudioResampleContext is closed and reopened
  232. * with resampling enabled. In this case, any samples remaining in the output
  233. * FIFO and the current channel mixing matrix will be restored after reopening
  234. * the context.
  235. *
  236. * @param avr audio resample context
  237. * @param sample_delta compensation delta, in samples
  238. * @param compensation_distance compensation distance, in samples
  239. * @return 0 on success, negative AVERROR code on failure
  240. */
  241. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  242. int compensation_distance);
  243. /**
  244. * Convert input samples and write them to the output FIFO.
  245. *
  246. * The upper bound on the number of output samples is given by
  247. * avresample_available() + (avresample_get_delay() + number of input samples) *
  248. * output sample rate / input sample rate.
  249. *
  250. * The output data can be NULL or have fewer allocated samples than required.
  251. * In this case, any remaining samples not written to the output will be added
  252. * to an internal FIFO buffer, to be returned at the next call to this function
  253. * or to avresample_read().
  254. *
  255. * If converting sample rate, there may be data remaining in the internal
  256. * resampling delay buffer. avresample_get_delay() tells the number of remaining
  257. * samples. To get this data as output, call avresample_convert() with NULL
  258. * input.
  259. *
  260. * At the end of the conversion process, there may be data remaining in the
  261. * internal FIFO buffer. avresample_available() tells the number of remaining
  262. * samples. To get this data as output, either call avresample_convert() with
  263. * NULL input or call avresample_read().
  264. *
  265. * @see avresample_available()
  266. * @see avresample_read()
  267. * @see avresample_get_delay()
  268. *
  269. * @param avr audio resample context
  270. * @param output output data pointers
  271. * @param out_plane_size output plane size, in bytes.
  272. * This can be 0 if unknown, but that will lead to
  273. * optimized functions not being used directly on the
  274. * output, which could slow down some conversions.
  275. * @param out_samples maximum number of samples that the output buffer can hold
  276. * @param input input data pointers
  277. * @param in_plane_size input plane size, in bytes
  278. * This can be 0 if unknown, but that will lead to
  279. * optimized functions not being used directly on the
  280. * input, which could slow down some conversions.
  281. * @param in_samples number of input samples to convert
  282. * @return number of samples written to the output buffer,
  283. * not including converted samples added to the internal
  284. * output FIFO
  285. */
  286. int avresample_convert(AVAudioResampleContext *avr, uint8_t **output,
  287. int out_plane_size, int out_samples, uint8_t **input,
  288. int in_plane_size, int in_samples);
  289. /**
  290. * Return the number of samples currently in the resampling delay buffer.
  291. *
  292. * When resampling, there may be a delay between the input and output. Any
  293. * unconverted samples in each call are stored internally in a delay buffer.
  294. * This function allows the user to determine the current number of samples in
  295. * the delay buffer, which can be useful for synchronization.
  296. *
  297. * @see avresample_convert()
  298. *
  299. * @param avr audio resample context
  300. * @return number of samples currently in the resampling delay buffer
  301. */
  302. int avresample_get_delay(AVAudioResampleContext *avr);
  303. /**
  304. * Return the number of available samples in the output FIFO.
  305. *
  306. * During conversion, if the user does not specify an output buffer or
  307. * specifies an output buffer that is smaller than what is needed, remaining
  308. * samples that are not written to the output are stored to an internal FIFO
  309. * buffer. The samples in the FIFO can be read with avresample_read() or
  310. * avresample_convert().
  311. *
  312. * @see avresample_read()
  313. * @see avresample_convert()
  314. *
  315. * @param avr audio resample context
  316. * @return number of samples available for reading
  317. */
  318. int avresample_available(AVAudioResampleContext *avr);
  319. /**
  320. * Read samples from the output FIFO.
  321. *
  322. * During conversion, if the user does not specify an output buffer or
  323. * specifies an output buffer that is smaller than what is needed, remaining
  324. * samples that are not written to the output are stored to an internal FIFO
  325. * buffer. This function can be used to read samples from that internal FIFO.
  326. *
  327. * @see avresample_available()
  328. * @see avresample_convert()
  329. *
  330. * @param avr audio resample context
  331. * @param output output data pointers. May be NULL, in which case
  332. * nb_samples of data is discarded from output FIFO.
  333. * @param nb_samples number of samples to read from the FIFO
  334. * @return the number of samples written to output
  335. */
  336. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples);
  337. /**
  338. * @}
  339. */
  340. #endif /* AVRESAMPLE_AVRESAMPLE_H */