static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
                                  int coded, int codingset, int mquant)
{
    GetBitContext *gb = &v->s.gb;
    MpegEncContext *s = &v->s;
    int dc_pred_dir = 0;
    int i;
    int16_t *dc_val;
    int16_t *ac_val, *ac_val2;
    int dcdiff;
    int a_avail = v->a_avail, c_avail = v->c_avail;
    int use_pred = s->ac_pred;
    int scale;
    int q1, q2 = 0;
    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
    if (n < 4) {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    } else {
        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
    }
    if (dcdiff < 0) {
        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
        return -1;
    }
    if (dcdiff) {
        if (dcdiff == 119  ) {
            if (mquant == 1)      dcdiff = get_bits(gb, 10);
            else if (mquant == 2) dcdiff = get_bits(gb, 9);
            else                  dcdiff = get_bits(gb, 8);
        } else {
            if (mquant == 1)
                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
            else if (mquant == 2)
                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
        }
        if (get_bits1(gb))
            dcdiff = -dcdiff;
    }
    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
    *dc_val = dcdiff;
    if (n < 4) {
        block[0] = dcdiff * s->y_dc_scale;
    } else {
        block[0] = dcdiff * s->c_dc_scale;
    }
    i = 1;
    if (!a_avail && !c_avail)
        use_pred = 0;
    ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
    ac_val2 = ac_val;
    scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
    if (dc_pred_dir)
        ac_val -= 16;
    else
        ac_val -= 16 * s->block_wrap[n];
    q1 = s->current_picture.f.qscale_table[mb_pos];
    if ( dc_pred_dir && c_avail && mb_pos)
        q2 = s->current_picture.f.qscale_table[mb_pos - 1];
    if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
        q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
    if ( dc_pred_dir && n == 1)
        q2 = q1;
    if (!dc_pred_dir && n == 2)
        q2 = q1;
    if (n == 3)
        q2 = q1;
    if (coded) {
        int last = 0, skip, value;
        const uint8_t *zz_table;
        int k;
        if (v->s.ac_pred) {
            if (!use_pred && v->fcm == ILACE_FRAME) {
                zz_table = v->zzi_8x8;
            } else {
                if (!dc_pred_dir)
                    zz_table = v->zz_8x8[2];
                else
                    zz_table = v->zz_8x8[3];
            }
        } else {
            if (v->fcm != ILACE_FRAME)
                zz_table = v->zz_8x8[1];
            else
                zz_table = v->zzi_8x8;
        }
        while (!last) {
            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
            i += skip;
            if (i > 63)
                break;
            block[zz_table[i++]] = value;
        }
        if (use_pred) {
            if (q2 && q1 != q2) {
                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
                if (dc_pred_dir) {
                    for (k = 1; k < 8; k++)
                        block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                } else {
                    for (k = 1; k < 8; k++)
                        block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            } else {
                if (dc_pred_dir) {
                    for (k = 1; k < 8; k++)
                        block[k << v->left_blk_sh] += ac_val[k];
                } else {
                    for (k = 1; k < 8; k++)
                        block[k << v->top_blk_sh] += ac_val[k + 8];
                }
            }
        }
        for (k = 1; k < 8; k++) {
            ac_val2[k    ] = block[k << v->left_blk_sh];
            ac_val2[k + 8] = block[k << v->top_blk_sh];
        }
        for (k = 1; k < 64; k++)
            if (block[k]) {
                block[k] *= scale;
                if (!v->pquantizer)
                    block[k] += (block[k] < 0) ? -mquant : mquant;
            }
        if (use_pred) i = 63;
    } else {
        int k;
        memset(ac_val2, 0, 16 * 2);
        if (dc_pred_dir) {
            if (use_pred) {
                memcpy(ac_val2, ac_val, 8 * 2);
                if (q2 && q1 != q2) {
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
                    for (k = 1; k < 8; k++)
                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            }
        } else {
            if (use_pred) {
                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
                if (q2 && q1 != q2) {
                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
                    for (k = 1; k < 8; k++)
                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
                }
            }
        }
        if (use_pred) {
            if (dc_pred_dir) {
                for (k = 1; k < 8; k++) {
                    block[k << v->left_blk_sh] = ac_val2[k] * scale;
                    if (!v->pquantizer && block[k << v->left_blk_sh])
                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
                }
            } else {
                for (k = 1; k < 8; k++) {
                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
                    if (!v->pquantizer && block[k << v->top_blk_sh])
                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
                }
            }
            i = 63;
        }
    }
    s->block_last_index[n] = i;
    return 0;
}
['static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,\n                                  int coded, int codingset, int mquant)\n{\n    GetBitContext *gb = &v->s.gb;\n    MpegEncContext *s = &v->s;\n    int dc_pred_dir = 0;\n    int i;\n    int16_t *dc_val;\n    int16_t *ac_val, *ac_val2;\n    int dcdiff;\n    int a_avail = v->a_avail, c_avail = v->c_avail;\n    int use_pred = s->ac_pred;\n    int scale;\n    int q1, q2 = 0;\n    int mb_pos = s->mb_x + s->mb_y * s->mb_stride;\n    if (n < 4) {\n        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);\n    } else {\n        dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);\n    }\n    if (dcdiff < 0) {\n        av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\\n");\n        return -1;\n    }\n    if (dcdiff) {\n        if (dcdiff == 119  ) {\n            if (mquant == 1)      dcdiff = get_bits(gb, 10);\n            else if (mquant == 2) dcdiff = get_bits(gb, 9);\n            else                  dcdiff = get_bits(gb, 8);\n        } else {\n            if (mquant == 1)\n                dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;\n            else if (mquant == 2)\n                dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;\n        }\n        if (get_bits1(gb))\n            dcdiff = -dcdiff;\n    }\n    dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);\n    *dc_val = dcdiff;\n    if (n < 4) {\n        block[0] = dcdiff * s->y_dc_scale;\n    } else {\n        block[0] = dcdiff * s->c_dc_scale;\n    }\n    i = 1;\n    if (!a_avail && !c_avail)\n        use_pred = 0;\n    ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;\n    ac_val2 = ac_val;\n    scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);\n    if (dc_pred_dir)\n        ac_val -= 16;\n    else\n        ac_val -= 16 * s->block_wrap[n];\n    q1 = s->current_picture.f.qscale_table[mb_pos];\n    if ( dc_pred_dir && c_avail && mb_pos)\n        q2 = s->current_picture.f.qscale_table[mb_pos - 1];\n    if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)\n        q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];\n    if ( dc_pred_dir && n == 1)\n        q2 = q1;\n    if (!dc_pred_dir && n == 2)\n        q2 = q1;\n    if (n == 3)\n        q2 = q1;\n    if (coded) {\n        int last = 0, skip, value;\n        const uint8_t *zz_table;\n        int k;\n        if (v->s.ac_pred) {\n            if (!use_pred && v->fcm == ILACE_FRAME) {\n                zz_table = v->zzi_8x8;\n            } else {\n                if (!dc_pred_dir)\n                    zz_table = v->zz_8x8[2];\n                else\n                    zz_table = v->zz_8x8[3];\n            }\n        } else {\n            if (v->fcm != ILACE_FRAME)\n                zz_table = v->zz_8x8[1];\n            else\n                zz_table = v->zzi_8x8;\n        }\n        while (!last) {\n            vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);\n            i += skip;\n            if (i > 63)\n                break;\n            block[zz_table[i++]] = value;\n        }\n        if (use_pred) {\n            if (q2 && q1 != q2) {\n                q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;\n                q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;\n                if (dc_pred_dir) {\n                    for (k = 1; k < 8; k++)\n                        block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;\n                } else {\n                    for (k = 1; k < 8; k++)\n                        block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;\n                }\n            } else {\n                if (dc_pred_dir) {\n                    for (k = 1; k < 8; k++)\n                        block[k << v->left_blk_sh] += ac_val[k];\n                } else {\n                    for (k = 1; k < 8; k++)\n                        block[k << v->top_blk_sh] += ac_val[k + 8];\n                }\n            }\n        }\n        for (k = 1; k < 8; k++) {\n            ac_val2[k    ] = block[k << v->left_blk_sh];\n            ac_val2[k + 8] = block[k << v->top_blk_sh];\n        }\n        for (k = 1; k < 64; k++)\n            if (block[k]) {\n                block[k] *= scale;\n                if (!v->pquantizer)\n                    block[k] += (block[k] < 0) ? -mquant : mquant;\n            }\n        if (use_pred) i = 63;\n    } else {\n        int k;\n        memset(ac_val2, 0, 16 * 2);\n        if (dc_pred_dir) {\n            if (use_pred) {\n                memcpy(ac_val2, ac_val, 8 * 2);\n                if (q2 && q1 != q2) {\n                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;\n                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;\n                    for (k = 1; k < 8; k++)\n                        ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;\n                }\n            }\n        } else {\n            if (use_pred) {\n                memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);\n                if (q2 && q1 != q2) {\n                    q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;\n                    q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;\n                    for (k = 1; k < 8; k++)\n                        ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;\n                }\n            }\n        }\n        if (use_pred) {\n            if (dc_pred_dir) {\n                for (k = 1; k < 8; k++) {\n                    block[k << v->left_blk_sh] = ac_val2[k] * scale;\n                    if (!v->pquantizer && block[k << v->left_blk_sh])\n                        block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;\n                }\n            } else {\n                for (k = 1; k < 8; k++) {\n                    block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;\n                    if (!v->pquantizer && block[k << v->top_blk_sh])\n                        block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;\n                }\n            }\n            i = 63;\n        }\n    }\n    s->block_last_index[n] = i;\n    return 0;\n}', 'static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,\n                                int *value, int codingset)\n{\n    GetBitContext *gb = &v->s.gb;\n    int index, escape, run = 0, level = 0, lst = 0;\n    index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);\n    if (index != vc1_ac_sizes[codingset] - 1) {\n        run   = vc1_index_decode_table[codingset][index][0];\n        level = vc1_index_decode_table[codingset][index][1];\n        lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;\n        if (get_bits1(gb))\n            level = -level;\n    } else {\n        escape = decode210(gb);\n        if (escape != 2) {\n            index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);\n            run   = vc1_index_decode_table[codingset][index][0];\n            level = vc1_index_decode_table[codingset][index][1];\n            lst   = index >= vc1_last_decode_table[codingset];\n            if (escape == 0) {\n                if (lst)\n                    level += vc1_last_delta_level_table[codingset][run];\n                else\n                    level += vc1_delta_level_table[codingset][run];\n            } else {\n                if (lst)\n                    run += vc1_last_delta_run_table[codingset][level] + 1;\n                else\n                    run += vc1_delta_run_table[codingset][level] + 1;\n            }\n            if (get_bits1(gb))\n                level = -level;\n        } else {\n            int sign;\n            lst = get_bits1(gb);\n            if (v->s.esc3_level_length == 0) {\n                if (v->pq < 8 || v->dquantfrm) {\n                    v->s.esc3_level_length = get_bits(gb, 3);\n                    if (!v->s.esc3_level_length)\n                        v->s.esc3_level_length = get_bits(gb, 2) + 8;\n                } else {\n                    v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;\n                }\n                v->s.esc3_run_length = 3 + get_bits(gb, 2);\n            }\n            run   = get_bits(gb, v->s.esc3_run_length);\n            sign  = get_bits1(gb);\n            level = get_bits(gb, v->s.esc3_level_length);\n            if (sign)\n                level = -level;\n        }\n    }\n    *last  = lst;\n    *skip  = run;\n    *value = level;\n}']