static int compute_bit_allocation(AC3EncodeContext *s,

                                  uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],

                                  uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],

                                  uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],

                                  int frame_bits)

{

    int i, ch;

    int csnroffst, fsnroffst;

    uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];

    static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };



    /* init default parameters */

    s->sdecaycod = 2;

    s->fdecaycod = 1;

    s->sgaincod = 1;

    s->dbkneecod = 2;

    s->floorcod = 4;

    for(ch=0;ch<s->nb_all_channels;ch++) 

        s->fgaincod[ch] = 4;

    

    /* compute real values */

    s->bit_alloc.fscod = s->fscod;

    s->bit_alloc.halfratecod = s->halfratecod;

    s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;

    s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;

    s->bit_alloc.sgain = sgaintab[s->sgaincod];

    s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];

    s->bit_alloc.floor = floortab[s->floorcod];

    

    /* header size */

    frame_bits += 65;

    // if (s->acmod == 2)

    //    frame_bits += 2;

    frame_bits += frame_bits_inc[s->acmod];



    /* audio blocks */

    for(i=0;i<NB_BLOCKS;i++) {

        frame_bits += s->nb_channels * 2 + 2; /* blksw * c, dithflag * c, dynrnge, cplstre */

        if (s->acmod == 2)

            frame_bits++; /* rematstr */

        frame_bits += 2 * s->nb_channels; /* chexpstr[2] * c */

	if (s->lfe)

	    frame_bits++; /* lfeexpstr */

        for(ch=0;ch<s->nb_channels;ch++) {

            if (exp_strategy[i][ch] != EXP_REUSE)

                frame_bits += 6 + 2; /* chbwcod[6], gainrng[2] */

        }

        frame_bits++; /* baie */

        frame_bits++; /* snr */

        frame_bits += 2; /* delta / skip */

    }

    frame_bits++; /* cplinu for block 0 */

    /* bit alloc info */

    /* sdcycod[2], fdcycod[2], sgaincod[2], dbpbcod[2], floorcod[3] */

    /* csnroffset[6] */

    /* (fsnoffset[4] + fgaincod[4]) * c */

    frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);



    /* auxdatae, crcrsv */

    frame_bits += 2;



    /* CRC */

    frame_bits += 16;



    /* now the big work begins : do the bit allocation. Modify the snr

       offset until we can pack everything in the requested frame size */



    csnroffst = s->csnroffst;

    while (csnroffst >= 0 && 

	   bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)

	csnroffst -= SNR_INC1;

    if (csnroffst < 0) {

	av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n");

	return -1;

    }

    while ((csnroffst + SNR_INC1) <= 63 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 

                     csnroffst + SNR_INC1, 0) >= 0) {

        csnroffst += SNR_INC1;

        memcpy(bap, bap1, sizeof(bap1));

    }

    while ((csnroffst + 1) <= 63 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {

        csnroffst++;

        memcpy(bap, bap1, sizeof(bap1));

    }



    fsnroffst = 0;

    while ((fsnroffst + SNR_INC1) <= 15 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 

                     csnroffst, fsnroffst + SNR_INC1) >= 0) {

        fsnroffst += SNR_INC1;

        memcpy(bap, bap1, sizeof(bap1));

    }

    while ((fsnroffst + 1) <= 15 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 

                     csnroffst, fsnroffst + 1) >= 0) {

        fsnroffst++;

        memcpy(bap, bap1, sizeof(bap1));

    }

    

    s->csnroffst = csnroffst;

    for(ch=0;ch<s->nb_all_channels;ch++)

        s->fsnroffst[ch] = fsnroffst;

#if defined(DEBUG_BITALLOC)

    {

        int j;



        for(i=0;i<6;i++) {

            for(ch=0;ch<s->nb_all_channels;ch++) {

                printf("Block #%d Ch%d:\n", i, ch);

                printf("bap=");

                for(j=0;j<s->nb_coefs[ch];j++) {

                    printf("%d ",bap[i][ch][j]);

                }

                printf("\n");

            }

        }

    }

#endif

    return 0;

}

static int compute_bit_allocation(AC3EncodeContext *s,

                                  uint8_t bap[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],

                                  uint8_t encoded_exp[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],

                                  uint8_t exp_strategy[NB_BLOCKS][AC3_MAX_CHANNELS],

                                  int frame_bits)

