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.

1900 lines
70KB

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