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