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.

553 lines
20KB

  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. * Audio resampling, sample format conversion and mixing library.
  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. * The first thing you will need to do in order to use lswr is to allocate
  38. * SwrContext. This can be done with swr_alloc() or swr_alloc_set_opts(). If you
  39. * are using the former, you must set options through the @ref avoptions API.
  40. * The latter function provides the same feature, but it allows you to set some
  41. * common options in the same statement.
  42. *
  43. * For example the following code will setup conversion from planar float sample
  44. * format to interleaved signed 16-bit integer, downsampling from 48kHz to
  45. * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
  46. * matrix). This is using the swr_alloc() function.
  47. * @code
  48. * SwrContext *swr = swr_alloc();
  49. * av_opt_set_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);
  50. * av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
  51. * av_opt_set_int(swr, "in_sample_rate", 48000, 0);
  52. * av_opt_set_int(swr, "out_sample_rate", 44100, 0);
  53. * av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
  54. * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
  55. * @endcode
  56. *
  57. * The same job can be done using swr_alloc_set_opts() as well:
  58. * @code
  59. * SwrContext *swr = swr_alloc_set_opts(NULL, // we're allocating a new context
  60. * AV_CH_LAYOUT_STEREO, // out_ch_layout
  61. * AV_SAMPLE_FMT_S16, // out_sample_fmt
  62. * 44100, // out_sample_rate
  63. * AV_CH_LAYOUT_5POINT1, // in_ch_layout
  64. * AV_SAMPLE_FMT_FLTP, // in_sample_fmt
  65. * 48000, // in_sample_rate
  66. * 0, // log_offset
  67. * NULL); // log_ctx
  68. * @endcode
  69. *
  70. * Once all values have been set, it must be initialized with swr_init(). If
  71. * you need to change the conversion parameters, you can change the parameters
  72. * using @ref AVOptions, as described above in the first example; or by using
  73. * swr_alloc_set_opts(), but with the first argument the allocated context.
  74. * You must then call swr_init() again.
  75. *
  76. * The conversion itself is done by repeatedly calling swr_convert().
  77. * Note that the samples may get buffered in swr if you provide insufficient
  78. * output space or if sample rate conversion is done, which requires "future"
  79. * samples. Samples that do not require future input can be retrieved at any
  80. * time by using swr_convert() (in_count can be set to 0).
  81. * At the end of conversion the resampling buffer can be flushed by calling
  82. * swr_convert() with NULL in and 0 in_count.
  83. *
  84. * The samples used in the conversion process can be managed with the libavutil
  85. * @ref lavu_sampmanip "samples manipulation" API, including av_samples_alloc()
  86. * function used in the following example.
  87. *
  88. * The delay between input and output, can at any time be found by using
  89. * swr_get_delay().
  90. *
  91. * The following code demonstrates the conversion loop assuming the parameters
  92. * from above and caller-defined functions get_input() and handle_output():
  93. * @code
  94. * uint8_t **input;
  95. * int in_samples;
  96. *
  97. * while (get_input(&input, &in_samples)) {
  98. * uint8_t *output;
  99. * int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +
  100. * in_samples, 44100, 48000, AV_ROUND_UP);
  101. * av_samples_alloc(&output, NULL, 2, out_samples,
  102. * AV_SAMPLE_FMT_S16, 0);
  103. * out_samples = swr_convert(swr, &output, out_samples,
  104. * input, in_samples);
  105. * handle_output(output, out_samples);
  106. * av_freep(&output);
  107. * }
  108. * @endcode
  109. *
  110. * When the conversion is finished, the conversion
  111. * context and everything associated with it must be freed with swr_free().
  112. * A swr_close() function is also available, but it exists mainly for
  113. * compatibility with libavresample, and is not required to be called.
  114. *
  115. * There will be no memory leak if the data is not completely flushed before
  116. * swr_free().
  117. */
  118. #include <stdint.h>
  119. #include "libavutil/frame.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. * @note The context must be configured using the AVOption API.
  197. *
  198. * @see av_opt_set_int()
  199. * @see av_opt_set_dict()
  200. *
  201. * @param[in,out] s Swr context to initialize
  202. * @return AVERROR error code in case of failure.
  203. */
  204. int swr_init(struct SwrContext *s);
  205. /**
  206. * Check whether an swr context has been initialized or not.
  207. *
  208. * @param[in] s Swr context to check
  209. * @see swr_init()
  210. * @return positive if it has been initialized, 0 if not initialized
  211. */
  212. int swr_is_initialized(struct SwrContext *s);
  213. /**
  214. * Allocate SwrContext if needed and set/reset common parameters.
  215. *
  216. * This function does not require s to be allocated with swr_alloc(). On the
  217. * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
  218. * on the allocated context.
  219. *
  220. * @param s existing Swr context if available, or NULL if not
  221. * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
  222. * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
  223. * @param out_sample_rate output sample rate (frequency in Hz)
  224. * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
  225. * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
  226. * @param in_sample_rate input sample rate (frequency in Hz)
  227. * @param log_offset logging level offset
  228. * @param log_ctx parent logging context, can be NULL
  229. *
  230. * @see swr_init(), swr_free()
  231. * @return NULL on error, allocated context otherwise
  232. */
  233. struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
  234. int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
  235. int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
  236. int log_offset, void *log_ctx);
  237. /**
  238. * @}
  239. *
  240. * @name SwrContext destructor functions
  241. * @{
  242. */
  243. /**
  244. * Free the given SwrContext and set the pointer to NULL.
  245. *
  246. * @param[in] s a pointer to a pointer to Swr context
  247. */
  248. void swr_free(struct SwrContext **s);
  249. /**
  250. * Closes the context so that swr_is_initialized() returns 0.
  251. *
  252. * The context can be brought back to life by running swr_init(),
  253. * swr_init() can also be used without swr_close().
  254. * This function is mainly provided for simplifying the usecase
  255. * where one tries to support libavresample and libswresample.
  256. *
  257. * @param[in,out] s Swr context to be closed
  258. */
  259. void swr_close(struct SwrContext *s);
  260. /**
  261. * @}
  262. *
  263. * @name Core conversion functions
  264. * @{
  265. */
  266. /** Convert audio.
  267. *
  268. * in and in_count can be set to 0 to flush the last few samples out at the
  269. * end.
  270. *
  271. * If more input is provided than output space, then the input will be buffered.
  272. * You can avoid this buffering by using swr_get_out_samples() to retrieve an
  273. * upper bound on the required number of output samples for the given number of
  274. * input samples. Conversion will run directly without copying whenever possible.
  275. *
  276. * @param s allocated Swr context, with parameters set
  277. * @param out output buffers, only the first one need be set in case of packed audio
  278. * @param out_count amount of space available for output in samples per channel
  279. * @param in input buffers, only the first one need to be set in case of packed audio
  280. * @param in_count number of input samples available in one channel
  281. *
  282. * @return number of samples output per channel, negative value on error
  283. */
  284. int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
  285. const uint8_t **in , int in_count);
  286. /**
  287. * Convert the next timestamp from input to output
  288. * timestamps are in 1/(in_sample_rate * out_sample_rate) units.
  289. *
  290. * @note There are 2 slightly differently behaving modes.
  291. * @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
  292. * in this case timestamps will be passed through with delays compensated
  293. * @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX)
  294. * in this case the output timestamps will match output sample numbers.
  295. * See ffmpeg-resampler(1) for the two modes of compensation.
  296. *
  297. * @param s[in] initialized Swr context
  298. * @param pts[in] timestamp for the next input sample, INT64_MIN if unknown
  299. * @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are
  300. * function used internally for timestamp compensation.
  301. * @return the output timestamp for the next output sample
  302. */
  303. int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
  304. /**
  305. * @}
  306. *
  307. * @name Low-level option setting functions
  308. * These functons provide a means to set low-level options that is not possible
  309. * with the AVOption API.
  310. * @{
  311. */
  312. /**
  313. * Activate resampling compensation ("soft" compensation). This function is
  314. * internally called when needed in swr_next_pts().
  315. *
  316. * @param[in,out] s allocated Swr context. If it is not initialized,
  317. * or SWR_FLAG_RESAMPLE is not set, swr_init() is
  318. * called with the flag set.
  319. * @param[in] sample_delta delta in PTS per sample
  320. * @param[in] compensation_distance number of samples to compensate for
  321. * @return >= 0 on success, AVERROR error codes if:
  322. * @li @c s is NULL,
  323. * @li @c compensation_distance is less than 0,
  324. * @li @c compensation_distance is 0 but sample_delta is not,
  325. * @li compensation unsupported by resampler, or
  326. * @li swr_init() fails when called.
  327. */
  328. int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
  329. /**
  330. * Set a customized input channel mapping.
  331. *
  332. * @param[in,out] s allocated Swr context, not yet initialized
  333. * @param[in] channel_map customized input channel mapping (array of channel
  334. * indexes, -1 for a muted channel)
  335. * @return >= 0 on success, or AVERROR error code in case of failure.
  336. */
  337. int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
  338. /**
  339. * Set a customized remix matrix.
  340. *
  341. * @param s allocated Swr context, not yet initialized
  342. * @param matrix remix coefficients; matrix[i + stride * o] is
  343. * the weight of input channel i in output channel o
  344. * @param stride offset between lines of the matrix
  345. * @return >= 0 on success, or AVERROR error code in case of failure.
  346. */
  347. int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
  348. /**
  349. * @}
  350. *
  351. * @name Sample handling functions
  352. * @{
  353. */
  354. /**
  355. * Drops the specified number of output samples.
  356. *
  357. * This function, along with swr_inject_silence(), is called by swr_next_pts()
  358. * if needed for "hard" compensation.
  359. *
  360. * @param s allocated Swr context
  361. * @param count number of samples to be dropped
  362. *
  363. * @return >= 0 on success, or a negative AVERROR code on failure
  364. */
  365. int swr_drop_output(struct SwrContext *s, int count);
  366. /**
  367. * Injects the specified number of silence samples.
  368. *
  369. * This function, along with swr_drop_output(), is called by swr_next_pts()
  370. * if needed for "hard" compensation.
  371. *
  372. * @param s allocated Swr context
  373. * @param count number of samples to be dropped
  374. *
  375. * @return >= 0 on success, or a negative AVERROR code on failure
  376. */
  377. int swr_inject_silence(struct SwrContext *s, int count);
  378. /**
  379. * Gets the delay the next input sample will experience relative to the next output sample.
  380. *
  381. * Swresample can buffer data if more input has been provided than available
  382. * output space, also converting between sample rates needs a delay.
  383. * This function returns the sum of all such delays.
  384. * The exact delay is not necessarily an integer value in either input or
  385. * output sample rate. Especially when downsampling by a large value, the
  386. * output sample rate may be a poor choice to represent the delay, similarly
  387. * for upsampling and the input sample rate.
  388. *
  389. * @param s swr context
  390. * @param base timebase in which the returned delay will be:
  391. * @li if it's set to 1 the returned delay is in seconds
  392. * @li if it's set to 1000 the returned delay is in milliseconds
  393. * @li if it's set to the input sample rate then the returned
  394. * delay is in input samples
  395. * @li if it's set to the output sample rate then the returned
  396. * delay is in output samples
  397. * @li if it's the least common multiple of in_sample_rate and
  398. * out_sample_rate then an exact rounding-free delay will be
  399. * returned
  400. * @returns the delay in 1 / @c base units.
  401. */
  402. int64_t swr_get_delay(struct SwrContext *s, int64_t base);
  403. /**
  404. * Find an upper bound on the number of samples that the next swr_convert
  405. * call will output, if called with in_samples of input samples. This
  406. * depends on the internal state, and anything changing the internal state
  407. * (like further swr_convert() calls) will may change the number of samples
  408. * swr_get_out_samples() returns for the same number of input samples.
  409. *
  410. * @param in_samples number of input samples.
  411. * @note any call to swr_inject_silence(), swr_convert(), swr_next_pts()
  412. * or swr_set_compensation() invalidates this limit
  413. * @note it is recommended to pass the correct available buffer size
  414. * to all functions like swr_convert() even if swr_get_out_samples()
  415. * indicates that less would be used.
  416. * @returns an upper bound on the number of samples that the next swr_convert
  417. * will output or a negative value to indicate an error
  418. */
  419. int swr_get_out_samples(struct SwrContext *s, int in_samples);
  420. /**
  421. * @}
  422. *
  423. * @name Configuration accessors
  424. * @{
  425. */
  426. /**
  427. * Return the @ref LIBSWRESAMPLE_VERSION_INT constant.
  428. *
  429. * This is useful to check if the build-time libswresample has the same version
  430. * as the run-time one.
  431. *
  432. * @returns the unsigned int-typed version
  433. */
  434. unsigned swresample_version(void);
  435. /**
  436. * Return the swr build-time configuration.
  437. *
  438. * @returns the build-time @c ./configure flags
  439. */
  440. const char *swresample_configuration(void);
  441. /**
  442. * Return the swr license.
  443. *
  444. * @returns the license of libswresample, determined at build-time
  445. */
  446. const char *swresample_license(void);
  447. /**
  448. * @}
  449. *
  450. * @name AVFrame based API
  451. * @{
  452. */
  453. /**
  454. * Convert the samples in the input AVFrame and write them to the output AVFrame.
  455. *
  456. * Input and output AVFrames must have channel_layout, sample_rate and format set.
  457. *
  458. * If the output AVFrame does not have the data pointers allocated the nb_samples
  459. * field will be set using av_frame_get_buffer()
  460. * is called to allocate the frame.
  461. *
  462. * The output AVFrame can be NULL or have fewer allocated samples than required.
  463. * In this case, any remaining samples not written to the output will be added
  464. * to an internal FIFO buffer, to be returned at the next call to this function
  465. * or to swr_convert().
  466. *
  467. * If converting sample rate, there may be data remaining in the internal
  468. * resampling delay buffer. swr_get_delay() tells the number of
  469. * remaining samples. To get this data as output, call this function or
  470. * swr_convert() with NULL input.
  471. *
  472. * If the SwrContext configuration does not match the output and
  473. * input AVFrame settings the conversion does not take place and depending on
  474. * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED
  475. * or the result of a bitwise-OR of them is returned.
  476. *
  477. * @see swr_delay()
  478. * @see swr_convert()
  479. * @see swr_get_delay()
  480. *
  481. * @param swr audio resample context
  482. * @param output output AVFrame
  483. * @param input input AVFrame
  484. * @return 0 on success, AVERROR on failure or nonmatching
  485. * configuration.
  486. */
  487. int swr_convert_frame(SwrContext *swr,
  488. AVFrame *output, const AVFrame *input);
  489. /**
  490. * Configure or reconfigure the SwrContext using the information
  491. * provided by the AVFrames.
  492. *
  493. * The original resampling context is reset even on failure.
  494. * The function calls swr_close() internally if the context is open.
  495. *
  496. * @see swr_close();
  497. *
  498. * @param swr audio resample context
  499. * @param output output AVFrame
  500. * @param input input AVFrame
  501. * @return 0 on success, AVERROR on failure.
  502. */
  503. int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);
  504. /**
  505. * @}
  506. * @}
  507. */
  508. #endif /* SWRESAMPLE_SWRESAMPLE_H */