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