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.

1070 lines
37KB

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