Statistics
| Branch: | Revision:

ffmpeg / libavcodec / alac.c @ 313b52fb

History | View | Annotate | Download (22.7 KB)

1 6d6d7970 Mike Melanson
/*
2
 * ALAC (Apple Lossless Audio Codec) decoder
3
 * Copyright (c) 2005 David Hammerton
4
 *
5 b78e7197 Diego Biurrun
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8 6d6d7970 Mike Melanson
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10 b78e7197 Diego Biurrun
 * version 2.1 of the License, or (at your option) any later version.
11 6d6d7970 Mike Melanson
 *
12 b78e7197 Diego Biurrun
 * FFmpeg is distributed in the hope that it will be useful,
13 6d6d7970 Mike Melanson
 * 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 b78e7197 Diego Biurrun
 * License along with FFmpeg; if not, write to the Free Software
19 5509bffa Diego Biurrun
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 6d6d7970 Mike Melanson
 */
21
22
/**
23 ba87f080 Diego Biurrun
 * @file
24 6d6d7970 Mike Melanson
 * ALAC (Apple Lossless Audio Codec) decoder
25
 * @author 2005 David Hammerton
26
 *
27
 * For more information on the ALAC format, visit:
28
 *  http://crazney.net/programs/itunes/alac.html
29
 *
30
 * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
31
 * passed through the extradata[_size] fields. This atom is tacked onto
32
 * the end of an 'alac' stsd atom and has the following format:
33
 *  bytes 0-3   atom size (0x24), big-endian
34
 *  bytes 4-7   atom type ('alac', not the 'alac' tag from start of stsd)
35
 *  bytes 8-35  data bytes needed by decoder
36 a1db1fc4 Alex Beregszaszi
 *
37
 * Extradata:
38
 * 32bit  size
39
 * 32bit  tag (=alac)
40
 * 32bit  zero?
41
 * 32bit  max sample per frame
42
 *  8bit  ?? (zero?)
43
 *  8bit  sample size
44
 *  8bit  history mult
45
 *  8bit  initial history
46
 *  8bit  kmodifier
47
 *  8bit  channels?
48
 * 16bit  ??
49
 * 32bit  max coded frame size
50
 * 32bit  bitrate?
51
 * 32bit  samplerate
52 6d6d7970 Mike Melanson
 */
