X-Git-Url: https://git.sesse.net/?a=blobdiff_plain;f=libavcodec%2Fmotion_est.c;h=047fbd669797d111101d5a93b104a2b0cd6d4a68;hb=ddffcb2d3a81e203cff6f3e39e40bf5720f7391e;hp=cb6cc64529f79e60d20ac804362107a5afa59031;hpb=e828d257e7a513f55bdc9f5260d9606460b45052;p=ffmpeg diff --git a/libavcodec/motion_est.c b/libavcodec/motion_est.c index cb6cc64529f..047fbd66979 100644 --- a/libavcodec/motion_est.c +++ b/libavcodec/motion_est.c @@ -1,8 +1,9 @@ /* * Motion estimation - * Copyright (c) 2000,2001 Fabrice Bellard. + * Copyright (c) 2000,2001 Fabrice Bellard * Copyright (c) 2002-2004 Michael Niedermayer * + * new motion estimation (X1/EPZS) by Michael Niedermayer * * This file is part of FFmpeg. * @@ -19,12 +20,10 @@ * 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 */ /** - * @file motion_est.c + * @file libavcodec/motion_est.c * Motion estimation. */ @@ -33,6 +32,7 @@ #include #include "avcodec.h" #include "dsputil.h" +#include "mathops.h" #include "mpegvideo.h" #undef NDEBUG @@ -103,6 +103,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 +122,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 +232,27 @@ 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<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; + } + //special case of snow is needed because snow uses its own iterative ME code + if(s->me_method!=ME_ZERO && s->me_method!=ME_EPZS && s->me_method!=ME_X1 && s->avctx->codec_id != CODEC_ID_SNOW){ + 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 +290,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; @@ -285,7 +308,7 @@ void ff_init_me(MpegEncContext *s){ c->sub_motion_search= no_sub_motion_search; } - 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){ @@ -1199,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: { @@ -1487,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: { @@ -1784,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<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<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<?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; @@ -1830,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); @@ -1951,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; @@ -1994,7 +1781,7 @@ int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) continue; for(j=0; jpict_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; } } @@ -2026,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);