FS#8961 - Anti-Aliased Fonts.
[kugel-rb/myfork.git] / apps / codecs / demac / libdemac / decoder.c
blob0763c11037ed6df236dd53fb38a23dbfbcd190db
1 /*
3 libdemac - A Monkey's Audio decoder
5 $Id$
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
25 #include <inttypes.h>
26 #include <string.h>
28 #include "demac.h"
29 #include "predictor.h"
30 #include "entropy.h"
31 #include "filter.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,
49 int* bytesconsumed)
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)
59 case 2000:
60 init_filter_16_11(filterbuf32);
61 break;
63 case 3000:
64 init_filter_64_11(filterbuf256);
65 break;
67 case 4000:
68 init_filter_256_13(filterbuf256);
69 init_filter_32_10(filterbuf32);
70 break;
72 case 5000:
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,
81 int* bytesconsumed,
82 int32_t* decoded0, int32_t* decoded1,
83 int count)
85 int32_t left, right;
86 #ifdef ROCKBOX
87 int scale = (APE_OUTPUT_DEPTH - ape_ctx->bps);
88 #define SCALE(x) ((x) << scale)
89 #else
90 #define SCALE(x) (x)
91 #endif
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. */
102 return 0;
105 switch (ape_ctx->compressiontype)
107 case 2000:
108 apply_filter_16_11(ape_ctx->fileversion,decoded0,NULL,count);
109 break;
111 case 3000:
112 apply_filter_64_11(ape_ctx->fileversion,decoded0,NULL,count);
113 break;
115 case 4000:
116 apply_filter_32_10(ape_ctx->fileversion,decoded0,NULL,count);
117 apply_filter_256_13(ape_ctx->fileversion,decoded0,NULL,count);
118 break;
120 case 5000:
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 */
131 while (count--)
133 left = *decoded0;
134 *(decoded1++) = *(decoded0++) = SCALE(left);
137 #ifdef ROCKBOX
138 else {
139 /* Scale to output depth */
140 while (count--)
142 left = *decoded0;
143 *(decoded0++) = SCALE(left);
146 #endif
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. */
154 return 0;
157 /* Apply filters - compression type 1000 doesn't have any */
158 switch (ape_ctx->compressiontype)
160 case 2000:
161 apply_filter_16_11(ape_ctx->fileversion,decoded0,decoded1,count);
162 break;
164 case 3000:
165 apply_filter_64_11(ape_ctx->fileversion,decoded0,decoded1,count);
166 break;
168 case 4000:
169 apply_filter_32_10(ape_ctx->fileversion,decoded0,decoded1,count);
170 apply_filter_256_13(ape_ctx->fileversion,decoded0,decoded1,count);
171 break;
173 case 5000:
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 */
183 while (count--)
185 left = *decoded1 - (*decoded0 / 2);
186 right = left + *decoded0;
188 *(decoded0++) = SCALE(left);
189 *(decoded1++) = SCALE(right);
192 return 0;