1 /*
2 * Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
28
29
30 /* double template */
31 #define CONFIG_RESAMPLE_DBL
33 #undef CONFIG_RESAMPLE_DBL
34
35 /* float template */
36 #define CONFIG_RESAMPLE_FLT
38 #undef CONFIG_RESAMPLE_FLT
39
40 /* s32 template */
41 #define CONFIG_RESAMPLE_S32
43 #undef CONFIG_RESAMPLE_S32
44
45 /* s16 template */
47
48
49 /* 0th order modified Bessel function of the first kind. */
51 {
52 double v = 1;
53 double lastv = 0;
54 double t = 1;
55 int i;
56
57 x = x * x / 4;
58 for (i = 1; v != lastv; i++) {
59 lastv = v;
60 t *= x / (i * i);
61 v += t;
62 }
63 return v;
64 }
65
66 /* Build a polyphase filterbank. */
68 {
69 int ph, i;
74 const int center = (tap_count - 1) / 2;
75
76 tab =
av_malloc(tap_count *
sizeof(*tab));
77 if (!tab)
79
80 for (ph = 0; ph < phase_count; ph++) {
81 double norm = 0;
82 for (i = 0; i < tap_count; i++) {
83 x =
M_PI * ((double)(i - center) - (double)ph / phase_count) *
factor;
84 if (x == 0) y = 1.0;
85 else y = sin(x) / x;
88 const float d = -0.5; //first order derivative = -0.5
89 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
90 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
91 else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
92 break;
93 }
95 w = 2.0 * x / (factor * tap_count) +
M_PI;
96 y *= 0.3635819 - 0.4891775 * cos( w) +
97 0.1365995 * cos(2 * w) -
98 0.0106411 * cos(3 * w);
99 break;
101 w = 2.0 * x / (factor * tap_count *
M_PI);
103 break;
104 }
105
106 tab[i] = y;
107 norm += y;
108 }
109 /* normalize so that an uniform color remains the same */
110 for (i = 0; i < tap_count; i++)
111 tab[i] = tab[i] / norm;
112
114 }
115
117 return 0;
118 }
119
121 {
127 int felem_size;
128
134 "resampling: %s\n",
137 }
139 if (!c)
141
149
155 break;
160 break;
165 break;
170 break;
171 }
172
173 if (ARCH_AARCH64)
175 if (ARCH_ARM)
177
182
185
190
193 in_rate * (int64_t)phase_count, INT32_MAX / 2))
196
201
202 /* allocate internal buffer */
205 "resample buffer");
210
214
216
222 }
223
225 {
226 if (!*c)
227 return;
231 }
232
234 int compensation_distance)
235 {
237
238 if (compensation_distance < 0)
240 if (!compensation_distance && sample_delta)
242
246 }
249 if (compensation_distance) {
251 (int64_t)sample_delta / compensation_distance;
252 } else {
254 }
255
256 return 0;
257 }
258
260 int *consumed, int src_size, int dst_size, int update_ctx,
261 int nearest_neighbour)
262 {
263 int dst_index;
269
270 if (!dst != !src)
272
273 if (nearest_neighbour) {
274 uint64_t index2 = ((uint64_t)index) << 32;
276 dst_size =
FFMIN(dst_size,
277 (src_size-1-index) * (int64_t)c->
src_incr /
279
280 if (dst) {
281 for(dst_index = 0; dst_index < dst_size; dst_index++) {
283 index2 += incr;
284 }
285 } else {
286 dst_index = dst_size;
287 }
288 index += dst_index * dst_incr;
289 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->
src_incr;
290 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->
src_incr;
291 } else {
292 for (dst_index = 0; dst_index < dst_size; dst_index++) {
294
296 break;
297
298 if (dst)
300
301 frac += dst_incr_frac;
302 index += dst_incr;
305 index++;
306 }
307 if (dst_index + 1 == compensation_distance) {
308 compensation_distance = 0;
311 }
312 }
313 }
314 if (consumed)
316
317 if (update_ctx) {
319
320 if (compensation_distance) {
321 compensation_distance -= dst_index;
322 if (compensation_distance <= 0)
324 }
329 }
330
331 return dst_index;
332 }
333
335 {
336 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
341
344
345 /* add input samples to the internal buffer */
346 if (src) {
348 if (ret < 0)
349 return ret;
350 } else if (in_leftover <= c->final_padding_samples) {
351 /* no remaining samples to flush */
352 return 0;
353 }
354
357 int i;
358
360 return 0;
361
367 } else {
368 memset(c->
buffer->
data[ch] + bps * i, 0, bps);
369 }
370 }
372 }
373
376 int i;
377
379 FFMAX(in_samples, in_leftover) +
381 if (ret < 0) {
384 }
385
388 if (in_leftover > i) {
389 memcpy(c->
buffer->
data[ch] + bps * (in_leftover + i),
390 c->
buffer->
data[ch] + bps * (in_leftover - i - 1),
391 bps);
392 } else {
393 memset(c->
buffer->
data[ch] + bps * (in_leftover + i),
394 0, bps);
395 }
396 }
400 }
401
402
403 /* calculate output size and reallocate output buffer if needed */
404 /* TODO: try to calculate this without the dummy resample() run */
407 INT_MAX, 0, nearest_neighbour);
409 if (ret < 0) {
411 return ret;
412 }
413 }
414
415 /* resample each channel plane */
421 }
422 if (out_samples < 0) {
424 return out_samples;
425 }
426
427 /* drain consumed samples from the internal buffer */
430
433
435 return 0;
436 }
437
439 {
441
443 return 0;
444
446 }
int initial_padding_filled
int initial_padding_samples
int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta, int compensation_distance)
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
Audio buffer used for intermediate storage between conversion phases.
int avresample_get_delay(AVAudioResampleContext *avr)
void(* set_filter)(void *filter, double *tab, int phase, int tap_count)
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
int allow_realloc
realloc is allowed
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
double cutoff
resampling cutoff frequency.
int nb_samples
current number of samples
static int resample(ResampleContext *c, void *dst, const void *src, int *consumed, int src_size, int dst_size, int update_ctx, int nearest_neighbour)
AVAudioResampleContext * avr
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
int read_only
data is read-only
int compensation_distance
enum AVResampleFilterType filter_type
int av_reduce(int *dst_num, int *dst_den, int64_t num, int64_t den, int64_t max)
Reduce a fraction.
int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
Resample audio data.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int channels
channel count
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL
Blackman Nuttall Windowed Sinc.
ResampleContext * resample
resampling context
av_cold void ff_audio_resample_init_arm(ResampleContext *c, enum AVSampleFormat sample_fmt)
int phase_shift
log2 of the number of entries in the resampling polyphase filterbank
int ff_audio_data_combine(AudioData *dst, int dst_offset, AudioData *src, int src_offset, int nb_samples)
Append data from one AudioData to the end of another.
void ff_audio_data_drain(AudioData *a, int nb_samples)
Drain samples from the start of the AudioData.
int linear_interp
if 1 then the resampling FIR filter will be linearly interpolated
int kaiser_beta
beta value for Kaiser window (only applicable if filter_type == AV_FILTER_TYPE_KAISER) ...
static void error(const char *err)
int in_sample_rate
input sample rate
void ff_audio_resample_free(ResampleContext **c)
Free a ResampleContext.
static int build_filter(ResampleContext *c, double factor)
uint8_t * data[AVRESAMPLE_MAX_CHANNELS]
data plane pointers
enum AVResampleFilterType filter_type
resampling filter type
enum AVSampleFormat internal_sample_fmt
internal sample format
Replacements for frequently missing libm functions.
#define AVERROR_BUG
Internal bug, also see AVERROR_BUG2.
ResampleContext * ff_audio_resample_init(AVAudioResampleContext *avr)
Allocate and initialize a ResampleContext.
int filter_size
length of each FIR filter in the resampling filterbank relative to the cutoff frequency ...
AV_RESAMPLE_FILTER_TYPE_CUBIC
Cubic.
static const int factor[16]
void(* resample_one)(struct ResampleContext *c, void *dst0, int dst_index, const void *src0, unsigned int index, int frac)
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
common internal and external API header
int resample_channels
number of channels used for resampling
int resample_needed
resampling is needed
int final_padding_samples
int allocated_samples
number of samples the buffer can hold
AV_RESAMPLE_FILTER_TYPE_KAISER
Kaiser Windowed Sinc.
static const struct twinvq_data tab
int out_sample_rate
output sample rate
void ff_audio_data_free(AudioData **a)
Free AudioData.
void(* resample_nearest)(void *dst0, int dst_index, const void *src0, unsigned int index)
static double bessel(double x)
av_cold void ff_audio_resample_init_aarch64(ResampleContext *c, enum AVSampleFormat sample_fmt)
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){intplanes=out->planar?out->ch_count:1;unsignedm=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch