Statistics
| Branch: | Revision:

ffmpeg / libavcodec / alac.c @ 2912e87a

History | View | Annotate | Download (22.7 KB)

1
/*
2
 * ALAC (Apple Lossless Audio Codec) decoder
3
 * Copyright (c) 2005 David Hammerton
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21

    
22
/**
23
 * @file
24
 * ALAC (Apple Lossless Audio Codec) decoder
25
 * @author 2005 David Hammerton
26
 *
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
 *
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
 */
53

    
54

    
55
#include "avcodec.h"
56
#include "get_bits.h"
57
#include "bytestream.h"
58
#include "unary.h"
59
#include "mathops.h"
60

    
61
#define ALAC_EXTRADATA_SIZE 36
62
#define MAX_CHANNELS 2
63

    
64
typedef struct {
65

    
66
    AVCodecContext *avctx;
67
    GetBitContext gb;
68

    
69
    int numchannels;
70
    int bytespersample;
71

    
72
    /* buffers */
73
    int32_t *predicterror_buffer[MAX_CHANNELS];
74

    
75
    int32_t *outputsamples_buffer[MAX_CHANNELS];
76

    
77
    int32_t *wasted_bits_buffer[MAX_CHANNELS];
78

    
79
    /* 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
    int wasted_bits;
88
} ALACContext;
89

    
90
static void allocate_buffers(ALACContext *alac)
91
{
92
    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

    
97
        alac->outputsamples_buffer[chan] =
98
            av_malloc(alac->setinfo_max_samples_per_frame * 4);
99

    
100
        alac->wasted_bits_buffer[chan] = av_malloc(alac->setinfo_max_samples_per_frame * 4);
101
    }
102
}
103

    
104
static int alac_set_info(ALACContext *alac)
105
{
106
    const unsigned char *ptr = alac->avctx->extradata;
107

    
108
    ptr += 4; /* size */
109
    ptr += 4; /* alac */
110
    ptr += 4; /* 0 ? */
111

    
112
    if(AV_RB32(ptr) >= UINT_MAX/4){
113
        av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
114
        return -1;
115
    }
116

    
117
    /* buffer size / 2 ? */
118
    alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
119
    ptr++;                          /* ??? */
120
    alac->setinfo_sample_size           = *ptr++;
121
    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
    alac->setinfo_rice_historymult      = *ptr++;
126
    alac->setinfo_rice_initialhistory   = *ptr++;
127
    alac->setinfo_rice_kmodifier        = *ptr++;
128
    ptr++;                         /* channels? */
129
    bytestream_get_be16(&ptr);      /* ??? */
130
    bytestream_get_be32(&ptr);      /* max coded frame size */
131
    bytestream_get_be32(&ptr);      /* bitrate ? */
132
    bytestream_get_be32(&ptr);      /* samplerate */
133

    
134
    allocate_buffers(alac);
135

    
136
    return 0;
137
}
138

    
139
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
        if (k >= limit)
148
            k = limit;
149

    
150
        if (k != 1) {
151
            int extrabits = show_bits(gb, k);
152

    
153
            /* multiply x by 2^k - 1, as part of their strange algorithm */
154
            x = (x << k) - x;
155

    
156
            if (extrabits > 1) {
157
                x += extrabits - 1;
158
                skip_bits(gb, k);
159
            } else
160
                skip_bits(gb, k - 1);
161
        }
162
    }
163
    return x;
164
}
165

    
166
static void bastardized_rice_decompress(ALACContext *alac,
167
                                 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
        int32_t x;
182
        int32_t x_modified;
183
        int32_t final_val;
184

    
185
        /* standard rice encoding */
186
        int k; /* size of extra bits */
187

    
188
        /* read k, that is bits as is */
189
        k = av_log2((history >> 9) + 3);
190
        x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
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 k;
210
            unsigned int block_size;
211

    
212
            sign_modifier = 1;
213

    
214
            k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
215

    
216
            block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
217

    
218
            if (block_size > 0) {
219
                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
                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
static inline int sign_only(int v)
236
{
237
    return v ? FFSIGN(v) : 0;
238
}
239

    
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
        if (output_size <= 1)
255
            return;
256

    
257
        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
        if (output_size <= 1)
266
            return;
267
        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
            buffer_out[i+1] =
274
                sign_extend((prev_value + error_value), readsamplesize);
275
        }
276
        return;
277
    }
278

    
279
    /* read warm-up samples */
280
    if (predictor_coef_num > 0)