{

    int i, ch;

    int csnroffst, fsnroffst;

    uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];

    static int frame_bits_inc[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };



    

    s->sdecaycod = 2;

    s->fdecaycod = 1;

    s->sgaincod = 1;

    s->dbkneecod = 2;

    s->floorcod = 4;

    for(ch=0;ch<s->nb_all_channels;ch++) 

        s->fgaincod[ch] = 4;

    

    

    s->bit_alloc.fscod = s->fscod;

    s->bit_alloc.halfratecod = s->halfratecod;

    s->bit_alloc.sdecay = sdecaytab[s->sdecaycod] >> s->halfratecod;

    s->bit_alloc.fdecay = fdecaytab[s->fdecaycod] >> s->halfratecod;

    s->bit_alloc.sgain = sgaintab[s->sgaincod];

    s->bit_alloc.dbknee = dbkneetab[s->dbkneecod];

    s->bit_alloc.floor = floortab[s->floorcod];

    

    

    frame_bits += 65;

    

    

    frame_bits += frame_bits_inc[s->acmod];



    

    for(i=0;i<NB_BLOCKS;i++) {

        frame_bits += s->nb_channels * 2 + 2; 

        if (s->acmod == 2)

            frame_bits++; 

        frame_bits += 2 * s->nb_channels; 

	if (s->lfe)

	    frame_bits++; 

        for(ch=0;ch<s->nb_channels;ch++) {

            if (exp_strategy[i][ch] != EXP_REUSE)

                frame_bits += 6 + 2; 

        }

        frame_bits++; 

        frame_bits++; 

        frame_bits += 2; 

    }

    frame_bits++; 

    

    

    

    

    frame_bits += 2*4 + 3 + 6 + s->nb_all_channels * (4 + 3);



    

    frame_bits += 2;



    

    frame_bits += 16;



    





    csnroffst = s->csnroffst;

    while (csnroffst >= 0 && 

	   bit_alloc(s, bap, encoded_exp, exp_strategy, frame_bits, csnroffst, 0) < 0)

	csnroffst -= SNR_INC1;

    if (csnroffst < 0) {

	av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n");

	return -1;

    }

    while ((csnroffst + SNR_INC1) <= 63 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 

                     csnroffst + SNR_INC1, 0) >= 0) {

        csnroffst += SNR_INC1;

        memcpy(bap, bap1, sizeof(bap1));

    }

    while ((csnroffst + 1) <= 63 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, csnroffst + 1, 0) >= 0) {

        csnroffst++;

        memcpy(bap, bap1, sizeof(bap1));

    }



    fsnroffst = 0;

    while ((fsnroffst + SNR_INC1) <= 15 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 

                     csnroffst, fsnroffst + SNR_INC1) >= 0) {

        fsnroffst += SNR_INC1;

        memcpy(bap, bap1, sizeof(bap1));

    }

    while ((fsnroffst + 1) <= 15 && 

           bit_alloc(s, bap1, encoded_exp, exp_strategy, frame_bits, 

                     csnroffst, fsnroffst + 1) >= 0) {

        fsnroffst++;

        memcpy(bap, bap1, sizeof(bap1));

    }

    

    s->csnroffst = csnroffst;

    for(ch=0;ch<s->nb_all_channels;ch++)

        s->fsnroffst[ch] = fsnroffst;

#if defined(DEBUG_BITALLOC)

    {

        int j;



        for(i=0;i<6;i++) {

            for(ch=0;ch<s->nb_all_channels;ch++) {

                printf("Block #%d Ch%d:\n", i, ch);

                printf("bap=");

                for(j=0;j<s->nb_coefs[ch];j++) {

                    printf("%d ",bap[i][ch][j]);

                }

                printf("\n");

            }

        }

    }

#endif

    return 0;

}

        if (s->acmod == 2)
static int FUNC_0(AC3EncodeContext *VAR_0,

                                  uint8_t VAR_1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],

                                  uint8_t VAR_2[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],

                                  uint8_t VAR_3[NB_BLOCKS][AC3_MAX_CHANNELS],

                                  int VAR_4)

