From 0bf6e36628291855701de14e8575ac6fa5e25341 Mon Sep 17 00:00:00 2001 From: Jens Arnold Date: Sun, 21 Dec 2008 01:29:36 +0000 Subject: [PATCH] Fix handling of 8 bit mono and stereo APE files, and also optimise 16 and 24 bit output in the standalone decoder a bit. git-svn-id: svn://svn.rockbox.org/rockbox/trunk@19517 a1c6a512-1295-4272-9138-f99709370657 --- apps/codecs/demac/demac.c | 26 ++++++++++----- apps/codecs/demac/libdemac/decoder.c | 47 ++++++++++++---------------- 2 files changed, 38 insertions(+), 35 deletions(-) diff --git a/apps/codecs/demac/demac.c b/apps/codecs/demac/demac.c index da132ff248..5e9893687d 100644 --- a/apps/codecs/demac/demac.c +++ b/apps/codecs/demac/demac.c @@ -2,7 +2,7 @@ demac - A Monkey's Audio decoder -$Id:$ +$Id$ Copyright (C) Dave Chapman 2007 @@ -180,17 +180,27 @@ int ape_decode(char* infile, char* outfile) /* Convert the output samples to WAV format and write to output file */ p = wavbuffer; - if (ape_ctx.bps == 16) { + if (ape_ctx.bps == 8) { + for (i = 0 ; i < blockstodecode ; i++) + { + /* 8 bit WAV uses unsigned samples */ + *(p++) = (decoded0[i] + 0x80) & 0xff; + + if (ape_ctx.channels == 2) { + *(p++) = (decoded1[i] + 0x80) & 0xff; + } + } + } else if (ape_ctx.bps == 16) { for (i = 0 ; i < blockstodecode ; i++) { sample16 = decoded0[i]; *(p++) = sample16 & 0xff; - *(p++) = (sample16&0xff00) >> 8; + *(p++) = (sample16 >> 8) & 0xff; if (ape_ctx.channels == 2) { sample16 = decoded1[i]; *(p++) = sample16 & 0xff; - *(p++) = (sample16&0xff00) >> 8; + *(p++) = (sample16 >> 8) & 0xff; } } } else if (ape_ctx.bps == 24) { @@ -198,14 +208,14 @@ int ape_decode(char* infile, char* outfile) { sample32 = decoded0[i]; *(p++) = sample32 & 0xff; - *(p++) = (sample32&0xff00) >> 8; - *(p++) = (sample32&0xff0000) >> 16; + *(p++) = (sample32 >> 8) & 0xff; + *(p++) = (sample32 >> 16) & 0xff; if (ape_ctx.channels == 2) { sample32 = decoded1[i]; *(p++) = sample32 & 0xff; - *(p++) = (sample32&0xff00) >> 8; - *(p++) = (sample32&0xff0000) >> 16; + *(p++) = (sample32 >> 8) & 0xff; + *(p++) = (sample32 >> 16) & 0xff; } } } diff --git a/apps/codecs/demac/libdemac/decoder.c b/apps/codecs/demac/libdemac/decoder.c index 79b5255ce0..2200faf187 100644 --- a/apps/codecs/demac/libdemac/decoder.c +++ b/apps/codecs/demac/libdemac/decoder.c @@ -91,14 +91,14 @@ int ICODE_ATTR_DEMAC decode_chunk(struct ape_ctx_t* ape_ctx, #endif if ((ape_ctx->channels==1) || (ape_ctx->frameflags & APE_FRAMECODE_PSEUDO_STEREO)) { - if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { + if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, decoded1, count); /* We are pure silence, so we're done. */ return 0; } else { entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, NULL, count); } - + switch (ape_ctx->compressiontype) { case 2000: @@ -124,26 +124,23 @@ int ICODE_ATTR_DEMAC decode_chunk(struct ape_ctx_t* ape_ctx, predictor_decode_mono(&ape_ctx->predictor,decoded0,count); if (ape_ctx->channels==2) { - /* Pseudo-stereo - just copy left channel to right channel */ + /* Pseudo-stereo - copy left channel to right channel */ while (count--) { left = *decoded0; *(decoded1++) = *(decoded0++) = SCALE(left); } - } else { - /* Mono - do nothing unless it's 8-bit audio */ - if (ape_ctx->bps == 8) { - /* TODO: Handle 8-bit streams */ - } else { - /* Scale to output depth */ - while (count--) - { - left = *decoded0; - *(decoded0++) = SCALE(left); - } - } - } +#ifdef ROCKBOX + else { + /* Scale to output depth */ + while (count--) + { + left = *decoded0; + *(decoded0++) = SCALE(left); + } + } +#endif } else { /* Stereo */ if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { /* We are pure silence, so we're done. */ @@ -177,18 +174,14 @@ int ICODE_ATTR_DEMAC decode_chunk(struct ape_ctx_t* ape_ctx, /* Now apply the predictor decoding */ predictor_decode_stereo(&ape_ctx->predictor,decoded0,decoded1,count); - if (ape_ctx->bps == 8) { - /* TODO: Handle 8-bit streams */ - } else { - /* Decorrelate and scale to output depth */ - while (count--) - { - left = *decoded1 - (*decoded0 / 2); - right = left + *decoded0; + /* Decorrelate and scale to output depth */ + while (count--) + { + left = *decoded1 - (*decoded0 / 2); + right = left + *decoded0; - *(decoded0++) = SCALE(left); - *(decoded1++) = SCALE(right); - } + *(decoded0++) = SCALE(left); + *(decoded1++) = SCALE(right); } } return 0;