libavcodec/alac.c
6d6d7970
 /*
  * ALAC (Apple Lossless Audio Codec) decoder
  * Copyright (c) 2005 David Hammerton
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
6d6d7970
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
6d6d7970
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
6d6d7970
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
6d6d7970
  */
 
 /**
ba87f080
  * @file
6d6d7970
  * ALAC (Apple Lossless Audio Codec) decoder
  * @author 2005 David Hammerton
  *
  * For more information on the ALAC format, visit:
  *  http://crazney.net/programs/itunes/alac.html
  *
  * Note: This decoder expects a 36- (0x24-)byte QuickTime atom to be
  * passed through the extradata[_size] fields. This atom is tacked onto
  * the end of an 'alac' stsd atom and has the following format:
  *  bytes 0-3   atom size (0x24), big-endian
  *  bytes 4-7   atom type ('alac', not the 'alac' tag from start of stsd)
  *  bytes 8-35  data bytes needed by decoder
a1db1fc4
  *
  * Extradata:
  * 32bit  size
  * 32bit  tag (=alac)
  * 32bit  zero?
  * 32bit  max sample per frame
  *  8bit  ?? (zero?)
  *  8bit  sample size
  *  8bit  history mult
  *  8bit  initial history
  *  8bit  kmodifier
  *  8bit  channels?
  * 16bit  ??
  * 32bit  max coded frame size
  * 32bit  bitrate?
  * 32bit  samplerate
6d6d7970
  */
 
 
 #include "avcodec.h"
9106a698
 #include "get_bits.h"
f79488d4
 #include "bytestream.h"
becc0ef9
 #include "unary.h"
8858990f
 #include "mathops.h"
6d6d7970
 
 #define ALAC_EXTRADATA_SIZE 36
153696a6
 #define MAX_CHANNELS 2
6d6d7970
 
6d021b00
 typedef struct {
 
     AVCodecContext *avctx;
     GetBitContext gb;
6d6d7970
 
     int numchannels;
     int bytespersample;
 
     /* buffers */
153696a6
     int32_t *predicterror_buffer[MAX_CHANNELS];
6d6d7970
 
153696a6
     int32_t *outputsamples_buffer[MAX_CHANNELS];
6d6d7970
 
f430c7b6
     int32_t *wasted_bits_buffer[MAX_CHANNELS];
 
6d6d7970
     /* stuff from setinfo */
     uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */    /* max samples per frame? */
     uint8_t setinfo_sample_size; /* 0x10 */
     uint8_t setinfo_rice_historymult; /* 0x28 */
     uint8_t setinfo_rice_initialhistory; /* 0x0a */
     uint8_t setinfo_rice_kmodifier; /* 0x0e */
     /* end setinfo stuff */
 
f430c7b6
     int wasted_bits;
6d6d7970
 } ALACContext;
 
6d021b00
 static void allocate_buffers(ALACContext *alac)
6d6d7970
 {
153696a6
     int chan;
     for (chan = 0; chan < MAX_CHANNELS; chan++) {
         alac->predicterror_buffer[chan] =
             av_malloc(alac->setinfo_max_samples_per_frame * 4);
6d6d7970
 
153696a6
         alac->outputsamples_buffer[chan] =
             av_malloc(alac->setinfo_max_samples_per_frame * 4);
f430c7b6
 
         alac->wasted_bits_buffer[chan] = av_malloc(alac->setinfo_max_samples_per_frame * 4);
153696a6
     }
6d6d7970
 }
 
3a1a7e32
 static int alac_set_info(ALACContext *alac)
