static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,

                                                uint8_t *dest_y,

                                                uint8_t *dest_cb,

                                                uint8_t *dest_cr,

                                                int field_based,

                                                int bottom_field,

                                                int field_select,

                                                uint8_t **ref_picture,

                                                h264_chroma_mc_func *pix_op,

                                                int motion_x, int motion_y,

                                                int h, int mb_y)

{

    uint8_t *ptr_y, *ptr_cb, *ptr_cr;

    int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,

        uvsx, uvsy;

    const int lowres     = s->avctx->lowres;

    const int op_index   = FFMIN(lowres-1+s->chroma_x_shift, 2);

    const int block_s    = 8>>lowres;

    const int s_mask     = (2 << lowres) - 1;

    const int h_edge_pos = s->h_edge_pos >> lowres;

    const int v_edge_pos = s->v_edge_pos >> lowres;

    linesize   = s->current_picture.f.linesize[0] << field_based;

    uvlinesize = s->current_picture.f.linesize[1] << field_based;



    // FIXME obviously not perfect but qpel will not work in lowres anyway

    if (s->quarter_sample) {

        motion_x /= 2;

        motion_y /= 2;

    }



    if(field_based){

        motion_y += (bottom_field - field_select)*((1 << lowres)-1);

    }



    sx = motion_x & s_mask;

    sy = motion_y & s_mask;

    src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);

    src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);



    if (s->out_format == FMT_H263) {

        uvsx    = ((motion_x >> 1) & s_mask) | (sx & 1);

        uvsy    = ((motion_y >> 1) & s_mask) | (sy & 1);

        uvsrc_x = src_x >> 1;

        uvsrc_y = src_y >> 1;

    } else if (s->out_format == FMT_H261) {

        // even chroma mv's are full pel in H261

        mx      = motion_x / 4;

        my      = motion_y / 4;

        uvsx    = (2 * mx) & s_mask;

        uvsy    = (2 * my) & s_mask;

        uvsrc_x = s->mb_x * block_s + (mx >> lowres);

        uvsrc_y =    mb_y * block_s + (my >> lowres);

    } else {

        if(s->chroma_y_shift){

            mx      = motion_x / 2;

            my      = motion_y / 2;

            uvsx    = mx & s_mask;

            uvsy    = my & s_mask;

            uvsrc_x = s->mb_x * block_s                 + (mx >> lowres + 1);

            uvsrc_y =   (mb_y * block_s >> field_based) + (my >> lowres + 1);

        } else {

            if(s->chroma_x_shift){

            //Chroma422

                mx = motion_x / 2;

                uvsx = mx & s_mask;

                uvsy = motion_y & s_mask;

                uvsrc_y = src_y;

                uvsrc_x = s->mb_x*block_s               + (mx >> (lowres+1));

            } else {

            //Chroma444

                uvsx = motion_x & s_mask;

                uvsy = motion_y & s_mask;

                uvsrc_x = src_x;

                uvsrc_y = src_y;

            }

        }

    }



    ptr_y  = ref_picture[0] + src_y   * linesize   + src_x;

    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;

    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;



    if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s,       0) || uvsrc_y<0 ||

        (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {

        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,

                                linesize >> field_based, 17, 17 + field_based,

                                src_x, src_y << field_based, h_edge_pos,

                                v_edge_pos);

        ptr_y = s->edge_emu_buffer;

        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {

            uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;

            s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,

                                    9 + field_based,

                                    uvsrc_x, uvsrc_y << field_based,

                                    h_edge_pos >> 1, v_edge_pos >> 1);

            s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,

                                    9 + field_based,

                                    uvsrc_x, uvsrc_y << field_based,

                                    h_edge_pos >> 1, v_edge_pos >> 1);

            ptr_cb = uvbuf;

            ptr_cr = uvbuf + 16;

        }

    }



    // FIXME use this for field pix too instead of the obnoxious hack which changes picture.f.data

    if (bottom_field) {

        dest_y  += s->linesize;

        dest_cb += s->uvlinesize;

        dest_cr += s->uvlinesize;

    }



    if (field_select) {

        ptr_y   += s->linesize;

        ptr_cb  += s->uvlinesize;

        ptr_cr  += s->uvlinesize;

    }



    sx = (sx << 2) >> lowres;

    sy = (sy << 2) >> lowres;

    pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);



    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {

        int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;

        uvsx = (uvsx << 2) >> lowres;

        uvsy = (uvsy << 2) >> lowres;

        if (hc) {

            pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);

            pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);

        }

    }

    // FIXME h261 lowres loop filter

}

