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.

1154 lines
41KB

  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. //FIXME ref2_y next_pic?
  25. //lets hope gcc will remove the unused vars ...(gcc 3.2.2 seems to do it ...)
  26. //Note, the last line is there to kill these ugly unused var warnings
  27. #define LOAD_COMMON\
  28. uint32_t * const score_map= s->me.score_map;\
  29. const int time_pp= s->pp_time;\
  30. const int time_pb= s->pb_time;\
  31. const int xmin= s->me.xmin;\
  32. const int ymin= s->me.ymin;\
  33. const int xmax= s->me.xmax;\
  34. const int ymax= s->me.ymax;\
  35. uint8_t * const src_y= src_data[0];\
  36. uint8_t * const src_u= src_data[1];\
  37. uint8_t * const src_v= src_data[2];\
  38. uint8_t * const ref_y= ref_data[0];\
  39. uint8_t * const ref_u= ref_data[1];\
  40. uint8_t * const ref_v= ref_data[2];\
  41. op_pixels_func (*hpel_put)[4];\
  42. op_pixels_func (*hpel_avg)[4]= &s->dsp.avg_pixels_tab[size];\
  43. op_pixels_func (*chroma_hpel_put)[4];\
  44. qpel_mc_func (*qpel_put)[16];\
  45. qpel_mc_func (*qpel_avg)[16]= &s->dsp.avg_qpel_pixels_tab[size];\
  46. 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\
  47. + (size_t)hpel_avg + (size_t)qpel_avg + (size_t)score_map\
  48. + xmin + xmax + ymin + ymax;\
  49. if(s->no_rounding /*FIXME b_type*/){\
  50. hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];\
  51. chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];\
  52. qpel_put= &s->dsp.put_no_rnd_qpel_pixels_tab[size];\
  53. }else{\
  54. hpel_put=& s->dsp.put_pixels_tab[size];\
  55. chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];\
  56. qpel_put= &s->dsp.put_qpel_pixels_tab[size];\
  57. }
  58. #ifdef CMP_HPEL
  59. #define CHECK_HALF_MV(dx, dy, x, y)\
  60. {\
  61. const int hx= 2*(x)+(dx);\
  62. const int hy= 2*(y)+(dy);\
  63. CMP_HPEL(d, dx, dy, x, y, size);\
  64. d += (mv_penalty[hx - pred_x] + mv_penalty[hy - pred_y])*penalty_factor;\
  65. COPY3_IF_LT(dmin, d, bx, hx, by, hy)\
  66. }
  67. #if 0
  68. static int RENAME(hpel_motion_search)(MpegEncContext * s,
  69. int *mx_ptr, int *my_ptr, int dmin,
  70. int pred_x, int pred_y, uint8_t *ref_data[3],
  71. int size, uint8_t * const mv_penalty)
  72. {
  73. const int xx = 16 * s->mb_x + 8*(n&1);
  74. const int yy = 16 * s->mb_y + 8*(n>>1);
  75. const int mx = *mx_ptr;
  76. const int my = *my_ptr;
  77. const int penalty_factor= s->me.sub_penalty_factor;
  78. LOAD_COMMON
  79. // INIT;
  80. //FIXME factorize
  81. me_cmp_func cmp, chroma_cmp, cmp_sub, chroma_cmp_sub;
  82. if(s->no_rounding /*FIXME b_type*/){
  83. hpel_put= &s->dsp.put_no_rnd_pixels_tab[size];
  84. chroma_hpel_put= &s->dsp.put_no_rnd_pixels_tab[size+1];
  85. }else{
  86. hpel_put=& s->dsp.put_pixels_tab[size];
  87. chroma_hpel_put= &s->dsp.put_pixels_tab[size+1];
  88. }
  89. cmp= s->dsp.me_cmp[size];
  90. chroma_cmp= s->dsp.me_cmp[size+1];
  91. cmp_sub= s->dsp.me_sub_cmp[size];
  92. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  93. if(s->me.skip){ //FIXME somehow move up (benchmark)
  94. *mx_ptr = 0;
  95. *my_ptr = 0;
  96. return dmin;
  97. }
  98. if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
  99. CMP_HPEL(dmin, 0, 0, mx, my, size);
  100. if(mx || my)
  101. dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
  102. }
  103. if (mx > xmin && mx < xmax &&
  104. my > ymin && my < ymax) {
  105. int bx=2*mx, by=2*my;
  106. int d= dmin;
  107. CHECK_HALF_MV(1, 1, mx-1, my-1)
  108. CHECK_HALF_MV(0, 1, mx , my-1)
  109. CHECK_HALF_MV(1, 1, mx , my-1)
  110. CHECK_HALF_MV(1, 0, mx-1, my )
  111. CHECK_HALF_MV(1, 0, mx , my )
  112. CHECK_HALF_MV(1, 1, mx-1, my )
  113. CHECK_HALF_MV(0, 1, mx , my )
  114. CHECK_HALF_MV(1, 1, mx , my )
  115. assert(bx >= xmin*2 || bx <= xmax*2 || by >= ymin*2 || by <= ymax*2);
  116. *mx_ptr = bx;
  117. *my_ptr = by;
  118. }else{
  119. *mx_ptr =2*mx;
  120. *my_ptr =2*my;
  121. }
  122. return dmin;
  123. }
  124. #else
  125. static int RENAME(hpel_motion_search)(MpegEncContext * s,
  126. int *mx_ptr, int *my_ptr, int dmin,
  127. int pred_x, int pred_y, uint8_t *src_data[3],
  128. uint8_t *ref_data[3], int stride, int uvstride,
  129. int size, int h, uint8_t * const mv_penalty)
  130. {
  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
  137. //FIXME factorize
  138. cmp_sub= s->dsp.me_sub_cmp[size];
  139. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  140. if(s->me.skip){ //FIXME move out of hpel?
  141. *mx_ptr = 0;
  142. *my_ptr = 0;
  143. return dmin;
  144. }
  145. if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
  146. CMP_HPEL(dmin, 0, 0, mx, my, size);
  147. if(mx || my || size>0)
  148. dmin += (mv_penalty[2*mx - pred_x] + mv_penalty[2*my - pred_y])*penalty_factor;
  149. }
  150. if (mx > xmin && mx < xmax &&
  151. my > ymin && my < ymax) {
  152. int d= dmin;
  153. const int index= (my<<ME_MAP_SHIFT) + mx;
  154. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
  155. + (mv_penalty[bx - pred_x] + mv_penalty[by-2 - pred_y])*s->me.penalty_factor;
  156. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)]
  157. + (mv_penalty[bx-2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
  158. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)]
  159. + (mv_penalty[bx+2 - pred_x] + mv_penalty[by - pred_y])*s->me.penalty_factor;
  160. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)]
  161. + (mv_penalty[bx - pred_x] + mv_penalty[by+2 - pred_y])*s->me.penalty_factor;
  162. #if 1
  163. int key;
  164. int map_generation= s->me.map_generation;
  165. #ifndef NDEBUG
  166. uint32_t *map= s->me.map;
  167. #endif
  168. key= ((my-1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
  169. assert(map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
  170. key= ((my+1)<<ME_MAP_MV_BITS) + (mx) + map_generation;
  171. assert(map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)] == key);
  172. key= ((my)<<ME_MAP_MV_BITS) + (mx+1) + map_generation;
  173. assert(map[(index+1)&(ME_MAP_SIZE-1)] == key);
  174. key= ((my)<<ME_MAP_MV_BITS) + (mx-1) + map_generation;
  175. assert(map[(index-1)&(ME_MAP_SIZE-1)] == key);
  176. #endif
  177. if(t<=b){
  178. CHECK_HALF_MV(0, 1, mx ,my-1)
  179. if(l<=r){
  180. CHECK_HALF_MV(1, 1, mx-1, my-1)
  181. if(t+r<=b+l){
  182. CHECK_HALF_MV(1, 1, mx , my-1)
  183. }else{
  184. CHECK_HALF_MV(1, 1, mx-1, my )
  185. }
  186. CHECK_HALF_MV(1, 0, mx-1, my )
  187. }else{
  188. CHECK_HALF_MV(1, 1, mx , my-1)
  189. if(t+l<=b+r){
  190. CHECK_HALF_MV(1, 1, mx-1, my-1)
  191. }else{
  192. CHECK_HALF_MV(1, 1, mx , my )
  193. }
  194. CHECK_HALF_MV(1, 0, mx , my )
  195. }
  196. }else{
  197. if(l<=r){
  198. if(t+l<=b+r){
  199. CHECK_HALF_MV(1, 1, mx-1, my-1)
  200. }else{
  201. CHECK_HALF_MV(1, 1, mx , my )
  202. }
  203. CHECK_HALF_MV(1, 0, mx-1, my)
  204. CHECK_HALF_MV(1, 1, mx-1, my)
  205. }else{
  206. if(t+r<=b+l){
  207. CHECK_HALF_MV(1, 1, mx , my-1)
  208. }else{
  209. CHECK_HALF_MV(1, 1, mx-1, my)
  210. }
  211. CHECK_HALF_MV(1, 0, mx , my)
  212. CHECK_HALF_MV(1, 1, mx , my)
  213. }
  214. CHECK_HALF_MV(0, 1, mx , my)
  215. }
  216. assert(bx >= xmin*2 && bx <= xmax*2 && by >= ymin*2 && by <= ymax*2);
  217. }
  218. *mx_ptr = bx;
  219. *my_ptr = by;
  220. return dmin;
  221. }
  222. #endif
  223. static int RENAME(hpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3],
  224. uint8_t *ref_data[3], int stride, int uvstride,
  225. uint8_t * const mv_penalty)
  226. {
  227. // const int check_luma= s->dsp.me_sub_cmp != s->dsp.mb_cmp;
  228. const int size= 0;
  229. const int h= 16;
  230. const int penalty_factor= s->me.mb_penalty_factor;
  231. me_cmp_func cmp_sub, chroma_cmp_sub;
  232. int d;
  233. LOAD_COMMON
  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 pred_x, int pred_y, uint8_t *src_data[3],
  258. uint8_t *ref_data[3], int stride, int uvstride,
  259. int size, int h, uint8_t * const mv_penalty)
  260. {
  261. const int mx = *mx_ptr;
  262. const int my = *my_ptr;
  263. const int penalty_factor= s->me.sub_penalty_factor;
  264. const int map_generation= s->me.map_generation;
  265. const int subpel_quality= s->avctx->me_subpel_quality;
  266. uint32_t *map= s->me.map;
  267. me_cmp_func cmp, chroma_cmp;
  268. me_cmp_func cmp_sub, chroma_cmp_sub;
  269. LOAD_COMMON
  270. cmp= s->dsp.me_cmp[size];
  271. chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
  272. //FIXME factorize
  273. cmp_sub= s->dsp.me_sub_cmp[size];
  274. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  275. if(s->me.skip){ //FIXME somehow move up (benchmark)
  276. *mx_ptr = 0;
  277. *my_ptr = 0;
  278. return dmin;
  279. }
  280. if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
  281. CMP_QPEL(dmin, 0, 0, mx, my, size);
  282. if(mx || my || size>0)
  283. dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
  284. }
  285. if (mx > xmin && mx < xmax &&
  286. my > ymin && my < ymax) {
  287. int bx=4*mx, by=4*my;
  288. int d= dmin;
  289. int i, nx, ny;
  290. const int index= (my<<ME_MAP_SHIFT) + mx;
  291. const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  292. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  293. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  294. const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  295. const int c= score_map[(index )&(ME_MAP_SIZE-1)];
  296. int best[8];
  297. int best_pos[8][2];
  298. memset(best, 64, sizeof(int)*8);
  299. #if 1
  300. if(s->me.dia_size>=2){
  301. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  302. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  303. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  304. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  305. for(ny= -3; ny <= 3; ny++){
  306. for(nx= -3; nx <= 3; nx++){
  307. const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
  308. const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
  309. const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
  310. int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
  311. int i;
  312. if((nx&3)==0 && (ny&3)==0) continue;
  313. score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  314. // if(nx&1) score-=1024*s->me.penalty_factor;
  315. // if(ny&1) score-=1024*s->me.penalty_factor;
  316. for(i=0; i<8; i++){
  317. if(score < best[i]){
  318. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  319. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  320. best[i]= score;
  321. best_pos[i][0]= nx + 4*mx;
  322. best_pos[i][1]= ny + 4*my;
  323. break;
  324. }
  325. }
  326. }
  327. }
  328. }else{
  329. int tl;
  330. const int cx = 4*(r - l);
  331. const int cx2= r + l - 2*c;
  332. const int cy = 4*(b - t);
  333. const int cy2= b + t - 2*c;
  334. int cxy;
  335. if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
  336. tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  337. }else{
  338. CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
  339. }
  340. cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
  341. assert(16*cx2 + 4*cx + 32*c == 32*r);
  342. assert(16*cx2 - 4*cx + 32*c == 32*l);
  343. assert(16*cy2 + 4*cy + 32*c == 32*b);
  344. assert(16*cy2 - 4*cy + 32*c == 32*t);
  345. assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
  346. for(ny= -3; ny <= 3; ny++){
  347. for(nx= -3; nx <= 3; nx++){
  348. int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
  349. int i;
  350. if((nx&3)==0 && (ny&3)==0) continue;
  351. score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  352. // if(nx&1) score-=32*s->me.penalty_factor;
  353. // if(ny&1) score-=32*s->me.penalty_factor;
  354. for(i=0; i<8; i++){
  355. if(score < best[i]){
  356. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  357. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  358. best[i]= score;
  359. best_pos[i][0]= nx + 4*mx;
  360. best_pos[i][1]= ny + 4*my;
  361. break;
  362. }
  363. }
  364. }
  365. }
  366. }
  367. for(i=0; i<subpel_quality; i++){
  368. nx= best_pos[i][0];
  369. ny= best_pos[i][1];
  370. CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
  371. }
  372. #if 0
  373. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  374. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  375. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  376. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  377. // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
  378. if(tl<br){
  379. // nx= FFMAX(4*mx - bx, bx - 4*mx);
  380. // ny= FFMAX(4*my - by, by - 4*my);
  381. static int stats[7][7], count;
  382. count++;
  383. stats[4*mx - bx + 3][4*my - by + 3]++;
  384. if(256*256*256*64 % count ==0){
  385. for(i=0; i<49; i++){
  386. if((i%7)==0) printf("\n");
  387. printf("%6d ", stats[0][i]);
  388. }
  389. printf("\n");
  390. }
  391. }
  392. #endif
  393. #else
  394. CHECK_QUARTER_MV(2, 2, mx-1, my-1)
  395. CHECK_QUARTER_MV(0, 2, mx , my-1)
  396. CHECK_QUARTER_MV(2, 2, mx , my-1)
  397. CHECK_QUARTER_MV(2, 0, mx , my )
  398. CHECK_QUARTER_MV(2, 2, mx , my )
  399. CHECK_QUARTER_MV(0, 2, mx , my )
  400. CHECK_QUARTER_MV(2, 2, mx-1, my )
  401. CHECK_QUARTER_MV(2, 0, mx-1, my )
  402. nx= bx;
  403. ny= by;
  404. for(i=0; i<8; i++){
  405. int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
  406. int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
  407. CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
  408. }
  409. #endif
  410. #if 0
  411. //outer ring
  412. CHECK_QUARTER_MV(1, 3, mx-1, my-1)
  413. CHECK_QUARTER_MV(1, 2, mx-1, my-1)
  414. CHECK_QUARTER_MV(1, 1, mx-1, my-1)
  415. CHECK_QUARTER_MV(2, 1, mx-1, my-1)
  416. CHECK_QUARTER_MV(3, 1, mx-1, my-1)
  417. CHECK_QUARTER_MV(0, 1, mx , my-1)
  418. CHECK_QUARTER_MV(1, 1, mx , my-1)
  419. CHECK_QUARTER_MV(2, 1, mx , my-1)
  420. CHECK_QUARTER_MV(3, 1, mx , my-1)
  421. CHECK_QUARTER_MV(3, 2, mx , my-1)
  422. CHECK_QUARTER_MV(3, 3, mx , my-1)
  423. CHECK_QUARTER_MV(3, 0, mx , my )
  424. CHECK_QUARTER_MV(3, 1, mx , my )
  425. CHECK_QUARTER_MV(3, 2, mx , my )
  426. CHECK_QUARTER_MV(3, 3, mx , my )
  427. CHECK_QUARTER_MV(2, 3, mx , my )
  428. CHECK_QUARTER_MV(1, 3, mx , my )
  429. CHECK_QUARTER_MV(0, 3, mx , my )
  430. CHECK_QUARTER_MV(3, 3, mx-1, my )
  431. CHECK_QUARTER_MV(2, 3, mx-1, my )
  432. CHECK_QUARTER_MV(1, 3, mx-1, my )
  433. CHECK_QUARTER_MV(1, 2, mx-1, my )
  434. CHECK_QUARTER_MV(1, 1, mx-1, my )
  435. CHECK_QUARTER_MV(1, 0, mx-1, my )
  436. #endif
  437. assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
  438. *mx_ptr = bx;
  439. *my_ptr = by;
  440. }else{
  441. *mx_ptr =4*mx;
  442. *my_ptr =4*my;
  443. }
  444. return dmin;
  445. }
  446. static int RENAME(qpel_get_mb_score)(MpegEncContext * s, int mx, int my, int pred_x, int pred_y, uint8_t *src_data[3],
  447. uint8_t *ref_data[3], int stride, int uvstride,
  448. uint8_t * const mv_penalty)
  449. {
  450. const int size= 0;
  451. const int h= 16;
  452. const int penalty_factor= s->me.mb_penalty_factor;
  453. me_cmp_func cmp_sub, chroma_cmp_sub;
  454. int d;
  455. LOAD_COMMON
  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. uint8_t *src_data[3],
  514. uint8_t *ref_data[3], int stride, int uvstride,
  515. int const pred_x, int const pred_y, int const penalty_factor,
  516. int const shift,
  517. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  518. )
  519. {
  520. me_cmp_func cmp, chroma_cmp;
  521. int next_dir=-1;
  522. LOAD_COMMON
  523. cmp= s->dsp.me_cmp[size];
  524. chroma_cmp= s->dsp.me_cmp[size+1];
  525. { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
  526. const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
  527. const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
  528. if(map[index]!=key){ //this will be executed only very rarey
  529. CMP(score_map[index], best[0], best[1], size);
  530. map[index]= key;
  531. }
  532. }
  533. for(;;){
  534. int d;
  535. const int dir= next_dir;
  536. const int x= best[0];
  537. const int y= best[1];
  538. next_dir=-1;
  539. //printf("%d", dir);
  540. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  541. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  542. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  543. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  544. if(next_dir==-1){
  545. return dmin;
  546. }
  547. }
  548. }
  549. static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
  550. uint8_t *src_data[3],
  551. uint8_t *ref_data[3], int stride, int uvstride,
  552. int const pred_x, int const pred_y, int const penalty_factor,
  553. int const shift,
  554. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  555. )
  556. {
  557. me_cmp_func cmp, chroma_cmp;
  558. int dia_size;
  559. LOAD_COMMON
  560. cmp= s->dsp.me_cmp[size];
  561. chroma_cmp= s->dsp.me_cmp[size+1];
  562. for(dia_size=1; dia_size<=4; dia_size++){
  563. int dir;
  564. const int x= best[0];
  565. const int y= best[1];
  566. if(dia_size&(dia_size-1)) continue;
  567. if( x + dia_size > xmax
  568. || x - dia_size < xmin
  569. || y + dia_size > ymax
  570. || y - dia_size < ymin)
  571. continue;
  572. for(dir= 0; dir<dia_size; dir+=2){
  573. int d;
  574. CHECK_MV(x + dir , y + dia_size - dir);
  575. CHECK_MV(x + dia_size - dir, y - dir );
  576. CHECK_MV(x - dir , y - dia_size + dir);
  577. CHECK_MV(x - dia_size + dir, y + dir );
  578. }
  579. if(x!=best[0] || y!=best[1])
  580. dia_size=0;
  581. #if 0
  582. {
  583. int dx, dy, i;
  584. static int stats[8*8];
  585. dx= ABS(x-best[0]);
  586. dy= ABS(y-best[1]);
  587. if(dy>dx){
  588. dx^=dy; dy^=dx; dx^=dy;
  589. }
  590. stats[dy*8 + dx] ++;
  591. if(256*256*256*64 % (stats[0]+1)==0){
  592. for(i=0; i<64; i++){
  593. if((i&7)==0) printf("\n");
  594. printf("%8d ", stats[i]);
  595. }
  596. printf("\n");
  597. }
  598. }
  599. #endif
  600. }
  601. return dmin;
  602. }
  603. #define SAB_CHECK_MV(ax,ay)\
  604. {\
  605. const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
  606. const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
  607. /*printf("sab check %d %d\n", ax, ay);*/\
  608. if(map[index]!=key){\
  609. CMP(d, ax, ay, size);\
  610. map[index]= key;\
  611. score_map[index]= d;\
  612. d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
  613. /*printf("score: %d\n", d);*/\
  614. if(d < minima[minima_count-1].height){\
  615. int j=0;\
  616. \
  617. while(d >= minima[j].height) j++;\
  618. \
  619. memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
  620. \
  621. minima[j].checked= 0;\
  622. minima[j].height= d;\
  623. minima[j].x= ax;\
  624. minima[j].y= ay;\
  625. \
  626. i=-1;\
  627. continue;\
  628. }\
  629. }\
  630. }
  631. #define MAX_SAB_SIZE 16
  632. static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
  633. uint8_t *src_data[3],
  634. uint8_t *ref_data[3], int stride, int uvstride,
  635. int const pred_x, int const pred_y, int const penalty_factor,
  636. int const shift,
  637. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  638. )
  639. {
  640. me_cmp_func cmp, chroma_cmp;
  641. Minima minima[MAX_SAB_SIZE];
  642. const int minima_count= ABS(s->me.dia_size);
  643. int i, j;
  644. LOAD_COMMON
  645. cmp= s->dsp.me_cmp[size];
  646. chroma_cmp= s->dsp.me_cmp[size+1];
  647. for(j=i=0; i<ME_MAP_SIZE; i++){
  648. uint32_t key= map[i];
  649. key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
  650. if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
  651. assert(j<MAX_SAB_SIZE); //max j = number of predictors
  652. minima[j].height= score_map[i];
  653. minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
  654. minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
  655. minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
  656. minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
  657. minima[j].checked=0;
  658. if(minima[j].x || minima[j].y)
  659. minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
  660. j++;
  661. }
  662. qsort(minima, j, sizeof(Minima), minima_cmp);
  663. for(; j<minima_count; j++){
  664. minima[j].height=256*256*256*64;
  665. minima[j].checked=0;
  666. minima[j].x= minima[j].y=0;
  667. }
  668. for(i=0; i<minima_count; i++){
  669. const int x= minima[i].x;
  670. const int y= minima[i].y;
  671. int d;
  672. if(minima[i].checked) continue;
  673. if( x >= xmax || x <= xmin
  674. || y >= ymax || y <= ymin)
  675. continue;
  676. SAB_CHECK_MV(x-1, y)
  677. SAB_CHECK_MV(x+1, y)
  678. SAB_CHECK_MV(x , y-1)
  679. SAB_CHECK_MV(x , y+1)
  680. minima[i].checked= 1;
  681. }
  682. best[0]= minima[0].x;
  683. best[1]= minima[0].y;
  684. dmin= minima[0].height;
  685. if( best[0] < xmax && best[0] > xmin
  686. && best[1] < ymax && best[1] > ymin){
  687. int d;
  688. //ensure that the refernece samples for hpel refinement are in the map
  689. CHECK_MV(best[0]-1, best[1])
  690. CHECK_MV(best[0]+1, best[1])
  691. CHECK_MV(best[0], best[1]-1)
  692. CHECK_MV(best[0], best[1]+1)
  693. }
  694. return dmin;
  695. }
  696. static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
  697. uint8_t *src_data[3],
  698. uint8_t *ref_data[3], int stride, int uvstride,
  699. int const pred_x, int const pred_y, int const penalty_factor,
  700. int const shift,
  701. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  702. )
  703. {
  704. me_cmp_func cmp, chroma_cmp;
  705. int dia_size;
  706. LOAD_COMMON
  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,
  763. int *mx_ptr, int *my_ptr,
  764. int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3],
  765. uint8_t *ref_data[3], int stride, int uvstride, 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 h=16;
  776. const int ref_mv_stride= s->mb_stride; //pass as arg FIXME
  777. const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
  778. me_cmp_func cmp, chroma_cmp;
  779. LOAD_COMMON
  780. cmp= s->dsp.me_cmp[size];
  781. chroma_cmp= s->dsp.me_cmp[size+1];
  782. map_generation= update_map_generation(s);
  783. CMP(dmin, 0, 0, size);
  784. map[0]= map_generation;
  785. score_map[0]= dmin;
  786. /* first line */
  787. if (s->mb_y == 0) {
  788. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  789. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  790. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  791. }else{
  792. if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
  793. |P_TOP[0] |P_TOP[1]
  794. |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
  795. *mx_ptr= 0;
  796. *my_ptr= 0;
  797. s->me.skip=1;
  798. return dmin;
  799. }
  800. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  801. if(dmin>256*2){
  802. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  803. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  804. CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
  805. CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
  806. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  807. }
  808. }
  809. if(dmin>256*4){
  810. if(s->me.pre_pass){
  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. }else{
  816. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  817. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  818. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  819. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  820. }
  821. }
  822. if(s->avctx->last_predictor_count){
  823. const int count= s->avctx->last_predictor_count;
  824. const int xstart= FFMAX(0, s->mb_x - count);
  825. const int ystart= FFMAX(0, s->mb_y - count);
  826. const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
  827. const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
  828. int mb_y;
  829. for(mb_y=ystart; mb_y<yend; mb_y++){
  830. int mb_x;
  831. for(mb_x=xstart; mb_x<xend; mb_x++){
  832. const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
  833. int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
  834. int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
  835. if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
  836. CHECK_MV(mx,my)
  837. }
  838. }
  839. }
  840. //check(best[0],best[1],0, b0)
  841. if(s->me.dia_size==-1)
  842. dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  843. pred_x, pred_y, penalty_factor,
  844. shift, map, map_generation, size, h, mv_penalty);
  845. else if(s->me.dia_size<-1)
  846. dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  847. pred_x, pred_y, penalty_factor,
  848. shift, map, map_generation, size, h, mv_penalty);
  849. else if(s->me.dia_size<2)
  850. dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  851. pred_x, pred_y, penalty_factor,
  852. shift, map, map_generation, size, h, mv_penalty);
  853. else
  854. dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  855. pred_x, pred_y, penalty_factor,
  856. shift, map, map_generation, size, h, mv_penalty);
  857. //check(best[0],best[1],0, b1)
  858. *mx_ptr= best[0];
  859. *my_ptr= best[1];
  860. // printf("%d %d %d \n", best[0], best[1], dmin);
  861. return dmin;
  862. }
  863. #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
  864. static int RENAME(epzs_motion_search4)(MpegEncContext * s,
  865. int *mx_ptr, int *my_ptr,
  866. int P[10][2], int pred_x, int pred_y,
  867. uint8_t *src_data[3],
  868. uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
  869. int ref_mv_scale, uint8_t * const mv_penalty)
  870. {
  871. int best[2]={0, 0};
  872. int d, dmin;
  873. const int shift= 1+s->quarter_sample;
  874. uint32_t *map= s->me.map;
  875. int map_generation;
  876. const int penalty_factor= s->me.penalty_factor;
  877. const int size=1;
  878. const int h=8;
  879. const int ref_mv_stride= s->mb_stride;
  880. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  881. me_cmp_func cmp, chroma_cmp;
  882. LOAD_COMMON
  883. cmp= s->dsp.me_cmp[size];
  884. chroma_cmp= s->dsp.me_cmp[size+1];
  885. map_generation= update_map_generation(s);
  886. dmin = 1000000;
  887. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  888. /* first line */
  889. if (s->mb_y == 0/* && block<2*/) {
  890. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[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. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  894. }else{
  895. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  896. //FIXME try some early stop
  897. if(dmin>64*2){
  898. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  899. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  900. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  901. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  902. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  903. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  904. }
  905. }
  906. if(dmin>64*4){
  907. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  908. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  909. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  910. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  911. }
  912. if(s->me.dia_size==-1)
  913. dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  914. pred_x, pred_y, penalty_factor,
  915. shift, map, map_generation, size, h, mv_penalty);
  916. else if(s->me.dia_size<-1)
  917. dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  918. pred_x, pred_y, penalty_factor,
  919. shift, map, map_generation, size, h, mv_penalty);
  920. else if(s->me.dia_size<2)
  921. dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  922. pred_x, pred_y, penalty_factor,
  923. shift, map, map_generation, size, h, mv_penalty);
  924. else
  925. dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  926. pred_x, pred_y, penalty_factor,
  927. shift, map, map_generation, size, h, mv_penalty);
  928. *mx_ptr= best[0];
  929. *my_ptr= best[1];
  930. // printf("%d %d %d \n", best[0], best[1], dmin);
  931. return dmin;
  932. }
  933. //try to merge with above FIXME (needs PSNR test)
  934. static int RENAME(epzs_motion_search2)(MpegEncContext * s,
  935. int *mx_ptr, int *my_ptr,
  936. int P[10][2], int pred_x, int pred_y,
  937. uint8_t *src_data[3],
  938. uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
  939. int ref_mv_scale, uint8_t * const mv_penalty)
  940. {
  941. int best[2]={0, 0};
  942. int d, dmin;
  943. const int shift= 1+s->quarter_sample;
  944. uint32_t *map= s->me.map;
  945. int map_generation;
  946. const int penalty_factor= s->me.penalty_factor;
  947. const int size=0; //FIXME pass as arg
  948. const int h=8;
  949. const int ref_mv_stride= s->mb_stride;
  950. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  951. me_cmp_func cmp, chroma_cmp;
  952. LOAD_COMMON
  953. cmp= s->dsp.me_cmp[size];
  954. chroma_cmp= s->dsp.me_cmp[size+1];
  955. map_generation= update_map_generation(s);
  956. dmin = 1000000;
  957. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  958. /* first line */
  959. if (s->mb_y == 0) {
  960. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  961. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  962. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  963. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  964. }else{
  965. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  966. //FIXME try some early stop
  967. if(dmin>64*2){
  968. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  969. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  970. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  971. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  972. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  973. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  974. }
  975. }
  976. if(dmin>64*4){
  977. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  978. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  979. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  980. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  981. }
  982. if(s->me.dia_size==-1)
  983. dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  984. pred_x, pred_y, penalty_factor,
  985. shift, map, map_generation, size, h, mv_penalty);
  986. else if(s->me.dia_size<-1)
  987. dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  988. pred_x, pred_y, penalty_factor,
  989. shift, map, map_generation, size, h, mv_penalty);
  990. else if(s->me.dia_size<2)
  991. dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  992. pred_x, pred_y, penalty_factor,
  993. shift, map, map_generation, size, h, mv_penalty);
  994. else
  995. dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  996. pred_x, pred_y, penalty_factor,
  997. shift, map, map_generation, size, h, mv_penalty);
  998. *mx_ptr= best[0];
  999. *my_ptr= best[1];
  1000. // printf("%d %d %d \n", best[0], best[1], dmin);
  1001. return dmin;
  1002. }
  1003. #endif /* !CMP_DIRECT */