swresample.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. /*
  2. * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at)
  3. *
  4. * This file is part of libswresample
  5. *
  6. * libswresample 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. * libswresample 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 libswresample; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. #ifndef SWRESAMPLE_SWRESAMPLE_H
  21. #define SWRESAMPLE_SWRESAMPLE_H
  22. /**
  23. * @file
  24. * @ingroup lswr
  25. * libswresample public header
  26. */
  27. /**
  28. * @defgroup lswr Libswresample
  29. * @{
  30. *
  31. * Libswresample (lswr) is a library that handles audio resampling, sample
  32. * format conversion and mixing.
  33. *
  34. * Interaction with lswr is done through SwrContext, which is
  35. * allocated with swr_alloc() or swr_alloc_set_opts(). 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. * SwrContext *swr = swr_alloc();
  44. * av_opt_set_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);
  45. * av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
  46. * av_opt_set_int(swr, "in_sample_rate", 48000, 0);
  47. * av_opt_set_int(swr, "out_sample_rate", 44100, 0);
  48. * av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
  49. * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
  50. * @endcode
  51. *
  52. * Once all values have been set, it must be initialized with swr_init(). If
  53. * you need to change the conversion parameters, you can change the parameters
  54. * as described above, or by using swr_alloc_set_opts(), then call swr_init()
  55. * again.
  56. *
  57. * The conversion itself is done by repeatedly calling swr_convert().
  58. * Note that the samples may get buffered in swr if you provide insufficient
  59. * output space or if sample rate conversion is done, which requires "future"
  60. * samples. Samples that do not require future input can be retrieved at any
  61. * time by using swr_convert() (in_count can be set to 0).
  62. * At the end of conversion the resampling buffer can be flushed by calling
  63. * swr_convert() with NULL in and 0 in_count.
  64. *
  65. * The delay between input and output, can at any time be found by using
  66. * swr_get_delay().
  67. *
  68. * The following code demonstrates the conversion loop assuming the parameters
  69. * from above and caller-defined functions get_input() and handle_output():
  70. * @code
  71. * uint8_t **input;
  72. * int in_samples;
  73. *
  74. * while (get_input(&input, &in_samples)) {
  75. * uint8_t *output;
  76. * int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +
  77. * in_samples, 44100, 48000, AV_ROUND_UP);
  78. * av_samples_alloc(&output, NULL, 2, out_samples,
  79. * AV_SAMPLE_FMT_S16, 0);
  80. * out_samples = swr_convert(swr, &output, out_samples,
  81. * input, in_samples);
  82. * handle_output(output, out_samples);
  83. * av_freep(&output);
  84. * }
  85. * @endcode
  86. *
  87. * When the conversion is finished, the conversion
  88. * context and everything associated with it must be freed with swr_free().
  89. * There will be no memory leak if the data is not completely flushed before
  90. * swr_free().
  91. */
  92. #include <stdint.h>
  93. #include "libavutil/samplefmt.h"
  94. #include "libswresample/version.h"
  95. #if LIBSWRESAMPLE_VERSION_MAJOR < 1
  96. #define SWR_CH_MAX 32 ///< Maximum number of channels
  97. #endif
  98. #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
  99. //TODO use int resample ?
  100. //long term TODO can we enable this dynamically?
  101. enum SwrDitherType {
  102. SWR_DITHER_NONE = 0,
  103. SWR_DITHER_RECTANGULAR,
  104. SWR_DITHER_TRIANGULAR,
  105. SWR_DITHER_TRIANGULAR_HIGHPASS,
  106. SWR_DITHER_NS = 64, ///< not part of API/ABI
  107. SWR_DITHER_NS_LIPSHITZ,
  108. SWR_DITHER_NS_F_WEIGHTED,
  109. SWR_DITHER_NS_MODIFIED_E_WEIGHTED,
  110. SWR_DITHER_NS_IMPROVED_E_WEIGHTED,
  111. SWR_DITHER_NS_SHIBATA,
  112. SWR_DITHER_NS_LOW_SHIBATA,
  113. SWR_DITHER_NS_HIGH_SHIBATA,
  114. SWR_DITHER_NB, ///< not part of API/ABI
  115. };
  116. /** Resampling Engines */
  117. enum SwrEngine {
  118. SWR_ENGINE_SWR, /**< SW Resampler */
  119. SWR_ENGINE_SOXR, /**< SoX Resampler */
  120. SWR_ENGINE_NB, ///< not part of API/ABI
  121. };
  122. /** Resampling Filter Types */
  123. enum SwrFilterType {
  124. SWR_FILTER_TYPE_CUBIC, /**< Cubic */
  125. SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */
  126. SWR_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */
  127. };
  128. typedef struct SwrContext SwrContext;
  129. /**
  130. * Get the AVClass for swrContext. It can be used in combination with
  131. * AV_OPT_SEARCH_FAKE_OBJ for examining options.
  132. *
  133. * @see av_opt_find().
  134. */
  135. const AVClass *swr_get_class(void);
  136. /**
  137. * Allocate SwrContext.
  138. *
  139. * If you use this function you will need to set the parameters (manually or
  140. * with swr_alloc_set_opts()) before calling swr_init().
  141. *
  142. * @see swr_alloc_set_opts(), swr_init(), swr_free()
  143. * @return NULL on error, allocated context otherwise
  144. */
  145. struct SwrContext *swr_alloc(void);
  146. /**
  147. * Initialize context after user parameters have been set.
  148. *
  149. * @return AVERROR error code in case of failure.
  150. */
  151. int swr_init(struct SwrContext *s);
  152. /**
  153. * Check whether an swr context has been initialized or not.
  154. *
  155. * @return positive if it has been initialized, 0 if not initialized
  156. */
  157. int swr_is_initialized(struct SwrContext *s);
  158. /**
  159. * Allocate SwrContext if needed and set/reset common parameters.
  160. *
  161. * This function does not require s to be allocated with swr_alloc(). On the
  162. * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
  163. * on the allocated context.
  164. *
  165. * @param s Swr context, can be NULL
  166. * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
  167. * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
  168. * @param out_sample_rate output sample rate (frequency in Hz)
  169. * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
  170. * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
  171. * @param in_sample_rate input sample rate (frequency in Hz)
  172. * @param log_offset logging level offset
  173. * @param log_ctx parent logging context, can be NULL
  174. *
  175. * @see swr_init(), swr_free()
  176. * @return NULL on error, allocated context otherwise
  177. */
  178. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  179. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  180. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  181. int log_offset, void *log_ctx);
  182. /**
  183. * Free the given SwrContext and set the pointer to NULL.
  184. */
  185. void swr_free(struct SwrContext **s);
  186. /**
  187. * Convert audio.
  188. *
  189. * in and in_count can be set to 0 to flush the last few samples out at the
  190. * end.
  191. *
  192. * If more input is provided than output space then the input will be buffered.
  193. * You can avoid this buffering by providing more output space than input.
  194. * Convertion will run directly without copying whenever possible.
  195. *
  196. * @param s allocated Swr context, with parameters set
  197. * @param out output buffers, only the first one need be set in case of packed audio
  198. * @param out_count amount of space available for output in samples per channel
  199. * @param in input buffers, only the first one need to be set in case of packed audio
  200. * @param in_count number of input samples available in one channel
  201. *
  202. * @return number of samples output per channel, negative value on error
  203. */
  204. int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
  205. const uint8_t **in , int in_count);
  206. /**
  207. * Convert the next timestamp from input to output
  208. * timestamps are in 1/(in_sample_rate * out_sample_rate) units.
  209. *
  210. * @note There are 2 slightly differently behaving modes.
  211. * First is when automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
  212. * in this case timestamps will be passed through with delays compensated
  213. * Second is when automatic timestamp compensation is used, (min_compensation < FLT_MAX)
  214. * in this case the output timestamps will match output sample numbers
  215. *
  216. * @param pts timestamp for the next input sample, INT64_MIN if unknown
  217. * @return the output timestamp for the next output sample
  218. */
  219. int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
  220. /**
  221. * Activate resampling compensation.
  222. */
  223. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
  224. /**
  225. * Set a customized input channel mapping.
  226. *
  227. * @param s allocated Swr context, not yet initialized
  228. * @param channel_map customized input channel mapping (array of channel
  229. * indexes, -1 for a muted channel)
  230. * @return AVERROR error code in case of failure.
  231. */
  232. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
  233. /**
  234. * Set a customized remix matrix.
  235. *
  236. * @param s allocated Swr context, not yet initialized
  237. * @param matrix remix coefficients; matrix[i + stride * o] is
  238. * the weight of input channel i in output channel o
  239. * @param stride offset between lines of the matrix
  240. * @return AVERROR error code in case of failure.
  241. */
  242. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
  243. /**
  244. * Drops the specified number of output samples.
  245. */
  246. int swr_drop_output(struct SwrContext *s, int count);
  247. /**
  248. * Injects the specified number of silence samples.
  249. */
  250. int swr_inject_silence(struct SwrContext *s, int count);
  251. /**
  252. * Gets the delay the next input sample will experience relative to the next output sample.
  253. *
  254. * Swresample can buffer data if more input has been provided than available
  255. * output space, also converting between sample rates needs a delay.
  256. * This function returns the sum of all such delays.
  257. * The exact delay is not necessarily an integer value in either input or
  258. * output sample rate. Especially when downsampling by a large value, the
  259. * output sample rate may be a poor choice to represent the delay, similarly
  260. * for upsampling and the input sample rate.
  261. *
  262. * @param s swr context
  263. * @param base timebase in which the returned delay will be
  264. * if its set to 1 the returned delay is in seconds
  265. * if its set to 1000 the returned delay is in milli seconds
  266. * if its set to the input sample rate then the returned delay is in input samples
  267. * if its set to the output sample rate then the returned delay is in output samples
  268. * an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate)
  269. * @returns the delay in 1/base units.
  270. */
  271. int64_t swr_get_delay(struct SwrContext *s, int64_t base);
  272. /**
  273. * Return the LIBSWRESAMPLE_VERSION_INT constant.
  274. */
  275. unsigned swresample_version(void);
  276. /**
  277. * Return the swr build-time configuration.
  278. */
  279. const char *swresample_configuration(void);
  280. /**
  281. * Return the swr license.
  282. */
  283. const char *swresample_license(void);
  284. /**
  285. * @}
  286. */
  287. #endif /* SWRESAMPLE_SWRESAMPLE_H */