static av_always_inline void mpeg_motion_lowres(MpegEncContext *s,

                                                uint8_t *dest_y,

                                                uint8_t *dest_cb,

                                                uint8_t *dest_cr,

                                                int field_based,

                                                int bottom_field,

                                                int field_select,

                                                uint8_t **ref_picture,

                                                h264_chroma_mc_func *pix_op,

                                                int motion_x, int motion_y,

                                                int h, int mb_y)

{

    uint8_t *ptr_y, *ptr_cb, *ptr_cr;

    int mx, my, src_x, src_y, uvsrc_x, uvsrc_y, uvlinesize, linesize, sx, sy,

        uvsx, uvsy;

    const int lowres     = s->avctx->lowres;

    const int op_index   = FFMIN(lowres-1+s->chroma_x_shift, 2);

    const int block_s    = 8>>lowres;

    const int s_mask     = (2 << lowres) - 1;

    const int h_edge_pos = s->h_edge_pos >> lowres;

    const int v_edge_pos = s->v_edge_pos >> lowres;

    linesize   = s->current_picture.f.linesize[0] << field_based;

    uvlinesize = s->current_picture.f.linesize[1] << field_based;



    

    if (s->quarter_sample) {

        motion_x /= 2;

        motion_y /= 2;

    }



    if(field_based){

        motion_y += (bottom_field - field_select)*((1 << lowres)-1);

    }



    sx = motion_x & s_mask;

    sy = motion_y & s_mask;

    src_x = s->mb_x * 2 * block_s + (motion_x >> lowres + 1);

    src_y = (mb_y * 2 * block_s >> field_based) + (motion_y >> lowres + 1);



    if (s->out_format == FMT_H263) {

        uvsx    = ((motion_x >> 1) & s_mask) | (sx & 1);

        uvsy    = ((motion_y >> 1) & s_mask) | (sy & 1);

        uvsrc_x = src_x >> 1;

        uvsrc_y = src_y >> 1;

    } else if (s->out_format == FMT_H261) {

        

        mx      = motion_x / 4;

        my      = motion_y / 4;

        uvsx    = (2 * mx) & s_mask;

        uvsy    = (2 * my) & s_mask;

        uvsrc_x = s->mb_x * block_s + (mx >> lowres);

        uvsrc_y =    mb_y * block_s + (my >> lowres);

    } else {

        if(s->chroma_y_shift){

            mx      = motion_x / 2;

            my      = motion_y / 2;

            uvsx    = mx & s_mask;

            uvsy    = my & s_mask;

            uvsrc_x = s->mb_x * block_s                 + (mx >> lowres + 1);

            uvsrc_y =   (mb_y * block_s >> field_based) + (my >> lowres + 1);

        } else {

            if(s->chroma_x_shift){

            

                mx = motion_x / 2;

                uvsx = mx & s_mask;

                uvsy = motion_y & s_mask;

                uvsrc_y = src_y;

                uvsrc_x = s->mb_x*block_s               + (mx >> (lowres+1));

            } else {

            

                uvsx = motion_x & s_mask;

                uvsy = motion_y & s_mask;

                uvsrc_x = src_x;

                uvsrc_y = src_y;

            }

        }

    }



    ptr_y  = ref_picture[0] + src_y   * linesize   + src_x;

    ptr_cb = ref_picture[1] + uvsrc_y * uvlinesize + uvsrc_x;

    ptr_cr = ref_picture[2] + uvsrc_y * uvlinesize + uvsrc_x;



    if ((unsigned) src_x > FFMAX( h_edge_pos - (!!sx) - 2 * block_s,       0) || uvsrc_y<0 ||

        (unsigned) src_y > FFMAX((v_edge_pos >> field_based) - (!!sy) - h, 0)) {

        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,

                                linesize >> field_based, 17, 17 + field_based,

                                src_x, src_y << field_based, h_edge_pos,

                                v_edge_pos);

        ptr_y = s->edge_emu_buffer;

        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {

            uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->linesize;

            s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, uvlinesize >> field_based, 9,

                                    9 + field_based,

                                    uvsrc_x, uvsrc_y << field_based,

                                    h_edge_pos >> 1, v_edge_pos >> 1);

            s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, uvlinesize >> field_based, 9,

                                    9 + field_based,

                                    uvsrc_x, uvsrc_y << field_based,

                                    h_edge_pos >> 1, v_edge_pos >> 1);

            ptr_cb = uvbuf;

            ptr_cr = uvbuf + 16;

        }

    }



    

    if (bottom_field) {

        dest_y  += s->linesize;

        dest_cb += s->uvlinesize;

        dest_cr += s->uvlinesize;

    }



    if (field_select) {

        ptr_y   += s->linesize;

        ptr_cb  += s->uvlinesize;

        ptr_cr  += s->uvlinesize;

    }



    sx = (sx << 2) >> lowres;

    sy = (sy << 2) >> lowres;

    pix_op[lowres - 1](dest_y, ptr_y, linesize, h, sx, sy);



    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {

        int hc = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;

        uvsx = (uvsx << 2) >> lowres;

        uvsy = (uvsy << 2) >> lowres;

        if (hc) {

            pix_op[op_index](dest_cb, ptr_cb, uvlinesize, hc, uvsx, uvsy);

            pix_op[op_index](dest_cr, ptr_cr, uvlinesize, hc, uvsx, uvsy);

        }

    }

    

}

