static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)

{

    ALSSpecificConfig *sconf = &ctx->sconf;

    AVCodecContext *avctx    = ctx->avctx;

    GetBitContext *gb = &ctx->gb;

    unsigned int div_blocks[32];                ///< block sizes.

    unsigned int c;

    unsigned int js_blocks[2];



    uint32_t bs_info = 0;



    // skip the size of the ra unit if present in the frame

    if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)

        skip_bits_long(gb, 32);



    if (sconf->mc_coding && sconf->joint_stereo) {

        ctx->js_switch = get_bits1(gb);

        align_get_bits(gb);

    }



    if (!sconf->mc_coding || ctx->js_switch) {

        int independent_bs = !sconf->joint_stereo;



        for (c = 0; c < avctx->channels; c++) {

            js_blocks[0] = 0;

            js_blocks[1] = 0;



            get_block_sizes(ctx, div_blocks, &bs_info);



            // if joint_stereo and block_switching is set, independent decoding

            // is signaled via the first bit of bs_info

            if (sconf->joint_stereo && sconf->block_switching)

                if (bs_info >> 31)

                    independent_bs = 2;



            // if this is the last channel, it has to be decoded independently

            if (c == avctx->channels - 1)

                independent_bs = 1;



            if (independent_bs) {

                if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))

                    return -1;



                independent_bs--;

            } else {

                if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))

                    return -1;



                c++;

            }



            // store carryover raw samples

            memmove(ctx->raw_samples[c] - sconf->max_order,

                    ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,

                    sizeof(*ctx->raw_samples[c]) * sconf->max_order);

        }

    } else { // multi-channel coding

        ALSBlockData   bd;

        int            b;

        int            *reverted_channels = ctx->reverted_channels;

        unsigned int   offset             = 0;



        for (c = 0; c < avctx->channels; c++)

            if (ctx->chan_data[c] < ctx->chan_data_buffer) {

                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");

                return -1;

            }



        memset(&bd,               0, sizeof(ALSBlockData));

        memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);



        bd.ra_block         = ra_frame;

        bd.prev_raw_samples = ctx->prev_raw_samples;



        get_block_sizes(ctx, div_blocks, &bs_info);



        for (b = 0; b < ctx->num_blocks; b++) {

            bd.block_length = div_blocks[b];



            for (c = 0; c < avctx->channels; c++) {

                bd.const_block = ctx->const_block + c;

                bd.shift_lsbs  = ctx->shift_lsbs + c;

                bd.opt_order   = ctx->opt_order + c;

                bd.store_prev_samples = ctx->store_prev_samples + c;

                bd.use_ltp     = ctx->use_ltp + c;

                bd.ltp_lag     = ctx->ltp_lag + c;

                bd.ltp_gain    = ctx->ltp_gain[c];

                bd.lpc_cof     = ctx->lpc_cof[c];

                bd.quant_cof   = ctx->quant_cof[c];

                bd.raw_samples = ctx->raw_samples[c] + offset;

                bd.raw_other   = NULL;



                read_block(ctx, &bd);

                if (read_channel_data(ctx, ctx->chan_data[c], c))

                    return -1;

            }



            for (c = 0; c < avctx->channels; c++)

                if (revert_channel_correlation(ctx, &bd, ctx->chan_data,

                                               reverted_channels, offset, c))

                    return -1;



            for (c = 0; c < avctx->channels; c++) {

                bd.const_block = ctx->const_block + c;

                bd.shift_lsbs  = ctx->shift_lsbs + c;

                bd.opt_order   = ctx->opt_order + c;

                bd.store_prev_samples = ctx->store_prev_samples + c;

                bd.use_ltp     = ctx->use_ltp + c;

                bd.ltp_lag     = ctx->ltp_lag + c;

                bd.ltp_gain    = ctx->ltp_gain[c];

                bd.lpc_cof     = ctx->lpc_cof[c];

                bd.quant_cof   = ctx->quant_cof[c];

                bd.raw_samples = ctx->raw_samples[c] + offset;

                decode_block(ctx, &bd);

            }



            memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));

            offset      += div_blocks[b];

            bd.ra_block  = 0;

        }



        // store carryover raw samples

        for (c = 0; c < avctx->channels; c++)

            memmove(ctx->raw_samples[c] - sconf->max_order,

                    ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,

                    sizeof(*ctx->raw_samples[c]) * sconf->max_order);

    }



    // TODO: read_diff_float_data



    return 0;

}

static int read_frame_data(ALSDecContext *ctx, unsigned int ra_frame)