6d6d7970
 {
b5777b94
     const unsigned char *ptr = alac->avctx->extradata;
6d6d7970
 
     ptr += 4; /* size */
     ptr += 4; /* alac */
     ptr += 4; /* 0 ? */
 
fead30d4
     if(AV_RB32(ptr) >= UINT_MAX/4){
3a1a7e32
         av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
         return -1;
     }
f79488d4
 
     /* buffer size / 2 ? */
     alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
4c63c597
     ptr++;                          /* ??? */
a1301f29
     alac->setinfo_sample_size           = *ptr++;
e5838c90
     if (alac->setinfo_sample_size > 32) {
         av_log(alac->avctx, AV_LOG_ERROR, "setinfo_sample_size too large\n");
         return -1;
     }
a1301f29
     alac->setinfo_rice_historymult      = *ptr++;
     alac->setinfo_rice_initialhistory   = *ptr++;
     alac->setinfo_rice_kmodifier        = *ptr++;
4c63c597
     ptr++;                         /* channels? */
db08882e
     bytestream_get_be16(&ptr);      /* ??? */
     bytestream_get_be32(&ptr);      /* max coded frame size */
     bytestream_get_be32(&ptr);      /* bitrate ? */
4c63c597
     bytestream_get_be32(&ptr);      /* samplerate */
6d6d7970
 
     allocate_buffers(alac);
3a1a7e32
 
     return 0;
6d6d7970
 }
 
5eeba07e
 static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
     /* read x - number of 1s before 0 represent the rice */
     int x = get_unary_0_9(gb);
 
     if (x > 8) { /* RICE THRESHOLD */
         /* use alternative encoding */
         x = get_bits(gb, readsamplesize);
     } else {
1f655c4f
         if (k >= limit)
             k = limit;
5a5a27c5
 
1f655c4f
         if (k != 1) {
             int extrabits = show_bits(gb, k);
5a5a27c5
 
1f655c4f
             /* multiply x by 2^k - 1, as part of their strange algorithm */
             x = (x << k) - x;
5a5a27c5
 
1f655c4f
             if (extrabits > 1) {
                 x += extrabits - 1;
                 skip_bits(gb, k);
             } else
                 skip_bits(gb, k - 1);
         }
5eeba07e
     }
5a5a27c5
     return x;
 }
 
1b47fafd
 static void bastardized_rice_decompress(ALACContext *alac,
6d6d7970
                                  int32_t *output_buffer,
                                  int output_size,
                                  int readsamplesize, /* arg_10 */
                                  int rice_initialhistory, /* arg424->b */
                                  int rice_kmodifier, /* arg424->d */
                                  int rice_historymult, /* arg424->c */
                                  int rice_kmodifier_mask /* arg424->e */
         )
 {
     int output_count;
     unsigned int history = rice_initialhistory;
     int sign_modifier = 0;
 
     for (output_count = 0; output_count < output_size; output_count++) {
becc0ef9
         int32_t x;
6d6d7970
         int32_t x_modified;
         int32_t final_val;
 
1f655c4f
         /* standard rice encoding */
         int k; /* size of extra bits */
6d6d7970
 
1f655c4f
         /* read k, that is bits as is */
8431603a
         k = av_log2((history >> 9) + 3);
1f655c4f
         x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
6d6d7970
 
         x_modified = sign_modifier + x;
         final_val = (x_modified + 1) / 2;
         if (x_modified & 1) final_val *= -1;
 
         output_buffer[output_count] = final_val;
 
         sign_modifier = 0;
 
         /* now update the history */
10024d44
         history += x_modified * rice_historymult
                    - ((history * rice_historymult) >> 9);
6d6d7970
 
         if (x_modified > 0xffff)
             history = 0xffff;
 
         /* special case: there may be compressed blocks of 0 */
         if ((history < 128) && (output_count+1 < output_size)) {
83e9a67d
             int k;
             unsigned int block_size;
6d6d7970
 
             sign_modifier = 1;
 
8431603a
             k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
6d6d7970
 
1f655c4f
             block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
6d6d7970
 
             if (block_size > 0) {
83e9a67d
                 if(block_size >= output_size - output_count){
                     av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
                     block_size= output_size - output_count - 1;
                 }
6d6d7970
                 memset(&output_buffer[output_count+1], 0, block_size * 4);
                 output_count += block_size;
             }
 
             if (block_size > 0xffff)
                 sign_modifier = 0;
 
             history = 0;
         }
     }
 }
 
321c3138
 static inline int sign_only(int v)
 {
     return v ? FFSIGN(v) : 0;
 }
