2 * ALAC (Apple Lossless Audio Codec) decoder
3 * Copyright (c) 2005 David Hammerton
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 * ALAC (Apple Lossless Audio Codec) decoder
24 * @author 2005 David Hammerton
26 * For more information on the ALAC format, visit:
27 * http://crazney.net/programs/itunes/alac.html
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
40 #define ALAC_EXTRADATA_SIZE 36
43 unsigned char *input_buffer;
44 int input_buffer_index;
45 int input_buffer_size;
46 int input_buffer_bitaccumulator; /* used so we can do arbitary
55 int32_t *predicterror_buffer_a;
56 int32_t *predicterror_buffer_b;
58 int32_t *outputsamples_buffer_a;
59 int32_t *outputsamples_buffer_b;
62 /* stuff from setinfo */
63 uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */ /* max samples per frame? */
64 uint8_t setinfo_7a; /* 0x00 */
65 uint8_t setinfo_sample_size; /* 0x10 */
66 uint8_t setinfo_rice_historymult; /* 0x28 */
67 uint8_t setinfo_rice_initialhistory; /* 0x0a */
68 uint8_t setinfo_rice_kmodifier; /* 0x0e */
69 uint8_t setinfo_7f; /* 0x02 */
70 uint16_t setinfo_80; /* 0x00ff */
71 uint32_t setinfo_82; /* 0x000020e7 */
72 uint32_t setinfo_86; /* 0x00069fe4 */
73 uint32_t setinfo_8a_rate; /* 0x0000ac44 */
74 /* end setinfo stuff */
77 typedef struct alac_file alac_file;
81 AVCodecContext *avctx;
82 /* init to 0; first frame decode should initialize from extradata and
84 int context_initialized;
89 static void allocate_buffers(alac_file *alac)
91 alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
92 alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
94 alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
95 alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
98 void alac_set_info(alac_file *alac, char *inputbuffer)
100 unsigned char *ptr = inputbuffer;
106 alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
108 alac->setinfo_7a = *ptr++;
109 alac->setinfo_sample_size = *ptr++;
110 alac->setinfo_rice_historymult = *ptr++;
111 alac->setinfo_rice_initialhistory = *ptr++;
112 alac->setinfo_rice_kmodifier = *ptr++;
113 alac->setinfo_7f = *ptr++;
114 alac->setinfo_80 = BE_16(ptr);
116 alac->setinfo_82 = BE_32(ptr);
118 alac->setinfo_86 = BE_32(ptr);
120 alac->setinfo_8a_rate = BE_32(ptr);
123 allocate_buffers(alac);
128 /* supports reading 1 to 16 bits, in big endian format */
129 static uint32_t readbits_16(alac_file *alac, int bits)
134 if (alac->input_buffer_index + 2 >= alac->input_buffer_size) {
135 av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
136 alac->input_buffer_index + 2, alac->input_buffer_size);
139 result = (alac->input_buffer[alac->input_buffer_index + 0] << 16) |
140 (alac->input_buffer[alac->input_buffer_index + 1] << 8) |
141 (alac->input_buffer[alac->input_buffer_index + 2]);
143 /* shift left by the number of bits we've already read,
144 * so that the top 'n' bits of the 24 bits we read will
145 * be the return bits */
146 result = result << alac->input_buffer_bitaccumulator;
148 result = result & 0x00ffffff;
150 /* and then only want the top 'n' bits from that, where
152 result = result >> (24 - bits);
154 new_accumulator = (alac->input_buffer_bitaccumulator + bits);
156 /* increase the buffer pointer if we've read over n bytes. */
157 alac->input_buffer_index += (new_accumulator >> 3);
159 /* and the remainder goes back into the bit accumulator */
160 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
165 /* supports reading 1 to 32 bits, in big endian format */
166 static uint32_t readbits(alac_file *alac, int bits)
172 result = readbits_16(alac, 16) << bits;
175 result |= readbits_16(alac, bits);
180 /* reads a single bit */
181 static int readbit(alac_file *alac)
186 if (alac->input_buffer_index >= alac->input_buffer_size) {
187 av_log(NULL, AV_LOG_ERROR, "alac: input buffer went out of bounds (%d >= %d)\n",
188 alac->input_buffer_index + 2, alac->input_buffer_size);
192 result = alac->input_buffer[alac->input_buffer_index];
194 result = result << alac->input_buffer_bitaccumulator;
196 result = result >> 7 & 1;
198 new_accumulator = (alac->input_buffer_bitaccumulator + 1);
200 alac->input_buffer_index += (new_accumulator / 8);
202 alac->input_buffer_bitaccumulator = (new_accumulator % 8);
207 static void unreadbits(alac_file *alac, int bits)
209 int new_accumulator = (alac->input_buffer_bitaccumulator - bits);
211 alac->input_buffer_index += (new_accumulator >> 3);
213 alac->input_buffer_bitaccumulator = (new_accumulator & 7);
214 if (alac->input_buffer_bitaccumulator < 0)
215 alac->input_buffer_bitaccumulator *= -1;
218 /* hideously inefficient. could use a bitmask search,
219 * alternatively bsr on x86,
221 static int count_leading_zeros(int32_t input)
224 while (!(0x80000000 & input) && i < 32) {
231 void bastardized_rice_decompress(alac_file *alac,
232 int32_t *output_buffer,
234 int readsamplesize, /* arg_10 */
235 int rice_initialhistory, /* arg424->b */
236 int rice_kmodifier, /* arg424->d */
237 int rice_historymult, /* arg424->c */
238 int rice_kmodifier_mask /* arg424->e */
242 unsigned int history = rice_initialhistory;
243 int sign_modifier = 0;
245 for (output_count = 0; output_count < output_size; output_count++) {
250 /* read x - number of 1s before 0 represent the rice */
251 while (x <= 8 && readbit(alac)) {
256 if (x > 8) { /* RICE THRESHOLD */
257 /* use alternative encoding */
260 value = readbits(alac, readsamplesize);
262 /* mask value to readsamplesize size */
263 if (readsamplesize != 32)
264 value &= (0xffffffff >> (32 - readsamplesize));
268 /* standard rice encoding */
270 int k; /* size of extra bits */
272 /* read k, that is bits as is */
273 k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
281 extrabits = readbits(alac, k);
283 /* multiply x by 2^k - 1, as part of their strange algorithm */
293 x_modified = sign_modifier + x;
294 final_val = (x_modified + 1) / 2;
295 if (x_modified & 1) final_val *= -1;
297 output_buffer[output_count] = final_val;
301 /* now update the history */
302 history += (x_modified * rice_historymult)
303 - ((history * rice_historymult) >> 9);
305 if (x_modified > 0xffff)
308 /* special case: there may be compressed blocks of 0 */
309 if ((history < 128) && (output_count+1 < output_size)) {
315 while (x <= 8 && readbit(alac)) {
320 block_size = readbits(alac, 16);
321 block_size &= 0xffff;
326 k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
328 extrabits = readbits(alac, k);
330 block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
340 if (block_size > 0) {
341 memset(&output_buffer[output_count+1], 0, block_size * 4);
342 output_count += block_size;
346 if (block_size > 0xffff)
354 #define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
356 #define SIGN_ONLY(v) \
361 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
365 int16_t *predictor_coef_table,
366 int predictor_coef_num,
367 int predictor_quantitization)
371 /* first sample always copies */
372 *buffer_out = *error_buffer;
374 if (!predictor_coef_num) {
375 if (output_size <= 1) return;
376 memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
380 if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
381 /* second-best case scenario for fir decompression,
382 * error describes a small difference from the previous sample only
384 if (output_size <= 1) return;
385 for (i = 0; i < output_size - 1; i++) {
389 prev_value = buffer_out[i];
390 error_value = error_buffer[i+1];
391 buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
396 /* read warm-up samples */
397 if (predictor_coef_num > 0) {
399 for (i = 0; i < predictor_coef_num; i++) {
402 val = buffer_out[i] + error_buffer[i+1];
404 val = SIGN_EXTENDED32(val, readsamplesize);
406 buffer_out[i+1] = val;
411 /* 4 and 8 are very common cases (the only ones i've seen). these
412 * should be unrolled and optimised
414 if (predictor_coef_num == 4) {
415 /* FIXME: optimised general case */
419 if (predictor_coef_table == 8) {
420 /* FIXME: optimised general case */
427 if (predictor_coef_num > 0) {
428 for (i = predictor_coef_num + 1;
434 int error_val = error_buffer[i];
436 for (j = 0; j < predictor_coef_num; j++) {
437 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
438 predictor_coef_table[j];
441 outval = (1 << (predictor_quantitization-1)) + sum;
442 outval = outval >> predictor_quantitization;
443 outval = outval + buffer_out[0] + error_val;
444 outval = SIGN_EXTENDED32(outval, readsamplesize);
446 buffer_out[predictor_coef_num+1] = outval;
449 int predictor_num = predictor_coef_num - 1;
451 while (predictor_num >= 0 && error_val > 0) {
452 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
453 int sign = SIGN_ONLY(val);
455 predictor_coef_table[predictor_num] -= sign;
457 val *= sign; /* absolute value */
459 error_val -= ((val >> predictor_quantitization) *
460 (predictor_coef_num - predictor_num));
464 } else if (error_val < 0) {
465 int predictor_num = predictor_coef_num - 1;
467 while (predictor_num >= 0 && error_val < 0) {
468 int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
469 int sign = - SIGN_ONLY(val);
471 predictor_coef_table[predictor_num] -= sign;
473 val *= sign; /* neg value */
475 error_val -= ((val >> predictor_quantitization) *
476 (predictor_coef_num - predictor_num));
487 void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
489 int numchannels, int numsamples,
490 uint8_t interlacing_shift,
491 uint8_t interlacing_leftweight)
494 if (numsamples <= 0) return;
496 /* weighted interlacing */
497 if (interlacing_leftweight) {
498 for (i = 0; i < numsamples; i++) {
499 int32_t difference, midright;
503 midright = buffer_a[i];
504 difference = buffer_b[i];
507 right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
508 left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
511 buffer_out[i*numchannels] = left;
512 buffer_out[i*numchannels + 1] = right;
518 /* otherwise basic interlacing took place */
519 for (i = 0; i < numsamples; i++) {
525 buffer_out[i*numchannels] = left;
526 buffer_out[i*numchannels + 1] = right;
530 static int alac_decode_frame(AVCodecContext *avctx,
531 void *outbuffer, int *outputsize,
532 uint8_t *inbuffer, int input_buffer_size)
534 ALACContext *s = avctx->priv_data;
535 alac_file *alac = s->alac;
538 int32_t outputsamples;
540 /* short-circuit null buffers */
541 if (!inbuffer || !input_buffer_size)
542 return input_buffer_size;
544 /* initialize from the extradata */
545 if (!s->context_initialized) {
546 if (s->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
547 av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
548 ALAC_EXTRADATA_SIZE);
549 return input_buffer_size;
551 alac_set_info(s->alac, s->avctx->extradata);
552 s->context_initialized = 1;
555 outputsamples = alac->setinfo_max_samples_per_frame;
557 /* setup the stream */
558 alac->input_buffer = inbuffer;
559 alac->input_buffer_index = 0;
560 alac->input_buffer_size = input_buffer_size;
561 alac->input_buffer_bitaccumulator = 0;
563 channels = readbits(alac, 3);
565 *outputsize = outputsamples * alac->bytespersample;
568 case 0: { /* 1 channel */
577 /* 2^result = something to do with output waiting.
578 * perhaps matters if we read > 1 frame in a pass?
582 readbits(alac, 12); /* unknown, skip 12 bits */
584 hassize = readbits(alac, 1); /* the output sample size is stored soon */
586 wasted_bytes = readbits(alac, 2); /* unknown ? */
588 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
591 /* now read the number of samples,
592 * as a 32bit integer */
593 outputsamples = readbits(alac, 32);
594 *outputsize = outputsamples * alac->bytespersample;
597 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
599 if (!isnotcompressed) {
600 /* so it is compressed */
601 int16_t predictor_coef_table[32];
602 int predictor_coef_num;
604 int prediction_quantitization;
607 /* skip 16 bits, not sure what they are. seem to be used in
608 * two channel case */
612 prediction_type = readbits(alac, 4);
613 prediction_quantitization = readbits(alac, 4);
615 ricemodifier = readbits(alac, 3);
616 predictor_coef_num = readbits(alac, 5);
618 /* read the predictor table */
619 for (i = 0; i < predictor_coef_num; i++) {
620 predictor_coef_table[i] = (int16_t)readbits(alac, 16);
624 /* these bytes seem to have something to do with
627 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
630 bastardized_rice_decompress(alac,
631 alac->predicterror_buffer_a,
634 alac->setinfo_rice_initialhistory,
635 alac->setinfo_rice_kmodifier,
636 ricemodifier * alac->setinfo_rice_historymult / 4,
637 (1 << alac->setinfo_rice_kmodifier) - 1);
639 if (prediction_type == 0) {
641 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
642 alac->outputsamples_buffer_a,
645 predictor_coef_table,
647 prediction_quantitization);
649 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
650 /* i think the only other prediction type (or perhaps this is just a
651 * boolean?) runs adaptive fir twice.. like:
652 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
653 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
659 /* not compressed, easy case */
660 if (readsamplesize <= 16) {
662 for (i = 0; i < outputsamples; i++) {
663 int32_t audiobits = readbits(alac, readsamplesize);
665 audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
667 alac->outputsamples_buffer_a[i] = audiobits;
671 for (i = 0; i < outputsamples; i++) {
674 audiobits = readbits(alac, 16);
675 /* special case of sign extension..
676 * as we'll be ORing the low 16bits into this */
677 audiobits = audiobits << 16;
678 audiobits = audiobits >> (32 - readsamplesize);
680 audiobits |= readbits(alac, readsamplesize - 16);
682 alac->outputsamples_buffer_a[i] = audiobits;
685 /* wasted_bytes = 0; // unused */
688 switch(alac->setinfo_sample_size) {
691 for (i = 0; i < outputsamples; i++) {
692 int16_t sample = alac->outputsamples_buffer_a[i];
694 ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
701 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
708 case 1: { /* 2 channels */
715 uint8_t interlacing_shift;
716 uint8_t interlacing_leftweight;
718 /* 2^result = something to do with output waiting.
719 * perhaps matters if we read > 1 frame in a pass?
723 readbits(alac, 12); /* unknown, skip 12 bits */
725 hassize = readbits(alac, 1); /* the output sample size is stored soon */
727 wasted_bytes = readbits(alac, 2); /* unknown ? */
729 isnotcompressed = readbits(alac, 1); /* whether the frame is compressed */
732 /* now read the number of samples,
733 * as a 32bit integer */
734 outputsamples = readbits(alac, 32);
735 *outputsize = outputsamples * alac->bytespersample;
738 readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
740 if (!isnotcompressed) {
742 int16_t predictor_coef_table_a[32];
743 int predictor_coef_num_a;
744 int prediction_type_a;
745 int prediction_quantitization_a;
748 int16_t predictor_coef_table_b[32];
749 int predictor_coef_num_b;
750 int prediction_type_b;
751 int prediction_quantitization_b;
756 interlacing_shift = readbits(alac, 8);
757 interlacing_leftweight = readbits(alac, 8);
759 /******** channel 1 ***********/
760 prediction_type_a = readbits(alac, 4);
761 prediction_quantitization_a = readbits(alac, 4);
763 ricemodifier_a = readbits(alac, 3);
764 predictor_coef_num_a = readbits(alac, 5);
766 /* read the predictor table */
767 for (i = 0; i < predictor_coef_num_a; i++) {
768 predictor_coef_table_a[i] = (int16_t)readbits(alac, 16);
771 /******** channel 2 *********/
772 prediction_type_b = readbits(alac, 4);
773 prediction_quantitization_b = readbits(alac, 4);
775 ricemodifier_b = readbits(alac, 3);
776 predictor_coef_num_b = readbits(alac, 5);
778 /* read the predictor table */
779 for (i = 0; i < predictor_coef_num_b; i++) {
780 predictor_coef_table_b[i] = (int16_t)readbits(alac, 16);
783 /*********************/
786 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
790 bastardized_rice_decompress(alac,
791 alac->predicterror_buffer_a,
794 alac->setinfo_rice_initialhistory,
795 alac->setinfo_rice_kmodifier,
796 ricemodifier_a * alac->setinfo_rice_historymult / 4,
797 (1 << alac->setinfo_rice_kmodifier) - 1);
799 if (prediction_type_a == 0) {
801 predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
802 alac->outputsamples_buffer_a,
805 predictor_coef_table_a,
806 predictor_coef_num_a,
807 prediction_quantitization_a);
810 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
814 bastardized_rice_decompress(alac,
815 alac->predicterror_buffer_b,
818 alac->setinfo_rice_initialhistory,
819 alac->setinfo_rice_kmodifier,
820 ricemodifier_b * alac->setinfo_rice_historymult / 4,
821 (1 << alac->setinfo_rice_kmodifier) - 1);
823 if (prediction_type_b == 0) {
825 predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
826 alac->outputsamples_buffer_b,
829 predictor_coef_table_b,
830 predictor_coef_num_b,
831 prediction_quantitization_b);
833 av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
836 /* not compressed, easy case */
837 if (alac->setinfo_sample_size <= 16) {
839 for (i = 0; i < outputsamples; i++) {
840 int32_t audiobits_a, audiobits_b;
842 audiobits_a = readbits(alac, alac->setinfo_sample_size);
843 audiobits_b = readbits(alac, alac->setinfo_sample_size);
845 audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
846 audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
848 alac->outputsamples_buffer_a[i] = audiobits_a;
849 alac->outputsamples_buffer_b[i] = audiobits_b;
853 for (i = 0; i < outputsamples; i++) {
854 int32_t audiobits_a, audiobits_b;
856 audiobits_a = readbits(alac, 16);
857 audiobits_a = audiobits_a << 16;
858 audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
859 audiobits_a |= readbits(alac, alac->setinfo_sample_size - 16);
861 audiobits_b = readbits(alac, 16);
862 audiobits_b = audiobits_b << 16;
863 audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
864 audiobits_b |= readbits(alac, alac->setinfo_sample_size - 16);
866 alac->outputsamples_buffer_a[i] = audiobits_a;
867 alac->outputsamples_buffer_b[i] = audiobits_b;
870 /* wasted_bytes = 0; */
871 interlacing_shift = 0;
872 interlacing_leftweight = 0;
875 switch(alac->setinfo_sample_size) {
877 deinterlace_16(alac->outputsamples_buffer_a,
878 alac->outputsamples_buffer_b,
883 interlacing_leftweight);
889 av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
899 return input_buffer_size;
902 static int alac_decode_init(AVCodecContext * avctx)
904 ALACContext *s = avctx->priv_data;
906 s->context_initialized = 0;
908 s->alac = av_malloc(sizeof(alac_file));
910 s->alac->samplesize = s->avctx->bits_per_sample;
911 s->alac->numchannels = s->avctx->channels;
912 s->alac->bytespersample = (s->alac->samplesize / 8) * s->alac->numchannels;
917 static int alac_decode_close(AVCodecContext *avctx)
919 ALACContext *s = avctx->priv_data;
921 av_free(s->alac->predicterror_buffer_a);
922 av_free(s->alac->predicterror_buffer_b);
924 av_free(s->alac->outputsamples_buffer_a);
925 av_free(s->alac->outputsamples_buffer_b);
930 AVCodec alac_decoder = {