r654: Initial revision
[cinelerra_cv.git] / quicktime / ffmpeg / libavcodec / alac.c
blob2943b4dc51512d95f586f9c59c068acc07c08044
1 /*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
4 * All rights reserved.
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.
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.
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
21 /**
22 * @file alac.c
23 * ALAC (Apple Lossless Audio Codec) decoder
24 * @author 2005 David Hammerton
26 * For more information on the ALAC format, visit:
27 * http://crazney.net/programs/itunes/alac.html
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
38 #include "avcodec.h"
39 #include "bitstream.h"
41 #define ALAC_EXTRADATA_SIZE 36
43 typedef struct {
45 AVCodecContext *avctx;
46 GetBitContext gb;
47 /* init to 0; first frame decode should initialize from extradata and
48 * set this to 1 */
49 int context_initialized;
51 int samplesize;
52 int numchannels;
53 int bytespersample;
55 /* buffers */
56 int32_t *predicterror_buffer_a;
57 int32_t *predicterror_buffer_b;
59 int32_t *outputsamples_buffer_a;
60 int32_t *outputsamples_buffer_b;
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 */
76 } ALACContext;
78 static void allocate_buffers(ALACContext *alac)
80 alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
81 alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
83 alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
84 alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
87 void alac_set_info(ALACContext *alac)
89 unsigned char *ptr = alac->avctx->extradata;
91 ptr += 4; /* size */
92 ptr += 4; /* alac */
93 ptr += 4; /* 0 ? */
95 alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
96 ptr += 4;
97 alac->setinfo_7a = *ptr++;
98 alac->setinfo_sample_size = *ptr++;
99 alac->setinfo_rice_historymult = *ptr++;
100 alac->setinfo_rice_initialhistory = *ptr++;
101 alac->setinfo_rice_kmodifier = *ptr++;
102 alac->setinfo_7f = *ptr++;
103 alac->setinfo_80 = BE_16(ptr);
104 ptr += 2;
105 alac->setinfo_82 = BE_32(ptr);
106 ptr += 4;
107 alac->setinfo_86 = BE_32(ptr);
108 ptr += 4;
109 alac->setinfo_8a_rate = BE_32(ptr);
110 ptr += 4;
112 allocate_buffers(alac);
115 /* hideously inefficient. could use a bitmask search,
116 * alternatively bsr on x86,
118 static int count_leading_zeros(int32_t input)
120 int i = 0;
121 while (!(0x80000000 & input) && i < 32) {
122 i++;
123 input = input << 1;
125 return i;
128 void bastardized_rice_decompress(ALACContext *alac,
129 int32_t *output_buffer,
130 int output_size,
131 int readsamplesize, /* arg_10 */
132 int rice_initialhistory, /* arg424->b */
133 int rice_kmodifier, /* arg424->d */
134 int rice_historymult, /* arg424->c */
135 int rice_kmodifier_mask /* arg424->e */
138 int output_count;
139 unsigned int history = rice_initialhistory;
140 int sign_modifier = 0;
142 for (output_count = 0; output_count < output_size; output_count++) {
143 int32_t x = 0;
144 int32_t x_modified;
145 int32_t final_val;
147 /* read x - number of 1s before 0 represent the rice */
148 while (x <= 8 && get_bits1(&alac->gb)) {
149 x++;
153 if (x > 8) { /* RICE THRESHOLD */
154 /* use alternative encoding */
155 int32_t value;
157 value = get_bits(&alac->gb, readsamplesize);
159 /* mask value to readsamplesize size */
160 if (readsamplesize != 32)
161 value &= (0xffffffff >> (32 - readsamplesize));
163 x = value;
164 } else {
165 /* standard rice encoding */
166 int extrabits;
167 int k; /* size of extra bits */
169 /* read k, that is bits as is */
170 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
172 if (k < 0)
173 k += rice_kmodifier;
174 else
175 k = rice_kmodifier;
177 if (k != 1) {
178 extrabits = show_bits(&alac->gb, k);
180 /* multiply x by 2^k - 1, as part of their strange algorithm */
181 x = (x << k) - x;
183 if (extrabits > 1) {
184 x += extrabits - 1;
185 get_bits(&alac->gb, k);
186 } else {
187 get_bits(&alac->gb, k - 1);
192 x_modified = sign_modifier + x;
193 final_val = (x_modified + 1) / 2;
194 if (x_modified & 1) final_val *= -1;
196 output_buffer[output_count] = final_val;
198 sign_modifier = 0;
200 /* now update the history */
201 history += (x_modified * rice_historymult)
202 - ((history * rice_historymult) >> 9);
204 if (x_modified > 0xffff)
205 history = 0xffff;
207 /* special case: there may be compressed blocks of 0 */
208 if ((history < 128) && (output_count+1 < output_size)) {
209 int block_size;
211 sign_modifier = 1;
213 x = 0;
214 while (x <= 8 && get_bits1(&alac->gb)) {
215 x++;
218 if (x > 8) {
219 block_size = get_bits(&alac->gb, 16);
220 block_size &= 0xffff;
221 } else {
222 int k;
223 int extrabits;
225 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
227 extrabits = show_bits(&alac->gb, k);
229 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
230 + extrabits - 1;
232 if (extrabits < 2) {
233 x = 1 - extrabits;
234 block_size += x;
235 get_bits(&alac->gb, k - 1);
236 } else {
237 get_bits(&alac->gb, k);
241 if (block_size > 0) {
242 memset(&output_buffer[output_count+1], 0, block_size * 4);
243 output_count += block_size;
247 if (block_size > 0xffff)
248 sign_modifier = 0;
250 history = 0;
255 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
257 #define SIGN_ONLY(v) \
258 ((v < 0) ? (-1) : \
259 ((v > 0) ? (1) : \
260 (0)))
262 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
263 int32_t *buffer_out,
264 int output_size,
265 int readsamplesize,
266 int16_t *predictor_coef_table,
267 int predictor_coef_num,
268 int predictor_quantitization)
270 int i;
272 /* first sample always copies */
273 *buffer_out = *error_buffer;
275 if (!predictor_coef_num) {
276 if (output_size <= 1) return;
277 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
278 return;
281 if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
282 /* second-best case scenario for fir decompression,
283 * error describes a small difference from the previous sample only
285 if (output_size <= 1) return;
286 for (i = 0; i < output_size - 1; i++) {
287 int32_t prev_value;
288 int32_t error_value;
290 prev_value = buffer_out[i];
291 error_value = error_buffer[i+1];
292 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
294 return;
297 /* read warm-up samples */
298 if (predictor_coef_num > 0) {
299 int i;
300 for (i = 0; i < predictor_coef_num; i++) {
301 int32_t val;
303 val = buffer_out[i] + error_buffer[i+1];
305 val = SIGN_EXTENDED32(val, readsamplesize);
307 buffer_out[i+1] = val;
311 #if 0
312 /* 4 and 8 are very common cases (the only ones i've seen). these
313 * should be unrolled and optimised
315 if (predictor_coef_num == 4) {
316 /* FIXME: optimised general case */
317 return;
320 if (predictor_coef_table == 8) {
321 /* FIXME: optimised general case */
322 return;
324 #endif
327 /* general case */
328 if (predictor_coef_num > 0) {
329 for (i = predictor_coef_num + 1;
330 i < output_size;
331 i++) {
332 int j;
333 int sum = 0;
334 int outval;
335 int error_val = error_buffer[i];
337 for (j = 0; j < predictor_coef_num; j++) {
338 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
339 predictor_coef_table[j];
342 outval = (1 << (predictor_quantitization-1)) + sum;
343 outval = outval >> predictor_quantitization;
344 outval = outval + buffer_out[0] + error_val;
345 outval = SIGN_EXTENDED32(outval, readsamplesize);
347 buffer_out[predictor_coef_num+1] = outval;
349 if (error_val > 0) {
350 int predictor_num = predictor_coef_num - 1;
352 while (predictor_num >= 0 && error_val > 0) {
353 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
354 int sign = SIGN_ONLY(val);
356 predictor_coef_table[predictor_num] -= sign;
358 val *= sign; /* absolute value */
360 error_val -= ((val >> predictor_quantitization) *
361 (predictor_coef_num - predictor_num));
363 predictor_num--;
365 } else if (error_val < 0) {
366 int predictor_num = predictor_coef_num - 1;
368 while (predictor_num >= 0 && error_val < 0) {
369 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
370 int sign = - SIGN_ONLY(val);
372 predictor_coef_table[predictor_num] -= sign;
374 val *= sign; /* neg value */
376 error_val -= ((val >> predictor_quantitization) *
377 (predictor_coef_num - predictor_num));
379 predictor_num--;
383 buffer_out++;
388 void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
389 int16_t *buffer_out,
390 int numchannels, int numsamples,
391 uint8_t interlacing_shift,
392 uint8_t interlacing_leftweight)
394 int i;
395 if (numsamples <= 0) return;
397 /* weighted interlacing */
398 if (interlacing_leftweight) {
399 for (i = 0; i < numsamples; i++) {
400 int32_t difference, midright;
401 int16_t left;
402 int16_t right;
404 midright = buffer_a[i];
405 difference = buffer_b[i];
408 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
409 left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
410 + difference;
412 buffer_out[i*numchannels] = left;
413 buffer_out[i*numchannels + 1] = right;
416 return;
419 /* otherwise basic interlacing took place */
420 for (i = 0; i < numsamples; i++) {
421 int16_t left, right;
423 left = buffer_a[i];
424 right = buffer_b[i];
426 buffer_out[i*numchannels] = left;
427 buffer_out[i*numchannels + 1] = right;
431 static int alac_decode_frame(AVCodecContext *avctx,
432 void *outbuffer, int *outputsize,
433 uint8_t *inbuffer, int input_buffer_size)
435 ALACContext *alac = avctx->priv_data;
437 int channels;
438 int32_t outputsamples;
440 /* short-circuit null buffers */
441 if (!inbuffer || !input_buffer_size)
442 return input_buffer_size;
444 /* initialize from the extradata */
445 if (!alac->context_initialized) {
446 if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
447 av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
448 ALAC_EXTRADATA_SIZE);
449 return input_buffer_size;
451 alac_set_info(alac);
452 alac->context_initialized = 1;
455 outputsamples = alac->setinfo_max_samples_per_frame;
457 init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
459 channels = get_bits(&alac->gb, 3);
461 *outputsize = outputsamples * alac->bytespersample;
463 switch(channels) {
464 case 0: { /* 1 channel */
465 int hassize;
466 int isnotcompressed;
467 int readsamplesize;
469 int wasted_bytes;
470 int ricemodifier;
473 /* 2^result = something to do with output waiting.
474 * perhaps matters if we read > 1 frame in a pass?
476 get_bits(&alac->gb, 4);
478 get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
480 hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
482 wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
484 isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
486 if (hassize) {
487 /* now read the number of samples,
488 * as a 32bit integer */
489 outputsamples = get_bits(&alac->gb, 32);
490 *outputsize = outputsamples * alac->bytespersample;
493 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
495 if (!isnotcompressed) {
496 /* so it is compressed */
497 int16_t predictor_coef_table[32];
498 int predictor_coef_num;
499 int prediction_type;
500 int prediction_quantitization;
501 int i;
503 /* skip 16 bits, not sure what they are. seem to be used in
504 * two channel case */
505 get_bits(&alac->gb, 8);
506 get_bits(&alac->gb, 8);
508 prediction_type = get_bits(&alac->gb, 4);
509 prediction_quantitization = get_bits(&alac->gb, 4);
511 ricemodifier = get_bits(&alac->gb, 3);
512 predictor_coef_num = get_bits(&alac->gb, 5);
514 /* read the predictor table */
515 for (i = 0; i < predictor_coef_num; i++) {
516 predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
519 if (wasted_bytes) {
520 /* these bytes seem to have something to do with
521 * > 2 channel files.
523 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
526 bastardized_rice_decompress(alac,
527 alac->predicterror_buffer_a,
528 outputsamples,
529 readsamplesize,
530 alac->setinfo_rice_initialhistory,
531 alac->setinfo_rice_kmodifier,
532 ricemodifier * alac->setinfo_rice_historymult / 4,
533 (1 << alac->setinfo_rice_kmodifier) - 1);
535 if (prediction_type == 0) {
536 /* adaptive fir */
537 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
538 alac->outputsamples_buffer_a,
539 outputsamples,
540 readsamplesize,
541 predictor_coef_table,
542 predictor_coef_num,
543 prediction_quantitization);
544 } else {
545 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
546 /* i think the only other prediction type (or perhaps this is just a
547 * boolean?) runs adaptive fir twice.. like:
548 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
549 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
550 * little strange..
554 } else {
555 /* not compressed, easy case */
556 if (readsamplesize <= 16) {
557 int i;
558 for (i = 0; i < outputsamples; i++) {
559 int32_t audiobits = get_bits(&alac->gb, readsamplesize);
561 audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
563 alac->outputsamples_buffer_a[i] = audiobits;
565 } else {
566 int i;
567 for (i = 0; i < outputsamples; i++) {
568 int32_t audiobits;
570 audiobits = get_bits(&alac->gb, 16);
571 /* special case of sign extension..
572 * as we'll be ORing the low 16bits into this */
573 audiobits = audiobits << 16;
574 audiobits = audiobits >> (32 - readsamplesize);
576 audiobits |= get_bits(&alac->gb, readsamplesize - 16);
578 alac->outputsamples_buffer_a[i] = audiobits;
581 /* wasted_bytes = 0; // unused */
584 switch(alac->setinfo_sample_size) {
585 case 16: {
586 int i;
587 for (i = 0; i < outputsamples; i++) {
588 int16_t sample = alac->outputsamples_buffer_a[i];
589 sample = be2me_16(sample);
590 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
592 break;
594 case 20:
595 case 24:
596 case 32:
597 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
598 break;
599 default:
600 break;
602 break;
604 case 1: { /* 2 channels */
605 int hassize;
606 int isnotcompressed;
607 int readsamplesize;
609 int wasted_bytes;
611 uint8_t interlacing_shift;
612 uint8_t interlacing_leftweight;
614 /* 2^result = something to do with output waiting.
615 * perhaps matters if we read > 1 frame in a pass?
617 get_bits(&alac->gb, 4);
619 get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
621 hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
623 wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
625 isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
627 if (hassize) {
628 /* now read the number of samples,
629 * as a 32bit integer */
630 outputsamples = get_bits(&alac->gb, 32);
631 *outputsize = outputsamples * alac->bytespersample;
634 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
636 if (!isnotcompressed) {
637 /* compressed */
638 int16_t predictor_coef_table_a[32];
639 int predictor_coef_num_a;
640 int prediction_type_a;
641 int prediction_quantitization_a;
642 int ricemodifier_a;
644 int16_t predictor_coef_table_b[32];
645 int predictor_coef_num_b;
646 int prediction_type_b;
647 int prediction_quantitization_b;
648 int ricemodifier_b;
650 int i;
652 interlacing_shift = get_bits(&alac->gb, 8);
653 interlacing_leftweight = get_bits(&alac->gb, 8);
655 /******** channel 1 ***********/
656 prediction_type_a = get_bits(&alac->gb, 4);
657 prediction_quantitization_a = get_bits(&alac->gb, 4);
659 ricemodifier_a = get_bits(&alac->gb, 3);
660 predictor_coef_num_a = get_bits(&alac->gb, 5);
662 /* read the predictor table */
663 for (i = 0; i < predictor_coef_num_a; i++) {
664 predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16);
667 /******** channel 2 *********/
668 prediction_type_b = get_bits(&alac->gb, 4);
669 prediction_quantitization_b = get_bits(&alac->gb, 4);
671 ricemodifier_b = get_bits(&alac->gb, 3);
672 predictor_coef_num_b = get_bits(&alac->gb, 5);
674 /* read the predictor table */
675 for (i = 0; i < predictor_coef_num_b; i++) {
676 predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16);
679 /*********************/
680 if (wasted_bytes) {
681 /* see mono case */
682 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
685 /* channel 1 */
686 bastardized_rice_decompress(alac,
687 alac->predicterror_buffer_a,
688 outputsamples,
689 readsamplesize,
690 alac->setinfo_rice_initialhistory,
691 alac->setinfo_rice_kmodifier,
692 ricemodifier_a * alac->setinfo_rice_historymult / 4,
693 (1 << alac->setinfo_rice_kmodifier) - 1);
695 if (prediction_type_a == 0) {
696 /* adaptive fir */
697 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
698 alac->outputsamples_buffer_a,
699 outputsamples,
700 readsamplesize,
701 predictor_coef_table_a,
702 predictor_coef_num_a,
703 prediction_quantitization_a);
704 } else {
705 /* see mono case */
706 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
709 /* channel 2 */
710 bastardized_rice_decompress(alac,
711 alac->predicterror_buffer_b,
712 outputsamples,
713 readsamplesize,
714 alac->setinfo_rice_initialhistory,
715 alac->setinfo_rice_kmodifier,
716 ricemodifier_b * alac->setinfo_rice_historymult / 4,
717 (1 << alac->setinfo_rice_kmodifier) - 1);
719 if (prediction_type_b == 0) {
720 /* adaptive fir */
721 predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
722 alac->outputsamples_buffer_b,
723 outputsamples,
724 readsamplesize,
725 predictor_coef_table_b,
726 predictor_coef_num_b,
727 prediction_quantitization_b);
728 } else {
729 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
731 } else {
732 /* not compressed, easy case */
733 if (alac->setinfo_sample_size <= 16) {
734 int i;
735 for (i = 0; i < outputsamples; i++) {
736 int32_t audiobits_a, audiobits_b;
738 audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size);
739 audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size);
741 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
742 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
744 alac->outputsamples_buffer_a[i] = audiobits_a;
745 alac->outputsamples_buffer_b[i] = audiobits_b;
747 } else {
748 int i;
749 for (i = 0; i < outputsamples; i++) {
750 int32_t audiobits_a, audiobits_b;
752 audiobits_a = get_bits(&alac->gb, 16);
753 audiobits_a = audiobits_a << 16;
754 audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
755 audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
757 audiobits_b = get_bits(&alac->gb, 16);
758 audiobits_b = audiobits_b << 16;
759 audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
760 audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
762 alac->outputsamples_buffer_a[i] = audiobits_a;
763 alac->outputsamples_buffer_b[i] = audiobits_b;
766 /* wasted_bytes = 0; */
767 interlacing_shift = 0;
768 interlacing_leftweight = 0;
771 switch(alac->setinfo_sample_size) {
772 case 16: {
773 deinterlace_16(alac->outputsamples_buffer_a,
774 alac->outputsamples_buffer_b,
775 (int16_t*)outbuffer,
776 alac->numchannels,
777 outputsamples,
778 interlacing_shift,
779 interlacing_leftweight);
780 break;
782 case 20:
783 case 24:
784 case 32:
785 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
786 break;
787 default:
788 break;
791 break;
795 return input_buffer_size;
798 static int alac_decode_init(AVCodecContext * avctx)
800 ALACContext *alac = avctx->priv_data;
801 alac->avctx = avctx;
802 alac->context_initialized = 0;
804 alac->samplesize = alac->avctx->bits_per_sample;
805 alac->numchannels = alac->avctx->channels;
806 alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
808 return 0;
811 static int alac_decode_close(AVCodecContext *avctx)
813 ALACContext *alac = avctx->priv_data;
815 av_free(alac->predicterror_buffer_a);
816 av_free(alac->predicterror_buffer_b);
818 av_free(alac->outputsamples_buffer_a);
819 av_free(alac->outputsamples_buffer_b);
821 return 0;
824 AVCodec alac_decoder = {
825 "alac",
826 CODEC_TYPE_AUDIO,
827 CODEC_ID_ALAC,
828 sizeof(ALACContext),
829 alac_decode_init,
830 NULL,
831 alac_decode_close,
832 alac_decode_frame,