summaryrefslogtreecommitdiff
path: root/modules/mpa/decode.c
diff options
context:
space:
mode:
Diffstat (limited to 'modules/mpa/decode.c')
-rw-r--r--modules/mpa/decode.c118
1 files changed, 65 insertions, 53 deletions
diff --git a/modules/mpa/decode.c b/modules/mpa/decode.c
index 43adfaa..b71a335 100644
--- a/modules/mpa/decode.c
+++ b/modules/mpa/decode.c
@@ -24,10 +24,13 @@ 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
- debug("mpa: decoder destroyed\n");
+ debug("MPA dec destroyed\n");
#endif
}
@@ -45,7 +48,7 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac,
ads = *adsp;
#ifdef DEBUG
- debug("mpa: decoder created %s\n",fmtp);
+ debug("MPA dec created %s\n",fmtp);
#endif
if (ads)
@@ -60,7 +63,7 @@ int mpa_decode_update(struct audec_state **adsp, const struct aucodec *ac,
ads->dec = mpg123_new(NULL,&result);
if (!ads->dec) {
- error("mpa: decoder create: %s\n",
+ error("MPA dec create: %s\n",
mpg123_plain_strerror(result));
err = ENOMEM;
goto out;
@@ -72,7 +75,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 dec param error %s\n",
mpg123_plain_strerror(result));
err = EINVAL;
goto out;
@@ -81,7 +84,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 dec format error %s\n",
mpg123_plain_strerror(result));
err = EINVAL;
goto out;
@@ -89,7 +92,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 dec open feed error %s\n",
mpg123_plain_strerror(result));
err = EINVAL;
goto out;
@@ -114,72 +117,44 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc,
size_t n;
spx_uint32_t intermediate_len;
spx_uint32_t out_len;
- uint32_t header;
+
+#ifdef DEBUG
+ debug("MPA dec start %d %ld\n",len, *sampc);
+#endif
if (!ads || !sampv || !sampc || !buf || len<=4)
return EINVAL;
- header = *(uint32_t*)(void *)buf;
- if (header != 0) {
- error("MPA header is not zero %08X, not supported yet\n",
- header);
+ if (*(uint32_t*)buf != 0) {
+ error("MPA dec header is not zero %08X, not supported yet\n",
+ *(uint32_t*)buf);
return EPROTO;
}
- if (ads->resampler) {
- result = mpg123_decode(ads->dec, buf+4, len-4,
+ 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 = (uint32_t)(n / 2 / ads->channels);
- /* intermediate_len counts samples per channel */
- out_len = (uint32_t)*sampc;
- 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;
- }
-#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);
+ debug("MPA dec %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=(int)(*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);
- info("MPA libmpg123 format change %d %d %04X\n",samplerate
+ info("MPA dec format change %d %d %04X\n",samplerate
,channels,encoding);
ads->channels = channels;
ads->start = 0;
- if (samplerate != 48000) {
+ if (ads->resampler)
+ speex_resampler_destroy(ads->resampler);
+ if (samplerate != MPA_IORATE) {
ads->resampler = speex_resampler_init(channels,
- (uint32_t)samplerate, 48000, 3, &result);
+ (uint32_t)samplerate, MPA_IORATE, 3, &result);
if (result!=RESAMPLER_ERR_SUCCESS
|| ads->resampler==NULL) {
- error("mpa: upsampler failed %d\n",result);
+ error("MPA dec upsampler failed %d\n",result);
return EINVAL;
}
}
@@ -187,16 +162,53 @@ int mpa_decode_frm(struct audec_state *ads, int16_t *sampv, size_t *sampc,
ads->resampler = NULL;
}
else if (result == MPG123_NEED_MORE)
- return 0;
+ ; /* workaround: do nothing */
else if (result != MPG123_OK) {
- error("MPA libmpg123 feed error %d %s", result,
+ error("MPA dec 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 dec 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);
+ debug("MPA dec done %d\n",*sampc);
#endif
+ }
+
return 0;
}