swresample.h 7.75 KB
Newer Older
Michael Niedermayer's avatar
Michael Niedermayer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
 *
 * This file is part of libswresample
 *
 * libswresample is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * libswresample is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with libswresample; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

21 22 23 24 25
/**
 * @file
 * libswresample public header
 */

Michael Niedermayer's avatar
Michael Niedermayer committed
26 27 28 29 30 31 32
#ifndef SWR_H
#define SWR_H

#include <inttypes.h>
#include "libavutil/samplefmt.h"

#define LIBSWRESAMPLE_VERSION_MAJOR 0
33
#define LIBSWRESAMPLE_VERSION_MINOR 15
34
#define LIBSWRESAMPLE_VERSION_MICRO 100
Michael Niedermayer's avatar
Michael Niedermayer committed
35

36 37 38 39
#define LIBSWRESAMPLE_VERSION_INT  AV_VERSION_INT(LIBSWRESAMPLE_VERSION_MAJOR, \
                                                  LIBSWRESAMPLE_VERSION_MINOR, \
                                                  LIBSWRESAMPLE_VERSION_MICRO)

40
#if LIBSWRESAMPLE_VERSION_MAJOR < 1
41
#define SWR_CH_MAX 32   ///< Maximum number of channels
42
#endif
Michael Niedermayer's avatar
Michael Niedermayer committed
43

44
#define SWR_FLAG_RESAMPLE 1 ///< Force resampling even if equal sample rate
Michael Niedermayer's avatar
Michael Niedermayer committed
45 46 47
//TODO use int resample ?
//long term TODO can we enable this dynamically?

48 49 50
enum SwrDitherType {
    SWR_DITHER_NONE = 0,
    SWR_DITHER_RECTANGULAR,
51
    SWR_DITHER_TRIANGULAR,
52
    SWR_DITHER_TRIANGULAR_HIGHPASS,
53 54
    SWR_DITHER_NB,              ///< not part of API/ABI
};
Michael Niedermayer's avatar
Michael Niedermayer committed
55

56
typedef struct SwrContext SwrContext;
Michael Niedermayer's avatar
Michael Niedermayer committed
57

58 59 60 61 62 63 64 65
/**
 * Get the AVClass for swrContext. It can be used in combination with
 * AV_OPT_SEARCH_FAKE_OBJ for examining options.
 *
 * @see av_opt_find().
 */
const AVClass *swr_get_class(void);

Michael Niedermayer's avatar
Michael Niedermayer committed
66 67
/**
 * Allocate SwrContext.
68 69 70 71 72 73
 *
 * If you use this function you will need to set the parameters (manually or
 * with swr_alloc_set_opts()) before calling swr_init().
 *
 * @see swr_alloc_set_opts(), swr_init(), swr_free()
 * @return NULL on error, allocated context otherwise
Michael Niedermayer's avatar
Michael Niedermayer committed
74 75 76 77 78
 */
struct SwrContext *swr_alloc(void);

/**
 * Initialize context after user parameters have been set.
79 80
 *
 * @return AVERROR error code in case of failure.
Michael Niedermayer's avatar
Michael Niedermayer committed
81 82 83 84
 */
int swr_init(struct SwrContext *s);

/**
85 86 87 88 89 90 91 92
 * Allocate SwrContext if needed and set/reset common parameters.
 *
 * This function does not require s to be allocated with swr_alloc(). On the
 * other hand, swr_alloc() can use swr_alloc_set_opts() to set the parameters
 * on the allocated context.
 *
 * @param s               Swr context, can be NULL
 * @param out_ch_layout   output channel layout (AV_CH_LAYOUT_*)
93
 * @param out_sample_fmt  output sample format (AV_SAMPLE_FMT_*).
94 95
 * @param out_sample_rate output sample rate (frequency in Hz)
 * @param in_ch_layout    input channel layout (AV_CH_LAYOUT_*)
96
 * @param in_sample_fmt   input sample format (AV_SAMPLE_FMT_*).
97 98 99 100 101 102
 * @param in_sample_rate  input sample rate (frequency in Hz)
 * @param log_offset      logging level offset
 * @param log_ctx         parent logging context, can be NULL
 *
 * @see swr_init(), swr_free()
 * @return NULL on error, allocated context otherwise
Michael Niedermayer's avatar
Michael Niedermayer committed
103
 */
104 105 106
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,
107
                                      int log_offset, void *log_ctx);
