Statistics
| Branch: | Revision:

ffmpeg / libavcodec / alac.c @ 5509bffa

History | View | Annotate | Download (27.5 KB)

1 6d6d7970 Mike Melanson
/*
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 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 6d6d7970 Mike Melanson
 */
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
37
38
#include "avcodec.h"
39 6d021b00 Mike Melanson
#include "bitstream.h"
40 6d6d7970 Mike Melanson
41
#define ALAC_EXTRADATA_SIZE 36
42
43 6d021b00 Mike Melanson
typedef struct {
44
45
    AVCodecContext *avctx;
46
    GetBitContext gb;
47
    /* init to 0; first frame decode should initialize from extradata and
48
     * set this to 1 */
49
    int context_initialized;
50 6d6d7970 Mike Melanson
51
    int samplesize;
52
    int numchannels;
53
    int bytespersample;
54
55
    /* buffers */
56
    int32_t *predicterror_buffer_a;
57
    int32_t *predicterror_buffer_b;
58
59
    int32_t *outputsamples_buffer_a;
60
    int32_t *outputsamples_buffer_b;
61
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 */
75
76
} ALACContext;
77
78 6d021b00 Mike Melanson
static void allocate_buffers(ALACContext *alac)
79 6d6d7970 Mike Melanson
{
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);
82
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);
85
}
86
87 6d021b00 Mike Melanson
void alac_set_info(ALACContext *alac)
88 6d6d7970 Mike Melanson
{
89 6d021b00 Mike Melanson
    unsigned char *ptr = alac->avctx->extradata;
90 6d6d7970 Mike Melanson
91
    ptr += 4; /* size */
92
    ptr += 4; /* alac */
93
    ptr += 4; /* 0 ? */
94
95
    alac->setinfo_max_samples_per_frame = BE_32(ptr); /* buffer size / 2 ? */
96
    ptr += 4;
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);
104
    ptr += 2;
105
    alac->setinfo_82 = BE_32(ptr);
106
    ptr += 4;
107
    alac->setinfo_86 = BE_32(ptr);
108
    ptr += 4;
109
    alac->setinfo_8a_rate = BE_32(ptr);
110
    ptr += 4;
111
112
    allocate_buffers(alac);
113
}
114
115
/* hideously inefficient. could use a bitmask search,
116
 * alternatively bsr on x86,
117
 */
118
static int count_leading_zeros(int32_t input)
119
{
120
    int i = 0;
121
    while (!(0x80000000 & input) && i < 32) {
122
        i++;
123
        input = input << 1;
124
    }
125
    return i;
126
}
127
128 6d021b00 Mike Melanson
void bastardized_rice_decompress(ALACContext *alac,
129 6d6d7970 Mike Melanson
                                 int32_t *output_buffer,
130
                                 int output_size,
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 */
136
        )
