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.

989 lines
35KB

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