1 | /*
|
---|
2 | * ALAC (Apple Lossless Audio Codec) decoder
|
---|
3 | * Copyright (c) 2005 David Hammerton
|
---|
4 | * All rights reserved.
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | */
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * @file alac.c
|
---|
23 | * ALAC (Apple Lossless Audio Codec) decoder
|
---|
24 | * @author 2005 David Hammerton
|
---|
25 | *
|
---|
26 | * For more information on the ALAC format, visit:
|
---|
27 | * http://crazney.net/programs/itunes/alac.html
|
---|
28 | *
|
---|
29 | * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
|
---|
30 | * passed through the extradata[_size] fields. This atom is tacked onto
|
---|
31 | * the end of an 'alac' stsd atom and has the following format:
|
---|
32 | * bytes 0-3 atom size (0x24), big-endian
|
---|
33 | * bytes 4-7 atom type ('alac', not the 'alac' tag from start of stsd)
|
---|
34 | * bytes 8-35 data bytes needed by decoder
|
---|
35 | *
|
---|
36 | * Extradata:
|
---|
37 | * 32bit size
|
---|
38 | * 32bit tag (=alac)
|
---|
39 | * 32bit zero?
|
---|
40 | * 32bit max sample per frame
|
---|
41 | * 8bit ?? (zero?)
|
---|
42 | * 8bit sample size
|
---|
43 | * 8bit history mult
|
---|
44 | * 8bit initial history
|
---|
45 | * 8bit kmodifier
|
---|
46 | * 8bit channels?
|
---|
47 | * 16bit ??
|
---|
48 | * 32bit max coded frame size
|
---|
49 | * 32bit bitrate?
|
---|
50 | * 32bit samplerate
|
---|
51 | */
|
---|
52 |
|
---|
53 |
|
---|
54 | #include "avcodec.h"
|
---|
55 | #include "bitstream.h"
|
---|
56 |
|
---|
57 | #define ALAC_EXTRADATA_SIZE 36
|
---|
58 |
|
---|
59 | typedef struct {
|
---|
60 |
|
---|
61 | AVCodecContext *avctx;
|
---|
62 | GetBitContext gb;
|
---|
63 | /* init to 0; first frame decode should initialize from extradata and
|
---|
64 | * set this to 1 */
|
---|
65 | int context_initialized;
|
---|
66 |
|
---|
67 | int samplesize;
|
---|
68 | int numchannels;
|
---|
69 | int bytespersample;
|
---|
70 |
|
---|
71 | /* buffers */
|
---|
72 | int32_t *predicterror_buffer_a;
|
---|
73 | int32_t *predicterror_buffer_b;
|
---|
74 |
|
---|
75 | int32_t *outputsamples_buffer_a;
|
---|
76 | int32_t *outputsamples_buffer_b;
|
---|
77 |
|
---|
78 | /* stuff from setinfo */
|
---|
79 | uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
|
---|
80 | uint8_t setinfo_7a; /* 0x00 */
|
---|
81 | uint8_t setinfo_sample_size; /* 0x10 */
|
---|
82 | uint8_t setinfo_rice_historymult; /* 0x28 */
|
---|
83 | uint8_t setinfo_rice_initialhistory; /* 0x0a */
|
---|
84 | uint8_t setinfo_rice_kmodifier; /* 0x0e */
|
---|
85 | uint8_t setinfo_7f; /* 0x02 */
|
---|
86 | uint16_t setinfo_80; /* 0x00ff */
|
---|
87 | uint32_t setinfo_82; /* 0x000020e7 */
|
---|
88 | uint32_t setinfo_86; /* 0x00069fe4 */
|
---|
89 | uint32_t setinfo_8a_rate; /* 0x0000ac44 */
|
---|
90 | /* end setinfo stuff */
|
---|
91 |
|
---|
92 | } ALACContext;
|
---|
93 |
|
---|
94 | static void allocate_buffers(ALACContext *alac)
|
---|
95 | {
|
---|
96 | alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
|
---|
97 | alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
|
---|
98 |
|
---|
99 | alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
|
---|
100 | alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
|
---|
101 | }
|
---|
102 |
|
---|
103 | static int alac_set_info(ALACContext *alac)
|
---|
104 | {
|
---|
105 | unsigned char *ptr = alac->avctx->extradata;
|
---|
106 |
|
---|
107 | ptr += 4; /* size */
|
---|
108 | ptr += 4; /* alac */
|
---|
109 | ptr += 4; /* 0 ? */
|
---|
110 |
|
---|
111 | if(BE_32(ptr) >= UINT_MAX/4){
|
---|
112 | av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
|
---|
113 | return -1;
|
---|
114 | }
|
---|
115 | alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
|
---|
116 | ptr += 4;
|
---|
117 | alac->setinfo_7a = *ptr++;
|
---|
118 | alac->setinfo_sample_size = *ptr++;
|
---|
119 | alac->setinfo_rice_historymult = *ptr++;
|
---|
120 | alac->setinfo_rice_initialhistory = *ptr++;
|
---|
121 | alac->setinfo_rice_kmodifier = *ptr++;
|
---|
122 | alac->setinfo_7f = *ptr++; // channels?
|
---|
123 | alac->setinfo_80 = BE_16(ptr);
|
---|
124 | ptr += 2;
|
---|
125 | alac->setinfo_82 = BE_32(ptr); // max coded frame size
|
---|
126 | ptr += 4;
|
---|
127 | alac->setinfo_86 = BE_32(ptr); // bitrate ?
|
---|
128 | ptr += 4;
|
---|
129 | alac->setinfo_8a_rate = BE_32(ptr); // samplerate
|
---|
130 | ptr += 4;
|
---|
131 |
|
---|
132 | allocate_buffers(alac);
|
---|
133 |
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /* hideously inefficient. could use a bitmask search,
|
---|
138 | * alternatively bsr on x86,
|
---|
139 | */
|
---|
140 | static int count_leading_zeros(int32_t input)
|
---|
141 | {
|
---|
142 | int i = 0;
|
---|
143 | while (!(0x80000000 & input) && i < 32) {
|
---|
144 | i++;
|
---|
145 | input = input << 1;
|
---|
146 | }
|
---|
147 | return i;
|
---|
148 | }
|
---|
149 |
|
---|
150 | static void bastardized_rice_decompress(ALACContext *alac,
|
---|
151 | int32_t *output_buffer,
|
---|
152 | int output_size,
|
---|
153 | int readsamplesize, /* arg_10 */
|
---|
154 | int rice_initialhistory, /* arg424->b */
|
---|
155 | int rice_kmodifier, /* arg424->d */
|
---|
156 | int rice_historymult, /* arg424->c */
|
---|
157 | int rice_kmodifier_mask /* arg424->e */
|
---|
158 | )
|
---|
159 | {
|
---|
160 | int output_count;
|
---|
161 | unsigned int history = rice_initialhistory;
|
---|
162 | int sign_modifier = 0;
|
---|
163 |
|
---|
164 | for (output_count = 0; output_count < output_size; output_count++) {
|
---|
165 | int32_t x = 0;
|
---|
166 | int32_t x_modified;
|
---|
167 | int32_t final_val;
|
---|
168 |
|
---|
169 | /* read x - number of 1s before 0 represent the rice */
|
---|
170 | while (x <= 8 && get_bits1(&alac->gb)) {
|
---|
171 | x++;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | if (x > 8) { /* RICE THRESHOLD */
|
---|
176 | /* use alternative encoding */
|
---|
177 | int32_t value;
|
---|
178 |
|
---|
179 | value = get_bits(&alac->gb, readsamplesize);
|
---|
180 |
|
---|
181 | /* mask value to readsamplesize size */
|
---|
182 | if (readsamplesize != 32)
|
---|
183 | value &= (0xffffffff >> (32 - readsamplesize));
|
---|
184 |
|
---|
185 | x = value;
|
---|
186 | } else {
|
---|
187 | /* standard rice encoding */
|
---|
188 | int extrabits;
|
---|
189 | int k; /* size of extra bits */
|
---|
190 |
|
---|
191 | /* read k, that is bits as is */
|
---|
192 | k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
|
---|
193 |
|
---|
194 | if (k < 0)
|
---|
195 | k += rice_kmodifier;
|
---|
196 | else
|
---|
197 | k = rice_kmodifier;
|
---|
198 |
|
---|
199 | if (k != 1) {
|
---|
200 | extrabits = show_bits(&alac->gb, k);
|
---|
201 |
|
---|
202 | /* multiply x by 2^k - 1, as part of their strange algorithm */
|
---|
203 | x = (x << k) - x;
|
---|
204 |
|
---|
205 | if (extrabits > 1) {
|
---|
206 | x += extrabits - 1;
|
---|
207 | get_bits(&alac->gb, k);
|
---|
208 | } else {
|
---|
209 | get_bits(&alac->gb, k - 1);
|
---|
210 | }
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 | x_modified = sign_modifier + x;
|
---|
215 | final_val = (x_modified + 1) / 2;
|
---|
216 | if (x_modified & 1) final_val *= -1;
|
---|
217 |
|
---|
218 | output_buffer[output_count] = final_val;
|
---|
219 |
|
---|
220 | sign_modifier = 0;
|
---|
221 |
|
---|
222 | /* now update the history */
|
---|
223 | history += (x_modified * rice_historymult)
|
---|
224 | - ((history * rice_historymult) >> 9);
|
---|
225 |
|
---|
226 | if (x_modified > 0xffff)
|
---|
227 | history = 0xffff;
|
---|
228 |
|
---|
229 | /* special case: there may be compressed blocks of 0 */
|
---|
230 | if ((history < 128) && (output_count+1 < output_size)) {
|
---|
231 | int block_size;
|
---|
232 |
|
---|
233 | sign_modifier = 1;
|
---|
234 |
|
---|
235 | x = 0;
|
---|
236 | while (x <= 8 && get_bits1(&alac->gb)) {
|
---|
237 | x++;
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (x > 8) {
|
---|
241 | block_size = get_bits(&alac->gb, 16);
|
---|
242 | block_size &= 0xffff;
|
---|
243 | } else {
|
---|
244 | int k;
|
---|
245 | int extrabits;
|
---|
246 |
|
---|
247 | k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
|
---|
248 |
|
---|
249 | extrabits = show_bits(&alac->gb, k);
|
---|
250 |
|
---|
251 | block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
|
---|
252 | + extrabits - 1;
|
---|
253 |
|
---|
254 | if (extrabits < 2) {
|
---|
255 | x = 1 - extrabits;
|
---|
256 | block_size += x;
|
---|
257 | get_bits(&alac->gb, k - 1);
|
---|
258 | } else {
|
---|
259 | get_bits(&alac->gb, k);
|
---|
260 | }
|
---|
261 | }
|
---|
262 |
|
---|
263 | if (block_size > 0) {
|
---|
264 | memset(&output_buffer[output_count+1], 0, block_size * 4);
|
---|
265 | output_count += block_size;
|
---|
266 |
|
---|
267 | }
|
---|
268 |
|
---|
269 | if (block_size > 0xffff)
|
---|
270 | sign_modifier = 0;
|
---|
271 |
|
---|
272 | history = 0;
|
---|
273 | }
|
---|
274 | }
|
---|
275 | }
|
---|
276 |
|
---|
277 | #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
|
---|
278 |
|
---|
279 | #define SIGN_ONLY(v) \
|
---|
280 | ((v < 0) ? (-1) : \
|
---|
281 | ((v > 0) ? (1) : \
|
---|
282 | (0)))
|
---|
283 |
|
---|
284 | static void predictor_decompress_fir_adapt(int32_t *error_buffer,
|
---|
285 | int32_t *buffer_out,
|
---|
286 | int output_size,
|
---|
287 | int readsamplesize,
|
---|
288 | int16_t *predictor_coef_table,
|
---|
289 | int predictor_coef_num,
|
---|
290 | int predictor_quantitization)
|
---|
291 | {
|
---|
292 | int i;
|
---|
293 |
|
---|
294 | /* first sample always copies */
|
---|
295 | *buffer_out = *error_buffer;
|
---|
296 |
|
---|
297 | if (!predictor_coef_num) {
|
---|
298 | if (output_size <= 1) return;
|
---|
299 | memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
|
---|
300 | return;
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
|
---|
304 | /* second-best case scenario for fir decompression,
|
---|
305 | * error describes a small difference from the previous sample only
|
---|
306 | */
|
---|
307 | if (output_size <= 1) return;
|
---|
308 | for (i = 0; i < output_size - 1; i++) {
|
---|
309 | int32_t prev_value;
|
---|
310 | int32_t error_value;
|
---|
311 |
|
---|
312 | prev_value = buffer_out[i];
|
---|
313 | error_value = error_buffer[i+1];
|
---|
314 | buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
|
---|
315 | }
|
---|
316 | return;
|
---|
317 | }
|
---|
318 |
|
---|
319 | /* read warm-up samples */
|
---|
320 | if (predictor_coef_num > 0) {
|
---|
321 | int i;
|
---|
322 | for (i = 0; i < predictor_coef_num; i++) {
|
---|
323 | int32_t val;
|
---|
324 |
|
---|
325 | val = buffer_out[i] + error_buffer[i+1];
|
---|
326 |
|
---|
327 | val = SIGN_EXTENDED32(val, readsamplesize);
|
---|
328 |
|
---|
329 | buffer_out[i+1] = val;
|
---|
330 | }
|
---|
331 | }
|
---|
332 |
|
---|
333 | #if 0
|
---|
334 | /* 4 and 8 are very common cases (the only ones i've seen). these
|
---|
335 | * should be unrolled and optimised
|
---|
336 | */
|
---|
337 | if (predictor_coef_num == 4) {
|
---|
338 | /* FIXME: optimised general case */
|
---|
339 | return;
|
---|
340 | }
|
---|
341 |
|
---|
342 | if (predictor_coef_table == 8) {
|
---|
343 | /* FIXME: optimised general case */
|
---|
344 | return;
|
---|
345 | }
|
---|
346 | #endif
|
---|
347 |
|
---|
348 |
|
---|
349 | /* general case */
|
---|
350 | if (predictor_coef_num > 0) {
|
---|
351 | for (i = predictor_coef_num + 1;
|
---|
352 | i < output_size;
|
---|
353 | i++) {
|
---|
354 | int j;
|
---|
355 | int sum = 0;
|
---|
356 | int outval;
|
---|
357 | int error_val = error_buffer[i];
|
---|
358 |
|
---|
359 | for (j = 0; j < predictor_coef_num; j++) {
|
---|
360 | sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
|
---|
361 | predictor_coef_table[j];
|
---|
362 | }
|
---|
363 |
|
---|
364 | outval = (1 << (predictor_quantitization-1)) + sum;
|
---|
365 | outval = outval >> predictor_quantitization;
|
---|
366 | outval = outval + buffer_out[0] + error_val;
|
---|
367 | outval = SIGN_EXTENDED32(outval, readsamplesize);
|
---|
368 |
|
---|
369 | buffer_out[predictor_coef_num+1] = outval;
|
---|
370 |
|
---|
371 | if (error_val > 0) {
|
---|
372 | int predictor_num = predictor_coef_num - 1;
|
---|
373 |
|
---|
374 | while (predictor_num >= 0 && error_val > 0) {
|
---|
375 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
|
---|
376 | int sign = SIGN_ONLY(val);
|
---|
377 |
|
---|
378 | predictor_coef_table[predictor_num] -= sign;
|
---|
379 |
|
---|
380 | val *= sign; /* absolute value */
|
---|
381 |
|
---|
382 | error_val -= ((val >> predictor_quantitization) *
|
---|
383 | (predictor_coef_num - predictor_num));
|
---|
384 |
|
---|
385 | predictor_num--;
|
---|
386 | }
|
---|
387 | } else if (error_val < 0) {
|
---|
388 | int predictor_num = predictor_coef_num - 1;
|
---|
389 |
|
---|
390 | while (predictor_num >= 0 && error_val < 0) {
|
---|
391 | int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
|
---|
392 | int sign = - SIGN_ONLY(val);
|
---|
393 |
|
---|
394 | predictor_coef_table[predictor_num] -= sign;
|
---|
395 |
|
---|
396 | val *= sign; /* neg value */
|
---|
397 |
|
---|
398 | error_val -= ((val >> predictor_quantitization) *
|
---|
399 | (predictor_coef_num - predictor_num));
|
---|
400 |
|
---|
401 | predictor_num--;
|
---|
402 | }
|
---|
403 | }
|
---|
404 |
|
---|
405 | buffer_out++;
|
---|
406 | }
|
---|
407 | }
|
---|
408 | }
|
---|
409 |
|
---|
410 | static void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
|
---|
411 | int16_t *buffer_out,
|
---|
412 | int numchannels, int numsamples,
|
---|
413 | uint8_t interlacing_shift,
|
---|
414 | uint8_t interlacing_leftweight)
|
---|
415 | {
|
---|
416 | int i;
|
---|
417 | if (numsamples <= 0) return;
|
---|
418 |
|
---|
419 | /* weighted interlacing */
|
---|
420 | if (interlacing_leftweight) {
|
---|
421 | for (i = 0; i < numsamples; i++) {
|
---|
422 | int32_t difference, midright;
|
---|
423 | int16_t left;
|
---|
424 | int16_t right;
|
---|
425 |
|
---|
426 | midright = buffer_a[i];
|
---|
427 | difference = buffer_b[i];
|
---|
428 |
|
---|
429 |
|
---|
430 | right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
|
---|
431 | left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
|
---|
432 | + difference;
|
---|
433 |
|
---|
434 | buffer_out[i*numchannels] = left;
|
---|
435 | buffer_out[i*numchannels + 1] = right;
|
---|
436 | }
|
---|
437 |
|
---|
438 | return;
|
---|
439 | }
|
---|
440 |
|
---|
441 | /* otherwise basic interlacing took place */
|
---|
442 | for (i = 0; i < numsamples; i++) {
|
---|
443 | int16_t left, right;
|
---|
444 |
|
---|
445 | left = buffer_a[i];
|
---|
446 | right = buffer_b[i];
|
---|
447 |
|
---|
448 | buffer_out[i*numchannels] = left;
|
---|
449 | buffer_out[i*numchannels + 1] = right;
|
---|
450 | }
|
---|
451 | }
|
---|
452 |
|
---|
453 | static int alac_decode_frame(AVCodecContext *avctx,
|
---|
454 | void *outbuffer, int *outputsize,
|
---|
455 | uint8_t *inbuffer, int input_buffer_size)
|
---|
456 | {
|
---|
457 | ALACContext *alac = avctx->priv_data;
|
---|
458 |
|
---|
459 | int channels;
|
---|
460 | int32_t outputsamples;
|
---|
461 |
|
---|
462 | /* short-circuit null buffers */
|
---|
463 | if (!inbuffer || !input_buffer_size)
|
---|
464 | return input_buffer_size;
|
---|
465 |
|
---|
466 | /* initialize from the extradata */
|
---|
467 | if (!alac->context_initialized) {
|
---|
468 | if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
|
---|
469 | av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
|
---|
470 | ALAC_EXTRADATA_SIZE);
|
---|
471 | return input_buffer_size;
|
---|
472 | }
|
---|
473 | alac_set_info(alac);
|
---|
474 | alac->context_initialized = 1;
|
---|
475 | }
|
---|
476 |
|
---|
477 | outputsamples = alac->setinfo_max_samples_per_frame;
|
---|
478 |
|
---|
479 | init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
|
---|
480 |
|
---|
481 | channels = get_bits(&alac->gb, 3);
|
---|
482 |
|
---|
483 | *outputsize = outputsamples * alac->bytespersample;
|
---|
484 |
|
---|
485 | switch(channels) {
|
---|
486 | case 0: { /* 1 channel */
|
---|
487 | int hassize;
|
---|
488 | int isnotcompressed;
|
---|
489 | int readsamplesize;
|
---|
490 |
|
---|
491 | int wasted_bytes;
|
---|
492 | int ricemodifier;
|
---|
493 |
|
---|
494 |
|
---|
495 | /* 2^result = something to do with output waiting.
|
---|
496 | * perhaps matters if we read > 1 frame in a pass?
|
---|
497 | */
|
---|
498 | get_bits(&alac->gb, 4);
|
---|
499 |
|
---|
500 | get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
|
---|
501 |
|
---|
502 | hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
|
---|
503 |
|
---|
504 | wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
|
---|
505 |
|
---|
506 | isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
|
---|
507 |
|
---|
508 | if (hassize) {
|
---|
509 | /* now read the number of samples,
|
---|
510 | * as a 32bit integer */
|
---|
511 | outputsamples = get_bits(&alac->gb, 32);
|
---|
512 | *outputsize = outputsamples * alac->bytespersample;
|
---|
513 | }
|
---|
514 |
|
---|
515 | readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
|
---|
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 |
|
---|
525 | /* FIXME: skip 16 bits, not sure what they are. seem to be used in
|
---|
526 | * two channel case */
|
---|
527 | get_bits(&alac->gb, 8);
|
---|
528 | get_bits(&alac->gb, 8);
|
---|
529 |
|
---|
530 | prediction_type = get_bits(&alac->gb, 4);
|
---|
531 | prediction_quantitization = get_bits(&alac->gb, 4);
|
---|
532 |
|
---|
533 | ricemodifier = get_bits(&alac->gb, 3);
|
---|
534 | predictor_coef_num = get_bits(&alac->gb, 5);
|
---|
535 |
|
---|
536 | /* read the predictor table */
|
---|
537 | for (i = 0; i < predictor_coef_num; i++) {
|
---|
538 | predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
|
---|
539 | }
|
---|
540 |
|
---|
541 | if (wasted_bytes) {
|
---|
542 | /* these bytes seem to have something to do with
|
---|
543 | * > 2 channel files.
|
---|
544 | */
|
---|
545 | av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
|
---|
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 {
|
---|
567 | av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
|
---|
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++) {
|
---|
581 | int32_t audiobits = get_bits(&alac->gb, readsamplesize);
|
---|
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 |
|
---|
592 | audiobits = get_bits(&alac->gb, 16);
|
---|
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 |
|
---|
598 | audiobits |= get_bits(&alac->gb, readsamplesize - 16);
|
---|
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];
|
---|
611 | ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
|
---|
612 | }
|
---|
613 | break;
|
---|
614 | }
|
---|
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 | }
|
---|
623 | break;
|
---|
624 | }
|
---|
625 | case 1: { /* 2 channels */
|
---|
626 | int hassize;
|
---|
627 | int isnotcompressed;
|
---|
628 | int readsamplesize;
|
---|
629 |
|
---|
630 | int wasted_bytes;
|
---|
631 |
|
---|
632 | uint8_t interlacing_shift;
|
---|
633 | uint8_t interlacing_leftweight;
|
---|
634 |
|
---|
635 | /* 2^result = something to do with output waiting.
|
---|
636 | * perhaps matters if we read > 1 frame in a pass?
|
---|
637 | */
|
---|
638 | get_bits(&alac->gb, 4);
|
---|
639 |
|
---|
640 | get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
|
---|
641 |
|
---|
642 | hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
|
---|
643 |
|
---|
644 | wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
|
---|
645 |
|
---|
646 | isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
|
---|
647 |
|
---|
648 | if (hassize) {
|
---|
649 | /* now read the number of samples,
|
---|
650 | * as a 32bit integer */
|
---|
651 | outputsamples = get_bits(&alac->gb, 32);
|
---|
652 | *outputsize = outputsamples * alac->bytespersample;
|
---|
653 | }
|
---|
654 |
|
---|
655 | readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
|
---|
656 |
|
---|
657 | if (!isnotcompressed) {
|
---|
658 | /* compressed */
|
---|
659 | int16_t predictor_coef_table_a[32];
|
---|
660 | int predictor_coef_num_a;
|
---|
661 | int prediction_type_a;
|
---|
662 | int prediction_quantitization_a;
|
---|
663 | int ricemodifier_a;
|
---|
664 |
|
---|
665 | int16_t predictor_coef_table_b[32];
|
---|
666 | int predictor_coef_num_b;
|
---|
667 | int prediction_type_b;
|
---|
668 | int prediction_quantitization_b;
|
---|
669 | int ricemodifier_b;
|
---|
670 |
|
---|
671 | int i;
|
---|
672 |
|
---|
673 | interlacing_shift = get_bits(&alac->gb, 8);
|
---|
674 | interlacing_leftweight = get_bits(&alac->gb, 8);
|
---|
675 |
|
---|
676 | /******** channel 1 ***********/
|
---|
677 | prediction_type_a = get_bits(&alac->gb, 4);
|
---|
678 | prediction_quantitization_a = get_bits(&alac->gb, 4);
|
---|
679 |
|
---|
680 | ricemodifier_a = get_bits(&alac->gb, 3);
|
---|
681 | predictor_coef_num_a = get_bits(&alac->gb, 5);
|
---|
682 |
|
---|
683 | /* read the predictor table */
|
---|
684 | for (i = 0; i < predictor_coef_num_a; i++) {
|
---|
685 | predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16);
|
---|
686 | }
|
---|
687 |
|
---|
688 | /******** channel 2 *********/
|
---|
689 | prediction_type_b = get_bits(&alac->gb, 4);
|
---|
690 | prediction_quantitization_b = get_bits(&alac->gb, 4);
|
---|
691 |
|
---|
692 | ricemodifier_b = get_bits(&alac->gb, 3);
|
---|
693 | predictor_coef_num_b = get_bits(&alac->gb, 5);
|
---|
694 |
|
---|
695 | /* read the predictor table */
|
---|
696 | for (i = 0; i < predictor_coef_num_b; i++) {
|
---|
697 | predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16);
|
---|
698 | }
|
---|
699 |
|
---|
700 | /*********************/
|
---|
701 | if (wasted_bytes) {
|
---|
702 | /* see mono case */
|
---|
703 | av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
|
---|
704 | }
|
---|
705 |
|
---|
706 | /* channel 1 */
|
---|
707 | bastardized_rice_decompress(alac,
|
---|
708 | alac->predicterror_buffer_a,
|
---|
709 | outputsamples,
|
---|
710 | readsamplesize,
|
---|
711 | alac->setinfo_rice_initialhistory,
|
---|
712 | alac->setinfo_rice_kmodifier,
|
---|
713 | ricemodifier_a * alac->setinfo_rice_historymult / 4,
|
---|
714 | (1 << alac->setinfo_rice_kmodifier) - 1);
|
---|
715 |
|
---|
716 | if (prediction_type_a == 0) {
|
---|
717 | /* adaptive fir */
|
---|
718 | predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
|
---|
719 | alac->outputsamples_buffer_a,
|
---|
720 | outputsamples,
|
---|
721 | readsamplesize,
|
---|
722 | predictor_coef_table_a,
|
---|
723 | predictor_coef_num_a,
|
---|
724 | prediction_quantitization_a);
|
---|
725 | } else {
|
---|
726 | /* see mono case */
|
---|
727 | av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
|
---|
728 | }
|
---|
729 |
|
---|
730 | /* channel 2 */
|
---|
731 | bastardized_rice_decompress(alac,
|
---|
732 | alac->predicterror_buffer_b,
|
---|
733 | outputsamples,
|
---|
734 | readsamplesize,
|
---|
735 | alac->setinfo_rice_initialhistory,
|
---|
736 | alac->setinfo_rice_kmodifier,
|
---|
737 | ricemodifier_b * alac->setinfo_rice_historymult / 4,
|
---|
738 | (1 << alac->setinfo_rice_kmodifier) - 1);
|
---|
739 |
|
---|
740 | if (prediction_type_b == 0) {
|
---|
741 | /* adaptive fir */
|
---|
742 | predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
|
---|
743 | alac->outputsamples_buffer_b,
|
---|
744 | outputsamples,
|
---|
745 | readsamplesize,
|
---|
746 | predictor_coef_table_b,
|
---|
747 | predictor_coef_num_b,
|
---|
748 | prediction_quantitization_b);
|
---|
749 | } else {
|
---|
750 | av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
|
---|
751 | }
|
---|
752 | } else {
|
---|
753 | /* not compressed, easy case */
|
---|
754 | if (alac->setinfo_sample_size <= 16) {
|
---|
755 | int i;
|
---|
756 | for (i = 0; i < outputsamples; i++) {
|
---|
757 | int32_t audiobits_a, audiobits_b;
|
---|
758 |
|
---|
759 | audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size);
|
---|
760 | audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size);
|
---|
761 |
|
---|
762 | audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
|
---|
763 | audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
|
---|
764 |
|
---|
765 | alac->outputsamples_buffer_a[i] = audiobits_a;
|
---|
766 | alac->outputsamples_buffer_b[i] = audiobits_b;
|
---|
767 | }
|
---|
768 | } else {
|
---|
769 | int i;
|
---|
770 | for (i = 0; i < outputsamples; i++) {
|
---|
771 | int32_t audiobits_a, audiobits_b;
|
---|
772 |
|
---|
773 | audiobits_a = get_bits(&alac->gb, 16);
|
---|
774 | audiobits_a = audiobits_a << 16;
|
---|
775 | audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
|
---|
776 | audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
|
---|
777 |
|
---|
778 | audiobits_b = get_bits(&alac->gb, 16);
|
---|
779 | audiobits_b = audiobits_b << 16;
|
---|
780 | audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
|
---|
781 | audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
|
---|
782 |
|
---|
783 | alac->outputsamples_buffer_a[i] = audiobits_a;
|
---|
784 | alac->outputsamples_buffer_b[i] = audiobits_b;
|
---|
785 | }
|
---|
786 | }
|
---|
787 | /* wasted_bytes = 0; */
|
---|
788 | interlacing_shift = 0;
|
---|
789 | interlacing_leftweight = 0;
|
---|
790 | }
|
---|
791 |
|
---|
792 | switch(alac->setinfo_sample_size) {
|
---|
793 | case 16: {
|
---|
794 | deinterlace_16(alac->outputsamples_buffer_a,
|
---|
795 | alac->outputsamples_buffer_b,
|
---|
796 | (int16_t*)outbuffer,
|
---|
797 | alac->numchannels,
|
---|
798 | outputsamples,
|
---|
799 | interlacing_shift,
|
---|
800 | interlacing_leftweight);
|
---|
801 | break;
|
---|
802 | }
|
---|
803 | case 20:
|
---|
804 | case 24:
|
---|
805 | case 32:
|
---|
806 | av_log(avctx, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
|
---|
807 | break;
|
---|
808 | default:
|
---|
809 | break;
|
---|
810 | }
|
---|
811 |
|
---|
812 | break;
|
---|
813 | }
|
---|
814 | }
|
---|
815 |
|
---|
816 | return input_buffer_size;
|
---|
817 | }
|
---|
818 |
|
---|
819 | static int alac_decode_init(AVCodecContext * avctx)
|
---|
820 | {
|
---|
821 | ALACContext *alac = avctx->priv_data;
|
---|
822 | alac->avctx = avctx;
|
---|
823 | alac->context_initialized = 0;
|
---|
824 |
|
---|
825 | alac->samplesize = alac->avctx->bits_per_sample;
|
---|
826 | alac->numchannels = alac->avctx->channels;
|
---|
827 | alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
|
---|
828 |
|
---|
829 | return 0;
|
---|
830 | }
|
---|
831 |
|
---|
832 | static int alac_decode_close(AVCodecContext *avctx)
|
---|
833 | {
|
---|
834 | ALACContext *alac = avctx->priv_data;
|
---|
835 |
|
---|
836 | av_free(alac->predicterror_buffer_a);
|
---|
837 | av_free(alac->predicterror_buffer_b);
|
---|
838 |
|
---|
839 | av_free(alac->outputsamples_buffer_a);
|
---|
840 | av_free(alac->outputsamples_buffer_b);
|
---|
841 |
|
---|
842 | return 0;
|
---|
843 | }
|
---|
844 |
|
---|
845 | AVCodec alac_decoder = {
|
---|
846 | "alac",
|
---|
847 | CODEC_TYPE_AUDIO,
|
---|
848 | CODEC_ID_ALAC,
|
---|
849 | sizeof(ALACContext),
|
---|
850 | alac_decode_init,
|
---|
851 | NULL,
|
---|
852 | alac_decode_close,
|
---|
853 | alac_decode_frame,
|
---|
854 | };
|
---|