53
54
55
#include "avcodec.h"
56 9106a698 Stefano Sabatini
#include "get_bits.h"
57 f79488d4 Vitor Sessak
#include "bytestream.h"
58 becc0ef9 Vitor Sessak
#include "unary.h"
59 8858990f Måns Rullgård
#include "mathops.h"
60 6d6d7970 Mike Melanson
61
#define ALAC_EXTRADATA_SIZE 36
62 153696a6 Vitor Sessak
#define MAX_CHANNELS 2
63 6d6d7970 Mike Melanson
64 6d021b00 Mike Melanson
typedef struct {
65
66
    AVCodecContext *avctx;
67
    GetBitContext gb;
68 6d6d7970 Mike Melanson
69
    int numchannels;
70
    int bytespersample;
71
72
    /* buffers */
73 153696a6 Vitor Sessak
    int32_t *predicterror_buffer[MAX_CHANNELS];
74 6d6d7970 Mike Melanson
75 153696a6 Vitor Sessak
    int32_t *outputsamples_buffer[MAX_CHANNELS];
76 6d6d7970 Mike Melanson
77 f430c7b6 Jai Menon
    int32_t *wasted_bits_buffer[MAX_CHANNELS];
78
79 6d6d7970 Mike Melanson
    /* stuff from setinfo */
80
    uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */    /* max samples per frame? */
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
    /* end setinfo stuff */
86
87 f430c7b6 Jai Menon
    int wasted_bits;
88 6d6d7970 Mike Melanson
} ALACContext;
89
90 6d021b00 Mike Melanson
static void allocate_buffers(ALACContext *alac)
91 6d6d7970 Mike Melanson
{
92 153696a6 Vitor Sessak
    int chan;
93
    for (chan = 0; chan < MAX_CHANNELS; chan++) {
94
        alac->predicterror_buffer[chan] =
95
            av_malloc(alac->setinfo_max_samples_per_frame * 4);
96 6d6d7970 Mike Melanson
97 153696a6 Vitor Sessak
        alac->outputsamples_buffer[chan] =
98
            av_malloc(alac->setinfo_max_samples_per_frame * 4);
99 f430c7b6 Jai Menon
100
        alac->wasted_bits_buffer[chan] = av_malloc(alac->setinfo_max_samples_per_frame * 4);
101 153696a6 Vitor Sessak
    }
102 6d6d7970 Mike Melanson
}
103
104 3a1a7e32 Michael Niedermayer
static int alac_set_info(ALACContext *alac)
105 6d6d7970 Mike Melanson
{
106 b5777b94 Michael Niedermayer
    const unsigned char *ptr = alac->avctx->extradata;
107 6d6d7970 Mike Melanson
108
    ptr += 4; /* size */
109
    ptr += 4; /* alac */
110
    ptr += 4; /* 0 ? */
111
112 fead30d4 Alex Beregszaszi
    if(AV_RB32(ptr) >= UINT_MAX/4){
113 3a1a7e32 Michael Niedermayer
        av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
114
        return -1;
115
    }
116 f79488d4 Vitor Sessak
117
    /* buffer size / 2 ? */
118
    alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
119 4c63c597 Vitor Sessak
    ptr++;                          /* ??? */
120 a1301f29 Vitor Sessak
    alac->setinfo_sample_size           = *ptr++;
121 e5838c90 Matthieu Castet
    if (alac->setinfo_sample_size > 32) {
122
        av_log(alac->avctx, AV_LOG_ERROR, "setinfo_sample_size too large\n");
123
        return -1;
124
    }
125 a1301f29 Vitor Sessak
    alac->setinfo_rice_historymult      = *ptr++;
126
    alac->setinfo_rice_initialhistory   = *ptr++;
127
    alac->setinfo_rice_kmodifier        = *ptr++;
128 4c63c597 Vitor Sessak
    ptr++;                         /* channels? */
129 db08882e Vitor Sessak
    bytestream_get_be16(&ptr);      /* ??? */
130
    bytestream_get_be32(&ptr);      /* max coded frame size */
131
    bytestream_get_be32(&ptr);      /* bitrate ? */
132 4c63c597 Vitor Sessak
    bytestream_get_be32(&ptr);      /* samplerate */
133 6d6d7970 Mike Melanson
134
    allocate_buffers(alac);
135 3a1a7e32 Michael Niedermayer
136
    return 0;
137 6d6d7970 Mike Melanson
}
138
139 5eeba07e Michael Niedermayer
static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
140
    /* read x - number of 1s before 0 represent the rice */
141
    int x = get_unary_0_9(gb);
142
143
    if (x > 8) { /* RICE THRESHOLD */
144
        /* use alternative encoding */
145
        x = get_bits(gb, readsamplesize);
146
    } else {
147 1f655c4f Michael Niedermayer
        if (k >= limit)
148
            k = limit;
149 5a5a27c5 Michael Niedermayer
150 1f655c4f Michael Niedermayer
        if (k != 1) {
151
            int extrabits = show_bits(gb, k);
152 5a5a27c5 Michael Niedermayer
153 1f655c4f Michael Niedermayer
            /* multiply x by 2^k - 1, as part of their strange algorithm */
154
            x = (x << k) - x;
155 5a5a27c5 Michael Niedermayer
156 1f655c4f Michael Niedermayer
            if (extrabits > 1) {
157
                x += extrabits - 1;
158
                skip_bits(gb, k);
159
            } else
160
                skip_bits(gb, k - 1);
161
        }
162 5eeba07e Michael Niedermayer
    }
163 5a5a27c5 Michael Niedermayer
    return x;
164
}
165
166 1b47fafd Dieter
static void bastardized_rice_decompress(ALACContext *alac,
167 6d6d7970 Mike Melanson
                                 int32_t *output_buffer,
168
                                 int output_size,
169
                                 int readsamplesize, /* arg_10 */
170
                                 int rice_initialhistory, /* arg424->b */
171
                                 int rice_kmodifier, /* arg424->d */
172
                                 int rice_historymult, /* arg424->c */
173
                                 int rice_kmodifier_mask /* arg424->e */
174
        )