static av_always_inline void FUNC_0(MpegEncContext *s,

                                                uint8_t *dest_y,

                                                uint8_t *dest_cb,

                                                uint8_t *dest_cr,

                                                int field_based,

                                                int bottom_field,

                                                int field_select,

                                                uint8_t **ref_picture,

                                                h264_chroma_mc_func *pix_op,

                                                int motion_x, int motion_y,

                                                int h, int mb_y)

{

    uint8_t *ptr_y, *ptr_cb, *ptr_cr;

    int VAR_0, VAR_1, VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9,

        VAR_10, VAR_11;

    const int VAR_12     = s->avctx->VAR_12;

    const int VAR_13   = FFMIN(VAR_12-1+s->chroma_x_shift, 2);

    const int VAR_14    = 8>>VAR_12;

    const int VAR_15     = (2 << VAR_12) - 1;

    const int VAR_16 = s->VAR_16 >> VAR_12;

    const int VAR_17 = s->VAR_17 >> VAR_12;

    VAR_7   = s->current_picture.f.VAR_7[0] << field_based;

    VAR_6 = s->current_picture.f.VAR_7[1] << field_based;



    

    if (s->quarter_sample) {

        motion_x /= 2;

        motion_y /= 2;

    }



    if(field_based){

        motion_y += (bottom_field - field_select)*((1 << VAR_12)-1);

    }



    VAR_8 = motion_x & VAR_15;

    VAR_9 = motion_y & VAR_15;

    VAR_2 = s->mb_x * 2 * VAR_14 + (motion_x >> VAR_12 + 1);

    VAR_3 = (mb_y * 2 * VAR_14 >> field_based) + (motion_y >> VAR_12 + 1);



    if (s->out_format == FMT_H263) {

        VAR_10    = ((motion_x >> 1) & VAR_15) | (VAR_8 & 1);

        VAR_11    = ((motion_y >> 1) & VAR_15) | (VAR_9 & 1);

        VAR_4 = VAR_2 >> 1;

        VAR_5 = VAR_3 >> 1;

    } else if (s->out_format == FMT_H261) {

        

        VAR_0      = motion_x / 4;

        VAR_1      = motion_y / 4;

        VAR_10    = (2 * VAR_0) & VAR_15;

        VAR_11    = (2 * VAR_1) & VAR_15;

        VAR_4 = s->mb_x * VAR_14 + (VAR_0 >> VAR_12);

        VAR_5 =    mb_y * VAR_14 + (VAR_1 >> VAR_12);

    } else {

        if(s->chroma_y_shift){

            VAR_0      = motion_x / 2;

            VAR_1      = motion_y / 2;

            VAR_10    = VAR_0 & VAR_15;

            VAR_11    = VAR_1 & VAR_15;

            VAR_4 = s->mb_x * VAR_14                 + (VAR_0 >> VAR_12 + 1);

            VAR_5 =   (mb_y * VAR_14 >> field_based) + (VAR_1 >> VAR_12 + 1);

        } else {

            if(s->chroma_x_shift){

            

                VAR_0 = motion_x / 2;

                VAR_10 = VAR_0 & VAR_15;

                VAR_11 = motion_y & VAR_15;

                VAR_5 = VAR_3;

                VAR_4 = s->mb_x*VAR_14               + (VAR_0 >> (VAR_12+1));

            } else {

            

                VAR_10 = motion_x & VAR_15;

                VAR_11 = motion_y & VAR_15;

                VAR_4 = VAR_2;

                VAR_5 = VAR_3;

            }

        }

    }



    ptr_y  = ref_picture[0] + VAR_3   * VAR_7   + VAR_2;

    ptr_cb = ref_picture[1] + VAR_5 * VAR_6 + VAR_4;

    ptr_cr = ref_picture[2] + VAR_5 * VAR_6 + VAR_4;



    if ((unsigned) VAR_2 > FFMAX( VAR_16 - (!!VAR_8) - 2 * VAR_14,       0) || VAR_5<0 ||

        (unsigned) VAR_3 > FFMAX((VAR_17 >> field_based) - (!!VAR_9) - h, 0)) {

        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,

                                VAR_7 >> field_based, 17, 17 + field_based,

                                VAR_2, VAR_3 << field_based, VAR_16,

                                VAR_17);

        ptr_y = s->edge_emu_buffer;

        if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {

            uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->VAR_7;

            s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, VAR_6 >> field_based, 9,

                                    9 + field_based,

                                    VAR_4, VAR_5 << field_based,

                                    VAR_16 >> 1, VAR_17 >> 1);

            s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, VAR_6 >> field_based, 9,

                                    9 + field_based,

                                    VAR_4, VAR_5 << field_based,

                                    VAR_16 >> 1, VAR_17 >> 1);

            ptr_cb = uvbuf;

            ptr_cr = uvbuf + 16;

        }

    }



    

    if (bottom_field) {

        dest_y  += s->VAR_7;

        dest_cb += s->VAR_6;

        dest_cr += s->VAR_6;

    }



    if (field_select) {

        ptr_y   += s->VAR_7;

        ptr_cb  += s->VAR_6;

        ptr_cr  += s->VAR_6;

    }



    VAR_8 = (VAR_8 << 2) >> VAR_12;

    VAR_9 = (VAR_9 << 2) >> VAR_12;

    pix_op[VAR_12 - 1](dest_y, ptr_y, VAR_7, h, VAR_8, VAR_9);



    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {

        int VAR_18 = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h;

        VAR_10 = (VAR_10 << 2) >> VAR_12;

        VAR_11 = (VAR_11 << 2) >> VAR_12;

        if (VAR_18) {

            pix_op[VAR_13](dest_cb, ptr_cb, VAR_6, VAR_18, VAR_10, VAR_11);

            pix_op[VAR_13](dest_cr, ptr_cr, VAR_6, VAR_18, VAR_10, VAR_11);

        }

    }

    

}