137
{
138
    int output_count;
139
    unsigned int history = rice_initialhistory;
140
    int sign_modifier = 0;
141
142
    for (output_count = 0; output_count < output_size; output_count++) {
143
        int32_t x = 0;
144
        int32_t x_modified;
145
        int32_t final_val;
146
147
        /* read x - number of 1s before 0 represent the rice */
148 6d021b00 Mike Melanson
        while (x <= 8 && get_bits1(&alac->gb)) {
149 6d6d7970 Mike Melanson
            x++;
150
        }
151
152
153
        if (x > 8) { /* RICE THRESHOLD */
154
          /* use alternative encoding */
155
            int32_t value;
156
157 6d021b00 Mike Melanson
            value = get_bits(&alac->gb, readsamplesize);
158 6d6d7970 Mike Melanson
159
            /* mask value to readsamplesize size */
160
            if (readsamplesize != 32)
161
                value &= (0xffffffff >> (32 - readsamplesize));
162
163
            x = value;
164
        } else {
165
          /* standard rice encoding */
166
            int extrabits;
167
            int k; /* size of extra bits */
168
169
            /* read k, that is bits as is */
170
            k = 31 - rice_kmodifier - count_leading_zeros((history >> 9) + 3);
171
172 115329f1 Diego Biurrun
            if (k < 0)
173 6d6d7970 Mike Melanson
                k += rice_kmodifier;
174 115329f1 Diego Biurrun
            else
175 6d6d7970 Mike Melanson
                k = rice_kmodifier;
176
177
            if (k != 1) {
178 6d021b00 Mike Melanson
                extrabits = show_bits(&alac->gb, k);
179 6d6d7970 Mike Melanson
180
                /* multiply x by 2^k - 1, as part of their strange algorithm */
181
                x = (x << k) - x;
182
183
                if (extrabits > 1) {
184
                    x += extrabits - 1;
185 6d021b00 Mike Melanson
                    get_bits(&alac->gb, k);
186
                } else {
187
                    get_bits(&alac->gb, k - 1);
188
                }
189 6d6d7970 Mike Melanson
            }
190
        }
191
192
        x_modified = sign_modifier + x;
193
        final_val = (x_modified + 1) / 2;
194
        if (x_modified & 1) final_val *= -1;
195
196
        output_buffer[output_count] = final_val;
197
198
        sign_modifier = 0;
199
200
        /* now update the history */
201
        history += (x_modified * rice_historymult)
202
                 - ((history * rice_historymult) >> 9);
203
204
        if (x_modified > 0xffff)
205
            history = 0xffff;
206
207
        /* special case: there may be compressed blocks of 0 */
208
        if ((history < 128) && (output_count+1 < output_size)) {
209
            int block_size;
210
211
            sign_modifier = 1;
212
213
            x = 0;
214 6d021b00 Mike Melanson
            while (x <= 8 && get_bits1(&alac->gb)) {
215 6d6d7970 Mike Melanson
                x++;
216
            }
217
218
            if (x > 8) {
219 6d021b00 Mike Melanson
                block_size = get_bits(&alac->gb, 16);
220 6d6d7970 Mike Melanson
                block_size &= 0xffff;
221
            } else {
222
                int k;
223
                int extrabits;
224
225
                k = count_leading_zeros(history) + ((history + 16) >> 6 /* / 64 */) - 24;
226
227 6d021b00 Mike Melanson
                extrabits = show_bits(&alac->gb, k);
228 6d6d7970 Mike Melanson
229
                block_size = (((1 << k) - 1) & rice_kmodifier_mask) * x
230
                           + extrabits - 1;
231
232
                if (extrabits < 2) {
233
                    x = 1 - extrabits;
234
                    block_size += x;
235 6d021b00 Mike Melanson
                    get_bits(&alac->gb, k - 1);
236
                } else {
237
                    get_bits(&alac->gb, k);
238 6d6d7970 Mike Melanson
                }
239
            }
240
241
            if (block_size > 0) {
242
                memset(&output_buffer[output_count+1], 0, block_size * 4);
243
                output_count += block_size;
244
245
            }
246
247
            if (block_size > 0xffff)
248
                sign_modifier = 0;
249
250
            history = 0;
251
        }
252
    }
253
}
254
255
#define SIGN_EXTENDED32(val, bits) ((val << (32 - bits)) >> (32 - bits))
256
257
#define SIGN_ONLY(v) \
258
                     ((v < 0) ? (-1) : \
259
                                ((v > 0) ? (1) : \
260
                                           (0)))
261
262
static void predictor_decompress_fir_adapt(int32_t *error_buffer,
263
                                           int32_t *buffer_out,
264
                                           int output_size,
265
                                           int readsamplesize,
266
                                           int16_t *predictor_coef_table,
267
                                           int predictor_coef_num,
268
                                           int predictor_quantitization)
269
{
270
    int i;
271
272
    /* first sample always copies */
273
    *buffer_out = *error_buffer;
274
275
    if (!predictor_coef_num) {
276
        if (output_size <= 1) return;
277
        memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
278
        return;
279
    }
280
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
284
       */
285
        if (output_size <= 1) return;
286
        for (i = 0; i < output_size - 1; i++) {
287
            int32_t prev_value;
288
            int32_t error_value;
289
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);
293
        }
294
        return;
295
    }
296
297
    /* read warm-up samples */
