alac: change VLAs to fixed size
[libav.git] / libavcodec / alac.c
CommitLineData
6d6d7970
MM
1/*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
6d6d7970 4 *
b78e7197
DB
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
6d6d7970
MM
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
b78e7197 10 * version 2.1 of the License, or (at your option) any later version.
6d6d7970 11 *
b78e7197 12 * FFmpeg is distributed in the hope that it will be useful,
6d6d7970
MM
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
b78e7197 18 * License along with FFmpeg; if not, write to the Free Software
5509bffa 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6d6d7970
MM
20 */
21
22/**
ba87f080 23 * @file
6d6d7970
MM
24 * ALAC (Apple Lossless Audio Codec) decoder
25 * @author 2005 David Hammerton
26 *
27 * For more information on the ALAC format, visit:
28 * http://crazney.net/programs/itunes/alac.html
29 *
30 * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
31 * passed through the extradata[_size] fields. This atom is tacked onto
32 * the end of an 'alac' stsd atom and has the following format:
33 * bytes 0-3 atom size (0x24), big-endian
34 * bytes 4-7 atom type ('alac', not the 'alac' tag from start of stsd)
35 * bytes 8-35 data bytes needed by decoder
a1db1fc4
AB
36 *
37 * Extradata:
38 * 32bit size
39 * 32bit tag (=alac)
40 * 32bit zero?
41 * 32bit max sample per frame
42 * 8bit ?? (zero?)
43 * 8bit sample size
44 * 8bit history mult
45 * 8bit initial history
46 * 8bit kmodifier
47 * 8bit channels?
48 * 16bit ??
49 * 32bit max coded frame size
50 * 32bit bitrate?
51 * 32bit samplerate
6d6d7970
MM
52 */
53
54
55#include "avcodec.h"
9106a698 56#include "get_bits.h"
f79488d4 57#include "bytestream.h"
becc0ef9 58#include "unary.h"
8858990f 59#include "mathops.h"
6d6d7970
MM
60
61#define ALAC_EXTRADATA_SIZE 36
153696a6 62#define MAX_CHANNELS 2
6d6d7970 63
6d021b00
MM
64typedef struct {
65
66 AVCodecContext *avctx;
67 GetBitContext gb;
68 /* init to 0; first frame decode should initialize from extradata and
69 * set this to 1 */
70 int context_initialized;
6d6d7970 71
6d6d7970
MM
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
f430c7b6
JM
80 int32_t *wasted_bits_buffer[MAX_CHANNELS];
81
6d6d7970
MM
82 /* stuff from setinfo */
83 uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
6d6d7970
MM
84 uint8_t setinfo_sample_size; /* 0x10 */
85 uint8_t setinfo_rice_historymult; /* 0x28 */
86 uint8_t setinfo_rice_initialhistory; /* 0x0a */
87 uint8_t setinfo_rice_kmodifier; /* 0x0e */
6d6d7970 88 /* end setinfo stuff */
6d6d7970 89
f430c7b6 90 int wasted_bits;
6d6d7970
MM
91} ALACContext;
92
6d021b00 93static void allocate_buffers(ALACContext *alac)
6d6d7970 94{
153696a6
VS
95 int chan;
96 for (chan = 0; chan < MAX_CHANNELS; chan++) {
97 alac->predicterror_buffer[chan] =
98 av_malloc(alac->setinfo_max_samples_per_frame * 4);
6d6d7970 99
153696a6
VS
100 alac->outputsamples_buffer[chan] =
101 av_malloc(alac->setinfo_max_samples_per_frame * 4);
f430c7b6
JM
102
103 alac->wasted_bits_buffer[chan] = av_malloc(alac->setinfo_max_samples_per_frame * 4);
153696a6 104 }
6d6d7970
MM
105}
106
3a1a7e32 107static int alac_set_info(ALACContext *alac)
6d6d7970 108{
b5777b94 109 const unsigned char *ptr = alac->avctx->extradata;
6d6d7970
MM
110
111 ptr += 4; /* size */
112 ptr += 4; /* alac */
113 ptr += 4; /* 0 ? */
114
fead30d4 115 if(AV_RB32(ptr) >= UINT_MAX/4){
3a1a7e32
MN
116 av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
117 return -1;
118 }
f79488d4
VS
119
120 /* buffer size / 2 ? */
121 alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
4c63c597 122 ptr++; /* ??? */
a1301f29 123 alac->setinfo_sample_size = *ptr++;
e5838c90
MC
124 if (alac->setinfo_sample_size > 32) {
125 av_log(alac->avctx, AV_LOG_ERROR, "setinfo_sample_size too large\n");
126 return -1;
127 }
a1301f29
VS
128 alac->setinfo_rice_historymult = *ptr++;
129 alac->setinfo_rice_initialhistory = *ptr++;
130 alac->setinfo_rice_kmodifier = *ptr++;
4c63c597 131 ptr++; /* channels? */
db08882e
VS
132 bytestream_get_be16(&ptr); /* ??? */
133 bytestream_get_be32(&ptr); /* max coded frame size */
134 bytestream_get_be32(&ptr); /* bitrate ? */
4c63c597 135 bytestream_get_be32(&ptr); /* samplerate */
6d6d7970
MM
136
137 allocate_buffers(alac);
3a1a7e32
MN
138
139 return 0;
6d6d7970
MM
140}
141
5eeba07e
MN
142static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
143 /* read x - number of 1s before 0 represent the rice */
144 int x = get_unary_0_9(gb);
145
146 if (x > 8) { /* RICE THRESHOLD */
147 /* use alternative encoding */
148 x = get_bits(gb, readsamplesize);
149 } else {
1f655c4f
MN
150 if (k >= limit)
151 k = limit;
5a5a27c5 152
1f655c4f
MN
153 if (k != 1) {
154 int extrabits = show_bits(gb, k);
5a5a27c5 155
1f655c4f
MN
156 /* multiply x by 2^k - 1, as part of their strange algorithm */
157 x = (x << k) - x;
5a5a27c5 158
1f655c4f
MN
159 if (extrabits > 1) {
160 x += extrabits - 1;
161 skip_bits(gb, k);
162 } else
163 skip_bits(gb, k - 1);
164 }
5eeba07e 165 }
5a5a27c5
MN
166 return x;
167}
168
1b47fafd 169static void bastardized_rice_decompress(ALACContext *alac,
6d6d7970
MM
170 int32_t *output_buffer,
171 int output_size,
172 int readsamplesize, /* arg_10 */
173 int rice_initialhistory, /* arg424->b */
174 int rice_kmodifier, /* arg424->d */
175 int rice_historymult, /* arg424->c */
176 int rice_kmodifier_mask /* arg424->e */
177 )
178{
179 int output_count;
180 unsigned int history = rice_initialhistory;
181 int sign_modifier = 0;
182
183 for (output_count = 0; output_count < output_size; output_count++) {
becc0ef9 184 int32_t x;
6d6d7970
MM
185 int32_t x_modified;
186 int32_t final_val;
187
1f655c4f
MN
188 /* standard rice encoding */
189 int k; /* size of extra bits */
6d6d7970 190
1f655c4f 191 /* read k, that is bits as is */
8431603a 192 k = av_log2((history >> 9) + 3);
1f655c4f 193 x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
6d6d7970
MM
194
195 x_modified = sign_modifier + x;
196 final_val = (x_modified + 1) / 2;
197 if (x_modified & 1) final_val *= -1;
198
199 output_buffer[output_count] = final_val;
200
201 sign_modifier = 0;
202
203 /* now update the history */
10024d44
VS
204 history += x_modified * rice_historymult
205 - ((history * rice_historymult) >> 9);
6d6d7970
MM
206
207 if (x_modified > 0xffff)
208 history = 0xffff;
209
210 /* special case: there may be compressed blocks of 0 */
211 if ((history < 128) && (output_count+1 < output_size)) {
83e9a67d
MN
212 int k;
213 unsigned int block_size;
6d6d7970
MM
214
215 sign_modifier = 1;
216
8431603a 217 k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
6d6d7970 218
1f655c4f 219 block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
6d6d7970
MM
220
221 if (block_size > 0) {
83e9a67d
MN
222 if(block_size >= output_size - output_count){
223 av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
224 block_size= output_size - output_count - 1;
225 }
6d6d7970
MM
226 memset(&output_buffer[output_count+1], 0, block_size * 4);
227 output_count += block_size;
6d6d7970
MM
228 }
229
230 if (block_size > 0xffff)
231 sign_modifier = 0;
232
233 history = 0;
234 }
235 }
236}
237
321c3138
VS
238static inline int sign_only(int v)
239{
240 return v ? FFSIGN(v) : 0;
241}
6d6d7970
MM
242
243static void predictor_decompress_fir_adapt(int32_t *error_buffer,
244 int32_t *buffer_out,
245 int output_size,
246 int readsamplesize,
247 int16_t *predictor_coef_table,
248 int predictor_coef_num,
249 int predictor_quantitization)
250{
251 int i;
252
253 /* first sample always copies */
254 *buffer_out = *error_buffer;
255
256 if (!predictor_coef_num) {
10024d44
VS
257 if (output_size <= 1)
258 return;
259
6d6d7970
MM
260 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
261 return;
262 }
263
264 if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
265 /* second-best case scenario for fir decompression,
266 * error describes a small difference from the previous sample only
267 */
10024d44
VS
268 if (output_size <= 1)
269 return;
6d6d7970
MM
270 for (i = 0; i < output_size - 1; i++) {
271 int32_t prev_value;
272 int32_t error_value;
273
274 prev_value = buffer_out[i];
275 error_value = error_buffer[i+1];
10024d44 276 buffer_out[i+1] =
8858990f 277 sign_extend((prev_value + error_value), readsamplesize);
6d6d7970
MM
278 }
279 return;
280 }
281
282 /* read warm-up samples */
10024d44 283 if (predictor_coef_num > 0)
6d6d7970
MM
284 for (i = 0; i < predictor_coef_num; i++) {
285 int32_t val;
286
287 val = buffer_out[i] + error_buffer[i+1];
8858990f 288 val = sign_extend(val, readsamplesize);
6d6d7970
MM
289 buffer_out[i+1] = val;
290 }
6d6d7970
MM
291
292#if 0
293 /* 4 and 8 are very common cases (the only ones i've seen). these
52b541ad 294 * should be unrolled and optimized
6d6d7970
MM
295 */
296 if (predictor_coef_num == 4) {
52b541ad 297 /* FIXME: optimized general case */
6d6d7970
MM
298 return;
299 }
300
301 if (predictor_coef_table == 8) {
52b541ad 302 /* FIXME: optimized general case */
6d6d7970
MM
303 return;
304 }
305#endif
306
6d6d7970
MM
307 /* general case */
308 if (predictor_coef_num > 0) {
10024d44 309 for (i = predictor_coef_num + 1; i < output_size; i++) {
6d6d7970
MM
310 int j;
311 int sum = 0;
312 int outval;
313 int error_val = error_buffer[i];
314
315 for (j = 0; j < predictor_coef_num; j++) {
316 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
317 predictor_coef_table[j];
318 }
319
320 outval = (1 << (predictor_quantitization-1)) + sum;
321 outval = outval >> predictor_quantitization;
322 outval = outval + buffer_out[0] + error_val;
8858990f 323 outval = sign_extend(outval, readsamplesize);
6d6d7970
MM
324
325 buffer_out[predictor_coef_num+1] = outval;
326
327 if (error_val > 0) {
328 int predictor_num = predictor_coef_num - 1;
329
330 while (predictor_num >= 0 && error_val > 0) {
331 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138 332 int sign = sign_only(val);
6d6d7970
MM
333
334 predictor_coef_table[predictor_num] -= sign;
335
336 val *= sign; /* absolute value */
337
338 error_val -= ((val >> predictor_quantitization) *
339 (predictor_coef_num - predictor_num));
340
341 predictor_num--;
342 }
343 } else if (error_val < 0) {
344 int predictor_num = predictor_coef_num - 1;
345
346 while (predictor_num >= 0 && error_val < 0) {
347 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138 348 int sign = - sign_only(val);
6d6d7970
MM
349
350 predictor_coef_table[predictor_num] -= sign;
351
352 val *= sign; /* neg value */
353
354 error_val -= ((val >> predictor_quantitization) *
355 (predictor_coef_num - predictor_num));
356
357 predictor_num--;
358 }
359 }
360
361 buffer_out++;
362 }
363 }
364}
365
4a5e6389 366static void reconstruct_stereo_16(int32_t *buffer[MAX_CHANNELS],
db695867
VS
367 int16_t *buffer_out,
368 int numchannels, int numsamples,
369 uint8_t interlacing_shift,
370 uint8_t interlacing_leftweight)
7ff85a81 371{
6d6d7970 372 int i;
10024d44
VS
373 if (numsamples <= 0)
374 return;
6d6d7970
MM
375
376 /* weighted interlacing */
377 if (interlacing_leftweight) {
378 for (i = 0; i < numsamples; i++) {
9c8d9f25 379 int32_t a, b;
6d6d7970 380
9c8d9f25
VS
381 a = buffer[0][i];
382 b = buffer[1][i];
6d6d7970 383
9c8d9f25
VS
384 a -= (b * interlacing_leftweight) >> interlacing_shift;
385 b += a;
6d6d7970 386
9c8d9f25
VS
387 buffer_out[i*numchannels] = b;
388 buffer_out[i*numchannels + 1] = a;
6d6d7970
MM
389 }
390
391 return;
392 }
393
394 /* otherwise basic interlacing took place */
395 for (i = 0; i < numsamples; i++) {
396 int16_t left, right;
397
c9128d56
VS
398 left = buffer[0][i];
399 right = buffer[1][i];
6d6d7970 400
6d6d7970
MM
401 buffer_out[i*numchannels] = left;
402 buffer_out[i*numchannels + 1] = right;
403 }
404}
405
f430c7b6
JM
406static void decorrelate_stereo_24(int32_t *buffer[MAX_CHANNELS],
407 int32_t *buffer_out,
408 int32_t *wasted_bits_buffer[MAX_CHANNELS],
409 int wasted_bits,
410 int numchannels, int numsamples,
411 uint8_t interlacing_shift,
412 uint8_t interlacing_leftweight)
413{
414 int i;
415
416 if (numsamples <= 0)
417 return;
418
419 /* weighted interlacing */
420 if (interlacing_leftweight) {
421 for (i = 0; i < numsamples; i++) {
422 int32_t a, b;
423
424 a = buffer[0][i];
425 b = buffer[1][i];
426
427 a -= (b * interlacing_leftweight) >> interlacing_shift;
428 b += a;
429
430 if (wasted_bits) {
431 b = (b << wasted_bits) | wasted_bits_buffer[0][i];
432 a = (a << wasted_bits) | wasted_bits_buffer[1][i];
433 }
434
435 buffer_out[i * numchannels] = b << 8;
436 buffer_out[i * numchannels + 1] = a << 8;
437 }
438 } else {
439 for (i = 0; i < numsamples; i++) {
440 int32_t left, right;
441
442 left = buffer[0][i];
443 right = buffer[1][i];
444
445 if (wasted_bits) {
446 left = (left << wasted_bits) | wasted_bits_buffer[0][i];
447 right = (right << wasted_bits) | wasted_bits_buffer[1][i];
448 }
449
450 buffer_out[i * numchannels] = left << 8;
451 buffer_out[i * numchannels + 1] = right << 8;
452 }
453 }
454}
455
f770ee03
MM
456static int alac_decode_frame(AVCodecContext *avctx,
457 void *outbuffer, int *outputsize,
7a00bbad 458 AVPacket *avpkt)
7ff85a81 459{
7a00bbad
TB
460 const uint8_t *inbuffer = avpkt->data;
461 int input_buffer_size = avpkt->size;
6d021b00 462 ALACContext *alac = avctx->priv_data;
f770ee03 463
6d6d7970 464 int channels;
494e3531 465 unsigned int outputsamples;
a562e2e6 466 int hassize;
95801b6a 467 unsigned int readsamplesize;
a562e2e6 468 int isnotcompressed;
d562ba23
VS
469 uint8_t interlacing_shift;
470 uint8_t interlacing_leftweight;
6d6d7970 471
f770ee03
MM
472 /* short-circuit null buffers */
473 if (!inbuffer || !input_buffer_size)
474 return input_buffer_size;
475
6d6d7970 476 /* initialize from the extradata */
6d021b00
MM
477 if (!alac->context_initialized) {
478 if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
a1db1fc4 479 av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
6d6d7970
MM
480 ALAC_EXTRADATA_SIZE);
481 return input_buffer_size;
482 }
1e25a7e7
MH
483 if (alac_set_info(alac)) {
484 av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
485 return input_buffer_size;
486 }
6d021b00 487 alac->context_initialized = 1;
6d6d7970 488 }
7ff85a81 489
6d021b00 490 init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
6d6d7970 491
e3be5693 492 channels = get_bits(&alac->gb, 3) + 1;
f1752010
VS
493 if (channels > MAX_CHANNELS) {
494 av_log(avctx, AV_LOG_ERROR, "channels > %d not supported\n",
495 MAX_CHANNELS);
496 return input_buffer_size;
497 }
6d6d7970 498
586e5bd9
VS
499 /* 2^result = something to do with output waiting.
500 * perhaps matters if we read > 1 frame in a pass?
501 */
7ae7300e 502 skip_bits(&alac->gb, 4);
6d6d7970 503
7ae7300e 504 skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
6d6d7970 505
586e5bd9 506 /* the output sample size is stored soon */
5fc32c27 507 hassize = get_bits1(&alac->gb);
6d6d7970 508
f430c7b6 509 alac->wasted_bits = get_bits(&alac->gb, 2) << 3;
6d6d7970 510
586e5bd9 511 /* whether the frame is compressed */
5fc32c27 512 isnotcompressed = get_bits1(&alac->gb);
6d6d7970 513
586e5bd9
VS
514 if (hassize) {
515 /* now read the number of samples as a 32bit integer */
59d598b9 516 outputsamples = get_bits_long(&alac->gb, 32);
494e3531
MN
517 if(outputsamples > alac->setinfo_max_samples_per_frame){
518 av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
519 return -1;
520 }
586e5bd9
VS
521 } else
522 outputsamples = alac->setinfo_max_samples_per_frame;
a562e2e6 523
f430c7b6
JM
524 switch (alac->setinfo_sample_size) {
525 case 16: avctx->sample_fmt = SAMPLE_FMT_S16;
526 alac->bytespersample = channels << 1;
527 break;
528 case 24: avctx->sample_fmt = SAMPLE_FMT_S32;
529 alac->bytespersample = channels << 2;
530 break;
531 default: av_log(avctx, AV_LOG_ERROR, "Sample depth %d is not supported.\n",
532 alac->setinfo_sample_size);
533 return -1;
534 }
535
f7739f37
MN
536 if(outputsamples > *outputsize / alac->bytespersample){
537 av_log(avctx, AV_LOG_ERROR, "sample buffer too small\n");
538 return -1;
539 }
540
586e5bd9 541 *outputsize = outputsamples * alac->bytespersample;
f430c7b6 542 readsamplesize = alac->setinfo_sample_size - (alac->wasted_bits) + channels - 1;
95801b6a
MC
543 if (readsamplesize > MIN_CACHE_BITS) {
544 av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
545 return -1;
546 }
6d6d7970 547
586e5bd9
VS
548 if (!isnotcompressed) {
549 /* so it is compressed */
986f143a
MR
550 int16_t predictor_coef_table[MAX_CHANNELS][32];
551 int predictor_coef_num[MAX_CHANNELS];
552 int prediction_type[MAX_CHANNELS];
553 int prediction_quantitization[MAX_CHANNELS];
554 int ricemodifier[MAX_CHANNELS];
586e5bd9 555 int i, chan;
6d6d7970 556
586e5bd9
VS
557 interlacing_shift = get_bits(&alac->gb, 8);
558 interlacing_leftweight = get_bits(&alac->gb, 8);
6d6d7970 559
586e5bd9 560 for (chan = 0; chan < channels; chan++) {
7f268016
VS
561 prediction_type[chan] = get_bits(&alac->gb, 4);
562 prediction_quantitization[chan] = get_bits(&alac->gb, 4);
6d6d7970 563
7f268016
VS
564 ricemodifier[chan] = get_bits(&alac->gb, 3);
565 predictor_coef_num[chan] = get_bits(&alac->gb, 5);
6d6d7970
MM
566
567 /* read the predictor table */
10024d44 568 for (i = 0; i < predictor_coef_num[chan]; i++)
7f268016 569 predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
586e5bd9 570 }
6d6d7970 571
f430c7b6
JM
572 if (alac->wasted_bits) {
573 int i, ch;
574 for (i = 0; i < outputsamples; i++) {
575 for (ch = 0; ch < channels; ch++)
576 alac->wasted_bits_buffer[ch][i] = get_bits(&alac->gb, alac->wasted_bits);
577 }
578 }
586e5bd9 579 for (chan = 0; chan < channels; chan++) {
6d6d7970 580 bastardized_rice_decompress(alac,
7f268016 581 alac->predicterror_buffer[chan],
6d6d7970
MM
582 outputsamples,
583 readsamplesize,
584 alac->setinfo_rice_initialhistory,
585 alac->setinfo_rice_kmodifier,
7f268016 586 ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
6d6d7970
MM
587 (1 << alac->setinfo_rice_kmodifier) - 1);
588
7f268016 589 if (prediction_type[chan] == 0) {
586e5bd9 590 /* adaptive fir */
7f268016
VS
591 predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
592 alac->outputsamples_buffer[chan],
6d6d7970
MM
593 outputsamples,
594 readsamplesize,
7f268016
VS
595 predictor_coef_table[chan],
596 predictor_coef_num[chan],
597 prediction_quantitization[chan]);
6d6d7970 598 } else {
7f268016 599 av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
60c4a31c
VS
600 /* I think the only other prediction type (or perhaps this is
601 * just a boolean?) runs adaptive fir twice.. like:
10fb5763
VS
602 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
603 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
604 * little strange..
605 */
6d6d7970 606 }
586e5bd9
VS
607 }
608 } else {
609 /* not compressed, easy case */
6827f6f2 610 int i, chan;
f430c7b6 611 if (alac->setinfo_sample_size <= 16) {
66b26265
JM
612 for (i = 0; i < outputsamples; i++)
613 for (chan = 0; chan < channels; chan++) {
6827f6f2 614 int32_t audiobits;
6d6d7970 615
6053da01 616 audiobits = get_sbits_long(&alac->gb, alac->setinfo_sample_size);
10fb5763 617
6827f6f2
MC
618 alac->outputsamples_buffer[chan][i] = audiobits;
619 }
f430c7b6
JM
620 } else {
621 for (i = 0; i < outputsamples; i++) {
622 for (chan = 0; chan < channels; chan++) {
623 alac->outputsamples_buffer[chan][i] = get_bits(&alac->gb,
624 alac->setinfo_sample_size);
625 alac->outputsamples_buffer[chan][i] = sign_extend(alac->outputsamples_buffer[chan][i],
626 alac->setinfo_sample_size);
627 }
628 }
629 }
630 alac->wasted_bits = 0;
586e5bd9
VS
631 interlacing_shift = 0;
632 interlacing_leftweight = 0;
633 }
e5ab7379
MC
634 if (get_bits(&alac->gb, 3) != 7)
635 av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
6d6d7970 636
586e5bd9 637 switch(alac->setinfo_sample_size) {
d58d7ade 638 case 16:
586e5bd9 639 if (channels == 2) {
4a5e6389 640 reconstruct_stereo_16(alac->outputsamples_buffer,
92df8910
VS
641 (int16_t*)outbuffer,
642 alac->numchannels,
643 outputsamples,
644 interlacing_shift,
645 interlacing_leftweight);
586e5bd9
VS
646 } else {
647 int i;
648 for (i = 0; i < outputsamples; i++) {
f6d29165 649 ((int16_t*)outbuffer)[i] = alac->outputsamples_buffer[0][i];
586e5bd9 650 }
6d6d7970 651 }
586e5bd9 652 break;
586e5bd9 653 case 24:
f430c7b6
JM
654 if (channels == 2) {
655 decorrelate_stereo_24(alac->outputsamples_buffer,
656 outbuffer,
657 alac->wasted_bits_buffer,
658 alac->wasted_bits,
659 alac->numchannels,
660 outputsamples,
661 interlacing_shift,
662 interlacing_leftweight);
663 } else {
664 int i;
665 for (i = 0; i < outputsamples; i++)
666 ((int32_t *)outbuffer)[i] = alac->outputsamples_buffer[0][i] << 8;
667 }
586e5bd9
VS
668 break;
669 }
6d6d7970 670
e5ab7379
MC
671 if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
672 av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
673
f770ee03 674 return input_buffer_size;
6d6d7970
MM
675}
676
98a6fff9 677static av_cold int alac_decode_init(AVCodecContext * avctx)
6d6d7970 678{
6d021b00
MM
679 ALACContext *alac = avctx->priv_data;
680 alac->avctx = avctx;
681 alac->context_initialized = 0;
6d6d7970 682
6d021b00 683 alac->numchannels = alac->avctx->channels;
6d6d7970
MM
684
685 return 0;
686}
687
98a6fff9 688static av_cold int alac_decode_close(AVCodecContext *avctx)
6d6d7970 689{
6d021b00 690 ALACContext *alac = avctx->priv_data;
6d6d7970 691
153696a6
VS
692 int chan;
693 for (chan = 0; chan < MAX_CHANNELS; chan++) {
e243eee4
JM
694 av_freep(&alac->predicterror_buffer[chan]);
695 av_freep(&alac->outputsamples_buffer[chan]);
f430c7b6 696 av_freep(&alac->wasted_bits_buffer[chan]);
153696a6 697 }
6d6d7970
MM
698
699 return 0;
700}
701
702AVCodec alac_decoder = {
703 "alac",
72415b2a 704 AVMEDIA_TYPE_AUDIO,
6d6d7970
MM
705 CODEC_ID_ALAC,
706 sizeof(ALACContext),
707 alac_decode_init,
708 NULL,
709 alac_decode_close,
710 alac_decode_frame,
fe4bf374 711 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
6d6d7970 712};