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