cache: read up to 64 KiB at once from stream_file
[mplayer.git] / libaf / af_scaletempo.c
blob4ea25bf9e361eeb8e1df81f709d57eeac4ea9a30
1 /*
2 * scaletempo audio filter
4 * scale tempo while maintaining pitch
5 * (WSOLA technique with cross correlation)
6 * inspired by SoundTouch library by Olli Parviainen
8 * basic algorithm
9 * - produce 'stride' output samples per loop
10 * - consume stride*scale input samples per loop
12 * to produce smoother transitions between strides, blend next overlap
13 * samples from last stride with correlated samples of current input
15 * Copyright (c) 2007 Robert Juliano
17 * This file is part of MPlayer.
19 * MPlayer is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
24 * MPlayer is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License along
30 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
31 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
34 #include <stdlib.h>
35 #include <string.h>
36 #include <limits.h>
38 #include "af.h"
39 #include "libavutil/common.h"
40 #include "subopt-helper.h"
42 // Data for specific instances of this filter
43 typedef struct af_scaletempo_s
45 // stride
46 float scale;
47 float speed;
48 float frames_stride_scaled;
49 float frames_stride_error;
50 int bytes_per_frame;
51 int bytes_stride;
52 float bytes_stride_scaled;
53 int bytes_queue;
54 int bytes_queued;
55 int bytes_to_slide;
56 int8_t* buf_queue;
57 // overlap
58 int samples_overlap;
59 int samples_standing;
60 int bytes_overlap;
61 int bytes_standing;
62 void* buf_overlap;
63 void* table_blend;
64 void (*output_overlap)(struct af_scaletempo_s* s, void* out_buf, int bytes_off);
65 // best overlap
66 int frames_search;
67 int num_channels;
68 void* buf_pre_corr;
69 void* table_window;
70 int (*best_overlap_offset)(struct af_scaletempo_s* s);
71 // command line
72 float scale_nominal;
73 float ms_stride;
74 float percent_overlap;
75 float ms_search;
76 short speed_tempo;
77 short speed_pitch;
78 } af_scaletempo_t;
80 static int fill_queue(struct af_instance_s* af, af_data_t* data, int offset)
82 af_scaletempo_t* s = af->setup;
83 int bytes_in = data->len - offset;
84 int offset_unchanged = offset;
86 if (s->bytes_to_slide > 0) {
87 if (s->bytes_to_slide < s->bytes_queued) {
88 int bytes_move = s->bytes_queued - s->bytes_to_slide;
89 memmove(s->buf_queue,
90 s->buf_queue + s->bytes_to_slide,
91 bytes_move);
92 s->bytes_to_slide = 0;
93 s->bytes_queued = bytes_move;
94 } else {
95 int bytes_skip;
96 s->bytes_to_slide -= s->bytes_queued;
97 bytes_skip = FFMIN(s->bytes_to_slide, bytes_in);
98 s->bytes_queued = 0;
99 s->bytes_to_slide -= bytes_skip;
100 offset += bytes_skip;
101 bytes_in -= bytes_skip;
105 if (bytes_in > 0) {
106 int bytes_copy = FFMIN(s->bytes_queue - s->bytes_queued, bytes_in);
107 memcpy(s->buf_queue + s->bytes_queued,
108 (int8_t*)data->audio + offset,
109 bytes_copy);
110 s->bytes_queued += bytes_copy;
111 offset += bytes_copy;
114 return offset - offset_unchanged;
117 #define UNROLL_PADDING (4*4)
119 static int best_overlap_offset_float(af_scaletempo_t* s)
121 float *pw, *po, *ppc, *search_start;
122 float best_corr = INT_MIN;
123 int best_off = 0;
124 int i, off;
126 pw = s->table_window;
127 po = s->buf_overlap;
128 po += s->num_channels;
129 ppc = s->buf_pre_corr;
130 for (i=s->num_channels; i<s->samples_overlap; i++) {
131 *ppc++ = *pw++ * *po++;
134 search_start = (float*)s->buf_queue + s->num_channels;
135 for (off=0; off<s->frames_search; off++) {
136 float corr = 0;
137 float* ps = search_start;
138 ppc = s->buf_pre_corr;
139 for (i=s->num_channels; i<s->samples_overlap; i++) {
140 corr += *ppc++ * *ps++;
142 if (corr > best_corr) {
143 best_corr = corr;
144 best_off = off;
146 search_start += s->num_channels;
149 return best_off * 4 * s->num_channels;
152 static int best_overlap_offset_s16(af_scaletempo_t* s)
154 int32_t *pw, *ppc;
155 int16_t *po, *search_start;
156 int64_t best_corr = INT64_MIN;
157 int best_off = 0;
158 int off;
159 long i;
161 pw = s->table_window;
162 po = s->buf_overlap;
163 po += s->num_channels;
164 ppc = s->buf_pre_corr;
165 for (i=s->num_channels; i<s->samples_overlap; i++) {
166 *ppc++ = ( *pw++ * *po++ ) >> 15;
169 search_start = (int16_t*)s->buf_queue + s->num_channels;
170 for (off=0; off<s->frames_search; off++) {
171 int64_t corr = 0;
172 int16_t* ps = search_start;
173 ppc = s->buf_pre_corr;
174 ppc += s->samples_overlap - s->num_channels;
175 ps += s->samples_overlap - s->num_channels;
176 i = -(s->samples_overlap - s->num_channels);
177 do {
178 corr += ppc[i+0] * ps[i+0];
179 corr += ppc[i+1] * ps[i+1];
180 corr += ppc[i+2] * ps[i+2];
181 corr += ppc[i+3] * ps[i+3];
182 i += 4;
183 } while (i < 0);
184 if (corr > best_corr) {
185 best_corr = corr;
186 best_off = off;
188 search_start += s->num_channels;
191 return best_off * 2 * s->num_channels;
194 static void output_overlap_float(af_scaletempo_t* s, void* buf_out,
195 int bytes_off)
197 float* pout = buf_out;
198 float* pb = s->table_blend;
199 float* po = s->buf_overlap;
200 float* pin = (float*)(s->buf_queue + bytes_off);
201 int i;
202 for (i=0; i<s->samples_overlap; i++) {
203 *pout++ = *po - *pb++ * ( *po - *pin++ ); po++;
206 static void output_overlap_s16(af_scaletempo_t* s, void* buf_out,
207 int bytes_off)
209 int16_t* pout = buf_out;
210 int32_t* pb = s->table_blend;
211 int16_t* po = s->buf_overlap;
212 int16_t* pin = (int16_t*)(s->buf_queue + bytes_off);
213 int i;
214 for (i=0; i<s->samples_overlap; i++) {
215 *pout++ = *po - ( ( *pb++ * ( *po - *pin++ ) ) >> 16 ); po++;
219 // Filter data through filter
220 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
222 af_scaletempo_t* s = af->setup;
223 int offset_in;
224 int max_bytes_out;
225 int8_t* pout;
227 if (s->scale == 1.0) {
228 af->delay = 0;
229 return data;
232 // RESIZE_LOCAL_BUFFER - can't use macro
233 max_bytes_out = ((int)(data->len / s->bytes_stride_scaled) + 1) * s->bytes_stride;
234 if (max_bytes_out > af->data->len) {
235 mp_msg(MSGT_AFILTER, MSGL_V, "[libaf] Reallocating memory in module %s, "
236 "old len = %i, new len = %i\n",af->info->name,af->data->len,max_bytes_out);
237 af->data->audio = realloc(af->data->audio, max_bytes_out);
238 if (!af->data->audio) {
239 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[libaf] Could not allocate memory\n");
240 return NULL;
242 af->data->len = max_bytes_out;
245 offset_in = fill_queue(af, data, 0);
246 pout = af->data->audio;
247 while (s->bytes_queued >= s->bytes_queue) {
248 int ti;
249 float tf;
250 int bytes_off = 0;
252 // output stride
253 if (s->output_overlap) {
254 if (s->best_overlap_offset)
255 bytes_off = s->best_overlap_offset(s);
256 s->output_overlap(s, pout, bytes_off);
258 memcpy(pout + s->bytes_overlap,
259 s->buf_queue + bytes_off + s->bytes_overlap,
260 s->bytes_standing);
261 pout += s->bytes_stride;
263 // input stride
264 memcpy(s->buf_overlap,
265 s->buf_queue + bytes_off + s->bytes_stride,
266 s->bytes_overlap);
267 tf = s->frames_stride_scaled + s->frames_stride_error;
268 ti = (int)tf;
269 s->frames_stride_error = tf - ti;
270 s->bytes_to_slide = ti * s->bytes_per_frame;
272 offset_in += fill_queue(af, data, offset_in);
275 // This filter can have a negative delay when scale > 1:
276 // output corresponding to some length of input can be decided and written
277 // after receiving only a part of that input.
278 af->delay = s->bytes_queued - s->bytes_to_slide;
280 data->audio = af->data->audio;
281 data->len = pout - (int8_t *)af->data->audio;
282 return data;
285 // Initialization and runtime control
286 static int control(struct af_instance_s* af, int cmd, void* arg)
288 af_scaletempo_t* s = af->setup;
289 switch(cmd){
290 case AF_CONTROL_REINIT:{
291 af_data_t* data = (af_data_t*)arg;
292 float srate = data->rate / 1000;
293 int nch = data->nch;
294 int bps;
295 int use_int = 0;
296 int frames_stride, frames_overlap;
297 int i, j;
299 mp_msg(MSGT_AFILTER, MSGL_V,
300 "[scaletempo] %.3f speed * %.3f scale_nominal = %.3f\n",
301 s->speed, s->scale_nominal, s->scale);
303 if (s->scale == 1.0) {
304 if (s->speed_tempo && s->speed_pitch)
305 return AF_DETACH;
306 memcpy(af->data, data, sizeof(af_data_t));
307 return af_test_output(af, data);
310 af->data->rate = data->rate;
311 af->data->nch = data->nch;
312 if ( data->format == AF_FORMAT_S16_LE
313 || data->format == AF_FORMAT_S16_BE ) {
314 use_int = 1;
315 af->data->format = AF_FORMAT_S16_NE;
316 af->data->bps = bps = 2;
317 } else {
318 af->data->format = AF_FORMAT_FLOAT_NE;
319 af->data->bps = bps = 4;
322 frames_stride = srate * s->ms_stride;
323 s->bytes_stride = frames_stride * bps * nch;
324 s->bytes_stride_scaled = s->scale * s->bytes_stride;
325 s->frames_stride_scaled = s->scale * frames_stride;
326 s->frames_stride_error = 0;
327 af->mul = (double)s->bytes_stride / s->bytes_stride_scaled;
329 frames_overlap = frames_stride * s->percent_overlap;
330 if (frames_overlap <= 0) {
331 s->bytes_standing = s->bytes_stride;
332 s->samples_standing = s->bytes_standing / bps;
333 s->output_overlap = NULL;
334 } else {
335 s->samples_overlap = frames_overlap * nch;
336 s->bytes_overlap = frames_overlap * nch * bps;
337 s->bytes_standing = s->bytes_stride - s->bytes_overlap;
338 s->samples_standing = s->bytes_standing / bps;
339 s->buf_overlap = realloc(s->buf_overlap, s->bytes_overlap);
340 s->table_blend = realloc(s->table_blend, s->bytes_overlap * 4);
341 if(!s->buf_overlap || !s->table_blend) {
342 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[scaletempo] Out of memory\n");
343 return AF_ERROR;
345 memset(s->buf_overlap, 0, s->bytes_overlap);
346 if (use_int) {
347 int32_t* pb = s->table_blend;
348 int64_t blend = 0;
349 for (i=0; i<frames_overlap; i++) {
350 int32_t v = blend / frames_overlap;
351 for (j=0; j<nch; j++) {
352 *pb++ = v;
354 blend += 65536; // 2^16
356 s->output_overlap = output_overlap_s16;
357 } else {
358 float* pb = s->table_blend;
359 for (i=0; i<frames_overlap; i++) {
360 float v = i / (float)frames_overlap;
361 for (j=0; j<nch; j++) {
362 *pb++ = v;
365 s->output_overlap = output_overlap_float;
369 s->frames_search = (frames_overlap > 1) ? srate * s->ms_search : 0;
370 if (s->frames_search <= 0) {
371 s->best_overlap_offset = NULL;
372 } else {
373 if (use_int) {
374 int64_t t = frames_overlap;
375 int32_t n = 8589934588LL / (t * t); // 4 * (2^31 - 1) / t^2
376 int32_t* pw;
377 s->buf_pre_corr = realloc(s->buf_pre_corr, s->bytes_overlap * 2 + UNROLL_PADDING);
378 s->table_window = realloc(s->table_window, s->bytes_overlap * 2 - nch * bps * 2);
379 if(!s->buf_pre_corr || !s->table_window) {
380 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[scaletempo] Out of memory\n");
381 return AF_ERROR;
383 memset((char *)s->buf_pre_corr + s->bytes_overlap * 2, 0, UNROLL_PADDING);
384 pw = s->table_window;
385 for (i=1; i<frames_overlap; i++) {
386 int32_t v = ( i * (t - i) * n ) >> 15;
387 for (j=0; j<nch; j++) {
388 *pw++ = v;
391 s->best_overlap_offset = best_overlap_offset_s16;
392 } else {
393 float* pw;
394 s->buf_pre_corr = realloc(s->buf_pre_corr, s->bytes_overlap);
395 s->table_window = realloc(s->table_window, s->bytes_overlap - nch * bps);
396 if(!s->buf_pre_corr || !s->table_window) {
397 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[scaletempo] Out of memory\n");
398 return AF_ERROR;
400 pw = s->table_window;
401 for (i=1; i<frames_overlap; i++) {
402 float v = i * (frames_overlap - i);
403 for (j=0; j<nch; j++) {
404 *pw++ = v;
407 s->best_overlap_offset = best_overlap_offset_float;
411 s->bytes_per_frame = bps * nch;
412 s->num_channels = nch;
414 s->bytes_queue
415 = (s->frames_search + frames_stride + frames_overlap) * bps * nch;
416 s->buf_queue = realloc(s->buf_queue, s->bytes_queue + UNROLL_PADDING);
417 if(!s->buf_queue) {
418 mp_msg(MSGT_AFILTER, MSGL_FATAL, "[scaletempo] Out of memory\n");
419 return AF_ERROR;
422 mp_msg (MSGT_AFILTER, MSGL_DBG2, "[scaletempo] "
423 "%.2f stride_in, %i stride_out, %i standing, "
424 "%i overlap, %i search, %i queue, %s mode\n",
425 s->frames_stride_scaled,
426 (int)(s->bytes_stride / nch / bps),
427 (int)(s->bytes_standing / nch / bps),
428 (int)(s->bytes_overlap / nch / bps),
429 s->frames_search,
430 (int)(s->bytes_queue / nch / bps),
431 (use_int?"s16":"float"));
433 return af_test_output(af, (af_data_t*)arg);
435 case AF_CONTROL_PLAYBACK_SPEED | AF_CONTROL_SET:{
436 if (s->speed_tempo) {
437 if (s->speed_pitch) {
438 break;
440 s->speed = *(float*)arg;
441 s->scale = s->speed * s->scale_nominal;
442 } else {
443 if (s->speed_pitch) {
444 s->speed = 1 / *(float*)arg;
445 s->scale = s->speed * s->scale_nominal;
446 break;
449 return AF_OK;
451 case AF_CONTROL_SCALETEMPO_AMOUNT | AF_CONTROL_SET:{
452 s->scale = *(float*)arg;
453 s->scale = s->speed * s->scale_nominal;
454 return AF_OK;
456 case AF_CONTROL_SCALETEMPO_AMOUNT | AF_CONTROL_GET:
457 *(float*)arg = s->scale;
458 return AF_OK;
459 case AF_CONTROL_COMMAND_LINE:{
460 strarg_t speed = {};
461 opt_t subopts[] = {
462 {"scale", OPT_ARG_FLOAT, &s->scale_nominal, NULL},
463 {"stride", OPT_ARG_FLOAT, &s->ms_stride, NULL},
464 {"overlap", OPT_ARG_FLOAT, &s->percent_overlap, NULL},
465 {"search", OPT_ARG_FLOAT, &s->ms_search, NULL},
466 {"speed", OPT_ARG_STR, &speed, NULL},
467 {NULL},
469 if (subopt_parse(arg, subopts) != 0) {
470 return AF_ERROR;
472 if (s->scale_nominal <= 0) {
473 mp_msg(MSGT_AFILTER, MSGL_ERR, "[scaletempo] %s: %s: scale > 0\n",
474 mp_gtext("error parsing command line"),
475 mp_gtext("value out of range"));
476 return AF_ERROR;
478 if (s->ms_stride <= 0) {
479 mp_msg(MSGT_AFILTER, MSGL_ERR, "[scaletempo] %s: %s: stride > 0\n",
480 mp_gtext("error parsing command line"),
481 mp_gtext("value out of range"));
482 return AF_ERROR;
484 if (s->percent_overlap < 0 || s->percent_overlap > 1) {
485 mp_msg(MSGT_AFILTER, MSGL_ERR,
486 "[scaletempo] %s: %s: 0 <= overlap <= 1\n",
487 mp_gtext("error parsing command line"),
488 mp_gtext("value out of range"));
489 return AF_ERROR;
491 if (s->ms_search < 0) {
492 mp_msg(MSGT_AFILTER, MSGL_ERR, "[scaletempo] %s: %s: search >= 0\n",
493 mp_gtext("error parsing command line"),
494 mp_gtext("value out of range"));
495 return AF_ERROR;
497 if (speed.len > 0) {
498 if (strcmp(speed.str, "pitch") == 0) {
499 s->speed_tempo = 0;
500 s->speed_pitch = 1;
501 } else if (strcmp(speed.str, "tempo") == 0) {
502 s->speed_tempo = 1;
503 s->speed_pitch = 0;
504 } else if (strcmp(speed.str, "none") == 0) {
505 s->speed_tempo = 0;
506 s->speed_pitch = 0;
507 } else if (strcmp(speed.str, "both") == 0) {
508 s->speed_tempo = 1;
509 s->speed_pitch = 1;
510 } else {
511 mp_msg(MSGT_AFILTER, MSGL_ERR,
512 "[scaletempo] %s: %s: speed=[pitch|tempo|none|both]\n",
513 mp_gtext("error parsing command line"),
514 mp_gtext("value out of range"));
515 return AF_ERROR;
518 s->scale = s->speed * s->scale_nominal;
519 mp_msg(MSGT_AFILTER, MSGL_DBG2, "[scaletempo] %6.3f scale, %6.2f stride, %6.2f overlap, %6.2f search, speed = %s\n", s->scale_nominal, s->ms_stride, s->percent_overlap, s->ms_search, (s->speed_tempo?(s->speed_pitch?"tempo and speed":"tempo"):(s->speed_pitch?"pitch":"none")));
520 return AF_OK;
523 return AF_UNKNOWN;
526 // Deallocate memory
527 static void uninit(struct af_instance_s* af)
529 af_scaletempo_t* s = af->setup;
530 free(af->data->audio);
531 free(af->data);
532 free(s->buf_queue);
533 free(s->buf_overlap);
534 free(s->buf_pre_corr);
535 free(s->table_blend);
536 free(s->table_window);
537 free(af->setup);
540 // Allocate memory and set function pointers
541 static int af_open(af_instance_t* af){
542 af_scaletempo_t* s;
544 af->control = control;
545 af->uninit = uninit;
546 af->play = play;
547 af->mul = 1;
548 af->data = calloc(1,sizeof(af_data_t));
549 af->setup = calloc(1,sizeof(af_scaletempo_t));
550 if(af->data == NULL || af->setup == NULL)
551 return AF_ERROR;
553 s = af->setup;
554 s->scale = s->speed = s->scale_nominal = 1.0;
555 s->speed_tempo = 1;
556 s->speed_pitch = 0;
557 s->ms_stride = 60;
558 s->percent_overlap = .20;
559 s->ms_search = 14;
561 return AF_OK;
564 // Description of this filter
565 af_info_t af_info_scaletempo = {
566 "Scale audio tempo while maintaining pitch",
567 "scaletempo",
568 "Robert Juliano",
570 AF_FLAGS_REENTRANT,
571 af_open