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.

1712 lines
66KB

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