6d6d7970
 
 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
                                            int32_t *buffer_out,
                                            int output_size,
                                            int readsamplesize,
                                            int16_t *predictor_coef_table,
                                            int predictor_coef_num,
                                            int predictor_quantitization)
 {
     int i;
 
     /* first sample always copies */
     *buffer_out = *error_buffer;
 
     if (!predictor_coef_num) {
10024d44
         if (output_size <= 1)
             return;
 
6d6d7970
         memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
         return;
     }
 
     if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
       /* second-best case scenario for fir decompression,
        * error describes a small difference from the previous sample only
        */
10024d44
         if (output_size <= 1)
             return;
6d6d7970
         for (i = 0; i < output_size - 1; i++) {
             int32_t prev_value;
             int32_t error_value;
 
             prev_value = buffer_out[i];
             error_value = error_buffer[i+1];
10024d44
             buffer_out[i+1] =
8858990f
                 sign_extend((prev_value + error_value), readsamplesize);
6d6d7970
         }
         return;
     }
 
     /* read warm-up samples */
10024d44
     if (predictor_coef_num > 0)
6d6d7970
         for (i = 0; i < predictor_coef_num; i++) {
             int32_t val;
 
             val = buffer_out[i] + error_buffer[i+1];
8858990f
             val = sign_extend(val, readsamplesize);
6d6d7970
             buffer_out[i+1] = val;
         }
 
 #if 0
     /* 4 and 8 are very common cases (the only ones i've seen). these
52b541ad
      * should be unrolled and optimized
6d6d7970
      */
     if (predictor_coef_num == 4) {
52b541ad
         /* FIXME: optimized general case */
6d6d7970
         return;
     }
 
     if (predictor_coef_table == 8) {
52b541ad
         /* FIXME: optimized general case */
6d6d7970
         return;
     }
 #endif
 
     /* general case */
     if (predictor_coef_num > 0) {
10024d44
         for (i = predictor_coef_num + 1; i < output_size; i++) {
6d6d7970
             int j;
             int sum = 0;
             int outval;
             int error_val = error_buffer[i];
 
             for (j = 0; j < predictor_coef_num; j++) {
                 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
                        predictor_coef_table[j];
             }
 
             outval = (1 << (predictor_quantitization-1)) + sum;
             outval = outval >> predictor_quantitization;
             outval = outval + buffer_out[0] + error_val;
8858990f
             outval = sign_extend(outval, readsamplesize);
6d6d7970
 
             buffer_out[predictor_coef_num+1] = outval;
 
             if (error_val > 0) {
                 int predictor_num = predictor_coef_num - 1;
 
                 while (predictor_num >= 0 && error_val > 0) {
                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138
                     int sign = sign_only(val);
6d6d7970
 
                     predictor_coef_table[predictor_num] -= sign;
 
                     val *= sign; /* absolute value */
 
                     error_val -= ((val >> predictor_quantitization) *
                                   (predictor_coef_num - predictor_num));
 
                     predictor_num--;
                 }
             } else if (error_val < 0) {
                 int predictor_num = predictor_coef_num - 1;
 
                 while (predictor_num >= 0 && error_val < 0) {
                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
321c3138
                     int sign = - sign_only(val);
6d6d7970
 
                     predictor_coef_table[predictor_num] -= sign;
 
                     val *= sign; /* neg value */
 
                     error_val -= ((val >> predictor_quantitization) *
                                   (predictor_coef_num - predictor_num));
 
                     predictor_num--;
                 }
             }
 
             buffer_out++;
         }
     }
 }
 
4a5e6389
 static void reconstruct_stereo_16(int32_t *buffer[MAX_CHANNELS],
db695867
                                   int16_t *buffer_out,
                                   int numchannels, int numsamples,
                                   uint8_t interlacing_shift,
                                   uint8_t interlacing_leftweight)
7ff85a81
 {
6d6d7970
     int i;
10024d44
     if (numsamples <= 0)
         return;
6d6d7970
 
     /* weighted interlacing */
     if (interlacing_leftweight) {
         for (i = 0; i < numsamples; i++) {
9c8d9f25
             int32_t a, b;
6d6d7970
 
9c8d9f25
             a = buffer[0][i];
             b = buffer[1][i];
6d6d7970
 
9c8d9f25
             a -= (b * interlacing_leftweight) >> interlacing_shift;
             b += a;
6d6d7970
 
9c8d9f25
             buffer_out[i*numchannels] = b;
             buffer_out[i*numchannels + 1] = a;
6d6d7970
         }
 
         return;
     }
 
     /* otherwise basic interlacing took place */
     for (i = 0; i < numsamples; i++) {
         int16_t left, right;
 
c9128d56
         left = buffer[0][i];
         right = buffer[1][i];
6d6d7970
 
         buffer_out[i*numchannels] = left;
         buffer_out[i*numchannels + 1] = right;
     }
 }
 
