]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/mpegvideo_motion.c
dnxhddata: Fix 10-bit DNxHD quant matrices
[ffmpeg] / libavcodec / mpegvideo_motion.c
index 9b704ed3a198234ff3857c85daf52efdeff28067..05375a1afa5c8540f5ad0517932196d08d113035 100644 (file)
 
 #include "libavutil/internal.h"
 #include "avcodec.h"
-#include "dsputil.h"
 #include "h261.h"
+#include "mpegutils.h"
 #include "mpegvideo.h"
 #include "mjpegenc.h"
 #include "msmpeg4.h"
+#include "qpeldsp.h"
+#include "wmv2.h"
 #include <limits.h>
 
 static void gmc1_motion(MpegEncContext *s,
@@ -59,23 +61,21 @@ static void gmc1_motion(MpegEncContext *s,
 
     ptr = ref_picture[0] + src_y * linesize + src_x;
 
-    if (s->flags & CODEC_FLAG_EMU_EDGE) {
-        if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) ||
-            (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) {
-            s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
-                                     linesize,
-                                     17, 17,
-                                     src_x, src_y,
-                                     s->h_edge_pos, s->v_edge_pos);
-            ptr = s->edge_emu_buffer;
-        }
+    if ((unsigned)src_x >= FFMAX(s->h_edge_pos - 17, 0) ||
+        (unsigned)src_y >= FFMAX(s->v_edge_pos - 17, 0)) {
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
+                                 linesize, linesize,
+                                 17, 17,
+                                 src_x, src_y,
+                                 s->h_edge_pos, s->v_edge_pos);
+        ptr = s->sc.edge_emu_buffer;
     }
 
     if ((motion_x | motion_y) & 7) {
-        s->dsp.gmc1(dest_y, ptr, linesize, 16,
-                    motion_x & 15, motion_y & 15, 128 - s->no_rounding);
-        s->dsp.gmc1(dest_y + 8, ptr + 8, linesize, 16,
-                    motion_x & 15, motion_y & 15, 128 - s->no_rounding);
+        s->mdsp.gmc1(dest_y, ptr, linesize, 16,
+                     motion_x & 15, motion_y & 15, 128 - s->no_rounding);
+        s->mdsp.gmc1(dest_y + 8, ptr + 8, linesize, 16,
+                     motion_x & 15, motion_y & 15, 128 - s->no_rounding);
     } else {
         int dxy;
 
@@ -87,7 +87,7 @@ static void gmc1_motion(MpegEncContext *s,
         }
     }
 
-    if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
+    if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
         return;
 
     motion_x   = s->sprite_offset[1][0];
@@ -105,32 +105,30 @@ static void gmc1_motion(MpegEncContext *s,
 
     offset = (src_y * uvlinesize) + src_x;
     ptr    = ref_picture[1] + offset;
-    if (s->flags & CODEC_FLAG_EMU_EDGE) {
-        if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) ||
-            (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) {
-            s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
-                                     uvlinesize,
-                                     9, 9,
-                                     src_x, src_y,
-                                     s->h_edge_pos >> 1, s->v_edge_pos >> 1);
-            ptr = s->edge_emu_buffer;
-            emu = 1;
-        }
+    if ((unsigned)src_x >= FFMAX((s->h_edge_pos >> 1) - 9, 0) ||
+        (unsigned)src_y >= FFMAX((s->v_edge_pos >> 1) - 9, 0)) {
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
+                                 uvlinesize, uvlinesize,
+                                 9, 9,
+                                 src_x, src_y,
+                                 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
+        ptr = s->sc.edge_emu_buffer;
+        emu = 1;
     }
-    s->dsp.gmc1(dest_cb, ptr, uvlinesize, 8,
-                motion_x & 15, motion_y & 15, 128 - s->no_rounding);
+    s->mdsp.gmc1(dest_cb, ptr, uvlinesize, 8,
+                 motion_x & 15, motion_y & 15, 128 - s->no_rounding);
 
     ptr = ref_picture[2] + offset;
     if (emu) {
-        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
-                                 uvlinesize,
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
+                                 uvlinesize, uvlinesize,
                                  9, 9,
                                  src_x, src_y,
                                  s->h_edge_pos >> 1, s->v_edge_pos >> 1);
-        ptr = s->edge_emu_buffer;
+        ptr = s->sc.edge_emu_buffer;
     }