{

    int VAR_5, VAR_6;

    int VAR_7, VAR_8;

    uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2];

    static int VAR_9[8] = { 0, 0, 2, 2, 2, 4, 2, 4 };



    

    VAR_0->sdecaycod = 2;

    VAR_0->fdecaycod = 1;

    VAR_0->sgaincod = 1;

    VAR_0->dbkneecod = 2;

    VAR_0->floorcod = 4;

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

        VAR_0->fgaincod[VAR_6] = 4;

    

    

    VAR_0->bit_alloc.fscod = VAR_0->fscod;

    VAR_0->bit_alloc.halfratecod = VAR_0->halfratecod;

    VAR_0->bit_alloc.sdecay = sdecaytab[VAR_0->sdecaycod] >> VAR_0->halfratecod;

    VAR_0->bit_alloc.fdecay = fdecaytab[VAR_0->fdecaycod] >> VAR_0->halfratecod;

    VAR_0->bit_alloc.sgain = sgaintab[VAR_0->sgaincod];

    VAR_0->bit_alloc.dbknee = dbkneetab[VAR_0->dbkneecod];

    VAR_0->bit_alloc.floor = floortab[VAR_0->floorcod];

    

    

    VAR_4 += 65;

    

    

    VAR_4 += VAR_9[VAR_0->acmod];



    

    for(VAR_5=0;VAR_5<NB_BLOCKS;VAR_5++) {

        VAR_4 += VAR_0->nb_channels * 2 + 2; 

        if (VAR_0->acmod == 2)

            VAR_4++; 

        VAR_4 += 2 * VAR_0->nb_channels; 

	if (VAR_0->lfe)

	    VAR_4++; 

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

            if (VAR_3[VAR_5][VAR_6] != EXP_REUSE)

                VAR_4 += 6 + 2; 

        }

        VAR_4++; 

        VAR_4++; 

        VAR_4 += 2; 

    }

    VAR_4++; 

    

    

    

    

    VAR_4 += 2*4 + 3 + 6 + VAR_0->nb_all_channels * (4 + 3);



    

    VAR_4 += 2;



    

    VAR_4 += 16;



    





    VAR_7 = VAR_0->VAR_7;

    while (VAR_7 >= 0 && 

	   bit_alloc(VAR_0, VAR_1, VAR_2, VAR_3, VAR_4, VAR_7, 0) < 0)

	VAR_7 -= SNR_INC1;

    if (VAR_7 < 0) {

	av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n");

	return -1;

    }

    while ((VAR_7 + SNR_INC1) <= 63 && 

           bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4, 

                     VAR_7 + SNR_INC1, 0) >= 0) {

        VAR_7 += SNR_INC1;

        memcpy(VAR_1, bap1, sizeof(bap1));

    }

    while ((VAR_7 + 1) <= 63 && 

           bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4, VAR_7 + 1, 0) >= 0) {

        VAR_7++;

        memcpy(VAR_1, bap1, sizeof(bap1));

    }



    VAR_8 = 0;

    while ((VAR_8 + SNR_INC1) <= 15 && 

           bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4, 

                     VAR_7, VAR_8 + SNR_INC1) >= 0) {

        VAR_8 += SNR_INC1;

        memcpy(VAR_1, bap1, sizeof(bap1));

    }

    while ((VAR_8 + 1) <= 15 && 

           bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4, 

                     VAR_7, VAR_8 + 1) >= 0) {

        VAR_8++;

        memcpy(VAR_1, bap1, sizeof(bap1));

    }

    

    VAR_0->VAR_7 = VAR_7;

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

        VAR_0->VAR_8[VAR_6] = VAR_8;

#if defined(DEBUG_BITALLOC)

    {

        int j;



        for(VAR_5=0;VAR_5<6;VAR_5++) {

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

                printf("Block #%d Ch%d:\n", VAR_5, VAR_6);

                printf("VAR_1=");

                for(j=0;j<VAR_0->nb_coefs[VAR_6];j++) {

                    printf("%d ",VAR_1[VAR_5][VAR_6][j]);

                }

                printf("\n");

            }

        }

    }

#endif

    return 0;

}