175
{
176
    int output_count;
177
    unsigned int history = rice_initialhistory;
178
    int sign_modifier = 0;
179
180
    for (output_count = 0; output_count < output_size; output_count++) {
181 becc0ef9 Vitor Sessak
        int32_t x;
182 6d6d7970 Mike Melanson
        int32_t x_modified;
183
        int32_t final_val;
184
185 1f655c4f Michael Niedermayer
        /* standard rice encoding */
186
        int k; /* size of extra bits */
187 6d6d7970 Mike Melanson
188 1f655c4f Michael Niedermayer
        /* read k, that is bits as is */
189 8431603a Vitor Sessak
        k = av_log2((history >> 9) + 3);
190 1f655c4f Michael Niedermayer
        x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
191 6d6d7970 Mike Melanson
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 10024d44 Vitor Sessak
        history += x_modified * rice_historymult
202
                   - ((history * rice_historymult) >> 9);
203 6d6d7970 Mike Melanson
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 83e9a67d Michael Niedermayer
            int k;
210
            unsigned int block_size;
211 6d6d7970 Mike Melanson
212
            sign_modifier = 1;
213
214 8431603a Vitor Sessak
            k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
215 6d6d7970 Mike Melanson
216 1f655c4f Michael Niedermayer
            block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
217 6d6d7970 Mike Melanson
218
            if (block_size > 0) {
219 83e9a67d Michael Niedermayer
                if(block_size >= output_size - output_count){
220
                    av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
221
                    block_size= output_size - output_count - 1;
222
                }
223 6d6d7970 Mike Melanson
                memset(&output_buffer[output_count+1], 0, block_size * 4);
224
                output_count += block_size;
225
            }
226
227
            if (block_size > 0xffff)
228
                sign_modifier = 0;
229
230
            history = 0;
231
        }
232
    }
233
}
234
235 321c3138 Vitor Sessak
static inline int sign_only(int v)
236
{
237
    return v ? FFSIGN(v) : 0;
238
}
239 6d6d7970 Mike Melanson
240
static void predictor_decompress_fir_adapt(int32_t *error_buffer,
241
                                           int32_t *buffer_out,
242
                                           int output_size,
243
                                           int readsamplesize,
244
                                           int16_t *predictor_coef_table,
245
                                           int predictor_coef_num,
246
                                           int predictor_quantitization)
247
{
248
    int i;
249
250
    /* first sample always copies */
251
    *buffer_out = *error_buffer;
252
253
    if (!predictor_coef_num) {
254 10024d44 Vitor Sessak
        if (output_size <= 1)
255
            return;
256
257 6d6d7970 Mike Melanson
        memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
258
        return;
259
    }
260
261
    if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
262
      /* second-best case scenario for fir decompression,
263
       * error describes a small difference from the previous sample only
264
       */
265 10024d44 Vitor Sessak
        if (output_size <= 1)
266
            return;
267 6d6d7970 Mike Melanson
        for (i = 0; i < output_size - 1; i++) {
268
            int32_t prev_value;
269
            int32_t error_value;
270
271
            prev_value = buffer_out[i];
272
            error_value = error_buffer[i+1];
273 10024d44 Vitor Sessak
            buffer_out[i+1] =
274 8858990f Måns Rullgård
                sign_extend((prev_value + error_value), readsamplesize);
275 6d6d7970 Mike Melanson
        }
276
        return;
277
    }
278
279
    /* read warm-up samples */
280 10024d44 Vitor Sessak
    if (predictor_coef_num > 0)
281 6d6d7970 Mike Melanson
        for (i = 0; i < predictor_coef_num; i++) {
282
            int32_t val;
283
284
            val = buffer_out[i] + error_buffer[i+1];
285 8858990f Måns Rullgård
            val = sign_extend(val, readsamplesize);
286 6d6d7970 Mike Melanson
            buffer_out[i+1] = val;
287
        }