-    s->dsp.gmc1(dest_cr, ptr, uvlinesize, 8,
-                motion_x & 15, motion_y & 15, 128 - s->no_rounding);
+    s->mdsp.gmc1(dest_cr, ptr, uvlinesize, 8,
+                 motion_x & 15, motion_y & 15, 128 - s->no_rounding);
 }
 
 static void gmc_motion(MpegEncContext *s,
@@ -152,21 +150,21 @@ static void gmc_motion(MpegEncContext *s,
     oy = s->sprite_offset[0][1] + s->sprite_delta[1][0] * s->mb_x * 16 +
          s->sprite_delta[1][1] * s->mb_y * 16;
 
-    s->dsp.gmc(dest_y, ptr, linesize, 16,
-               ox, oy,
-               s->sprite_delta[0][0], s->sprite_delta[0][1],
-               s->sprite_delta[1][0], s->sprite_delta[1][1],
-               a + 1, (1 << (2 * a + 1)) - s->no_rounding,
-               s->h_edge_pos, s->v_edge_pos);
-    s->dsp.gmc(dest_y + 8, ptr, linesize, 16,
-               ox + s->sprite_delta[0][0] * 8,
-               oy + s->sprite_delta[1][0] * 8,
-               s->sprite_delta[0][0], s->sprite_delta[0][1],
-               s->sprite_delta[1][0], s->sprite_delta[1][1],
-               a + 1, (1 << (2 * a + 1)) - s->no_rounding,
-               s->h_edge_pos, s->v_edge_pos);
-
-    if (CONFIG_GRAY && s->flags & CODEC_FLAG_GRAY)
+    s->mdsp.gmc(dest_y, ptr, linesize, 16,
+                ox, oy,
+                s->sprite_delta[0][0], s->sprite_delta[0][1],
+                s->sprite_delta[1][0], s->sprite_delta[1][1],
+                a + 1, (1 << (2 * a + 1)) - s->no_rounding,
+                s->h_edge_pos, s->v_edge_pos);
+    s->mdsp.gmc(dest_y + 8, ptr, linesize, 16,
+                ox + s->sprite_delta[0][0] * 8,
+                oy + s->sprite_delta[1][0] * 8,
+                s->sprite_delta[0][0], s->sprite_delta[0][1],
+                s->sprite_delta[1][0], s->sprite_delta[1][1],
+                a + 1, (1 << (2 * a + 1)) - s->no_rounding,
+                s->h_edge_pos, s->v_edge_pos);
+
+    if (CONFIG_GRAY && s->avctx->flags & AV_CODEC_FLAG_GRAY)
         return;
 
     ox = s->sprite_offset[1][0] + s->sprite_delta[0][0] * s->mb_x * 8 +
@@ -175,20 +173,20 @@ static void gmc_motion(MpegEncContext *s,
          s->sprite_delta[1][1] * s->mb_y * 8;
 
     ptr = ref_picture[1];
-    s->dsp.gmc(dest_cb, ptr, uvlinesize, 8,
-               ox, oy,
-               s->sprite_delta[0][0], s->sprite_delta[0][1],
-               s->sprite_delta[1][0], s->sprite_delta[1][1],
-               a + 1, (1 << (2 * a + 1)) - s->no_rounding,
-               s->h_edge_pos >> 1, s->v_edge_pos >> 1);
+    s->mdsp.gmc(dest_cb, ptr, uvlinesize, 8,
+                ox, oy,
+                s->sprite_delta[0][0], s->sprite_delta[0][1],
+                s->sprite_delta[1][0], s->sprite_delta[1][1],
+                a + 1, (1 << (2 * a + 1)) - s->no_rounding,
+                s->h_edge_pos >> 1, s->v_edge_pos >> 1);
 
     ptr = ref_picture[2];
-    s->dsp.gmc(dest_cr, ptr, uvlinesize, 8,
-               ox, oy,
-               s->sprite_delta[0][0], s->sprite_delta[0][1],
-               s->sprite_delta[1][0], s->sprite_delta[1][1],
-               a + 1, (1 << (2 * a + 1)) - s->no_rounding,
-               s->h_edge_pos >> 1, s->v_edge_pos >> 1);
+    s->mdsp.gmc(dest_cr, ptr, uvlinesize, 8,
+                ox, oy,
+                s->sprite_delta[0][0], s->sprite_delta[0][1],
+                s->sprite_delta[1][0], s->sprite_delta[1][1],
+                a + 1, (1 << (2 * a + 1)) - s->no_rounding,
+                s->h_edge_pos >> 1, s->v_edge_pos >> 1);
 }
 
 static inline int hpel_motion(MpegEncContext *s,
@@ -212,15 +210,15 @@ static inline int hpel_motion(MpegEncContext *s,
         dxy |= (motion_y & 1) << 1;
     src += src_y * s->linesize + src_x;
 
-    if (s->unrestricted_mv && (s->flags & CODEC_FLAG_EMU_EDGE)) {
+    if (s->unrestricted_mv) {
         if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 1) - 8, 0) ||
             (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 1) - 8, 0)) {
-            s->vdsp.emulated_edge_mc(s->edge_emu_buffer, src,
-                                     s->linesize,
+            s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, src,
+                                     s->linesize, s->linesize,
                                      9, 9,
                                      src_x, src_y, s->h_edge_pos,
                                      s->v_edge_pos);
-            src = s->edge_emu_buffer;
+            src = s->sc.edge_emu_buffer;
             emu = 1;
         }
     }
@@ -257,8 +255,8 @@ void mpeg_motion_internal(MpegEncContext *s,
 #endif
 
     v_edge_pos = s->v_edge_pos >> field_based;
-    linesize   = s->current_picture.f.linesize[0] << field_based;
-    uvlinesize = s->current_picture.f.linesize[1] << field_based;
+    linesize   = s->current_picture.f->linesize[0] << field_based;
+    uvlinesize = s->current_picture.f->linesize[1] << field_based;
 
     dxy   = ((motion_y & 1) << 1) | (motion_x & 1);
     src_x = s->mb_x * 16 + (motion_x >> 1);
@@ -320,21 +318,21 @@ void mpeg_motion_internal(MpegEncContext *s,
                    src_y);
             return;
         }
-        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
-                                 s->linesize,
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
+                                 s->linesize, s->linesize,
                                  17, 17 + field_based,
                                  src_x, src_y << field_based,
                                  s->h_edge_pos, s->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;
+        ptr_y = s->sc.edge_emu_buffer;
+        if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
+            uint8_t *uvbuf = s->sc.edge_emu_buffer + 18 * s->linesize;
             s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
-                                     s->uvlinesize,
+                                     s->uvlinesize, s->uvlinesize,
                                      9, 9 + field_based,
                                      uvsrc_x, uvsrc_y << field_based,
                                      s->h_edge_pos >> 1, s->v_edge_pos >> 1);
             s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
