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