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