-                                     s->uvlinesize,
+                                     s->uvlinesize, s->uvlinesize,
                                      9, 9 + field_based,
                                      uvsrc_x, uvsrc_y << field_based,
                                      s->h_edge_pos >> 1, s->v_edge_pos >> 1);
@@ -359,7 +357,7 @@ void mpeg_motion_internal(MpegEncContext *s,
 
     pix_op[0][dxy](dest_y, ptr_y, linesize, h);
 
-    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
+    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
         pix_op[s->chroma_x_shift][uvdxy]
             (dest_cb, ptr_cb, uvlinesize, h >> s->chroma_y_shift);
         pix_op[s->chroma_x_shift][uvdxy]
@@ -408,7 +406,7 @@ static void mpeg_motion_field(MpegEncContext *s, uint8_t *dest_y,
                              motion_x, motion_y, h, 0, mb_y);
 }
 
-// FIXME move to dsputil, avg variant, 16x16 version
+// FIXME: SIMDify, avg variant, 16x16 version
 static inline void put_obmc(uint8_t *dst, uint8_t *src[5], int stride)
 {
     int x;
@@ -478,7 +476,7 @@ static inline void obmc_motion(MpegEncContext *s,
         if (i && mv[i][0] == mv[MID][0] && mv[i][1] == mv[MID][1]) {
             ptr[i] = ptr[MID];
         } else {
-            ptr[i] = s->obmc_scratchpad + 8 * (i & 1) +
+            ptr[i] = s->sc.obmc_scratchpad + 8 * (i & 1) +
                      s->linesize * 8 * (i >> 1);
             hpel_motion(s, ptr[i], src, src_x, src_y, pix_op,
                         mv[i][0], mv[i][1]);
@@ -541,21 +539,21 @@ static inline void qpel_motion(MpegEncContext *s,
 
     if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 16, 0) ||
         (unsigned)src_y > FFMAX(v_edge_pos - (motion_y & 3) - h, 0)) {
-        s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr_y,
-                                 s->linesize,
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr_y,
+                                 s->linesize, s->linesize,
                                  17, 17 + field_based,
                                  src_x, src_y << field_based,
                                  s->h_edge_pos, s->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;
+        ptr_y = s->sc.edge_emu_buffer;
+        if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
+            uint8_t *uvbuf = s->sc.edge_emu_buffer + 18 * s->linesize;
             s->vdsp.emulated_edge_mc(uvbuf, ptr_cb,
-                                     s->uvlinesize,
+                                     s->uvlinesize, s->uvlinesize,
                                      9, 9 + field_based,
                                      uvsrc_x, uvsrc_y << field_based,
                                      s->h_edge_pos >> 1, s->v_edge_pos >> 1);
             s->vdsp.emulated_edge_mc(uvbuf + 16, ptr_cr,
-                                     s->uvlinesize,
+                                     s->uvlinesize, s->uvlinesize,
                                      9, 9 + field_based,
                                      uvsrc_x, uvsrc_y << field_based,
                                      s->h_edge_pos >> 1, s->v_edge_pos >> 1);
@@ -583,7 +581,7 @@ static inline void qpel_motion(MpegEncContext *s,
         qpix_op[1][dxy](dest_y, ptr_y, linesize);
         qpix_op[1][dxy](dest_y + 8, ptr_y + 8, linesize);
     }
-    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY)) {
+    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY)) {
         pix_op[1][uvdxy](dest_cr, ptr_cr, uvlinesize, h >> 1);
         pix_op[1][uvdxy](dest_cb, ptr_cb, uvlinesize, h >> 1);
     }
