rtpdec_vp8: Cosmetics: Fix bad alignment/indentation
[libav.git] / libavresample / resample.c
CommitLineData
c8af852b
JR
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 Libav.
6 *
7 * Libav 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 * Libav 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 Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
1d9c2dc8 22#include "libavutil/common.h"
c8af852b
JR
23#include "libavutil/libm.h"
24#include "libavutil/log.h"
25#include "internal.h"
26#include "audio_data.h"
27
c8af852b
JR
28struct ResampleContext {
29 AVAudioResampleContext *avr;
30 AudioData *buffer;
64103976 31 uint8_t *filter_bank;
c8af852b
JR
32 int filter_length;
33 int ideal_dst_incr;
34 int dst_incr;
35 int index;
36 int frac;
37 int src_incr;
38 int compensation_distance;
39 int phase_shift;
40 int phase_mask;
41 int linear;
372647ae
JR
42 enum AVResampleFilterType filter_type;
43 int kaiser_beta;
c8af852b 44 double factor;
64103976
JR
45 void (*set_filter)(void *filter, double *tab, int phase, int tap_count);
46 void (*resample_one)(struct ResampleContext *c, int no_filter, void *dst0,
47 int dst_index, const void *src0, int src_size,
48 int index, int frac);
c8af852b
JR
49};
50
64103976
JR
51
52/* double template */
53#define CONFIG_RESAMPLE_DBL
54#include "resample_template.c"
55#undef CONFIG_RESAMPLE_DBL
56
57/* float template */
58#define CONFIG_RESAMPLE_FLT
59#include "resample_template.c"
60#undef CONFIG_RESAMPLE_FLT
61
62/* s32 template */
63#define CONFIG_RESAMPLE_S32
64#include "resample_template.c"
65#undef CONFIG_RESAMPLE_S32
66
67/* s16 template */
68#include "resample_template.c"
69
70
3aa696e8 71/* 0th order modified bessel function of the first kind. */
c8af852b
JR
72static double bessel(double x)
73{
74 double v = 1;
75 double lastv = 0;
76 double t = 1;
77 int i;
78
79 x = x * x / 4;
80 for (i = 1; v != lastv; i++) {
81 lastv = v;
82 t *= x / (i * i);
83 v += t;
84 }
85 return v;
86}
87
3aa696e8 88/* Build a polyphase filterbank. */
64103976 89static int build_filter(ResampleContext *c)
c8af852b
JR
90{
91 int ph, i;
64103976 92 double x, y, w, factor;
c8af852b 93 double *tab;
64103976
JR
94 int tap_count = c->filter_length;
95 int phase_count = 1 << c->phase_shift;
c8af852b
JR
96 const int center = (tap_count - 1) / 2;
97
98 tab = av_malloc(tap_count * sizeof(*tab));
99 if (!tab)
100 return AVERROR(ENOMEM);
101
102 /* if upsampling, only need to interpolate, no filter */
64103976 103 factor = FFMIN(c->factor, 1.0);
c8af852b
JR
104
105 for (ph = 0; ph < phase_count; ph++) {
106 double norm = 0;
107 for (i = 0; i < tap_count; i++) {
108 x = M_PI * ((double)(i - center) - (double)ph / phase_count) * factor;
109 if (x == 0) y = 1.0;
110 else y = sin(x) / x;
64103976 111 switch (c->filter_type) {
372647ae 112 case AV_RESAMPLE_FILTER_TYPE_CUBIC: {
c8af852b
JR
113 const float d = -0.5; //first order derivative = -0.5
114 x = fabs(((double)(i - center) - (double)ph / phase_count) * factor);
115 if (x < 1.0) y = 1 - 3 * x*x + 2 * x*x*x + d * ( -x*x + x*x*x);
116 else y = d * (-4 + 8 * x - 5 * x*x + x*x*x);
117 break;
118 }
372647ae 119 case AV_RESAMPLE_FILTER_TYPE_BLACKMAN_NUTTALL:
c8af852b
JR
120 w = 2.0 * x / (factor * tap_count) + M_PI;
121 y *= 0.3635819 - 0.4891775 * cos( w) +
122 0.1365995 * cos(2 * w) -
123 0.0106411 * cos(3 * w);
124 break;
372647ae 125 case AV_RESAMPLE_FILTER_TYPE_KAISER:
c8af852b 126 w = 2.0 * x / (factor * tap_count * M_PI);
64103976 127 y *= bessel(c->kaiser_beta * sqrt(FFMAX(1 - w * w, 0)));
c8af852b
JR
128 break;
129 }
130
131 tab[i] = y;
132 norm += y;
133 }
c8af852b 134 /* normalize so that an uniform color remains the same */
64103976
JR
135 for (i = 0; i < tap_count; i++)
136 tab[i] = tab[i] / norm;
137
138 c->set_filter(c->filter_bank, tab, ph, tap_count);
c8af852b
JR
139 }
140
141 av_free(tab);
142 return 0;
143}
144
145ResampleContext *ff_audio_resample_init(AVAudioResampleContext *avr)
146{
147 ResampleContext *c;
148 int out_rate = avr->out_sample_rate;
149 int in_rate = avr->in_sample_rate;
150 double factor = FFMIN(out_rate * avr->cutoff / in_rate, 1.0);
151 int phase_count = 1 << avr->phase_shift;
64103976 152 int felem_size;
c8af852b 153
64103976
JR
154 if (avr->internal_sample_fmt != AV_SAMPLE_FMT_S16P &&
155 avr->internal_sample_fmt != AV_SAMPLE_FMT_S32P &&
156 avr->internal_sample_fmt != AV_SAMPLE_FMT_FLTP &&
157 avr->internal_sample_fmt != AV_SAMPLE_FMT_DBLP) {
c8af852b
JR
158 av_log(avr, AV_LOG_ERROR, "Unsupported internal format for "
159 "resampling: %s\n",
160 av_get_sample_fmt_name(avr->internal_sample_fmt));
161 return NULL;
162 }
163 c = av_mallocz(sizeof(*c));
164 if (!c)
165 return NULL;
166
167 c->avr = avr;
168 c->phase_shift = avr->phase_shift;
169 c->phase_mask = phase_count - 1;
170 c->linear = avr->linear_interp;
171 c->factor = factor;
172 c->filter_length = FFMAX((int)ceil(avr->filter_size / factor), 1);
372647ae
JR
173 c->filter_type = avr->filter_type;
174 c->kaiser_beta = avr->kaiser_beta;
c8af852b 175
64103976
JR
176 switch (avr->internal_sample_fmt) {
177 case AV_SAMPLE_FMT_DBLP:
178 c->resample_one = resample_one_dbl;
179 c->set_filter = set_filter_dbl;
180 break;
181 case AV_SAMPLE_FMT_FLTP:
182 c->resample_one = resample_one_flt;
183 c->set_filter = set_filter_flt;
184 break;
185 case AV_SAMPLE_FMT_S32P:
186 c->resample_one = resample_one_s32;
187 c->set_filter = set_filter_s32;
188 break;
189 case AV_SAMPLE_FMT_S16P:
190 c->resample_one = resample_one_s16;
191 c->set_filter = set_filter_s16;
192 break;
193 }
194
195 felem_size = av_get_bytes_per_sample(avr->internal_sample_fmt);
196 c->filter_bank = av_mallocz(c->filter_length * (phase_count + 1) * felem_size);
c8af852b
JR
197 if (!c->filter_bank)
198 goto error;
199
64103976 200 if (build_filter(c) < 0)
c8af852b
JR
201 goto error;
202
64103976
JR
203 memcpy(&c->filter_bank[(c->filter_length * phase_count + 1) * felem_size],
204 c->filter_bank, (c->filter_length - 1) * felem_size);
205 memcpy(&c->filter_bank[c->filter_length * phase_count * felem_size],
206 &c->filter_bank[(c->filter_length - 1) * felem_size], felem_size);
c8af852b
JR
207
208 c->compensation_distance = 0;
209 if (!av_reduce(&c->src_incr, &c->dst_incr, out_rate,
210 in_rate * (int64_t)phase_count, INT32_MAX / 2))
211 goto error;
212 c->ideal_dst_incr = c->dst_incr;
213
214 c->index = -phase_count * ((c->filter_length - 1) / 2);
215 c->frac = 0;
216
217 /* allocate internal buffer */
218 c->buffer = ff_audio_data_alloc(avr->resample_channels, 0,
219 avr->internal_sample_fmt,
220 "resample buffer");
221 if (!c->buffer)
222 goto error;
223
224 av_log(avr, AV_LOG_DEBUG, "resample: %s from %d Hz to %d Hz\n",
225 av_get_sample_fmt_name(avr->internal_sample_fmt),
226 avr->in_sample_rate, avr->out_sample_rate);
227
228 return c;
229
230error:
231 ff_audio_data_free(&c->buffer);
232 av_free(c->filter_bank);
233 av_free(c);
234 return NULL;
235}
236
237void ff_audio_resample_free(ResampleContext **c)
238{
239 if (!*c)
240 return;
241 ff_audio_data_free(&(*c)->buffer);
242 av_free((*c)->filter_bank);
243 av_freep(c);
244}
245
246int avresample_set_compensation(AVAudioResampleContext *avr, int sample_delta,
247 int compensation_distance)
248{
249 ResampleContext *c;
250 AudioData *fifo_buf = NULL;
251 int ret = 0;
252
253 if (compensation_distance < 0)
254 return AVERROR(EINVAL);
255 if (!compensation_distance && sample_delta)
256 return AVERROR(EINVAL);
257
258 /* if resampling was not enabled previously, re-initialize the
259 AVAudioResampleContext and force resampling */
260 if (!avr->resample_needed) {
261 int fifo_samples;
262 double matrix[AVRESAMPLE_MAX_CHANNELS * AVRESAMPLE_MAX_CHANNELS] = { 0 };
263
264 /* buffer any remaining samples in the output FIFO before closing */
265 fifo_samples = av_audio_fifo_size(avr->out_fifo);
266 if (fifo_samples > 0) {
267 fifo_buf = ff_audio_data_alloc(avr->out_channels, fifo_samples,
268 avr->out_sample_fmt, NULL);
269 if (!fifo_buf)
270 return AVERROR(EINVAL);
271 ret = ff_audio_data_read_from_fifo(avr->out_fifo, fifo_buf,
272 fifo_samples);
273 if (ret < 0)
274 goto reinit_fail;
275 }
276 /* save the channel mixing matrix */
277 ret = avresample_get_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
278 if (ret < 0)
279 goto reinit_fail;
280
281 /* close the AVAudioResampleContext */
282 avresample_close(avr);
283
284 avr->force_resampling = 1;
285
286 /* restore the channel mixing matrix */
287 ret = avresample_set_matrix(avr, matrix, AVRESAMPLE_MAX_CHANNELS);
288 if (ret < 0)
289 goto reinit_fail;
290
291 /* re-open the AVAudioResampleContext */
292 ret = avresample_open(avr);
293 if (ret < 0)
294 goto reinit_fail;
295
296 /* restore buffered samples to the output FIFO */
297 if (fifo_samples > 0) {
298 ret = ff_audio_data_add_to_fifo(avr->out_fifo, fifo_buf, 0,
299 fifo_samples);
300 if (ret < 0)
301 goto reinit_fail;
302 ff_audio_data_free(&fifo_buf);
303 }
304 }
305 c = avr->resample;
306 c->compensation_distance = compensation_distance;
307 if (compensation_distance) {
308 c->dst_incr = c->ideal_dst_incr - c->ideal_dst_incr *
309 (int64_t)sample_delta / compensation_distance;
310 } else {
311 c->dst_incr = c->ideal_dst_incr;
312 }
313 return 0;
314
315reinit_fail:
316 ff_audio_data_free(&fifo_buf);
317 return ret;
318}
319
64103976 320static int resample(ResampleContext *c, void *dst, const void *src,
c8af852b
JR
321 int *consumed, int src_size, int dst_size, int update_ctx)
322{
64103976 323 int dst_index;
c8af852b
JR
324 int index = c->index;
325 int frac = c->frac;
326 int dst_incr_frac = c->dst_incr % c->src_incr;
327 int dst_incr = c->dst_incr / c->src_incr;
328 int compensation_distance = c->compensation_distance;
329
330 if (!dst != !src)
331 return AVERROR(EINVAL);
332
333 if (compensation_distance == 0 && c->filter_length == 1 &&
334 c->phase_shift == 0) {
335 int64_t index2 = ((int64_t)index) << 32;
336 int64_t incr = (1LL << 32) * c->dst_incr / c->src_incr;
337 dst_size = FFMIN(dst_size,
338 (src_size-1-index) * (int64_t)c->src_incr /
339 c->dst_incr);
340
341 if (dst) {
342 for(dst_index = 0; dst_index < dst_size; dst_index++) {
64103976 343 c->resample_one(c, 1, dst, dst_index, src, 0, index2 >> 32, 0);
c8af852b
JR
344 index2 += incr;
345 }
346 } else {
347 dst_index = dst_size;
348 }
349 index += dst_index * dst_incr;
350 index += (frac + dst_index * (int64_t)dst_incr_frac) / c->src_incr;
351 frac = (frac + dst_index * (int64_t)dst_incr_frac) % c->src_incr;
352 } else {
353 for (dst_index = 0; dst_index < dst_size; dst_index++) {
c8af852b
JR
354 int sample_index = index >> c->phase_shift;
355
64103976
JR
356 if (sample_index + c->filter_length > src_size ||
357 -sample_index >= src_size)
c8af852b
JR
358 break;
359
64103976
JR
360 if (dst)
361 c->resample_one(c, 0, dst, dst_index, src, src_size, index, frac);
c8af852b
JR
362
363 frac += dst_incr_frac;
364 index += dst_incr;
365 if (frac >= c->src_incr) {
366 frac -= c->src_incr;
367 index++;
368 }
369 if (dst_index + 1 == compensation_distance) {
370 compensation_distance = 0;
371 dst_incr_frac = c->ideal_dst_incr % c->src_incr;
372 dst_incr = c->ideal_dst_incr / c->src_incr;
373 }
374 }
375 }
376 if (consumed)
377 *consumed = FFMAX(index, 0) >> c->phase_shift;
378
379 if (update_ctx) {
380 if (index >= 0)
381 index &= c->phase_mask;
382
383 if (compensation_distance) {
384 compensation_distance -= dst_index;
385 if (compensation_distance <= 0)
386 return AVERROR_BUG;
387 }
388 c->frac = frac;
389 c->index = index;
390 c->dst_incr = dst_incr_frac + c->src_incr*dst_incr;
391 c->compensation_distance = compensation_distance;
392 }
393
394 return dst_index;
395}
396
1d86aa8b 397int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
c8af852b 398{
1d86aa8b 399 int ch, in_samples, in_leftover, consumed = 0, out_samples = 0;
c8af852b
JR
400 int ret = AVERROR(EINVAL);
401
402 in_samples = src ? src->nb_samples : 0;
403 in_leftover = c->buffer->nb_samples;
404
405 /* add input samples to the internal buffer */
406 if (src) {
407 ret = ff_audio_data_combine(c->buffer, in_leftover, src, 0, in_samples);
408 if (ret < 0)
409 return ret;
410 } else if (!in_leftover) {
411 /* no remaining samples to flush */
412 return 0;
413 } else {
414 /* TODO: pad buffer to flush completely */
415 }
416
417 /* calculate output size and reallocate output buffer if needed */
418 /* TODO: try to calculate this without the dummy resample() run */
419 if (!dst->read_only && dst->allow_realloc) {
420 out_samples = resample(c, NULL, NULL, NULL, c->buffer->nb_samples,
421 INT_MAX, 0);
422 ret = ff_audio_data_realloc(dst, out_samples);
423 if (ret < 0) {
424 av_log(c->avr, AV_LOG_ERROR, "error reallocating output\n");
425 return ret;
426 }
427 }
428
429 /* resample each channel plane */
430 for (ch = 0; ch < c->buffer->channels; ch++) {
64103976 431 out_samples = resample(c, (void *)dst->data[ch],
1d86aa8b 432 (const void *)c->buffer->data[ch], &consumed,
c8af852b
JR
433 c->buffer->nb_samples, dst->allocated_samples,
434 ch + 1 == c->buffer->channels);
435 }
436 if (out_samples < 0) {
437 av_log(c->avr, AV_LOG_ERROR, "error during resampling\n");
438 return out_samples;
439 }
440
441 /* drain consumed samples from the internal buffer */
1d86aa8b 442 ff_audio_data_drain(c->buffer, consumed);
c8af852b
JR
443
444 av_dlog(c->avr, "resampled %d in + %d leftover to %d out + %d leftover\n",
445 in_samples, in_leftover, out_samples, c->buffer->nb_samples);
446
447 dst->nb_samples = out_samples;
448 return 0;
449}
450
451int avresample_get_delay(AVAudioResampleContext *avr)
452{
453 if (!avr->resample_needed || !avr->resample)
454 return 0;
455
456 return avr->resample->buffer->nb_samples;
457}