2 * scaletempo audio filter
4 * scale tempo while maintaining pitch
5 * (WSOLA technique with cross correlation)
6 * inspired by SoundTouch library by Olli Parviainen
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.
39 #include "libavutil/common.h"
40 #include "subopt-helper.h"
43 // Data for specific instances of this filter
44 typedef struct af_scaletempo_s
49 float frames_stride_scaled
;
50 float frames_stride_error
;
53 float bytes_stride_scaled
;
65 void (*output_overlap
)(struct af_scaletempo_s
* s
, void* out_buf
, int bytes_off
);
71 int (*best_overlap_offset
)(struct af_scaletempo_s
* s
);
75 float percent_overlap
;
81 static int fill_queue(struct af_instance_s
* af
, af_data_t
* data
, int offset
)
83 af_scaletempo_t
* s
= af
->setup
;
84 int bytes_in
= data
->len
- offset
;
85 int offset_unchanged
= offset
;
87 if (s
->bytes_to_slide
> 0) {
88 if (s
->bytes_to_slide
< s
->bytes_queued
) {
89 int bytes_move
= s
->bytes_queued
- s
->bytes_to_slide
;
91 s
->buf_queue
+ s
->bytes_to_slide
,
93 s
->bytes_to_slide
= 0;
94 s
->bytes_queued
= bytes_move
;
97 s
->bytes_to_slide
-= s
->bytes_queued
;
98 bytes_skip
= FFMIN(s
->bytes_to_slide
, bytes_in
);
100 s
->bytes_to_slide
-= bytes_skip
;
101 offset
+= bytes_skip
;
102 bytes_in
-= bytes_skip
;
107 int bytes_copy
= FFMIN(s
->bytes_queue
- s
->bytes_queued
, bytes_in
);
108 memcpy(s
->buf_queue
+ s
->bytes_queued
,
109 (int8_t*)data
->audio
+ offset
,
111 s
->bytes_queued
+= bytes_copy
;
112 offset
+= bytes_copy
;
115 return offset
- offset_unchanged
;
118 #define UNROLL_PADDING (4*4)
120 static int best_overlap_offset_float(af_scaletempo_t
* s
)
122 float *pw
, *po
, *ppc
, *search_start
;
123 float best_corr
= INT_MIN
;
127 pw
= s
->table_window
;
129 po
+= s
->num_channels
;
130 ppc
= s
->buf_pre_corr
;
131 for (i
=s
->num_channels
; i
<s
->samples_overlap
; i
++) {
132 *ppc
++ = *pw
++ * *po
++;
135 search_start
= (float*)s
->buf_queue
+ s
->num_channels
;
136 for (off
=0; off
<s
->frames_search
; off
++) {
138 float* ps
= search_start
;
139 ppc
= s
->buf_pre_corr
;
140 for (i
=s
->num_channels
; i
<s
->samples_overlap
; i
++) {
141 corr
+= *ppc
++ * *ps
++;
143 if (corr
> best_corr
) {
147 search_start
+= s
->num_channels
;
150 return best_off
* 4 * s
->num_channels
;
153 static int best_overlap_offset_s16(af_scaletempo_t
* s
)
156 int16_t *po
, *search_start
;
157 int64_t best_corr
= INT64_MIN
;
162 pw
= s
->table_window
;
164 po
+= s
->num_channels
;
165 ppc
= s
->buf_pre_corr
;
166 for (i
=s
->num_channels
; i
<s
->samples_overlap
; i
++) {
167 *ppc
++ = ( *pw
++ * *po
++ ) >> 15;
170 search_start
= (int16_t*)s
->buf_queue
+ s
->num_channels
;
171 for (off
=0; off
<s
->frames_search
; off
++) {
173 int16_t* ps
= search_start
;
174 ppc
= s
->buf_pre_corr
;
175 ppc
+= s
->samples_overlap
- s
->num_channels
;
176 ps
+= s
->samples_overlap
- s
->num_channels
;
177 i
= -(s
->samples_overlap
- s
->num_channels
);
179 corr
+= ppc
[i
+0] * ps
[i
+0];
180 corr
+= ppc
[i
+1] * ps
[i
+1];
181 corr
+= ppc
[i
+2] * ps
[i
+2];
182 corr
+= ppc
[i
+3] * ps
[i
+3];
185 if (corr
> best_corr
) {
189 search_start
+= s
->num_channels
;
192 return best_off
* 2 * s
->num_channels
;
195 static void output_overlap_float(af_scaletempo_t
* s
, void* buf_out
,
198 float* pout
= buf_out
;
199 float* pb
= s
->table_blend
;
200 float* po
= s
->buf_overlap
;
201 float* pin
= (float*)(s
->buf_queue
+ bytes_off
);
203 for (i
=0; i
<s
->samples_overlap
; i
++) {
204 *pout
++ = *po
- *pb
++ * ( *po
- *pin
++ ); po
++;
207 static void output_overlap_s16(af_scaletempo_t
* s
, void* buf_out
,
210 int16_t* pout
= buf_out
;
211 int32_t* pb
= s
->table_blend
;
212 int16_t* po
= s
->buf_overlap
;
213 int16_t* pin
= (int16_t*)(s
->buf_queue
+ bytes_off
);
215 for (i
=0; i
<s
->samples_overlap
; i
++) {
216 *pout
++ = *po
- ( ( *pb
++ * ( *po
- *pin
++ ) ) >> 16 ); po
++;
220 // Filter data through filter
221 static af_data_t
* play(struct af_instance_s
* af
, af_data_t
* data
)
223 af_scaletempo_t
* s
= af
->setup
;
228 if (s
->scale
== 1.0) {
233 // RESIZE_LOCAL_BUFFER - can't use macro
234 max_bytes_out
= ((int)(data
->len
/ s
->bytes_stride_scaled
) + 1) * s
->bytes_stride
;
235 if (max_bytes_out
> af
->data
->len
) {
236 mp_msg(MSGT_AFILTER
, MSGL_V
, "[libaf] Reallocating memory in module %s, "
237 "old len = %i, new len = %i\n",af
->info
->name
,af
->data
->len
,max_bytes_out
);
238 af
->data
->audio
= realloc(af
->data
->audio
, max_bytes_out
);
239 if (!af
->data
->audio
) {
240 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[libaf] Could not allocate memory\n");
243 af
->data
->len
= max_bytes_out
;
246 offset_in
= fill_queue(af
, data
, 0);
247 pout
= af
->data
->audio
;
248 while (s
->bytes_queued
>= s
->bytes_queue
) {
254 if (s
->output_overlap
) {
255 if (s
->best_overlap_offset
)
256 bytes_off
= s
->best_overlap_offset(s
);
257 s
->output_overlap(s
, pout
, bytes_off
);
259 memcpy(pout
+ s
->bytes_overlap
,
260 s
->buf_queue
+ bytes_off
+ s
->bytes_overlap
,
262 pout
+= s
->bytes_stride
;
265 memcpy(s
->buf_overlap
,
266 s
->buf_queue
+ bytes_off
+ s
->bytes_stride
,
268 tf
= s
->frames_stride_scaled
+ s
->frames_stride_error
;
270 s
->frames_stride_error
= tf
- ti
;
271 s
->bytes_to_slide
= ti
* s
->bytes_per_frame
;
273 offset_in
+= fill_queue(af
, data
, offset_in
);
276 // This filter can have a negative delay when scale > 1:
277 // output corresponding to some length of input can be decided and written
278 // after receiving only a part of that input.
279 af
->delay
= s
->bytes_queued
- s
->bytes_to_slide
;
281 data
->audio
= af
->data
->audio
;
282 data
->len
= pout
- (int8_t *)af
->data
->audio
;
286 // Initialization and runtime control
287 static int control(struct af_instance_s
* af
, int cmd
, void* arg
)
289 af_scaletempo_t
* s
= af
->setup
;
291 case AF_CONTROL_REINIT
:{
292 af_data_t
* data
= (af_data_t
*)arg
;
293 float srate
= data
->rate
/ 1000;
297 int frames_stride
, frames_overlap
;
300 mp_msg(MSGT_AFILTER
, MSGL_V
,
301 "[scaletempo] %.3f speed * %.3f scale_nominal = %.3f\n",
302 s
->speed
, s
->scale_nominal
, s
->scale
);
304 if (s
->scale
== 1.0) {
305 if (s
->speed_tempo
&& s
->speed_pitch
)
307 memcpy(af
->data
, data
, sizeof(af_data_t
));
308 return af_test_output(af
, data
);
311 af
->data
->rate
= data
->rate
;
312 af
->data
->nch
= data
->nch
;
313 if ( data
->format
== AF_FORMAT_S16_LE
314 || data
->format
== AF_FORMAT_S16_BE
) {
316 af
->data
->format
= AF_FORMAT_S16_NE
;
317 af
->data
->bps
= bps
= 2;
319 af
->data
->format
= AF_FORMAT_FLOAT_NE
;
320 af
->data
->bps
= bps
= 4;
323 frames_stride
= srate
* s
->ms_stride
;
324 s
->bytes_stride
= frames_stride
* bps
* nch
;
325 s
->bytes_stride_scaled
= s
->scale
* s
->bytes_stride
;
326 s
->frames_stride_scaled
= s
->scale
* frames_stride
;
327 s
->frames_stride_error
= 0;
328 af
->mul
= (double)s
->bytes_stride
/ s
->bytes_stride_scaled
;
330 frames_overlap
= frames_stride
* s
->percent_overlap
;
331 if (frames_overlap
<= 0) {
332 s
->bytes_standing
= s
->bytes_stride
;
333 s
->samples_standing
= s
->bytes_standing
/ bps
;
334 s
->output_overlap
= NULL
;
336 s
->samples_overlap
= frames_overlap
* nch
;
337 s
->bytes_overlap
= frames_overlap
* nch
* bps
;
338 s
->bytes_standing
= s
->bytes_stride
- s
->bytes_overlap
;
339 s
->samples_standing
= s
->bytes_standing
/ bps
;
340 s
->buf_overlap
= realloc(s
->buf_overlap
, s
->bytes_overlap
);
341 s
->table_blend
= realloc(s
->table_blend
, s
->bytes_overlap
* 4);
342 if(!s
->buf_overlap
|| !s
->table_blend
) {
343 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[scaletempo] Out of memory\n");
346 memset(s
->buf_overlap
, 0, s
->bytes_overlap
);
348 int32_t* pb
= s
->table_blend
;
350 for (i
=0; i
<frames_overlap
; i
++) {
351 int32_t v
= blend
/ frames_overlap
;
352 for (j
=0; j
<nch
; j
++) {
355 blend
+= 65536; // 2^16
357 s
->output_overlap
= output_overlap_s16
;
359 float* pb
= s
->table_blend
;
360 for (i
=0; i
<frames_overlap
; i
++) {
361 float v
= i
/ (float)frames_overlap
;
362 for (j
=0; j
<nch
; j
++) {
366 s
->output_overlap
= output_overlap_float
;
370 s
->frames_search
= (frames_overlap
> 1) ? srate
* s
->ms_search
: 0;
371 if (s
->frames_search
<= 0) {
372 s
->best_overlap_offset
= NULL
;
375 int64_t t
= frames_overlap
;
376 int32_t n
= 8589934588LL / (t
* t
); // 4 * (2^31 - 1) / t^2
378 s
->buf_pre_corr
= realloc(s
->buf_pre_corr
, s
->bytes_overlap
* 2 + UNROLL_PADDING
);
379 s
->table_window
= realloc(s
->table_window
, s
->bytes_overlap
* 2 - nch
* bps
* 2);
380 if(!s
->buf_pre_corr
|| !s
->table_window
) {
381 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[scaletempo] Out of memory\n");
384 memset((char *)s
->buf_pre_corr
+ s
->bytes_overlap
* 2, 0, UNROLL_PADDING
);
385 pw
= s
->table_window
;
386 for (i
=1; i
<frames_overlap
; i
++) {
387 int32_t v
= ( i
* (t
- i
) * n
) >> 15;
388 for (j
=0; j
<nch
; j
++) {
392 s
->best_overlap_offset
= best_overlap_offset_s16
;
395 s
->buf_pre_corr
= realloc(s
->buf_pre_corr
, s
->bytes_overlap
);
396 s
->table_window
= realloc(s
->table_window
, s
->bytes_overlap
- nch
* bps
);
397 if(!s
->buf_pre_corr
|| !s
->table_window
) {
398 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[scaletempo] Out of memory\n");
401 pw
= s
->table_window
;
402 for (i
=1; i
<frames_overlap
; i
++) {
403 float v
= i
* (frames_overlap
- i
);
404 for (j
=0; j
<nch
; j
++) {
408 s
->best_overlap_offset
= best_overlap_offset_float
;
412 s
->bytes_per_frame
= bps
* nch
;
413 s
->num_channels
= nch
;
416 = (s
->frames_search
+ frames_stride
+ frames_overlap
) * bps
* nch
;
417 s
->buf_queue
= realloc(s
->buf_queue
, s
->bytes_queue
+ UNROLL_PADDING
);
419 mp_msg(MSGT_AFILTER
, MSGL_FATAL
, "[scaletempo] Out of memory\n");
423 mp_msg (MSGT_AFILTER
, MSGL_DBG2
, "[scaletempo] "
424 "%.2f stride_in, %i stride_out, %i standing, "
425 "%i overlap, %i search, %i queue, %s mode\n",
426 s
->frames_stride_scaled
,
427 (int)(s
->bytes_stride
/ nch
/ bps
),
428 (int)(s
->bytes_standing
/ nch
/ bps
),
429 (int)(s
->bytes_overlap
/ nch
/ bps
),
431 (int)(s
->bytes_queue
/ nch
/ bps
),
432 (use_int
?"s16":"float"));
434 return af_test_output(af
, (af_data_t
*)arg
);
436 case AF_CONTROL_PLAYBACK_SPEED
| AF_CONTROL_SET
:{
437 if (s
->speed_tempo
) {
438 if (s
->speed_pitch
) {
441 s
->speed
= *(float*)arg
;
442 s
->scale
= s
->speed
* s
->scale_nominal
;
444 if (s
->speed_pitch
) {
445 s
->speed
= 1 / *(float*)arg
;
446 s
->scale
= s
->speed
* s
->scale_nominal
;
452 case AF_CONTROL_SCALETEMPO_AMOUNT
| AF_CONTROL_SET
:{
453 s
->scale
= *(float*)arg
;
454 s
->scale
= s
->speed
* s
->scale_nominal
;
457 case AF_CONTROL_SCALETEMPO_AMOUNT
| AF_CONTROL_GET
:
458 *(float*)arg
= s
->scale
;
460 case AF_CONTROL_COMMAND_LINE
:{
463 {"scale", OPT_ARG_FLOAT
, &s
->scale_nominal
, NULL
},
464 {"stride", OPT_ARG_FLOAT
, &s
->ms_stride
, NULL
},
465 {"overlap", OPT_ARG_FLOAT
, &s
->percent_overlap
, NULL
},
466 {"search", OPT_ARG_FLOAT
, &s
->ms_search
, NULL
},
467 {"speed", OPT_ARG_STR
, &speed
, NULL
},
470 if (subopt_parse(arg
, subopts
) != 0) {
473 if (s
->scale_nominal
<= 0) {
474 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "[scaletempo] "
475 _("error parsing command line") ": " _("value out of range")
479 if (s
->ms_stride
<= 0) {
480 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "[scaletempo] "
481 _("error parsing command line") ": " _("value out of range")
485 if (s
->percent_overlap
< 0 || s
->percent_overlap
> 1) {
486 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "[scaletempo] "
487 _("error parsing command line") ": " _("value out of range")
488 ": 0 <= overlap <= 1\n");
491 if (s
->ms_search
< 0) {
492 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "[scaletempo] "
493 _("error parsing command line") ": " _("value out of range")
498 if (strcmp(speed
.str
, "pitch") == 0) {
501 } else if (strcmp(speed
.str
, "tempo") == 0) {
504 } else if (strcmp(speed
.str
, "none") == 0) {
507 } else if (strcmp(speed
.str
, "both") == 0) {
511 mp_tmsg(MSGT_AFILTER
, MSGL_ERR
, "[scaletempo] "
512 _("error parsing command line") ": " _("value out of range")
513 ": speed=[pitch|tempo|none|both]\n");
517 s
->scale
= s
->speed
* s
->scale_nominal
;
518 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")));
526 static void uninit(struct af_instance_s
* af
)
528 af_scaletempo_t
* s
= af
->setup
;
529 free(af
->data
->audio
);
532 free(s
->buf_overlap
);
533 free(s
->buf_pre_corr
);
534 free(s
->table_blend
);
535 free(s
->table_window
);
539 // Allocate memory and set function pointers
540 static int af_open(af_instance_t
* af
){
543 af
->control
= control
;
547 af
->data
= calloc(1,sizeof(af_data_t
));
548 af
->setup
= calloc(1,sizeof(af_scaletempo_t
));
549 if(af
->data
== NULL
|| af
->setup
== NULL
)
553 s
->scale
= s
->speed
= s
->scale_nominal
= 1.0;
557 s
->percent_overlap
= .20;
563 // Description of this filter
564 af_info_t af_info_scaletempo
= {
565 "Scale audio tempo while maintaining pitch",