288
289
#if 0
290
    /* 4 and 8 are very common cases (the only ones i've seen). these
291 52b541ad Vitor Sessak
     * should be unrolled and optimized
292 6d6d7970 Mike Melanson
     */
293
    if (predictor_coef_num == 4) {
294 52b541ad Vitor Sessak
        /* FIXME: optimized general case */
295 6d6d7970 Mike Melanson
        return;
296
    }
297

298
    if (predictor_coef_table == 8) {
299 52b541ad Vitor Sessak
        /* FIXME: optimized general case */
300 6d6d7970 Mike Melanson
        return;
301
    }
302
#endif
303
304
    /* general case */
305
    if (predictor_coef_num > 0) {
306 10024d44 Vitor Sessak
        for (i = predictor_coef_num + 1; i < output_size; i++) {
307 6d6d7970 Mike Melanson
            int j;
308
            int sum = 0;
309
            int outval;
310
            int error_val = error_buffer[i];
311
312
            for (j = 0; j < predictor_coef_num; j++) {
313
                sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
314
                       predictor_coef_table[j];
315
            }
316
317
            outval = (1 << (predictor_quantitization-1)) + sum;
318
            outval = outval >> predictor_quantitization;
319
            outval = outval + buffer_out[0] + error_val;
320 8858990f Måns Rullgård
            outval = sign_extend(outval, readsamplesize);
321 6d6d7970 Mike Melanson
322
            buffer_out[predictor_coef_num+1] = outval;
323
324
            if (error_val > 0) {
325
                int predictor_num = predictor_coef_num - 1;
326
327
                while (predictor_num >= 0 && error_val > 0) {
328
                    int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
329 321c3138 Vitor Sessak
                    int sign = sign_only(val);
330 6d6d7970 Mike Melanson
331
                    predictor_coef_table[predictor_num] -= sign;
332
333
                    val *= sign; /* absolute value */
334
335
                    error_val -= ((val >> predictor_quantitization) *
336
                                  (predictor_coef_num - predictor_num));
337
338
                    predictor_num--;
339
                }
340
            } else if (error_val < 0) {
341
                int predictor_num = predictor_coef_num - 1;
342
343
                while (predictor_num >= 0 && error_val < 0) {
344
                    int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
345 321c3138 Vitor Sessak
                    int sign = - sign_only(val);
346 6d6d7970 Mike Melanson
347
                    predictor_coef_table[predictor_num] -= sign;
348
349
                    val *= sign; /* neg value */
350
351
                    error_val -= ((val >> predictor_quantitization) *
352
                                  (predictor_coef_num - predictor_num));
353
354
                    predictor_num--;
355
                }
356
            }
357
358
            buffer_out++;
359
        }
360
    }
361
}
362
363 4a5e6389 Vitor Sessak
static void reconstruct_stereo_16(int32_t *buffer[MAX_CHANNELS],
364 db695867 Vitor Sessak
                                  int16_t *buffer_out,
365
                                  int numchannels, int numsamples,
366
                                  uint8_t interlacing_shift,
367
                                  uint8_t interlacing_leftweight)
368 7ff85a81 Mike Melanson
{
369 6d6d7970 Mike Melanson
    int i;
370 10024d44 Vitor Sessak
    if (numsamples <= 0)
371
        return;
372 6d6d7970 Mike Melanson
373
    /* weighted interlacing */
374
    if (interlacing_leftweight) {
375
        for (i = 0; i < numsamples; i++) {
376 9c8d9f25 Vitor Sessak
            int32_t a, b;
377 6d6d7970 Mike Melanson
378 9c8d9f25 Vitor Sessak
            a = buffer[0][i];
379
            b = buffer[1][i];
380 6d6d7970 Mike Melanson
381 9c8d9f25 Vitor Sessak
            a -= (b * interlacing_leftweight) >> interlacing_shift;
382
            b += a;
383 6d6d7970 Mike Melanson
384 9c8d9f25 Vitor Sessak
            buffer_out[i*numchannels] = b;
385
            buffer_out[i*numchannels + 1] = a;
386 6d6d7970 Mike Melanson
        }
387
388
        return;
389
    }
390
391
    /* otherwise basic interlacing took place */
392
    for (i = 0; i < numsamples; i++) {
393
        int16_t left, right;
394
395 c9128d56 Vitor Sessak
        left = buffer[0][i];
396
        right = buffer[1][i];
397 6d6d7970 Mike Melanson
398
        buffer_out[i*numchannels] = left;
399
        buffer_out[i*numchannels + 1] = right;
400
    }
401
}
402
403 f430c7b6 Jai Menon
static void decorrelate_stereo_24(int32_t *buffer[MAX_CHANNELS],
404
                                  int32_t *buffer_out,
405
                                  int32_t *wasted_bits_buffer[MAX_CHANNELS],
406
                                  int wasted_bits,
407
                                  int numchannels, int numsamples,
408
                                  uint8_t interlacing_shift,
409
                                  uint8_t interlacing_leftweight)
