static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){
    int s, i, j;
    const int dec_count= w==8 ? 3 : 4;
    int tmp[32*32];
    int level, ori;
    static const int scale[2][2][4][4]={
      {
        {
            {268, 239, 239, 213},
            {  0, 224, 224, 152},
            {  0, 135, 135, 110},
        },{
            {344, 310, 310, 280},
            {  0, 320, 320, 228},
            {  0, 175, 175, 136},
            {  0, 129, 129, 102},
        }
      },{
        {
            {275, 245, 245, 218},
            {  0, 230, 230, 156},
            {  0, 138, 138, 113},
        },{
            {352, 317, 317, 286},
            {  0, 328, 328, 233},
            {  0, 180, 180, 140},
            {  0, 132, 132, 105},
        }
      }
    };
    for (i = 0; i < h; i++) {
        for (j = 0; j < w; j+=4) {
            tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;
            tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;
            tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;
            tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;
        }
        pix1 += line_size;
        pix2 += line_size;
    }
    ff_spatial_dwt(tmp, w, h, 32, type, dec_count);
    s=0;
    assert(w==h);
    for(level=0; level<dec_count; level++){
        for(ori= level ? 1 : 0; ori<4; ori++){
            int size= w>>(dec_count-level);
            int sx= (ori&1) ? size : 0;
            int stride= 32<<(dec_count-level);
            int sy= (ori&2) ? stride>>1 : 0;
            for(i=0; i<size; i++){
                for(j=0; j<size; j++){
                    int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];
                    s += FFABS(v);
                }
            }
        }
    }
    assert(s>=0);
    return s>>9;
}
['static int get_block_rd(SnowContext *s, int mb_x, int mb_y, int plane_index, const uint8_t *obmc_edged){\n    Plane *p= &s->plane[plane_index];\n    const int block_size = MB_SIZE >> s->block_max_depth;\n    const int block_w    = plane_index ? block_size/2 : block_size;\n    const int obmc_stride= plane_index ? block_size : 2*block_size;\n    const int ref_stride= s->current_picture.linesize[plane_index];\n    uint8_t *dst= s->current_picture.data[plane_index];\n    uint8_t *src= s->  input_picture.data[plane_index];\n    IDWTELEM *pred= (IDWTELEM*)s->m.obmc_scratchpad + plane_index*block_size*block_size*4;\n    uint8_t cur[ref_stride*2*MB_SIZE];\n    uint8_t tmp[ref_stride*(2*MB_SIZE+HTAPS_MAX-1)];\n    const int b_stride = s->b_width << s->block_max_depth;\n    const int b_height = s->b_height<< s->block_max_depth;\n    const int w= p->width;\n    const int h= p->height;\n    int distortion;\n    int rate= 0;\n    const int penalty_factor= get_penalty_factor(s->lambda, s->lambda2, s->avctx->me_cmp);\n    int sx= block_w*mb_x - block_w/2;\n    int sy= block_w*mb_y - block_w/2;\n    int x0= FFMAX(0,-sx);\n    int y0= FFMAX(0,-sy);\n    int x1= FFMIN(block_w*2, w-sx);\n    int y1= FFMIN(block_w*2, h-sy);\n    int i,x,y;\n    pred_block(s, cur, tmp, ref_stride, sx, sy, block_w*2, block_w*2, &s->block[mb_x + mb_y*b_stride], plane_index, w, h);\n    for(y=y0; y<y1; y++){\n        const uint8_t *obmc1= obmc_edged + y*obmc_stride;\n        const IDWTELEM *pred1 = pred + y*obmc_stride;\n        uint8_t *cur1 = cur + y*ref_stride;\n        uint8_t *dst1 = dst + sx + (sy+y)*ref_stride;\n        for(x=x0; x<x1; x++){\n#if FRAC_BITS >= LOG2_OBMC_MAX\n            int v = (cur1[x] * obmc1[x]) << (FRAC_BITS - LOG2_OBMC_MAX);\n#else\n            int v = (cur1[x] * obmc1[x] + (1<<(LOG2_OBMC_MAX - FRAC_BITS-1))) >> (LOG2_OBMC_MAX - FRAC_BITS);\n#endif\n            v = (v + pred1[x]) >> FRAC_BITS;\n            if(v&(~255)) v= ~(v>>31);\n            dst1[x] = v;\n        }\n    }\n    if(LOG2_OBMC_MAX == 8\n        && (mb_x == 0 || mb_x == b_stride-1)\n        && (mb_y == 0 || mb_y == b_height-1)){\n        if(mb_x == 0)\n            x1 = block_w;\n        else\n            x0 = block_w;\n        if(mb_y == 0)\n            y1 = block_w;\n        else\n            y0 = block_w;\n        for(y=y0; y<y1; y++)\n            memcpy(dst + sx+x0 + (sy+y)*ref_stride, cur + x0 + y*ref_stride, x1-x0);\n    }\n    if(block_w==16){\n        if(s->avctx->me_cmp == FF_CMP_W97)\n            distortion = w97_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);\n        else if(s->avctx->me_cmp == FF_CMP_W53)\n            distortion = w53_32_c(&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, 32);\n        else{\n            distortion = 0;\n            for(i=0; i<4; i++){\n                int off = sx+16*(i&1) + (sy+16*(i>>1))*ref_stride;\n                distortion += s->dsp.me_cmp[0](&s->m, src + off, dst + off, ref_stride, 16);\n            }\n        }\n    }else{\n        assert(block_w==8);\n        distortion = s->dsp.me_cmp[0](&s->m, src + sx + sy*ref_stride, dst + sx + sy*ref_stride, ref_stride, block_w*2);\n    }\n    if(plane_index==0){\n        for(i=0; i<4; i++){\n            rate += get_block_bits(s, mb_x + (i&1) - (i>>1), mb_y + (i>>1), 1);\n        }\n        if(mb_x == b_stride-2)\n            rate += get_block_bits(s, mb_x + 1, mb_y + 1, 1);\n    }\n    return distortion + rate*penalty_factor;\n}', 'int w97_32_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int h){\n    return w_c(v, pix1, pix2, line_size, 32, h, 0);\n}', 'static inline int w_c(void *v, uint8_t * pix1, uint8_t * pix2, int line_size, int w, int h, int type){\n    int s, i, j;\n    const int dec_count= w==8 ? 3 : 4;\n    int tmp[32*32];\n    int level, ori;\n    static const int scale[2][2][4][4]={\n      {\n        {\n            {268, 239, 239, 213},\n            {  0, 224, 224, 152},\n            {  0, 135, 135, 110},\n        },{\n            {344, 310, 310, 280},\n            {  0, 320, 320, 228},\n            {  0, 175, 175, 136},\n            {  0, 129, 129, 102},\n        }\n      },{\n        {\n            {275, 245, 245, 218},\n            {  0, 230, 230, 156},\n            {  0, 138, 138, 113},\n        },{\n            {352, 317, 317, 286},\n            {  0, 328, 328, 233},\n            {  0, 180, 180, 140},\n            {  0, 132, 132, 105},\n        }\n      }\n    };\n    for (i = 0; i < h; i++) {\n        for (j = 0; j < w; j+=4) {\n            tmp[32*i+j+0] = (pix1[j+0] - pix2[j+0])<<4;\n            tmp[32*i+j+1] = (pix1[j+1] - pix2[j+1])<<4;\n            tmp[32*i+j+2] = (pix1[j+2] - pix2[j+2])<<4;\n            tmp[32*i+j+3] = (pix1[j+3] - pix2[j+3])<<4;\n        }\n        pix1 += line_size;\n        pix2 += line_size;\n    }\n    ff_spatial_dwt(tmp, w, h, 32, type, dec_count);\n    s=0;\n    assert(w==h);\n    for(level=0; level<dec_count; level++){\n        for(ori= level ? 1 : 0; ori<4; ori++){\n            int size= w>>(dec_count-level);\n            int sx= (ori&1) ? size : 0;\n            int stride= 32<<(dec_count-level);\n            int sy= (ori&2) ? stride>>1 : 0;\n            for(i=0; i<size; i++){\n                for(j=0; j<size; j++){\n                    int v= tmp[sx + sy + i*stride + j] * scale[type][dec_count-3][level][ori];\n                    s += FFABS(v);\n                }\n            }\n        }\n    }\n    assert(s>=0);\n    return s>>9;\n}']