
static int count_hq_slice(VC2EncContext *s, int slice_x,
                          int slice_y, int quant_idx)
{
    int x, y, left, right, top, bottom, qfactor;
    uint8_t quants[MAX_DWT_LEVELS][4];
    int bits = 0, p, level, orientation;

    bits += 8*s->prefix_bytes;
    bits += 8; /* quant_idx */

    for (level = 0; level < s->wavelet_depth; level++)
        for (orientation = !!level; orientation < 4; orientation++)
            quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);

    for (p = 0; p < 3; p++) {
        int bytes_start, bytes_len, pad_s, pad_c;
        bytes_start = bits >> 3;
        bits += 8;
        for (level = 0; level < s->wavelet_depth; level++) {
            for (orientation = !!level; orientation < 4; orientation++) {
                dwtcoef *buf;
                SubBand *b = &s->plane[p].band[level][orientation];

                quant_idx = quants[level][orientation];
                qfactor = ff_dirac_qscale_tab[quant_idx];

                left   = b->width  * slice_x    / s->num_x;
                right  = b->width  *(slice_x+1) / s->num_x;
                top    = b->height * slice_y    / s->num_y;
                bottom = b->height *(slice_y+1) / s->num_y;

                buf = b->buf + top * b->stride;

                for (y = top; y < bottom; y++) {
                    for (x = left; x < right; x++) {
                        qcoef coeff = (qcoef)buf[x];
                        if (coeff >= -COEF_LUT_TAB && coeff < COEF_LUT_TAB) {
                            bits += s->coef_lut_len[2*quant_idx*COEF_LUT_TAB + coeff + COEF_LUT_TAB];
                        } else {
                            QUANT(coeff)
                            bits += count_vc2_ue_uint(abs(coeff));
                            bits += !!coeff;
                        }
                    }
                    buf += b->stride;
                }
            }
        }
        bits += FFALIGN(bits, 8) - bits;
        bytes_len = (bits >> 3) - bytes_start - 1;
        pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
        pad_c = (pad_s*s->size_scaler) - bytes_len;
        bits += pad_c*8;
    }

    return bits;

static int count_hq_slice(VC2EncContext *s, int slice_x,
                          int slice_y, int quant_idx)
{
    int x, y, left, right, top, bottom, qfactor;
    uint8_t quants[MAX_DWT_LEVELS][4];
    int bits = 0, p, level, orientation;

    bits += 8*s->prefix_bytes;
    bits += 8;  

    for (level = 0; level < s->wavelet_depth; level++)
        for (orientation = !!level; orientation < 4; orientation++)
            quants[level][orientation] = FFMAX(quant_idx - s->quant[level][orientation], 0);

    for (p = 0; p < 3; p++) {
        int bytes_start, bytes_len, pad_s, pad_c;
        bytes_start = bits >> 3;
        bits += 8;
        for (level = 0; level < s->wavelet_depth; level++) {
            for (orientation = !!level; orientation < 4; orientation++) {
                dwtcoef *buf;
                SubBand *b = &s->plane[p].band[level][orientation];

                quant_idx = quants[level][orientation];
                qfactor = ff_dirac_qscale_tab[quant_idx];

                left   = b->width  * slice_x    / s->num_x;
                right  = b->width  *(slice_x+1) / s->num_x;
                top    = b->height * slice_y    / s->num_y;
                bottom = b->height *(slice_y+1) / s->num_y;

                buf = b->buf + top * b->stride;

                for (y = top; y < bottom; y++) {
                    for (x = left; x < right; x++) {
                        qcoef coeff = (qcoef)buf[x];
                        if (coeff >= -COEF_LUT_TAB && coeff < COEF_LUT_TAB) {
                            bits += s->coef_lut_len[2*quant_idx*COEF_LUT_TAB + coeff + COEF_LUT_TAB];
                        } else {
                            QUANT(coeff)
                            bits += count_vc2_ue_uint(abs(coeff));
                            bits += !!coeff;
                        }
                    }
                    buf += b->stride;
                }
            }
        }
        bits += FFALIGN(bits, 8) - bits;
        bytes_len = (bits >> 3) - bytes_start - 1;
        pad_s = FFALIGN(bytes_len, s->size_scaler)/s->size_scaler;
        pad_c = (pad_s*s->size_scaler) - bytes_len;
        bits += pad_c*8;
    }

    return bits;