@@ -622,26 +620,24 @@ static void chroma_4mv_motion(MpegEncContext *s,
 
     offset = src_y * s->uvlinesize + src_x;
     ptr    = ref_picture[1] + offset;
-    if (s->flags & CODEC_FLAG_EMU_EDGE) {
-        if ((unsigned)src_x > FFMAX((s->h_edge_pos >> 1) - (dxy & 1) - 8, 0) ||
-            (unsigned)src_y > FFMAX((s->v_edge_pos >> 1) - (dxy >> 1) - 8, 0)) {
-            s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
-                                     ptr, s->uvlinesize,
-                                     9, 9, src_x, src_y,
-                                     s->h_edge_pos >> 1, s->v_edge_pos >> 1);
-            ptr = s->edge_emu_buffer;
-            emu = 1;
-        }
+    if ((unsigned)src_x > FFMAX((s->h_edge_pos >> 1) - (dxy & 1) - 8, 0) ||
+        (unsigned)src_y > FFMAX((s->v_edge_pos >> 1) - (dxy >> 1) - 8, 0)) {
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
+                                 s->uvlinesize, s->uvlinesize,
+                                 9, 9, src_x, src_y,
+                                 s->h_edge_pos >> 1, s->v_edge_pos >> 1);
+        ptr = s->sc.edge_emu_buffer;
+        emu = 1;
     }
     pix_op[dxy](dest_cb, ptr, s->uvlinesize, 8);
 
     ptr = ref_picture[2] + offset;
     if (emu) {
-        s->vdsp.emulated_edge_mc(s->edge_emu_buffer,
-                                 ptr, s->uvlinesize,
+        s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
+                                 s->uvlinesize, s->uvlinesize,
                                  9, 9, src_x, src_y,
                                  s->h_edge_pos >> 1, s->v_edge_pos >> 1);
-        ptr = s->edge_emu_buffer;
+        ptr = s->sc.edge_emu_buffer;
     }
     pix_op[dxy](dest_cr, ptr, s->uvlinesize, 8);
 }
@@ -741,12 +737,84 @@ static inline void apply_obmc(MpegEncContext *s,
         mx += mv[0][0];
         my += mv[0][1];
     }
-    if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
+    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
         chroma_4mv_motion(s, dest_cb, dest_cr,
                           ref_picture, pix_op[1],
                           mx, my);
 }
 
