]> rtime.felk.cvut.cz Git - frescor/ffmpeg.git/blob - libavcodec/alac.c
Move var. declaration to allow further clean up
[frescor/ffmpeg.git] / libavcodec / alac.c
1 /*
2  * ALAC (Apple Lossless Audio Codec) decoder
3  * Copyright (c) 2005 David Hammerton
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file alac.c
24  * ALAC (Apple Lossless Audio Codec) decoder
25  * @author 2005 David Hammerton
26  *
27  * For more information on the ALAC format, visit:
28  *  http://crazney.net/programs/itunes/alac.html
29  *
30  * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
31  * passed through the extradata[_size] fields. This atom is tacked onto
32  * the end of an 'alac' stsd atom and has the following format:
33  *  bytes 0-3   atom size (0x24), big-endian
34  *  bytes 4-7   atom type ('alac', not the 'alac' tag from start of stsd)
35  *  bytes 8-35  data bytes needed by decoder
36  *
37  * Extradata:
38  * 32bit  size
39  * 32bit  tag (=alac)
40  * 32bit  zero?
41  * 32bit  max sample per frame
42  *  8bit  ?? (zero?)
43  *  8bit  sample size
44  *  8bit  history mult
45  *  8bit  initial history
46  *  8bit  kmodifier
47  *  8bit  channels?
48  * 16bit  ??
49  * 32bit  max coded frame size
50  * 32bit  bitrate?
51  * 32bit  samplerate
52  */
53
54
55 #include "avcodec.h"
56 #include "bitstream.h"
57 #include "bytestream.h"
58
59 #define ALAC_EXTRADATA_SIZE 36
60 #define MAX_CHANNELS 2
61
62 typedef struct {
63
64     AVCodecContext *avctx;
65     GetBitContext gb;
66     /* init to 0; first frame decode should initialize from extradata and
67      * set this to 1 */
68     int context_initialized;
69
70     int samplesize;
71     int numchannels;
72     int bytespersample;
73
74     /* buffers */
75     int32_t *predicterror_buffer[MAX_CHANNELS];
76
77     int32_t *outputsamples_buffer[MAX_CHANNELS];
78
79     /* stuff from setinfo */
80     uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */    /* max samples per frame? */
81     uint8_t setinfo_7a; /* 0x00 */
82     uint8_t setinfo_sample_size; /* 0x10 */
83     uint8_t setinfo_rice_historymult; /* 0x28 */
84     uint8_t setinfo_rice_initialhistory; /* 0x0a */
85     uint8_t setinfo_rice_kmodifier; /* 0x0e */
86     uint8_t setinfo_7f; /* 0x02 */
87     uint16_t setinfo_80; /* 0x00ff */
88     uint32_t setinfo_82; /* 0x000020e7 */ /* max sample size?? */
89     uint32_t setinfo_86; /* 0x00069fe4 */ /* bit rate (average)?? */
90     uint32_t setinfo_8a_rate; /* 0x0000ac44 */
91     /* end setinfo stuff */
92
93 } ALACContext;
94
95 static void allocate_buffers(ALACContext *alac)
96 {
97     int chan;
98     for (chan = 0; chan < MAX_CHANNELS; chan++) {
99         alac->predicterror_buffer[chan] =
100             av_malloc(alac->setinfo_max_samples_per_frame * 4);
101
102         alac->outputsamples_buffer[chan] =
103             av_malloc(alac->setinfo_max_samples_per_frame * 4);
104     }
105 }
106
107 static int alac_set_info(ALACContext *alac)
108 {
109     unsigned char *ptr = alac->avctx->extradata;
110
111     ptr += 4; /* size */
112     ptr += 4; /* alac */
113     ptr += 4; /* 0 ? */
114
115     if(AV_RB32(ptr) >= UINT_MAX/4){
116         av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
117         return -1;
118     }
119
120     /* buffer size / 2 ? */
121     alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
122     alac->setinfo_7a = *ptr++;
123     alac->setinfo_sample_size = *ptr++;
124     alac->setinfo_rice_historymult = *ptr++;
125     alac->setinfo_rice_initialhistory = *ptr++;
126     alac->setinfo_rice_kmodifier = *ptr++;
127     alac->setinfo_7f = *ptr++; // channels?
128     alac->setinfo_80                    = bytestream_get_be16(&ptr);
129     /* max coded frame size */
130     alac->setinfo_82                    = bytestream_get_be32(&ptr);
131     /* bitrate ? */
132     alac->setinfo_86                    = bytestream_get_be32(&ptr);
133     /* samplerate */
134     alac->setinfo_8a_rate               = bytestream_get_be32(&ptr);
135
136     allocate_buffers(alac);
137
138     return 0;
139 }
140
141 /* hideously inefficient. could use a bitmask search,
142  * alternatively bsr on x86,
143  */
144 static int count_leading_zeros(int32_t input)
145 {
146     int i = 0;
147     while (!(0x80000000 & input) && i < 32) {
148         i++;
149         input = input << 1;
150     }
151     return i;
152 }
153
154 static void bastardized_rice_decompress(ALACContext *alac,
155                                  int32_t *output_buffer,
156                                  int output_size,
157                                  int readsamplesize, /* arg_10 */
158                                  int rice_initialhistory, /* arg424->b */
159                                  int rice_kmodifier, /* arg424->d */
160                                  int rice_historymult, /* arg424->c */
161                                  int rice_kmodifier_mask /* arg424->e */
162         )
163 {
164     int output_count;
165     unsigned int history = rice_initialhistory;
166     int sign_modifier = 0;
167
168     for (output_count = 0; output_count < output_size; output_count++) {
169         int32_t x = 0;
170         int32_t x_modified;
171         int32_t final_val;
172
173         /* read x - number of 1s before 0 represent the rice */
174         while (x <= 8 && get_bits1(&alac->gb)) {
175             x++;
176         }
177
178
179         if (x > 8) { /* RICE THRESHOLD */
180           /* use alternative encoding */
181             int32_t value;
182
183             value = get_bits(&alac->gb, readsamplesize);
184
185             /* mask value to readsamplesize size */
186             if (readsamplesize != 32)
187                 value &= (0xffffffff >> (32 - readsamplesize));
188
189             x = value;
190         } else {
191           /* standard rice encoding */
192             int extrabits;
193             int k; /* size of extra bits */
194
195             /* read k, that is bits as is */
196             k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
197
198             if (k < 0)
199                 k += rice_kmodifier;
200             else
201                 k = rice_kmodifier;
202
203             if (k != 1) {
204                 extrabits = show_bits(&alac->gb, k);
205
206                 /* multiply x by 2^k - 1, as part of their strange algorithm */
207                 x = (x << k) - x;
208
209                 if (extrabits > 1) {
210                     x += extrabits - 1;
211                     get_bits(&alac->gb, k);
212                 } else {
213                     get_bits(&alac->gb, k - 1);
214                 }
215             }
216         }
217
218         x_modified = sign_modifier + x;
219         final_val = (x_modified + 1) / 2;
220         if (x_modified & 1) final_val *= -1;
221
222         output_buffer[output_count] = final_val;
223
224         sign_modifier = 0;
225
226         /* now update the history */
227         history += (x_modified * rice_historymult)
228                  - ((history * rice_historymult) >> 9);
229
230         if (x_modified > 0xffff)
231             history = 0xffff;
232
233         /* special case: there may be compressed blocks of 0 */
234         if ((history < 128) && (output_count+1 < output_size)) {
235             int block_size;
236
237             sign_modifier = 1;
238
239             x = 0;
240             while (x <= 8 && get_bits1(&alac->gb)) {
241                 x++;
242             }
243
244             if (x > 8) {
245                 block_size = get_bits(&alac->gb, 16);
246                 block_size &= 0xffff;
247             } else {
248                 int k;
249                 int extrabits;
250
251                 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
252
253                 extrabits = show_bits(&alac->gb, k);
254
255                 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
256                            + extrabits - 1;
257
258                 if (extrabits < 2) {
259                     x = 1 - extrabits;
260                     block_size += x;
261                     get_bits(&alac->gb, k - 1);
262                 } else {
263                     get_bits(&alac->gb, k);
264                 }
265             }
266
267             if (block_size > 0) {
268                 memset(&output_buffer[output_count+1], 0, block_size * 4);
269                 output_count += block_size;
270
271             }
272
273             if (block_size > 0xffff)
274                 sign_modifier = 0;
275
276             history = 0;
277         }
278     }
279 }
280
281 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
282
283 #define SIGN_ONLY(v) \
284                      ((v < 0) ? (-1) : \
285                                 ((v > 0) ? (1) : \
286                                            (0)))
287
288 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
289                                            int32_t *buffer_out,
290                                            int output_size,
291                                            int readsamplesize,
292                                            int16_t *predictor_coef_table,
293                                            int predictor_coef_num,
294                                            int predictor_quantitization)
295 {
296     int i;
297
298     /* first sample always copies */
299     *buffer_out = *error_buffer;
300
301     if (!predictor_coef_num) {
302         if (output_size <= 1) return;
303         memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
304         return;
305     }
306
307     if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
308       /* second-best case scenario for fir decompression,
309        * error describes a small difference from the previous sample only
310        */
311         if (output_size <= 1) return;
312         for (i = 0; i < output_size - 1; i++) {
313             int32_t prev_value;
314             int32_t error_value;
315
316             prev_value = buffer_out[i];
317             error_value = error_buffer[i+1];
318             buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
319         }
320         return;
321     }
322
323     /* read warm-up samples */
324     if (predictor_coef_num > 0) {
325         int i;
326         for (i = 0; i < predictor_coef_num; i++) {
327             int32_t val;
328
329             val = buffer_out[i] + error_buffer[i+1];
330
331             val = SIGN_EXTENDED32(val, readsamplesize);
332
333             buffer_out[i+1] = val;
334         }
335     }
336
337 #if 0
338     /* 4 and 8 are very common cases (the only ones i've seen). these
339      * should be unrolled and optimised
340      */
341     if (predictor_coef_num == 4) {
342         /* FIXME: optimised general case */
343         return;
344     }
345
346     if (predictor_coef_table == 8) {
347         /* FIXME: optimised general case */
348         return;
349     }
350 #endif
351
352
353     /* general case */
354     if (predictor_coef_num > 0) {
355         for (i = predictor_coef_num + 1;
356              i < output_size;
357              i++) {
358             int j;
359             int sum = 0;
360             int outval;
361             int error_val = error_buffer[i];
362
363             for (j = 0; j < predictor_coef_num; j++) {
364                 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
365                        predictor_coef_table[j];
366             }
367
368             outval = (1 << (predictor_quantitization-1)) + sum;
369             outval = outval >> predictor_quantitization;
370             outval = outval + buffer_out[0] + error_val;
371             outval = SIGN_EXTENDED32(outval, readsamplesize);
372
373             buffer_out[predictor_coef_num+1] = outval;
374
375             if (error_val > 0) {
376                 int predictor_num = predictor_coef_num - 1;
377
378                 while (predictor_num >= 0 && error_val > 0) {
379                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
380                     int sign = SIGN_ONLY(val);
381
382                     predictor_coef_table[predictor_num] -= sign;
383
384                     val *= sign; /* absolute value */
385
386                     error_val -= ((val >> predictor_quantitization) *
387                                   (predictor_coef_num - predictor_num));
388
389                     predictor_num--;
390                 }
391             } else if (error_val < 0) {
392                 int predictor_num = predictor_coef_num - 1;
393
394                 while (predictor_num >= 0 && error_val < 0) {
395                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
396                     int sign = - SIGN_ONLY(val);
397
398                     predictor_coef_table[predictor_num] -= sign;
399
400                     val *= sign; /* neg value */
401
402                     error_val -= ((val >> predictor_quantitization) *
403                                   (predictor_coef_num - predictor_num));
404
405                     predictor_num--;
406                 }
407             }
408
409             buffer_out++;
410         }
411     }
412 }
413
414 static void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
415                     int16_t *buffer_out,
416                     int numchannels, int numsamples,
417                     uint8_t interlacing_shift,
418                     uint8_t interlacing_leftweight)
419 {
420     int i;
421     if (numsamples <= 0) return;
422
423     /* weighted interlacing */
424     if (interlacing_leftweight) {
425         for (i = 0; i < numsamples; i++) {
426             int32_t difference, midright;
427             int16_t left;
428             int16_t right;
429
430             midright = buffer_a[i];
431             difference = buffer_b[i];
432
433
434             right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
435             left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
436                  + difference;
437
438             buffer_out[i*numchannels] = left;
439             buffer_out[i*numchannels + 1] = right;
440         }
441
442         return;
443     }
444
445     /* otherwise basic interlacing took place */
446     for (i = 0; i < numsamples; i++) {
447         int16_t left, right;
448
449         left = buffer_a[i];
450         right = buffer_b[i];
451
452         buffer_out[i*numchannels] = left;
453         buffer_out[i*numchannels + 1] = right;
454     }
455 }
456
457 static int alac_decode_frame(AVCodecContext *avctx,
458                              void *outbuffer, int *outputsize,
459                              uint8_t *inbuffer, int input_buffer_size)
460 {
461     ALACContext *alac = avctx->priv_data;
462
463     int channels;
464     int32_t outputsamples;
465     int hassize;
466     int readsamplesize;
467     int wasted_bytes;
468     int isnotcompressed;
469     uint8_t interlacing_shift;
470     uint8_t interlacing_leftweight;
471
472     /* short-circuit null buffers */
473     if (!inbuffer || !input_buffer_size)
474         return input_buffer_size;
475
476     /* initialize from the extradata */
477     if (!alac->context_initialized) {
478         if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
479             av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
480                 ALAC_EXTRADATA_SIZE);
481             return input_buffer_size;
482         }
483         if (alac_set_info(alac)) {
484             av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
485             return input_buffer_size;
486         }
487         alac->context_initialized = 1;
488     }
489
490     init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
491
492     channels = get_bits(&alac->gb, 3) + 1;
493
494         /* 2^result = something to do with output waiting.
495          * perhaps matters if we read > 1 frame in a pass?
496          */
497         get_bits(&alac->gb, 4);
498
499         get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
500
501         hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
502
503         wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
504
505         isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
506
507         if (hassize) {
508             /* now read the number of samples,
509              * as a 32bit integer */
510             outputsamples = get_bits(&alac->gb, 32);
511         } else
512             outputsamples = alac->setinfo_max_samples_per_frame;
513
514         *outputsize = outputsamples * alac->bytespersample;
515         readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + channels - 1;
516
517     switch(channels) {
518     case 1: { /* 1 channel */
519         int ricemodifier;
520
521         if (!isnotcompressed) {
522          /* so it is compressed */
523             int16_t predictor_coef_table[32];
524             int predictor_coef_num;
525             int prediction_type;
526             int prediction_quantitization;
527             int i;
528
529             /* FIXME: skip 16 bits, not sure what they are. seem to be used in
530              * two channel case */
531             get_bits(&alac->gb, 8);
532             get_bits(&alac->gb, 8);
533
534             prediction_type = get_bits(&alac->gb, 4);
535             prediction_quantitization = get_bits(&alac->gb, 4);
536
537             ricemodifier = get_bits(&alac->gb, 3);
538             predictor_coef_num = get_bits(&alac->gb, 5);
539
540             /* read the predictor table */
541             for (i = 0; i < predictor_coef_num; i++) {
542                 predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
543             }
544
545             if (wasted_bytes) {
546                 /* these bytes seem to have something to do with
547                  * > 2 channel files.
548                  */
549                 av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
550             }
551
552             bastardized_rice_decompress(alac,
553                                         alac->predicterror_buffer[0],
554                                         outputsamples,
555                                         readsamplesize,
556                                         alac->setinfo_rice_initialhistory,
557                                         alac->setinfo_rice_kmodifier,
558                                         ricemodifier * alac->setinfo_rice_historymult / 4,
559                                         (1 << alac->setinfo_rice_kmodifier) - 1);
560
561             if (prediction_type == 0) {
562               /* adaptive fir */
563                 predictor_decompress_fir_adapt(alac->predicterror_buffer[0],
564                                                alac->outputsamples_buffer[0],
565                                                outputsamples,
566                                                readsamplesize,
567                                                predictor_coef_table,
568                                                predictor_coef_num,
569                                                prediction_quantitization);
570             } else {
571                 av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
572                 /* i think the only other prediction type (or perhaps this is just a
573                  * boolean?) runs adaptive fir twice.. like:
574                  * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
575                  * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
576                  * little strange..
577                  */
578             }
579
580         } else {
581           /* not compressed, easy case */
582             if (readsamplesize <= 16) {
583                 int i;
584                 for (i = 0; i < outputsamples; i++) {
585                     int32_t audiobits = get_bits(&alac->gb, readsamplesize);
586
587                     audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
588
589                     alac->outputsamples_buffer[0][i] = audiobits;
590                 }
591             } else {
592                 int i;
593                 for (i = 0; i < outputsamples; i++) {
594                     int32_t audiobits;
595
596                     audiobits = get_bits(&alac->gb, 16);
597                     /* special case of sign extension..
598                      * as we'll be ORing the low 16bits into this */
599                     audiobits = audiobits << 16;
600                     audiobits = audiobits >> (32 - readsamplesize);
601
602                     audiobits |= get_bits(&alac->gb, readsamplesize - 16);
603
604                     alac->outputsamples_buffer[0][i] = audiobits;
605                 }
606             }
607             /* wasted_bytes = 0; // unused */
608         }
609
610         switch(alac->setinfo_sample_size) {
611         case 16: {
612             int i;
613             for (i = 0; i < outputsamples; i++) {
614                 int16_t sample = alac->outputsamples_buffer[0][i];
615                 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
616             }
617             break;
618         }
619         case 20:
620         case 24:
621         case 32:
622             av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
623             break;
624         default:
625             break;
626         }
627         break;
628     }
629     case 2: { /* 2 channels */
630
631         if (!isnotcompressed) {
632          /* compressed */
633             int16_t predictor_coef_table[channels][32];
634             int predictor_coef_num[channels];
635             int prediction_type[channels];
636             int prediction_quantitization[channels];
637             int ricemodifier[channels];
638
639             int i, chan;
640
641             interlacing_shift = get_bits(&alac->gb, 8);
642             interlacing_leftweight = get_bits(&alac->gb, 8);
643
644           for (chan = 0; chan < channels; chan++) {
645             prediction_type[chan] = get_bits(&alac->gb, 4);
646             prediction_quantitization[chan] = get_bits(&alac->gb, 4);
647
648             ricemodifier[chan] = get_bits(&alac->gb, 3);
649             predictor_coef_num[chan] = get_bits(&alac->gb, 5);
650
651             /* read the predictor table */
652             for (i = 0; i < predictor_coef_num[chan]; i++) {
653                 predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
654             }
655           }
656
657             if (wasted_bytes) {
658               /* see mono case */
659                 av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
660             }
661
662           for (chan = 0; chan < channels; chan++) {
663             bastardized_rice_decompress(alac,
664                                         alac->predicterror_buffer[chan],
665                                         outputsamples,
666                                         readsamplesize,
667                                         alac->setinfo_rice_initialhistory,
668                                         alac->setinfo_rice_kmodifier,
669                                         ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
670                                         (1 << alac->setinfo_rice_kmodifier) - 1);
671
672             if (prediction_type[chan] == 0) {
673               /* adaptive fir */
674                 predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
675                                                alac->outputsamples_buffer[chan],
676                                                outputsamples,
677                                                readsamplesize,
678                                                predictor_coef_table[chan],
679                                                predictor_coef_num[chan],
680                                                prediction_quantitization[chan]);
681             } else {
682               /* see mono case */
683                 av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
684             }
685           }
686         } else {
687          /* not compressed, easy case */
688             if (alac->setinfo_sample_size <= 16) {
689                 int i, chan;
690               for (chan = 0; chan < channels; chan++) {
691                 for (i = 0; i < outputsamples; i++) {
692                     int32_t audiobits;
693
694                     audiobits = get_bits(&alac->gb, alac->setinfo_sample_size);
695                     audiobits = SIGN_EXTENDED32(audiobits, alac->setinfo_sample_size);
696                     alac->outputsamples_buffer[chan][i] = audiobits;
697                 }
698               }
699             } else {
700                 int i, chan;
701               for (chan = 0; chan < channels; chan++) {
702                 for (i = 0; i < outputsamples; i++) {
703                     int32_t audiobits;
704
705                     audiobits = get_bits(&alac->gb, 16);
706                     audiobits = audiobits << 16;
707                     audiobits = audiobits >> (32 - alac->setinfo_sample_size);
708                     audiobits |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
709
710                     alac->outputsamples_buffer[chan][i] = audiobits;
711                 }
712               }
713             }
714             /* wasted_bytes = 0; */
715             interlacing_shift = 0;
716             interlacing_leftweight = 0;
717         }
718
719         switch(alac->setinfo_sample_size) {
720         case 16: {
721             deinterlace_16(alac->outputsamples_buffer[0],
722                            alac->outputsamples_buffer[1],
723                            (int16_t*)outbuffer,
724                            alac->numchannels,
725                            outputsamples,
726                            interlacing_shift,
727                            interlacing_leftweight);
728             break;
729         }
730         case 20:
731         case 24:
732         case 32:
733             av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
734             break;
735         default:
736             break;
737         }
738
739         break;
740     }
741     }
742
743     return input_buffer_size;
744 }
745
746 static int alac_decode_init(AVCodecContext * avctx)
747 {
748     ALACContext *alac = avctx->priv_data;
749     alac->avctx = avctx;
750     alac->context_initialized = 0;
751
752     alac->samplesize = alac->avctx->bits_per_sample;
753     alac->numchannels = alac->avctx->channels;
754     alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
755
756     return 0;
757 }
758
759 static int alac_decode_close(AVCodecContext *avctx)
760 {
761     ALACContext *alac = avctx->priv_data;
762
763     int chan;
764     for (chan = 0; chan < MAX_CHANNELS; chan++) {
765         av_free(alac->predicterror_buffer[chan]);
766         av_free(alac->outputsamples_buffer[chan]);
767     }
768
769     return 0;
770 }
771
772 AVCodec alac_decoder = {
773     "alac",
774     CODEC_TYPE_AUDIO,
775     CODEC_ID_ALAC,
776     sizeof(ALACContext),
777     alac_decode_init,
778     NULL,
779     alac_decode_close,
780     alac_decode_frame,
781 };