Michael Niedermayer's avatar
Michael Niedermayer committed
108 109

/**
110
 * Free the given SwrContext and set the pointer to NULL.
Michael Niedermayer's avatar
Michael Niedermayer committed
111 112 113 114 115
 */
void swr_free(struct SwrContext **s);

/**
 * Convert audio.
116
 *
117 118 119
 * in and in_count can be set to 0 to flush the last few samples out at the
 * end.
 *
120 121 122 123
 * If more input is provided than output space then the input will be buffered.
 * You can avoid this buffering by providing more output space than input.
 * Convertion will run directly without copying whenever possible.
 *
124 125 126 127 128 129
 * @param s         allocated Swr context, with parameters set
 * @param out       output buffers, only the first one need be set in case of packed audio
 * @param out_count amount of space available for output in samples per channel
 * @param in        input buffers, only the first one need to be set in case of packed audio
 * @param in_count  number of input samples available in one channel
 *
130
 * @return number of samples output per channel, negative value on error
Michael Niedermayer's avatar
Michael Niedermayer committed
131
 */
132 133
int swr_convert(struct SwrContext *s, uint8_t **out, int out_count,
                                const uint8_t **in , int in_count);
Michael Niedermayer's avatar
Michael Niedermayer committed
134

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
/**
 * Convert the next timestamp from input to output
 * timestampe are in 1/(in_sample_rate * out_sample_rate) units.
 *
 * @note There are 2 slightly differently behaving modes.
 *       First is when automatic timestamp compensation is not used, (min_compensation >= FLT_MAX)
 *              in this case timestamps will be passed through with delays compensated
 *       Second is when automatic timestamp compensation is used, (min_compensation < FLT_MAX)
 *              in this case the output timestamps will match output sample numbers
 *
 * @param pts   timstamp for the next input sample, INT64_MIN if unknown
 * @returns the output timestamp for the next output sample
 */
int64_t swr_next_pts(struct SwrContext *s, int64_t pts);

150 151 152
/**
 * Activate resampling compensation.
 */
153
int swr_set_compensation(struct SwrContext *s, int sample_delta, int compensation_distance);
Michael Niedermayer's avatar
Michael Niedermayer committed
154

155 156 157 158 159 160 161 162 163 164
/**
 * Set a customized input channel mapping.
 *
 * @param s           allocated Swr context, not yet initialized
 * @param channel_map customized input channel mapping (array of channel
 *                    indexes, -1 for a muted channel)
 * @return AVERROR error code in case of failure.
 */
int swr_set_channel_mapping(struct SwrContext *s, const int *channel_map);

165 166 167 168 169 170 171 172 173 174 175
/**
 * Set a customized remix matrix.
 *
 * @param s       allocated Swr context, not yet initialized
 * @param matrix  remix coefficients; matrix[i + stride * o] is
 *                the weight of input channel i in output channel o
 * @param stride  offset between lines of the matrix
 * @return  AVERROR error code in case of failure.
 */
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride);

176 177 178 179 180
/**
 * Drops the specified number of output samples.
 */
int swr_drop_output(struct SwrContext *s, int count);

181 182 183 184 185
/**
 * Injects the specified number of silence samples.
 */
int swr_inject_silence(struct SwrContext *s, int count);

186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/**
 * Gets the delay the next input sample will experience relative to the next output sample.
 *
 * Swresample can buffer data if more input has been provided than available
 * output space, also converting between sample rates needs a delay.
 * This function returns the sum of all such delays.
 *
 * @param s     swr context
 * @param base  timebase in which the returned delay will be
 *              if its set to 1 the returned delay is in seconds
 *              if its set to 1000 the returned delay is in milli seconds
 *              if its set to the input sample rate then the returned delay is in input samples
 *              if its set to the output sample rate then the returned delay is in output samples
 *              an exact rounding free delay can be found by using LCM(in_sample_rate, out_sample_rate)
 * @returns     the delay in 1/base units.
 */
int64_t swr_get_delay(struct SwrContext *s, int64_t base);

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
/**
 * Return the LIBSWRESAMPLE_VERSION_INT constant.
 */
unsigned swresample_version(void);

/**
 * Return the swr build-time configuration.
 */
const char *swresample_configuration(void);

/**
 * Return the swr license.
 */
const char *swresample_license(void);

Michael Niedermayer's avatar
Michael Niedermayer committed
219
#endif