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