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.

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