libavcodec/alsdec.c
Go to the documentation of this file.
00001 /*
00002  * MPEG-4 ALS decoder
00003  * Copyright (c) 2009 Thilo Borgmann <thilo.borgmann _at_ googlemail.com>
00004  *
00005  * This file is part of Libav.
00006  *
00007  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 
00029 //#define DEBUG
00030 
00031 
00032 #include "avcodec.h"
00033 #include "get_bits.h"
00034 #include "unary.h"
00035 #include "mpeg4audio.h"
00036 #include "bytestream.h"
00037 #include "bgmc.h"
00038 #include "dsputil.h"
00039 #include "libavutil/samplefmt.h"
00040 #include "libavutil/crc.h"
00041 
00042 #include <stdint.h>
00043 
00048 static const int8_t parcor_rice_table[3][20][2] = {
00049     { {-52, 4}, {-29, 5}, {-31, 4}, { 19, 4}, {-16, 4},
00050       { 12, 3}, { -7, 3}, {  9, 3}, { -5, 3}, {  6, 3},
00051       { -4, 3}, {  3, 3}, { -3, 2}, {  3, 2}, { -2, 2},
00052       {  3, 2}, { -1, 2}, {  2, 2}, { -1, 2}, {  2, 2} },
00053     { {-58, 3}, {-42, 4}, {-46, 4}, { 37, 5}, {-36, 4},
00054       { 29, 4}, {-29, 4}, { 25, 4}, {-23, 4}, { 20, 4},
00055       {-17, 4}, { 16, 4}, {-12, 4}, { 12, 3}, {-10, 4},
00056       {  7, 3}, { -4, 4}, {  3, 3}, { -1, 3}, {  1, 3} },
00057     { {-59, 3}, {-45, 5}, {-50, 4}, { 38, 4}, {-39, 4},
00058       { 32, 4}, {-30, 4}, { 25, 3}, {-23, 3}, { 20, 3},
00059       {-20, 3}, { 16, 3}, {-13, 3}, { 10, 3}, { -7, 3},
00060       {  3, 3}, {  0, 3}, { -1, 3}, {  2, 3}, { -1, 2} }
00061 };
00062 
00063 
00069 static const int16_t parcor_scaled_values[] = {
00070     -1048544 / 32, -1048288 / 32, -1047776 / 32, -1047008 / 32,
00071     -1045984 / 32, -1044704 / 32, -1043168 / 32, -1041376 / 32,
00072     -1039328 / 32, -1037024 / 32, -1034464 / 32, -1031648 / 32,
00073     -1028576 / 32, -1025248 / 32, -1021664 / 32, -1017824 / 32,
00074     -1013728 / 32, -1009376 / 32, -1004768 / 32,  -999904 / 32,
00075      -994784 / 32,  -989408 / 32,  -983776 / 32,  -977888 / 32,
00076      -971744 / 32,  -965344 / 32,  -958688 / 32,  -951776 / 32,
00077      -944608 / 32,  -937184 / 32,  -929504 / 32,  -921568 / 32,
00078      -913376 / 32,  -904928 / 32,  -896224 / 32,  -887264 / 32,
00079      -878048 / 32,  -868576 / 32,  -858848 / 32,  -848864 / 32,
00080      -838624 / 32,  -828128 / 32,  -817376 / 32,  -806368 / 32,
00081      -795104 / 32,  -783584 / 32,  -771808 / 32,  -759776 / 32,
00082      -747488 / 32,  -734944 / 32,  -722144 / 32,  -709088 / 32,
00083      -695776 / 32,  -682208 / 32,  -668384 / 32,  -654304 / 32,
00084      -639968 / 32,  -625376 / 32,  -610528 / 32,  -595424 / 32,
00085      -580064 / 32,  -564448 / 32,  -548576 / 32,  -532448 / 32,
00086      -516064 / 32,  -499424 / 32,  -482528 / 32,  -465376 / 32,
00087      -447968 / 32,  -430304 / 32,  -412384 / 32,  -394208 / 32,
00088      -375776 / 32,  -357088 / 32,  -338144 / 32,  -318944 / 32,
00089      -299488 / 32,  -279776 / 32,  -259808 / 32,  -239584 / 32,
00090      -219104 / 32,  -198368 / 32,  -177376 / 32,  -156128 / 32,
00091      -134624 / 32,  -112864 / 32,   -90848 / 32,   -68576 / 32,
00092       -46048 / 32,   -23264 / 32,     -224 / 32,    23072 / 32,
00093        46624 / 32,    70432 / 32,    94496 / 32,   118816 / 32,
00094       143392 / 32,   168224 / 32,   193312 / 32,   218656 / 32,
00095       244256 / 32,   270112 / 32,   296224 / 32,   322592 / 32,
00096       349216 / 32,   376096 / 32,   403232 / 32,   430624 / 32,
00097       458272 / 32,   486176 / 32,   514336 / 32,   542752 / 32,
00098       571424 / 32,   600352 / 32,   629536 / 32,   658976 / 32,
00099       688672 / 32,   718624 / 32,   748832 / 32,   779296 / 32,
00100       810016 / 32,   840992 / 32,   872224 / 32,   903712 / 32,
00101       935456 / 32,   967456 / 32,   999712 / 32,  1032224 / 32
00102 };
00103 
00104 
00108 static const uint8_t ltp_gain_values [4][4] = {
00109     { 0,  8, 16,  24},
00110     {32, 40, 48,  56},
00111     {64, 70, 76,  82},
00112     {88, 92, 96, 100}
00113 };
00114 
00115 
00119 static const int16_t mcc_weightings[] = {
00120     204,  192,  179,  166,  153,  140,  128,  115,
00121     102,   89,   76,   64,   51,   38,   25,   12,
00122       0,  -12,  -25,  -38,  -51,  -64,  -76,  -89,
00123    -102, -115, -128, -140, -153, -166, -179, -192
00124 };
00125 
00126 
00129 static const uint8_t tail_code[16][6] = {
00130     { 74, 44, 25, 13,  7, 3},
00131     { 68, 42, 24, 13,  7, 3},
00132     { 58, 39, 23, 13,  7, 3},
00133     {126, 70, 37, 19, 10, 5},
00134     {132, 70, 37, 20, 10, 5},
00135     {124, 70, 38, 20, 10, 5},
00136     {120, 69, 37, 20, 11, 5},
00137     {116, 67, 37, 20, 11, 5},
00138     {108, 66, 36, 20, 10, 5},
00139     {102, 62, 36, 20, 10, 5},
00140     { 88, 58, 34, 19, 10, 5},
00141     {162, 89, 49, 25, 13, 7},
00142     {156, 87, 49, 26, 14, 7},
00143     {150, 86, 47, 26, 14, 7},
00144     {142, 84, 47, 26, 14, 7},
00145     {131, 79, 46, 26, 14, 7}
00146 };
00147 
00148 
00149 enum RA_Flag {
00150     RA_FLAG_NONE,
00151     RA_FLAG_FRAMES,
00152     RA_FLAG_HEADER
00153 };
00154 
00155 
00156 typedef struct {
00157     uint32_t samples;         
00158     int resolution;           
00159     int floating;             
00160     int msb_first;            
00161     int frame_length;         
00162     int ra_distance;          
00163     enum RA_Flag ra_flag;     
00164     int adapt_order;          
00165     int coef_table;           
00166     int long_term_prediction; 
00167     int max_order;            
00168     int block_switching;      
00169     int bgmc;                 
00170     int sb_part;              
00171     int joint_stereo;         
00172     int mc_coding;            
00173     int chan_config;          
00174     int chan_sort;            
00175     int rlslms;               
00176     int chan_config_info;     
00177     int *chan_pos;            
00178     int crc_enabled;          
00179 } ALSSpecificConfig;
00180 
00181 
00182 typedef struct {
00183     int stop_flag;
00184     int master_channel;
00185     int time_diff_flag;
00186     int time_diff_sign;
00187     int time_diff_index;
00188     int weighting[6];
00189 } ALSChannelData;
00190 
00191 
00192 typedef struct {
00193     AVCodecContext *avctx;
00194     AVFrame frame;
00195     ALSSpecificConfig sconf;
00196     GetBitContext gb;
00197     DSPContext dsp;
00198     const AVCRC *crc_table;
00199     uint32_t crc_org;               
00200     uint32_t crc;                   
00201     unsigned int cur_frame_length;  
00202     unsigned int frame_id;          
00203     unsigned int js_switch;         
00204     unsigned int num_blocks;        
00205     unsigned int s_max;             
00206     uint8_t *bgmc_lut;              
00207     int *bgmc_lut_status;           
00208     int ltp_lag_length;             
00209     int *const_block;               
00210     unsigned int *shift_lsbs;       
00211     unsigned int *opt_order;        
00212     int *store_prev_samples;        
00213     int *use_ltp;                   
00214     int *ltp_lag;                   
00215     int **ltp_gain;                 
00216     int *ltp_gain_buffer;           
00217     int32_t **quant_cof;            
00218     int32_t *quant_cof_buffer;      
00219     int32_t **lpc_cof;              
00220     int32_t *lpc_cof_buffer;        
00221     int32_t *lpc_cof_reversed_buffer; 
00222     ALSChannelData **chan_data;     
00223     ALSChannelData *chan_data_buffer; 
00224     int *reverted_channels;         
00225     int32_t *prev_raw_samples;      
00226     int32_t **raw_samples;          
00227     int32_t *raw_buffer;            
00228     uint8_t *crc_buffer;            
00229 } ALSDecContext;
00230 
00231 
00232 typedef struct {
00233     unsigned int block_length;      
00234     unsigned int ra_block;          
00235     int          *const_block;      
00236     int          js_blocks;         
00237     unsigned int *shift_lsbs;       
00238     unsigned int *opt_order;        
00239     int          *store_prev_samples;
00240     int          *use_ltp;          
00241     int          *ltp_lag;          
00242     int          *ltp_gain;         
00243     int32_t      *quant_cof;        
00244     int32_t      *lpc_cof;          
00245     int32_t      *raw_samples;      
00246     int32_t      *prev_raw_samples; 
00247     int32_t      *raw_other;        
00248 } ALSBlockData;
00249 
00250 
00251 static av_cold void dprint_specific_config(ALSDecContext *ctx)
00252 {
00253 #ifdef DEBUG
00254     AVCodecContext *avctx    = ctx->avctx;
00255     ALSSpecificConfig *sconf = &ctx->sconf;
00256 
00257     av_dlog(avctx, "resolution = %i\n",           sconf->resolution);
00258     av_dlog(avctx, "floating = %i\n",             sconf->floating);
00259     av_dlog(avctx, "frame_length = %i\n",         sconf->frame_length);
00260     av_dlog(avctx, "ra_distance = %i\n",          sconf->ra_distance);
00261     av_dlog(avctx, "ra_flag = %i\n",              sconf->ra_flag);
00262     av_dlog(avctx, "adapt_order = %i\n",          sconf->adapt_order);
00263     av_dlog(avctx, "coef_table = %i\n",           sconf->coef_table);
00264     av_dlog(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
00265     av_dlog(avctx, "max_order = %i\n",            sconf->max_order);
00266     av_dlog(avctx, "block_switching = %i\n",      sconf->block_switching);
00267     av_dlog(avctx, "bgmc = %i\n",                 sconf->bgmc);
00268     av_dlog(avctx, "sb_part = %i\n",              sconf->sb_part);
00269     av_dlog(avctx, "joint_stereo = %i\n",         sconf->joint_stereo);
00270     av_dlog(avctx, "mc_coding = %i\n",            sconf->mc_coding);
00271     av_dlog(avctx, "chan_config = %i\n",          sconf->chan_config);
00272     av_dlog(avctx, "chan_sort = %i\n",            sconf->chan_sort);
00273     av_dlog(avctx, "RLSLMS = %i\n",               sconf->rlslms);
00274     av_dlog(avctx, "chan_config_info = %i\n",     sconf->chan_config_info);
00275 #endif
00276 }
00277 
00278 
00281 static av_cold int read_specific_config(ALSDecContext *ctx)
00282 {
00283     GetBitContext gb;
00284     uint64_t ht_size;
00285     int i, config_offset;
00286     MPEG4AudioConfig m4ac;
00287     ALSSpecificConfig *sconf = &ctx->sconf;
00288     AVCodecContext *avctx    = ctx->avctx;
00289     uint32_t als_id, header_size, trailer_size;
00290 
00291     init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
00292 
00293     config_offset = avpriv_mpeg4audio_get_config(&m4ac, avctx->extradata,
00294                                                  avctx->extradata_size * 8, 1);
00295 
00296     if (config_offset < 0)
00297         return AVERROR_INVALIDDATA;
00298 
00299     skip_bits_long(&gb, config_offset);
00300 
00301     if (get_bits_left(&gb) < (30 << 3))
00302         return AVERROR_INVALIDDATA;
00303 
00304     // read the fixed items
00305     als_id                      = get_bits_long(&gb, 32);
00306     avctx->sample_rate          = m4ac.sample_rate;
00307     skip_bits_long(&gb, 32); // sample rate already known
00308     sconf->samples              = get_bits_long(&gb, 32);
00309     avctx->channels             = m4ac.channels;
00310     skip_bits(&gb, 16);      // number of channels already knwon
00311     skip_bits(&gb, 3);       // skip file_type
00312     sconf->resolution           = get_bits(&gb, 3);
00313     sconf->floating             = get_bits1(&gb);
00314     sconf->msb_first            = get_bits1(&gb);
00315     sconf->frame_length         = get_bits(&gb, 16) + 1;
00316     sconf->ra_distance          = get_bits(&gb, 8);
00317     sconf->ra_flag              = get_bits(&gb, 2);
00318     sconf->adapt_order          = get_bits1(&gb);
00319     sconf->coef_table           = get_bits(&gb, 2);
00320     sconf->long_term_prediction = get_bits1(&gb);
00321     sconf->max_order            = get_bits(&gb, 10);
00322     sconf->block_switching      = get_bits(&gb, 2);
00323     sconf->bgmc                 = get_bits1(&gb);
00324     sconf->sb_part              = get_bits1(&gb);
00325     sconf->joint_stereo         = get_bits1(&gb);
00326     sconf->mc_coding            = get_bits1(&gb);
00327     sconf->chan_config          = get_bits1(&gb);
00328     sconf->chan_sort            = get_bits1(&gb);
00329     sconf->crc_enabled          = get_bits1(&gb);
00330     sconf->rlslms               = get_bits1(&gb);
00331     skip_bits(&gb, 5);       // skip 5 reserved bits
00332     skip_bits1(&gb);         // skip aux_data_enabled
00333 
00334 
00335     // check for ALSSpecificConfig struct
00336     if (als_id != MKBETAG('A','L','S','\0'))
00337         return AVERROR_INVALIDDATA;
00338 
00339     ctx->cur_frame_length = sconf->frame_length;
00340 
00341     // read channel config
00342     if (sconf->chan_config)
00343         sconf->chan_config_info = get_bits(&gb, 16);
00344     // TODO: use this to set avctx->channel_layout
00345 
00346 
00347     // read channel sorting
00348     if (sconf->chan_sort && avctx->channels > 1) {
00349         int chan_pos_bits = av_ceil_log2(avctx->channels);
00350         int bits_needed  = avctx->channels * chan_pos_bits + 7;
00351         if (get_bits_left(&gb) < bits_needed)
00352             return AVERROR_INVALIDDATA;
00353 
00354         if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
00355             return AVERROR(ENOMEM);
00356 
00357         for (i = 0; i < avctx->channels; i++)
00358             sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
00359 
00360         align_get_bits(&gb);
00361         // TODO: use this to actually do channel sorting
00362     } else {
00363         sconf->chan_sort = 0;
00364     }
00365 
00366 
00367     // read fixed header and trailer sizes,
00368     // if size = 0xFFFFFFFF then there is no data field!
00369     if (get_bits_left(&gb) < 64)
00370         return AVERROR_INVALIDDATA;
00371 
00372     header_size  = get_bits_long(&gb, 32);
00373     trailer_size = get_bits_long(&gb, 32);
00374     if (header_size  == 0xFFFFFFFF)
00375         header_size  = 0;
00376     if (trailer_size == 0xFFFFFFFF)
00377         trailer_size = 0;
00378 
00379     ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3;
00380 
00381 
00382     // skip the header and trailer data
00383     if (get_bits_left(&gb) < ht_size)
00384         return AVERROR_INVALIDDATA;
00385 
00386     if (ht_size > INT32_MAX)
00387         return AVERROR_PATCHWELCOME;
00388 
00389     skip_bits_long(&gb, ht_size);
00390 
00391 
00392     // initialize CRC calculation
00393     if (sconf->crc_enabled) {
00394         if (get_bits_left(&gb) < 32)
00395             return AVERROR_INVALIDDATA;
00396 
00397         if (avctx->err_recognition & AV_EF_CRCCHECK) {
00398             ctx->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
00399             ctx->crc       = 0xFFFFFFFF;
00400             ctx->crc_org   = ~get_bits_long(&gb, 32);
00401         } else
00402             skip_bits_long(&gb, 32);
00403     }
00404 
00405 
00406     // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
00407 
00408     dprint_specific_config(ctx);
00409 
00410     return 0;
00411 }
00412 
00413 
00416 static int check_specific_config(ALSDecContext *ctx)
00417 {
00418     ALSSpecificConfig *sconf = &ctx->sconf;
00419     int error = 0;
00420 
00421     // report unsupported feature and set error value
00422     #define MISSING_ERR(cond, str, errval)              \
00423     {                                                   \
00424         if (cond) {                                     \
00425             av_log_missing_feature(ctx->avctx, str, 0); \
00426             error = errval;                             \
00427         }                                               \
00428     }
00429 
00430     MISSING_ERR(sconf->floating,             "Floating point decoding",     -1);
00431     MISSING_ERR(sconf->rlslms,               "Adaptive RLS-LMS prediction", -1);
00432     MISSING_ERR(sconf->chan_sort,            "Channel sorting",              0);
00433 
00434     return error;
00435 }
00436 
00437 
00441 static void parse_bs_info(const uint32_t bs_info, unsigned int n,
00442                           unsigned int div, unsigned int **div_blocks,
00443                           unsigned int *num_blocks)
00444 {
00445     if (n < 31 && ((bs_info << n) & 0x40000000)) {
00446         // if the level is valid and the investigated bit n is set
00447         // then recursively check both children at bits (2n+1) and (2n+2)
00448         n   *= 2;
00449         div += 1;
00450         parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
00451         parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
00452     } else {
00453         // else the bit is not set or the last level has been reached
00454         // (bit implicitly not set)
00455         **div_blocks = div;
00456         (*div_blocks)++;
00457         (*num_blocks)++;
00458     }
00459 }
00460 
00461 
00464 static int32_t decode_rice(GetBitContext *gb, unsigned int k)
00465 {
00466     int max = get_bits_left(gb) - k;
00467     int q   = get_unary(gb, 0, max);
00468     int r   = k ? get_bits1(gb) : !(q & 1);
00469 
00470     if (k > 1) {
00471         q <<= (k - 1);
00472         q  += get_bits_long(gb, k - 1);
00473     } else if (!k) {
00474         q >>= 1;
00475     }
00476     return r ? q : ~q;
00477 }
00478 
00479 
00482 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
00483 {
00484     int i, j;
00485 
00486     for (i = 0, j = k - 1; i < j; i++, j--) {
00487         int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
00488         cof[j]  += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
00489         cof[i]  += tmp1;
00490     }
00491     if (i == j)
00492         cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
00493 
00494     cof[k] = par[k];
00495 }
00496 
00497 
00502 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
00503                             uint32_t *bs_info)
00504 {
00505     ALSSpecificConfig *sconf     = &ctx->sconf;
00506     GetBitContext *gb            = &ctx->gb;
00507     unsigned int *ptr_div_blocks = div_blocks;
00508     unsigned int b;
00509 
00510     if (sconf->block_switching) {
00511         unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
00512         *bs_info = get_bits_long(gb, bs_info_len);
00513         *bs_info <<= (32 - bs_info_len);
00514     }
00515 
00516     ctx->num_blocks = 0;
00517     parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
00518 
00519     // The last frame may have an overdetermined block structure given in
00520     // the bitstream. In that case the defined block structure would need
00521     // more samples than available to be consistent.
00522     // The block structure is actually used but the block sizes are adapted
00523     // to fit the actual number of available samples.
00524     // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
00525     // This results in the actual block sizes:    2 2 1 0.
00526     // This is not specified in 14496-3 but actually done by the reference
00527     // codec RM22 revision 2.
00528     // This appears to happen in case of an odd number of samples in the last
00529     // frame which is actually not allowed by the block length switching part
00530     // of 14496-3.
00531     // The ALS conformance files feature an odd number of samples in the last
00532     // frame.
00533 
00534     for (b = 0; b < ctx->num_blocks; b++)
00535         div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
00536 
00537     if (ctx->cur_frame_length != ctx->sconf.frame_length) {
00538         unsigned int remaining = ctx->cur_frame_length;
00539 
00540         for (b = 0; b < ctx->num_blocks; b++) {
00541             if (remaining <= div_blocks[b]) {
00542                 div_blocks[b] = remaining;
00543                 ctx->num_blocks = b + 1;
00544                 break;
00545             }
00546 
00547             remaining -= div_blocks[b];
00548         }
00549     }
00550 }
00551 
00552 
00555 static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00556 {
00557     ALSSpecificConfig *sconf = &ctx->sconf;
00558     AVCodecContext *avctx    = ctx->avctx;
00559     GetBitContext *gb        = &ctx->gb;
00560 
00561     *bd->raw_samples = 0;
00562     *bd->const_block = get_bits1(gb);    // 1 = constant value, 0 = zero block (silence)
00563     bd->js_blocks    = get_bits1(gb);
00564 
00565     // skip 5 reserved bits
00566     skip_bits(gb, 5);
00567 
00568     if (*bd->const_block) {
00569         unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
00570         *bd->raw_samples = get_sbits_long(gb, const_val_bits);
00571     }
00572 
00573     // ensure constant block decoding by reusing this field
00574     *bd->const_block = 1;
00575 }
00576 
00577 
00580 static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00581 {
00582     int      smp = bd->block_length - 1;
00583     int32_t  val = *bd->raw_samples;
00584     int32_t *dst = bd->raw_samples + 1;
00585 
00586     // write raw samples into buffer
00587     for (; smp; smp--)
00588         *dst++ = val;
00589 }
00590 
00591 
00594 static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00595 {
00596     ALSSpecificConfig *sconf = &ctx->sconf;
00597     AVCodecContext *avctx    = ctx->avctx;
00598     GetBitContext *gb        = &ctx->gb;
00599     unsigned int k;
00600     unsigned int s[8];
00601     unsigned int sx[8];
00602     unsigned int sub_blocks, log2_sub_blocks, sb_length;
00603     unsigned int start      = 0;
00604     unsigned int opt_order;
00605     int          sb;
00606     int32_t      *quant_cof = bd->quant_cof;
00607     int32_t      *current_res;
00608 
00609 
00610     // ensure variable block decoding by reusing this field
00611     *bd->const_block = 0;
00612 
00613     *bd->opt_order  = 1;
00614     bd->js_blocks   = get_bits1(gb);
00615 
00616     opt_order       = *bd->opt_order;
00617 
00618     // determine the number of subblocks for entropy decoding
00619     if (!sconf->bgmc && !sconf->sb_part) {
00620         log2_sub_blocks = 0;
00621     } else {
00622         if (sconf->bgmc && sconf->sb_part)
00623             log2_sub_blocks = get_bits(gb, 2);
00624         else
00625             log2_sub_blocks = 2 * get_bits1(gb);
00626     }
00627 
00628     sub_blocks = 1 << log2_sub_blocks;
00629 
00630     // do not continue in case of a damaged stream since
00631     // block_length must be evenly divisible by sub_blocks
00632     if (bd->block_length & (sub_blocks - 1)) {
00633         av_log(avctx, AV_LOG_WARNING,
00634                "Block length is not evenly divisible by the number of subblocks.\n");
00635         return AVERROR_INVALIDDATA;
00636     }
00637 
00638     sb_length = bd->block_length >> log2_sub_blocks;
00639 
00640     if (sconf->bgmc) {
00641         s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
00642         for (k = 1; k < sub_blocks; k++)
00643             s[k] = s[k - 1] + decode_rice(gb, 2);
00644 
00645         for (k = 0; k < sub_blocks; k++) {
00646             sx[k]   = s[k] & 0x0F;
00647             s [k] >>= 4;
00648         }
00649     } else {
00650         s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
00651         for (k = 1; k < sub_blocks; k++)
00652             s[k] = s[k - 1] + decode_rice(gb, 0);
00653     }
00654     for (k = 1; k < sub_blocks; k++)
00655         if (s[k] > 32) {
00656             av_log(avctx, AV_LOG_ERROR, "k invalid for rice code.\n");
00657             return AVERROR_INVALIDDATA;
00658         }
00659 
00660     if (get_bits1(gb))
00661         *bd->shift_lsbs = get_bits(gb, 4) + 1;
00662 
00663     *bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || *bd->shift_lsbs;
00664 
00665 
00666     if (!sconf->rlslms) {
00667         if (sconf->adapt_order) {
00668             int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
00669                                                 2, sconf->max_order + 1));
00670             *bd->opt_order       = get_bits(gb, opt_order_length);
00671             if (*bd->opt_order > sconf->max_order) {
00672                 *bd->opt_order = sconf->max_order;
00673                 av_log(avctx, AV_LOG_ERROR, "Predictor order too large!\n");
00674                 return AVERROR_INVALIDDATA;
00675             }
00676         } else {
00677             *bd->opt_order = sconf->max_order;
00678         }
00679 
00680         opt_order = *bd->opt_order;
00681 
00682         if (opt_order) {
00683             int add_base;
00684 
00685             if (sconf->coef_table == 3) {
00686                 add_base = 0x7F;
00687 
00688                 // read coefficient 0
00689                 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
00690 
00691                 // read coefficient 1
00692                 if (opt_order > 1)
00693                     quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
00694 
00695                 // read coefficients 2 to opt_order
00696                 for (k = 2; k < opt_order; k++)
00697                     quant_cof[k] = get_bits(gb, 7);
00698             } else {
00699                 int k_max;
00700                 add_base = 1;
00701 
00702                 // read coefficient 0 to 19
00703                 k_max = FFMIN(opt_order, 20);
00704                 for (k = 0; k < k_max; k++) {
00705                     int rice_param = parcor_rice_table[sconf->coef_table][k][1];
00706                     int offset     = parcor_rice_table[sconf->coef_table][k][0];
00707                     quant_cof[k] = decode_rice(gb, rice_param) + offset;
00708                     if (quant_cof[k] < -64 || quant_cof[k] > 63) {
00709                         av_log(avctx, AV_LOG_ERROR, "quant_cof %d is out of range\n", quant_cof[k]);
00710                         return AVERROR_INVALIDDATA;
00711                     }
00712                 }
00713 
00714                 // read coefficients 20 to 126
00715                 k_max = FFMIN(opt_order, 127);
00716                 for (; k < k_max; k++)
00717                     quant_cof[k] = decode_rice(gb, 2) + (k & 1);
00718 
00719                 // read coefficients 127 to opt_order
00720                 for (; k < opt_order; k++)
00721                     quant_cof[k] = decode_rice(gb, 1);
00722 
00723                 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
00724 
00725                 if (opt_order > 1)
00726                     quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
00727             }
00728 
00729             for (k = 2; k < opt_order; k++)
00730                 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
00731         }
00732     }
00733 
00734     // read LTP gain and lag values
00735     if (sconf->long_term_prediction) {
00736         *bd->use_ltp = get_bits1(gb);
00737 
00738         if (*bd->use_ltp) {
00739             int r, c;
00740 
00741             bd->ltp_gain[0]   = decode_rice(gb, 1) << 3;
00742             bd->ltp_gain[1]   = decode_rice(gb, 2) << 3;
00743 
00744             r                 = get_unary(gb, 0, 3);
00745             c                 = get_bits(gb, 2);
00746             bd->ltp_gain[2]   = ltp_gain_values[r][c];
00747 
00748             bd->ltp_gain[3]   = decode_rice(gb, 2) << 3;
00749             bd->ltp_gain[4]   = decode_rice(gb, 1) << 3;
00750 
00751             *bd->ltp_lag      = get_bits(gb, ctx->ltp_lag_length);
00752             *bd->ltp_lag     += FFMAX(4, opt_order + 1);
00753         }
00754     }
00755 
00756     // read first value and residuals in case of a random access block
00757     if (bd->ra_block) {
00758         if (opt_order)
00759             bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
00760         if (opt_order > 1)
00761             bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max));
00762         if (opt_order > 2)
00763             bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max));
00764 
00765         start = FFMIN(opt_order, 3);
00766     }
00767 
00768     // read all residuals
00769     if (sconf->bgmc) {
00770         int          delta[8];
00771         unsigned int k    [8];
00772         unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
00773 
00774         // read most significant bits
00775         unsigned int high;
00776         unsigned int low;
00777         unsigned int value;
00778 
00779         ff_bgmc_decode_init(gb, &high, &low, &value);
00780 
00781         current_res = bd->raw_samples + start;
00782 
00783         for (sb = 0; sb < sub_blocks; sb++) {
00784             unsigned int sb_len  = sb_length - (sb ? 0 : start);
00785 
00786             k    [sb] = s[sb] > b ? s[sb] - b : 0;
00787             delta[sb] = 5 - s[sb] + k[sb];
00788 
00789             ff_bgmc_decode(gb, sb_len, current_res,
00790                         delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
00791 
00792             current_res += sb_len;
00793         }
00794 
00795         ff_bgmc_decode_end(gb);
00796 
00797 
00798         // read least significant bits and tails
00799         current_res = bd->raw_samples + start;
00800 
00801         for (sb = 0; sb < sub_blocks; sb++, start = 0) {
00802             unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
00803             unsigned int cur_k         = k[sb];
00804             unsigned int cur_s         = s[sb];
00805 
00806             for (; start < sb_length; start++) {
00807                 int32_t res = *current_res;
00808 
00809                 if (res == cur_tail_code) {
00810                     unsigned int max_msb =   (2 + (sx[sb] > 2) + (sx[sb] > 10))
00811                                           << (5 - delta[sb]);
00812 
00813                     res = decode_rice(gb, cur_s);
00814 
00815                     if (res >= 0) {
00816                         res += (max_msb    ) << cur_k;
00817                     } else {
00818                         res -= (max_msb - 1) << cur_k;
00819                     }
00820                 } else {
00821                     if (res > cur_tail_code)
00822                         res--;
00823 
00824                     if (res & 1)
00825                         res = -res;
00826 
00827                     res >>= 1;
00828 
00829                     if (cur_k) {
00830                         res <<= cur_k;
00831                         res  |= get_bits_long(gb, cur_k);
00832                     }
00833                 }
00834 
00835                 *current_res++ = res;
00836             }
00837         }
00838     } else {
00839         current_res = bd->raw_samples + start;
00840 
00841         for (sb = 0; sb < sub_blocks; sb++, start = 0)
00842             for (; start < sb_length; start++)
00843                 *current_res++ = decode_rice(gb, s[sb]);
00844      }
00845 
00846     if (!sconf->mc_coding || ctx->js_switch)
00847         align_get_bits(gb);
00848 
00849     return 0;
00850 }
00851 
00852 
00855 static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00856 {
00857     ALSSpecificConfig *sconf = &ctx->sconf;
00858     unsigned int block_length = bd->block_length;
00859     unsigned int smp = 0;
00860     unsigned int k;
00861     int opt_order             = *bd->opt_order;
00862     int sb;
00863     int64_t y;
00864     int32_t *quant_cof        = bd->quant_cof;
00865     int32_t *lpc_cof          = bd->lpc_cof;
00866     int32_t *raw_samples      = bd->raw_samples;
00867     int32_t *raw_samples_end  = bd->raw_samples + bd->block_length;
00868     int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer;
00869 
00870     // reverse long-term prediction
00871     if (*bd->use_ltp) {
00872         int ltp_smp;
00873 
00874         for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
00875             int center = ltp_smp - *bd->ltp_lag;
00876             int begin  = FFMAX(0, center - 2);
00877             int end    = center + 3;
00878             int tab    = 5 - (end - begin);
00879             int base;
00880 
00881             y = 1 << 6;
00882 
00883             for (base = begin; base < end; base++, tab++)
00884                 y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
00885 
00886             raw_samples[ltp_smp] += y >> 7;
00887         }
00888     }
00889 
00890     // reconstruct all samples from residuals
00891     if (bd->ra_block) {
00892         for (smp = 0; smp < opt_order; smp++) {
00893             y = 1 << 19;
00894 
00895             for (sb = 0; sb < smp; sb++)
00896                 y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]);
00897 
00898             *raw_samples++ -= y >> 20;
00899             parcor_to_lpc(smp, quant_cof, lpc_cof);
00900         }
00901     } else {
00902         for (k = 0; k < opt_order; k++)
00903             parcor_to_lpc(k, quant_cof, lpc_cof);
00904 
00905         // store previous samples in case that they have to be altered
00906         if (*bd->store_prev_samples)
00907             memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
00908                    sizeof(*bd->prev_raw_samples) * sconf->max_order);
00909 
00910         // reconstruct difference signal for prediction (joint-stereo)
00911         if (bd->js_blocks && bd->raw_other) {
00912             int32_t *left, *right;
00913 
00914             if (bd->raw_other > raw_samples) {  // D = R - L
00915                 left  = raw_samples;
00916                 right = bd->raw_other;
00917             } else {                                // D = R - L
00918                 left  = bd->raw_other;
00919                 right = raw_samples;
00920             }
00921 
00922             for (sb = -1; sb >= -sconf->max_order; sb--)
00923                 raw_samples[sb] = right[sb] - left[sb];
00924         }
00925 
00926         // reconstruct shifted signal
00927         if (*bd->shift_lsbs)
00928             for (sb = -1; sb >= -sconf->max_order; sb--)
00929                 raw_samples[sb] >>= *bd->shift_lsbs;
00930     }
00931 
00932     // reverse linear prediction coefficients for efficiency
00933     lpc_cof = lpc_cof + opt_order;
00934 
00935     for (sb = 0; sb < opt_order; sb++)
00936         lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)];
00937 
00938     // reconstruct raw samples
00939     raw_samples = bd->raw_samples + smp;
00940     lpc_cof     = lpc_cof_reversed + opt_order;
00941 
00942     for (; raw_samples < raw_samples_end; raw_samples++) {
00943         y = 1 << 19;
00944 
00945         for (sb = -opt_order; sb < 0; sb++)
00946             y += MUL64(lpc_cof[sb], raw_samples[sb]);
00947 
00948         *raw_samples -= y >> 20;
00949     }
00950 
00951     raw_samples = bd->raw_samples;
00952 
00953     // restore previous samples in case that they have been altered
00954     if (*bd->store_prev_samples)
00955         memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
00956                sizeof(*raw_samples) * sconf->max_order);
00957 
00958     return 0;
00959 }
00960 
00961 
00964 static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
00965 {
00966     int ret = 0;
00967     GetBitContext *gb        = &ctx->gb;
00968 
00969     *bd->shift_lsbs = 0;
00970     // read block type flag and read the samples accordingly
00971     if (get_bits1(gb)) {
00972         ret = read_var_block_data(ctx, bd);
00973     } else {
00974         read_const_block_data(ctx, bd);
00975     }
00976 
00977     return ret;
00978 }
00979 
00980 
00983 static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
00984 {
00985     unsigned int smp;
00986     int ret = 0;
00987 
00988     // read block type flag and read the samples accordingly
00989     if (*bd->const_block)
00990         decode_const_block_data(ctx, bd);
00991     else
00992         ret = decode_var_block_data(ctx, bd); // always return 0
00993 
00994     if (ret < 0)
00995         return ret;
00996 
00997     // TODO: read RLSLMS extension data
00998 
00999     if (*bd->shift_lsbs)
01000         for (smp = 0; smp < bd->block_length; smp++)
01001             bd->raw_samples[smp] <<= *bd->shift_lsbs;
01002 
01003     return 0;
01004 }
01005 
01006 
01009 static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
01010 {
01011     int ret;
01012 
01013     if ((ret = read_block(ctx, bd)) < 0)
01014         return ret;
01015 
01016     return decode_block(ctx, bd);
01017 }
01018 
01019 
01023 static void zero_remaining(unsigned int b, unsigned int b_max,
01024                            const unsigned int *div_blocks, int32_t *buf)
01025 {
01026     unsigned int count = 0;
01027 
01028     for (; b < b_max; b++)
01029         count += div_blocks[b];
01030 
01031     if (count)
01032         memset(buf, 0, sizeof(*buf) * count);
01033 }
01034 
01035 
01038 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
01039                              unsigned int c, const unsigned int *div_blocks,
01040                              unsigned int *js_blocks)
01041 {
01042     int ret;
01043     unsigned int b;
01044     ALSBlockData bd;
01045 
01046     memset(&bd, 0, sizeof(ALSBlockData));
01047 
01048     bd.ra_block         = ra_frame;
01049     bd.const_block      = ctx->const_block;
01050     bd.shift_lsbs       = ctx->shift_lsbs;
01051     bd.opt_order        = ctx->opt_order;
01052     bd.store_prev_samples = ctx->store_prev_samples;
01053     bd.use_ltp          = ctx->use_ltp;
01054     bd.ltp_lag          = ctx->ltp_lag;
01055     bd.ltp_gain         = ctx->ltp_gain[0];
01056     bd.quant_cof        = ctx->quant_cof[0];
01057     bd.lpc_cof          = ctx->lpc_cof[0];
01058     bd.prev_raw_samples = ctx->prev_raw_samples;
01059     bd.raw_samples      = ctx->raw_samples[c];
01060 
01061 
01062     for (b = 0; b < ctx->num_blocks; b++) {
01063         bd.block_length     = div_blocks[b];
01064 
01065         if ((ret = read_decode_block(ctx, &bd)) < 0) {
01066             // damaged block, write zero for the rest of the frame
01067             zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
01068             return ret;
01069         }
01070         bd.raw_samples += div_blocks[b];
01071         bd.ra_block     = 0;
01072     }
01073 
01074     return 0;
01075 }
01076 
01077 
01080 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
01081                          unsigned int c, const unsigned int *div_blocks,
01082                          unsigned int *js_blocks)
01083 {
01084     ALSSpecificConfig *sconf = &ctx->sconf;
01085     unsigned int offset = 0;
01086     unsigned int b;
01087     int ret;
01088     ALSBlockData bd[2];
01089 
01090     memset(bd, 0, 2 * sizeof(ALSBlockData));
01091 
01092     bd[0].ra_block         = ra_frame;
01093     bd[0].const_block      = ctx->const_block;
01094     bd[0].shift_lsbs       = ctx->shift_lsbs;
01095     bd[0].opt_order        = ctx->opt_order;
01096     bd[0].store_prev_samples = ctx->store_prev_samples;
01097     bd[0].use_ltp          = ctx->use_ltp;
01098     bd[0].ltp_lag          = ctx->ltp_lag;
01099     bd[0].ltp_gain         = ctx->ltp_gain[0];
01100     bd[0].quant_cof        = ctx->quant_cof[0];
01101     bd[0].lpc_cof          = ctx->lpc_cof[0];
01102     bd[0].prev_raw_samples = ctx->prev_raw_samples;
01103     bd[0].js_blocks        = *js_blocks;
01104 
01105     bd[1].ra_block         = ra_frame;
01106     bd[1].const_block      = ctx->const_block;
01107     bd[1].shift_lsbs       = ctx->shift_lsbs;
01108     bd[1].opt_order        = ctx->opt_order;
01109     bd[1].store_prev_samples = ctx->store_prev_samples;
01110     bd[1].use_ltp          = ctx->use_ltp;
01111     bd[1].ltp_lag          = ctx->ltp_lag;
01112     bd[1].ltp_gain         = ctx->ltp_gain[0];
01113     bd[1].quant_cof        = ctx->quant_cof[0];
01114     bd[1].lpc_cof          = ctx->lpc_cof[0];
01115     bd[1].prev_raw_samples = ctx->prev_raw_samples;
01116     bd[1].js_blocks        = *(js_blocks + 1);
01117 
01118     // decode all blocks
01119     for (b = 0; b < ctx->num_blocks; b++) {
01120         unsigned int s;
01121 
01122         bd[0].block_length = div_blocks[b];
01123         bd[1].block_length = div_blocks[b];
01124 
01125         bd[0].raw_samples  = ctx->raw_samples[c    ] + offset;
01126         bd[1].raw_samples  = ctx->raw_samples[c + 1] + offset;
01127 
01128         bd[0].raw_other    = bd[1].raw_samples;
01129         bd[1].raw_other    = bd[0].raw_samples;
01130 
01131         if ((ret = read_decode_block(ctx, &bd[0])) < 0 ||
01132             (ret = read_decode_block(ctx, &bd[1])) < 0)
01133             goto fail;
01134 
01135 
01136         // reconstruct joint-stereo blocks
01137         if (bd[0].js_blocks) {
01138             if (bd[1].js_blocks)
01139                 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
01140 
01141             for (s = 0; s < div_blocks[b]; s++)
01142                 bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
01143         } else if (bd[1].js_blocks) {
01144             for (s = 0; s < div_blocks[b]; s++)
01145                 bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
01146         }
01147 
01148         offset  += div_blocks[b];
01149         bd[0].ra_block = 0;
01150         bd[1].ra_block = 0;
01151     }
01152 
01153     // store carryover raw samples,
01154     // the others channel raw samples are stored by the calling function.
01155     memmove(ctx->raw_samples[c] - sconf->max_order,
01156             ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01157             sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01158 
01159     return 0;
01160 fail:
01161     // damaged block, write zero for the rest of the frame
01162     zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
01163     zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
01164     return ret;
01165 }
01166 
01167 static inline int als_weighting(GetBitContext *gb, int k, int off)
01168 {
01169     int idx = av_clip(decode_rice(gb, k) + off,
01170                       0, FF_ARRAY_ELEMS(mcc_weightings) - 1);
01171     return mcc_weightings[idx];
01172 }
01173 
01176 static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
01177 {
01178     GetBitContext *gb       = &ctx->gb;
01179     ALSChannelData *current = cd;
01180     unsigned int channels   = ctx->avctx->channels;
01181     int entries             = 0;
01182 
01183     while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
01184         current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
01185 
01186         if (current->master_channel >= channels) {
01187             av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
01188             return AVERROR_INVALIDDATA;
01189         }
01190 
01191         if (current->master_channel != c) {
01192             current->time_diff_flag = get_bits1(gb);
01193             current->weighting[0]   = als_weighting(gb, 1, 16);
01194             current->weighting[1]   = als_weighting(gb, 2, 14);
01195             current->weighting[2]   = als_weighting(gb, 1, 16);
01196 
01197             if (current->time_diff_flag) {
01198                 current->weighting[3] = als_weighting(gb, 1, 16);
01199                 current->weighting[4] = als_weighting(gb, 1, 16);
01200                 current->weighting[5] = als_weighting(gb, 1, 16);
01201 
01202                 current->time_diff_sign  = get_bits1(gb);
01203                 current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
01204             }
01205         }
01206 
01207         current++;
01208         entries++;
01209     }
01210 
01211     if (entries == channels) {
01212         av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
01213         return AVERROR_INVALIDDATA;
01214     }
01215 
01216     align_get_bits(gb);
01217     return 0;
01218 }
01219 
01220 
01223 static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
01224                                        ALSChannelData **cd, int *reverted,
01225                                        unsigned int offset, int c)
01226 {
01227     ALSChannelData *ch = cd[c];
01228     unsigned int   dep = 0;
01229     unsigned int channels = ctx->avctx->channels;
01230 
01231     if (reverted[c])
01232         return 0;
01233 
01234     reverted[c] = 1;
01235 
01236     while (dep < channels && !ch[dep].stop_flag) {
01237         revert_channel_correlation(ctx, bd, cd, reverted, offset,
01238                                    ch[dep].master_channel);
01239 
01240         dep++;
01241     }
01242 
01243     if (dep == channels) {
01244         av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
01245         return AVERROR_INVALIDDATA;
01246     }
01247 
01248     bd->const_block = ctx->const_block + c;
01249     bd->shift_lsbs  = ctx->shift_lsbs + c;
01250     bd->opt_order   = ctx->opt_order + c;
01251     bd->store_prev_samples = ctx->store_prev_samples + c;
01252     bd->use_ltp     = ctx->use_ltp + c;
01253     bd->ltp_lag     = ctx->ltp_lag + c;
01254     bd->ltp_gain    = ctx->ltp_gain[c];
01255     bd->lpc_cof     = ctx->lpc_cof[c];
01256     bd->quant_cof   = ctx->quant_cof[c];
01257     bd->raw_samples = ctx->raw_samples[c] + offset;
01258 
01259     dep = 0;
01260     while (!ch[dep].stop_flag) {
01261         unsigned int smp;
01262         unsigned int begin = 1;
01263         unsigned int end   = bd->block_length - 1;
01264         int64_t y;
01265         int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
01266 
01267         if (ch[dep].time_diff_flag) {
01268             int t = ch[dep].time_diff_index;
01269 
01270             if (ch[dep].time_diff_sign) {
01271                 t      = -t;
01272                 begin -= t;
01273             } else {
01274                 end   -= t;
01275             }
01276 
01277             for (smp = begin; smp < end; smp++) {
01278                 y  = (1 << 6) +
01279                      MUL64(ch[dep].weighting[0], master[smp - 1    ]) +
01280                      MUL64(ch[dep].weighting[1], master[smp        ]) +
01281                      MUL64(ch[dep].weighting[2], master[smp + 1    ]) +
01282                      MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
01283                      MUL64(ch[dep].weighting[4], master[smp     + t]) +
01284                      MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
01285 
01286                 bd->raw_samples[smp] += y >> 7;
01287             }
01288         } else {
01289             for (smp = begin; smp < end; smp++) {
01290                 y  = (1 << 6) +
01291                      MUL64(ch[dep].weighting[0], master[smp - 1]) +
01292                      MUL64(ch[dep].weighting[1], master[smp    ]) +
01293                      MUL64(ch[dep].weighting[2], master[smp + 1]);
01294 
01295                 bd->raw_samples[smp] += y >> 7;
01296             }
01297         }
01298 
01299         dep++;
01300     }
01301 
01302     return 0;
01303 }
01304 
01305 
01308 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
01309 {
01310     ALSSpecificConfig *sconf = &ctx->sconf;
01311     AVCodecContext *avctx    = ctx->avctx;
01312     GetBitContext *gb = &ctx->gb;
01313     unsigned int div_blocks[32];                
01314     unsigned int c;
01315     unsigned int js_blocks[2];
01316 
01317     uint32_t bs_info = 0;
01318     int ret;
01319 
01320     // skip the size of the ra unit if present in the frame
01321     if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
01322         skip_bits_long(gb, 32);
01323 
01324     if (sconf->mc_coding && sconf->joint_stereo) {
01325         ctx->js_switch = get_bits1(gb);
01326         align_get_bits(gb);
01327     }
01328 
01329     if (!sconf->mc_coding || ctx->js_switch) {
01330         int independent_bs = !sconf->joint_stereo;
01331 
01332         for (c = 0; c < avctx->channels; c++) {
01333             js_blocks[0] = 0;
01334             js_blocks[1] = 0;
01335 
01336             get_block_sizes(ctx, div_blocks, &bs_info);
01337 
01338             // if joint_stereo and block_switching is set, independent decoding
01339             // is signaled via the first bit of bs_info
01340             if (sconf->joint_stereo && sconf->block_switching)
01341                 if (bs_info >> 31)
01342                     independent_bs = 2;
01343 
01344             // if this is the last channel, it has to be decoded independently
01345             if (c == avctx->channels - 1)
01346                 independent_bs = 1;
01347 
01348             if (independent_bs) {
01349                 ret = decode_blocks_ind(ctx, ra_frame, c,
01350                                         div_blocks, js_blocks);
01351                 if (ret < 0)
01352                     return ret;
01353                 independent_bs--;
01354             } else {
01355                 ret = decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks);
01356                 if (ret < 0)
01357                     return ret;
01358 
01359                 c++;
01360             }
01361 
01362             // store carryover raw samples
01363             memmove(ctx->raw_samples[c] - sconf->max_order,
01364                     ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01365                     sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01366         }
01367     } else { // multi-channel coding
01368         ALSBlockData   bd;
01369         int            b;
01370         int            *reverted_channels = ctx->reverted_channels;
01371         unsigned int   offset             = 0;
01372 
01373         for (c = 0; c < avctx->channels; c++)
01374             if (ctx->chan_data[c] < ctx->chan_data_buffer) {
01375                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
01376                 return AVERROR_INVALIDDATA;
01377             }
01378 
01379         memset(&bd,               0, sizeof(ALSBlockData));
01380         memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);
01381 
01382         bd.ra_block         = ra_frame;
01383         bd.prev_raw_samples = ctx->prev_raw_samples;
01384 
01385         get_block_sizes(ctx, div_blocks, &bs_info);
01386 
01387         for (b = 0; b < ctx->num_blocks; b++) {
01388             bd.block_length = div_blocks[b];
01389             if (bd.block_length <= 0) {
01390                 av_log(ctx->avctx, AV_LOG_WARNING,
01391                        "Invalid block length %d in channel data!\n", bd.block_length);
01392                 continue;
01393             }
01394 
01395             for (c = 0; c < avctx->channels; c++) {
01396                 bd.const_block = ctx->const_block + c;
01397                 bd.shift_lsbs  = ctx->shift_lsbs + c;
01398                 bd.opt_order   = ctx->opt_order + c;
01399                 bd.store_prev_samples = ctx->store_prev_samples + c;
01400                 bd.use_ltp     = ctx->use_ltp + c;
01401                 bd.ltp_lag     = ctx->ltp_lag + c;
01402                 bd.ltp_gain    = ctx->ltp_gain[c];
01403                 bd.lpc_cof     = ctx->lpc_cof[c];
01404                 bd.quant_cof   = ctx->quant_cof[c];
01405                 bd.raw_samples = ctx->raw_samples[c] + offset;
01406                 bd.raw_other   = NULL;
01407 
01408                 read_block(ctx, &bd);
01409                 if (read_channel_data(ctx, ctx->chan_data[c], c))
01410                     return -1;
01411             }
01412 
01413             for (c = 0; c < avctx->channels; c++) {
01414                 ret = revert_channel_correlation(ctx, &bd, ctx->chan_data,
01415                                                  reverted_channels, offset, c);
01416                 if (ret < 0)
01417                     return ret;
01418             }
01419             for (c = 0; c < avctx->channels; c++) {
01420                 bd.const_block = ctx->const_block + c;
01421                 bd.shift_lsbs  = ctx->shift_lsbs + c;
01422                 bd.opt_order   = ctx->opt_order + c;
01423                 bd.store_prev_samples = ctx->store_prev_samples + c;
01424                 bd.use_ltp     = ctx->use_ltp + c;
01425                 bd.ltp_lag     = ctx->ltp_lag + c;
01426                 bd.ltp_gain    = ctx->ltp_gain[c];
01427                 bd.lpc_cof     = ctx->lpc_cof[c];
01428                 bd.quant_cof   = ctx->quant_cof[c];
01429                 bd.raw_samples = ctx->raw_samples[c] + offset;
01430                 decode_block(ctx, &bd);
01431             }
01432 
01433             memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));
01434             offset      += div_blocks[b];
01435             bd.ra_block  = 0;
01436         }
01437 
01438         // store carryover raw samples
01439         for (c = 0; c < avctx->channels; c++)
01440             memmove(ctx->raw_samples[c] - sconf->max_order,
01441                     ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01442                     sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01443     }
01444 
01445     // TODO: read_diff_float_data
01446 
01447     return 0;
01448 }
01449 
01450 
01453 static int decode_frame(AVCodecContext *avctx, void *data, int *got_frame_ptr,
01454                         AVPacket *avpkt)
01455 {
01456     ALSDecContext *ctx       = avctx->priv_data;
01457     ALSSpecificConfig *sconf = &ctx->sconf;
01458     const uint8_t *buffer    = avpkt->data;
01459     int buffer_size          = avpkt->size;
01460     int invalid_frame, ret;
01461     unsigned int c, sample, ra_frame, bytes_read, shift;
01462 
01463     init_get_bits(&ctx->gb, buffer, buffer_size * 8);
01464 
01465     // In the case that the distance between random access frames is set to zero
01466     // (sconf->ra_distance == 0) no frame is treated as a random access frame.
01467     // For the first frame, if prediction is used, all samples used from the
01468     // previous frame are assumed to be zero.
01469     ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
01470 
01471     // the last frame to decode might have a different length
01472     if (sconf->samples != 0xFFFFFFFF)
01473         ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
01474                                       sconf->frame_length);
01475     else
01476         ctx->cur_frame_length = sconf->frame_length;
01477 
01478     // decode the frame data
01479     if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
01480         av_log(ctx->avctx, AV_LOG_WARNING,
01481                "Reading frame data failed. Skipping RA unit.\n");
01482 
01483     ctx->frame_id++;
01484 
01485     /* get output buffer */
01486     ctx->frame.nb_samples = ctx->cur_frame_length;
01487     if ((ret = avctx->get_buffer(avctx, &ctx->frame)) < 0) {
01488         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
01489         return ret;
01490     }
01491 
01492     // transform decoded frame into output format
01493     #define INTERLEAVE_OUTPUT(bps)                                 \
01494     {                                                              \
01495         int##bps##_t *dest = (int##bps##_t*)ctx->frame.data[0];    \
01496         shift = bps - ctx->avctx->bits_per_raw_sample;             \
01497         for (sample = 0; sample < ctx->cur_frame_length; sample++) \
01498             for (c = 0; c < avctx->channels; c++)                  \
01499                 *dest++ = ctx->raw_samples[c][sample] << shift;    \
01500     }
01501 
01502     if (ctx->avctx->bits_per_raw_sample <= 16) {
01503         INTERLEAVE_OUTPUT(16)
01504     } else {
01505         INTERLEAVE_OUTPUT(32)
01506     }
01507 
01508     // update CRC
01509     if (sconf->crc_enabled && (avctx->err_recognition & AV_EF_CRCCHECK)) {
01510         int swap = HAVE_BIGENDIAN != sconf->msb_first;
01511 
01512         if (ctx->avctx->bits_per_raw_sample == 24) {
01513             int32_t *src = (int32_t *)ctx->frame.data[0];
01514 
01515             for (sample = 0;
01516                  sample < ctx->cur_frame_length * avctx->channels;
01517                  sample++) {
01518                 int32_t v;
01519 
01520                 if (swap)
01521                     v = av_bswap32(src[sample]);
01522                 else
01523                     v = src[sample];
01524                 if (!HAVE_BIGENDIAN)
01525                     v >>= 8;
01526 
01527                 ctx->crc = av_crc(ctx->crc_table, ctx->crc, (uint8_t*)(&v), 3);
01528             }
01529         } else {
01530             uint8_t *crc_source;
01531 
01532             if (swap) {
01533                 if (ctx->avctx->bits_per_raw_sample <= 16) {
01534                     int16_t *src  = (int16_t*) ctx->frame.data[0];
01535                     int16_t *dest = (int16_t*) ctx->crc_buffer;
01536                     for (sample = 0;
01537                          sample < ctx->cur_frame_length * avctx->channels;
01538                          sample++)
01539                         *dest++ = av_bswap16(src[sample]);
01540                 } else {
01541                     ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer,
01542                                        (uint32_t *)ctx->frame.data[0],
01543                                        ctx->cur_frame_length * avctx->channels);
01544                 }
01545                 crc_source = ctx->crc_buffer;
01546             } else {
01547                 crc_source = ctx->frame.data[0];
01548             }
01549 
01550             ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source,
01551                               ctx->cur_frame_length * avctx->channels *
01552                               av_get_bytes_per_sample(avctx->sample_fmt));
01553         }
01554 
01555 
01556         // check CRC sums if this is the last frame
01557         if (ctx->cur_frame_length != sconf->frame_length &&
01558             ctx->crc_org != ctx->crc) {
01559             av_log(avctx, AV_LOG_ERROR, "CRC error.\n");
01560         }
01561     }
01562 
01563     *got_frame_ptr   = 1;
01564     *(AVFrame *)data = ctx->frame;
01565 
01566 
01567     bytes_read = invalid_frame ? buffer_size :
01568                                  (get_bits_count(&ctx->gb) + 7) >> 3;
01569 
01570     return bytes_read;
01571 }
01572 
01573 
01576 static av_cold int decode_end(AVCodecContext *avctx)
01577 {
01578     ALSDecContext *ctx = avctx->priv_data;
01579 
01580     av_freep(&ctx->sconf.chan_pos);
01581 
01582     ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
01583 
01584     av_freep(&ctx->const_block);
01585     av_freep(&ctx->shift_lsbs);
01586     av_freep(&ctx->opt_order);
01587     av_freep(&ctx->store_prev_samples);
01588     av_freep(&ctx->use_ltp);
01589     av_freep(&ctx->ltp_lag);
01590     av_freep(&ctx->ltp_gain);
01591     av_freep(&ctx->ltp_gain_buffer);
01592     av_freep(&ctx->quant_cof);
01593     av_freep(&ctx->lpc_cof);
01594     av_freep(&ctx->quant_cof_buffer);
01595     av_freep(&ctx->lpc_cof_buffer);
01596     av_freep(&ctx->lpc_cof_reversed_buffer);
01597     av_freep(&ctx->prev_raw_samples);
01598     av_freep(&ctx->raw_samples);
01599     av_freep(&ctx->raw_buffer);
01600     av_freep(&ctx->chan_data);
01601     av_freep(&ctx->chan_data_buffer);
01602     av_freep(&ctx->reverted_channels);
01603     av_freep(&ctx->crc_buffer);
01604 
01605     return 0;
01606 }
01607 
01608 
01611 static av_cold int decode_init(AVCodecContext *avctx)
01612 {
01613     unsigned int c;
01614     unsigned int channel_size;
01615     int num_buffers, ret;
01616     ALSDecContext *ctx = avctx->priv_data;
01617     ALSSpecificConfig *sconf = &ctx->sconf;
01618     ctx->avctx = avctx;
01619 
01620     if (!avctx->extradata) {
01621         av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
01622         return AVERROR_INVALIDDATA;
01623     }
01624 
01625     if ((ret = read_specific_config(ctx)) < 0) {
01626         av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
01627         goto fail;
01628     }
01629 
01630     if ((ret = check_specific_config(ctx)) < 0) {
01631         goto fail;
01632     }
01633 
01634     if (sconf->bgmc) {
01635         ret = ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
01636         if (ret < 0)
01637             goto fail;
01638     }
01639 
01640     if (sconf->floating) {
01641         avctx->sample_fmt          = AV_SAMPLE_FMT_FLT;
01642         avctx->bits_per_raw_sample = 32;
01643     } else {
01644         avctx->sample_fmt          = sconf->resolution > 1
01645                                      ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
01646         avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
01647     }
01648 
01649     // set maximum Rice parameter for progressive decoding based on resolution
01650     // This is not specified in 14496-3 but actually done by the reference
01651     // codec RM22 revision 2.
01652     ctx->s_max = sconf->resolution > 1 ? 31 : 15;
01653 
01654     // set lag value for long-term prediction
01655     ctx->ltp_lag_length = 8 + (avctx->sample_rate >=  96000) +
01656                               (avctx->sample_rate >= 192000);
01657 
01658     // allocate quantized parcor coefficient buffer
01659     num_buffers = sconf->mc_coding ? avctx->channels : 1;
01660 
01661     ctx->quant_cof        = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
01662     ctx->lpc_cof          = av_malloc(sizeof(*ctx->lpc_cof)   * num_buffers);
01663     ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
01664                                       num_buffers * sconf->max_order);
01665     ctx->lpc_cof_buffer   = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01666                                       num_buffers * sconf->max_order);
01667     ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01668                                              sconf->max_order);
01669 
01670     if (!ctx->quant_cof              || !ctx->lpc_cof        ||
01671         !ctx->quant_cof_buffer       || !ctx->lpc_cof_buffer ||
01672         !ctx->lpc_cof_reversed_buffer) {
01673         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01674         ret = AVERROR(ENOMEM);
01675         goto fail;
01676     }
01677 
01678     // assign quantized parcor coefficient buffers
01679     for (c = 0; c < num_buffers; c++) {
01680         ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order;
01681         ctx->lpc_cof[c]   = ctx->lpc_cof_buffer   + c * sconf->max_order;
01682     }
01683 
01684     // allocate and assign lag and gain data buffer for ltp mode
01685     ctx->const_block     = av_malloc (sizeof(*ctx->const_block) * num_buffers);
01686     ctx->shift_lsbs      = av_malloc (sizeof(*ctx->shift_lsbs)  * num_buffers);
01687     ctx->opt_order       = av_malloc (sizeof(*ctx->opt_order)   * num_buffers);
01688     ctx->store_prev_samples = av_malloc(sizeof(*ctx->store_prev_samples) * num_buffers);
01689     ctx->use_ltp         = av_mallocz(sizeof(*ctx->use_ltp)  * num_buffers);
01690     ctx->ltp_lag         = av_malloc (sizeof(*ctx->ltp_lag)  * num_buffers);
01691     ctx->ltp_gain        = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
01692     ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
01693                                       num_buffers * 5);
01694 
01695     if (!ctx->const_block || !ctx->shift_lsbs ||
01696         !ctx->opt_order || !ctx->store_prev_samples ||
01697         !ctx->use_ltp  || !ctx->ltp_lag ||
01698         !ctx->ltp_gain || !ctx->ltp_gain_buffer) {
01699         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01700         ret = AVERROR(ENOMEM);
01701         goto fail;
01702     }
01703 
01704     for (c = 0; c < num_buffers; c++)
01705         ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
01706 
01707     // allocate and assign channel data buffer for mcc mode
01708     if (sconf->mc_coding) {
01709         ctx->chan_data_buffer  = av_malloc(sizeof(*ctx->chan_data_buffer) *
01710                                            num_buffers * num_buffers);
01711         ctx->chan_data         = av_malloc(sizeof(*ctx->chan_data) *
01712                                            num_buffers);
01713         ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
01714                                            num_buffers);
01715 
01716         if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
01717             av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01718             ret = AVERROR(ENOMEM);
01719             goto fail;
01720         }
01721 
01722         for (c = 0; c < num_buffers; c++)
01723             ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers;
01724     } else {
01725         ctx->chan_data         = NULL;
01726         ctx->chan_data_buffer  = NULL;
01727         ctx->reverted_channels = NULL;
01728     }
01729 
01730     avctx->frame_size = sconf->frame_length;
01731     channel_size      = sconf->frame_length + sconf->max_order;
01732 
01733     ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
01734     ctx->raw_buffer       = av_mallocz(sizeof(*ctx->     raw_buffer)  * avctx->channels * channel_size);
01735     ctx->raw_samples      = av_malloc (sizeof(*ctx->     raw_samples) * avctx->channels);
01736 
01737     // allocate previous raw sample buffer
01738     if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
01739         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01740         ret = AVERROR(ENOMEM);
01741         goto fail;
01742     }
01743 
01744     // assign raw samples buffers
01745     ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
01746     for (c = 1; c < avctx->channels; c++)
01747         ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
01748 
01749     // allocate crc buffer
01750     if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
01751         (avctx->err_recognition & AV_EF_CRCCHECK)) {
01752         ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
01753                                     ctx->cur_frame_length *
01754                                     avctx->channels *
01755                                     av_get_bytes_per_sample(avctx->sample_fmt));
01756         if (!ctx->crc_buffer) {
01757             av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01758             ret = AVERROR(ENOMEM);
01759             goto fail;
01760         }
01761     }
01762 
01763     dsputil_init(&ctx->dsp, avctx);
01764 
01765     avcodec_get_frame_defaults(&ctx->frame);
01766     avctx->coded_frame = &ctx->frame;
01767 
01768     return 0;
01769 
01770 fail:
01771     decode_end(avctx);
01772     return ret;
01773 }
01774 
01775 
01778 static av_cold void flush(AVCodecContext *avctx)
01779 {
01780     ALSDecContext *ctx = avctx->priv_data;
01781 
01782     ctx->frame_id = 0;
01783 }
01784 
01785 
01786 AVCodec ff_als_decoder = {
01787     .name           = "als",
01788     .type           = AVMEDIA_TYPE_AUDIO,
01789     .id             = CODEC_ID_MP4ALS,
01790     .priv_data_size = sizeof(ALSDecContext),
01791     .init           = decode_init,
01792     .close          = decode_end,
01793     .decode         = decode_frame,
01794     .flush = flush,
01795     .capabilities = CODEC_CAP_SUBFRAMES | CODEC_CAP_DR1,
01796     .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
01797 };
01798