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.

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