298
    if (predictor_coef_num > 0) {
299
        int i;
300
        for (i = 0; i < predictor_coef_num; i++) {
301
            int32_t val;
302
303
            val = buffer_out[i] + error_buffer[i+1];
304
305
            val = SIGN_EXTENDED32(val, readsamplesize);
306
307
            buffer_out[i+1] = val;
308
        }
309
    }
310
311
#if 0
312
    /* 4 and 8 are very common cases (the only ones i've seen). these
313
     * should be unrolled and optimised
314
     */
315
    if (predictor_coef_num == 4) {
316
        /* FIXME: optimised general case */
317
        return;
318
    }
319

320
    if (predictor_coef_table == 8) {
321
        /* FIXME: optimised general case */
322
        return;
323
    }
324
#endif
325
326
327
    /* general case */
328
    if (predictor_coef_num > 0) {
329
        for (i = predictor_coef_num + 1;
330
             i < output_size;
331
             i++) {
332
            int j;
333
            int sum = 0;
334
            int outval;
335
            int error_val = error_buffer[i];
336
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];
340
            }
341
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);
346
347
            buffer_out[predictor_coef_num+1] = outval;
348
349
            if (error_val > 0) {
350
                int predictor_num = predictor_coef_num - 1;
351
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);
355
356
                    predictor_coef_table[predictor_num] -= sign;
357
358
                    val *= sign; /* absolute value */
359
360
                    error_val -= ((val >> predictor_quantitization) *
361
                                  (predictor_coef_num - predictor_num));
362
363
                    predictor_num--;
364
                }
365
            } else if (error_val < 0) {
366
                int predictor_num = predictor_coef_num - 1;
367
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);
371
372
                    predictor_coef_table[predictor_num] -= sign;
373
374
                    val *= sign; /* neg value */
375
376
                    error_val -= ((val >> predictor_quantitization) *
377
                                  (predictor_coef_num - predictor_num));
378
379
                    predictor_num--;
380
                }
381
            }
382
383
            buffer_out++;
384
        }
385
    }
386
}
387
388
void deinterlace_16(int32_t *buffer_a, int32_t *buffer_b,
389
                    int16_t *buffer_out,
390
                    int numchannels, int numsamples,
391
                    uint8_t interlacing_shift,
392 7ff85a81 Mike Melanson
                    uint8_t interlacing_leftweight)
393
{
394 6d6d7970 Mike Melanson
    int i;
395
    if (numsamples <= 0) return;
396
397
    /* weighted interlacing */
398
    if (interlacing_leftweight) {
399
        for (i = 0; i < numsamples; i++) {
400
            int32_t difference, midright;
401
            int16_t left;
402
            int16_t right;
403
404
            midright = buffer_a[i];
405
            difference = buffer_b[i];
406
407
408
            right = midright - ((difference * interlacing_leftweight) >> interlacing_shift);
409
            left = (midright - ((difference * interlacing_leftweight) >> interlacing_shift))
410
                 + difference;
411
412
            buffer_out[i*numchannels] = left;
413
            buffer_out[i*numchannels + 1] = right;
414
        }
415
416
        return;
417
    }
418
419
    /* otherwise basic interlacing took place */
420
    for (i = 0; i < numsamples; i++) {
421
        int16_t left, right;
422
423
        left = buffer_a[i];
424
        right = buffer_b[i];
425
426
        buffer_out[i*numchannels] = left;
427
        buffer_out[i*numchannels + 1] = right;
428
    }
429
}
430
431 f770ee03 Mike Melanson
static int alac_decode_frame(AVCodecContext *avctx,
432
                             void *outbuffer, int *outputsize,
433
                             uint8_t *inbuffer, int input_buffer_size)