static int FUNC_0(AC3EncodeContext *VAR_0,
uint8_t VAR_1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
uint8_t VAR_2[NB_BLOCKS][AC3_MAX_CHANNELS][N/2],
uint8_t VAR_3[NB_BLOCKS][AC3_MAX_CHANNELS],
int VAR_4)
{ int VAR_5, VAR_6; int VAR_7, VAR_8; uint8_t bap1[NB_BLOCKS][AC3_MAX_CHANNELS][N/2]; static int VAR_9[8] = { 0, 0, 2, 2, 2, 4, 2, 4 }; VAR_0->sdecaycod = 2; VAR_0->fdecaycod = 1; VAR_0->sgaincod = 1; VAR_0->dbkneecod = 2; VAR_0->floorcod = 4; for(VAR_6=0;VAR_6<VAR_0->nb_all_channels;VAR_6++) VAR_0->fgaincod[VAR_6] = 4; VAR_0->bit_alloc.fscod = VAR_0->fscod; VAR_0->bit_alloc.halfratecod = VAR_0->halfratecod; VAR_0->bit_alloc.sdecay = sdecaytab[VAR_0->sdecaycod] >> VAR_0->halfratecod; VAR_0->bit_alloc.fdecay = fdecaytab[VAR_0->fdecaycod] >> VAR_0->halfratecod; VAR_0->bit_alloc.sgain = sgaintab[VAR_0->sgaincod]; VAR_0->bit_alloc.dbknee = dbkneetab[VAR_0->dbkneecod]; VAR_0->bit_alloc.floor = floortab[VAR_0->floorcod]; VAR_4 += 65; VAR_4 += VAR_9[VAR_0->acmod]; for(VAR_5=0;VAR_5<NB_BLOCKS;VAR_5++) { VAR_4 += VAR_0->nb_channels * 2 + 2; if (VAR_0->acmod == 2)
VAR_4++; VAR_4 += 2 * VAR_0->nb_channels; if (VAR_0->lfe)
VAR_4++; for(VAR_6=0;VAR_6<VAR_0->nb_channels;VAR_6++) { if (VAR_3[VAR_5][VAR_6] != EXP_REUSE)
VAR_4 += 6 + 2; } VAR_4++; VAR_4++; VAR_4 += 2; } VAR_4++; VAR_4 += 2*4 + 3 + 6 + VAR_0->nb_all_channels * (4 + 3); VAR_4 += 2; VAR_4 += 16; VAR_7 = VAR_0->VAR_7; while (VAR_7 >= 0 &&
bit_alloc(VAR_0, VAR_1, VAR_2, VAR_3, VAR_4, VAR_7, 0) < 0)
VAR_7 -= SNR_INC1; if (VAR_7 < 0) { av_log(NULL, AV_LOG_ERROR, "Yack, Error !!!\n"); return -1; } while ((VAR_7 + SNR_INC1) <= 63 &&
bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4,
VAR_7 + SNR_INC1, 0) >= 0) { VAR_7 += SNR_INC1; memcpy(VAR_1, bap1, sizeof(bap1)); } while ((VAR_7 + 1) <= 63 &&
bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4, VAR_7 + 1, 0) >= 0) { VAR_7++; memcpy(VAR_1, bap1, sizeof(bap1)); } VAR_8 = 0; while ((VAR_8 + SNR_INC1) <= 15 &&
bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4,
VAR_7, VAR_8 + SNR_INC1) >= 0) { VAR_8 += SNR_INC1; memcpy(VAR_1, bap1, sizeof(bap1)); } while ((VAR_8 + 1) <= 15 &&
bit_alloc(VAR_0, bap1, VAR_2, VAR_3, VAR_4,
VAR_7, VAR_8 + 1) >= 0) { VAR_8++; memcpy(VAR_1, bap1, sizeof(bap1)); } VAR_0->VAR_7 = VAR_7; for(VAR_6=0;VAR_6<VAR_0->nb_all_channels;VAR_6++) VAR_0->VAR_8[VAR_6] = VAR_8; #if defined(DEBUG_BITALLOC)
{ int j; for(VAR_5=0;VAR_5<6;VAR_5++) { for(VAR_6=0;VAR_6<VAR_0->nb_all_channels;VAR_6++) { printf("Block #%d Ch%d:\n", VAR_5, VAR_6); printf("VAR_1="); for(j=0;j<VAR_0->nb_coefs[VAR_6];j++) { printf("%d ",VAR_1[VAR_5][VAR_6][j]); } printf("\n"); } } } #endif
return 0; }