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.

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