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.

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