]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/motion_est.c
Warn the user about me_method values that are not supported.
[ffmpeg] / libavcodec / motion_est.c
index fc057712d4fa3f4620d5645b17549a333afb2c78..5187ef7bb3c656bac662ad8f8bc94efb76d3433f 100644 (file)
@@ -3,6 +3,7 @@
  * Copyright (c) 2000,2001 Fabrice Bellard.
  * Copyright (c) 2002-2004 Michael Niedermayer
  *
+ * new motion estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  *
  * This file is part of FFmpeg.
  *
@@ -19,8 +20,6 @@
  * You should have received a copy of the GNU Lesser General Public
  * License along with FFmpeg; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- *
- * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  */
 
 /**
@@ -103,6 +102,9 @@ static int get_flags(MotionEstContext *c, int direct, int chroma){
            + (chroma ? FLAG_CHROMA : 0);
 }
 
+/*! \brief compares a block (either a full macroblock or a partition thereof)
+    against a proposed motion-compensated prediction of that block
+ */
 static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
                       const int size, const int h, int ref_index, int src_index,
                       me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
@@ -119,6 +121,7 @@ static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, con
     int d;
     //FIXME check chroma 4mv, (no crashes ...)
     if(flags&FLAG_DIRECT){
+        assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
         if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
             const int time_pp= s->pp_time;
             const int time_pb= s->pb_time;
@@ -228,10 +231,26 @@ static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
 static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
 }
 
