do not use a variable before proper initialization
[libav.git] / libavcodec / alac.c
CommitLineData
6d6d7970
MM
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
42struct 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
77typedef struct alac_file alac_file;
78
79typedef 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
89static 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
98void alac_set_info(alac_file *alac, char *inputbuffer)
99{
7ff85a81 100 unsigned char *ptr = inputbuffer;
6d6d7970
MM
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 */
129static 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_INFO, "alac: input buffer went out of bounds (%d >= %d)\n",
136 alac->input_buffer_index + 2, alac->input_buffer_size);
7ff85a81 137// exit (0);
6d6d7970
MM
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 */
166static 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 */
181static 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_INFO, "alac: input buffer went out of bounds (%d >= %d)\n",
188 alac->input_buffer_index + 2, alac->input_buffer_size);
7ff85a81 189// exit (0);
6d6d7970
MM
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
207static 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 */
221static 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
231void 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
361static 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
487void 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,
7ff85a81
MM
491 uint8_t interlacing_leftweight)
492{
6d6d7970
MM
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 /* output is always little endian */
512/*
513 if (host_bigendian) {
514 be2me_16(left);
515 be2me_16(right);
516 }
517*/
518
519 buffer_out[i*numchannels] = left;
520 buffer_out[i*numchannels + 1] = right;
521 }
522
523 return;
524 }
525
526 /* otherwise basic interlacing took place */
527 for (i = 0; i < numsamples; i++) {
528 int16_t left, right;
529
530 left = buffer_a[i];
531 right = buffer_b[i];
532
533 /* output is always little endian */
534/*
535 if (host_bigendian) {
536 be2me_16(left);
537 be2me_16(right);
538 }
539*/
540
541 buffer_out[i*numchannels] = left;
542 buffer_out[i*numchannels + 1] = right;
543 }
544}
545
546int decode_frame(ALACContext *s, alac_file *alac,
547 unsigned char *inbuffer,
548 int input_buffer_size,
7ff85a81
MM
549 void *outbuffer, int *outputsize)
550{
6d6d7970 551 int channels;
7ff85a81 552 int32_t outputsamples;
6d6d7970
MM
553
554 /* initialize from the extradata */
555 if (!s->context_initialized) {
556 if (s->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
557 av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
558 ALAC_EXTRADATA_SIZE);
559 return input_buffer_size;
560 }
561 alac_set_info(s->alac, s->avctx->extradata);
562 s->context_initialized = 1;
563 }
7ff85a81
MM
564
565 outputsamples = alac->setinfo_max_samples_per_frame;
6d6d7970
MM
566
567 /* setup the stream */
568 alac->input_buffer = inbuffer;
569 alac->input_buffer_index = 0;
570 alac->input_buffer_size = input_buffer_size;
571 alac->input_buffer_bitaccumulator = 0;
572
573 channels = readbits(alac, 3);
574
575 *outputsize = outputsamples * alac->bytespersample;
576
577 switch(channels) {
578 case 0: { /* 1 channel */
579 int hassize;
580 int isnotcompressed;
581 int readsamplesize;
582
583 int wasted_bytes;
584 int ricemodifier;
585
586
587 /* 2^result = something to do with output waiting.
588 * perhaps matters if we read > 1 frame in a pass?
589 */
590 readbits(alac, 4);
591
592 readbits(alac, 12); /* unknown, skip 12 bits */
593
594 hassize = readbits(alac, 1); /* the output sample size is stored soon */
595
596 wasted_bytes = readbits(alac, 2); /* unknown ? */
597
598 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
599
600 if (hassize) {
601 /* now read the number of samples,
602 * as a 32bit integer */
603 outputsamples = readbits(alac, 32);
604 *outputsize = outputsamples * alac->bytespersample;
605 }
606
607 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
608
609 if (!isnotcompressed) {
610 /* so it is compressed */
611 int16_t predictor_coef_table[32];
612 int predictor_coef_num;
613 int prediction_type;
614 int prediction_quantitization;
615 int i;
616
617 /* skip 16 bits, not sure what they are. seem to be used in
618 * two channel case */
619 readbits(alac, 8);
620 readbits(alac, 8);
621
622 prediction_type = readbits(alac, 4);
623 prediction_quantitization = readbits(alac, 4);
624
625 ricemodifier = readbits(alac, 3);
626 predictor_coef_num = readbits(alac, 5);
627
628 /* read the predictor table */
629 for (i = 0; i < predictor_coef_num; i++) {
630 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
631 }
632
633 if (wasted_bytes) {
634 /* these bytes seem to have something to do with
635 * > 2 channel files.
636 */
637 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
638 }
639
640 bastardized_rice_decompress(alac,
641 alac->predicterror_buffer_a,
642 outputsamples,
643 readsamplesize,
644 alac->setinfo_rice_initialhistory,
645 alac->setinfo_rice_kmodifier,
646 ricemodifier * alac->setinfo_rice_historymult / 4,
647 (1 << alac->setinfo_rice_kmodifier) - 1);
648
649 if (prediction_type == 0) {
650 /* adaptive fir */
651 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
652 alac->outputsamples_buffer_a,
653 outputsamples,
654 readsamplesize,
655 predictor_coef_table,
656 predictor_coef_num,
657 prediction_quantitization);
658 } else {
659 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
660 /* i think the only other prediction type (or perhaps this is just a
661 * boolean?) runs adaptive fir twice.. like:
662 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
663 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
664 * little strange..
665 */
666 }
667
668 } else {
669 /* not compressed, easy case */
670 if (readsamplesize <= 16) {
671 int i;
672 for (i = 0; i < outputsamples; i++) {
673 int32_t audiobits = readbits(alac, readsamplesize);
674
675 audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
676
677 alac->outputsamples_buffer_a[i] = audiobits;
678 }
679 } else {
680 int i;
681 for (i = 0; i < outputsamples; i++) {
682 int32_t audiobits;
683
684 audiobits = readbits(alac, 16);
685 /* special case of sign extension..
686 * as we'll be ORing the low 16bits into this */
687 audiobits = audiobits << 16;
688 audiobits = audiobits >> (32 - readsamplesize);
689
690 audiobits |= readbits(alac, readsamplesize - 16);
691
692 alac->outputsamples_buffer_a[i] = audiobits;
693 }
694 }
695 /* wasted_bytes = 0; // unused */
696 }
697
698 switch(alac->setinfo_sample_size) {
699 case 16: {
700 int i;
701 for (i = 0; i < outputsamples; i++) {
702 int16_t sample = alac->outputsamples_buffer_a[i];
703 be2me_16(sample);
704 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
705 }
706 break;
707 }
708 case 20:
709 case 24:
710 case 32:
711 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
712 break;
713 default:
714 break;
715 }
716 break;
717 }
718 case 1: { /* 2 channels */
719 int hassize;
720 int isnotcompressed;
721 int readsamplesize;
722
723 int wasted_bytes;
724
725 uint8_t interlacing_shift;
726 uint8_t interlacing_leftweight;
727
728 /* 2^result = something to do with output waiting.
729 * perhaps matters if we read > 1 frame in a pass?
730 */
731 readbits(alac, 4);
732
733 readbits(alac, 12); /* unknown, skip 12 bits */
734
735 hassize = readbits(alac, 1); /* the output sample size is stored soon */
736
737 wasted_bytes = readbits(alac, 2); /* unknown ? */
738
739 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
740
741 if (hassize) {
742 /* now read the number of samples,
743 * as a 32bit integer */
744 outputsamples = readbits(alac, 32);
745 *outputsize = outputsamples * alac->bytespersample;
746 }
747
748 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
749
750 if (!isnotcompressed) {
751 /* compressed */
752 int16_t predictor_coef_table_a[32];
753 int predictor_coef_num_a;
754 int prediction_type_a;
755 int prediction_quantitization_a;
756 int ricemodifier_a;
757
758 int16_t predictor_coef_table_b[32];
759 int predictor_coef_num_b;
760 int prediction_type_b;
761 int prediction_quantitization_b;
762 int ricemodifier_b;
763
764 int i;
765
766 interlacing_shift = readbits(alac, 8);
767 interlacing_leftweight = readbits(alac, 8);
768
769 /******** channel 1 ***********/
770 prediction_type_a = readbits(alac, 4);
771 prediction_quantitization_a = readbits(alac, 4);
772
773 ricemodifier_a = readbits(alac, 3);
774 predictor_coef_num_a = readbits(alac, 5);
775
776 /* read the predictor table */
777 for (i = 0; i < predictor_coef_num_a; i++) {
778 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
779 }
780
781 /******** channel 2 *********/
782 prediction_type_b = readbits(alac, 4);
783 prediction_quantitization_b = readbits(alac, 4);
784
785 ricemodifier_b = readbits(alac, 3);
786 predictor_coef_num_b = readbits(alac, 5);
787
788 /* read the predictor table */
789 for (i = 0; i < predictor_coef_num_b; i++) {
790 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
791 }
792
793 /*********************/
794 if (wasted_bytes) {
795 /* see mono case */
796 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
797 }
798
799 /* channel 1 */
800 bastardized_rice_decompress(alac,
801 alac->predicterror_buffer_a,
802 outputsamples,
803 readsamplesize,
804 alac->setinfo_rice_initialhistory,
805 alac->setinfo_rice_kmodifier,
806 ricemodifier_a * alac->setinfo_rice_historymult / 4,
807 (1 << alac->setinfo_rice_kmodifier) - 1);
808
809 if (prediction_type_a == 0) {
810 /* adaptive fir */
811 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
812 alac->outputsamples_buffer_a,
813 outputsamples,
814 readsamplesize,
815 predictor_coef_table_a,
816 predictor_coef_num_a,
817 prediction_quantitization_a);
818 } else {
819 /* see mono case */
820 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
821 }
822
823 /* channel 2 */
824 bastardized_rice_decompress(alac,
825 alac->predicterror_buffer_b,
826 outputsamples,
827 readsamplesize,
828 alac->setinfo_rice_initialhistory,
829 alac->setinfo_rice_kmodifier,
830 ricemodifier_b * alac->setinfo_rice_historymult / 4,
831 (1 << alac->setinfo_rice_kmodifier) - 1);
832
833 if (prediction_type_b == 0) {
834 /* adaptive fir */
835 predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
836 alac->outputsamples_buffer_b,
837 outputsamples,
838 readsamplesize,
839 predictor_coef_table_b,
840 predictor_coef_num_b,
841 prediction_quantitization_b);
842 } else {
843 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
844 }
845 } else {
846 /* not compressed, easy case */
847 if (alac->setinfo_sample_size <= 16) {
848 int i;
849 for (i = 0; i < outputsamples; i++) {
850 int32_t audiobits_a, audiobits_b;
851
852 audiobits_a = readbits(alac, alac->setinfo_sample_size);
853 audiobits_b = readbits(alac, alac->setinfo_sample_size);
854
855 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
856 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
857
858 alac->outputsamples_buffer_a[i] = audiobits_a;
859 alac->outputsamples_buffer_b[i] = audiobits_b;
860 }
861 } else {
862 int i;
863 for (i = 0; i < outputsamples; i++) {
864 int32_t audiobits_a, audiobits_b;
865
866 audiobits_a = readbits(alac, 16);
867 audiobits_a = audiobits_a << 16;
868 audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
869 audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
870
871 audiobits_b = readbits(alac, 16);
872 audiobits_b = audiobits_b << 16;
873 audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
874 audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
875
876 alac->outputsamples_buffer_a[i] = audiobits_a;
877 alac->outputsamples_buffer_b[i] = audiobits_b;
878 }
879 }
880 /* wasted_bytes = 0; */
881 interlacing_shift = 0;
882 interlacing_leftweight = 0;
883 }
884
885 switch(alac->setinfo_sample_size) {
886 case 16: {
887 deinterlace_16(alac->outputsamples_buffer_a,
888 alac->outputsamples_buffer_b,
889 (int16_t*)outbuffer,
890 alac->numchannels,
891 outputsamples,
892 interlacing_shift,
893 interlacing_leftweight);
894 break;
895 }
896 case 20:
897 case 24:
898 case 32:
899 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
900 break;
901 default:
902 break;
903 }
904
905 break;
906 }
907 }
908
909av_log(NULL, AV_LOG_INFO, "buf size = %d, consumed %d\n",
910 input_buffer_size, alac->input_buffer_index);
911
912 /* avoid infinite loop: if decoder consumed 0 bytes; report all bytes
913 * consumed */
914// if (alac->input_buffer_index)
915// return alac->input_buffer_index;
916// else
917 return input_buffer_size;
918}
919
920static int alac_decode_init(AVCodecContext * avctx)
921{
922 ALACContext *s = avctx->priv_data;
923 s->avctx = avctx;
924 s->context_initialized = 0;
925
926 s->alac = av_malloc(sizeof(alac_file));
927
928 s->alac->samplesize = s->avctx->bits_per_sample;
929 s->alac->numchannels = s->avctx->channels;
930 s->alac->bytespersample = (s->alac->samplesize / 8) * s->alac->numchannels;
931
932 return 0;
933}
934
935static int alac_decode_frame(AVCodecContext *avctx,
936 void *data, int *data_size,
937 uint8_t *buf, int buf_size)
938{
939 ALACContext *s = avctx->priv_data;
940 int bytes_consumed = buf_size;
941
942 if (buf)
943 bytes_consumed = decode_frame(s, s->alac, buf, buf_size,
944 data, data_size);
945
946 return bytes_consumed;
947}
948
949static int alac_decode_close(AVCodecContext *avctx)
950{
951 ALACContext *s = avctx->priv_data;
952
953 av_free(s->alac->predicterror_buffer_a);
954 av_free(s->alac->predicterror_buffer_b);
955
956 av_free(s->alac->outputsamples_buffer_a);
957 av_free(s->alac->outputsamples_buffer_b);
958
959 return 0;
960}
961
962AVCodec alac_decoder = {
963 "alac",
964 CODEC_TYPE_AUDIO,
965 CODEC_ID_ALAC,
966 sizeof(ALACContext),
967 alac_decode_init,
968 NULL,
969 alac_decode_close,
970 alac_decode_frame,
971};