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.

1160 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= ax;\
  485. const int y= ay;\
  486. const int x2= FFMAX(xmin, FFMIN(x, xmax));\
  487. const int y2= FFMAX(ymin, FFMIN(y, ymax));\
  488. CHECK_MV(x2, y2)\
  489. }
  490. #define CHECK_MV_DIR(x,y,new_dir)\
  491. {\
  492. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  493. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  494. /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
  495. if(map[index]!=key){\
  496. CMP(d, x, y, size);\
  497. map[index]= key;\
  498. score_map[index]= d;\
  499. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  500. /*printf("score:%d\n", d);*/\
  501. if(d<dmin){\
  502. best[0]=x;\
  503. best[1]=y;\
  504. dmin=d;\
  505. next_dir= new_dir;\
  506. }\
  507. }\
  508. }
  509. #define check(x,y,S,v)\
  510. if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
  511. if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
  512. if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
  513. if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
  514. static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
  515. uint8_t *src_data[3],
  516. uint8_t *ref_data[3], int stride, int uvstride,
  517. int const pred_x, int const pred_y, int const penalty_factor,
  518. int const shift,
  519. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  520. )
  521. {
  522. me_cmp_func cmp, chroma_cmp;
  523. int next_dir=-1;
  524. LOAD_COMMON
  525. cmp= s->dsp.me_cmp[size];
  526. chroma_cmp= s->dsp.me_cmp[size+1];
  527. { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
  528. const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
  529. const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
  530. if(map[index]!=key){ //this will be executed only very rarey
  531. CMP(score_map[index], best[0], best[1], size);
  532. map[index]= key;
  533. }
  534. }
  535. for(;;){
  536. int d;
  537. const int dir= next_dir;
  538. const int x= best[0];
  539. const int y= best[1];
  540. next_dir=-1;
  541. //printf("%d", dir);
  542. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  543. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  544. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  545. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  546. if(next_dir==-1){
  547. return dmin;
  548. }
  549. }
  550. }
  551. static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
  552. uint8_t *src_data[3],
  553. uint8_t *ref_data[3], int stride, int uvstride,
  554. int const pred_x, int const pred_y, int const penalty_factor,
  555. int const shift,
  556. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  557. )
  558. {
  559. me_cmp_func cmp, chroma_cmp;
  560. int dia_size;
  561. LOAD_COMMON
  562. cmp= s->dsp.me_cmp[size];
  563. chroma_cmp= s->dsp.me_cmp[size+1];
  564. for(dia_size=1; dia_size<=4; dia_size++){
  565. int dir;
  566. const int x= best[0];
  567. const int y= best[1];
  568. if(dia_size&(dia_size-1)) continue;
  569. if( x + dia_size > xmax
  570. || x - dia_size < xmin
  571. || y + dia_size > ymax
  572. || y - dia_size < ymin)
  573. continue;
  574. for(dir= 0; dir<dia_size; dir+=2){
  575. int d;
  576. CHECK_MV(x + dir , y + dia_size - dir);
  577. CHECK_MV(x + dia_size - dir, y - dir );
  578. CHECK_MV(x - dir , y - dia_size + dir);
  579. CHECK_MV(x - dia_size + dir, y + dir );
  580. }
  581. if(x!=best[0] || y!=best[1])
  582. dia_size=0;
  583. #if 0
  584. {
  585. int dx, dy, i;
  586. static int stats[8*8];
  587. dx= ABS(x-best[0]);
  588. dy= ABS(y-best[1]);
  589. if(dy>dx){
  590. dx^=dy; dy^=dx; dx^=dy;
  591. }
  592. stats[dy*8 + dx] ++;
  593. if(256*256*256*64 % (stats[0]+1)==0){
  594. for(i=0; i<64; i++){
  595. if((i&7)==0) printf("\n");
  596. printf("%8d ", stats[i]);
  597. }
  598. printf("\n");
  599. }
  600. }
  601. #endif
  602. }
  603. return dmin;
  604. }
  605. #define SAB_CHECK_MV(ax,ay)\
  606. {\
  607. const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
  608. const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
  609. /*printf("sab check %d %d\n", ax, ay);*/\
  610. if(map[index]!=key){\
  611. CMP(d, ax, ay, size);\
  612. map[index]= key;\
  613. score_map[index]= d;\
  614. d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
  615. /*printf("score: %d\n", d);*/\
  616. if(d < minima[minima_count-1].height){\
  617. int j=0;\
  618. \
  619. while(d >= minima[j].height) j++;\
  620. \
  621. memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
  622. \
  623. minima[j].checked= 0;\
  624. minima[j].height= d;\
  625. minima[j].x= ax;\
  626. minima[j].y= ay;\
  627. \
  628. i=-1;\
  629. continue;\
  630. }\
  631. }\
  632. }
  633. #define MAX_SAB_SIZE 16
  634. static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
  635. uint8_t *src_data[3],
  636. uint8_t *ref_data[3], int stride, int uvstride,
  637. int const pred_x, int const pred_y, int const penalty_factor,
  638. int const shift,
  639. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  640. )
  641. {
  642. me_cmp_func cmp, chroma_cmp;
  643. Minima minima[MAX_SAB_SIZE];
  644. const int minima_count= ABS(s->me.dia_size);
  645. int i, j;
  646. LOAD_COMMON
  647. cmp= s->dsp.me_cmp[size];
  648. chroma_cmp= s->dsp.me_cmp[size+1];
  649. for(j=i=0; i<ME_MAP_SIZE; i++){
  650. uint32_t key= map[i];
  651. key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
  652. if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
  653. assert(j<MAX_SAB_SIZE); //max j = number of predictors
  654. minima[j].height= score_map[i];
  655. minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
  656. minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
  657. minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
  658. minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
  659. minima[j].checked=0;
  660. if(minima[j].x || minima[j].y)
  661. minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
  662. j++;
  663. }
  664. qsort(minima, j, sizeof(Minima), minima_cmp);
  665. for(; j<minima_count; j++){
  666. minima[j].height=256*256*256*64;
  667. minima[j].checked=0;
  668. minima[j].x= minima[j].y=0;
  669. }
  670. for(i=0; i<minima_count; i++){
  671. const int x= minima[i].x;
  672. const int y= minima[i].y;
  673. int d;
  674. if(minima[i].checked) continue;
  675. if( x >= xmax || x <= xmin
  676. || y >= ymax || y <= ymin)
  677. continue;
  678. SAB_CHECK_MV(x-1, y)
  679. SAB_CHECK_MV(x+1, y)
  680. SAB_CHECK_MV(x , y-1)
  681. SAB_CHECK_MV(x , y+1)
  682. minima[i].checked= 1;
  683. }
  684. best[0]= minima[0].x;
  685. best[1]= minima[0].y;
  686. dmin= minima[0].height;
  687. if( best[0] < xmax && best[0] > xmin
  688. && best[1] < ymax && best[1] > ymin){
  689. int d;
  690. //ensure that the refernece samples for hpel refinement are in the map
  691. CHECK_MV(best[0]-1, best[1])
  692. CHECK_MV(best[0]+1, best[1])
  693. CHECK_MV(best[0], best[1]-1)
  694. CHECK_MV(best[0], best[1]+1)
  695. }
  696. return dmin;
  697. }
  698. static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
  699. uint8_t *src_data[3],
  700. uint8_t *ref_data[3], int stride, int uvstride,
  701. int const pred_x, int const pred_y, int const penalty_factor,
  702. int const shift,
  703. uint32_t *map, int map_generation, int size, int h, uint8_t * const mv_penalty
  704. )
  705. {
  706. me_cmp_func cmp, chroma_cmp;
  707. int dia_size;
  708. LOAD_COMMON
  709. cmp= s->dsp.me_cmp[size];
  710. chroma_cmp= s->dsp.me_cmp[size+1];
  711. for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
  712. int dir, start, end;
  713. const int x= best[0];
  714. const int y= best[1];
  715. start= FFMAX(0, y + dia_size - ymax);
  716. end = FFMIN(dia_size, xmax - x + 1);
  717. for(dir= start; dir<end; dir++){
  718. int d;
  719. //check(x + dir,y + dia_size - dir,0, a0)
  720. CHECK_MV(x + dir , y + dia_size - dir);
  721. }
  722. start= FFMAX(0, x + dia_size - xmax);
  723. end = FFMIN(dia_size, y - ymin + 1);
  724. for(dir= start; dir<end; dir++){
  725. int d;
  726. //check(x + dia_size - dir, y - dir,0, a1)
  727. CHECK_MV(x + dia_size - dir, y - dir );
  728. }
  729. start= FFMAX(0, -y + dia_size + ymin );
  730. end = FFMIN(dia_size, x - xmin + 1);
  731. for(dir= start; dir<end; dir++){
  732. int d;
  733. //check(x - dir,y - dia_size + dir,0, a2)
  734. CHECK_MV(x - dir , y - dia_size + dir);
  735. }
  736. start= FFMAX(0, -x + dia_size + xmin );
  737. end = FFMIN(dia_size, ymax - y + 1);
  738. for(dir= start; dir<end; dir++){
  739. int d;
  740. //check(x - dia_size + dir, y + dir,0, a3)
  741. CHECK_MV(x - dia_size + dir, y + dir );
  742. }
  743. if(x!=best[0] || y!=best[1])
  744. dia_size=0;
  745. #if 0
  746. {
  747. int dx, dy, i;
  748. static int stats[8*8];
  749. dx= ABS(x-best[0]);
  750. dy= ABS(y-best[1]);
  751. stats[dy*8 + dx] ++;
  752. if(256*256*256*64 % (stats[0]+1)==0){
  753. for(i=0; i<64; i++){
  754. if((i&7)==0) printf("\n");
  755. printf("%6d ", stats[i]);
  756. }
  757. printf("\n");
  758. }
  759. }
  760. #endif
  761. }
  762. return dmin;
  763. }
  764. static int RENAME(epzs_motion_search)(MpegEncContext * s,
  765. int *mx_ptr, int *my_ptr,
  766. int P[10][2], int pred_x, int pred_y, uint8_t *src_data[3],
  767. uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
  768. int ref_mv_scale, uint8_t * const mv_penalty)
  769. {
  770. int best[2]={0, 0};
  771. int d, dmin;
  772. const int shift= 1+s->quarter_sample;
  773. uint32_t *map= s->me.map;
  774. int map_generation;
  775. const int penalty_factor= s->me.penalty_factor;
  776. const int size=0;
  777. const int h=16;
  778. const int ref_mv_stride= s->mb_stride; //pass as arg FIXME
  779. const int ref_mv_xy= s->mb_x + s->mb_y*ref_mv_stride; //add to last_mv beforepassing FIXME
  780. me_cmp_func cmp, chroma_cmp;
  781. LOAD_COMMON
  782. cmp= s->dsp.me_cmp[size];
  783. chroma_cmp= s->dsp.me_cmp[size+1];
  784. map_generation= update_map_generation(s);
  785. CMP(dmin, 0, 0, size);
  786. map[0]= map_generation;
  787. score_map[0]= dmin;
  788. /* first line */
  789. if (s->first_slice_line) {
  790. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  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. }else{
  794. if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
  795. |P_TOP[0] |P_TOP[1]
  796. |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
  797. *mx_ptr= 0;
  798. *my_ptr= 0;
  799. s->me.skip=1;
  800. return dmin;
  801. }
  802. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  803. if(dmin>256*2){
  804. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  805. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  806. CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
  807. CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
  808. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  809. }
  810. }
  811. if(dmin>256*4){
  812. if(s->me.pre_pass){
  813. CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
  814. (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
  815. if(!s->first_slice_line)
  816. CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  817. (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  818. }else{
  819. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  820. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  821. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  822. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  823. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  824. }
  825. }
  826. if(s->avctx->last_predictor_count){
  827. const int count= s->avctx->last_predictor_count;
  828. const int xstart= FFMAX(0, s->mb_x - count);
  829. const int ystart= FFMAX(0, s->mb_y - count);
  830. const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
  831. const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
  832. int mb_y;
  833. for(mb_y=ystart; mb_y<yend; mb_y++){
  834. int mb_x;
  835. for(mb_x=xstart; mb_x<xend; mb_x++){
  836. const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
  837. int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
  838. int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
  839. if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
  840. CHECK_MV(mx,my)
  841. }
  842. }
  843. }
  844. //check(best[0],best[1],0, b0)
  845. if(s->me.dia_size==-1)
  846. dmin= RENAME(funny_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<-1)
  850. dmin= RENAME(sab_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 if(s->me.dia_size<2)
  854. dmin= RENAME(small_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. else
  858. dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  859. pred_x, pred_y, penalty_factor,
  860. shift, map, map_generation, size, h, mv_penalty);
  861. //check(best[0],best[1],0, b1)
  862. *mx_ptr= best[0];
  863. *my_ptr= best[1];
  864. // printf("%d %d %d \n", best[0], best[1], dmin);
  865. return dmin;
  866. }
  867. #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
  868. static int RENAME(epzs_motion_search4)(MpegEncContext * s,
  869. int *mx_ptr, int *my_ptr,
  870. int P[10][2], int pred_x, int pred_y,
  871. uint8_t *src_data[3],
  872. uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
  873. int ref_mv_scale, uint8_t * const mv_penalty)
  874. {
  875. int best[2]={0, 0};
  876. int d, dmin;
  877. const int shift= 1+s->quarter_sample;
  878. uint32_t *map= s->me.map;
  879. int map_generation;
  880. const int penalty_factor= s->me.penalty_factor;
  881. const int size=1;
  882. const int h=8;
  883. const int ref_mv_stride= s->mb_stride;
  884. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  885. me_cmp_func cmp, chroma_cmp;
  886. LOAD_COMMON
  887. cmp= s->dsp.me_cmp[size];
  888. chroma_cmp= s->dsp.me_cmp[size+1];
  889. map_generation= update_map_generation(s);
  890. dmin = 1000000;
  891. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  892. /* first line */
  893. if (s->first_slice_line) {
  894. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[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. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  898. }else{
  899. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  900. //FIXME try some early stop
  901. if(dmin>64*2){
  902. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  903. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  904. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  905. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  906. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  907. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  908. }
  909. }
  910. if(dmin>64*4){
  911. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  912. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  913. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  914. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  915. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  916. }
  917. if(s->me.dia_size==-1)
  918. dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  919. pred_x, pred_y, penalty_factor,
  920. shift, map, map_generation, size, h, mv_penalty);
  921. else if(s->me.dia_size<-1)
  922. dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  923. pred_x, pred_y, penalty_factor,
  924. shift, map, map_generation, size, h, mv_penalty);
  925. else if(s->me.dia_size<2)
  926. dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  927. pred_x, pred_y, penalty_factor,
  928. shift, map, map_generation, size, h, mv_penalty);
  929. else
  930. dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  931. pred_x, pred_y, penalty_factor,
  932. shift, map, map_generation, size, h, mv_penalty);
  933. *mx_ptr= best[0];
  934. *my_ptr= best[1];
  935. // printf("%d %d %d \n", best[0], best[1], dmin);
  936. return dmin;
  937. }
  938. //try to merge with above FIXME (needs PSNR test)
  939. static int RENAME(epzs_motion_search2)(MpegEncContext * s,
  940. int *mx_ptr, int *my_ptr,
  941. int P[10][2], int pred_x, int pred_y,
  942. uint8_t *src_data[3],
  943. uint8_t *ref_data[3], int stride, int uvstride, int16_t (*last_mv)[2],
  944. int ref_mv_scale, uint8_t * const mv_penalty)
  945. {
  946. int best[2]={0, 0};
  947. int d, dmin;
  948. const int shift= 1+s->quarter_sample;
  949. uint32_t *map= s->me.map;
  950. int map_generation;
  951. const int penalty_factor= s->me.penalty_factor;
  952. const int size=0; //FIXME pass as arg
  953. const int h=8;
  954. const int ref_mv_stride= s->mb_stride;
  955. const int ref_mv_xy= s->mb_x + s->mb_y *ref_mv_stride;
  956. me_cmp_func cmp, chroma_cmp;
  957. LOAD_COMMON
  958. cmp= s->dsp.me_cmp[size];
  959. chroma_cmp= s->dsp.me_cmp[size+1];
  960. map_generation= update_map_generation(s);
  961. dmin = 1000000;
  962. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  963. /* first line */
  964. if (s->first_slice_line) {
  965. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  966. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  967. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  968. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  969. }else{
  970. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  971. //FIXME try some early stop
  972. if(dmin>64*2){
  973. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  974. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  975. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  976. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  977. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  978. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  979. }
  980. }
  981. if(dmin>64*4){
  982. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  983. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  984. if(s->mb_y+1<s->end_mb_y) //FIXME replace at least with last_slice_line
  985. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  986. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  987. }
  988. if(s->me.dia_size==-1)
  989. dmin= RENAME(funny_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  990. pred_x, pred_y, penalty_factor,
  991. shift, map, map_generation, size, h, mv_penalty);
  992. else if(s->me.dia_size<-1)
  993. dmin= RENAME(sab_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  994. pred_x, pred_y, penalty_factor,
  995. shift, map, map_generation, size, h, mv_penalty);
  996. else if(s->me.dia_size<2)
  997. dmin= RENAME(small_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  998. pred_x, pred_y, penalty_factor,
  999. shift, map, map_generation, size, h, mv_penalty);
  1000. else
  1001. dmin= RENAME(var_diamond_search)(s, best, dmin, src_data, ref_data, stride, uvstride,
  1002. pred_x, pred_y, penalty_factor,
  1003. shift, map, map_generation, size, h, mv_penalty);
  1004. *mx_ptr= best[0];
  1005. *my_ptr= best[1];
  1006. // printf("%d %d %d \n", best[0], best[1], dmin);
  1007. return dmin;
  1008. }
  1009. #endif /* !CMP_DIRECT */