swresample.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. * The first thing you will need to do in order to use lswr is to allocate
  39. * SwrContext. This can be done with swr_alloc() or swr_alloc_set_opts(). If you
  40. * are using the former, you must set options through the @ref avoptions API.
  41. * The latter function provides the same feature, but it allows you to set some
  42. * common options in the same statement.
  43. *
  44. * For example the following code will setup conversion from planar float sample
  45. * format to interleaved signed 16-bit integer, downsampling from 48kHz to
  46. * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
  47. * matrix). This is using the swr_alloc() function.
  48. * @code
  49. * SwrContext *swr = swr_alloc();
  50. * av_opt_set_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);
  51. * av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
  52. * av_opt_set_int(swr, "in_sample_rate", 48000, 0);
  53. * av_opt_set_int(swr, "out_sample_rate", 44100, 0);
  54. * av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
  55. * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
  56. * @endcode
  57. *
  58. * The same job can be done using swr_alloc_set_opts() as well:
  59. * @code
  60. * SwrContext *swr = swr_alloc_set_opts(NULL, // we're allocating a new context
  61. * AV_CH_LAYOUT_STEREO, // out_ch_layout
  62. * AV_SAMPLE_FMT_S16, // out_sample_fmt
  63. * 44100, // out_sample_rate
  64. * AV_CH_LAYOUT_5POINT1, // in_ch_layout
  65. * AV_SAMPLE_FMT_FLTP, // in_sample_fmt
  66. * 48000, // in_sample_rate
  67. * 0, // log_offset
  68. * NULL); // log_ctx
  69. * @endcode
  70. *
  71. * Once all values have been set, it must be initialized with swr_init(). If
  72. * you need to change the conversion parameters, you can change the parameters
  73. * using @ref AVOptions, as described above in the first example; or by using
  74. * swr_alloc_set_opts(), but with the first argument the allocated context.
  75. * You must then call swr_init() again.
  76. *
  77. * The conversion itself is done by repeatedly calling swr_convert().
  78. * Note that the samples may get buffered in swr if you provide insufficient
  79. * output space or if sample rate conversion is done, which requires "future"
  80. * samples. Samples that do not require future input can be retrieved at any
  81. * time by using swr_convert() (in_count can be set to 0).
  82. * At the end of conversion the resampling buffer can be flushed by calling
  83. * swr_convert() with NULL in and 0 in_count.
  84. *
  85. * The samples used in the conversion process can be managed with the libavutil
  86. * @ref lavu_sampmanip "samples manipulation" API, including av_samples_alloc()
  87. * function used in the following example.
  88. *
  89. * The delay between input and output, can at any time be found by using
  90. * swr_get_delay().
  91. *
  92. * The following code demonstrates the conversion loop assuming the parameters
  93. * from above and caller-defined functions get_input() and handle_output():
  94. * @code
  95. * uint8_t **input;
  96. * int in_samples;
  97. *
  98. * while (get_input(&input, &in_samples)) {
  99. * uint8_t *output;
  100. * int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +
  101. * in_samples, 44100, 48000, AV_ROUND_UP);
  102. * av_samples_alloc(&output, NULL, 2, out_samples,
  103. * AV_SAMPLE_FMT_S16, 0);
  104. * out_samples = swr_convert(swr, &output, out_samples,
  105. * input, in_samples);
  106. * handle_output(output, out_samples);
  107. * av_freep(&output);
  108. * }
  109. * @endcode
  110. *
  111. * When the conversion is finished, the conversion
  112. * context and everything associated with it must be freed with swr_free().
  113. * A swr_close() function is also available, but it exists mainly for
  114. * compatibility with libavresample, and is not required to be called.
  115. *
  116. * There will be no memory leak if the data is not completely flushed before
  117. * swr_free().
  118. */
  119. #include <stdint.h>
  120. #include "libavutil/samplefmt.h"
  121. #include "libswresample/version.h"
  122. #if LIBSWRESAMPLE_VERSION_MAJOR < 1
  123. #define SWR_CH_MAX 32 ///< Maximum number of channels
  124. #endif
  125. /**
  126. * @name Option constants
  127. * These constants are used for the @ref avoptions interface for lswr.
  128. * @{
  129. *
  130. */
  131. #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
  132. //TODO use int resample ?
  133. //long term TODO can we enable this dynamically?
  134. /** Dithering algorithms */
  135. enum SwrDitherType {
  136. SWR_DITHER_NONE = 0,
  137. SWR_DITHER_RECTANGULAR,
  138. SWR_DITHER_TRIANGULAR,
  139. SWR_DITHER_TRIANGULAR_HIGHPASS,
  140. SWR_DITHER_NS = 64, ///< not part of API/ABI
  141. SWR_DITHER_NS_LIPSHITZ,
  142. SWR_DITHER_NS_F_WEIGHTED,
  143. SWR_DITHER_NS_MODIFIED_E_WEIGHTED,
  144. SWR_DITHER_NS_IMPROVED_E_WEIGHTED,
  145. SWR_DITHER_NS_SHIBATA,
  146. SWR_DITHER_NS_LOW_SHIBATA,
  147. SWR_DITHER_NS_HIGH_SHIBATA,
  148. SWR_DITHER_NB, ///< not part of API/ABI
  149. };
  150. /** Resampling Engines */
  151. enum SwrEngine {
  152. SWR_ENGINE_SWR, /**< SW Resampler */
  153. SWR_ENGINE_SOXR, /**< SoX Resampler */
  154. SWR_ENGINE_NB, ///< not part of API/ABI
  155. };
  156. /** Resampling Filter Types */
  157. enum SwrFilterType {
  158. SWR_FILTER_TYPE_CUBIC, /**< Cubic */
  159. SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */
  160. SWR_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */
  161. };
  162. /**
  163. * @}
  164. */
  165. /**
  166. * The libswresample context. Unlike libavcodec and libavformat, this structure
  167. * is opaque. This means that if you would like to set options, you must use
  168. * the @ref avoptions API and cannot directly set values to members of the
  169. * structure.
  170. */
  171. typedef struct SwrContext SwrContext;
  172. /**
  173. * Get the AVClass for SwrContext. It can be used in combination with
  174. * AV_OPT_SEARCH_FAKE_OBJ for examining options.
  175. *
  176. * @see av_opt_find().
  177. * @return the AVClass of SwrContext
  178. */
  179. const AVClass *swr_get_class(void);
  180. /**
  181. * @name SwrContext constructor functions
  182. * @{
  183. */
  184. /**
  185. * Allocate SwrContext.
  186. *
  187. * If you use this function you will need to set the parameters (manually or
  188. * with swr_alloc_set_opts()) before calling swr_init().
  189. *
  190. * @see swr_alloc_set_opts(), swr_init(), swr_free()
  191. * @return NULL on error, allocated context otherwise
  192. */
  193. struct SwrContext *swr_alloc(void);
  194. /**
  195. * Initialize context after user parameters have been set.
  196. *
  197. * @param[in,out] s Swr context to initialize
  198. * @return AVERROR error code in case of failure.
  199. */
  200. int swr_init(struct SwrContext *s);
  201. /**
  202. * Check whether an swr context has been initialized or not.
  203. *
  204. * @param[in] s Swr context to check
  205. * @see swr_init()
  206. * @return positive if it has been initialized, 0 if not initialized
  207. */
  208. int swr_is_initialized(struct SwrContext *s);
  209. /**
  210. * Allocate SwrContext if needed and set/reset common parameters.
  211. *
  212. * This function does not require s to be allocated with swr_alloc(). On the
  213. * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
  214. * on the allocated context.
  215. *
  216. * @param s existing Swr context if available, or NULL if not
  217. * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
  218. * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
  219. * @param out_sample_rate output sample rate (frequency in Hz)
  220. * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
  221. * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
  222. * @param in_sample_rate input sample rate (frequency in Hz)
  223. * @param log_offset logging level offset
  224. * @param log_ctx parent logging context, can be NULL
  225. *
  226. * @see swr_init(), swr_free()
  227. * @return NULL on error, allocated context otherwise
  228. */
  229. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  230. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  231. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  232. int log_offset, void *log_ctx);
  233. /**
  234. * @}
  235. *
  236. * @name SwrContext destructor functions
  237. * @{
  238. */
  239. /**
  240. * Free the given SwrContext and set the pointer to NULL.
  241. *
  242. * @param[in] s a pointer to a pointer to Swr context
  243. */
  244. void swr_free(struct SwrContext **s);
  245. /**
  246. * Closes the context so that swr_is_initialized() returns 0.
  247. *
  248. * The context can be brought back to life by running swr_init(),
  249. * swr_init() can also be used without swr_close().
  250. * This function is mainly provided for simplifying the usecase
  251. * where one tries to support libavresample and libswresample.
  252. *
  253. * @param[in,out] s Swr context to be closed
  254. */
  255. void swr_close(struct SwrContext *s);
  256. /**
  257. * @}
  258. *
  259. * @name Core conversion functions
  260. * @{
  261. */
  262. /** Convert audio.
  263. *
  264. * in and in_count can be set to 0 to flush the last few samples out at the
  265. * end.
  266. *
  267. * If more input is provided than output space then the input will be buffered.
  268. * You can avoid this buffering by providing more output space than input.
  269. * Conversion will run directly without copying whenever possible.
  270. *
  271. * @param s allocated Swr context, with parameters set
  272. * @param out output buffers, only the first one need be set in case of packed audio
  273. * @param out_count amount of space available for output in samples per channel
  274. * @param in input buffers, only the first one need to be set in case of packed audio
  275. * @param in_count number of input samples available in one channel
  276. *
  277. * @return number of samples output per channel, negative value on error
  278. */
  279. int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
  280. const uint8_t **in , int in_count);
  281. /**
  282. * Convert the next timestamp from input to output
  283. * timestamps are in 1/(in_sample_rate * out_sample_rate) units.
  284. *
  285. * @note There are 2 slightly differently behaving modes.
  286. * @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
  287. * in this case timestamps will be passed through with delays compensated
  288. * @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX)
  289. * in this case the output timestamps will match output sample numbers.
  290. * See ffmpeg-resampler(1) for the two modes of compensation.
  291. *
  292. * @param s[in] initialized Swr context
  293. * @param pts[in] timestamp for the next input sample, INT64_MIN if unknown
  294. * @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are
  295. * function used internally for timestamp compensation.
  296. * @return the output timestamp for the next output sample
  297. */
  298. int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
  299. /**
  300. * @}
  301. *
  302. * @name Low-level option setting functions
  303. * These functons provide a means to set low-level options that is not possible
  304. * with the AVOption API.
  305. * @{
  306. */
  307. /**
  308. * Activate resampling compensation ("soft" compensation). This function is
  309. * internally called when needed in swr_next_pts().
  310. *
  311. * @param[in,out] s allocated Swr context. If it is not initialized,
  312. * or SWR_FLAG_RESAMPLE is not set, swr_init() is
  313. * called with the flag set.
  314. * @param[in] sample_delta delta in PTS per sample
  315. * @param[in] compensation_distance number of samples to compensate for
  316. * @return >= 0 on success, AVERROR error codes if:
  317. * @li @c s is NULL,
  318. * @li @c compensation_distance is less than 0,
  319. * @li @c compensation_distance is 0 but sample_delta is not,
  320. * @li compensation unsupported by resampler, or
  321. * @li swr_init() fails when called.
  322. */
  323. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
  324. /**
  325. * Set a customized input channel mapping.
  326. *
  327. * @param[in,out] s allocated Swr context, not yet initialized
  328. * @param[in] channel_map customized input channel mapping (array of channel
  329. * indexes, -1 for a muted channel)
  330. * @return >= 0 on success, or AVERROR error code in case of failure.
  331. */
  332. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
  333. /**
  334. * Set a customized remix matrix.
  335. *
  336. * @param s allocated Swr context, not yet initialized
  337. * @param matrix remix coefficients; matrix[i + stride * o] is
  338. * the weight of input channel i in output channel o
  339. * @param stride offset between lines of the matrix
  340. * @return >= 0 on success, or AVERROR error code in case of failure.
  341. */
  342. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
  343. /**
  344. * @}
  345. *
  346. * @name Sample handling functions
  347. * @{
  348. */
  349. /**
  350. * Drops the specified number of output samples.
  351. *
  352. * This function, along with swr_inject_silence(), is called by swr_next_pts()
  353. * if needed for "hard" compensation.
  354. *
  355. * @param s allocated Swr context
  356. * @param count number of samples to be dropped
  357. *
  358. * @return >= 0 on success, or a negative AVERROR code on failure
  359. */
  360. int swr_drop_output(struct SwrContext *s, int count);
  361. /**
  362. * Injects the specified number of silence samples.
  363. *
  364. * This function, along with swr_drop_output(), is called by swr_next_pts()
  365. * if needed for "hard" compensation.
  366. *
  367. * @param s allocated Swr context
  368. * @param count number of samples to be dropped
  369. *
  370. * @return >= 0 on success, or a negative AVERROR code on failure
  371. */
  372. int swr_inject_silence(struct SwrContext *s, int count);
  373. /**
  374. * Gets the delay the next input sample will experience relative to the next output sample.
  375. *
  376. * Swresample can buffer data if more input has been provided than available
  377. * output space, also converting between sample rates needs a delay.
  378. * This function returns the sum of all such delays.
  379. * The exact delay is not necessarily an integer value in either input or
  380. * output sample rate. Especially when downsampling by a large value, the
  381. * output sample rate may be a poor choice to represent the delay, similarly
  382. * for upsampling and the input sample rate.
  383. *
  384. * @param s swr context
  385. * @param base timebase in which the returned delay will be:
  386. * @li if it's set to 1 the returned delay is in seconds
  387. * @li if it's set to 1000 the returned delay is in milliseconds
  388. * @li if it's set to the input sample rate then the returned
  389. * delay is in input samples
  390. * @li if it's set to the output sample rate then the returned
  391. * delay is in output samples
  392. * @li if it's the least common multiple of in_sample_rate and
  393. * out_sample_rate then an exact rounding-free delay will be
  394. * returned
  395. * @returns the delay in 1 / @c base units.
  396. */
  397. int64_t swr_get_delay(struct SwrContext *s, int64_t base);
  398. /**
  399. * @}
  400. *
  401. * @name Configuration accessors
  402. * @{
  403. */
  404. /**
  405. * Return the @ref LIBSWRESAMPLE_VERSION_INT constant.
  406. *
  407. * This is useful to check if the build-time libswresample has the same version
  408. * as the run-time one.
  409. *
  410. * @returns the unsigned int-typed version
  411. */
  412. unsigned swresample_version(void);
  413. /**
  414. * Return the swr build-time configuration.
  415. *
  416. * @returns the build-time @c ./configure flags
  417. */
  418. const char *swresample_configuration(void);
  419. /**
  420. * Return the swr license.
  421. *
  422. * @returns the license of libswresample, determined at build-time
  423. */
  424. const char *swresample_license(void);
  425. /**
  426. * @}
  427. * @}
  428. */
  429. #endif /* SWRESAMPLE_SWRESAMPLE_H */