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.

1075 lines
38KB

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