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.

1089 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. //FIXME this could overflow (unlikely though)
  286. const int64_t t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
  287. const int64_t c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
  288. const int64_t b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
  289. int score= (ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2 + 512)>>10;
  290. int i;
  291. if((nx&3)==0 && (ny&3)==0) continue;
  292. score += (mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  293. // if(nx&1) score-=1024*c->penalty_factor;
  294. // if(ny&1) score-=1024*c->penalty_factor;
  295. for(i=0; i<8; i++){
  296. if(score < best[i]){
  297. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  298. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  299. best[i]= score;
  300. best_pos[i][0]= nx + 4*mx;
  301. best_pos[i][1]= ny + 4*my;
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. }else{
  308. int tl;
  309. //FIXME this could overflow (unlikely though)
  310. const int cx = 4*(r - l);
  311. const int cx2= r + l - 2*c;
  312. const int cy = 4*(b - t);
  313. const int cy2= b + t - 2*c;
  314. int cxy;
  315. if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
  316. tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  317. }else{
  318. 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
  319. }
  320. cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
  321. assert(16*cx2 + 4*cx + 32*c == 32*r);
  322. assert(16*cx2 - 4*cx + 32*c == 32*l);
  323. assert(16*cy2 + 4*cy + 32*c == 32*b);
  324. assert(16*cy2 - 4*cy + 32*c == 32*t);
  325. assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
  326. for(ny= -3; ny <= 3; ny++){
  327. for(nx= -3; nx <= 3; nx++){
  328. //FIXME this could overflow (unlikely though)
  329. int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
  330. int i;
  331. if((nx&3)==0 && (ny&3)==0) continue;
  332. score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  333. // if(nx&1) score-=32*c->penalty_factor;
  334. // if(ny&1) score-=32*c->penalty_factor;
  335. for(i=0; i<8; i++){
  336. if(score < best[i]){
  337. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  338. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  339. best[i]= score;
  340. best_pos[i][0]= nx + 4*mx;
  341. best_pos[i][1]= ny + 4*my;
  342. break;
  343. }
  344. }
  345. }
  346. }
  347. }
  348. for(i=0; i<subpel_quality; i++){
  349. nx= best_pos[i][0];
  350. ny= best_pos[i][1];
  351. CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
  352. }
  353. #if 0
  354. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  355. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  356. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  357. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  358. // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
  359. if(tl<br){
  360. // nx= FFMAX(4*mx - bx, bx - 4*mx);
  361. // ny= FFMAX(4*my - by, by - 4*my);
  362. static int stats[7][7], count;
  363. count++;
  364. stats[4*mx - bx + 3][4*my - by + 3]++;
  365. if(256*256*256*64 % count ==0){
  366. for(i=0; i<49; i++){
  367. if((i%7)==0) printf("\n");
  368. printf("%6d ", stats[0][i]);
  369. }
  370. printf("\n");
  371. }
  372. }
  373. #endif
  374. #else
  375. CHECK_QUARTER_MV(2, 2, mx-1, my-1)
  376. CHECK_QUARTER_MV(0, 2, mx , my-1)
  377. CHECK_QUARTER_MV(2, 2, mx , my-1)
  378. CHECK_QUARTER_MV(2, 0, mx , my )
  379. CHECK_QUARTER_MV(2, 2, mx , my )
  380. CHECK_QUARTER_MV(0, 2, mx , my )
  381. CHECK_QUARTER_MV(2, 2, mx-1, my )
  382. CHECK_QUARTER_MV(2, 0, mx-1, my )
  383. nx= bx;
  384. ny= by;
  385. for(i=0; i<8; i++){
  386. int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
  387. int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
  388. CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
  389. }
  390. #endif
  391. #if 0
  392. //outer ring
  393. CHECK_QUARTER_MV(1, 3, mx-1, my-1)
  394. CHECK_QUARTER_MV(1, 2, mx-1, my-1)
  395. CHECK_QUARTER_MV(1, 1, mx-1, my-1)
  396. CHECK_QUARTER_MV(2, 1, mx-1, my-1)
  397. CHECK_QUARTER_MV(3, 1, mx-1, my-1)
  398. CHECK_QUARTER_MV(0, 1, mx , my-1)
  399. CHECK_QUARTER_MV(1, 1, mx , my-1)
  400. CHECK_QUARTER_MV(2, 1, mx , my-1)
  401. CHECK_QUARTER_MV(3, 1, mx , my-1)
  402. CHECK_QUARTER_MV(3, 2, mx , my-1)
  403. CHECK_QUARTER_MV(3, 3, mx , my-1)
  404. CHECK_QUARTER_MV(3, 0, mx , my )
  405. CHECK_QUARTER_MV(3, 1, mx , my )
  406. CHECK_QUARTER_MV(3, 2, mx , my )
  407. CHECK_QUARTER_MV(3, 3, mx , my )
  408. CHECK_QUARTER_MV(2, 3, mx , my )
  409. CHECK_QUARTER_MV(1, 3, mx , my )
  410. CHECK_QUARTER_MV(0, 3, mx , my )
  411. CHECK_QUARTER_MV(3, 3, mx-1, my )
  412. CHECK_QUARTER_MV(2, 3, mx-1, my )
  413. CHECK_QUARTER_MV(1, 3, mx-1, my )
  414. CHECK_QUARTER_MV(1, 2, mx-1, my )
  415. CHECK_QUARTER_MV(1, 1, mx-1, my )
  416. CHECK_QUARTER_MV(1, 0, mx-1, my )
  417. #endif
  418. assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
  419. *mx_ptr = bx;
  420. *my_ptr = by;
  421. }else{
  422. *mx_ptr =4*mx;
  423. *my_ptr =4*my;
  424. }
  425. return dmin;
  426. }
  427. #define CHECK_MV(x,y)\
  428. {\
  429. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  430. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  431. assert((x) >= xmin);\
  432. assert((x) <= xmax);\
  433. assert((y) >= ymin);\
  434. assert((y) <= ymax);\
  435. /*printf("check_mv %d %d\n", x, y);*/\
  436. if(map[index]!=key){\
  437. d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  438. map[index]= key;\
  439. score_map[index]= d;\
  440. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  441. /*printf("score:%d\n", d);*/\
  442. COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
  443. }\
  444. }
  445. #define CHECK_CLIPED_MV(ax,ay)\
  446. {\
  447. const int x= ax;\
  448. const int y= ay;\
  449. const int x2= FFMAX(xmin, FFMIN(x, xmax));\
  450. const int y2= FFMAX(ymin, FFMIN(y, ymax));\
  451. CHECK_MV(x2, y2)\
  452. }
  453. #define CHECK_MV_DIR(x,y,new_dir)\
  454. {\
  455. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  456. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  457. /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
  458. if(map[index]!=key){\
  459. d= cmp(s, x, y, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  460. map[index]= key;\
  461. score_map[index]= d;\
  462. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  463. /*printf("score:%d\n", d);*/\
  464. if(d<dmin){\
  465. best[0]=x;\
  466. best[1]=y;\
  467. dmin=d;\
  468. next_dir= new_dir;\
  469. }\
  470. }\
  471. }
  472. #define check(x,y,S,v)\
  473. if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
  474. if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
  475. if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
  476. if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
  477. #define LOAD_COMMON2\
  478. uint32_t *map= c->map;\
  479. const int qpel= flags&FLAG_QPEL;\
  480. const int shift= 1+qpel;\
  481. static always_inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
  482. int src_index, int ref_index, int const penalty_factor,
  483. int size, int h, int flags)
  484. {
  485. MotionEstContext * const c= &s->me;
  486. me_cmp_func cmpf, chroma_cmpf;
  487. int next_dir=-1;
  488. LOAD_COMMON
  489. LOAD_COMMON2
  490. int map_generation= c->map_generation;
  491. cmpf= s->dsp.me_cmp[size];
  492. chroma_cmpf= s->dsp.me_cmp[size+1];
  493. { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
  494. const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
  495. const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
  496. if(map[index]!=key){ //this will be executed only very rarey
  497. score_map[index]= cmp(s, best[0], best[1], 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
  498. map[index]= key;
  499. }
  500. }
  501. for(;;){
  502. int d;
  503. const int dir= next_dir;
  504. const int x= best[0];
  505. const int y= best[1];
  506. next_dir=-1;
  507. //printf("%d", dir);
  508. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  509. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  510. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  511. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  512. if(next_dir==-1){
  513. return dmin;
  514. }
  515. }
  516. }
  517. static int funny_diamond_search(MpegEncContext * s, int *best, int dmin,
  518. int src_index, int ref_index, int const penalty_factor,
  519. int size, int h, int flags)
  520. {
  521. MotionEstContext * const c= &s->me;
  522. me_cmp_func cmpf, chroma_cmpf;
  523. int dia_size;
  524. LOAD_COMMON
  525. LOAD_COMMON2
  526. int map_generation= c->map_generation;
  527. cmpf= s->dsp.me_cmp[size];
  528. chroma_cmpf= s->dsp.me_cmp[size+1];
  529. for(dia_size=1; dia_size<=4; dia_size++){
  530. int dir;
  531. const int x= best[0];
  532. const int y= best[1];
  533. if(dia_size&(dia_size-1)) continue;
  534. if( x + dia_size > xmax
  535. || x - dia_size < xmin
  536. || y + dia_size > ymax
  537. || y - dia_size < ymin)
  538. continue;
  539. for(dir= 0; dir<dia_size; dir+=2){
  540. int d;
  541. CHECK_MV(x + dir , y + dia_size - dir);
  542. CHECK_MV(x + dia_size - dir, y - dir );
  543. CHECK_MV(x - dir , y - dia_size + dir);
  544. CHECK_MV(x - dia_size + dir, y + dir );
  545. }
  546. if(x!=best[0] || y!=best[1])
  547. dia_size=0;
  548. #if 0
  549. {
  550. int dx, dy, i;
  551. static int stats[8*8];
  552. dx= ABS(x-best[0]);
  553. dy= ABS(y-best[1]);
  554. if(dy>dx){
  555. dx^=dy; dy^=dx; dx^=dy;
  556. }
  557. stats[dy*8 + dx] ++;
  558. if(256*256*256*64 % (stats[0]+1)==0){
  559. for(i=0; i<64; i++){
  560. if((i&7)==0) printf("\n");
  561. printf("%8d ", stats[i]);
  562. }
  563. printf("\n");
  564. }
  565. }
  566. #endif
  567. }
  568. return dmin;
  569. }
  570. #define SAB_CHECK_MV(ax,ay)\
  571. {\
  572. const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
  573. const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
  574. /*printf("sab check %d %d\n", ax, ay);*/\
  575. if(map[index]!=key){\
  576. d= cmp(s, ax, ay, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);\
  577. map[index]= key;\
  578. score_map[index]= d;\
  579. d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
  580. /*printf("score: %d\n", d);*/\
  581. if(d < minima[minima_count-1].height){\
  582. int j=0;\
  583. \
  584. while(d >= minima[j].height) j++;\
  585. \
  586. memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
  587. \
  588. minima[j].checked= 0;\
  589. minima[j].height= d;\
  590. minima[j].x= ax;\
  591. minima[j].y= ay;\
  592. \
  593. i=-1;\
  594. continue;\
  595. }\
  596. }\
  597. }
  598. #define MAX_SAB_SIZE ME_MAP_SIZE
  599. static int sab_diamond_search(MpegEncContext * s, int *best, int dmin,
  600. int src_index, int ref_index, int const penalty_factor,
  601. int size, int h, int flags)
  602. {
  603. MotionEstContext * const c= &s->me;
  604. me_cmp_func cmpf, chroma_cmpf;
  605. Minima minima[MAX_SAB_SIZE];
  606. const int minima_count= ABS(c->dia_size);
  607. int i, j;
  608. LOAD_COMMON
  609. LOAD_COMMON2
  610. int map_generation= c->map_generation;
  611. cmpf= s->dsp.me_cmp[size];
  612. chroma_cmpf= s->dsp.me_cmp[size+1];
  613. for(j=i=0; i<ME_MAP_SIZE; i++){
  614. uint32_t key= map[i];
  615. key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
  616. if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
  617. assert(j<MAX_SAB_SIZE); //max j = number of predictors
  618. minima[j].height= score_map[i];
  619. minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
  620. minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
  621. minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
  622. minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
  623. minima[j].checked=0;
  624. if(minima[j].x || minima[j].y)
  625. minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
  626. j++;
  627. }
  628. qsort(minima, j, sizeof(Minima), minima_cmp);
  629. for(; j<minima_count; j++){
  630. minima[j].height=256*256*256*64;
  631. minima[j].checked=0;
  632. minima[j].x= minima[j].y=0;
  633. }
  634. for(i=0; i<minima_count; i++){
  635. const int x= minima[i].x;
  636. const int y= minima[i].y;
  637. int d;
  638. if(minima[i].checked) continue;
  639. if( x >= xmax || x <= xmin
  640. || y >= ymax || y <= ymin)
  641. continue;
  642. SAB_CHECK_MV(x-1, y)
  643. SAB_CHECK_MV(x+1, y)
  644. SAB_CHECK_MV(x , y-1)
  645. SAB_CHECK_MV(x , y+1)
  646. minima[i].checked= 1;
  647. }
  648. best[0]= minima[0].x;
  649. best[1]= minima[0].y;
  650. dmin= minima[0].height;
  651. if( best[0] < xmax && best[0] > xmin
  652. && best[1] < ymax && best[1] > ymin){
  653. int d;
  654. //ensure that the refernece samples for hpel refinement are in the map
  655. CHECK_MV(best[0]-1, best[1])
  656. CHECK_MV(best[0]+1, best[1])
  657. CHECK_MV(best[0], best[1]-1)
  658. CHECK_MV(best[0], best[1]+1)
  659. }
  660. return dmin;
  661. }
  662. static int var_diamond_search(MpegEncContext * s, int *best, int dmin,
  663. int src_index, int ref_index, int const penalty_factor,
  664. int size, int h, int flags)
  665. {
  666. MotionEstContext * const c= &s->me;
  667. me_cmp_func cmpf, chroma_cmpf;
  668. int dia_size;
  669. LOAD_COMMON
  670. LOAD_COMMON2
  671. int map_generation= c->map_generation;
  672. cmpf= s->dsp.me_cmp[size];
  673. chroma_cmpf= s->dsp.me_cmp[size+1];
  674. for(dia_size=1; dia_size<=c->dia_size; dia_size++){
  675. int dir, start, end;
  676. const int x= best[0];
  677. const int y= best[1];
  678. start= FFMAX(0, y + dia_size - ymax);
  679. end = FFMIN(dia_size, xmax - x + 1);
  680. for(dir= start; dir<end; dir++){
  681. int d;
  682. //check(x + dir,y + dia_size - dir,0, a0)
  683. CHECK_MV(x + dir , y + dia_size - dir);
  684. }
  685. start= FFMAX(0, x + dia_size - xmax);
  686. end = FFMIN(dia_size, y - ymin + 1);
  687. for(dir= start; dir<end; dir++){
  688. int d;
  689. //check(x + dia_size - dir, y - dir,0, a1)
  690. CHECK_MV(x + dia_size - dir, y - dir );
  691. }
  692. start= FFMAX(0, -y + dia_size + ymin );
  693. end = FFMIN(dia_size, x - xmin + 1);
  694. for(dir= start; dir<end; dir++){
  695. int d;
  696. //check(x - dir,y - dia_size + dir,0, a2)
  697. CHECK_MV(x - dir , y - dia_size + dir);
  698. }
  699. start= FFMAX(0, -x + dia_size + xmin );
  700. end = FFMIN(dia_size, ymax - y + 1);
  701. for(dir= start; dir<end; dir++){
  702. int d;
  703. //check(x - dia_size + dir, y + dir,0, a3)
  704. CHECK_MV(x - dia_size + dir, y + dir );
  705. }
  706. if(x!=best[0] || y!=best[1])
  707. dia_size=0;
  708. #if 0
  709. {
  710. int dx, dy, i;
  711. static int stats[8*8];
  712. dx= ABS(x-best[0]);
  713. dy= ABS(y-best[1]);
  714. stats[dy*8 + dx] ++;
  715. if(256*256*256*64 % (stats[0]+1)==0){
  716. for(i=0; i<64; i++){
  717. if((i&7)==0) printf("\n");
  718. printf("%6d ", stats[i]);
  719. }
  720. printf("\n");
  721. }
  722. }
  723. #endif
  724. }
  725. return dmin;
  726. }
  727. static always_inline int diamond_search(MpegEncContext * s, int *best, int dmin,
  728. int src_index, int ref_index, int const penalty_factor,
  729. int size, int h, int flags){
  730. MotionEstContext * const c= &s->me;
  731. if(c->dia_size==-1)
  732. return funny_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  733. else if(c->dia_size<-1)
  734. return sab_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  735. else if(c->dia_size<2)
  736. return small_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  737. else
  738. return var_diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  739. }
  740. static always_inline int epzs_motion_search_internal(MpegEncContext * s, int *mx_ptr, int *my_ptr,
  741. int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
  742. int ref_mv_scale, int flags)
  743. {
  744. MotionEstContext * const c= &s->me;
  745. int best[2]={0, 0};
  746. int d, dmin;
  747. int map_generation;
  748. const int penalty_factor= c->penalty_factor;
  749. const int size=0;
  750. const int h=16;
  751. const int ref_mv_stride= s->mb_stride; //pass as arg FIXME
  752. const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
  753. me_cmp_func cmpf, chroma_cmpf;
  754. LOAD_COMMON
  755. LOAD_COMMON2
  756. cmpf= s->dsp.me_cmp[size];
  757. chroma_cmpf= s->dsp.me_cmp[size+1];
  758. map_generation= update_map_generation(c);
  759. dmin= cmp(s, 0, 0, 0, 0, size, h, ref_index, src_index, cmpf, chroma_cmpf, flags);
  760. map[0]= map_generation;
  761. score_map[0]= dmin;
  762. /* first line */
  763. if (s->first_slice_line) {
  764. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  765. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  766. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  767. }else{
  768. if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
  769. |P_TOP[0] |P_TOP[1]
  770. |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
  771. *mx_ptr= 0;
  772. *my_ptr= 0;
  773. c->skip=1;
  774. return dmin;
  775. }
  776. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  777. if(dmin>256*2){
  778. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  779. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  780. CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
  781. CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
  782. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  783. }
  784. }
  785. if(dmin>256*4){
  786. if(c->pre_pass){
  787. CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
  788. (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
  789. if(!s->first_slice_line)
  790. CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  791. (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  792. }else{
  793. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  794. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  795. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  796. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  797. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  798. }
  799. }
  800. if(c->avctx->last_predictor_count){
  801. const int count= c->avctx->last_predictor_count;
  802. const int xstart= FFMAX(0, s->mb_x - count);
  803. const int ystart= FFMAX(0, s->mb_y - count);
  804. const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
  805. const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
  806. int mb_y;
  807. for(mb_y=ystart; mb_y<yend; mb_y++){
  808. int mb_x;
  809. for(mb_x=xstart; mb_x<xend; mb_x++){
  810. const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
  811. int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
  812. int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
  813. if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
  814. CHECK_MV(mx,my)
  815. }
  816. }
  817. }
  818. //check(best[0],best[1],0, b0)
  819. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  820. //check(best[0],best[1],0, b1)
  821. *mx_ptr= best[0];
  822. *my_ptr= best[1];
  823. // printf("%d %d %d \n", best[0], best[1], dmin);
  824. return dmin;
  825. }
  826. //this function is dedicated to the braindamaged gcc
  827. static inline int epzs_motion_search(MpegEncContext * s, int *mx_ptr, int *my_ptr,
  828. int P[10][2], int src_index, int ref_index, int16_t (*last_mv)[2],
  829. int ref_mv_scale)
  830. {
  831. MotionEstContext * const c= &s->me;
  832. //FIXME convert other functions in the same way if faster
  833. switch(c->flags){
  834. case 0:
  835. return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, 0);
  836. // case FLAG_QPEL:
  837. // return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, FLAG_QPEL);
  838. default:
  839. return epzs_motion_search_internal(s, mx_ptr, my_ptr, P, src_index, ref_index, last_mv, ref_mv_scale, c->flags);
  840. }
  841. }
  842. static int epzs_motion_search4(MpegEncContext * s,
  843. int *mx_ptr, int *my_ptr, int P[10][2],
  844. int src_index, int ref_index, int16_t (*last_mv)[2],
  845. int ref_mv_scale)
  846. {
  847. MotionEstContext * const c= &s->me;
  848. int best[2]={0, 0};
  849. int d, dmin;
  850. int map_generation;
  851. const int penalty_factor= c->penalty_factor;
  852. const int size=1;
  853. const int h=8;
  854. const int ref_mv_stride= s->mb_stride;
  855. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  856. me_cmp_func cmpf, chroma_cmpf;
  857. LOAD_COMMON
  858. int flags= c->flags;
  859. LOAD_COMMON2
  860. cmpf= s->dsp.me_cmp[size];
  861. chroma_cmpf= s->dsp.me_cmp[size+1];
  862. map_generation= update_map_generation(c);
  863. dmin = 1000000;
  864. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  865. /* first line */
  866. if (s->first_slice_line) {
  867. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  868. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  869. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  870. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  871. }else{
  872. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  873. //FIXME try some early stop
  874. if(dmin>64*2){
  875. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  876. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  877. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  878. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  879. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  880. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  881. }
  882. }
  883. if(dmin>64*4){
  884. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  885. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  886. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  887. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  888. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  889. }
  890. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  891. *mx_ptr= best[0];
  892. *my_ptr= best[1];
  893. // printf("%d %d %d \n", best[0], best[1], dmin);
  894. return dmin;
  895. }
  896. //try to merge with above FIXME (needs PSNR test)
  897. static int epzs_motion_search2(MpegEncContext * s,
  898. int *mx_ptr, int *my_ptr, int P[10][2],
  899. int src_index, int ref_index, int16_t (*last_mv)[2],
  900. int ref_mv_scale)
  901. {
  902. MotionEstContext * const c= &s->me;
  903. int best[2]={0, 0};
  904. int d, dmin;
  905. int map_generation;
  906. const int penalty_factor= c->penalty_factor;
  907. const int size=0; //FIXME pass as arg
  908. const int h=8;
  909. const int ref_mv_stride= s->mb_stride;
  910. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  911. me_cmp_func cmpf, chroma_cmpf;
  912. LOAD_COMMON
  913. int flags= c->flags;
  914. LOAD_COMMON2
  915. cmpf= s->dsp.me_cmp[size];
  916. chroma_cmpf= s->dsp.me_cmp[size+1];
  917. map_generation= update_map_generation(c);
  918. dmin = 1000000;
  919. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  920. /* first line */
  921. if (s->first_slice_line) {
  922. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  923. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  924. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  925. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  926. }else{
  927. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  928. //FIXME try some early stop
  929. if(dmin>64*2){
  930. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  931. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  932. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  933. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  934. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  935. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  936. }
  937. }
  938. if(dmin>64*4){
  939. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  940. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  941. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  942. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  943. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  944. }
  945. dmin= diamond_search(s, best, dmin, src_index, ref_index, penalty_factor, size, h, flags);
  946. *mx_ptr= best[0];
  947. *my_ptr= best[1];
  948. // printf("%d %d %d \n", best[0], best[1], dmin);
  949. return dmin;
  950. }