434 7ff85a81 Mike Melanson
{
435 6d021b00 Mike Melanson
    ALACContext *alac = avctx->priv_data;
436 f770ee03 Mike Melanson
437 6d6d7970 Mike Melanson
    int channels;
438 7ff85a81 Mike Melanson
    int32_t outputsamples;
439 6d6d7970 Mike Melanson
440 f770ee03 Mike Melanson
    /* short-circuit null buffers */
441
    if (!inbuffer || !input_buffer_size)
442
        return input_buffer_size;
443
444 6d6d7970 Mike Melanson
    /* initialize from the extradata */
445 6d021b00 Mike Melanson
    if (!alac->context_initialized) {
446
        if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
447 115329f1 Diego Biurrun
            av_log(NULL, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
448 6d6d7970 Mike Melanson
                ALAC_EXTRADATA_SIZE);
449
            return input_buffer_size;
450
        }
451 6d021b00 Mike Melanson
        alac_set_info(alac);
452
        alac->context_initialized = 1;
453 6d6d7970 Mike Melanson
    }
454 7ff85a81 Mike Melanson
455
    outputsamples = alac->setinfo_max_samples_per_frame;
456 6d6d7970 Mike Melanson
457 6d021b00 Mike Melanson
    init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
458 6d6d7970 Mike Melanson
459 6d021b00 Mike Melanson
    channels = get_bits(&alac->gb, 3);
460 6d6d7970 Mike Melanson
461
    *outputsize = outputsamples * alac->bytespersample;
462
463
    switch(channels) {
464
    case 0: { /* 1 channel */
465
        int hassize;
466
        int isnotcompressed;
467
        int readsamplesize;
468
469
        int wasted_bytes;
470
        int ricemodifier;
471
472
473
        /* 2^result = something to do with output waiting.
474
         * perhaps matters if we read > 1 frame in a pass?
475
         */
476 6d021b00 Mike Melanson
        get_bits(&alac->gb, 4);
477 6d6d7970 Mike Melanson
478 6d021b00 Mike Melanson
        get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
479 6d6d7970 Mike Melanson
480 6d021b00 Mike Melanson
        hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
481 6d6d7970 Mike Melanson
482 6d021b00 Mike Melanson
        wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
483 6d6d7970 Mike Melanson
484 6d021b00 Mike Melanson
        isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
485 6d6d7970 Mike Melanson
486
        if (hassize) {
487
            /* now read the number of samples,
488
             * as a 32bit integer */
489 6d021b00 Mike Melanson
            outputsamples = get_bits(&alac->gb, 32);
490 6d6d7970 Mike Melanson
            *outputsize = outputsamples * alac->bytespersample;
491
        }
492
493
        readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8);
494
495
        if (!isnotcompressed) {
496
         /* so it is compressed */
497
            int16_t predictor_coef_table[32];
498
            int predictor_coef_num;
499
            int prediction_type;
500
            int prediction_quantitization;
501
            int i;
502
503
            /* skip 16 bits, not sure what they are. seem to be used in
504
             * two channel case */
505 6d021b00 Mike Melanson
            get_bits(&alac->gb, 8);
506
            get_bits(&alac->gb, 8);
507 6d6d7970 Mike Melanson
508 6d021b00 Mike Melanson
            prediction_type = get_bits(&alac->gb, 4);
509
            prediction_quantitization = get_bits(&alac->gb, 4);
510 6d6d7970 Mike Melanson
511 6d021b00 Mike Melanson
            ricemodifier = get_bits(&alac->gb, 3);
512
            predictor_coef_num = get_bits(&alac->gb, 5);
513 6d6d7970 Mike Melanson
514
            /* read the predictor table */
515
            for (i = 0; i < predictor_coef_num; i++) {
516 6d021b00 Mike Melanson
                predictor_coef_table[i] = (int16_t)get_bits(&alac->gb, 16);
517 6d6d7970 Mike Melanson
            }
518
519
            if (wasted_bytes) {
520
                /* these bytes seem to have something to do with
521
                 * > 2 channel files.
522
                 */
523
                av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
524
            }
525
526
            bastardized_rice_decompress(alac,
527
                                        alac->predicterror_buffer_a,
528
                                        outputsamples,
529
                                        readsamplesize,
530
                                        alac->setinfo_rice_initialhistory,
531
                                        alac->setinfo_rice_kmodifier,
532
                                        ricemodifier * alac->setinfo_rice_historymult / 4,
533
                                        (1 << alac->setinfo_rice_kmodifier) - 1);
534
535
            if (prediction_type == 0) {
536
              /* adaptive fir */
537
                predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
538
                                               alac->outputsamples_buffer_a,
539
                                               outputsamples,
540
                                               readsamplesize,
541
                                               predictor_coef_table,
542
                                               predictor_coef_num,
543
                                               prediction_quantitization);
544
            } else {
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 ...)
550
                 * little strange..
551
                 */
552
            }
