Libav 0.7.1
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     ALSSpecificConfig sconf;
00195     GetBitContext gb;
00196     DSPContext dsp;
00197     const AVCRC *crc_table;
00198     uint32_t crc_org;               
00199     uint32_t crc;                   
00200     unsigned int cur_frame_length;  
00201     unsigned int frame_id;          
00202     unsigned int js_switch;         
00203     unsigned int num_blocks;        
00204     unsigned int s_max;             
00205     uint8_t *bgmc_lut;              
00206     int *bgmc_lut_status;           
00207     int ltp_lag_length;             
00208     int *const_block;               
00209     unsigned int *shift_lsbs;       
00210     unsigned int *opt_order;        
00211     int *store_prev_samples;        
00212     int *use_ltp;                   
00213     int *ltp_lag;                   
00214     int **ltp_gain;                 
00215     int *ltp_gain_buffer;           
00216     int32_t **quant_cof;            
00217     int32_t *quant_cof_buffer;      
00218     int32_t **lpc_cof;              
00219     int32_t *lpc_cof_buffer;        
00220     int32_t *lpc_cof_reversed_buffer; 
00221     ALSChannelData **chan_data;     
00222     ALSChannelData *chan_data_buffer; 
00223     int *reverted_channels;         
00224     int32_t *prev_raw_samples;      
00225     int32_t **raw_samples;          
00226     int32_t *raw_buffer;            
00227     uint8_t *crc_buffer;            
00228 } ALSDecContext;
00229 
00230 
00231 typedef struct {
00232     unsigned int block_length;      
00233     unsigned int ra_block;          
00234     int          *const_block;      
00235     int          js_blocks;         
00236     unsigned int *shift_lsbs;       
00237     unsigned int *opt_order;        
00238     int          *store_prev_samples;
00239     int          *use_ltp;          
00240     int          *ltp_lag;          
00241     int          *ltp_gain;         
00242     int32_t      *quant_cof;        
00243     int32_t      *lpc_cof;          
00244     int32_t      *raw_samples;      
00245     int32_t      *prev_raw_samples; 
00246     int32_t      *raw_other;        
00247 } ALSBlockData;
00248 
00249 
00250 static av_cold void dprint_specific_config(ALSDecContext *ctx)
00251 {
00252 #ifdef DEBUG
00253     AVCodecContext *avctx    = ctx->avctx;
00254     ALSSpecificConfig *sconf = &ctx->sconf;
00255 
00256     av_dlog(avctx, "resolution = %i\n",           sconf->resolution);
00257     av_dlog(avctx, "floating = %i\n",             sconf->floating);
00258     av_dlog(avctx, "frame_length = %i\n",         sconf->frame_length);
00259     av_dlog(avctx, "ra_distance = %i\n",          sconf->ra_distance);
00260     av_dlog(avctx, "ra_flag = %i\n",              sconf->ra_flag);
00261     av_dlog(avctx, "adapt_order = %i\n",          sconf->adapt_order);
00262     av_dlog(avctx, "coef_table = %i\n",           sconf->coef_table);
00263     av_dlog(avctx, "long_term_prediction = %i\n", sconf->long_term_prediction);
00264     av_dlog(avctx, "max_order = %i\n",            sconf->max_order);
00265     av_dlog(avctx, "block_switching = %i\n",      sconf->block_switching);
00266     av_dlog(avctx, "bgmc = %i\n",                 sconf->bgmc);
00267     av_dlog(avctx, "sb_part = %i\n",              sconf->sb_part);
00268     av_dlog(avctx, "joint_stereo = %i\n",         sconf->joint_stereo);
00269     av_dlog(avctx, "mc_coding = %i\n",            sconf->mc_coding);
00270     av_dlog(avctx, "chan_config = %i\n",          sconf->chan_config);
00271     av_dlog(avctx, "chan_sort = %i\n",            sconf->chan_sort);
00272     av_dlog(avctx, "RLSLMS = %i\n",               sconf->rlslms);
00273     av_dlog(avctx, "chan_config_info = %i\n",     sconf->chan_config_info);
00274 #endif
00275 }
00276 
00277 
00280 static av_cold int read_specific_config(ALSDecContext *ctx)
00281 {
00282     GetBitContext gb;
00283     uint64_t ht_size;
00284     int i, config_offset;
00285     MPEG4AudioConfig m4ac;
00286     ALSSpecificConfig *sconf = &ctx->sconf;
00287     AVCodecContext *avctx    = ctx->avctx;
00288     uint32_t als_id, header_size, trailer_size;
00289 
00290     init_get_bits(&gb, avctx->extradata, avctx->extradata_size * 8);
00291 
00292     config_offset = ff_mpeg4audio_get_config(&m4ac, avctx->extradata,
00293                                              avctx->extradata_size);
00294 
00295     if (config_offset < 0)
00296         return -1;
00297 
00298     skip_bits_long(&gb, config_offset);
00299 
00300     if (get_bits_left(&gb) < (30 << 3))
00301         return -1;
00302 
00303     // read the fixed items
00304     als_id                      = get_bits_long(&gb, 32);
00305     avctx->sample_rate          = m4ac.sample_rate;
00306     skip_bits_long(&gb, 32); // sample rate already known
00307     sconf->samples              = get_bits_long(&gb, 32);
00308     avctx->channels             = m4ac.channels;
00309     skip_bits(&gb, 16);      // number of channels already knwon
00310     skip_bits(&gb, 3);       // skip file_type
00311     sconf->resolution           = get_bits(&gb, 3);
00312     sconf->floating             = get_bits1(&gb);
00313     sconf->msb_first            = get_bits1(&gb);
00314     sconf->frame_length         = get_bits(&gb, 16) + 1;
00315     sconf->ra_distance          = get_bits(&gb, 8);
00316     sconf->ra_flag              = get_bits(&gb, 2);
00317     sconf->adapt_order          = get_bits1(&gb);
00318     sconf->coef_table           = get_bits(&gb, 2);
00319     sconf->long_term_prediction = get_bits1(&gb);
00320     sconf->max_order            = get_bits(&gb, 10);
00321     sconf->block_switching      = get_bits(&gb, 2);
00322     sconf->bgmc                 = get_bits1(&gb);
00323     sconf->sb_part              = get_bits1(&gb);
00324     sconf->joint_stereo         = get_bits1(&gb);
00325     sconf->mc_coding            = get_bits1(&gb);
00326     sconf->chan_config          = get_bits1(&gb);
00327     sconf->chan_sort            = get_bits1(&gb);
00328     sconf->crc_enabled          = get_bits1(&gb);
00329     sconf->rlslms               = get_bits1(&gb);
00330     skip_bits(&gb, 5);       // skip 5 reserved bits
00331     skip_bits1(&gb);         // skip aux_data_enabled
00332 
00333 
00334     // check for ALSSpecificConfig struct
00335     if (als_id != MKBETAG('A','L','S','\0'))
00336         return -1;
00337 
00338     ctx->cur_frame_length = sconf->frame_length;
00339 
00340     // read channel config
00341     if (sconf->chan_config)
00342         sconf->chan_config_info = get_bits(&gb, 16);
00343     // TODO: use this to set avctx->channel_layout
00344 
00345 
00346     // read channel sorting
00347     if (sconf->chan_sort && avctx->channels > 1) {
00348         int chan_pos_bits = av_ceil_log2(avctx->channels);
00349         int bits_needed  = avctx->channels * chan_pos_bits + 7;
00350         if (get_bits_left(&gb) < bits_needed)
00351             return -1;
00352 
00353         if (!(sconf->chan_pos = av_malloc(avctx->channels * sizeof(*sconf->chan_pos))))
00354             return AVERROR(ENOMEM);
00355 
00356         for (i = 0; i < avctx->channels; i++)
00357             sconf->chan_pos[i] = get_bits(&gb, chan_pos_bits);
00358 
00359         align_get_bits(&gb);
00360         // TODO: use this to actually do channel sorting
00361     } else {
00362         sconf->chan_sort = 0;
00363     }
00364 
00365 
00366     // read fixed header and trailer sizes,
00367     // if size = 0xFFFFFFFF then there is no data field!
00368     if (get_bits_left(&gb) < 64)
00369         return -1;
00370 
00371     header_size  = get_bits_long(&gb, 32);
00372     trailer_size = get_bits_long(&gb, 32);
00373     if (header_size  == 0xFFFFFFFF)
00374         header_size  = 0;
00375     if (trailer_size == 0xFFFFFFFF)
00376         trailer_size = 0;
00377 
00378     ht_size = ((int64_t)(header_size) + (int64_t)(trailer_size)) << 3;
00379 
00380 
00381     // skip the header and trailer data
00382     if (get_bits_left(&gb) < ht_size)
00383         return -1;
00384 
00385     if (ht_size > INT32_MAX)
00386         return -1;
00387 
00388     skip_bits_long(&gb, ht_size);
00389 
00390 
00391     // initialize CRC calculation
00392     if (sconf->crc_enabled) {
00393         if (get_bits_left(&gb) < 32)
00394             return -1;
00395 
00396         if (avctx->error_recognition >= FF_ER_CAREFUL) {
00397             ctx->crc_table = av_crc_get_table(AV_CRC_32_IEEE_LE);
00398             ctx->crc       = 0xFFFFFFFF;
00399             ctx->crc_org   = ~get_bits_long(&gb, 32);
00400         } else
00401             skip_bits_long(&gb, 32);
00402     }
00403 
00404 
00405     // no need to read the rest of ALSSpecificConfig (ra_unit_size & aux data)
00406 
00407     dprint_specific_config(ctx);
00408 
00409     return 0;
00410 }
00411 
00412 
00415 static int check_specific_config(ALSDecContext *ctx)
00416 {
00417     ALSSpecificConfig *sconf = &ctx->sconf;
00418     int error = 0;
00419 
00420     // report unsupported feature and set error value
00421     #define MISSING_ERR(cond, str, errval)              \
00422     {                                                   \
00423         if (cond) {                                     \
00424             av_log_missing_feature(ctx->avctx, str, 0); \
00425             error = errval;                             \
00426         }                                               \
00427     }
00428 
00429     MISSING_ERR(sconf->floating,             "Floating point decoding",     -1);
00430     MISSING_ERR(sconf->rlslms,               "Adaptive RLS-LMS prediction", -1);
00431     MISSING_ERR(sconf->chan_sort,            "Channel sorting",              0);
00432 
00433     return error;
00434 }
00435 
00436 
00440 static void parse_bs_info(const uint32_t bs_info, unsigned int n,
00441                           unsigned int div, unsigned int **div_blocks,
00442                           unsigned int *num_blocks)
00443 {
00444     if (n < 31 && ((bs_info << n) & 0x40000000)) {
00445         // if the level is valid and the investigated bit n is set
00446         // then recursively check both children at bits (2n+1) and (2n+2)
00447         n   *= 2;
00448         div += 1;
00449         parse_bs_info(bs_info, n + 1, div, div_blocks, num_blocks);
00450         parse_bs_info(bs_info, n + 2, div, div_blocks, num_blocks);
00451     } else {
00452         // else the bit is not set or the last level has been reached
00453         // (bit implicitly not set)
00454         **div_blocks = div;
00455         (*div_blocks)++;
00456         (*num_blocks)++;
00457     }
00458 }
00459 
00460 
00463 static int32_t decode_rice(GetBitContext *gb, unsigned int k)
00464 {
00465     int max = get_bits_left(gb) - k;
00466     int q   = get_unary(gb, 0, max);
00467     int r   = k ? get_bits1(gb) : !(q & 1);
00468 
00469     if (k > 1) {
00470         q <<= (k - 1);
00471         q  += get_bits_long(gb, k - 1);
00472     } else if (!k) {
00473         q >>= 1;
00474     }
00475     return r ? q : ~q;
00476 }
00477 
00478 
00481 static void parcor_to_lpc(unsigned int k, const int32_t *par, int32_t *cof)
00482 {
00483     int i, j;
00484 
00485     for (i = 0, j = k - 1; i < j; i++, j--) {
00486         int tmp1 = ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
00487         cof[j]  += ((MUL64(par[k], cof[i]) + (1 << 19)) >> 20);
00488         cof[i]  += tmp1;
00489     }
00490     if (i == j)
00491         cof[i] += ((MUL64(par[k], cof[j]) + (1 << 19)) >> 20);
00492 
00493     cof[k] = par[k];
00494 }
00495 
00496 
00501 static void get_block_sizes(ALSDecContext *ctx, unsigned int *div_blocks,
00502                             uint32_t *bs_info)
00503 {
00504     ALSSpecificConfig *sconf     = &ctx->sconf;
00505     GetBitContext *gb            = &ctx->gb;
00506     unsigned int *ptr_div_blocks = div_blocks;
00507     unsigned int b;
00508 
00509     if (sconf->block_switching) {
00510         unsigned int bs_info_len = 1 << (sconf->block_switching + 2);
00511         *bs_info = get_bits_long(gb, bs_info_len);
00512         *bs_info <<= (32 - bs_info_len);
00513     }
00514 
00515     ctx->num_blocks = 0;
00516     parse_bs_info(*bs_info, 0, 0, &ptr_div_blocks, &ctx->num_blocks);
00517 
00518     // The last frame may have an overdetermined block structure given in
00519     // the bitstream. In that case the defined block structure would need
00520     // more samples than available to be consistent.
00521     // The block structure is actually used but the block sizes are adapted
00522     // to fit the actual number of available samples.
00523     // Example: 5 samples, 2nd level block sizes: 2 2 2 2.
00524     // This results in the actual block sizes:    2 2 1 0.
00525     // This is not specified in 14496-3 but actually done by the reference
00526     // codec RM22 revision 2.
00527     // This appears to happen in case of an odd number of samples in the last
00528     // frame which is actually not allowed by the block length switching part
00529     // of 14496-3.
00530     // The ALS conformance files feature an odd number of samples in the last
00531     // frame.
00532 
00533     for (b = 0; b < ctx->num_blocks; b++)
00534         div_blocks[b] = ctx->sconf.frame_length >> div_blocks[b];
00535 
00536     if (ctx->cur_frame_length != ctx->sconf.frame_length) {
00537         unsigned int remaining = ctx->cur_frame_length;
00538 
00539         for (b = 0; b < ctx->num_blocks; b++) {
00540             if (remaining <= div_blocks[b]) {
00541                 div_blocks[b] = remaining;
00542                 ctx->num_blocks = b + 1;
00543                 break;
00544             }
00545 
00546             remaining -= div_blocks[b];
00547         }
00548     }
00549 }
00550 
00551 
00554 static void read_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00555 {
00556     ALSSpecificConfig *sconf = &ctx->sconf;
00557     AVCodecContext *avctx    = ctx->avctx;
00558     GetBitContext *gb        = &ctx->gb;
00559 
00560     *bd->raw_samples = 0;
00561     *bd->const_block = get_bits1(gb);    // 1 = constant value, 0 = zero block (silence)
00562     bd->js_blocks    = get_bits1(gb);
00563 
00564     // skip 5 reserved bits
00565     skip_bits(gb, 5);
00566 
00567     if (*bd->const_block) {
00568         unsigned int const_val_bits = sconf->floating ? 24 : avctx->bits_per_raw_sample;
00569         *bd->raw_samples = get_sbits_long(gb, const_val_bits);
00570     }
00571 
00572     // ensure constant block decoding by reusing this field
00573     *bd->const_block = 1;
00574 }
00575 
00576 
00579 static void decode_const_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00580 {
00581     int      smp = bd->block_length - 1;
00582     int32_t  val = *bd->raw_samples;
00583     int32_t *dst = bd->raw_samples + 1;
00584 
00585     // write raw samples into buffer
00586     for (; smp; smp--)
00587         *dst++ = val;
00588 }
00589 
00590 
00593 static int read_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00594 {
00595     ALSSpecificConfig *sconf = &ctx->sconf;
00596     AVCodecContext *avctx    = ctx->avctx;
00597     GetBitContext *gb        = &ctx->gb;
00598     unsigned int k;
00599     unsigned int s[8];
00600     unsigned int sx[8];
00601     unsigned int sub_blocks, log2_sub_blocks, sb_length;
00602     unsigned int start      = 0;
00603     unsigned int opt_order;
00604     int          sb;
00605     int32_t      *quant_cof = bd->quant_cof;
00606     int32_t      *current_res;
00607 
00608 
00609     // ensure variable block decoding by reusing this field
00610     *bd->const_block = 0;
00611 
00612     *bd->opt_order  = 1;
00613     bd->js_blocks   = get_bits1(gb);
00614 
00615     opt_order       = *bd->opt_order;
00616 
00617     // determine the number of subblocks for entropy decoding
00618     if (!sconf->bgmc && !sconf->sb_part) {
00619         log2_sub_blocks = 0;
00620     } else {
00621         if (sconf->bgmc && sconf->sb_part)
00622             log2_sub_blocks = get_bits(gb, 2);
00623         else
00624             log2_sub_blocks = 2 * get_bits1(gb);
00625     }
00626 
00627     sub_blocks = 1 << log2_sub_blocks;
00628 
00629     // do not continue in case of a damaged stream since
00630     // block_length must be evenly divisible by sub_blocks
00631     if (bd->block_length & (sub_blocks - 1)) {
00632         av_log(avctx, AV_LOG_WARNING,
00633                "Block length is not evenly divisible by the number of subblocks.\n");
00634         return -1;
00635     }
00636 
00637     sb_length = bd->block_length >> log2_sub_blocks;
00638 
00639     if (sconf->bgmc) {
00640         s[0] = get_bits(gb, 8 + (sconf->resolution > 1));
00641         for (k = 1; k < sub_blocks; k++)
00642             s[k] = s[k - 1] + decode_rice(gb, 2);
00643 
00644         for (k = 0; k < sub_blocks; k++) {
00645             sx[k]   = s[k] & 0x0F;
00646             s [k] >>= 4;
00647         }
00648     } else {
00649         s[0] = get_bits(gb, 4 + (sconf->resolution > 1));
00650         for (k = 1; k < sub_blocks; k++)
00651             s[k] = s[k - 1] + decode_rice(gb, 0);
00652     }
00653 
00654     if (get_bits1(gb))
00655         *bd->shift_lsbs = get_bits(gb, 4) + 1;
00656 
00657     *bd->store_prev_samples = (bd->js_blocks && bd->raw_other) || *bd->shift_lsbs;
00658 
00659 
00660     if (!sconf->rlslms) {
00661         if (sconf->adapt_order) {
00662             int opt_order_length = av_ceil_log2(av_clip((bd->block_length >> 3) - 1,
00663                                                 2, sconf->max_order + 1));
00664             *bd->opt_order       = get_bits(gb, opt_order_length);
00665             if (*bd->opt_order > sconf->max_order) {
00666                 *bd->opt_order = sconf->max_order;
00667                 av_log(avctx, AV_LOG_ERROR, "Predictor order too large!\n");
00668                 return AVERROR_INVALIDDATA;
00669             }
00670         } else {
00671             *bd->opt_order = sconf->max_order;
00672         }
00673 
00674         opt_order = *bd->opt_order;
00675 
00676         if (opt_order) {
00677             int add_base;
00678 
00679             if (sconf->coef_table == 3) {
00680                 add_base = 0x7F;
00681 
00682                 // read coefficient 0
00683                 quant_cof[0] = 32 * parcor_scaled_values[get_bits(gb, 7)];
00684 
00685                 // read coefficient 1
00686                 if (opt_order > 1)
00687                     quant_cof[1] = -32 * parcor_scaled_values[get_bits(gb, 7)];
00688 
00689                 // read coefficients 2 to opt_order
00690                 for (k = 2; k < opt_order; k++)
00691                     quant_cof[k] = get_bits(gb, 7);
00692             } else {
00693                 int k_max;
00694                 add_base = 1;
00695 
00696                 // read coefficient 0 to 19
00697                 k_max = FFMIN(opt_order, 20);
00698                 for (k = 0; k < k_max; k++) {
00699                     int rice_param = parcor_rice_table[sconf->coef_table][k][1];
00700                     int offset     = parcor_rice_table[sconf->coef_table][k][0];
00701                     quant_cof[k] = decode_rice(gb, rice_param) + offset;
00702                 }
00703 
00704                 // read coefficients 20 to 126
00705                 k_max = FFMIN(opt_order, 127);
00706                 for (; k < k_max; k++)
00707                     quant_cof[k] = decode_rice(gb, 2) + (k & 1);
00708 
00709                 // read coefficients 127 to opt_order
00710                 for (; k < opt_order; k++)
00711                     quant_cof[k] = decode_rice(gb, 1);
00712 
00713                 quant_cof[0] = 32 * parcor_scaled_values[quant_cof[0] + 64];
00714 
00715                 if (opt_order > 1)
00716                     quant_cof[1] = -32 * parcor_scaled_values[quant_cof[1] + 64];
00717             }
00718 
00719             for (k = 2; k < opt_order; k++)
00720                 quant_cof[k] = (quant_cof[k] << 14) + (add_base << 13);
00721         }
00722     }
00723 
00724     // read LTP gain and lag values
00725     if (sconf->long_term_prediction) {
00726         *bd->use_ltp = get_bits1(gb);
00727 
00728         if (*bd->use_ltp) {
00729             int r, c;
00730 
00731             bd->ltp_gain[0]   = decode_rice(gb, 1) << 3;
00732             bd->ltp_gain[1]   = decode_rice(gb, 2) << 3;
00733 
00734             r                 = get_unary(gb, 0, 4);
00735             c                 = get_bits(gb, 2);
00736             bd->ltp_gain[2]   = ltp_gain_values[r][c];
00737 
00738             bd->ltp_gain[3]   = decode_rice(gb, 2) << 3;
00739             bd->ltp_gain[4]   = decode_rice(gb, 1) << 3;
00740 
00741             *bd->ltp_lag      = get_bits(gb, ctx->ltp_lag_length);
00742             *bd->ltp_lag     += FFMAX(4, opt_order + 1);
00743         }
00744     }
00745 
00746     // read first value and residuals in case of a random access block
00747     if (bd->ra_block) {
00748         if (opt_order)
00749             bd->raw_samples[0] = decode_rice(gb, avctx->bits_per_raw_sample - 4);
00750         if (opt_order > 1)
00751             bd->raw_samples[1] = decode_rice(gb, FFMIN(s[0] + 3, ctx->s_max));
00752         if (opt_order > 2)
00753             bd->raw_samples[2] = decode_rice(gb, FFMIN(s[0] + 1, ctx->s_max));
00754 
00755         start = FFMIN(opt_order, 3);
00756     }
00757 
00758     // read all residuals
00759     if (sconf->bgmc) {
00760         int          delta[8];
00761         unsigned int k    [8];
00762         unsigned int b = av_clip((av_ceil_log2(bd->block_length) - 3) >> 1, 0, 5);
00763 
00764         // read most significant bits
00765         unsigned int high;
00766         unsigned int low;
00767         unsigned int value;
00768 
00769         ff_bgmc_decode_init(gb, &high, &low, &value);
00770 
00771         current_res = bd->raw_samples + start;
00772 
00773         for (sb = 0; sb < sub_blocks; sb++) {
00774             unsigned int sb_len  = sb_length - (sb ? 0 : start);
00775 
00776             k    [sb] = s[sb] > b ? s[sb] - b : 0;
00777             delta[sb] = 5 - s[sb] + k[sb];
00778 
00779             ff_bgmc_decode(gb, sb_len, current_res,
00780                         delta[sb], sx[sb], &high, &low, &value, ctx->bgmc_lut, ctx->bgmc_lut_status);
00781 
00782             current_res += sb_len;
00783         }
00784 
00785         ff_bgmc_decode_end(gb);
00786 
00787 
00788         // read least significant bits and tails
00789         current_res = bd->raw_samples + start;
00790 
00791         for (sb = 0; sb < sub_blocks; sb++, start = 0) {
00792             unsigned int cur_tail_code = tail_code[sx[sb]][delta[sb]];
00793             unsigned int cur_k         = k[sb];
00794             unsigned int cur_s         = s[sb];
00795 
00796             for (; start < sb_length; start++) {
00797                 int32_t res = *current_res;
00798 
00799                 if (res == cur_tail_code) {
00800                     unsigned int max_msb =   (2 + (sx[sb] > 2) + (sx[sb] > 10))
00801                                           << (5 - delta[sb]);
00802 
00803                     res = decode_rice(gb, cur_s);
00804 
00805                     if (res >= 0) {
00806                         res += (max_msb    ) << cur_k;
00807                     } else {
00808                         res -= (max_msb - 1) << cur_k;
00809                     }
00810                 } else {
00811                     if (res > cur_tail_code)
00812                         res--;
00813 
00814                     if (res & 1)
00815                         res = -res;
00816 
00817                     res >>= 1;
00818 
00819                     if (cur_k) {
00820                         res <<= cur_k;
00821                         res  |= get_bits_long(gb, cur_k);
00822                     }
00823                 }
00824 
00825                 *current_res++ = res;
00826             }
00827         }
00828     } else {
00829         current_res = bd->raw_samples + start;
00830 
00831         for (sb = 0; sb < sub_blocks; sb++, start = 0)
00832             for (; start < sb_length; start++)
00833                 *current_res++ = decode_rice(gb, s[sb]);
00834      }
00835 
00836     if (!sconf->mc_coding || ctx->js_switch)
00837         align_get_bits(gb);
00838 
00839     return 0;
00840 }
00841 
00842 
00845 static int decode_var_block_data(ALSDecContext *ctx, ALSBlockData *bd)
00846 {
00847     ALSSpecificConfig *sconf = &ctx->sconf;
00848     unsigned int block_length = bd->block_length;
00849     unsigned int smp = 0;
00850     unsigned int k;
00851     int opt_order             = *bd->opt_order;
00852     int sb;
00853     int64_t y;
00854     int32_t *quant_cof        = bd->quant_cof;
00855     int32_t *lpc_cof          = bd->lpc_cof;
00856     int32_t *raw_samples      = bd->raw_samples;
00857     int32_t *raw_samples_end  = bd->raw_samples + bd->block_length;
00858     int32_t *lpc_cof_reversed = ctx->lpc_cof_reversed_buffer;
00859 
00860     // reverse long-term prediction
00861     if (*bd->use_ltp) {
00862         int ltp_smp;
00863 
00864         for (ltp_smp = FFMAX(*bd->ltp_lag - 2, 0); ltp_smp < block_length; ltp_smp++) {
00865             int center = ltp_smp - *bd->ltp_lag;
00866             int begin  = FFMAX(0, center - 2);
00867             int end    = center + 3;
00868             int tab    = 5 - (end - begin);
00869             int base;
00870 
00871             y = 1 << 6;
00872 
00873             for (base = begin; base < end; base++, tab++)
00874                 y += MUL64(bd->ltp_gain[tab], raw_samples[base]);
00875 
00876             raw_samples[ltp_smp] += y >> 7;
00877         }
00878     }
00879 
00880     // reconstruct all samples from residuals
00881     if (bd->ra_block) {
00882         for (smp = 0; smp < opt_order; smp++) {
00883             y = 1 << 19;
00884 
00885             for (sb = 0; sb < smp; sb++)
00886                 y += MUL64(lpc_cof[sb], raw_samples[-(sb + 1)]);
00887 
00888             *raw_samples++ -= y >> 20;
00889             parcor_to_lpc(smp, quant_cof, lpc_cof);
00890         }
00891     } else {
00892         for (k = 0; k < opt_order; k++)
00893             parcor_to_lpc(k, quant_cof, lpc_cof);
00894 
00895         // store previous samples in case that they have to be altered
00896         if (*bd->store_prev_samples)
00897             memcpy(bd->prev_raw_samples, raw_samples - sconf->max_order,
00898                    sizeof(*bd->prev_raw_samples) * sconf->max_order);
00899 
00900         // reconstruct difference signal for prediction (joint-stereo)
00901         if (bd->js_blocks && bd->raw_other) {
00902             int32_t *left, *right;
00903 
00904             if (bd->raw_other > raw_samples) {  // D = R - L
00905                 left  = raw_samples;
00906                 right = bd->raw_other;
00907             } else {                                // D = R - L
00908                 left  = bd->raw_other;
00909                 right = raw_samples;
00910             }
00911 
00912             for (sb = -1; sb >= -sconf->max_order; sb--)
00913                 raw_samples[sb] = right[sb] - left[sb];
00914         }
00915 
00916         // reconstruct shifted signal
00917         if (*bd->shift_lsbs)
00918             for (sb = -1; sb >= -sconf->max_order; sb--)
00919                 raw_samples[sb] >>= *bd->shift_lsbs;
00920     }
00921 
00922     // reverse linear prediction coefficients for efficiency
00923     lpc_cof = lpc_cof + opt_order;
00924 
00925     for (sb = 0; sb < opt_order; sb++)
00926         lpc_cof_reversed[sb] = lpc_cof[-(sb + 1)];
00927 
00928     // reconstruct raw samples
00929     raw_samples = bd->raw_samples + smp;
00930     lpc_cof     = lpc_cof_reversed + opt_order;
00931 
00932     for (; raw_samples < raw_samples_end; raw_samples++) {
00933         y = 1 << 19;
00934 
00935         for (sb = -opt_order; sb < 0; sb++)
00936             y += MUL64(lpc_cof[sb], raw_samples[sb]);
00937 
00938         *raw_samples -= y >> 20;
00939     }
00940 
00941     raw_samples = bd->raw_samples;
00942 
00943     // restore previous samples in case that they have been altered
00944     if (*bd->store_prev_samples)
00945         memcpy(raw_samples - sconf->max_order, bd->prev_raw_samples,
00946                sizeof(*raw_samples) * sconf->max_order);
00947 
00948     return 0;
00949 }
00950 
00951 
00954 static int read_block(ALSDecContext *ctx, ALSBlockData *bd)
00955 {
00956     GetBitContext *gb        = &ctx->gb;
00957 
00958     *bd->shift_lsbs = 0;
00959     // read block type flag and read the samples accordingly
00960     if (get_bits1(gb)) {
00961         if (read_var_block_data(ctx, bd))
00962             return -1;
00963     } else {
00964         read_const_block_data(ctx, bd);
00965     }
00966 
00967     return 0;
00968 }
00969 
00970 
00973 static int decode_block(ALSDecContext *ctx, ALSBlockData *bd)
00974 {
00975     unsigned int smp;
00976 
00977     // read block type flag and read the samples accordingly
00978     if (*bd->const_block)
00979         decode_const_block_data(ctx, bd);
00980     else if (decode_var_block_data(ctx, bd))
00981         return -1;
00982 
00983     // TODO: read RLSLMS extension data
00984 
00985     if (*bd->shift_lsbs)
00986         for (smp = 0; smp < bd->block_length; smp++)
00987             bd->raw_samples[smp] <<= *bd->shift_lsbs;
00988 
00989     return 0;
00990 }
00991 
00992 
00995 static int read_decode_block(ALSDecContext *ctx, ALSBlockData *bd)
00996 {
00997     int ret;
00998 
00999     ret = read_block(ctx, bd);
01000 
01001     if (ret)
01002         return ret;
01003 
01004     ret = decode_block(ctx, bd);
01005 
01006     return ret;
01007 }
01008 
01009 
01013 static void zero_remaining(unsigned int b, unsigned int b_max,
01014                            const unsigned int *div_blocks, int32_t *buf)
01015 {
01016     unsigned int count = 0;
01017 
01018     for (; b < b_max; b++)
01019         count += div_blocks[b];
01020 
01021     if (count)
01022         memset(buf, 0, sizeof(*buf) * count);
01023 }
01024 
01025 
01028 static int decode_blocks_ind(ALSDecContext *ctx, unsigned int ra_frame,
01029                              unsigned int c, const unsigned int *div_blocks,
01030                              unsigned int *js_blocks)
01031 {
01032     unsigned int b;
01033     ALSBlockData bd;
01034 
01035     memset(&bd, 0, sizeof(ALSBlockData));
01036 
01037     bd.ra_block         = ra_frame;
01038     bd.const_block      = ctx->const_block;
01039     bd.shift_lsbs       = ctx->shift_lsbs;
01040     bd.opt_order        = ctx->opt_order;
01041     bd.store_prev_samples = ctx->store_prev_samples;
01042     bd.use_ltp          = ctx->use_ltp;
01043     bd.ltp_lag          = ctx->ltp_lag;
01044     bd.ltp_gain         = ctx->ltp_gain[0];
01045     bd.quant_cof        = ctx->quant_cof[0];
01046     bd.lpc_cof          = ctx->lpc_cof[0];
01047     bd.prev_raw_samples = ctx->prev_raw_samples;
01048     bd.raw_samples      = ctx->raw_samples[c];
01049 
01050 
01051     for (b = 0; b < ctx->num_blocks; b++) {
01052         bd.block_length     = div_blocks[b];
01053 
01054         if (read_decode_block(ctx, &bd)) {
01055             // damaged block, write zero for the rest of the frame
01056             zero_remaining(b, ctx->num_blocks, div_blocks, bd.raw_samples);
01057             return -1;
01058         }
01059         bd.raw_samples += div_blocks[b];
01060         bd.ra_block     = 0;
01061     }
01062 
01063     return 0;
01064 }
01065 
01066 
01069 static int decode_blocks(ALSDecContext *ctx, unsigned int ra_frame,
01070                          unsigned int c, const unsigned int *div_blocks,
01071                          unsigned int *js_blocks)
01072 {
01073     ALSSpecificConfig *sconf = &ctx->sconf;
01074     unsigned int offset = 0;
01075     unsigned int b;
01076     ALSBlockData bd[2];
01077 
01078     memset(bd, 0, 2 * sizeof(ALSBlockData));
01079 
01080     bd[0].ra_block         = ra_frame;
01081     bd[0].const_block      = ctx->const_block;
01082     bd[0].shift_lsbs       = ctx->shift_lsbs;
01083     bd[0].opt_order        = ctx->opt_order;
01084     bd[0].store_prev_samples = ctx->store_prev_samples;
01085     bd[0].use_ltp          = ctx->use_ltp;
01086     bd[0].ltp_lag          = ctx->ltp_lag;
01087     bd[0].ltp_gain         = ctx->ltp_gain[0];
01088     bd[0].quant_cof        = ctx->quant_cof[0];
01089     bd[0].lpc_cof          = ctx->lpc_cof[0];
01090     bd[0].prev_raw_samples = ctx->prev_raw_samples;
01091     bd[0].js_blocks        = *js_blocks;
01092 
01093     bd[1].ra_block         = ra_frame;
01094     bd[1].const_block      = ctx->const_block;
01095     bd[1].shift_lsbs       = ctx->shift_lsbs;
01096     bd[1].opt_order        = ctx->opt_order;
01097     bd[1].store_prev_samples = ctx->store_prev_samples;
01098     bd[1].use_ltp          = ctx->use_ltp;
01099     bd[1].ltp_lag          = ctx->ltp_lag;
01100     bd[1].ltp_gain         = ctx->ltp_gain[0];
01101     bd[1].quant_cof        = ctx->quant_cof[0];
01102     bd[1].lpc_cof          = ctx->lpc_cof[0];
01103     bd[1].prev_raw_samples = ctx->prev_raw_samples;
01104     bd[1].js_blocks        = *(js_blocks + 1);
01105 
01106     // decode all blocks
01107     for (b = 0; b < ctx->num_blocks; b++) {
01108         unsigned int s;
01109 
01110         bd[0].block_length = div_blocks[b];
01111         bd[1].block_length = div_blocks[b];
01112 
01113         bd[0].raw_samples  = ctx->raw_samples[c    ] + offset;
01114         bd[1].raw_samples  = ctx->raw_samples[c + 1] + offset;
01115 
01116         bd[0].raw_other    = bd[1].raw_samples;
01117         bd[1].raw_other    = bd[0].raw_samples;
01118 
01119         if(read_decode_block(ctx, &bd[0]) || read_decode_block(ctx, &bd[1])) {
01120             // damaged block, write zero for the rest of the frame
01121             zero_remaining(b, ctx->num_blocks, div_blocks, bd[0].raw_samples);
01122             zero_remaining(b, ctx->num_blocks, div_blocks, bd[1].raw_samples);
01123             return -1;
01124         }
01125 
01126         // reconstruct joint-stereo blocks
01127         if (bd[0].js_blocks) {
01128             if (bd[1].js_blocks)
01129                 av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel pair!\n");
01130 
01131             for (s = 0; s < div_blocks[b]; s++)
01132                 bd[0].raw_samples[s] = bd[1].raw_samples[s] - bd[0].raw_samples[s];
01133         } else if (bd[1].js_blocks) {
01134             for (s = 0; s < div_blocks[b]; s++)
01135                 bd[1].raw_samples[s] = bd[1].raw_samples[s] + bd[0].raw_samples[s];
01136         }
01137 
01138         offset  += div_blocks[b];
01139         bd[0].ra_block = 0;
01140         bd[1].ra_block = 0;
01141     }
01142 
01143     // store carryover raw samples,
01144     // the others channel raw samples are stored by the calling function.
01145     memmove(ctx->raw_samples[c] - sconf->max_order,
01146             ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01147             sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01148 
01149     return 0;
01150 }
01151 
01152 
01155 static int read_channel_data(ALSDecContext *ctx, ALSChannelData *cd, int c)
01156 {
01157     GetBitContext *gb       = &ctx->gb;
01158     ALSChannelData *current = cd;
01159     unsigned int channels   = ctx->avctx->channels;
01160     int entries             = 0;
01161 
01162     while (entries < channels && !(current->stop_flag = get_bits1(gb))) {
01163         current->master_channel = get_bits_long(gb, av_ceil_log2(channels));
01164 
01165         if (current->master_channel >= channels) {
01166             av_log(ctx->avctx, AV_LOG_ERROR, "Invalid master channel!\n");
01167             return -1;
01168         }
01169 
01170         if (current->master_channel != c) {
01171             current->time_diff_flag = get_bits1(gb);
01172             current->weighting[0]   = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01173             current->weighting[1]   = mcc_weightings[av_clip(decode_rice(gb, 2) + 14, 0, 32)];
01174             current->weighting[2]   = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01175 
01176             if (current->time_diff_flag) {
01177                 current->weighting[3] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01178                 current->weighting[4] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01179                 current->weighting[5] = mcc_weightings[av_clip(decode_rice(gb, 1) + 16, 0, 32)];
01180 
01181                 current->time_diff_sign  = get_bits1(gb);
01182                 current->time_diff_index = get_bits(gb, ctx->ltp_lag_length - 3) + 3;
01183             }
01184         }
01185 
01186         current++;
01187         entries++;
01188     }
01189 
01190     if (entries == channels) {
01191         av_log(ctx->avctx, AV_LOG_ERROR, "Damaged channel data!\n");
01192         return -1;
01193     }
01194 
01195     align_get_bits(gb);
01196     return 0;
01197 }
01198 
01199 
01202 static int revert_channel_correlation(ALSDecContext *ctx, ALSBlockData *bd,
01203                                        ALSChannelData **cd, int *reverted,
01204                                        unsigned int offset, int c)
01205 {
01206     ALSChannelData *ch = cd[c];
01207     unsigned int   dep = 0;
01208     unsigned int channels = ctx->avctx->channels;
01209 
01210     if (reverted[c])
01211         return 0;
01212 
01213     reverted[c] = 1;
01214 
01215     while (dep < channels && !ch[dep].stop_flag) {
01216         revert_channel_correlation(ctx, bd, cd, reverted, offset,
01217                                    ch[dep].master_channel);
01218 
01219         dep++;
01220     }
01221 
01222     if (dep == channels) {
01223         av_log(ctx->avctx, AV_LOG_WARNING, "Invalid channel correlation!\n");
01224         return -1;
01225     }
01226 
01227     bd->const_block = ctx->const_block + c;
01228     bd->shift_lsbs  = ctx->shift_lsbs + c;
01229     bd->opt_order   = ctx->opt_order + c;
01230     bd->store_prev_samples = ctx->store_prev_samples + c;
01231     bd->use_ltp     = ctx->use_ltp + c;
01232     bd->ltp_lag     = ctx->ltp_lag + c;
01233     bd->ltp_gain    = ctx->ltp_gain[c];
01234     bd->lpc_cof     = ctx->lpc_cof[c];
01235     bd->quant_cof   = ctx->quant_cof[c];
01236     bd->raw_samples = ctx->raw_samples[c] + offset;
01237 
01238     dep = 0;
01239     while (!ch[dep].stop_flag) {
01240         unsigned int smp;
01241         unsigned int begin = 1;
01242         unsigned int end   = bd->block_length - 1;
01243         int64_t y;
01244         int32_t *master = ctx->raw_samples[ch[dep].master_channel] + offset;
01245 
01246         if (ch[dep].time_diff_flag) {
01247             int t = ch[dep].time_diff_index;
01248 
01249             if (ch[dep].time_diff_sign) {
01250                 t      = -t;
01251                 begin -= t;
01252             } else {
01253                 end   -= t;
01254             }
01255 
01256             for (smp = begin; smp < end; smp++) {
01257                 y  = (1 << 6) +
01258                      MUL64(ch[dep].weighting[0], master[smp - 1    ]) +
01259                      MUL64(ch[dep].weighting[1], master[smp        ]) +
01260                      MUL64(ch[dep].weighting[2], master[smp + 1    ]) +
01261                      MUL64(ch[dep].weighting[3], master[smp - 1 + t]) +
01262                      MUL64(ch[dep].weighting[4], master[smp     + t]) +
01263                      MUL64(ch[dep].weighting[5], master[smp + 1 + t]);
01264 
01265                 bd->raw_samples[smp] += y >> 7;
01266             }
01267         } else {
01268             for (smp = begin; smp < end; smp++) {
01269                 y  = (1 << 6) +
01270                      MUL64(ch[dep].weighting[0], master[smp - 1]) +
01271                      MUL64(ch[dep].weighting[1], master[smp    ]) +
01272                      MUL64(ch[dep].weighting[2], master[smp + 1]);
01273 
01274                 bd->raw_samples[smp] += y >> 7;
01275             }
01276         }
01277 
01278         dep++;
01279     }
01280 
01281     return 0;
01282 }
01283 
01284 
01287 static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)
01288 {
01289     ALSSpecificConfig *sconf = &ctx->sconf;
01290     AVCodecContext *avctx    = ctx->avctx;
01291     GetBitContext *gb = &ctx->gb;
01292     unsigned int div_blocks[32];                
01293     unsigned int c;
01294     unsigned int js_blocks[2];
01295 
01296     uint32_t bs_info = 0;
01297 
01298     // skip the size of the ra unit if present in the frame
01299     if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)
01300         skip_bits_long(gb, 32);
01301 
01302     if (sconf->mc_coding && sconf->joint_stereo) {
01303         ctx->js_switch = get_bits1(gb);
01304         align_get_bits(gb);
01305     }
01306 
01307     if (!sconf->mc_coding || ctx->js_switch) {
01308         int independent_bs = !sconf->joint_stereo;
01309 
01310         for (c = 0; c < avctx->channels; c++) {
01311             js_blocks[0] = 0;
01312             js_blocks[1] = 0;
01313 
01314             get_block_sizes(ctx, div_blocks, &bs_info);
01315 
01316             // if joint_stereo and block_switching is set, independent decoding
01317             // is signaled via the first bit of bs_info
01318             if (sconf->joint_stereo && sconf->block_switching)
01319                 if (bs_info >> 31)
01320                     independent_bs = 2;
01321 
01322             // if this is the last channel, it has to be decoded independently
01323             if (c == avctx->channels - 1)
01324                 independent_bs = 1;
01325 
01326             if (independent_bs) {
01327                 if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))
01328                     return -1;
01329 
01330                 independent_bs--;
01331             } else {
01332                 if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))
01333                     return -1;
01334 
01335                 c++;
01336             }
01337 
01338             // store carryover raw samples
01339             memmove(ctx->raw_samples[c] - sconf->max_order,
01340                     ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01341                     sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01342         }
01343     } else { // multi-channel coding
01344         ALSBlockData   bd;
01345         int            b;
01346         int            *reverted_channels = ctx->reverted_channels;
01347         unsigned int   offset             = 0;
01348 
01349         for (c = 0; c < avctx->channels; c++)
01350             if (ctx->chan_data[c] < ctx->chan_data_buffer) {
01351                 av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");
01352                 return -1;
01353             }
01354 
01355         memset(&bd,               0, sizeof(ALSBlockData));
01356         memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);
01357 
01358         bd.ra_block         = ra_frame;
01359         bd.prev_raw_samples = ctx->prev_raw_samples;
01360 
01361         get_block_sizes(ctx, div_blocks, &bs_info);
01362 
01363         for (b = 0; b < ctx->num_blocks; b++) {
01364             bd.block_length = div_blocks[b];
01365 
01366             for (c = 0; c < avctx->channels; c++) {
01367                 bd.const_block = ctx->const_block + c;
01368                 bd.shift_lsbs  = ctx->shift_lsbs + c;
01369                 bd.opt_order   = ctx->opt_order + c;
01370                 bd.store_prev_samples = ctx->store_prev_samples + c;
01371                 bd.use_ltp     = ctx->use_ltp + c;
01372                 bd.ltp_lag     = ctx->ltp_lag + c;
01373                 bd.ltp_gain    = ctx->ltp_gain[c];
01374                 bd.lpc_cof     = ctx->lpc_cof[c];
01375                 bd.quant_cof   = ctx->quant_cof[c];
01376                 bd.raw_samples = ctx->raw_samples[c] + offset;
01377                 bd.raw_other   = NULL;
01378 
01379                 read_block(ctx, &bd);
01380                 if (read_channel_data(ctx, ctx->chan_data[c], c))
01381                     return -1;
01382             }
01383 
01384             for (c = 0; c < avctx->channels; c++)
01385                 if (revert_channel_correlation(ctx, &bd, ctx->chan_data,
01386                                                reverted_channels, offset, c))
01387                     return -1;
01388 
01389             for (c = 0; c < avctx->channels; c++) {
01390                 bd.const_block = ctx->const_block + c;
01391                 bd.shift_lsbs  = ctx->shift_lsbs + c;
01392                 bd.opt_order   = ctx->opt_order + c;
01393                 bd.store_prev_samples = ctx->store_prev_samples + c;
01394                 bd.use_ltp     = ctx->use_ltp + c;
01395                 bd.ltp_lag     = ctx->ltp_lag + c;
01396                 bd.ltp_gain    = ctx->ltp_gain[c];
01397                 bd.lpc_cof     = ctx->lpc_cof[c];
01398                 bd.quant_cof   = ctx->quant_cof[c];
01399                 bd.raw_samples = ctx->raw_samples[c] + offset;
01400                 decode_block(ctx, &bd);
01401             }
01402 
01403             memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));
01404             offset      += div_blocks[b];
01405             bd.ra_block  = 0;
01406         }
01407 
01408         // store carryover raw samples
01409         for (c = 0; c < avctx->channels; c++)
01410             memmove(ctx->raw_samples[c] - sconf->max_order,
01411                     ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,
01412                     sizeof(*ctx->raw_samples[c]) * sconf->max_order);
01413     }
01414 
01415     // TODO: read_diff_float_data
01416 
01417     return 0;
01418 }
01419 
01420 
01423 static int decode_frame(AVCodecContext *avctx,
01424                         void *data, int *data_size,
01425                         AVPacket *avpkt)
01426 {
01427     ALSDecContext *ctx       = avctx->priv_data;
01428     ALSSpecificConfig *sconf = &ctx->sconf;
01429     const uint8_t *buffer    = avpkt->data;
01430     int buffer_size          = avpkt->size;
01431     int invalid_frame, size;
01432     unsigned int c, sample, ra_frame, bytes_read, shift;
01433 
01434     init_get_bits(&ctx->gb, buffer, buffer_size * 8);
01435 
01436     // In the case that the distance between random access frames is set to zero
01437     // (sconf->ra_distance == 0) no frame is treated as a random access frame.
01438     // For the first frame, if prediction is used, all samples used from the
01439     // previous frame are assumed to be zero.
01440     ra_frame = sconf->ra_distance && !(ctx->frame_id % sconf->ra_distance);
01441 
01442     // the last frame to decode might have a different length
01443     if (sconf->samples != 0xFFFFFFFF)
01444         ctx->cur_frame_length = FFMIN(sconf->samples - ctx->frame_id * (uint64_t) sconf->frame_length,
01445                                       sconf->frame_length);
01446     else
01447         ctx->cur_frame_length = sconf->frame_length;
01448 
01449     // decode the frame data
01450     if ((invalid_frame = read_frame_data(ctx, ra_frame) < 0))
01451         av_log(ctx->avctx, AV_LOG_WARNING,
01452                "Reading frame data failed. Skipping RA unit.\n");
01453 
01454     ctx->frame_id++;
01455 
01456     // check for size of decoded data
01457     size = ctx->cur_frame_length * avctx->channels *
01458            (av_get_bits_per_sample_fmt(avctx->sample_fmt) >> 3);
01459 
01460     if (size > *data_size) {
01461         av_log(avctx, AV_LOG_ERROR, "Decoded data exceeds buffer size.\n");
01462         return -1;
01463     }
01464 
01465     *data_size = size;
01466 
01467     // transform decoded frame into output format
01468     #define INTERLEAVE_OUTPUT(bps)                                 \
01469     {                                                              \
01470         int##bps##_t *dest = (int##bps##_t*) data;                 \
01471         shift = bps - ctx->avctx->bits_per_raw_sample;             \
01472         for (sample = 0; sample < ctx->cur_frame_length; sample++) \
01473             for (c = 0; c < avctx->channels; c++)                  \
01474                 *dest++ = ctx->raw_samples[c][sample] << shift;    \
01475     }
01476 
01477     if (ctx->avctx->bits_per_raw_sample <= 16) {
01478         INTERLEAVE_OUTPUT(16)
01479     } else {
01480         INTERLEAVE_OUTPUT(32)
01481     }
01482 
01483     // update CRC
01484     if (sconf->crc_enabled && avctx->error_recognition >= FF_ER_CAREFUL) {
01485         int swap = HAVE_BIGENDIAN != sconf->msb_first;
01486 
01487         if (ctx->avctx->bits_per_raw_sample == 24) {
01488             int32_t *src = data;
01489 
01490             for (sample = 0;
01491                  sample < ctx->cur_frame_length * avctx->channels;
01492                  sample++) {
01493                 int32_t v;
01494 
01495                 if (swap)
01496                     v = av_bswap32(src[sample]);
01497                 else
01498                     v = src[sample];
01499                 if (!HAVE_BIGENDIAN)
01500                     v >>= 8;
01501 
01502                 ctx->crc = av_crc(ctx->crc_table, ctx->crc, (uint8_t*)(&v), 3);
01503             }
01504         } else {
01505             uint8_t *crc_source;
01506 
01507             if (swap) {
01508                 if (ctx->avctx->bits_per_raw_sample <= 16) {
01509                     int16_t *src  = (int16_t*) data;
01510                     int16_t *dest = (int16_t*) ctx->crc_buffer;
01511                     for (sample = 0;
01512                          sample < ctx->cur_frame_length * avctx->channels;
01513                          sample++)
01514                         *dest++ = av_bswap16(src[sample]);
01515                 } else {
01516                     ctx->dsp.bswap_buf((uint32_t*)ctx->crc_buffer, data,
01517                                        ctx->cur_frame_length * avctx->channels);
01518                 }
01519                 crc_source = ctx->crc_buffer;
01520             } else {
01521                 crc_source = data;
01522             }
01523 
01524             ctx->crc = av_crc(ctx->crc_table, ctx->crc, crc_source, size);
01525         }
01526 
01527 
01528         // check CRC sums if this is the last frame
01529         if (ctx->cur_frame_length != sconf->frame_length &&
01530             ctx->crc_org != ctx->crc) {
01531             av_log(avctx, AV_LOG_ERROR, "CRC error.\n");
01532         }
01533     }
01534 
01535 
01536     bytes_read = invalid_frame ? buffer_size :
01537                                  (get_bits_count(&ctx->gb) + 7) >> 3;
01538 
01539     return bytes_read;
01540 }
01541 
01542 
01545 static av_cold int decode_end(AVCodecContext *avctx)
01546 {
01547     ALSDecContext *ctx = avctx->priv_data;
01548 
01549     av_freep(&ctx->sconf.chan_pos);
01550 
01551     ff_bgmc_end(&ctx->bgmc_lut, &ctx->bgmc_lut_status);
01552 
01553     av_freep(&ctx->const_block);
01554     av_freep(&ctx->shift_lsbs);
01555     av_freep(&ctx->opt_order);
01556     av_freep(&ctx->store_prev_samples);
01557     av_freep(&ctx->use_ltp);
01558     av_freep(&ctx->ltp_lag);
01559     av_freep(&ctx->ltp_gain);
01560     av_freep(&ctx->ltp_gain_buffer);
01561     av_freep(&ctx->quant_cof);
01562     av_freep(&ctx->lpc_cof);
01563     av_freep(&ctx->quant_cof_buffer);
01564     av_freep(&ctx->lpc_cof_buffer);
01565     av_freep(&ctx->lpc_cof_reversed_buffer);
01566     av_freep(&ctx->prev_raw_samples);
01567     av_freep(&ctx->raw_samples);
01568     av_freep(&ctx->raw_buffer);
01569     av_freep(&ctx->chan_data);
01570     av_freep(&ctx->chan_data_buffer);
01571     av_freep(&ctx->reverted_channels);
01572     av_freep(&ctx->crc_buffer);
01573 
01574     return 0;
01575 }
01576 
01577 
01580 static av_cold int decode_init(AVCodecContext *avctx)
01581 {
01582     unsigned int c;
01583     unsigned int channel_size;
01584     int num_buffers;
01585     ALSDecContext *ctx = avctx->priv_data;
01586     ALSSpecificConfig *sconf = &ctx->sconf;
01587     ctx->avctx = avctx;
01588 
01589     if (!avctx->extradata) {
01590         av_log(avctx, AV_LOG_ERROR, "Missing required ALS extradata.\n");
01591         return -1;
01592     }
01593 
01594     if (read_specific_config(ctx)) {
01595         av_log(avctx, AV_LOG_ERROR, "Reading ALSSpecificConfig failed.\n");
01596         decode_end(avctx);
01597         return -1;
01598     }
01599 
01600     if (check_specific_config(ctx)) {
01601         decode_end(avctx);
01602         return -1;
01603     }
01604 
01605     if (sconf->bgmc)
01606         ff_bgmc_init(avctx, &ctx->bgmc_lut, &ctx->bgmc_lut_status);
01607 
01608     if (sconf->floating) {
01609         avctx->sample_fmt          = AV_SAMPLE_FMT_FLT;
01610         avctx->bits_per_raw_sample = 32;
01611     } else {
01612         avctx->sample_fmt          = sconf->resolution > 1
01613                                      ? AV_SAMPLE_FMT_S32 : AV_SAMPLE_FMT_S16;
01614         avctx->bits_per_raw_sample = (sconf->resolution + 1) * 8;
01615     }
01616 
01617     // set maximum Rice parameter for progressive decoding based on resolution
01618     // This is not specified in 14496-3 but actually done by the reference
01619     // codec RM22 revision 2.
01620     ctx->s_max = sconf->resolution > 1 ? 31 : 15;
01621 
01622     // set lag value for long-term prediction
01623     ctx->ltp_lag_length = 8 + (avctx->sample_rate >=  96000) +
01624                               (avctx->sample_rate >= 192000);
01625 
01626     // allocate quantized parcor coefficient buffer
01627     num_buffers = sconf->mc_coding ? avctx->channels : 1;
01628 
01629     ctx->quant_cof        = av_malloc(sizeof(*ctx->quant_cof) * num_buffers);
01630     ctx->lpc_cof          = av_malloc(sizeof(*ctx->lpc_cof)   * num_buffers);
01631     ctx->quant_cof_buffer = av_malloc(sizeof(*ctx->quant_cof_buffer) *
01632                                       num_buffers * sconf->max_order);
01633     ctx->lpc_cof_buffer   = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01634                                       num_buffers * sconf->max_order);
01635     ctx->lpc_cof_reversed_buffer = av_malloc(sizeof(*ctx->lpc_cof_buffer) *
01636                                              sconf->max_order);
01637 
01638     if (!ctx->quant_cof              || !ctx->lpc_cof        ||
01639         !ctx->quant_cof_buffer       || !ctx->lpc_cof_buffer ||
01640         !ctx->lpc_cof_reversed_buffer) {
01641         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01642         return AVERROR(ENOMEM);
01643     }
01644 
01645     // assign quantized parcor coefficient buffers
01646     for (c = 0; c < num_buffers; c++) {
01647         ctx->quant_cof[c] = ctx->quant_cof_buffer + c * sconf->max_order;
01648         ctx->lpc_cof[c]   = ctx->lpc_cof_buffer   + c * sconf->max_order;
01649     }
01650 
01651     // allocate and assign lag and gain data buffer for ltp mode
01652     ctx->const_block     = av_malloc (sizeof(*ctx->const_block) * num_buffers);
01653     ctx->shift_lsbs      = av_malloc (sizeof(*ctx->shift_lsbs)  * num_buffers);
01654     ctx->opt_order       = av_malloc (sizeof(*ctx->opt_order)   * num_buffers);
01655     ctx->store_prev_samples = av_malloc(sizeof(*ctx->store_prev_samples) * num_buffers);
01656     ctx->use_ltp         = av_mallocz(sizeof(*ctx->use_ltp)  * num_buffers);
01657     ctx->ltp_lag         = av_malloc (sizeof(*ctx->ltp_lag)  * num_buffers);
01658     ctx->ltp_gain        = av_malloc (sizeof(*ctx->ltp_gain) * num_buffers);
01659     ctx->ltp_gain_buffer = av_malloc (sizeof(*ctx->ltp_gain_buffer) *
01660                                       num_buffers * 5);
01661 
01662     if (!ctx->const_block || !ctx->shift_lsbs ||
01663         !ctx->opt_order || !ctx->store_prev_samples ||
01664         !ctx->use_ltp  || !ctx->ltp_lag ||
01665         !ctx->ltp_gain || !ctx->ltp_gain_buffer) {
01666         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01667         decode_end(avctx);
01668         return AVERROR(ENOMEM);
01669     }
01670 
01671     for (c = 0; c < num_buffers; c++)
01672         ctx->ltp_gain[c] = ctx->ltp_gain_buffer + c * 5;
01673 
01674     // allocate and assign channel data buffer for mcc mode
01675     if (sconf->mc_coding) {
01676         ctx->chan_data_buffer  = av_malloc(sizeof(*ctx->chan_data_buffer) *
01677                                            num_buffers * num_buffers);
01678         ctx->chan_data         = av_malloc(sizeof(*ctx->chan_data) *
01679                                            num_buffers);
01680         ctx->reverted_channels = av_malloc(sizeof(*ctx->reverted_channels) *
01681                                            num_buffers);
01682 
01683         if (!ctx->chan_data_buffer || !ctx->chan_data || !ctx->reverted_channels) {
01684             av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01685             decode_end(avctx);
01686             return AVERROR(ENOMEM);
01687         }
01688 
01689         for (c = 0; c < num_buffers; c++)
01690             ctx->chan_data[c] = ctx->chan_data_buffer + c * num_buffers;
01691     } else {
01692         ctx->chan_data         = NULL;
01693         ctx->chan_data_buffer  = NULL;
01694         ctx->reverted_channels = NULL;
01695     }
01696 
01697     avctx->frame_size = sconf->frame_length;
01698     channel_size      = sconf->frame_length + sconf->max_order;
01699 
01700     ctx->prev_raw_samples = av_malloc (sizeof(*ctx->prev_raw_samples) * sconf->max_order);
01701     ctx->raw_buffer       = av_mallocz(sizeof(*ctx->     raw_buffer)  * avctx->channels * channel_size);
01702     ctx->raw_samples      = av_malloc (sizeof(*ctx->     raw_samples) * avctx->channels);
01703 
01704     // allocate previous raw sample buffer
01705     if (!ctx->prev_raw_samples || !ctx->raw_buffer|| !ctx->raw_samples) {
01706         av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01707         decode_end(avctx);
01708         return AVERROR(ENOMEM);
01709     }
01710 
01711     // assign raw samples buffers
01712     ctx->raw_samples[0] = ctx->raw_buffer + sconf->max_order;
01713     for (c = 1; c < avctx->channels; c++)
01714         ctx->raw_samples[c] = ctx->raw_samples[c - 1] + channel_size;
01715 
01716     // allocate crc buffer
01717     if (HAVE_BIGENDIAN != sconf->msb_first && sconf->crc_enabled &&
01718         avctx->error_recognition >= FF_ER_CAREFUL) {
01719         ctx->crc_buffer = av_malloc(sizeof(*ctx->crc_buffer) *
01720                                     ctx->cur_frame_length *
01721                                     avctx->channels *
01722                                     (av_get_bits_per_sample_fmt(avctx->sample_fmt) >> 3));
01723         if (!ctx->crc_buffer) {
01724             av_log(avctx, AV_LOG_ERROR, "Allocating buffer memory failed.\n");
01725             decode_end(avctx);
01726             return AVERROR(ENOMEM);
01727         }
01728     }
01729 
01730     dsputil_init(&ctx->dsp, avctx);
01731 
01732     return 0;
01733 }
01734 
01735 
01738 static av_cold void flush(AVCodecContext *avctx)
01739 {
01740     ALSDecContext *ctx = avctx->priv_data;
01741 
01742     ctx->frame_id = 0;
01743 }
01744 
01745 
01746 AVCodec ff_als_decoder = {
01747     "als",
01748     AVMEDIA_TYPE_AUDIO,
01749     CODEC_ID_MP4ALS,
01750     sizeof(ALSDecContext),
01751     decode_init,
01752     NULL,
01753     decode_end,
01754     decode_frame,
01755     .flush = flush,
01756     .capabilities = CODEC_CAP_SUBFRAMES,
01757     .long_name = NULL_IF_CONFIG_SMALL("MPEG-4 Audio Lossless Coding (ALS)"),
01758 };
01759