1 /*
2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
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
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 /**
23 * @file
24 * ALAC (Apple Lossless Audio Codec) decoder
25 * @author 2005 David Hammerton
26 * @see http://crazney.net/programs/itunes/alac.html
27 *
28 * Note: This decoder expects a 36-byte QuickTime atom to be
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:
31 *
32 * 32 bits atom size
33 * 32 bits tag ("alac")
34 * 32 bits tag version (0)
35 * 32 bits samples per frame (used when not set explicitly in the frames)
36 * 8 bits compatible version (0)
37 * 8 bits sample size
38 * 8 bits history mult (40)
39 * 8 bits initial history (10)
40 * 8 bits rice param limit (14)
41 * 8 bits channels
42 * 16 bits maxRun (255)
43 * 32 bits max coded frame size (0 means unknown)
44 * 32 bits average bitrate (0 means unknown)
45 * 32 bits samplerate
46 */
47
48 #include <inttypes.h>
49
61
62 #define ALAC_EXTRADATA_SIZE 36
63
69
73
80
81 int extra_bits;
/**< number of extra bits beyond 16-bit */
82 int nb_samples;
/**< number of samples in the current frame */
83
86
89
91 {
93
94 if (x > 8) { /* RICE THRESHOLD */
95 /* use alternative encoding */
97 } else if (k != 1) {
99
100 /* multiply x by 2^k - 1, as part of their strange algorithm */
101 x = (x << k) - x;
102
103 if (extrabits > 1) {
104 x += extrabits - 1;
106 } else
108 }
109 return x;
110 }
111
113 int nb_samples,
int bps,
int rice_history_mult)
114 {
117 int sign_modifier = 0;
118
119 for (
i = 0;
i < nb_samples;
i++) {
120 int k;
121 unsigned int x;
122
125
126 /* calculate rice param and decode next value */
127 k =
av_log2((history >> 9) + 3);
130 x += sign_modifier;
131 sign_modifier = 0;
132 output_buffer[
i] = (x >> 1) ^ -(x & 1);
133
134 /* update the history */
135 if (x > 0xffff)
136 history = 0xffff;
137 else
138 history += x * rice_history_mult -
139 ((history * rice_history_mult) >> 9);
140
141 /* special case: there may be compressed blocks of 0 */
142 if ((history < 128) && (
i + 1 < nb_samples)) {
143 int block_size;
144
145 /* calculate rice param and decode block size */
146 k = 7 -
av_log2(history) + ((history + 16) >> 6);
149
150 if (block_size > 0) {
151 if (block_size >= nb_samples -
i) {
153 "invalid zero block size of %d %d %d\n", block_size,
155 block_size = nb_samples -
i - 1;
156 }
157 memset(&output_buffer[
i + 1], 0,
158 block_size * sizeof(*output_buffer));
160 }
161 if (block_size <= 0xffff)
162 sign_modifier = 1;
163 history = 0;
164 }
165 }
166 return 0;
167 }
168
170 {
172 }
173
175 int nb_samples,
int bps, int16_t *lpc_coefs,
176 int lpc_order, int lpc_quant)
177 {
179 uint32_t *
pred = buffer_out;
180
181 /* first sample always copies */
182 *buffer_out = *error_buffer;
183
184 if (nb_samples <= 1)
185 return;
186
187 if (!lpc_order) {
188 memcpy(&buffer_out[1], &error_buffer[1],
189 (nb_samples - 1) * sizeof(*buffer_out));
190 return;
191 }
192
193 if (lpc_order == 31) {
194 /* simple 1st-order prediction */
195 for (
i = 1;
i < nb_samples;
i++) {
198 }
199 return;
200 }
201
202 /* read warm-up samples */
203 for (
i = 1;
i <= lpc_order &&
i < nb_samples;
i++)
205
206 /* NOTE: 4 and 8 are very common cases that could be optimized. */
207
208 for (;
i < nb_samples;
i++) {
209 int j;
211 unsigned error_val = error_buffer[
i];
212 int error_sign;
214
215 /* LPC prediction */
216 for (j = 0; j < lpc_order; j++)
217 val += (
pred[j] -
d) * lpc_coefs[j];
218 val = (
val + (1LL << (lpc_quant - 1))) >> lpc_quant;
219 val +=
d + error_val;
221
222 /* adapt LPC coefficients */
224 if (error_sign) {
225 for (j = 0; j < lpc_order && (
int)(error_val * error_sign) > 0; j++) {
226 int sign;
229 lpc_coefs[j] -= sign;
230 val *= (unsigned)sign;
231 error_val -= (
val >> lpc_quant) * (j + 1
U);
232 }
233 }
234 }
235 }
236
239 {
241 int has_size,
bps, is_compressed, decorr_shift, decorr_left_weight,
ret;
242 uint32_t output_samples;
244
245 skip_bits(&alac->
gb, 4);
/* element instance tag */
247
248 /* the number of output samples is stored in the frame */
250
256 }
259
260 /* whether the frame is compressed */
262
263 if (has_size)
265 else
269 output_samples);
271 }
274 /* get output buffer */
275 frame->nb_samples = output_samples;
278 }
else if (output_samples != alac->
nb_samples) {
282 }
287 }
288
289 if (is_compressed) {
290 int16_t lpc_coefs[2][32];
291 int lpc_order[2];
292 int prediction_type[2];
293 int lpc_quant[2];
294 int rice_history_mult[2];
295
298 "Compression with rice limit 0");
300 }
301
304
305 if (
channels == 2 && decorr_left_weight && decorr_shift > 31)
307
311 rice_history_mult[ch] =
get_bits(&alac->
gb, 3);
313
316
317 /* read the predictor table */
318 for (
i = lpc_order[ch] - 1;
i >= 0;
i--)
320 }
321
328 }
329 }
336
337 /* adaptive FIR filter */
338 if (prediction_type[ch] == 15) {
339 /* Prediction type 15 runs the adaptive FIR twice.
340 * The first pass uses the special-case coef_num = 31, while
341 * the second pass uses the coefs from the bitstream.
342 *
343 * However, this prediction type is not currently used by the
344 * reference encoder.
345 */
349 } else if (prediction_type[ch] > 0) {
351 prediction_type[ch]);
352 }
355 bps, lpc_coefs[ch], lpc_order[ch], lpc_quant[ch]);
356 }
357 } else {
358 /* not compressed, easy case */
365 }
366 }
368 decorr_shift = 0;
369 decorr_left_weight = 0;
370 }
371
376 }
377
378 if (decorr_left_weight) {
380 decorr_shift, decorr_left_weight);
381 }
382
386 }
390 }
391
393 case 16: {
395 int16_t *outbuffer = (int16_t *)
frame->extended_data[ch_index + ch];
398 }}
399 break;
400 case 20: {
404 }}
405 break;
406 case 24: {
410 }}
411 break;
412 }
413
414 return 0;
415 }
416
418 int *got_frame_ptr,
AVPacket *avpkt)
419 {
424 int ch,
ret, got_end;
425
428
429 got_end = 0;
431 ch = 0;
435 got_end = 1;
436 break;
437 }
441 }
442
448 }
449
455
457 }
458 if (!got_end) {
461 }
462
466 }
467
469 *got_frame_ptr = 1;
470 else
472
474 }
475
477 {
479
480 int ch;
486 }
487
488 return 0;
489 }
490
492 {
493 int ch;
496
497 for (ch = 0; ch < 2; ch++) {
501 }
502
506
511 }
512
515 }
516 return 0;
517 }
518
520 {
522
525
527
532 "max samples per frame invalid: %"PRIu32"\n",
535 }
540 alac->
rice_limit = bytestream2_get_byteu(&gb);
541 alac->
channels = bytestream2_get_byteu(&gb);
542 bytestream2_get_be16u(&gb); // maxRun
543 bytestream2_get_be32u(&gb); // max coded frame size
544 bytestream2_get_be32u(&gb); // average bitrate
546
547 return 0;
548 }
549
551 {
555
556 /* initialize from the extradata */
560 }
564 }
565
568 break;
569 case 20:
570 case 24:
572 break;
575 }
578
582 } else {
585 else
587 }
592 }
594
598 }
599
601
602 return 0;
603 }
604
606 { "extra_bits_bug", "Force non-standard decoding process",
610 };
611
617 };
618
631 };