410
{
411
    int i;
412
413
    if (numsamples <= 0)
414
        return;
415
416
    /* weighted interlacing */
417
    if (interlacing_leftweight) {
418
        for (i = 0; i < numsamples; i++) {
419
            int32_t a, b;
420
421
            a = buffer[0][i];
422
            b = buffer[1][i];
423
424
            a -= (b * interlacing_leftweight) >> interlacing_shift;
425
            b += a;
426
427
            if (wasted_bits) {
428
                b  = (b  << wasted_bits) | wasted_bits_buffer[0][i];
429
                a  = (a  << wasted_bits) | wasted_bits_buffer[1][i];
430
            }
431
432
            buffer_out[i * numchannels]     = b << 8;
433
            buffer_out[i * numchannels + 1] = a << 8;
434
        }
435
    } else {
436
        for (i = 0; i < numsamples; i++) {
437
            int32_t left, right;
438
439
            left  = buffer[0][i];
440
            right = buffer[1][i];
441
442
            if (wasted_bits) {
443
                left   = (left   << wasted_bits) | wasted_bits_buffer[0][i];
444
                right  = (right  << wasted_bits) | wasted_bits_buffer[1][i];
445
            }
446
447
            buffer_out[i * numchannels]     = left  << 8;
448
            buffer_out[i * numchannels + 1] = right << 8;
449
        }
450
    }
451
}
452
453 f770ee03 Mike Melanson
static int alac_decode_frame(AVCodecContext *avctx,
454
                             void *outbuffer, int *outputsize,
455 7a00bbad Thilo Borgmann
                             AVPacket *avpkt)
