diff options
Diffstat (limited to 'lib/rbcodec/codecs/libalac')
-rw-r--r-- | lib/rbcodec/codecs/libalac/README | 43 | ||||
-rw-r--r-- | lib/rbcodec/codecs/libalac/README.rockbox | 80 | ||||
-rw-r--r-- | lib/rbcodec/codecs/libalac/SOURCES | 1 | ||||
-rw-r--r-- | lib/rbcodec/codecs/libalac/alac.c | 1148 | ||||
-rw-r--r-- | lib/rbcodec/codecs/libalac/decomp.h | 54 | ||||
-rw-r--r-- | lib/rbcodec/codecs/libalac/libalac.make | 18 |
6 files changed, 1344 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libalac/README b/lib/rbcodec/codecs/libalac/README new file mode 100644 index 0000000000..1b304d2d93 --- /dev/null +++ b/lib/rbcodec/codecs/libalac/README | |||
@@ -0,0 +1,43 @@ | |||
1 | INSTALLATION: | ||
2 | ------------- | ||
3 | |||
4 | Simply compile by running 'make' | ||
5 | |||
6 | USAGE: | ||
7 | ------ | ||
8 | |||
9 | Then run the program, it will give you usage instructions. | ||
10 | |||
11 | It's really quite trivial to use. | ||
12 | |||
13 | For example, to decode input.m4a to output.wav: | ||
14 | ./alac -f output.wav input.m4a | ||
15 | |||
16 | Or, as another example, say if you wanted to stream play | ||
17 | http://www.mplayerhq.hu/MPlayer/samples/A-codecs/lossless/luckynight.m4a | ||
18 | and you're system uses the ALSA sound system: | ||
19 | wget -O - http://www.mplayerhq.hu/MPlayer/samples/A-codecs/lossless/luckynight.m4a | ./alac - | aplay | ||
20 | |||
21 | By default the output file is in WAV format. To output as raw PCM, provide the | ||
22 | -r option on the command line. | ||
23 | |||
24 | MORE INFORMATION: | ||
25 | ----------------- | ||
26 | |||
27 | Please visit http://crazney.net/programs/itunes/alac.html for more information. | ||
28 | |||
29 | HELP NEEDED: | ||
30 | ------------ | ||
31 | I need help to allow myself to continue hacking on various Apple Audio things, please visit | ||
32 | http://crazney.net/programs/itunes/help.html | ||
33 | |||
34 | AUTHOR: | ||
35 | ------- | ||
36 | |||
37 | David Hammerton | ||
38 | |||
39 | CONTRIBUTORS: | ||
40 | ------------- | ||
41 | |||
42 | Cody Brocious | ||
43 | |||
diff --git a/lib/rbcodec/codecs/libalac/README.rockbox b/lib/rbcodec/codecs/libalac/README.rockbox new file mode 100644 index 0000000000..891e581cfc --- /dev/null +++ b/lib/rbcodec/codecs/libalac/README.rockbox | |||
@@ -0,0 +1,80 @@ | |||
1 | Library: Reverse-engineered ALAC decoder v0.1.0 | ||
2 | Imported: 2005-08-14 by Dave Chapman | ||
3 | |||
4 | |||
5 | This directory contains a local version of an ALAC (Apple Lossless Audio | ||
6 | Codec) for use by Rockbox for software decoding of ALAC files. It is | ||
7 | based on the reverse-engineered decoder by David Hamilton. | ||
8 | |||
9 | LICENSING INFORMATION | ||
10 | |||
11 | /* | ||
12 | * ALAC (Apple Lossless Audio Codec) decoder | ||
13 | * Copyright (c) 2005 David Hammerton | ||
14 | * All rights reserved. | ||
15 | * | ||
16 | * This is the actual decoder. | ||
17 | * | ||
18 | * http://crazney.net/programs/itunes/alac.html | ||
19 | * | ||
20 | * Permission is hereby granted, free of charge, to any person | ||
21 | * obtaining a copy of this software and associated documentation | ||
22 | * files (the "Software"), to deal in the Software without | ||
23 | * restriction, including without limitation the rights to use, | ||
24 | * copy, modify, merge, publish, distribute, sublicense, and/or | ||
25 | * sell copies of the Software, and to permit persons to whom the | ||
26 | * Software is furnished to do so, subject to the following conditions: | ||
27 | * | ||
28 | * The above copyright notice and this permission notice shall be | ||
29 | * included in all copies or substantial portions of the Software. | ||
30 | * | ||
31 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
32 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
33 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
34 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
35 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
36 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
37 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
38 | * OTHER DEALINGS IN THE SOFTWARE. | ||
39 | * | ||
40 | */ | ||
41 | |||
42 | IMPORT DETAILS | ||
43 | |||
44 | The base version first imported into Rockbox was the first release | ||
45 | (v0.1.0) of the ALAC decoder by David Hammerton. | ||
46 | |||
47 | Only the files alac.[ch], demux.[ch] and stream.h were used. | ||
48 | |||
49 | stream.c (the original FILE* based I/O implementation) was replaced with | ||
50 | functions in the ALAC codec - to interface with the Rockbox audio playback | ||
51 | system. | ||
52 | |||
53 | References to <stdint.h> were replaced with <inttypes.h> and debugging | ||
54 | calls to fprintf were removed. | ||
55 | |||
56 | The ALAC decoder itself was modified to return samples in host-endian | ||
57 | order, instead of little-endian. | ||
58 | |||
59 | The run-time detection of CPU endianness was replaced with | ||
60 | compile-time tests of the ROCKBOX_LITTLE_ENDIAN define. | ||
61 | |||
62 | All malloc calls were removed from alac.c, but some are still present | ||
63 | in the metadata parser in demux.c - to store unbounded data such as | ||
64 | the size in bytes of each compressed block in the file. | ||
65 | |||
66 | The only changes to demux.c were to remove debugging calls to fprintf. | ||
67 | |||
68 | The most-used buffers (the temporary 32-bit output buffer) were moved | ||
69 | into IRAM (on the iRiver). This was enough to make the decoder work | ||
70 | in real-time. | ||
71 | |||
72 | A point of interest - the -O3 gcc option (the setting used in the | ||
73 | original Makefile provided with the alac decoder) gives a significant | ||
74 | speedup compared to -O2. With -O2, the Coldfire runs at a constant | ||
75 | 120MHz, but with -O3, it can power-down to 40MHz for a small amount of | ||
76 | time. | ||
77 | |||
78 | The file alac.c contained some hints from the original author for | ||
79 | places where major optimisations can be made - specifically the | ||
80 | unrolling and optimisation of certain cases of general loops. | ||
diff --git a/lib/rbcodec/codecs/libalac/SOURCES b/lib/rbcodec/codecs/libalac/SOURCES new file mode 100644 index 0000000000..854b9e6f88 --- /dev/null +++ b/lib/rbcodec/codecs/libalac/SOURCES | |||
@@ -0,0 +1 @@ | |||
alac.c | |||
diff --git a/lib/rbcodec/codecs/libalac/alac.c b/lib/rbcodec/codecs/libalac/alac.c new file mode 100644 index 0000000000..fd24656504 --- /dev/null +++ b/lib/rbcodec/codecs/libalac/alac.c | |||
@@ -0,0 +1,1148 @@ | |||
1 | /* | ||
2 | * ALAC (Apple Lossless Audio Codec) decoder | ||
3 | * Copyright (c) 2005 David Hammerton | ||
4 | * All rights reserved. | ||
5 | * | ||
6 | * This is the actual decoder. | ||
7 | * | ||
8 | * http://crazney.net/programs/itunes/alac.html | ||
9 | * | ||
10 | * Permission is hereby granted, free of charge, to any person | ||
11 | * obtaining a copy of this software and associated documentation | ||
12 | * files (the "Software"), to deal in the Software without | ||
13 | * restriction, including without limitation the rights to use, | ||
14 | * copy, modify, merge, publish, distribute, sublicense, and/or | ||
15 | * sell copies of the Software, and to permit persons to whom the | ||
16 | * Software is furnished to do so, subject to the following conditions: | ||
17 | * | ||
18 | * The above copyright notice and this permission notice shall be | ||
19 | * included in all copies or substantial portions of the Software. | ||
20 | * | ||
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES | ||
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | ||
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR | ||
28 | * OTHER DEALINGS IN THE SOFTWARE. | ||
29 | * | ||
30 | */ | ||
31 | |||
32 | |||
33 | #include <stdio.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <string.h> | ||
36 | #include <inttypes.h> | ||
37 | |||
38 | #include "codeclib.h" | ||
39 | #include "decomp.h" | ||
40 | |||
41 | #define SIGNEXTEND24(val) (((signed)val<<8)>>8) | ||
42 | |||
43 | static int16_t predictor_coef_table[32] IBSS_ATTR; | ||
44 | static int16_t predictor_coef_table_a[32] IBSS_ATTR; | ||
45 | static int16_t predictor_coef_table_b[32] IBSS_ATTR; | ||
46 | |||
47 | |||
48 | /* Endian/aligment safe functions - only used in alac_set_info() */ | ||
49 | static uint32_t get_uint32be(unsigned char* p) | ||
50 | { | ||
51 | return((p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]); | ||
52 | } | ||
53 | |||
54 | static uint16_t get_uint16be(unsigned char* p) | ||
55 | { | ||
56 | return((p[0]<<8) | p[1]); | ||
57 | } | ||
58 | |||
59 | void alac_set_info(alac_file *alac, char *inputbuffer) | ||
60 | { | ||
61 | unsigned char* ptr = (unsigned char*)inputbuffer; | ||
62 | ptr += 4; /* size */ | ||
63 | ptr += 4; /* frma */ | ||
64 | ptr += 4; /* alac */ | ||
65 | ptr += 4; /* size */ | ||
66 | ptr += 4; /* alac */ | ||
67 | |||
68 | ptr += 4; /* 0 ? */ | ||
69 | |||
70 | alac->setinfo_max_samples_per_frame = get_uint32be(ptr); /* buffer size / 2 ? */ | ||
71 | ptr += 4; | ||
72 | alac->setinfo_7a = *ptr++; | ||
73 | alac->setinfo_sample_size = *ptr++; | ||
74 | alac->setinfo_rice_historymult = *ptr++; | ||
75 | alac->setinfo_rice_initialhistory = *ptr++; | ||
76 | alac->setinfo_rice_kmodifier = *ptr++; | ||
77 | alac->setinfo_7f = *ptr++; | ||
78 | ptr += 1; | ||
79 | alac->setinfo_80 = get_uint16be(ptr); | ||
80 | ptr += 2; | ||
81 | alac->setinfo_82 = get_uint32be(ptr); | ||
82 | ptr += 4; | ||
83 | alac->setinfo_86 = get_uint32be(ptr); | ||
84 | ptr += 4; | ||
85 | alac->setinfo_8a_rate = get_uint32be(ptr); | ||
86 | ptr += 4; | ||
87 | } | ||
88 | |||
89 | /* stream reading */ | ||
90 | |||
91 | /* supports reading 1 to 16 bits, in big endian format */ | ||
92 | static inline uint32_t readbits_16(alac_file *alac, int bits) | ||
93 | { | ||
94 | uint32_t result; | ||
95 | int new_accumulator; | ||
96 | |||
97 | result = (alac->input_buffer[0] << 16) | | ||
98 | (alac->input_buffer[1] << 8) | | ||
99 | (alac->input_buffer[2]); | ||
100 | |||
101 | /* shift left by the number of bits we've already read, | ||
102 | * so that the top 'n' bits of the 24 bits we read will | ||
103 | * be the return bits */ | ||
104 | result = result << alac->input_buffer_bitaccumulator; | ||
105 | |||
106 | result = result & 0x00ffffff; | ||
107 | |||
108 | /* and then only want the top 'n' bits from that, where | ||
109 | * n is 'bits' */ | ||
110 | result = result >> (24 - bits); | ||
111 | |||
112 | new_accumulator = (alac->input_buffer_bitaccumulator + bits); | ||
113 | |||
114 | /* increase the buffer pointer if we've read over n bytes. */ | ||
115 | alac->input_buffer += (new_accumulator >> 3); | ||
116 | |||
117 | /* and the remainder goes back into the bit accumulator */ | ||
118 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); | ||
119 | |||
120 | return result; | ||
121 | } | ||
122 | |||
123 | /* supports reading 1 to 32 bits, in big endian format */ | ||
124 | static inline uint32_t readbits(alac_file *alac, int bits) | ||
125 | { | ||
126 | int32_t result = 0; | ||
127 | |||
128 | if (bits > 16) | ||
129 | { | ||
130 | bits -= 16; | ||
131 | result = readbits_16(alac, 16) << bits; | ||
132 | } | ||
133 | |||
134 | result |= readbits_16(alac, bits); | ||
135 | |||
136 | return result; | ||
137 | } | ||
138 | |||
139 | /* reads a single bit */ | ||
140 | static inline int readbit(alac_file *alac) | ||
141 | { | ||
142 | int result; | ||
143 | int new_accumulator; | ||
144 | |||
145 | result = alac->input_buffer[0]; | ||
146 | |||
147 | result = result << alac->input_buffer_bitaccumulator; | ||
148 | |||
149 | result = result >> 7 & 1; | ||
150 | |||
151 | new_accumulator = (alac->input_buffer_bitaccumulator + 1); | ||
152 | |||
153 | alac->input_buffer += (new_accumulator / 8); | ||
154 | |||
155 | alac->input_buffer_bitaccumulator = (new_accumulator % 8); | ||
156 | |||
157 | return result; | ||
158 | } | ||
159 | |||
160 | static inline void unreadbits(alac_file *alac, int bits) | ||
161 | { | ||
162 | int new_accumulator = (alac->input_buffer_bitaccumulator - bits); | ||
163 | |||
164 | alac->input_buffer += (new_accumulator >> 3); | ||
165 | |||
166 | alac->input_buffer_bitaccumulator = (new_accumulator & 7); | ||
167 | if (alac->input_buffer_bitaccumulator < 0) | ||
168 | alac->input_buffer_bitaccumulator *= -1; | ||
169 | } | ||
170 | |||
171 | #define count_leading_zeros(x) bs_generic(x, BS_CLZ|BS_SHORT) | ||
172 | |||
173 | #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix. | ||
174 | |||
175 | static inline int32_t entropy_decode_value(alac_file* alac, | ||
176 | int readsamplesize, | ||
177 | int k) ICODE_ATTR_ALAC; | ||
178 | static inline int32_t entropy_decode_value(alac_file* alac, | ||
179 | int readsamplesize, | ||
180 | int k) | ||
181 | { | ||
182 | int32_t x = 0; // decoded value | ||
183 | |||
184 | // read x, number of 1s before 0 represent the rice value. | ||
185 | while (x <= RICE_THRESHOLD && readbit(alac)) | ||
186 | { | ||
187 | x++; | ||
188 | } | ||
189 | |||
190 | if (x > RICE_THRESHOLD) | ||
191 | { | ||
192 | // read the number from the bit stream (raw value) | ||
193 | int32_t value; | ||
194 | |||
195 | value = readbits(alac, readsamplesize); | ||
196 | |||
197 | /* mask value to readsamplesize size */ | ||
198 | if (readsamplesize != 32) | ||
199 | value &= (((uint32_t)0xffffffff) >> (32 - readsamplesize)); | ||
200 | |||
201 | x = value; | ||
202 | } | ||
203 | else | ||
204 | { | ||
205 | if (k != 1) | ||
206 | { | ||
207 | int extrabits = readbits(alac, k); | ||
208 | |||
209 | // x = x * (2^k - 1) | ||
210 | x = (x << k) - x; | ||
211 | |||
212 | if (extrabits > 1) | ||
213 | x += extrabits - 1; | ||
214 | else | ||
215 | unreadbits(alac, 1); | ||
216 | } | ||
217 | } | ||
218 | |||
219 | return x; | ||
220 | } | ||
221 | |||
222 | static void entropy_rice_decode(alac_file* alac, | ||
223 | int32_t* output_buffer, | ||
224 | int output_size, | ||
225 | int readsamplesize, | ||
226 | int rice_initialhistory, | ||
227 | int rice_kmodifier, | ||
228 | int rice_historymult, | ||
229 | int rice_kmodifier_mask) ICODE_ATTR_ALAC; | ||
230 | static void entropy_rice_decode(alac_file* alac, | ||
231 | int32_t* output_buffer, | ||
232 | int output_size, | ||
233 | int readsamplesize, | ||
234 | int rice_initialhistory, | ||
235 | int rice_kmodifier, | ||
236 | int rice_historymult, | ||
237 | int rice_kmodifier_mask) | ||
238 | { | ||
239 | int output_count; | ||
240 | int history = rice_initialhistory; | ||
241 | int sign_modifier = 0; | ||
242 | |||
243 | for (output_count = 0; output_count < output_size; output_count++) | ||
244 | { | ||
245 | int32_t decoded_value; | ||
246 | int32_t final_value; | ||
247 | int32_t k; | ||
248 | |||
249 | k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3); | ||
250 | |||
251 | if (k < 0) k += rice_kmodifier; | ||
252 | else k = rice_kmodifier; | ||
253 | |||
254 | decoded_value = entropy_decode_value(alac, readsamplesize, k); | ||
255 | |||
256 | decoded_value += sign_modifier; | ||
257 | final_value = (decoded_value + 1) / 2; // inc by 1 and shift out sign bit | ||
258 | if (decoded_value & 1) // the sign is stored in the low bit | ||
259 | final_value *= -1; | ||
260 | |||
261 | output_buffer[output_count] = final_value; | ||
262 | |||
263 | sign_modifier = 0; | ||
264 | |||
265 | // update history | ||
266 | history += (decoded_value * rice_historymult) | ||
267 | - ((history * rice_historymult) >> 9); | ||
268 | |||
269 | if (decoded_value > 0xFFFF) | ||
270 | history = 0xFFFF; | ||
271 | |||
272 | // special case, for compressed blocks of 0 | ||
273 | if ((history < 128) && (output_count + 1 < output_size)) | ||
274 | { | ||
275 | int32_t block_size; | ||
276 | |||
277 | sign_modifier = 1; | ||
278 | |||
279 | k = count_leading_zeros(history) + ((history + 16) / 64) - 24; | ||
280 | |||
281 | // note: block_size is always 16bit | ||
282 | block_size = entropy_decode_value(alac, 16, k) & rice_kmodifier_mask; | ||
283 | |||
284 | // got block_size 0s | ||
285 | if (block_size > 0) | ||
286 | { | ||
287 | memset(&output_buffer[output_count + 1], 0, | ||
288 | block_size * sizeof(*output_buffer)); | ||
289 | output_count += block_size; | ||
290 | } | ||
291 | |||
292 | if (block_size > 0xFFFF) | ||
293 | sign_modifier = 0; | ||
294 | |||
295 | history = 0; | ||
296 | } | ||
297 | } | ||
298 | } | ||
299 | |||
300 | #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits)) | ||
301 | |||
302 | #define SIGN_ONLY(v) \ | ||
303 | ((v < 0) ? (-1) : \ | ||
304 | ((v > 0) ? (1) : \ | ||
305 | (0))) | ||
306 | |||
307 | static void predictor_decompress_fir_adapt(int32_t *error_buffer, | ||
308 | int32_t *buffer_out, | ||
309 | int output_size, | ||
310 | int readsamplesize, | ||
311 | int16_t *predictor_coef_table, | ||
312 | int predictor_coef_num, | ||
313 | int predictor_quantitization) ICODE_ATTR_ALAC; | ||
314 | static void predictor_decompress_fir_adapt(int32_t *error_buffer, | ||
315 | int32_t *buffer_out, | ||
316 | int output_size, | ||
317 | int readsamplesize, | ||
318 | int16_t *predictor_coef_table, | ||
319 | int predictor_coef_num, | ||
320 | int predictor_quantitization) | ||
321 | { | ||
322 | int i; | ||
323 | |||
324 | /* first sample always copies */ | ||
325 | *buffer_out = *error_buffer; | ||
326 | |||
327 | if (!predictor_coef_num) | ||
328 | { | ||
329 | if (output_size <= 1) return; | ||
330 | memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4); | ||
331 | return; | ||
332 | } | ||
333 | |||
334 | if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */ | ||
335 | { /* second-best case scenario for fir decompression, | ||
336 | * error describes a small difference from the previous sample only | ||
337 | */ | ||
338 | if (output_size <= 1) return; | ||
339 | for (i = 0; i < output_size - 1; i++) | ||
340 | { | ||
341 | int32_t prev_value; | ||
342 | int32_t error_value; | ||
343 | |||
344 | prev_value = buffer_out[i]; | ||
345 | error_value = error_buffer[i+1]; | ||
346 | buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize); | ||
347 | } | ||
348 | return; | ||
349 | } | ||
350 | |||
351 | /* read warm-up samples */ | ||
352 | if (predictor_coef_num > 0) | ||
353 | { | ||
354 | int i; | ||
355 | for (i = 0; i < predictor_coef_num; i++) | ||
356 | { | ||
357 | int32_t val; | ||
358 | |||
359 | val = buffer_out[i] + error_buffer[i+1]; | ||
360 | |||
361 | val = SIGN_EXTENDED32(val, readsamplesize); | ||
362 | |||
363 | buffer_out[i+1] = val; | ||
364 | } | ||
365 | } | ||
366 | |||
367 | /* 4 and 8 are very common cases (the only ones i've seen). | ||
368 | |||
369 | The following code is an initial attempt to unroll and optimise | ||
370 | these two cases by the Rockbox project. More work is needed. | ||
371 | */ | ||
372 | |||
373 | /* optimised case: 4 */ | ||
374 | if (predictor_coef_num == 4) | ||
375 | { | ||
376 | for (i = 4 + 1; i < output_size; i++) | ||
377 | { | ||
378 | int sum = 0; | ||
379 | int outval; | ||
380 | int error_val = error_buffer[i]; | ||
381 | |||
382 | sum = (buffer_out[4] - buffer_out[0]) * predictor_coef_table[0] | ||
383 | + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[1] | ||
384 | + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[2] | ||
385 | + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[3]; | ||
386 | |||
387 | outval = (1 << (predictor_quantitization-1)) + sum; | ||
388 | outval = outval >> predictor_quantitization; | ||
389 | outval = outval + buffer_out[0] + error_val; | ||
390 | outval = SIGN_EXTENDED32(outval, readsamplesize); | ||
391 | |||
392 | buffer_out[4+1] = outval; | ||
393 | |||
394 | if (error_val > 0) | ||
395 | { | ||
396 | int predictor_num = 4 - 1; | ||
397 | |||
398 | while (predictor_num >= 0 && error_val > 0) | ||
399 | { | ||
400 | int val = buffer_out[0] - buffer_out[4 - predictor_num]; | ||
401 | |||
402 | if (val!=0) { | ||
403 | if (val < 0) { | ||
404 | predictor_coef_table[predictor_num]++; | ||
405 | val=-val; | ||
406 | } else { | ||
407 | predictor_coef_table[predictor_num]--; | ||
408 | } | ||
409 | error_val -= ((val >> predictor_quantitization) * (4 - predictor_num)); | ||
410 | } | ||
411 | predictor_num--; | ||
412 | } | ||
413 | } | ||
414 | else if (error_val < 0) | ||
415 | { | ||
416 | int predictor_num = 4 - 1; | ||
417 | |||
418 | while (predictor_num >= 0 && error_val < 0) | ||
419 | { | ||
420 | int val = buffer_out[0] - buffer_out[4 - predictor_num]; | ||
421 | |||
422 | if (val != 0) { | ||
423 | if (val > 0) { | ||
424 | predictor_coef_table[predictor_num]++; | ||
425 | val=-val; /* neg value */ | ||
426 | } else { | ||
427 | predictor_coef_table[predictor_num]--; | ||
428 | } | ||
429 | error_val -= ((val >> predictor_quantitization) * (4 - predictor_num)); | ||
430 | } | ||
431 | predictor_num--; | ||
432 | } | ||
433 | } | ||
434 | |||
435 | buffer_out++; | ||
436 | } | ||
437 | return; | ||
438 | } | ||
439 | |||
440 | /* optimised case: 8 */ | ||
441 | if (predictor_coef_num == 8) | ||
442 | { | ||
443 | for (i = 8 + 1; | ||
444 | i < output_size; | ||
445 | i++) | ||
446 | { | ||
447 | int sum; | ||
448 | int outval; | ||
449 | int error_val = error_buffer[i]; | ||
450 | |||
451 | sum = (buffer_out[8] - buffer_out[0]) * predictor_coef_table[0] | ||
452 | + (buffer_out[7] - buffer_out[0]) * predictor_coef_table[1] | ||
453 | + (buffer_out[6] - buffer_out[0]) * predictor_coef_table[2] | ||
454 | + (buffer_out[5] - buffer_out[0]) * predictor_coef_table[3] | ||
455 | + (buffer_out[4] - buffer_out[0]) * predictor_coef_table[4] | ||
456 | + (buffer_out[3] - buffer_out[0]) * predictor_coef_table[5] | ||
457 | + (buffer_out[2] - buffer_out[0]) * predictor_coef_table[6] | ||
458 | + (buffer_out[1] - buffer_out[0]) * predictor_coef_table[7]; | ||
459 | |||
460 | outval = (1 << (predictor_quantitization-1)) + sum; | ||
461 | outval = outval >> predictor_quantitization; | ||
462 | outval = outval + buffer_out[0] + error_val; | ||
463 | outval = SIGN_EXTENDED32(outval, readsamplesize); | ||
464 | |||
465 | buffer_out[8+1] = outval; | ||
466 | |||
467 | if (error_val > 0) | ||
468 | { | ||
469 | int predictor_num = 8 - 1; | ||
470 | |||
471 | while (predictor_num >= 0 && error_val > 0) | ||
472 | { | ||
473 | int val = buffer_out[0] - buffer_out[8 - predictor_num]; | ||
474 | |||
475 | if (val!=0) { | ||
476 | if (val < 0) { | ||
477 | predictor_coef_table[predictor_num]++; | ||
478 | val=-val; | ||
479 | } else { | ||
480 | predictor_coef_table[predictor_num]--; | ||
481 | } | ||
482 | error_val -= ((val >> predictor_quantitization) * (8 - predictor_num)); | ||
483 | } | ||
484 | predictor_num--; | ||
485 | } | ||
486 | } | ||
487 | else if (error_val < 0) | ||
488 | { | ||
489 | int predictor_num = 8 - 1; | ||
490 | |||
491 | while (predictor_num >= 0 && error_val < 0) | ||
492 | { | ||
493 | int val = buffer_out[0] - buffer_out[8 - predictor_num]; | ||
494 | if (val != 0) { | ||
495 | if (val > 0) { | ||
496 | predictor_coef_table[predictor_num]++; | ||
497 | val=-val; /* neg value */ | ||
498 | } else { | ||
499 | predictor_coef_table[predictor_num]--; | ||
500 | } | ||
501 | error_val -= ((val >> predictor_quantitization) * (8 - predictor_num)); | ||
502 | } | ||
503 | predictor_num--; | ||
504 | } | ||
505 | } | ||
506 | |||
507 | buffer_out++; | ||
508 | } | ||
509 | return; | ||
510 | } | ||
511 | |||
512 | /* general case */ | ||
513 | if (predictor_coef_num > 0) | ||
514 | { | ||
515 | for (i = predictor_coef_num + 1; | ||
516 | i < output_size; | ||
517 | i++) | ||
518 | { | ||
519 | int j; | ||
520 | int sum = 0; | ||
521 | int outval; | ||
522 | int error_val = error_buffer[i]; | ||
523 | |||
524 | for (j = 0; j < predictor_coef_num; j++) | ||
525 | { | ||
526 | sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) * | ||
527 | predictor_coef_table[j]; | ||
528 | } | ||
529 | |||
530 | outval = (1 << (predictor_quantitization-1)) + sum; | ||
531 | outval = outval >> predictor_quantitization; | ||
532 | outval = outval + buffer_out[0] + error_val; | ||
533 | outval = SIGN_EXTENDED32(outval, readsamplesize); | ||
534 | |||
535 | buffer_out[predictor_coef_num+1] = outval; | ||
536 | |||
537 | if (error_val > 0) | ||
538 | { | ||
539 | int predictor_num = predictor_coef_num - 1; | ||
540 | |||
541 | while (predictor_num >= 0 && error_val > 0) | ||
542 | { | ||
543 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; | ||
544 | int sign = SIGN_ONLY(val); | ||
545 | |||
546 | predictor_coef_table[predictor_num] -= sign; | ||
547 | |||
548 | val *= sign; /* absolute value */ | ||
549 | |||
550 | error_val -= ((val >> predictor_quantitization) * | ||
551 | (predictor_coef_num - predictor_num)); | ||
552 | |||
553 | predictor_num--; | ||
554 | } | ||
555 | } | ||
556 | else if (error_val < 0) | ||
557 | { | ||
558 | int predictor_num = predictor_coef_num - 1; | ||
559 | |||
560 | while (predictor_num >= 0 && error_val < 0) | ||
561 | { | ||
562 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num]; | ||
563 | int sign = - SIGN_ONLY(val); | ||
564 | |||
565 | predictor_coef_table[predictor_num] -= sign; | ||
566 | |||
567 | val *= sign; /* neg value */ | ||
568 | |||
569 | error_val -= ((val >> predictor_quantitization) * | ||
570 | (predictor_coef_num - predictor_num)); | ||
571 | |||
572 | predictor_num--; | ||
573 | } | ||
574 | } | ||
575 | |||
576 | buffer_out++; | ||
577 | } | ||
578 | } | ||
579 | } | ||
580 | |||
581 | static void deinterlace_16(int32_t* buffer0, | ||
582 | int32_t* buffer1, | ||
583 | int numsamples, | ||
584 | uint8_t interlacing_shift, | ||
585 | uint8_t interlacing_leftweight) ICODE_ATTR_ALAC; | ||
586 | static void deinterlace_16(int32_t* buffer0, | ||
587 | int32_t* buffer1, | ||
588 | int numsamples, | ||
589 | uint8_t interlacing_shift, | ||
590 | uint8_t interlacing_leftweight) | ||
591 | { | ||
592 | int i; | ||
593 | if (numsamples <= 0) return; | ||
594 | |||
595 | /* weighted interlacing */ | ||
596 | if (interlacing_leftweight) | ||
597 | { | ||
598 | for (i = 0; i < numsamples; i++) | ||
599 | { | ||
600 | int32_t difference, midright; | ||
601 | |||
602 | midright = buffer0[i]; | ||
603 | difference = buffer1[i]; | ||
604 | |||
605 | buffer0[i] = ((midright - ((difference * interlacing_leftweight) | ||
606 | >> interlacing_shift)) + difference) << SCALE16; | ||
607 | buffer1[i] = (midright - ((difference * interlacing_leftweight) | ||
608 | >> interlacing_shift)) << SCALE16; | ||
609 | } | ||
610 | |||
611 | return; | ||
612 | } | ||
613 | |||
614 | /* otherwise basic interlacing took place */ | ||
615 | for (i = 0; i < numsamples; i++) | ||
616 | { | ||
617 | buffer0[i] = buffer0[i] << SCALE16; | ||
618 | buffer1[i] = buffer1[i] << SCALE16; | ||
619 | } | ||
620 | } | ||
621 | |||
622 | static void deinterlace_24(int32_t *buffer0, int32_t *buffer1, | ||
623 | int uncompressed_bytes, | ||
624 | int32_t *uncompressed_bytes_buffer0, | ||
625 | int32_t *uncompressed_bytes_buffer1, | ||
626 | int numsamples, | ||
627 | uint8_t interlacing_shift, | ||
628 | uint8_t interlacing_leftweight) ICODE_ATTR_ALAC; | ||
629 | static void deinterlace_24(int32_t *buffer0, int32_t *buffer1, | ||
630 | int uncompressed_bytes, | ||
631 | int32_t *uncompressed_bytes_buffer0, | ||
632 | int32_t *uncompressed_bytes_buffer1, | ||
633 | int numsamples, | ||
634 | uint8_t interlacing_shift, | ||
635 | uint8_t interlacing_leftweight) | ||
636 | { | ||
637 | int i; | ||
638 | if (numsamples <= 0) return; | ||
639 | |||
640 | /* weighted interlacing */ | ||
641 | if (interlacing_leftweight) | ||
642 | { | ||
643 | for (i = 0; i < numsamples; i++) | ||
644 | { | ||
645 | int32_t difference, midright; | ||
646 | |||
647 | midright = buffer0[i]; | ||
648 | difference = buffer1[i]; | ||
649 | |||
650 | buffer0[i] = ((midright - ((difference * interlacing_leftweight) | ||
651 | >> interlacing_shift)) + difference) << SCALE24; | ||
652 | buffer1[i] = (midright - ((difference * interlacing_leftweight) | ||
653 | >> interlacing_shift)) << SCALE24; | ||
654 | |||
655 | if (uncompressed_bytes) | ||
656 | { | ||
657 | uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); | ||
658 | buffer0[i] <<= (uncompressed_bytes * 8); | ||
659 | buffer1[i] <<= (uncompressed_bytes * 8); | ||
660 | |||
661 | buffer0[i] |= uncompressed_bytes_buffer0[i] & mask; | ||
662 | buffer1[i] |= uncompressed_bytes_buffer1[i] & mask; | ||
663 | } | ||
664 | |||
665 | } | ||
666 | |||
667 | return; | ||
668 | } | ||
669 | |||
670 | /* otherwise basic interlacing took place */ | ||
671 | for (i = 0; i < numsamples; i++) | ||
672 | { | ||
673 | if (uncompressed_bytes) | ||
674 | { | ||
675 | uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); | ||
676 | buffer0[i] <<= (uncompressed_bytes * 8); | ||
677 | buffer1[i] <<= (uncompressed_bytes * 8); | ||
678 | |||
679 | buffer0[i] |= uncompressed_bytes_buffer0[i] & mask; | ||
680 | buffer1[i] |= uncompressed_bytes_buffer1[i] & mask; | ||
681 | } | ||
682 | |||
683 | buffer0[i] = buffer0[i] << SCALE24; | ||
684 | buffer1[i] = buffer1[i] << SCALE24; | ||
685 | } | ||
686 | |||
687 | } | ||
688 | |||
689 | static inline int decode_frame_mono( | ||
690 | alac_file *alac, | ||
691 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], | ||
692 | void (*yield)(void)) | ||
693 | { | ||
694 | int hassize; | ||
695 | int isnotcompressed; | ||
696 | int readsamplesize; | ||
697 | int infosamplesize = alac->setinfo_sample_size; | ||
698 | int outputsamples = alac->setinfo_max_samples_per_frame; | ||
699 | |||
700 | int uncompressed_bytes; | ||
701 | int ricemodifier; | ||
702 | |||
703 | |||
704 | /* 2^result = something to do with output waiting. | ||
705 | * perhaps matters if we read > 1 frame in a pass? | ||
706 | */ | ||
707 | readbits(alac, 4); | ||
708 | |||
709 | readbits(alac, 12); /* unknown, skip 12 bits */ | ||
710 | |||
711 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ | ||
712 | |||
713 | /* number of bytes in the (compressed) stream that are not compressed */ | ||
714 | uncompressed_bytes = readbits(alac, 2); | ||
715 | |||
716 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ | ||
717 | |||
718 | if (hassize) | ||
719 | { | ||
720 | /* now read the number of samples, | ||
721 | * as a 32bit integer */ | ||
722 | outputsamples = readbits(alac, 32); | ||
723 | } | ||
724 | |||
725 | readsamplesize = infosamplesize - (uncompressed_bytes * 8); | ||
726 | |||
727 | if (!isnotcompressed) | ||
728 | { /* so it is compressed */ | ||
729 | int predictor_coef_num; | ||
730 | int prediction_type; | ||
731 | int prediction_quantitization; | ||
732 | int i; | ||
733 | |||
734 | /* skip 16 bits, not sure what they are. seem to be used in | ||
735 | * two channel case */ | ||
736 | readbits(alac, 8); | ||
737 | readbits(alac, 8); | ||
738 | |||
739 | prediction_type = readbits(alac, 4); | ||
740 | prediction_quantitization = readbits(alac, 4); | ||
741 | |||
742 | ricemodifier = readbits(alac, 3); | ||
743 | predictor_coef_num = readbits(alac, 5); | ||
744 | |||
745 | /* read the predictor table */ | ||
746 | for (i = 0; i < predictor_coef_num; i++) | ||
747 | { | ||
748 | predictor_coef_table[i] = (int16_t)readbits(alac, 16); | ||
749 | } | ||
750 | |||
751 | if (uncompressed_bytes) | ||
752 | { | ||
753 | int i; | ||
754 | for (i = 0; i < outputsamples; i++) | ||
755 | { | ||
756 | outputbuffer[0][i] = readbits(alac, uncompressed_bytes * 8); | ||
757 | outputbuffer[1][i] = outputbuffer[0][i]; | ||
758 | } | ||
759 | } | ||
760 | |||
761 | yield(); | ||
762 | |||
763 | entropy_rice_decode(alac, | ||
764 | outputbuffer[0], | ||
765 | outputsamples, | ||
766 | readsamplesize, | ||
767 | alac->setinfo_rice_initialhistory, | ||
768 | alac->setinfo_rice_kmodifier, | ||
769 | ricemodifier * alac->setinfo_rice_historymult / 4, | ||
770 | (1 << alac->setinfo_rice_kmodifier) - 1); | ||
771 | |||
772 | yield(); | ||
773 | |||
774 | if (prediction_type == 0) | ||
775 | { /* adaptive fir */ | ||
776 | predictor_decompress_fir_adapt(outputbuffer[0], | ||
777 | outputbuffer[0], | ||
778 | outputsamples, | ||
779 | readsamplesize, | ||
780 | predictor_coef_table, | ||
781 | predictor_coef_num, | ||
782 | prediction_quantitization); | ||
783 | } | ||
784 | else | ||
785 | { | ||
786 | //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type); | ||
787 | /* i think the only other prediction type (or perhaps this is just a | ||
788 | * boolean?) runs adaptive fir twice.. like: | ||
789 | * predictor_decompress_fir_adapt(predictor_error, tempout, ...) | ||
790 | * predictor_decompress_fir_adapt(predictor_error, outputsamples ...) | ||
791 | * little strange.. | ||
792 | */ | ||
793 | } | ||
794 | |||
795 | } | ||
796 | else | ||
797 | { /* not compressed, easy case */ | ||
798 | if (infosamplesize <= 16) | ||
799 | { | ||
800 | int i; | ||
801 | for (i = 0; i < outputsamples; i++) | ||
802 | { | ||
803 | int32_t audiobits = readbits(alac, infosamplesize); | ||
804 | |||
805 | audiobits = SIGN_EXTENDED32(audiobits, infosamplesize); | ||
806 | |||
807 | outputbuffer[0][i] = audiobits; | ||
808 | } | ||
809 | } | ||
810 | else | ||
811 | { | ||
812 | int i; | ||
813 | for (i = 0; i < outputsamples; i++) | ||
814 | { | ||
815 | int32_t audiobits; | ||
816 | |||
817 | audiobits = readbits(alac, 16); | ||
818 | /* special case of sign extension.. | ||
819 | * as we'll be ORing the low 16bits into this */ | ||
820 | audiobits = audiobits << (infosamplesize - 16); | ||
821 | audiobits |= readbits(alac, infosamplesize - 16); | ||
822 | audiobits = SIGNEXTEND24(audiobits); | ||
823 | |||
824 | outputbuffer[0][i] = audiobits; | ||
825 | } | ||
826 | } | ||
827 | uncompressed_bytes = 0; // always 0 for uncompressed | ||
828 | } | ||
829 | |||
830 | yield(); | ||
831 | |||
832 | switch(infosamplesize) | ||
833 | { | ||
834 | case 16: | ||
835 | { | ||
836 | int i; | ||
837 | for (i = 0; i < outputsamples; i++) | ||
838 | { | ||
839 | /* Output mono data as stereo */ | ||
840 | outputbuffer[0][i] = outputbuffer[0][i] << SCALE16; | ||
841 | outputbuffer[1][i] = outputbuffer[0][i]; | ||
842 | } | ||
843 | break; | ||
844 | } | ||
845 | case 24: | ||
846 | { | ||
847 | int i; | ||
848 | for (i = 0; i < outputsamples; i++) | ||
849 | { | ||
850 | int32_t sample = outputbuffer[0][i]; | ||
851 | |||
852 | if (uncompressed_bytes) | ||
853 | { | ||
854 | uint32_t mask; | ||
855 | sample = sample << (uncompressed_bytes * 8); | ||
856 | mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8)); | ||
857 | sample |= outputbuffer[0][i] & mask; | ||
858 | } | ||
859 | |||
860 | outputbuffer[0][i] = sample << SCALE24; | ||
861 | outputbuffer[1][i] = outputbuffer[0][i]; | ||
862 | } | ||
863 | break; | ||
864 | } | ||
865 | case 20: | ||
866 | case 32: | ||
867 | //fprintf(stderr, "FIXME: unimplemented sample size %i\n", infosamplesize); | ||
868 | break; | ||
869 | default: | ||
870 | break; | ||
871 | } | ||
872 | |||
873 | return outputsamples; | ||
874 | } | ||
875 | |||
876 | static inline int decode_frame_stereo( | ||
877 | alac_file *alac, | ||
878 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], | ||
879 | void (*yield)(void)) | ||
880 | { | ||
881 | int hassize; | ||
882 | int isnotcompressed; | ||
883 | int readsamplesize; | ||
884 | int infosamplesize = alac->setinfo_sample_size; | ||
885 | int outputsamples = alac->setinfo_max_samples_per_frame; | ||
886 | int uncompressed_bytes; | ||
887 | |||
888 | uint8_t interlacing_shift; | ||
889 | uint8_t interlacing_leftweight; | ||
890 | |||
891 | /* 2^result = something to do with output waiting. | ||
892 | * perhaps matters if we read > 1 frame in a pass? | ||
893 | */ | ||
894 | readbits(alac, 4); | ||
895 | |||
896 | readbits(alac, 12); /* unknown, skip 12 bits */ | ||
897 | |||
898 | hassize = readbits(alac, 1); /* the output sample size is stored soon */ | ||
899 | |||
900 | /* the number of bytes in the (compressed) stream that are not compressed */ | ||
901 | uncompressed_bytes = readbits(alac, 2); | ||
902 | |||
903 | isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */ | ||
904 | |||
905 | if (hassize) | ||
906 | { | ||
907 | /* now read the number of samples, | ||
908 | * as a 32bit integer */ | ||
909 | outputsamples = readbits(alac, 32); | ||
910 | } | ||
911 | |||
912 | readsamplesize = infosamplesize - (uncompressed_bytes * 8) + 1; | ||
913 | |||
914 | yield(); | ||
915 | if (!isnotcompressed) | ||
916 | { /* compressed */ | ||
917 | int predictor_coef_num_a; | ||
918 | int prediction_type_a; | ||
919 | int prediction_quantitization_a; | ||
920 | int ricemodifier_a; | ||
921 | |||
922 | int predictor_coef_num_b; | ||
923 | int prediction_type_b; | ||
924 | int prediction_quantitization_b; | ||
925 | int ricemodifier_b; | ||
926 | |||
927 | int i; | ||
928 | |||
929 | interlacing_shift = readbits(alac, 8); | ||
930 | interlacing_leftweight = readbits(alac, 8); | ||
931 | |||
932 | /******** channel 1 ***********/ | ||
933 | prediction_type_a = readbits(alac, 4); | ||
934 | prediction_quantitization_a = readbits(alac, 4); | ||
935 | |||
936 | ricemodifier_a = readbits(alac, 3); | ||
937 | predictor_coef_num_a = readbits(alac, 5); | ||
938 | |||
939 | /* read the predictor table */ | ||
940 | for (i = 0; i < predictor_coef_num_a; i++) | ||
941 | { | ||
942 | predictor_coef_table_a[i] = (int16_t)readbits(alac, 16); | ||
943 | } | ||
944 | |||
945 | /******** channel 2 *********/ | ||
946 | prediction_type_b = readbits(alac, 4); | ||
947 | prediction_quantitization_b = readbits(alac, 4); | ||
948 | |||
949 | ricemodifier_b = readbits(alac, 3); | ||
950 | predictor_coef_num_b = readbits(alac, 5); | ||
951 | |||
952 | /* read the predictor table */ | ||
953 | for (i = 0; i < predictor_coef_num_b; i++) | ||
954 | { | ||
955 | predictor_coef_table_b[i] = (int16_t)readbits(alac, 16); | ||
956 | } | ||
957 | |||
958 | /*********************/ | ||
959 | if (uncompressed_bytes) | ||
960 | { /* see mono case */ | ||
961 | int i; | ||
962 | for (i = 0; i < outputsamples; i++) | ||
963 | { | ||
964 | outputbuffer[0][i] = readbits(alac, uncompressed_bytes * 8); | ||
965 | outputbuffer[1][i] = readbits(alac, uncompressed_bytes * 8); | ||
966 | } | ||
967 | } | ||
968 | |||
969 | yield(); | ||
970 | /* channel 1 */ | ||
971 | entropy_rice_decode(alac, | ||
972 | outputbuffer[0], | ||
973 | outputsamples, | ||
974 | readsamplesize, | ||
975 | alac->setinfo_rice_initialhistory, | ||
976 | alac->setinfo_rice_kmodifier, | ||
977 | ricemodifier_a * alac->setinfo_rice_historymult / 4, | ||
978 | (1 << alac->setinfo_rice_kmodifier) - 1); | ||
979 | |||
980 | yield(); | ||
981 | if (prediction_type_a == 0) | ||
982 | { /* adaptive fir */ | ||
983 | predictor_decompress_fir_adapt(outputbuffer[0], | ||
984 | outputbuffer[0], | ||
985 | outputsamples, | ||
986 | readsamplesize, | ||
987 | predictor_coef_table_a, | ||
988 | predictor_coef_num_a, | ||
989 | prediction_quantitization_a); | ||
990 | } | ||
991 | else | ||
992 | { /* see mono case */ | ||
993 | //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_a); | ||
994 | } | ||
995 | |||
996 | yield(); | ||
997 | |||
998 | /* channel 2 */ | ||
999 | entropy_rice_decode(alac, | ||
1000 | outputbuffer[1], | ||
1001 | outputsamples, | ||
1002 | readsamplesize, | ||
1003 | alac->setinfo_rice_initialhistory, | ||
1004 | alac->setinfo_rice_kmodifier, | ||
1005 | ricemodifier_b * alac->setinfo_rice_historymult / 4, | ||
1006 | (1 << alac->setinfo_rice_kmodifier) - 1); | ||
1007 | |||
1008 | yield(); | ||
1009 | if (prediction_type_b == 0) | ||
1010 | { /* adaptive fir */ | ||
1011 | predictor_decompress_fir_adapt(outputbuffer[1], | ||
1012 | outputbuffer[1], | ||
1013 | outputsamples, | ||
1014 | readsamplesize, | ||
1015 | predictor_coef_table_b, | ||
1016 | predictor_coef_num_b, | ||
1017 | prediction_quantitization_b); | ||
1018 | } | ||
1019 | else | ||
1020 | { | ||
1021 | //fprintf(stderr, "FIXME: unhandled predicition type: %i\n", prediction_type_b); | ||
1022 | } | ||
1023 | } | ||
1024 | else | ||
1025 | { /* not compressed, easy case */ | ||
1026 | if (infosamplesize <= 16) | ||
1027 | { | ||
1028 | int i; | ||
1029 | for (i = 0; i < outputsamples; i++) | ||
1030 | { | ||
1031 | int32_t audiobits_a, audiobits_b; | ||
1032 | |||
1033 | audiobits_a = readbits(alac, infosamplesize); | ||
1034 | audiobits_b = readbits(alac, infosamplesize); | ||
1035 | |||
1036 | audiobits_a = SIGN_EXTENDED32(audiobits_a, infosamplesize); | ||
1037 | audiobits_b = SIGN_EXTENDED32(audiobits_b, infosamplesize); | ||
1038 | |||
1039 | outputbuffer[0][i] = audiobits_a; | ||
1040 | outputbuffer[1][i] = audiobits_b; | ||
1041 | } | ||
1042 | } | ||
1043 | else | ||
1044 | { | ||
1045 | int i; | ||
1046 | for (i = 0; i < outputsamples; i++) | ||
1047 | { | ||
1048 | int32_t audiobits_a, audiobits_b; | ||
1049 | |||
1050 | audiobits_a = readbits(alac, 16); | ||
1051 | audiobits_a = audiobits_a << (infosamplesize - 16); | ||
1052 | audiobits_a |= readbits(alac, infosamplesize - 16); | ||
1053 | audiobits_a = SIGNEXTEND24(audiobits_a); | ||
1054 | |||
1055 | audiobits_b = readbits(alac, 16); | ||
1056 | audiobits_b = audiobits_b << (infosamplesize - 16); | ||
1057 | audiobits_b |= readbits(alac, infosamplesize - 16); | ||
1058 | audiobits_b = SIGNEXTEND24(audiobits_b); | ||
1059 | |||
1060 | outputbuffer[0][i] = audiobits_a; | ||
1061 | outputbuffer[1][i] = audiobits_b; | ||
1062 | } | ||
1063 | } | ||
1064 | uncompressed_bytes = 0; // always 0 for uncompressed | ||
1065 | interlacing_shift = 0; | ||
1066 | interlacing_leftweight = 0; | ||
1067 | } | ||
1068 | |||
1069 | yield(); | ||
1070 | |||
1071 | switch(infosamplesize) | ||
1072 | { | ||
1073 | case 16: | ||
1074 | { | ||
1075 | deinterlace_16(outputbuffer[0], | ||
1076 | outputbuffer[1], | ||
1077 | outputsamples, | ||
1078 | interlacing_shift, | ||
1079 | interlacing_leftweight); | ||
1080 | break; | ||
1081 | } | ||
1082 | case 24: | ||
1083 | { | ||
1084 | deinterlace_24(outputbuffer[0], | ||
1085 | outputbuffer[1], | ||
1086 | uncompressed_bytes, | ||
1087 | outputbuffer[0], | ||
1088 | outputbuffer[1], | ||
1089 | outputsamples, | ||
1090 | interlacing_shift, | ||
1091 | interlacing_leftweight); | ||
1092 | break; | ||
1093 | } | ||
1094 | case 20: | ||
1095 | case 32: | ||
1096 | //fprintf(stderr, "FIXME: unimplemented sample size %i\n", infosamplesize); | ||
1097 | break; | ||
1098 | default: | ||
1099 | break; | ||
1100 | } | ||
1101 | return outputsamples; | ||
1102 | } | ||
1103 | |||
1104 | int alac_decode_frame(alac_file *alac, | ||
1105 | unsigned char *inbuffer, | ||
1106 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], | ||
1107 | void (*yield)(void)) | ||
1108 | { | ||
1109 | int channels; | ||
1110 | int outputsamples; | ||
1111 | unsigned char *input_buffer_start; | ||
1112 | |||
1113 | /* setup the stream */ | ||
1114 | alac->input_buffer = inbuffer; | ||
1115 | alac->input_buffer_bitaccumulator = 0; | ||
1116 | |||
1117 | /* save to gather byte consumption */ | ||
1118 | input_buffer_start = alac->input_buffer; | ||
1119 | |||
1120 | channels = readbits(alac, 3); | ||
1121 | |||
1122 | /* TODO: The mono and stereo functions should be combined. */ | ||
1123 | switch(channels) | ||
1124 | { | ||
1125 | case 0: /* 1 channel */ | ||
1126 | outputsamples=decode_frame_mono(alac,outputbuffer,yield); | ||
1127 | break; | ||
1128 | case 1: /* 2 channels */ | ||
1129 | outputsamples=decode_frame_stereo(alac,outputbuffer,yield); | ||
1130 | break; | ||
1131 | default: /* Unsupported */ | ||
1132 | return -1; | ||
1133 | } | ||
1134 | |||
1135 | /* calculate consumed bytes */ | ||
1136 | alac->bytes_consumed = (int)(alac->input_buffer - input_buffer_start); | ||
1137 | alac->bytes_consumed += (alac->input_buffer_bitaccumulator>5) ? 2 : 1; | ||
1138 | |||
1139 | return outputsamples; | ||
1140 | } | ||
1141 | |||
1142 | /* rockbox: not used | ||
1143 | void create_alac(int samplesize, int numchannels, alac_file* alac) | ||
1144 | { | ||
1145 | alac->samplesize = samplesize; | ||
1146 | alac->numchannels = numchannels; | ||
1147 | alac->bytespersample = (samplesize / 8) * numchannels; | ||
1148 | } */ | ||
diff --git a/lib/rbcodec/codecs/libalac/decomp.h b/lib/rbcodec/codecs/libalac/decomp.h new file mode 100644 index 0000000000..21dabd86b0 --- /dev/null +++ b/lib/rbcodec/codecs/libalac/decomp.h | |||
@@ -0,0 +1,54 @@ | |||
1 | #ifndef __ALAC__DECOMP_H | ||
2 | #define __ALAC__DECOMP_H | ||
3 | |||
4 | #ifndef ICODE_ATTR_ALAC | ||
5 | #define ICODE_ATTR_ALAC ICODE_ATTR | ||
6 | #endif | ||
7 | |||
8 | /* Always output samples shifted to 28 bits + sign*/ | ||
9 | #define ALAC_OUTPUT_DEPTH 29 | ||
10 | #define SCALE16 (ALAC_OUTPUT_DEPTH - 16) | ||
11 | #define SCALE24 (ALAC_OUTPUT_DEPTH - 24) | ||
12 | #define ALAC_MAX_CHANNELS 2 | ||
13 | #define ALAC_BLOCKSIZE 4096 /* Number of samples per channel per block */ | ||
14 | |||
15 | typedef struct | ||
16 | { | ||
17 | unsigned char *input_buffer; | ||
18 | int input_buffer_bitaccumulator; /* used so we can do arbitary | ||
19 | bit reads */ | ||
20 | |||
21 | /* rockbox: not used | ||
22 | int samplesize; | ||
23 | int numchannels; | ||
24 | int bytespersample; */ | ||
25 | |||
26 | int bytes_consumed; | ||
27 | |||
28 | /* stuff from setinfo */ | ||
29 | uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */ | ||
30 | uint8_t setinfo_7a; /* 0x00 */ | ||
31 | uint8_t setinfo_sample_size; /* 0x10 */ | ||
32 | uint8_t setinfo_rice_historymult; /* 0x28 */ | ||
33 | uint8_t setinfo_rice_initialhistory; /* 0x0a */ | ||
34 | uint8_t setinfo_rice_kmodifier; /* 0x0e */ | ||
35 | uint8_t setinfo_7f; /* 0x02 */ | ||
36 | uint16_t setinfo_80; /* 0x00ff */ | ||
37 | uint32_t setinfo_82; /* 0x000020e7 */ | ||
38 | uint32_t setinfo_86; /* 0x00069fe4 */ | ||
39 | uint32_t setinfo_8a_rate; /* 0x0000ac44 */ | ||
40 | /* end setinfo stuff */ | ||
41 | } alac_file; | ||
42 | |||
43 | /* rockbox: not used | ||
44 | void create_alac(int samplesize, int numchannels, alac_file* alac) | ||
45 | ICODE_ATTR_ALAC; */ | ||
46 | |||
47 | int alac_decode_frame(alac_file *alac, | ||
48 | unsigned char *inbuffer, | ||
49 | int32_t outputbuffer[ALAC_MAX_CHANNELS][ALAC_BLOCKSIZE], | ||
50 | void (*yield)(void)) ICODE_ATTR_ALAC; | ||
51 | void alac_set_info(alac_file *alac, char *inputbuffer) ICODE_ATTR_ALAC; | ||
52 | |||
53 | #endif /* __ALAC__DECOMP_H */ | ||
54 | |||
diff --git a/lib/rbcodec/codecs/libalac/libalac.make b/lib/rbcodec/codecs/libalac/libalac.make new file mode 100644 index 0000000000..8b8a842f8e --- /dev/null +++ b/lib/rbcodec/codecs/libalac/libalac.make | |||
@@ -0,0 +1,18 @@ | |||
1 | # __________ __ ___. | ||
2 | # Open \______ \ ____ ____ | | _\_ |__ _______ ___ | ||
3 | # Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ / | ||
4 | # Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < < | ||
5 | # Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \ | ||
6 | # \/ \/ \/ \/ \/ | ||
7 | # $Id$ | ||
8 | # | ||
9 | |||
10 | # libalac | ||
11 | ALACLIB := $(CODECDIR)/libalac.a | ||
12 | ALACLIB_SRC := $(call preprocess, $(RBCODECLIB_DIR)/codecs/libalac/SOURCES) | ||
13 | ALACLIB_OBJ := $(call c2obj, $(ALACLIB_SRC)) | ||
14 | OTHER_SRC += $(ALACLIB_SRC) | ||
15 | |||
16 | $(ALACLIB): $(ALACLIB_OBJ) | ||
17 | $(SILENT)$(shell rm -f $@) | ||
18 | $(call PRINTS,AR $(@F))$(AR) rcs $@ $^ >/dev/null | ||