f430c7b6
 static void decorrelate_stereo_24(int32_t *buffer[MAX_CHANNELS],
                                   int32_t *buffer_out,
                                   int32_t *wasted_bits_buffer[MAX_CHANNELS],
                                   int wasted_bits,
                                   int numchannels, int numsamples,
                                   uint8_t interlacing_shift,
                                   uint8_t interlacing_leftweight)
 {
     int i;
 
     if (numsamples <= 0)
         return;
 
     /* weighted interlacing */
     if (interlacing_leftweight) {
         for (i = 0; i < numsamples; i++) {
             int32_t a, b;
 
             a = buffer[0][i];
             b = buffer[1][i];
 
             a -= (b * interlacing_leftweight) >> interlacing_shift;
             b += a;
 
             if (wasted_bits) {
                 b  = (b  << wasted_bits) | wasted_bits_buffer[0][i];
                 a  = (a  << wasted_bits) | wasted_bits_buffer[1][i];
             }
 
             buffer_out[i * numchannels]     = b << 8;
             buffer_out[i * numchannels + 1] = a << 8;
         }
     } else {
         for (i = 0; i < numsamples; i++) {
             int32_t left, right;
 
             left  = buffer[0][i];
             right = buffer[1][i];
 
             if (wasted_bits) {
                 left   = (left   << wasted_bits) | wasted_bits_buffer[0][i];
                 right  = (right  << wasted_bits) | wasted_bits_buffer[1][i];
             }
 
             buffer_out[i * numchannels]     = left  << 8;
             buffer_out[i * numchannels + 1] = right << 8;
         }
     }
 }
 
f770ee03
 static int alac_decode_frame(AVCodecContext *avctx,
                              void *outbuffer, int *outputsize,
7a00bbad
                              AVPacket *avpkt)
