From 922cd8aa2dc18fb9b62623d22ac989bfd552db02 Mon Sep 17 00:00:00 2001 From: smorlat Date: Thu, 9 Jul 2009 11:19:36 +0000 Subject: [PATCH] simplify resample (possibly fix bug with 22050Hz input rate) make it more dynamic git-svn-id: svn+ssh://svn.savannah.nongnu.org/linphone/trunk@522 3f6dc0c8-ddfe-455d-9043-3cd528dc4637 --- linphone/mediastreamer2/src/msresample.c | 104 +++++++++++++++++++---- 1 file changed, 86 insertions(+), 18 deletions(-) diff --git a/linphone/mediastreamer2/src/msresample.c b/linphone/mediastreamer2/src/msresample.c index 7f2f3d1bd..52f3d965d 100644 --- a/linphone/mediastreamer2/src/msresample.c +++ b/linphone/mediastreamer2/src/msresample.c @@ -1,3 +1,21 @@ +/* +mediastreamer2 library - modular sound and video processing and streaming +Copyright (C) 2006 Simon MORLAT (simon.morlat@linphone.org) + +This program 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. + +This program 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ #include "mediastreamer2/msfilter.h" @@ -31,6 +49,8 @@ static ResampleData * resample_data_new(){ } static void resample_data_destroy(ResampleData *obj){ + if (obj->handle!=NULL) + speex_resampler_destroy(obj->handle); ms_bufferizer_destroy(obj->bz); ms_free(obj); } @@ -44,20 +64,7 @@ static void resample_uninit(MSFilter *obj){ } -static void resample_preprocess(MSFilter *obj){ - ResampleData *dt=(ResampleData*)obj->data; - int err=0; - - dt->handle = speex_resampler_init(1, dt->input_rate, dt->output_rate, SPEEX_RESAMPLER_QUALITY_VOIP, &err); -} - -static void resample_postprocess(MSFilter *obj){ - ResampleData *dt=(ResampleData*)obj->data; - if (dt->handle!=NULL) - speex_resampler_destroy((SpeexResamplerState*)dt->handle); - dt->handle=NULL; -} - +#if 0 static void resample_process_ms2(MSFilter *obj){ ResampleData *dt=(ResampleData*)obj->data; MSBufferizer *bz=dt->bz; @@ -74,6 +81,19 @@ static void resample_process_ms2(MSFilter *obj){ } return; } + if (dt->handle!=NULL){ + unsigned int inrate=0, outrate=0; + speex_resampler_get_rate(dt->handle,&inrate,&outrate); + if (inrate!=dt->input_rate || outrate!=dt->output_rate){ + speex_resampler_destroy(dt->handle); + dt->handle=0; + } + } + if (dt->handle==NULL){ + int err=0; + dt->handle=speex_resampler_init(1, dt->input_rate, dt->output_rate, SPEEX_RESAMPLER_QUALITY_VOIP, &err); + } + if (dt->input_rateoutput_rate) size_of_input=320*dt->input_rate/8000; @@ -122,6 +142,58 @@ static void resample_process_ms2(MSFilter *obj){ } } +#else +static void resample_process_ms2(MSFilter *obj){ + ResampleData *dt=(ResampleData*)obj->data; + mblk_t *m; + + if (dt->output_rate==dt->input_rate){ + while((m=ms_queue_get(obj->inputs[0]))!=NULL){ + ms_queue_put(obj->outputs[0],m); + } + return; + } + + if (dt->handle!=NULL){ + unsigned int inrate=0, outrate=0; + speex_resampler_get_rate(dt->handle,&inrate,&outrate); + if (inrate!=dt->input_rate || outrate!=dt->output_rate){ + speex_resampler_destroy(dt->handle); + dt->handle=0; + } + } + if (dt->handle==NULL){ + int err=0; + dt->handle=speex_resampler_init(1, dt->input_rate, dt->output_rate, SPEEX_RESAMPLER_QUALITY_VOIP, &err); + } + + + while((m=ms_queue_get(obj->inputs[0]))!=NULL){ + unsigned int inlen=(m->b_wptr-m->b_rptr)/2; + unsigned int outlen=((inlen*dt->output_rate)/dt->input_rate)+1; + unsigned int inlen_orig=inlen; + mblk_t *om=allocb(outlen*2,0); + speex_resampler_process_int(dt->handle, + 0, + (int16_t*)m->b_rptr, + &inlen, + (int16_t*)om->b_wptr, + &outlen); + if (inlen_orig!=inlen){ + ms_error("Bug in resampler ! only %u samples consumed instead of %u, out=%u", + inlen,inlen_orig,outlen); + } + om->b_wptr+=outlen*2; + mblk_set_timestamp_info(om,dt->ts); + dt->ts+=outlen; + ms_queue_put(obj->outputs[0],om); + } +} + +#endif + + + int ms_resample_set_sr(MSFilter *obj, void *arg){ ResampleData *dt=(ResampleData*)obj->data; dt->input_rate=((int*)arg)[0]; @@ -151,9 +223,7 @@ MSFilterDesc ms_resample_desc={ 1, 1, resample_init, - resample_preprocess, resample_process_ms2, - resample_postprocess, resample_uninit, enc_methods }; @@ -168,9 +238,7 @@ MSFilterDesc ms_resample_desc={ .ninputs=1, .noutputs=1, .init=resample_init, - .preprocess=resample_preprocess, .process=resample_process_ms2, - .postprocess=resample_postprocess, .uninit=resample_uninit, .methods=enc_methods };