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