2007-12-31 10:41:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
|
|
|
|
*
|
|
|
|
* This file is part of MPlayer.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
|
|
|
* MPlayer 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.
|
|
|
|
*
|
2008-05-14 17:24:10 +00:00
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with MPlayer; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-31 10:41:20 +00:00
|
|
|
*/
|
2004-10-21 03:32:31 +00:00
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
|
2005-11-13 18:28:44 +00:00
|
|
|
#include "config.h"
|
2004-10-21 03:32:31 +00:00
|
|
|
#include "af.h"
|
2008-02-25 15:46:36 +00:00
|
|
|
#include "libavcodec/avcodec.h"
|
|
|
|
#include "libavutil/rational.h"
|
2004-10-21 03:32:31 +00:00
|
|
|
|
|
|
|
// Data for specific instances of this filter
|
|
|
|
typedef struct af_resample_s{
|
|
|
|
struct AVResampleContext *avrctx;
|
2007-02-10 18:05:18 +00:00
|
|
|
int16_t *in[AF_NCH];
|
2004-10-21 03:32:31 +00:00
|
|
|
int in_alloc;
|
|
|
|
int index;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-10-21 03:32:31 +00:00
|
|
|
int filter_length;
|
|
|
|
int linear;
|
|
|
|
int phase_shift;
|
2004-10-21 21:15:21 +00:00
|
|
|
double cutoff;
|
2010-07-11 09:46:58 +00:00
|
|
|
|
|
|
|
int ctx_out_rate;
|
|
|
|
int ctx_in_rate;
|
|
|
|
int ctx_filter_size;
|
|
|
|
int ctx_phase_shift;
|
|
|
|
int ctx_linear;
|
|
|
|
double ctx_cutoff;
|
2004-10-21 03:32:31 +00:00
|
|
|
}af_resample_t;
|
|
|
|
|
|
|
|
|
|
|
|
// Initialization and runtime control
|
|
|
|
static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
|
|
{
|
2009-07-06 23:26:13 +00:00
|
|
|
af_resample_t* s = (af_resample_t*)af->setup;
|
2004-10-21 03:32:31 +00:00
|
|
|
af_data_t *data= (af_data_t*)arg;
|
2004-12-22 00:12:00 +00:00
|
|
|
int out_rate, test_output_res; // helpers for checking input format
|
2004-10-21 03:32:31 +00:00
|
|
|
|
|
|
|
switch(cmd){
|
|
|
|
case AF_CONTROL_REINIT:
|
|
|
|
if((af->data->rate == data->rate) || (af->data->rate == 0))
|
|
|
|
return AF_DETACH;
|
|
|
|
|
|
|
|
af->data->nch = data->nch;
|
2007-02-10 18:05:18 +00:00
|
|
|
if (af->data->nch > AF_NCH) af->data->nch = AF_NCH;
|
2004-12-27 17:30:15 +00:00
|
|
|
af->data->format = AF_FORMAT_S16_NE;
|
2004-10-21 03:32:31 +00:00
|
|
|
af->data->bps = 2;
|
2007-11-01 06:52:01 +00:00
|
|
|
af->mul = (double)af->data->rate / data->rate;
|
2007-11-01 06:52:50 +00:00
|
|
|
af->delay = af->data->nch * s->filter_length / min(af->mul, 1); // *bps*.5
|
2004-10-21 03:32:31 +00:00
|
|
|
|
2010-07-11 09:46:58 +00:00
|
|
|
if (s->ctx_out_rate != af->data->rate || s->ctx_in_rate != data->rate || s->ctx_filter_size != s->filter_length ||
|
|
|
|
s->ctx_phase_shift != s->phase_shift || s->ctx_linear != s->linear || s->ctx_cutoff != s->cutoff) {
|
|
|
|
if(s->avrctx) av_resample_close(s->avrctx);
|
|
|
|
s->avrctx= av_resample_init(af->data->rate, /*in_rate*/data->rate, s->filter_length, s->phase_shift, s->linear, s->cutoff);
|
|
|
|
s->ctx_out_rate = af->data->rate;
|
|
|
|
s->ctx_in_rate = data->rate;
|
|
|
|
s->ctx_filter_size = s->filter_length;
|
|
|
|
s->ctx_phase_shift = s->phase_shift;
|
|
|
|
s->ctx_linear = s->linear;
|
|
|
|
s->ctx_cutoff = s->cutoff;
|
|
|
|
}
|
2004-10-21 03:32:31 +00:00
|
|
|
|
2004-12-22 00:12:00 +00:00
|
|
|
// hack to make af_test_output ignore the samplerate change
|
|
|
|
out_rate = af->data->rate;
|
|
|
|
af->data->rate = data->rate;
|
|
|
|
test_output_res = af_test_output(af, (af_data_t*)arg);
|
|
|
|
af->data->rate = out_rate;
|
|
|
|
return test_output_res;
|
2004-10-21 03:32:31 +00:00
|
|
|
case AF_CONTROL_COMMAND_LINE:{
|
2007-03-22 01:50:04 +00:00
|
|
|
s->cutoff= 0.0;
|
2004-10-21 21:15:21 +00:00
|
|
|
sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff);
|
2007-03-22 01:50:04 +00:00
|
|
|
if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80);
|
2004-10-21 03:32:31 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET:
|
|
|
|
af->data->rate = *(int*)arg;
|
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
return AF_UNKNOWN;
|
|
|
|
}
|
|
|
|
|
2009-07-06 23:26:13 +00:00
|
|
|
// Deallocate memory
|
2004-10-21 03:32:31 +00:00
|
|
|
static void uninit(struct af_instance_s* af)
|
|
|
|
{
|
|
|
|
if(af->data)
|
2007-02-10 18:12:03 +00:00
|
|
|
free(af->data->audio);
|
|
|
|
free(af->data);
|
2004-10-21 03:32:31 +00:00
|
|
|
if(af->setup){
|
2007-02-10 20:33:27 +00:00
|
|
|
int i;
|
2004-10-21 03:32:31 +00:00
|
|
|
af_resample_t *s = af->setup;
|
|
|
|
if(s->avrctx) av_resample_close(s->avrctx);
|
2007-02-10 20:32:56 +00:00
|
|
|
for (i=0; i < AF_NCH; i++)
|
|
|
|
free(s->in[i]);
|
2004-10-21 03:32:31 +00:00
|
|
|
free(s);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Filter data through filter
|
|
|
|
static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
2009-07-06 23:26:13 +00:00
|
|
|
{
|
2004-10-21 03:32:31 +00:00
|
|
|
af_resample_t *s = af->setup;
|
|
|
|
int i, j, consumed, ret;
|
|
|
|
int16_t *in = (int16_t*)data->audio;
|
|
|
|
int16_t *out;
|
|
|
|
int chans = data->nch;
|
|
|
|
int in_len = data->len/(2*chans);
|
2007-11-01 06:52:01 +00:00
|
|
|
int out_len = in_len * af->mul + 10;
|
2007-02-10 18:05:18 +00:00
|
|
|
int16_t tmp[AF_NCH][out_len];
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-10-21 03:32:31 +00:00
|
|
|
if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
|
|
|
|
return NULL;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-10-21 03:32:31 +00:00
|
|
|
out= (int16_t*)af->data->audio;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-10-21 21:15:21 +00:00
|
|
|
out_len= min(out_len, af->data->len/(2*chans));
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-10-21 03:32:31 +00:00
|
|
|
if(s->in_alloc < in_len + s->index){
|
|
|
|
s->in_alloc= in_len + s->index;
|
|
|
|
for(i=0; i<chans; i++){
|
2007-02-10 18:12:03 +00:00
|
|
|
s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t));
|
2004-10-21 03:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-12-31 13:14:01 +00:00
|
|
|
if(chans==1){
|
|
|
|
memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t));
|
|
|
|
}else if(chans==2){
|
|
|
|
for(j=0; j<in_len; j++){
|
|
|
|
s->in[0][j + s->index]= *(in++);
|
|
|
|
s->in[1][j + s->index]= *(in++);
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(j=0; j<in_len; j++){
|
|
|
|
for(i=0; i<chans; i++){
|
|
|
|
s->in[i][j + s->index]= *(in++);
|
|
|
|
}
|
2004-10-21 03:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
in_len += s->index;
|
|
|
|
|
|
|
|
for(i=0; i<chans; i++){
|
|
|
|
ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans);
|
|
|
|
}
|
|
|
|
out_len= ret;
|
2009-07-06 23:26:13 +00:00
|
|
|
|
2004-10-21 03:32:31 +00:00
|
|
|
s->index= in_len - consumed;
|
|
|
|
for(i=0; i<chans; i++){
|
|
|
|
memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t));
|
|
|
|
}
|
|
|
|
|
2004-12-31 13:14:01 +00:00
|
|
|
if(chans==1){
|
|
|
|
memcpy(out, tmp[0], out_len*sizeof(int16_t));
|
|
|
|
}else if(chans==2){
|
|
|
|
for(j=0; j<out_len; j++){
|
|
|
|
*(out++)= tmp[0][j];
|
|
|
|
*(out++)= tmp[1][j];
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
for(j=0; j<out_len; j++){
|
|
|
|
for(i=0; i<chans; i++){
|
|
|
|
*(out++)= tmp[i][j];
|
|
|
|
}
|
2004-10-21 03:32:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
data->audio = af->data->audio;
|
|
|
|
data->len = out_len*chans*2;
|
|
|
|
data->rate = af->data->rate;
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2007-03-20 09:46:00 +00:00
|
|
|
static int af_open(af_instance_t* af){
|
2004-12-19 16:28:34 +00:00
|
|
|
af_resample_t *s = calloc(1,sizeof(af_resample_t));
|
2004-10-21 03:32:31 +00:00
|
|
|
af->control=control;
|
|
|
|
af->uninit=uninit;
|
|
|
|
af->play=play;
|
2007-11-01 06:52:01 +00:00
|
|
|
af->mul=1;
|
2004-10-21 03:32:31 +00:00
|
|
|
af->data=calloc(1,sizeof(af_data_t));
|
2004-12-19 16:28:34 +00:00
|
|
|
s->filter_length= 16;
|
2007-03-22 01:50:04 +00:00
|
|
|
s->cutoff= max(1.0 - 6.5/(s->filter_length+8), 0.80);
|
2004-12-19 16:28:34 +00:00
|
|
|
s->phase_shift= 10;
|
|
|
|
// s->setup = RSMP_INT | FREQ_SLOPPY;
|
|
|
|
af->setup=s;
|
2004-10-21 03:32:31 +00:00
|
|
|
return AF_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
af_info_t af_info_lavcresample = {
|
|
|
|
"Sample frequency conversion using libavcodec",
|
|
|
|
"lavcresample",
|
|
|
|
"Michael Niedermayer",
|
|
|
|
"",
|
|
|
|
AF_FLAGS_REENTRANT,
|
2007-03-20 09:46:00 +00:00
|
|
|
af_open
|
2004-10-21 03:32:31 +00:00
|
|
|
};
|