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