2 * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at)
4 * This file is part of libswresample
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.
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.
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
21 #ifndef SWRESAMPLE_SWRESAMPLE_H
22 #define SWRESAMPLE_SWRESAMPLE_H
27 * libswresample public header
31 * @defgroup lswr libswresample
34 * Audio resampling, sample format conversion and mixing library.
36 * Interaction with lswr is done through SwrContext, which is
37 * allocated with swr_alloc() or swr_alloc_set_opts(). It is opaque, so all parameters
38 * must be set with the @ref avoptions API.
40 * The first thing you will need to do in order to use lswr is to allocate
41 * SwrContext. This can be done with swr_alloc() or swr_alloc_set_opts(). If you
42 * are using the former, you must set options through the @ref avoptions API.
43 * The latter function provides the same feature, but it allows you to set some
44 * common options in the same statement.
46 * For example the following code will setup conversion from planar float sample
47 * format to interleaved signed 16-bit integer, downsampling from 48kHz to
48 * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
49 * matrix). This is using the swr_alloc() function.
51 * SwrContext *swr = swr_alloc();
52 * av_opt_set_channel_layout(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);
53 * av_opt_set_channel_layout(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
54 * av_opt_set_int(swr, "in_sample_rate", 48000, 0);
55 * av_opt_set_int(swr, "out_sample_rate", 44100, 0);
56 * av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
57 * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
60 * The same job can be done using swr_alloc_set_opts() as well:
62 * SwrContext *swr = swr_alloc_set_opts(NULL, // we're allocating a new context
63 * AV_CH_LAYOUT_STEREO, // out_ch_layout
64 * AV_SAMPLE_FMT_S16, // out_sample_fmt
65 * 44100, // out_sample_rate
66 * AV_CH_LAYOUT_5POINT1, // in_ch_layout
67 * AV_SAMPLE_FMT_FLTP, // in_sample_fmt
68 * 48000, // in_sample_rate
73 * Once all values have been set, it must be initialized with swr_init(). If
74 * you need to change the conversion parameters, you can change the parameters
75 * using @ref AVOptions, as described above in the first example; or by using
76 * swr_alloc_set_opts(), but with the first argument the allocated context.
77 * You must then call swr_init() again.
79 * The conversion itself is done by repeatedly calling swr_convert().
80 * Note that the samples may get buffered in swr if you provide insufficient
81 * output space or if sample rate conversion is done, which requires "future"
82 * samples. Samples that do not require future input can be retrieved at any
83 * time by using swr_convert() (in_count can be set to 0).
84 * At the end of conversion the resampling buffer can be flushed by calling
85 * swr_convert() with NULL in and 0 in_count.
87 * The samples used in the conversion process can be managed with the libavutil
88 * @ref lavu_sampmanip "samples manipulation" API, including av_samples_alloc()
89 * function used in the following example.
91 * The delay between input and output, can at any time be found by using
94 * The following code demonstrates the conversion loop assuming the parameters
95 * from above and caller-defined functions get_input() and handle_output():
100 * while (get_input(&input, &in_samples)) {
102 * int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +
103 * in_samples, 44100, 48000, AV_ROUND_UP);
104 * av_samples_alloc(&output, NULL, 2, out_samples,
105 * AV_SAMPLE_FMT_S16, 0);
106 * out_samples = swr_convert(swr, &output, out_samples,
107 * input, in_samples);
108 * handle_output(output, out_samples);
113 * When the conversion is finished, the conversion
114 * context and everything associated with it must be freed with swr_free().
115 * A swr_close() function is also available, but it exists mainly for
116 * compatibility with libavresample, and is not required to be called.
118 * There will be no memory leak if the data is not completely flushed before
123 #include "libavutil/channel_layout.h"
124 #include "libavutil/frame.h"
125 #include "libavutil/samplefmt.h"
127 #include "libswresample/version.h"
130 * @name Option constants
131 * These constants are used for the @ref avoptions interface for lswr.
136 #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
137 //TODO use int resample ?
138 //long term TODO can we enable this dynamically?
140 /** Dithering algorithms */
143 SWR_DITHER_RECTANGULAR,
144 SWR_DITHER_TRIANGULAR,
145 SWR_DITHER_TRIANGULAR_HIGHPASS,
147 SWR_DITHER_NS = 64, ///< not part of API/ABI
148 SWR_DITHER_NS_LIPSHITZ,
149 SWR_DITHER_NS_F_WEIGHTED,
150 SWR_DITHER_NS_MODIFIED_E_WEIGHTED,
151 SWR_DITHER_NS_IMPROVED_E_WEIGHTED,
152 SWR_DITHER_NS_SHIBATA,
153 SWR_DITHER_NS_LOW_SHIBATA,
154 SWR_DITHER_NS_HIGH_SHIBATA,
155 SWR_DITHER_NB, ///< not part of API/ABI
158 /** Resampling Engines */
160 SWR_ENGINE_SWR, /**< SW Resampler */
161 SWR_ENGINE_SOXR, /**< SoX Resampler */
162 SWR_ENGINE_NB, ///< not part of API/ABI
165 /** Resampling Filter Types */
167 SWR_FILTER_TYPE_CUBIC, /**< Cubic */
168 SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall windowed sinc */
169 SWR_FILTER_TYPE_KAISER, /**< Kaiser windowed sinc */
177 * The libswresample context. Unlike libavcodec and libavformat, this structure
178 * is opaque. This means that if you would like to set options, you must use
179 * the @ref avoptions API and cannot directly set values to members of the
182 typedef struct SwrContext SwrContext;
185 * Get the AVClass for SwrContext. It can be used in combination with
186 * AV_OPT_SEARCH_FAKE_OBJ for examining options.
188 * @see av_opt_find().
189 * @return the AVClass of SwrContext
191 const AVClass *swr_get_class(void);
194 * @name SwrContext constructor functions
199 * Allocate SwrContext.
201 * If you use this function you will need to set the parameters (manually or
202 * with swr_alloc_set_opts()) before calling swr_init().
204 * @see swr_alloc_set_opts(), swr_init(), swr_free()
205 * @return NULL on error, allocated context otherwise
207 struct SwrContext *swr_alloc(void);
210 * Initialize context after user parameters have been set.
211 * @note The context must be configured using the AVOption API.
213 * @see av_opt_set_int()
214 * @see av_opt_set_dict()
216 * @param[in,out] s Swr context to initialize
217 * @return AVERROR error code in case of failure.
219 int swr_init(struct SwrContext *s);
222 * Check whether an swr context has been initialized or not.
224 * @param[in] s Swr context to check
226 * @return positive if it has been initialized, 0 if not initialized
228 int swr_is_initialized(struct SwrContext *s);
231 * Allocate SwrContext if needed and set/reset common parameters.
233 * This function does not require s to be allocated with swr_alloc(). On the
234 * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
235 * on the allocated context.
237 * @param s existing Swr context if available, or NULL if not
238 * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
239 * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
240 * @param out_sample_rate output sample rate (frequency in Hz)
241 * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
242 * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
243 * @param in_sample_rate input sample rate (frequency in Hz)
244 * @param log_offset logging level offset
245 * @param log_ctx parent logging context, can be NULL
247 * @see swr_init(), swr_free()
248 * @return NULL on error, allocated context otherwise
250 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
251 int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
252 int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
253 int log_offset, void *log_ctx);
258 * @name SwrContext destructor functions
263 * Free the given SwrContext and set the pointer to NULL.
265 * @param[in] s a pointer to a pointer to Swr context
267 void swr_free(struct SwrContext **s);
270 * Closes the context so that swr_is_initialized() returns 0.
272 * The context can be brought back to life by running swr_init(),
273 * swr_init() can also be used without swr_close().
274 * This function is mainly provided for simplifying the usecase
275 * where one tries to support libavresample and libswresample.
277 * @param[in,out] s Swr context to be closed
279 void swr_close(struct SwrContext *s);
284 * @name Core conversion functions
290 * in and in_count can be set to 0 to flush the last few samples out at the
293 * If more input is provided than output space, then the input will be buffered.
294 * You can avoid this buffering by using swr_get_out_samples() to retrieve an
295 * upper bound on the required number of output samples for the given number of
296 * input samples. Conversion will run directly without copying whenever possible.
298 * @param s allocated Swr context, with parameters set
299 * @param out output buffers, only the first one need be set in case of packed audio
300 * @param out_count amount of space available for output in samples per channel
301 * @param in input buffers, only the first one need to be set in case of packed audio
302 * @param in_count number of input samples available in one channel
304 * @return number of samples output per channel, negative value on error
306 int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
307 const uint8_t **in , int in_count);
310 * Convert the next timestamp from input to output
311 * timestamps are in 1/(in_sample_rate * out_sample_rate) units.
313 * @note There are 2 slightly differently behaving modes.
314 * @li When automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
315 * in this case timestamps will be passed through with delays compensated
316 * @li When automatic timestamp compensation is used, (min_compensation < FLT_MAX)
317 * in this case the output timestamps will match output sample numbers.
318 * See ffmpeg-resampler(1) for the two modes of compensation.
320 * @param s[in] initialized Swr context
321 * @param pts[in] timestamp for the next input sample, INT64_MIN if unknown
322 * @see swr_set_compensation(), swr_drop_output(), and swr_inject_silence() are
323 * function used internally for timestamp compensation.
324 * @return the output timestamp for the next output sample
326 int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
331 * @name Low-level option setting functions
332 * These functons provide a means to set low-level options that is not possible
333 * with the AVOption API.
338 * Activate resampling compensation ("soft" compensation). This function is
339 * internally called when needed in swr_next_pts().
341 * @param[in,out] s allocated Swr context. If it is not initialized,
342 * or SWR_FLAG_RESAMPLE is not set, swr_init() is
343 * called with the flag set.
344 * @param[in] sample_delta delta in PTS per sample
345 * @param[in] compensation_distance number of samples to compensate for
346 * @return >= 0 on success, AVERROR error codes if:
348 * @li @c compensation_distance is less than 0,
349 * @li @c compensation_distance is 0 but sample_delta is not,
350 * @li compensation unsupported by resampler, or
351 * @li swr_init() fails when called.
353 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
356 * Set a customized input channel mapping.
358 * @param[in,out] s allocated Swr context, not yet initialized
359 * @param[in] channel_map customized input channel mapping (array of channel
360 * indexes, -1 for a muted channel)
361 * @return >= 0 on success, or AVERROR error code in case of failure.
363 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
366 * Generate a channel mixing matrix.
368 * This function is the one used internally by libswresample for building the
369 * default mixing matrix. It is made public just as a utility function for
370 * building custom matrices.
372 * @param in_layout input channel layout
373 * @param out_layout output channel layout
374 * @param center_mix_level mix level for the center channel
375 * @param surround_mix_level mix level for the surround channel(s)
376 * @param lfe_mix_level mix level for the low-frequency effects channel
377 * @param rematrix_maxval if 1.0, coefficients will be normalized to prevent
378 * overflow. if INT_MAX, coefficients will not be
380 * @param[out] matrix mixing coefficients; matrix[i + stride * o] is
381 * the weight of input channel i in output channel o.
382 * @param stride distance between adjacent input channels in the
384 * @param matrix_encoding matrixed stereo downmix mode (e.g. dplii)
385 * @param log_ctx parent logging context, can be NULL
386 * @return 0 on success, negative AVERROR code on failure
388 int swr_build_matrix(uint64_t in_layout, uint64_t out_layout,
389 double center_mix_level, double surround_mix_level,
390 double lfe_mix_level, double rematrix_maxval,
391 double rematrix_volume, double *matrix,
392 int stride, enum AVMatrixEncoding matrix_encoding,
396 * Set a customized remix matrix.
398 * @param s allocated Swr context, not yet initialized
399 * @param matrix remix coefficients; matrix[i + stride * o] is
400 * the weight of input channel i in output channel o
401 * @param stride offset between lines of the matrix
402 * @return >= 0 on success, or AVERROR error code in case of failure.
404 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
409 * @name Sample handling functions
414 * Drops the specified number of output samples.
416 * This function, along with swr_inject_silence(), is called by swr_next_pts()
417 * if needed for "hard" compensation.
419 * @param s allocated Swr context
420 * @param count number of samples to be dropped
422 * @return >= 0 on success, or a negative AVERROR code on failure
424 int swr_drop_output(struct SwrContext *s, int count);
427 * Injects the specified number of silence samples.
429 * This function, along with swr_drop_output(), is called by swr_next_pts()
430 * if needed for "hard" compensation.
432 * @param s allocated Swr context
433 * @param count number of samples to be dropped
435 * @return >= 0 on success, or a negative AVERROR code on failure
437 int swr_inject_silence(struct SwrContext *s, int count);
440 * Gets the delay the next input sample will experience relative to the next output sample.
442 * Swresample can buffer data if more input has been provided than available
443 * output space, also converting between sample rates needs a delay.
444 * This function returns the sum of all such delays.
445 * The exact delay is not necessarily an integer value in either input or
446 * output sample rate. Especially when downsampling by a large value, the
447 * output sample rate may be a poor choice to represent the delay, similarly
448 * for upsampling and the input sample rate.
450 * @param s swr context
451 * @param base timebase in which the returned delay will be:
452 * @li if it's set to 1 the returned delay is in seconds
453 * @li if it's set to 1000 the returned delay is in milliseconds
454 * @li if it's set to the input sample rate then the returned
455 * delay is in input samples
456 * @li if it's set to the output sample rate then the returned
457 * delay is in output samples
458 * @li if it's the least common multiple of in_sample_rate and
459 * out_sample_rate then an exact rounding-free delay will be
461 * @returns the delay in 1 / @c base units.
463 int64_t swr_get_delay(struct SwrContext *s, int64_t base);
466 * Find an upper bound on the number of samples that the next swr_convert
467 * call will output, if called with in_samples of input samples. This
468 * depends on the internal state, and anything changing the internal state
469 * (like further swr_convert() calls) will may change the number of samples
470 * swr_get_out_samples() returns for the same number of input samples.
472 * @param in_samples number of input samples.
473 * @note any call to swr_inject_silence(), swr_convert(), swr_next_pts()
474 * or swr_set_compensation() invalidates this limit
475 * @note it is recommended to pass the correct available buffer size
476 * to all functions like swr_convert() even if swr_get_out_samples()
477 * indicates that less would be used.
478 * @returns an upper bound on the number of samples that the next swr_convert
479 * will output or a negative value to indicate an error
481 int swr_get_out_samples(struct SwrContext *s, int in_samples);
486 * @name Configuration accessors
491 * Return the @ref LIBSWRESAMPLE_VERSION_INT constant.
493 * This is useful to check if the build-time libswresample has the same version
494 * as the run-time one.
496 * @returns the unsigned int-typed version
498 unsigned swresample_version(void);
501 * Return the swr build-time configuration.
503 * @returns the build-time @c ./configure flags
505 const char *swresample_configuration(void);
508 * Return the swr license.
510 * @returns the license of libswresample, determined at build-time
512 const char *swresample_license(void);
517 * @name AVFrame based API
522 * Convert the samples in the input AVFrame and write them to the output AVFrame.
524 * Input and output AVFrames must have channel_layout, sample_rate and format set.
526 * If the output AVFrame does not have the data pointers allocated the nb_samples
527 * field will be set using av_frame_get_buffer()
528 * is called to allocate the frame.
530 * The output AVFrame can be NULL or have fewer allocated samples than required.
531 * In this case, any remaining samples not written to the output will be added
532 * to an internal FIFO buffer, to be returned at the next call to this function
533 * or to swr_convert().
535 * If converting sample rate, there may be data remaining in the internal
536 * resampling delay buffer. swr_get_delay() tells the number of
537 * remaining samples. To get this data as output, call this function or
538 * swr_convert() with NULL input.
540 * If the SwrContext configuration does not match the output and
541 * input AVFrame settings the conversion does not take place and depending on
542 * which AVFrame is not matching AVERROR_OUTPUT_CHANGED, AVERROR_INPUT_CHANGED
543 * or the result of a bitwise-OR of them is returned.
547 * @see swr_get_delay()
549 * @param swr audio resample context
550 * @param output output AVFrame
551 * @param input input AVFrame
552 * @return 0 on success, AVERROR on failure or nonmatching
555 int swr_convert_frame(SwrContext *swr,
556 AVFrame *output, const AVFrame *input);
559 * Configure or reconfigure the SwrContext using the information
560 * provided by the AVFrames.
562 * The original resampling context is reset even on failure.
563 * The function calls swr_close() internally if the context is open.
567 * @param swr audio resample context
568 * @param output output AVFrame
569 * @param input input AVFrame
570 * @return 0 on success, AVERROR on failure.
572 int swr_config_frame(SwrContext *swr, const AVFrame *out, const AVFrame *in);
579 #endif /* SWRESAMPLE_SWRESAMPLE_H */