{

    ALSSpecificConfig *sconf = &ctx->sconf;

    AVCodecContext *avctx    = ctx->avctx;

    GetBitContext *gb = &ctx->gb;

    unsigned int div_blocks[32];                

    unsigned int c;

    unsigned int js_blocks[2];



    uint32_t bs_info = 0;



    

    if (sconf->ra_flag == RA_FLAG_FRAMES && ra_frame)

        skip_bits_long(gb, 32);



    if (sconf->mc_coding && sconf->joint_stereo) {

        ctx->js_switch = get_bits1(gb);

        align_get_bits(gb);

    }



    if (!sconf->mc_coding || ctx->js_switch) {

        int independent_bs = !sconf->joint_stereo;



        for (c = 0; c < avctx->channels; c++) {

            js_blocks[0] = 0;

            js_blocks[1] = 0;



            get_block_sizes(ctx, div_blocks, &bs_info);



            

            

            if (sconf->joint_stereo && sconf->block_switching)

                if (bs_info >> 31)

                    independent_bs = 2;



            

            if (c == avctx->channels - 1)

                independent_bs = 1;



            if (independent_bs) {

                if (decode_blocks_ind(ctx, ra_frame, c, div_blocks, js_blocks))

                    return -1;



                independent_bs--;

            } else {

                if (decode_blocks(ctx, ra_frame, c, div_blocks, js_blocks))

                    return -1;



                c++;

            }



            

            memmove(ctx->raw_samples[c] - sconf->max_order,

                    ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,

                    sizeof(*ctx->raw_samples[c]) * sconf->max_order);

        }

    } else { 

        ALSBlockData   bd;

        int            b;

        int            *reverted_channels = ctx->reverted_channels;

        unsigned int   offset             = 0;



        for (c = 0; c < avctx->channels; c++)

            if (ctx->chan_data[c] < ctx->chan_data_buffer) {

                av_log(ctx->avctx, AV_LOG_ERROR, "Invalid channel data!\n");

                return -1;

            }



        memset(&bd,               0, sizeof(ALSBlockData));

        memset(reverted_channels, 0, sizeof(*reverted_channels) * avctx->channels);



        bd.ra_block         = ra_frame;

        bd.prev_raw_samples = ctx->prev_raw_samples;



        get_block_sizes(ctx, div_blocks, &bs_info);



        for (b = 0; b < ctx->num_blocks; b++) {

            bd.block_length = div_blocks[b];



            for (c = 0; c < avctx->channels; c++) {

                bd.const_block = ctx->const_block + c;

                bd.shift_lsbs  = ctx->shift_lsbs + c;

                bd.opt_order   = ctx->opt_order + c;

                bd.store_prev_samples = ctx->store_prev_samples + c;

                bd.use_ltp     = ctx->use_ltp + c;

                bd.ltp_lag     = ctx->ltp_lag + c;

                bd.ltp_gain    = ctx->ltp_gain[c];

                bd.lpc_cof     = ctx->lpc_cof[c];

                bd.quant_cof   = ctx->quant_cof[c];

                bd.raw_samples = ctx->raw_samples[c] + offset;

                bd.raw_other   = NULL;



                read_block(ctx, &bd);

                if (read_channel_data(ctx, ctx->chan_data[c], c))

                    return -1;

            }



            for (c = 0; c < avctx->channels; c++)

                if (revert_channel_correlation(ctx, &bd, ctx->chan_data,

                                               reverted_channels, offset, c))

                    return -1;



            for (c = 0; c < avctx->channels; c++) {

                bd.const_block = ctx->const_block + c;

                bd.shift_lsbs  = ctx->shift_lsbs + c;

                bd.opt_order   = ctx->opt_order + c;

                bd.store_prev_samples = ctx->store_prev_samples + c;

                bd.use_ltp     = ctx->use_ltp + c;

                bd.ltp_lag     = ctx->ltp_lag + c;

                bd.ltp_gain    = ctx->ltp_gain[c];

                bd.lpc_cof     = ctx->lpc_cof[c];

                bd.quant_cof   = ctx->quant_cof[c];

                bd.raw_samples = ctx->raw_samples[c] + offset;

                decode_block(ctx, &bd);

            }



            memset(reverted_channels, 0, avctx->channels * sizeof(*reverted_channels));

            offset      += div_blocks[b];

            bd.ra_block  = 0;

        }



        

        for (c = 0; c < avctx->channels; c++)

            memmove(ctx->raw_samples[c] - sconf->max_order,

                    ctx->raw_samples[c] - sconf->max_order + sconf->frame_length,

                    sizeof(*ctx->raw_samples[c]) * sconf->max_order);

    }



    



    return 0;

}

static int FUNC_0(ALSDecContext *VAR_0, unsigned int VAR_1)

