diff options
Diffstat (limited to 'apps/codecs/demac/libdemac/decoder.c')
-rw-r--r-- | apps/codecs/demac/libdemac/decoder.c | 184 |
1 files changed, 184 insertions, 0 deletions
diff --git a/apps/codecs/demac/libdemac/decoder.c b/apps/codecs/demac/libdemac/decoder.c new file mode 100644 index 0000000000..22b6e8d325 --- /dev/null +++ b/apps/codecs/demac/libdemac/decoder.c | |||
@@ -0,0 +1,184 @@ | |||
1 | /* | ||
2 | |||
3 | libdemac - A Monkey's Audio decoder | ||
4 | |||
5 | $Id:$ | ||
6 | |||
7 | Copyright (C) Dave Chapman 2007 | ||
8 | |||
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. | ||
13 | |||
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. | ||
18 | |||
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 | ||
22 | |||
23 | */ | ||
24 | |||
25 | #include <inttypes.h> | ||
26 | #include <string.h> | ||
27 | |||
28 | #include "demac.h" | ||
29 | #include "predictor.h" | ||
30 | #include "entropy.h" | ||
31 | #include "filter.h" | ||
32 | |||
33 | /* Statically allocate the filter buffers */ | ||
34 | |||
35 | static int16_t filterbuf32[(32*3 + HISTORY_SIZE) * 2] IBSS_ATTR; /* 4480 bytes */ | ||
36 | static int16_t filterbuf256[(256*3 + HISTORY_SIZE) * 2] IBSS_ATTR; /* 5120 bytes */ | ||
37 | |||
38 | /* This is only needed for "insane" files, and no Rockbox targets can | ||
39 | hope to decode them in realtime anyway. */ | ||
40 | static int16_t filterbuf1280[(1280*3 + HISTORY_SIZE) * 2]; /* 17408 bytes */ | ||
41 | |||
42 | void init_frame_decoder(struct ape_ctx_t* ape_ctx, | ||
43 | unsigned char* inbuffer, int* firstbyte, | ||
44 | int* bytesconsumed) | ||
45 | { | ||
46 | init_entropy_decoder(ape_ctx, inbuffer, firstbyte, bytesconsumed); | ||
47 | //printf("CRC=0x%08x\n",ape_ctx->CRC); | ||
48 | //printf("Flags=0x%08x\n",ape_ctx->frameflags); | ||
49 | |||
50 | init_predictor_decoder(ape_ctx); | ||
51 | |||
52 | switch (ape_ctx->compressiontype) | ||
53 | { | ||
54 | case 2000: | ||
55 | init_filter_16_11(filterbuf32); | ||
56 | break; | ||
57 | |||
58 | case 3000: | ||
59 | init_filter_64_11(filterbuf256); | ||
60 | break; | ||
61 | |||
62 | case 4000: | ||
63 | init_filter_256_13(filterbuf256); | ||
64 | init_filter_32_10(filterbuf32); | ||
65 | break; | ||
66 | |||
67 | case 5000: | ||
68 | init_filter_1280_15(filterbuf1280); | ||
69 | init_filter_256_13(filterbuf256); | ||
70 | init_filter_16_11(filterbuf32); | ||
71 | } | ||
72 | } | ||
73 | |||
74 | int decode_chunk(struct ape_ctx_t* ape_ctx, | ||
75 | unsigned char* inbuffer, int* firstbyte, | ||
76 | int* bytesconsumed, | ||
77 | int32_t* decoded0, int32_t* decoded1, | ||
78 | int count) | ||
79 | { | ||
80 | int res; | ||
81 | int32_t left, right; | ||
82 | #ifdef ROCKBOX | ||
83 | int scale = (APE_OUTPUT_DEPTH - ape_ctx->bps); | ||
84 | #define SCALE(x) ((x) << scale) | ||
85 | #else | ||
86 | #define SCALE(x) (x) | ||
87 | #endif | ||
88 | |||
89 | if ((ape_ctx->channels==1) || (ape_ctx->frameflags & APE_FRAMECODE_PSEUDO_STEREO)) { | ||
90 | if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { | ||
91 | res = entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, decoded1, count); | ||
92 | /* We are pure silence, so we're done. */ | ||
93 | return 0; | ||
94 | } else { | ||
95 | res = entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, NULL, count); | ||
96 | } | ||
97 | |||
98 | switch (ape_ctx->compressiontype) | ||
99 | { | ||
100 | case 2000: | ||
101 | apply_filter_16_11(ape_ctx->fileversion,decoded0,NULL,count); | ||
102 | break; | ||
103 | |||
104 | case 3000: | ||
105 | apply_filter_64_11(ape_ctx->fileversion,decoded0,NULL,count); | ||
106 | break; | ||
107 | |||
108 | case 4000: | ||
109 | apply_filter_32_10(ape_ctx->fileversion,decoded0,NULL,count); | ||
110 | apply_filter_256_13(ape_ctx->fileversion,decoded0,NULL,count); | ||
111 | break; | ||
112 | |||
113 | case 5000: | ||
114 | apply_filter_16_11(ape_ctx->fileversion,decoded0,NULL,count); | ||
115 | apply_filter_256_13(ape_ctx->fileversion,decoded0,NULL,count); | ||
116 | apply_filter_1280_15(ape_ctx->fileversion,decoded0,NULL,count); | ||
117 | } | ||
118 | |||
119 | /* Now apply the predictor decoding */ | ||
120 | predictor_decode_mono(ape_ctx,decoded0,count); | ||
121 | |||
122 | if (ape_ctx->channels==2) { | ||
123 | /* Pseudo-stereo - just copy left channel to right channel */ | ||
124 | while (count--) | ||
125 | { | ||
126 | left = *decoded0; | ||
127 | *(decoded1++) = *(decoded0++) = SCALE(left); | ||
128 | } | ||
129 | } else { | ||
130 | /* Mono - do nothing unless it's 8-bit audio */ | ||
131 | if (ape_ctx->bps == 8) { | ||
132 | /* TODO: Handle 8-bit streams */ | ||
133 | } | ||
134 | } | ||
135 | } else { /* Stereo */ | ||
136 | if (ape_ctx->frameflags & APE_FRAMECODE_STEREO_SILENCE) { | ||
137 | /* We are pure silence, so we're done. */ | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | res = entropy_decode(ape_ctx, inbuffer, firstbyte, bytesconsumed, decoded0, decoded1, count); | ||
142 | |||
143 | /* Apply filters - compression type 1000 doesn't have any */ | ||
144 | switch (ape_ctx->compressiontype) | ||
145 | { | ||
146 | case 2000: | ||
147 | apply_filter_16_11(ape_ctx->fileversion,decoded0,decoded1,count); | ||
148 | break; | ||
149 | |||
150 | case 3000: | ||
151 | apply_filter_64_11(ape_ctx->fileversion,decoded0,decoded1,count); | ||
152 | break; | ||
153 | |||
154 | case 4000: | ||
155 | apply_filter_32_10(ape_ctx->fileversion,decoded0,decoded1,count); | ||
156 | apply_filter_256_13(ape_ctx->fileversion,decoded0,decoded1,count); | ||
157 | break; | ||
158 | |||
159 | case 5000: | ||
160 | apply_filter_16_11(ape_ctx->fileversion,decoded0,decoded1,count); | ||
161 | apply_filter_256_13(ape_ctx->fileversion,decoded0,decoded1,count); | ||
162 | apply_filter_1280_15(ape_ctx->fileversion,decoded0,decoded1,count); | ||
163 | } | ||
164 | |||
165 | /* Now apply the predictor decoding */ | ||
166 | predictor_decode_stereo(ape_ctx,decoded0,decoded1,count); | ||
167 | |||
168 | if (ape_ctx->bps == 8) { | ||
169 | /* TODO: Handle 8-bit streams */ | ||
170 | } else { | ||
171 | /* Decorrelate and scale to output depth */ | ||
172 | while (count--) | ||
173 | { | ||
174 | left = *decoded1 - (*decoded0 / 2); | ||
175 | right = left + *decoded0; | ||
176 | |||
177 | *(decoded0++) = SCALE(left); | ||
178 | *(decoded1++) = SCALE(right); | ||
179 | } | ||
180 | } | ||
181 | } | ||
182 | |||
183 | return res; | ||
184 | } | ||