456 7ff85a81 Mike Melanson
{
457 7a00bbad Thilo Borgmann
    const uint8_t *inbuffer = avpkt->data;
458
    int input_buffer_size = avpkt->size;
459 6d021b00 Mike Melanson
    ALACContext *alac = avctx->priv_data;
460 f770ee03 Mike Melanson
461 6d6d7970 Mike Melanson
    int channels;
462 494e3531 Michael Niedermayer
    unsigned int outputsamples;
463 a562e2e6 Vitor Sessak
    int hassize;
464 95801b6a Matthieu Castet
    unsigned int readsamplesize;
465 a562e2e6 Vitor Sessak
    int isnotcompressed;
466 d562ba23 Vitor Sessak
    uint8_t interlacing_shift;
467
    uint8_t interlacing_leftweight;
468 6d6d7970 Mike Melanson
469 f770ee03 Mike Melanson
    /* short-circuit null buffers */
470
    if (!inbuffer || !input_buffer_size)
471 313b52fb Jason Garrett-Glaser
        return -1;
472 7ff85a81 Mike Melanson
473 6d021b00 Mike Melanson
    init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
474 6d6d7970 Mike Melanson
475 e3be5693 Vitor Sessak
    channels = get_bits(&alac->gb, 3) + 1;
476 f1752010 Vitor Sessak
    if (channels > MAX_CHANNELS) {
477
        av_log(avctx, AV_LOG_ERROR, "channels > %d not supported\n",
478
               MAX_CHANNELS);
479 313b52fb Jason Garrett-Glaser
        return -1;
480 f1752010 Vitor Sessak
    }
481 6d6d7970 Mike Melanson
482 586e5bd9 Vitor Sessak
    /* 2^result = something to do with output waiting.
483
     * perhaps matters if we read > 1 frame in a pass?
484
     */
485 7ae7300e Alex Beregszaszi
    skip_bits(&alac->gb, 4);
486 6d6d7970 Mike Melanson
487 7ae7300e Alex Beregszaszi
    skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
488 6d6d7970 Mike Melanson
489 586e5bd9 Vitor Sessak
    /* the output sample size is stored soon */
490 5fc32c27 Alex Beregszaszi
    hassize = get_bits1(&alac->gb);
491 6d6d7970 Mike Melanson
492 f430c7b6 Jai Menon
    alac->wasted_bits = get_bits(&alac->gb, 2) << 3;
493 6d6d7970 Mike Melanson
494 586e5bd9 Vitor Sessak
    /* whether the frame is compressed */
495 5fc32c27 Alex Beregszaszi
    isnotcompressed = get_bits1(&alac->gb);
496 6d6d7970 Mike Melanson
497 586e5bd9 Vitor Sessak
    if (hassize) {
498
        /* now read the number of samples as a 32bit integer */
499 59d598b9 Justin Ruggles
        outputsamples = get_bits_long(&alac->gb, 32);
500 494e3531 Michael Niedermayer
        if(outputsamples > alac->setinfo_max_samples_per_frame){
501
            av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
502
            return -1;
503
        }
504 586e5bd9 Vitor Sessak
    } else
505
        outputsamples = alac->setinfo_max_samples_per_frame;
506 a562e2e6 Vitor Sessak
507 f430c7b6 Jai Menon
    switch (alac->setinfo_sample_size) {
508
    case 16: avctx->sample_fmt    = SAMPLE_FMT_S16;
509
             alac->bytespersample = channels << 1;
510
             break;
511
    case 24: avctx->sample_fmt    = SAMPLE_FMT_S32;
512
             alac->bytespersample = channels << 2;
513
             break;
514
    default: av_log(avctx, AV_LOG_ERROR, "Sample depth %d is not supported.\n",
515
                    alac->setinfo_sample_size);
516
             return -1;
517
    }
518
519 f7739f37 Michael Niedermayer
    if(outputsamples > *outputsize / alac->bytespersample){
520
        av_log(avctx, AV_LOG_ERROR, "sample buffer too small\n");
521
        return -1;
522
    }
523
524 586e5bd9 Vitor Sessak
    *outputsize = outputsamples * alac->bytespersample;
525 f430c7b6 Jai Menon
    readsamplesize = alac->setinfo_sample_size - (alac->wasted_bits) + channels - 1;
526 95801b6a Matthieu Castet
    if (readsamplesize > MIN_CACHE_BITS) {
527
        av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
528
        return -1;
529
    }
530 6d6d7970 Mike Melanson
531 586e5bd9 Vitor Sessak
    if (!isnotcompressed) {
532
        /* so it is compressed */
533 986f143a Måns Rullgård
        int16_t predictor_coef_table[MAX_CHANNELS][32];
534
        int predictor_coef_num[MAX_CHANNELS];
535
        int prediction_type[MAX_CHANNELS];
536
        int prediction_quantitization[MAX_CHANNELS];
537
        int ricemodifier[MAX_CHANNELS];
538 586e5bd9 Vitor Sessak
        int i, chan;
539 6d6d7970 Mike Melanson
540 586e5bd9 Vitor Sessak
        interlacing_shift = get_bits(&alac->gb, 8);
541
        interlacing_leftweight = get_bits(&alac->gb, 8);
542 6d6d7970 Mike Melanson
543 586e5bd9 Vitor Sessak
        for (chan = 0; chan < channels; chan++) {
544 7f268016 Vitor Sessak
            prediction_type[chan] = get_bits(&alac->gb, 4);
545
            prediction_quantitization[chan] = get_bits(&alac->gb, 4);
546 6d6d7970 Mike Melanson
547 7f268016 Vitor Sessak
            ricemodifier[chan] = get_bits(&alac->gb, 3);
548
            predictor_coef_num[chan] = get_bits(&alac->gb, 5);
549 6d6d7970 Mike Melanson
550
            /* read the predictor table */
551 10024d44 Vitor Sessak
            for (i = 0; i < predictor_coef_num[chan]; i++)
552 7f268016 Vitor Sessak
                predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
553 586e5bd9 Vitor Sessak
        }
554 6d6d7970 Mike Melanson
555 f430c7b6 Jai Menon
        if (alac->wasted_bits) {
556
            int i, ch;
557
            for (i = 0; i < outputsamples; i++) {
558
                for (ch = 0; ch < channels; ch++)
559
                    alac->wasted_bits_buffer[ch][i] = get_bits(&alac->gb, alac->wasted_bits);
560
            }
561
        }
562 586e5bd9 Vitor Sessak
        for (chan = 0; chan < channels; chan++) {
563 6d6d7970 Mike Melanson
            bastardized_rice_decompress(alac,
564 7f268016 Vitor Sessak
                                        alac->predicterror_buffer[chan],
565 6d6d7970 Mike Melanson
                                        outputsamples,
566
                                        readsamplesize,
567
                                        alac->setinfo_rice_initialhistory,
568
                                        alac->setinfo_rice_kmodifier,
569 7f268016 Vitor Sessak
                                        ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
570 6d6d7970 Mike Melanson
                                        (1 << alac->setinfo_rice_kmodifier) - 1);
571
572 7f268016 Vitor Sessak
            if (prediction_type[chan] == 0) {
573 586e5bd9 Vitor Sessak
                /* adaptive fir */
574 7f268016 Vitor Sessak
                predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
575
                                               alac->outputsamples_buffer[chan],
576 6d6d7970 Mike Melanson
                                               outputsamples,
577
                                               readsamplesize,
578 7f268016 Vitor Sessak
                                               predictor_coef_table[chan],
579
                                               predictor_coef_num[chan],
580
                                               prediction_quantitization[chan]);
581 6d6d7970 Mike Melanson
            } else {
582 7f268016 Vitor Sessak
                av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
583 60c4a31c Vitor Sessak
                /* I think the only other prediction type (or perhaps this is
584
                 * just a boolean?) runs adaptive fir twice.. like:
585 10fb5763 Vitor Sessak
                 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
586
                 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
587
                 * little strange..
588
                 */
589 6d6d7970 Mike Melanson
            }
590 586e5bd9 Vitor Sessak
        }
591
    } else {
592
        /* not compressed, easy case */
593 6827f6f2 Matthieu Castet
        int i, chan;
594 f430c7b6 Jai Menon
        if (alac->setinfo_sample_size <= 16) {
595 66b26265 Jai Menon
        for (i = 0; i < outputsamples; i++)
596
            for (chan = 0; chan < channels; chan++) {
597 6827f6f2 Matthieu Castet
                int32_t audiobits;
598 6d6d7970 Mike Melanson
599 6053da01 Justin Ruggles
                audiobits = get_sbits_long(&alac->gb, alac->setinfo_sample_size);
600 10fb5763 Vitor Sessak
601 6827f6f2 Matthieu Castet
                alac->outputsamples_buffer[chan][i] = audiobits;
602
            }
603 f430c7b6 Jai Menon
        } else {
604
            for (i = 0; i < outputsamples; i++) {
605
                for (chan = 0; chan < channels; chan++) {
606
                    alac->outputsamples_buffer[chan][i] = get_bits(&alac->gb,
607
                                                          alac->setinfo_sample_size);
608
                    alac->outputsamples_buffer[chan][i] = sign_extend(alac->outputsamples_buffer[chan][i],
609
                                                                      alac->setinfo_sample_size);
610
                }
611
            }
612
        }
613
        alac->wasted_bits = 0;
614 586e5bd9 Vitor Sessak
        interlacing_shift = 0;
615
        interlacing_leftweight = 0;
616
    }
