yading@11
|
1 /*
|
yading@11
|
2 * Copyright (C) 2011-2013 Michael Niedermayer (michaelni@gmx.at)
|
yading@11
|
3 *
|
yading@11
|
4 * This file is part of libswresample
|
yading@11
|
5 *
|
yading@11
|
6 * libswresample is free software; you can redistribute it and/or
|
yading@11
|
7 * modify it under the terms of the GNU Lesser General Public
|
yading@11
|
8 * License as published by the Free Software Foundation; either
|
yading@11
|
9 * version 2.1 of the License, or (at your option) any later version.
|
yading@11
|
10 *
|
yading@11
|
11 * libswresample is distributed in the hope that it will be useful,
|
yading@11
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
yading@11
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
yading@11
|
14 * Lesser General Public License for more details.
|
yading@11
|
15 *
|
yading@11
|
16 * You should have received a copy of the GNU Lesser General Public
|
yading@11
|
17 * License along with libswresample; if not, write to the Free Software
|
yading@11
|
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
yading@11
|
19 */
|
yading@11
|
20
|
yading@11
|
21 #ifndef SWR_INTERNAL_H
|
yading@11
|
22 #define SWR_INTERNAL_H
|
yading@11
|
23
|
yading@11
|
24 #include "swresample.h"
|
yading@11
|
25 #include "libavutil/channel_layout.h"
|
yading@11
|
26 #include "config.h"
|
yading@11
|
27
|
yading@11
|
28 #define SQRT3_2 1.22474487139158904909 /* sqrt(3/2) */
|
yading@11
|
29
|
yading@11
|
30 #define NS_TAPS 20
|
yading@11
|
31
|
yading@11
|
32 #if ARCH_X86_64
|
yading@11
|
33 typedef int64_t integer;
|
yading@11
|
34 #else
|
yading@11
|
35 typedef int integer;
|
yading@11
|
36 #endif
|
yading@11
|
37
|
yading@11
|
38 typedef void (mix_1_1_func_type)(void *out, const void *in, void *coeffp, integer index, integer len);
|
yading@11
|
39 typedef void (mix_2_1_func_type)(void *out, const void *in1, const void *in2, void *coeffp, integer index1, integer index2, integer len);
|
yading@11
|
40
|
yading@11
|
41 typedef void (mix_any_func_type)(uint8_t **out, const uint8_t **in1, void *coeffp, integer len);
|
yading@11
|
42
|
yading@11
|
43 typedef struct AudioData{
|
yading@11
|
44 uint8_t *ch[SWR_CH_MAX]; ///< samples buffer per channel
|
yading@11
|
45 uint8_t *data; ///< samples buffer
|
yading@11
|
46 int ch_count; ///< number of channels
|
yading@11
|
47 int bps; ///< bytes per sample
|
yading@11
|
48 int count; ///< number of samples
|
yading@11
|
49 int planar; ///< 1 if planar audio, 0 otherwise
|
yading@11
|
50 enum AVSampleFormat fmt; ///< sample format
|
yading@11
|
51 } AudioData;
|
yading@11
|
52
|
yading@11
|
53 struct DitherContext {
|
yading@11
|
54 enum SwrDitherType method;
|
yading@11
|
55 int noise_pos;
|
yading@11
|
56 float scale;
|
yading@11
|
57 float noise_scale; ///< Noise scale
|
yading@11
|
58 int ns_taps; ///< Noise shaping dither taps
|
yading@11
|
59 float ns_scale; ///< Noise shaping dither scale
|
yading@11
|
60 float ns_scale_1; ///< Noise shaping dither scale^-1
|
yading@11
|
61 int ns_pos; ///< Noise shaping dither position
|
yading@11
|
62 float ns_coeffs[NS_TAPS]; ///< Noise shaping filter coefficients
|
yading@11
|
63 float ns_errors[SWR_CH_MAX][2*NS_TAPS];
|
yading@11
|
64 AudioData noise; ///< noise used for dithering
|
yading@11
|
65 AudioData temp; ///< temporary storage when writing into the input buffer isnt possible
|
yading@11
|
66 int output_sample_bits; ///< the number of used output bits, needed to scale dither correctly
|
yading@11
|
67 };
|
yading@11
|
68
|
yading@11
|
69 struct SwrContext {
|
yading@11
|
70 const AVClass *av_class; ///< AVClass used for AVOption and av_log()
|
yading@11
|
71 int log_level_offset; ///< logging level offset
|
yading@11
|
72 void *log_ctx; ///< parent logging context
|
yading@11
|
73 enum AVSampleFormat in_sample_fmt; ///< input sample format
|
yading@11
|
74 enum AVSampleFormat int_sample_fmt; ///< internal sample format (AV_SAMPLE_FMT_FLTP or AV_SAMPLE_FMT_S16P)
|
yading@11
|
75 enum AVSampleFormat out_sample_fmt; ///< output sample format
|
yading@11
|
76 int64_t in_ch_layout; ///< input channel layout
|
yading@11
|
77 int64_t out_ch_layout; ///< output channel layout
|
yading@11
|
78 int in_sample_rate; ///< input sample rate
|
yading@11
|
79 int out_sample_rate; ///< output sample rate
|
yading@11
|
80 int flags; ///< miscellaneous flags such as SWR_FLAG_RESAMPLE
|
yading@11
|
81 float slev; ///< surround mixing level
|
yading@11
|
82 float clev; ///< center mixing level
|
yading@11
|
83 float lfe_mix_level; ///< LFE mixing level
|
yading@11
|
84 float rematrix_volume; ///< rematrixing volume coefficient
|
yading@11
|
85 enum AVMatrixEncoding matrix_encoding; /**< matrixed stereo encoding */
|
yading@11
|
86 const int *channel_map; ///< channel index (or -1 if muted channel) map
|
yading@11
|
87 int used_ch_count; ///< number of used input channels (mapped channel count if channel_map, otherwise in.ch_count)
|
yading@11
|
88 enum SwrEngine engine;
|
yading@11
|
89
|
yading@11
|
90 struct DitherContext dither;
|
yading@11
|
91
|
yading@11
|
92 int filter_size; /**< length of each FIR filter in the resampling filterbank relative to the cutoff frequency */
|
yading@11
|
93 int phase_shift; /**< log2 of the number of entries in the resampling polyphase filterbank */
|
yading@11
|
94 int linear_interp; /**< if 1 then the resampling FIR filter will be linearly interpolated */
|
yading@11
|
95 double cutoff; /**< resampling cutoff frequency (swr: 6dB point; soxr: 0dB point). 1.0 corresponds to half the output sample rate */
|
yading@11
|
96 enum SwrFilterType filter_type; /**< swr resampling filter type */
|
yading@11
|
97 int kaiser_beta; /**< swr beta value for Kaiser window (only applicable if filter_type == AV_FILTER_TYPE_KAISER) */
|
yading@11
|
98 double precision; /**< soxr resampling precision (in bits) */
|
yading@11
|
99 int cheby; /**< soxr: if 1 then passband rolloff will be none (Chebyshev) & irrational ratio approximation precision will be higher */
|
yading@11
|
100
|
yading@11
|
101 float min_compensation; ///< swr minimum below which no compensation will happen
|
yading@11
|
102 float min_hard_compensation; ///< swr minimum below which no silence inject / sample drop will happen
|
yading@11
|
103 float soft_compensation_duration; ///< swr duration over which soft compensation is applied
|
yading@11
|
104 float max_soft_compensation; ///< swr maximum soft compensation in seconds over soft_compensation_duration
|
yading@11
|
105 float async; ///< swr simple 1 parameter async, similar to ffmpegs -async
|
yading@11
|
106 int64_t firstpts_in_samples; ///< swr first pts in samples
|
yading@11
|
107
|
yading@11
|
108 int resample_first; ///< 1 if resampling must come first, 0 if rematrixing
|
yading@11
|
109 int rematrix; ///< flag to indicate if rematrixing is needed (basically if input and output layouts mismatch)
|
yading@11
|
110 int rematrix_custom; ///< flag to indicate that a custom matrix has been defined
|
yading@11
|
111
|
yading@11
|
112 AudioData in; ///< input audio data
|
yading@11
|
113 AudioData postin; ///< post-input audio data: used for rematrix/resample
|
yading@11
|
114 AudioData midbuf; ///< intermediate audio data (postin/preout)
|
yading@11
|
115 AudioData preout; ///< pre-output audio data: used for rematrix/resample
|
yading@11
|
116 AudioData out; ///< converted output audio data
|
yading@11
|
117 AudioData in_buffer; ///< cached audio data (convert and resample purpose)
|
yading@11
|
118 AudioData silence; ///< temporary with silence
|
yading@11
|
119 AudioData drop_temp; ///< temporary used to discard output
|
yading@11
|
120 int in_buffer_index; ///< cached buffer position
|
yading@11
|
121 int in_buffer_count; ///< cached buffer length
|
yading@11
|
122 int resample_in_constraint; ///< 1 if the input end was reach before the output end, 0 otherwise
|
yading@11
|
123 int flushed; ///< 1 if data is to be flushed and no further input is expected
|
yading@11
|
124 int64_t outpts; ///< output PTS
|
yading@11
|
125 int64_t firstpts; ///< first PTS
|
yading@11
|
126 int drop_output; ///< number of output samples to drop
|
yading@11
|
127
|
yading@11
|
128 struct AudioConvert *in_convert; ///< input conversion context
|
yading@11
|
129 struct AudioConvert *out_convert; ///< output conversion context
|
yading@11
|
130 struct AudioConvert *full_convert; ///< full conversion context (single conversion for input and output)
|
yading@11
|
131 struct ResampleContext *resample; ///< resampling context
|
yading@11
|
132 struct Resampler const *resampler; ///< resampler virtual function table
|
yading@11
|
133
|
yading@11
|
134 float matrix[SWR_CH_MAX][SWR_CH_MAX]; ///< floating point rematrixing coefficients
|
yading@11
|
135 uint8_t *native_matrix;
|
yading@11
|
136 uint8_t *native_one;
|
yading@11
|
137 uint8_t *native_simd_matrix;
|
yading@11
|
138 int32_t matrix32[SWR_CH_MAX][SWR_CH_MAX]; ///< 17.15 fixed point rematrixing coefficients
|
yading@11
|
139 uint8_t matrix_ch[SWR_CH_MAX][SWR_CH_MAX+1]; ///< Lists of input channels per output channel that have non zero rematrixing coefficients
|
yading@11
|
140 mix_1_1_func_type *mix_1_1_f;
|
yading@11
|
141 mix_1_1_func_type *mix_1_1_simd;
|
yading@11
|
142
|
yading@11
|
143 mix_2_1_func_type *mix_2_1_f;
|
yading@11
|
144 mix_2_1_func_type *mix_2_1_simd;
|
yading@11
|
145
|
yading@11
|
146 mix_any_func_type *mix_any_f;
|
yading@11
|
147
|
yading@11
|
148 /* TODO: callbacks for ASM optimizations */
|
yading@11
|
149 };
|
yading@11
|
150
|
yading@11
|
151 typedef struct ResampleContext * (* resample_init_func)(struct ResampleContext *c, int out_rate, int in_rate, int filter_size, int phase_shift, int linear,
|
yading@11
|
152 double cutoff, enum AVSampleFormat format, enum SwrFilterType filter_type, int kaiser_beta, double precision, int cheby);
|
yading@11
|
153 typedef void (* resample_free_func)(struct ResampleContext **c);
|
yading@11
|
154 typedef int (* multiple_resample_func)(struct ResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed);
|
yading@11
|
155 typedef int (* resample_flush_func)(struct SwrContext *c);
|
yading@11
|
156 typedef int (* set_compensation_func)(struct ResampleContext *c, int sample_delta, int compensation_distance);
|
yading@11
|
157 typedef int64_t (* get_delay_func)(struct SwrContext *s, int64_t base);
|
yading@11
|
158
|
yading@11
|
159 struct Resampler {
|
yading@11
|
160 resample_init_func init;
|
yading@11
|
161 resample_free_func free;
|
yading@11
|
162 multiple_resample_func multiple_resample;
|
yading@11
|
163 resample_flush_func flush;
|
yading@11
|
164 set_compensation_func set_compensation;
|
yading@11
|
165 get_delay_func get_delay;
|
yading@11
|
166 };
|
yading@11
|
167
|
yading@11
|
168 extern struct Resampler const swri_resampler;
|
yading@11
|
169
|
yading@11
|
170 int swri_realloc_audio(AudioData *a, int count);
|
yading@11
|
171 int swri_resample_int16(struct ResampleContext *c, int16_t *dst, const int16_t *src, int *consumed, int src_size, int dst_size, int update_ctx);
|
yading@11
|
172 int swri_resample_int32(struct ResampleContext *c, int32_t *dst, const int32_t *src, int *consumed, int src_size, int dst_size, int update_ctx);
|
yading@11
|
173 int swri_resample_float(struct ResampleContext *c, float *dst, const float *src, int *consumed, int src_size, int dst_size, int update_ctx);
|
yading@11
|
174 int swri_resample_double(struct ResampleContext *c,double *dst, const double *src, int *consumed, int src_size, int dst_size, int update_ctx);
|
yading@11
|
175
|
yading@11
|
176 void swri_noise_shaping_int16 (SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count);
|
yading@11
|
177 void swri_noise_shaping_int32 (SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count);
|
yading@11
|
178 void swri_noise_shaping_float (SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count);
|
yading@11
|
179 void swri_noise_shaping_double(SwrContext *s, AudioData *dsts, const AudioData *srcs, const AudioData *noises, int count);
|
yading@11
|
180
|
yading@11
|
181 int swri_rematrix_init(SwrContext *s);
|
yading@11
|
182 void swri_rematrix_free(SwrContext *s);
|
yading@11
|
183 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy);
|
yading@11
|
184 void swri_rematrix_init_x86(struct SwrContext *s);
|
yading@11
|
185
|
yading@11
|
186 void swri_get_dither(SwrContext *s, void *dst, int len, unsigned seed, enum AVSampleFormat noise_fmt);
|
yading@11
|
187 int swri_dither_init(SwrContext *s, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt);
|
yading@11
|
188
|
yading@11
|
189 void swri_audio_convert_init_arm(struct AudioConvert *ac,
|
yading@11
|
190 enum AVSampleFormat out_fmt,
|
yading@11
|
191 enum AVSampleFormat in_fmt,
|
yading@11
|
192 int channels);
|
yading@11
|
193 void swri_audio_convert_init_x86(struct AudioConvert *ac,
|
yading@11
|
194 enum AVSampleFormat out_fmt,
|
yading@11
|
195 enum AVSampleFormat in_fmt,
|
yading@11
|
196 int channels);
|
yading@11
|
197 #endif
|