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.

1112 lines
40KB

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