motion_est_template.c 44 KB

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