swresample.h
Go to the documentation of this file.
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 
21 #ifndef SWRESAMPLE_SWRESAMPLE_H
22 #define SWRESAMPLE_SWRESAMPLE_H
23 
24 /**
25  * @file
26  * @ingroup lswr
27  * libswresample public header
28  */
29 
30 /**
31  * @defgroup lswr Libswresample
32  * @{
33  *
34  * Libswresample (lswr) is a library that handles audio resampling, sample
35  * format conversion and mixing.
36  *
37  * Interaction with lswr is done through SwrContext, which is
38  * allocated with swr_alloc() or swr_alloc_set_opts(). It is opaque, so all parameters
39  * must be set with the @ref avoptions API.
40  *
41  * For example the following code will setup conversion from planar float sample
42  * format to interleaved signed 16-bit integer, downsampling from 48kHz to
43  * 44.1kHz and downmixing from 5.1 channels to stereo (using the default mixing
44  * matrix):
45  * @code
46  * SwrContext *swr = swr_alloc();
47  * av_opt_set_int(swr, "in_channel_layout", AV_CH_LAYOUT_5POINT1, 0);
48  * av_opt_set_int(swr, "out_channel_layout", AV_CH_LAYOUT_STEREO, 0);
49  * av_opt_set_int(swr, "in_sample_rate", 48000, 0);
50  * av_opt_set_int(swr, "out_sample_rate", 44100, 0);
51  * av_opt_set_sample_fmt(swr, "in_sample_fmt", AV_SAMPLE_FMT_FLTP, 0);
52  * av_opt_set_sample_fmt(swr, "out_sample_fmt", AV_SAMPLE_FMT_S16, 0);
53  * @endcode
54  *
55  * Once all values have been set, it must be initialized with swr_init(). If
56  * you need to change the conversion parameters, you can change the parameters
57  * as described above, or by using swr_alloc_set_opts(), then call swr_init()
58  * again.
59  *
60  * The conversion itself is done by repeatedly calling swr_convert().
61  * Note that the samples may get buffered in swr if you provide insufficient
62  * output space or if sample rate conversion is done, which requires "future"
63  * samples. Samples that do not require future input can be retrieved at any
64  * time by using swr_convert() (in_count can be set to 0).
65  * At the end of conversion the resampling buffer can be flushed by calling
66  * swr_convert() with NULL in and 0 in_count.
67  *
68  * The delay between input and output, can at any time be found by using
69  * swr_get_delay().
70  *
71  * The following code demonstrates the conversion loop assuming the parameters
72  * from above and caller-defined functions get_input() and handle_output():
73  * @code
74  * uint8_t **input;
75  * int in_samples;
76  *
77  * while (get_input(&input, &in_samples)) {
78  * uint8_t *output;
79  * int out_samples = av_rescale_rnd(swr_get_delay(swr, 48000) +
80  * in_samples, 44100, 48000, AV_ROUND_UP);
81  * av_samples_alloc(&output, NULL, 2, out_samples,
82  * AV_SAMPLE_FMT_S16, 0);
83  * out_samples = swr_convert(swr, &output, out_samples,
84  * input, in_samples);
85  * handle_output(output, out_samples);
86  * av_freep(&output);
87  * }
88  * @endcode
89  *
90  * When the conversion is finished, the conversion
91  * context and everything associated with it must be freed with swr_free().
92  * There will be no memory leak if the data is not completely flushed before
93  * swr_free().
94  */
95 
96 #include <stdint.h>
97 #include "libavutil/samplefmt.h"
98 
99 #include "libswresample/version.h"
100 
101 #if LIBSWRESAMPLE_VERSION_MAJOR < 1
102 #define SWR_CH_MAX 32 ///< Maximum number of channels
103 #endif
104 
105 #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
106 //TODO use int resample ?
107 //long term TODO can we enable this dynamically?
108 
114 
115  SWR_DITHER_NS = 64, ///< not part of API/ABI
123  SWR_DITHER_NB, ///< not part of API/ABI
124 };
125 
126 /** Resampling Engines */
127 enum SwrEngine {
128  SWR_ENGINE_SWR, /**< SW Resampler */
129  SWR_ENGINE_SOXR, /**< SoX Resampler */
130  SWR_ENGINE_NB, ///< not part of API/ABI
131 };
132 
133 /** Resampling Filter Types */
135  SWR_FILTER_TYPE_CUBIC, /**< Cubic */
136  SWR_FILTER_TYPE_BLACKMAN_NUTTALL, /**< Blackman Nuttall Windowed Sinc */
137  SWR_FILTER_TYPE_KAISER, /**< Kaiser Windowed Sinc */
138 };
139 
140 typedef struct SwrContext SwrContext;
141 
142 /**
143  * Get the AVClass for swrContext. It can be used in combination with
144  * AV_OPT_SEARCH_FAKE_OBJ for examining options.
145  *
146  * @see av_opt_find().
147  */
148 const AVClass *swr_get_class(void);
149 
150 /**
151  * Allocate SwrContext.
152  *
153  * If you use this function you will need to set the parameters (manually or
154  * with swr_alloc_set_opts()) before calling swr_init().
155  *
156  * @see swr_alloc_set_opts(), swr_init(), swr_free()
157  * @return NULL on error, allocated context otherwise
158  */
159 struct SwrContext *swr_alloc(void);
160 
161 /**
162  * Initialize context after user parameters have been set.
163  *
164  * @return AVERROR error code in case of failure.
165  */
166 int swr_init(struct SwrContext *s);
167 
168 /**
169  * Allocate SwrContext if needed and set/reset common parameters.
170  *
171  * This function does not require s to be allocated with swr_alloc(). On the
172  * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
173  * on the allocated context.
174  *
175  * @param s Swr context, can be NULL
176  * @param out_ch_layout output channel layout (AV_CH_LAYOUT_*)
177  * @param out_sample_fmt output sample format (AV_SAMPLE_FMT_*).
178  * @param out_sample_rate output sample rate (frequency in Hz)
179  * @param in_ch_layout input channel layout (AV_CH_LAYOUT_*)
180  * @param in_sample_fmt input sample format (AV_SAMPLE_FMT_*).
181  * @param in_sample_rate input sample rate (frequency in Hz)
182  * @param log_offset logging level offset
183  * @param log_ctx parent logging context, can be NULL
184  *
185  * @see swr_init(), swr_free()
186  * @return NULL on error, allocated context otherwise
187  */
188 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
191  int log_offset, void *log_ctx);
192 
193 /**
194  * Free the given SwrContext and set the pointer to NULL.
195  */
196 void swr_free(struct SwrContext **s);
197 
198 /**
199  * Convert audio.
200  *
201  * in and in_count can be set to 0 to flush the last few samples out at the
202  * end.
203  *
204  * If more input is provided than output space then the input will be buffered.
205  * You can avoid this buffering by providing more output space than input.
206  * Convertion will run directly without copying whenever possible.
207  *
208  * @param s allocated Swr context, with parameters set
209  * @param out output buffers, only the first one need be set in case of packed audio
210  * @param out_count amount of space available for output in samples per channel
211  * @param in input buffers, only the first one need to be set in case of packed audio
212  * @param in_count number of input samples available in one channel
213  *
214  * @return number of samples output per channel, negative value on error
215  */
216 int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
217  const uint8_t **in , int in_count);
218 
219 /**
220  * Convert the next timestamp from input to output
221  * timestamps are in 1/(in_sample_rate * out_sample_rate) units.
222  *
223  * @note There are 2 slightly differently behaving modes.
224  * First is when automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
225  * in this case timestamps will be passed through with delays compensated
226  * Second is when automatic timestamp compensation is used, (min_compensation < FLT_MAX)
227  * in this case the output timestamps will match output sample numbers
228  *
229  * @param pts timestamp for the next input sample, INT64_MIN if unknown
230  * @return the output timestamp for the next output sample
231  */
232 int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
233 
234 /**
235  * Activate resampling compensation.
236  */
237 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
238 
239 /**
240  * Set a customized input channel mapping.
241  *
242  * @param s allocated Swr context, not yet initialized
243  * @param channel_map customized input channel mapping (array of channel
244  * indexes, -1 for a muted channel)
245  * @return AVERROR error code in case of failure.
246  */
247 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
248 
249 /**
250  * Set a customized remix matrix.
251  *
252  * @param s allocated Swr context, not yet initialized
253  * @param matrix remix coefficients; matrix[i + stride * o] is
254  * the weight of input channel i in output channel o
255  * @param stride offset between lines of the matrix
256  * @return AVERROR error code in case of failure.
257  */
258 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
259 
260 /**
261  * Drops the specified number of output samples.
262  */
263 int swr_drop_output(struct SwrContext *s, int count);
264 
265 /**
266  * Injects the specified number of silence samples.
267  */
268 int swr_inject_silence(struct SwrContext *s, int count);
269 
270 /**
271  * Gets the delay the next input sample will experience relative to the next output sample.
272  *
273  * Swresample can buffer data if more input has been provided than available
274  * output space, also converting between sample rates needs a delay.
275  * This function returns the sum of all such delays.
276  * The exact delay is not necessarily an integer value in either input or
277  * output sample rate. Especially when downsampling by a large value, the
278  * output sample rate may be a poor choice to represent the delay, similarly
279  * for upsampling and the input sample rate.
280  *
281  * @param s swr context
282  * @param base timebase in which the returned delay will be
283  * if its set to 1 the returned delay is in seconds
284  * if its set to 1000 the returned delay is in milli seconds
285  * if its set to the input sample rate then the returned delay is in input samples
286  * if its set to the output sample rate then the returned delay is in output samples
287  * an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate)
288  * @returns the delay in 1/base units.
289  */
290 int64_t swr_get_delay(struct SwrContext *s, int64_t base);
291 
292 /**
293  * Return the LIBSWRESAMPLE_VERSION_INT constant.
294  */
295 unsigned swresample_version(void);
296 
297 /**
298  * Return the swr build-time configuration.
299  */
300 const char *swresample_configuration(void);
301 
302 /**
303  * Return the swr license.
304  */
305 const char *swresample_license(void);
306 
307 /**
308  * @}
309  */
310 
311 #endif /* SWRESAMPLE_SWRESAMPLE_H */
const char * s
Definition: avisynth_c.h:668
int out_sample_rate
output sample rate
SoX Resampler.
Definition: swresample.h:129
SwrFilterType
Resampling Filter Types.
Definition: swresample.h:134
About Git write you should know how to use GIT properly Luckily Git comes with excellent documentation git help man git shows you the available git< command > help man git< command > shows information about the subcommand< command > The most comprehensive manual is the website Git Reference visit they are quite exhaustive You do not need a special username or password All you need is to provide a ssh public key to the Git server admin What follows now is a basic introduction to Git and some FFmpeg specific guidelines Read it at least if you are granted commit privileges to the FFmpeg project you are expected to be familiar with these rules I if not You can get git from etc no matter how small Every one of them has been saved from looking like a fool by this many times It s very easy for stray debug output or cosmetic modifications to slip in
Definition: git-howto.txt:5
int64_t swr_next_pts(struct SwrContext *s, int64_t pts)
Convert the next timestamp from input to output timestamps are in 1/(in_sample_rate * out_sample_rate...
Definition: swresample.c:900
int stride
Definition: mace.c:144
const int * channel_map
channel index (or -1 if muted channel) map
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance)
Activate resampling compensation.
Definition: swresample.c:880
uint8_t
struct SwrContext * swr_alloc(void)
Allocate SwrContext.
Definition: swresample.c:177
SwrDitherType
Definition: swresample.h:109
void * log_ctx
parent logging context
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count, const uint8_t **in, int in_count)
Convert audio.
Kaiser Windowed Sinc.
Definition: swresample.h:137
enum AVSampleFormat out_sample_fmt
output sample format
SwrEngine
Resampling Engines.
Definition: swresample.h:127
Blackman Nuttall Windowed Sinc.
Definition: swresample.h:136
int64_t swr_get_delay(struct SwrContext *s, int64_t base)
Gets the delay the next input sample will experience relative to the next output sample.
Definition: swresample.c:872
not part of API/ABI
Definition: swresample.h:130
not part of API/ABI
Definition: swresample.h:123
int swr_drop_output(struct SwrContext *s, int count)
Drops the specified number of output samples.
Definition: swresample.c:834
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
Set a customized remix matrix.
Definition: rematrix.c:56
int64_t out_ch_layout
output channel layout
struct SwrContext * swr_alloc_set_opts(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate, int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate, int log_offset, void *log_ctx)
Allocate SwrContext if needed and set/reset common parameters.
Definition: swresample.c:186
not part of API/ABI
Definition: swresample.h:115
int in_sample_rate
input sample rate
const AVClass * swr_get_class(void)
Get the AVClass for swrContext.
Definition: swresample.c:172
void swr_free(struct SwrContext **s)
Free the given SwrContext and set the pointer to NULL.
Definition: swresample.c:220
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map)
Set a customized input channel mapping.
Definition: swresample.c:165
Describe the class of an AVClass context structure.
Definition: log.h:50
const char * swresample_license(void)
Return the swr license.
Definition: swresample.c:159
enum AVSampleFormat in_sample_fmt
input sample format
Libswresample version macros.
SW Resampler.
Definition: swresample.h:128
int64_t in_ch_layout
input channel layout
AVSampleFormat
Audio Sample Formats.
Definition: samplefmt.h:49
unsigned swresample_version(void)
Return the LIBSWRESAMPLE_VERSION_INT constant.
Definition: swresample.c:148
float matrix[SWR_CH_MAX][SWR_CH_MAX]
floating point rematrixing coefficients
void INT64 INT64 count
Definition: avisynth_c.h:594
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(const int16_t *) pi >> 8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(const int32_t *) pi >> 24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31))))#define SET_CONV_FUNC_GROUP(ofmt, ifmt) static void set_generic_function(AudioConvert *ac){}void ff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map){AudioConvert *ac;int in_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) return NULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt) > 2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);return NULL;}return ac;}in_planar=av_sample_fmt_is_planar(in_fmt);out_planar=av_sample_fmt_is_planar(out_fmt);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}else if(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;else ac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);return ac;}int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){int use_generic=1;int len=in->nb_samples;int p;if(ac->dc){av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));return ff_convert_dither(ac-> out
const char * swresample_configuration(void)
Return the swr build-time configuration.
Definition: swresample.c:154
int swr_inject_silence(struct SwrContext *s, int count)
Injects the specified number of silence samples.
Definition: swresample.c:844
int swr_init(struct SwrContext *s)
Initialize context after user parameters have been set.
Definition: swresample.c:242