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.

1007 lines
36KB

  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. const int subpel_quality= s->avctx->me_subpel_quality;
  243. uint32_t *map= s->me.map;
  244. me_cmp_func cmp, chroma_cmp;
  245. me_cmp_func cmp_sub, chroma_cmp_sub;
  246. LOAD_COMMON(xx, yy);
  247. cmp= s->dsp.me_cmp[size];
  248. chroma_cmp= s->dsp.me_cmp[size+1]; //factorize FIXME
  249. //FIXME factorize
  250. cmp_sub= s->dsp.me_sub_cmp[size];
  251. chroma_cmp_sub= s->dsp.me_sub_cmp[size+1];
  252. if(s->me.skip){ //FIXME somehow move up (benchmark)
  253. *mx_ptr = 0;
  254. *my_ptr = 0;
  255. return dmin;
  256. }
  257. if(s->avctx->me_cmp != s->avctx->me_sub_cmp){
  258. CMP_QPEL(dmin, 0, 0, mx, my, size);
  259. if(mx || my)
  260. dmin += (mv_penalty[4*mx - pred_x] + mv_penalty[4*my - pred_y])*penalty_factor;
  261. }
  262. if (mx > xmin && mx < xmax &&
  263. my > ymin && my < ymax) {
  264. int bx=4*mx, by=4*my;
  265. int d= dmin;
  266. int i, nx, ny;
  267. const int index= (my<<ME_MAP_SHIFT) + mx;
  268. const int t= score_map[(index-(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  269. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  270. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  271. const int b= score_map[(index+(1<<ME_MAP_SHIFT) )&(ME_MAP_SIZE-1)];
  272. const int c= score_map[(index )&(ME_MAP_SIZE-1)];
  273. int best[8];
  274. int best_pos[8][2];
  275. memset(best, 64, sizeof(int)*8);
  276. #if 1
  277. if(s->me.dia_size>=2){
  278. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  279. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  280. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  281. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  282. for(ny= -3; ny <= 3; ny++){
  283. for(nx= -3; nx <= 3; nx++){
  284. const int t2= nx*nx*(tr + tl - 2*t) + 4*nx*(tr-tl) + 32*t;
  285. const int c2= nx*nx*( r + l - 2*c) + 4*nx*( r- l) + 32*c;
  286. const int b2= nx*nx*(br + bl - 2*b) + 4*nx*(br-bl) + 32*b;
  287. int score= ny*ny*(b2 + t2 - 2*c2) + 4*ny*(b2 - t2) + 32*c2;
  288. int i;
  289. if((nx&3)==0 && (ny&3)==0) continue;
  290. score += 1024*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  291. // if(nx&1) score-=1024*s->me.penalty_factor;
  292. // if(ny&1) score-=1024*s->me.penalty_factor;
  293. for(i=0; i<8; i++){
  294. if(score < best[i]){
  295. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  296. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  297. best[i]= score;
  298. best_pos[i][0]= nx + 4*mx;
  299. best_pos[i][1]= ny + 4*my;
  300. break;
  301. }
  302. }
  303. }
  304. }
  305. }else{
  306. int tl;
  307. const int cx = 4*(r - l);
  308. const int cx2= r + l - 2*c;
  309. const int cy = 4*(b - t);
  310. const int cy2= b + t - 2*c;
  311. int cxy;
  312. if(map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)] == (my<<ME_MAP_MV_BITS) + mx + map_generation && 0){ //FIXME
  313. tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  314. }else{
  315. CMP(tl, mx-1, my-1, size); //FIXME wrong if chroma me is different
  316. }
  317. cxy= 2*tl + (cx + cy)/4 - (cx2 + cy2) - 2*c;
  318. assert(16*cx2 + 4*cx + 32*c == 32*r);
  319. assert(16*cx2 - 4*cx + 32*c == 32*l);
  320. assert(16*cy2 + 4*cy + 32*c == 32*b);
  321. assert(16*cy2 - 4*cy + 32*c == 32*t);
  322. assert(16*cxy + 16*cy2 + 16*cx2 - 4*cy - 4*cx + 32*c == 32*tl);
  323. for(ny= -3; ny <= 3; ny++){
  324. for(nx= -3; nx <= 3; nx++){
  325. int score= ny*nx*cxy + nx*nx*cx2 + ny*ny*cy2 + nx*cx + ny*cy + 32*c; //FIXME factor
  326. int i;
  327. if((nx&3)==0 && (ny&3)==0) continue;
  328. score += 32*(mv_penalty[4*mx + nx - pred_x] + mv_penalty[4*my + ny - pred_y])*penalty_factor;
  329. // if(nx&1) score-=32*s->me.penalty_factor;
  330. // if(ny&1) score-=32*s->me.penalty_factor;
  331. for(i=0; i<8; i++){
  332. if(score < best[i]){
  333. memmove(&best[i+1], &best[i], sizeof(int)*(7-i));
  334. memmove(&best_pos[i+1][0], &best_pos[i][0], sizeof(int)*2*(7-i));
  335. best[i]= score;
  336. best_pos[i][0]= nx + 4*mx;
  337. best_pos[i][1]= ny + 4*my;
  338. break;
  339. }
  340. }
  341. }
  342. }
  343. }
  344. for(i=0; i<subpel_quality; i++){
  345. nx= best_pos[i][0];
  346. ny= best_pos[i][1];
  347. CHECK_QUARTER_MV(nx&3, ny&3, nx>>2, ny>>2)
  348. }
  349. #if 0
  350. const int tl= score_map[(index-(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  351. const int bl= score_map[(index+(1<<ME_MAP_SHIFT)-1)&(ME_MAP_SIZE-1)];
  352. const int tr= score_map[(index-(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  353. const int br= score_map[(index+(1<<ME_MAP_SHIFT)+1)&(ME_MAP_SIZE-1)];
  354. // if(l < r && l < t && l < b && l < tl && l < bl && l < tr && l < br && bl < tl){
  355. if(tl<br){
  356. // nx= FFMAX(4*mx - bx, bx - 4*mx);
  357. // ny= FFMAX(4*my - by, by - 4*my);
  358. static int stats[7][7], count;
  359. count++;
  360. stats[4*mx - bx + 3][4*my - by + 3]++;
  361. if(256*256*256*64 % count ==0){
  362. for(i=0; i<49; i++){
  363. if((i%7)==0) printf("\n");
  364. printf("%6d ", stats[0][i]);
  365. }
  366. printf("\n");
  367. }
  368. }
  369. #endif
  370. #else
  371. CHECK_QUARTER_MV(2, 2, mx-1, my-1)
  372. CHECK_QUARTER_MV(0, 2, mx , my-1)
  373. CHECK_QUARTER_MV(2, 2, mx , my-1)
  374. CHECK_QUARTER_MV(2, 0, mx , my )
  375. CHECK_QUARTER_MV(2, 2, mx , my )
  376. CHECK_QUARTER_MV(0, 2, mx , my )
  377. CHECK_QUARTER_MV(2, 2, mx-1, my )
  378. CHECK_QUARTER_MV(2, 0, mx-1, my )
  379. nx= bx;
  380. ny= by;
  381. for(i=0; i<8; i++){
  382. int ox[8]= {0, 1, 1, 1, 0,-1,-1,-1};
  383. int oy[8]= {1, 1, 0,-1,-1,-1, 0, 1};
  384. CHECK_QUARTER_MV((nx + ox[i])&3, (ny + oy[i])&3, (nx + ox[i])>>2, (ny + oy[i])>>2)
  385. }
  386. #endif
  387. #if 0
  388. //outer ring
  389. CHECK_QUARTER_MV(1, 3, mx-1, my-1)
  390. CHECK_QUARTER_MV(1, 2, mx-1, my-1)
  391. CHECK_QUARTER_MV(1, 1, mx-1, my-1)
  392. CHECK_QUARTER_MV(2, 1, mx-1, my-1)
  393. CHECK_QUARTER_MV(3, 1, mx-1, my-1)
  394. CHECK_QUARTER_MV(0, 1, mx , my-1)
  395. CHECK_QUARTER_MV(1, 1, mx , my-1)
  396. CHECK_QUARTER_MV(2, 1, mx , my-1)
  397. CHECK_QUARTER_MV(3, 1, mx , my-1)
  398. CHECK_QUARTER_MV(3, 2, mx , my-1)
  399. CHECK_QUARTER_MV(3, 3, mx , my-1)
  400. CHECK_QUARTER_MV(3, 0, mx , my )
  401. CHECK_QUARTER_MV(3, 1, mx , my )
  402. CHECK_QUARTER_MV(3, 2, mx , my )
  403. CHECK_QUARTER_MV(3, 3, mx , my )
  404. CHECK_QUARTER_MV(2, 3, mx , my )
  405. CHECK_QUARTER_MV(1, 3, mx , my )
  406. CHECK_QUARTER_MV(0, 3, mx , my )
  407. CHECK_QUARTER_MV(3, 3, mx-1, my )
  408. CHECK_QUARTER_MV(2, 3, mx-1, my )
  409. CHECK_QUARTER_MV(1, 3, mx-1, my )
  410. CHECK_QUARTER_MV(1, 2, mx-1, my )
  411. CHECK_QUARTER_MV(1, 1, mx-1, my )
  412. CHECK_QUARTER_MV(1, 0, mx-1, my )
  413. #endif
  414. assert(bx >= xmin*4 && bx <= xmax*4 && by >= ymin*4 && by <= ymax*4);
  415. *mx_ptr = bx;
  416. *my_ptr = by;
  417. }else{
  418. *mx_ptr =4*mx;
  419. *my_ptr =4*my;
  420. }
  421. return dmin;
  422. }
  423. #endif /* CMP_QPEL */
  424. #define CHECK_MV(x,y)\
  425. {\
  426. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  427. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  428. /*printf("check_mv %d %d\n", x, y);*/\
  429. if(map[index]!=key){\
  430. CMP(d, x, y, size);\
  431. map[index]= key;\
  432. score_map[index]= d;\
  433. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  434. /*printf("score:%d\n", d);*/\
  435. COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
  436. }\
  437. }
  438. #define CHECK_CLIPED_MV(ax,ay)\
  439. {\
  440. const int x= FFMAX(xmin, FFMIN(ax, xmax));\
  441. const int y= FFMAX(ymin, FFMIN(ay, ymax));\
  442. CHECK_MV(x, y)\
  443. }
  444. #define CHECK_MV_DIR(x,y,new_dir)\
  445. {\
  446. const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
  447. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
  448. /*printf("check_mv_dir %d %d %d\n", x, y, new_dir);*/\
  449. if(map[index]!=key){\
  450. CMP(d, x, y, size);\
  451. map[index]= key;\
  452. score_map[index]= d;\
  453. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*penalty_factor;\
  454. /*printf("score:%d\n", d);*/\
  455. if(d<dmin){\
  456. best[0]=x;\
  457. best[1]=y;\
  458. dmin=d;\
  459. next_dir= new_dir;\
  460. }\
  461. }\
  462. }
  463. #define check(x,y,S,v)\
  464. if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
  465. if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
  466. if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
  467. if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
  468. static inline int RENAME(small_diamond_search)(MpegEncContext * s, int *best, int dmin,
  469. Picture *ref_picture,
  470. int const pred_x, int const pred_y, int const penalty_factor,
  471. int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
  472. uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
  473. )
  474. {
  475. me_cmp_func cmp, chroma_cmp;
  476. int next_dir=-1;
  477. LOAD_COMMON(s->mb_x*16, s->mb_y*16);
  478. cmp= s->dsp.me_cmp[size];
  479. chroma_cmp= s->dsp.me_cmp[size+1];
  480. { /* ensure that the best point is in the MAP as h/qpel refinement needs it */
  481. const int key= (best[1]<<ME_MAP_MV_BITS) + best[0] + map_generation;
  482. const int index= ((best[1]<<ME_MAP_SHIFT) + best[0])&(ME_MAP_SIZE-1);
  483. if(map[index]!=key){ //this will be executed only very rarey
  484. CMP(score_map[index], best[0], best[1], size);
  485. map[index]= key;
  486. }
  487. }
  488. for(;;){
  489. int d;
  490. const int dir= next_dir;
  491. const int x= best[0];
  492. const int y= best[1];
  493. next_dir=-1;
  494. //printf("%d", dir);
  495. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  496. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  497. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  498. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  499. if(next_dir==-1){
  500. return dmin;
  501. }
  502. }
  503. }
  504. static inline int RENAME(funny_diamond_search)(MpegEncContext * s, int *best, int dmin,
  505. Picture *ref_picture,
  506. int const pred_x, int const pred_y, int const penalty_factor,
  507. int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
  508. uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
  509. )
  510. {
  511. me_cmp_func cmp, chroma_cmp;
  512. int dia_size;
  513. LOAD_COMMON(s->mb_x*16, s->mb_y*16);
  514. cmp= s->dsp.me_cmp[size];
  515. chroma_cmp= s->dsp.me_cmp[size+1];
  516. for(dia_size=1; dia_size<=4; dia_size++){
  517. int dir;
  518. const int x= best[0];
  519. const int y= best[1];
  520. if(dia_size&(dia_size-1)) continue;
  521. if( x + dia_size > xmax
  522. || x - dia_size < xmin
  523. || y + dia_size > ymax
  524. || y - dia_size < ymin)
  525. continue;
  526. for(dir= 0; dir<dia_size; dir+=2){
  527. int d;
  528. CHECK_MV(x + dir , y + dia_size - dir);
  529. CHECK_MV(x + dia_size - dir, y - dir );
  530. CHECK_MV(x - dir , y - dia_size + dir);
  531. CHECK_MV(x - dia_size + dir, y + dir );
  532. }
  533. if(x!=best[0] || y!=best[1])
  534. dia_size=0;
  535. #if 0
  536. {
  537. int dx, dy, i;
  538. static int stats[8*8];
  539. dx= ABS(x-best[0]);
  540. dy= ABS(y-best[1]);
  541. if(dy>dx){
  542. dx^=dy; dy^=dx; dx^=dy;
  543. }
  544. stats[dy*8 + dx] ++;
  545. if(256*256*256*64 % (stats[0]+1)==0){
  546. for(i=0; i<64; i++){
  547. if((i&7)==0) printf("\n");
  548. printf("%8d ", stats[i]);
  549. }
  550. printf("\n");
  551. }
  552. }
  553. #endif
  554. }
  555. return dmin;
  556. }
  557. #define SAB_CHECK_MV(ax,ay)\
  558. {\
  559. const int key= ((ay)<<ME_MAP_MV_BITS) + (ax) + map_generation;\
  560. const int index= (((ay)<<ME_MAP_SHIFT) + (ax))&(ME_MAP_SIZE-1);\
  561. /*printf("sab check %d %d\n", ax, ay);*/\
  562. if(map[index]!=key){\
  563. CMP(d, ax, ay, size);\
  564. map[index]= key;\
  565. score_map[index]= d;\
  566. d += (mv_penalty[((ax)<<shift)-pred_x] + mv_penalty[((ay)<<shift)-pred_y])*penalty_factor;\
  567. /*printf("score: %d\n", d);*/\
  568. if(d < minima[minima_count-1].height){\
  569. int j=0;\
  570. \
  571. while(d >= minima[j].height) j++;\
  572. \
  573. memmove(&minima [j+1], &minima [j], (minima_count - j - 1)*sizeof(Minima));\
  574. \
  575. minima[j].checked= 0;\
  576. minima[j].height= d;\
  577. minima[j].x= ax;\
  578. minima[j].y= ay;\
  579. \
  580. i=-1;\
  581. continue;\
  582. }\
  583. }\
  584. }
  585. #define MAX_SAB_SIZE 16
  586. static inline int RENAME(sab_diamond_search)(MpegEncContext * s, int *best, int dmin,
  587. Picture *ref_picture,
  588. int const pred_x, int const pred_y, int const penalty_factor,
  589. int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
  590. uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
  591. )
  592. {
  593. me_cmp_func cmp, chroma_cmp;
  594. Minima minima[MAX_SAB_SIZE];
  595. const int minima_count= ABS(s->me.dia_size);
  596. int i, j;
  597. LOAD_COMMON(s->mb_x*16, s->mb_y*16);
  598. cmp= s->dsp.me_cmp[size];
  599. chroma_cmp= s->dsp.me_cmp[size+1];
  600. for(j=i=0; i<ME_MAP_SIZE; i++){
  601. uint32_t key= map[i];
  602. key += (1<<(ME_MAP_MV_BITS-1)) + (1<<(2*ME_MAP_MV_BITS-1));
  603. if((key&((-1)<<(2*ME_MAP_MV_BITS))) != map_generation) continue;
  604. assert(j<MAX_SAB_SIZE); //max j = number of predictors
  605. minima[j].height= score_map[i];
  606. minima[j].x= key & ((1<<ME_MAP_MV_BITS)-1); key>>=ME_MAP_MV_BITS;
  607. minima[j].y= key & ((1<<ME_MAP_MV_BITS)-1);
  608. minima[j].x-= (1<<(ME_MAP_MV_BITS-1));
  609. minima[j].y-= (1<<(ME_MAP_MV_BITS-1));
  610. minima[j].checked=0;
  611. if(minima[j].x || minima[j].y)
  612. minima[j].height+= (mv_penalty[((minima[j].x)<<shift)-pred_x] + mv_penalty[((minima[j].y)<<shift)-pred_y])*penalty_factor;
  613. j++;
  614. }
  615. qsort(minima, j, sizeof(Minima), minima_cmp);
  616. for(; j<minima_count; j++){
  617. minima[j].height=256*256*256*64;
  618. minima[j].checked=0;
  619. minima[j].x= minima[j].y=0;
  620. }
  621. for(i=0; i<minima_count; i++){
  622. const int x= minima[i].x;
  623. const int y= minima[i].y;
  624. int d;
  625. if(minima[i].checked) continue;
  626. if( x >= xmax || x <= xmin
  627. || y >= ymax || y <= ymin)
  628. continue;
  629. SAB_CHECK_MV(x-1, y)
  630. SAB_CHECK_MV(x+1, y)
  631. SAB_CHECK_MV(x , y-1)
  632. SAB_CHECK_MV(x , y+1)
  633. minima[i].checked= 1;
  634. }
  635. best[0]= minima[0].x;
  636. best[1]= minima[0].y;
  637. dmin= minima[0].height;
  638. if( best[0] < xmax && best[0] > xmin
  639. && best[1] < ymax && best[1] > ymin){
  640. int d;
  641. //ensure that the refernece samples for hpel refinement are in the map
  642. CHECK_MV(best[0]-1, best[1])
  643. CHECK_MV(best[0]+1, best[1])
  644. CHECK_MV(best[0], best[1]-1)
  645. CHECK_MV(best[0], best[1]+1)
  646. }
  647. return dmin;
  648. }
  649. static inline int RENAME(var_diamond_search)(MpegEncContext * s, int *best, int dmin,
  650. Picture *ref_picture,
  651. int const pred_x, int const pred_y, int const penalty_factor,
  652. int const xmin, int const ymin, int const xmax, int const ymax, int const shift,
  653. uint32_t *map, int map_generation, int size, uint16_t * const mv_penalty
  654. )
  655. {
  656. me_cmp_func cmp, chroma_cmp;
  657. int dia_size;
  658. LOAD_COMMON(s->mb_x*16, s->mb_y*16);
  659. cmp= s->dsp.me_cmp[size];
  660. chroma_cmp= s->dsp.me_cmp[size+1];
  661. for(dia_size=1; dia_size<=s->me.dia_size; dia_size++){
  662. int dir, start, end;
  663. const int x= best[0];
  664. const int y= best[1];
  665. start= FFMAX(0, y + dia_size - ymax);
  666. end = FFMIN(dia_size, xmax - x + 1);
  667. for(dir= start; dir<end; dir++){
  668. int d;
  669. //check(x + dir,y + dia_size - dir,0, a0)
  670. CHECK_MV(x + dir , y + dia_size - dir);
  671. }
  672. start= FFMAX(0, x + dia_size - xmax);
  673. end = FFMIN(dia_size, y - ymin + 1);
  674. for(dir= start; dir<end; dir++){
  675. int d;
  676. //check(x + dia_size - dir, y - dir,0, a1)
  677. CHECK_MV(x + dia_size - dir, y - dir );
  678. }
  679. start= FFMAX(0, -y + dia_size + ymin );
  680. end = FFMIN(dia_size, x - xmin + 1);
  681. for(dir= start; dir<end; dir++){
  682. int d;
  683. //check(x - dir,y - dia_size + dir,0, a2)
  684. CHECK_MV(x - dir , y - dia_size + dir);
  685. }
  686. start= FFMAX(0, -x + dia_size + xmin );
  687. end = FFMIN(dia_size, ymax - y + 1);
  688. for(dir= start; dir<end; dir++){
  689. int d;
  690. //check(x - dia_size + dir, y + dir,0, a3)
  691. CHECK_MV(x - dia_size + dir, y + dir );
  692. }
  693. if(x!=best[0] || y!=best[1])
  694. dia_size=0;
  695. #if 0
  696. {
  697. int dx, dy, i;
  698. static int stats[8*8];
  699. dx= ABS(x-best[0]);
  700. dy= ABS(y-best[1]);
  701. stats[dy*8 + dx] ++;
  702. if(256*256*256*64 % (stats[0]+1)==0){
  703. for(i=0; i<64; i++){
  704. if((i&7)==0) printf("\n");
  705. printf("%6d ", stats[i]);
  706. }
  707. printf("\n");
  708. }
  709. }
  710. #endif
  711. }
  712. return dmin;
  713. }
  714. static int RENAME(epzs_motion_search)(MpegEncContext * s, int block,
  715. int *mx_ptr, int *my_ptr,
  716. int P[10][2], int pred_x, int pred_y,
  717. int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
  718. int ref_mv_scale, uint16_t * const mv_penalty)
  719. {
  720. int best[2]={0, 0};
  721. int d, dmin;
  722. const int shift= 1+s->quarter_sample;
  723. uint32_t *map= s->me.map;
  724. int map_generation;
  725. const int penalty_factor= s->me.penalty_factor;
  726. const int size=0;
  727. const int ref_mv_stride= s->mb_width+2;
  728. const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
  729. me_cmp_func cmp, chroma_cmp;
  730. LOAD_COMMON(s->mb_x*16, s->mb_y*16);
  731. cmp= s->dsp.me_cmp[size];
  732. chroma_cmp= s->dsp.me_cmp[size+1];
  733. map_generation= update_map_generation(s);
  734. CMP(dmin, 0, 0, size);
  735. map[0]= map_generation;
  736. score_map[0]= dmin;
  737. /* first line */
  738. if (s->mb_y == 0) {
  739. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  740. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  741. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  742. }else{
  743. if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
  744. |P_TOP[0] |P_TOP[1]
  745. |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
  746. *mx_ptr= 0;
  747. *my_ptr= 0;
  748. s->me.skip=1;
  749. return dmin;
  750. }
  751. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  752. if(dmin>256*2){
  753. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  754. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  755. CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
  756. CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
  757. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  758. }
  759. }
  760. if(dmin>256*4){
  761. if(s->me.pre_pass){
  762. CHECK_CLIPED_MV((last_mv[ref_mv_xy-1][0]*ref_mv_scale + (1<<15))>>16,
  763. (last_mv[ref_mv_xy-1][1]*ref_mv_scale + (1<<15))>>16)
  764. CHECK_CLIPED_MV((last_mv[ref_mv_xy-ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  765. (last_mv[ref_mv_xy-ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  766. }else{
  767. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  768. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  769. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  770. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  771. }
  772. }
  773. if(s->avctx->last_predictor_count){
  774. const int count= s->avctx->last_predictor_count;
  775. const int xstart= FFMAX(0, s->mb_x - count);
  776. const int ystart= FFMAX(0, s->mb_y - count);
  777. const int xend= FFMIN(s->mb_width , s->mb_x + count + 1);
  778. const int yend= FFMIN(s->mb_height, s->mb_y + count + 1);
  779. int mb_y;
  780. for(mb_y=ystart; mb_y<yend; mb_y++){
  781. int mb_x;
  782. for(mb_x=xstart; mb_x<xend; mb_x++){
  783. const int xy= mb_x + 1 + (mb_y + 1)*ref_mv_stride;
  784. int mx= (last_mv[xy][0]*ref_mv_scale + (1<<15))>>16;
  785. int my= (last_mv[xy][1]*ref_mv_scale + (1<<15))>>16;
  786. if(mx>xmax || mx<xmin || my>ymax || my<ymin) continue;
  787. CHECK_MV(mx,my)
  788. }
  789. }
  790. }
  791. //check(best[0],best[1],0, b0)
  792. if(s->me.dia_size==-1)
  793. dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
  794. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  795. shift, map, map_generation, size, mv_penalty);
  796. else if(s->me.dia_size<-1)
  797. dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
  798. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  799. shift, map, map_generation, size, mv_penalty);
  800. else if(s->me.dia_size<2)
  801. dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
  802. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  803. shift, map, map_generation, size, mv_penalty);
  804. else
  805. dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
  806. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  807. shift, map, map_generation, size, mv_penalty);
  808. //check(best[0],best[1],0, b1)
  809. *mx_ptr= best[0];
  810. *my_ptr= best[1];
  811. // printf("%d %d %d \n", best[0], best[1], dmin);
  812. return dmin;
  813. }
  814. #ifndef CMP_DIRECT /* no 4mv search needed in direct mode */
  815. static int RENAME(epzs_motion_search4)(MpegEncContext * s, int block,
  816. int *mx_ptr, int *my_ptr,
  817. int P[10][2], int pred_x, int pred_y,
  818. int xmin, int ymin, int xmax, int ymax, Picture *ref_picture, int16_t (*last_mv)[2],
  819. int ref_mv_scale, uint16_t * const mv_penalty)
  820. {
  821. int best[2]={0, 0};
  822. int d, dmin;
  823. const int shift= 1+s->quarter_sample;
  824. uint32_t *map= s->me.map;
  825. int map_generation;
  826. const int penalty_factor= s->me.penalty_factor;
  827. const int size=1;
  828. const int ref_mv_stride= s->mb_width+2;
  829. const int ref_mv_xy= 1 + s->mb_x + (s->mb_y + 1)*ref_mv_stride;
  830. me_cmp_func cmp, chroma_cmp;
  831. LOAD_COMMON((s->mb_x*2 + (block&1))*8, (s->mb_y*2 + (block>>1))*8);
  832. cmp= s->dsp.me_cmp[size];
  833. chroma_cmp= s->dsp.me_cmp[size+1];
  834. map_generation= update_map_generation(s);
  835. dmin = 1000000;
  836. //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
  837. /* first line */
  838. if (s->mb_y == 0 && block<2) {
  839. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  840. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  841. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  842. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  843. }else{
  844. CHECK_MV(P_MV1[0]>>shift, P_MV1[1]>>shift)
  845. //FIXME try some early stop
  846. if(dmin>64*2){
  847. CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
  848. CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
  849. CHECK_MV(P_TOP[0]>>shift, P_TOP[1]>>shift)
  850. CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
  851. CHECK_CLIPED_MV((last_mv[ref_mv_xy][0]*ref_mv_scale + (1<<15))>>16,
  852. (last_mv[ref_mv_xy][1]*ref_mv_scale + (1<<15))>>16)
  853. }
  854. }
  855. if(dmin>64*4){
  856. CHECK_CLIPED_MV((last_mv[ref_mv_xy+1][0]*ref_mv_scale + (1<<15))>>16,
  857. (last_mv[ref_mv_xy+1][1]*ref_mv_scale + (1<<15))>>16)
  858. CHECK_CLIPED_MV((last_mv[ref_mv_xy+ref_mv_stride][0]*ref_mv_scale + (1<<15))>>16,
  859. (last_mv[ref_mv_xy+ref_mv_stride][1]*ref_mv_scale + (1<<15))>>16)
  860. }
  861. if(s->me.dia_size==-1)
  862. dmin= RENAME(funny_diamond_search)(s, best, dmin, ref_picture,
  863. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  864. shift, map, map_generation, size, mv_penalty);
  865. else if(s->me.dia_size<-1)
  866. dmin= RENAME(sab_diamond_search)(s, best, dmin, ref_picture,
  867. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  868. shift, map, map_generation, size, mv_penalty);
  869. else if(s->me.dia_size<2)
  870. dmin= RENAME(small_diamond_search)(s, best, dmin, ref_picture,
  871. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  872. shift, map, map_generation, size, mv_penalty);
  873. else
  874. dmin= RENAME(var_diamond_search)(s, best, dmin, ref_picture,
  875. pred_x, pred_y, penalty_factor, xmin, ymin, xmax, ymax,
  876. shift, map, map_generation, size, mv_penalty);
  877. *mx_ptr= best[0];
  878. *my_ptr= best[1];
  879. // printf("%d %d %d \n", best[0], best[1], dmin);
  880. return dmin;
  881. }
  882. #endif /* !CMP_DIRECT */