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.

1760 lines
67KB

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