From 2bf9664d0f4c29569f783cb68367f70c3b9a2280 Mon Sep 17 00:00:00 2001 From: Christian Hoene Date: Sat, 23 Apr 2016 18:31:50 +0200 Subject: Tested encoder and cleanup --- modules/mpa/decode.c | 71 +++++++++++++++++++++++++--------------------------- modules/mpa/encode.c | 52 ++++++++++++++++++++++---------------- modules/mpa/mpa.c | 19 ++++++++------ modules/mpa/sdp.c | 2 +- 4 files changed, 78 insertions(+), 66 deletions(-) (limited to 'modules') diff --git a/modules/mpa/decode.c b/modules/mpa/decode.c index 7d57563..65035e4 100644 --- a/modules/mpa/decode.c +++ b/modules/mpa/decode.c @@ -11,10 +11,12 @@ #include #include "mpa.h" +#undef DEBUG struct audec_state { mpg123_handle *dec; SpeexResamplerState *resampler; + int channels; }; @@ -24,8 +26,9 @@ static void destructor(void *arg) mpg123_close(ads->dec); mpg123_delete(ads->dec); - - warning("mpa: decoder destroyed\n"); +#ifdef DEBUG + debug("mpa: decoder destroyed\n"); +#endif } @@ -40,7 +43,9 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac, ads = *adsp; - warning("mpa: decoder created %s\n",fmtp); +#ifdef DEBUG + debug("mpa: decoder created %s\n",fmtp); +#endif if (ads) mem_deref(ads); @@ -48,15 +53,21 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac, ads = mem_zalloc(sizeof(*ads), destructor); if (!ads) return ENOMEM; + ads->channels = 0; + ads->resampler = NULL; ads->dec = mpg123_new(NULL,&mpaerr); if (!ads->dec) { - warning("mpa: decoder create: %s\n", mpg123_plain_strerror(mpaerr)); + error("mpa: decoder create: %s\n", mpg123_plain_strerror(mpaerr)); err = ENOMEM; goto out; } +#ifdef DEBUG mpaerr = mpg123_param(ads->dec, MPG123_VERBOSE, 4, 4.); +#else + mpaerr = mpg123_param(ads->dec, MPG123_VERBOSE, 0, 0.); +#endif if(mpaerr != MPG123_OK) { error("MPA libmpg123 param error %s", mpg123_plain_strerror(mpaerr)); err = EINVAL; @@ -92,7 +103,7 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac, int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc, const uint8_t *buf, size_t len) { - int mpaerr, channels, encoding; + int mpaerr, channels, encoding, i; long samplerate,res; size_t n; uint32_t header; @@ -111,10 +122,6 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc, return EPROTO; } - - - - if(ads->resampler) { in_len = *sampc; ds_len = 2304*2; @@ -122,32 +129,37 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc, ds_len = n / 4; /* ds_len counts samples per channel */ res=speex_resampler_process_interleaved_int(ads->resampler, ds, &ds_len, sampv, &in_len); if (res!=RESAMPLER_ERR_SUCCESS) { - warning("mpa: upsample error: %s %d %d\n", strerror(res), in_len, *sampc/2); + error("mpa: upsample error: %s %d %d\n", strerror(res), in_len, *sampc/2); return EPROTO; } - warning("mpa decode %d %d %d %d\n",ds_len,*sampc,in_len,n); +#ifdef DEBUG + debug("mpa decode %d %d %d %d\n",ds_len,*sampc,in_len,n); +#endif *sampc = in_len * 2; } else { mpaerr = mpg123_decode(ads->dec, buf+4, len-4, (unsigned char*)sampv, *sampc*2, &n); - warning("mpa decode %d %d\n",*sampc,n); +#ifdef DEBUG + debug("mpa decode %d %d\n",*sampc,n); +#endif *sampc = n / 2; } + if(ads->channels==1) { + for(i=*sampc-1;i>=0;i--) + sampv[i+i+1]=sampv[i+i]=sampv[i]; + *sampc *= 2; + } if(mpaerr == MPG123_NEW_FORMAT) { mpg123_getformat(ads->dec, &samplerate, &channels, &encoding); info("MPA libmpg123 format change %d %d %04X\n",samplerate,channels,encoding); - if(channels == 1) { - warning("mpa: resampler channel 1\n"); - ads->resampler = NULL; - return EINVAL; - } + ads->channels = channels; if(samplerate != 48000) { - ads->resampler = speex_resampler_init(2, samplerate, 48000, 3, &mpaerr); + ads->resampler = speex_resampler_init(channels, samplerate, 48000, 3, &mpaerr); if(mpaerr!=RESAMPLER_ERR_SUCCESS || ads->resampler==NULL) { - warning("mpa: upsampler init failed %d\n",mpaerr); + error("mpa: upsampler init failed %d\n",mpaerr); return EINVAL; } } @@ -162,24 +174,9 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc, return EPROTO; } -// warning("mpa decode %d %d %d\n",*sampc,len,n); - +#ifdef DEBUG + debug("mpa decode %d %d %d\n",*sampc,len,n); +#endif return 0; } -int mpa_decode_pkloss(struct audec_state *ads, int16_t *sampv, size_t *sampc) -{ - if (!ads || !sampv || !sampc) - return EINVAL; - - warning("mpa packet loss %d\n",*sampc); -// n = opus_decode(ads->dec, NULL, 0, sampv, (int)(*sampc/ads->ch), 0); -// if (n < 0) -// return EPROTO; - -// *sampc = n * ads->ch; - - return 0; -} - - diff --git a/modules/mpa/encode.c b/modules/mpa/encode.c index de75845..f9a7476 100644 --- a/modules/mpa/encode.c +++ b/modules/mpa/encode.c @@ -11,6 +11,7 @@ #include #include "mpa.h" +#undef DEBUG struct auenc_state { twolame_options *enc; @@ -25,9 +26,9 @@ static void destructor(void *arg) if (aes->enc) twolame_close(&aes->enc); - - warning("mpa: encoder destroyed\n"); - +#ifdef DEBUG + debug("mpa: encoder destroyed\n"); +#endif } int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac, @@ -47,20 +48,20 @@ int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac, if (aes) { mem_deref(aes); -// twolame_close(&aes->enc); } - aes = mem_zalloc(sizeof(*aes), destructor); - aes->enc = twolame_init(); - if (!aes->enc) { - warning("mpa: encoder create failed"); - mem_deref(aes); - return ENOMEM; - } - aes->channels = auc->ch; - *aesp = aes; - - warning("mpa: encoder created %s\n",fmtp); + aes = mem_zalloc(sizeof(*aes), destructor); + aes->enc = twolame_init(); + if (!aes->enc) { + error("mpa: encoder create failed"); + mem_deref(aes); + return ENOMEM; + } + aes->channels = auc->ch; + *aesp = aes; +#ifdef DEBUG + debug("mpa: encoder created %s\n",fmtp); +#endif prm.samplerate = 32000; prm.bitrate = 128000; @@ -69,7 +70,12 @@ int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac, mpa_decode_fmtp(&prm, fmtp); mpares = 0; +#ifdef DEBUG mpares |= twolame_set_verbosity(aes->enc, 5); +#else + mpares |= twolame_set_verbosity(aes->enc, 0); +#endif + mpares |= twolame_set_mode(aes->enc, prm.mode == SINGLE_CHANNEL ? TWOLAME_MONO : prm.mode == DUAL_CHANNEL ? TWOLAME_DUAL_CHANNEL : prm.mode == JOINT_STEREO ? TWOLAME_JOINT_STEREO : @@ -80,13 +86,13 @@ int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac, mpares |= twolame_set_out_samplerate(aes->enc, prm.samplerate); mpares |= twolame_set_num_channels(aes->enc, 2); if(mpares!=0) { - warning("mpa: encoder set failed\n"); + error("mpa: encoder set failed\n"); return EINVAL; } mpares = twolame_init_params(aes->enc); if(mpares!=0) { - warning("mpa: encoder init params failed\n"); + error("mpa: encoder init params failed\n"); return EINVAL; } @@ -96,7 +102,7 @@ int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac, if(prm.samplerate != 48000) { aes->resampler = speex_resampler_init(2, 48000, prm.samplerate, 3, &mpares); if(mpares!=RESAMPLER_ERR_SUCCESS) { - warning("mpa: resampler init failed %d\n",mpares); + error("mpa: resampler init failed %d\n",mpares); return EINVAL; } @@ -127,14 +133,16 @@ int mpa_encode_frm(struct auenc_state *aes, uint8_t *buf, size_t *len, } n = twolame_encode_buffer_interleaved(aes->enc, ds, ds_len, buf+4, (*len)-4); -// warning("mpa encode %d %d %d %d %d\n",ds_len,sampc,aes->channels,*len,n); +#ifdef DEBUG + debug("mpa encode %d %d %d %d %d\n",ds_len,sampc,aes->channels,*len,n); +#endif } else n = twolame_encode_buffer_interleaved(aes->enc, sampv, (int)(sampc/2), buf+4, (*len)-4); if (n < 0) { - warning("mpa: encode error: %s\n", strerror((int)n)); + error("mpa: encode error: %s\n", strerror((int)n)); return EPROTO; } @@ -145,7 +153,9 @@ int mpa_encode_frm(struct auenc_state *aes, uint8_t *buf, size_t *len, else *len = 0; -// warning("mpa encode %d %d %d %d\n",sampc,aes->channels,*len,n); +#ifdef DEBUG + debug("mpa encode %d %d %d %d\n",sampc,aes->channels,*len,n); +#endif return 0; } diff --git a/modules/mpa/mpa.c b/modules/mpa/mpa.c index cf0a273..04a6a8c 100644 --- a/modules/mpa/mpa.c +++ b/modules/mpa/mpa.c @@ -88,7 +88,6 @@ static struct aucodec mpa = { .ench = mpa_encode_frm, .decupdh = mpa_decode_update, .dech = mpa_decode_frm, - .plch = mpa_decode_pkloss, }; @@ -105,8 +104,13 @@ static int module_init(void) strcpy(mode,mpa.fmtp); if (0 == conf_get_u32(conf, "mpa_bitrate", &value)) { + if(value<8000 || value>384000) { + error("MPA bitrate between 8000 and 384000 are allowed."); + return -1; + } + (void)re_snprintf(fmtp+strlen(fmtp), sizeof(fmtp)-strlen(fmtp), - ";bitrate=%d", + "; bitrate=%d", value); } if (0 == conf_get_u32(conf, "mpa_layer", &value)) { @@ -115,7 +119,7 @@ static int module_init(void) return -1; } (void)re_snprintf(fmtp+strlen(fmtp), sizeof(fmtp)-strlen(fmtp), - ";layer=%d", + "; layer=%d", value); } if (0 == conf_get_u32(conf, "mpa_samplerate", &value)) { @@ -132,7 +136,7 @@ static int module_init(void) return -1; } (void)re_snprintf(fmtp+strlen(fmtp), sizeof(fmtp)-strlen(fmtp), - ";samplerate=%d", + "; samplerate=%d", value); } if (0 == conf_get_str(conf, "mpa_mode", mode, sizeof(mode))) { @@ -149,12 +153,12 @@ static int module_init(void) (void)re_snprintf(fmtp+strlen(fmtp), sizeof(fmtp)-strlen(fmtp), - ";mode=%s", + "; mode=%s", mode); } - if(fmtp[0]==';') - mpa.fmtp = fmtp+1; + if(fmtp[0]==';' && fmtp[1]==' ') + mpa.fmtp = fmtp+2; else mpa.fmtp = fmtp; @@ -187,3 +191,4 @@ EXPORT_SYM const struct mod_export DECL_EXPORTS(mpa) = { module_init, module_close, }; + diff --git a/modules/mpa/sdp.c b/modules/mpa/sdp.c index 0e4d811..ed0c2fd 100644 --- a/modules/mpa/sdp.c +++ b/modules/mpa/sdp.c @@ -32,7 +32,7 @@ void mpa_decode_fmtp(struct mpa_param *prm, const char *fmtp) pl_set_str(&pl, fmtp); if (fmt_param_get(&pl, "bitrate", &val)) - assign_if(&prm->bitrate, &val, 32000, 512000); + assign_if(&prm->bitrate, &val, 8000, 384000); if (fmt_param_get(&pl, "samplerate", &val)) assign_if(&prm->samplerate, &val, 16000, 48000); -- cgit v1.2.3