3 libdemac - A Monkey's Audio decoder
7 Copyright (C) Dave Chapman 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
29 #include "predictor.h"
32 #include "demac_config.h"
34 /* Statically allocate the filter buffers */
36 static filter_int filterbuf32
[(32*3 + FILTER_HISTORY_SIZE
) * 2]
37 IBSS_ATTR
__attribute__((aligned(16))); /* 2432/4864 bytes */
38 static filter_int filterbuf256
[(256*3 + FILTER_HISTORY_SIZE
) * 2]
39 IBSS_ATTR
__attribute__((aligned(16))); /* 5120/10240 bytes */
41 /* This is only needed for "insane" files, and no current Rockbox targets
42 can hope to decode them in realtime, although the Gigabeat S comes close. */
43 static filter_int filterbuf1280
[(1280*3 + FILTER_HISTORY_SIZE
) * 2]
44 IBSS_ATTR_DEMAC_INSANEBUF
__attribute__((aligned(16)));
45 /* 17408 or 34816 bytes */
47 void init_frame_decoder(struct ape_ctx_t
* ape_ctx
,
48 unsigned char* inbuffer
, int* firstbyte
,
51 init_entropy_decoder(ape_ctx
, inbuffer
, firstbyte
, bytesconsumed
);
52 //printf("CRC=0x%08x\n",ape_ctx->CRC);
53 //printf("Flags=0x%08x\n",ape_ctx->frameflags);
55 init_predictor_decoder(&ape_ctx
->predictor
);
57 switch (ape_ctx
->compressiontype
)
60 init_filter_16_11(filterbuf32
);
64 init_filter_64_11(filterbuf256
);
68 init_filter_256_13(filterbuf256
);
69 init_filter_32_10(filterbuf32
);
73 init_filter_1280_15(filterbuf1280
);
74 init_filter_256_13(filterbuf256
);
75 init_filter_16_11(filterbuf32
);
79 int ICODE_ATTR_DEMAC
decode_chunk(struct ape_ctx_t
* ape_ctx
,
80 unsigned char* inbuffer
, int* firstbyte
,
82 int32_t* decoded0
, int32_t* decoded1
,
87 int scale
= (APE_OUTPUT_DEPTH
- ape_ctx
->bps
);
88 #define SCALE(x) ((x) << scale)
93 if ((ape_ctx
->channels
==1) || ((ape_ctx
->frameflags
94 & (APE_FRAMECODE_PSEUDO_STEREO
|APE_FRAMECODE_STEREO_SILENCE
))
95 == APE_FRAMECODE_PSEUDO_STEREO
)) {
97 entropy_decode(ape_ctx
, inbuffer
, firstbyte
, bytesconsumed
,
98 decoded0
, NULL
, count
);
100 if (ape_ctx
->frameflags
& APE_FRAMECODE_MONO_SILENCE
) {
101 /* We are pure silence, so we're done. */
105 switch (ape_ctx
->compressiontype
)
108 apply_filter_16_11(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
112 apply_filter_64_11(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
116 apply_filter_32_10(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
117 apply_filter_256_13(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
121 apply_filter_16_11(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
122 apply_filter_256_13(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
123 apply_filter_1280_15(ape_ctx
->fileversion
,decoded0
,NULL
,count
);
126 /* Now apply the predictor decoding */
127 predictor_decode_mono(&ape_ctx
->predictor
,decoded0
,count
);
129 if (ape_ctx
->channels
==2) {
130 /* Pseudo-stereo - copy left channel to right channel */
134 *(decoded1
++) = *(decoded0
++) = SCALE(left
);
139 /* Scale to output depth */
143 *(decoded0
++) = SCALE(left
);
147 } else { /* Stereo */
148 entropy_decode(ape_ctx
, inbuffer
, firstbyte
, bytesconsumed
,
149 decoded0
, decoded1
, count
);
151 if ((ape_ctx
->frameflags
& APE_FRAMECODE_STEREO_SILENCE
)
152 == APE_FRAMECODE_STEREO_SILENCE
) {
153 /* We are pure silence, so we're done. */
157 /* Apply filters - compression type 1000 doesn't have any */
158 switch (ape_ctx
->compressiontype
)
161 apply_filter_16_11(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
165 apply_filter_64_11(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
169 apply_filter_32_10(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
170 apply_filter_256_13(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
174 apply_filter_16_11(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
175 apply_filter_256_13(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
176 apply_filter_1280_15(ape_ctx
->fileversion
,decoded0
,decoded1
,count
);
179 /* Now apply the predictor decoding */
180 predictor_decode_stereo(&ape_ctx
->predictor
,decoded0
,decoded1
,count
);
182 /* Decorrelate and scale to output depth */
185 left
= *decoded1
- (*decoded0
/ 2);
186 right
= left
+ *decoded0
;
188 *(decoded0
++) = SCALE(left
);
189 *(decoded1
++) = SCALE(right
);