553
554
        } else {
555
          /* not compressed, easy case */
556
            if (readsamplesize <= 16) {
557
                int i;
558
                for (i = 0; i < outputsamples; i++) {
559 6d021b00 Mike Melanson
                    int32_t audiobits = get_bits(&alac->gb, readsamplesize);
560 6d6d7970 Mike Melanson
561
                    audiobits = SIGN_EXTENDED32(audiobits, readsamplesize);
562
563
                    alac->outputsamples_buffer_a[i] = audiobits;
564
                }
565
            } else {
566
                int i;
567
                for (i = 0; i < outputsamples; i++) {
568
                    int32_t audiobits;
569
570 6d021b00 Mike Melanson
                    audiobits = get_bits(&alac->gb, 16);
571 6d6d7970 Mike Melanson
                    /* 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);
575
576 6d021b00 Mike Melanson
                    audiobits |= get_bits(&alac->gb, readsamplesize - 16);
577 6d6d7970 Mike Melanson
578
                    alac->outputsamples_buffer_a[i] = audiobits;
579
                }
580
            }
581
            /* wasted_bytes = 0; // unused */
582
        }
583
584
        switch(alac->setinfo_sample_size) {
585
        case 16: {
586
            int i;
587
            for (i = 0; i < outputsamples; i++) {
588
                int16_t sample = alac->outputsamples_buffer_a[i];
589 41a33f51 Alex Beregszaszi
                sample = be2me_16(sample);
590 6d6d7970 Mike Melanson
                ((int16_t*)outbuffer)[i * alac->numchannels] = sample;
591
            }
592
            break;
593
        }
594
        case 20:
595
        case 24:
596
        case 32:
597
            av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
598
            break;
599
        default:
600
            break;
601
        }
602
        break;
603
    }
604
    case 1: { /* 2 channels */
605
        int hassize;
606
        int isnotcompressed;
607
        int readsamplesize;
608
609
        int wasted_bytes;
610
611
        uint8_t interlacing_shift;
612
        uint8_t interlacing_leftweight;
613
614
        /* 2^result = something to do with output waiting.
615
         * perhaps matters if we read > 1 frame in a pass?
616
         */
617 6d021b00 Mike Melanson
        get_bits(&alac->gb, 4);
618 6d6d7970 Mike Melanson
619 6d021b00 Mike Melanson
        get_bits(&alac->gb, 12); /* unknown, skip 12 bits */
620 6d6d7970 Mike Melanson
621 6d021b00 Mike Melanson
        hassize = get_bits(&alac->gb, 1); /* the output sample size is stored soon */
622 6d6d7970 Mike Melanson
623 6d021b00 Mike Melanson
        wasted_bytes = get_bits(&alac->gb, 2); /* unknown ? */
624 6d6d7970 Mike Melanson
625 6d021b00 Mike Melanson
        isnotcompressed = get_bits(&alac->gb, 1); /* whether the frame is compressed */
626 6d6d7970 Mike Melanson
627
        if (hassize) {
628
            /* now read the number of samples,
629
             * as a 32bit integer */
630 6d021b00 Mike Melanson
            outputsamples = get_bits(&alac->gb, 32);
631 6d6d7970 Mike Melanson
            *outputsize = outputsamples * alac->bytespersample;
632
        }
633
634
        readsamplesize = alac->setinfo_sample_size - (wasted_bytes * 8) + 1;
635
636
        if (!isnotcompressed) {
637
         /* compressed */
638
            int16_t predictor_coef_table_a[32];
639
            int predictor_coef_num_a;
640
            int prediction_type_a;
641
            int prediction_quantitization_a;
642
            int ricemodifier_a;
643
644
            int16_t predictor_coef_table_b[32];
645
            int predictor_coef_num_b;
646
            int prediction_type_b;
647
            int prediction_quantitization_b;
648
            int ricemodifier_b;
649
650
            int i;
651
652 6d021b00 Mike Melanson
            interlacing_shift = get_bits(&alac->gb, 8);
653
            interlacing_leftweight = get_bits(&alac->gb, 8);
654 6d6d7970 Mike Melanson
655
            /******** channel 1 ***********/
656 6d021b00 Mike Melanson
            prediction_type_a = get_bits(&alac->gb, 4);
657
            prediction_quantitization_a = get_bits(&alac->gb, 4);
658 6d6d7970 Mike Melanson
659 6d021b00 Mike Melanson
            ricemodifier_a = get_bits(&alac->gb, 3);
660
            predictor_coef_num_a = get_bits(&alac->gb, 5);
661 6d6d7970 Mike Melanson
662
            /* read the predictor table */
663
            for (i = 0; i < predictor_coef_num_a; i++) {
664 6d021b00 Mike Melanson
                predictor_coef_table_a[i] = (int16_t)get_bits(&alac->gb, 16);
665 6d6d7970 Mike Melanson
            }
666
667
            /******** channel 2 *********/
668 6d021b00 Mike Melanson
            prediction_type_b = get_bits(&alac->gb, 4);
669
            prediction_quantitization_b = get_bits(&alac->gb, 4);
670 6d6d7970 Mike Melanson
671 6d021b00 Mike Melanson
            ricemodifier_b = get_bits(&alac->gb, 3);
672
            predictor_coef_num_b = get_bits(&alac->gb, 5);
673 6d6d7970 Mike Melanson
674
            /* read the predictor table */
675
            for (i = 0; i < predictor_coef_num_b; i++) {
676 6d021b00 Mike Melanson
                predictor_coef_table_b[i] = (int16_t)get_bits(&alac->gb, 16);
677 6d6d7970 Mike Melanson
            }
678
679
            /*********************/
680
            if (wasted_bytes) {
681
              /* see mono case */
682
                av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented, unhandling of wasted_bytes\n");
683
            }
684
685
            /* channel 1 */
686
            bastardized_rice_decompress(alac,
687
                                        alac->predicterror_buffer_a,
688
                                        outputsamples,
689
                                        readsamplesize,
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);
694
695
            if (prediction_type_a == 0) {
696
              /* adaptive fir */
697
                predictor_decompress_fir_adapt(alac->predicterror_buffer_a,
698
                                               alac->outputsamples_buffer_a,
699
                                               outputsamples,
700
                                               readsamplesize,
701
                                               predictor_coef_table_a,
702
                                               predictor_coef_num_a,
703
                                               prediction_quantitization_a);
704
            } else {
705
              /* see mono case */
706
                av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_a);
707
            }
708
709
            /* channel 2 */
710
            bastardized_rice_decompress(alac,
711
                                        alac->predicterror_buffer_b,
712
                                        outputsamples,
713
                                        readsamplesize,
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);
718
719
            if (prediction_type_b == 0) {
720
              /* adaptive fir */
721
                predictor_decompress_fir_adapt(alac->predicterror_buffer_b,
722
                                               alac->outputsamples_buffer_b,
723
                                               outputsamples,
724
                                               readsamplesize,
725
                                               predictor_coef_table_b,
726
                                               predictor_coef_num_b,
727
                                               prediction_quantitization_b);
728
            } else {
729
                av_log(NULL, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type_b);
730
            }
731 115329f1 Diego Biurrun
        } else {
732 6d6d7970 Mike Melanson
         /* not compressed, easy case */
733
            if (alac->setinfo_sample_size <= 16) {
734
                int i;
735
                for (i = 0; i < outputsamples; i++) {
736
                    int32_t audiobits_a, audiobits_b;
737
738 6d021b00 Mike Melanson
                    audiobits_a = get_bits(&alac->gb, alac->setinfo_sample_size);
739
                    audiobits_b = get_bits(&alac->gb, alac->setinfo_sample_size);
740 6d6d7970 Mike Melanson
741
                    audiobits_a = SIGN_EXTENDED32(audiobits_a, alac->setinfo_sample_size);
742
                    audiobits_b = SIGN_EXTENDED32(audiobits_b, alac->setinfo_sample_size);
743
744
                    alac->outputsamples_buffer_a[i] = audiobits_a;
745
                    alac->outputsamples_buffer_b[i] = audiobits_b;
746
                }
747
            } else {
748
                int i;
749
                for (i = 0; i < outputsamples; i++) {
750
                    int32_t audiobits_a, audiobits_b;
751
752 6d021b00 Mike Melanson
                    audiobits_a = get_bits(&alac->gb, 16);
753 6d6d7970 Mike Melanson
                    audiobits_a = audiobits_a << 16;
754
                    audiobits_a = audiobits_a >> (32 - alac->setinfo_sample_size);
755 6d021b00 Mike Melanson
                    audiobits_a |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
756 6d6d7970 Mike Melanson
757 6d021b00 Mike Melanson
                    audiobits_b = get_bits(&alac->gb, 16);
758 6d6d7970 Mike Melanson
                    audiobits_b = audiobits_b << 16;
759
                    audiobits_b = audiobits_b >> (32 - alac->setinfo_sample_size);
760 6d021b00 Mike Melanson
                    audiobits_b |= get_bits(&alac->gb, alac->setinfo_sample_size - 16);
761 6d6d7970 Mike Melanson
762
                    alac->outputsamples_buffer_a[i] = audiobits_a;
763
                    alac->outputsamples_buffer_b[i] = audiobits_b;
764
                }
765
            }
766
            /* wasted_bytes = 0; */
767
            interlacing_shift = 0;
768
            interlacing_leftweight = 0;
769
        }
770
771
        switch(alac->setinfo_sample_size) {
772
        case 16: {
773
            deinterlace_16(alac->outputsamples_buffer_a,
774
                           alac->outputsamples_buffer_b,
775
                           (int16_t*)outbuffer,
776
                           alac->numchannels,
777
                           outputsamples,
778
                           interlacing_shift,
779
                           interlacing_leftweight);
780
            break;
781
        }
782
        case 20:
783
        case 24:
784
        case 32:
785
            av_log(NULL, AV_LOG_ERROR, "FIXME: unimplemented sample size %i\n", alac->setinfo_sample_size);
786
            break;
787
        default:
788
            break;
789
        }
790
791
        break;
792
    }
