static int cbr_bit_allocation(AC3EncodeContext *s)

{

    int ch;

    int bits_left;

    int snr_offset, snr_incr;



    bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);



    snr_offset = s->coarse_snr_offset << 4;



    while (snr_offset >= 0 &&

           bit_alloc(s, snr_offset) > bits_left) {

        snr_offset -= 64;

    }

    if (snr_offset < 0)

        return AVERROR(EINVAL);



    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);

    for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {

        while (snr_offset + 64 <= 1023 &&

               bit_alloc(s, snr_offset + snr_incr) <= bits_left) {

            snr_offset += snr_incr;

            FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);

        }

    }

    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);

    reset_block_bap(s);



    s->coarse_snr_offset = snr_offset >> 4;

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

        s->fine_snr_offset[ch] = snr_offset & 0xF;



    return 0;

}

static int cbr_bit_allocation(AC3EncodeContext *s)

{

    int ch;

    int bits_left;

    int snr_offset, snr_incr;



    bits_left = 8 * s->frame_size - (s->frame_bits + s->exponent_bits);



    snr_offset = s->coarse_snr_offset << 4;



    while (snr_offset >= 0 &&

           bit_alloc(s, snr_offset) > bits_left) {

        snr_offset -= 64;

    }

    if (snr_offset < 0)

        return AVERROR(EINVAL);



    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);

    for (snr_incr = 64; snr_incr > 0; snr_incr >>= 2) {

        while (snr_offset + 64 <= 1023 &&

               bit_alloc(s, snr_offset + snr_incr) <= bits_left) {

            snr_offset += snr_incr;

            FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);

        }

    }

    FFSWAP(uint8_t *, s->bap_buffer, s->bap1_buffer);

    reset_block_bap(s);



    s->coarse_snr_offset = snr_offset >> 4;

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

        s->fine_snr_offset[ch] = snr_offset & 0xF;



    return 0;

}

static int FUNC_0(AC3EncodeContext *VAR_0)

{

    int VAR_1;

    int VAR_2;

    int VAR_3, VAR_4;



    VAR_2 = 8 * VAR_0->frame_size - (VAR_0->frame_bits + VAR_0->exponent_bits);



    VAR_3 = VAR_0->coarse_snr_offset << 4;



    while (VAR_3 >= 0 &&

           bit_alloc(VAR_0, VAR_3) > VAR_2) {

        VAR_3 -= 64;

    }

    if (VAR_3 < 0)

        return AVERROR(EINVAL);



    FFSWAP(uint8_t *, VAR_0->bap_buffer, VAR_0->bap1_buffer);

    for (VAR_4 = 64; VAR_4 > 0; VAR_4 >>= 2) {

        while (VAR_3 + 64 <= 1023 &&

               bit_alloc(VAR_0, VAR_3 + VAR_4) <= VAR_2) {

            VAR_3 += VAR_4;

            FFSWAP(uint8_t *, VAR_0->bap_buffer, VAR_0->bap1_buffer);

        }

    }

    FFSWAP(uint8_t *, VAR_0->bap_buffer, VAR_0->bap1_buffer);

    reset_block_bap(VAR_0);



    VAR_0->coarse_snr_offset = VAR_3 >> 4;

    for (VAR_1 = 0; VAR_1 < VAR_0->channels; VAR_1++)

        VAR_0->fine_snr_offset[VAR_1] = VAR_3 & 0xF;



    return 0;

}

static int FUNC_0(AC3EncodeContext *VAR_0)
{ int VAR_1; int VAR_2; int VAR_3, VAR_4; VAR_2 = 8 * VAR_0->frame_size - (VAR_0->frame_bits + VAR_0->exponent_bits); VAR_3 = VAR_0->coarse_snr_offset << 4; while (VAR_3 >= 0 &&
bit_alloc(VAR_0, VAR_3) > VAR_2) { VAR_3 -= 64; } if (VAR_3 < 0)
return AVERROR(EINVAL); FFSWAP(uint8_t *, VAR_0->bap_buffer, VAR_0->bap1_buffer); for (VAR_4 = 64; VAR_4 > 0; VAR_4 >>= 2) { while (VAR_3 + 64 <= 1023 &&
bit_alloc(VAR_0, VAR_3 + VAR_4) <= VAR_2) { VAR_3 += VAR_4; FFSWAP(uint8_t *, VAR_0->bap_buffer, VAR_0->bap1_buffer); } } FFSWAP(uint8_t *, VAR_0->bap_buffer, VAR_0->bap1_buffer); reset_block_bap(VAR_0); VAR_0->coarse_snr_offset = VAR_3 >> 4; for (VAR_1 = 0; VAR_1 < VAR_0->channels; VAR_1++) VAR_0->fine_snr_offset[VAR_1] = VAR_3 & 0xF; return 0; }