-void ff_init_me(MpegEncContext *s){
+int ff_init_me(MpegEncContext *s){
     MotionEstContext * const c= &s->me;
+    int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
+    int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
+
+    if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){
+        av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
+        return -1;
+    }
+    if(s->me_method!=ME_ZERO && s->me_method!=ME_EPZS && s->me_method!=ME_X1){
+        av_log(s->avctx, AV_LOG_ERROR, "me_method is only allowed to be set to zero and epzs; for hex,umh,full and others see dia_size\n");
+        return -1;
+    }
+
     c->avctx= s->avctx;
 
+    if(cache_size < 2*dia_size && !c->stride){
+        av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
+    }
+
     ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
     ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
     ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
@@ -269,7 +288,9 @@ void ff_init_me(MpegEncContext *s){
         c->uvstride=  8*s->mb_width + 16;
     }
 
-    // 8x8 fullpel search would need a 4x4 chroma compare, which we dont have yet, and even if we had the motion estimation code doesnt expect it
+    /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
+     * not have yet, and even if we had, the motion estimation code
+     * does not expect it. */
     if(s->codec_id != CODEC_ID_SNOW){
         if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
             s->dsp.me_cmp[2]= zero_cmp;
@@ -286,6 +307,8 @@ void ff_init_me(MpegEncContext *s){
     }
 
     c->temp= c->scratchpad;
+
+    return 0;
 }
 
 #if 0
@@ -319,229 +342,6 @@ static inline void no_motion_search(MpegEncContext * s,
     *my_ptr = 16 * s->mb_y;
 }
 
-#if 0  /* the use of these functions is inside #if 0 */
-static int full_motion_search(MpegEncContext * s,
-                              int *mx_ptr, int *my_ptr, int range,
-                              int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
-{
-    int x1, y1, x2, y2, xx, yy, x, y;
-    int mx, my, dmin, d;
-    uint8_t *pix;
-
-    xx = 16 * s->mb_x;
-    yy = 16 * s->mb_y;
-    x1 = xx - range + 1;        /* we loose one pixel to avoid boundary pb with half pixel pred */
-    if (x1 < xmin)
-        x1 = xmin;
-    x2 = xx + range - 1;
-    if (x2 > xmax)
-        x2 = xmax;
-    y1 = yy - range + 1;
-    if (y1 < ymin)
-        y1 = ymin;
-    y2 = yy + range - 1;
-    if (y2 > ymax)
-        y2 = ymax;
-    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
-    dmin = 0x7fffffff;
-    mx = 0;
-    my = 0;
-    for (y = y1; y <= y2; y++) {
-        for (x = x1; x <= x2; x++) {
-            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
-                             s->linesize, 16);
-            if (d < dmin ||
-                (d == dmin &&
-                 (abs(x - xx) + abs(y - yy)) <
-                 (abs(mx - xx) + abs(my - yy)))) {
-                dmin = d;
-                mx = x;
-                my = y;
-            }
-        }
-    }
-
-    *mx_ptr = mx;
-    *my_ptr = my;
-
-#if 0
-    if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
-        *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
-        av_log(NULL, AV_LOG_ERROR, "error %d %d\n", *mx_ptr, *my_ptr);
-    }
-#endif
-    return dmin;
-}
-
-
-static int log_motion_search(MpegEncContext * s,
-                             int *mx_ptr, int *my_ptr, int range,
-                             int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
-{
-    int x1, y1, x2, y2, xx, yy, x, y;
-    int mx, my, dmin, d;
-    uint8_t *pix;
-
-    xx = s->mb_x << 4;
-    yy = s->mb_y << 4;
-
-    /* Left limit */
-    x1 = xx - range;
-    if (x1 < xmin)
-        x1 = xmin;
-
-    /* Right limit */
-    x2 = xx + range;
-    if (x2 > xmax)
-        x2 = xmax;
-
-    /* Upper limit */
-    y1 = yy - range;
-    if (y1 < ymin)
-        y1 = ymin;
-
-    /* Lower limit */
-    y2 = yy + range;
-    if (y2 > ymax)
-        y2 = ymax;
-
-    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
-    dmin = 0x7fffffff;
-    mx = 0;
-    my = 0;
-
-    do {
-        for (y = y1; y <= y2; y += range) {
-            for (x = x1; x <= x2; x += range) {
-                d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
-                if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
-                    dmin = d;
-                    mx = x;
-                    my = y;
-                }
-            }
-        }
-
-        range = range >> 1;
-
-        x1 = mx - range;
-        if (x1 < xmin)
-            x1 = xmin;
-
-        x2 = mx + range;
-        if (x2 > xmax)
-            x2 = xmax;
-
-        y1 = my - range;
-        if (y1 < ymin)
-            y1 = ymin;
-
-        y2 = my + range;
-        if (y2 > ymax)
-            y2 = ymax;
-
-    } while (range >= 1);
-
-#ifdef DEBUG
-    av_log(s->avctx, AV_LOG_DEBUG, "log       - MX: %d\tMY: %d\n", mx, my);
-#endif
-    *mx_ptr = mx;
-    *my_ptr = my;
-    return dmin;
-}
-
-static int phods_motion_search(MpegEncContext * s,
-                               int *mx_ptr, int *my_ptr, int range,
-                               int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
-{
-    int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
-    int mx, my, dminx, dminy;
-    uint8_t *pix;
-
-    xx = s->mb_x << 4;
-    yy = s->mb_y << 4;
-
-    /* Left limit */
-    x1 = xx - range;
-    if (x1 < xmin)
-        x1 = xmin;
-
-    /* Right limit */
-    x2 = xx + range;
-    if (x2 > xmax)
-        x2 = xmax;
-
-    /* Upper limit */
-    y1 = yy - range;
-    if (y1 < ymin)
-        y1 = ymin;
-
-    /* Lower limit */
-    y2 = yy + range;
-    if (y2 > ymax)
-        y2 = ymax;
-
-    pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
-    mx = 0;
-    my = 0;
-
-    x = xx;
-    y = yy;
-    do {
-        dminx = 0x7fffffff;
-        dminy = 0x7fffffff;
-
-        lastx = x;
-        for (x = x1; x <= x2; x += range) {
-            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
-            if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
-                dminx = d;
-                mx = x;
-            }
-        }
-
-        x = lastx;
-        for (y = y1; y <= y2; y += range) {
-            d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
-            if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
-                dminy = d;
-                my = y;
-            }
-        }
-
-        range = range >> 1;
-
-        x = mx;
-        y = my;
-        x1 = mx - range;
-        if (x1 < xmin)
-            x1 = xmin;
-
-        x2 = mx + range;
-        if (x2 > xmax)
-            x2 = xmax;
-
-        y1 = my - range;
-        if (y1 < ymin)
-            y1 = ymin;
-
-        y2 = my + range;
-        if (y2 > ymax)
-            y2 = ymax;
-
-    } while (range >= 1);
-
-#ifdef DEBUG
-    av_log(s->avctx, AV_LOG_DEBUG, "phods     - MX: %d\tMY: %d\n", mx, my);
-#endif
-
-    /* half pixel search */
-    *mx_ptr = mx;
-    *my_ptr = my;
-    return dminy;
-}
-#endif /* 0 */
-
 #define Z_THRESHOLD 256
 
 #define CHECK_SAD_HALF_MV(suffix, x, y) \
