]> git.sesse.net Git - ffmpeg/blobdiff - libavcodec/motion_est.c
mpeg12: Move finding the best frame rate to common code
[ffmpeg] / libavcodec / motion_est.c
index d0c04393193a85f318ca48da1875be53c87f60c3..c6dd3b86afb2bc311052c8a24a036f9403204d3e 100644 (file)
@@ -102,7 +102,7 @@ static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3
 }
 
 static int get_flags(MotionEstContext *c, int direct, int chroma){
-    return   ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
+    return   ((c->avctx->flags&AV_CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
            + (direct ? FLAG_DIRECT : 0)
            + (chroma ? FLAG_CHROMA : 0);
 }
@@ -202,7 +202,7 @@ static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int
                     cx= (cx>>1)|(cx&1);
                     cy= (cy>>1)|(cy&1);
                     uvdxy= (cx&1) + 2*(cy&1);
-                    //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
+                    // FIXME x/y wrong, but MPEG-4 qpel is sick anyway, we should drop as much of it as possible in favor for H.264
                 }
             }else{
                 c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
@@ -309,12 +309,6 @@ int ff_init_me(MpegEncContext *s){
         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;
 
@@ -332,7 +326,7 @@ int ff_init_me(MpegEncContext *s){
     c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp    &FF_CMP_CHROMA);
 
 /*FIXME s->no_rounding b_type*/
-    if (s->avctx->flags & CODEC_FLAG_QPEL) {
+    if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
         c->sub_motion_search= qpel_motion_search;
         c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
         if (s->no_rounding)
@@ -534,7 +528,7 @@ static inline void get_limits(MpegEncContext *s, int x, int y)
         c->xmax = - x + s->mb_width *16;
         c->ymax = - y + s->mb_height*16;
     } else if (s->out_format == FMT_H261){
-        // Search range of H261 is different from other codec standards
+        // Search range of H.261 is different from other codec standards
         c->xmin = (x > 15) ? - 15 : 0;
         c->ymin = (y > 15) ? - 15 : 0;
         c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
@@ -863,7 +857,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
 {
     MotionEstContext * const c= &s->me;
     uint8_t *pix, *ppix;
-    int sum, mx, my, dmin;
+    int sum, mx = 0, my = 0, dmin = 0;
     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];
@@ -895,52 +889,43 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
     pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
     c->mb_var_sum_temp += (varc+128)>>8;
 
-    switch(s->me_method) {
-    case ME_ZERO:
-    default:
-        mx   = 0;
-        my   = 0;
-        dmin = 0;
-        break;
-    case ME_X1:
-    case ME_EPZS:
-       {
-            const int mot_stride = s->b8_stride;
-            const int mot_xy = s->block_index[0];
-
-            P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
-            P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
-
-            if(P_LEFT[0]       > (c->xmax<<shift)) P_LEFT[0]       = (c->xmax<<shift);
-
-            if(!s->first_slice_line) {
-                P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];
-                P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];
-                P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
-                P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
-                if(P_TOP[1]      > (c->ymax<<shift)) P_TOP[1]     = (c->ymax<<shift);
-                if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
-                if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
+    if (s->motion_est != FF_ME_ZERO) {
+        const int mot_stride = s->b8_stride;
+        const int mot_xy = s->block_index[0];
 
-                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]);
+        P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
+        P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
 
-                if(s->out_format == FMT_H263){
-                    c->pred_x = P_MEDIAN[0];
-                    c->pred_y = P_MEDIAN[1];
-                }else { /* mpeg1 at least */
-                    c->pred_x= P_LEFT[0];
-                    c->pred_y= P_LEFT[1];
-                }
-            }else{
-                c->pred_x= P_LEFT[0];
-                c->pred_y= P_LEFT[1];
-            }
+        if (P_LEFT[0] > (c->xmax << shift))
+            P_LEFT[0] =  c->xmax << shift;
 
+        if (!s->first_slice_line) {
+            P_TOP[0]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][0];
+            P_TOP[1]      = s->current_picture.motion_val[0][mot_xy - mot_stride    ][1];
+            P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
+            P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
+            if (P_TOP[1] > (c->ymax << shift))
+                P_TOP[1] =  c->ymax << shift;
+            if (P_TOPRIGHT[0] < (c->xmin << shift))
+                P_TOPRIGHT[0] =  c->xmin << shift;
+            if (P_TOPRIGHT[1] > (c->ymax << shift))
+                P_TOPRIGHT[1] =  c->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]);
+
+            if (s->out_format == FMT_H263) {
+                c->pred_x = P_MEDIAN[0];
+                c->pred_y = P_MEDIAN[1];
+            } else { /* MPEG-1 at least */
+                c->pred_x = P_LEFT[0];
+                c->pred_y = P_LEFT[1];
+            }
+        } else {
+            c->pred_x = P_LEFT[0];
+            c->pred_y = P_LEFT[1];
         }
         dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
