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.

1753 lines
67KB

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