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