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