793
    }
794
795 f770ee03 Mike Melanson
    return input_buffer_size;
796 6d6d7970 Mike Melanson
}
797
798
static int alac_decode_init(AVCodecContext * avctx)
799
{
800 6d021b00 Mike Melanson
    ALACContext *alac = avctx->priv_data;
801
    alac->avctx = avctx;
802
    alac->context_initialized = 0;
803 6d6d7970 Mike Melanson
804 6d021b00 Mike Melanson
    alac->samplesize = alac->avctx->bits_per_sample;
805
    alac->numchannels = alac->avctx->channels;
806
    alac->bytespersample = (alac->samplesize / 8) * alac->numchannels;
807 6d6d7970 Mike Melanson
808
    return 0;
809
}
810
811
static int alac_decode_close(AVCodecContext *avctx)
812
{
813 6d021b00 Mike Melanson
    ALACContext *alac = avctx->priv_data;
814 6d6d7970 Mike Melanson
815 6d021b00 Mike Melanson
    av_free(alac->predicterror_buffer_a);
816
    av_free(alac->predicterror_buffer_b);
817 6d6d7970 Mike Melanson
818 6d021b00 Mike Melanson
    av_free(alac->outputsamples_buffer_a);
819
    av_free(alac->outputsamples_buffer_b);
820 6d6d7970 Mike Melanson
821
    return 0;
822
}
823
824
AVCodec alac_decoder = {
825
    "alac",
826
    CODEC_TYPE_AUDIO,
827
    CODEC_ID_ALAC,
828
    sizeof(ALACContext),
829
    alac_decode_init,
830
    NULL,
831
    alac_decode_close,
832
    alac_decode_frame,
833
};