spelling
[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/**
23 * @file alac.c
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"
6d021b00 56#include "bitstream.h"
f79488d4 57#include "bytestream.h"
becc0ef9 58#include "unary.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);
a1301f29
VS
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++;
128 /* channels? */
129 alac->setinfo_7f = *ptr++;
f79488d4
VS
130 alac->setinfo_80 = bytestream_get_be16(&ptr);
131 /* max coded frame size */
132 alac->setinfo_82 = bytestream_get_be32(&ptr);
133 /* bitrate ? */
134 alac->setinfo_86 = bytestream_get_be32(&ptr);
135 /* samplerate */
136 alac->setinfo_8a_rate = bytestream_get_be32(&ptr);
6d6d7970
MM
137
138 allocate_buffers(alac);
3a1a7e32
MN
139
140 return 0;
6d6d7970
MM
141}
142
d0da8020 143static inline int count_leading_zeros(int32_t input)
6d6d7970 144{
d0da8020 145 return 31-av_log2(input);
6d6d7970
MM
146}
147
1b47fafd 148static void bastardized_rice_decompress(ALACContext *alac,
6d6d7970
MM
149 int32_t *output_buffer,
150 int output_size,
151 int readsamplesize, /* arg_10 */
152 int rice_initialhistory, /* arg424->b */
153 int rice_kmodifier, /* arg424->d */
154 int rice_historymult, /* arg424->c */
155 int rice_kmodifier_mask /* arg424->e */
156 )
157{
158 int output_count;
159 unsigned int history = rice_initialhistory;
160 int sign_modifier = 0;
161
162 for (output_count = 0; output_count < output_size; output_count++) {
becc0ef9 163 int32_t x;
6d6d7970
MM
164 int32_t x_modified;
165 int32_t final_val;
166
167 /* read x - number of 1s before 0 represent the rice */
becc0ef9 168 x = get_unary_0_9(&alac->gb);
6d6d7970 169
6d6d7970 170 if (x > 8) { /* RICE THRESHOLD */
10024d44 171 /* use alternative encoding */
6d6d7970
MM
172 int32_t value;
173
6d021b00 174 value = get_bits(&alac->gb, readsamplesize);
6d6d7970
MM
175
176 /* mask value to readsamplesize size */
177 if (readsamplesize != 32)
178 value &= (0xffffffff >> (32 - readsamplesize));
179
180 x = value;
181 } else {
10024d44 182 /* standard rice encoding */
6d6d7970
MM
183 int extrabits;
184 int k; /* size of extra bits */
185
186 /* read k, that is bits as is */
187 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
188
115329f1 189 if (k < 0)
6d6d7970 190 k += rice_kmodifier;
115329f1 191 else
6d6d7970
MM
192 k = rice_kmodifier;
193
194 if (k != 1) {
6d021b00 195 extrabits = show_bits(&alac->gb, k);
6d6d7970
MM
196
197 /* multiply x by 2^k - 1, as part of their strange algorithm */
198 x = (x << k) - x;
199
200 if (extrabits > 1) {
201 x += extrabits - 1;
7ae7300e 202 skip_bits(&alac->gb, k);
10024d44 203 } else
7ae7300e 204 skip_bits(&alac->gb, k - 1);
6d6d7970
MM
205 }
206 }
207
208 x_modified = sign_modifier + x;
209 final_val = (x_modified + 1) / 2;
210 if (x_modified & 1) final_val *= -1;
211
212 output_buffer[output_count] = final_val;
213
214 sign_modifier = 0;
215
216 /* now update the history */
10024d44
VS
217 history += x_modified * rice_historymult
218 - ((history * rice_historymult) >> 9);
6d6d7970
MM
219
220 if (x_modified > 0xffff)
221 history = 0xffff;
222
223 /* special case: there may be compressed blocks of 0 */
224 if ((history < 128) && (output_count+1 < output_size)) {
225 int block_size;
226
227 sign_modifier = 1;
228
becc0ef9 229 x = get_unary_0_9(&alac->gb);
6d6d7970
MM
230
231 if (x > 8) {
6d021b00 232 block_size = get_bits(&alac->gb, 16);
6d6d7970
MM
233 block_size &= 0xffff;
234 } else {
235 int k;
236 int extrabits;
237
238 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
239
6d021b00 240 extrabits = show_bits(&alac->gb, k);
6d6d7970
MM
241
242 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
243 + extrabits - 1;
244
245 if (extrabits < 2) {
246 x = 1 - extrabits;
247 block_size += x;
7ae7300e 248 skip_bits(&alac->gb, k - 1);
6d021b00 249 } else {
7ae7300e 250 skip_bits(&alac->gb, k);
6d6d7970
MM
251 }
252 }
253
254 if (block_size > 0) {
255 memset(&output_buffer[output_count+1], 0, block_size * 4);
256 output_count += block_size;
6d6d7970
MM
257 }
258
259 if (block_size > 0xffff)
260 sign_modifier = 0;
261
262 history = 0;
263 }
264 }
265}
266
aa33d809 267static inline int32_t extend_sign32(int32_t val, int bits)
321c3138
VS
268{
269 return (val << (32 - bits)) >> (32 - bits);
270}
6d6d7970 271
321c3138
VS
272static inline int sign_only(int v)
273{
274 return v ? FFSIGN(v) : 0;
275}
6d6d7970
MM
276
277static void predictor_decompress_fir_adapt(int32_t *error_buffer,
278 int32_t *buffer_out,
279 int output_size,
280 int readsamplesize,
281 int16_t *predictor_coef_table,
282 int predictor_coef_num,
283 int predictor_quantitization)
284{
285 int i;
286
287 /* first sample always copies */
288 *buffer_out = *error_buffer;
289
290 if (!predictor_coef_num) {
10024d44
VS
291 if (output_size <= 1)
292 return;
293
6d6d7970
MM
294 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
295 return;
296 }
297
298 if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
299 /* second-best case scenario for fir decompression,
300 * error describes a small difference from the previous sample only
301 */
10024d44
VS
302 if (output_size <= 1)
303 return;
6d6d7970
MM
304 for (i = 0; i < output_size - 1; i++) {
305 int32_t prev_value;
306 int32_t error_value;
307
308 prev_value = buffer_out[i];
309 error_value = error_buffer[i+1];
10024d44 310 buffer_out[i+1] =
aa33d809 311 extend_sign32((prev_value + error_value), readsamplesize);
6d6d7970
MM
312 }
313 return;
314 }
315
316 /* read warm-up samples */
10024d44 317 if (predictor_coef_num > 0)
6d6d7970
MM
318 for (i = 0; i < predictor_coef_num; i++) {
319 int32_t val;
320
321 val = buffer_out[i] + error_buffer[i+1];
aa33d809 322 val = extend_sign32(val, readsamplesize);
6d6d7970
MM
323 buffer_out[i+1] = val;
324 }
6d6d7970
MM
325
326#if 0
327 /* 4 and 8 are very common cases (the only ones i've seen). these
52b541ad 328 * should be unrolled and optimized
6d6d7970
MM
329 */
330 if (predictor_coef_num == 4) {
52b541ad 331 /* FIXME: optimized general case */
6d6d7970
MM
332 return;
333 }
334
335 if (predictor_coef_table == 8) {
52b541ad 336 /* FIXME: optimized general case */
6d6d7970
MM
337 return;
338 }
339#endif
340
6d6d7970
MM
341 /* general case */
342 if (predictor_coef_num > 0) {
10024d44 343 for (i = predictor_coef_num + 1; i < output_size; i++) {
6d6d7970
MM
344 int j;
345 int sum = 0;
346 int outval;
347 int error_val = error_buffer[i];
348
349 for (j = 0; j < predictor_coef_num; j++) {
350 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
351 predictor_coef_table[j];
352 }
353
354 outval = (1 << (predictor_quantitization-1)) + sum;
355 outval = outval >> predictor_quantitization;
356 outval = outval + buffer_out[0] + error_val;
aa33d809 357 outval = extend_sign32(outval, readsamplesize);
6d6d7970
MM
358
359 buffer_out[predictor_coef_num+1] = outval;
360
361 if (error_val > 0) {
362 int predictor_num = predictor_coef_num - 1;
363
364 while (predictor_num >= 0 && error_val > 0) {
365 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138 366 int sign = sign_only(val);
6d6d7970
MM
367
368 predictor_coef_table[predictor_num] -= sign;
369
370 val *= sign; /* absolute value */
371
372 error_val -= ((val >> predictor_quantitization) *
373 (predictor_coef_num - predictor_num));
374
375 predictor_num--;
376 }
377 } else if (error_val < 0) {
378 int predictor_num = predictor_coef_num - 1;
379
380 while (predictor_num >= 0 && error_val < 0) {
381 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138 382 int sign = - sign_only(val);
6d6d7970
MM
383
384 predictor_coef_table[predictor_num] -= sign;
385
386 val *= sign; /* neg value */
387
388 error_val -= ((val >> predictor_quantitization) *
389 (predictor_coef_num - predictor_num));
390
391 predictor_num--;
392 }
393 }
394
395 buffer_out++;
396 }
397 }
398}
399
4a5e6389 400static void reconstruct_stereo_16(int32_t *buffer[MAX_CHANNELS],
db695867
VS
401 int16_t *buffer_out,
402 int numchannels, int numsamples,
403 uint8_t interlacing_shift,
404 uint8_t interlacing_leftweight)
7ff85a81 405{
6d6d7970 406 int i;
10024d44
VS
407 if (numsamples <= 0)
408 return;
6d6d7970
MM
409
410 /* weighted interlacing */
411 if (interlacing_leftweight) {
412 for (i = 0; i < numsamples; i++) {
9c8d9f25 413 int32_t a, b;
6d6d7970 414
9c8d9f25
VS
415 a = buffer[0][i];
416 b = buffer[1][i];
6d6d7970 417
9c8d9f25
VS
418 a -= (b * interlacing_leftweight) >> interlacing_shift;
419 b += a;
6d6d7970 420
9c8d9f25
VS
421 buffer_out[i*numchannels] = b;
422 buffer_out[i*numchannels + 1] = a;
6d6d7970
MM
423 }
424
425 return;
426 }
427
428 /* otherwise basic interlacing took place */
429 for (i = 0; i < numsamples; i++) {
430 int16_t left, right;
431
c9128d56
VS
432 left = buffer[0][i];
433 right = buffer[1][i];
6d6d7970 434
6d6d7970
MM
435 buffer_out[i*numchannels] = left;
436 buffer_out[i*numchannels + 1] = right;
437 }
438}
439
f770ee03
MM
440static int alac_decode_frame(AVCodecContext *avctx,
441 void *outbuffer, int *outputsize,
442 uint8_t *inbuffer, int input_buffer_size)
7ff85a81 443{
6d021b00 444 ALACContext *alac = avctx->priv_data;
f770ee03 445
6d6d7970 446 int channels;
7ff85a81 447 int32_t outputsamples;
a562e2e6
VS
448 int hassize;
449 int readsamplesize;
450 int wasted_bytes;
451 int isnotcompressed;
d562ba23
VS
452 uint8_t interlacing_shift;
453 uint8_t interlacing_leftweight;
6d6d7970 454
f770ee03
MM
455 /* short-circuit null buffers */
456 if (!inbuffer || !input_buffer_size)
457 return input_buffer_size;
458
6d6d7970 459 /* initialize from the extradata */
6d021b00
MM
460 if (!alac->context_initialized) {
461 if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
a1db1fc4 462 av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
6d6d7970
MM
463 ALAC_EXTRADATA_SIZE);
464 return input_buffer_size;
465 }
1e25a7e7
MH
466 if (alac_set_info(alac)) {
467 av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
468 return input_buffer_size;
469 }
6d021b00 470 alac->context_initialized = 1;
6d6d7970 471 }
7ff85a81 472
6d021b00 473 init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
6d6d7970 474
e3be5693 475 channels = get_bits(&alac->gb, 3) + 1;
f1752010
VS
476 if (channels > MAX_CHANNELS) {
477 av_log(avctx, AV_LOG_ERROR, "channels > %d not supported\n",
478 MAX_CHANNELS);
479 return input_buffer_size;
480 }
6d6d7970 481
586e5bd9
VS
482 /* 2^result = something to do with output waiting.
483 * perhaps matters if we read > 1 frame in a pass?
484 */
7ae7300e 485 skip_bits(&alac->gb, 4);
6d6d7970 486
7ae7300e 487 skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
6d6d7970 488
586e5bd9 489 /* the output sample size is stored soon */
5fc32c27 490 hassize = get_bits1(&alac->gb);
6d6d7970 491
586e5bd9 492 wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
6d6d7970 493
586e5bd9 494 /* whether the frame is compressed */
5fc32c27 495 isnotcompressed = get_bits1(&alac->gb);
6d6d7970 496
586e5bd9
VS
497 if (hassize) {
498 /* now read the number of samples as a 32bit integer */
499 outputsamples = get_bits(&alac->gb, 32);
500 } else
501 outputsamples = alac->setinfo_max_samples_per_frame;
a562e2e6 502
586e5bd9
VS
503 *outputsize = outputsamples * alac->bytespersample;
504 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + channels - 1;
6d6d7970 505
586e5bd9
VS
506 if (!isnotcompressed) {
507 /* so it is compressed */
508 int16_t predictor_coef_table[channels][32];
509 int predictor_coef_num[channels];
510 int prediction_type[channels];
511 int prediction_quantitization[channels];
512 int ricemodifier[channels];
513 int i, chan;
6d6d7970 514
586e5bd9
VS
515 interlacing_shift = get_bits(&alac->gb, 8);
516 interlacing_leftweight = get_bits(&alac->gb, 8);
6d6d7970 517
586e5bd9 518 for (chan = 0; chan < channels; chan++) {
7f268016
VS
519 prediction_type[chan] = get_bits(&alac->gb, 4);
520 prediction_quantitization[chan] = get_bits(&alac->gb, 4);
6d6d7970 521
7f268016
VS
522 ricemodifier[chan] = get_bits(&alac->gb, 3);
523 predictor_coef_num[chan] = get_bits(&alac->gb, 5);
6d6d7970
MM
524
525 /* read the predictor table */
10024d44 526 for (i = 0; i < predictor_coef_num[chan]; i++)
7f268016 527 predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
586e5bd9 528 }
6d6d7970 529
d58d7ade 530 if (wasted_bytes)
586e5bd9 531 av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
6d6d7970 532
586e5bd9 533 for (chan = 0; chan < channels; chan++) {
6d6d7970 534 bastardized_rice_decompress(alac,
7f268016 535 alac->predicterror_buffer[chan],
6d6d7970
MM
536 outputsamples,
537 readsamplesize,
538 alac->setinfo_rice_initialhistory,
539 alac->setinfo_rice_kmodifier,
7f268016 540 ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
6d6d7970
MM
541 (1 << alac->setinfo_rice_kmodifier) - 1);
542
7f268016 543 if (prediction_type[chan] == 0) {
586e5bd9 544 /* adaptive fir */
7f268016
VS
545 predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
546 alac->outputsamples_buffer[chan],
6d6d7970
MM
547 outputsamples,
548 readsamplesize,
7f268016
VS
549 predictor_coef_table[chan],
550 predictor_coef_num[chan],
551 prediction_quantitization[chan]);
6d6d7970 552 } else {
7f268016 553 av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
60c4a31c
VS
554 /* I think the only other prediction type (or perhaps this is
555 * just a boolean?) runs adaptive fir twice.. like:
10fb5763
VS
556 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
557 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
558 * little strange..
559 */
6d6d7970 560 }
586e5bd9
VS
561 }
562 } else {
563 /* not compressed, easy case */
564 if (alac->setinfo_sample_size <= 16) {
565 int i, chan;
60c4a31c 566 for (chan = 0; chan < channels; chan++)
6d6d7970 567 for (i = 0; i < outputsamples; i++) {
7f268016 568 int32_t audiobits;
6d6d7970 569
7f268016 570 audiobits = get_bits(&alac->gb, alac->setinfo_sample_size);
aa33d809 571 audiobits = extend_sign32(audiobits, readsamplesize);
10fb5763 572
7f268016 573 alac->outputsamples_buffer[chan][i] = audiobits;
6d6d7970 574 }
586e5bd9
VS
575 } else {
576 int i, chan;
60c4a31c 577 for (chan = 0; chan < channels; chan++)
6d6d7970 578 for (i = 0; i < outputsamples; i++) {
7f268016 579 int32_t audiobits;
6d6d7970 580
7f268016 581 audiobits = get_bits(&alac->gb, 16);
10fb5763
VS
582 /* special case of sign extension..
583 * as we'll be ORing the low 16bits into this */
7f268016
VS
584 audiobits = audiobits << 16;
585 audiobits = audiobits >> (32 - alac->setinfo_sample_size);
586 audiobits |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
6d6d7970 587
7f268016 588 alac->outputsamples_buffer[chan][i] = audiobits;
6d6d7970 589 }
6d6d7970 590 }
586e5bd9
VS
591 /* wasted_bytes = 0; */
592 interlacing_shift = 0;
593 interlacing_leftweight = 0;
594 }
6d6d7970 595
586e5bd9 596 switch(alac->setinfo_sample_size) {
d58d7ade 597 case 16:
586e5bd9 598 if (channels == 2) {
4a5e6389 599 reconstruct_stereo_16(alac->outputsamples_buffer,
92df8910
VS
600 (int16_t*)outbuffer,
601 alac->numchannels,
602 outputsamples,
603 interlacing_shift,
604 interlacing_leftweight);
586e5bd9
VS
605 } else {
606 int i;
607 for (i = 0; i < outputsamples; i++) {
608 int16_t sample = alac->outputsamples_buffer[0][i];
609 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
610 }
6d6d7970 611 }
586e5bd9 612 break;
586e5bd9
VS
613 case 20:
614 case 24:
63ecfc1f
VS
615 // It is not clear if there exist any encoder that creates 24 bit ALAC
616 // files. iTunes convert 24 bit raw files to 16 bit before encoding.
586e5bd9
VS
617 case 32:
618 av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
619 break;
620 default:
621 break;
622 }
6d6d7970 623
f770ee03 624 return input_buffer_size;
6d6d7970
MM
625}
626
627static int alac_decode_init(AVCodecContext * avctx)
628{
6d021b00
MM
629 ALACContext *alac = avctx->priv_data;
630 alac->avctx = avctx;
631 alac->context_initialized = 0;
6d6d7970 632
6d021b00
MM
633 alac->samplesize = alac->avctx->bits_per_sample;
634 alac->numchannels = alac->avctx->channels;
635 alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
6d6d7970
MM
636
637 return 0;
638}
639
6d6d7970
MM
640static int alac_decode_close(AVCodecContext *avctx)
641{
6d021b00 642 ALACContext *alac = avctx->priv_data;
6d6d7970 643
153696a6
VS
644 int chan;
645 for (chan = 0; chan < MAX_CHANNELS; chan++) {
646 av_free(alac->predicterror_buffer[chan]);
647 av_free(alac->outputsamples_buffer[chan]);
648 }
6d6d7970
MM
649
650 return 0;
651}
652
653AVCodec alac_decoder = {
654 "alac",
655 CODEC_TYPE_AUDIO,
656 CODEC_ID_ALAC,
657 sizeof(ALACContext),
658 alac_decode_init,
659 NULL,
660 alac_decode_close,
661 alac_decode_frame,
662};