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.

295 lines
11KB

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