43 #define P_TOPRIGHT P[3]
48 int *mx_ptr,
int *my_ptr,
int dmin,
49 int src_index,
int ref_index,
76 #define FLAG_QPEL 1 //must be 1
88 c->
src[0][i]= src [i] + offset[i];
89 c->
ref[0][i]= ref [i] + offset[i];
93 c->
ref[ref_index][i]= ref2[i] + offset[i];
105 const int size,
const int h,
int ref_index,
int src_index,
109 const int hx= subx + (x<<(1+qpel));
110 const int hy= suby + (y<<(1+qpel));
111 uint8_t *
const *
const ref= c->
ref[ref_index];
115 av_assert2(x >= c->
xmin && hx <= c->xmax<<(qpel+1) && y >= c->
ymin && hy <= c->ymax<<(qpel+1));
116 if(x >= c->
xmin && hx <= c->xmax<<(qpel+1) && y >= c->
ymin && hy <= c->ymax<<(qpel+1)){
119 const int mask= 2*qpel+1;
127 int fxy= (fx&
mask) + ((fy&mask)<<(qpel+1));
128 int bxy= (bx&
mask) + ((by&mask)<<(qpel+1));
144 int fxy= (fx&
mask) + ((fy&mask)<<(qpel+1));
145 int bxy= (bx&
mask) + ((by&mask)<<(qpel+1));
170 d = cmp_func(s, c->
temp, src[0], stride, 16);
177 const int size,
const int h,
int ref_index,
int src_index,
182 const int dxy= subx + (suby<<(1+qpel));
183 const int hx= subx + (x<<(1+qpel));
184 const int hy= suby + (y<<(1+qpel));
185 uint8_t *
const *
const ref= c->
ref[ref_index];
198 uvdxy= (cx&1) + 2*(cy&1);
204 uvdxy= dxy | (x&1) | (2*(y&1));
206 d = cmp_func(s, c->
temp, src[0], stride, h);
208 d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
210 uvdxy= (x&1) + 2*(y&1);
214 c->
hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
215 c->
hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
216 d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
217 d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
223 int ref_index,
int src_index,
225 return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
229 const int size,
const int h,
int ref_index,
int src_index,
232 return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&
FLAG_QPEL);
234 return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&
FLAG_CHROMA);
239 const int size,
const int h,
int ref_index,
int src_index,
242 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&
FLAG_QPEL);
244 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);
252 const int size,
const int h,
int ref_index,
int src_index,
256 && flags==0 && h==16 && size==0 && subx==0 && suby==0){
257 return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
259 && subx==0 && suby==0){
260 return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
262 return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
267 const int size,
const int h,
int ref_index,
int src_index,
270 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
272 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&
FLAG_CHROMA);
277 const int size,
const int h,
int ref_index,
int src_index,
280 return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
282 return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&
FLAG_CHROMA);
306 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");
312 if(cache_size < 2*dia_size && !c->
stride){
376 #define CHECK_SAD_HALF_MV(suffix, x, y) \
378 d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
379 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
380 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
384 int *mx_ptr,
int *my_ptr,
int dmin,
385 int src_index,
int ref_index,
404 pix = c->
src[src_index][0];
408 ptr = c->
ref[ref_index][0] + (my *
stride) + mx;
412 if (mx > xmin && mx < xmax &&
413 my > ymin && my < ymax) {
418 const int l= score_map[(index- 1 )&(
ME_MAP_SIZE-1)];
532 c->
xmin = (x > 15) ? - 15 : 0;
533 c->
ymin = (y > 15) ? - 15 : 0;
542 if(!range || range > max_range)
555 c->
ref[1][0] = c->
ref[0][0] + 8;
557 c->
ref[3][0] = c->
ref[2][0] + 8;
558 c->
src[1][0] = c->
src[0][0] + 8;
560 c->
src[3][0] = c->
src[2][0] + 8;
570 int dmin_sum=0, mx4_sum=0, my4_sum=0, i;
578 for(block=0; block<4; block++){
580 int pred_x4, pred_y4;
582 static const int off[4]= {2, 1, 1, -1};
621 if(P[i][1] > (c->
ymax<<shift)) P[i][1]= (c->
ymax<<shift);
634 dxy = ((my4 & 3) << 2) | (mx4 & 3);
642 dxy = ((my4 & 1) << 1) | (mx4 & 1);
649 dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->
mb_penalty_factor;
664 if(mx4 != mx || my4 != my) same=0;
681 dxy = ((my & 1) << 1) | (mx & 1);
724 int16_t (*mv_tables[2][2])[2],
uint8_t *field_select_tables[2],
int mx,
int my,
int user_field_select)
736 const int xy= s->
mb_x + s->
mb_y*mot_stride;
744 for(block=0; block<2; block++){
746 int best_dmin= INT_MAX;
749 for(field_select=0; field_select<2; field_select++){
750 int dmin, mx_i, my_i;
751 int16_t (*mv_table)[2]= mv_tables[
block][field_select];
753 if(user_field_select){
754 av_assert1(field_select==0 || field_select==1);
755 av_assert1(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
756 if(field_select_tables[block][xy] != field_select)
760 P_LEFT[0] = mv_table[xy - 1][0];
761 P_LEFT[1] = mv_table[xy - 1][1];
768 P_TOP[0] = mv_table[xy - mot_stride][0];
769 P_TOP[1] = mv_table[xy - mot_stride][1];
770 P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
771 P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
783 dmin =
epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
785 dmin= c->
sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
787 mv_table[xy][0]= mx_i;
788 mv_table[xy][1]= my_i;
794 uint8_t *ref= c->
ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*
stride;
795 dxy = ((my_i & 1) << 1) | (mx_i & 1);
807 dmin += field_select !=
block;
809 if(dmin < best_dmin){
811 best_field= field_select;
815 int16_t (*mv_table)[2]= mv_tables[
block][best_field];
817 if(mv_table[xy][0] != mx) same=0;
818 if(mv_table[xy][1]&1) same=0;
819 if(mv_table[xy][1]*2 != my) same=0;
820 if(best_field != block) same=0;
823 field_select_tables[
block][xy]= best_field;
824 dmin_sum += best_dmin;
851 return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
874 int sum, mx, my, dmin;
986 if (vard*2 + 200*256 > varc)
988 if (varc*2 + 200*256 > vard || s->
qscale > 24){
1000 && !c->
skip && varc>50<<8 && vard>10<<8){
1021 && !c->
skip && varc>50<<8 && vard>10<<8){
1041 intra_score= varc - 500;
1043 unsigned mean = (sum+128)>>8;
1046 for(i=0; i<16; i++){
1057 if(intra_score < dmin){
1127 int16_t (*mv_table)[2],
int ref_index,
int f_code)
1134 const int mot_xy = mb_y*mot_stride + mb_x;
1154 P_LEFT[0] = mv_table[mot_xy - 1][0];
1155 P_LEFT[1] = mv_table[mot_xy - 1][1];
1161 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1162 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1163 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
1164 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
1192 mv_table[mot_xy][0]= mx;
1193 mv_table[mot_xy][1]= my;
1199 int motion_fx,
int motion_fy,
1200 int motion_bx,
int motion_by,
1201 int pred_fx,
int pred_fy,
1202 int pred_bx,
int pred_by,
1222 dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
1223 src_x = motion_fx >> 2;
1224 src_y = motion_fy >> 2;
1226 ptr = ref_data[0] + (src_y *
stride) + src_x;
1229 dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
1230 src_x = motion_bx >> 2;
1231 src_y = motion_by >> 2;
1233 ptr = ref2_data[0] + (src_y *
stride) + src_x;
1236 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1237 src_x = motion_fx >> 1;
1238 src_y = motion_fy >> 1;
1240 ptr = ref_data[0] + (src_y *
stride) + src_x;
1243 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1244 src_x = motion_bx >> 1;
1245 src_y = motion_by >> 1;
1247 ptr = ref2_data[0] + (src_y *
stride) + src_x;
1251 fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->
mb_penalty_factor
1252 +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->
mb_penalty_factor
1267 const int xy = mb_y *mot_stride + mb_x;
1279 const int shift= 1+qpel;
1284 #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
1285 #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
1286 int hashidx=
HASH(motion_fx,motion_fy, motion_bx, motion_by);
1289 map[hashidx&255] = 1;
1292 motion_bx, motion_by,
1299 static const uint8_t limittab[5]={0,8,32,64,80};
1301 static const int8_t vect[][4]={
1302 { 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1, 1,-1,-1}, {-1, 1, 1, 1}, { 1,-1,-1,-1},
1316 { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
1319 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),
1321 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),
1322 HASH8( 0, 1, 0, 1),
HASH8( 0,-1, 0,-1),
HASH8( 1, 0, 1, 0),
HASH8(-1, 0,-1, 0),
1323 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),
1324 HASH8( 0,-1, 0, 1),
HASH8( 0, 1, 0,-1),
HASH8(-1, 0, 1, 0),
HASH8( 1, 0,-1, 0),
1326 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),
1327 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),
1328 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),
1329 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),
1332 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),
1333 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),
1336 #define CHECK_BIDIR(fx,fy,bx,by)\
1337 if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
1338 &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
1339 &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
1341 map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
1342 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);\
1344 hashidx += HASH(fx,fy,bx,by);\
1353 #define CHECK_BIDIR2(a,b,c,d)\
1354 CHECK_BIDIR(a,b,c,d)\
1355 CHECK_BIDIR(-(a),-(b),-(c),-(d))
1367 for(i=8; i<limit; i++){
1368 int fx= motion_fx+vect[i][0];
1369 int fy= motion_fy+vect[i][1];
1370 int bx= motion_bx+vect[i][2];
1371 int by= motion_by+vect[i][3];
1373 int a= (xmax -
FFMAX(fx,bx))|(
FFMIN(fx,bx) - xmin);
1374 int b= (ymax -
FFMAX(fy,by))|(
FFMIN(fy,by) - ymin);
1376 map[(hashidx+hash[i])&255] = 1;
1378 if(!map[(hashidx+hash[i])&255]){
1380 map[(hashidx+hash[i])&255] = 1;
1381 score=
check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
1394 borderdist=
FFMIN(a,b);
1415 const int mot_xy = mb_y*mot_stride + mb_x;
1418 const int time_pp= s->
pp_time;
1419 const int time_pb= s->
pb_time;
1420 int mx, my, xmin, xmax, ymin, ymax;
1424 ymin= xmin=(-32)>>shift;
1425 ymax= xmax= 31>>
shift;
1449 xmin=
FFMAX(xmin, - 16 - min);
1456 ymin=
FFMAX(ymin, - 16 - min);
1461 av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
1463 if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
1467 return 256*256*256*64;
1479 P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
1480 P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
1484 P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
1485 P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
1486 P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
1487 P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
1504 mv_table[mot_xy][0]= mx;
1505 mv_table[mot_xy][1]= my;
1517 int fmin, bmin, dmin, fbmin, bimin, fimin;
1519 const int xy = mb_y*s->
mb_stride + mb_x;
1530 score= ((unsigned)(score*score + 128*256))>>16;
1554 av_dlog(s,
"%d %d %d %d\n", dmin, fmin, bmin, fbmin);
1568 fimin= bimin= INT_MAX;
1595 score= ((unsigned)(score*score + 128*256))>>16;
1606 if(fimin < INT_MAX && bimin < INT_MAX){
1626 int best_score=-10000000;
1629 range=
FFMIN(range, 16);
1631 range=
FFMIN(range, 256);
1633 for(i=0; i<8; i++) score[i]= s->
mb_num*(8-i);
1640 int mx= mv_table[xy][0];
1641 int my= mv_table[xy][1];
1646 if(mx >= range || mx < -range ||
1647 my >= range || my < -range)
1650 for(j=0; j<fcode && j<8; j++){
1660 if(score[i] > best_score){
1661 best_score= score[i];
1675 const int f_code= s->
f_code;
1681 av_assert0(range <= 16 || !s->msmpeg4_version);
1698 for(block=0; block<4; block++){
1699 int off= (block& 1) + (block>>1)*
wrap;
1703 if( mx >=range || mx <-range
1704 || my >=range || my <-range){
1705 s->
mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
1723 int16_t (*mv_table)[2],
int f_code,
int type,
int truncate)
1726 int y, h_range, v_range;
1734 v_range= field_select_table ? range>>1 : range;
1742 if(field_select_table==NULL || field_select_table[xy] == field_select){
1743 if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
1744 || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
1747 if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
1748 else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
1749 if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
1750 else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;