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.

1937 lines
76KB

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