Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[81392c]: src / post / audio / stretch.c Maximize Restore History

Download this file

stretch.c    698 lines (526 with data), 20.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
/*
* Copyright (C) 2000-2004 the xine project
*
* This file is part of xine, a free video player.
*
* xine is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* xine is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110, USA
*
* Time stretch by a given factor, optionally preserving pitch
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include "xine_internal.h"
#include "xineutils.h"
#include "post.h"
#include "dsp.h"
#include "resample.h"
#include "audio_filters.h"
#define AUDIO_FRAGMENT 120/1000 /* ms of audio */
#define CLIP_INT16(s) ((s) < INT16_MIN) ? INT16_MIN : \
(((s) > INT16_MAX) ? INT16_MAX : (s))
/*
* ***************************************************
* stretchable unix System Clock Reference
* use stretch factor to calculate speed
* ***************************************************
*/
struct stretchscr_s {
scr_plugin_t scr;
struct timeval cur_time;
int64_t cur_pts;
int xine_speed;
double speed_factor;
double *stretch_factor;
pthread_mutex_t lock;
};
typedef struct stretchscr_s stretchscr_t;
static int stretchscr_get_priority (scr_plugin_t *scr) {
return 10; /* high priority */
}
/* Only call this when already mutex locked */
static void stretchscr_set_pivot (stretchscr_t *this) {
struct timeval tv;
int64_t pts;
double pts_calc;
xine_monotonic_clock(&tv, NULL);
pts_calc = (tv.tv_sec - this->cur_time.tv_sec) * this->speed_factor;
pts_calc += (tv.tv_usec - this->cur_time.tv_usec) * this->speed_factor / 1e6;
pts = this->cur_pts + pts_calc;
/* This next part introduces a one off inaccuracy
* to the scr due to rounding tv to pts.
*/
this->cur_time.tv_sec=tv.tv_sec;
this->cur_time.tv_usec=tv.tv_usec;
this->cur_pts=pts;
return ;
}
static int stretchscr_set_speed (scr_plugin_t *scr, int speed) {
stretchscr_t *this = (stretchscr_t*) scr;
pthread_mutex_lock (&this->lock);
stretchscr_set_pivot( this );
this->xine_speed = speed;
this->speed_factor = (double) speed * 90000.0 / XINE_FINE_SPEED_NORMAL /
(*this->stretch_factor);
pthread_mutex_unlock (&this->lock);
return speed;
}
static void stretchscr_adjust (scr_plugin_t *scr, int64_t vpts) {
stretchscr_t *this = (stretchscr_t*) scr;
struct timeval tv;
pthread_mutex_lock (&this->lock);
xine_monotonic_clock(&tv, NULL);
this->cur_time.tv_sec=tv.tv_sec;
this->cur_time.tv_usec=tv.tv_usec;
this->cur_pts = vpts;
pthread_mutex_unlock (&this->lock);
}
static void stretchscr_start (scr_plugin_t *scr, int64_t start_vpts) {
stretchscr_t *this = (stretchscr_t*) scr;
pthread_mutex_lock (&this->lock);
xine_monotonic_clock(&this->cur_time, NULL);
this->cur_pts = start_vpts;
pthread_mutex_unlock (&this->lock);
stretchscr_set_speed (&this->scr, XINE_FINE_SPEED_NORMAL);
}
static int64_t stretchscr_get_current (scr_plugin_t *scr) {
stretchscr_t *this = (stretchscr_t*) scr;
struct timeval tv;
int64_t pts;
double pts_calc;
pthread_mutex_lock (&this->lock);
xine_monotonic_clock(&tv, NULL);
pts_calc = (tv.tv_sec - this->cur_time.tv_sec) * this->speed_factor;
pts_calc += (tv.tv_usec - this->cur_time.tv_usec) * this->speed_factor / 1e6;
pts = this->cur_pts + pts_calc;
pthread_mutex_unlock (&this->lock);
return pts;
}
static void stretchscr_exit (scr_plugin_t *scr) {
stretchscr_t *this = (stretchscr_t*) scr;
pthread_mutex_destroy (&this->lock);
free(this);
}
static stretchscr_t *XINE_MALLOC stretchscr_init (double *stretch_factor) {
stretchscr_t *this;
this = calloc(1, sizeof(stretchscr_t));
this->scr.interface_version = 3;
this->scr.get_priority = stretchscr_get_priority;
this->scr.set_fine_speed = stretchscr_set_speed;
this->scr.adjust = stretchscr_adjust;
this->scr.start = stretchscr_start;
this->scr.get_current = stretchscr_get_current;
this->scr.exit = stretchscr_exit;
pthread_mutex_init (&this->lock, NULL);
this->stretch_factor = stretch_factor;
stretchscr_set_speed (&this->scr, XINE_SPEED_PAUSE);
return this;
}
/*****************************************************/
typedef struct post_plugin_stretch_s post_plugin_stretch_t;
typedef struct post_class_stretch_s post_class_stretch_t;
struct post_class_stretch_s {
post_class_t post_class;
xine_t *xine;
};
typedef struct stretch_parameters_s {
int preserve_pitch;
double factor;
} stretch_parameters_t;
/*
* description of params struct
*/
START_PARAM_DESCR( stretch_parameters_t )
PARAM_ITEM( POST_PARAM_TYPE_BOOL, preserve_pitch, NULL, 0, 1, 0,
"Preserve pitch" )
PARAM_ITEM( POST_PARAM_TYPE_DOUBLE, factor, NULL, 0.5, 1.5, 0,
"Time stretch factor (<1.0 shorten duration)" )
END_PARAM_DESCR( param_descr )
/* plugin structure */
struct post_plugin_stretch_s {
post_plugin_t post;
stretchscr_t* scr;
/* private data */
stretch_parameters_t params;
xine_post_in_t params_input;
int params_changed;
int channels;
int bytes_per_frame;
int16_t *audiofrag; /* audio fragment to work on */
int16_t *outfrag; /* processed audio fragment */
_ftype_t *w;
int frames_per_frag;
int frames_per_outfrag;
int num_frames; /* current # of frames on audiofrag */
int16_t last_sample[RESAMPLE_MAX_CHANNELS];
int64_t pts; /* pts for audiofrag */
pthread_mutex_t lock;
};
/**************************************************************************
* stretch parameters functions
*************************************************************************/
static int set_parameters (xine_post_t *this_gen, void *param_gen) {
post_plugin_stretch_t *this = (post_plugin_stretch_t *)this_gen;
stretch_parameters_t *param = (stretch_parameters_t *)param_gen;
pthread_mutex_lock (&this->lock);
memcpy( &this->params, param, sizeof(stretch_parameters_t) );
this->params_changed = 1;
pthread_mutex_unlock (&this->lock);
return 1;
}
static int get_parameters (xine_post_t *this_gen, void *param_gen) {
post_plugin_stretch_t *this = (post_plugin_stretch_t *)this_gen;
stretch_parameters_t *param = (stretch_parameters_t *)param_gen;
pthread_mutex_lock (&this->lock);
memcpy( param, &this->params, sizeof(stretch_parameters_t) );
pthread_mutex_unlock (&this->lock);
return 1;
}
static xine_post_api_descr_t * get_param_descr (void) {
return &param_descr;
}
static char * get_help (void) {
return _("This filter will perform a time stretch, playing the "
"stream faster or slower by a factor. Pitch is optionally "
"preserved, so it is possible, for example, to use it to "
"watch a movie in less time than it was originally shot.\n"
);
}
static xine_post_api_t post_api = {
set_parameters,
get_parameters,
get_param_descr,
get_help,
};
/**************************************************************************
* xine audio post plugin functions
*************************************************************************/
static int stretch_port_open(xine_audio_port_t *port_gen, xine_stream_t *stream,
uint32_t bits, uint32_t rate, int mode) {
post_audio_port_t *port = (post_audio_port_t *)port_gen;
post_plugin_stretch_t *this = (post_plugin_stretch_t *)port->post;
int64_t time;
_x_post_rewire(&this->post);
_x_post_inc_usage(port);
port->stream = stream;
port->bits = bits;
port->rate = rate;
port->mode = mode;
/* register our own scr provider */
time = port->stream->xine->clock->get_current_time(port->stream->xine->clock);
this->scr = stretchscr_init(&this->params.factor);
this->scr->scr.start(&this->scr->scr, time);
port->stream->xine->clock->register_scr(port->stream->xine->clock, &this->scr->scr);
/* force updating on stretch_port_put_buffer */
this->params_changed = 1;
return (port->original_port->open) (port->original_port, stream, bits, rate, mode);
}
static void stretch_port_close(xine_audio_port_t *port_gen, xine_stream_t *stream ) {
post_audio_port_t *port = (post_audio_port_t *)port_gen;
post_plugin_stretch_t *this = (post_plugin_stretch_t *)port->post;
if (this->scr) {
port->stream->xine->clock->unregister_scr(port->stream->xine->clock, &this->scr->scr);
this->scr->scr.exit(&this->scr->scr);
}
if(this->audiofrag) {
free(this->audiofrag);
this->audiofrag = NULL;
}
if(this->outfrag) {
free(this->outfrag);
this->outfrag = NULL;
}
if(this->w) {
free(this->w);
this->w = NULL;
}
port->stream = NULL;
port->original_port->close(port->original_port, stream );
_x_post_dec_usage(port);
}
static void stretch_process_fragment( post_audio_port_t *port,
xine_stream_t *stream, extra_info_t *extra_info )
{
post_plugin_stretch_t *this = (post_plugin_stretch_t *)port->post;
audio_buffer_t *outbuf;
int16_t *data_out = this->outfrag;
int num_frames_in = this->num_frames;
int num_frames_out = this->num_frames * this->frames_per_outfrag /
this->frames_per_frag;
if( !this->params.preserve_pitch ) {
if( this->channels == 2 )
_x_audio_out_resample_stereo(this->last_sample, this->audiofrag, num_frames_in,
this->outfrag, num_frames_out);
else if( this->channels == 1 )
_x_audio_out_resample_mono(this->last_sample, this->audiofrag, num_frames_in,
this->outfrag, num_frames_out);
} else {
if (this->channels == 2)
memcpy (this->last_sample, &this->audiofrag[(num_frames_in - 1) * 2], 2 * sizeof (this->last_sample[0]));
else if (this->channels == 1)
memcpy (this->last_sample, &this->audiofrag[num_frames_in - 1], sizeof (this->last_sample[0]));
if( num_frames_in > num_frames_out )
{
/*
* time compressing strategy
*
* input chunk has two halves, A and B.
* output chunk is composed as follow:
* - some frames copied directly from A
* - some frames copied from A merged with frames from B
* weighted by an increasing factor (0 -> 1.0)
* - frames from A weighted by a decreasing factor (1.0 -> 0)
* merged with frames copied from B
* - some frames copied directly from B
*/
int merge_frames = num_frames_in - num_frames_out;
int copy_frames;
int16_t *src = this->audiofrag;
int16_t *dst = this->outfrag;
int i, j;
if( merge_frames > num_frames_out )
merge_frames = num_frames_out;
copy_frames = num_frames_out - merge_frames;
memcpy(dst, src, copy_frames/2 * this->bytes_per_frame);
dst += copy_frames/2 * this->channels;
src += copy_frames/2 * this->channels;
for( i = 0; i < merge_frames/2; i++ )
{
for( j = 0; j < this->channels; j++, src++, dst++ ) {
int32_t s = (int32_t) ((_ftype_t) src[0] +
src[merge_frames * this->channels] * this->w[i]);
*dst = CLIP_INT16(s);
}
}
for( ; i < merge_frames; i++ )
{
for( j = 0; j < this->channels; j++, src++, dst++ ) {
int32_t s = (int32_t) ((_ftype_t) src[0] * this->w[i] +
src[merge_frames * this->channels]);
*dst = CLIP_INT16(s);
}
}
src += merge_frames * this->channels;
memcpy(dst, src, (copy_frames - copy_frames/2) *
this->bytes_per_frame);
} else {
/*
* time expansion strategy
*
* output chunk is composed of two versions of the
* input chunk:
* - first part copied directly from input, and then
* merged with the second (delayed) part using a
* decreasing factor (1.0 -> 0)
* - the delayed version of the input is merged with
* an increasing factor (0 -> 1.0) and then (when
* factor reaches 1.0) just copied until the end.
*/
int merge_frames = num_frames_out - num_frames_in;
int copy_frames = num_frames_out - merge_frames;
int16_t *src1 = this->audiofrag;
int16_t *src2;
int16_t *dst = this->outfrag;
int i, j;
memcpy(dst, src1, copy_frames/2 * this->bytes_per_frame);
dst += copy_frames/2 * this->channels;
src1 += copy_frames/2 * this->channels;
src2 = src1 - merge_frames * this->channels;
for( i = 0; i < merge_frames/2; i++ )
{
for( j = 0; j < this->channels; j++, src1++, src2++, dst++ ) {
int32_t s = (int32_t) ((_ftype_t) *src1 +
*src2 * this->w[i]);
*dst = CLIP_INT16(s);
}
}
for( ; i < merge_frames; i++ )
{
for( j = 0; j < this->channels; j++, src1++, src2++, dst++ ) {
int32_t s = (int32_t) ((_ftype_t) *src1 * this->w[i] +
*src2);
*dst = CLIP_INT16(s);
}
}
memcpy(dst, src2, (copy_frames - copy_frames/2) *
this->bytes_per_frame);
}
}
/* copy processed fragment into multiple audio buffers, if needed */
while( num_frames_out ) {
outbuf = port->original_port->get_buffer(port->original_port);
outbuf->num_frames = outbuf->mem_size / this->bytes_per_frame;
if( outbuf->num_frames > num_frames_out )
outbuf->num_frames = num_frames_out;
memcpy( outbuf->mem, data_out,
outbuf->num_frames * this->bytes_per_frame );
num_frames_out -= outbuf->num_frames;
data_out = (uint16_t *)((uint8_t *)data_out + outbuf->num_frames * this->bytes_per_frame);
outbuf->vpts = this->pts;
this->pts = 0;
outbuf->stream = stream;
outbuf->format.bits = port->bits;
outbuf->format.rate = port->rate;
outbuf->format.mode = port->mode;
_x_extra_info_merge( outbuf->extra_info, extra_info );
port->original_port->put_buffer(port->original_port, outbuf, stream );
}
this->num_frames = 0;
}
static void stretch_port_put_buffer (xine_audio_port_t *port_gen,
audio_buffer_t *buf, xine_stream_t *stream) {
post_audio_port_t *port = (post_audio_port_t *)port_gen;
post_plugin_stretch_t *this = (post_plugin_stretch_t *)port->post;
int16_t *data_in;
pthread_mutex_lock (&this->lock);
if( this->params_changed ) {
int64_t audio_step;
if( this->num_frames && this->audiofrag && this->outfrag ) {
/* output whatever we have before changing parameters */
stretch_process_fragment( port, stream, buf->extra_info );
}
this->channels = _x_ao_mode2channels(port->mode);
this->bytes_per_frame = port->bits / 8 * this->channels;
audio_step = ((int64_t)90000 * (int64_t)32768) / (int64_t)port->rate;
audio_step = (int64_t) ((double)audio_step / this->params.factor);
stream->metronom->set_audio_rate(stream->metronom, audio_step);
stretchscr_set_speed(&this->scr->scr, this->scr->xine_speed);
if(this->audiofrag) {
free(this->audiofrag);
this->audiofrag = NULL;
}
if(this->outfrag) {
free(this->outfrag);
this->outfrag = NULL;
}
if(this->w) {
free(this->w);
this->w = NULL;
}
this->frames_per_frag = port->rate * AUDIO_FRAGMENT;
this->frames_per_outfrag = (int) ((double)this->params.factor * this->frames_per_frag);
if( this->frames_per_frag != this->frames_per_outfrag ) {
int wsize;
this->audiofrag = malloc( this->frames_per_frag * this->bytes_per_frame );
this->outfrag = malloc( this->frames_per_outfrag * this->bytes_per_frame );
if( this->frames_per_frag > this->frames_per_outfrag )
wsize = this->frames_per_frag - this->frames_per_outfrag;
else
wsize = this->frames_per_outfrag - this->frames_per_frag;
this->w = (_ftype_t*) malloc( wsize * sizeof(_ftype_t) );
triang(wsize, this->w);
}
this->num_frames = 0;
this->pts = 0;
this->params_changed = 0;
}
pthread_mutex_unlock (&this->lock);
/* just pass data through if we have nothing to do */
if( this->frames_per_frag == this->frames_per_outfrag ||
/* FIXME: we only handle 1 or 2 channels, 16 bits for now */
(this->channels != 1 && this->channels != 2) ||
port->bits != 16 ) {
port->original_port->put_buffer(port->original_port, buf, stream );
return;
}
/* update pts for our current audio fragment */
if( buf->vpts )
this->pts = buf->vpts - (this->num_frames * 90000 / port->rate);
data_in = buf->mem;
while( buf->num_frames ) {
int frames_to_copy = this->frames_per_frag - this->num_frames;
if( frames_to_copy > buf->num_frames )
frames_to_copy = buf->num_frames;
/* copy up to one fragment from input buf to our buffer */
memcpy( (uint8_t *)this->audiofrag + this->num_frames * this->bytes_per_frame,
data_in, frames_to_copy * this->bytes_per_frame );
data_in = (uint16_t *)((uint8_t *)data_in + frames_to_copy * this->bytes_per_frame);
this->num_frames += frames_to_copy;
buf->num_frames -= frames_to_copy;
/* check if we have a complete audio fragment to process */
if( this->num_frames == this->frames_per_frag ) {
stretch_process_fragment( port, stream, buf->extra_info );
}
}
buf->num_frames=0; /* UNDOCUMENTED, but hey, it works! Force old audio_out buffer free. */
port->original_port->put_buffer(port->original_port, buf, stream );
return;
}
static void stretch_dispose(post_plugin_t *this_gen)
{
post_plugin_stretch_t *this = (post_plugin_stretch_t *)this_gen;
if (_x_post_dispose(this_gen)) {
free(this);
}
}
/* plugin class functions */
static post_plugin_t *stretch_open_plugin(post_class_t *class_gen, int inputs,
xine_audio_port_t **audio_target,
xine_video_port_t **video_target)
{
post_plugin_stretch_t *this = calloc(1, sizeof(post_plugin_stretch_t));
post_in_t *input;
post_out_t *output;
xine_post_in_t *input_api;
post_audio_port_t *port;
stretch_parameters_t init_params;
if (!this || !audio_target || !audio_target[0] ) {
free(this);
return NULL;
}
_x_post_init(&this->post, 1, 0);
init_params.preserve_pitch = 1;
init_params.factor = 0.80;
pthread_mutex_init (&this->lock, NULL);
set_parameters (&this->post.xine_post, &init_params);
port = _x_post_intercept_audio_port(&this->post, audio_target[0], &input, &output);
port->new_port.open = stretch_port_open;
port->new_port.close = stretch_port_close;
port->new_port.put_buffer = stretch_port_put_buffer;
input_api = &this->params_input;
input_api->name = "parameters";
input_api->type = XINE_POST_DATA_PARAMETERS;
input_api->data = &post_api;
xine_list_push_back(this->post.input, input_api);
this->post.xine_post.audio_input[0] = &port->new_port;
this->post.dispose = stretch_dispose;
return &this->post;
}
static char *stretch_get_identifier(post_class_t *class_gen)
{
return "stretch";
}
static char *stretch_get_description(post_class_t *class_gen)
{
return "Time stretch by a given factor, optionally preserving pitch";
}
static void stretch_class_dispose(post_class_t *class_gen)
{
free(class_gen);
}
/* plugin class initialization function */
void *stretch_init_plugin(xine_t *xine, void *data)
{
post_class_stretch_t *class = (post_class_stretch_t *)malloc(sizeof(post_class_stretch_t));
if (!class)
return NULL;
class->post_class.open_plugin = stretch_open_plugin;
class->post_class.get_identifier = stretch_get_identifier;
class->post_class.get_description = stretch_get_description;
class->post_class.dispose = stretch_class_dispose;
class->xine = xine;
return class;
}