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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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
39
#include "bitstream.h"
41
#define ALAC_EXTRADATA_SIZE 36
45
AVCodecContext *avctx;
47
/* init to 0; first frame decode should initialize from extradata and
49
int context_initialized;
56
int32_t *predicterror_buffer_a;
57
int32_t *predicterror_buffer_b;
59
int32_t *outputsamples_buffer_a;
60
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 */
78
static void allocate_buffers(ALACContext *alac)
80
alac->predicterror_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
81
alac->predicterror_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
83
alac->outputsamples_buffer_a = av_malloc(alac->setinfo_max_samples_per_frame * 4);
84
alac->outputsamples_buffer_b = av_malloc(alac->setinfo_max_samples_per_frame * 4);
87
void alac_set_info(ALACContext *alac)
89
unsigned char *ptr = alac->avctx->extradata;
95
alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
97
alac->setinfo_7a = *ptr++;
98
alac->setinfo_sample_size = *ptr++;
99
alac->setinfo_rice_historymult = *ptr++;
100
alac->setinfo_rice_initialhistory = *ptr++;
101
alac->setinfo_rice_kmodifier = *ptr++;
102
alac->setinfo_7f = *ptr++;
103
alac->setinfo_80 = BE_16(ptr);
105
alac->setinfo_82 = BE_32(ptr);
107
alac->setinfo_86 = BE_32(ptr);
109
alac->setinfo_8a_rate = BE_32(ptr);
112
allocate_buffers(alac);
115
/* hideously inefficient. could use a bitmask search,
116
* alternatively bsr on x86,
118
static int count_leading_zeros(int32_t input)
121
while (!(0x80000000 & input) && i < 32) {
128
void bastardized_rice_decompress(ALACContext *alac,
129
int32_t *output_buffer,
131
int readsamplesize, /* arg_10 */
132
int rice_initialhistory, /* arg424->b */
133
int rice_kmodifier, /* arg424->d */
134
int rice_historymult, /* arg424->c */
135
int rice_kmodifier_mask /* arg424->e */
139
unsigned int history = rice_initialhistory;
140
int sign_modifier = 0;
142
for (output_count = 0; output_count < output_size; output_count++) {
147
/* read x - number of 1s before 0 represent the rice */
148
while (x <= 8 && get_bits1(&alac->gb)) {
153
if (x > 8) { /* RICE THRESHOLD */
154
/* use alternative encoding */
157
value = get_bits(&alac->gb, readsamplesize);
159
/* mask value to readsamplesize size */
160
if (readsamplesize != 32)
161
value &= (0xffffffff >> (32 - readsamplesize));
165
/* standard rice encoding */
167
int k; /* size of extra bits */
169
/* read k, that is bits as is */
170
k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
178
extrabits = show_bits(&alac->gb, k);
180
/* multiply x by 2^k - 1, as part of their strange algorithm */
185
get_bits(&alac->gb, k);
187
get_bits(&alac->gb, k - 1);
192
x_modified = sign_modifier + x;
193
final_val = (x_modified + 1) / 2;
194
if (x_modified & 1) final_val *= -1;
196
output_buffer[output_count] = final_val;
200
/* now update the history */
201
history += (x_modified * rice_historymult)
202
- ((history * rice_historymult) >> 9);
204
if (x_modified > 0xffff)
207
/* special case: there may be compressed blocks of 0 */
208
if ((history < 128) && (output_count+1 < output_size)) {
214
while (x <= 8 && get_bits1(&alac->gb)) {
219
block_size = get_bits(&alac->gb, 16);
220
block_size &= 0xffff;
225
k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
227
extrabits = show_bits(&alac->gb, k);
229
block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
235
get_bits(&alac->gb, k - 1);
237
get_bits(&alac->gb, k);
241
if (block_size > 0) {
242
memset(&output_buffer[output_count+1], 0, block_size * 4);
243
output_count += block_size;
247
if (block_size > 0xffff)
255
#define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
257
#define SIGN_ONLY(v) \
262
static void predictor_decompress_fir_adapt(int32_t *error_buffer,
266
int16_t *predictor_coef_table,
267
int predictor_coef_num,
268
int predictor_quantitization)
272
/* first sample always copies */
273
*buffer_out = *error_buffer;
275
if (!predictor_coef_num) {
276
if (output_size <= 1) return;
277
memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
281
if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
282
/* second-best case scenario for fir decompression,
283
* error describes a small difference from the previous sample only
285
if (output_size <= 1) return;
286
for (i = 0; i < output_size - 1; i++) {
290
prev_value = buffer_out[i];
291
error_value = error_buffer[i+1];
292
buffer_out[i+1] = SIGN_EXTENDED32((prev_value + error_value), readsamplesize);
297
/* read warm-up samples */
298
if (predictor_coef_num > 0) {
300
for (i = 0; i < predictor_coef_num; i++) {
303
val = buffer_out[i] + error_buffer[i+1];
305
val = SIGN_EXTENDED32(val, readsamplesize);
307
buffer_out[i+1] = val;
312
/* 4 and 8 are very common cases (the only ones i've seen). these
313
* should be unrolled and optimised
315
if (predictor_coef_num == 4) {
316
/* FIXME: optimised general case */
320
if (predictor_coef_table == 8) {
321
/* FIXME: optimised general case */
328
if (predictor_coef_num > 0) {
329
for (i = predictor_coef_num + 1;
335
int error_val = error_buffer[i];
337
for (j = 0; j < predictor_coef_num; j++) {
338
sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
339
predictor_coef_table[j];
342
outval = (1 << (predictor_quantitization-1)) + sum;
343
outval = outval >> predictor_quantitization;
344
outval = outval + buffer_out[0] + error_val;
345
outval = SIGN_EXTENDED32(outval, readsamplesize);
347
buffer_out[predictor_coef_num+1] = outval;
350
int predictor_num = predictor_coef_num - 1;
352
while (predictor_num >= 0 && error_val > 0) {
353
int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
354
int sign = SIGN_ONLY(val);
356
predictor_coef_table[predictor_num] -= sign;
358
val *= sign; /* absolute value */
360
error_val -= ((val >> predictor_quantitization) *
361
(predictor_coef_num - predictor_num));
365
} else if (error_val < 0) {
366
int predictor_num = predictor_coef_num - 1;
368
while (predictor_num >= 0 && error_val < 0) {
369
int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
370
int sign = - SIGN_ONLY(val);
372
predictor_coef_table[predictor_num] -= sign;
374
val *= sign; /* neg value */
376
error_val -= ((val >> predictor_quantitization) *
377
(predictor_coef_num - predictor_num));
388
void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
390
int numchannels, int numsamples,
391
uint8_t interlacing_shift,
392
uint8_t interlacing_leftweight)
395
if (numsamples <= 0) return;
397
/* weighted interlacing */
398
if (interlacing_leftweight) {
399
for (i = 0; i < numsamples; i++) {
400
int32_t difference, midright;
404
midright = buffer_a[i];
405
difference = buffer_b[i];
408
right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
409
left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
412
buffer_out[i*numchannels] = left;
413
buffer_out[i*numchannels + 1] = right;
419
/* otherwise basic interlacing took place */
420
for (i = 0; i < numsamples; i++) {
426
buffer_out[i*numchannels] = left;
427
buffer_out[i*numchannels + 1] = right;
431
static int alac_decode_frame(AVCodecContext *avctx,
432
void *outbuffer, int *outputsize,
433
uint8_t *inbuffer, int input_buffer_size)
435
ALACContext *alac = avctx->priv_data;
438
int32_t outputsamples;
440
/* short-circuit null buffers */
441
if (!inbuffer || !input_buffer_size)
442
return input_buffer_size;
444
/* initialize from the extradata */
445
if (!alac->context_initialized) {
446
if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
447
av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
448
ALAC_EXTRADATA_SIZE);
449
return input_buffer_size;
452
alac->context_initialized = 1;
455
outputsamples = alac->setinfo_max_samples_per_frame;
457
init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
459
channels = get_bits(&alac->gb, 3);
461
*outputsize = outputsamples * alac->bytespersample;
464
case 0: { /* 1 channel */
473
/* 2^result = something to do with output waiting.
474
* perhaps matters if we read > 1 frame in a pass?
476
get_bits(&alac->gb, 4);
478
get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
480
hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
482
wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
484
isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
487
/* now read the number of samples,
488
* as a 32bit integer */
489
outputsamples = get_bits(&alac->gb, 32);
490
*outputsize = outputsamples * alac->bytespersample;
493
readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
495
if (!isnotcompressed) {
496
/* so it is compressed */
497
int16_t predictor_coef_table[32];
498
int predictor_coef_num;
500
int prediction_quantitization;
503
/* skip 16 bits, not sure what they are. seem to be used in
504
* two channel case */
505
get_bits(&alac->gb, 8);
506
get_bits(&alac->gb, 8);
508
prediction_type = get_bits(&alac->gb, 4);
509
prediction_quantitization = get_bits(&alac->gb, 4);
511
ricemodifier = get_bits(&alac->gb, 3);
512
predictor_coef_num = get_bits(&alac->gb, 5);
514
/* read the predictor table */
515
for (i = 0; i < predictor_coef_num; i++) {
516
predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
520
/* these bytes seem to have something to do with
523
av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
526
bastardized_rice_decompress(alac,
527
alac->predicterror_buffer_a,
530
alac->setinfo_rice_initialhistory,
531
alac->setinfo_rice_kmodifier,
532
ricemodifier * alac->setinfo_rice_historymult / 4,
533
(1 << alac->setinfo_rice_kmodifier) - 1);
535
if (prediction_type == 0) {
537
predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
538
alac->outputsamples_buffer_a,
541
predictor_coef_table,
543
prediction_quantitization);
545
av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type);
546
/* i think the only other prediction type (or perhaps this is just a
547
* boolean?) runs adaptive fir twice.. like:
548
* predictor_decompress_fir_adapt(predictor_error, tempout, ...)
549
* predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
555
/* not compressed, easy case */
556
if (readsamplesize <= 16) {
558
for (i = 0; i < outputsamples; i++) {
559
int32_t audiobits = get_bits(&alac->gb, readsamplesize);
561
audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
563
alac->outputsamples_buffer_a[i] = audiobits;
567
for (i = 0; i < outputsamples; i++) {
570
audiobits = get_bits(&alac->gb, 16);
571
/* special case of sign extension..
572
* as we'll be ORing the low 16bits into this */
573
audiobits = audiobits << 16;
574
audiobits = audiobits >> (32 - readsamplesize);
576
audiobits |= get_bits(&alac->gb, readsamplesize - 16);
578
alac->outputsamples_buffer_a[i] = audiobits;
581
/* wasted_bytes = 0; // unused */
584
switch(alac->setinfo_sample_size) {
587
for (i = 0; i < outputsamples; i++) {
588
int16_t sample = alac->outputsamples_buffer_a[i];
589
sample = be2me_16(sample);
590
((int16_t*)outbuffer)[i * alac->numchannels] = sample;
597
av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
604
case 1: { /* 2 channels */
611
uint8_t interlacing_shift;
612
uint8_t interlacing_leftweight;
614
/* 2^result = something to do with output waiting.
615
* perhaps matters if we read > 1 frame in a pass?
617
get_bits(&alac->gb, 4);
619
get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
621
hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
623
wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
625
isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
628
/* now read the number of samples,
629
* as a 32bit integer */
630
outputsamples = get_bits(&alac->gb, 32);
631
*outputsize = outputsamples * alac->bytespersample;
634
readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
636
if (!isnotcompressed) {
638
int16_t predictor_coef_table_a[32];
639
int predictor_coef_num_a;
640
int prediction_type_a;
641
int prediction_quantitization_a;
644
int16_t predictor_coef_table_b[32];
645
int predictor_coef_num_b;
646
int prediction_type_b;
647
int prediction_quantitization_b;
652
interlacing_shift = get_bits(&alac->gb, 8);
653
interlacing_leftweight = get_bits(&alac->gb, 8);
655
/******** channel 1 ***********/
656
prediction_type_a = get_bits(&alac->gb, 4);
657
prediction_quantitization_a = get_bits(&alac->gb, 4);
659
ricemodifier_a = get_bits(&alac->gb, 3);
660
predictor_coef_num_a = get_bits(&alac->gb, 5);
662
/* read the predictor table */
663
for (i = 0; i < predictor_coef_num_a; i++) {
664
predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16);
667
/******** channel 2 *********/
668
prediction_type_b = get_bits(&alac->gb, 4);
669
prediction_quantitization_b = get_bits(&alac->gb, 4);
671
ricemodifier_b = get_bits(&alac->gb, 3);
672
predictor_coef_num_b = get_bits(&alac->gb, 5);
674
/* read the predictor table */
675
for (i = 0; i < predictor_coef_num_b; i++) {
676
predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16);
679
/*********************/
682
av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
686
bastardized_rice_decompress(alac,
687
alac->predicterror_buffer_a,
690
alac->setinfo_rice_initialhistory,
691
alac->setinfo_rice_kmodifier,
692
ricemodifier_a * alac->setinfo_rice_historymult / 4,
693
(1 << alac->setinfo_rice_kmodifier) - 1);
695
if (prediction_type_a == 0) {
697
predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
698
alac->outputsamples_buffer_a,
701
predictor_coef_table_a,
702
predictor_coef_num_a,
703
prediction_quantitization_a);
706
av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
710
bastardized_rice_decompress(alac,
711
alac->predicterror_buffer_b,
714
alac->setinfo_rice_initialhistory,
715
alac->setinfo_rice_kmodifier,
716
ricemodifier_b * alac->setinfo_rice_historymult / 4,
717
(1 << alac->setinfo_rice_kmodifier) - 1);
719
if (prediction_type_b == 0) {
721
predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
722
alac->outputsamples_buffer_b,
725
predictor_coef_table_b,
726
predictor_coef_num_b,
727
prediction_quantitization_b);
729
av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
732
/* not compressed, easy case */
733
if (alac->setinfo_sample_size <= 16) {
735
for (i = 0; i < outputsamples; i++) {
736
int32_t audiobits_a, audiobits_b;
738
audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size);
739
audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size);
741
audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
742
audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
744
alac->outputsamples_buffer_a[i] = audiobits_a;
745
alac->outputsamples_buffer_b[i] = audiobits_b;
749
for (i = 0; i < outputsamples; i++) {
750
int32_t audiobits_a, audiobits_b;
752
audiobits_a = get_bits(&alac->gb, 16);
753
audiobits_a = audiobits_a << 16;
754
audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
755
audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
757
audiobits_b = get_bits(&alac->gb, 16);
758
audiobits_b = audiobits_b << 16;
759
audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
760
audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
762
alac->outputsamples_buffer_a[i] = audiobits_a;
763
alac->outputsamples_buffer_b[i] = audiobits_b;
766
/* wasted_bytes = 0; */
767
interlacing_shift = 0;
768
interlacing_leftweight = 0;
771
switch(alac->setinfo_sample_size) {
773
deinterlace_16(alac->outputsamples_buffer_a,
774
alac->outputsamples_buffer_b,
779
interlacing_leftweight);
785
av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
795
return input_buffer_size;
798
static int alac_decode_init(AVCodecContext * avctx)
800
ALACContext *alac = avctx->priv_data;
802
alac->context_initialized = 0;
804
alac->samplesize = alac->avctx->bits_per_sample;
805
alac->numchannels = alac->avctx->channels;
806
alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
811
static int alac_decode_close(AVCodecContext *avctx)
813
ALACContext *alac = avctx->priv_data;
815
av_free(alac->predicterror_buffer_a);
816
av_free(alac->predicterror_buffer_b);
818
av_free(alac->outputsamples_buffer_a);
819
av_free(alac->outputsamples_buffer_b);
824
AVCodec alac_decoder = {