617 e5ab7379 Matthieu Castet
    if (get_bits(&alac->gb, 3) != 7)
618
        av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
619 6d6d7970 Mike Melanson
620 586e5bd9 Vitor Sessak
    switch(alac->setinfo_sample_size) {
621 d58d7ade Vitor Sessak
    case 16:
622 586e5bd9 Vitor Sessak
        if (channels == 2) {
623 4a5e6389 Vitor Sessak
            reconstruct_stereo_16(alac->outputsamples_buffer,
624 92df8910 Vitor Sessak
                                  (int16_t*)outbuffer,
625
                                  alac->numchannels,
626
                                  outputsamples,
627
                                  interlacing_shift,
628
                                  interlacing_leftweight);
629 586e5bd9 Vitor Sessak
        } else {
630
            int i;
631
            for (i = 0; i < outputsamples; i++) {
632 f6d29165 Jai Menon
                ((int16_t*)outbuffer)[i] = alac->outputsamples_buffer[0][i];
633 586e5bd9 Vitor Sessak
            }
634 6d6d7970 Mike Melanson
        }
635 586e5bd9 Vitor Sessak
        break;
636
    case 24:
637 f430c7b6 Jai Menon
        if (channels == 2) {
638
            decorrelate_stereo_24(alac->outputsamples_buffer,
639
                                  outbuffer,
640
                                  alac->wasted_bits_buffer,
641
                                  alac->wasted_bits,
642
                                  alac->numchannels,
643
                                  outputsamples,
644
                                  interlacing_shift,
645
                                  interlacing_leftweight);
646
        } else {
647
            int i;
648
            for (i = 0; i < outputsamples; i++)
649
                ((int32_t *)outbuffer)[i] = alac->outputsamples_buffer[0][i] << 8;
650
        }
651 586e5bd9 Vitor Sessak
        break;
652
    }
