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.

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