You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1086 lines
38KB

  1. /*
  2. * Motion estimation
  3. * Copyright (c) 2002-2004 Michael Niedermayer
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. *
  19. */
  20. /**
  21. * @file motion_est_template.c
  22. * Motion estimation template.
  23. */
  24. //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
  25. #define LOAD_COMMON\
  26. uint32_t * const score_map= c->score_map;\
  27. const int xmin= c->xmin;\
  28. const int ymin= c->ymin;\
  29. const int xmax= c->xmax;\
  30. const int ymax= c->ymax;\
  31. uint8_t *mv_penalty= c->current_mv_penalty;\
  32. const int pred_x= c->pred_x;\
  33. const int pred_y= c->pred_y;\
  34. #define CHECK_HALF_MV(dx, dy, x, y)\
  35. {\
  36. const int hx= 2*(x)+(dx);\
  37. const int hy= 2*(y)+(dy);\
  38. d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);\
  39. d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
  40. COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
  41. }
  42. #if 0
  43. static int hpel_motion_search)(MpegEncContext * s,
  44. int *mx_ptr, int *my_ptr, int dmin,
  45. uint8_t *ref_data[3],
  46. int size)
  47. {
  48. const int xx = 16 * s->mb_x + 8*(n&1);
  49. const int yy = 16 * s->mb_y + 8*(n>>1);
  50. const int mx = *mx_ptr;
  51. const int my = *my_ptr;
  52. const int penalty_factor= c->sub_penalty_factor;
  53. LOAD_COMMON
  54. // INIT;
  55. //FIXME factorize
  56. me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
  57. if(s->no_rounding /*FIXME b_type*/){
  58. hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
  59. chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
  60. }else{
  61. hpel_put=& s->dsp.put_pixels_tab[size];
  62. chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
  63. }
  64. cmpf= s->dsp.me_cmp[size];
  65. chroma_cmpf= s->dsp.me_cmp[size+1];
  66. cmp_sub= s->dsp.me_sub_cmp[size];
  67. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  68. if(c->skip){ //FIXME somehow move up (benchmark)
  69. *mx_ptr = 0;
  70. *my_ptr = 0;
  71. return dmin;
  72. }
  73. if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
  74. CMP_HPEL(dmin, 0, 0, mx, my, size);
  75. if(mx || my)
  76. dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
  77. }
  78. if (mx > xmin && mx < xmax &&
  79. my > ymin && my < ymax) {
  80. int bx=2*mx, by=2*my;
  81. int d= dmin;
  82. CHECK_HALF_MV(1, 1, mx-1, my-1)
  83. CHECK_HALF_MV(0, 1, mx , my-1)
  84. CHECK_HALF_MV(1, 1, mx , my-1)
  85. CHECK_HALF_MV(1, 0, mx-1, my )
  86. CHECK_HALF_MV(1, 0, mx , my )
  87. CHECK_HALF_MV(1, 1, mx-1, my )
  88. CHECK_HALF_MV(0, 1, mx , my )
  89. CHECK_HALF_MV(1, 1, mx , my )
  90. assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
  91. *mx_ptr = bx;
  92. *my_ptr = by;
  93. }else{
  94. *mx_ptr =2*mx;
  95. *my_ptr =2*my;
  96. }
  97. return dmin;
  98. }
  99. #else
  100. static int hpel_motion_search(MpegEncContext * s,
  101. int *mx_ptr, int *my_ptr, int dmin,
  102. int src_index, int ref_index,
  103. int size, int h)
  104. {
  105. MotionEstContext * const c= &s->me;
  106. const int mx = *mx_ptr;
  107. const int my = *my_ptr;
  108. const int penalty_factor= c->sub_penalty_factor;
  109. me_cmp_func cmp_sub, chroma_cmp_sub;
  110. int bx=2*mx, by=2*my;
  111. LOAD_COMMON
  112. int flags= c->sub_flags;
  113. //FIXME factorize
  114. cmp_sub= s->dsp.me_sub_cmp[size];
  115. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  116. if(c->skip){ //FIXME move out of hpel?
  117. *mx_ptr = 0;
  118. *my_ptr = 0;
  119. return dmin;
  120. }
  121. if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
  122. dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
  123. if(mx || my || size>0)
  124. dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
  125. }
  126. if (mx > xmin && mx < xmax &&
  127. my > ymin && my < ymax) {
  128. int d= dmin;
  129. const int index= (my<<ME_MAP_SHIFT) + mx;
  130. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
  131. + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*c->penalty_factor;
  132. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
  133. + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor;
  134. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
  135. + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*c->penalty_factor;
  136. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
  137. + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*c->penalty_factor;
  138. #if 1
  139. int key;
  140. int map_generation= c->map_generation;
  141. #ifndef NDEBUG
  142. uint32_t *map= c->map;
  143. #endif
  144. key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
  145. assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
  146. key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
  147. assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
  148. key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
  149. assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
  150. key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
  151. assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
  152. #endif
  153. if(t<=b){
  154. CHECK_HALF_MV(0, 1, mx ,my-1)
  155. if(l<=r){
  156. CHECK_HALF_MV(1, 1, mx-1, my-1)
  157. if(t+r<=b+l){
  158. CHECK_HALF_MV(1, 1, mx , my-1)
  159. }else{
  160. CHECK_HALF_MV(1, 1, mx-1, my )
  161. }
  162. CHECK_HALF_MV(1, 0, mx-1, my )
  163. }else{
  164. CHECK_HALF_MV(1, 1, mx , my-1)
  165. if(t+l<=b+r){
  166. CHECK_HALF_MV(1, 1, mx-1, my-1)
  167. }else{
  168. CHECK_HALF_MV(1, 1, mx , my )
  169. }
  170. CHECK_HALF_MV(1, 0, mx , my )
  171. }
  172. }else{
  173. if(l<=r){
  174. if(t+l<=b+r){
  175. CHECK_HALF_MV(1, 1, mx-1, my-1)
  176. }else{
  177. CHECK_HALF_MV(1, 1, mx , my )
  178. }
  179. CHECK_HALF_MV(1, 0, mx-1, my)
  180. CHECK_HALF_MV(1, 1, mx-1, my)
  181. }else{
  182. if(t+r<=b+l){
  183. CHECK_HALF_MV(1, 1, mx , my-1)
  184. }else{
  185. CHECK_HALF_MV(1, 1, mx-1, my)
  186. }
  187. CHECK_HALF_MV(1, 0, mx , my)
  188. CHECK_HALF_MV(1, 1, mx , my)
  189. }
  190. CHECK_HALF_MV(0, 1, mx , my)
  191. }
  192. assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
  193. }
  194. *mx_ptr = bx;
  195. *my_ptr = by;
  196. return dmin;
  197. }
  198. #endif
  199. static int inline get_mb_score(MpegEncContext * s, int mx, int my, int src_index,
  200. int ref_index)
  201. {
  202. // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
  203. MotionEstContext * const c= &s->me;
  204. const int size= 0;
  205. const int h= 16;
  206. const int penalty_factor= c->mb_penalty_factor;
  207. const int flags= c->mb_flags;
  208. const int qpel= flags & FLAG_QPEL;
  209. const int mask= 1+2*qpel;
  210. me_cmp_func cmp_sub, chroma_cmp_sub;
  211. int d;
  212. LOAD_COMMON
  213. //FIXME factorize
  214. cmp_sub= s->dsp.mb_cmp[size];
  215. chroma_cmp_sub= s->dsp.mb_cmp[size+1];
  216. assert(!c->skip);
  217. assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp);
  218. d= cmp(s, mx>>(qpel+1), my>>(qpel+1), mx&mask, my&mask, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
  219. //FIXME check cbp before adding penalty for (0,0) vector
  220. if(mx || my || size>0)
  221. d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
  222. return d;
  223. }
  224. #define CHECK_QUARTER_MV(dx, dy, x, y)\
  225. {\
  226. const int hx= 4*(x)+(dx);\
  227. const int hy= 4*(y)+(dy);\
  228. d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  229. d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
  230. COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
  231. }
  232. static int qpel_motion_search(MpegEncContext * s,
  233. int *mx_ptr, int *my_ptr, int dmin,
  234. int src_index, int ref_index,
  235. int size, int h)
  236. {
  237. MotionEstContext * const c= &s->me;
  238. const int mx = *mx_ptr;
  239. const int my = *my_ptr;
  240. const int penalty_factor= c->sub_penalty_factor;
  241. const int map_generation= c->map_generation;
  242. const int subpel_quality= c->avctx->me_subpel_quality;
  243. uint32_t *map= c->map;
  244. me_cmp_func cmpf, chroma_cmpf;
  245. me_cmp_func cmp_sub, chroma_cmp_sub;
  246. LOAD_COMMON
  247. int flags= c->sub_flags;
  248. cmpf= s->dsp.me_cmp[size];
  249. chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME
  250. //FIXME factorize
  251. cmp_sub= s->dsp.me_sub_cmp[size];
  252. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  253. if(c->skip){ //FIXME somehow move up (benchmark)
  254. *mx_ptr = 0;
  255. *my_ptr = 0;
  256. return dmin;
  257. }
  258. if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
  259. dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
  260. if(mx || my || size>0)
  261. dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
  262. }
  263. if (mx > xmin && mx < xmax &&
  264. my > ymin && my < ymax) {
  265. int bx=4*mx, by=4*my;
  266. int d= dmin;
  267. int i, nx, ny;
  268. const int index= (my<<ME_MAP_SHIFT) + mx;
  269. const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  270. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  271. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  272. const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  273. const int c= score_map[(index )&(ME_MAP_SIZE-1)];
  274. int best[8];
  275. int best_pos[8][2];
  276. memset(best, 64, sizeof(int)*8);
  277. #if 1
  278. if(s->me.dia_size>=2){
  279. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  280. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  281. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  282. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  283. for(ny= -3; ny <= 3; ny++){
  284. for(nx= -3; nx <= 3; nx++){
  285. const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
  286. const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
  287. const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
  288. int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
  289. int i;
  290. if((nx&3)==0 && (ny&3)==0) continue;
  291. score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  292. // if(nx&1) score-=1024*c->penalty_factor;
  293. // if(ny&1) score-=1024*c->penalty_factor;
  294. for(i=0; i<8; i++){
  295. if(score < best[i]){
  296. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  297. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  298. best[i]= score;
  299. best_pos[i][0]= nx + 4*mx;
  300. best_pos[i][1]= ny + 4*my;
  301. break;
  302. }
  303. }
  304. }
  305. }
  306. }else{
  307. int tl;
  308. const int cx = 4*(r - l);
  309. const int cx2= r + l - 2*c;
  310. const int cy = 4*(b - t);
  311. const int cy2= b + t - 2*c;
  312. int cxy;
  313. if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
  314. tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  315. }else{
  316. tl= cmp(s, mx-1, my-1, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);//FIXME wrong if chroma me is different
  317. }
  318. cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
  319. assert(16*cx2 + 4*cx + 32*c == 32*r);
  320. assert(16*cx2 - 4*cx + 32*c == 32*l);
  321. assert(16*cy2 + 4*cy + 32*c == 32*b);
  322. assert(16*cy2 - 4*cy + 32*c == 32*t);
  323. assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
  324. for(ny= -3; ny <= 3; ny++){
  325. for(nx= -3; nx <= 3; nx++){
  326. int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
  327. int i;
  328. if((nx&3)==0 && (ny&3)==0) continue;
  329. score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  330. // if(nx&1) score-=32*c->penalty_factor;
  331. // if(ny&1) score-=32*c->penalty_factor;
  332. for(i=0; i<8; i++){
  333. if(score < best[i]){
  334. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  335. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  336. best[i]= score;
  337. best_pos[i][0]= nx + 4*mx;
  338. best_pos[i][1]= ny + 4*my;
  339. break;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. for(i=0; i<subpel_quality; i++){
  346. nx= best_pos[i][0];
  347. ny= best_pos[i][1];
  348. CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
  349. }
  350. #if 0
  351. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  352. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  353. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  354. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  355. // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
  356. if(tl<br){
  357. // nx= FFMAX(4*mx - bx, bx - 4*mx);
  358. // ny= FFMAX(4*my - by, by - 4*my);
  359. static int stats[7][7], count;
  360. count++;
  361. stats[4*mx - bx + 3][4*my - by + 3]++;
  362. if(256*256*256*64 % count ==0){
  363. for(i=0; i<49; i++){
  364. if((i%7)==0) printf("\n");
  365. printf("%6d ", stats[0][i]);
  366. }
  367. printf("\n");
  368. }
  369. }
  370. #endif
  371. #else
  372. CHECK_QUARTER_MV(2, 2, mx-1, my-1)
  373. CHECK_QUARTER_MV(0, 2, mx , my-1)
  374. CHECK_QUARTER_MV(2, 2, mx , my-1)
  375. CHECK_QUARTER_MV(2, 0, mx , my )
  376. CHECK_QUARTER_MV(2, 2, mx , my )
  377. CHECK_QUARTER_MV(0, 2, mx , my )
  378. CHECK_QUARTER_MV(2, 2, mx-1, my )
  379. CHECK_QUARTER_MV(2, 0, mx-1, my )
  380. nx= bx;
  381. ny= by;
  382. for(i=0; i<8; i++){
  383. int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
  384. int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
  385. CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
  386. }
  387. #endif
  388. #if 0
  389. //outer ring
  390. CHECK_QUARTER_MV(1, 3, mx-1, my-1)
  391. CHECK_QUARTER_MV(1, 2, mx-1, my-1)
  392. CHECK_QUARTER_MV(1, 1, mx-1, my-1)
  393. CHECK_QUARTER_MV(2, 1, mx-1, my-1)
  394. CHECK_QUARTER_MV(3, 1, mx-1, my-1)
  395. CHECK_QUARTER_MV(0, 1, mx , my-1)
  396. CHECK_QUARTER_MV(1, 1, mx , my-1)
  397. CHECK_QUARTER_MV(2, 1, mx , my-1)
  398. CHECK_QUARTER_MV(3, 1, mx , my-1)
  399. CHECK_QUARTER_MV(3, 2, mx , my-1)
  400. CHECK_QUARTER_MV(3, 3, mx , my-1)
  401. CHECK_QUARTER_MV(3, 0, mx , my )
  402. CHECK_QUARTER_MV(3, 1, mx , my )
  403. CHECK_QUARTER_MV(3, 2, mx , my )
  404. CHECK_QUARTER_MV(3, 3, mx , my )
  405. CHECK_QUARTER_MV(2, 3, mx , my )
  406. CHECK_QUARTER_MV(1, 3, mx , my )
  407. CHECK_QUARTER_MV(0, 3, mx , my )
  408. CHECK_QUARTER_MV(3, 3, mx-1, my )
  409. CHECK_QUARTER_MV(2, 3, mx-1, my )
  410. CHECK_QUARTER_MV(1, 3, mx-1, my )
  411. CHECK_QUARTER_MV(1, 2, mx-1, my )
  412. CHECK_QUARTER_MV(1, 1, mx-1, my )
  413. CHECK_QUARTER_MV(1, 0, mx-1, my )
  414. #endif
  415. assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
  416. *mx_ptr = bx;
  417. *my_ptr = by;
  418. }else{
  419. *mx_ptr =4*mx;
  420. *my_ptr =4*my;
  421. }
  422. return dmin;
  423. }
  424. #define CHECK_MV(x,y)\
  425. {\
  426. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  427. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  428. assert((x) >= xmin);\
  429. assert((x) <= xmax);\
  430. assert((y) >= ymin);\
  431. assert((y) <= ymax);\
  432. /*printf("check_mv %d %d\n", x, y);*/\
  433. if(map[index]!=key){\
  434. d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  435. map[index]= key;\
  436. score_map[index]= d;\
  437. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  438. /*printf("score:%d\n", d);*/\
  439. COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
  440. }\
  441. }
  442. #define CHECK_CLIPED_MV(ax,ay)\
  443. {\
  444. const int x= ax;\
  445. const int y= ay;\
  446. const int x2= FFMAX(xmin, FFMIN(x, xmax));\
  447. const int y2= FFMAX(ymin, FFMIN(y, ymax));\
  448. CHECK_MV(x2, y2)\
  449. }
  450. #define CHECK_MV_DIR(x,y,new_dir)\
  451. {\
  452. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  453. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  454. /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
  455. if(map[index]!=key){\
  456. d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  457. map[index]= key;\
  458. score_map[index]= d;\
  459. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  460. /*printf("score:%d\n", d);*/\
  461. if(d<dmin){\
  462. best[0]=x;\
  463. best[1]=y;\
  464. dmin=d;\
  465. next_dir= new_dir;\
  466. }\
  467. }\
  468. }
  469. #define check(x,y,S,v)\
  470. if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
  471. if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
  472. if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
  473. if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
  474. #define LOAD_COMMON2\
  475. uint32_t *map= c->map;\
  476. const int qpel= flags&FLAG_QPEL;\
  477. const int shift= 1+qpel;\
  478. static always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
  479. int src_index, int ref_index, int const penalty_factor,
  480. int size, int h, int flags)
  481. {
  482. MotionEstContext * const c= &s->me;
  483. me_cmp_func cmpf, chroma_cmpf;
  484. int next_dir=-1;
  485. LOAD_COMMON
  486. LOAD_COMMON2
  487. int map_generation= c->map_generation;
  488. cmpf= s->dsp.me_cmp[size];
  489. chroma_cmpf= s->dsp.me_cmp[size+1];
  490. { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
  491. const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
  492. const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
  493. if(map[index]!=key){ //this will be executed only very rarey
  494. score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
  495. map[index]= key;
  496. }
  497. }
  498. for(;;){
  499. int d;
  500. const int dir= next_dir;
  501. const int x= best[0];
  502. const int y= best[1];
  503. next_dir=-1;
  504. //printf("%d", dir);
  505. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  506. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  507. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  508. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  509. if(next_dir==-1){
  510. return dmin;
  511. }
  512. }
  513. }
  514. static int funny_diamond_search(MpegEncContext * s, int *best, int dmin,
  515. int src_index, int ref_index, int const penalty_factor,
  516. int size, int h, int flags)
  517. {
  518. MotionEstContext * const c= &s->me;
  519. me_cmp_func cmpf, chroma_cmpf;
  520. int dia_size;
  521. LOAD_COMMON
  522. LOAD_COMMON2
  523. int map_generation= c->map_generation;
  524. cmpf= s->dsp.me_cmp[size];
  525. chroma_cmpf= s->dsp.me_cmp[size+1];
  526. for(dia_size=1; dia_size<=4; dia_size++){
  527. int dir;
  528. const int x= best[0];
  529. const int y= best[1];
  530. if(dia_size&(dia_size-1)) continue;
  531. if( x + dia_size > xmax
  532. || x - dia_size < xmin
  533. || y + dia_size > ymax
  534. || y - dia_size < ymin)
  535. continue;
  536. for(dir= 0; dir<dia_size; dir+=2){
  537. int d;
  538. CHECK_MV(x + dir , y + dia_size - dir);
  539. CHECK_MV(x + dia_size - dir, y - dir );
  540. CHECK_MV(x - dir , y - dia_size + dir);
  541. CHECK_MV(x - dia_size + dir, y + dir );
  542. }
  543. if(x!=best[0] || y!=best[1])
  544. dia_size=0;
  545. #if 0
  546. {
  547. int dx, dy, i;
  548. static int stats[8*8];
  549. dx= ABS(x-best[0]);
  550. dy= ABS(y-best[1]);
  551. if(dy>dx){
  552. dx^=dy; dy^=dx; dx^=dy;
  553. }
  554. stats[dy*8 + dx] ++;
  555. if(256*256*256*64 % (stats[0]+1)==0){
  556. for(i=0; i<64; i++){
  557. if((i&7)==0) printf("\n");
  558. printf("%8d ", stats[i]);
  559. }
  560. printf("\n");
  561. }
  562. }
  563. #endif
  564. }
  565. return dmin;
  566. }
  567. #define SAB_CHECK_MV(ax,ay)\
  568. {\
  569. const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
  570. const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
  571. /*printf("sab check %d %d\n", ax, ay);*/\
  572. if(map[index]!=key){\
  573. d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  574. map[index]= key;\
  575. score_map[index]= d;\
  576. d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
  577. /*printf("score: %d\n", d);*/\
  578. if(d < minima[minima_count-1].height){\
  579. int j=0;\
  580. \
  581. while(d >= minima[j].height) j++;\
  582. \
  583. memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
  584. \
  585. minima[j].checked= 0;\
  586. minima[j].height= d;\
  587. minima[j].x= ax;\
  588. minima[j].y= ay;\
  589. \
  590. i=-1;\
  591. continue;\
  592. }\
  593. }\
  594. }
  595. #define MAX_SAB_SIZE 16
  596. static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
  597. int src_index, int ref_index, int const penalty_factor,
  598. int size, int h, int flags)
  599. {
  600. MotionEstContext * const c= &s->me;
  601. me_cmp_func cmpf, chroma_cmpf;
  602. Minima minima[MAX_SAB_SIZE];
  603. const int minima_count= ABS(c->dia_size);
  604. int i, j;
  605. LOAD_COMMON
  606. LOAD_COMMON2
  607. int map_generation= c->map_generation;
  608. cmpf= s->dsp.me_cmp[size];
  609. chroma_cmpf= s->dsp.me_cmp[size+1];
  610. for(j=i=0; i<ME_MAP_SIZE; i++){
  611. uint32_t key= map[i];
  612. key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
  613. if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
  614. assert(j<MAX_SAB_SIZE); //max j = number of predictors
  615. minima[j].height= score_map[i];
  616. minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
  617. minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
  618. minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
  619. minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
  620. minima[j].checked=0;
  621. if(minima[j].x || minima[j].y)
  622. minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
  623. j++;
  624. }
  625. qsort(minima, j, sizeof(Minima), minima_cmp);
  626. for(; j<minima_count; j++){
  627. minima[j].height=256*256*256*64;
  628. minima[j].checked=0;
  629. minima[j].x= minima[j].y=0;
  630. }
  631. for(i=0; i<minima_count; i++){
  632. const int x= minima[i].x;
  633. const int y= minima[i].y;
  634. int d;
  635. if(minima[i].checked) continue;
  636. if( x >= xmax || x <= xmin
  637. || y >= ymax || y <= ymin)
  638. continue;
  639. SAB_CHECK_MV(x-1, y)
  640. SAB_CHECK_MV(x+1, y)
  641. SAB_CHECK_MV(x , y-1)
  642. SAB_CHECK_MV(x , y+1)
  643. minima[i].checked= 1;
  644. }
  645. best[0]= minima[0].x;
  646. best[1]= minima[0].y;
  647. dmin= minima[0].height;
  648. if( best[0] < xmax && best[0] > xmin
  649. && best[1] < ymax && best[1] > ymin){
  650. int d;
  651. //ensure that the refernece samples for hpel refinement are in the map
  652. CHECK_MV(best[0]-1, best[1])
  653. CHECK_MV(best[0]+1, best[1])
  654. CHECK_MV(best[0], best[1]-1)
  655. CHECK_MV(best[0], best[1]+1)
  656. }
  657. return dmin;
  658. }
  659. static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
  660. int src_index, int ref_index, int const penalty_factor,
  661. int size, int h, int flags)
  662. {
  663. MotionEstContext * const c= &s->me;
  664. me_cmp_func cmpf, chroma_cmpf;
  665. int dia_size;
  666. LOAD_COMMON
  667. LOAD_COMMON2
  668. int map_generation= c->map_generation;
  669. cmpf= s->dsp.me_cmp[size];
  670. chroma_cmpf= s->dsp.me_cmp[size+1];
  671. for(dia_size=1; dia_size<=c->dia_size; dia_size++){
  672. int dir, start, end;
  673. const int x= best[0];
  674. const int y= best[1];
  675. start= FFMAX(0, y + dia_size - ymax);
  676. end = FFMIN(dia_size, xmax - x + 1);
  677. for(dir= start; dir<end; dir++){
  678. int d;
  679. //check(x + dir,y + dia_size - dir,0, a0)
  680. CHECK_MV(x + dir , y + dia_size - dir);
  681. }
  682. start= FFMAX(0, x + dia_size - xmax);
  683. end = FFMIN(dia_size, y - ymin + 1);
  684. for(dir= start; dir<end; dir++){
  685. int d;
  686. //check(x + dia_size - dir, y - dir,0, a1)
  687. CHECK_MV(x + dia_size - dir, y - dir );
  688. }
  689. start= FFMAX(0, -y + dia_size + ymin );
  690. end = FFMIN(dia_size, x - xmin + 1);
  691. for(dir= start; dir<end; dir++){
  692. int d;
  693. //check(x - dir,y - dia_size + dir,0, a2)
  694. CHECK_MV(x - dir , y - dia_size + dir);
  695. }
  696. start= FFMAX(0, -x + dia_size + xmin );
  697. end = FFMIN(dia_size, ymax - y + 1);
  698. for(dir= start; dir<end; dir++){
  699. int d;
  700. //check(x - dia_size + dir, y + dir,0, a3)
  701. CHECK_MV(x - dia_size + dir, y + dir );
  702. }
  703. if(x!=best[0] || y!=best[1])
  704. dia_size=0;
  705. #if 0
  706. {
  707. int dx, dy, i;
  708. static int stats[8*8];
  709. dx= ABS(x-best[0]);
  710. dy= ABS(y-best[1]);
  711. stats[dy*8 + dx] ++;
  712. if(256*256*256*64 % (stats[0]+1)==0){
  713. for(i=0; i<64; i++){
  714. if((i&7)==0) printf("\n");
  715. printf("%6d ", stats[i]);
  716. }
  717. printf("\n");
  718. }
  719. }
  720. #endif
  721. }
  722. return dmin;
  723. }
  724. static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
  725. int src_index, int ref_index, int const penalty_factor,
  726. int size, int h, int flags){
  727. MotionEstContext * const c= &s->me;
  728. if(c->dia_size==-1)
  729. return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  730. else if(c->dia_size<-1)
  731. return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  732. else if(c->dia_size<2)
  733. return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  734. else
  735. return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  736. }
  737. static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,
  738. int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
  739. int ref_mv_scale, int flags)
  740. {
  741. MotionEstContext * const c= &s->me;
  742. int best[2]={0, 0};
  743. int d, dmin;
  744. int map_generation;
  745. const int penalty_factor= c->penalty_factor;
  746. const int size=0;
  747. const int h=16;
  748. const int ref_mv_stride= s->mb_stride; //pass as arg FIXME
  749. const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
  750. me_cmp_func cmpf, chroma_cmpf;
  751. LOAD_COMMON
  752. LOAD_COMMON2
  753. cmpf= s->dsp.me_cmp[size];
  754. chroma_cmpf= s->dsp.me_cmp[size+1];
  755. map_generation= update_map_generation(c);
  756. dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
  757. map[0]= map_generation;
  758. score_map[0]= dmin;
  759. /* first line */
  760. if (s->first_slice_line) {
  761. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  762. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  763. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  764. }else{
  765. if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
  766. |P_TOP[0] |P_TOP[1]
  767. |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
  768. *mx_ptr= 0;
  769. *my_ptr= 0;
  770. c->skip=1;
  771. return dmin;
  772. }
  773. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  774. if(dmin>256*2){
  775. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  776. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  777. CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
  778. CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
  779. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  780. }
  781. }
  782. if(dmin>256*4){
  783. if(c->pre_pass){
  784. CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
  785. (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
  786. if(!s->first_slice_line)
  787. CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  788. (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  789. }else{
  790. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  791. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  792. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  793. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  794. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  795. }
  796. }
  797. if(c->avctx->last_predictor_count){
  798. const int count= c->avctx->last_predictor_count;
  799. const int xstart= FFMAX(0, s->mb_x - count);
  800. const int ystart= FFMAX(0, s->mb_y - count);
  801. const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
  802. const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
  803. int mb_y;
  804. for(mb_y=ystart; mb_y<yend; mb_y++){
  805. int mb_x;
  806. for(mb_x=xstart; mb_x<xend; mb_x++){
  807. const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
  808. int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
  809. int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
  810. if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
  811. CHECK_MV(mx,my)
  812. }
  813. }
  814. }
  815. //check(best[0],best[1],0, b0)
  816. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  817. //check(best[0],best[1],0, b1)
  818. *mx_ptr= best[0];
  819. *my_ptr= best[1];
  820. // printf("%d %d %d \n", best[0], best[1], dmin);
  821. return dmin;
  822. }
  823. //this function is dedicated to the braindamaged gcc
  824. static inline int epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,
  825. int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
  826. int ref_mv_scale)
  827. {
  828. MotionEstContext * const c= &s->me;
  829. //FIXME convert other functions in the same way if faster
  830. switch(c->flags){
  831. case 0:
  832. return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0);
  833. // case FLAG_QPEL:
  834. // return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);
  835. default:
  836. return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags);
  837. }
  838. }
  839. static int epzs_motion_search4(MpegEncContext * s,
  840. int *mx_ptr, int *my_ptr, int P[10][2],
  841. int src_index, int ref_index, int16_t (*last_mv)[2],
  842. int ref_mv_scale)
  843. {
  844. MotionEstContext * const c= &s->me;
  845. int best[2]={0, 0};
  846. int d, dmin;
  847. int map_generation;
  848. const int penalty_factor= c->penalty_factor;
  849. const int size=1;
  850. const int h=8;
  851. const int ref_mv_stride= s->mb_stride;
  852. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  853. me_cmp_func cmpf, chroma_cmpf;
  854. LOAD_COMMON
  855. int flags= c->flags;
  856. LOAD_COMMON2
  857. cmpf= s->dsp.me_cmp[size];
  858. chroma_cmpf= s->dsp.me_cmp[size+1];
  859. map_generation= update_map_generation(c);
  860. dmin = 1000000;
  861. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  862. /* first line */
  863. if (s->first_slice_line) {
  864. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  865. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  866. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  867. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  868. }else{
  869. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  870. //FIXME try some early stop
  871. if(dmin>64*2){
  872. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  873. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  874. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  875. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  876. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  877. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  878. }
  879. }
  880. if(dmin>64*4){
  881. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  882. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  883. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  884. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  885. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  886. }
  887. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  888. *mx_ptr= best[0];
  889. *my_ptr= best[1];
  890. // printf("%d %d %d \n", best[0], best[1], dmin);
  891. return dmin;
  892. }
  893. //try to merge with above FIXME (needs PSNR test)
  894. static int epzs_motion_search2(MpegEncContext * s,
  895. int *mx_ptr, int *my_ptr, int P[10][2],
  896. int src_index, int ref_index, int16_t (*last_mv)[2],
  897. int ref_mv_scale)
  898. {
  899. MotionEstContext * const c= &s->me;
  900. int best[2]={0, 0};
  901. int d, dmin;
  902. int map_generation;
  903. const int penalty_factor= c->penalty_factor;
  904. const int size=0; //FIXME pass as arg
  905. const int h=8;
  906. const int ref_mv_stride= s->mb_stride;
  907. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  908. me_cmp_func cmpf, chroma_cmpf;
  909. LOAD_COMMON
  910. int flags= c->flags;
  911. LOAD_COMMON2
  912. cmpf= s->dsp.me_cmp[size];
  913. chroma_cmpf= s->dsp.me_cmp[size+1];
  914. map_generation= update_map_generation(c);
  915. dmin = 1000000;
  916. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  917. /* first line */
  918. if (s->first_slice_line) {
  919. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  920. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  921. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  922. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  923. }else{
  924. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  925. //FIXME try some early stop
  926. if(dmin>64*2){
  927. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  928. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  929. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  930. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  931. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  932. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  933. }
  934. }
  935. if(dmin>64*4){
  936. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  937. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  938. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  939. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  940. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  941. }
  942. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  943. *mx_ptr= best[0];
  944. *my_ptr= best[1];
  945. // printf("%d %d %d \n", best[0], best[1], dmin);
  946. return dmin;
  947. }