653 6d6d7970 Mike Melanson
654 e5ab7379 Matthieu Castet
    if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
655
        av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
656
657 f770ee03 Mike Melanson
    return input_buffer_size;
658 6d6d7970 Mike Melanson
}
659
660 98a6fff9 Zuxy Meng
static av_cold int alac_decode_init(AVCodecContext * avctx)
661 6d6d7970 Mike Melanson
{
662 6d021b00 Mike Melanson
    ALACContext *alac = avctx->priv_data;
663
    alac->avctx = avctx;
664
    alac->numchannels = alac->avctx->channels;
665 6d6d7970 Mike Melanson
666 313b52fb Jason Garrett-Glaser
    /* initialize from the extradata */
667
    if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
668
        av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
669
            ALAC_EXTRADATA_SIZE);
670
        return -1;
671
    }
672
    if (alac_set_info(alac)) {
673
        av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
674
        return -1;
675
    }
676
677 6d6d7970 Mike Melanson
    return 0;
678
}
679
680 98a6fff9 Zuxy Meng
static av_cold int alac_decode_close(AVCodecContext *avctx)
681 6d6d7970 Mike Melanson
{
682 6d021b00 Mike Melanson
    ALACContext *alac = avctx->priv_data;
683 6d6d7970 Mike Melanson
684 153696a6 Vitor Sessak
    int chan;
685
    for (chan = 0; chan < MAX_CHANNELS; chan++) {
686 e243eee4 Jai Menon
        av_freep(&alac->predicterror_buffer[chan]);
687
        av_freep(&alac->outputsamples_buffer[chan]);
688 f430c7b6 Jai Menon
        av_freep(&alac->wasted_bits_buffer[chan]);
689 153696a6 Vitor Sessak
    }
690 6d6d7970 Mike Melanson
691
    return 0;
692
}
693
694
AVCodec alac_decoder = {
695
    "alac",
696 72415b2a Stefano Sabatini
    AVMEDIA_TYPE_AUDIO,
697 6d6d7970 Mike Melanson
    CODEC_ID_ALAC,
698
    sizeof(ALACContext),
699
    alac_decode_init,
700
    NULL,
701
    alac_decode_close,
702
    alac_decode_frame,
703 fe4bf374 Stefano Sabatini
    .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
704 6d6d7970 Mike Melanson
};