static void get_sample_bits(const float *buf, int *bits)
{
    int i, j;
    short sbuf[128];
    int bitsum = 0, last_bitsum, small_bitsum, big_bitsum;
    short shift, shift_saved;
    int max, sum, last_off, tmp;
    int big_off, small_off;
    int off;
    max = 0;
    for (i = 0; i < NELLY_FILL_LEN; i++) {
        max = FFMAX(max, buf[i]);
    }
    shift = -16;
    shift += headroom(&max);
    sum = 0;
    for (i = 0; i < NELLY_FILL_LEN; i++) {
        sbuf[i] = signed_shift(buf[i], shift);
        sbuf[i] = (3*sbuf[i])>>2;
        sum += sbuf[i];
    }
    shift += 11;
    shift_saved = shift;
    sum -= NELLY_DETAIL_BITS << shift;
    shift += headroom(&sum);
    small_off = (NELLY_BASE_OFF * (sum>>16)) >> 15;
    shift = shift_saved - (NELLY_BASE_SHIFT+shift-31);
    small_off = signed_shift(small_off, shift);
    bitsum = sum_bits(sbuf, shift_saved, small_off);
    if (bitsum != NELLY_DETAIL_BITS) {
        shift = 0;
        off = bitsum - NELLY_DETAIL_BITS;
        for(shift=0; FFABS(off) <= 16383; shift++)
            off *= 2;
        off = (off * NELLY_BASE_OFF) >> 15;
        shift = shift_saved-(NELLY_BASE_SHIFT+shift-15);
        off = signed_shift(off, shift);
        for (j = 1; j < 20; j++) {
            last_off = small_off;
            small_off += off;
            last_bitsum = bitsum;
            bitsum = sum_bits(sbuf, shift_saved, small_off);
            if ((bitsum-NELLY_DETAIL_BITS) * (last_bitsum-NELLY_DETAIL_BITS) <= 0)
                break;
        }
        if (bitsum > NELLY_DETAIL_BITS) {
            big_off = small_off;
            small_off = last_off;
            big_bitsum=bitsum;
            small_bitsum=last_bitsum;
        } else {
            big_off = last_off;
            big_bitsum=last_bitsum;
            small_bitsum=bitsum;
        }
        while (bitsum != NELLY_DETAIL_BITS && j <= 19) {
            off = (big_off+small_off)>>1;
            bitsum = sum_bits(sbuf, shift_saved, off);
            if (bitsum > NELLY_DETAIL_BITS) {
                big_off=off;
                big_bitsum=bitsum;
            } else {
                small_off = off;
                small_bitsum=bitsum;
            }
            j++;
        }
        if (abs(big_bitsum-NELLY_DETAIL_BITS) >=
            abs(small_bitsum-NELLY_DETAIL_BITS)) {
            bitsum = small_bitsum;
        } else {
            small_off = big_off;
            bitsum = big_bitsum;
        }
    }
    for (i = 0; i < NELLY_FILL_LEN; i++) {
        tmp = sbuf[i]-small_off;
        tmp = ((tmp>>(shift_saved-1))+1)>>1;
        bits[i] = av_clip(tmp, 0, NELLY_BIT_CAP);
    }
    if (bitsum > NELLY_DETAIL_BITS) {
        tmp = i = 0;
        while (tmp < NELLY_DETAIL_BITS) {
            tmp += bits[i];
            i++;
        }
        bits[i-1] -= tmp - NELLY_DETAIL_BITS;
        for(; i < NELLY_FILL_LEN; i++)
            bits[i] = 0;
    }
}
['void nelly_decode_block(NellyMoserDecodeContext *s, const unsigned char block[NELLY_BLOCK_LEN], float audio[NELLY_SAMPLES])\n{\n    int i,j;\n    float buf[NELLY_FILL_LEN], pows[NELLY_FILL_LEN];\n    float *aptr, *bptr, *pptr, val, pval;\n    int bits[NELLY_BUF_LEN];\n    unsigned char v;\n    init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);\n    bptr = buf;\n    pptr = pows;\n    val = nelly_init_table[get_bits(&s->gb, 6)];\n    for (i=0 ; i<NELLY_BANDS ; i++) {\n        if (i > 0)\n            val += nelly_delta_table[get_bits(&s->gb, 5)];\n        pval = pow(2, val/2048);\n        for (j = 0; j < nelly_band_sizes_table[i]; j++) {\n            *bptr++ = val;\n            *pptr++ = pval;\n        }\n    }\n    get_sample_bits(buf, bits);\n    for (i = 0; i < 2; i++) {\n        aptr = audio + i * NELLY_BUF_LEN;\n        init_get_bits(&s->gb, block, NELLY_BLOCK_LEN * 8);\n        skip_bits(&s->gb, NELLY_HEADER_BITS + i*NELLY_DETAIL_BITS);\n        for (j = 0; j < NELLY_FILL_LEN; j++) {\n            if (bits[j] <= 0) {\n                aptr[j] = M_SQRT1_2*pows[j];\n                if (av_random(&s->random_state) & 1)\n                    aptr[j] *= -1.0;\n            } else {\n                v = get_bits(&s->gb, bits[j]);\n                aptr[j] = dequantization_table[(1<<bits[j])-1+v]*pows[j];\n            }\n        }\n        memset(&aptr[NELLY_FILL_LEN], 0,\n               (NELLY_BUF_LEN - NELLY_FILL_LEN) * sizeof(float));\n        s->imdct_ctx.fft.imdct_calc(&s->imdct_ctx, s->imdct_out,\n                                    aptr, s->imdct_tmp);\n        memcpy(&aptr[0],&s->imdct_out[NELLY_BUF_LEN+NELLY_BUF_LEN/2], (NELLY_BUF_LEN/2)*sizeof(float));\n        memcpy(&aptr[NELLY_BUF_LEN / 2],&s->imdct_out[0],(NELLY_BUF_LEN/2)*sizeof(float));\n        overlap_and_window(s, s->state, aptr);\n    }\n}', 'static void get_sample_bits(const float *buf, int *bits)\n{\n    int i, j;\n    short sbuf[128];\n    int bitsum = 0, last_bitsum, small_bitsum, big_bitsum;\n    short shift, shift_saved;\n    int max, sum, last_off, tmp;\n    int big_off, small_off;\n    int off;\n    max = 0;\n    for (i = 0; i < NELLY_FILL_LEN; i++) {\n        max = FFMAX(max, buf[i]);\n    }\n    shift = -16;\n    shift += headroom(&max);\n    sum = 0;\n    for (i = 0; i < NELLY_FILL_LEN; i++) {\n        sbuf[i] = signed_shift(buf[i], shift);\n        sbuf[i] = (3*sbuf[i])>>2;\n        sum += sbuf[i];\n    }\n    shift += 11;\n    shift_saved = shift;\n    sum -= NELLY_DETAIL_BITS << shift;\n    shift += headroom(&sum);\n    small_off = (NELLY_BASE_OFF * (sum>>16)) >> 15;\n    shift = shift_saved - (NELLY_BASE_SHIFT+shift-31);\n    small_off = signed_shift(small_off, shift);\n    bitsum = sum_bits(sbuf, shift_saved, small_off);\n    if (bitsum != NELLY_DETAIL_BITS) {\n        shift = 0;\n        off = bitsum - NELLY_DETAIL_BITS;\n        for(shift=0; FFABS(off) <= 16383; shift++)\n            off *= 2;\n        off = (off * NELLY_BASE_OFF) >> 15;\n        shift = shift_saved-(NELLY_BASE_SHIFT+shift-15);\n        off = signed_shift(off, shift);\n        for (j = 1; j < 20; j++) {\n            last_off = small_off;\n            small_off += off;\n            last_bitsum = bitsum;\n            bitsum = sum_bits(sbuf, shift_saved, small_off);\n            if ((bitsum-NELLY_DETAIL_BITS) * (last_bitsum-NELLY_DETAIL_BITS) <= 0)\n                break;\n        }\n        if (bitsum > NELLY_DETAIL_BITS) {\n            big_off = small_off;\n            small_off = last_off;\n            big_bitsum=bitsum;\n            small_bitsum=last_bitsum;\n        } else {\n            big_off = last_off;\n            big_bitsum=last_bitsum;\n            small_bitsum=bitsum;\n        }\n        while (bitsum != NELLY_DETAIL_BITS && j <= 19) {\n            off = (big_off+small_off)>>1;\n            bitsum = sum_bits(sbuf, shift_saved, off);\n            if (bitsum > NELLY_DETAIL_BITS) {\n                big_off=off;\n                big_bitsum=bitsum;\n            } else {\n                small_off = off;\n                small_bitsum=bitsum;\n            }\n            j++;\n        }\n        if (abs(big_bitsum-NELLY_DETAIL_BITS) >=\n            abs(small_bitsum-NELLY_DETAIL_BITS)) {\n            bitsum = small_bitsum;\n        } else {\n            small_off = big_off;\n            bitsum = big_bitsum;\n        }\n    }\n    for (i = 0; i < NELLY_FILL_LEN; i++) {\n        tmp = sbuf[i]-small_off;\n        tmp = ((tmp>>(shift_saved-1))+1)>>1;\n        bits[i] = av_clip(tmp, 0, NELLY_BIT_CAP);\n    }\n    if (bitsum > NELLY_DETAIL_BITS) {\n        tmp = i = 0;\n        while (tmp < NELLY_DETAIL_BITS) {\n            tmp += bits[i];\n            i++;\n        }\n        bits[i-1] -= tmp - NELLY_DETAIL_BITS;\n        for(; i < NELLY_FILL_LEN; i++)\n            bits[i] = 0;\n    }\n}']