alacdec: implement the 2-pass prediction type.
[libav.git] / libavcodec / alac.c
CommitLineData
6d6d7970
MM
1/*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
6d6d7970 4 *
2912e87a 5 * This file is part of Libav.
b78e7197 6 *
2912e87a 7 * Libav 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 *
2912e87a 12 * Libav 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
2912e87a 18 * License along with Libav; 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
ad4cd0c2 26 * @see http://crazney.net/programs/itunes/alac.html
6d6d7970 27 *
e76c7b85 28 * Note: This decoder expects a 36-byte QuickTime atom to be
6d6d7970
MM
29 * passed through the extradata[_size] fields. This atom is tacked onto
30 * the end of an 'alac' stsd atom and has the following format:
a1db1fc4 31 *
e76c7b85
JR
32 * 32bit atom size
33 * 32bit tag ("alac")
34 * 32bit tag version (0)
35 * 32bit samples per frame (used when not set explicitly in the frames)
36 * 8bit compatible version (0)
a1db1fc4 37 * 8bit sample size
e76c7b85
JR
38 * 8bit history mult (40)
39 * 8bit initial history (14)
40 * 8bit kmodifier (10)
41 * 8bit channels
42 * 16bit maxRun (255)
43 * 32bit max coded frame size (0 means unknown)
44 * 32bit average bitrate (0 means unknown)
a1db1fc4 45 * 32bit samplerate
6d6d7970
MM
46 */
47
48
49#include "avcodec.h"
9106a698 50#include "get_bits.h"
f79488d4 51#include "bytestream.h"
becc0ef9 52#include "unary.h"
8858990f 53#include "mathops.h"
6d6d7970
MM
54
55#define ALAC_EXTRADATA_SIZE 36
153696a6 56#define MAX_CHANNELS 2
6d6d7970 57
6d021b00
MM
58typedef struct {
59
60 AVCodecContext *avctx;
0eea2129 61 AVFrame frame;
6d021b00 62 GetBitContext gb;
6d6d7970 63
6d6d7970 64 int numchannels;
6d6d7970 65
6d6d7970 66 /* buffers */
153696a6 67 int32_t *predicterror_buffer[MAX_CHANNELS];
6d6d7970 68
153696a6 69 int32_t *outputsamples_buffer[MAX_CHANNELS];
6d6d7970 70
d251c85d 71 int32_t *extra_bits_buffer[MAX_CHANNELS];
f430c7b6 72
6d6d7970
MM
73 /* stuff from setinfo */
74 uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
6d6d7970
MM
75 uint8_t setinfo_sample_size; /* 0x10 */
76 uint8_t setinfo_rice_historymult; /* 0x28 */
77 uint8_t setinfo_rice_initialhistory; /* 0x0a */
78 uint8_t setinfo_rice_kmodifier; /* 0x0e */
6d6d7970 79 /* end setinfo stuff */
6d6d7970 80
d251c85d 81 int extra_bits; /**< number of extra bits beyond 16-bit */
6d6d7970
MM
82} ALACContext;
83
5eeba07e
MN
84static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
85 /* read x - number of 1s before 0 represent the rice */
86 int x = get_unary_0_9(gb);
87
88 if (x > 8) { /* RICE THRESHOLD */
89 /* use alternative encoding */
90 x = get_bits(gb, readsamplesize);
91 } else {
1f655c4f
MN
92 if (k >= limit)
93 k = limit;
5a5a27c5 94
1f655c4f
MN
95 if (k != 1) {
96 int extrabits = show_bits(gb, k);
5a5a27c5 97
1f655c4f
MN
98 /* multiply x by 2^k - 1, as part of their strange algorithm */
99 x = (x << k) - x;
5a5a27c5 100
1f655c4f
MN
101 if (extrabits > 1) {
102 x += extrabits - 1;
103 skip_bits(gb, k);
104 } else
105 skip_bits(gb, k - 1);
106 }
5eeba07e 107 }
5a5a27c5
MN
108 return x;
109}
110
1b47fafd 111static void bastardized_rice_decompress(ALACContext *alac,
6d6d7970
MM
112 int32_t *output_buffer,
113 int output_size,
114 int readsamplesize, /* arg_10 */
115 int rice_initialhistory, /* arg424->b */
116 int rice_kmodifier, /* arg424->d */
117 int rice_historymult, /* arg424->c */
118 int rice_kmodifier_mask /* arg424->e */
119 )
120{
121 int output_count;
122 unsigned int history = rice_initialhistory;
123 int sign_modifier = 0;
124
125 for (output_count = 0; output_count < output_size; output_count++) {
becc0ef9 126 int32_t x;
6d6d7970
MM
127 int32_t x_modified;
128 int32_t final_val;
129
1f655c4f
MN
130 /* standard rice encoding */
131 int k; /* size of extra bits */
6d6d7970 132
1f655c4f 133 /* read k, that is bits as is */
8431603a 134 k = av_log2((history >> 9) + 3);
1f655c4f 135 x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
6d6d7970
MM
136
137 x_modified = sign_modifier + x;
138 final_val = (x_modified + 1) / 2;
139 if (x_modified & 1) final_val *= -1;
140
141 output_buffer[output_count] = final_val;
142
143 sign_modifier = 0;
144
145 /* now update the history */
10024d44
VS
146 history += x_modified * rice_historymult
147 - ((history * rice_historymult) >> 9);
6d6d7970
MM
148
149 if (x_modified > 0xffff)
150 history = 0xffff;
151
152 /* special case: there may be compressed blocks of 0 */
153 if ((history < 128) && (output_count+1 < output_size)) {
83e9a67d
MN
154 int k;
155 unsigned int block_size;
6d6d7970
MM
156
157 sign_modifier = 1;
158
8431603a 159 k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
6d6d7970 160
1f655c4f 161 block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
6d6d7970
MM
162
163 if (block_size > 0) {
83e9a67d
MN
164 if(block_size >= output_size - output_count){
165 av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
166 block_size= output_size - output_count - 1;
167 }
6d6d7970
MM
168 memset(&output_buffer[output_count+1], 0, block_size * 4);
169 output_count += block_size;
6d6d7970
MM
170 }
171
172 if (block_size > 0xffff)
173 sign_modifier = 0;
174
175 history = 0;
176 }
177 }
178}
179
321c3138
VS
180static inline int sign_only(int v)
181{
182 return v ? FFSIGN(v) : 0;
183}
6d6d7970
MM
184
185static void predictor_decompress_fir_adapt(int32_t *error_buffer,
186 int32_t *buffer_out,
187 int output_size,
188 int readsamplesize,
189 int16_t *predictor_coef_table,
190 int predictor_coef_num,
191 int predictor_quantitization)
192{
193 int i;
194
195 /* first sample always copies */
196 *buffer_out = *error_buffer;
197
198 if (!predictor_coef_num) {
10024d44
VS
199 if (output_size <= 1)
200 return;
201
6d6d7970
MM
202 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
203 return;
204 }
205
206 if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
207 /* second-best case scenario for fir decompression,
208 * error describes a small difference from the previous sample only
209 */
10024d44
VS
210 if (output_size <= 1)
211 return;
6d6d7970
MM
212 for (i = 0; i < output_size - 1; i++) {
213 int32_t prev_value;
214 int32_t error_value;
215
216 prev_value = buffer_out[i];
217 error_value = error_buffer[i+1];
10024d44 218 buffer_out[i+1] =
8858990f 219 sign_extend((prev_value + error_value), readsamplesize);
6d6d7970
MM
220 }
221 return;
222 }
223
224 /* read warm-up samples */
10024d44 225 if (predictor_coef_num > 0)
6d6d7970
MM
226 for (i = 0; i < predictor_coef_num; i++) {
227 int32_t val;
228
229 val = buffer_out[i] + error_buffer[i+1];
8858990f 230 val = sign_extend(val, readsamplesize);
6d6d7970
MM
231 buffer_out[i+1] = val;
232 }
6d6d7970 233
6d6d7970 234 /* 4 and 8 are very common cases (the only ones i've seen). these
52b541ad 235 * should be unrolled and optimized
6d6d7970 236 */
6d6d7970 237
6d6d7970
MM
238 /* general case */
239 if (predictor_coef_num > 0) {
10024d44 240 for (i = predictor_coef_num + 1; i < output_size; i++) {
6d6d7970
MM
241 int j;
242 int sum = 0;
243 int outval;
244 int error_val = error_buffer[i];
245
246 for (j = 0; j < predictor_coef_num; j++) {
247 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
248 predictor_coef_table[j];
249 }
250
251 outval = (1 << (predictor_quantitization-1)) + sum;
252 outval = outval >> predictor_quantitization;
253 outval = outval + buffer_out[0] + error_val;
8858990f 254 outval = sign_extend(outval, readsamplesize);
6d6d7970
MM
255
256 buffer_out[predictor_coef_num+1] = outval;
257
258 if (error_val > 0) {
259 int predictor_num = predictor_coef_num - 1;
260
261 while (predictor_num >= 0 && error_val > 0) {
262 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138 263 int sign = sign_only(val);
6d6d7970
MM
264
265 predictor_coef_table[predictor_num] -= sign;
266
267 val *= sign; /* absolute value */
268
269 error_val -= ((val >> predictor_quantitization) *
270 (predictor_coef_num - predictor_num));
271
272 predictor_num--;
273 }
274 } else if (error_val < 0) {
275 int predictor_num = predictor_coef_num - 1;
276
277 while (predictor_num >= 0 && error_val < 0) {
278 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138 279 int sign = - sign_only(val);
6d6d7970
MM
280
281 predictor_coef_table[predictor_num] -= sign;
282
283 val *= sign; /* neg value */
284
285 error_val -= ((val >> predictor_quantitization) *
286 (predictor_coef_num - predictor_num));
287
288 predictor_num--;
289 }
290 }
291
292 buffer_out++;
293 }
294 }
295}
296
e739d351 297static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS],
7080533c 298 int numsamples, uint8_t interlacing_shift,
e739d351 299 uint8_t interlacing_leftweight)
7ff85a81 300{
6d6d7970 301 int i;
6d6d7970 302
e739d351
JR
303 for (i = 0; i < numsamples; i++) {
304 int32_t a, b;
6d6d7970 305
e739d351
JR
306 a = buffer[0][i];
307 b = buffer[1][i];
6d6d7970 308
e739d351
JR
309 a -= (b * interlacing_leftweight) >> interlacing_shift;
310 b += a;
6d6d7970 311
e739d351
JR
312 buffer[0][i] = b;
313 buffer[1][i] = a;
6d6d7970 314 }
e739d351
JR
315}
316
c39bddd3
JR
317static void append_extra_bits(int32_t *buffer[MAX_CHANNELS],
318 int32_t *extra_bits_buffer[MAX_CHANNELS],
319 int extra_bits, int numchannels, int numsamples)
320{
321 int i, ch;
322
323 for (ch = 0; ch < numchannels; ch++)
324 for (i = 0; i < numsamples; i++)
325 buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
326}
327
cb50329f 328static void interleave_stereo_16(int32_t *buffer[MAX_CHANNELS],
7080533c 329 int16_t *buffer_out, int numsamples)
e739d351
JR
330{
331 int i;
6d6d7970 332
6d6d7970 333 for (i = 0; i < numsamples; i++) {
7080533c
JR
334 *buffer_out++ = buffer[0][i];
335 *buffer_out++ = buffer[1][i];
6d6d7970
MM
336 }
337}
338
cb50329f 339static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS],
7080533c 340 int32_t *buffer_out, int numsamples)
f430c7b6
JM
341{
342 int i;
343
7080533c
JR
344 for (i = 0; i < numsamples; i++) {
345 *buffer_out++ = buffer[0][i] << 8;
346 *buffer_out++ = buffer[1][i] << 8;
347 }
f430c7b6
JM
348}
349
0eea2129
JR
350static int alac_decode_frame(AVCodecContext *avctx, void *data,
351 int *got_frame_ptr, AVPacket *avpkt)
7ff85a81 352{
7a00bbad
TB
353 const uint8_t *inbuffer = avpkt->data;
354 int input_buffer_size = avpkt->size;
6d021b00 355 ALACContext *alac = avctx->priv_data;
f770ee03 356
6d6d7970 357 int channels;
494e3531 358 unsigned int outputsamples;
a562e2e6 359 int hassize;
95801b6a 360 unsigned int readsamplesize;
a562e2e6 361 int isnotcompressed;
d562ba23
VS
362 uint8_t interlacing_shift;
363 uint8_t interlacing_leftweight;
0eea2129 364 int i, ch, ret;
6d6d7970 365
6d021b00 366 init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
6d6d7970 367
e3be5693 368 channels = get_bits(&alac->gb, 3) + 1;
dcaa83a0
JR
369 if (channels != avctx->channels) {
370 av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\n");
371 return AVERROR_INVALIDDATA;
f1752010 372 }
6d6d7970 373
586e5bd9
VS
374 /* 2^result = something to do with output waiting.
375 * perhaps matters if we read > 1 frame in a pass?
376 */
7ae7300e 377 skip_bits(&alac->gb, 4);
6d6d7970 378
7ae7300e 379 skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
6d6d7970 380
586e5bd9 381 /* the output sample size is stored soon */
5fc32c27 382 hassize = get_bits1(&alac->gb);
6d6d7970 383
d251c85d 384 alac->extra_bits = get_bits(&alac->gb, 2) << 3;
6d6d7970 385
586e5bd9 386 /* whether the frame is compressed */
5fc32c27 387 isnotcompressed = get_bits1(&alac->gb);
6d6d7970 388
586e5bd9
VS
389 if (hassize) {
390 /* now read the number of samples as a 32bit integer */
59d598b9 391 outputsamples = get_bits_long(&alac->gb, 32);
494e3531
MN
392 if(outputsamples > alac->setinfo_max_samples_per_frame){
393 av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
394 return -1;
395 }
586e5bd9
VS
396 } else
397 outputsamples = alac->setinfo_max_samples_per_frame;
a562e2e6 398
0eea2129
JR
399 /* get output buffer */
400 if (outputsamples > INT32_MAX) {
401 av_log(avctx, AV_LOG_ERROR, "unsupported block size: %u\n", outputsamples);
402 return AVERROR_INVALIDDATA;
403 }
404 alac->frame.nb_samples = outputsamples;
405 if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {
406 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
407 return ret;
f7739f37
MN
408 }
409
d251c85d 410 readsamplesize = alac->setinfo_sample_size - alac->extra_bits + channels - 1;
95801b6a
MC
411 if (readsamplesize > MIN_CACHE_BITS) {
412 av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
413 return -1;
414 }
6d6d7970 415
586e5bd9
VS
416 if (!isnotcompressed) {
417 /* so it is compressed */
986f143a
MR
418 int16_t predictor_coef_table[MAX_CHANNELS][32];
419 int predictor_coef_num[MAX_CHANNELS];
420 int prediction_type[MAX_CHANNELS];
421 int prediction_quantitization[MAX_CHANNELS];
422 int ricemodifier[MAX_CHANNELS];
6d6d7970 423
586e5bd9
VS
424 interlacing_shift = get_bits(&alac->gb, 8);
425 interlacing_leftweight = get_bits(&alac->gb, 8);
6d6d7970 426
01200f12
JR
427 for (ch = 0; ch < channels; ch++) {
428 prediction_type[ch] = get_bits(&alac->gb, 4);
429 prediction_quantitization[ch] = get_bits(&alac->gb, 4);
6d6d7970 430
01200f12
JR
431 ricemodifier[ch] = get_bits(&alac->gb, 3);
432 predictor_coef_num[ch] = get_bits(&alac->gb, 5);
6d6d7970
MM
433
434 /* read the predictor table */
01200f12
JR
435 for (i = 0; i < predictor_coef_num[ch]; i++)
436 predictor_coef_table[ch][i] = (int16_t)get_bits(&alac->gb, 16);
586e5bd9 437 }
6d6d7970 438
d251c85d 439 if (alac->extra_bits) {
f430c7b6
JM
440 for (i = 0; i < outputsamples; i++) {
441 for (ch = 0; ch < channels; ch++)
d251c85d 442 alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
f430c7b6
JM
443 }
444 }
01200f12 445 for (ch = 0; ch < channels; ch++) {
6d6d7970 446 bastardized_rice_decompress(alac,
01200f12 447 alac->predicterror_buffer[ch],
6d6d7970
MM
448 outputsamples,
449 readsamplesize,
450 alac->setinfo_rice_initialhistory,
451 alac->setinfo_rice_kmodifier,
01200f12 452 ricemodifier[ch] * alac->setinfo_rice_historymult / 4,
6d6d7970
MM
453 (1 << alac->setinfo_rice_kmodifier) - 1);
454
e49d2130
JR
455 /* adaptive FIR filter */
456 if (prediction_type[ch] == 15) {
457 /* Prediction type 15 runs the adaptive FIR twice.
458 * The first pass uses the special-case coef_num = 31, while
459 * the second pass uses the coefs from the bitstream.
460 *
461 * However, this prediction type is not currently used by the
462 * reference encoder.
10fb5763 463 */
e49d2130
JR
464 predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
465 alac->predicterror_buffer[ch],
466 outputsamples, readsamplesize,
467 NULL, 31, 0);
468 } else if (prediction_type[ch] > 0) {
469 av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
470 prediction_type[ch]);
6d6d7970 471 }
e49d2130
JR
472 predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
473 alac->outputsamples_buffer[ch],
474 outputsamples, readsamplesize,
475 predictor_coef_table[ch],
476 predictor_coef_num[ch],
477 prediction_quantitization[ch]);
586e5bd9
VS
478 }
479 } else {
480 /* not compressed, easy case */
c3a92412 481 for (i = 0; i < outputsamples; i++) {
01200f12
JR
482 for (ch = 0; ch < channels; ch++) {
483 alac->outputsamples_buffer[ch][i] = get_sbits_long(&alac->gb,
484 alac->setinfo_sample_size);
6827f6f2 485 }
f430c7b6 486 }
d251c85d 487 alac->extra_bits = 0;
586e5bd9
VS
488 interlacing_shift = 0;
489 interlacing_leftweight = 0;
490 }
e5ab7379
MC
491 if (get_bits(&alac->gb, 3) != 7)
492 av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
6d6d7970 493
e739d351 494 if (channels == 2 && interlacing_leftweight) {
7080533c
JR
495 decorrelate_stereo(alac->outputsamples_buffer, outputsamples,
496 interlacing_shift, interlacing_leftweight);
e739d351
JR
497 }
498
c39bddd3
JR
499 if (alac->extra_bits) {
500 append_extra_bits(alac->outputsamples_buffer, alac->extra_bits_buffer,
501 alac->extra_bits, alac->numchannels, outputsamples);
502 }
503
586e5bd9 504 switch(alac->setinfo_sample_size) {
d58d7ade 505 case 16:
586e5bd9 506 if (channels == 2) {
0eea2129
JR
507 interleave_stereo_16(alac->outputsamples_buffer,
508 (int16_t *)alac->frame.data[0], outputsamples);
586e5bd9 509 } else {
0eea2129 510 int16_t *outbuffer = (int16_t *)alac->frame.data[0];
586e5bd9 511 for (i = 0; i < outputsamples; i++) {
0eea2129 512 outbuffer[i] = alac->outputsamples_buffer[0][i];
586e5bd9 513 }
6d6d7970 514 }
586e5bd9 515 break;
586e5bd9 516 case 24:
f430c7b6 517 if (channels == 2) {
0eea2129
JR
518 interleave_stereo_24(alac->outputsamples_buffer,
519 (int32_t *)alac->frame.data[0], outputsamples);
f430c7b6 520 } else {
0eea2129 521 int32_t *outbuffer = (int32_t *)alac->frame.data[0];
f430c7b6 522 for (i = 0; i < outputsamples; i++)
0eea2129 523 outbuffer[i] = alac->outputsamples_buffer[0][i] << 8;
f430c7b6 524 }
586e5bd9
VS
525 break;
526 }
6d6d7970 527
e5ab7379
MC
528 if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
529 av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
530
0eea2129
JR
531 *got_frame_ptr = 1;
532 *(AVFrame *)data = alac->frame;
533
f770ee03 534 return input_buffer_size;
6d6d7970
MM
535}
536
53df079a
JR
537static av_cold int alac_decode_close(AVCodecContext *avctx)
538{
539 ALACContext *alac = avctx->priv_data;
540
63cf54df
JR
541 int ch;
542 for (ch = 0; ch < alac->numchannels; ch++) {
543 av_freep(&alac->predicterror_buffer[ch]);
544 av_freep(&alac->outputsamples_buffer[ch]);
545 av_freep(&alac->extra_bits_buffer[ch]);
53df079a
JR
546 }
547
548 return 0;
549}
550
551static int allocate_buffers(ALACContext *alac)
552{
63cf54df
JR
553 int ch;
554 for (ch = 0; ch < alac->numchannels; ch++) {
68f7e9cd 555 int buf_size = alac->setinfo_max_samples_per_frame * sizeof(int32_t);
53df079a 556
68f7e9cd
JR
557 FF_ALLOC_OR_GOTO(alac->avctx, alac->predicterror_buffer[ch],
558 buf_size, buf_alloc_fail);
53df079a 559
68f7e9cd
JR
560 FF_ALLOC_OR_GOTO(alac->avctx, alac->outputsamples_buffer[ch],
561 buf_size, buf_alloc_fail);
53df079a 562
68f7e9cd
JR
563 FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
564 buf_size, buf_alloc_fail);
53df079a
JR
565 }
566 return 0;
68f7e9cd
JR
567buf_alloc_fail:
568 alac_decode_close(alac->avctx);
569 return AVERROR(ENOMEM);
53df079a
JR
570}
571
572static int alac_set_info(ALACContext *alac)
573{
574 const unsigned char *ptr = alac->avctx->extradata;
575
576 ptr += 4; /* size */
577 ptr += 4; /* alac */
e76c7b85 578 ptr += 4; /* version */
53df079a
JR
579
580 if(AV_RB32(ptr) >= UINT_MAX/4){
581 av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
582 return -1;
583 }
584
585 /* buffer size / 2 ? */
586 alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
e76c7b85 587 ptr++; /* compatible version */
53df079a
JR
588 alac->setinfo_sample_size = *ptr++;
589 alac->setinfo_rice_historymult = *ptr++;
590 alac->setinfo_rice_initialhistory = *ptr++;
591 alac->setinfo_rice_kmodifier = *ptr++;
592 alac->numchannels = *ptr++;
e76c7b85 593 bytestream_get_be16(&ptr); /* maxRun */
53df079a 594 bytestream_get_be32(&ptr); /* max coded frame size */
e76c7b85 595 bytestream_get_be32(&ptr); /* average bitrate */
53df079a
JR
596 bytestream_get_be32(&ptr); /* samplerate */
597
598 return 0;
599}
600
98a6fff9 601static av_cold int alac_decode_init(AVCodecContext * avctx)
6d6d7970 602{
53df079a 603 int ret;
6d021b00
MM
604 ALACContext *alac = avctx->priv_data;
605 alac->avctx = avctx;
6d6d7970 606
313b52fb
JGG
607 /* initialize from the extradata */
608 if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
609 av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
610 ALAC_EXTRADATA_SIZE);
611 return -1;
612 }
613 if (alac_set_info(alac)) {
614 av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
615 return -1;
616 }
617
0f26f3d5
JR
618 switch (alac->setinfo_sample_size) {
619 case 16: avctx->sample_fmt = AV_SAMPLE_FMT_S16;
620 break;
621 case 24: avctx->sample_fmt = AV_SAMPLE_FMT_S32;
622 break;
b316af7a
JR
623 default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
624 alac->setinfo_sample_size);
625 return AVERROR_PATCHWELCOME;
0f26f3d5
JR
626 }
627
dcaa83a0
JR
628 if (alac->numchannels < 1) {
629 av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
630 alac->numchannels = avctx->channels;
631 } else {
632 if (alac->numchannels > MAX_CHANNELS)
633 alac->numchannels = avctx->channels;
634 else
635 avctx->channels = alac->numchannels;
636 }
637 if (avctx->channels > MAX_CHANNELS) {
638 av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
639 avctx->channels);
640 return AVERROR_PATCHWELCOME;
641 }
642
53df079a
JR
643 if ((ret = allocate_buffers(alac)) < 0) {
644 av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
645 return ret;
153696a6 646 }
6d6d7970 647
0eea2129
JR
648 avcodec_get_frame_defaults(&alac->frame);
649 avctx->coded_frame = &alac->frame;
650
6d6d7970
MM
651 return 0;
652}
653
d36beb3f 654AVCodec ff_alac_decoder = {
ec6402b7
AK
655 .name = "alac",
656 .type = AVMEDIA_TYPE_AUDIO,
657 .id = CODEC_ID_ALAC,
658 .priv_data_size = sizeof(ALACContext),
659 .init = alac_decode_init,
660 .close = alac_decode_close,
661 .decode = alac_decode_frame,
0eea2129 662 .capabilities = CODEC_CAP_DR1,
fe4bf374 663 .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
6d6d7970 664};