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.

1085 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. int inline ff_get_mb_score(MpegEncContext * s, int mx, int my, int src_index,
  200. int ref_index, int size, int h, int add_rate)
  201. {
  202. // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
  203. MotionEstContext * const c= &s->me;
  204. const int penalty_factor= c->mb_penalty_factor;
  205. const int flags= c->mb_flags;
  206. const int qpel= flags & FLAG_QPEL;
  207. const int mask= 1+2*qpel;
  208. me_cmp_func cmp_sub, chroma_cmp_sub;
  209. int d;
  210. LOAD_COMMON
  211. //FIXME factorize
  212. cmp_sub= s->dsp.mb_cmp[size];
  213. chroma_cmp_sub= s->dsp.mb_cmp[size+1];
  214. // assert(!c->skip);
  215. // assert(c->avctx->me_sub_cmp != c->avctx->mb_cmp);
  216. 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);
  217. //FIXME check cbp before adding penalty for (0,0) vector
  218. if(add_rate && (mx || my || size>0))
  219. d += (mv_penalty[mx - pred_x] + mv_penalty[my - pred_y])*penalty_factor;
  220. return d;
  221. }
  222. #define CHECK_QUARTER_MV(dx, dy, x, y)\
  223. {\
  224. const int hx= 4*(x)+(dx);\
  225. const int hy= 4*(y)+(dy);\
  226. d= cmp(s, x, y, dx, dy, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  227. d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
  228. COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
  229. }
  230. static int qpel_motion_search(MpegEncContext * s,
  231. int *mx_ptr, int *my_ptr, int dmin,
  232. int src_index, int ref_index,
  233. int size, int h)
  234. {
  235. MotionEstContext * const c= &s->me;
  236. const int mx = *mx_ptr;
  237. const int my = *my_ptr;
  238. const int penalty_factor= c->sub_penalty_factor;
  239. const int map_generation= c->map_generation;
  240. const int subpel_quality= c->avctx->me_subpel_quality;
  241. uint32_t *map= c->map;
  242. me_cmp_func cmpf, chroma_cmpf;
  243. me_cmp_func cmp_sub, chroma_cmp_sub;
  244. LOAD_COMMON
  245. int flags= c->sub_flags;
  246. cmpf= s->dsp.me_cmp[size];
  247. chroma_cmpf= s->dsp.me_cmp[size+1]; //factorize FIXME
  248. //FIXME factorize
  249. cmp_sub= s->dsp.me_sub_cmp[size];
  250. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  251. if(c->skip){ //FIXME somehow move up (benchmark)
  252. *mx_ptr = 0;
  253. *my_ptr = 0;
  254. return dmin;
  255. }
  256. if(c->avctx->me_cmp != c->avctx->me_sub_cmp){
  257. dmin= cmp(s, mx, my, 0, 0, size, h, ref_index, src_index, cmp_sub, chroma_cmp_sub, flags);
  258. if(mx || my || size>0)
  259. dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
  260. }
  261. if (mx > xmin && mx < xmax &&
  262. my > ymin && my < ymax) {
  263. int bx=4*mx, by=4*my;
  264. int d= dmin;
  265. int i, nx, ny;
  266. const int index= (my<<ME_MAP_SHIFT) + mx;
  267. const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  268. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  269. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  270. const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  271. const int c= score_map[(index )&(ME_MAP_SIZE-1)];
  272. int best[8];
  273. int best_pos[8][2];
  274. memset(best, 64, sizeof(int)*8);
  275. #if 1
  276. if(s->me.dia_size>=2){
  277. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  278. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  279. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  280. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  281. for(ny= -3; ny <= 3; ny++){
  282. for(nx= -3; nx <= 3; nx++){
  283. //FIXME this could overflow (unlikely though)
  284. const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
  285. const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
  286. const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
  287. int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;
  288. int i;
  289. if((nx&3)==0 && (ny&3)==0) continue;
  290. score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  291. // if(nx&1) score-=1024*c->penalty_factor;
  292. // if(ny&1) score-=1024*c->penalty_factor;
  293. for(i=0; i<8; i++){
  294. if(score < best[i]){
  295. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  296. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  297. best[i]= score;
  298. best_pos[i][0]= nx + 4*mx;
  299. best_pos[i][1]= ny + 4*my;
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. }else{
  306. int tl;
  307. //FIXME this could overflow (unlikely though)
  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. //FIXME this could overflow (unlikely though)
  327. int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
  328. int i;
  329. if((nx&3)==0 && (ny&3)==0) continue;
  330. score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  331. // if(nx&1) score-=32*c->penalty_factor;
  332. // if(ny&1) score-=32*c->penalty_factor;
  333. for(i=0; i<8; i++){
  334. if(score < best[i]){
  335. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  336. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  337. best[i]= score;
  338. best_pos[i][0]= nx + 4*mx;
  339. best_pos[i][1]= ny + 4*my;
  340. break;
  341. }
  342. }
  343. }
  344. }
  345. }
  346. for(i=0; i<subpel_quality; i++){
  347. nx= best_pos[i][0];
  348. ny= best_pos[i][1];
  349. CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
  350. }
  351. #if 0
  352. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  353. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  354. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  355. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  356. // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
  357. if(tl<br){
  358. // nx= FFMAX(4*mx - bx, bx - 4*mx);
  359. // ny= FFMAX(4*my - by, by - 4*my);
  360. static int stats[7][7], count;
  361. count++;
  362. stats[4*mx - bx + 3][4*my - by + 3]++;
  363. if(256*256*256*64 % count ==0){
  364. for(i=0; i<49; i++){
  365. if((i%7)==0) printf("\n");
  366. printf("%6d ", stats[0][i]);
  367. }
  368. printf("\n");
  369. }
  370. }
  371. #endif
  372. #else
  373. CHECK_QUARTER_MV(2, 2, mx-1, my-1)
  374. CHECK_QUARTER_MV(0, 2, mx , my-1)
  375. CHECK_QUARTER_MV(2, 2, mx , my-1)
  376. CHECK_QUARTER_MV(2, 0, mx , my )
  377. CHECK_QUARTER_MV(2, 2, mx , my )
  378. CHECK_QUARTER_MV(0, 2, mx , my )
  379. CHECK_QUARTER_MV(2, 2, mx-1, my )
  380. CHECK_QUARTER_MV(2, 0, mx-1, my )
  381. nx= bx;
  382. ny= by;
  383. for(i=0; i<8; i++){
  384. int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
  385. int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
  386. CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
  387. }
  388. #endif
  389. #if 0
  390. //outer ring
  391. CHECK_QUARTER_MV(1, 3, mx-1, my-1)
  392. CHECK_QUARTER_MV(1, 2, mx-1, my-1)
  393. CHECK_QUARTER_MV(1, 1, mx-1, my-1)
  394. CHECK_QUARTER_MV(2, 1, mx-1, my-1)
  395. CHECK_QUARTER_MV(3, 1, mx-1, my-1)
  396. CHECK_QUARTER_MV(0, 1, mx , my-1)
  397. CHECK_QUARTER_MV(1, 1, mx , my-1)
  398. CHECK_QUARTER_MV(2, 1, mx , my-1)
  399. CHECK_QUARTER_MV(3, 1, mx , my-1)
  400. CHECK_QUARTER_MV(3, 2, mx , my-1)
  401. CHECK_QUARTER_MV(3, 3, mx , my-1)
  402. CHECK_QUARTER_MV(3, 0, mx , my )
  403. CHECK_QUARTER_MV(3, 1, mx , my )
  404. CHECK_QUARTER_MV(3, 2, mx , my )
  405. CHECK_QUARTER_MV(3, 3, mx , my )
  406. CHECK_QUARTER_MV(2, 3, mx , my )
  407. CHECK_QUARTER_MV(1, 3, mx , my )
  408. CHECK_QUARTER_MV(0, 3, mx , my )
  409. CHECK_QUARTER_MV(3, 3, mx-1, my )
  410. CHECK_QUARTER_MV(2, 3, mx-1, my )
  411. CHECK_QUARTER_MV(1, 3, mx-1, my )
  412. CHECK_QUARTER_MV(1, 2, mx-1, my )
  413. CHECK_QUARTER_MV(1, 1, mx-1, my )
  414. CHECK_QUARTER_MV(1, 0, mx-1, my )
  415. #endif
  416. assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
  417. *mx_ptr = bx;
  418. *my_ptr = by;
  419. }else{
  420. *mx_ptr =4*mx;
  421. *my_ptr =4*my;
  422. }
  423. return dmin;
  424. }
  425. #define CHECK_MV(x,y)\
  426. {\
  427. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  428. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  429. assert((x) >= xmin);\
  430. assert((x) <= xmax);\
  431. assert((y) >= ymin);\
  432. assert((y) <= ymax);\
  433. /*printf("check_mv %d %d\n", x, y);*/\
  434. if(map[index]!=key){\
  435. d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  436. map[index]= key;\
  437. score_map[index]= d;\
  438. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  439. /*printf("score:%d\n", d);*/\
  440. COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
  441. }\
  442. }
  443. #define CHECK_CLIPED_MV(ax,ay)\
  444. {\
  445. const int x= ax;\
  446. const int y= ay;\
  447. const int x2= FFMAX(xmin, FFMIN(x, xmax));\
  448. const int y2= FFMAX(ymin, FFMIN(y, ymax));\
  449. CHECK_MV(x2, y2)\
  450. }
  451. #define CHECK_MV_DIR(x,y,new_dir)\
  452. {\
  453. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  454. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  455. /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
  456. if(map[index]!=key){\
  457. d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  458. map[index]= key;\
  459. score_map[index]= d;\
  460. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  461. /*printf("score:%d\n", d);*/\
  462. if(d<dmin){\
  463. best[0]=x;\
  464. best[1]=y;\
  465. dmin=d;\
  466. next_dir= new_dir;\
  467. }\
  468. }\
  469. }
  470. #define check(x,y,S,v)\
  471. if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
  472. if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
  473. if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
  474. if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
  475. #define LOAD_COMMON2\
  476. uint32_t *map= c->map;\
  477. const int qpel= flags&FLAG_QPEL;\
  478. const int shift= 1+qpel;\
  479. static always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
  480. int src_index, int ref_index, int const penalty_factor,
  481. int size, int h, int flags)
  482. {
  483. MotionEstContext * const c= &s->me;
  484. me_cmp_func cmpf, chroma_cmpf;
  485. int next_dir=-1;
  486. LOAD_COMMON
  487. LOAD_COMMON2
  488. int map_generation= c->map_generation;
  489. cmpf= s->dsp.me_cmp[size];
  490. chroma_cmpf= s->dsp.me_cmp[size+1];
  491. { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
  492. const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
  493. const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
  494. if(map[index]!=key){ //this will be executed only very rarey
  495. score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
  496. map[index]= key;
  497. }
  498. }
  499. for(;;){
  500. int d;
  501. const int dir= next_dir;
  502. const int x= best[0];
  503. const int y= best[1];
  504. next_dir=-1;
  505. //printf("%d", dir);
  506. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  507. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  508. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  509. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  510. if(next_dir==-1){
  511. return dmin;
  512. }
  513. }
  514. }
  515. static int funny_diamond_search(MpegEncContext * s, int *best, int dmin,
  516. int src_index, int ref_index, int const penalty_factor,
  517. int size, int h, int flags)
  518. {
  519. MotionEstContext * const c= &s->me;
  520. me_cmp_func cmpf, chroma_cmpf;
  521. int dia_size;
  522. LOAD_COMMON
  523. LOAD_COMMON2
  524. int map_generation= c->map_generation;
  525. cmpf= s->dsp.me_cmp[size];
  526. chroma_cmpf= s->dsp.me_cmp[size+1];
  527. for(dia_size=1; dia_size<=4; dia_size++){
  528. int dir;
  529. const int x= best[0];
  530. const int y= best[1];
  531. if(dia_size&(dia_size-1)) continue;
  532. if( x + dia_size > xmax
  533. || x - dia_size < xmin
  534. || y + dia_size > ymax
  535. || y - dia_size < ymin)
  536. continue;
  537. for(dir= 0; dir<dia_size; dir+=2){
  538. int d;
  539. CHECK_MV(x + dir , y + dia_size - dir);
  540. CHECK_MV(x + dia_size - dir, y - dir );
  541. CHECK_MV(x - dir , y - dia_size + dir);
  542. CHECK_MV(x - dia_size + dir, y + dir );
  543. }
  544. if(x!=best[0] || y!=best[1])
  545. dia_size=0;
  546. #if 0
  547. {
  548. int dx, dy, i;
  549. static int stats[8*8];
  550. dx= ABS(x-best[0]);
  551. dy= ABS(y-best[1]);
  552. if(dy>dx){
  553. dx^=dy; dy^=dx; dx^=dy;
  554. }
  555. stats[dy*8 + dx] ++;
  556. if(256*256*256*64 % (stats[0]+1)==0){
  557. for(i=0; i<64; i++){
  558. if((i&7)==0) printf("\n");
  559. printf("%8d ", stats[i]);
  560. }
  561. printf("\n");
  562. }
  563. }
  564. #endif
  565. }
  566. return dmin;
  567. }
  568. #define SAB_CHECK_MV(ax,ay)\
  569. {\
  570. const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
  571. const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
  572. /*printf("sab check %d %d\n", ax, ay);*/\
  573. if(map[index]!=key){\
  574. d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  575. map[index]= key;\
  576. score_map[index]= d;\
  577. d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
  578. /*printf("score: %d\n", d);*/\
  579. if(d < minima[minima_count-1].height){\
  580. int j=0;\
  581. \
  582. while(d >= minima[j].height) j++;\
  583. \
  584. memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
  585. \
  586. minima[j].checked= 0;\
  587. minima[j].height= d;\
  588. minima[j].x= ax;\
  589. minima[j].y= ay;\
  590. \
  591. i=-1;\
  592. continue;\
  593. }\
  594. }\
  595. }
  596. #define MAX_SAB_SIZE ME_MAP_SIZE
  597. static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
  598. int src_index, int ref_index, int const penalty_factor,
  599. int size, int h, int flags)
  600. {
  601. MotionEstContext * const c= &s->me;
  602. me_cmp_func cmpf, chroma_cmpf;
  603. Minima minima[MAX_SAB_SIZE];
  604. const int minima_count= ABS(c->dia_size);
  605. int i, j;
  606. LOAD_COMMON
  607. LOAD_COMMON2
  608. int map_generation= c->map_generation;
  609. cmpf= s->dsp.me_cmp[size];
  610. chroma_cmpf= s->dsp.me_cmp[size+1];
  611. for(j=i=0; i<ME_MAP_SIZE; i++){
  612. uint32_t key= map[i];
  613. key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
  614. if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
  615. assert(j<MAX_SAB_SIZE); //max j = number of predictors
  616. minima[j].height= score_map[i];
  617. minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
  618. minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
  619. minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
  620. minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
  621. minima[j].checked=0;
  622. if(minima[j].x || minima[j].y)
  623. minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
  624. j++;
  625. }
  626. qsort(minima, j, sizeof(Minima), minima_cmp);
  627. for(; j<minima_count; j++){
  628. minima[j].height=256*256*256*64;
  629. minima[j].checked=0;
  630. minima[j].x= minima[j].y=0;
  631. }
  632. for(i=0; i<minima_count; i++){
  633. const int x= minima[i].x;
  634. const int y= minima[i].y;
  635. int d;
  636. if(minima[i].checked) continue;
  637. if( x >= xmax || x <= xmin
  638. || y >= ymax || y <= ymin)
  639. continue;
  640. SAB_CHECK_MV(x-1, y)
  641. SAB_CHECK_MV(x+1, y)
  642. SAB_CHECK_MV(x , y-1)
  643. SAB_CHECK_MV(x , y+1)
  644. minima[i].checked= 1;
  645. }
  646. best[0]= minima[0].x;
  647. best[1]= minima[0].y;
  648. dmin= minima[0].height;
  649. if( best[0] < xmax && best[0] > xmin
  650. && best[1] < ymax && best[1] > ymin){
  651. int d;
  652. //ensure that the refernece samples for hpel refinement are in the map
  653. CHECK_MV(best[0]-1, best[1])
  654. CHECK_MV(best[0]+1, best[1])
  655. CHECK_MV(best[0], best[1]-1)
  656. CHECK_MV(best[0], best[1]+1)
  657. }
  658. return dmin;
  659. }
  660. static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
  661. int src_index, int ref_index, int const penalty_factor,
  662. int size, int h, int flags)
  663. {
  664. MotionEstContext * const c= &s->me;
  665. me_cmp_func cmpf, chroma_cmpf;
  666. int dia_size;
  667. LOAD_COMMON
  668. LOAD_COMMON2
  669. int map_generation= c->map_generation;
  670. cmpf= s->dsp.me_cmp[size];
  671. chroma_cmpf= s->dsp.me_cmp[size+1];
  672. for(dia_size=1; dia_size<=c->dia_size; dia_size++){
  673. int dir, start, end;
  674. const int x= best[0];
  675. const int y= best[1];
  676. start= FFMAX(0, y + dia_size - ymax);
  677. end = FFMIN(dia_size, xmax - x + 1);
  678. for(dir= start; dir<end; dir++){
  679. int d;
  680. //check(x + dir,y + dia_size - dir,0, a0)
  681. CHECK_MV(x + dir , y + dia_size - dir);
  682. }
  683. start= FFMAX(0, x + dia_size - xmax);
  684. end = FFMIN(dia_size, y - ymin + 1);
  685. for(dir= start; dir<end; dir++){
  686. int d;
  687. //check(x + dia_size - dir, y - dir,0, a1)
  688. CHECK_MV(x + dia_size - dir, y - dir );
  689. }
  690. start= FFMAX(0, -y + dia_size + ymin );
  691. end = FFMIN(dia_size, x - xmin + 1);
  692. for(dir= start; dir<end; dir++){
  693. int d;
  694. //check(x - dir,y - dia_size + dir,0, a2)
  695. CHECK_MV(x - dir , y - dia_size + dir);
  696. }
  697. start= FFMAX(0, -x + dia_size + xmin );
  698. end = FFMIN(dia_size, ymax - y + 1);
  699. for(dir= start; dir<end; dir++){
  700. int d;
  701. //check(x - dia_size + dir, y + dir,0, a3)
  702. CHECK_MV(x - dia_size + dir, y + dir );
  703. }
  704. if(x!=best[0] || y!=best[1])
  705. dia_size=0;
  706. #if 0
  707. {
  708. int dx, dy, i;
  709. static int stats[8*8];
  710. dx= ABS(x-best[0]);
  711. dy= ABS(y-best[1]);
  712. stats[dy*8 + dx] ++;
  713. if(256*256*256*64 % (stats[0]+1)==0){
  714. for(i=0; i<64; i++){
  715. if((i&7)==0) printf("\n");
  716. printf("%6d ", stats[i]);
  717. }
  718. printf("\n");
  719. }
  720. }
  721. #endif
  722. }
  723. return dmin;
  724. }
  725. static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
  726. int src_index, int ref_index, int const penalty_factor,
  727. int size, int h, int flags){
  728. MotionEstContext * const c= &s->me;
  729. if(c->dia_size==-1)
  730. return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  731. else if(c->dia_size<-1)
  732. return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  733. else if(c->dia_size<2)
  734. return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  735. else
  736. return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  737. }
  738. static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,
  739. int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
  740. int ref_mv_scale, int flags, int size, int h)
  741. {
  742. MotionEstContext * const c= &s->me;
  743. int best[2]={0, 0};
  744. int d, dmin;
  745. int map_generation;
  746. const int penalty_factor= c->penalty_factor;
  747. const int ref_mv_stride= s->mb_stride; //pass as arg FIXME
  748. const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
  749. me_cmp_func cmpf, chroma_cmpf;
  750. LOAD_COMMON
  751. LOAD_COMMON2
  752. cmpf= s->dsp.me_cmp[size];
  753. chroma_cmpf= s->dsp.me_cmp[size+1];
  754. map_generation= update_map_generation(c);
  755. assert(cmpf);
  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<h*h && ( 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>h*h*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>h*h*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. inline int ff_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, int size, int h)
  827. {
  828. MotionEstContext * const c= &s->me;
  829. //FIXME convert other functions in the same way if faster
  830. if(c->flags==0 && h==16 && size==0){
  831. return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0, 0, 16);
  832. // case FLAG_QPEL:
  833. // return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);
  834. }else{
  835. return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags, size, h);
  836. }
  837. }
  838. static int epzs_motion_search4(MpegEncContext * s,
  839. int *mx_ptr, int *my_ptr, int P[10][2],
  840. int src_index, int ref_index, int16_t (*last_mv)[2],
  841. int ref_mv_scale)
  842. {
  843. MotionEstContext * const c= &s->me;
  844. int best[2]={0, 0};
  845. int d, dmin;
  846. int map_generation;
  847. const int penalty_factor= c->penalty_factor;
  848. const int size=1;
  849. const int h=8;
  850. const int ref_mv_stride= s->mb_stride;
  851. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  852. me_cmp_func cmpf, chroma_cmpf;
  853. LOAD_COMMON
  854. int flags= c->flags;
  855. LOAD_COMMON2
  856. cmpf= s->dsp.me_cmp[size];
  857. chroma_cmpf= s->dsp.me_cmp[size+1];
  858. map_generation= update_map_generation(c);
  859. dmin = 1000000;
  860. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  861. /* first line */
  862. if (s->first_slice_line) {
  863. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  864. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  865. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  866. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  867. }else{
  868. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  869. //FIXME try some early stop
  870. if(dmin>64*2){
  871. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  872. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  873. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  874. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  875. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  876. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  877. }
  878. }
  879. if(dmin>64*4){
  880. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  881. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  882. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  883. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  884. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  885. }
  886. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  887. *mx_ptr= best[0];
  888. *my_ptr= best[1];
  889. // printf("%d %d %d \n", best[0], best[1], dmin);
  890. return dmin;
  891. }
  892. //try to merge with above FIXME (needs PSNR test)
  893. static int epzs_motion_search2(MpegEncContext * s,
  894. int *mx_ptr, int *my_ptr, int P[10][2],
  895. int src_index, int ref_index, int16_t (*last_mv)[2],
  896. int ref_mv_scale)
  897. {
  898. MotionEstContext * const c= &s->me;
  899. int best[2]={0, 0};
  900. int d, dmin;
  901. int map_generation;
  902. const int penalty_factor= c->penalty_factor;
  903. const int size=0; //FIXME pass as arg
  904. const int h=8;
  905. const int ref_mv_stride= s->mb_stride;
  906. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  907. me_cmp_func cmpf, chroma_cmpf;
  908. LOAD_COMMON
  909. int flags= c->flags;
  910. LOAD_COMMON2
  911. cmpf= s->dsp.me_cmp[size];
  912. chroma_cmpf= s->dsp.me_cmp[size+1];
  913. map_generation= update_map_generation(c);
  914. dmin = 1000000;
  915. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  916. /* first line */
  917. if (s->first_slice_line) {
  918. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  919. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  920. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  921. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  922. }else{
  923. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  924. //FIXME try some early stop
  925. if(dmin>64*2){
  926. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  927. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  928. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  929. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  930. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  931. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  932. }
  933. }
  934. if(dmin>64*4){
  935. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  936. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  937. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  938. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  939. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  940. }
  941. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  942. *mx_ptr= best[0];
  943. *my_ptr= best[1];
  944. // printf("%d %d %d \n", best[0], best[1], dmin);
  945. return dmin;
  946. }