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.

1544 lines
50KB

  1. /*
  2. * Motion estimation
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002 Michael Niedermayer
  5. *
  6. *
  7. * This library 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 of the License, or (at your option) any later version.
  11. *
  12. * This library 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 this library; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  22. */
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include "avcodec.h"
  26. #include "dsputil.h"
  27. #include "mpegvideo.h"
  28. //#undef NDEBUG
  29. //#include <assert.h>
  30. #define SQ(a) ((a)*(a))
  31. #define P_LEFT P[1]
  32. #define P_TOP P[2]
  33. #define P_TOPRIGHT P[3]
  34. #define P_MEDIAN P[4]
  35. #define P_MV1 P[9]
  36. static inline int sad_hpel_motion_search(MpegEncContext * s,
  37. int *mx_ptr, int *my_ptr, int dmin,
  38. int xmin, int ymin, int xmax, int ymax,
  39. int pred_x, int pred_y, Picture *picture,
  40. int n, int size, uint16_t * const mv_penalty);
  41. static inline int update_map_generation(MpegEncContext * s)
  42. {
  43. s->me.map_generation+= 1<<(ME_MAP_MV_BITS*2);
  44. if(s->me.map_generation==0){
  45. s->me.map_generation= 1<<(ME_MAP_MV_BITS*2);
  46. memset(s->me.map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
  47. }
  48. return s->me.map_generation;
  49. }
  50. /* shape adaptive search stuff */
  51. typedef struct Minima{
  52. int height;
  53. int x, y;
  54. int checked;
  55. }Minima;
  56. static int minima_cmp(const void *a, const void *b){
  57. Minima *da = (Minima *) a;
  58. Minima *db = (Minima *) b;
  59. return da->height - db->height;
  60. }
  61. /* SIMPLE */
  62. #define RENAME(a) simple_ ## a
  63. #define CMP(d, x, y, size)\
  64. d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride);
  65. #define CMP_HPEL(d, dx, dy, x, y, size)\
  66. {\
  67. const int dxy= (dx) + 2*(dy);\
  68. hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\
  69. d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
  70. }
  71. #define CMP_QPEL(d, dx, dy, x, y, size)\
  72. {\
  73. const int dxy= (dx) + 4*(dy);\
  74. qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\
  75. d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
  76. }
  77. #include "motion_est_template.c"
  78. #undef RENAME
  79. #undef CMP
  80. #undef CMP_HPEL
  81. #undef CMP_QPEL
  82. #undef INIT
  83. /* SIMPLE CHROMA */
  84. #define RENAME(a) simple_chroma_ ## a
  85. #define CMP(d, x, y, size)\
  86. d = cmp(s, src_y, (ref_y) + (x) + (y)*(stride), stride);\
  87. if(chroma_cmp){\
  88. int dxy= ((x)&1) + 2*((y)&1);\
  89. int c= ((x)>>1) + ((y)>>1)*uvstride;\
  90. \
  91. chroma_hpel_put[0][dxy](s->me.scratchpad, ref_u + c, uvstride, 8);\
  92. d += chroma_cmp(s, s->me.scratchpad, src_u, uvstride);\
  93. chroma_hpel_put[0][dxy](s->me.scratchpad, ref_v + c, uvstride, 8);\
  94. d += chroma_cmp(s, s->me.scratchpad, src_v, uvstride);\
  95. }
  96. #define CMP_HPEL(d, dx, dy, x, y, size)\
  97. {\
  98. const int dxy= (dx) + 2*(dy);\
  99. hpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride, (16>>size));\
  100. d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
  101. if(chroma_cmp_sub){\
  102. int cxy= (dxy) | ((x)&1) | (2*((y)&1));\
  103. int c= ((x)>>1) + ((y)>>1)*uvstride;\
  104. chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\
  105. d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\
  106. chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\
  107. d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\
  108. }\
  109. }
  110. #define CMP_QPEL(d, dx, dy, x, y, size)\
  111. {\
  112. const int dxy= (dx) + 4*(dy);\
  113. qpel_put[0][dxy](s->me.scratchpad, (ref_y) + (x) + (y)*(stride), stride);\
  114. d = cmp_sub(s, s->me.scratchpad, src_y, stride);\
  115. if(chroma_cmp_sub){\
  116. int cxy, c;\
  117. int cx= (4*(x) + (dx))/2;\
  118. int cy= (4*(y) + (dy))/2;\
  119. cx= (cx>>1)|(cx&1);\
  120. cy= (cy>>1)|(cy&1);\
  121. cxy= (cx&1) + 2*(cy&1);\
  122. c= ((cx)>>1) + ((cy)>>1)*uvstride;\
  123. chroma_hpel_put[0][cxy](s->me.scratchpad, ref_u + c, uvstride, 8);\
  124. d += chroma_cmp_sub(s, s->me.scratchpad, src_u, uvstride);\
  125. chroma_hpel_put[0][cxy](s->me.scratchpad, ref_v + c, uvstride, 8);\
  126. d += chroma_cmp_sub(s, s->me.scratchpad, src_v, uvstride);\
  127. }\
  128. }
  129. #include "motion_est_template.c"
  130. #undef RENAME
  131. #undef CMP
  132. #undef CMP_HPEL
  133. #undef CMP_QPEL
  134. #undef INIT
  135. /* SIMPLE DIRECT HPEL */
  136. #define RENAME(a) simple_direct_hpel_ ## a
  137. //FIXME precalc divisions stuff
  138. #define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\
  139. if((x) >= xmin && 2*(x) + (dx) <= 2*xmax && (y) >= ymin && 2*(y) + (dy) <= 2*ymax){\
  140. const int hx= 2*(x) + (dx);\
  141. const int hy= 2*(y) + (dy);\
  142. if(s->mv_type==MV_TYPE_8X8){\
  143. int i;\
  144. for(i=0; i<4; i++){\
  145. int fx = s->me.direct_basis_mv[i][0] + hx;\
  146. int fy = s->me.direct_basis_mv[i][1] + hy;\
  147. int bx = hx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\
  148. int by = hy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\
  149. int fxy= (fx&1) + 2*(fy&1);\
  150. int bxy= (bx&1) + 2*(by&1);\
  151. \
  152. uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\
  153. hpel_put[1][fxy](dst, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 8);\
  154. hpel_avg[1][bxy](dst, (ref2_y) + (bx>>1) + (by>>1)*(stride), stride, 8);\
  155. }\
  156. }else{\
  157. int fx = s->me.direct_basis_mv[0][0] + hx;\
  158. int fy = s->me.direct_basis_mv[0][1] + hy;\
  159. int bx = hx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\
  160. int by = hy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\
  161. int fxy= (fx&1) + 2*(fy&1);\
  162. int bxy= (bx&1) + 2*(by&1);\
  163. \
  164. hpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>1) + (fy>>1)*(stride), stride, 16);\
  165. hpel_avg[0][bxy](s->me.scratchpad, (ref2_y) + (bx>>1) + (by>>1)*(stride), stride, 16);\
  166. }\
  167. d = cmp_func(s, s->me.scratchpad, src_y, stride);\
  168. }else\
  169. d= 256*256*256*32;
  170. #define CMP_HPEL(d, dx, dy, x, y, size)\
  171. CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub)
  172. #define CMP(d, x, y, size)\
  173. CMP_DIRECT(d, 0, 0, x, y, size, cmp)
  174. #include "motion_est_template.c"
  175. #undef RENAME
  176. #undef CMP
  177. #undef CMP_HPEL
  178. #undef CMP_QPEL
  179. #undef INIT
  180. #undef CMP_DIRECT
  181. /* SIMPLE DIRECT QPEL */
  182. #define RENAME(a) simple_direct_qpel_ ## a
  183. #define CMP_DIRECT(d, dx, dy, x, y, size, cmp_func)\
  184. if((x) >= xmin && 4*(x) + (dx) <= 4*xmax && (y) >= ymin && 4*(y) + (dy) <= 4*ymax){\
  185. const int qx= 4*(x) + (dx);\
  186. const int qy= 4*(y) + (dy);\
  187. if(s->mv_type==MV_TYPE_8X8){\
  188. int i;\
  189. for(i=0; i<4; i++){\
  190. int fx = s->me.direct_basis_mv[i][0] + qx;\
  191. int fy = s->me.direct_basis_mv[i][1] + qy;\
  192. int bx = qx ? fx - s->me.co_located_mv[i][0] : s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + (i &1)*16;\
  193. int by = qy ? fy - s->me.co_located_mv[i][1] : s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + (i>>1)*16;\
  194. int fxy= (fx&3) + 4*(fy&3);\
  195. int bxy= (bx&3) + 4*(by&3);\
  196. \
  197. uint8_t *dst= s->me.scratchpad + 8*(i&1) + 8*stride*(i>>1);\
  198. qpel_put[1][fxy](dst, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\
  199. qpel_avg[1][bxy](dst, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\
  200. }\
  201. }else{\
  202. int fx = s->me.direct_basis_mv[0][0] + qx;\
  203. int fy = s->me.direct_basis_mv[0][1] + qy;\
  204. int bx = qx ? fx - s->me.co_located_mv[0][0] : s->me.co_located_mv[0][0]*(time_pb - time_pp)/time_pp;\
  205. int by = qy ? fy - s->me.co_located_mv[0][1] : s->me.co_located_mv[0][1]*(time_pb - time_pp)/time_pp;\
  206. int fxy= (fx&3) + 4*(fy&3);\
  207. int bxy= (bx&3) + 4*(by&3);\
  208. \
  209. qpel_put[0][fxy](s->me.scratchpad, (ref_y ) + (fx>>2) + (fy>>2)*(stride), stride);\
  210. qpel_avg[0][bxy](s->me.scratchpad, (ref2_y) + (bx>>2) + (by>>2)*(stride), stride);\
  211. }\
  212. d = cmp_func(s, s->me.scratchpad, src_y, stride);\
  213. }else\
  214. d= 256*256*256*32;
  215. #define CMP_QPEL(d, dx, dy, x, y, size)\
  216. CMP_DIRECT(d, dx, dy, x, y, size, cmp_sub)
  217. #define CMP(d, x, y, size)\
  218. CMP_DIRECT(d, 0, 0, x, y, size, cmp)
  219. #include "motion_est_template.c"
  220. #undef RENAME
  221. #undef CMP
  222. #undef CMP_HPEL
  223. #undef CMP_QPEL
  224. #undef INIT
  225. #undef CMP__DIRECT
  226. static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride){
  227. return 0;
  228. }
  229. static void set_cmp(MpegEncContext *s, me_cmp_func *cmp, int type){
  230. DSPContext* c= &s->dsp;
  231. int i;
  232. memset(cmp, 0, sizeof(void*)*11);
  233. switch(type&0xFF){
  234. case FF_CMP_SAD:
  235. cmp[0]= c->sad[0];
  236. cmp[1]= c->sad[1];
  237. break;
  238. case FF_CMP_SATD:
  239. cmp[0]= c->hadamard8_diff[0];
  240. cmp[1]= c->hadamard8_diff[1];
  241. break;
  242. case FF_CMP_SSE:
  243. cmp[0]= c->sse[0];
  244. cmp[1]= c->sse[1];
  245. break;
  246. case FF_CMP_DCT:
  247. cmp[0]= c->dct_sad[0];
  248. cmp[1]= c->dct_sad[1];
  249. break;
  250. case FF_CMP_PSNR:
  251. cmp[0]= c->quant_psnr[0];
  252. cmp[1]= c->quant_psnr[1];
  253. break;
  254. case FF_CMP_ZERO:
  255. for(i=0; i<7; i++){
  256. cmp[i]= zero_cmp;
  257. }
  258. break;
  259. default:
  260. fprintf(stderr,"internal error in cmp function selection\n");
  261. }
  262. };
  263. static inline int get_penalty_factor(MpegEncContext *s, int type){
  264. switch(type){
  265. default:
  266. case FF_CMP_SAD:
  267. return s->qscale;
  268. case FF_CMP_SSE:
  269. // return s->qscale*8;
  270. case FF_CMP_DCT:
  271. case FF_CMP_SATD:
  272. return s->qscale*8;
  273. }
  274. }
  275. void ff_init_me(MpegEncContext *s){
  276. set_cmp(s, s->dsp.me_cmp, s->avctx->me_cmp);
  277. set_cmp(s, s->dsp.me_sub_cmp, s->avctx->me_sub_cmp);
  278. set_cmp(s, s->dsp.mb_cmp, s->avctx->mb_cmp);
  279. if(s->flags&CODEC_FLAG_QPEL){
  280. if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
  281. s->me.sub_motion_search= simple_chroma_qpel_motion_search;
  282. else
  283. s->me.sub_motion_search= simple_qpel_motion_search;
  284. }else{
  285. if(s->avctx->me_sub_cmp&FF_CMP_CHROMA)
  286. s->me.sub_motion_search= simple_chroma_hpel_motion_search;
  287. else if(s->avctx->me_sub_cmp == FF_CMP_SAD && s->avctx->me_cmp == FF_CMP_SAD)
  288. s->me.sub_motion_search= sad_hpel_motion_search;
  289. else
  290. s->me.sub_motion_search= simple_hpel_motion_search;
  291. }
  292. if(s->avctx->me_cmp&FF_CMP_CHROMA){
  293. s->me.motion_search[0]= simple_chroma_epzs_motion_search;
  294. s->me.motion_search[1]= simple_chroma_epzs_motion_search4;
  295. }else{
  296. s->me.motion_search[0]= simple_epzs_motion_search;
  297. s->me.motion_search[1]= simple_epzs_motion_search4;
  298. }
  299. }
  300. static int pix_dev(UINT8 * pix, int line_size, int mean)
  301. {
  302. int s, i, j;
  303. s = 0;
  304. for (i = 0; i < 16; i++) {
  305. for (j = 0; j < 16; j += 8) {
  306. s += ABS(pix[0]-mean);
  307. s += ABS(pix[1]-mean);
  308. s += ABS(pix[2]-mean);
  309. s += ABS(pix[3]-mean);
  310. s += ABS(pix[4]-mean);
  311. s += ABS(pix[5]-mean);
  312. s += ABS(pix[6]-mean);
  313. s += ABS(pix[7]-mean);
  314. pix += 8;
  315. }
  316. pix += line_size - 16;
  317. }
  318. return s;
  319. }
  320. static inline void no_motion_search(MpegEncContext * s,
  321. int *mx_ptr, int *my_ptr)
  322. {
  323. *mx_ptr = 16 * s->mb_x;
  324. *my_ptr = 16 * s->mb_y;
  325. }
  326. static int full_motion_search(MpegEncContext * s,
  327. int *mx_ptr, int *my_ptr, int range,
  328. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  329. {
  330. int x1, y1, x2, y2, xx, yy, x, y;
  331. int mx, my, dmin, d;
  332. UINT8 *pix;
  333. xx = 16 * s->mb_x;
  334. yy = 16 * s->mb_y;
  335. x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
  336. if (x1 < xmin)
  337. x1 = xmin;
  338. x2 = xx + range - 1;
  339. if (x2 > xmax)
  340. x2 = xmax;
  341. y1 = yy - range + 1;
  342. if (y1 < ymin)
  343. y1 = ymin;
  344. y2 = yy + range - 1;
  345. if (y2 > ymax)
  346. y2 = ymax;
  347. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  348. dmin = 0x7fffffff;
  349. mx = 0;
  350. my = 0;
  351. for (y = y1; y <= y2; y++) {
  352. for (x = x1; x <= x2; x++) {
  353. d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
  354. s->linesize);
  355. if (d < dmin ||
  356. (d == dmin &&
  357. (abs(x - xx) + abs(y - yy)) <
  358. (abs(mx - xx) + abs(my - yy)))) {
  359. dmin = d;
  360. mx = x;
  361. my = y;
  362. }
  363. }
  364. }
  365. *mx_ptr = mx;
  366. *my_ptr = my;
  367. #if 0
  368. if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
  369. *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
  370. fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
  371. }
  372. #endif
  373. return dmin;
  374. }
  375. static int log_motion_search(MpegEncContext * s,
  376. int *mx_ptr, int *my_ptr, int range,
  377. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  378. {
  379. int x1, y1, x2, y2, xx, yy, x, y;
  380. int mx, my, dmin, d;
  381. UINT8 *pix;
  382. xx = s->mb_x << 4;
  383. yy = s->mb_y << 4;
  384. /* Left limit */
  385. x1 = xx - range;
  386. if (x1 < xmin)
  387. x1 = xmin;
  388. /* Right limit */
  389. x2 = xx + range;
  390. if (x2 > xmax)
  391. x2 = xmax;
  392. /* Upper limit */
  393. y1 = yy - range;
  394. if (y1 < ymin)
  395. y1 = ymin;
  396. /* Lower limit */
  397. y2 = yy + range;
  398. if (y2 > ymax)
  399. y2 = ymax;
  400. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  401. dmin = 0x7fffffff;
  402. mx = 0;
  403. my = 0;
  404. do {
  405. for (y = y1; y <= y2; y += range) {
  406. for (x = x1; x <= x2; x += range) {
  407. d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
  408. if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  409. dmin = d;
  410. mx = x;
  411. my = y;
  412. }
  413. }
  414. }
  415. range = range >> 1;
  416. x1 = mx - range;
  417. if (x1 < xmin)
  418. x1 = xmin;
  419. x2 = mx + range;
  420. if (x2 > xmax)
  421. x2 = xmax;
  422. y1 = my - range;
  423. if (y1 < ymin)
  424. y1 = ymin;
  425. y2 = my + range;
  426. if (y2 > ymax)
  427. y2 = ymax;
  428. } while (range >= 1);
  429. #ifdef DEBUG
  430. fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
  431. #endif
  432. *mx_ptr = mx;
  433. *my_ptr = my;
  434. return dmin;
  435. }
  436. static int phods_motion_search(MpegEncContext * s,
  437. int *mx_ptr, int *my_ptr, int range,
  438. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  439. {
  440. int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
  441. int mx, my, dminx, dminy;
  442. UINT8 *pix;
  443. xx = s->mb_x << 4;
  444. yy = s->mb_y << 4;
  445. /* Left limit */
  446. x1 = xx - range;
  447. if (x1 < xmin)
  448. x1 = xmin;
  449. /* Right limit */
  450. x2 = xx + range;
  451. if (x2 > xmax)
  452. x2 = xmax;
  453. /* Upper limit */
  454. y1 = yy - range;
  455. if (y1 < ymin)
  456. y1 = ymin;
  457. /* Lower limit */
  458. y2 = yy + range;
  459. if (y2 > ymax)
  460. y2 = ymax;
  461. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  462. mx = 0;
  463. my = 0;
  464. x = xx;
  465. y = yy;
  466. do {
  467. dminx = 0x7fffffff;
  468. dminy = 0x7fffffff;
  469. lastx = x;
  470. for (x = x1; x <= x2; x += range) {
  471. d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
  472. if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  473. dminx = d;
  474. mx = x;
  475. }
  476. }
  477. x = lastx;
  478. for (y = y1; y <= y2; y += range) {
  479. d = s->dsp.pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
  480. if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  481. dminy = d;
  482. my = y;
  483. }
  484. }
  485. range = range >> 1;
  486. x = mx;
  487. y = my;
  488. x1 = mx - range;
  489. if (x1 < xmin)
  490. x1 = xmin;
  491. x2 = mx + range;
  492. if (x2 > xmax)
  493. x2 = xmax;
  494. y1 = my - range;
  495. if (y1 < ymin)
  496. y1 = ymin;
  497. y2 = my + range;
  498. if (y2 > ymax)
  499. y2 = ymax;
  500. } while (range >= 1);
  501. #ifdef DEBUG
  502. fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
  503. #endif
  504. /* half pixel search */
  505. *mx_ptr = mx;
  506. *my_ptr = my;
  507. return dminy;
  508. }
  509. #define Z_THRESHOLD 256
  510. #define CHECK_SAD_HALF_MV(suffix, x, y) \
  511. {\
  512. d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
  513. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
  514. COPY3_IF_LT(dminh, d, dx, x, dy, y)\
  515. }
  516. static inline int sad_hpel_motion_search(MpegEncContext * s,
  517. int *mx_ptr, int *my_ptr, int dmin,
  518. int xmin, int ymin, int xmax, int ymax,
  519. int pred_x, int pred_y, Picture *picture,
  520. int n, int size, uint16_t * const mv_penalty)
  521. {
  522. uint8_t *ref_picture= picture->data[0];
  523. uint32_t *score_map= s->me.score_map;
  524. const int penalty_factor= s->me.sub_penalty_factor;
  525. int mx, my, xx, yy, dminh;
  526. UINT8 *pix, *ptr;
  527. op_pixels_abs_func pix_abs_x2;
  528. op_pixels_abs_func pix_abs_y2;
  529. op_pixels_abs_func pix_abs_xy2;
  530. if(size==0){
  531. pix_abs_x2 = s->dsp.pix_abs16x16_x2;
  532. pix_abs_y2 = s->dsp.pix_abs16x16_y2;
  533. pix_abs_xy2= s->dsp.pix_abs16x16_xy2;
  534. }else{
  535. pix_abs_x2 = s->dsp.pix_abs8x8_x2;
  536. pix_abs_y2 = s->dsp.pix_abs8x8_y2;
  537. pix_abs_xy2= s->dsp.pix_abs8x8_xy2;
  538. }
  539. if(s->me.skip){
  540. // printf("S");
  541. *mx_ptr = 0;
  542. *my_ptr = 0;
  543. return dmin;
  544. }
  545. // printf("N");
  546. xx = 16 * s->mb_x + 8*(n&1);
  547. yy = 16 * s->mb_y + 8*(n>>1);
  548. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  549. mx = *mx_ptr;
  550. my = *my_ptr;
  551. ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
  552. dminh = dmin;
  553. if (mx > xmin && mx < xmax &&
  554. my > ymin && my < ymax) {
  555. int dx=0, dy=0;
  556. int d, pen_x, pen_y;
  557. const int index= (my<<ME_MAP_SHIFT) + mx;
  558. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  559. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  560. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  561. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  562. mx<<=1;
  563. my<<=1;
  564. pen_x= pred_x + mx;
  565. pen_y= pred_y + my;
  566. ptr-= s->linesize;
  567. if(t<=b){
  568. CHECK_SAD_HALF_MV(y2 , 0, -1)
  569. if(l<=r){
  570. CHECK_SAD_HALF_MV(xy2, -1, -1)
  571. if(t+r<=b+l){
  572. CHECK_SAD_HALF_MV(xy2, +1, -1)
  573. ptr+= s->linesize;
  574. }else{
  575. ptr+= s->linesize;
  576. CHECK_SAD_HALF_MV(xy2, -1, +1)
  577. }
  578. CHECK_SAD_HALF_MV(x2 , -1, 0)
  579. }else{
  580. CHECK_SAD_HALF_MV(xy2, +1, -1)
  581. if(t+l<=b+r){
  582. CHECK_SAD_HALF_MV(xy2, -1, -1)
  583. ptr+= s->linesize;
  584. }else{
  585. ptr+= s->linesize;
  586. CHECK_SAD_HALF_MV(xy2, +1, +1)
  587. }
  588. CHECK_SAD_HALF_MV(x2 , +1, 0)
  589. }
  590. }else{
  591. if(l<=r){
  592. if(t+l<=b+r){
  593. CHECK_SAD_HALF_MV(xy2, -1, -1)
  594. ptr+= s->linesize;
  595. }else{
  596. ptr+= s->linesize;
  597. CHECK_SAD_HALF_MV(xy2, +1, +1)
  598. }
  599. CHECK_SAD_HALF_MV(x2 , -1, 0)
  600. CHECK_SAD_HALF_MV(xy2, -1, +1)
  601. }else{
  602. if(t+r<=b+l){
  603. CHECK_SAD_HALF_MV(xy2, +1, -1)
  604. ptr+= s->linesize;
  605. }else{
  606. ptr+= s->linesize;
  607. CHECK_SAD_HALF_MV(xy2, -1, +1)
  608. }
  609. CHECK_SAD_HALF_MV(x2 , +1, 0)
  610. CHECK_SAD_HALF_MV(xy2, +1, +1)
  611. }
  612. CHECK_SAD_HALF_MV(y2 , 0, +1)
  613. }
  614. mx+=dx;
  615. my+=dy;
  616. }else{
  617. mx<<=1;
  618. my<<=1;
  619. }
  620. *mx_ptr = mx;
  621. *my_ptr = my;
  622. return dminh;
  623. }
  624. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  625. {
  626. const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
  627. s->p_mv_table[xy][0] = mx;
  628. s->p_mv_table[xy][1] = my;
  629. /* has allready been set to the 4 MV if 4MV is done */
  630. if(mv4){
  631. int mot_xy= s->block_index[0];
  632. s->motion_val[mot_xy ][0]= mx;
  633. s->motion_val[mot_xy ][1]= my;
  634. s->motion_val[mot_xy+1][0]= mx;
  635. s->motion_val[mot_xy+1][1]= my;
  636. mot_xy += s->block_wrap[0];
  637. s->motion_val[mot_xy ][0]= mx;
  638. s->motion_val[mot_xy ][1]= my;
  639. s->motion_val[mot_xy+1][0]= mx;
  640. s->motion_val[mot_xy+1][1]= my;
  641. }
  642. }
  643. static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
  644. {
  645. *range = 8 * (1 << (f_code - 1));
  646. /* XXX: temporary kludge to avoid overflow for msmpeg4 */
  647. if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
  648. *range *= 2;
  649. if (s->unrestricted_mv) {
  650. *xmin = -16;
  651. *ymin = -16;
  652. if (s->h263_plus)
  653. *range *= 2;
  654. if(s->avctx->codec->id!=CODEC_ID_MPEG4){
  655. *xmax = s->mb_width*16;
  656. *ymax = s->mb_height*16;
  657. }else {
  658. *xmax = s->width;
  659. *ymax = s->height;
  660. }
  661. } else {
  662. *xmin = 0;
  663. *ymin = 0;
  664. *xmax = s->mb_width*16 - 16;
  665. *ymax = s->mb_height*16 - 16;
  666. }
  667. }
  668. static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift)
  669. {
  670. int block;
  671. int P[10][2];
  672. uint8_t *ref_picture= s->last_picture.data[0];
  673. int dmin_sum=0;
  674. uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
  675. for(block=0; block<4; block++){
  676. int mx4, my4;
  677. int pred_x4, pred_y4;
  678. int dmin4;
  679. static const int off[4]= {2, 1, 1, -1};
  680. const int mot_stride = s->block_wrap[0];
  681. const int mot_xy = s->block_index[block];
  682. // const int block_x= (block&1);
  683. // const int block_y= (block>>1);
  684. #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way
  685. const int rel_xmin4= xmin;
  686. const int rel_xmax4= xmax;
  687. const int rel_ymin4= ymin;
  688. const int rel_ymax4= ymax;
  689. #else
  690. const int rel_xmin4= xmin - block_x*8;
  691. const int rel_xmax4= xmax - block_x*8 + 8;
  692. const int rel_ymin4= ymin - block_y*8;
  693. const int rel_ymax4= ymax - block_y*8 + 8;
  694. #endif
  695. P_LEFT[0] = s->motion_val[mot_xy - 1][0];
  696. P_LEFT[1] = s->motion_val[mot_xy - 1][1];
  697. if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift);
  698. /* special case for first line */
  699. if (s->mb_y == 0 && block<2) {
  700. pred_x4= P_LEFT[0];
  701. pred_y4= P_LEFT[1];
  702. } else {
  703. P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
  704. P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
  705. P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
  706. P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
  707. if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift);
  708. if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift);
  709. if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift);
  710. if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift);
  711. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  712. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  713. if(s->out_format == FMT_H263){
  714. pred_x4 = P_MEDIAN[0];
  715. pred_y4 = P_MEDIAN[1];
  716. }else { /* mpeg1 at least */
  717. pred_x4= P_LEFT[0];
  718. pred_y4= P_LEFT[1];
  719. }
  720. }
  721. P_MV1[0]= mx;
  722. P_MV1[1]= my;
  723. dmin4 = s->me.motion_search[1](s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
  724. &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty);
  725. dmin4= s->me.sub_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
  726. pred_x4, pred_y4, &s->last_picture, block, 1, mv_penalty);
  727. s->motion_val[ s->block_index[block] ][0]= mx4;
  728. s->motion_val[ s->block_index[block] ][1]= my4;
  729. dmin_sum+= dmin4;
  730. }
  731. return dmin_sum;
  732. }
  733. void ff_estimate_p_frame_motion(MpegEncContext * s,
  734. int mb_x, int mb_y)
  735. {
  736. UINT8 *pix, *ppix;
  737. int sum, varc, vard, mx, my, range, dmin, xx, yy;
  738. int xmin, ymin, xmax, ymax;
  739. int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
  740. int pred_x=0, pred_y=0;
  741. int P[10][2];
  742. const int shift= 1+s->quarter_sample;
  743. int mb_type=0;
  744. uint8_t *ref_picture= s->last_picture.data[0];
  745. Picture * const pic= &s->current_picture;
  746. uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
  747. assert(s->quarter_sample==0 || s->quarter_sample==1);
  748. s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp);
  749. s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
  750. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
  751. rel_xmin= xmin - mb_x*16;
  752. rel_xmax= xmax - mb_x*16;
  753. rel_ymin= ymin - mb_y*16;
  754. rel_ymax= ymax - mb_y*16;
  755. s->me.skip=0;
  756. switch(s->me_method) {
  757. case ME_ZERO:
  758. default:
  759. no_motion_search(s, &mx, &my);
  760. mx-= mb_x*16;
  761. my-= mb_y*16;
  762. dmin = 0;
  763. break;
  764. case ME_FULL:
  765. dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
  766. mx-= mb_x*16;
  767. my-= mb_y*16;
  768. break;
  769. case ME_LOG:
  770. dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  771. mx-= mb_x*16;
  772. my-= mb_y*16;
  773. break;
  774. case ME_PHODS:
  775. dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  776. mx-= mb_x*16;
  777. my-= mb_y*16;
  778. break;
  779. case ME_X1:
  780. case ME_EPZS:
  781. {
  782. const int mot_stride = s->block_wrap[0];
  783. const int mot_xy = s->block_index[0];
  784. P_LEFT[0] = s->motion_val[mot_xy - 1][0];
  785. P_LEFT[1] = s->motion_val[mot_xy - 1][1];
  786. if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
  787. if(mb_y) {
  788. P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
  789. P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
  790. P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0];
  791. P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1];
  792. if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift);
  793. if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
  794. if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
  795. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  796. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  797. if(s->out_format == FMT_H263){
  798. pred_x = P_MEDIAN[0];
  799. pred_y = P_MEDIAN[1];
  800. }else { /* mpeg1 at least */
  801. pred_x= P_LEFT[0];
  802. pred_y= P_LEFT[1];
  803. }
  804. }else{
  805. pred_x= P_LEFT[0];
  806. pred_y= P_LEFT[1];
  807. }
  808. }
  809. dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
  810. &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty);
  811. break;
  812. }
  813. /* intra / predictive decision */
  814. xx = mb_x * 16;
  815. yy = mb_y * 16;
  816. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  817. /* At this point (mx,my) are full-pell and the relative displacement */
  818. ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx);
  819. sum = s->dsp.pix_sum(pix, s->linesize);
  820. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  821. vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize)+128)>>8;
  822. //printf("%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
  823. pic->mb_var [s->mb_width * mb_y + mb_x] = varc;
  824. pic->mc_mb_var[s->mb_width * mb_y + mb_x] = vard;
  825. pic->mb_mean [s->mb_width * mb_y + mb_x] = (sum+128)>>8;
  826. pic->mb_var_sum += varc;
  827. pic->mc_mb_var_sum += vard;
  828. //printf("E%d %d %d %X %X %X\n", s->mb_width, mb_x, mb_y,(int)s, (int)s->mb_var, (int)s->mc_mb_var); fflush(stdout);
  829. #if 0
  830. printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
  831. varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
  832. #endif
  833. if(s->flags&CODEC_FLAG_HQ){
  834. if (vard <= 64 || vard < varc)
  835. s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  836. else
  837. s->scene_change_score+= s->qscale;
  838. if (vard*2 + 200 > varc)
  839. mb_type|= MB_TYPE_INTRA;
  840. if (varc*2 + 200 > vard){
  841. mb_type|= MB_TYPE_INTER;
  842. s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
  843. pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty);
  844. }else{
  845. mx <<=shift;
  846. my <<=shift;
  847. }
  848. if((s->flags&CODEC_FLAG_4MV)
  849. && !s->me.skip && varc>50 && vard>10){
  850. mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
  851. mb_type|=MB_TYPE_INTER4V;
  852. set_p_mv_tables(s, mx, my, 0);
  853. }else
  854. set_p_mv_tables(s, mx, my, 1);
  855. }else{
  856. if (vard <= 64 || vard < varc) {
  857. // if (sadP <= 32 || sadP < sadI + 500) {
  858. s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  859. mb_type|= MB_TYPE_INTER;
  860. if (s->me_method != ME_ZERO) {
  861. dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
  862. pred_x, pred_y, &s->last_picture, 0, 0, mv_penalty);
  863. if((s->flags&CODEC_FLAG_4MV)
  864. && !s->me.skip && varc>50 && vard>10){
  865. int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
  866. if(dmin4 + 128 <dmin)
  867. mb_type= MB_TYPE_INTER4V;
  868. }
  869. set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V);
  870. } else {
  871. mx <<=shift;
  872. my <<=shift;
  873. }
  874. #if 0
  875. if (vard < 10) {
  876. skip++;
  877. fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d",
  878. skip, vard, varc, dmin);
  879. }
  880. #endif
  881. }else{
  882. s->scene_change_score+= 20;
  883. mb_type|= MB_TYPE_INTRA;
  884. mx = 0;
  885. my = 0;
  886. }
  887. }
  888. s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  889. }
  890. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  891. int mb_x, int mb_y)
  892. {
  893. int mx, my, range, dmin;
  894. int xmin, ymin, xmax, ymax;
  895. int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
  896. int pred_x=0, pred_y=0;
  897. int P[10][2];
  898. const int shift= 1+s->quarter_sample;
  899. uint16_t * const mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV;
  900. const int mv_stride= s->mb_width + 2;
  901. const int xy= mb_x + 1 + (mb_y + 1)*mv_stride;
  902. assert(s->quarter_sample==0 || s->quarter_sample==1);
  903. s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp);
  904. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
  905. rel_xmin= xmin - mb_x*16;
  906. rel_xmax= xmax - mb_x*16;
  907. rel_ymin= ymin - mb_y*16;
  908. rel_ymax= ymax - mb_y*16;
  909. s->me.skip=0;
  910. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  911. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  912. if(P_LEFT[0] < (rel_xmin<<shift)) P_LEFT[0] = (rel_xmin<<shift);
  913. /* special case for first line */
  914. if (mb_y == s->mb_height-1) {
  915. pred_x= P_LEFT[0];
  916. pred_y= P_LEFT[1];
  917. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  918. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  919. } else {
  920. P_TOP[0] = s->p_mv_table[xy + mv_stride ][0];
  921. P_TOP[1] = s->p_mv_table[xy + mv_stride ][1];
  922. P_TOPRIGHT[0] = s->p_mv_table[xy + mv_stride - 1][0];
  923. P_TOPRIGHT[1] = s->p_mv_table[xy + mv_stride - 1][1];
  924. if(P_TOP[1] < (rel_ymin<<shift)) P_TOP[1] = (rel_ymin<<shift);
  925. if(P_TOPRIGHT[0] > (rel_xmax<<shift)) P_TOPRIGHT[0]= (rel_xmax<<shift);
  926. if(P_TOPRIGHT[1] < (rel_ymin<<shift)) P_TOPRIGHT[1]= (rel_ymin<<shift);
  927. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  928. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  929. pred_x = P_MEDIAN[0];
  930. pred_y = P_MEDIAN[1];
  931. }
  932. dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
  933. &s->last_picture, s->p_mv_table, (1<<16)>>shift, mv_penalty);
  934. s->p_mv_table[xy][0] = mx<<shift;
  935. s->p_mv_table[xy][1] = my<<shift;
  936. return dmin;
  937. }
  938. int ff_estimate_motion_b(MpegEncContext * s,
  939. int mb_x, int mb_y, int16_t (*mv_table)[2], Picture *picture, int f_code)
  940. {
  941. int mx, my, range, dmin;
  942. int xmin, ymin, xmax, ymax;
  943. int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
  944. int pred_x=0, pred_y=0;
  945. int P[10][2];
  946. const int shift= 1+s->quarter_sample;
  947. const int mot_stride = s->mb_width + 2;
  948. const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
  949. uint8_t * const ref_picture= picture->data[0];
  950. uint16_t * const mv_penalty= s->me.mv_penalty[f_code] + MAX_MV;
  951. int mv_scale;
  952. s->me.penalty_factor = get_penalty_factor(s, s->avctx->me_cmp);
  953. s->me.sub_penalty_factor= get_penalty_factor(s, s->avctx->me_sub_cmp);
  954. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
  955. rel_xmin= xmin - mb_x*16;
  956. rel_xmax= xmax - mb_x*16;
  957. rel_ymin= ymin - mb_y*16;
  958. rel_ymax= ymax - mb_y*16;
  959. switch(s->me_method) {
  960. case ME_ZERO:
  961. default:
  962. no_motion_search(s, &mx, &my);
  963. dmin = 0;
  964. mx-= mb_x*16;
  965. my-= mb_y*16;
  966. break;
  967. case ME_FULL:
  968. dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
  969. mx-= mb_x*16;
  970. my-= mb_y*16;
  971. break;
  972. case ME_LOG:
  973. dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  974. mx-= mb_x*16;
  975. my-= mb_y*16;
  976. break;
  977. case ME_PHODS:
  978. dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  979. mx-= mb_x*16;
  980. my-= mb_y*16;
  981. break;
  982. case ME_X1:
  983. case ME_EPZS:
  984. {
  985. P_LEFT[0] = mv_table[mot_xy - 1][0];
  986. P_LEFT[1] = mv_table[mot_xy - 1][1];
  987. if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
  988. /* special case for first line */
  989. if (mb_y) {
  990. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  991. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  992. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
  993. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
  994. if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift);
  995. if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
  996. if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
  997. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  998. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  999. }
  1000. pred_x= P_LEFT[0];
  1001. pred_y= P_LEFT[1];
  1002. }
  1003. if(mv_table == s->b_forw_mv_table){
  1004. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1005. }else{
  1006. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1007. }
  1008. dmin = s->me.motion_search[0](s, 0, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
  1009. picture, s->p_mv_table, mv_scale, mv_penalty);
  1010. break;
  1011. }
  1012. dmin= s->me.sub_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
  1013. pred_x, pred_y, picture, 0, 0, mv_penalty);
  1014. //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
  1015. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1016. mv_table[mot_xy][0]= mx;
  1017. mv_table[mot_xy][1]= my;
  1018. return dmin;
  1019. }
  1020. static inline int check_bidir_mv(MpegEncContext * s,
  1021. int mb_x, int mb_y,
  1022. int motion_fx, int motion_fy,
  1023. int motion_bx, int motion_by,
  1024. int pred_fx, int pred_fy,
  1025. int pred_bx, int pred_by)
  1026. {
  1027. //FIXME optimize?
  1028. //FIXME move into template?
  1029. //FIXME better f_code prediction (max mv & distance)
  1030. UINT16 *mv_penalty= s->me.mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1031. uint8_t *dest_y = s->me.scratchpad;
  1032. uint8_t *ptr;
  1033. int dxy;
  1034. int src_x, src_y;
  1035. int fbmin;
  1036. if(s->quarter_sample){
  1037. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1038. src_x = mb_x * 16 + (motion_fx >> 2);
  1039. src_y = mb_y * 16 + (motion_fy >> 2);
  1040. assert(src_x >=-16 && src_x<=s->width);
  1041. assert(src_y >=-16 && src_y<=s->height);
  1042. ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x;
  1043. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize);
  1044. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1045. src_x = mb_x * 16 + (motion_bx >> 2);
  1046. src_y = mb_y * 16 + (motion_by >> 2);
  1047. assert(src_x >=-16 && src_x<=s->width);
  1048. assert(src_y >=-16 && src_y<=s->height);
  1049. ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x;
  1050. s->dsp.avg_qpel_pixels_tab[0][dxy](dest_y , ptr , s->linesize);
  1051. }else{
  1052. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1053. src_x = mb_x * 16 + (motion_fx >> 1);
  1054. src_y = mb_y * 16 + (motion_fy >> 1);
  1055. assert(src_x >=-16 && src_x<=s->width);
  1056. assert(src_y >=-16 && src_y<=s->height);
  1057. ptr = s->last_picture.data[0] + (src_y * s->linesize) + src_x;
  1058. s->dsp.put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
  1059. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1060. src_x = mb_x * 16 + (motion_bx >> 1);
  1061. src_y = mb_y * 16 + (motion_by >> 1);
  1062. assert(src_x >=-16 && src_x<=s->width);
  1063. assert(src_y >=-16 && src_y<=s->height);
  1064. ptr = s->next_picture.data[0] + (src_y * s->linesize) + src_x;
  1065. s->dsp.avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
  1066. }
  1067. fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->me.sub_penalty_factor
  1068. +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->me.sub_penalty_factor;
  1069. + s->dsp.me_sub_cmp[0](s, s->new_picture.data[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
  1070. return fbmin;
  1071. }
  1072. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1073. static inline int bidir_refine(MpegEncContext * s,
  1074. int mb_x, int mb_y)
  1075. {
  1076. const int mot_stride = s->mb_width + 2;
  1077. const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
  1078. int fbmin;
  1079. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1080. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1081. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1082. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1083. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1084. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1085. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1086. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1087. //FIXME do refinement and add flag
  1088. fbmin= check_bidir_mv(s, mb_x, mb_y,
  1089. motion_fx, motion_fy,
  1090. motion_bx, motion_by,
  1091. pred_fx, pred_fy,
  1092. pred_bx, pred_by);
  1093. return fbmin;
  1094. }
  1095. static inline int direct_search(MpegEncContext * s,
  1096. int mb_x, int mb_y)
  1097. {
  1098. int P[10][2];
  1099. const int mot_stride = s->mb_width + 2;
  1100. const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
  1101. const int shift= 1+s->quarter_sample;
  1102. int dmin, i;
  1103. const int time_pp= s->pp_time;
  1104. const int time_pb= s->pb_time;
  1105. int mx, my, xmin, xmax, ymin, ymax;
  1106. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1107. uint16_t * const mv_penalty= s->me.mv_penalty[1] + MAX_MV;
  1108. ymin= xmin=(-32)>>shift;
  1109. ymax= xmax= 31>>shift;
  1110. if(s->co_located_type_table[mb_x + mb_y*s->mb_width]==CO_LOCATED_TYPE_4MV){
  1111. s->mv_type= MV_TYPE_8X8;
  1112. }else{
  1113. s->mv_type= MV_TYPE_16X16;
  1114. }
  1115. for(i=0; i<4; i++){
  1116. int index= s->block_index[i];
  1117. int min, max;
  1118. s->me.co_located_mv[i][0]= s->motion_val[index][0];
  1119. s->me.co_located_mv[i][1]= s->motion_val[index][1];
  1120. s->me.direct_basis_mv[i][0]= s->me.co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1121. s->me.direct_basis_mv[i][1]= s->me.co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1122. // s->me.direct_basis_mv[1][i][0]= s->me.co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1123. // s->me.direct_basis_mv[1][i][1]= s->me.co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1124. max= FFMAX(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
  1125. min= FFMIN(s->me.direct_basis_mv[i][0], s->me.direct_basis_mv[i][0] - s->me.co_located_mv[i][0])>>shift;
  1126. max+= (2*mb_x + (i& 1))*8 - 1; // +-1 is for the simpler rounding
  1127. min+= (2*mb_x + (i& 1))*8 + 1;
  1128. if(max >= s->width) xmax= s->width - max - 1;
  1129. if(min < -16 ) xmin= - 32 - min;
  1130. max= FFMAX(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
  1131. min= FFMIN(s->me.direct_basis_mv[i][1], s->me.direct_basis_mv[i][1] - s->me.co_located_mv[i][1])>>shift;
  1132. max+= (2*mb_y + (i>>1))*8 - 1; // +-1 is for the simpler rounding
  1133. min+= (2*mb_y + (i>>1))*8 + 1;
  1134. if(max >= s->height) ymax= s->height - max - 1;
  1135. if(min < -16 ) ymin= - 32 - min;
  1136. if(s->mv_type == MV_TYPE_16X16) break;
  1137. }
  1138. assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1139. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1140. s->b_direct_mv_table[mot_xy][0]= 0;
  1141. s->b_direct_mv_table[mot_xy][1]= 0;
  1142. return 256*256*256*64;
  1143. }
  1144. P_LEFT[0] = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1145. P_LEFT[1] = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1146. /* special case for first line */
  1147. if (mb_y) {
  1148. P_TOP[0] = clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1149. P_TOP[1] = clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1150. P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1151. P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1152. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1153. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1154. }
  1155. if(s->flags&CODEC_FLAG_QPEL){
  1156. dmin = simple_direct_qpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax,
  1157. &s->last_picture, mv_table, 1<<14, mv_penalty);
  1158. dmin = simple_direct_qpel_qpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax,
  1159. 0, 0, &s->last_picture, 0, 0, mv_penalty);
  1160. }else{
  1161. dmin = simple_direct_hpel_epzs_motion_search(s, 0, &mx, &my, P, 0, 0, xmin, ymin, xmax, ymax,
  1162. &s->last_picture, mv_table, 1<<15, mv_penalty);
  1163. dmin = simple_direct_hpel_hpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax,
  1164. 0, 0, &s->last_picture, 0, 0, mv_penalty);
  1165. }
  1166. s->b_direct_mv_table[mot_xy][0]= mx;
  1167. s->b_direct_mv_table[mot_xy][1]= my;
  1168. return dmin;
  1169. }
  1170. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1171. int mb_x, int mb_y)
  1172. {
  1173. const int penalty_factor= s->me.penalty_factor;
  1174. int fmin, bmin, dmin, fbmin;
  1175. int type=0;
  1176. dmin= direct_search(s, mb_x, mb_y);
  1177. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, &s->last_picture, s->f_code);
  1178. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, &s->next_picture, s->b_code) - penalty_factor;
  1179. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1180. fbmin= bidir_refine(s, mb_x, mb_y);
  1181. {
  1182. int score= dmin;
  1183. type=MB_TYPE_DIRECT;
  1184. if(fmin<score){
  1185. score=fmin;
  1186. type= MB_TYPE_FORWARD;
  1187. }
  1188. if(bmin<score){
  1189. score=bmin;
  1190. type= MB_TYPE_BACKWARD;
  1191. }
  1192. if(fbmin<score){
  1193. score=fbmin;
  1194. type= MB_TYPE_BIDIR;
  1195. }
  1196. score= ((unsigned)(score*score + 128*256))>>16;
  1197. s->current_picture.mc_mb_var_sum += score;
  1198. s->current_picture.mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSD
  1199. }
  1200. if(s->flags&CODEC_FLAG_HQ){
  1201. type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter
  1202. if(dmin>256*256*16) type&= ~MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
  1203. }
  1204. s->mb_type[mb_y*s->mb_width + mb_x]= type;
  1205. }
  1206. /* find best f_code for ME which do unlimited searches */
  1207. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1208. {
  1209. if(s->me_method>=ME_EPZS){
  1210. int score[8];
  1211. int i, y;
  1212. UINT8 * fcode_tab= s->fcode_tab;
  1213. int best_fcode=-1;
  1214. int best_score=-10000000;
  1215. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1216. for(y=0; y<s->mb_height; y++){
  1217. int x;
  1218. int xy= (y+1)* (s->mb_width+2) + 1;
  1219. i= y*s->mb_width;
  1220. for(x=0; x<s->mb_width; x++){
  1221. if(s->mb_type[i] & type){
  1222. int fcode= FFMAX(fcode_tab[mv_table[xy][0] + MAX_MV],
  1223. fcode_tab[mv_table[xy][1] + MAX_MV]);
  1224. int j;
  1225. for(j=0; j<fcode && j<8; j++){
  1226. if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[i] < s->current_picture.mb_var[i])
  1227. score[j]-= 170;
  1228. }
  1229. }
  1230. i++;
  1231. xy++;
  1232. }
  1233. }
  1234. for(i=1; i<8; i++){
  1235. if(score[i] > best_score){
  1236. best_score= score[i];
  1237. best_fcode= i;
  1238. }
  1239. // printf("%d %d\n", i, score[i]);
  1240. }
  1241. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1242. return best_fcode;
  1243. /* for(i=0; i<=MAX_FCODE; i++){
  1244. printf("%d ", mv_num[i]);
  1245. }
  1246. printf("\n");*/
  1247. }else{
  1248. return 1;
  1249. }
  1250. }
  1251. void ff_fix_long_p_mvs(MpegEncContext * s)
  1252. {
  1253. const int f_code= s->f_code;
  1254. int y;
  1255. UINT8 * fcode_tab= s->fcode_tab;
  1256. //int clip=0;
  1257. //int noclip=0;
  1258. /* clip / convert to intra 16x16 type MVs */
  1259. for(y=0; y<s->mb_height; y++){
  1260. int x;
  1261. int xy= (y+1)* (s->mb_width+2)+1;
  1262. int i= y*s->mb_width;
  1263. for(x=0; x<s->mb_width; x++){
  1264. if(s->mb_type[i]&MB_TYPE_INTER){
  1265. if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
  1266. || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
  1267. || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
  1268. || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
  1269. s->mb_type[i] &= ~MB_TYPE_INTER;
  1270. s->mb_type[i] |= MB_TYPE_INTRA;
  1271. s->p_mv_table[xy][0] = 0;
  1272. s->p_mv_table[xy][1] = 0;
  1273. //clip++;
  1274. }
  1275. //else
  1276. // noclip++;
  1277. }
  1278. xy++;
  1279. i++;
  1280. }
  1281. }
  1282. //printf("%d no:%d %d//\n", clip, noclip, f_code);
  1283. if(s->flags&CODEC_FLAG_4MV){
  1284. const int wrap= 2+ s->mb_width*2;
  1285. /* clip / convert to intra 8x8 type MVs */
  1286. for(y=0; y<s->mb_height; y++){
  1287. int xy= (y*2 + 1)*wrap + 1;
  1288. int i= y*s->mb_width;
  1289. int x;
  1290. for(x=0; x<s->mb_width; x++){
  1291. if(s->mb_type[i]&MB_TYPE_INTER4V){
  1292. int block;
  1293. for(block=0; block<4; block++){
  1294. int off= (block& 1) + (block>>1)*wrap;
  1295. int mx= s->motion_val[ xy + off ][0];
  1296. int my= s->motion_val[ xy + off ][1];
  1297. if( fcode_tab[mx + MAX_MV] > f_code
  1298. || fcode_tab[mx + MAX_MV] == 0
  1299. || fcode_tab[my + MAX_MV] > f_code
  1300. || fcode_tab[my + MAX_MV] == 0 ){
  1301. s->mb_type[i] &= ~MB_TYPE_INTER4V;
  1302. s->mb_type[i] |= MB_TYPE_INTRA;
  1303. }
  1304. }
  1305. }
  1306. xy+=2;
  1307. i++;
  1308. }
  1309. }
  1310. }
  1311. }
  1312. void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
  1313. {
  1314. int y;
  1315. UINT8 * fcode_tab= s->fcode_tab;
  1316. /* clip / convert to intra 16x16 type MVs */
  1317. for(y=0; y<s->mb_height; y++){
  1318. int x;
  1319. int xy= (y+1)* (s->mb_width+2)+1;
  1320. int i= y*s->mb_width;
  1321. for(x=0; x<s->mb_width; x++){
  1322. if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
  1323. || fcode_tab[mv_table[xy][0] + MAX_MV] == 0){
  1324. if(mv_table[xy][0]>0) mv_table[xy][0]= (16<<f_code)-1;
  1325. else mv_table[xy][0]= -(16<<f_code);
  1326. }
  1327. if( fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
  1328. || fcode_tab[mv_table[xy][1] + MAX_MV] == 0){
  1329. if(mv_table[xy][1]>0) mv_table[xy][1]= (16<<f_code)-1;
  1330. else mv_table[xy][1]= -(16<<f_code);
  1331. }
  1332. xy++;
  1333. i++;
  1334. }
  1335. }
  1336. }