avresample.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. /*
  2. * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg 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. * FFmpeg 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 FFmpeg; 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_get_out_samples(avr, in_samples);
  77. *
  78. * av_samples_alloc(&output, &out_linesize, 2, out_samples,
  79. * AV_SAMPLE_FMT_S16, 0);
  80. * out_samples = avresample_convert(avr, &output, out_linesize, out_samples,
  81. * input, in_linesize, in_samples);
  82. * handle_output(output, out_linesize, out_samples);
  83. * av_freep(&output);
  84. * }
  85. * @endcode
  86. *
  87. * When the conversion is finished and the FIFOs are flushed if required, the
  88. * conversion context and everything associated with it must be freed with
  89. * avresample_free().
  90. */
  91. #include "libavutil/avutil.h"
  92. #include "libavutil/channel_layout.h"
  93. #include "libavutil/dict.h"
  94. #include "libavutil/log.h"
  95. #include "libavutil/mathematics.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. enum AVResampleDitherMethod {
  113. AV_RESAMPLE_DITHER_NONE, /**< Do not use dithering */
  114. AV_RESAMPLE_DITHER_RECTANGULAR, /**< Rectangular Dither */
  115. AV_RESAMPLE_DITHER_TRIANGULAR, /**< Triangular Dither*/
  116. AV_RESAMPLE_DITHER_TRIANGULAR_HP, /**< Triangular Dither with High Pass */
  117. AV_RESAMPLE_DITHER_TRIANGULAR_NS, /**< Triangular Dither with Noise Shaping */
  118. AV_RESAMPLE_DITHER_NB, /**< Number of dither types. Not part of ABI. */
  119. };
  120. /**
  121. * Return the LIBAVRESAMPLE_VERSION_INT constant.
  122. */
  123. unsigned avresample_version(void);
  124. /**
  125. * Return the libavresample build-time configuration.
  126. * @return configure string
  127. */
  128. const char *avresample_configuration(void);
  129. /**
  130. * Return the libavresample license.
  131. */
  132. const char *avresample_license(void);
  133. /**
  134. * Get the AVClass for AVAudioResampleContext.
  135. *
  136. * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
  137. * without allocating a context.
  138. *
  139. * @see av_opt_find().
  140. *
  141. * @return AVClass for AVAudioResampleContext
  142. */
  143. const AVClass *avresample_get_class(void);
  144. /**
  145. * Allocate AVAudioResampleContext and set options.
  146. *
  147. * @return allocated audio resample context, or NULL on failure
  148. */
  149. AVAudioResampleContext *avresample_alloc_context(void);
  150. /**
  151. * Initialize AVAudioResampleContext.
  152. *
  153. * @param avr audio resample context
  154. * @return 0 on success, negative AVERROR code on failure
  155. */
  156. int avresample_open(AVAudioResampleContext *avr);
  157. /**
  158. * Check whether an AVAudioResampleContext is open or closed.
  159. *
  160. * @param avr AVAudioResampleContext to check
  161. * @return 1 if avr is open, 0 if avr is closed.
  162. */
  163. int avresample_is_open(AVAudioResampleContext *avr);
  164. /**
  165. * Close AVAudioResampleContext.
  166. *
  167. * This closes the context, but it does not change the parameters. The context
  168. * can be reopened with avresample_open(). It does, however, clear the output
  169. * FIFO and any remaining leftover samples in the resampling delay buffer. If
  170. * there was a custom matrix being used, that is also cleared.
  171. *
  172. * @see avresample_convert()
  173. * @see avresample_set_matrix()
  174. *
  175. * @param avr audio resample context
  176. */
  177. void avresample_close(AVAudioResampleContext *avr);
  178. /**
  179. * Free AVAudioResampleContext and associated AVOption values.
  180. *
  181. * This also calls avresample_close() before freeing.
  182. *
  183. * @param avr audio resample context
  184. */
  185. void avresample_free(AVAudioResampleContext **avr);
  186. /**
  187. * Generate a channel mixing matrix.
  188. *
  189. * This function is the one used internally by libavresample for building the
  190. * default mixing matrix. It is made public just as a utility function for
  191. * building custom matrices.
  192. *
  193. * @param in_layout input channel layout
  194. * @param out_layout output channel layout
  195. * @param center_mix_level mix level for the center channel
  196. * @param surround_mix_level mix level for the surround channel(s)
  197. * @param lfe_mix_level mix level for the low-frequency effects channel
  198. * @param normalize if 1, coefficients will be normalized to prevent
  199. * overflow. if 0, coefficients will not be
  200. * normalized.
  201. * @param[out] matrix mixing coefficients; matrix[i + stride * o] is
  202. * the weight of input channel i in output channel o.
  203. * @param stride distance between adjacent input channels in the
  204. * matrix array
  205. * @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)
  206. * @return 0 on success, negative AVERROR code on failure
  207. */
  208. int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
  209. double center_mix_level, double surround_mix_level,
  210. double lfe_mix_level, int normalize, double *matrix,
  211. int stride, enum AVMatrixEncoding matrix_encoding);
  212. /**
  213. * Get the current channel mixing matrix.
  214. *
  215. * If no custom matrix has been previously set or the AVAudioResampleContext is
  216. * not open, an error is returned.
  217. *
  218. * @param avr audio resample context
  219. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  220. * input channel i in output channel o.
  221. * @param stride distance between adjacent input channels in the matrix array
  222. * @return 0 on success, negative AVERROR code on failure
  223. */
  224. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  225. int stride);
  226. /**
  227. * Set channel mixing matrix.
  228. *
  229. * Allows for setting a custom mixing matrix, overriding the default matrix
  230. * generated internally during avresample_open(). This function can be called
  231. * anytime on an allocated context, either before or after calling
  232. * avresample_open(), as long as the channel layouts have been set.
  233. * avresample_convert() always uses the current matrix.
  234. * Calling avresample_close() on the context will clear the current matrix.
  235. *
  236. * @see avresample_close()
  237. *
  238. * @param avr audio resample context
  239. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  240. * input channel i in output channel o.
  241. * @param stride distance between adjacent input channels in the matrix array
  242. * @return 0 on success, negative AVERROR code on failure
  243. */
  244. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  245. int stride);
  246. /**
  247. * Set a customized input channel mapping.
  248. *
  249. * This function can only be called when the allocated context is not open.
  250. * Also, the input channel layout must have already been set.
  251. *
  252. * Calling avresample_close() on the context will clear the channel mapping.
  253. *
  254. * The map for each input channel specifies the channel index in the source to
  255. * use for that particular channel, or -1 to mute the channel. Source channels
  256. * can be duplicated by using the same index for multiple input channels.
  257. *
  258. * Examples:
  259. *
  260. * Reordering 5.1 AAC order (C,L,R,Ls,Rs,LFE) to FFmpeg order (L,R,C,LFE,Ls,Rs):
  261. * { 1, 2, 0, 5, 3, 4 }
  262. *
  263. * Muting the 3rd channel in 4-channel input:
  264. * { 0, 1, -1, 3 }
  265. *
  266. * Duplicating the left channel of stereo input:
  267. * { 0, 0 }
  268. *
  269. * @param avr audio resample context
  270. * @param channel_map customized input channel mapping
  271. * @return 0 on success, negative AVERROR code on failure
  272. */
  273. int avresample_set_channel_mapping(AVAudioResampleContext *avr,
  274. const int *channel_map);
  275. /**
  276. * Set compensation for resampling.
  277. *
  278. * This can be called anytime after avresample_open(). If resampling is not
  279. * automatically enabled because of a sample rate conversion, the
  280. * "force_resampling" option must have been set to 1 when opening the context
  281. * in order to use resampling compensation.
  282. *
  283. * @param avr audio resample context
  284. * @param sample_delta compensation delta, in samples
  285. * @param compensation_distance compensation distance, in samples
  286. * @return 0 on success, negative AVERROR code on failure
  287. */
  288. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  289. int compensation_distance);
  290. /**
  291. * Provide the upper bound on the number of samples the configured
  292. * conversion would output.
  293. *
  294. * @param avr audio resample context
  295. * @param in_nb_samples number of input samples
  296. *
  297. * @return number of samples or AVERROR(EINVAL) if the value
  298. * would exceed INT_MAX
  299. */
  300. int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples);
  301. /**
  302. * Convert input samples and write them to the output FIFO.
  303. *
  304. * The upper bound on the number of output samples can be obtained through
  305. * avresample_get_out_samples().
  306. *
  307. * The output data can be NULL or have fewer allocated samples than required.
  308. * In this case, any remaining samples not written to the output will be added
  309. * to an internal FIFO buffer, to be returned at the next call to this function
  310. * or to avresample_read().
  311. *
  312. * If converting sample rate, there may be data remaining in the internal
  313. * resampling delay buffer. avresample_get_delay() tells the number of remaining
  314. * samples. To get this data as output, call avresample_convert() with NULL
  315. * input.
  316. *
  317. * At the end of the conversion process, there may be data remaining in the
  318. * internal FIFO buffer. avresample_available() tells the number of remaining
  319. * samples. To get this data as output, either call avresample_convert() with
  320. * NULL input or call avresample_read().
  321. *
  322. * @see avresample_get_out_samples()
  323. * @see avresample_read()
  324. * @see avresample_get_delay()
  325. *
  326. * @param avr audio resample context
  327. * @param output output data pointers
  328. * @param out_plane_size output plane size, in bytes.
  329. * This can be 0 if unknown, but that will lead to
  330. * optimized functions not being used directly on the
  331. * output, which could slow down some conversions.
  332. * @param out_samples maximum number of samples that the output buffer can hold
  333. * @param input input data pointers
  334. * @param in_plane_size input plane size, in bytes
  335. * This can be 0 if unknown, but that will lead to
  336. * optimized functions not being used directly on the
  337. * input, which could slow down some conversions.
  338. * @param in_samples number of input samples to convert
  339. * @return number of samples written to the output buffer,
  340. * not including converted samples added to the internal
  341. * output FIFO
  342. */
  343. int avresample_convert(AVAudioResampleContext *avr, uint8_t **output,
  344. int out_plane_size, int out_samples, uint8_t **input,
  345. int in_plane_size, int in_samples);
  346. /**
  347. * Return the number of samples currently in the resampling delay buffer.
  348. *
  349. * When resampling, there may be a delay between the input and output. Any
  350. * unconverted samples in each call are stored internally in a delay buffer.
  351. * This function allows the user to determine the current number of samples in
  352. * the delay buffer, which can be useful for synchronization.
  353. *
  354. * @see avresample_convert()
  355. *
  356. * @param avr audio resample context
  357. * @return number of samples currently in the resampling delay buffer
  358. */
  359. int avresample_get_delay(AVAudioResampleContext *avr);
  360. /**
  361. * Return the number of available samples in the output FIFO.
  362. *
  363. * During conversion, if the user does not specify an output buffer or
  364. * specifies an output buffer that is smaller than what is needed, remaining
  365. * samples that are not written to the output are stored to an internal FIFO
  366. * buffer. The samples in the FIFO can be read with avresample_read() or
  367. * avresample_convert().
  368. *
  369. * @see avresample_read()
  370. * @see avresample_convert()
  371. *
  372. * @param avr audio resample context
  373. * @return number of samples available for reading
  374. */
  375. int avresample_available(AVAudioResampleContext *avr);
  376. /**
  377. * Read samples from the output FIFO.
  378. *
  379. * During conversion, if the user does not specify an output buffer or
  380. * specifies an output buffer that is smaller than what is needed, remaining
  381. * samples that are not written to the output are stored to an internal FIFO
  382. * buffer. This function can be used to read samples from that internal FIFO.
  383. *
  384. * @see avresample_available()
  385. * @see avresample_convert()
  386. *
  387. * @param avr audio resample context
  388. * @param output output data pointers. May be NULL, in which case
  389. * nb_samples of data is discarded from output FIFO.
  390. * @param nb_samples number of samples to read from the FIFO
  391. * @return the number of samples written to output
  392. */
  393. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples);
  394. /**
  395. * @}
  396. */
  397. #endif /* AVRESAMPLE_AVRESAMPLE_H */