-
-        break;
     }
 
     /* At this point (mx,my) are full-pell and the relative displacement */
@@ -969,7 +954,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
             mx <<=shift;
             my <<=shift;
         }
-        if ((s->avctx->flags & CODEC_FLAG_4MV)
+        if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
            && !c->skip && varc>50<<8 && vard>10<<8){
             if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
                 mb_type|=CANDIDATE_MB_TYPE_INTER4V;
@@ -977,7 +962,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
             set_p_mv_tables(s, mx, my, 0);
         }else
             set_p_mv_tables(s, mx, my, 1);
-        if ((s->avctx->flags & CODEC_FLAG_INTERLACED_ME)
+        if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
            && !c->skip){ //FIXME varc/d checks
             if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
                 mb_type |= CANDIDATE_MB_TYPE_INTER_I;
@@ -990,7 +975,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
         if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
             dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
 
-        if ((s->avctx->flags & CODEC_FLAG_4MV)
+        if ((s->avctx->flags & AV_CODEC_FLAG_4MV)
            && !c->skip && varc>50<<8 && vard>10<<8){
             int dmin4= h263_mv4_search(s, mx, my, shift);
             if(dmin4 < dmin){
@@ -998,7 +983,7 @@ void ff_estimate_p_frame_motion(MpegEncContext * s,
                 dmin=dmin4;
             }
         }
-        if ((s->avctx->flags & CODEC_FLAG_INTERLACED_ME)
+        if ((s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME)
            && !c->skip){ //FIXME varc/d checks
             int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
             if(dmin_i < dmin){
@@ -1100,7 +1085,7 @@ static int estimate_motion_b(MpegEncContext *s, int mb_x, int mb_y,
                              int16_t (*mv_table)[2], int ref_index, int f_code)
 {
     MotionEstContext * const c= &s->me;
-    int mx, my, dmin;
+    int mx = 0, my = 0, dmin = 0;
     int P[10][2];
     const int shift= 1+s->quarter_sample;
     const int mot_stride = s->mb_stride;
@@ -1115,15 +1100,7 @@ static int estimate_motion_b(MpegEncContext *s, int mb_x, int mb_y,
 
     get_limits(s, 16*mb_x, 16*mb_y);
 
-    switch(s->me_method) {
-    case ME_ZERO:
-    default:
-        mx   = 0;
-        my   = 0;
-        dmin = 0;
-        break;
-    case ME_X1:
-    case ME_EPZS:
+    if (s->motion_est != FF_ME_ZERO) {
         P_LEFT[0] = mv_table[mot_xy - 1][0];
         P_LEFT[1] = mv_table[mot_xy - 1][1];
 
@@ -1152,8 +1129,6 @@ static int estimate_motion_b(MpegEncContext *s, int mb_x, int mb_y,
         }
 
         dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
-
-        break;
     }
 
     dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
@@ -1512,7 +1487,7 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
         dmin= direct_search(s, mb_x, mb_y);
     else
         dmin= INT_MAX;
-//FIXME penalty stuff for non mpeg4
+// FIXME penalty stuff for non-MPEG-4
     c->skip=0;
     fmin = estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) +
            3 * penalty_factor;
@@ -1526,7 +1501,7 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
     fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
     ff_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
 
-    if (s->avctx->flags & CODEC_FLAG_INTERLACED_ME) {
+    if (s->avctx->flags & AV_CODEC_FLAG_INTERLACED_ME) {
 //FIXME mb type penalty
         c->skip=0;
         c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
@@ -1592,7 +1567,7 @@ void ff_estimate_b_frame_motion(MpegEncContext * s,
 /* find best f_code for ME which do unlimited searches */
 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
 {
-    if(s->me_method>=ME_EPZS){
+    if (s->motion_est != FF_ME_ZERO) {
         int score[8];
         int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
         uint8_t * fcode_tab= s->fcode_tab;
@@ -1657,7 +1632,7 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
 
     if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
 
-    if (s->avctx->flags & CODEC_FLAG_4MV) {
+    if (s->avctx->flags & AV_CODEC_FLAG_4MV) {
         const int wrap= s->b8_stride;
 
         /* clip / convert to intra 8x8 type MVs */
@@ -1690,7 +1665,6 @@ void ff_fix_long_p_mvs(MpegEncContext * s)
 }
 
 /**
- *
  * @param truncate 1 for truncation, 0 for using intra
  */
 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,