static av_always_inline void FUNC_0(MpegEncContext *s,
uint8_t *dest_y,
uint8_t *dest_cb,
uint8_t *dest_cr,
int field_based,
int bottom_field,
int field_select,
uint8_t **ref_picture,
h264_chroma_mc_func *pix_op,
int motion_x, int motion_y,
int h, int mb_y)
{ uint8_t *ptr_y, *ptr_cb, *ptr_cr; int VAR_0, VAR_1, VAR_2, VAR_3, VAR_4, VAR_5, VAR_6, VAR_7, VAR_8, VAR_9,
VAR_10, VAR_11; const int VAR_12     = s->avctx->VAR_12; const int VAR_13   = FFMIN(VAR_12-1+s->chroma_x_shift, 2); const int VAR_14    = 8>>VAR_12; const int VAR_15     = (2 << VAR_12) - 1; const int VAR_16 = s->VAR_16 >> VAR_12; const int VAR_17 = s->VAR_17 >> VAR_12; VAR_7   = s->current_picture.f.VAR_7[0] << field_based; VAR_6 = s->current_picture.f.VAR_7[1] << field_based; if (s->quarter_sample) { motion_x /= 2; motion_y /= 2; } if(field_based){ motion_y += (bottom_field - field_select)*((1 << VAR_12)-1); } VAR_8 = motion_x & VAR_15; VAR_9 = motion_y & VAR_15; VAR_2 = s->mb_x * 2 * VAR_14 + (motion_x >> VAR_12 + 1); VAR_3 = (mb_y * 2 * VAR_14 >> field_based) + (motion_y >> VAR_12 + 1); if (s->out_format == FMT_H263) { VAR_10    = ((motion_x >> 1) & VAR_15) | (VAR_8 & 1); VAR_11    = ((motion_y >> 1) & VAR_15) | (VAR_9 & 1); VAR_4 = VAR_2 >> 1; VAR_5 = VAR_3 >> 1; } else if (s->out_format == FMT_H261) { VAR_0      = motion_x / 4; VAR_1      = motion_y / 4; VAR_10    = (2 * VAR_0) & VAR_15; VAR_11    = (2 * VAR_1) & VAR_15; VAR_4 = s->mb_x * VAR_14 + (VAR_0 >> VAR_12); VAR_5 =    mb_y * VAR_14 + (VAR_1 >> VAR_12); } else { if(s->chroma_y_shift){ VAR_0      = motion_x / 2; VAR_1      = motion_y / 2; VAR_10    = VAR_0 & VAR_15; VAR_11    = VAR_1 & VAR_15; VAR_4 = s->mb_x * VAR_14                 + (VAR_0 >> VAR_12 + 1); VAR_5 =   (mb_y * VAR_14 >> field_based) + (VAR_1 >> VAR_12 + 1); } else { if(s->chroma_x_shift){ VAR_0 = motion_x / 2; VAR_10 = VAR_0 & VAR_15; VAR_11 = motion_y & VAR_15; VAR_5 = VAR_3; VAR_4 = s->mb_x*VAR_14               + (VAR_0 >> (VAR_12+1)); } else { VAR_10 = motion_x & VAR_15; VAR_11 = motion_y & VAR_15; VAR_4 = VAR_2; VAR_5 = VAR_3; } } } ptr_y  = ref_picture[0] + VAR_3   * VAR_7   + VAR_2; ptr_cb = ref_picture[1] + VAR_5 * VAR_6 + VAR_4; ptr_cr = ref_picture[2] + VAR_5 * VAR_6 + VAR_4; if ((unsigned) VAR_2 > FFMAX( VAR_16 - (!!VAR_8) - 2 * VAR_14,       0) || VAR_5<0 ||
(unsigned) VAR_3 > FFMAX((VAR_17 >> field_based) - (!!VAR_9) - h, 0)) { s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
VAR_7 >> field_based, 17, 17 + field_based,
VAR_2, VAR_3 << field_based, VAR_16,
VAR_17); ptr_y = s->edge_emu_buffer; if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) { uint8_t *uvbuf = s->edge_emu_buffer + 18 * s->VAR_7; s->vdsp.emulated_edge_mc(uvbuf , ptr_cb, VAR_6 >> field_based, 9,
9 + field_based,
VAR_4, VAR_5 << field_based,
VAR_16 >> 1, VAR_17 >> 1); s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr, VAR_6 >> field_based, 9,
9 + field_based,
VAR_4, VAR_5 << field_based,
VAR_16 >> 1, VAR_17 >> 1); ptr_cb = uvbuf; ptr_cr = uvbuf + 16; } } if (bottom_field) { dest_y  += s->VAR_7; dest_cb += s->VAR_6; dest_cr += s->VAR_6; } if (field_select) { ptr_y   += s->VAR_7; ptr_cb  += s->VAR_6; ptr_cr  += s->VAR_6; } VAR_8 = (VAR_8 << 2) >> VAR_12; VAR_9 = (VAR_9 << 2) >> VAR_12; pix_op[VAR_12 - 1](dest_y, ptr_y, VAR_7, h, VAR_8, VAR_9); if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) { int VAR_18 = s->chroma_y_shift ? (h+1-bottom_field)>>1 : h; VAR_10 = (VAR_10 << 2) >> VAR_12; VAR_11 = (VAR_11 << 2) >> VAR_12; if (VAR_18) { pix_op[VAR_13](dest_cb, ptr_cb, VAR_6, VAR_18, VAR_10, VAR_11); pix_op[VAR_13](dest_cr, ptr_cr, VAR_6, VAR_18, VAR_10, VAR_11); } } }