Libav 0.7.1
|
00001 /* 00002 * Motion estimation 00003 * Copyright (c) 2000,2001 Fabrice Bellard 00004 * Copyright (c) 2002-2004 Michael Niedermayer 00005 * 00006 * new motion estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at> 00007 * 00008 * This file is part of Libav. 00009 * 00010 * Libav is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * Libav is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with Libav; if not, write to the Free Software 00022 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 00023 */ 00024 00030 #include <stdlib.h> 00031 #include <stdio.h> 00032 #include <limits.h> 00033 #include "libavutil/intmath.h" 00034 #include "avcodec.h" 00035 #include "dsputil.h" 00036 #include "mathops.h" 00037 #include "mpegvideo.h" 00038 00039 #undef NDEBUG 00040 #include <assert.h> 00041 00042 #define SQ(a) ((a)*(a)) 00043 00044 #define P_LEFT P[1] 00045 #define P_TOP P[2] 00046 #define P_TOPRIGHT P[3] 00047 #define P_MEDIAN P[4] 00048 #define P_MV1 P[9] 00049 00050 static inline int sad_hpel_motion_search(MpegEncContext * s, 00051 int *mx_ptr, int *my_ptr, int dmin, 00052 int src_index, int ref_index, 00053 int size, int h); 00054 00055 static inline unsigned update_map_generation(MotionEstContext *c) 00056 { 00057 c->map_generation+= 1<<(ME_MAP_MV_BITS*2); 00058 if(c->map_generation==0){ 00059 c->map_generation= 1<<(ME_MAP_MV_BITS*2); 00060 memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE); 00061 } 00062 return c->map_generation; 00063 } 00064 00065 /* shape adaptive search stuff */ 00066 typedef struct Minima{ 00067 int height; 00068 int x, y; 00069 int checked; 00070 }Minima; 00071 00072 static int minima_cmp(const void *a, const void *b){ 00073 const Minima *da = (const Minima *) a; 00074 const Minima *db = (const Minima *) b; 00075 00076 return da->height - db->height; 00077 } 00078 00079 #define FLAG_QPEL 1 //must be 1 00080 #define FLAG_CHROMA 2 00081 #define FLAG_DIRECT 4 00082 00083 static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){ 00084 const int offset[3]= { 00085 y*c-> stride + x, 00086 ((y*c->uvstride + x)>>1), 00087 ((y*c->uvstride + x)>>1), 00088 }; 00089 int i; 00090 for(i=0; i<3; i++){ 00091 c->src[0][i]= src [i] + offset[i]; 00092 c->ref[0][i]= ref [i] + offset[i]; 00093 } 00094 if(ref_index){ 00095 for(i=0; i<3; i++){ 00096 c->ref[ref_index][i]= ref2[i] + offset[i]; 00097 } 00098 } 00099 } 00100 00101 static int get_flags(MotionEstContext *c, int direct, int chroma){ 00102 return ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0) 00103 + (direct ? FLAG_DIRECT : 0) 00104 + (chroma ? FLAG_CHROMA : 0); 00105 } 00106 00107 static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby, 00108 const int size, const int h, int ref_index, int src_index, 00109 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){ 00110 MotionEstContext * const c= &s->me; 00111 const int stride= c->stride; 00112 const int hx= subx + (x<<(1+qpel)); 00113 const int hy= suby + (y<<(1+qpel)); 00114 uint8_t * const * const ref= c->ref[ref_index]; 00115 uint8_t * const * const src= c->src[src_index]; 00116 int d; 00117 //FIXME check chroma 4mv, (no crashes ...) 00118 assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)); 00119 if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){ 00120 const int time_pp= s->pp_time; 00121 const int time_pb= s->pb_time; 00122 const int mask= 2*qpel+1; 00123 if(s->mv_type==MV_TYPE_8X8){ 00124 int i; 00125 for(i=0; i<4; i++){ 00126 int fx = c->direct_basis_mv[i][0] + hx; 00127 int fy = c->direct_basis_mv[i][1] + hy; 00128 int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4)); 00129 int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4)); 00130 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); 00131 int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); 00132 00133 uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1); 00134 if(qpel){ 00135 c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride); 00136 c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride); 00137 }else{ 00138 c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8); 00139 c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8); 00140 } 00141 } 00142 }else{ 00143 int fx = c->direct_basis_mv[0][0] + hx; 00144 int fy = c->direct_basis_mv[0][1] + hy; 00145 int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp); 00146 int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp); 00147 int fxy= (fx&mask) + ((fy&mask)<<(qpel+1)); 00148 int bxy= (bx&mask) + ((by&mask)<<(qpel+1)); 00149 00150 if(qpel){ 00151 c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride); 00152 c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride); 00153 c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride); 00154 c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride); 00155 c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride); 00156 c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride); 00157 c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride); 00158 c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride); 00159 }else{ 00160 assert((fx>>1) + 16*s->mb_x >= -16); 00161 assert((fy>>1) + 16*s->mb_y >= -16); 00162 assert((fx>>1) + 16*s->mb_x <= s->width); 00163 assert((fy>>1) + 16*s->mb_y <= s->height); 00164 assert((bx>>1) + 16*s->mb_x >= -16); 00165 assert((by>>1) + 16*s->mb_y >= -16); 00166 assert((bx>>1) + 16*s->mb_x <= s->width); 00167 assert((by>>1) + 16*s->mb_y <= s->height); 00168 00169 c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16); 00170 c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16); 00171 } 00172 } 00173 d = cmp_func(s, c->temp, src[0], stride, 16); 00174 }else 00175 d= 256*256*256*32; 00176 return d; 00177 } 00178 00179 static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby, 00180 const int size, const int h, int ref_index, int src_index, 00181 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){ 00182 MotionEstContext * const c= &s->me; 00183 const int stride= c->stride; 00184 const int uvstride= c->uvstride; 00185 const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel? 00186 const int hx= subx + (x<<(1+qpel)); 00187 const int hy= suby + (y<<(1+qpel)); 00188 uint8_t * const * const ref= c->ref[ref_index]; 00189 uint8_t * const * const src= c->src[src_index]; 00190 int d; 00191 //FIXME check chroma 4mv, (no crashes ...) 00192 int uvdxy; /* no, it might not be used uninitialized */ 00193 if(dxy){ 00194 if(qpel){ 00195 c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h) 00196 if(chroma){ 00197 int cx= hx/2; 00198 int cy= hy/2; 00199 cx= (cx>>1)|(cx&1); 00200 cy= (cy>>1)|(cy&1); 00201 uvdxy= (cx&1) + 2*(cy&1); 00202 //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264 00203 } 00204 }else{ 00205 c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h); 00206 if(chroma) 00207 uvdxy= dxy | (x&1) | (2*(y&1)); 00208 } 00209 d = cmp_func(s, c->temp, src[0], stride, h); 00210 }else{ 00211 d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h); 00212 if(chroma) 00213 uvdxy= (x&1) + 2*(y&1); 00214 } 00215 if(chroma){ 00216 uint8_t * const uvtemp= c->temp + 16*stride; 00217 c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); 00218 c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1); 00219 d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1); 00220 d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1); 00221 } 00222 return d; 00223 } 00224 00225 static int cmp_simple(MpegEncContext *s, const int x, const int y, 00226 int ref_index, int src_index, 00227 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){ 00228 return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0); 00229 } 00230 00231 static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y, 00232 const int size, const int h, int ref_index, int src_index, 00233 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ 00234 if(flags&FLAG_DIRECT){ 00235 return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL); 00236 }else{ 00237 return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA); 00238 } 00239 } 00240 00241 static int cmp_internal(MpegEncContext *s, const int x, const int y, const int subx, const int suby, 00242 const int size, const int h, int ref_index, int src_index, 00243 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ 00244 if(flags&FLAG_DIRECT){ 00245 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL); 00246 }else{ 00247 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL, flags&FLAG_CHROMA); 00248 } 00249 } 00250 00254 static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby, 00255 const int size, const int h, int ref_index, int src_index, 00256 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ 00257 if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size) 00258 && av_builtin_constant_p(subx) && av_builtin_constant_p(suby) 00259 && flags==0 && h==16 && size==0 && subx==0 && suby==0){ 00260 return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func); 00261 }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby) 00262 && subx==0 && suby==0){ 00263 return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags); 00264 }else{ 00265 return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags); 00266 } 00267 } 00268 00269 static int cmp_hpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby, 00270 const int size, const int h, int ref_index, int src_index, 00271 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ 00272 if(flags&FLAG_DIRECT){ 00273 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0); 00274 }else{ 00275 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA); 00276 } 00277 } 00278 00279 static int cmp_qpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby, 00280 const int size, const int h, int ref_index, int src_index, 00281 me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){ 00282 if(flags&FLAG_DIRECT){ 00283 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1); 00284 }else{ 00285 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA); 00286 } 00287 } 00288 00289 #include "motion_est_template.c" 00290 00291 static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){ 00292 return 0; 00293 } 00294 00295 static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){ 00296 } 00297 00298 int ff_init_me(MpegEncContext *s){ 00299 MotionEstContext * const c= &s->me; 00300 int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT); 00301 int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255); 00302 00303 if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){ 00304 av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n"); 00305 return -1; 00306 } 00307 //special case of snow is needed because snow uses its own iterative ME code 00308 if(s->me_method!=ME_ZERO && s->me_method!=ME_EPZS && s->me_method!=ME_X1 && s->avctx->codec_id != CODEC_ID_SNOW){ 00309 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"); 00310 return -1; 00311 } 00312 00313 c->avctx= s->avctx; 00314 00315 if(cache_size < 2*dia_size && !c->stride){ 00316 av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n"); 00317 } 00318 00319 ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp); 00320 ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp); 00321 ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp); 00322 ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp); 00323 00324 c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA); 00325 c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA); 00326 c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA); 00327 00328 /*FIXME s->no_rounding b_type*/ 00329 if(s->flags&CODEC_FLAG_QPEL){ 00330 c->sub_motion_search= qpel_motion_search; 00331 c->qpel_avg= s->dsp.avg_qpel_pixels_tab; 00332 if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab; 00333 else c->qpel_put= s->dsp.put_qpel_pixels_tab; 00334 }else{ 00335 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA) 00336 c->sub_motion_search= hpel_motion_search; 00337 else if( c->avctx->me_sub_cmp == FF_CMP_SAD 00338 && c->avctx-> me_cmp == FF_CMP_SAD 00339 && c->avctx-> mb_cmp == FF_CMP_SAD) 00340 c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles 00341 else 00342 c->sub_motion_search= hpel_motion_search; 00343 } 00344 c->hpel_avg= s->dsp.avg_pixels_tab; 00345 if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab; 00346 else c->hpel_put= s->dsp.put_pixels_tab; 00347 00348 if(s->linesize){ 00349 c->stride = s->linesize; 00350 c->uvstride= s->uvlinesize; 00351 }else{ 00352 c->stride = 16*s->mb_width + 32; 00353 c->uvstride= 8*s->mb_width + 16; 00354 } 00355 00356 /* 8x8 fullpel search would need a 4x4 chroma compare, which we do 00357 * not have yet, and even if we had, the motion estimation code 00358 * does not expect it. */ 00359 if(s->codec_id != CODEC_ID_SNOW){ 00360 if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){ 00361 s->dsp.me_cmp[2]= zero_cmp; 00362 } 00363 if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){ 00364 s->dsp.me_sub_cmp[2]= zero_cmp; 00365 } 00366 c->hpel_put[2][0]= c->hpel_put[2][1]= 00367 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel; 00368 } 00369 00370 if(s->codec_id == CODEC_ID_H261){ 00371 c->sub_motion_search= no_sub_motion_search; 00372 } 00373 00374 return 0; 00375 } 00376 00377 #if 0 00378 static int pix_dev(uint8_t * pix, int line_size, int mean) 00379 { 00380 int s, i, j; 00381 00382 s = 0; 00383 for (i = 0; i < 16; i++) { 00384 for (j = 0; j < 16; j += 8) { 00385 s += FFABS(pix[0]-mean); 00386 s += FFABS(pix[1]-mean); 00387 s += FFABS(pix[2]-mean); 00388 s += FFABS(pix[3]-mean); 00389 s += FFABS(pix[4]-mean); 00390 s += FFABS(pix[5]-mean); 00391 s += FFABS(pix[6]-mean); 00392 s += FFABS(pix[7]-mean); 00393 pix += 8; 00394 } 00395 pix += line_size - 16; 00396 } 00397 return s; 00398 } 00399 #endif 00400 00401 static inline void no_motion_search(MpegEncContext * s, 00402 int *mx_ptr, int *my_ptr) 00403 { 00404 *mx_ptr = 16 * s->mb_x; 00405 *my_ptr = 16 * s->mb_y; 00406 } 00407 00408 #define Z_THRESHOLD 256 00409 00410 #define CHECK_SAD_HALF_MV(suffix, x, y) \ 00411 {\ 00412 d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\ 00413 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\ 00414 COPY3_IF_LT(dminh, d, dx, x, dy, y)\ 00415 } 00416 00417 static inline int sad_hpel_motion_search(MpegEncContext * s, 00418 int *mx_ptr, int *my_ptr, int dmin, 00419 int src_index, int ref_index, 00420 int size, int h) 00421 { 00422 MotionEstContext * const c= &s->me; 00423 const int penalty_factor= c->sub_penalty_factor; 00424 int mx, my, dminh; 00425 uint8_t *pix, *ptr; 00426 int stride= c->stride; 00427 const int flags= c->sub_flags; 00428 LOAD_COMMON 00429 00430 assert(flags == 0); 00431 00432 if(c->skip){ 00433 // printf("S"); 00434 *mx_ptr = 0; 00435 *my_ptr = 0; 00436 return dmin; 00437 } 00438 // printf("N"); 00439 00440 pix = c->src[src_index][0]; 00441 00442 mx = *mx_ptr; 00443 my = *my_ptr; 00444 ptr = c->ref[ref_index][0] + (my * stride) + mx; 00445 00446 dminh = dmin; 00447 00448 if (mx > xmin && mx < xmax && 00449 my > ymin && my < ymax) { 00450 int dx=0, dy=0; 00451 int d, pen_x, pen_y; 00452 const int index= (my<<ME_MAP_SHIFT) + mx; 00453 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; 00454 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]; 00455 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]; 00456 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]; 00457 mx<<=1; 00458 my<<=1; 00459 00460 00461 pen_x= pred_x + mx; 00462 pen_y= pred_y + my; 00463 00464 ptr-= stride; 00465 if(t<=b){ 00466 CHECK_SAD_HALF_MV(y2 , 0, -1) 00467 if(l<=r){ 00468 CHECK_SAD_HALF_MV(xy2, -1, -1) 00469 if(t+r<=b+l){ 00470 CHECK_SAD_HALF_MV(xy2, +1, -1) 00471 ptr+= stride; 00472 }else{ 00473 ptr+= stride; 00474 CHECK_SAD_HALF_MV(xy2, -1, +1) 00475 } 00476 CHECK_SAD_HALF_MV(x2 , -1, 0) 00477 }else{ 00478 CHECK_SAD_HALF_MV(xy2, +1, -1) 00479 if(t+l<=b+r){ 00480 CHECK_SAD_HALF_MV(xy2, -1, -1) 00481 ptr+= stride; 00482 }else{ 00483 ptr+= stride; 00484 CHECK_SAD_HALF_MV(xy2, +1, +1) 00485 } 00486 CHECK_SAD_HALF_MV(x2 , +1, 0) 00487 } 00488 }else{ 00489 if(l<=r){ 00490 if(t+l<=b+r){ 00491 CHECK_SAD_HALF_MV(xy2, -1, -1) 00492 ptr+= stride; 00493 }else{ 00494 ptr+= stride; 00495 CHECK_SAD_HALF_MV(xy2, +1, +1) 00496 } 00497 CHECK_SAD_HALF_MV(x2 , -1, 0) 00498 CHECK_SAD_HALF_MV(xy2, -1, +1) 00499 }else{ 00500 if(t+r<=b+l){ 00501 CHECK_SAD_HALF_MV(xy2, +1, -1) 00502 ptr+= stride; 00503 }else{ 00504 ptr+= stride; 00505 CHECK_SAD_HALF_MV(xy2, -1, +1) 00506 } 00507 CHECK_SAD_HALF_MV(x2 , +1, 0) 00508 CHECK_SAD_HALF_MV(xy2, +1, +1) 00509 } 00510 CHECK_SAD_HALF_MV(y2 , 0, +1) 00511 } 00512 mx+=dx; 00513 my+=dy; 00514 00515 }else{ 00516 mx<<=1; 00517 my<<=1; 00518 } 00519 00520 *mx_ptr = mx; 00521 *my_ptr = my; 00522 return dminh; 00523 } 00524 00525 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4) 00526 { 00527 const int xy= s->mb_x + s->mb_y*s->mb_stride; 00528 00529 s->p_mv_table[xy][0] = mx; 00530 s->p_mv_table[xy][1] = my; 00531 00532 /* has already been set to the 4 MV if 4MV is done */ 00533 if(mv4){ 00534 int mot_xy= s->block_index[0]; 00535 00536 s->current_picture.motion_val[0][mot_xy ][0]= mx; 00537 s->current_picture.motion_val[0][mot_xy ][1]= my; 00538 s->current_picture.motion_val[0][mot_xy+1][0]= mx; 00539 s->current_picture.motion_val[0][mot_xy+1][1]= my; 00540 00541 mot_xy += s->b8_stride; 00542 s->current_picture.motion_val[0][mot_xy ][0]= mx; 00543 s->current_picture.motion_val[0][mot_xy ][1]= my; 00544 s->current_picture.motion_val[0][mot_xy+1][0]= mx; 00545 s->current_picture.motion_val[0][mot_xy+1][1]= my; 00546 } 00547 } 00548 00552 static inline void get_limits(MpegEncContext *s, int x, int y) 00553 { 00554 MotionEstContext * const c= &s->me; 00555 int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL)); 00556 /* 00557 if(c->avctx->me_range) c->range= c->avctx->me_range >> 1; 00558 else c->range= 16; 00559 */ 00560 if (s->unrestricted_mv) { 00561 c->xmin = - x - 16; 00562 c->ymin = - y - 16; 00563 c->xmax = - x + s->mb_width *16; 00564 c->ymax = - y + s->mb_height*16; 00565 } else if (s->out_format == FMT_H261){ 00566 // Search range of H261 is different from other codec standards 00567 c->xmin = (x > 15) ? - 15 : 0; 00568 c->ymin = (y > 15) ? - 15 : 0; 00569 c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0; 00570 c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0; 00571 } else { 00572 c->xmin = - x; 00573 c->ymin = - y; 00574 c->xmax = - x + s->mb_width *16 - 16; 00575 c->ymax = - y + s->mb_height*16 - 16; 00576 } 00577 if(range){ 00578 c->xmin = FFMAX(c->xmin,-range); 00579 c->xmax = FFMIN(c->xmax, range); 00580 c->ymin = FFMAX(c->ymin,-range); 00581 c->ymax = FFMIN(c->ymax, range); 00582 } 00583 } 00584 00585 static inline void init_mv4_ref(MotionEstContext *c){ 00586 const int stride= c->stride; 00587 00588 c->ref[1][0] = c->ref[0][0] + 8; 00589 c->ref[2][0] = c->ref[0][0] + 8*stride; 00590 c->ref[3][0] = c->ref[2][0] + 8; 00591 c->src[1][0] = c->src[0][0] + 8; 00592 c->src[2][0] = c->src[0][0] + 8*stride; 00593 c->src[3][0] = c->src[2][0] + 8; 00594 } 00595 00596 static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift) 00597 { 00598 MotionEstContext * const c= &s->me; 00599 const int size= 1; 00600 const int h=8; 00601 int block; 00602 int P[10][2]; 00603 int dmin_sum=0, mx4_sum=0, my4_sum=0; 00604 int same=1; 00605 const int stride= c->stride; 00606 uint8_t *mv_penalty= c->current_mv_penalty; 00607 00608 init_mv4_ref(c); 00609 00610 for(block=0; block<4; block++){ 00611 int mx4, my4; 00612 int pred_x4, pred_y4; 00613 int dmin4; 00614 static const int off[4]= {2, 1, 1, -1}; 00615 const int mot_stride = s->b8_stride; 00616 const int mot_xy = s->block_index[block]; 00617 00618 P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0]; 00619 P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1]; 00620 00621 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift); 00622 00623 /* special case for first line */ 00624 if (s->first_slice_line && block<2) { 00625 c->pred_x= pred_x4= P_LEFT[0]; 00626 c->pred_y= pred_y4= P_LEFT[1]; 00627 } else { 00628 P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0]; 00629 P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1]; 00630 P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0]; 00631 P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1]; 00632 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift); 00633 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift); 00634 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); 00635 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift); 00636 00637 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); 00638 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); 00639 00640 c->pred_x= pred_x4 = P_MEDIAN[0]; 00641 c->pred_y= pred_y4 = P_MEDIAN[1]; 00642 } 00643 P_MV1[0]= mx; 00644 P_MV1[1]= my; 00645 00646 dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift); 00647 00648 dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h); 00649 00650 if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ 00651 int dxy; 00652 const int offset= ((block&1) + (block>>1)*stride)*8; 00653 uint8_t *dest_y = c->scratchpad + offset; 00654 if(s->quarter_sample){ 00655 uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride; 00656 dxy = ((my4 & 3) << 2) | (mx4 & 3); 00657 00658 if(s->no_rounding) 00659 s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride); 00660 else 00661 s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride); 00662 }else{ 00663 uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride; 00664 dxy = ((my4 & 1) << 1) | (mx4 & 1); 00665 00666 if(s->no_rounding) 00667 s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h); 00668 else 00669 s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h); 00670 } 00671 dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor; 00672 }else 00673 dmin_sum+= dmin4; 00674 00675 if(s->quarter_sample){ 00676 mx4_sum+= mx4/2; 00677 my4_sum+= my4/2; 00678 }else{ 00679 mx4_sum+= mx4; 00680 my4_sum+= my4; 00681 } 00682 00683 s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4; 00684 s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4; 00685 00686 if(mx4 != mx || my4 != my) same=0; 00687 } 00688 00689 if(same) 00690 return INT_MAX; 00691 00692 if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ 00693 dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16); 00694 } 00695 00696 if(c->avctx->mb_cmp&FF_CMP_CHROMA){ 00697 int dxy; 00698 int mx, my; 00699 int offset; 00700 00701 mx= ff_h263_round_chroma(mx4_sum); 00702 my= ff_h263_round_chroma(my4_sum); 00703 dxy = ((my & 1) << 1) | (mx & 1); 00704 00705 offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize; 00706 00707 if(s->no_rounding){ 00708 s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8); 00709 s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); 00710 }else{ 00711 s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8); 00712 s->dsp.put_pixels_tab [1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8); 00713 } 00714 00715 dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad , s->uvlinesize, 8); 00716 dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8); 00717 } 00718 00719 c->pred_x= mx; 00720 c->pred_y= my; 00721 00722 switch(c->avctx->mb_cmp&0xFF){ 00723 /*case FF_CMP_SSE: 00724 return dmin_sum+ 32*s->qscale*s->qscale;*/ 00725 case FF_CMP_RD: 00726 return dmin_sum; 00727 default: 00728 return dmin_sum+ 11*c->mb_penalty_factor; 00729 } 00730 } 00731 00732 static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){ 00733 MotionEstContext * const c= &s->me; 00734 00735 c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize; 00736 c->src[1][0] = c->src[0][0] + s->linesize; 00737 if(c->flags & FLAG_CHROMA){ 00738 c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize; 00739 c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize; 00740 c->src[1][1] = c->src[0][1] + s->uvlinesize; 00741 c->src[1][2] = c->src[0][2] + s->uvlinesize; 00742 } 00743 } 00744 00745 static int interlaced_search(MpegEncContext *s, int ref_index, 00746 int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select) 00747 { 00748 MotionEstContext * const c= &s->me; 00749 const int size=0; 00750 const int h=8; 00751 int block; 00752 int P[10][2]; 00753 uint8_t * const mv_penalty= c->current_mv_penalty; 00754 int same=1; 00755 const int stride= 2*s->linesize; 00756 int dmin_sum= 0; 00757 const int mot_stride= s->mb_stride; 00758 const int xy= s->mb_x + s->mb_y*mot_stride; 00759 00760 c->ymin>>=1; 00761 c->ymax>>=1; 00762 c->stride<<=1; 00763 c->uvstride<<=1; 00764 init_interlaced_ref(s, ref_index); 00765 00766 for(block=0; block<2; block++){ 00767 int field_select; 00768 int best_dmin= INT_MAX; 00769 int best_field= -1; 00770 00771 for(field_select=0; field_select<2; field_select++){ 00772 int dmin, mx_i, my_i; 00773 int16_t (*mv_table)[2]= mv_tables[block][field_select]; 00774 00775 if(user_field_select){ 00776 assert(field_select==0 || field_select==1); 00777 assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1); 00778 if(field_select_tables[block][xy] != field_select) 00779 continue; 00780 } 00781 00782 P_LEFT[0] = mv_table[xy - 1][0]; 00783 P_LEFT[1] = mv_table[xy - 1][1]; 00784 if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1); 00785 00786 c->pred_x= P_LEFT[0]; 00787 c->pred_y= P_LEFT[1]; 00788 00789 if(!s->first_slice_line){ 00790 P_TOP[0] = mv_table[xy - mot_stride][0]; 00791 P_TOP[1] = mv_table[xy - mot_stride][1]; 00792 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0]; 00793 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1]; 00794 if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1); 00795 if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1); 00796 if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1); 00797 if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1); 00798 00799 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); 00800 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); 00801 } 00802 P_MV1[0]= mx; //FIXME not correct if block != field_select 00803 P_MV1[1]= my / 2; 00804 00805 dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1); 00806 00807 dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h); 00808 00809 mv_table[xy][0]= mx_i; 00810 mv_table[xy][1]= my_i; 00811 00812 if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){ 00813 int dxy; 00814 00815 //FIXME chroma ME 00816 uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride; 00817 dxy = ((my_i & 1) << 1) | (mx_i & 1); 00818 00819 if(s->no_rounding){ 00820 s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h); 00821 }else{ 00822 s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h); 00823 } 00824 dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h); 00825 dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor; 00826 }else 00827 dmin+= c->mb_penalty_factor; //field_select bits 00828 00829 dmin += field_select != block; //slightly prefer same field 00830 00831 if(dmin < best_dmin){ 00832 best_dmin= dmin; 00833 best_field= field_select; 00834 } 00835 } 00836 { 00837 int16_t (*mv_table)[2]= mv_tables[block][best_field]; 00838 00839 if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all 00840 if(mv_table[xy][1]&1) same=0; 00841 if(mv_table[xy][1]*2 != my) same=0; 00842 if(best_field != block) same=0; 00843 } 00844 00845 field_select_tables[block][xy]= best_field; 00846 dmin_sum += best_dmin; 00847 } 00848 00849 c->ymin<<=1; 00850 c->ymax<<=1; 00851 c->stride>>=1; 00852 c->uvstride>>=1; 00853 00854 if(same) 00855 return INT_MAX; 00856 00857 switch(c->avctx->mb_cmp&0xFF){ 00858 /*case FF_CMP_SSE: 00859 return dmin_sum+ 32*s->qscale*s->qscale;*/ 00860 case FF_CMP_RD: 00861 return dmin_sum; 00862 default: 00863 return dmin_sum+ 11*c->mb_penalty_factor; 00864 } 00865 } 00866 00867 static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){ 00868 int ymax= s->me.ymax>>interlaced; 00869 int ymin= s->me.ymin>>interlaced; 00870 00871 if(mv[0] < s->me.xmin) mv[0] = s->me.xmin; 00872 if(mv[0] > s->me.xmax) mv[0] = s->me.xmax; 00873 if(mv[1] < ymin) mv[1] = ymin; 00874 if(mv[1] > ymax) mv[1] = ymax; 00875 } 00876 00877 static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){ 00878 MotionEstContext * const c= &s->me; 00879 Picture *p= s->current_picture_ptr; 00880 int mb_xy= mb_x + mb_y*s->mb_stride; 00881 int xy= 2*mb_x + 2*mb_y*s->b8_stride; 00882 int mb_type= s->current_picture.mb_type[mb_xy]; 00883 int flags= c->flags; 00884 int shift= (flags&FLAG_QPEL) + 1; 00885 int mask= (1<<shift)-1; 00886 int x, y, i; 00887 int d=0; 00888 me_cmp_func cmpf= s->dsp.sse[0]; 00889 me_cmp_func chroma_cmpf= s->dsp.sse[1]; 00890 00891 if(p_type && USES_LIST(mb_type, 1)){ 00892 av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n"); 00893 return INT_MAX/2; 00894 } 00895 assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1)); 00896 00897 for(i=0; i<4; i++){ 00898 int xy= s->block_index[i]; 00899 clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type)); 00900 clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type)); 00901 } 00902 00903 if(IS_INTERLACED(mb_type)){ 00904 int xy2= xy + s->b8_stride; 00905 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA; 00906 c->stride<<=1; 00907 c->uvstride<<=1; 00908 00909 if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){ 00910 av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n"); 00911 return INT_MAX/2; 00912 } 00913 00914 if(USES_LIST(mb_type, 0)){ 00915 int field_select0= p->ref_index[0][4*mb_xy ]; 00916 int field_select1= p->ref_index[0][4*mb_xy+2]; 00917 assert(field_select0==0 ||field_select0==1); 00918 assert(field_select1==0 ||field_select1==1); 00919 init_interlaced_ref(s, 0); 00920 00921 if(p_type){ 00922 s->p_field_select_table[0][mb_xy]= field_select0; 00923 s->p_field_select_table[1][mb_xy]= field_select1; 00924 *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ]; 00925 *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2]; 00926 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I; 00927 }else{ 00928 s->b_field_select_table[0][0][mb_xy]= field_select0; 00929 s->b_field_select_table[0][1][mb_xy]= field_select1; 00930 *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ]; 00931 *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2]; 00932 s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I; 00933 } 00934 00935 x= p->motion_val[0][xy ][0]; 00936 y= p->motion_val[0][xy ][1]; 00937 d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags); 00938 x= p->motion_val[0][xy2][0]; 00939 y= p->motion_val[0][xy2][1]; 00940 d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags); 00941 } 00942 if(USES_LIST(mb_type, 1)){ 00943 int field_select0= p->ref_index[1][4*mb_xy ]; 00944 int field_select1= p->ref_index[1][4*mb_xy+2]; 00945 assert(field_select0==0 ||field_select0==1); 00946 assert(field_select1==0 ||field_select1==1); 00947 init_interlaced_ref(s, 2); 00948 00949 s->b_field_select_table[1][0][mb_xy]= field_select0; 00950 s->b_field_select_table[1][1][mb_xy]= field_select1; 00951 *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ]; 00952 *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2]; 00953 if(USES_LIST(mb_type, 0)){ 00954 s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I; 00955 }else{ 00956 s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I; 00957 } 00958 00959 x= p->motion_val[1][xy ][0]; 00960 y= p->motion_val[1][xy ][1]; 00961 d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags); 00962 x= p->motion_val[1][xy2][0]; 00963 y= p->motion_val[1][xy2][1]; 00964 d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags); 00965 //FIXME bidir scores 00966 } 00967 c->stride>>=1; 00968 c->uvstride>>=1; 00969 }else if(IS_8X8(mb_type)){ 00970 if(!(s->flags & CODEC_FLAG_4MV)){ 00971 av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n"); 00972 return INT_MAX/2; 00973 } 00974 cmpf= s->dsp.sse[1]; 00975 chroma_cmpf= s->dsp.sse[1]; 00976 init_mv4_ref(c); 00977 for(i=0; i<4; i++){ 00978 xy= s->block_index[i]; 00979 x= p->motion_val[0][xy][0]; 00980 y= p->motion_val[0][xy][1]; 00981 d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags); 00982 } 00983 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V; 00984 }else{ 00985 if(USES_LIST(mb_type, 0)){ 00986 if(p_type){ 00987 *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy]; 00988 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER; 00989 }else if(USES_LIST(mb_type, 1)){ 00990 *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy]; 00991 *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy]; 00992 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR; 00993 }else{ 00994 *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy]; 00995 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD; 00996 } 00997 x= p->motion_val[0][xy][0]; 00998 y= p->motion_val[0][xy][1]; 00999 d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags); 01000 }else if(USES_LIST(mb_type, 1)){ 01001 *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy]; 01002 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD; 01003 01004 x= p->motion_val[1][xy][0]; 01005 y= p->motion_val[1][xy][1]; 01006 d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags); 01007 }else 01008 s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA; 01009 } 01010 return d; 01011 } 01012 01013 void ff_estimate_p_frame_motion(MpegEncContext * s, 01014 int mb_x, int mb_y) 01015 { 01016 MotionEstContext * const c= &s->me; 01017 uint8_t *pix, *ppix; 01018 int sum, mx, my, dmin; 01019 int varc; 01020 int vard; 01021 int P[10][2]; 01022 const int shift= 1+s->quarter_sample; 01023 int mb_type=0; 01024 Picture * const pic= &s->current_picture; 01025 01026 init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0); 01027 01028 assert(s->quarter_sample==0 || s->quarter_sample==1); 01029 assert(s->linesize == c->stride); 01030 assert(s->uvlinesize == c->uvstride); 01031 01032 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp); 01033 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp); 01034 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp); 01035 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; 01036 01037 get_limits(s, 16*mb_x, 16*mb_y); 01038 c->skip=0; 01039 01040 /* intra / predictive decision */ 01041 pix = c->src[0][0]; 01042 sum = s->dsp.pix_sum(pix, s->linesize); 01043 varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500; 01044 01045 pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8; 01046 pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8; 01047 c->mb_var_sum_temp += (varc+128)>>8; 01048 01049 if(c->avctx->me_threshold){ 01050 vard= check_input_motion(s, mb_x, mb_y, 1); 01051 01052 if((vard+128)>>8 < c->avctx->me_threshold){ 01053 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); 01054 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; 01055 pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8; 01056 c->mc_mb_var_sum_temp += (vard+128)>>8; 01057 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); 01058 return; 01059 } 01060 if((vard+128)>>8 < c->avctx->mb_threshold) 01061 mb_type= s->mb_type[mb_x + mb_y*s->mb_stride]; 01062 } 01063 01064 switch(s->me_method) { 01065 case ME_ZERO: 01066 default: 01067 no_motion_search(s, &mx, &my); 01068 mx-= mb_x*16; 01069 my-= mb_y*16; 01070 dmin = 0; 01071 break; 01072 case ME_X1: 01073 case ME_EPZS: 01074 { 01075 const int mot_stride = s->b8_stride; 01076 const int mot_xy = s->block_index[0]; 01077 01078 P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0]; 01079 P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1]; 01080 01081 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift); 01082 01083 if(!s->first_slice_line) { 01084 P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0]; 01085 P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1]; 01086 P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0]; 01087 P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1]; 01088 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift); 01089 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift); 01090 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift); 01091 01092 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); 01093 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); 01094 01095 if(s->out_format == FMT_H263){ 01096 c->pred_x = P_MEDIAN[0]; 01097 c->pred_y = P_MEDIAN[1]; 01098 }else { /* mpeg1 at least */ 01099 c->pred_x= P_LEFT[0]; 01100 c->pred_y= P_LEFT[1]; 01101 } 01102 }else{ 01103 c->pred_x= P_LEFT[0]; 01104 c->pred_y= P_LEFT[1]; 01105 } 01106 01107 } 01108 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16); 01109 01110 break; 01111 } 01112 01113 /* At this point (mx,my) are full-pell and the relative displacement */ 01114 ppix = c->ref[0][0] + (my * s->linesize) + mx; 01115 01116 vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16); 01117 01118 pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8; 01119 // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 01120 c->mc_mb_var_sum_temp += (vard+128)>>8; 01121 01122 if(mb_type){ 01123 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); 01124 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; 01125 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); 01126 01127 if(mb_type == CANDIDATE_MB_TYPE_INTER){ 01128 c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); 01129 set_p_mv_tables(s, mx, my, 1); 01130 }else{ 01131 mx <<=shift; 01132 my <<=shift; 01133 } 01134 if(mb_type == CANDIDATE_MB_TYPE_INTER4V){ 01135 h263_mv4_search(s, mx, my, shift); 01136 01137 set_p_mv_tables(s, mx, my, 0); 01138 } 01139 if(mb_type == CANDIDATE_MB_TYPE_INTER_I){ 01140 interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1); 01141 } 01142 }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ 01143 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); 01144 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; 01145 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); 01146 01147 if (vard*2 + 200*256 > varc) 01148 mb_type|= CANDIDATE_MB_TYPE_INTRA; 01149 if (varc*2 + 200*256 > vard || s->qscale > 24){ 01150 // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){ 01151 mb_type|= CANDIDATE_MB_TYPE_INTER; 01152 c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); 01153 if(s->flags&CODEC_FLAG_MV0) 01154 if(mx || my) 01155 mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference 01156 }else{ 01157 mx <<=shift; 01158 my <<=shift; 01159 } 01160 if((s->flags&CODEC_FLAG_4MV) 01161 && !c->skip && varc>50<<8 && vard>10<<8){ 01162 if(h263_mv4_search(s, mx, my, shift) < INT_MAX) 01163 mb_type|=CANDIDATE_MB_TYPE_INTER4V; 01164 01165 set_p_mv_tables(s, mx, my, 0); 01166 }else 01167 set_p_mv_tables(s, mx, my, 1); 01168 if((s->flags&CODEC_FLAG_INTERLACED_ME) 01169 && !c->skip){ //FIXME varc/d checks 01170 if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX) 01171 mb_type |= CANDIDATE_MB_TYPE_INTER_I; 01172 } 01173 }else{ 01174 int intra_score, i; 01175 mb_type= CANDIDATE_MB_TYPE_INTER; 01176 01177 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); 01178 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip) 01179 dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1); 01180 01181 if((s->flags&CODEC_FLAG_4MV) 01182 && !c->skip && varc>50<<8 && vard>10<<8){ 01183 int dmin4= h263_mv4_search(s, mx, my, shift); 01184 if(dmin4 < dmin){ 01185 mb_type= CANDIDATE_MB_TYPE_INTER4V; 01186 dmin=dmin4; 01187 } 01188 } 01189 if((s->flags&CODEC_FLAG_INTERLACED_ME) 01190 && !c->skip){ //FIXME varc/d checks 01191 int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0); 01192 if(dmin_i < dmin){ 01193 mb_type = CANDIDATE_MB_TYPE_INTER_I; 01194 dmin= dmin_i; 01195 } 01196 } 01197 01198 // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin; 01199 set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V); 01200 01201 /* get intra luma score */ 01202 if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){ 01203 intra_score= varc - 500; 01204 }else{ 01205 unsigned mean = (sum+128)>>8; 01206 mean*= 0x01010101; 01207 01208 for(i=0; i<16; i++){ 01209 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean; 01210 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean; 01211 *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean; 01212 *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean; 01213 } 01214 01215 intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16); 01216 } 01217 #if 0 //FIXME 01218 /* get chroma score */ 01219 if(c->avctx->mb_cmp&FF_CMP_CHROMA){ 01220 for(i=1; i<3; i++){ 01221 uint8_t *dest_c; 01222 int mean; 01223 01224 if(s->out_format == FMT_H263){ 01225 mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;) 01226 }else{ 01227 mean= (s->last_dc[i] + 4)>>3; 01228 } 01229 dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8; 01230 01231 mean*= 0x01010101; 01232 for(i=0; i<8; i++){ 01233 *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean; 01234 *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean; 01235 } 01236 01237 intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize); 01238 } 01239 } 01240 #endif 01241 intra_score += c->mb_penalty_factor*16; 01242 01243 if(intra_score < dmin){ 01244 mb_type= CANDIDATE_MB_TYPE_INTRA; 01245 s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup 01246 }else 01247 s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0; 01248 01249 { 01250 int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100); 01251 int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20; 01252 c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score); 01253 } 01254 } 01255 01256 s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type; 01257 } 01258 01259 int ff_pre_estimate_p_frame_motion(MpegEncContext * s, 01260 int mb_x, int mb_y) 01261 { 01262 MotionEstContext * const c= &s->me; 01263 int mx, my, dmin; 01264 int P[10][2]; 01265 const int shift= 1+s->quarter_sample; 01266 const int xy= mb_x + mb_y*s->mb_stride; 01267 init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0); 01268 01269 assert(s->quarter_sample==0 || s->quarter_sample==1); 01270 01271 c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp); 01272 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; 01273 01274 get_limits(s, 16*mb_x, 16*mb_y); 01275 c->skip=0; 01276 01277 P_LEFT[0] = s->p_mv_table[xy + 1][0]; 01278 P_LEFT[1] = s->p_mv_table[xy + 1][1]; 01279 01280 if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift); 01281 01282 /* special case for first line */ 01283 if (s->first_slice_line) { 01284 c->pred_x= P_LEFT[0]; 01285 c->pred_y= P_LEFT[1]; 01286 P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]= 01287 P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME 01288 } else { 01289 P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0]; 01290 P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1]; 01291 P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0]; 01292 P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1]; 01293 if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift); 01294 if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift); 01295 if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift); 01296 01297 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); 01298 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); 01299 01300 c->pred_x = P_MEDIAN[0]; 01301 c->pred_y = P_MEDIAN[1]; 01302 } 01303 01304 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16); 01305 01306 s->p_mv_table[xy][0] = mx<<shift; 01307 s->p_mv_table[xy][1] = my<<shift; 01308 01309 return dmin; 01310 } 01311 01312 static int ff_estimate_motion_b(MpegEncContext * s, 01313 int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code) 01314 { 01315 MotionEstContext * const c= &s->me; 01316 int mx, my, dmin; 01317 int P[10][2]; 01318 const int shift= 1+s->quarter_sample; 01319 const int mot_stride = s->mb_stride; 01320 const int mot_xy = mb_y*mot_stride + mb_x; 01321 uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV; 01322 int mv_scale; 01323 01324 c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp); 01325 c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp); 01326 c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp); 01327 c->current_mv_penalty= mv_penalty; 01328 01329 get_limits(s, 16*mb_x, 16*mb_y); 01330 01331 switch(s->me_method) { 01332 case ME_ZERO: 01333 default: 01334 no_motion_search(s, &mx, &my); 01335 dmin = 0; 01336 mx-= mb_x*16; 01337 my-= mb_y*16; 01338 break; 01339 case ME_X1: 01340 case ME_EPZS: 01341 { 01342 P_LEFT[0] = mv_table[mot_xy - 1][0]; 01343 P_LEFT[1] = mv_table[mot_xy - 1][1]; 01344 01345 if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift); 01346 01347 /* special case for first line */ 01348 if (!s->first_slice_line) { 01349 P_TOP[0] = mv_table[mot_xy - mot_stride ][0]; 01350 P_TOP[1] = mv_table[mot_xy - mot_stride ][1]; 01351 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0]; 01352 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1]; 01353 if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift); 01354 if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift); 01355 if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift); 01356 01357 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); 01358 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); 01359 } 01360 c->pred_x= P_LEFT[0]; 01361 c->pred_y= P_LEFT[1]; 01362 } 01363 01364 if(mv_table == s->b_forw_mv_table){ 01365 mv_scale= (s->pb_time<<16) / (s->pp_time<<shift); 01366 }else{ 01367 mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift); 01368 } 01369 01370 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16); 01371 01372 break; 01373 } 01374 01375 dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16); 01376 01377 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip) 01378 dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1); 01379 01380 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my); 01381 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type; 01382 mv_table[mot_xy][0]= mx; 01383 mv_table[mot_xy][1]= my; 01384 01385 return dmin; 01386 } 01387 01388 static inline int check_bidir_mv(MpegEncContext * s, 01389 int motion_fx, int motion_fy, 01390 int motion_bx, int motion_by, 01391 int pred_fx, int pred_fy, 01392 int pred_bx, int pred_by, 01393 int size, int h) 01394 { 01395 //FIXME optimize? 01396 //FIXME better f_code prediction (max mv & distance) 01397 //FIXME pointers 01398 MotionEstContext * const c= &s->me; 01399 uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame 01400 uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame 01401 int stride= c->stride; 01402 uint8_t *dest_y = c->scratchpad; 01403 uint8_t *ptr; 01404 int dxy; 01405 int src_x, src_y; 01406 int fbmin; 01407 uint8_t **src_data= c->src[0]; 01408 uint8_t **ref_data= c->ref[0]; 01409 uint8_t **ref2_data= c->ref[2]; 01410 01411 if(s->quarter_sample){ 01412 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3); 01413 src_x = motion_fx >> 2; 01414 src_y = motion_fy >> 2; 01415 01416 ptr = ref_data[0] + (src_y * stride) + src_x; 01417 s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride); 01418 01419 dxy = ((motion_by & 3) << 2) | (motion_bx & 3); 01420 src_x = motion_bx >> 2; 01421 src_y = motion_by >> 2; 01422 01423 ptr = ref2_data[0] + (src_y * stride) + src_x; 01424 s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride); 01425 }else{ 01426 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1); 01427 src_x = motion_fx >> 1; 01428 src_y = motion_fy >> 1; 01429 01430 ptr = ref_data[0] + (src_y * stride) + src_x; 01431 s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h); 01432 01433 dxy = ((motion_by & 1) << 1) | (motion_bx & 1); 01434 src_x = motion_bx >> 1; 01435 src_y = motion_by >> 1; 01436 01437 ptr = ref2_data[0] + (src_y * stride) + src_x; 01438 s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h); 01439 } 01440 01441 fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor 01442 +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor 01443 + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic 01444 01445 if(c->avctx->mb_cmp&FF_CMP_CHROMA){ 01446 } 01447 //FIXME CHROMA !!! 01448 01449 return fbmin; 01450 } 01451 01452 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/ 01453 static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y) 01454 { 01455 MotionEstContext * const c= &s->me; 01456 const int mot_stride = s->mb_stride; 01457 const int xy = mb_y *mot_stride + mb_x; 01458 int fbmin; 01459 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0]; 01460 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1]; 01461 int pred_bx= s->b_bidir_back_mv_table[xy-1][0]; 01462 int pred_by= s->b_bidir_back_mv_table[xy-1][1]; 01463 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0]; 01464 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1]; 01465 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0]; 01466 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1]; 01467 const int flags= c->sub_flags; 01468 const int qpel= flags&FLAG_QPEL; 01469 const int shift= 1+qpel; 01470 const int xmin= c->xmin<<shift; 01471 const int ymin= c->ymin<<shift; 01472 const int xmax= c->xmax<<shift; 01473 const int ymax= c->ymax<<shift; 01474 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by)) 01475 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by)) 01476 int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by); 01477 uint8_t map[256]; 01478 01479 memset(map,0,sizeof(map)); 01480 map[hashidx&255] = 1; 01481 01482 fbmin= check_bidir_mv(s, motion_fx, motion_fy, 01483 motion_bx, motion_by, 01484 pred_fx, pred_fy, 01485 pred_bx, pred_by, 01486 0, 16); 01487 01488 if(s->avctx->bidir_refine){ 01489 int end; 01490 static const uint8_t limittab[5]={0,8,32,64,80}; 01491 const int limit= limittab[s->avctx->bidir_refine]; 01492 static const int8_t vect[][4]={ 01493 { 0, 0, 0, 1}, { 0, 0, 0,-1}, { 0, 0, 1, 0}, { 0, 0,-1, 0}, { 0, 1, 0, 0}, { 0,-1, 0, 0}, { 1, 0, 0, 0}, {-1, 0, 0, 0}, 01494 01495 { 0, 0, 1, 1}, { 0, 0,-1,-1}, { 0, 1, 1, 0}, { 0,-1,-1, 0}, { 1, 1, 0, 0}, {-1,-1, 0, 0}, { 1, 0, 0, 1}, {-1, 0, 0,-1}, 01496 { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0}, 01497 { 0, 0,-1, 1}, { 0, 0, 1,-1}, { 0,-1, 1, 0}, { 0, 1,-1, 0}, {-1, 1, 0, 0}, { 1,-1, 0, 0}, { 1, 0, 0,-1}, {-1, 0, 0, 1}, 01498 { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0}, 01499 01500 { 0, 1, 1, 1}, { 0,-1,-1,-1}, { 1, 1, 1, 0}, {-1,-1,-1, 0}, { 1, 1, 0, 1}, {-1,-1, 0,-1}, { 1, 0, 1, 1}, {-1, 0,-1,-1}, 01501 { 0,-1, 1, 1}, { 0, 1,-1,-1}, {-1, 1, 1, 0}, { 1,-1,-1, 0}, { 1, 1, 0,-1}, {-1,-1, 0, 1}, { 1, 0,-1, 1}, {-1, 0, 1,-1}, 01502 { 0, 1,-1, 1}, { 0,-1, 1,-1}, { 1,-1, 1, 0}, {-1, 1,-1, 0}, {-1, 1, 0, 1}, { 1,-1, 0,-1}, { 1, 0, 1,-1}, {-1, 0,-1, 1}, 01503 { 0, 1, 1,-1}, { 0,-1,-1, 1}, { 1, 1,-1, 0}, {-1,-1, 1, 0}, { 1,-1, 0, 1}, {-1, 1, 0,-1}, {-1, 0, 1, 1}, { 1, 0,-1,-1}, 01504 01505 { 1, 1, 1, 1}, {-1,-1,-1,-1}, 01506 { 1, 1, 1,-1}, {-1,-1,-1, 1}, { 1, 1,-1, 1}, {-1,-1, 1,-1}, { 1,-1, 1, 1}, {-1, 1,-1,-1}, {-1, 1, 1, 1}, { 1,-1,-1,-1}, 01507 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1}, 01508 }; 01509 static const uint8_t hash[]={ 01510 HASH8( 0, 0, 0, 1), HASH8( 0, 0, 0,-1), HASH8( 0, 0, 1, 0), HASH8( 0, 0,-1, 0), HASH8( 0, 1, 0, 0), HASH8( 0,-1, 0, 0), HASH8( 1, 0, 0, 0), HASH8(-1, 0, 0, 0), 01511 01512 HASH8( 0, 0, 1, 1), HASH8( 0, 0,-1,-1), HASH8( 0, 1, 1, 0), HASH8( 0,-1,-1, 0), HASH8( 1, 1, 0, 0), HASH8(-1,-1, 0, 0), HASH8( 1, 0, 0, 1), HASH8(-1, 0, 0,-1), 01513 HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0), 01514 HASH8( 0, 0,-1, 1), HASH8( 0, 0, 1,-1), HASH8( 0,-1, 1, 0), HASH8( 0, 1,-1, 0), HASH8(-1, 1, 0, 0), HASH8( 1,-1, 0, 0), HASH8( 1, 0, 0,-1), HASH8(-1, 0, 0, 1), 01515 HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0), 01516 01517 HASH8( 0, 1, 1, 1), HASH8( 0,-1,-1,-1), HASH8( 1, 1, 1, 0), HASH8(-1,-1,-1, 0), HASH8( 1, 1, 0, 1), HASH8(-1,-1, 0,-1), HASH8( 1, 0, 1, 1), HASH8(-1, 0,-1,-1), 01518 HASH8( 0,-1, 1, 1), HASH8( 0, 1,-1,-1), HASH8(-1, 1, 1, 0), HASH8( 1,-1,-1, 0), HASH8( 1, 1, 0,-1), HASH8(-1,-1, 0, 1), HASH8( 1, 0,-1, 1), HASH8(-1, 0, 1,-1), 01519 HASH8( 0, 1,-1, 1), HASH8( 0,-1, 1,-1), HASH8( 1,-1, 1, 0), HASH8(-1, 1,-1, 0), HASH8(-1, 1, 0, 1), HASH8( 1,-1, 0,-1), HASH8( 1, 0, 1,-1), HASH8(-1, 0,-1, 1), 01520 HASH8( 0, 1, 1,-1), HASH8( 0,-1,-1, 1), HASH8( 1, 1,-1, 0), HASH8(-1,-1, 1, 0), HASH8( 1,-1, 0, 1), HASH8(-1, 1, 0,-1), HASH8(-1, 0, 1, 1), HASH8( 1, 0,-1,-1), 01521 01522 HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1), 01523 HASH8( 1, 1, 1,-1), HASH8(-1,-1,-1, 1), HASH8( 1, 1,-1, 1), HASH8(-1,-1, 1,-1), HASH8( 1,-1, 1, 1), HASH8(-1, 1,-1,-1), HASH8(-1, 1, 1, 1), HASH8( 1,-1,-1,-1), 01524 HASH8( 1, 1,-1,-1), HASH8(-1,-1, 1, 1), HASH8( 1,-1,-1, 1), HASH8(-1, 1, 1,-1), HASH8( 1,-1, 1,-1), HASH8(-1, 1,-1, 1), 01525 }; 01526 01527 #define CHECK_BIDIR(fx,fy,bx,by)\ 01528 if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\ 01529 &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\ 01530 &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\ 01531 int score;\ 01532 map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\ 01533 score= check_bidir_mv(s, motion_fx+fx, motion_fy+fy, motion_bx+bx, motion_by+by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);\ 01534 if(score < fbmin){\ 01535 hashidx += HASH(fx,fy,bx,by);\ 01536 fbmin= score;\ 01537 motion_fx+=fx;\ 01538 motion_fy+=fy;\ 01539 motion_bx+=bx;\ 01540 motion_by+=by;\ 01541 end=0;\ 01542 }\ 01543 } 01544 #define CHECK_BIDIR2(a,b,c,d)\ 01545 CHECK_BIDIR(a,b,c,d)\ 01546 CHECK_BIDIR(-(a),-(b),-(c),-(d)) 01547 01548 do{ 01549 int i; 01550 int borderdist=0; 01551 end=1; 01552 01553 CHECK_BIDIR2(0,0,0,1) 01554 CHECK_BIDIR2(0,0,1,0) 01555 CHECK_BIDIR2(0,1,0,0) 01556 CHECK_BIDIR2(1,0,0,0) 01557 01558 for(i=8; i<limit; i++){ 01559 int fx= motion_fx+vect[i][0]; 01560 int fy= motion_fy+vect[i][1]; 01561 int bx= motion_bx+vect[i][2]; 01562 int by= motion_by+vect[i][3]; 01563 if(borderdist<=0){ 01564 int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin); 01565 int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin); 01566 if((a|b) < 0) 01567 map[(hashidx+hash[i])&255] = 1; 01568 } 01569 if(!map[(hashidx+hash[i])&255]){ 01570 int score; 01571 map[(hashidx+hash[i])&255] = 1; 01572 score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16); 01573 if(score < fbmin){ 01574 hashidx += hash[i]; 01575 fbmin= score; 01576 motion_fx=fx; 01577 motion_fy=fy; 01578 motion_bx=bx; 01579 motion_by=by; 01580 end=0; 01581 borderdist--; 01582 if(borderdist<=0){ 01583 int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin); 01584 int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin); 01585 borderdist= FFMIN(a,b); 01586 } 01587 } 01588 } 01589 } 01590 }while(!end); 01591 } 01592 01593 s->b_bidir_forw_mv_table[xy][0]= motion_fx; 01594 s->b_bidir_forw_mv_table[xy][1]= motion_fy; 01595 s->b_bidir_back_mv_table[xy][0]= motion_bx; 01596 s->b_bidir_back_mv_table[xy][1]= motion_by; 01597 01598 return fbmin; 01599 } 01600 01601 static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y) 01602 { 01603 MotionEstContext * const c= &s->me; 01604 int P[10][2]; 01605 const int mot_stride = s->mb_stride; 01606 const int mot_xy = mb_y*mot_stride + mb_x; 01607 const int shift= 1+s->quarter_sample; 01608 int dmin, i; 01609 const int time_pp= s->pp_time; 01610 const int time_pb= s->pb_time; 01611 int mx, my, xmin, xmax, ymin, ymax; 01612 int16_t (*mv_table)[2]= s->b_direct_mv_table; 01613 01614 c->current_mv_penalty= c->mv_penalty[1] + MAX_MV; 01615 ymin= xmin=(-32)>>shift; 01616 ymax= xmax= 31>>shift; 01617 01618 if(IS_8X8(s->next_picture.mb_type[mot_xy])){ 01619 s->mv_type= MV_TYPE_8X8; 01620 }else{ 01621 s->mv_type= MV_TYPE_16X16; 01622 } 01623 01624 for(i=0; i<4; i++){ 01625 int index= s->block_index[i]; 01626 int min, max; 01627 01628 c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0]; 01629 c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1]; 01630 c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3)); 01631 c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3)); 01632 // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3); 01633 // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3); 01634 01635 max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift; 01636 min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift; 01637 max+= 16*mb_x + 1; // +-1 is for the simpler rounding 01638 min+= 16*mb_x - 1; 01639 xmax= FFMIN(xmax, s->width - max); 01640 xmin= FFMAX(xmin, - 16 - min); 01641 01642 max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift; 01643 min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift; 01644 max+= 16*mb_y + 1; // +-1 is for the simpler rounding 01645 min+= 16*mb_y - 1; 01646 ymax= FFMIN(ymax, s->height - max); 01647 ymin= FFMAX(ymin, - 16 - min); 01648 01649 if(s->mv_type == MV_TYPE_16X16) break; 01650 } 01651 01652 assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16); 01653 01654 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){ 01655 s->b_direct_mv_table[mot_xy][0]= 0; 01656 s->b_direct_mv_table[mot_xy][1]= 0; 01657 01658 return 256*256*256*64; 01659 } 01660 01661 c->xmin= xmin; 01662 c->ymin= ymin; 01663 c->xmax= xmax; 01664 c->ymax= ymax; 01665 c->flags |= FLAG_DIRECT; 01666 c->sub_flags |= FLAG_DIRECT; 01667 c->pred_x=0; 01668 c->pred_y=0; 01669 01670 P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift); 01671 P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift); 01672 01673 /* special case for first line */ 01674 if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped 01675 P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift); 01676 P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift); 01677 P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift); 01678 P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift); 01679 01680 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]); 01681 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]); 01682 } 01683 01684 dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16); 01685 if(c->sub_flags&FLAG_QPEL) 01686 dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); 01687 else 01688 dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16); 01689 01690 if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip) 01691 dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1); 01692 01693 get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed 01694 01695 mv_table[mot_xy][0]= mx; 01696 mv_table[mot_xy][1]= my; 01697 c->flags &= ~FLAG_DIRECT; 01698 c->sub_flags &= ~FLAG_DIRECT; 01699 01700 return dmin; 01701 } 01702 01703 void ff_estimate_b_frame_motion(MpegEncContext * s, 01704 int mb_x, int mb_y) 01705 { 01706 MotionEstContext * const c= &s->me; 01707 const int penalty_factor= c->mb_penalty_factor; 01708 int fmin, bmin, dmin, fbmin, bimin, fimin; 01709 int type=0; 01710 const int xy = mb_y*s->mb_stride + mb_x; 01711 init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2); 01712 01713 get_limits(s, 16*mb_x, 16*mb_y); 01714 01715 c->skip=0; 01716 01717 if(s->codec_id == CODEC_ID_MPEG4 && s->next_picture.mbskip_table[xy]){ 01718 int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0 01719 01720 score= ((unsigned)(score*score + 128*256))>>16; 01721 c->mc_mb_var_sum_temp += score; 01722 s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE 01723 s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0; 01724 01725 return; 01726 } 01727 01728 if(c->avctx->me_threshold){ 01729 int vard= check_input_motion(s, mb_x, mb_y, 0); 01730 01731 if((vard+128)>>8 < c->avctx->me_threshold){ 01732 // pix = c->src[0][0]; 01733 // sum = s->dsp.pix_sum(pix, s->linesize); 01734 // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500; 01735 01736 // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8; 01737 s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8; 01738 /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8; 01739 c->mb_var_sum_temp += (varc+128)>>8;*/ 01740 c->mc_mb_var_sum_temp += (vard+128)>>8; 01741 /* if (vard <= 64<<8 || vard < varc) { 01742 c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc); 01743 }else{ 01744 c->scene_change_score+= s->qscale * s->avctx->scenechange_factor; 01745 }*/ 01746 return; 01747 } 01748 if((vard+128)>>8 < c->avctx->mb_threshold){ 01749 type= s->mb_type[mb_y*s->mb_stride + mb_x]; 01750 if(type == CANDIDATE_MB_TYPE_DIRECT){ 01751 direct_search(s, mb_x, mb_y); 01752 } 01753 if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){ 01754 c->skip=0; 01755 ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code); 01756 } 01757 if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){ 01758 c->skip=0; 01759 ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code); 01760 } 01761 if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){ 01762 c->skip=0; 01763 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; 01764 interlaced_search(s, 0, 01765 s->b_field_mv_table[0], s->b_field_select_table[0], 01766 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1); 01767 } 01768 if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){ 01769 c->skip=0; 01770 c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV; 01771 interlaced_search(s, 2, 01772 s->b_field_mv_table[1], s->b_field_select_table[1], 01773 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1); 01774 } 01775 return; 01776 } 01777 } 01778 01779 if (s->codec_id == CODEC_ID_MPEG4) 01780 dmin= direct_search(s, mb_x, mb_y); 01781 else 01782 dmin= INT_MAX; 01783 //FIXME penalty stuff for non mpeg4 01784 c->skip=0; 01785 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor; 01786 01787 c->skip=0; 01788 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor; 01789 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]); 01790 01791 c->skip=0; 01792 fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor; 01793 //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin); 01794 01795 if(s->flags & CODEC_FLAG_INTERLACED_ME){ 01796 //FIXME mb type penalty 01797 c->skip=0; 01798 c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; 01799 fimin= interlaced_search(s, 0, 01800 s->b_field_mv_table[0], s->b_field_select_table[0], 01801 s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0); 01802 c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV; 01803 bimin= interlaced_search(s, 2, 01804 s->b_field_mv_table[1], s->b_field_select_table[1], 01805 s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0); 01806 }else 01807 fimin= bimin= INT_MAX; 01808 01809 { 01810 int score= fmin; 01811 type = CANDIDATE_MB_TYPE_FORWARD; 01812 01813 if (dmin <= score){ 01814 score = dmin; 01815 type = CANDIDATE_MB_TYPE_DIRECT; 01816 } 01817 if(bmin<score){ 01818 score=bmin; 01819 type= CANDIDATE_MB_TYPE_BACKWARD; 01820 } 01821 if(fbmin<score){ 01822 score=fbmin; 01823 type= CANDIDATE_MB_TYPE_BIDIR; 01824 } 01825 if(fimin<score){ 01826 score=fimin; 01827 type= CANDIDATE_MB_TYPE_FORWARD_I; 01828 } 01829 if(bimin<score){ 01830 score=bimin; 01831 type= CANDIDATE_MB_TYPE_BACKWARD_I; 01832 } 01833 01834 score= ((unsigned)(score*score + 128*256))>>16; 01835 c->mc_mb_var_sum_temp += score; 01836 s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE 01837 } 01838 01839 if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){ 01840 type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT; 01841 if(fimin < INT_MAX) 01842 type |= CANDIDATE_MB_TYPE_FORWARD_I; 01843 if(bimin < INT_MAX) 01844 type |= CANDIDATE_MB_TYPE_BACKWARD_I; 01845 if(fimin < INT_MAX && bimin < INT_MAX){ 01846 type |= CANDIDATE_MB_TYPE_BIDIR_I; 01847 } 01848 //FIXME something smarter 01849 if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB 01850 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]) 01851 type |= CANDIDATE_MB_TYPE_DIRECT0; 01852 #if 0 01853 if(s->out_format == FMT_MPEG1) 01854 type |= CANDIDATE_MB_TYPE_INTRA; 01855 #endif 01856 } 01857 01858 s->mb_type[mb_y*s->mb_stride + mb_x]= type; 01859 } 01860 01861 /* find best f_code for ME which do unlimited searches */ 01862 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type) 01863 { 01864 if(s->me_method>=ME_EPZS){ 01865 int score[8]; 01866 int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2); 01867 uint8_t * fcode_tab= s->fcode_tab; 01868 int best_fcode=-1; 01869 int best_score=-10000000; 01870 01871 if(s->msmpeg4_version) 01872 range= FFMIN(range, 16); 01873 else if(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL) 01874 range= FFMIN(range, 256); 01875 01876 for(i=0; i<8; i++) score[i]= s->mb_num*(8-i); 01877 01878 for(y=0; y<s->mb_height; y++){ 01879 int x; 01880 int xy= y*s->mb_stride; 01881 for(x=0; x<s->mb_width; x++){ 01882 if(s->mb_type[xy] & type){ 01883 int mx= mv_table[xy][0]; 01884 int my= mv_table[xy][1]; 01885 int fcode= FFMAX(fcode_tab[mx + MAX_MV], 01886 fcode_tab[my + MAX_MV]); 01887 int j; 01888 01889 if(mx >= range || mx < -range || 01890 my >= range || my < -range) 01891 continue; 01892 01893 for(j=0; j<fcode && j<8; j++){ 01894 if(s->pict_type==AV_PICTURE_TYPE_B || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy]) 01895 score[j]-= 170; 01896 } 01897 } 01898 xy++; 01899 } 01900 } 01901 01902 for(i=1; i<8; i++){ 01903 if(score[i] > best_score){ 01904 best_score= score[i]; 01905 best_fcode= i; 01906 } 01907 // printf("%d %d\n", i, score[i]); 01908 } 01909 01910 // printf("fcode: %d type: %d\n", i, s->pict_type); 01911 return best_fcode; 01912 /* for(i=0; i<=MAX_FCODE; i++){ 01913 printf("%d ", mv_num[i]); 01914 } 01915 printf("\n");*/ 01916 }else{ 01917 return 1; 01918 } 01919 } 01920 01921 void ff_fix_long_p_mvs(MpegEncContext * s) 01922 { 01923 MotionEstContext * const c= &s->me; 01924 const int f_code= s->f_code; 01925 int y, range; 01926 assert(s->pict_type==AV_PICTURE_TYPE_P); 01927 01928 range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code); 01929 01930 assert(range <= 16 || !s->msmpeg4_version); 01931 assert(range <=256 || !(s->codec_id == CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)); 01932 01933 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range; 01934 01935 //printf("%d no:%d %d//\n", clip, noclip, f_code); 01936 if(s->flags&CODEC_FLAG_4MV){ 01937 const int wrap= s->b8_stride; 01938 01939 /* clip / convert to intra 8x8 type MVs */ 01940 for(y=0; y<s->mb_height; y++){ 01941 int xy= y*2*wrap; 01942 int i= y*s->mb_stride; 01943 int x; 01944 01945 for(x=0; x<s->mb_width; x++){ 01946 if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){ 01947 int block; 01948 for(block=0; block<4; block++){ 01949 int off= (block& 1) + (block>>1)*wrap; 01950 int mx= s->current_picture.motion_val[0][ xy + off ][0]; 01951 int my= s->current_picture.motion_val[0][ xy + off ][1]; 01952 01953 if( mx >=range || mx <-range 01954 || my >=range || my <-range){ 01955 s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V; 01956 s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA; 01957 s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA; 01958 } 01959 } 01960 } 01961 xy+=2; 01962 i++; 01963 } 01964 } 01965 } 01966 } 01967 01972 void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select, 01973 int16_t (*mv_table)[2], int f_code, int type, int truncate) 01974 { 01975 MotionEstContext * const c= &s->me; 01976 int y, h_range, v_range; 01977 01978 // RAL: 8 in MPEG-1, 16 in MPEG-4 01979 int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code); 01980 01981 if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range; 01982 01983 h_range= range; 01984 v_range= field_select_table ? range>>1 : range; 01985 01986 /* clip / convert to intra 16x16 type MVs */ 01987 for(y=0; y<s->mb_height; y++){ 01988 int x; 01989 int xy= y*s->mb_stride; 01990 for(x=0; x<s->mb_width; x++){ 01991 if (s->mb_type[xy] & type){ // RAL: "type" test added... 01992 if(field_select_table==NULL || field_select_table[xy] == field_select){ 01993 if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range 01994 || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){ 01995 01996 if(truncate){ 01997 if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1; 01998 else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range; 01999 if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1; 02000 else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range; 02001 }else{ 02002 s->mb_type[xy] &= ~type; 02003 s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA; 02004 mv_table[xy][0]= 02005 mv_table[xy][1]= 0; 02006 } 02007 } 02008 } 02009 } 02010 xy++; 02011 } 02012 } 02013 }