7ff85a81
 {
7a00bbad
     const uint8_t *inbuffer = avpkt->data;
     int input_buffer_size = avpkt->size;
6d021b00
     ALACContext *alac = avctx->priv_data;
f770ee03
 
6d6d7970
     int channels;
494e3531
     unsigned int outputsamples;
a562e2e6
     int hassize;
95801b6a
     unsigned int readsamplesize;
a562e2e6
     int isnotcompressed;
d562ba23
     uint8_t interlacing_shift;
     uint8_t interlacing_leftweight;
6d6d7970
 
f770ee03
     /* short-circuit null buffers */
     if (!inbuffer || !input_buffer_size)
313b52fb
         return -1;
7ff85a81
 
6d021b00
     init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
6d6d7970
 
e3be5693
     channels = get_bits(&alac->gb, 3) + 1;
f1752010
     if (channels > MAX_CHANNELS) {
         av_log(avctx, AV_LOG_ERROR, "channels > %d not supported\n",
                MAX_CHANNELS);
313b52fb
         return -1;
f1752010
     }
6d6d7970
 
586e5bd9
     /* 2^result = something to do with output waiting.
      * perhaps matters if we read > 1 frame in a pass?
      */
7ae7300e
     skip_bits(&alac->gb, 4);
6d6d7970
 
7ae7300e
     skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
6d6d7970
 
586e5bd9
     /* the output sample size is stored soon */
5fc32c27
     hassize = get_bits1(&alac->gb);
6d6d7970
 
f430c7b6
     alac->wasted_bits = get_bits(&alac->gb, 2) << 3;
6d6d7970
 
586e5bd9
     /* whether the frame is compressed */
5fc32c27
     isnotcompressed = get_bits1(&alac->gb);
6d6d7970
 
586e5bd9
     if (hassize) {
         /* now read the number of samples as a 32bit integer */
59d598b9
         outputsamples = get_bits_long(&alac->gb, 32);
494e3531
         if(outputsamples > alac->setinfo_max_samples_per_frame){
             av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
             return -1;
         }
586e5bd9
     } else
         outputsamples = alac->setinfo_max_samples_per_frame;
a562e2e6
 
f430c7b6
     switch (alac->setinfo_sample_size) {
5d6e4c16
     case 16: avctx->sample_fmt    = AV_SAMPLE_FMT_S16;
f430c7b6
              alac->bytespersample = channels << 1;
              break;
5d6e4c16
     case 24: avctx->sample_fmt    = AV_SAMPLE_FMT_S32;
f430c7b6
              alac->bytespersample = channels << 2;
              break;
     default: av_log(avctx, AV_LOG_ERROR, "Sample depth %d is not supported.\n",
                     alac->setinfo_sample_size);
              return -1;
     }
 
f7739f37
     if(outputsamples > *outputsize / alac->bytespersample){
         av_log(avctx, AV_LOG_ERROR, "sample buffer too small\n");
         return -1;
     }
 
586e5bd9
     *outputsize = outputsamples * alac->bytespersample;
f430c7b6
     readsamplesize = alac->setinfo_sample_size - (alac->wasted_bits) + channels - 1;
95801b6a
     if (readsamplesize > MIN_CACHE_BITS) {
         av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
         return -1;
     }
6d6d7970
 
586e5bd9
     if (!isnotcompressed) {
         /* so it is compressed */
986f143a
         int16_t predictor_coef_table[MAX_CHANNELS][32];
         int predictor_coef_num[MAX_CHANNELS];
         int prediction_type[MAX_CHANNELS];
         int prediction_quantitization[MAX_CHANNELS];
         int ricemodifier[MAX_CHANNELS];
586e5bd9
         int i, chan;
6d6d7970
 
586e5bd9
         interlacing_shift = get_bits(&alac->gb, 8);
         interlacing_leftweight = get_bits(&alac->gb, 8);
6d6d7970
 
586e5bd9
         for (chan = 0; chan < channels; chan++) {
7f268016
             prediction_type[chan] = get_bits(&alac->gb, 4);
             prediction_quantitization[chan] = get_bits(&alac->gb, 4);
6d6d7970
 
7f268016
             ricemodifier[chan] = get_bits(&alac->gb, 3);
             predictor_coef_num[chan] = get_bits(&alac->gb, 5);
6d6d7970
 
             /* read the predictor table */
10024d44
             for (i = 0; i < predictor_coef_num[chan]; i++)
7f268016
                 predictor_coef_table[chan][i] = (int16_t)get_bits(&alac->gb, 16);
586e5bd9
         }
6d6d7970
 
f430c7b6
         if (alac->wasted_bits) {
             int i, ch;
             for (i = 0; i < outputsamples; i++) {
                 for (ch = 0; ch < channels; ch++)
                     alac->wasted_bits_buffer[ch][i] = get_bits(&alac->gb, alac->wasted_bits);
             }
         }
586e5bd9
         for (chan = 0; chan < channels; chan++) {
6d6d7970
             bastardized_rice_decompress(alac,
7f268016
                                         alac->predicterror_buffer[chan],
6d6d7970
                                         outputsamples,
                                         readsamplesize,
                                         alac->setinfo_rice_initialhistory,
                                         alac->setinfo_rice_kmodifier,
7f268016
                                         ricemodifier[chan] * alac->setinfo_rice_historymult / 4,
6d6d7970
                                         (1 << alac->setinfo_rice_kmodifier) - 1);
 
7f268016
             if (prediction_type[chan] == 0) {
586e5bd9
                 /* adaptive fir */
7f268016
                 predictor_decompress_fir_adapt(alac->predicterror_buffer[chan],
                                                alac->outputsamples_buffer[chan],
6d6d7970
                                                outputsamples,
                                                readsamplesize,
7f268016
                                                predictor_coef_table[chan],
                                                predictor_coef_num[chan],
                                                prediction_quantitization[chan]);
6d6d7970
             } else {
7f268016
                 av_log(avctx, AV_LOG_ERROR, "FIXME: unhandled prediction type: %i\n", prediction_type[chan]);
60c4a31c
                 /* I think the only other prediction type (or perhaps this is
                  * just a boolean?) runs adaptive fir twice.. like:
10fb5763
                  * predictor_decompress_fir_adapt(predictor_error, tempout, ...)
                  * predictor_decompress_fir_adapt(predictor_error, outputsamples ...)
                  * little strange..
                  */
6d6d7970
             }
586e5bd9
         }
     } else {
         /* not compressed, easy case */
6827f6f2
         int i, chan;
f430c7b6
         if (alac->setinfo_sample_size <= 16) {
66b26265
         for (i = 0; i < outputsamples; i++)
             for (chan = 0; chan < channels; chan++) {
6827f6f2
                 int32_t audiobits;
6d6d7970
 
6053da01
                 audiobits = get_sbits_long(&alac->gb, alac->setinfo_sample_size);
10fb5763
 
6827f6f2
                 alac->outputsamples_buffer[chan][i] = audiobits;
             }
f430c7b6
         } else {
             for (i = 0; i < outputsamples; i++) {
                 for (chan = 0; chan < channels; chan++) {
                     alac->outputsamples_buffer[chan][i] = get_bits(&alac->gb,
                                                           alac->setinfo_sample_size);
                     alac->outputsamples_buffer[chan][i] = sign_extend(alac->outputsamples_buffer[chan][i],
                                                                       alac->setinfo_sample_size);
                 }
             }
         }
         alac->wasted_bits = 0;
586e5bd9
         interlacing_shift = 0;
         interlacing_leftweight = 0;
     }
e5ab7379
     if (get_bits(&alac->gb, 3) != 7)
         av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
6d6d7970
 
586e5bd9
     switch(alac->setinfo_sample_size) {
d58d7ade
     case 16:
586e5bd9
         if (channels == 2) {
4a5e6389
             reconstruct_stereo_16(alac->outputsamples_buffer,
92df8910
                                   (int16_t*)outbuffer,
                                   alac->numchannels,
                                   outputsamples,
                                   interlacing_shift,
                                   interlacing_leftweight);
586e5bd9
         } else {
             int i;
             for (i = 0; i < outputsamples; i++) {
f6d29165
                 ((int16_t*)outbuffer)[i] = alac->outputsamples_buffer[0][i];
586e5bd9
             }
6d6d7970
         }
586e5bd9
         break;
     case 24:
f430c7b6
         if (channels == 2) {
             decorrelate_stereo_24(alac->outputsamples_buffer,
                                   outbuffer,
                                   alac->wasted_bits_buffer,
                                   alac->wasted_bits,
                                   alac->numchannels,
                                   outputsamples,
                                   interlacing_shift,
                                   interlacing_leftweight);
         } else {
             int i;
             for (i = 0; i < outputsamples; i++)
                 ((int32_t *)outbuffer)[i] = alac->outputsamples_buffer[0][i] << 8;
         }
586e5bd9
         break;
     }
6d6d7970
 
e5ab7379
     if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
         av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
 
f770ee03
     return input_buffer_size;
6d6d7970
 }
 
