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