avresample.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498
  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/frame.h"
  95. #include "libavutil/log.h"
  96. #include "libavutil/mathematics.h"
  97. #include "libavresample/version.h"
  98. #define AVRESAMPLE_MAX_CHANNELS 32
  99. typedef struct AVAudioResampleContext AVAudioResampleContext;
  100. /** Mixing Coefficient Types */
  101. enum AVMixCoeffType {
  102. AV_MIX_COEFF_TYPE_Q8, /** 16-bit 8.8 fixed-point */
  103. AV_MIX_COEFF_TYPE_Q15, /** 32-bit 17.15 fixed-point */
  104. AV_MIX_COEFF_TYPE_FLT, /** floating-point */
  105. AV_MIX_COEFF_TYPE_NB, /** Number of coeff types. Not part of ABI */
  106. };
  107. /** Resampling Filter Types */
  108. enum AVResampleFilterType {
  109. AV_RESAMPLE_FILTER_TYPE_CUBIC, /**< Cubic */
  110. AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */
  111. AV_RESAMPLE_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */
  112. };
  113. enum AVResampleDitherMethod {
  114. AV_RESAMPLE_DITHER_NONE, /**< Do not use dithering */
  115. AV_RESAMPLE_DITHER_RECTANGULAR, /**< Rectangular Dither */
  116. AV_RESAMPLE_DITHER_TRIANGULAR, /**< Triangular Dither*/
  117. AV_RESAMPLE_DITHER_TRIANGULAR_HP, /**< Triangular Dither with High Pass */
  118. AV_RESAMPLE_DITHER_TRIANGULAR_NS, /**< Triangular Dither with Noise Shaping */
  119. AV_RESAMPLE_DITHER_NB, /**< Number of dither types. Not part of ABI. */
  120. };
  121. /**
  122. * Return the LIBAVRESAMPLE_VERSION_INT constant.
  123. */
  124. unsigned avresample_version(void);
  125. /**
  126. * Return the libavresample build-time configuration.
  127. * @return configure string
  128. */
  129. const char *avresample_configuration(void);
  130. /**
  131. * Return the libavresample license.
  132. */
  133. const char *avresample_license(void);
  134. /**
  135. * Get the AVClass for AVAudioResampleContext.
  136. *
  137. * Can be used in combination with AV_OPT_SEARCH_FAKE_OBJ for examining options
  138. * without allocating a context.
  139. *
  140. * @see av_opt_find().
  141. *
  142. * @return AVClass for AVAudioResampleContext
  143. */
  144. const AVClass *avresample_get_class(void);
  145. /**
  146. * Allocate AVAudioResampleContext and set options.
  147. *
  148. * @return allocated audio resample context, or NULL on failure
  149. */
  150. AVAudioResampleContext *avresample_alloc_context(void);
  151. /**
  152. * Initialize AVAudioResampleContext.
  153. * @note The context must be configured using the AVOption API.
  154. *
  155. * @see av_opt_set_int()
  156. * @see av_opt_set_dict()
  157. *
  158. * @param avr audio resample context
  159. * @return 0 on success, negative AVERROR code on failure
  160. */
  161. int avresample_open(AVAudioResampleContext *avr);
  162. /**
  163. * Check whether an AVAudioResampleContext is open or closed.
  164. *
  165. * @param avr AVAudioResampleContext to check
  166. * @return 1 if avr is open, 0 if avr is closed.
  167. */
  168. int avresample_is_open(AVAudioResampleContext *avr);
  169. /**
  170. * Close AVAudioResampleContext.
  171. *
  172. * This closes the context, but it does not change the parameters. The context
  173. * can be reopened with avresample_open(). It does, however, clear the output
  174. * FIFO and any remaining leftover samples in the resampling delay buffer. If
  175. * there was a custom matrix being used, that is also cleared.
  176. *
  177. * @see avresample_convert()
  178. * @see avresample_set_matrix()
  179. *
  180. * @param avr audio resample context
  181. */
  182. void avresample_close(AVAudioResampleContext *avr);
  183. /**
  184. * Free AVAudioResampleContext and associated AVOption values.
  185. *
  186. * This also calls avresample_close() before freeing.
  187. *
  188. * @param avr audio resample context
  189. */
  190. void avresample_free(AVAudioResampleContext **avr);
  191. /**
  192. * Generate a channel mixing matrix.
  193. *
  194. * This function is the one used internally by libavresample for building the
  195. * default mixing matrix. It is made public just as a utility function for
  196. * building custom matrices.
  197. *
  198. * @param in_layout input channel layout
  199. * @param out_layout output channel layout
  200. * @param center_mix_level mix level for the center channel
  201. * @param surround_mix_level mix level for the surround channel(s)
  202. * @param lfe_mix_level mix level for the low-frequency effects channel
  203. * @param normalize if 1, coefficients will be normalized to prevent
  204. * overflow. if 0, coefficients will not be
  205. * normalized.
  206. * @param[out] matrix mixing coefficients; matrix[i + stride * o] is
  207. * the weight of input channel i in output channel o.
  208. * @param stride distance between adjacent input channels in the
  209. * matrix array
  210. * @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)
  211. * @return 0 on success, negative AVERROR code on failure
  212. */
  213. int avresample_build_matrix(uint64_t in_layout, uint64_t out_layout,
  214. double center_mix_level, double surround_mix_level,
  215. double lfe_mix_level, int normalize, double *matrix,
  216. int stride, enum AVMatrixEncoding matrix_encoding);
  217. /**
  218. * Get the current channel mixing matrix.
  219. *
  220. * If no custom matrix has been previously set or the AVAudioResampleContext is
  221. * not open, an error is returned.
  222. *
  223. * @param avr audio resample context
  224. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  225. * input channel i in output channel o.
  226. * @param stride distance between adjacent input channels in the matrix array
  227. * @return 0 on success, negative AVERROR code on failure
  228. */
  229. int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix,
  230. int stride);
  231. /**
  232. * Set channel mixing matrix.
  233. *
  234. * Allows for setting a custom mixing matrix, overriding the default matrix
  235. * generated internally during avresample_open(). This function can be called
  236. * anytime on an allocated context, either before or after calling
  237. * avresample_open(), as long as the channel layouts have been set.
  238. * avresample_convert() always uses the current matrix.
  239. * Calling avresample_close() on the context will clear the current matrix.
  240. *
  241. * @see avresample_close()
  242. *
  243. * @param avr audio resample context
  244. * @param matrix mixing coefficients; matrix[i + stride * o] is the weight of
  245. * input channel i in output channel o.
  246. * @param stride distance between adjacent input channels in the matrix array
  247. * @return 0 on success, negative AVERROR code on failure
  248. */
  249. int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix,
  250. int stride);
  251. /**
  252. * Set a customized input channel mapping.
  253. *
  254. * This function can only be called when the allocated context is not open.
  255. * Also, the input channel layout must have already been set.
  256. *
  257. * Calling avresample_close() on the context will clear the channel mapping.
  258. *
  259. * The map for each input channel specifies the channel index in the source to
  260. * use for that particular channel, or -1 to mute the channel. Source channels
  261. * can be duplicated by using the same index for multiple input channels.
  262. *
  263. * Examples:
  264. *
  265. * Reordering 5.1 AAC order (C,L,R,Ls,Rs,LFE) to FFmpeg order (L,R,C,LFE,Ls,Rs):
  266. * { 1, 2, 0, 5, 3, 4 }
  267. *
  268. * Muting the 3rd channel in 4-channel input:
  269. * { 0, 1, -1, 3 }
  270. *
  271. * Duplicating the left channel of stereo input:
  272. * { 0, 0 }
  273. *
  274. * @param avr audio resample context
  275. * @param channel_map customized input channel mapping
  276. * @return 0 on success, negative AVERROR code on failure
  277. */
  278. int avresample_set_channel_mapping(AVAudioResampleContext *avr,
  279. const int *channel_map);
  280. /**
  281. * Set compensation for resampling.
  282. *
  283. * This can be called anytime after avresample_open(). If resampling is not
  284. * automatically enabled because of a sample rate conversion, the
  285. * "force_resampling" option must have been set to 1 when opening the context
  286. * in order to use resampling compensation.
  287. *
  288. * @param avr audio resample context
  289. * @param sample_delta compensation delta, in samples
  290. * @param compensation_distance compensation distance, in samples
  291. * @return 0 on success, negative AVERROR code on failure
  292. */
  293. int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
  294. int compensation_distance);
  295. /**
  296. * Provide the upper bound on the number of samples the configured
  297. * conversion would output.
  298. *
  299. * @param avr audio resample context
  300. * @param in_nb_samples number of input samples
  301. *
  302. * @return number of samples or AVERROR(EINVAL) if the value
  303. * would exceed INT_MAX
  304. */
  305. int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples);
  306. /**
  307. * Convert input samples and write them to the output FIFO.
  308. *
  309. * The upper bound on the number of output samples can be obtained through
  310. * avresample_get_out_samples().
  311. *
  312. * The output data can be NULL or have fewer allocated samples than required.
  313. * In this case, any remaining samples not written to the output will be added
  314. * to an internal FIFO buffer, to be returned at the next call to this function
  315. * or to avresample_read().
  316. *
  317. * If converting sample rate, there may be data remaining in the internal
  318. * resampling delay buffer. avresample_get_delay() tells the number of remaining
  319. * samples. To get this data as output, call avresample_convert() with NULL
  320. * input.
  321. *
  322. * At the end of the conversion process, there may be data remaining in the
  323. * internal FIFO buffer. avresample_available() tells the number of remaining
  324. * samples. To get this data as output, either call avresample_convert() with
  325. * NULL input or call avresample_read().
  326. *
  327. * @see avresample_get_out_samples()
  328. * @see avresample_read()
  329. * @see avresample_get_delay()
  330. *
  331. * @param avr audio resample context
  332. * @param output output data pointers
  333. * @param out_plane_size output plane size, in bytes.
  334. * This can be 0 if unknown, but that will lead to
  335. * optimized functions not being used directly on the
  336. * output, which could slow down some conversions.
  337. * @param out_samples maximum number of samples that the output buffer can hold
  338. * @param input input data pointers
  339. * @param in_plane_size input plane size, in bytes
  340. * This can be 0 if unknown, but that will lead to
  341. * optimized functions not being used directly on the
  342. * input, which could slow down some conversions.
  343. * @param in_samples number of input samples to convert
  344. * @return number of samples written to the output buffer,
  345. * not including converted samples added to the internal
  346. * output FIFO
  347. */
  348. int avresample_convert(AVAudioResampleContext *avr, uint8_t **output,
  349. int out_plane_size, int out_samples, uint8_t **input,
  350. int in_plane_size, int in_samples);
  351. /**
  352. * Return the number of samples currently in the resampling delay buffer.
  353. *
  354. * When resampling, there may be a delay between the input and output. Any
  355. * unconverted samples in each call are stored internally in a delay buffer.
  356. * This function allows the user to determine the current number of samples in
  357. * the delay buffer, which can be useful for synchronization.
  358. *
  359. * @see avresample_convert()
  360. *
  361. * @param avr audio resample context
  362. * @return number of samples currently in the resampling delay buffer
  363. */
  364. int avresample_get_delay(AVAudioResampleContext *avr);
  365. /**
  366. * Return the number of available samples in the output FIFO.
  367. *
  368. * During conversion, if the user does not specify an output buffer or
  369. * specifies an output buffer that is smaller than what is needed, remaining
  370. * samples that are not written to the output are stored to an internal FIFO
  371. * buffer. The samples in the FIFO can be read with avresample_read() or
  372. * avresample_convert().
  373. *
  374. * @see avresample_read()
  375. * @see avresample_convert()
  376. *
  377. * @param avr audio resample context
  378. * @return number of samples available for reading
  379. */
  380. int avresample_available(AVAudioResampleContext *avr);
  381. /**
  382. * Read samples from the output FIFO.
  383. *
  384. * During conversion, if the user does not specify an output buffer or
  385. * specifies an output buffer that is smaller than what is needed, remaining
  386. * samples that are not written to the output are stored to an internal FIFO
  387. * buffer. This function can be used to read samples from that internal FIFO.
  388. *
  389. * @see avresample_available()
  390. * @see avresample_convert()
  391. *
  392. * @param avr audio resample context
  393. * @param output output data pointers. May be NULL, in which case
  394. * nb_samples of data is discarded from output FIFO.
  395. * @param nb_samples number of samples to read from the FIFO
  396. * @return the number of samples written to output
  397. */
  398. int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples);
  399. /**
  400. * Convert the samples in the input AVFrame and write them to the output AVFrame.
  401. *
  402. * Input and output AVFrames must have channel_layout, sample_rate and format set.
  403. *
  404. * The upper bound on the number of output samples is obtained through
  405. * avresample_get_out_samples().
  406. *
  407. * If the output AVFrame does not have the data pointers allocated the nb_samples
  408. * field will be set using avresample_get_out_samples() and av_frame_get_buffer()
  409. * is called to allocate the frame.
  410. *
  411. * The output AVFrame can be NULL or have fewer allocated samples than required.
  412. * In this case, any remaining samples not written to the output will be added
  413. * to an internal FIFO buffer, to be returned at the next call to this function
  414. * or to avresample_convert() or to avresample_read().
  415. *
  416. * If converting sample rate, there may be data remaining in the internal
  417. * resampling delay buffer. avresample_get_delay() tells the number of
  418. * remaining samples. To get this data as output, call this function or
  419. * avresample_convert() with NULL input.
  420. *
  421. * At the end of the conversion process, there may be data remaining in the
  422. * internal FIFO buffer. avresample_available() tells the number of remaining
  423. * samples. To get this data as output, either call this function or
  424. * avresample_convert() with NULL input or call avresample_read().
  425. *
  426. * If the AVAudioResampleContext configuration does not match the output and
  427. * input AVFrame settings the conversion does not take place and depending on
  428. * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED
  429. * or AVERROR_OUTPUT_CHANGED|AVERROR_INPUT_CHANGED is returned.
  430. *
  431. * @see avresample_get_out_samples()
  432. * @see avresample_available()
  433. * @see avresample_convert()
  434. * @see avresample_read()
  435. * @see avresample_get_delay()
  436. *
  437. * @param avr audio resample context
  438. * @param output output AVFrame
  439. * @param input input AVFrame
  440. * @return 0 on success, AVERROR on failure or nonmatching
  441. * configuration.
  442. */
  443. int avresample_convert_frame(AVAudioResampleContext *avr,
  444. AVFrame *output, AVFrame *input);
  445. /**
  446. * Configure or reconfigure the AVAudioResampleContext using the information
  447. * provided by the AVFrames.
  448. *
  449. * The original resampling context is reset even on failure.
  450. * The function calls avresample_close() internally if the context is open.
  451. *
  452. * @see avresample_open();
  453. * @see avresample_close();
  454. *
  455. * @param avr audio resample context
  456. * @param output output AVFrame
  457. * @param input input AVFrame
  458. * @return 0 on success, AVERROR on failure.
  459. */
  460. int avresample_config(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in);
  461. /**
  462. * @}
  463. */
  464. #endif /* AVRESAMPLE_AVRESAMPLE_H */