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