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.

1102 lines
38KB

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