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 */
37 static filter_int filterbuf32
[(32*3 + FILTER_HISTORY_SIZE
) * 2]
38 IBSS_ATTR_DEMAC MEM_ALIGN_ATTR
;
39 /* 2432 or 4864 bytes */
40 static filter_int filterbuf256
[(256*3 + FILTER_HISTORY_SIZE
) * 2]
41 IBSS_ATTR_DEMAC MEM_ALIGN_ATTR
;
42 /* 5120 or 10240 bytes */
43 #define FILTERBUF64 filterbuf256
44 #define FILTERBUF32 filterbuf32
45 #define FILTERBUF16 filterbuf32
47 static filter_int filterbuf64
[(64*3 + FILTER_HISTORY_SIZE
) * 2]
48 IBSS_ATTR_DEMAC MEM_ALIGN_ATTR
;
49 /* 2432 or 4864 bytes */
50 static filter_int filterbuf256
[(256*3 + FILTER_HISTORY_SIZE
) * 2]
51 MEM_ALIGN_ATTR
; /* 5120 or 10240 bytes */
52 #define FILTERBUF64 filterbuf64
53 #define FILTERBUF32 filterbuf64
54 #define FILTERBUF16 filterbuf64
57 /* This is only needed for "insane" files, and no current Rockbox targets
58 can hope to decode them in realtime, except the Gigabeat S (at 528MHz). */
59 static filter_int filterbuf1280
[(1280*3 + FILTER_HISTORY_SIZE
) * 2]
60 IBSS_ATTR_DEMAC_INSANEBUF MEM_ALIGN_ATTR
;
61 /* 17408 or 34816 bytes */
63 void init_frame_decoder(struct ape_ctx_t
* ape_ctx
,
64 unsigned char* inbuffer
, int* firstbyte
,
67 init_entropy_decoder(ape_ctx
, inbuffer
, firstbyte
, bytesconsumed
);
68 //printf("CRC=0x%08x\n",ape_ctx->CRC);
69 //printf("Flags=0x%08x\n",ape_ctx->frameflags);
71 init_predictor_decoder(&ape_ctx
->predictor
);
73 switch (ape_ctx
->compressiontype
)
76 init_filter_16_11(FILTERBUF16
);
80 init_filter_64_11(FILTERBUF64
);
84 init_filter_256_13(filterbuf256
);
85 init_filter_32_10(FILTERBUF32
);
89 init_filter_1280_15(filterbuf1280
);
90 init_filter_256_13(filterbuf256
);
91 init_filter_16_11(FILTERBUF32
);
95 int ICODE_ATTR_DEMAC
decode_chunk(struct ape_ctx_t
* ape_ctx
,
96 unsigned char* inbuffer
, int* firstbyte
,
98 int32_t* decoded0
, int32_t* decoded1
,
103 int scale
= (APE_OUTPUT_DEPTH
- ape_ctx
->bps
);
104 #define SCALE(x) ((x) << scale)
109 if ((ape_ctx
->channels
==1) || ((ape_ctx
->frameflags
110 & (APE_FRAMECODE_PSEUDO_STEREO
|APE_FRAMECODE_STEREO_SILENCE
))
111 == APE_FRAMECODE_PSEUDO_STEREO
)) {
113 entropy_decode(ape_ctx
, inbuffer
, firstbyte
, bytesconsumed
,
114 decoded0
, NULL
, count
);
116 if (ape_ctx
->frameflags
& APE_FRAMECODE_MONO_SILENCE
) {
117 /* We are pure silence, so we're done. */
121 switch (ape_ctx
->compressiontype
)
124 apply_filter_16_11(ape_ctx
->fileversion
,0,decoded0
,count
);
128 apply_filter_64_11(ape_ctx
->fileversion
,0,decoded0
,count
);
132 apply_filter_32_10(ape_ctx
->fileversion
,0,decoded0
,count
);
133 apply_filter_256_13(ape_ctx
->fileversion
,0,decoded0
,count
);
137 apply_filter_16_11(ape_ctx
->fileversion
,0,decoded0
,count
);
138 apply_filter_256_13(ape_ctx
->fileversion
,0,decoded0
,count
);
139 apply_filter_1280_15(ape_ctx
->fileversion
,0,decoded0
,count
);
142 /* Now apply the predictor decoding */
143 predictor_decode_mono(&ape_ctx
->predictor
,decoded0
,count
);
145 if (ape_ctx
->channels
==2) {
146 /* Pseudo-stereo - copy left channel to right channel */
150 *(decoded1
++) = *(decoded0
++) = SCALE(left
);
155 /* Scale to output depth */
159 *(decoded0
++) = SCALE(left
);
163 } else { /* Stereo */
164 entropy_decode(ape_ctx
, inbuffer
, firstbyte
, bytesconsumed
,
165 decoded0
, decoded1
, count
);
167 if ((ape_ctx
->frameflags
& APE_FRAMECODE_STEREO_SILENCE
)
168 == APE_FRAMECODE_STEREO_SILENCE
) {
169 /* We are pure silence, so we're done. */
173 /* Apply filters - compression type 1000 doesn't have any */
174 switch (ape_ctx
->compressiontype
)
177 apply_filter_16_11(ape_ctx
->fileversion
,0,decoded0
,count
);
178 apply_filter_16_11(ape_ctx
->fileversion
,1,decoded1
,count
);
182 apply_filter_64_11(ape_ctx
->fileversion
,0,decoded0
,count
);
183 apply_filter_64_11(ape_ctx
->fileversion
,1,decoded1
,count
);
187 apply_filter_32_10(ape_ctx
->fileversion
,0,decoded0
,count
);
188 apply_filter_32_10(ape_ctx
->fileversion
,1,decoded1
,count
);
189 apply_filter_256_13(ape_ctx
->fileversion
,0,decoded0
,count
);
190 apply_filter_256_13(ape_ctx
->fileversion
,1,decoded1
,count
);
194 apply_filter_16_11(ape_ctx
->fileversion
,0,decoded0
,count
);
195 apply_filter_16_11(ape_ctx
->fileversion
,1,decoded1
,count
);
196 apply_filter_256_13(ape_ctx
->fileversion
,0,decoded0
,count
);
197 apply_filter_256_13(ape_ctx
->fileversion
,1,decoded1
,count
);
198 apply_filter_1280_15(ape_ctx
->fileversion
,0,decoded0
,count
);
199 apply_filter_1280_15(ape_ctx
->fileversion
,1,decoded1
,count
);
202 /* Now apply the predictor decoding */
203 predictor_decode_stereo(&ape_ctx
->predictor
,decoded0
,decoded1
,count
);
205 /* Decorrelate and scale to output depth */
208 left
= *decoded1
- (*decoded0
/ 2);
209 right
= left
+ *decoded0
;
211 *(decoded0
++) = SCALE(left
);
212 *(decoded1
++) = SCALE(right
);