@@ -689,6 +489,7 @@ static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
 static inline void get_limits(MpegEncContext *s, int x, int y)
 {
     MotionEstContext * const c= &s->me;
+    int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
 /*
     if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
     else                   c->range= 16;
@@ -710,6 +511,12 @@ static inline void get_limits(MpegEncContext *s, int x, int y)
         c->xmax = - x + s->mb_width *16 - 16;
         c->ymax = - y + s->mb_height*16 - 16;
     }
+    if(range){
+        c->xmin = FFMAX(c->xmin,-range);
+        c->xmax = FFMIN(c->xmax, range);
+        c->ymin = FFMAX(c->ymin,-range);
+        c->ymax = FFMIN(c->ymax, range);
+    }
 }
 
 static inline void init_mv4_ref(MotionEstContext *c){
@@ -1145,7 +952,9 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
 {
     MotionEstContext * const c= &s->me;
     uint8_t *pix, *ppix;
-    int sum, varc, vard, mx, my, dmin;
+    int sum, mx, my, dmin;
+    int varc;            ///< the variance of the block (sum of squared (p[y][x]-average))
+    int vard;            ///< sum of squared differences with the estimated motion vector
     int P[10][2];
     const int shift= 1+s->quarter_sample;
     int mb_type=0;
@@ -1197,23 +1006,6 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
         my-= mb_y*16;
         dmin = 0;
         break;
-#if 0
-    case ME_FULL:
-        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-    case ME_LOG:
-        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-    case ME_PHODS:
-        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-#endif
     case ME_X1:
     case ME_EPZS:
        {
@@ -1485,23 +1277,6 @@ static int ff_estimate_motion_b(MpegEncContext * s,
         mx-= mb_x*16;
         my-= mb_y*16;
         break;
-#if 0
-    case ME_FULL:
-        dmin = full_motion_search(s, &mx, &my, range, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-    case ME_LOG:
-        dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-    case ME_PHODS:
-        dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
-        mx-= mb_x*16;
-        my-= mb_y*16;
-        break;
-#endif
     case ME_X1:
     case ME_EPZS:
        {
@@ -1782,15 +1557,15 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
     c->pred_x=0;
     c->pred_y=0;
 
-    P_LEFT[0]        = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
-    P_LEFT[1]        = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
+    P_LEFT[0]        = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
+    P_LEFT[1]        = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
 
     /* special case for first line */
-    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its clipped
-        P_TOP[0]      = clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
-        P_TOP[1]      = clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
-        P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
-        P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
+    if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
+        P_TOP[0]      = av_clip(mv_table[mot_xy - mot_stride             ][0], xmin<<shift, xmax<<shift);
+        P_TOP[1]      = av_clip(mv_table[mot_xy - mot_stride             ][1], ymin<<shift, ymax<<shift);
+        P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1         ][0], xmin<<shift, xmax<<shift);
+        P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1         ][1], ymin<<shift, ymax<<shift);
 
         P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
         P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
@@ -1807,8 +1582,8 @@ static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
 
     get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
 
-    s->b_direct_mv_table[mot_xy][0]= mx;
-    s->b_direct_mv_table[mot_xy][1]= my;
+    mv_table[mot_xy][0]= mx;
+    mv_table[mot_xy][1]= my;
     c->flags     &= ~FLAG_DIRECT;
     c->sub_flags &= ~FLAG_DIRECT;
 
@@ -1828,6 +1603,18 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
     get_limits(s, 16*mb_x, 16*mb_y);
 
     c->skip=0;
+
+    if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){
+        int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
+
+        score= ((unsigned)(score*score + 128*256))>>16;
+        c->mc_mb_var_sum_temp += score;
+        s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
+        s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
+
+        return;
+    }
+
     if(c->avctx->me_threshold){
         int vard= check_input_motion(s, mb_x, mb_y, 0);
 
@@ -1949,7 +1736,9 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
             type |= CANDIDATE_MB_TYPE_BIDIR_I;
         }
          //FIXME something smarter
-        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
+        if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
+        if(s->codec_id == CODEC_ID_MPEG4 && type&CANDIDATE_MB_TYPE_DIRECT && s->flags&CODEC_FLAG_MV0 && *(uint32_t*)s->b_direct_mv_table[xy])
+            type |= CANDIDATE_MB_TYPE_DIRECT0;
 #if 0
         if(s->out_format == FMT_MPEG1)
             type |= CANDIDATE_MB_TYPE_INTRA;
@@ -1992,7 +1781,7 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
                             continue;
 
                     for(j=0; j<fcode && j<8; j++){
-                        if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
+                        if(s->pict_type==FF_B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
                             score[j]-= 170;
                     }
                 }
@@ -2024,7 +1813,7 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
     MotionEstContext * const c= &s->me;
     const int f_code= s->f_code;
     int y, range;
-    assert(s->pict_type==P_TYPE);
+    assert(s->pict_type==FF_P_TYPE);
 
     range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);