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