+static inline void apply_8x8(MpegEncContext *s,
+                             uint8_t *dest_y,
+                             uint8_t *dest_cb,
+                             uint8_t *dest_cr,
+                             int dir,
+                             uint8_t **ref_picture,
+                             qpel_mc_func (*qpix_op)[16],
+                             op_pixels_func (*pix_op)[4])
+{
+    int dxy, mx, my, src_x, src_y;
+    int i;
+    int mb_x = s->mb_x;
+    int mb_y = s->mb_y;
+    uint8_t *ptr, *dest;
+
+    mx = 0;
+    my = 0;
+    if (s->quarter_sample) {
+        for (i = 0; i < 4; i++) {
+            int motion_x = s->mv[dir][i][0];
+            int motion_y = s->mv[dir][i][1];
+
+            dxy   = ((motion_y & 3) << 2) | (motion_x & 3);
+            src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
+            src_y = mb_y * 16 + (motion_y >> 2) + (i >> 1) * 8;
+
+            /* WARNING: do no forget half pels */
+            src_x = av_clip(src_x, -16, s->width);
+            if (src_x == s->width)
+                dxy &= ~3;
+            src_y = av_clip(src_y, -16, s->height);
+            if (src_y == s->height)
+                dxy &= ~12;
+
+            ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
+            if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 8, 0) ||
+                (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 3) - 8, 0)) {
+                s->vdsp.emulated_edge_mc(s->sc.edge_emu_buffer, ptr,
+                                         s->linesize, s->linesize,
+                                         9, 9,
+                                         src_x, src_y,
+                                         s->h_edge_pos,
+                                         s->v_edge_pos);
+                ptr = s->sc.edge_emu_buffer;
+            }
+            dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
+            qpix_op[1][dxy](dest, ptr, s->linesize);
+
+            mx += s->mv[dir][i][0] / 2;
+            my += s->mv[dir][i][1] / 2;
+        }
+    } else {
+        for (i = 0; i < 4; i++) {
+            hpel_motion(s,
+                        dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
+                        ref_picture[0],
+                        mb_x * 16 + (i & 1) * 8,
+                        mb_y * 16 + (i >> 1) * 8,
+                        pix_op[1],
+                        s->mv[dir][i][0],
+                        s->mv[dir][i][1]);
+
+            mx += s->mv[dir][i][0];
+            my += s->mv[dir][i][1];
+        }
+    }
+
+    if (!CONFIG_GRAY || !(s->avctx->flags & AV_CODEC_FLAG_GRAY))
+        chroma_4mv_motion(s, dest_cb, dest_cr,
+                          ref_picture, pix_op[1], mx, my);
+}
+
 /**
  * motion compensation of a single macroblock
  * @param s context
@@ -759,7 +827,7 @@ static inline void apply_obmc(MpegEncContext *s,
  * @param qpix_op qpel motion compensation function (average or put normally)
  * the motion vectors are taken from s->mv and the MV type from s->mv_type
  */