98a6fff9
 static av_cold int alac_decode_init(AVCodecContext * avctx)
6d6d7970
 {
6d021b00
     ALACContext *alac = avctx->priv_data;
     alac->avctx = avctx;
     alac->numchannels = alac->avctx->channels;
6d6d7970
 
313b52fb
     /* initialize from the extradata */
     if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
         av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
             ALAC_EXTRADATA_SIZE);
         return -1;
     }
     if (alac_set_info(alac)) {
         av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
         return -1;
     }
 
6d6d7970
     return 0;
 }
 
98a6fff9
 static av_cold int alac_decode_close(AVCodecContext *avctx)
6d6d7970
 {
6d021b00
     ALACContext *alac = avctx->priv_data;
6d6d7970
 
153696a6
     int chan;
     for (chan = 0; chan < MAX_CHANNELS; chan++) {
e243eee4
         av_freep(&alac->predicterror_buffer[chan]);
         av_freep(&alac->outputsamples_buffer[chan]);
f430c7b6
         av_freep(&alac->wasted_bits_buffer[chan]);
153696a6
     }
6d6d7970
 
     return 0;
 }
 
e7e2df27
 AVCodec ff_alac_decoder = {
6d6d7970
     "alac",
72415b2a
     AVMEDIA_TYPE_AUDIO,
6d6d7970
     CODEC_ID_ALAC,
     sizeof(ALACContext),
     alac_decode_init,
     NULL,
     alac_decode_close,
     alac_decode_frame,
fe4bf374
     .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
6d6d7970
 };