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