-static av_always_inline void MPV_motion_internal(MpegEncContext *s,
+static av_always_inline void mpv_motion_internal(MpegEncContext *s,
                                                  uint8_t *dest_y,
                                                  uint8_t *dest_cb,
                                                  uint8_t *dest_cr,
@@ -769,12 +837,8 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s,
                                                  qpel_mc_func (*qpix_op)[16],
                                                  int is_mpeg12)
 {
-    int dxy, mx, my, src_x, src_y, motion_x, motion_y;
-    int mb_x, mb_y, i;
-    uint8_t *ptr, *dest;
-
-    mb_x = s->mb_x;
-    mb_y = s->mb_y;
+    int i;
+    int mb_y = s->mb_y;
 
     prefetch_motion(s, ref_picture, dir);
 
@@ -810,65 +874,9 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s,
         }
         break;
     case MV_TYPE_8X8:
-        if (!is_mpeg12) {
-            mx = 0;
-            my = 0;
-            if (s->quarter_sample) {
-                for (i = 0; i < 4; i++) {
-                    motion_x = s->mv[dir][i][0];
-                    motion_y = s->mv[dir][i][1];
-
-                    dxy   = ((motion_y & 3) << 2) | (motion_x & 3);
-                    src_x = mb_x * 16 + (motion_x >> 2) + (i & 1) * 8;
-                    src_y = mb_y * 16 + (motion_y >> 2) + (i >> 1) * 8;
-
-                    /* WARNING: do no forget half pels */
-                    src_x = av_clip(src_x, -16, s->width);
-                    if (src_x == s->width)
-                        dxy &= ~3;
-                    src_y = av_clip(src_y, -16, s->height);
-                    if (src_y == s->height)
-                        dxy &= ~12;
-
-                    ptr = ref_picture[0] + (src_y * s->linesize) + (src_x);
-                    if (s->flags & CODEC_FLAG_EMU_EDGE) {
-                        if ((unsigned)src_x > FFMAX(s->h_edge_pos - (motion_x & 3) - 8, 0) ||
-                            (unsigned)src_y > FFMAX(s->v_edge_pos - (motion_y & 3) - 8, 0)) {
-                            s->vdsp.emulated_edge_mc(s->edge_emu_buffer, ptr,
-                                                     s->linesize,
-                                                     9, 9,
-                                                     src_x, src_y,
-                                                     s->h_edge_pos,
-                                                     s->v_edge_pos);
-                            ptr = s->edge_emu_buffer;
-                        }
-                    }
-                    dest = dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize;
-                    qpix_op[1][dxy](dest, ptr, s->linesize);
-
-                    mx += s->mv[dir][i][0] / 2;
-                    my += s->mv[dir][i][1] / 2;
-                }
-            } else {
-                for (i = 0; i < 4; i++) {
-                    hpel_motion(s,
-                                dest_y + ((i & 1) * 8) + (i >> 1) * 8 * s->linesize,
-                                ref_picture[0],
-                                mb_x * 16 + (i & 1) * 8,
-                                mb_y * 16 + (i >> 1) * 8,
-                                pix_op[1],
-                                s->mv[dir][i][0],
-                                s->mv[dir][i][1]);
-
-                    mx += s->mv[dir][i][0];
-                    my += s->mv[dir][i][1];
-                }
-            }
-
-            if (!CONFIG_GRAY || !(s->flags & CODEC_FLAG_GRAY))
-                chroma_4mv_motion(s, dest_cb, dest_cr,
-                                  ref_picture, pix_op[1], mx, my);
-        }
+        if (!is_mpeg12)
+            apply_8x8(s, dest_y, dest_cb, dest_cr,
+                      dir, ref_picture, qpix_op, pix_op);
         break;
     case MV_TYPE_FIELD:
         if (s->picture_structure == PICT_FRAME) {
@@ -893,7 +901,7 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s,
         } else {
             if (s->picture_structure != s->field_select[dir][0] + 1 &&
                 s->pict_type != AV_PICTURE_TYPE_B && !s->first_field) {
-                ref_picture = s->current_picture_ptr->f.data;
+                ref_picture = s->current_picture_ptr->f->data;
             }
 
             mpeg_motion(s, dest_y, dest_cb, dest_cr,
@@ -910,7 +918,7 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s,
                 || s->pict_type == AV_PICTURE_TYPE_B || s->first_field) {
                 ref2picture = ref_picture;
             } else {
-                ref2picture = s->current_picture_ptr->f.data;
+                ref2picture = s->current_picture_ptr->f->data;
             }
 
             mpeg_motion(s, dest_y, dest_cb, dest_cr,
@@ -949,7 +957,7 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s,
                 /* opposite parity is always in the same frame if this is
                  * second field */
                 if (!s->first_field) {
-                    ref_picture = s->current_picture_ptr->f.data;
+                    ref_picture = s->current_picture_ptr->f->data;
                 }
             }
         }
@@ -958,7 +966,7 @@ static av_always_inline void MPV_motion_internal(MpegEncContext *s,
     }
 }
 
-void ff_MPV_motion(MpegEncContext *s,
+void ff_mpv_motion(MpegEncContext *s,
                    uint8_t *dest_y, uint8_t *dest_cb,
                    uint8_t *dest_cr, int dir,
                    uint8_t **ref_picture,
@@ -967,10 +975,10 @@ void ff_MPV_motion(MpegEncContext *s,
 {
 #if !CONFIG_SMALL
     if (s->out_format == FMT_MPEG1)
-        MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
+        mpv_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
                             ref_picture, pix_op, qpix_op, 1);
     else
 #endif
-        MPV_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
+        mpv_motion_internal(s, dest_y, dest_cb, dest_cr, dir,
                             ref_picture, pix_op, qpix_op, 0);
 }