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.

1903 lines
71KB

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