{

    ALSSpecificConfig *sconf = &VAR_0->sconf;

    AVCodecContext *avctx    = VAR_0->avctx;

    GetBitContext *gb = &VAR_0->gb;

    unsigned int VAR_2[32];                

    unsigned int VAR_3;

    unsigned int VAR_4[2];



    uint32_t bs_info = 0;



    

    if (sconf->ra_flag == RA_FLAG_FRAMES && VAR_1)

        skip_bits_long(gb, 32);



    if (sconf->mc_coding && sconf->joint_stereo) {

        VAR_0->js_switch = get_bits1(gb);

        align_get_bits(gb);

    }



    if (!sconf->mc_coding || VAR_0->js_switch) {

        int VAR_5 = !sconf->joint_stereo;



        for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) {

            VAR_4[0] = 0;

            VAR_4[1] = 0;



            get_block_sizes(VAR_0, VAR_2, &bs_info);



            

            

            if (sconf->joint_stereo && sconf->block_switching)

                if (bs_info >> 31)

                    VAR_5 = 2;



            

            if (VAR_3 == avctx->channels - 1)

                VAR_5 = 1;



            if (VAR_5) {

                if (decode_blocks_ind(VAR_0, VAR_1, VAR_3, VAR_2, VAR_4))

                    return -1;



                VAR_5--;

            } else {

                if (decode_blocks(VAR_0, VAR_1, VAR_3, VAR_2, VAR_4))

                    return -1;



                VAR_3++;

            }



            

            memmove(VAR_0->raw_samples[VAR_3] - sconf->max_order,

                    VAR_0->raw_samples[VAR_3] - sconf->max_order + sconf->frame_length,

                    sizeof(*VAR_0->raw_samples[VAR_3]) * sconf->max_order);

        }

    } else { 

        ALSBlockData   bd;

        int            VAR_6;

        int            *VAR_7 = VAR_0->VAR_7;

        unsigned int   VAR_8             = 0;



        for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++)

            if (VAR_0->chan_data[VAR_3] < VAR_0->chan_data_buffer) {

                av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid channel data!\n");

                return -1;

            }



        memset(&bd,               0, sizeof(ALSBlockData));

        memset(VAR_7, 0, sizeof(*VAR_7) * avctx->channels);



        bd.ra_block         = VAR_1;

        bd.prev_raw_samples = VAR_0->prev_raw_samples;



        get_block_sizes(VAR_0, VAR_2, &bs_info);



        for (VAR_6 = 0; VAR_6 < VAR_0->num_blocks; VAR_6++) {

            bd.block_length = VAR_2[VAR_6];



            for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) {

                bd.const_block = VAR_0->const_block + VAR_3;

                bd.shift_lsbs  = VAR_0->shift_lsbs + VAR_3;

                bd.opt_order   = VAR_0->opt_order + VAR_3;

                bd.store_prev_samples = VAR_0->store_prev_samples + VAR_3;

                bd.use_ltp     = VAR_0->use_ltp + VAR_3;

                bd.ltp_lag     = VAR_0->ltp_lag + VAR_3;

                bd.ltp_gain    = VAR_0->ltp_gain[VAR_3];

                bd.lpc_cof     = VAR_0->lpc_cof[VAR_3];

                bd.quant_cof   = VAR_0->quant_cof[VAR_3];

                bd.raw_samples = VAR_0->raw_samples[VAR_3] + VAR_8;

                bd.raw_other   = NULL;



                read_block(VAR_0, &bd);

                if (read_channel_data(VAR_0, VAR_0->chan_data[VAR_3], VAR_3))

                    return -1;

            }



            for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++)

                if (revert_channel_correlation(VAR_0, &bd, VAR_0->chan_data,

                                               VAR_7, VAR_8, VAR_3))

                    return -1;



            for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) {

                bd.const_block = VAR_0->const_block + VAR_3;

                bd.shift_lsbs  = VAR_0->shift_lsbs + VAR_3;

                bd.opt_order   = VAR_0->opt_order + VAR_3;

                bd.store_prev_samples = VAR_0->store_prev_samples + VAR_3;

                bd.use_ltp     = VAR_0->use_ltp + VAR_3;

                bd.ltp_lag     = VAR_0->ltp_lag + VAR_3;

                bd.ltp_gain    = VAR_0->ltp_gain[VAR_3];

                bd.lpc_cof     = VAR_0->lpc_cof[VAR_3];

                bd.quant_cof   = VAR_0->quant_cof[VAR_3];

                bd.raw_samples = VAR_0->raw_samples[VAR_3] + VAR_8;

                decode_block(VAR_0, &bd);

            }



            memset(VAR_7, 0, avctx->channels * sizeof(*VAR_7));

            VAR_8      += VAR_2[VAR_6];

            bd.ra_block  = 0;

        }



        

        for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++)

            memmove(VAR_0->raw_samples[VAR_3] - sconf->max_order,

                    VAR_0->raw_samples[VAR_3] - sconf->max_order + sconf->frame_length,

                    sizeof(*VAR_0->raw_samples[VAR_3]) * sconf->max_order);

    }



    



    return 0;

}

