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.

2039 lines
71KB

  1. /*
  2. * Motion estimation
  3. * Copyright (c) 2000,2001 Fabrice Bellard.
  4. * Copyright (c) 2002-2004 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. /**
  24. * @file motion_est.c
  25. * Motion estimation.
  26. */
  27. #include <stdlib.h>
  28. #include <stdio.h>
  29. #include <limits.h>
  30. #include "avcodec.h"
  31. #include "dsputil.h"
  32. #include "mpegvideo.h"
  33. #undef NDEBUG
  34. #include <assert.h>
  35. #define SQ(a) ((a)*(a))
  36. #define P_LEFT P[1]
  37. #define P_TOP P[2]
  38. #define P_TOPRIGHT P[3]
  39. #define P_MEDIAN P[4]
  40. #define P_MV1 P[9]
  41. static inline int sad_hpel_motion_search(MpegEncContext * s,
  42. int *mx_ptr, int *my_ptr, int dmin,
  43. int src_index, int ref_index,
  44. int size, int h);
  45. static inline int update_map_generation(MotionEstContext *c)
  46. {
  47. c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
  48. if(c->map_generation==0){
  49. c->map_generation= 1<<(ME_MAP_MV_BITS*2);
  50. memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
  51. }
  52. return c->map_generation;
  53. }
  54. /* shape adaptive search stuff */
  55. typedef struct Minima{
  56. int height;
  57. int x, y;
  58. int checked;
  59. }Minima;
  60. static int minima_cmp(const void *a, const void *b){
  61. const Minima *da = (const Minima *) a;
  62. const Minima *db = (const Minima *) b;
  63. return da->height - db->height;
  64. }
  65. #define FLAG_QPEL 1 //must be 1
  66. #define FLAG_CHROMA 2
  67. #define FLAG_DIRECT 4
  68. static inline void init_ref(MotionEstContext *c, uint8_t *src[3], uint8_t *ref[3], uint8_t *ref2[3], int x, int y, int ref_index){
  69. const int offset[3]= {
  70. y*c-> stride + x,
  71. ((y*c->uvstride + x)>>1),
  72. ((y*c->uvstride + x)>>1),
  73. };
  74. int i;
  75. for(i=0; i<3; i++){
  76. c->src[0][i]= src [i] + offset[i];
  77. c->ref[0][i]= ref [i] + offset[i];
  78. }
  79. if(ref_index){
  80. for(i=0; i<3; i++){
  81. c->ref[ref_index][i]= ref2[i] + offset[i];
  82. }
  83. }
  84. }
  85. static int get_flags(MotionEstContext *c, int direct, int chroma){
  86. return ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
  87. + (direct ? FLAG_DIRECT : 0)
  88. + (chroma ? FLAG_CHROMA : 0);
  89. }
  90. static always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  91. const int size, const int h, int ref_index, int src_index,
  92. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  93. MotionEstContext * const c= &s->me;
  94. const int stride= c->stride;
  95. const int uvstride= c->uvstride;
  96. const int qpel= flags&FLAG_QPEL;
  97. const int chroma= flags&FLAG_CHROMA;
  98. const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
  99. const int hx= subx + (x<<(1+qpel));
  100. const int hy= suby + (y<<(1+qpel));
  101. uint8_t * const * const ref= c->ref[ref_index];
  102. uint8_t * const * const src= c->src[src_index];
  103. int d;
  104. //FIXME check chroma 4mv, (no crashes ...)
  105. if(flags&FLAG_DIRECT){
  106. if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
  107. const int time_pp= s->pp_time;
  108. const int time_pb= s->pb_time;
  109. const int mask= 2*qpel+1;
  110. if(s->mv_type==MV_TYPE_8X8){
  111. int i;
  112. for(i=0; i<4; i++){
  113. int fx = c->direct_basis_mv[i][0] + hx;
  114. int fy = c->direct_basis_mv[i][1] + hy;
  115. int bx = hx ? fx - c->co_located_mv[i][0] : c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(qpel+4));
  116. int by = hy ? fy - c->co_located_mv[i][1] : c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(qpel+4));
  117. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  118. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  119. uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
  120. if(qpel){
  121. c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
  122. c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
  123. }else{
  124. c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
  125. c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
  126. }
  127. }
  128. }else{
  129. int fx = c->direct_basis_mv[0][0] + hx;
  130. int fy = c->direct_basis_mv[0][1] + hy;
  131. int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
  132. int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
  133. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  134. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  135. if(qpel){
  136. c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
  137. c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
  138. c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
  139. c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
  140. c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
  141. c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
  142. c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
  143. c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
  144. }else{
  145. assert((fx>>1) + 16*s->mb_x >= -16);
  146. assert((fy>>1) + 16*s->mb_y >= -16);
  147. assert((fx>>1) + 16*s->mb_x <= s->width);
  148. assert((fy>>1) + 16*s->mb_y <= s->height);
  149. assert((bx>>1) + 16*s->mb_x >= -16);
  150. assert((by>>1) + 16*s->mb_y >= -16);
  151. assert((bx>>1) + 16*s->mb_x <= s->width);
  152. assert((by>>1) + 16*s->mb_y <= s->height);
  153. c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
  154. c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
  155. }
  156. }
  157. d = cmp_func(s, c->temp, src[0], stride, 16);
  158. }else
  159. d= 256*256*256*32;
  160. }else{
  161. int uvdxy;
  162. if(dxy){
  163. if(qpel){
  164. c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
  165. if(chroma){
  166. int cx= hx/2;
  167. int cy= hy/2;
  168. cx= (cx>>1)|(cx&1);
  169. cy= (cy>>1)|(cy&1);
  170. uvdxy= (cx&1) + 2*(cy&1);
  171. //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
  172. }
  173. }else{
  174. c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
  175. if(chroma)
  176. uvdxy= dxy | (x&1) | (2*(y&1));
  177. }
  178. d = cmp_func(s, c->temp, src[0], stride, h);
  179. }else{
  180. d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
  181. if(chroma)
  182. uvdxy= (x&1) + 2*(y&1);
  183. }
  184. if(chroma){
  185. uint8_t * const uvtemp= c->temp + 16*stride;
  186. c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  187. c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  188. d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
  189. d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
  190. }
  191. }
  192. #if 0
  193. if(full_pel){
  194. const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
  195. score_map[index]= d;
  196. }
  197. d += (c->mv_penalty[hx - c->pred_x] + c->mv_penalty[hy - c->pred_y])*c->penalty_factor;
  198. #endif
  199. return d;
  200. }
  201. #include "motion_est_template.c"
  202. static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
  203. return 0;
  204. }
  205. static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
  206. }
  207. void ff_init_me(MpegEncContext *s){
  208. MotionEstContext * const c= &s->me;
  209. c->avctx= s->avctx;
  210. ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
  211. ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
  212. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
  213. ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
  214. c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
  215. c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
  216. c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
  217. /*FIXME s->no_rounding b_type*/
  218. if(s->flags&CODEC_FLAG_QPEL){
  219. c->sub_motion_search= qpel_motion_search;
  220. c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
  221. if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
  222. else c->qpel_put= s->dsp.put_qpel_pixels_tab;
  223. }else{
  224. if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
  225. c->sub_motion_search= hpel_motion_search;
  226. else if( c->avctx->me_sub_cmp == FF_CMP_SAD
  227. && c->avctx-> me_cmp == FF_CMP_SAD
  228. && c->avctx-> mb_cmp == FF_CMP_SAD)
  229. c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
  230. else
  231. c->sub_motion_search= hpel_motion_search;
  232. }
  233. c->hpel_avg= s->dsp.avg_pixels_tab;
  234. if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
  235. else c->hpel_put= s->dsp.put_pixels_tab;
  236. if(s->linesize){
  237. c->stride = s->linesize;
  238. c->uvstride= s->uvlinesize;
  239. }else{
  240. c->stride = 16*s->mb_width + 32;
  241. c->uvstride= 8*s->mb_width + 16;
  242. }
  243. // 8x8 fullpel search would need a 4x4 chroma compare, which we dont have yet, and even if we had the motion estimation code doesnt expect it
  244. if(s->codec_id != CODEC_ID_SNOW){
  245. if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
  246. s->dsp.me_cmp[2]= zero_cmp;
  247. }
  248. if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
  249. s->dsp.me_sub_cmp[2]= zero_cmp;
  250. }
  251. c->hpel_put[2][0]= c->hpel_put[2][1]=
  252. c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
  253. }
  254. if(s->codec_id == CODEC_ID_H261){
  255. c->sub_motion_search= no_sub_motion_search;
  256. }
  257. c->temp= c->scratchpad;
  258. }
  259. #if 0
  260. static int pix_dev(uint8_t * pix, int line_size, int mean)
  261. {
  262. int s, i, j;
  263. s = 0;
  264. for (i = 0; i < 16; i++) {
  265. for (j = 0; j < 16; j += 8) {
  266. s += ABS(pix[0]-mean);
  267. s += ABS(pix[1]-mean);
  268. s += ABS(pix[2]-mean);
  269. s += ABS(pix[3]-mean);
  270. s += ABS(pix[4]-mean);
  271. s += ABS(pix[5]-mean);
  272. s += ABS(pix[6]-mean);
  273. s += ABS(pix[7]-mean);
  274. pix += 8;
  275. }
  276. pix += line_size - 16;
  277. }
  278. return s;
  279. }
  280. #endif
  281. static inline void no_motion_search(MpegEncContext * s,
  282. int *mx_ptr, int *my_ptr)
  283. {
  284. *mx_ptr = 16 * s->mb_x;
  285. *my_ptr = 16 * s->mb_y;
  286. }
  287. #if 0 /* the use of these functions is inside #if 0 */
  288. static int full_motion_search(MpegEncContext * s,
  289. int *mx_ptr, int *my_ptr, int range,
  290. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  291. {
  292. int x1, y1, x2, y2, xx, yy, x, y;
  293. int mx, my, dmin, d;
  294. uint8_t *pix;
  295. xx = 16 * s->mb_x;
  296. yy = 16 * s->mb_y;
  297. x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
  298. if (x1 < xmin)
  299. x1 = xmin;
  300. x2 = xx + range - 1;
  301. if (x2 > xmax)
  302. x2 = xmax;
  303. y1 = yy - range + 1;
  304. if (y1 < ymin)
  305. y1 = ymin;
  306. y2 = yy + range - 1;
  307. if (y2 > ymax)
  308. y2 = ymax;
  309. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  310. dmin = 0x7fffffff;
  311. mx = 0;
  312. my = 0;
  313. for (y = y1; y <= y2; y++) {
  314. for (x = x1; x <= x2; x++) {
  315. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x,
  316. s->linesize, 16);
  317. if (d < dmin ||
  318. (d == dmin &&
  319. (abs(x - xx) + abs(y - yy)) <
  320. (abs(mx - xx) + abs(my - yy)))) {
  321. dmin = d;
  322. mx = x;
  323. my = y;
  324. }
  325. }
  326. }
  327. *mx_ptr = mx;
  328. *my_ptr = my;
  329. #if 0
  330. if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
  331. *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
  332. fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
  333. }
  334. #endif
  335. return dmin;
  336. }
  337. static int log_motion_search(MpegEncContext * s,
  338. int *mx_ptr, int *my_ptr, int range,
  339. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  340. {
  341. int x1, y1, x2, y2, xx, yy, x, y;
  342. int mx, my, dmin, d;
  343. uint8_t *pix;
  344. xx = s->mb_x << 4;
  345. yy = s->mb_y << 4;
  346. /* Left limit */
  347. x1 = xx - range;
  348. if (x1 < xmin)
  349. x1 = xmin;
  350. /* Right limit */
  351. x2 = xx + range;
  352. if (x2 > xmax)
  353. x2 = xmax;
  354. /* Upper limit */
  355. y1 = yy - range;
  356. if (y1 < ymin)
  357. y1 = ymin;
  358. /* Lower limit */
  359. y2 = yy + range;
  360. if (y2 > ymax)
  361. y2 = ymax;
  362. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  363. dmin = 0x7fffffff;
  364. mx = 0;
  365. my = 0;
  366. do {
  367. for (y = y1; y <= y2; y += range) {
  368. for (x = x1; x <= x2; x += range) {
  369. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
  370. if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  371. dmin = d;
  372. mx = x;
  373. my = y;
  374. }
  375. }
  376. }
  377. range = range >> 1;
  378. x1 = mx - range;
  379. if (x1 < xmin)
  380. x1 = xmin;
  381. x2 = mx + range;
  382. if (x2 > xmax)
  383. x2 = xmax;
  384. y1 = my - range;
  385. if (y1 < ymin)
  386. y1 = ymin;
  387. y2 = my + range;
  388. if (y2 > ymax)
  389. y2 = ymax;
  390. } while (range >= 1);
  391. #ifdef DEBUG
  392. fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
  393. #endif
  394. *mx_ptr = mx;
  395. *my_ptr = my;
  396. return dmin;
  397. }
  398. static int phods_motion_search(MpegEncContext * s,
  399. int *mx_ptr, int *my_ptr, int range,
  400. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  401. {
  402. int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
  403. int mx, my, dminx, dminy;
  404. uint8_t *pix;
  405. xx = s->mb_x << 4;
  406. yy = s->mb_y << 4;
  407. /* Left limit */
  408. x1 = xx - range;
  409. if (x1 < xmin)
  410. x1 = xmin;
  411. /* Right limit */
  412. x2 = xx + range;
  413. if (x2 > xmax)
  414. x2 = xmax;
  415. /* Upper limit */
  416. y1 = yy - range;
  417. if (y1 < ymin)
  418. y1 = ymin;
  419. /* Lower limit */
  420. y2 = yy + range;
  421. if (y2 > ymax)
  422. y2 = ymax;
  423. pix = s->new_picture.data[0] + (yy * s->linesize) + xx;
  424. mx = 0;
  425. my = 0;
  426. x = xx;
  427. y = yy;
  428. do {
  429. dminx = 0x7fffffff;
  430. dminy = 0x7fffffff;
  431. lastx = x;
  432. for (x = x1; x <= x2; x += range) {
  433. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
  434. if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  435. dminx = d;
  436. mx = x;
  437. }
  438. }
  439. x = lastx;
  440. for (y = y1; y <= y2; y += range) {
  441. d = s->dsp.pix_abs[0][0](NULL, pix, ref_picture + (y * s->linesize) + x, s->linesize, 16);
  442. if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  443. dminy = d;
  444. my = y;
  445. }
  446. }
  447. range = range >> 1;
  448. x = mx;
  449. y = my;
  450. x1 = mx - range;
  451. if (x1 < xmin)
  452. x1 = xmin;
  453. x2 = mx + range;
  454. if (x2 > xmax)
  455. x2 = xmax;
  456. y1 = my - range;
  457. if (y1 < ymin)
  458. y1 = ymin;
  459. y2 = my + range;
  460. if (y2 > ymax)
  461. y2 = ymax;
  462. } while (range >= 1);
  463. #ifdef DEBUG
  464. fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
  465. #endif
  466. /* half pixel search */
  467. *mx_ptr = mx;
  468. *my_ptr = my;
  469. return dminy;
  470. }
  471. #endif /* 0 */
  472. #define Z_THRESHOLD 256
  473. #define CHECK_SAD_HALF_MV(suffix, x, y) \
  474. {\
  475. d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
  476. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
  477. COPY3_IF_LT(dminh, d, dx, x, dy, y)\
  478. }
  479. static inline int sad_hpel_motion_search(MpegEncContext * s,
  480. int *mx_ptr, int *my_ptr, int dmin,
  481. int src_index, int ref_index,
  482. int size, int h)
  483. {
  484. MotionEstContext * const c= &s->me;
  485. const int penalty_factor= c->sub_penalty_factor;
  486. int mx, my, dminh;
  487. uint8_t *pix, *ptr;
  488. int stride= c->stride;
  489. const int flags= c->sub_flags;
  490. LOAD_COMMON
  491. assert(flags == 0);
  492. if(c->skip){
  493. // printf("S");
  494. *mx_ptr = 0;
  495. *my_ptr = 0;
  496. return dmin;
  497. }
  498. // printf("N");
  499. pix = c->src[src_index][0];
  500. mx = *mx_ptr;
  501. my = *my_ptr;
  502. ptr = c->ref[ref_index][0] + (my * stride) + mx;
  503. dminh = dmin;
  504. if (mx > xmin && mx < xmax &&
  505. my > ymin && my < ymax) {
  506. int dx=0, dy=0;
  507. int d, pen_x, pen_y;
  508. const int index= (my<<ME_MAP_SHIFT) + mx;
  509. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  510. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  511. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  512. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  513. mx<<=1;
  514. my<<=1;
  515. pen_x= pred_x + mx;
  516. pen_y= pred_y + my;
  517. ptr-= stride;
  518. if(t<=b){
  519. CHECK_SAD_HALF_MV(y2 , 0, -1)
  520. if(l<=r){
  521. CHECK_SAD_HALF_MV(xy2, -1, -1)
  522. if(t+r<=b+l){
  523. CHECK_SAD_HALF_MV(xy2, +1, -1)
  524. ptr+= stride;
  525. }else{
  526. ptr+= stride;
  527. CHECK_SAD_HALF_MV(xy2, -1, +1)
  528. }
  529. CHECK_SAD_HALF_MV(x2 , -1, 0)
  530. }else{
  531. CHECK_SAD_HALF_MV(xy2, +1, -1)
  532. if(t+l<=b+r){
  533. CHECK_SAD_HALF_MV(xy2, -1, -1)
  534. ptr+= stride;
  535. }else{
  536. ptr+= stride;
  537. CHECK_SAD_HALF_MV(xy2, +1, +1)
  538. }
  539. CHECK_SAD_HALF_MV(x2 , +1, 0)
  540. }
  541. }else{
  542. if(l<=r){
  543. if(t+l<=b+r){
  544. CHECK_SAD_HALF_MV(xy2, -1, -1)
  545. ptr+= stride;
  546. }else{
  547. ptr+= stride;
  548. CHECK_SAD_HALF_MV(xy2, +1, +1)
  549. }
  550. CHECK_SAD_HALF_MV(x2 , -1, 0)
  551. CHECK_SAD_HALF_MV(xy2, -1, +1)
  552. }else{
  553. if(t+r<=b+l){
  554. CHECK_SAD_HALF_MV(xy2, +1, -1)
  555. ptr+= stride;
  556. }else{
  557. ptr+= stride;
  558. CHECK_SAD_HALF_MV(xy2, -1, +1)
  559. }
  560. CHECK_SAD_HALF_MV(x2 , +1, 0)
  561. CHECK_SAD_HALF_MV(xy2, +1, +1)
  562. }
  563. CHECK_SAD_HALF_MV(y2 , 0, +1)
  564. }
  565. mx+=dx;
  566. my+=dy;
  567. }else{
  568. mx<<=1;
  569. my<<=1;
  570. }
  571. *mx_ptr = mx;
  572. *my_ptr = my;
  573. return dminh;
  574. }
  575. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  576. {
  577. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  578. s->p_mv_table[xy][0] = mx;
  579. s->p_mv_table[xy][1] = my;
  580. /* has already been set to the 4 MV if 4MV is done */
  581. if(mv4){
  582. int mot_xy= s->block_index[0];
  583. s->current_picture.motion_val[0][mot_xy ][0]= mx;
  584. s->current_picture.motion_val[0][mot_xy ][1]= my;
  585. s->current_picture.motion_val[0][mot_xy+1][0]= mx;
  586. s->current_picture.motion_val[0][mot_xy+1][1]= my;
  587. mot_xy += s->b8_stride;
  588. s->current_picture.motion_val[0][mot_xy ][0]= mx;
  589. s->current_picture.motion_val[0][mot_xy ][1]= my;
  590. s->current_picture.motion_val[0][mot_xy+1][0]= mx;
  591. s->current_picture.motion_val[0][mot_xy+1][1]= my;
  592. }
  593. }
  594. /**
  595. * get fullpel ME search limits.
  596. */
  597. static inline void get_limits(MpegEncContext *s, int x, int y)
  598. {
  599. MotionEstContext * const c= &s->me;
  600. /*
  601. if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
  602. else c->range= 16;
  603. */
  604. if (s->unrestricted_mv) {
  605. c->xmin = - x - 16;
  606. c->ymin = - y - 16;
  607. c->xmax = - x + s->mb_width *16;
  608. c->ymax = - y + s->mb_height*16;
  609. } else if (s->out_format == FMT_H261){
  610. // Search range of H261 is different from other codec standards
  611. c->xmin = (x > 15) ? - 15 : 0;
  612. c->ymin = (y > 15) ? - 15 : 0;
  613. c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
  614. c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
  615. } else {
  616. c->xmin = - x;
  617. c->ymin = - y;
  618. c->xmax = - x + s->mb_width *16 - 16;
  619. c->ymax = - y + s->mb_height*16 - 16;
  620. }
  621. }
  622. static inline void init_mv4_ref(MotionEstContext *c){
  623. const int stride= c->stride;
  624. c->ref[1][0] = c->ref[0][0] + 8;
  625. c->ref[2][0] = c->ref[0][0] + 8*stride;
  626. c->ref[3][0] = c->ref[2][0] + 8;
  627. c->src[1][0] = c->src[0][0] + 8;
  628. c->src[2][0] = c->src[0][0] + 8*stride;
  629. c->src[3][0] = c->src[2][0] + 8;
  630. }
  631. static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
  632. {
  633. MotionEstContext * const c= &s->me;
  634. const int size= 1;
  635. const int h=8;
  636. int block;
  637. int P[10][2];
  638. int dmin_sum=0, mx4_sum=0, my4_sum=0;
  639. int same=1;
  640. const int stride= c->stride;
  641. uint8_t *mv_penalty= c->current_mv_penalty;
  642. init_mv4_ref(c);
  643. for(block=0; block<4; block++){
  644. int mx4, my4;
  645. int pred_x4, pred_y4;
  646. int dmin4;
  647. static const int off[4]= {2, 1, 1, -1};
  648. const int mot_stride = s->b8_stride;
  649. const int mot_xy = s->block_index[block];
  650. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  651. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  652. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  653. /* special case for first line */
  654. if (s->first_slice_line && block<2) {
  655. c->pred_x= pred_x4= P_LEFT[0];
  656. c->pred_y= pred_y4= P_LEFT[1];
  657. } else {
  658. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  659. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  660. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][0];
  661. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + off[block]][1];
  662. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  663. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  664. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  665. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  666. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  667. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  668. c->pred_x= pred_x4 = P_MEDIAN[0];
  669. c->pred_y= pred_y4 = P_MEDIAN[1];
  670. }
  671. P_MV1[0]= mx;
  672. P_MV1[1]= my;
  673. dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
  674. dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
  675. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  676. int dxy;
  677. const int offset= ((block&1) + (block>>1)*stride)*8;
  678. uint8_t *dest_y = c->scratchpad + offset;
  679. if(s->quarter_sample){
  680. uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
  681. dxy = ((my4 & 3) << 2) | (mx4 & 3);
  682. if(s->no_rounding)
  683. s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride);
  684. else
  685. s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride);
  686. }else{
  687. uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
  688. dxy = ((my4 & 1) << 1) | (mx4 & 1);
  689. if(s->no_rounding)
  690. s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
  691. else
  692. s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
  693. }
  694. dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
  695. }else
  696. dmin_sum+= dmin4;
  697. if(s->quarter_sample){
  698. mx4_sum+= mx4/2;
  699. my4_sum+= my4/2;
  700. }else{
  701. mx4_sum+= mx4;
  702. my4_sum+= my4;
  703. }
  704. s->current_picture.motion_val[0][ s->block_index[block] ][0]= mx4;
  705. s->current_picture.motion_val[0][ s->block_index[block] ][1]= my4;
  706. if(mx4 != mx || my4 != my) same=0;
  707. }
  708. if(same)
  709. return INT_MAX;
  710. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  711. dmin_sum += s->dsp.mb_cmp[0](s, s->new_picture.data[0] + s->mb_x*16 + s->mb_y*16*stride, c->scratchpad, stride, 16);
  712. }
  713. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  714. int dxy;
  715. int mx, my;
  716. int offset;
  717. mx= ff_h263_round_chroma(mx4_sum);
  718. my= ff_h263_round_chroma(my4_sum);
  719. dxy = ((my & 1) << 1) | (mx & 1);
  720. offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
  721. if(s->no_rounding){
  722. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8);
  723. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8);
  724. }else{
  725. s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.data[1] + offset, s->uvlinesize, 8);
  726. s->dsp.put_pixels_tab [1][dxy](c->scratchpad+8 , s->last_picture.data[2] + offset, s->uvlinesize, 8);
  727. }
  728. dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[1] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad , s->uvlinesize, 8);
  729. dmin_sum += s->dsp.mb_cmp[1](s, s->new_picture.data[2] + s->mb_x*8 + s->mb_y*8*s->uvlinesize, c->scratchpad+8, s->uvlinesize, 8);
  730. }
  731. c->pred_x= mx;
  732. c->pred_y= my;
  733. switch(c->avctx->mb_cmp&0xFF){
  734. /*case FF_CMP_SSE:
  735. return dmin_sum+ 32*s->qscale*s->qscale;*/
  736. case FF_CMP_RD:
  737. return dmin_sum;
  738. default:
  739. return dmin_sum+ 11*c->mb_penalty_factor;
  740. }
  741. }
  742. static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
  743. MotionEstContext * const c= &s->me;
  744. c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
  745. c->src[1][0] = c->src[0][0] + s->linesize;
  746. if(c->flags & FLAG_CHROMA){
  747. c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
  748. c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
  749. c->src[1][1] = c->src[0][1] + s->uvlinesize;
  750. c->src[1][2] = c->src[0][2] + s->uvlinesize;
  751. }
  752. }
  753. static int interlaced_search(MpegEncContext *s, int ref_index,
  754. int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  755. {
  756. MotionEstContext * const c= &s->me;
  757. const int size=0;
  758. const int h=8;
  759. int block;
  760. int P[10][2];
  761. uint8_t * const mv_penalty= c->current_mv_penalty;
  762. int same=1;
  763. const int stride= 2*s->linesize;
  764. int dmin_sum= 0;
  765. const int mot_stride= s->mb_stride;
  766. const int xy= s->mb_x + s->mb_y*mot_stride;
  767. c->ymin>>=1;
  768. c->ymax>>=1;
  769. c->stride<<=1;
  770. c->uvstride<<=1;
  771. init_interlaced_ref(s, ref_index);
  772. for(block=0; block<2; block++){
  773. int field_select;
  774. int best_dmin= INT_MAX;
  775. int best_field= -1;
  776. for(field_select=0; field_select<2; field_select++){
  777. int dmin, mx_i, my_i;
  778. int16_t (*mv_table)[2]= mv_tables[block][field_select];
  779. if(user_field_select){
  780. if(field_select_tables[block][xy] != field_select)
  781. continue;
  782. }
  783. P_LEFT[0] = mv_table[xy - 1][0];
  784. P_LEFT[1] = mv_table[xy - 1][1];
  785. if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
  786. c->pred_x= P_LEFT[0];
  787. c->pred_y= P_LEFT[1];
  788. if(!s->first_slice_line){
  789. P_TOP[0] = mv_table[xy - mot_stride][0];
  790. P_TOP[1] = mv_table[xy - mot_stride][1];
  791. P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
  792. P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
  793. if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
  794. if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
  795. if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
  796. if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
  797. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  798. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  799. }
  800. P_MV1[0]= mx; //FIXME not correct if block != field_select
  801. P_MV1[1]= my / 2;
  802. dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
  803. dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
  804. mv_table[xy][0]= mx_i;
  805. mv_table[xy][1]= my_i;
  806. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  807. int dxy;
  808. //FIXME chroma ME
  809. uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
  810. dxy = ((my_i & 1) << 1) | (mx_i & 1);
  811. if(s->no_rounding){
  812. s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
  813. }else{
  814. s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
  815. }
  816. dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
  817. dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
  818. }else
  819. dmin+= c->mb_penalty_factor; //field_select bits
  820. dmin += field_select != block; //slightly prefer same field
  821. if(dmin < best_dmin){
  822. best_dmin= dmin;
  823. best_field= field_select;
  824. }
  825. }
  826. {
  827. int16_t (*mv_table)[2]= mv_tables[block][best_field];
  828. if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
  829. if(mv_table[xy][1]&1) same=0;
  830. if(mv_table[xy][1]*2 != my) same=0;
  831. if(best_field != block) same=0;
  832. }
  833. field_select_tables[block][xy]= best_field;
  834. dmin_sum += best_dmin;
  835. }
  836. c->ymin<<=1;
  837. c->ymax<<=1;
  838. c->stride>>=1;
  839. c->uvstride>>=1;
  840. if(same)
  841. return INT_MAX;
  842. switch(c->avctx->mb_cmp&0xFF){
  843. /*case FF_CMP_SSE:
  844. return dmin_sum+ 32*s->qscale*s->qscale;*/
  845. case FF_CMP_RD:
  846. return dmin_sum;
  847. default:
  848. return dmin_sum+ 11*c->mb_penalty_factor;
  849. }
  850. }
  851. static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  852. int ymax= s->me.ymax>>interlaced;
  853. int ymin= s->me.ymin>>interlaced;
  854. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  855. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  856. if(mv[1] < ymin) mv[1] = ymin;
  857. if(mv[1] > ymax) mv[1] = ymax;
  858. }
  859. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  860. MotionEstContext * const c= &s->me;
  861. Picture *p= s->current_picture_ptr;
  862. int mb_xy= mb_x + mb_y*s->mb_stride;
  863. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  864. int mb_type= s->current_picture.mb_type[mb_xy];
  865. int flags= c->flags;
  866. int shift= (flags&FLAG_QPEL) + 1;
  867. int mask= (1<<shift)-1;
  868. int x, y, i;
  869. int d=0;
  870. me_cmp_func cmpf= s->dsp.sse[0];
  871. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  872. if(p_type && USES_LIST(mb_type, 1)){
  873. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  874. return INT_MAX/2;
  875. }
  876. assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  877. for(i=0; i<4; i++){
  878. int xy= s->block_index[i];
  879. clip_input_mv(s, p->motion_val[0][xy], !!IS_INTERLACED(mb_type));
  880. clip_input_mv(s, p->motion_val[1][xy], !!IS_INTERLACED(mb_type));
  881. }
  882. if(IS_INTERLACED(mb_type)){
  883. int xy2= xy + s->b8_stride;
  884. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  885. c->stride<<=1;
  886. c->uvstride<<=1;
  887. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  888. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  889. return INT_MAX/2;
  890. }
  891. if(USES_LIST(mb_type, 0)){
  892. int field_select0= p->ref_index[0][xy ];
  893. int field_select1= p->ref_index[0][xy2];
  894. assert(field_select0==0 ||field_select0==1);
  895. assert(field_select1==0 ||field_select1==1);
  896. init_interlaced_ref(s, 0);
  897. if(p_type){
  898. s->p_field_select_table[0][mb_xy]= field_select0;
  899. s->p_field_select_table[1][mb_xy]= field_select1;
  900. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
  901. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
  902. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  903. }else{
  904. s->b_field_select_table[0][0][mb_xy]= field_select0;
  905. s->b_field_select_table[0][1][mb_xy]= field_select1;
  906. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[0][xy ];
  907. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[0][xy2];
  908. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  909. }
  910. x= p->motion_val[0][xy ][0];
  911. y= p->motion_val[0][xy ][1];
  912. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  913. x= p->motion_val[0][xy2][0];
  914. y= p->motion_val[0][xy2][1];
  915. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  916. }
  917. if(USES_LIST(mb_type, 1)){
  918. int field_select0= p->ref_index[1][xy ];
  919. int field_select1= p->ref_index[1][xy2];
  920. assert(field_select0==0 ||field_select0==1);
  921. assert(field_select1==0 ||field_select1==1);
  922. init_interlaced_ref(s, 2);
  923. s->b_field_select_table[1][0][mb_xy]= field_select0;
  924. s->b_field_select_table[1][1][mb_xy]= field_select1;
  925. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy]= *(uint32_t*)p->motion_val[1][xy ];
  926. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy]= *(uint32_t*)p->motion_val[1][xy2];
  927. if(USES_LIST(mb_type, 0)){
  928. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  929. }else{
  930. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  931. }
  932. x= p->motion_val[1][xy ][0];
  933. y= p->motion_val[1][xy ][1];
  934. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  935. x= p->motion_val[1][xy2][0];
  936. y= p->motion_val[1][xy2][1];
  937. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  938. //FIXME bidir scores
  939. }
  940. c->stride>>=1;
  941. c->uvstride>>=1;
  942. }else if(IS_8X8(mb_type)){
  943. if(!(s->flags & CODEC_FLAG_4MV)){
  944. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  945. return INT_MAX/2;
  946. }
  947. cmpf= s->dsp.sse[1];
  948. chroma_cmpf= s->dsp.sse[1];
  949. init_mv4_ref(c);
  950. for(i=0; i<4; i++){
  951. xy= s->block_index[i];
  952. x= p->motion_val[0][xy][0];
  953. y= p->motion_val[0][xy][1];
  954. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  955. }
  956. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  957. }else{
  958. if(USES_LIST(mb_type, 0)){
  959. if(p_type){
  960. *(uint32_t*)s->p_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  961. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  962. }else if(USES_LIST(mb_type, 1)){
  963. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  964. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
  965. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  966. }else{
  967. *(uint32_t*)s->b_forw_mv_table[mb_xy]= *(uint32_t*)p->motion_val[0][xy];
  968. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  969. }
  970. x= p->motion_val[0][xy][0];
  971. y= p->motion_val[0][xy][1];
  972. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  973. }else if(USES_LIST(mb_type, 1)){
  974. *(uint32_t*)s->b_back_mv_table[mb_xy]= *(uint32_t*)p->motion_val[1][xy];
  975. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  976. x= p->motion_val[1][xy][0];
  977. y= p->motion_val[1][xy][1];
  978. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  979. }else
  980. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  981. }
  982. return d;
  983. }
  984. void ff_estimate_p_frame_motion(MpegEncContext * s,
  985. int mb_x, int mb_y)
  986. {
  987. MotionEstContext * const c= &s->me;
  988. uint8_t *pix, *ppix;
  989. int sum, varc, vard, mx, my, dmin;
  990. int P[10][2];
  991. const int shift= 1+s->quarter_sample;
  992. int mb_type=0;
  993. Picture * const pic= &s->current_picture;
  994. init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
  995. assert(s->quarter_sample==0 || s->quarter_sample==1);
  996. assert(s->linesize == c->stride);
  997. assert(s->uvlinesize == c->uvstride);
  998. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  999. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1000. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1001. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1002. get_limits(s, 16*mb_x, 16*mb_y);
  1003. c->skip=0;
  1004. /* intra / predictive decision */
  1005. pix = c->src[0][0];
  1006. sum = s->dsp.pix_sum(pix, s->linesize);
  1007. varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  1008. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1009. pic->mb_var [s->mb_stride * mb_y + mb_x] = varc;
  1010. c->mb_var_sum_temp += varc;
  1011. if(c->avctx->me_threshold){
  1012. vard= (check_input_motion(s, mb_x, mb_y, 1)+128)>>8;
  1013. if(vard<c->avctx->me_threshold){
  1014. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
  1015. c->mc_mb_var_sum_temp += vard;
  1016. if (vard <= 64 || vard < varc) { //FIXME
  1017. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1018. }else{
  1019. c->scene_change_score+= s->qscale;
  1020. }
  1021. return;
  1022. }
  1023. if(vard<c->avctx->mb_threshold)
  1024. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  1025. }
  1026. switch(s->me_method) {
  1027. case ME_ZERO:
  1028. default:
  1029. no_motion_search(s, &mx, &my);
  1030. mx-= mb_x*16;
  1031. my-= mb_y*16;
  1032. dmin = 0;
  1033. break;
  1034. #if 0
  1035. case ME_FULL:
  1036. dmin = full_motion_search(s, &mx, &my, range, ref_picture);
  1037. mx-= mb_x*16;
  1038. my-= mb_y*16;
  1039. break;
  1040. case ME_LOG:
  1041. dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
  1042. mx-= mb_x*16;
  1043. my-= mb_y*16;
  1044. break;
  1045. case ME_PHODS:
  1046. dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
  1047. mx-= mb_x*16;
  1048. my-= mb_y*16;
  1049. break;
  1050. #endif
  1051. case ME_X1:
  1052. case ME_EPZS:
  1053. {
  1054. const int mot_stride = s->b8_stride;
  1055. const int mot_xy = s->block_index[0];
  1056. P_LEFT[0] = s->current_picture.motion_val[0][mot_xy - 1][0];
  1057. P_LEFT[1] = s->current_picture.motion_val[0][mot_xy - 1][1];
  1058. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1059. if(!s->first_slice_line) {
  1060. P_TOP[0] = s->current_picture.motion_val[0][mot_xy - mot_stride ][0];
  1061. P_TOP[1] = s->current_picture.motion_val[0][mot_xy - mot_stride ][1];
  1062. P_TOPRIGHT[0] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][0];
  1063. P_TOPRIGHT[1] = s->current_picture.motion_val[0][mot_xy - mot_stride + 2][1];
  1064. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  1065. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1066. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1067. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1068. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1069. if(s->out_format == FMT_H263){
  1070. c->pred_x = P_MEDIAN[0];
  1071. c->pred_y = P_MEDIAN[1];
  1072. }else { /* mpeg1 at least */
  1073. c->pred_x= P_LEFT[0];
  1074. c->pred_y= P_LEFT[1];
  1075. }
  1076. }else{
  1077. c->pred_x= P_LEFT[0];
  1078. c->pred_y= P_LEFT[1];
  1079. }
  1080. }
  1081. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1082. break;
  1083. }
  1084. /* At this point (mx,my) are full-pell and the relative displacement */
  1085. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  1086. vard = (s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16)+128)>>8;
  1087. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
  1088. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  1089. c->mc_mb_var_sum_temp += vard;
  1090. #if 0
  1091. printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
  1092. varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
  1093. #endif
  1094. if(mb_type){
  1095. if (vard <= 64 || vard < varc)
  1096. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1097. else
  1098. c->scene_change_score+= s->qscale;
  1099. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  1100. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1101. set_p_mv_tables(s, mx, my, 1);
  1102. }else{
  1103. mx <<=shift;
  1104. my <<=shift;
  1105. }
  1106. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  1107. h263_mv4_search(s, mx, my, shift);
  1108. set_p_mv_tables(s, mx, my, 0);
  1109. }
  1110. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  1111. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  1112. }
  1113. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1114. if (vard <= 64 || vard < varc)
  1115. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1116. else
  1117. c->scene_change_score+= s->qscale;
  1118. if (vard*2 + 200 > varc)
  1119. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  1120. if (varc*2 + 200 > vard){
  1121. mb_type|= CANDIDATE_MB_TYPE_INTER;
  1122. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1123. if(s->flags&CODEC_FLAG_MV0)
  1124. if(mx || my)
  1125. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  1126. }else{
  1127. mx <<=shift;
  1128. my <<=shift;
  1129. }
  1130. if((s->flags&CODEC_FLAG_4MV)
  1131. && !c->skip && varc>50 && vard>10){
  1132. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  1133. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  1134. set_p_mv_tables(s, mx, my, 0);
  1135. }else
  1136. set_p_mv_tables(s, mx, my, 1);
  1137. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1138. && !c->skip){ //FIXME varc/d checks
  1139. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  1140. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  1141. }
  1142. }else{
  1143. int intra_score, i;
  1144. mb_type= CANDIDATE_MB_TYPE_INTER;
  1145. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1146. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1147. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1148. if((s->flags&CODEC_FLAG_4MV)
  1149. && !c->skip && varc>50 && vard>10){
  1150. int dmin4= h263_mv4_search(s, mx, my, shift);
  1151. if(dmin4 < dmin){
  1152. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  1153. dmin=dmin4;
  1154. }
  1155. }
  1156. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1157. && !c->skip){ //FIXME varc/d checks
  1158. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  1159. if(dmin_i < dmin){
  1160. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  1161. dmin= dmin_i;
  1162. }
  1163. }
  1164. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  1165. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  1166. /* get intra luma score */
  1167. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  1168. intra_score= (varc<<8) - 500; //FIXME dont scale it down so we dont have to fix it
  1169. }else{
  1170. int mean= (sum+128)>>8;
  1171. mean*= 0x01010101;
  1172. for(i=0; i<16; i++){
  1173. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1174. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1175. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1176. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1177. }
  1178. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1179. }
  1180. #if 0 //FIXME
  1181. /* get chroma score */
  1182. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1183. for(i=1; i<3; i++){
  1184. uint8_t *dest_c;
  1185. int mean;
  1186. if(s->out_format == FMT_H263){
  1187. mean= (s->dc_val[i][mb_x + mb_y*s->b8_stride] + 4)>>3; //FIXME not exact but simple ;)
  1188. }else{
  1189. mean= (s->last_dc[i] + 4)>>3;
  1190. }
  1191. dest_c = s->new_picture.data[i] + (mb_y * 8 * (s->uvlinesize)) + mb_x * 8;
  1192. mean*= 0x01010101;
  1193. for(i=0; i<8; i++){
  1194. *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 0]) = mean;
  1195. *(uint32_t*)(&c->scratchpad[i*s->uvlinesize+ 4]) = mean;
  1196. }
  1197. intra_score+= s->dsp.mb_cmp[1](s, c->scratchpad, dest_c, s->uvlinesize);
  1198. }
  1199. }
  1200. #endif
  1201. intra_score += c->mb_penalty_factor*16;
  1202. if(intra_score < dmin){
  1203. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1204. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1205. }else
  1206. s->current_picture.mb_type[mb_y*s->mb_stride + mb_x]= 0;
  1207. if (vard <= 64 || vard < varc) { //FIXME
  1208. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1209. }else{
  1210. c->scene_change_score+= s->qscale;
  1211. }
  1212. }
  1213. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1214. }
  1215. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1216. int mb_x, int mb_y)
  1217. {
  1218. MotionEstContext * const c= &s->me;
  1219. int mx, my, dmin;
  1220. int P[10][2];
  1221. const int shift= 1+s->quarter_sample;
  1222. const int xy= mb_x + mb_y*s->mb_stride;
  1223. init_ref(c, s->new_picture.data, s->last_picture.data, NULL, 16*mb_x, 16*mb_y, 0);
  1224. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1225. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1226. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1227. get_limits(s, 16*mb_x, 16*mb_y);
  1228. c->skip=0;
  1229. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1230. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1231. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1232. /* special case for first line */
  1233. if (s->first_slice_line) {
  1234. c->pred_x= P_LEFT[0];
  1235. c->pred_y= P_LEFT[1];
  1236. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1237. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1238. } else {
  1239. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1240. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1241. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1242. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1243. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1244. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1245. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1246. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1247. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1248. c->pred_x = P_MEDIAN[0];
  1249. c->pred_y = P_MEDIAN[1];
  1250. }
  1251. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1252. s->p_mv_table[xy][0] = mx<<shift;
  1253. s->p_mv_table[xy][1] = my<<shift;
  1254. return dmin;
  1255. }
  1256. static int ff_estimate_motion_b(MpegEncContext * s,
  1257. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1258. {
  1259. MotionEstContext * const c= &s->me;
  1260. int mx, my, dmin;
  1261. int P[10][2];
  1262. const int shift= 1+s->quarter_sample;
  1263. const int mot_stride = s->mb_stride;
  1264. const int mot_xy = mb_y*mot_stride + mb_x;
  1265. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1266. int mv_scale;
  1267. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1268. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1269. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1270. c->current_mv_penalty= mv_penalty;
  1271. get_limits(s, 16*mb_x, 16*mb_y);
  1272. switch(s->me_method) {
  1273. case ME_ZERO:
  1274. default:
  1275. no_motion_search(s, &mx, &my);
  1276. dmin = 0;
  1277. mx-= mb_x*16;
  1278. my-= mb_y*16;
  1279. break;
  1280. #if 0
  1281. case ME_FULL:
  1282. dmin = full_motion_search(s, &mx, &my, range, ref_picture);
  1283. mx-= mb_x*16;
  1284. my-= mb_y*16;
  1285. break;
  1286. case ME_LOG:
  1287. dmin = log_motion_search(s, &mx, &my, range / 2, ref_picture);
  1288. mx-= mb_x*16;
  1289. my-= mb_y*16;
  1290. break;
  1291. case ME_PHODS:
  1292. dmin = phods_motion_search(s, &mx, &my, range / 2, ref_picture);
  1293. mx-= mb_x*16;
  1294. my-= mb_y*16;
  1295. break;
  1296. #endif
  1297. case ME_X1:
  1298. case ME_EPZS:
  1299. {
  1300. P_LEFT[0] = mv_table[mot_xy - 1][0];
  1301. P_LEFT[1] = mv_table[mot_xy - 1][1];
  1302. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  1303. /* special case for first line */
  1304. if (!s->first_slice_line) {
  1305. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  1306. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  1307. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
  1308. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
  1309. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1]= (c->ymax<<shift);
  1310. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  1311. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  1312. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1313. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1314. }
  1315. c->pred_x= P_LEFT[0];
  1316. c->pred_y= P_LEFT[1];
  1317. }
  1318. if(mv_table == s->b_forw_mv_table){
  1319. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1320. }else{
  1321. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1322. }
  1323. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1324. break;
  1325. }
  1326. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1327. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1328. dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1329. //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
  1330. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1331. mv_table[mot_xy][0]= mx;
  1332. mv_table[mot_xy][1]= my;
  1333. return dmin;
  1334. }
  1335. static inline int check_bidir_mv(MpegEncContext * s,
  1336. int motion_fx, int motion_fy,
  1337. int motion_bx, int motion_by,
  1338. int pred_fx, int pred_fy,
  1339. int pred_bx, int pred_by,
  1340. int size, int h)
  1341. {
  1342. //FIXME optimize?
  1343. //FIXME better f_code prediction (max mv & distance)
  1344. //FIXME pointers
  1345. MotionEstContext * const c= &s->me;
  1346. uint8_t * const mv_penalty= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1347. int stride= c->stride;
  1348. int uvstride= c->uvstride;
  1349. uint8_t *dest_y = c->scratchpad;
  1350. uint8_t *ptr;
  1351. int dxy;
  1352. int src_x, src_y;
  1353. int fbmin;
  1354. uint8_t **src_data= c->src[0];
  1355. uint8_t **ref_data= c->ref[0];
  1356. uint8_t **ref2_data= c->ref[2];
  1357. if(s->quarter_sample){
  1358. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1359. src_x = motion_fx >> 2;
  1360. src_y = motion_fy >> 2;
  1361. ptr = ref_data[0] + (src_y * stride) + src_x;
  1362. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
  1363. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1364. src_x = motion_bx >> 2;
  1365. src_y = motion_by >> 2;
  1366. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1367. s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
  1368. }else{
  1369. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1370. src_x = motion_fx >> 1;
  1371. src_y = motion_fy >> 1;
  1372. ptr = ref_data[0] + (src_y * stride) + src_x;
  1373. s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1374. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1375. src_x = motion_bx >> 1;
  1376. src_y = motion_by >> 1;
  1377. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1378. s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1379. }
  1380. fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*c->mb_penalty_factor
  1381. +(mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*c->mb_penalty_factor
  1382. + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
  1383. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1384. }
  1385. //FIXME CHROMA !!!
  1386. return fbmin;
  1387. }
  1388. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1389. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1390. {
  1391. const int mot_stride = s->mb_stride;
  1392. const int xy = mb_y *mot_stride + mb_x;
  1393. int fbmin;
  1394. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1395. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1396. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1397. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1398. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1399. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1400. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1401. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1402. //FIXME do refinement and add flag
  1403. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1404. motion_bx, motion_by,
  1405. pred_fx, pred_fy,
  1406. pred_bx, pred_by,
  1407. 0, 16);
  1408. return fbmin;
  1409. }
  1410. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1411. {
  1412. MotionEstContext * const c= &s->me;
  1413. int P[10][2];
  1414. const int mot_stride = s->mb_stride;
  1415. const int mot_xy = mb_y*mot_stride + mb_x;
  1416. const int shift= 1+s->quarter_sample;
  1417. int dmin, i;
  1418. const int time_pp= s->pp_time;
  1419. const int time_pb= s->pb_time;
  1420. int mx, my, xmin, xmax, ymin, ymax;
  1421. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1422. c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
  1423. ymin= xmin=(-32)>>shift;
  1424. ymax= xmax= 31>>shift;
  1425. if(IS_8X8(s->next_picture.mb_type[mot_xy])){
  1426. s->mv_type= MV_TYPE_8X8;
  1427. }else{
  1428. s->mv_type= MV_TYPE_16X16;
  1429. }
  1430. for(i=0; i<4; i++){
  1431. int index= s->block_index[i];
  1432. int min, max;
  1433. c->co_located_mv[i][0]= s->next_picture.motion_val[0][index][0];
  1434. c->co_located_mv[i][1]= s->next_picture.motion_val[0][index][1];
  1435. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1436. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1437. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1438. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1439. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1440. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1441. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1442. min+= 16*mb_x - 1;
  1443. xmax= FFMIN(xmax, s->width - max);
  1444. xmin= FFMAX(xmin, - 16 - min);
  1445. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1446. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1447. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1448. min+= 16*mb_y - 1;
  1449. ymax= FFMIN(ymax, s->height - max);
  1450. ymin= FFMAX(ymin, - 16 - min);
  1451. if(s->mv_type == MV_TYPE_16X16) break;
  1452. }
  1453. assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1454. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1455. s->b_direct_mv_table[mot_xy][0]= 0;
  1456. s->b_direct_mv_table[mot_xy][1]= 0;
  1457. return 256*256*256*64;
  1458. }
  1459. c->xmin= xmin;
  1460. c->ymin= ymin;
  1461. c->xmax= xmax;
  1462. c->ymax= ymax;
  1463. c->flags |= FLAG_DIRECT;
  1464. c->sub_flags |= FLAG_DIRECT;
  1465. c->pred_x=0;
  1466. c->pred_y=0;
  1467. P_LEFT[0] = clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1468. P_LEFT[1] = clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1469. /* special case for first line */
  1470. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as its cliped
  1471. P_TOP[0] = clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1472. P_TOP[1] = clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1473. P_TOPRIGHT[0] = clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1474. P_TOPRIGHT[1] = clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1475. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1476. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1477. }
  1478. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1479. if(c->sub_flags&FLAG_QPEL)
  1480. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1481. else
  1482. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1483. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1484. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1485. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1486. s->b_direct_mv_table[mot_xy][0]= mx;
  1487. s->b_direct_mv_table[mot_xy][1]= my;
  1488. c->flags &= ~FLAG_DIRECT;
  1489. c->sub_flags &= ~FLAG_DIRECT;
  1490. return dmin;
  1491. }
  1492. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1493. int mb_x, int mb_y)
  1494. {
  1495. MotionEstContext * const c= &s->me;
  1496. const int penalty_factor= c->mb_penalty_factor;
  1497. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1498. int type=0;
  1499. const int xy = mb_y*s->mb_stride + mb_x;
  1500. init_ref(c, s->new_picture.data, s->last_picture.data, s->next_picture.data, 16*mb_x, 16*mb_y, 2);
  1501. get_limits(s, 16*mb_x, 16*mb_y);
  1502. c->skip=0;
  1503. if(c->avctx->me_threshold){
  1504. int vard= (check_input_motion(s, mb_x, mb_y, 0)+128)>>8;
  1505. if(vard<c->avctx->me_threshold){
  1506. // pix = c->src[0][0];
  1507. // sum = s->dsp.pix_sum(pix, s->linesize);
  1508. // varc = (s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500 + 128)>>8;
  1509. // pic->mb_var [s->mb_stride * mb_y + mb_x] = varc;
  1510. s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = vard;
  1511. /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1512. c->mb_var_sum_temp += varc;*/
  1513. c->mc_mb_var_sum_temp += vard;
  1514. /* if (vard <= 64 || vard < varc) {
  1515. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1516. }else{
  1517. c->scene_change_score+= s->qscale;
  1518. }*/
  1519. return;
  1520. }
  1521. if(vard<c->avctx->mb_threshold){
  1522. type= s->mb_type[mb_y*s->mb_stride + mb_x];
  1523. if(type == CANDIDATE_MB_TYPE_DIRECT){
  1524. direct_search(s, mb_x, mb_y);
  1525. }
  1526. if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1527. c->skip=0;
  1528. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
  1529. }
  1530. if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1531. c->skip=0;
  1532. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
  1533. }
  1534. if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1535. c->skip=0;
  1536. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1537. interlaced_search(s, 0,
  1538. s->b_field_mv_table[0], s->b_field_select_table[0],
  1539. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
  1540. }
  1541. if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1542. c->skip=0;
  1543. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1544. interlaced_search(s, 2,
  1545. s->b_field_mv_table[1], s->b_field_select_table[1],
  1546. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
  1547. }
  1548. return;
  1549. }
  1550. }
  1551. if (s->codec_id == CODEC_ID_MPEG4)
  1552. dmin= direct_search(s, mb_x, mb_y);
  1553. else
  1554. dmin= INT_MAX;
  1555. //FIXME penalty stuff for non mpeg4
  1556. c->skip=0;
  1557. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
  1558. c->skip=0;
  1559. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
  1560. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1561. c->skip=0;
  1562. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1563. //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1564. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1565. //FIXME mb type penalty
  1566. c->skip=0;
  1567. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1568. fimin= interlaced_search(s, 0,
  1569. s->b_field_mv_table[0], s->b_field_select_table[0],
  1570. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1571. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1572. bimin= interlaced_search(s, 2,
  1573. s->b_field_mv_table[1], s->b_field_select_table[1],
  1574. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1575. }else
  1576. fimin= bimin= INT_MAX;
  1577. {
  1578. int score= fmin;
  1579. type = CANDIDATE_MB_TYPE_FORWARD;
  1580. if (dmin <= score){
  1581. score = dmin;
  1582. type = CANDIDATE_MB_TYPE_DIRECT;
  1583. }
  1584. if(bmin<score){
  1585. score=bmin;
  1586. type= CANDIDATE_MB_TYPE_BACKWARD;
  1587. }
  1588. if(fbmin<score){
  1589. score=fbmin;
  1590. type= CANDIDATE_MB_TYPE_BIDIR;
  1591. }
  1592. if(fimin<score){
  1593. score=fimin;
  1594. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1595. }
  1596. if(bimin<score){
  1597. score=bimin;
  1598. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1599. }
  1600. score= ((unsigned)(score*score + 128*256))>>16;
  1601. c->mc_mb_var_sum_temp += score;
  1602. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1603. }
  1604. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1605. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1606. if(fimin < INT_MAX)
  1607. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1608. if(bimin < INT_MAX)
  1609. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1610. if(fimin < INT_MAX && bimin < INT_MAX){
  1611. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1612. }
  1613. //FIXME something smarter
  1614. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //dont try direct mode if its invalid for this MB
  1615. #if 0
  1616. if(s->out_format == FMT_MPEG1)
  1617. type |= CANDIDATE_MB_TYPE_INTRA;
  1618. #endif
  1619. }
  1620. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1621. }
  1622. /* find best f_code for ME which do unlimited searches */
  1623. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1624. {
  1625. if(s->me_method>=ME_EPZS){
  1626. int score[8];
  1627. int i, y, range= s->avctx->me_range;
  1628. uint8_t * fcode_tab= s->fcode_tab;
  1629. int best_fcode=-1;
  1630. int best_score=-10000000;
  1631. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1632. for(y=0; y<s->mb_height; y++){
  1633. int x;
  1634. int xy= y*s->mb_stride;
  1635. for(x=0; x<s->mb_width; x++){
  1636. if(s->mb_type[xy] & type){
  1637. int mx= mv_table[xy][0];
  1638. int my= mv_table[xy][1];
  1639. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1640. fcode_tab[my + MAX_MV]);
  1641. int j;
  1642. if(range){
  1643. if(mx >= range || mx < -range ||
  1644. my >= range || my < -range)
  1645. continue;
  1646. }
  1647. for(j=0; j<fcode && j<8; j++){
  1648. if(s->pict_type==B_TYPE || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1649. score[j]-= 170;
  1650. }
  1651. }
  1652. xy++;
  1653. }
  1654. }
  1655. for(i=1; i<8; i++){
  1656. if(score[i] > best_score){
  1657. best_score= score[i];
  1658. best_fcode= i;
  1659. }
  1660. // printf("%d %d\n", i, score[i]);
  1661. }
  1662. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1663. return best_fcode;
  1664. /* for(i=0; i<=MAX_FCODE; i++){
  1665. printf("%d ", mv_num[i]);
  1666. }
  1667. printf("\n");*/
  1668. }else{
  1669. return 1;
  1670. }
  1671. }
  1672. void ff_fix_long_p_mvs(MpegEncContext * s)
  1673. {
  1674. MotionEstContext * const c= &s->me;
  1675. const int f_code= s->f_code;
  1676. int y, range;
  1677. assert(s->pict_type==P_TYPE);
  1678. range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
  1679. if(s->msmpeg4_version) range= 16;
  1680. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1681. //printf("%d no:%d %d//\n", clip, noclip, f_code);
  1682. if(s->flags&CODEC_FLAG_4MV){
  1683. const int wrap= s->b8_stride;
  1684. /* clip / convert to intra 8x8 type MVs */
  1685. for(y=0; y<s->mb_height; y++){
  1686. int xy= y*2*wrap;
  1687. int i= y*s->mb_stride;
  1688. int x;
  1689. for(x=0; x<s->mb_width; x++){
  1690. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1691. int block;
  1692. for(block=0; block<4; block++){
  1693. int off= (block& 1) + (block>>1)*wrap;
  1694. int mx= s->current_picture.motion_val[0][ xy + off ][0];
  1695. int my= s->current_picture.motion_val[0][ xy + off ][1];
  1696. if( mx >=range || mx <-range
  1697. || my >=range || my <-range){
  1698. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1699. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1700. s->current_picture.mb_type[i]= CANDIDATE_MB_TYPE_INTRA;
  1701. }
  1702. }
  1703. }
  1704. xy+=2;
  1705. i++;
  1706. }
  1707. }
  1708. }
  1709. }
  1710. /**
  1711. *
  1712. * @param truncate 1 for truncation, 0 for using intra
  1713. */
  1714. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1715. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1716. {
  1717. MotionEstContext * const c= &s->me;
  1718. int y, h_range, v_range;
  1719. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1720. int range = (((s->out_format == FMT_MPEG1) ? 8 : 16) << f_code);
  1721. if(s->msmpeg4_version) range= 16;
  1722. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1723. h_range= range;
  1724. v_range= field_select_table ? range>>1 : range;
  1725. /* clip / convert to intra 16x16 type MVs */
  1726. for(y=0; y<s->mb_height; y++){
  1727. int x;
  1728. int xy= y*s->mb_stride;
  1729. for(x=0; x<s->mb_width; x++){
  1730. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1731. if(field_select_table==NULL || field_select_table[xy] == field_select){
  1732. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1733. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1734. if(truncate){
  1735. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1736. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1737. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1738. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1739. }else{
  1740. s->mb_type[xy] &= ~type;
  1741. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1742. mv_table[xy][0]=
  1743. mv_table[xy][1]= 0;
  1744. }
  1745. }
  1746. }
  1747. }
  1748. xy++;
  1749. }
  1750. }
  1751. }