Logo Search packages:      
Sourcecode: aften version File versions  Download package

static int cbr_bit_allocation ( A52ThreadContext *  tctx,
int  prepare 
) [static]

Calculates the snroffset values which, when used, keep the size of the encoded data within a fixed frame size.

Definition at line 241 of file bitalloc.c.

References bit_alloc().

Referenced by compute_bit_allocation(), and vbr_bit_allocation().

{
    A52Context *ctx = tctx->ctx;
    A52Frame *frame = &tctx->frame;
    int current_bits, avail_bits, leftover;
    int snroffst=0;

    current_bits = frame->frame_bits + frame->exp_bits;
    avail_bits = (16 * frame->frame_size) - current_bits;

    if (prepare)
        bit_alloc_prepare(tctx);

    // starting point
    if (ctx->params.encoding_mode == AFTEN_ENC_MODE_VBR)
        snroffst = ctx->params.quality;
    else if (ctx->params.encoding_mode == AFTEN_ENC_MODE_CBR)
        snroffst = tctx->last_quality;
    leftover = avail_bits - bit_alloc(tctx, snroffst);

    if (ctx->params.bitalloc_fast) {
        // fast bit allocation
        int leftover0, leftover1, snr0, snr1;
        snr0 = snr1 = snroffst;
        leftover0 = leftover1 = leftover;
        if (leftover != 0) {
            if (leftover > 0) {
                while (leftover1 > 0 && snr1+16 <= 1023) {
                    snr0 = snr1;
                    leftover0 = leftover1;
                    snr1 += 16;
                    leftover1 = avail_bits - bit_alloc(tctx, snr1);
                }
            } else {
                while (leftover0 < 0 && snr0-16 >= 0) {
                    snr1 = snr0;
                    leftover1 = leftover0;
                    snr0 -= 16;
                    leftover0 = avail_bits - bit_alloc(tctx, snr0);
                }
            }
        }
        if (snr0 != snr1) {
            snroffst = snr0;
            leftover = avail_bits - bit_alloc(tctx, snroffst);
        }
    } else {
        // take up to 3 jumps based on estimated distance from optimal
        if (leftover < -400) {
            snroffst += (leftover / (16 * ctx->n_channels));
            leftover = avail_bits - bit_alloc(tctx, snroffst);
        }
        if (leftover > 400) {
            snroffst += (leftover / (24 * ctx->n_channels));
            leftover = avail_bits - bit_alloc(tctx, snroffst);
        }
        if (leftover < -200) {
            snroffst += (leftover / (40 * ctx->n_channels));
            leftover = avail_bits - bit_alloc(tctx, snroffst);
        }
        // adjust snroffst until leftover <= -100
        while (leftover > -100) {
            snroffst += (10 / ctx->n_channels);
            if (snroffst > 1023) {
                snroffst = 1023;
                leftover = avail_bits - bit_alloc(tctx, snroffst);
                break;
            }
            leftover = avail_bits - bit_alloc(tctx, snroffst);
        }
        // adjust snroffst until leftover is positive
        while (leftover < 0 && snroffst > 0) {
            snroffst--;
            leftover = avail_bits - bit_alloc(tctx, snroffst);
        }
    }

    frame->mant_bits = avail_bits - leftover;
    if (leftover < 0) {
        fprintf(stderr, "bitrate: %d kbps too small\n", frame->bit_rate);
        return -1;
    }

    // set encoding parameters
    frame->csnroffst = snroffst >> 4;
    frame->fsnroffst = snroffst & 0xF;
    frame->quality = snroffst;
    tctx->last_quality = snroffst;

    return 0;
}


Generated by  Doxygen 1.6.0   Back to index