libavcodec/alac.c
Go to the documentation of this file.
00001 /*
00002  * ALAC (Apple Lossless Audio Codec) decoder
00003  * Copyright (c) 2005 David Hammerton
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00049 #include "avcodec.h"
00050 #include "get_bits.h"
00051 #include "bytestream.h"
00052 #include "unary.h"
00053 #include "mathops.h"
00054 
00055 #define ALAC_EXTRADATA_SIZE 36
00056 #define MAX_CHANNELS 2
00057 
00058 typedef struct {
00059 
00060     AVCodecContext *avctx;
00061     AVFrame frame;
00062     GetBitContext gb;
00063 
00064     int numchannels;
00065 
00066     /* buffers */
00067     int32_t *predicterror_buffer[MAX_CHANNELS];
00068 
00069     int32_t *outputsamples_buffer[MAX_CHANNELS];
00070 
00071     int32_t *extra_bits_buffer[MAX_CHANNELS];
00072 
00073     /* stuff from setinfo */
00074     uint32_t setinfo_max_samples_per_frame; /* 0x1000 = 4096 */    /* max samples per frame? */
00075     uint8_t setinfo_sample_size; /* 0x10 */
00076     uint8_t setinfo_rice_historymult; /* 0x28 */
00077     uint8_t setinfo_rice_initialhistory; /* 0x0a */
00078     uint8_t setinfo_rice_kmodifier; /* 0x0e */
00079     /* end setinfo stuff */
00080 
00081     int extra_bits;                         
00082 } ALACContext;
00083 
00084 static inline int decode_scalar(GetBitContext *gb, int k, int limit, int readsamplesize){
00085     /* read x - number of 1s before 0 represent the rice */
00086     int x = get_unary_0_9(gb);
00087 
00088     if (x > 8) { /* RICE THRESHOLD */
00089         /* use alternative encoding */
00090         x = get_bits(gb, readsamplesize);
00091     } else {
00092         if (k >= limit)
00093             k = limit;
00094 
00095         if (k != 1) {
00096             int extrabits = show_bits(gb, k);
00097 
00098             /* multiply x by 2^k - 1, as part of their strange algorithm */
00099             x = (x << k) - x;
00100 
00101             if (extrabits > 1) {
00102                 x += extrabits - 1;
00103                 skip_bits(gb, k);
00104             } else
00105                 skip_bits(gb, k - 1);
00106         }
00107     }
00108     return x;
00109 }
00110 
00111 static int bastardized_rice_decompress(ALACContext *alac,
00112                                  int32_t *output_buffer,
00113                                  int output_size,
00114                                  int readsamplesize, /* arg_10 */
00115                                  int rice_initialhistory, /* arg424->b */
00116                                  int rice_kmodifier, /* arg424->d */
00117                                  int rice_historymult, /* arg424->c */
00118                                  int rice_kmodifier_mask /* arg424->e */
00119         )
00120 {
00121     int output_count;
00122     unsigned int history = rice_initialhistory;
00123     int sign_modifier = 0;
00124 
00125     for (output_count = 0; output_count < output_size; output_count++) {
00126         int32_t x;
00127         int32_t x_modified;
00128         int32_t final_val;
00129 
00130         /* standard rice encoding */
00131         int k; /* size of extra bits */
00132 
00133         if(get_bits_left(&alac->gb) <= 0)
00134             return -1;
00135 
00136         /* read k, that is bits as is */
00137         k = av_log2((history >> 9) + 3);
00138         x= decode_scalar(&alac->gb, k, rice_kmodifier, readsamplesize);
00139 
00140         x_modified = sign_modifier + x;
00141         final_val = (x_modified + 1) / 2;
00142         if (x_modified & 1) final_val *= -1;
00143 
00144         output_buffer[output_count] = final_val;
00145 
00146         sign_modifier = 0;
00147 
00148         /* now update the history */
00149         history += x_modified * rice_historymult
00150                    - ((history * rice_historymult) >> 9);
00151 
00152         if (x_modified > 0xffff)
00153             history = 0xffff;
00154 
00155         /* special case: there may be compressed blocks of 0 */
00156         if ((history < 128) && (output_count+1 < output_size)) {
00157             int k;
00158             unsigned int block_size;
00159 
00160             sign_modifier = 1;
00161 
00162             k = 7 - av_log2(history) + ((history + 16) >> 6 /* / 64 */);
00163 
00164             block_size= decode_scalar(&alac->gb, k, rice_kmodifier, 16);
00165 
00166             if (block_size > 0) {
00167                 if(block_size >= output_size - output_count){
00168                     av_log(alac->avctx, AV_LOG_ERROR, "invalid zero block size of %d %d %d\n", block_size, output_size, output_count);
00169                     block_size= output_size - output_count - 1;
00170                 }
00171                 memset(&output_buffer[output_count+1], 0, block_size * 4);
00172                 output_count += block_size;
00173             }
00174 
00175             if (block_size > 0xffff)
00176                 sign_modifier = 0;
00177 
00178             history = 0;
00179         }
00180     }
00181     return 0;
00182 }
00183 
00184 static inline int sign_only(int v)
00185 {
00186     return v ? FFSIGN(v) : 0;
00187 }
00188 
00189 static void predictor_decompress_fir_adapt(int32_t *error_buffer,
00190                                            int32_t *buffer_out,
00191                                            int output_size,
00192                                            int readsamplesize,
00193                                            int16_t *predictor_coef_table,
00194                                            int predictor_coef_num,
00195                                            int predictor_quantitization)
00196 {
00197     int i;
00198 
00199     /* first sample always copies */
00200     *buffer_out = *error_buffer;
00201 
00202     if (!predictor_coef_num) {
00203         if (output_size <= 1)
00204             return;
00205 
00206         memcpy(buffer_out+1, error_buffer+1, (output_size-1) * 4);
00207         return;
00208     }
00209 
00210     if (predictor_coef_num == 0x1f) { /* 11111 - max value of predictor_coef_num */
00211       /* second-best case scenario for fir decompression,
00212        * error describes a small difference from the previous sample only
00213        */
00214         if (output_size <= 1)
00215             return;
00216         for (i = 0; i < output_size - 1; i++) {
00217             int32_t prev_value;
00218             int32_t error_value;
00219 
00220             prev_value = buffer_out[i];
00221             error_value = error_buffer[i+1];
00222             buffer_out[i+1] =
00223                 sign_extend((prev_value + error_value), readsamplesize);
00224         }
00225         return;
00226     }
00227 
00228     /* read warm-up samples */
00229     if (predictor_coef_num > 0)
00230         for (i = 0; i < predictor_coef_num; i++) {
00231             int32_t val;
00232 
00233             val = buffer_out[i] + error_buffer[i+1];
00234             val = sign_extend(val, readsamplesize);
00235             buffer_out[i+1] = val;
00236         }
00237 
00238     /* 4 and 8 are very common cases (the only ones i've seen). these
00239      * should be unrolled and optimized
00240      */
00241 
00242     /* general case */
00243     if (predictor_coef_num > 0) {
00244         for (i = predictor_coef_num + 1; i < output_size; i++) {
00245             int j;
00246             int sum = 0;
00247             int outval;
00248             int error_val = error_buffer[i];
00249 
00250             for (j = 0; j < predictor_coef_num; j++) {
00251                 sum += (buffer_out[predictor_coef_num-j] - buffer_out[0]) *
00252                        predictor_coef_table[j];
00253             }
00254 
00255             outval = (1 << (predictor_quantitization-1)) + sum;
00256             outval = outval >> predictor_quantitization;
00257             outval = outval + buffer_out[0] + error_val;
00258             outval = sign_extend(outval, readsamplesize);
00259 
00260             buffer_out[predictor_coef_num+1] = outval;
00261 
00262             if (error_val > 0) {
00263                 int predictor_num = predictor_coef_num - 1;
00264 
00265                 while (predictor_num >= 0 && error_val > 0) {
00266                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
00267                     int sign = sign_only(val);
00268 
00269                     predictor_coef_table[predictor_num] -= sign;
00270 
00271                     val *= sign; /* absolute value */
00272 
00273                     error_val -= ((val >> predictor_quantitization) *
00274                                   (predictor_coef_num - predictor_num));
00275 
00276                     predictor_num--;
00277                 }
00278             } else if (error_val < 0) {
00279                 int predictor_num = predictor_coef_num - 1;
00280 
00281                 while (predictor_num >= 0 && error_val < 0) {
00282                     int val = buffer_out[0] - buffer_out[predictor_coef_num - predictor_num];
00283                     int sign = - sign_only(val);
00284 
00285                     predictor_coef_table[predictor_num] -= sign;
00286 
00287                     val *= sign; /* neg value */
00288 
00289                     error_val -= ((val >> predictor_quantitization) *
00290                                   (predictor_coef_num - predictor_num));
00291 
00292                     predictor_num--;
00293                 }
00294             }
00295 
00296             buffer_out++;
00297         }
00298     }
00299 }
00300 
00301 static void decorrelate_stereo(int32_t *buffer[MAX_CHANNELS],
00302                                int numsamples, uint8_t interlacing_shift,
00303                                uint8_t interlacing_leftweight)
00304 {
00305     int i;
00306 
00307     for (i = 0; i < numsamples; i++) {
00308         int32_t a, b;
00309 
00310         a = buffer[0][i];
00311         b = buffer[1][i];
00312 
00313         a -= (b * interlacing_leftweight) >> interlacing_shift;
00314         b += a;
00315 
00316         buffer[0][i] = b;
00317         buffer[1][i] = a;
00318     }
00319 }
00320 
00321 static void append_extra_bits(int32_t *buffer[MAX_CHANNELS],
00322                               int32_t *extra_bits_buffer[MAX_CHANNELS],
00323                               int extra_bits, int numchannels, int numsamples)
00324 {
00325     int i, ch;
00326 
00327     for (ch = 0; ch < numchannels; ch++)
00328         for (i = 0; i < numsamples; i++)
00329             buffer[ch][i] = (buffer[ch][i] << extra_bits) | extra_bits_buffer[ch][i];
00330 }
00331 
00332 static void interleave_stereo_16(int32_t *buffer[MAX_CHANNELS],
00333                                  int16_t *buffer_out, int numsamples)
00334 {
00335     int i;
00336 
00337     for (i = 0; i < numsamples; i++) {
00338         *buffer_out++ = buffer[0][i];
00339         *buffer_out++ = buffer[1][i];
00340     }
00341 }
00342 
00343 static void interleave_stereo_24(int32_t *buffer[MAX_CHANNELS],
00344                                  int32_t *buffer_out, int numsamples)
00345 {
00346     int i;
00347 
00348     for (i = 0; i < numsamples; i++) {
00349         *buffer_out++ = buffer[0][i] << 8;
00350         *buffer_out++ = buffer[1][i] << 8;
00351     }
00352 }
00353 
00354 static void interleave_stereo_32(int32_t *buffer[MAX_CHANNELS],
00355                                  int32_t *buffer_out, int numsamples)
00356 {
00357     int i;
00358 
00359     for (i = 0; i < numsamples; i++) {
00360         *buffer_out++ = buffer[0][i];
00361         *buffer_out++ = buffer[1][i];
00362     }
00363 }
00364 
00365 static int alac_decode_frame(AVCodecContext *avctx, void *data,
00366                              int *got_frame_ptr, AVPacket *avpkt)
00367 {
00368     const uint8_t *inbuffer = avpkt->data;
00369     int input_buffer_size = avpkt->size;
00370     ALACContext *alac = avctx->priv_data;
00371 
00372     int channels;
00373     unsigned int outputsamples;
00374     int hassize;
00375     unsigned int readsamplesize;
00376     int isnotcompressed;
00377     uint8_t interlacing_shift;
00378     uint8_t interlacing_leftweight;
00379     int i, ch, ret;
00380 
00381     init_get_bits(&alac->gb, inbuffer, input_buffer_size * 8);
00382 
00383     channels = get_bits(&alac->gb, 3) + 1;
00384     if (channels != avctx->channels) {
00385         av_log(avctx, AV_LOG_ERROR, "frame header channel count mismatch\n");
00386         return AVERROR_INVALIDDATA;
00387     }
00388 
00389     /* 2^result = something to do with output waiting.
00390      * perhaps matters if we read > 1 frame in a pass?
00391      */
00392     skip_bits(&alac->gb, 4);
00393 
00394     skip_bits(&alac->gb, 12); /* unknown, skip 12 bits */
00395 
00396     /* the output sample size is stored soon */
00397     hassize = get_bits1(&alac->gb);
00398 
00399     alac->extra_bits = get_bits(&alac->gb, 2) << 3;
00400 
00401     /* whether the frame is compressed */
00402     isnotcompressed = get_bits1(&alac->gb);
00403 
00404     if (hassize) {
00405         /* now read the number of samples as a 32bit integer */
00406         outputsamples = get_bits_long(&alac->gb, 32);
00407         if(outputsamples > alac->setinfo_max_samples_per_frame){
00408             av_log(avctx, AV_LOG_ERROR, "outputsamples %d > %d\n", outputsamples, alac->setinfo_max_samples_per_frame);
00409             return -1;
00410         }
00411     } else
00412         outputsamples = alac->setinfo_max_samples_per_frame;
00413 
00414     /* get output buffer */
00415     if (outputsamples > INT32_MAX) {
00416         av_log(avctx, AV_LOG_ERROR, "unsupported block size: %u\n", outputsamples);
00417         return AVERROR_INVALIDDATA;
00418     }
00419     alac->frame.nb_samples = outputsamples;
00420     if ((ret = avctx->get_buffer(avctx, &alac->frame)) < 0) {
00421         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00422         return ret;
00423     }
00424 
00425     readsamplesize = alac->setinfo_sample_size - alac->extra_bits + channels - 1;
00426     if (readsamplesize > MIN_CACHE_BITS) {
00427         av_log(avctx, AV_LOG_ERROR, "readsamplesize too big (%d)\n", readsamplesize);
00428         return -1;
00429     }
00430 
00431     if (!isnotcompressed) {
00432         /* so it is compressed */
00433         int16_t predictor_coef_table[MAX_CHANNELS][32];
00434         int predictor_coef_num[MAX_CHANNELS];
00435         int prediction_type[MAX_CHANNELS];
00436         int prediction_quantitization[MAX_CHANNELS];
00437         int ricemodifier[MAX_CHANNELS];
00438 
00439         interlacing_shift = get_bits(&alac->gb, 8);
00440         interlacing_leftweight = get_bits(&alac->gb, 8);
00441 
00442         for (ch = 0; ch < channels; ch++) {
00443             prediction_type[ch] = get_bits(&alac->gb, 4);
00444             prediction_quantitization[ch] = get_bits(&alac->gb, 4);
00445 
00446             ricemodifier[ch] = get_bits(&alac->gb, 3);
00447             predictor_coef_num[ch] = get_bits(&alac->gb, 5);
00448 
00449             /* read the predictor table */
00450             for (i = 0; i < predictor_coef_num[ch]; i++)
00451                 predictor_coef_table[ch][i] = (int16_t)get_bits(&alac->gb, 16);
00452         }
00453 
00454         if (alac->extra_bits) {
00455             for (i = 0; i < outputsamples; i++) {
00456                 if(get_bits_left(&alac->gb) <= 0)
00457                     return -1;
00458                 for (ch = 0; ch < channels; ch++)
00459                     alac->extra_bits_buffer[ch][i] = get_bits(&alac->gb, alac->extra_bits);
00460             }
00461         }
00462         for (ch = 0; ch < channels; ch++) {
00463             int ret = bastardized_rice_decompress(alac,
00464                                         alac->predicterror_buffer[ch],
00465                                         outputsamples,
00466                                         readsamplesize,
00467                                         alac->setinfo_rice_initialhistory,
00468                                         alac->setinfo_rice_kmodifier,
00469                                         ricemodifier[ch] * alac->setinfo_rice_historymult / 4,
00470                                         (1 << alac->setinfo_rice_kmodifier) - 1);
00471             if(ret<0)
00472                 return ret;
00473 
00474             /* adaptive FIR filter */
00475             if (prediction_type[ch] == 15) {
00476                 /* Prediction type 15 runs the adaptive FIR twice.
00477                  * The first pass uses the special-case coef_num = 31, while
00478                  * the second pass uses the coefs from the bitstream.
00479                  *
00480                  * However, this prediction type is not currently used by the
00481                  * reference encoder.
00482                  */
00483                 predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
00484                                                alac->predicterror_buffer[ch],
00485                                                outputsamples, readsamplesize,
00486                                                NULL, 31, 0);
00487             } else if (prediction_type[ch] > 0) {
00488                 av_log(avctx, AV_LOG_WARNING, "unknown prediction type: %i\n",
00489                        prediction_type[ch]);
00490             }
00491             predictor_decompress_fir_adapt(alac->predicterror_buffer[ch],
00492                                            alac->outputsamples_buffer[ch],
00493                                            outputsamples, readsamplesize,
00494                                            predictor_coef_table[ch],
00495                                            predictor_coef_num[ch],
00496                                            prediction_quantitization[ch]);
00497         }
00498     } else {
00499         /* not compressed, easy case */
00500         for (i = 0; i < outputsamples; i++) {
00501             if(get_bits_left(&alac->gb) <= 0)
00502                 return -1;
00503             for (ch = 0; ch < channels; ch++) {
00504                 alac->outputsamples_buffer[ch][i] = get_sbits_long(&alac->gb,
00505                                                                    alac->setinfo_sample_size);
00506             }
00507         }
00508         alac->extra_bits = 0;
00509         interlacing_shift = 0;
00510         interlacing_leftweight = 0;
00511     }
00512     if (get_bits(&alac->gb, 3) != 7)
00513         av_log(avctx, AV_LOG_ERROR, "Error : Wrong End Of Frame\n");
00514 
00515     if (channels == 2 && interlacing_leftweight) {
00516         decorrelate_stereo(alac->outputsamples_buffer, outputsamples,
00517                            interlacing_shift, interlacing_leftweight);
00518     }
00519 
00520     if (alac->extra_bits) {
00521         append_extra_bits(alac->outputsamples_buffer, alac->extra_bits_buffer,
00522                           alac->extra_bits, alac->numchannels, outputsamples);
00523     }
00524 
00525     switch(alac->setinfo_sample_size) {
00526     case 16:
00527         if (channels == 2) {
00528             interleave_stereo_16(alac->outputsamples_buffer,
00529                                  (int16_t *)alac->frame.data[0], outputsamples);
00530         } else {
00531             int16_t *outbuffer = (int16_t *)alac->frame.data[0];
00532             for (i = 0; i < outputsamples; i++) {
00533                 outbuffer[i] = alac->outputsamples_buffer[0][i];
00534             }
00535         }
00536         break;
00537     case 24:
00538         if (channels == 2) {
00539             interleave_stereo_24(alac->outputsamples_buffer,
00540                                  (int32_t *)alac->frame.data[0], outputsamples);
00541         } else {
00542             int32_t *outbuffer = (int32_t *)alac->frame.data[0];
00543             for (i = 0; i < outputsamples; i++)
00544                 outbuffer[i] = alac->outputsamples_buffer[0][i] << 8;
00545         }
00546         break;
00547     case 32:
00548         if (channels == 2) {
00549             interleave_stereo_32(alac->outputsamples_buffer,
00550                                  (int32_t *)alac->frame.data[0], outputsamples);
00551         } else {
00552             int32_t *outbuffer = (int32_t *)alac->frame.data[0];
00553             for (i = 0; i < outputsamples; i++)
00554                 outbuffer[i] = alac->outputsamples_buffer[0][i];
00555         }
00556         break;
00557     }
00558 
00559     if (input_buffer_size * 8 - get_bits_count(&alac->gb) > 8)
00560         av_log(avctx, AV_LOG_ERROR, "Error : %d bits left\n", input_buffer_size * 8 - get_bits_count(&alac->gb));
00561 
00562     *got_frame_ptr   = 1;
00563     *(AVFrame *)data = alac->frame;
00564 
00565     return input_buffer_size;
00566 }
00567 
00568 static av_cold int alac_decode_close(AVCodecContext *avctx)
00569 {
00570     ALACContext *alac = avctx->priv_data;
00571 
00572     int ch;
00573     for (ch = 0; ch < alac->numchannels; ch++) {
00574         av_freep(&alac->predicterror_buffer[ch]);
00575         av_freep(&alac->outputsamples_buffer[ch]);
00576         av_freep(&alac->extra_bits_buffer[ch]);
00577     }
00578 
00579     return 0;
00580 }
00581 
00582 static int allocate_buffers(ALACContext *alac)
00583 {
00584     int ch;
00585     for (ch = 0; ch < alac->numchannels; ch++) {
00586         int buf_size = alac->setinfo_max_samples_per_frame * sizeof(int32_t);
00587 
00588         FF_ALLOC_OR_GOTO(alac->avctx, alac->predicterror_buffer[ch],
00589                          buf_size, buf_alloc_fail);
00590 
00591         FF_ALLOC_OR_GOTO(alac->avctx, alac->outputsamples_buffer[ch],
00592                          buf_size, buf_alloc_fail);
00593 
00594         FF_ALLOC_OR_GOTO(alac->avctx, alac->extra_bits_buffer[ch],
00595                          buf_size, buf_alloc_fail);
00596     }
00597     return 0;
00598 buf_alloc_fail:
00599     alac_decode_close(alac->avctx);
00600     return AVERROR(ENOMEM);
00601 }
00602 
00603 static int alac_set_info(ALACContext *alac)
00604 {
00605     const unsigned char *ptr = alac->avctx->extradata;
00606 
00607     ptr += 4; /* size */
00608     ptr += 4; /* alac */
00609     ptr += 4; /* version */
00610 
00611     if(AV_RB32(ptr) >= UINT_MAX/4){
00612         av_log(alac->avctx, AV_LOG_ERROR, "setinfo_max_samples_per_frame too large\n");
00613         return -1;
00614     }
00615 
00616     /* buffer size / 2 ? */
00617     alac->setinfo_max_samples_per_frame = bytestream_get_be32(&ptr);
00618     ptr++;                          /* compatible version */
00619     alac->setinfo_sample_size           = *ptr++;
00620     alac->setinfo_rice_historymult      = *ptr++;
00621     alac->setinfo_rice_initialhistory   = *ptr++;
00622     alac->setinfo_rice_kmodifier        = *ptr++;
00623     alac->numchannels                   = *ptr++;
00624     bytestream_get_be16(&ptr);      /* maxRun */
00625     bytestream_get_be32(&ptr);      /* max coded frame size */
00626     bytestream_get_be32(&ptr);      /* average bitrate */
00627     bytestream_get_be32(&ptr);      /* samplerate */
00628 
00629     return 0;
00630 }
00631 
00632 static av_cold int alac_decode_init(AVCodecContext * avctx)
00633 {
00634     int ret;
00635     ALACContext *alac = avctx->priv_data;
00636     alac->avctx = avctx;
00637 
00638     /* initialize from the extradata */
00639     if (alac->avctx->extradata_size != ALAC_EXTRADATA_SIZE) {
00640         av_log(avctx, AV_LOG_ERROR, "alac: expected %d extradata bytes\n",
00641             ALAC_EXTRADATA_SIZE);
00642         return -1;
00643     }
00644     if (alac_set_info(alac)) {
00645         av_log(avctx, AV_LOG_ERROR, "alac: set_info failed\n");
00646         return -1;
00647     }
00648 
00649     switch (alac->setinfo_sample_size) {
00650     case 16: avctx->sample_fmt    = AV_SAMPLE_FMT_S16;
00651              break;
00652     case 32:
00653     case 24: avctx->sample_fmt    = AV_SAMPLE_FMT_S32;
00654              break;
00655     default: av_log_ask_for_sample(avctx, "Sample depth %d is not supported.\n",
00656                                    alac->setinfo_sample_size);
00657              return AVERROR_PATCHWELCOME;
00658     }
00659 
00660     if (alac->numchannels < 1) {
00661         av_log(avctx, AV_LOG_WARNING, "Invalid channel count\n");
00662         alac->numchannels = avctx->channels;
00663     } else {
00664         if (alac->numchannels > MAX_CHANNELS)
00665             alac->numchannels = avctx->channels;
00666         else
00667             avctx->channels = alac->numchannels;
00668     }
00669     if (avctx->channels > MAX_CHANNELS) {
00670         av_log(avctx, AV_LOG_ERROR, "Unsupported channel count: %d\n",
00671                avctx->channels);
00672         return AVERROR_PATCHWELCOME;
00673     }
00674 
00675     if ((ret = allocate_buffers(alac)) < 0) {
00676         av_log(avctx, AV_LOG_ERROR, "Error allocating buffers\n");
00677         return ret;
00678     }
00679 
00680     avcodec_get_frame_defaults(&alac->frame);
00681     avctx->coded_frame = &alac->frame;
00682 
00683     return 0;
00684 }
00685 
00686 AVCodec ff_alac_decoder = {
00687     .name           = "alac",
00688     .type           = AVMEDIA_TYPE_AUDIO,
00689     .id             = CODEC_ID_ALAC,
00690     .priv_data_size = sizeof(ALACContext),
00691     .init           = alac_decode_init,
00692     .close          = alac_decode_close,
00693     .decode         = alac_decode_frame,
00694     .capabilities   = CODEC_CAP_DR1,
00695     .long_name = NULL_IF_CONFIG_SMALL("ALAC (Apple Lossless Audio Codec)"),
00696 };