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.

1972 lines
77KB

  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.f.motion_val[0][mot_xy ][0] = mx;
  444. s->current_picture.f.motion_val[0][mot_xy ][1] = my;
  445. s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
  446. s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
  447. mot_xy += s->b8_stride;
  448. s->current_picture.f.motion_val[0][mot_xy ][0] = mx;
  449. s->current_picture.f.motion_val[0][mot_xy ][1] = my;
  450. s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
  451. s->current_picture.f.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.f.motion_val[0][mot_xy - 1][0];
  526. P_LEFT[1] = s->current_picture.f.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.f.motion_val[0][mot_xy - mot_stride ][0];
  534. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  535. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
  536. P_TOPRIGHT[1] = s->current_picture.f.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.f.motion_val[0][s->block_index[block]][0] = mx4;
  585. s->current_picture.f.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 void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  734. int ymax= s->me.ymax>>interlaced;
  735. int ymin= s->me.ymin>>interlaced;
  736. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  737. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  738. if(mv[1] < ymin) mv[1] = ymin;
  739. if(mv[1] > ymax) mv[1] = ymax;
  740. }
  741. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  742. MotionEstContext * const c= &s->me;
  743. Picture *p= s->current_picture_ptr;
  744. int mb_xy= mb_x + mb_y*s->mb_stride;
  745. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  746. int mb_type= s->current_picture.f.mb_type[mb_xy];
  747. int flags= c->flags;
  748. int shift= (flags&FLAG_QPEL) + 1;
  749. int mask= (1<<shift)-1;
  750. int x, y, i;
  751. int d=0;
  752. me_cmp_func cmpf= s->dsp.sse[0];
  753. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  754. if(p_type && USES_LIST(mb_type, 1)){
  755. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  756. return INT_MAX/2;
  757. }
  758. av_assert0(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  759. for(i=0; i<4; i++){
  760. int xy= s->block_index[i];
  761. clip_input_mv(s, p->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
  762. clip_input_mv(s, p->f.motion_val[1][xy], !!IS_INTERLACED(mb_type));
  763. }
  764. if(IS_INTERLACED(mb_type)){
  765. int xy2= xy + s->b8_stride;
  766. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  767. c->stride<<=1;
  768. c->uvstride<<=1;
  769. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  770. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  771. return INT_MAX/2;
  772. }
  773. if(USES_LIST(mb_type, 0)){
  774. int field_select0= p->f.ref_index[0][4*mb_xy ];
  775. int field_select1= p->f.ref_index[0][4*mb_xy+2];
  776. av_assert0(field_select0==0 ||field_select0==1);
  777. av_assert0(field_select1==0 ||field_select1==1);
  778. init_interlaced_ref(s, 0);
  779. if(p_type){
  780. s->p_field_select_table[0][mb_xy]= field_select0;
  781. s->p_field_select_table[1][mb_xy]= field_select1;
  782. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  783. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  784. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  785. }else{
  786. s->b_field_select_table[0][0][mb_xy]= field_select0;
  787. s->b_field_select_table[0][1][mb_xy]= field_select1;
  788. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  789. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  790. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  791. }
  792. x = p->f.motion_val[0][xy ][0];
  793. y = p->f.motion_val[0][xy ][1];
  794. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  795. x = p->f.motion_val[0][xy2][0];
  796. y = p->f.motion_val[0][xy2][1];
  797. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  798. }
  799. if(USES_LIST(mb_type, 1)){
  800. int field_select0 = p->f.ref_index[1][4 * mb_xy ];
  801. int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
  802. av_assert0(field_select0==0 ||field_select0==1);
  803. av_assert0(field_select1==0 ||field_select1==1);
  804. init_interlaced_ref(s, 2);
  805. s->b_field_select_table[1][0][mb_xy]= field_select0;
  806. s->b_field_select_table[1][1][mb_xy]= field_select1;
  807. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy ];
  808. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy2];
  809. if(USES_LIST(mb_type, 0)){
  810. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  811. }else{
  812. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  813. }
  814. x = p->f.motion_val[1][xy ][0];
  815. y = p->f.motion_val[1][xy ][1];
  816. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  817. x = p->f.motion_val[1][xy2][0];
  818. y = p->f.motion_val[1][xy2][1];
  819. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  820. //FIXME bidir scores
  821. }
  822. c->stride>>=1;
  823. c->uvstride>>=1;
  824. }else if(IS_8X8(mb_type)){
  825. if(!(s->flags & CODEC_FLAG_4MV)){
  826. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  827. return INT_MAX/2;
  828. }
  829. cmpf= s->dsp.sse[1];
  830. chroma_cmpf= s->dsp.sse[1];
  831. init_mv4_ref(c);
  832. for(i=0; i<4; i++){
  833. xy= s->block_index[i];
  834. x= p->f.motion_val[0][xy][0];
  835. y= p->f.motion_val[0][xy][1];
  836. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  837. }
  838. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  839. }else{
  840. if(USES_LIST(mb_type, 0)){
  841. if(p_type){
  842. *(uint32_t*)s->p_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  843. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  844. }else if(USES_LIST(mb_type, 1)){
  845. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  846. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  847. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  848. }else{
  849. *(uint32_t*)s->b_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  850. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  851. }
  852. x = p->f.motion_val[0][xy][0];
  853. y = p->f.motion_val[0][xy][1];
  854. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  855. }else if(USES_LIST(mb_type, 1)){
  856. *(uint32_t*)s->b_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  857. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  858. x = p->f.motion_val[1][xy][0];
  859. y = p->f.motion_val[1][xy][1];
  860. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  861. }else
  862. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  863. }
  864. return d;
  865. }
  866. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  867. switch(type&0xFF){
  868. default:
  869. case FF_CMP_SAD:
  870. return lambda>>FF_LAMBDA_SHIFT;
  871. case FF_CMP_DCT:
  872. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  873. case FF_CMP_W53:
  874. return (4*lambda)>>(FF_LAMBDA_SHIFT);
  875. case FF_CMP_W97:
  876. return (2*lambda)>>(FF_LAMBDA_SHIFT);
  877. case FF_CMP_SATD:
  878. case FF_CMP_DCT264:
  879. return (2*lambda)>>FF_LAMBDA_SHIFT;
  880. case FF_CMP_RD:
  881. case FF_CMP_PSNR:
  882. case FF_CMP_SSE:
  883. case FF_CMP_NSSE:
  884. return lambda2>>FF_LAMBDA_SHIFT;
  885. case FF_CMP_BIT:
  886. return 1;
  887. }
  888. }
  889. void ff_estimate_p_frame_motion(MpegEncContext * s,
  890. int mb_x, int mb_y)
  891. {
  892. MotionEstContext * const c= &s->me;
  893. uint8_t *pix, *ppix;
  894. int sum, mx, my, dmin;
  895. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  896. int vard; ///< sum of squared differences with the estimated motion vector
  897. int P[10][2];
  898. const int shift= 1+s->quarter_sample;
  899. int mb_type=0;
  900. Picture * const pic= &s->current_picture;
  901. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  902. av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
  903. av_assert0(s->linesize == c->stride);
  904. av_assert0(s->uvlinesize == c->uvstride);
  905. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  906. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  907. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  908. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  909. get_limits(s, 16*mb_x, 16*mb_y);
  910. c->skip=0;
  911. /* intra / predictive decision */
  912. pix = c->src[0][0];
  913. sum = s->dsp.pix_sum(pix, s->linesize);
  914. varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500;
  915. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  916. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  917. c->mb_var_sum_temp += (varc+128)>>8;
  918. if(c->avctx->me_threshold){
  919. vard= check_input_motion(s, mb_x, mb_y, 1);
  920. if((vard+128)>>8 < c->avctx->me_threshold){
  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. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  924. c->mc_mb_var_sum_temp += (vard+128)>>8;
  925. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  926. return;
  927. }
  928. if((vard+128)>>8 < c->avctx->mb_threshold)
  929. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  930. }
  931. switch(s->me_method) {
  932. case ME_ZERO:
  933. default:
  934. mx = 0;
  935. my = 0;
  936. dmin = 0;
  937. break;
  938. case ME_X1:
  939. case ME_EPZS:
  940. {
  941. const int mot_stride = s->b8_stride;
  942. const int mot_xy = s->block_index[0];
  943. P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
  944. P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
  945. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  946. if(!s->first_slice_line) {
  947. P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
  948. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  949. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
  950. P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
  951. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  952. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  953. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  954. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  955. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  956. if(s->out_format == FMT_H263){
  957. c->pred_x = P_MEDIAN[0];
  958. c->pred_y = P_MEDIAN[1];
  959. }else { /* mpeg1 at least */
  960. c->pred_x= P_LEFT[0];
  961. c->pred_y= P_LEFT[1];
  962. }
  963. }else{
  964. c->pred_x= P_LEFT[0];
  965. c->pred_y= P_LEFT[1];
  966. }
  967. }
  968. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  969. break;
  970. }
  971. /* At this point (mx,my) are full-pell and the relative displacement */
  972. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  973. vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
  974. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  975. c->mc_mb_var_sum_temp += (vard+128)>>8;
  976. if(mb_type){
  977. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  978. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  979. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  980. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  981. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  982. set_p_mv_tables(s, mx, my, 1);
  983. }else{
  984. mx <<=shift;
  985. my <<=shift;
  986. }
  987. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  988. h263_mv4_search(s, mx, my, shift);
  989. set_p_mv_tables(s, mx, my, 0);
  990. }
  991. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  992. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  993. }
  994. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  995. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  996. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  997. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  998. if (vard*2 + 200*256 > varc)
  999. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  1000. if (varc*2 + 200*256 > vard || s->qscale > 24){
  1001. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  1002. mb_type|= CANDIDATE_MB_TYPE_INTER;
  1003. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1004. if(s->flags&CODEC_FLAG_MV0)
  1005. if(mx || my)
  1006. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  1007. }else{
  1008. mx <<=shift;
  1009. my <<=shift;
  1010. }
  1011. if((s->flags&CODEC_FLAG_4MV)
  1012. && !c->skip && varc>50<<8 && vard>10<<8){
  1013. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  1014. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  1015. set_p_mv_tables(s, mx, my, 0);
  1016. }else
  1017. set_p_mv_tables(s, mx, my, 1);
  1018. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1019. && !c->skip){ //FIXME varc/d checks
  1020. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  1021. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  1022. }
  1023. }else{
  1024. int intra_score, i;
  1025. mb_type= CANDIDATE_MB_TYPE_INTER;
  1026. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1027. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1028. dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1029. if((s->flags&CODEC_FLAG_4MV)
  1030. && !c->skip && varc>50<<8 && vard>10<<8){
  1031. int dmin4= h263_mv4_search(s, mx, my, shift);
  1032. if(dmin4 < dmin){
  1033. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  1034. dmin=dmin4;
  1035. }
  1036. }
  1037. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1038. && !c->skip){ //FIXME varc/d checks
  1039. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  1040. if(dmin_i < dmin){
  1041. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  1042. dmin= dmin_i;
  1043. }
  1044. }
  1045. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  1046. /* get intra luma score */
  1047. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  1048. intra_score= varc - 500;
  1049. }else{
  1050. unsigned mean = (sum+128)>>8;
  1051. mean*= 0x01010101;
  1052. for(i=0; i<16; i++){
  1053. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1054. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1055. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1056. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1057. }
  1058. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1059. }
  1060. intra_score += c->mb_penalty_factor*16;
  1061. if(intra_score < dmin){
  1062. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1063. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1064. }else
  1065. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
  1066. {
  1067. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1068. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1069. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1070. }
  1071. }
  1072. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1073. }
  1074. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1075. int mb_x, int mb_y)
  1076. {
  1077. MotionEstContext * const c= &s->me;
  1078. int mx, my, dmin;
  1079. int P[10][2];
  1080. const int shift= 1+s->quarter_sample;
  1081. const int xy= mb_x + mb_y*s->mb_stride;
  1082. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  1083. av_assert0(s->quarter_sample==0 || s->quarter_sample==1);
  1084. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1085. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1086. get_limits(s, 16*mb_x, 16*mb_y);
  1087. c->skip=0;
  1088. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1089. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1090. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1091. /* special case for first line */
  1092. if (s->first_slice_line) {
  1093. c->pred_x= P_LEFT[0];
  1094. c->pred_y= P_LEFT[1];
  1095. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1096. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1097. } else {
  1098. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1099. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1100. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1101. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1102. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1103. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1104. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1105. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1106. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1107. c->pred_x = P_MEDIAN[0];
  1108. c->pred_y = P_MEDIAN[1];
  1109. }
  1110. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1111. s->p_mv_table[xy][0] = mx<<shift;
  1112. s->p_mv_table[xy][1] = my<<shift;
  1113. return dmin;
  1114. }
  1115. static int ff_estimate_motion_b(MpegEncContext * s,
  1116. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1117. {
  1118. MotionEstContext * const c= &s->me;
  1119. int mx, my, dmin;
  1120. int P[10][2];
  1121. const int shift= 1+s->quarter_sample;
  1122. const int mot_stride = s->mb_stride;
  1123. const int mot_xy = mb_y*mot_stride + mb_x;
  1124. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1125. int mv_scale;
  1126. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1127. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1128. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1129. c->current_mv_penalty= mv_penalty;
  1130. get_limits(s, 16*mb_x, 16*mb_y);
  1131. switch(s->me_method) {
  1132. case ME_ZERO:
  1133. default:
  1134. mx = 0;
  1135. my = 0;
  1136. dmin = 0;
  1137. break;
  1138. case ME_X1:
  1139. case ME_EPZS:
  1140. P_LEFT[0] = mv_table[mot_xy - 1][0];
  1141. P_LEFT[1] = mv_table[mot_xy - 1][1];
  1142. if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
  1143. /* special case for first line */
  1144. if (!s->first_slice_line) {
  1145. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  1146. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  1147. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
  1148. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
  1149. if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
  1150. if (P_TOPRIGHT[0] < (c->xmin << shift)) P_TOPRIGHT[0] = (c->xmin << shift);
  1151. if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
  1152. P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1153. P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1154. }
  1155. c->pred_x = P_LEFT[0];
  1156. c->pred_y = P_LEFT[1];
  1157. if(mv_table == s->b_forw_mv_table){
  1158. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1159. }else{
  1160. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1161. }
  1162. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1163. break;
  1164. }
  1165. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1166. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1167. dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1168. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1169. mv_table[mot_xy][0]= mx;
  1170. mv_table[mot_xy][1]= my;
  1171. return dmin;
  1172. }
  1173. static inline int check_bidir_mv(MpegEncContext * s,
  1174. int motion_fx, int motion_fy,
  1175. int motion_bx, int motion_by,
  1176. int pred_fx, int pred_fy,
  1177. int pred_bx, int pred_by,
  1178. int size, int h)
  1179. {
  1180. //FIXME optimize?
  1181. //FIXME better f_code prediction (max mv & distance)
  1182. //FIXME pointers
  1183. MotionEstContext * const c= &s->me;
  1184. uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1185. uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
  1186. int stride= c->stride;
  1187. uint8_t *dest_y = c->scratchpad;
  1188. uint8_t *ptr;
  1189. int dxy;
  1190. int src_x, src_y;
  1191. int fbmin;
  1192. uint8_t **src_data= c->src[0];
  1193. uint8_t **ref_data= c->ref[0];
  1194. uint8_t **ref2_data= c->ref[2];
  1195. if(s->quarter_sample){
  1196. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1197. src_x = motion_fx >> 2;
  1198. src_y = motion_fy >> 2;
  1199. ptr = ref_data[0] + (src_y * stride) + src_x;
  1200. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
  1201. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1202. src_x = motion_bx >> 2;
  1203. src_y = motion_by >> 2;
  1204. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1205. s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
  1206. }else{
  1207. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1208. src_x = motion_fx >> 1;
  1209. src_y = motion_fy >> 1;
  1210. ptr = ref_data[0] + (src_y * stride) + src_x;
  1211. s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1212. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1213. src_x = motion_bx >> 1;
  1214. src_y = motion_by >> 1;
  1215. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1216. s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1217. }
  1218. fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
  1219. +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
  1220. + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
  1221. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1222. }
  1223. //FIXME CHROMA !!!
  1224. return fbmin;
  1225. }
  1226. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1227. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1228. {
  1229. MotionEstContext * const c= &s->me;
  1230. const int mot_stride = s->mb_stride;
  1231. const int xy = mb_y *mot_stride + mb_x;
  1232. int fbmin;
  1233. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1234. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1235. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1236. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1237. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1238. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1239. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1240. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1241. const int flags= c->sub_flags;
  1242. const int qpel= flags&FLAG_QPEL;
  1243. const int shift= 1+qpel;
  1244. const int xmin= c->xmin<<shift;
  1245. const int ymin= c->ymin<<shift;
  1246. const int xmax= c->xmax<<shift;
  1247. const int ymax= c->ymax<<shift;
  1248. #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
  1249. #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
  1250. int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
  1251. uint8_t map[256] = { 0 };
  1252. map[hashidx&255] = 1;
  1253. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1254. motion_bx, motion_by,
  1255. pred_fx, pred_fy,
  1256. pred_bx, pred_by,
  1257. 0, 16);
  1258. if(s->avctx->bidir_refine){
  1259. int end;
  1260. static const uint8_t limittab[5]={0,8,32,64,80};
  1261. const int limit= limittab[s->avctx->bidir_refine];
  1262. static const int8_t vect[][4]={
  1263. { 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},
  1264. { 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},
  1265. { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
  1266. { 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},
  1267. { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
  1268. { 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},
  1269. { 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},
  1270. { 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},
  1271. { 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},
  1272. { 1, 1, 1, 1}, {-1,-1,-1,-1},
  1273. { 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},
  1274. { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
  1275. };
  1276. static const uint8_t hash[]={
  1277. 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),
  1278. 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),
  1279. HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
  1280. 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),
  1281. HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
  1282. 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),
  1283. 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),
  1284. 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),
  1285. 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),
  1286. HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
  1287. 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),
  1288. 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),
  1289. };
  1290. #define CHECK_BIDIR(fx,fy,bx,by)\
  1291. if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
  1292. &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
  1293. &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
  1294. int score;\
  1295. map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
  1296. 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);\
  1297. if(score < fbmin){\
  1298. hashidx += HASH(fx,fy,bx,by);\
  1299. fbmin= score;\
  1300. motion_fx+=fx;\
  1301. motion_fy+=fy;\
  1302. motion_bx+=bx;\
  1303. motion_by+=by;\
  1304. end=0;\
  1305. }\
  1306. }
  1307. #define CHECK_BIDIR2(a,b,c,d)\
  1308. CHECK_BIDIR(a,b,c,d)\
  1309. CHECK_BIDIR(-(a),-(b),-(c),-(d))
  1310. do{
  1311. int i;
  1312. int borderdist=0;
  1313. end=1;
  1314. CHECK_BIDIR2(0,0,0,1)
  1315. CHECK_BIDIR2(0,0,1,0)
  1316. CHECK_BIDIR2(0,1,0,0)
  1317. CHECK_BIDIR2(1,0,0,0)
  1318. for(i=8; i<limit; i++){
  1319. int fx= motion_fx+vect[i][0];
  1320. int fy= motion_fy+vect[i][1];
  1321. int bx= motion_bx+vect[i][2];
  1322. int by= motion_by+vect[i][3];
  1323. if(borderdist<=0){
  1324. int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
  1325. int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
  1326. if((a|b) < 0)
  1327. map[(hashidx+hash[i])&255] = 1;
  1328. }
  1329. if(!map[(hashidx+hash[i])&255]){
  1330. int score;
  1331. map[(hashidx+hash[i])&255] = 1;
  1332. score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
  1333. if(score < fbmin){
  1334. hashidx += hash[i];
  1335. fbmin= score;
  1336. motion_fx=fx;
  1337. motion_fy=fy;
  1338. motion_bx=bx;
  1339. motion_by=by;
  1340. end=0;
  1341. borderdist--;
  1342. if(borderdist<=0){
  1343. int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
  1344. int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
  1345. borderdist= FFMIN(a,b);
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }while(!end);
  1351. }
  1352. s->b_bidir_forw_mv_table[xy][0]= motion_fx;
  1353. s->b_bidir_forw_mv_table[xy][1]= motion_fy;
  1354. s->b_bidir_back_mv_table[xy][0]= motion_bx;
  1355. s->b_bidir_back_mv_table[xy][1]= motion_by;
  1356. return fbmin;
  1357. }
  1358. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1359. {
  1360. MotionEstContext * const c= &s->me;
  1361. int P[10][2];
  1362. const int mot_stride = s->mb_stride;
  1363. const int mot_xy = mb_y*mot_stride + mb_x;
  1364. const int shift= 1+s->quarter_sample;
  1365. int dmin, i;
  1366. const int time_pp= s->pp_time;
  1367. const int time_pb= s->pb_time;
  1368. int mx, my, xmin, xmax, ymin, ymax;
  1369. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1370. c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
  1371. ymin= xmin=(-32)>>shift;
  1372. ymax= xmax= 31>>shift;
  1373. if (IS_8X8(s->next_picture.f.mb_type[mot_xy])) {
  1374. s->mv_type= MV_TYPE_8X8;
  1375. }else{
  1376. s->mv_type= MV_TYPE_16X16;
  1377. }
  1378. for(i=0; i<4; i++){
  1379. int index= s->block_index[i];
  1380. int min, max;
  1381. c->co_located_mv[i][0] = s->next_picture.f.motion_val[0][index][0];
  1382. c->co_located_mv[i][1] = s->next_picture.f.motion_val[0][index][1];
  1383. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1384. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1385. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1386. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1387. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1388. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1389. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1390. min+= 16*mb_x - 1;
  1391. xmax= FFMIN(xmax, s->width - max);
  1392. xmin= FFMAX(xmin, - 16 - min);
  1393. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1394. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1395. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1396. min+= 16*mb_y - 1;
  1397. ymax= FFMIN(ymax, s->height - max);
  1398. ymin= FFMAX(ymin, - 16 - min);
  1399. if(s->mv_type == MV_TYPE_16X16) break;
  1400. }
  1401. av_assert2(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1402. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1403. s->b_direct_mv_table[mot_xy][0]= 0;
  1404. s->b_direct_mv_table[mot_xy][1]= 0;
  1405. return 256*256*256*64;
  1406. }
  1407. c->xmin= xmin;
  1408. c->ymin= ymin;
  1409. c->xmax= xmax;
  1410. c->ymax= ymax;
  1411. c->flags |= FLAG_DIRECT;
  1412. c->sub_flags |= FLAG_DIRECT;
  1413. c->pred_x=0;
  1414. c->pred_y=0;
  1415. P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1416. P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1417. /* special case for first line */
  1418. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
  1419. P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1420. P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1421. P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1422. P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1423. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1424. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1425. }
  1426. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1427. if(c->sub_flags&FLAG_QPEL)
  1428. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1429. else
  1430. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1431. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1432. dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1433. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1434. mv_table[mot_xy][0]= mx;
  1435. mv_table[mot_xy][1]= my;
  1436. c->flags &= ~FLAG_DIRECT;
  1437. c->sub_flags &= ~FLAG_DIRECT;
  1438. return dmin;
  1439. }
  1440. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1441. int mb_x, int mb_y)
  1442. {
  1443. MotionEstContext * const c= &s->me;
  1444. const int penalty_factor= c->mb_penalty_factor;
  1445. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1446. int type=0;
  1447. const int xy = mb_y*s->mb_stride + mb_x;
  1448. init_ref(c, s->new_picture.f.data, s->last_picture.f.data,
  1449. s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);
  1450. get_limits(s, 16*mb_x, 16*mb_y);
  1451. c->skip=0;
  1452. if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {
  1453. int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
  1454. score= ((unsigned)(score*score + 128*256))>>16;
  1455. c->mc_mb_var_sum_temp += score;
  1456. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1457. s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
  1458. return;
  1459. }
  1460. if(c->avctx->me_threshold){
  1461. int vard= check_input_motion(s, mb_x, mb_y, 0);
  1462. if((vard+128)>>8 < c->avctx->me_threshold){
  1463. // pix = c->src[0][0];
  1464. // sum = s->dsp.pix_sum(pix, s->linesize);
  1465. // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  1466. // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  1467. s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1468. /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1469. c->mb_var_sum_temp += (varc+128)>>8;*/
  1470. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1471. /* if (vard <= 64<<8 || vard < varc) {
  1472. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1473. }else{
  1474. c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
  1475. }*/
  1476. return;
  1477. }
  1478. if((vard+128)>>8 < c->avctx->mb_threshold){
  1479. type= s->mb_type[mb_y*s->mb_stride + mb_x];
  1480. if(type == CANDIDATE_MB_TYPE_DIRECT){
  1481. direct_search(s, mb_x, mb_y);
  1482. }
  1483. if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1484. c->skip=0;
  1485. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
  1486. }
  1487. if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1488. c->skip=0;
  1489. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
  1490. }
  1491. if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1492. c->skip=0;
  1493. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1494. interlaced_search(s, 0,
  1495. s->b_field_mv_table[0], s->b_field_select_table[0],
  1496. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
  1497. }
  1498. if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1499. c->skip=0;
  1500. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1501. interlaced_search(s, 2,
  1502. s->b_field_mv_table[1], s->b_field_select_table[1],
  1503. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
  1504. }
  1505. return;
  1506. }
  1507. }
  1508. if (s->codec_id == AV_CODEC_ID_MPEG4)
  1509. dmin= direct_search(s, mb_x, mb_y);
  1510. else
  1511. dmin= INT_MAX;
  1512. //FIXME penalty stuff for non mpeg4
  1513. c->skip=0;
  1514. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
  1515. c->skip=0;
  1516. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
  1517. av_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1518. c->skip=0;
  1519. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1520. av_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1521. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1522. //FIXME mb type penalty
  1523. c->skip=0;
  1524. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1525. fimin= interlaced_search(s, 0,
  1526. s->b_field_mv_table[0], s->b_field_select_table[0],
  1527. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1528. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1529. bimin= interlaced_search(s, 2,
  1530. s->b_field_mv_table[1], s->b_field_select_table[1],
  1531. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1532. }else
  1533. fimin= bimin= INT_MAX;
  1534. {
  1535. int score= fmin;
  1536. type = CANDIDATE_MB_TYPE_FORWARD;
  1537. if (dmin <= score){
  1538. score = dmin;
  1539. type = CANDIDATE_MB_TYPE_DIRECT;
  1540. }
  1541. if(bmin<score){
  1542. score=bmin;
  1543. type= CANDIDATE_MB_TYPE_BACKWARD;
  1544. }
  1545. if(fbmin<score){
  1546. score=fbmin;
  1547. type= CANDIDATE_MB_TYPE_BIDIR;
  1548. }
  1549. if(fimin<score){
  1550. score=fimin;
  1551. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1552. }
  1553. if(bimin<score){
  1554. score=bimin;
  1555. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1556. }
  1557. score= ((unsigned)(score*score + 128*256))>>16;
  1558. c->mc_mb_var_sum_temp += score;
  1559. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1560. }
  1561. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1562. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1563. if(fimin < INT_MAX)
  1564. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1565. if(bimin < INT_MAX)
  1566. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1567. if(fimin < INT_MAX && bimin < INT_MAX){
  1568. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1569. }
  1570. //FIXME something smarter
  1571. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
  1572. 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])
  1573. type |= CANDIDATE_MB_TYPE_DIRECT0;
  1574. }
  1575. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1576. }
  1577. /* find best f_code for ME which do unlimited searches */
  1578. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1579. {
  1580. if(s->me_method>=ME_EPZS){
  1581. int score[8];
  1582. int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
  1583. uint8_t * fcode_tab= s->fcode_tab;
  1584. int best_fcode=-1;
  1585. int best_score=-10000000;
  1586. if(s->msmpeg4_version)
  1587. range= FFMIN(range, 16);
  1588. else if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
  1589. range= FFMIN(range, 256);
  1590. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1591. for(y=0; y<s->mb_height; y++){
  1592. int x;
  1593. int xy= y*s->mb_stride;
  1594. for(x=0; x<s->mb_width; x++){
  1595. if(s->mb_type[xy] & type){
  1596. int mx= mv_table[xy][0];
  1597. int my= mv_table[xy][1];
  1598. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1599. fcode_tab[my + MAX_MV]);
  1600. int j;
  1601. if(mx >= range || mx < -range ||
  1602. my >= range || my < -range)
  1603. continue;
  1604. for(j=0; j<fcode && j<8; j++){
  1605. if(s->pict_type==AV_PICTURE_TYPE_B || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1606. score[j]-= 170;
  1607. }
  1608. }
  1609. xy++;
  1610. }
  1611. }
  1612. for(i=1; i<8; i++){
  1613. if(score[i] > best_score){
  1614. best_score= score[i];
  1615. best_fcode= i;
  1616. }
  1617. }
  1618. return best_fcode;
  1619. }else{
  1620. return 1;
  1621. }
  1622. }
  1623. void ff_fix_long_p_mvs(MpegEncContext * s)
  1624. {
  1625. MotionEstContext * const c= &s->me;
  1626. const int f_code= s->f_code;
  1627. int y, range;
  1628. av_assert0(s->pict_type==AV_PICTURE_TYPE_P);
  1629. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1630. av_assert0(range <= 16 || !s->msmpeg4_version);
  1631. av_assert0(range <=256 || !(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
  1632. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1633. if(s->flags&CODEC_FLAG_4MV){
  1634. const int wrap= s->b8_stride;
  1635. /* clip / convert to intra 8x8 type MVs */
  1636. for(y=0; y<s->mb_height; y++){
  1637. int xy= y*2*wrap;
  1638. int i= y*s->mb_stride;
  1639. int x;
  1640. for(x=0; x<s->mb_width; x++){
  1641. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1642. int block;
  1643. for(block=0; block<4; block++){
  1644. int off= (block& 1) + (block>>1)*wrap;
  1645. int mx = s->current_picture.f.motion_val[0][ xy + off ][0];
  1646. int my = s->current_picture.f.motion_val[0][ xy + off ][1];
  1647. if( mx >=range || mx <-range
  1648. || my >=range || my <-range){
  1649. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1650. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1651. s->current_picture.f.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
  1652. }
  1653. }
  1654. }
  1655. xy+=2;
  1656. i++;
  1657. }
  1658. }
  1659. }
  1660. }
  1661. /**
  1662. *
  1663. * @param truncate 1 for truncation, 0 for using intra
  1664. */
  1665. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1666. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1667. {
  1668. MotionEstContext * const c= &s->me;
  1669. int y, h_range, v_range;
  1670. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1671. int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1672. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1673. h_range= range;
  1674. v_range= field_select_table ? range>>1 : range;
  1675. /* clip / convert to intra 16x16 type MVs */
  1676. for(y=0; y<s->mb_height; y++){
  1677. int x;
  1678. int xy= y*s->mb_stride;
  1679. for(x=0; x<s->mb_width; x++){
  1680. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1681. if(field_select_table==NULL || field_select_table[xy] == field_select){
  1682. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1683. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1684. if(truncate){
  1685. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1686. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1687. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1688. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1689. }else{
  1690. s->mb_type[xy] &= ~type;
  1691. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1692. mv_table[xy][0]=
  1693. mv_table[xy][1]= 0;
  1694. }
  1695. }
  1696. }
  1697. }
  1698. xy++;
  1699. }
  1700. }
  1701. }