]> git.ipfire.org Git - thirdparty/shairport-sync.git/blob - alac.c
Update check_classic_systemd_full.yml
[thirdparty/shairport-sync.git] / alac.c
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 static const int host_bigendian = 0;
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef _WIN32
38 #include "stdint_win.h"
39 #else
40 #include <stdint.h>
41 #endif
42
43 #include "alac.h"
44
45 #define _Swap32(v) \
46 do { \
47 v = (((v)&0x000000FF) << 0x18) | (((v)&0x0000FF00) << 0x08) | (((v)&0x00FF0000) >> 0x08) | \
48 (((v)&0xFF000000) >> 0x18); \
49 } while (0)
50
51 #define _Swap16(v) \
52 do { \
53 v = (((v)&0x00FF) << 0x08) | (((v)&0xFF00) >> 0x08); \
54 } while (0)
55
56 struct {
57 signed int x : 24;
58 } se_struct_24;
59 #define SignExtend24(val) (se_struct_24.x = val)
60
61 void alac_free(alac_file *alac) {
62 if (alac->predicterror_buffer_a)
63 free(alac->predicterror_buffer_a);
64 if (alac->predicterror_buffer_b)
65 free(alac->predicterror_buffer_b);
66
67 if (alac->outputsamples_buffer_a)
68 free(alac->outputsamples_buffer_a);
69 if (alac->outputsamples_buffer_b)
70 free(alac->outputsamples_buffer_b);
71
72 if (alac->uncompressed_bytes_buffer_a)
73 free(alac->uncompressed_bytes_buffer_a);
74 if (alac->uncompressed_bytes_buffer_b)
75 free(alac->uncompressed_bytes_buffer_b);
76
77 free(alac);
78 }
79
80 void alac_allocate_buffers(alac_file *alac) {
81 alac->predicterror_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4);
82 alac->predicterror_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);
83
84 alac->outputsamples_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4);
85 alac->outputsamples_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);
86
87 alac->uncompressed_bytes_buffer_a = malloc(alac->setinfo_max_samples_per_frame * 4);
88 alac->uncompressed_bytes_buffer_b = malloc(alac->setinfo_max_samples_per_frame * 4);
89 }
90
91 void alac_set_info(alac_file *alac, char *inputbuffer) {
92 char *ptr = inputbuffer;
93 ptr += 4; /* size */
94 ptr += 4; /* frma */
95 ptr += 4; /* alac */
96 ptr += 4; /* size */
97 ptr += 4; /* alac */
98
99 ptr += 4; /* 0 ? */
100
101 alac->setinfo_max_samples_per_frame = *(uint32_t *)ptr; /* buffer size / 2 ? */
102 if (!host_bigendian)
103 _Swap32(alac->setinfo_max_samples_per_frame);
104 ptr += 4;
105 alac->setinfo_7a = *(uint8_t *)ptr;
106 ptr += 1;
107 alac->setinfo_sample_size = *(uint8_t *)ptr;
108 ptr += 1;
109 alac->setinfo_rice_historymult = *(uint8_t *)ptr;
110 ptr += 1;
111 alac->setinfo_rice_initialhistory = *(uint8_t *)ptr;
112 ptr += 1;
113 alac->setinfo_rice_kmodifier = *(uint8_t *)ptr;
114 ptr += 1;
115 alac->setinfo_7f = *(uint8_t *)ptr;
116 ptr += 1;
117 alac->setinfo_80 = *(uint16_t *)ptr;
118 if (!host_bigendian)
119 _Swap16(alac->setinfo_80);
120 ptr += 2;
121 alac->setinfo_82 = *(uint32_t *)ptr;
122 if (!host_bigendian)
123 _Swap32(alac->setinfo_82);
124 ptr += 4;
125 alac->setinfo_86 = *(uint32_t *)ptr;
126 if (!host_bigendian)
127 _Swap32(alac->setinfo_86);
128 ptr += 4;
129 alac->setinfo_8a_rate = *(uint32_t *)ptr;
130 if (!host_bigendian)
131 _Swap32(alac->setinfo_8a_rate);
132
133 alac_allocate_buffers(alac);
134 }
135
136 /* stream reading */
137
138 /* supports reading 1 to 16 bits, in big endian format */
139 static uint32_t readbits_16(alac_file *alac, int bits) {
140 uint32_t result;
141 int new_accumulator;
142
143 result = (alac->input_buffer[0] << 16) | (alac->input_buffer[1] << 8) | (alac->input_buffer[2]);
144
145 /* shift left by the number of bits we've already read,
146 * so that the top 'n' bits of the 24 bits we read will
147 * be the return bits */
148 result = result << alac->input_buffer_bitaccumulator;
149
150 result = result & 0x00ffffff;
151
152 /* and then only want the top 'n' bits from that, where
153 * n is 'bits' */
154 result = result >> (24 - bits);
155
156 new_accumulator = (alac->input_buffer_bitaccumulator + bits);
157
158 /* increase the buffer pointer if we've read over n bytes. */
159 alac->input_buffer += (new_accumulator >> 3);
160
161 /* and the remainder goes back into the bit accumulator */
162 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
163
164 return result;
165 }
166
167 /* supports reading 1 to 32 bits, in big endian format */
168 static uint32_t readbits(alac_file *alac, int bits) {
169 int32_t result = 0;
170
171 if (bits > 16) {
172 bits -= 16;
173 result = readbits_16(alac, 16) << bits;
174 }
175
176 result |= readbits_16(alac, bits);
177
178 return result;
179 }
180
181 /* reads a single bit */
182 static int readbit(alac_file *alac) {
183 int result;
184 int new_accumulator;
185
186 result = alac->input_buffer[0];
187
188 result = result << alac->input_buffer_bitaccumulator;
189
190 result = result >> 7 & 1;
191
192 new_accumulator = (alac->input_buffer_bitaccumulator + 1);
193
194 alac->input_buffer += (new_accumulator / 8);
195
196 alac->input_buffer_bitaccumulator = (new_accumulator % 8);
197
198 return result;
199 }
200
201 static void unreadbits(alac_file *alac, int bits) {
202 int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
203
204 alac->input_buffer += (new_accumulator >> 3);
205
206 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
207 if (alac->input_buffer_bitaccumulator < 0)
208 alac->input_buffer_bitaccumulator *= -1;
209 }
210
211 /* various implementations of count_leading_zero:
212 * the first one is the original one, the simplest and most
213 * obvious for what it's doing. never use this.
214 * then there are the asm ones. fill in as necessary
215 * and finally an unrolled and optimised c version
216 * to fall back to
217 */
218 #if 0
219 /* hideously inefficient. could use a bitmask search,
220 * alternatively bsr on x86,
221 */
222 static int count_leading_zeros(int32_t input)
223 {
224 int i = 0;
225 while (!(0x80000000 & input) && i < 32)
226 {
227 i++;
228 input = input << 1;
229 }
230 return i;
231 }
232 #elif defined(__GNUC__)
233 /* for some reason the unrolled version (below) is
234 * actually faster than this. yay intel!
235 */
236 static int count_leading_zeros(int input) { return __builtin_clz(input); }
237 #elif defined(_MSC_VER) && defined(_M_IX86)
238 static int count_leading_zeros(int input) {
239 int output = 0;
240 if (!input)
241 return 32;
242 __asm
243 {
244 mov eax, input;
245 mov edx, 0x1f;
246 bsr ecx, eax;
247 sub edx, ecx;
248 mov output, edx;
249 }
250 return output;
251 }
252 #else
253 #warning using generic count leading zeroes. You may wish to write one for your CPU / compiler
254 static int count_leading_zeros(int input) {
255 int output = 0;
256 int curbyte = 0;
257
258 curbyte = input >> 24;
259 if (curbyte)
260 goto found;
261 output += 8;
262
263 curbyte = input >> 16;
264 if (curbyte & 0xff)
265 goto found;
266 output += 8;
267
268 curbyte = input >> 8;
269 if (curbyte & 0xff)
270 goto found;
271 output += 8;
272
273 curbyte = input;
274 if (curbyte & 0xff)
275 goto found;
276 output += 8;
277
278 return output;
279
280 found:
281 if (!(curbyte & 0xf0)) {
282 output += 4;
283 } else
284 curbyte >>= 4;
285
286 if (curbyte & 0x8)
287 return output;
288 if (curbyte & 0x4)
289 return output + 1;
290 if (curbyte & 0x2)
291 return output + 2;
292 if (curbyte & 0x1)
293 return output + 3;
294
295 /* shouldn't get here: */
296 return output + 4;
297 }
298 #endif
299
300 #define RICE_THRESHOLD 8 // maximum number of bits for a rice prefix.
301
302 static int32_t entropy_decode_value(alac_file *alac, int readSampleSize, int k,
303 int rice_kmodifier_mask) {
304 int32_t x = 0; // decoded value
305
306 // read x, number of 1s before 0 represent the rice value.
307 while (x <= RICE_THRESHOLD && readbit(alac)) {
308 x++;
309 }
310
311 if (x > RICE_THRESHOLD) {
312 // read the number from the bit stream (raw value)
313 int32_t value;
314
315 value = readbits(alac, readSampleSize);
316
317 // mask value
318 value &= (((uint32_t)0xffffffff) >> (32 - readSampleSize));
319
320 x = value;
321 } else {
322 if (k != 1) {
323 int extraBits = readbits(alac, k);
324
325 // x = x * (2^k - 1)
326 x *= (((1 << k) - 1) & rice_kmodifier_mask);
327
328 if (extraBits > 1)
329 x += extraBits - 1;
330 else
331 unreadbits(alac, 1);
332 }
333 }
334
335 return x;
336 }
337
338 static void entropy_rice_decode(alac_file *alac, int32_t *outputBuffer, int outputSize,
339 int readSampleSize, int rice_initialhistory, int rice_kmodifier,
340 int rice_historymult, int rice_kmodifier_mask) {
341 int outputCount;
342 int history = rice_initialhistory;
343 int signModifier = 0;
344
345 for (outputCount = 0; outputCount < outputSize; outputCount++) {
346 int32_t decodedValue;
347 int32_t finalValue;
348 int32_t k;
349
350 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
351
352 if (k < 0)
353 k += rice_kmodifier;
354 else
355 k = rice_kmodifier;
356
357 // note: don't use rice_kmodifier_mask here (set mask to 0xFFFFFFFF)
358 decodedValue = entropy_decode_value(alac, readSampleSize, k, 0xFFFFFFFF);
359
360 decodedValue += signModifier;
361 finalValue = (decodedValue + 1) / 2; // inc by 1 and shift out sign bit
362 if (decodedValue & 1) // the sign is stored in the low bit
363 finalValue *= -1;
364
365 outputBuffer[outputCount] = finalValue;
366
367 signModifier = 0;
368
369 // update history
370 history += (decodedValue * rice_historymult) - ((history * rice_historymult) >> 9);
371
372 if (decodedValue > 0xFFFF)
373 history = 0xFFFF;
374
375 // special case, for compressed blocks of 0
376 if ((history < 128) && (outputCount + 1 < outputSize)) {
377 int32_t blockSize;
378
379 signModifier = 1;
380
381 k = count_leading_zeros(history) + ((history + 16) / 64) - 24;
382
383 // note: blockSize is always 16bit
384 blockSize = entropy_decode_value(alac, 16, k, rice_kmodifier_mask);
385
386 // got blockSize 0s
387 if (blockSize > 0) {
388 memset(&outputBuffer[outputCount + 1], 0, blockSize * sizeof(*outputBuffer));
389 outputCount += blockSize;
390 }
391
392 if (blockSize > 0xFFFF)
393 signModifier = 0;
394
395 history = 0;
396 }
397 }
398 }
399
400 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
401
402 #define SIGN_ONLY(v) ((v < 0) ? (-1) : ((v > 0) ? (1) : (0)))
403
404 static void predictor_decompress_fir_adapt(int32_t *error_buffer, int32_t *buffer_out,
405 int output_size, int readsamplesize,
406 int16_t *predictor_coef_table, int predictor_coef_num,
407 int predictor_quantitization) {
408 int i;
409
410 /* first sample always copies */
411 *buffer_out = *error_buffer;
412
413 if (!predictor_coef_num) {
414 if (output_size <= 1)
415 return;
416 memcpy(buffer_out + 1, error_buffer + 1, (output_size - 1) * 4);
417 return;
418 }
419
420 if (predictor_coef_num == 0x1f) /* 11111 - max value of predictor_coef_num */
421 { /* second-best case scenario for fir decompression,
422 * error describes a small difference from the previous sample only
423 */
424 if (output_size <= 1)
425 return;
426 for (i = 0; i < output_size - 1; i++) {
427 int32_t prev_value;
428 int32_t error_value;
429
430 prev_value = buffer_out[i];
431 error_value = error_buffer[i + 1];
432 buffer_out[i + 1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
433 }
434 return;
435 }
436
437 /* read warm-up samples */
438 if (predictor_coef_num > 0) {
439 int i;
440 for (i = 0; i < predictor_coef_num; i++) {
441 int32_t val;
442
443 val = buffer_out[i] + error_buffer[i + 1];
444
445 val = SIGN_EXTENDED32(val, readsamplesize);
446
447 buffer_out[i + 1] = val;
448 }
449 }
450
451 #if 0
452 /* 4 and 8 are very common cases (the only ones i've seen). these
453 * should be unrolled and optimised
454 */
455 if (predictor_coef_num == 4)
456 {
457 /* FIXME: optimised general case */
458 return;
459 }
460
461 if (predictor_coef_table == 8)
462 {
463 /* FIXME: optimised general case */
464 return;
465 }
466 #endif
467
468 /* general case */
469 if (predictor_coef_num > 0) {
470 for (i = predictor_coef_num + 1; i < output_size; i++) {
471 int j;
472 int sum = 0;
473 int outval;
474 int error_val = error_buffer[i];
475
476 for (j = 0; j < predictor_coef_num; j++) {
477 sum += (buffer_out[predictor_coef_num - j] - buffer_out[0]) * predictor_coef_table[j];
478 }
479
480 outval = (1 << (predictor_quantitization - 1)) + sum;
481 outval = outval >> predictor_quantitization;
482 outval = outval + buffer_out[0] + error_val;
483 outval = SIGN_EXTENDED32(outval, readsamplesize);
484
485 buffer_out[predictor_coef_num + 1] = outval;
486
487 if (error_val > 0) {
488 int predictor_num = predictor_coef_num - 1;
489
490 while (predictor_num >= 0 && error_val > 0) {
491 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
492 int sign = SIGN_ONLY(val);
493
494 predictor_coef_table[predictor_num] -= sign;
495
496 val *= sign; /* absolute value */
497
498 error_val -= ((val >> predictor_quantitization) * (predictor_coef_num - predictor_num));
499
500 predictor_num--;
501 }
502 } else if (error_val < 0) {
503 int predictor_num = predictor_coef_num - 1;
504
505 while (predictor_num >= 0 && error_val < 0) {
506 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
507 int sign = -SIGN_ONLY(val);
508
509 predictor_coef_table[predictor_num] -= sign;
510
511 val *= sign; /* neg value */
512
513 error_val -= ((val >> predictor_quantitization) * (predictor_coef_num - predictor_num));
514
515 predictor_num--;
516 }
517 }
518
519 buffer_out++;
520 }
521 }
522 }
523
524 static void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b, int16_t *buffer_out,
525 int numchannels, int numsamples, uint8_t interlacing_shift,
526 uint8_t interlacing_leftweight) {
527 int i;
528 if (numsamples <= 0)
529 return;
530
531 /* weighted interlacing */
532 if (interlacing_leftweight) {
533 for (i = 0; i < numsamples; i++) {
534 int32_t difference, midright;
535 int16_t left;
536 int16_t right;
537
538 midright = buffer_a[i];
539 difference = buffer_b[i];
540
541 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
542 left = right + difference;
543
544 /* output is always little endian */
545 if (host_bigendian) {
546 _Swap16(left);
547 _Swap16(right);
548 }
549
550 buffer_out[i * numchannels] = left;
551 buffer_out[i * numchannels + 1] = right;
552 }
553
554 return;
555 }
556
557 /* otherwise basic interlacing took place */
558 for (i = 0; i < numsamples; i++) {
559 int16_t left, right;
560
561 left = buffer_a[i];
562 right = buffer_b[i];
563
564 /* output is always little endian */
565 if (host_bigendian) {
566 _Swap16(left);
567 _Swap16(right);
568 }
569
570 buffer_out[i * numchannels] = left;
571 buffer_out[i * numchannels + 1] = right;
572 }
573 }
574
575 static void deinterlace_24(int32_t *buffer_a, int32_t *buffer_b, int uncompressed_bytes,
576 int32_t *uncompressed_bytes_buffer_a,
577 int32_t *uncompressed_bytes_buffer_b, void *buffer_out, int numchannels,
578 int numsamples, uint8_t interlacing_shift,
579 uint8_t interlacing_leftweight) {
580 int i;
581 if (numsamples <= 0)
582 return;
583
584 /* weighted interlacing */
585 if (interlacing_leftweight) {
586 for (i = 0; i < numsamples; i++) {
587 int32_t difference, midright;
588 int32_t left;
589 int32_t right;
590
591 midright = buffer_a[i];
592 difference = buffer_b[i];
593
594 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
595 left = right + difference;
596
597 if (uncompressed_bytes) {
598 uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
599 left <<= (uncompressed_bytes * 8);
600 right <<= (uncompressed_bytes * 8);
601
602 left |= uncompressed_bytes_buffer_a[i] & mask;
603 right |= uncompressed_bytes_buffer_b[i] & mask;
604 }
605
606 ((uint8_t *)buffer_out)[i * numchannels * 3] = (left)&0xFF;
607 ((uint8_t *)buffer_out)[i * numchannels * 3 + 1] = (left >> 8) & 0xFF;
608 ((uint8_t *)buffer_out)[i * numchannels * 3 + 2] = (left >> 16) & 0xFF;
609
610 ((uint8_t *)buffer_out)[i * numchannels * 3 + 3] = (right)&0xFF;
611 ((uint8_t *)buffer_out)[i * numchannels * 3 + 4] = (right >> 8) & 0xFF;
612 ((uint8_t *)buffer_out)[i * numchannels * 3 + 5] = (right >> 16) & 0xFF;
613 }
614
615 return;
616 }
617
618 /* otherwise basic interlacing took place */
619 for (i = 0; i < numsamples; i++) {
620 int32_t left, right;
621
622 left = buffer_a[i];
623 right = buffer_b[i];
624
625 if (uncompressed_bytes) {
626 uint32_t mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
627 left <<= (uncompressed_bytes * 8);
628 right <<= (uncompressed_bytes * 8);
629
630 left |= uncompressed_bytes_buffer_a[i] & mask;
631 right |= uncompressed_bytes_buffer_b[i] & mask;
632 }
633
634 ((uint8_t *)buffer_out)[i * numchannels * 3] = (left)&0xFF;
635 ((uint8_t *)buffer_out)[i * numchannels * 3 + 1] = (left >> 8) & 0xFF;
636 ((uint8_t *)buffer_out)[i * numchannels * 3 + 2] = (left >> 16) & 0xFF;
637
638 ((uint8_t *)buffer_out)[i * numchannels * 3 + 3] = (right)&0xFF;
639 ((uint8_t *)buffer_out)[i * numchannels * 3 + 4] = (right >> 8) & 0xFF;
640 ((uint8_t *)buffer_out)[i * numchannels * 3 + 5] = (right >> 16) & 0xFF;
641 }
642 }
643
644 void alac_decode_frame(alac_file *alac, unsigned char *inbuffer, void *outbuffer, int *outputsize) {
645 int outbuffer_allocation_size = *outputsize; // initial value
646 int channels;
647 int32_t outputsamples = alac->setinfo_max_samples_per_frame;
648
649 /* setup the stream */
650 alac->input_buffer = inbuffer;
651 alac->input_buffer_bitaccumulator = 0;
652
653 channels = readbits(alac, 3);
654
655 *outputsize = outputsamples * alac->bytespersample;
656 if (*outputsize > outbuffer_allocation_size) {
657 fprintf(stderr, "FIXME: Not enough space if the output buffer for audio frame - E1.\n");
658 *outputsize = 0;
659 return;
660 }
661
662 switch (channels) {
663 case 0: /* 1 channel */
664 {
665 int hassize;
666 int isnotcompressed;
667 int readsamplesize;
668
669 int uncompressed_bytes;
670 int ricemodifier;
671
672 /* 2^result = something to do with output waiting.
673 * perhaps matters if we read > 1 frame in a pass?
674 */
675 readbits(alac, 4);
676
677 readbits(alac, 12); /* unknown, skip 12 bits */
678
679 hassize = readbits(alac, 1); /* the output sample size is stored soon */
680
681 uncompressed_bytes =
682 readbits(alac, 2); /* number of bytes in the (compressed) stream that are not compressed */
683
684 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
685
686 if (hassize) {
687 /* now read the number of samples,
688 * as a 32bit integer */
689 outputsamples = readbits(alac, 32);
690 *outputsize = outputsamples * alac->bytespersample;
691 if (*outputsize > outbuffer_allocation_size) {
692 fprintf(stderr, "FIXME: Not enough space if the output buffer for audio frame - E2.\n");
693 *outputsize = 0;
694 return;
695 }
696 }
697
698 readsamplesize = alac->setinfo_sample_size - (uncompressed_bytes * 8);
699
700 if (!isnotcompressed) { /* so it is compressed */
701 int16_t predictor_coef_table[32];
702 int predictor_coef_num;
703 int prediction_type;
704 int prediction_quantitization;
705 int i;
706
707 /* skip 16 bits, not sure what they are. seem to be used in
708 * two channel case */
709 readbits(alac, 8);
710 readbits(alac, 8);
711
712 prediction_type = readbits(alac, 4);
713 prediction_quantitization = readbits(alac, 4);
714
715 ricemodifier = readbits(alac, 3);
716 predictor_coef_num = readbits(alac, 5);
717
718 /* read the predictor table */
719 for (i = 0; i < predictor_coef_num; i++) {
720 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
721 }
722
723 if (uncompressed_bytes) {
724 int i;
725 for (i = 0; i < outputsamples; i++) {
726 alac->uncompressed_bytes_buffer_a[i] = readbits(alac, uncompressed_bytes * 8);
727 }
728 }
729
730 entropy_rice_decode(alac, alac->predicterror_buffer_a, outputsamples, readsamplesize,
731 alac->setinfo_rice_initialhistory, alac->setinfo_rice_kmodifier,
732 ricemodifier * alac->setinfo_rice_historymult / 4,
733 (1 << alac->setinfo_rice_kmodifier) - 1);
734
735 if (prediction_type == 0) { /* adaptive fir */
736 predictor_decompress_fir_adapt(alac->predicterror_buffer_a, alac->outputsamples_buffer_a,
737 outputsamples, readsamplesize, predictor_coef_table,
738 predictor_coef_num, prediction_quantitization);
739 } else {
740 fprintf(stderr, "FIXME: unhandled prediction type for compressed case: %i\n",
741 prediction_type);
742 /* i think the only other prediction type (or perhaps this is just a
743 * boolean?) runs adaptive fir twice.. like:
744 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
745 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
746 * little strange..
747 */
748 }
749
750 } else { /* not compressed, easy case */
751 if (alac->setinfo_sample_size <= 16) {
752 int i;
753 for (i = 0; i < outputsamples; i++) {
754 int32_t audiobits = readbits(alac, alac->setinfo_sample_size);
755
756 audiobits = SIGN_EXTENDED32(audiobits, alac->setinfo_sample_size);
757
758 alac->outputsamples_buffer_a[i] = audiobits;
759 }
760 } else {
761 int i;
762 for (i = 0; i < outputsamples; i++) {
763 int32_t audiobits;
764
765 audiobits = readbits(alac, 16);
766 /* special case of sign extension..
767 * as we'll be ORing the low 16bits into this */
768 audiobits = audiobits << (alac->setinfo_sample_size - 16);
769 audiobits |= readbits(alac, alac->setinfo_sample_size - 16);
770 audiobits = SignExtend24(audiobits);
771
772 alac->outputsamples_buffer_a[i] = audiobits;
773 }
774 }
775 uncompressed_bytes = 0; // always 0 for uncompressed
776 }
777
778 switch (alac->setinfo_sample_size) {
779 case 16: {
780 int i;
781 for (i = 0; i < outputsamples; i++) {
782 int16_t sample = alac->outputsamples_buffer_a[i];
783 if (host_bigendian)
784 _Swap16(sample);
785 ((int16_t *)outbuffer)[i * alac->numchannels] = sample;
786 }
787 break;
788 }
789 case 24: {
790 int i;
791 for (i = 0; i < outputsamples; i++) {
792 int32_t sample = alac->outputsamples_buffer_a[i];
793
794 if (uncompressed_bytes) {
795 uint32_t mask;
796 sample = sample << (uncompressed_bytes * 8);
797 mask = ~(0xFFFFFFFF << (uncompressed_bytes * 8));
798 sample |= alac->uncompressed_bytes_buffer_a[i] & mask;
799 }
800
801 ((uint8_t *)outbuffer)[i * alac->numchannels * 3] = (sample)&0xFF;
802 ((uint8_t *)outbuffer)[i * alac->numchannels * 3 + 1] = (sample >> 8) & 0xFF;
803 ((uint8_t *)outbuffer)[i * alac->numchannels * 3 + 2] = (sample >> 16) & 0xFF;
804 }
805 break;
806 }
807 case 20:
808 case 32:
809 fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
810 break;
811 default:
812 break;
813 }
814 break;
815 }
816 case 1: /* 2 channels */
817 {
818 int hassize;
819 int isnotcompressed;
820 int readsamplesize;
821
822 int uncompressed_bytes;
823
824 uint8_t interlacing_shift;
825 uint8_t interlacing_leftweight;
826
827 /* 2^result = something to do with output waiting.
828 * perhaps matters if we read > 1 frame in a pass?
829 */
830 readbits(alac, 4);
831
832 readbits(alac, 12); /* unknown, skip 12 bits */
833
834 hassize = readbits(alac, 1); /* the output sample size is stored soon */
835
836 uncompressed_bytes = readbits(
837 alac, 2); /* the number of bytes in the (compressed) stream that are not compressed */
838
839 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
840
841 if (hassize) {
842 /* now read the number of samples,
843 * as a 32bit integer */
844 outputsamples = readbits(alac, 32);
845 *outputsize = outputsamples * alac->bytespersample;
846 if (*outputsize > outbuffer_allocation_size) {
847 fprintf(stderr, "FIXME: Not enough space if the output buffer for audio frame - E3.\n");
848 *outputsize = 0;
849 return;
850 }
851 }
852
853 readsamplesize = alac->setinfo_sample_size - (uncompressed_bytes * 8) + 1;
854
855 if (!isnotcompressed) { /* compressed */
856 int16_t predictor_coef_table_a[32];
857 int predictor_coef_num_a;
858 int prediction_type_a;
859 int prediction_quantitization_a;
860 int ricemodifier_a;
861
862 int16_t predictor_coef_table_b[32];
863 int predictor_coef_num_b;
864 int prediction_type_b;
865 int prediction_quantitization_b;
866 int ricemodifier_b;
867
868 int i;
869
870 interlacing_shift = readbits(alac, 8);
871 interlacing_leftweight = readbits(alac, 8);
872
873 /******** channel 1 ***********/
874 prediction_type_a = readbits(alac, 4);
875 prediction_quantitization_a = readbits(alac, 4);
876
877 ricemodifier_a = readbits(alac, 3);
878 predictor_coef_num_a = readbits(alac, 5);
879
880 /* read the predictor table */
881 for (i = 0; i < predictor_coef_num_a; i++) {
882 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
883 }
884
885 /******** channel 2 *********/
886 prediction_type_b = readbits(alac, 4);
887 prediction_quantitization_b = readbits(alac, 4);
888
889 ricemodifier_b = readbits(alac, 3);
890 predictor_coef_num_b = readbits(alac, 5);
891
892 /* read the predictor table */
893 for (i = 0; i < predictor_coef_num_b; i++) {
894 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
895 }
896
897 /*********************/
898 if (uncompressed_bytes) { /* see mono case */
899 int i;
900 for (i = 0; i < outputsamples; i++) {
901 alac->uncompressed_bytes_buffer_a[i] = readbits(alac, uncompressed_bytes * 8);
902 alac->uncompressed_bytes_buffer_b[i] = readbits(alac, uncompressed_bytes * 8);
903 }
904 }
905
906 /* channel 1 */
907 entropy_rice_decode(alac, alac->predicterror_buffer_a, outputsamples, readsamplesize,
908 alac->setinfo_rice_initialhistory, alac->setinfo_rice_kmodifier,
909 ricemodifier_a * alac->setinfo_rice_historymult / 4,
910 (1 << alac->setinfo_rice_kmodifier) - 1);
911
912 if (prediction_type_a == 0) { /* adaptive fir */
913 predictor_decompress_fir_adapt(alac->predicterror_buffer_a, alac->outputsamples_buffer_a,
914 outputsamples, readsamplesize, predictor_coef_table_a,
915 predictor_coef_num_a, prediction_quantitization_a);
916 } else { /* see mono case */
917 fprintf(stderr, "FIXME: unhandled prediction type on channel 1: %i\n", prediction_type_a);
918 }
919
920 /* channel 2 */
921 entropy_rice_decode(alac, alac->predicterror_buffer_b, outputsamples, readsamplesize,
922 alac->setinfo_rice_initialhistory, alac->setinfo_rice_kmodifier,
923 ricemodifier_b * alac->setinfo_rice_historymult / 4,
924 (1 << alac->setinfo_rice_kmodifier) - 1);
925
926 if (prediction_type_b == 0) { /* adaptive fir */
927 predictor_decompress_fir_adapt(alac->predicterror_buffer_b, alac->outputsamples_buffer_b,
928 outputsamples, readsamplesize, predictor_coef_table_b,
929 predictor_coef_num_b, prediction_quantitization_b);
930 } else {
931 fprintf(stderr, "FIXME: unhandled prediction type on channel 2: %i\n", prediction_type_b);
932 }
933 } else { /* not compressed, easy case */
934 if (alac->setinfo_sample_size <= 16) {
935 int i;
936 for (i = 0; i < outputsamples; i++) {
937 int32_t audiobits_a, audiobits_b;
938
939 audiobits_a = readbits(alac, alac->setinfo_sample_size);
940 audiobits_b = readbits(alac, alac->setinfo_sample_size);
941
942 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
943 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
944
945 alac->outputsamples_buffer_a[i] = audiobits_a;
946 alac->outputsamples_buffer_b[i] = audiobits_b;
947 }
948 } else {
949 int i;
950 for (i = 0; i < outputsamples; i++) {
951 int32_t audiobits_a, audiobits_b;
952
953 audiobits_a = readbits(alac, 16);
954 audiobits_a = audiobits_a << (alac->setinfo_sample_size - 16);
955 audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
956 audiobits_a = SignExtend24(audiobits_a);
957
958 audiobits_b = readbits(alac, 16);
959 audiobits_b = audiobits_b << (alac->setinfo_sample_size - 16);
960 audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
961 audiobits_b = SignExtend24(audiobits_b);
962
963 alac->outputsamples_buffer_a[i] = audiobits_a;
964 alac->outputsamples_buffer_b[i] = audiobits_b;
965 }
966 }
967 uncompressed_bytes = 0; // always 0 for uncompressed
968 interlacing_shift = 0;
969 interlacing_leftweight = 0;
970 }
971
972 switch (alac->setinfo_sample_size) {
973 case 16: {
974 deinterlace_16(alac->outputsamples_buffer_a, alac->outputsamples_buffer_b,
975 (int16_t *)outbuffer, alac->numchannels, outputsamples, interlacing_shift,
976 interlacing_leftweight);
977 break;
978 }
979 case 24: {
980 deinterlace_24(alac->outputsamples_buffer_a, alac->outputsamples_buffer_b, uncompressed_bytes,
981 alac->uncompressed_bytes_buffer_a, alac->uncompressed_bytes_buffer_b,
982 (int16_t *)outbuffer, alac->numchannels, outputsamples, interlacing_shift,
983 interlacing_leftweight);
984 break;
985 }
986 case 20:
987 case 32:
988 fprintf(stderr, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
989 break;
990 default:
991 break;
992 }
993
994 break;
995 }
996 }
997 }
998
999 alac_file *alac_create(int samplesize, int numchannels) {
1000 alac_file *newfile = malloc(sizeof(alac_file));
1001 if (newfile) {
1002 memset(newfile, 0, sizeof(alac_file));
1003 newfile->samplesize = samplesize;
1004 newfile->numchannels = numchannels;
1005 newfile->bytespersample = (samplesize / 8) * numchannels;
1006 } else {
1007 fprintf(stderr, "FIXME: can not allocate memory for a new file in alac_cxreate.");
1008 }
1009 return newfile;
1010 }