summaryrefslogtreecommitdiff
path: root/modules
diff options
context:
space:
mode:
authorChristian Hoene <christian.hoene@symonics.com>2016-05-11 14:37:31 +0200
committerChristian Hoene <christian.hoene@symonics.com>2016-05-11 14:37:31 +0200
commit5fd811194ffa1f3c3a37ec9a0b32232bfc39b25d (patch)
tree678cf017308fcd569c98c6ebd354b01c1caa8504 /modules
parent949d7316947ad211d7f270a83e974a763a434f24 (diff)
Rewrote decoding, fixed memory leaks, add cr to print outs
Diffstat (limited to 'modules')
-rw-r--r--modules/mpa/decode.c97
-rw-r--r--modules/mpa/encode.c9
-rw-r--r--modules/mpa/mpa.c10
3 files changed, 67 insertions, 49 deletions
diff --git a/modules/mpa/decode.c b/modules/mpa/decode.c
index 5554d25..e862a1e 100644
--- a/modules/mpa/decode.c
+++ b/modules/mpa/decode.c
@@ -24,6 +24,9 @@ static void destructor(void *arg)
{
struct audec_state *ads = arg;
+ if(ads->resampler)
+ speex_resampler_destroy(ads->resampler);
+
mpg123_close(ads->dec);
mpg123_delete(ads->dec);
#ifdef DEBUG
@@ -71,7 +74,7 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac,
result = mpg123_param(ads->dec, MPG123_VERBOSE, 0, 0.);
#endif
if (result != MPG123_OK) {
- error("MPA libmpg123 param error %s",
+ error("MPA libmpg123 param error %s\n",
mpg123_plain_strerror(result));
err = EINVAL;
goto out;
@@ -80,7 +83,7 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac,
result = mpg123_format_all(ads->dec);
if (result != MPG123_OK) {
- error("MPA libmpg123 format error %s",
+ error("MPA libmpg123 format error %s\n",
mpg123_plain_strerror(result));
err = EINVAL;
goto out;
@@ -88,7 +91,7 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac,
result = mpg123_open_feed(ads->dec);
if (result != MPG123_OK) {
- error("MPA libmpg123 open feed error %s",
+ error("MPA libmpg123 open feed error %s\n",
mpg123_plain_strerror(result));
err = EINVAL;
goto out;
@@ -108,12 +111,16 @@ 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 result, channels, encoding, i, result2;
+ int result, channels, encoding, i;
long samplerate;
size_t n;
spx_uint32_t intermediate_len;
spx_uint32_t out_len;
+#ifdef DEBUG
+ info("mpa decode start %d %ld\n",len, *sampc);
+#endif
+
if (!ads || !sampv || !sampc || !buf || len<=4)
return EINVAL;
@@ -123,46 +130,16 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc,
return EPROTO;
}
- if (ads->resampler) {
+ n = 0;
result = mpg123_decode(ads->dec, buf+4, len-4,
(unsigned char*)ads->intermediate_buffer,
sizeof(ads->intermediate_buffer), &n);
/* n counts bytes */
- intermediate_len = n / 2 / ads->channels;
- /* intermediate_len counts samples per channel */
- out_len = *sampc;
- result2=speex_resampler_process_interleaved_int(
- ads->resampler, ads->intermediate_buffer,
- &intermediate_len, sampv, &out_len);
- if (result2!=RESAMPLER_ERR_SUCCESS) {
- error("mpa: upsample error: %s %d %d\n",
- strerror(result), out_len, *sampc/2);
- return EPROTO;
- }
-#ifdef DEBUG
- info("mpa decode %d %d %d %d\n",intermediate_len,*sampc,
- out_len,n);
-#endif
- *sampc = out_len * ads->channels;
- }
- else {
- result = mpg123_decode(ads->dec, buf+4, len-4,
- (unsigned char*)sampv, *sampc*2, &n);
+
#ifdef DEBUG
- info("mpa decode %d %d\n",*sampc,n);
+ info("mpa decoded %d %d %d %d\n",result, len-4, n, ads->channels);
#endif
- *sampc = n / 2;
- }
- if (ads->start<100) { /* mpg123 needs some to sync */
- ads->start++;
- *sampc=0;
- }
- if (ads->channels==1) {
- for (i=*sampc-1;i>=0;i--)
- sampv[i+i+1]=sampv[i+i]=sampv[i];
- *sampc *= 2;
- }
if (result == MPG123_NEW_FORMAT) {
mpg123_getformat(ads->dec, &samplerate, &channels, &encoding);
@@ -171,6 +148,8 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc,
ads->channels = channels;
ads->start = 0;
+ if(ads->resampler)
+ speex_resampler_destroy(ads->resampler);
if (samplerate != 48000) {
ads->resampler = speex_resampler_init(channels,
samplerate, 48000, 3, &result);
@@ -183,17 +162,53 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc,
else
ads->resampler = NULL;
}
- else if (result == MPG123_NEED_MORE)
- return 0;
+ else if (result == MPG123_NEED_MORE) ;
+// return 0;
else if (result != MPG123_OK) {
- error("MPA libmpg123 feed error %d %s", result,
+ error("MPA libmpg123 feed error %d %s\n", result,
mpg123_plain_strerror(result));
return EPROTO;
}
+ if (ads->resampler) {
+ intermediate_len = n / 2 / ads->channels;
+ /* intermediate_len counts samples per channel */
+ out_len = *sampc / 2;
+
+ result=speex_resampler_process_interleaved_int(
+ ads->resampler, ads->intermediate_buffer,
+ &intermediate_len, sampv, &out_len);
+ if (result!=RESAMPLER_ERR_SUCCESS) {
+ error("mpa: upsample error: %s %d %d\n",
+ strerror(result), out_len, *sampc/2);
+ return EPROTO;
+ }
+ if (ads->channels==1) {
+ for (i=out_len-1;i>=0;i--)
+ sampv[i+i+1]=sampv[i+i]=sampv[i];
+ *sampc = out_len * 2;
+ }
+ else
+ *sampc = out_len * ads->channels;
+ }
+ else {
+ n /= 2;
+ if(ads->channels!=1) {
+ for(i=0;(unsigned)i<n;i++)
+ sampv[i]=ads->intermediate_buffer[i];
+ *sampc = n;
+ }
+ else {
+ for(i=0;(unsigned)i<n;i++)
+ sampv[i*2]=sampv[i*2+1]=ads->intermediate_buffer[i];
+ *sampc = n * 2;
+ }
+
#ifdef DEBUG
- debug("mpa decode %d %d %d\n",*sampc,len,n);
+ info("mpa decode done %d\n",*sampc);
#endif
+ }
+
return 0;
}
diff --git a/modules/mpa/encode.c b/modules/mpa/encode.c
index 6ed6493..db18a61 100644
--- a/modules/mpa/encode.c
+++ b/modules/mpa/encode.c
@@ -23,6 +23,9 @@ static void destructor(void *arg)
{
struct auenc_state *aes = arg;
+ if(aes->resampler)
+ speex_resampler_destroy(aes->resampler);
+
if (aes->enc)
twolame_close(&aes->enc);
#ifdef DEBUG
@@ -51,7 +54,7 @@ int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac,
aes = mem_zalloc(sizeof(*aes), destructor);
aes->enc = twolame_init();
if (!aes->enc) {
- error("mpa: encoder create failed");
+ error("mpa: encoder create failed\n");
mem_deref(aes);
return ENOMEM;
}
@@ -96,9 +99,9 @@ int mpa_encode_update(struct auenc_state **aesp, const struct aucodec *ac,
err=EINVAL;
goto out;
}
-
+#ifdef DEBUG
twolame_print_config(aes->enc);
-
+#endif
if (prm.samplerate != 48000) {
aes->resampler = speex_resampler_init(2, 48000,
prm.samplerate, 3, &result);
diff --git a/modules/mpa/mpa.c b/modules/mpa/mpa.c
index 6276bf2..2086ee4 100644
--- a/modules/mpa/mpa.c
+++ b/modules/mpa/mpa.c
@@ -82,7 +82,7 @@ static struct aucodec mpa = {
.pt = "14",
.name = "MPA",
.srate = 90000,
- .ch = 1,
+ .ch = 2,
.fmtp = "",
.encupdh = mpa_encode_update,
.ench = mpa_encode_frm,
@@ -106,7 +106,7 @@ static int module_init(void)
if (0 == conf_get_u32(conf, "mpa_bitrate", &value)) {
if (value<8000 || value>384000) {
error("MPA bitrate between 8000 and "
- "384000 are allowed.");
+ "384000 are allowed.\n");
return -1;
}
@@ -134,7 +134,7 @@ static int module_init(void)
break;
default:
error("MPA samplerates of 16, 22.05, 24, 32, "
- "44.1, and 48 kHz are allowed.");
+ "44.1, and 48 kHz are allowed.\n");
return -1;
}
(void)re_snprintf(fmtp+strlen(fmtp),
@@ -153,7 +153,7 @@ static int module_init(void)
&& strcmp(mode,"single_channel")
&& strcmp(mode,"dual_channel")) {
error("MPA mode: Permissible values are stereo, "
- "joint_stereo, single_channel, dual_channel");
+ "joint_stereo, single_channel, dual_channel.\n");
return -1;
}
@@ -170,7 +170,7 @@ static int module_init(void)
/* init decoder library */
res = mpg123_init();
if (res != MPG123_OK) {
- error("MPA libmpg123 init error %s",
+ error("MPA libmpg123 init error %s\n",
mpg123_plain_strerror(res));
return -1;
}