static int FUNC_0(ALSDecContext *VAR_0, unsigned int VAR_1)
{ ALSSpecificConfig *sconf = &VAR_0->sconf; AVCodecContext *avctx    = VAR_0->avctx; GetBitContext *gb = &VAR_0->gb; unsigned int VAR_2[32]; unsigned int VAR_3; unsigned int VAR_4[2]; uint32_t bs_info = 0; if (sconf->ra_flag == RA_FLAG_FRAMES && VAR_1)
skip_bits_long(gb, 32); if (sconf->mc_coding && sconf->joint_stereo) { VAR_0->js_switch = get_bits1(gb); align_get_bits(gb); } if (!sconf->mc_coding || VAR_0->js_switch) { int VAR_5 = !sconf->joint_stereo; for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) { VAR_4[0] = 0; VAR_4[1] = 0; get_block_sizes(VAR_0, VAR_2, &bs_info); if (sconf->joint_stereo && sconf->block_switching)
if (bs_info >> 31)
VAR_5 = 2; if (VAR_3 == avctx->channels - 1)
VAR_5 = 1; if (VAR_5) { if (decode_blocks_ind(VAR_0, VAR_1, VAR_3, VAR_2, VAR_4))
return -1; VAR_5--; } else { if (decode_blocks(VAR_0, VAR_1, VAR_3, VAR_2, VAR_4))
return -1; VAR_3++; } memmove(VAR_0->raw_samples[VAR_3] - sconf->max_order,
VAR_0->raw_samples[VAR_3] - sconf->max_order + sconf->frame_length,
sizeof(*VAR_0->raw_samples[VAR_3]) * sconf->max_order); } } else { ALSBlockData   bd; int            VAR_6; int            *VAR_7 = VAR_0->VAR_7; unsigned int   VAR_8             = 0; for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) if (VAR_0->chan_data[VAR_3] < VAR_0->chan_data_buffer) { av_log(VAR_0->avctx, AV_LOG_ERROR, "Invalid channel data!\n"); return -1; } memset(&bd,               0, sizeof(ALSBlockData)); memset(VAR_7, 0, sizeof(*VAR_7) * avctx->channels); bd.ra_block         = VAR_1; bd.prev_raw_samples = VAR_0->prev_raw_samples; get_block_sizes(VAR_0, VAR_2, &bs_info); for (VAR_6 = 0; VAR_6 < VAR_0->num_blocks; VAR_6++) { bd.block_length = VAR_2[VAR_6]; for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) { bd.const_block = VAR_0->const_block + VAR_3; bd.shift_lsbs  = VAR_0->shift_lsbs + VAR_3; bd.opt_order   = VAR_0->opt_order + VAR_3; bd.store_prev_samples = VAR_0->store_prev_samples + VAR_3; bd.use_ltp     = VAR_0->use_ltp + VAR_3; bd.ltp_lag     = VAR_0->ltp_lag + VAR_3; bd.ltp_gain    = VAR_0->ltp_gain[VAR_3]; bd.lpc_cof     = VAR_0->lpc_cof[VAR_3]; bd.quant_cof   = VAR_0->quant_cof[VAR_3]; bd.raw_samples = VAR_0->raw_samples[VAR_3] + VAR_8; bd.raw_other   = NULL; read_block(VAR_0, &bd); if (read_channel_data(VAR_0, VAR_0->chan_data[VAR_3], VAR_3))
return -1; } for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) if (revert_channel_correlation(VAR_0, &bd, VAR_0->chan_data,
VAR_7, VAR_8, VAR_3))
return -1; for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) { bd.const_block = VAR_0->const_block + VAR_3; bd.shift_lsbs  = VAR_0->shift_lsbs + VAR_3; bd.opt_order   = VAR_0->opt_order + VAR_3; bd.store_prev_samples = VAR_0->store_prev_samples + VAR_3; bd.use_ltp     = VAR_0->use_ltp + VAR_3; bd.ltp_lag     = VAR_0->ltp_lag + VAR_3; bd.ltp_gain    = VAR_0->ltp_gain[VAR_3]; bd.lpc_cof     = VAR_0->lpc_cof[VAR_3]; bd.quant_cof   = VAR_0->quant_cof[VAR_3]; bd.raw_samples = VAR_0->raw_samples[VAR_3] + VAR_8; decode_block(VAR_0, &bd); } memset(VAR_7, 0, avctx->channels * sizeof(*VAR_7)); VAR_8      += VAR_2[VAR_6]; bd.ra_block  = 0; } for (VAR_3 = 0; VAR_3 < avctx->channels; VAR_3++) memmove(VAR_0->raw_samples[VAR_3] - sconf->max_order,
VAR_0->raw_samples[VAR_3] - sconf->max_order + sconf->frame_length,
sizeof(*VAR_0->raw_samples[VAR_3]) * sconf->max_order); } return 0; }