]> git.sesse.net Git - ffmpeg/blob - libswresample/swresample.h
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswresample / swresample.h
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
21 /**
22  * @file
23  * libswresample public header
24  */
25
26 #ifndef SWRESAMPLE_SWRESAMPLE_H
27 #define SWRESAMPLE_SWRESAMPLE_H
28
29 #include <inttypes.h>
30 #include "libavutil/samplefmt.h"
31
32 #include "libswresample/version.h"
33
34 #if LIBSWRESAMPLE_VERSION_MAJOR < 1
35 #define SWR_CH_MAX 32   ///< Maximum number of channels
36 #endif
37
38 #define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
39 //TODO use int resample ?
40 //long term TODO can we enable this dynamically?
41
42 enum SwrDitherType {
43     SWR_DITHER_NONE = 0,
44     SWR_DITHER_RECTANGULAR,
45     SWR_DITHER_TRIANGULAR,
46     SWR_DITHER_TRIANGULAR_HIGHPASS,
47     SWR_DITHER_NB,              ///< not part of API/ABI
48 };
49
50 /** Resampling Filter Types */
51 enum SwrFilterType {
52     SWR_FILTER_TYPE_CUBIC,              /**< Cubic */
53     SWR_FILTER_TYPE_BLACKMAN_NUTTALL,   /**< Blackman Nuttall Windowed Sinc */
54     SWR_FILTER_TYPE_KAISER,             /**< Kaiser Windowed Sinc */
55 };
56
57 typedef struct SwrContext SwrContext;
58
59 /**
60  * Get the AVClass for swrContext. It can be used in combination with
61  * AV_OPT_SEARCH_FAKE_OBJ for examining options.
62  *
63  * @see av_opt_find().
64  */
65 const AVClass *swr_get_class(void);
66
67 /**
68  * Allocate SwrContext.
69  *
70  * If you use this function you will need to set the parameters (manually or
71  * with swr_alloc_set_opts()) before calling swr_init().
72  *
73  * @see swr_alloc_set_opts(), swr_init(), swr_free()
74  * @return NULL on error, allocated context otherwise
75  */
76 struct SwrContext *swr_alloc(void);
77
78 /**
79  * Initialize context after user parameters have been set.
80  *
81  * @return AVERROR error code in case of failure.
82  */
83 int swr_init(struct SwrContext *s);
84
85 /**
86  * Allocate SwrContext if needed and set/reset common parameters.
87  *
88  * This function does not require s to be allocated with swr_alloc(). On the
89  * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
90  * on the allocated context.
91  *
92  * @param s               Swr context, can be NULL
93  * @param out_ch_layout   output channel layout (AV_CH_LAYOUT_*)
94  * @param out_sample_fmt  output sample format (AV_SAMPLE_FMT_*).
95  * @param out_sample_rate output sample rate (frequency in Hz)
96  * @param in_ch_layout    input channel layout (AV_CH_LAYOUT_*)
97  * @param in_sample_fmt   input sample format (AV_SAMPLE_FMT_*).
98  * @param in_sample_rate  input sample rate (frequency in Hz)
99  * @param log_offset      logging level offset
100  * @param log_ctx         parent logging context, can be NULL
101  *
102  * @see swr_init(), swr_free()
103  * @return NULL on error, allocated context otherwise
104  */
105 struct SwrContext *swr_alloc_set_opts(struct SwrContext *s,
106                                       int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
107                                       int64_t  in_ch_layout, enum AVSampleFormat  in_sample_fmt, int  in_sample_rate,
108                                       int log_offset, void *log_ctx);
109
110 /**
111  * Free the given SwrContext and set the pointer to NULL.
112  */
113 void swr_free(struct SwrContext **s);
114
115 /**
116  * Convert audio.
117  *
118  * in and in_count can be set to 0 to flush the last few samples out at the
119  * end.
120  *
121  * If more input is provided than output space then the input will be buffered.
122  * You can avoid this buffering by providing more output space than input.
123  * Convertion will run directly without copying whenever possible.
124  *
125  * @param s         allocated Swr context, with parameters set
126  * @param out       output buffers, only the first one need be set in case of packed audio
127  * @param out_count amount of space available for output in samples per channel
128  * @param in        input buffers, only the first one need to be set in case of packed audio
129  * @param in_count  number of input samples available in one channel
130  *
131  * @return number of samples output per channel, negative value on error
132  */
133 int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
134                                 const uint8_t **in , int in_count);
135
136 /**
137  * Convert the next timestamp from input to output
138  * timestampe are in 1/(in_sample_rate * out_sample_rate) units.
139  *
140  * @note There are 2 slightly differently behaving modes.
141  *       First is when automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
142  *              in this case timestamps will be passed through with delays compensated
143  *       Second is when automatic timestamp compensation is used, (min_compensation < FLT_MAX)
144  *              in this case the output timestamps will match output sample numbers
145  *
146  * @param pts   timstamp for the next input sample, INT64_MIN if unknown
147  * @returns the output timestamp for the next output sample
148  */
149 int64_t swr_next_pts(struct SwrContext *s, int64_t pts);
150
151 /**
152  * Activate resampling compensation.
153  */
154 int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
155
156 /**
157  * Set a customized input channel mapping.
158  *
159  * @param s           allocated Swr context, not yet initialized
160  * @param channel_map customized input channel mapping (array of channel
161  *                    indexes, -1 for a muted channel)
162  * @return AVERROR error code in case of failure.
163  */
164 int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);
165
166 /**
167  * Set a customized remix matrix.
168  *
169  * @param s       allocated Swr context, not yet initialized
170  * @param matrix  remix coefficients; matrix[i + stride * o] is
171  *                the weight of input channel i in output channel o
172  * @param stride  offset between lines of the matrix
173  * @return  AVERROR error code in case of failure.
174  */
175 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);
176
177 /**
178  * Drops the specified number of output samples.
179  */
180 int swr_drop_output(struct SwrContext *s, int count);
181
182 /**
183  * Injects the specified number of silence samples.
184  */
185 int swr_inject_silence(struct SwrContext *s, int count);
186
187 /**
188  * Gets the delay the next input sample will experience relative to the next output sample.
189  *
190  * Swresample can buffer data if more input has been provided than available
191  * output space, also converting between sample rates needs a delay.
192  * This function returns the sum of all such delays.
193  *
194  * @param s     swr context
195  * @param base  timebase in which the returned delay will be
196  *              if its set to 1 the returned delay is in seconds
197  *              if its set to 1000 the returned delay is in milli seconds
198  *              if its set to the input sample rate then the returned delay is in input samples
199  *              if its set to the output sample rate then the returned delay is in output samples
200  *              an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate)
201  * @returns     the delay in 1/base units.
202  */
203 int64_t swr_get_delay(struct SwrContext *s, int64_t base);
204
205 /**
206  * Return the LIBSWRESAMPLE_VERSION_INT constant.
207  */
208 unsigned swresample_version(void);
209
210 /**
211  * Return the swr build-time configuration.
212  */
213 const char *swresample_configuration(void);
214
215 /**
216  * Return the swr license.
217  */
218 const char *swresample_license(void);
219
220 #endif /* SWRESAMPLE_SWRESAMPLE_H */