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.

1958 lines
77KB

  1. /*
  2. * Motion estimation
  3. * Copyright (c) 2000,2001 Fabrice Bellard
  4. * Copyright (c) 2002-2004 Michael Niedermayer
  5. *
  6. * new motion estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  7. *
  8. * This file is part of FFmpeg.
  9. *
  10. * FFmpeg is free software; you can redistribute it and/or
  11. * modify it under the terms of the GNU Lesser General Public
  12. * License as published by the Free Software Foundation; either
  13. * version 2.1 of the License, or (at your option) any later version.
  14. *
  15. * FFmpeg is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with FFmpeg; if not, write to the Free Software
  22. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  23. */
  24. /**
  25. * @file
  26. * Motion estimation.
  27. */
  28. #include <stdlib.h>
  29. #include <stdio.h>
  30. #include <limits.h>
  31. #include "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->width;
  473. c->ymax = - y + s->height;
  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, i;
  510. int same=1;
  511. const int stride= c->stride;
  512. uint8_t *mv_penalty= c->current_mv_penalty;
  513. int saftey_cliping= s->unrestricted_mv && (s->width&15) && (s->height&15);
  514. init_mv4_ref(c);
  515. for(block=0; block<4; block++){
  516. int mx4, my4;
  517. int pred_x4, pred_y4;
  518. int dmin4;
  519. static const int off[4]= {2, 1, 1, -1};
  520. const int mot_stride = s->b8_stride;
  521. const int mot_xy = s->block_index[block];
  522. if(saftey_cliping){
  523. c->xmax = - 16*s->mb_x + s->width - 8*(block &1);
  524. c->ymax = - 16*s->mb_y + s->height - 8*(block>>1);
  525. }
  526. P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
  527. P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
  528. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  529. /* special case for first line */
  530. if (s->first_slice_line && block<2) {
  531. c->pred_x= pred_x4= P_LEFT[0];
  532. c->pred_y= pred_y4= P_LEFT[1];
  533. } else {
  534. P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
  535. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  536. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
  537. P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][1];
  538. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  539. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  540. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  541. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  542. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  543. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  544. c->pred_x= pred_x4 = P_MEDIAN[0];
  545. c->pred_y= pred_y4 = P_MEDIAN[1];
  546. }
  547. P_MV1[0]= mx;
  548. P_MV1[1]= my;
  549. if(saftey_cliping)
  550. for(i=0; i<10; i++){
  551. if(P[i][0] > (c->xmax<<shift)) P[i][0]= (c->xmax<<shift);
  552. if(P[i][1] > (c->ymax<<shift)) P[i][1]= (c->ymax<<shift);
  553. }
  554. dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
  555. dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
  556. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  557. int dxy;
  558. const int offset= ((block&1) + (block>>1)*stride)*8;
  559. uint8_t *dest_y = c->scratchpad + offset;
  560. if(s->quarter_sample){
  561. uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
  562. dxy = ((my4 & 3) << 2) | (mx4 & 3);
  563. if(s->no_rounding)
  564. s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride);
  565. else
  566. s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride);
  567. }else{
  568. uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
  569. dxy = ((my4 & 1) << 1) | (mx4 & 1);
  570. if(s->no_rounding)
  571. s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
  572. else
  573. s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
  574. }
  575. dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
  576. }else
  577. dmin_sum+= dmin4;
  578. if(s->quarter_sample){
  579. mx4_sum+= mx4/2;
  580. my4_sum+= my4/2;
  581. }else{
  582. mx4_sum+= mx4;
  583. my4_sum+= my4;
  584. }
  585. s->current_picture.f.motion_val[0][s->block_index[block]][0] = mx4;
  586. s->current_picture.f.motion_val[0][s->block_index[block]][1] = my4;
  587. if(mx4 != mx || my4 != my) same=0;
  588. }
  589. if(same)
  590. return INT_MAX;
  591. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  592. 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);
  593. }
  594. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  595. int dxy;
  596. int mx, my;
  597. int offset;
  598. mx= ff_h263_round_chroma(mx4_sum);
  599. my= ff_h263_round_chroma(my4_sum);
  600. dxy = ((my & 1) << 1) | (mx & 1);
  601. offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
  602. if(s->no_rounding){
  603. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
  604. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
  605. }else{
  606. s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
  607. s->dsp.put_pixels_tab [1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
  608. }
  609. 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);
  610. 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);
  611. }
  612. c->pred_x= mx;
  613. c->pred_y= my;
  614. switch(c->avctx->mb_cmp&0xFF){
  615. /*case FF_CMP_SSE:
  616. return dmin_sum+ 32*s->qscale*s->qscale;*/
  617. case FF_CMP_RD:
  618. return dmin_sum;
  619. default:
  620. return dmin_sum+ 11*c->mb_penalty_factor;
  621. }
  622. }
  623. static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
  624. MotionEstContext * const c= &s->me;
  625. c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
  626. c->src[1][0] = c->src[0][0] + s->linesize;
  627. if(c->flags & FLAG_CHROMA){
  628. c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
  629. c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
  630. c->src[1][1] = c->src[0][1] + s->uvlinesize;
  631. c->src[1][2] = c->src[0][2] + s->uvlinesize;
  632. }
  633. }
  634. static int interlaced_search(MpegEncContext *s, int ref_index,
  635. int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  636. {
  637. MotionEstContext * const c= &s->me;
  638. const int size=0;
  639. const int h=8;
  640. int block;
  641. int P[10][2];
  642. uint8_t * const mv_penalty= c->current_mv_penalty;
  643. int same=1;
  644. const int stride= 2*s->linesize;
  645. int dmin_sum= 0;
  646. const int mot_stride= s->mb_stride;
  647. const int xy= s->mb_x + s->mb_y*mot_stride;
  648. c->ymin>>=1;
  649. c->ymax>>=1;
  650. c->stride<<=1;
  651. c->uvstride<<=1;
  652. init_interlaced_ref(s, ref_index);
  653. for(block=0; block<2; block++){
  654. int field_select;
  655. int best_dmin= INT_MAX;
  656. int best_field= -1;
  657. for(field_select=0; field_select<2; field_select++){
  658. int dmin, mx_i, my_i;
  659. int16_t (*mv_table)[2]= mv_tables[block][field_select];
  660. if(user_field_select){
  661. assert(field_select==0 || field_select==1);
  662. assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
  663. if(field_select_tables[block][xy] != field_select)
  664. continue;
  665. }
  666. P_LEFT[0] = mv_table[xy - 1][0];
  667. P_LEFT[1] = mv_table[xy - 1][1];
  668. if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
  669. c->pred_x= P_LEFT[0];
  670. c->pred_y= P_LEFT[1];
  671. if(!s->first_slice_line){
  672. P_TOP[0] = mv_table[xy - mot_stride][0];
  673. P_TOP[1] = mv_table[xy - mot_stride][1];
  674. P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
  675. P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
  676. if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
  677. if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
  678. if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
  679. if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
  680. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  681. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  682. }
  683. P_MV1[0]= mx; //FIXME not correct if block != field_select
  684. P_MV1[1]= my / 2;
  685. dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
  686. dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
  687. mv_table[xy][0]= mx_i;
  688. mv_table[xy][1]= my_i;
  689. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  690. int dxy;
  691. //FIXME chroma ME
  692. uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
  693. dxy = ((my_i & 1) << 1) | (mx_i & 1);
  694. if(s->no_rounding){
  695. s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
  696. }else{
  697. s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
  698. }
  699. dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
  700. dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
  701. }else
  702. dmin+= c->mb_penalty_factor; //field_select bits
  703. dmin += field_select != block; //slightly prefer same field
  704. if(dmin < best_dmin){
  705. best_dmin= dmin;
  706. best_field= field_select;
  707. }
  708. }
  709. {
  710. int16_t (*mv_table)[2]= mv_tables[block][best_field];
  711. if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
  712. if(mv_table[xy][1]&1) same=0;
  713. if(mv_table[xy][1]*2 != my) same=0;
  714. if(best_field != block) same=0;
  715. }
  716. field_select_tables[block][xy]= best_field;
  717. dmin_sum += best_dmin;
  718. }
  719. c->ymin<<=1;
  720. c->ymax<<=1;
  721. c->stride>>=1;
  722. c->uvstride>>=1;
  723. if(same)
  724. return INT_MAX;
  725. switch(c->avctx->mb_cmp&0xFF){
  726. /*case FF_CMP_SSE:
  727. return dmin_sum+ 32*s->qscale*s->qscale;*/
  728. case FF_CMP_RD:
  729. return dmin_sum;
  730. default:
  731. return dmin_sum+ 11*c->mb_penalty_factor;
  732. }
  733. }
  734. static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  735. int ymax= s->me.ymax>>interlaced;
  736. int ymin= s->me.ymin>>interlaced;
  737. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  738. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  739. if(mv[1] < ymin) mv[1] = ymin;
  740. if(mv[1] > ymax) mv[1] = ymax;
  741. }
  742. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  743. MotionEstContext * const c= &s->me;
  744. Picture *p= s->current_picture_ptr;
  745. int mb_xy= mb_x + mb_y*s->mb_stride;
  746. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  747. int mb_type= s->current_picture.f.mb_type[mb_xy];
  748. int flags= c->flags;
  749. int shift= (flags&FLAG_QPEL) + 1;
  750. int mask= (1<<shift)-1;
  751. int x, y, i;
  752. int d=0;
  753. me_cmp_func cmpf= s->dsp.sse[0];
  754. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  755. if(p_type && USES_LIST(mb_type, 1)){
  756. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  757. return INT_MAX/2;
  758. }
  759. assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  760. for(i=0; i<4; i++){
  761. int xy= s->block_index[i];
  762. clip_input_mv(s, p->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
  763. clip_input_mv(s, p->f.motion_val[1][xy], !!IS_INTERLACED(mb_type));
  764. }
  765. if(IS_INTERLACED(mb_type)){
  766. int xy2= xy + s->b8_stride;
  767. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  768. c->stride<<=1;
  769. c->uvstride<<=1;
  770. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  771. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  772. return INT_MAX/2;
  773. }
  774. if(USES_LIST(mb_type, 0)){
  775. int field_select0= p->f.ref_index[0][4*mb_xy ];
  776. int field_select1= p->f.ref_index[0][4*mb_xy+2];
  777. assert(field_select0==0 ||field_select0==1);
  778. assert(field_select1==0 ||field_select1==1);
  779. init_interlaced_ref(s, 0);
  780. if(p_type){
  781. s->p_field_select_table[0][mb_xy]= field_select0;
  782. s->p_field_select_table[1][mb_xy]= field_select1;
  783. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  784. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  785. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  786. }else{
  787. s->b_field_select_table[0][0][mb_xy]= field_select0;
  788. s->b_field_select_table[0][1][mb_xy]= field_select1;
  789. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  790. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  791. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  792. }
  793. x = p->f.motion_val[0][xy ][0];
  794. y = p->f.motion_val[0][xy ][1];
  795. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  796. x = p->f.motion_val[0][xy2][0];
  797. y = p->f.motion_val[0][xy2][1];
  798. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  799. }
  800. if(USES_LIST(mb_type, 1)){
  801. int field_select0 = p->f.ref_index[1][4 * mb_xy ];
  802. int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
  803. assert(field_select0==0 ||field_select0==1);
  804. assert(field_select1==0 ||field_select1==1);
  805. init_interlaced_ref(s, 2);
  806. s->b_field_select_table[1][0][mb_xy]= field_select0;
  807. s->b_field_select_table[1][1][mb_xy]= field_select1;
  808. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy ];
  809. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy2];
  810. if(USES_LIST(mb_type, 0)){
  811. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  812. }else{
  813. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  814. }
  815. x = p->f.motion_val[1][xy ][0];
  816. y = p->f.motion_val[1][xy ][1];
  817. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  818. x = p->f.motion_val[1][xy2][0];
  819. y = p->f.motion_val[1][xy2][1];
  820. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  821. //FIXME bidir scores
  822. }
  823. c->stride>>=1;
  824. c->uvstride>>=1;
  825. }else if(IS_8X8(mb_type)){
  826. if(!(s->flags & CODEC_FLAG_4MV)){
  827. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  828. return INT_MAX/2;
  829. }
  830. cmpf= s->dsp.sse[1];
  831. chroma_cmpf= s->dsp.sse[1];
  832. init_mv4_ref(c);
  833. for(i=0; i<4; i++){
  834. xy= s->block_index[i];
  835. x= p->f.motion_val[0][xy][0];
  836. y= p->f.motion_val[0][xy][1];
  837. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  838. }
  839. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  840. }else{
  841. if(USES_LIST(mb_type, 0)){
  842. if(p_type){
  843. *(uint32_t*)s->p_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  844. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  845. }else if(USES_LIST(mb_type, 1)){
  846. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  847. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  848. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  849. }else{
  850. *(uint32_t*)s->b_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  851. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  852. }
  853. x = p->f.motion_val[0][xy][0];
  854. y = p->f.motion_val[0][xy][1];
  855. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  856. }else if(USES_LIST(mb_type, 1)){
  857. *(uint32_t*)s->b_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  858. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  859. x = p->f.motion_val[1][xy][0];
  860. y = p->f.motion_val[1][xy][1];
  861. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  862. }else
  863. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  864. }
  865. return d;
  866. }
  867. void ff_estimate_p_frame_motion(MpegEncContext * s,
  868. int mb_x, int mb_y)
  869. {
  870. MotionEstContext * const c= &s->me;
  871. uint8_t *pix, *ppix;
  872. int sum, mx, my, dmin;
  873. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  874. int vard; ///< sum of squared differences with the estimated motion vector
  875. int P[10][2];
  876. const int shift= 1+s->quarter_sample;
  877. int mb_type=0;
  878. Picture * const pic= &s->current_picture;
  879. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  880. assert(s->quarter_sample==0 || s->quarter_sample==1);
  881. assert(s->linesize == c->stride);
  882. assert(s->uvlinesize == c->uvstride);
  883. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  884. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  885. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  886. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  887. get_limits(s, 16*mb_x, 16*mb_y);
  888. c->skip=0;
  889. /* intra / predictive decision */
  890. pix = c->src[0][0];
  891. sum = s->dsp.pix_sum(pix, s->linesize);
  892. varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500;
  893. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  894. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  895. c->mb_var_sum_temp += (varc+128)>>8;
  896. if(c->avctx->me_threshold){
  897. vard= check_input_motion(s, mb_x, mb_y, 1);
  898. if((vard+128)>>8 < c->avctx->me_threshold){
  899. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  900. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  901. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  902. c->mc_mb_var_sum_temp += (vard+128)>>8;
  903. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  904. return;
  905. }
  906. if((vard+128)>>8 < c->avctx->mb_threshold)
  907. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  908. }
  909. switch(s->me_method) {
  910. case ME_ZERO:
  911. default:
  912. mx = 0;
  913. my = 0;
  914. dmin = 0;
  915. break;
  916. case ME_X1:
  917. case ME_EPZS:
  918. {
  919. const int mot_stride = s->b8_stride;
  920. const int mot_xy = s->block_index[0];
  921. P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
  922. P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
  923. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  924. if(!s->first_slice_line) {
  925. P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
  926. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  927. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
  928. P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
  929. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  930. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  931. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  932. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  933. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  934. if(s->out_format == FMT_H263){
  935. c->pred_x = P_MEDIAN[0];
  936. c->pred_y = P_MEDIAN[1];
  937. }else { /* mpeg1 at least */
  938. c->pred_x= P_LEFT[0];
  939. c->pred_y= P_LEFT[1];
  940. }
  941. }else{
  942. c->pred_x= P_LEFT[0];
  943. c->pred_y= P_LEFT[1];
  944. }
  945. }
  946. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  947. break;
  948. }
  949. /* At this point (mx,my) are full-pell and the relative displacement */
  950. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  951. vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
  952. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  953. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  954. c->mc_mb_var_sum_temp += (vard+128)>>8;
  955. if(mb_type){
  956. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  957. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  958. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  959. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  960. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  961. set_p_mv_tables(s, mx, my, 1);
  962. }else{
  963. mx <<=shift;
  964. my <<=shift;
  965. }
  966. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  967. h263_mv4_search(s, mx, my, shift);
  968. set_p_mv_tables(s, mx, my, 0);
  969. }
  970. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  971. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  972. }
  973. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  974. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  975. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  976. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  977. if (vard*2 + 200*256 > varc)
  978. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  979. if (varc*2 + 200*256 > vard || s->qscale > 24){
  980. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  981. mb_type|= CANDIDATE_MB_TYPE_INTER;
  982. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  983. if(s->flags&CODEC_FLAG_MV0)
  984. if(mx || my)
  985. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  986. }else{
  987. mx <<=shift;
  988. my <<=shift;
  989. }
  990. if((s->flags&CODEC_FLAG_4MV)
  991. && !c->skip && varc>50<<8 && vard>10<<8){
  992. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  993. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  994. set_p_mv_tables(s, mx, my, 0);
  995. }else
  996. set_p_mv_tables(s, mx, my, 1);
  997. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  998. && !c->skip){ //FIXME varc/d checks
  999. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  1000. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  1001. }
  1002. }else{
  1003. int intra_score, i;
  1004. mb_type= CANDIDATE_MB_TYPE_INTER;
  1005. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1006. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1007. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1008. if((s->flags&CODEC_FLAG_4MV)
  1009. && !c->skip && varc>50<<8 && vard>10<<8){
  1010. int dmin4= h263_mv4_search(s, mx, my, shift);
  1011. if(dmin4 < dmin){
  1012. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  1013. dmin=dmin4;
  1014. }
  1015. }
  1016. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1017. && !c->skip){ //FIXME varc/d checks
  1018. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  1019. if(dmin_i < dmin){
  1020. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  1021. dmin= dmin_i;
  1022. }
  1023. }
  1024. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  1025. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  1026. /* get intra luma score */
  1027. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  1028. intra_score= varc - 500;
  1029. }else{
  1030. unsigned mean = (sum+128)>>8;
  1031. mean*= 0x01010101;
  1032. for(i=0; i<16; i++){
  1033. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1034. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1035. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1036. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1037. }
  1038. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1039. }
  1040. intra_score += c->mb_penalty_factor*16;
  1041. if(intra_score < dmin){
  1042. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1043. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1044. }else
  1045. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
  1046. {
  1047. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1048. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1049. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1050. }
  1051. }
  1052. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1053. }
  1054. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1055. int mb_x, int mb_y)
  1056. {
  1057. MotionEstContext * const c= &s->me;
  1058. int mx, my, dmin;
  1059. int P[10][2];
  1060. const int shift= 1+s->quarter_sample;
  1061. const int xy= mb_x + mb_y*s->mb_stride;
  1062. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  1063. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1064. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1065. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1066. get_limits(s, 16*mb_x, 16*mb_y);
  1067. c->skip=0;
  1068. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1069. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1070. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1071. /* special case for first line */
  1072. if (s->first_slice_line) {
  1073. c->pred_x= P_LEFT[0];
  1074. c->pred_y= P_LEFT[1];
  1075. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1076. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1077. } else {
  1078. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1079. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1080. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1081. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1082. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1083. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1084. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1085. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1086. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1087. c->pred_x = P_MEDIAN[0];
  1088. c->pred_y = P_MEDIAN[1];
  1089. }
  1090. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1091. s->p_mv_table[xy][0] = mx<<shift;
  1092. s->p_mv_table[xy][1] = my<<shift;
  1093. return dmin;
  1094. }
  1095. static int ff_estimate_motion_b(MpegEncContext * s,
  1096. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1097. {
  1098. MotionEstContext * const c= &s->me;
  1099. int mx, my, dmin;
  1100. int P[10][2];
  1101. const int shift= 1+s->quarter_sample;
  1102. const int mot_stride = s->mb_stride;
  1103. const int mot_xy = mb_y*mot_stride + mb_x;
  1104. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1105. int mv_scale;
  1106. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1107. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1108. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1109. c->current_mv_penalty= mv_penalty;
  1110. get_limits(s, 16*mb_x, 16*mb_y);
  1111. switch(s->me_method) {
  1112. case ME_ZERO:
  1113. default:
  1114. mx = 0;
  1115. my = 0;
  1116. dmin = 0;
  1117. break;
  1118. case ME_X1:
  1119. case ME_EPZS:
  1120. P_LEFT[0] = mv_table[mot_xy - 1][0];
  1121. P_LEFT[1] = mv_table[mot_xy - 1][1];
  1122. if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
  1123. /* special case for first line */
  1124. if (!s->first_slice_line) {
  1125. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  1126. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  1127. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
  1128. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
  1129. if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
  1130. if (P_TOPRIGHT[0] < (c->xmin << shift)) P_TOPRIGHT[0] = (c->xmin << shift);
  1131. if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
  1132. P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1133. P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1134. }
  1135. c->pred_x = P_LEFT[0];
  1136. c->pred_y = P_LEFT[1];
  1137. if(mv_table == s->b_forw_mv_table){
  1138. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1139. }else{
  1140. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1141. }
  1142. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1143. break;
  1144. }
  1145. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1146. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1147. dmin= ff_get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1148. //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
  1149. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1150. mv_table[mot_xy][0]= mx;
  1151. mv_table[mot_xy][1]= my;
  1152. return dmin;
  1153. }
  1154. static inline int check_bidir_mv(MpegEncContext * s,
  1155. int motion_fx, int motion_fy,
  1156. int motion_bx, int motion_by,
  1157. int pred_fx, int pred_fy,
  1158. int pred_bx, int pred_by,
  1159. int size, int h)
  1160. {
  1161. //FIXME optimize?
  1162. //FIXME better f_code prediction (max mv & distance)
  1163. //FIXME pointers
  1164. MotionEstContext * const c= &s->me;
  1165. uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1166. uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
  1167. int stride= c->stride;
  1168. uint8_t *dest_y = c->scratchpad;
  1169. uint8_t *ptr;
  1170. int dxy;
  1171. int src_x, src_y;
  1172. int fbmin;
  1173. uint8_t **src_data= c->src[0];
  1174. uint8_t **ref_data= c->ref[0];
  1175. uint8_t **ref2_data= c->ref[2];
  1176. if(s->quarter_sample){
  1177. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1178. src_x = motion_fx >> 2;
  1179. src_y = motion_fy >> 2;
  1180. ptr = ref_data[0] + (src_y * stride) + src_x;
  1181. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
  1182. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1183. src_x = motion_bx >> 2;
  1184. src_y = motion_by >> 2;
  1185. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1186. s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
  1187. }else{
  1188. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1189. src_x = motion_fx >> 1;
  1190. src_y = motion_fy >> 1;
  1191. ptr = ref_data[0] + (src_y * stride) + src_x;
  1192. s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1193. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1194. src_x = motion_bx >> 1;
  1195. src_y = motion_by >> 1;
  1196. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1197. s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1198. }
  1199. fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
  1200. +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
  1201. + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
  1202. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1203. }
  1204. //FIXME CHROMA !!!
  1205. return fbmin;
  1206. }
  1207. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1208. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1209. {
  1210. MotionEstContext * const c= &s->me;
  1211. const int mot_stride = s->mb_stride;
  1212. const int xy = mb_y *mot_stride + mb_x;
  1213. int fbmin;
  1214. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1215. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1216. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1217. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1218. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1219. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1220. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1221. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1222. const int flags= c->sub_flags;
  1223. const int qpel= flags&FLAG_QPEL;
  1224. const int shift= 1+qpel;
  1225. const int xmin= c->xmin<<shift;
  1226. const int ymin= c->ymin<<shift;
  1227. const int xmax= c->xmax<<shift;
  1228. const int ymax= c->ymax<<shift;
  1229. #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
  1230. #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
  1231. int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
  1232. uint8_t map[256] = { 0 };
  1233. map[hashidx&255] = 1;
  1234. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1235. motion_bx, motion_by,
  1236. pred_fx, pred_fy,
  1237. pred_bx, pred_by,
  1238. 0, 16);
  1239. if(s->avctx->bidir_refine){
  1240. int end;
  1241. static const uint8_t limittab[5]={0,8,32,64,80};
  1242. const int limit= limittab[s->avctx->bidir_refine];
  1243. static const int8_t vect[][4]={
  1244. { 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},
  1245. { 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},
  1246. { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
  1247. { 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},
  1248. { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
  1249. { 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},
  1250. { 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},
  1251. { 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},
  1252. { 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},
  1253. { 1, 1, 1, 1}, {-1,-1,-1,-1},
  1254. { 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},
  1255. { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
  1256. };
  1257. static const uint8_t hash[]={
  1258. 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),
  1259. 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),
  1260. HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
  1261. 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),
  1262. HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
  1263. 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),
  1264. 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),
  1265. 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),
  1266. 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),
  1267. HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
  1268. 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),
  1269. 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),
  1270. };
  1271. #define CHECK_BIDIR(fx,fy,bx,by)\
  1272. if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
  1273. &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
  1274. &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
  1275. int score;\
  1276. map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
  1277. 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);\
  1278. if(score < fbmin){\
  1279. hashidx += HASH(fx,fy,bx,by);\
  1280. fbmin= score;\
  1281. motion_fx+=fx;\
  1282. motion_fy+=fy;\
  1283. motion_bx+=bx;\
  1284. motion_by+=by;\
  1285. end=0;\
  1286. }\
  1287. }
  1288. #define CHECK_BIDIR2(a,b,c,d)\
  1289. CHECK_BIDIR(a,b,c,d)\
  1290. CHECK_BIDIR(-(a),-(b),-(c),-(d))
  1291. do{
  1292. int i;
  1293. int borderdist=0;
  1294. end=1;
  1295. CHECK_BIDIR2(0,0,0,1)
  1296. CHECK_BIDIR2(0,0,1,0)
  1297. CHECK_BIDIR2(0,1,0,0)
  1298. CHECK_BIDIR2(1,0,0,0)
  1299. for(i=8; i<limit; i++){
  1300. int fx= motion_fx+vect[i][0];
  1301. int fy= motion_fy+vect[i][1];
  1302. int bx= motion_bx+vect[i][2];
  1303. int by= motion_by+vect[i][3];
  1304. if(borderdist<=0){
  1305. int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
  1306. int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
  1307. if((a|b) < 0)
  1308. map[(hashidx+hash[i])&255] = 1;
  1309. }
  1310. if(!map[(hashidx+hash[i])&255]){
  1311. int score;
  1312. map[(hashidx+hash[i])&255] = 1;
  1313. score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
  1314. if(score < fbmin){
  1315. hashidx += hash[i];
  1316. fbmin= score;
  1317. motion_fx=fx;
  1318. motion_fy=fy;
  1319. motion_bx=bx;
  1320. motion_by=by;
  1321. end=0;
  1322. borderdist--;
  1323. if(borderdist<=0){
  1324. int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
  1325. int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
  1326. borderdist= FFMIN(a,b);
  1327. }
  1328. }
  1329. }
  1330. }
  1331. }while(!end);
  1332. }
  1333. s->b_bidir_forw_mv_table[xy][0]= motion_fx;
  1334. s->b_bidir_forw_mv_table[xy][1]= motion_fy;
  1335. s->b_bidir_back_mv_table[xy][0]= motion_bx;
  1336. s->b_bidir_back_mv_table[xy][1]= motion_by;
  1337. return fbmin;
  1338. }
  1339. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1340. {
  1341. MotionEstContext * const c= &s->me;
  1342. int P[10][2];
  1343. const int mot_stride = s->mb_stride;
  1344. const int mot_xy = mb_y*mot_stride + mb_x;
  1345. const int shift= 1+s->quarter_sample;
  1346. int dmin, i;
  1347. const int time_pp= s->pp_time;
  1348. const int time_pb= s->pb_time;
  1349. int mx, my, xmin, xmax, ymin, ymax;
  1350. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1351. c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
  1352. ymin= xmin=(-32)>>shift;
  1353. ymax= xmax= 31>>shift;
  1354. if (IS_8X8(s->next_picture.f.mb_type[mot_xy])) {
  1355. s->mv_type= MV_TYPE_8X8;
  1356. }else{
  1357. s->mv_type= MV_TYPE_16X16;
  1358. }
  1359. for(i=0; i<4; i++){
  1360. int index= s->block_index[i];
  1361. int min, max;
  1362. c->co_located_mv[i][0] = s->next_picture.f.motion_val[0][index][0];
  1363. c->co_located_mv[i][1] = s->next_picture.f.motion_val[0][index][1];
  1364. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1365. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1366. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1367. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1368. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1369. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1370. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1371. min+= 16*mb_x - 1;
  1372. xmax= FFMIN(xmax, s->width - max);
  1373. xmin= FFMAX(xmin, - 16 - min);
  1374. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1375. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1376. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1377. min+= 16*mb_y - 1;
  1378. ymax= FFMIN(ymax, s->height - max);
  1379. ymin= FFMAX(ymin, - 16 - min);
  1380. if(s->mv_type == MV_TYPE_16X16) break;
  1381. }
  1382. assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1383. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1384. s->b_direct_mv_table[mot_xy][0]= 0;
  1385. s->b_direct_mv_table[mot_xy][1]= 0;
  1386. return 256*256*256*64;
  1387. }
  1388. c->xmin= xmin;
  1389. c->ymin= ymin;
  1390. c->xmax= xmax;
  1391. c->ymax= ymax;
  1392. c->flags |= FLAG_DIRECT;
  1393. c->sub_flags |= FLAG_DIRECT;
  1394. c->pred_x=0;
  1395. c->pred_y=0;
  1396. P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1397. P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1398. /* special case for first line */
  1399. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
  1400. P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1401. P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1402. P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1403. P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1404. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1405. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1406. }
  1407. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1408. if(c->sub_flags&FLAG_QPEL)
  1409. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1410. else
  1411. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1412. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1413. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1414. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1415. mv_table[mot_xy][0]= mx;
  1416. mv_table[mot_xy][1]= my;
  1417. c->flags &= ~FLAG_DIRECT;
  1418. c->sub_flags &= ~FLAG_DIRECT;
  1419. return dmin;
  1420. }
  1421. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1422. int mb_x, int mb_y)
  1423. {
  1424. MotionEstContext * const c= &s->me;
  1425. const int penalty_factor= c->mb_penalty_factor;
  1426. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1427. int type=0;
  1428. const int xy = mb_y*s->mb_stride + mb_x;
  1429. init_ref(c, s->new_picture.f.data, s->last_picture.f.data,
  1430. s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);
  1431. get_limits(s, 16*mb_x, 16*mb_y);
  1432. c->skip=0;
  1433. if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {
  1434. int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
  1435. score= ((unsigned)(score*score + 128*256))>>16;
  1436. c->mc_mb_var_sum_temp += score;
  1437. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1438. s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
  1439. return;
  1440. }
  1441. if(c->avctx->me_threshold){
  1442. int vard= check_input_motion(s, mb_x, mb_y, 0);
  1443. if((vard+128)>>8 < c->avctx->me_threshold){
  1444. // pix = c->src[0][0];
  1445. // sum = s->dsp.pix_sum(pix, s->linesize);
  1446. // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  1447. // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  1448. s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1449. /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1450. c->mb_var_sum_temp += (varc+128)>>8;*/
  1451. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1452. /* if (vard <= 64<<8 || vard < varc) {
  1453. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1454. }else{
  1455. c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
  1456. }*/
  1457. return;
  1458. }
  1459. if((vard+128)>>8 < c->avctx->mb_threshold){
  1460. type= s->mb_type[mb_y*s->mb_stride + mb_x];
  1461. if(type == CANDIDATE_MB_TYPE_DIRECT){
  1462. direct_search(s, mb_x, mb_y);
  1463. }
  1464. if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1465. c->skip=0;
  1466. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
  1467. }
  1468. if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1469. c->skip=0;
  1470. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
  1471. }
  1472. if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1473. c->skip=0;
  1474. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1475. interlaced_search(s, 0,
  1476. s->b_field_mv_table[0], s->b_field_select_table[0],
  1477. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
  1478. }
  1479. if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1480. c->skip=0;
  1481. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1482. interlaced_search(s, 2,
  1483. s->b_field_mv_table[1], s->b_field_select_table[1],
  1484. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
  1485. }
  1486. return;
  1487. }
  1488. }
  1489. if (s->codec_id == AV_CODEC_ID_MPEG4)
  1490. dmin= direct_search(s, mb_x, mb_y);
  1491. else
  1492. dmin= INT_MAX;
  1493. //FIXME penalty stuff for non mpeg4
  1494. c->skip=0;
  1495. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
  1496. c->skip=0;
  1497. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
  1498. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1499. c->skip=0;
  1500. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1501. //printf("%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1502. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1503. //FIXME mb type penalty
  1504. c->skip=0;
  1505. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1506. fimin= interlaced_search(s, 0,
  1507. s->b_field_mv_table[0], s->b_field_select_table[0],
  1508. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1509. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1510. bimin= interlaced_search(s, 2,
  1511. s->b_field_mv_table[1], s->b_field_select_table[1],
  1512. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1513. }else
  1514. fimin= bimin= INT_MAX;
  1515. {
  1516. int score= fmin;
  1517. type = CANDIDATE_MB_TYPE_FORWARD;
  1518. if (dmin <= score){
  1519. score = dmin;
  1520. type = CANDIDATE_MB_TYPE_DIRECT;
  1521. }
  1522. if(bmin<score){
  1523. score=bmin;
  1524. type= CANDIDATE_MB_TYPE_BACKWARD;
  1525. }
  1526. if(fbmin<score){
  1527. score=fbmin;
  1528. type= CANDIDATE_MB_TYPE_BIDIR;
  1529. }
  1530. if(fimin<score){
  1531. score=fimin;
  1532. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1533. }
  1534. if(bimin<score){
  1535. score=bimin;
  1536. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1537. }
  1538. score= ((unsigned)(score*score + 128*256))>>16;
  1539. c->mc_mb_var_sum_temp += score;
  1540. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1541. }
  1542. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1543. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1544. if(fimin < INT_MAX)
  1545. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1546. if(bimin < INT_MAX)
  1547. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1548. if(fimin < INT_MAX && bimin < INT_MAX){
  1549. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1550. }
  1551. //FIXME something smarter
  1552. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
  1553. 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])
  1554. type |= CANDIDATE_MB_TYPE_DIRECT0;
  1555. }
  1556. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1557. }
  1558. /* find best f_code for ME which do unlimited searches */
  1559. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1560. {
  1561. if(s->me_method>=ME_EPZS){
  1562. int score[8];
  1563. int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
  1564. uint8_t * fcode_tab= s->fcode_tab;
  1565. int best_fcode=-1;
  1566. int best_score=-10000000;
  1567. if(s->msmpeg4_version)
  1568. range= FFMIN(range, 16);
  1569. else if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
  1570. range= FFMIN(range, 256);
  1571. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1572. for(y=0; y<s->mb_height; y++){
  1573. int x;
  1574. int xy= y*s->mb_stride;
  1575. for(x=0; x<s->mb_width; x++){
  1576. if(s->mb_type[xy] & type){
  1577. int mx= mv_table[xy][0];
  1578. int my= mv_table[xy][1];
  1579. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1580. fcode_tab[my + MAX_MV]);
  1581. int j;
  1582. if(mx >= range || mx < -range ||
  1583. my >= range || my < -range)
  1584. continue;
  1585. for(j=0; j<fcode && j<8; j++){
  1586. if(s->pict_type==AV_PICTURE_TYPE_B || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1587. score[j]-= 170;
  1588. }
  1589. }
  1590. xy++;
  1591. }
  1592. }
  1593. for(i=1; i<8; i++){
  1594. if(score[i] > best_score){
  1595. best_score= score[i];
  1596. best_fcode= i;
  1597. }
  1598. // printf("%d %d\n", i, score[i]);
  1599. }
  1600. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1601. return best_fcode;
  1602. /* for(i=0; i<=MAX_FCODE; i++){
  1603. printf("%d ", mv_num[i]);
  1604. }
  1605. printf("\n");*/
  1606. }else{
  1607. return 1;
  1608. }
  1609. }
  1610. void ff_fix_long_p_mvs(MpegEncContext * s)
  1611. {
  1612. MotionEstContext * const c= &s->me;
  1613. const int f_code= s->f_code;
  1614. int y, range;
  1615. av_assert0(s->pict_type==AV_PICTURE_TYPE_P);
  1616. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1617. av_assert0(range <= 16 || !s->msmpeg4_version);
  1618. av_assert0(range <=256 || !(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
  1619. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1620. //printf("%d no:%d %d//\n", clip, noclip, f_code);
  1621. if(s->flags&CODEC_FLAG_4MV){
  1622. const int wrap= s->b8_stride;
  1623. /* clip / convert to intra 8x8 type MVs */
  1624. for(y=0; y<s->mb_height; y++){
  1625. int xy= y*2*wrap;
  1626. int i= y*s->mb_stride;
  1627. int x;
  1628. for(x=0; x<s->mb_width; x++){
  1629. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1630. int block;
  1631. for(block=0; block<4; block++){
  1632. int off= (block& 1) + (block>>1)*wrap;
  1633. int mx = s->current_picture.f.motion_val[0][ xy + off ][0];
  1634. int my = s->current_picture.f.motion_val[0][ xy + off ][1];
  1635. if( mx >=range || mx <-range
  1636. || my >=range || my <-range){
  1637. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1638. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1639. s->current_picture.f.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
  1640. }
  1641. }
  1642. }
  1643. xy+=2;
  1644. i++;
  1645. }
  1646. }
  1647. }
  1648. }
  1649. /**
  1650. *
  1651. * @param truncate 1 for truncation, 0 for using intra
  1652. */
  1653. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1654. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1655. {
  1656. MotionEstContext * const c= &s->me;
  1657. int y, h_range, v_range;
  1658. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1659. int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1660. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1661. h_range= range;
  1662. v_range= field_select_table ? range>>1 : range;
  1663. /* clip / convert to intra 16x16 type MVs */
  1664. for(y=0; y<s->mb_height; y++){
  1665. int x;
  1666. int xy= y*s->mb_stride;
  1667. for(x=0; x<s->mb_width; x++){
  1668. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1669. if(field_select_table==NULL || field_select_table[xy] == field_select){
  1670. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1671. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1672. if(truncate){
  1673. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1674. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1675. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1676. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1677. }else{
  1678. s->mb_type[xy] &= ~type;
  1679. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1680. mv_table[xy][0]=
  1681. mv_table[xy][1]= 0;
  1682. }
  1683. }
  1684. }
  1685. }
  1686. xy++;
  1687. }
  1688. }
  1689. }