]> git.sesse.net Git - ffmpeg/blob - libavcodec/alac.c
decoder works fine now, when fed properly-sized chunks by the demuxer;
[ffmpeg] / libavcodec / alac.c
1 /*
2  * ALAC (Apple Lossless Audio Codec) decoder
3  * Copyright (c) 2005 David Hammerton
4  * All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 /**
22  * @file alac.c
23  * ALAC (Apple Lossless Audio Codec) decoder
24  * @author 2005 David Hammerton
25  *
26  * For more information on the ALAC format, visit:
27  *  http://crazney.net/programs/itunes/alac.html
28  *
29  * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
30  * passed through the extradata[_size] fields. This atom is tacked onto
31  * the end of an 'alac' stsd atom and has the following format:
32  *  bytes 0-3   atom size (0x24), big-endian
33  *  bytes 4-7   atom type ('alac', not the 'alac' tag from start of stsd)
34  *  bytes 8-35  data bytes needed by decoder
35  */
36
37
38 #include "avcodec.h"
39
40 #define ALAC_EXTRADATA_SIZE 36
41
42 struct alac_file {
43     unsigned char *input_buffer;
44     int input_buffer_index;
45     int input_buffer_size;
46     int input_buffer_bitaccumulator; /* used so we can do arbitary
47                                         bit reads */
48
49     int samplesize;
50     int numchannels;
51     int bytespersample;
52
53
54     /* buffers */
55     int32_t *predicterror_buffer_a;
56     int32_t *predicterror_buffer_b;
57
58     int32_t *outputsamples_buffer_a;
59     int32_t *outputsamples_buffer_b;
60
61
62     /* stuff from setinfo */
63     uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */    /* max samples per frame? */
64     uint8_t setinfo_7a; /* 0x00 */
65     uint8_t setinfo_sample_size; /* 0x10 */
66     uint8_t setinfo_rice_historymult; /* 0x28 */
67     uint8_t setinfo_rice_initialhistory; /* 0x0a */
68     uint8_t setinfo_rice_kmodifier; /* 0x0e */
69     uint8_t setinfo_7f; /* 0x02 */
70     uint16_t setinfo_80; /* 0x00ff */
71     uint32_t setinfo_82; /* 0x000020e7 */
72     uint32_t setinfo_86; /* 0x00069fe4 */
73     uint32_t setinfo_8a_rate; /* 0x0000ac44 */
74     /* end setinfo stuff */
75 };
76
77 typedef struct alac_file alac_file;
78
79 typedef struct {
80
81     AVCodecContext *avctx;
82     /* init to 0; first frame decode should initialize from extradata and
83      * set this to 1 */
84     int context_initialized;
85
86     alac_file *alac;
87 } ALACContext;
88
89 static void allocate_buffers(alac_file *alac)
90 {
91     alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
92     alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
93
94     alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
95     alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
96 }
97
98 void alac_set_info(alac_file *alac, char *inputbuffer)
99 {
100     unsigned char *ptr = inputbuffer;
101
102     ptr += 4; /* size */
103     ptr += 4; /* alac */
104     ptr += 4; /* 0 ? */
105
106     alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
107     ptr += 4;
108     alac->setinfo_7a = *ptr++;
109     alac->setinfo_sample_size = *ptr++;
110     alac->setinfo_rice_historymult = *ptr++;
111     alac->setinfo_rice_initialhistory = *ptr++;
112     alac->setinfo_rice_kmodifier = *ptr++;
113     alac->setinfo_7f = *ptr++;
114     alac->setinfo_80 = BE_16(ptr);
115     ptr += 2;
116     alac->setinfo_82 = BE_32(ptr);
117     ptr += 4;
118     alac->setinfo_86 = BE_32(ptr);
119     ptr += 4;
120     alac->setinfo_8a_rate = BE_32(ptr);
121     ptr += 4;
122
123     allocate_buffers(alac);
124 }
125
126 /* stream reading */
127
128 /* supports reading 1 to 16 bits, in big endian format */
129 static uint32_t readbits_16(alac_file *alac, int bits)
130 {
131     uint32_t result;
132     int new_accumulator;
133
134     if (alac->input_buffer_index + 2 >= alac->input_buffer_size) {
135         av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
136             alac->input_buffer_index + 2, alac->input_buffer_size);
137 //        exit (0);
138     }
139     result = (alac->input_buffer[alac->input_buffer_index + 0] << 16) |
140              (alac->input_buffer[alac->input_buffer_index + 1] << 8) |
141              (alac->input_buffer[alac->input_buffer_index + 2]);
142
143     /* shift left by the number of bits we've already read,
144      * so that the top 'n' bits of the 24 bits we read will
145      * be the return bits */
146     result = result << alac->input_buffer_bitaccumulator;
147
148     result = result & 0x00ffffff;
149
150     /* and then only want the top 'n' bits from that, where
151      * n is 'bits' */
152     result = result >> (24 - bits);
153
154     new_accumulator = (alac->input_buffer_bitaccumulator + bits);
155
156     /* increase the buffer pointer if we've read over n bytes. */
157     alac->input_buffer_index += (new_accumulator >> 3);
158
159     /* and the remainder goes back into the bit accumulator */
160     alac->input_buffer_bitaccumulator = (new_accumulator & 7);
161
162     return result;
163 }
164
165 /* supports reading 1 to 32 bits, in big endian format */
166 static uint32_t readbits(alac_file *alac, int bits)
167 {
168     int32_t result = 0;
169
170     if (bits > 16) {
171         bits -= 16;
172         result = readbits_16(alac, 16) << bits;
173     }
174
175     result |= readbits_16(alac, bits);
176
177     return result;
178 }
179
180 /* reads a single bit */
181 static int readbit(alac_file *alac)
182 {
183     int result;
184     int new_accumulator;
185
186     if (alac->input_buffer_index >= alac->input_buffer_size) {
187         av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
188             alac->input_buffer_index + 2, alac->input_buffer_size);
189         exit (0);
190     }
191
192     result = alac->input_buffer[alac->input_buffer_index];
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_index += (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_index += (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;
216 }
217
218 /* hideously inefficient. could use a bitmask search,
219  * alternatively bsr on x86,
220  */
221 static int count_leading_zeros(int32_t input)
222 {
223     int i = 0;
224     while (!(0x80000000 & input) && i < 32) {
225         i++;
226         input = input << 1;
227     }
228     return i;
229 }
230
231 void bastardized_rice_decompress(alac_file *alac,
232                                  int32_t *output_buffer,
233                                  int output_size,
234                                  int readsamplesize, /* arg_10 */
235                                  int rice_initialhistory, /* arg424->b */
236                                  int rice_kmodifier, /* arg424->d */
237                                  int rice_historymult, /* arg424->c */
238                                  int rice_kmodifier_mask /* arg424->e */
239         )
240 {
241     int output_count;
242     unsigned int history = rice_initialhistory;
243     int sign_modifier = 0;
244
245     for (output_count = 0; output_count < output_size; output_count++) {
246         int32_t x = 0;
247         int32_t x_modified;
248         int32_t final_val;
249
250         /* read x - number of 1s before 0 represent the rice */
251         while (x <= 8 && readbit(alac)) {
252             x++;
253         }
254
255
256         if (x > 8) { /* RICE THRESHOLD */
257           /* use alternative encoding */
258             int32_t value;
259
260             value = readbits(alac, readsamplesize);
261
262             /* mask value to readsamplesize size */
263             if (readsamplesize != 32)
264                 value &= (0xffffffff >> (32 - readsamplesize));
265
266             x = value;
267         } else {
268           /* standard rice encoding */
269             int extrabits;
270             int k; /* size of extra bits */
271
272             /* read k, that is bits as is */
273             k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
274
275             if (k < 0) 
276                 k += rice_kmodifier;
277             else 
278                 k = rice_kmodifier;
279
280             if (k != 1) {
281                 extrabits = readbits(alac, k);
282
283                 /* multiply x by 2^k - 1, as part of their strange algorithm */
284                 x = (x << k) - x;
285
286                 if (extrabits > 1) {
287                     x += extrabits - 1;
288                 } else 
289                     unreadbits(alac, 1);
290             }
291         }
292
293         x_modified = sign_modifier + x;
294         final_val = (x_modified + 1) / 2;
295         if (x_modified & 1) final_val *= -1;
296
297         output_buffer[output_count] = final_val;
298
299         sign_modifier = 0;
300
301         /* now update the history */
302         history += (x_modified * rice_historymult)
303                  - ((history * rice_historymult) >> 9);
304
305         if (x_modified > 0xffff)
306             history = 0xffff;
307
308         /* special case: there may be compressed blocks of 0 */
309         if ((history < 128) && (output_count+1 < output_size)) {
310             int block_size;
311
312             sign_modifier = 1;
313
314             x = 0;
315             while (x <= 8 && readbit(alac)) {
316                 x++;
317             }
318
319             if (x > 8) {
320                 block_size = readbits(alac, 16);
321                 block_size &= 0xffff;
322             } else {
323                 int k;
324                 int extrabits;
325
326                 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
327
328                 extrabits = readbits(alac, k);
329
330                 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
331                            + extrabits - 1;
332
333                 if (extrabits < 2) {
334                     x = 1 - extrabits;
335                     block_size += x;
336                     unreadbits(alac, 1);
337                 }
338             }
339
340             if (block_size > 0) {
341                 memset(&output_buffer[output_count+1], 0, block_size * 4);
342                 output_count += block_size;
343
344             }
345
346             if (block_size > 0xffff)
347                 sign_modifier = 0;
348
349             history = 0;
350         }
351     }
352 }
353
354 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
355
356 #define SIGN_ONLY(v) \
357                      ((v < 0) ? (-1) : \
358                                 ((v > 0) ? (1) : \
359                                            (0)))
360
361 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
362                                            int32_t *buffer_out,
363                                            int output_size,
364                                            int readsamplesize,
365                                            int16_t *predictor_coef_table,
366                                            int predictor_coef_num,
367                                            int predictor_quantitization)
368 {
369     int i;
370
371     /* first sample always copies */
372     *buffer_out = *error_buffer;
373
374     if (!predictor_coef_num) {
375         if (output_size <= 1) return;
376         memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
377         return;
378     }
379
380     if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
381       /* second-best case scenario for fir decompression,
382        * error describes a small difference from the previous sample only
383        */
384         if (output_size <= 1) return;
385         for (i = 0; i < output_size - 1; i++) {
386             int32_t prev_value;
387             int32_t error_value;
388
389             prev_value = buffer_out[i];
390             error_value = error_buffer[i+1];
391             buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
392         }
393         return;
394     }
395
396     /* read warm-up samples */
397     if (predictor_coef_num > 0) {
398         int i;
399         for (i = 0; i < predictor_coef_num; i++) {
400             int32_t val;
401
402             val = buffer_out[i] + error_buffer[i+1];
403
404             val = SIGN_EXTENDED32(val, readsamplesize);
405
406             buffer_out[i+1] = val;
407         }
408     }
409
410 #if 0
411     /* 4 and 8 are very common cases (the only ones i've seen). these
412      * should be unrolled and optimised
413      */
414     if (predictor_coef_num == 4) {
415         /* FIXME: optimised general case */
416         return;
417     }
418
419     if (predictor_coef_table == 8) {
420         /* FIXME: optimised general case */
421         return;
422     }
423 #endif
424
425
426     /* general case */
427     if (predictor_coef_num > 0) {
428         for (i = predictor_coef_num + 1;
429              i < output_size;
430              i++) {
431             int j;
432             int sum = 0;
433             int outval;
434             int error_val = error_buffer[i];
435
436             for (j = 0; j < predictor_coef_num; j++) {
437                 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
438                        predictor_coef_table[j];
439             }
440
441             outval = (1 << (predictor_quantitization-1)) + sum;
442             outval = outval >> predictor_quantitization;
443             outval = outval + buffer_out[0] + error_val;
444             outval = SIGN_EXTENDED32(outval, readsamplesize);
445
446             buffer_out[predictor_coef_num+1] = outval;
447
448             if (error_val > 0) {
449                 int predictor_num = predictor_coef_num - 1;
450
451                 while (predictor_num >= 0 && error_val > 0) {
452                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
453                     int sign = SIGN_ONLY(val);
454
455                     predictor_coef_table[predictor_num] -= sign;
456
457                     val *= sign; /* absolute value */
458
459                     error_val -= ((val >> predictor_quantitization) *
460                                   (predictor_coef_num - predictor_num));
461
462                     predictor_num--;
463                 }
464             } else if (error_val < 0) {
465                 int predictor_num = predictor_coef_num - 1;
466
467                 while (predictor_num >= 0 && error_val < 0) {
468                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
469                     int sign = - SIGN_ONLY(val);
470
471                     predictor_coef_table[predictor_num] -= sign;
472
473                     val *= sign; /* neg value */
474
475                     error_val -= ((val >> predictor_quantitization) *
476                                   (predictor_coef_num - predictor_num));
477
478                     predictor_num--;
479                 }
480             }
481
482             buffer_out++;
483         }
484     }
485 }
486
487 void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
488                     int16_t *buffer_out,
489                     int numchannels, int numsamples,
490                     uint8_t interlacing_shift,
491                     uint8_t interlacing_leftweight)
492 {
493     int i;
494     if (numsamples <= 0) return;
495
496     /* weighted interlacing */
497     if (interlacing_leftweight) {
498         for (i = 0; i < numsamples; i++) {
499             int32_t difference, midright;
500             int16_t left;
501             int16_t right;
502
503             midright = buffer_a[i];
504             difference = buffer_b[i];
505
506
507             right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
508             left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
509                  + difference;
510
511             buffer_out[i*numchannels] = left;
512             buffer_out[i*numchannels + 1] = right;
513         }
514
515         return;
516     }
517
518     /* otherwise basic interlacing took place */
519     for (i = 0; i < numsamples; i++) {
520         int16_t left, right;
521
522         left = buffer_a[i];
523         right = buffer_b[i];
524
525         buffer_out[i*numchannels] = left;
526         buffer_out[i*numchannels + 1] = right;
527     }
528 }
529
530 static int alac_decode_frame(AVCodecContext *avctx,
531                              void *outbuffer, int *outputsize,
532                              uint8_t *inbuffer, int input_buffer_size)
533 {
534     ALACContext *s = avctx->priv_data;
535     alac_file *alac = s->alac;
536
537     int channels;
538     int32_t outputsamples;
539
540     /* short-circuit null buffers */
541     if (!inbuffer || !input_buffer_size)
542         return input_buffer_size;
543
544     /* initialize from the extradata */
545     if (!s->context_initialized) {
546         if (s->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
547             av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n", 
548                 ALAC_EXTRADATA_SIZE);
549             return input_buffer_size;
550         }
551         alac_set_info(s->alac, s->avctx->extradata);
552         s->context_initialized = 1;
553     }
554
555     outputsamples = alac->setinfo_max_samples_per_frame;
556
557     /* setup the stream */
558     alac->input_buffer = inbuffer;
559     alac->input_buffer_index = 0;
560     alac->input_buffer_size = input_buffer_size;
561     alac->input_buffer_bitaccumulator = 0;
562
563     channels = readbits(alac, 3);
564
565     *outputsize = outputsamples * alac->bytespersample;
566
567     switch(channels) {
568     case 0: { /* 1 channel */
569         int hassize;
570         int isnotcompressed;
571         int readsamplesize;
572
573         int wasted_bytes;
574         int ricemodifier;
575
576
577         /* 2^result = something to do with output waiting.
578          * perhaps matters if we read > 1 frame in a pass?
579          */
580         readbits(alac, 4);
581
582         readbits(alac, 12); /* unknown, skip 12 bits */
583
584         hassize = readbits(alac, 1); /* the output sample size is stored soon */
585
586         wasted_bytes = readbits(alac, 2); /* unknown ? */
587
588         isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
589
590         if (hassize) {
591             /* now read the number of samples,
592              * as a 32bit integer */
593             outputsamples = readbits(alac, 32);
594             *outputsize = outputsamples * alac->bytespersample;
595         }
596
597         readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
598
599         if (!isnotcompressed) {
600          /* so it is compressed */
601             int16_t predictor_coef_table[32];
602             int predictor_coef_num;
603             int prediction_type;
604             int prediction_quantitization;
605             int i;
606
607             /* skip 16 bits, not sure what they are. seem to be used in
608              * two channel case */
609             readbits(alac, 8);
610             readbits(alac, 8);
611
612             prediction_type = readbits(alac, 4);
613             prediction_quantitization = readbits(alac, 4);
614
615             ricemodifier = readbits(alac, 3);
616             predictor_coef_num = readbits(alac, 5);
617
618             /* read the predictor table */
619             for (i = 0; i < predictor_coef_num; i++) {
620                 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
621             }
622
623             if (wasted_bytes) {
624                 /* these bytes seem to have something to do with
625                  * > 2 channel files.
626                  */
627                 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
628             }
629
630             bastardized_rice_decompress(alac,
631                                         alac->predicterror_buffer_a,
632                                         outputsamples,
633                                         readsamplesize,
634                                         alac->setinfo_rice_initialhistory,
635                                         alac->setinfo_rice_kmodifier,
636                                         ricemodifier * alac->setinfo_rice_historymult / 4,
637                                         (1 << alac->setinfo_rice_kmodifier) - 1);
638
639             if (prediction_type == 0) {
640               /* adaptive fir */
641                 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
642                                                alac->outputsamples_buffer_a,
643                                                outputsamples,
644                                                readsamplesize,
645                                                predictor_coef_table,
646                                                predictor_coef_num,
647                                                prediction_quantitization);
648             } else {
649                 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
650                 /* i think the only other prediction type (or perhaps this is just a
651                  * boolean?) runs adaptive fir twice.. like:
652                  * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
653                  * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
654                  * little strange..
655                  */
656             }
657
658         } else {
659           /* not compressed, easy case */
660             if (readsamplesize <= 16) {
661                 int i;
662                 for (i = 0; i < outputsamples; i++) {
663                     int32_t audiobits = readbits(alac, readsamplesize);
664
665                     audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
666
667                     alac->outputsamples_buffer_a[i] = audiobits;
668                 }
669             } else {
670                 int i;
671                 for (i = 0; i < outputsamples; i++) {
672                     int32_t audiobits;
673
674                     audiobits = readbits(alac, 16);
675                     /* special case of sign extension..
676                      * as we'll be ORing the low 16bits into this */
677                     audiobits = audiobits << 16;
678                     audiobits = audiobits >> (32 - readsamplesize);
679
680                     audiobits |= readbits(alac, readsamplesize - 16);
681
682                     alac->outputsamples_buffer_a[i] = audiobits;
683                 }
684             }
685             /* wasted_bytes = 0; // unused */
686         }
687
688         switch(alac->setinfo_sample_size) {
689         case 16: {
690             int i;
691             for (i = 0; i < outputsamples; i++) {
692                 int16_t sample = alac->outputsamples_buffer_a[i];
693                 be2me_16(sample);
694                 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
695             }
696             break;
697         }
698         case 20:
699         case 24:
700         case 32:
701             av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
702             break;
703         default:
704             break;
705         }
706         break;
707     }
708     case 1: { /* 2 channels */
709         int hassize;
710         int isnotcompressed;
711         int readsamplesize;
712
713         int wasted_bytes;
714
715         uint8_t interlacing_shift;
716         uint8_t interlacing_leftweight;
717
718         /* 2^result = something to do with output waiting.
719          * perhaps matters if we read > 1 frame in a pass?
720          */
721         readbits(alac, 4);
722
723         readbits(alac, 12); /* unknown, skip 12 bits */
724
725         hassize = readbits(alac, 1); /* the output sample size is stored soon */
726
727         wasted_bytes = readbits(alac, 2); /* unknown ? */
728
729         isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
730
731         if (hassize) {
732             /* now read the number of samples,
733              * as a 32bit integer */
734             outputsamples = readbits(alac, 32);
735             *outputsize = outputsamples * alac->bytespersample;
736         }
737
738         readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
739
740         if (!isnotcompressed) {
741          /* compressed */
742             int16_t predictor_coef_table_a[32];
743             int predictor_coef_num_a;
744             int prediction_type_a;
745             int prediction_quantitization_a;
746             int ricemodifier_a;
747
748             int16_t predictor_coef_table_b[32];
749             int predictor_coef_num_b;
750             int prediction_type_b;
751             int prediction_quantitization_b;
752             int ricemodifier_b;
753
754             int i;
755
756             interlacing_shift = readbits(alac, 8);
757             interlacing_leftweight = readbits(alac, 8);
758
759             /******** channel 1 ***********/
760             prediction_type_a = readbits(alac, 4);
761             prediction_quantitization_a = readbits(alac, 4);
762
763             ricemodifier_a = readbits(alac, 3);
764             predictor_coef_num_a = readbits(alac, 5);
765
766             /* read the predictor table */
767             for (i = 0; i < predictor_coef_num_a; i++) {
768                 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
769             }
770
771             /******** channel 2 *********/
772             prediction_type_b = readbits(alac, 4);
773             prediction_quantitization_b = readbits(alac, 4);
774
775             ricemodifier_b = readbits(alac, 3);
776             predictor_coef_num_b = readbits(alac, 5);
777
778             /* read the predictor table */
779             for (i = 0; i < predictor_coef_num_b; i++) {
780                 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
781             }
782
783             /*********************/
784             if (wasted_bytes) {
785               /* see mono case */
786                 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
787             }
788
789             /* channel 1 */
790             bastardized_rice_decompress(alac,
791                                         alac->predicterror_buffer_a,
792                                         outputsamples,
793                                         readsamplesize,
794                                         alac->setinfo_rice_initialhistory,
795                                         alac->setinfo_rice_kmodifier,
796                                         ricemodifier_a * alac->setinfo_rice_historymult / 4,
797                                         (1 << alac->setinfo_rice_kmodifier) - 1);
798
799             if (prediction_type_a == 0) {
800               /* adaptive fir */
801                 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
802                                                alac->outputsamples_buffer_a,
803                                                outputsamples,
804                                                readsamplesize,
805                                                predictor_coef_table_a,
806                                                predictor_coef_num_a,
807                                                prediction_quantitization_a);
808             } else {
809               /* see mono case */
810                 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
811             }
812
813             /* channel 2 */
814             bastardized_rice_decompress(alac,
815                                         alac->predicterror_buffer_b,
816                                         outputsamples,
817                                         readsamplesize,
818                                         alac->setinfo_rice_initialhistory,
819                                         alac->setinfo_rice_kmodifier,
820                                         ricemodifier_b * alac->setinfo_rice_historymult / 4,
821                                         (1 << alac->setinfo_rice_kmodifier) - 1);
822
823             if (prediction_type_b == 0) {
824               /* adaptive fir */
825                 predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
826                                                alac->outputsamples_buffer_b,
827                                                outputsamples,
828                                                readsamplesize,
829                                                predictor_coef_table_b,
830                                                predictor_coef_num_b,
831                                                prediction_quantitization_b);
832             } else {
833                 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
834             }
835         } else { 
836          /* not compressed, easy case */
837             if (alac->setinfo_sample_size <= 16) {
838                 int i;
839                 for (i = 0; i < outputsamples; i++) {
840                     int32_t audiobits_a, audiobits_b;
841
842                     audiobits_a = readbits(alac, alac->setinfo_sample_size);
843                     audiobits_b = readbits(alac, alac->setinfo_sample_size);
844
845                     audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
846                     audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
847
848                     alac->outputsamples_buffer_a[i] = audiobits_a;
849                     alac->outputsamples_buffer_b[i] = audiobits_b;
850                 }
851             } else {
852                 int i;
853                 for (i = 0; i < outputsamples; i++) {
854                     int32_t audiobits_a, audiobits_b;
855
856                     audiobits_a = readbits(alac, 16);
857                     audiobits_a = audiobits_a << 16;
858                     audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
859                     audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
860
861                     audiobits_b = readbits(alac, 16);
862                     audiobits_b = audiobits_b << 16;
863                     audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
864                     audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
865
866                     alac->outputsamples_buffer_a[i] = audiobits_a;
867                     alac->outputsamples_buffer_b[i] = audiobits_b;
868                 }
869             }
870             /* wasted_bytes = 0; */
871             interlacing_shift = 0;
872             interlacing_leftweight = 0;
873         }
874
875         switch(alac->setinfo_sample_size) {
876         case 16: {
877             deinterlace_16(alac->outputsamples_buffer_a,
878                            alac->outputsamples_buffer_b,
879                            (int16_t*)outbuffer,
880                            alac->numchannels,
881                            outputsamples,
882                            interlacing_shift,
883                            interlacing_leftweight);
884             break;
885         }
886         case 20:
887         case 24:
888         case 32:
889             av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
890             break;
891         default:
892             break;
893         }
894
895         break;
896     }
897     }
898
899     return input_buffer_size;
900 }
901
902 static int alac_decode_init(AVCodecContext * avctx)
903 {
904     ALACContext *s = avctx->priv_data;
905     s->avctx = avctx;
906     s->context_initialized = 0;
907
908     s->alac = av_malloc(sizeof(alac_file));
909
910     s->alac->samplesize = s->avctx->bits_per_sample;
911     s->alac->numchannels = s->avctx->channels;
912     s->alac->bytespersample = (s->alac->samplesize / 8) * s->alac->numchannels;
913
914     return 0;
915 }
916
917 static int alac_decode_close(AVCodecContext *avctx)
918 {
919     ALACContext *s = avctx->priv_data;
920
921     av_free(s->alac->predicterror_buffer_a);
922     av_free(s->alac->predicterror_buffer_b);
923
924     av_free(s->alac->outputsamples_buffer_a);
925     av_free(s->alac->outputsamples_buffer_b);
926
927     return 0;
928 }
929
930 AVCodec alac_decoder = {
931     "alac",
932     CODEC_TYPE_AUDIO,
933     CODEC_ID_ALAC,
934     sizeof(ALACContext),
935     alac_decode_init,
936     NULL,
937     alac_decode_close,
938     alac_decode_frame,
939 };