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.

1766 lines
68KB

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