281
        for (i = 0; i < predictor_coef_num; i++) {
282
            int32_t val;
283

    
284
            val = buffer_out[i] + error_buffer[i+1];
285
            val = sign_extend(val, readsamplesize);
286
            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
     * should be unrolled and optimized
292
     */
293
    if (predictor_coef_num == 4) {
294
        /* FIXME: optimized general case */
295
        return;
296
    }
297

298
    if (predictor_coef_table == 8) {
299
        /* FIXME: optimized general case */
300
        return;
301
    }
302
#endif
303

    
304
    /* general case */
305
    if (predictor_coef_num > 0) {
306
        for (i = predictor_coef_num + 1; i < output_size; i++) {
307
            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
            outval = sign_extend(outval, readsamplesize);
321

    
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
                    int sign = sign_only(val);
330

    
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
                    int sign = - sign_only(val);
346

    
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
static void reconstruct_stereo_16(int32_t *buffer[MAX_CHANNELS],
364
                                  int16_t *buffer_out,
365
                                  int numchannels, int numsamples,
366
                                  uint8_t interlacing_shift,
367
                                  uint8_t interlacing_leftweight)
368
{
369
    int i;
370
    if (numsamples <= 0)
371
        return;
372

    
373
    /* weighted interlacing */
374
    if (interlacing_leftweight) {
375
        for (i = 0; i < numsamples; i++) {
376
            int32_t a, b;
377

    
378
            a = buffer[0][i];
379
            b = buffer[1][i];
380

    
381
            a -= (b * interlacing_leftweight) >> interlacing_shift;
382
            b += a;
383

    
384
            buffer_out[i*numchannels] = b;
385
            buffer_out[i*numchannels + 1] = a;
386
        }
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
        left = buffer[0][i];
396
        right = buffer[1][i];
397

    
398
        buffer_out[i*numchannels] = left;
399
        buffer_out[i*numchannels + 1] = right;
400
    }
401
}
402

    
403
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
static int alac_decode_frame(AVCodecContext *avctx,
454
                             void *outbuffer, int *outputsize,
455
                             AVPacket *avpkt)
456
{
457
    const uint8_t *inbuffer = avpkt->data;
458
    int input_buffer_size = avpkt->size;
459
    ALACContext *alac = avctx->priv_data;
460

    
461
    int channels;
462
    unsigned int outputsamples;
463
    int hassize;
464
    unsigned int readsamplesize;
465
    int isnotcompressed;
466
    uint8_t interlacing_shift;
467
    uint8_t interlacing_leftweight;
468

    
469
    /* short-circuit null buffers */
470
    if (!inbuffer || !input_buffer_size)
471
        return -1;
472

    
473
    init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
474

    
475
    channels = get_bits(&alac->gb, 3) + 1;
476
    if (channels > MAX_CHANNELS) {
477
        av_log(avctx, AV_LOG_ERROR, "channels > %d not supported\n",
478
               MAX_CHANNELS);
479
        return -1;
480
    }
481

    
482
    /* 2^result = something to do with output waiting.
483
     * perhaps matters if we read > 1 frame in a pass?
484
     */
485
    skip_bits(&alac->gb, 4);
486

    
487
    skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
488

    
489
    /* the output sample size is stored soon */
490
    hassize = get_bits1(&alac->gb);
491

    
492
    alac->wasted_bits = get_bits(&alac->gb, 2) << 3;
493

    
494
    /* whether the frame is compressed */
495
    isnotcompressed = get_bits1(&alac->gb);
496

    
497
    if (hassize) {
498
        /* now read the number of samples as a 32bit integer */
499
        outputsamples = get_bits_long(&alac->gb, 32);
500
        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
    } else
505
        outputsamples = alac->setinfo_max_samples_per_frame;
506

    
507
    switch (alac->setinfo_sample_size) {
508
    case 16: avctx->sample_fmt    = AV_SAMPLE_FMT_S16;
509
             alac->bytespersample = channels << 1;
510
             break;
511
    case 24: avctx->sample_fmt    = AV_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
    if(outputsamples > *outputsize / alac->bytespersample){
520
        av_log(avctx, AV_LOG_ERROR, "sample buffer too small\n");
521
        return -1;
522
    }
523

    
524
    *outputsize = outputsamples * alac->bytespersample;
525
    readsamplesize = alac->setinfo_sample_size - (alac->wasted_bits) + channels - 1;
526
    if (readsamplesize > MIN_CACHE_BITS) {
527
        av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
528
        return -1;
529
    }
530

    
531
    if (!isnotcompressed) {
532
        /* so it is compressed */
533
        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
        int i, chan;
539

    
540
        interlacing_shift = get_bits(&alac->gb, 8);
541
        interlacing_leftweight = get_bits(&alac->gb, 8);
542

    
543
        for (chan = 0; chan < channels; chan++) {
544
            prediction_type[chan] = get_bits(&alac->gb, 4);
545
            prediction_quantitization[chan] = get_bits(&alac->gb, 4);
546

    
547
            ricemodifier[chan] = get_bits(&alac->gb, 3);
548
            predictor_coef_num[chan] = get_bits(&alac->gb, 5);
549

    
550
            /* read the predictor table */
551
            for (i = 0; i < predictor_coef_num[chan]; i++)
552
                predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
553
        }
554

    
555
        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
        for (chan = 0; chan < channels; chan++) {
563
            bastardized_rice_decompress(alac,
564
                                        alac->predicterror_buffer[chan],
565
                                        outputsamples,
566
                                        readsamplesize,
567
                                        alac->setinfo_rice_initialhistory,
568
                                        alac->setinfo_rice_kmodifier,
569
                                        ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
570
                                        (1 << alac->setinfo_rice_kmodifier) - 1);
571

    
572
            if (prediction_type[chan] == 0) {
573
                /* adaptive fir */
574
                predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
575
                                               alac->outputsamples_buffer[chan],
576
                                               outputsamples,
577
                                               readsamplesize,
578
                                               predictor_coef_table[chan],
579
                                               predictor_coef_num[chan],
580
                                               prediction_quantitization[chan]);
581
            } else {
582
                av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
583
                /* I think the only other prediction type (or perhaps this is
584
                 * just a boolean?) runs adaptive fir twice.. like:
585
                 * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
586
                 * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
587
                 * little strange..
588
                 */
589
            }
590
        }
591
    } else {
592
        /* not compressed, easy case */
593
        int i, chan;
594
        if (alac->setinfo_sample_size <= 16) {
595
        for (i = 0; i < outputsamples; i++)
596
            for (chan = 0; chan < channels; chan++) {
597
                int32_t audiobits;
598

    
599
                audiobits = get_sbits_long(&alac->gb, alac->setinfo_sample_size);
600

    
601
                alac->outputsamples_buffer[chan][i] = audiobits;
602
            }
603
        } 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
        interlacing_shift = 0;
615
        interlacing_leftweight = 0;
616
    }
617
    if (get_bits(&alac->gb, 3) != 7)
618
        av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
619

    
620
    switch(alac->setinfo_sample_size) {
621
    case 16:
622
        if (channels == 2) {
623
            reconstruct_stereo_16(alac->outputsamples_buffer,
624
                                  (int16_t*)outbuffer,
625
                                  alac->numchannels,
626
                                  outputsamples,
627
                                  interlacing_shift,
628
                                  interlacing_leftweight);
629
        } else {
630
            int i;
631
            for (i = 0; i < outputsamples; i++) {
632
                ((int16_t*)outbuffer)[i] = alac->outputsamples_buffer[0][i];
633
            }
634
        }
635
        break;
636
    case 24:
637
        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
        break;
652
    }
653

    
654
    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
    return input_buffer_size;
658
}
659

    
660
static av_cold int alac_decode_init(AVCodecContext * avctx)
661
{
662
    ALACContext *alac = avctx->priv_data;
663
    alac->avctx = avctx;
664
    alac->numchannels = alac->avctx->channels;
665

    
666
    /* 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
    return 0;
678
}
679

    
680
static av_cold int alac_decode_close(AVCodecContext *avctx)
681
{
682
    ALACContext *alac = avctx->priv_data;
683

    
684
    int chan;
685
    for (chan = 0; chan < MAX_CHANNELS; chan++) {
686
        av_freep(&alac->predicterror_buffer[chan]);
687
        av_freep(&alac->outputsamples_buffer[chan]);
688
        av_freep(&alac->wasted_bits_buffer[chan]);
689
    }
690

    
691
    return 0;
692
}
693

    
694
AVCodec ff_alac_decoder = {
695
    "alac",
696
    AVMEDIA_TYPE_AUDIO,
697
    CODEC_ID_ALAC,
698
    sizeof(ALACContext),
699
    alac_decode_init,
700
    NULL,
701
    alac_decode_close,
702
    alac_decode_frame,
703
    .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
704
};