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.

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