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.

1960 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 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 SQ(a) ((a)*(a))
  39. #define P_LEFT P[1]
  40. #define P_TOP P[2]
  41. #define P_TOPRIGHT P[3]
  42. #define P_MEDIAN P[4]
  43. #define P_MV1 P[9]
  44. static inline int sad_hpel_motion_search(MpegEncContext * s,
  45. int *mx_ptr, int *my_ptr, int dmin,
  46. int src_index, int ref_index,
  47. int size, int h);
  48. static inline unsigned update_map_generation(MotionEstContext *c)
  49. {
  50. c->map_generation+= 1<<(ME_MAP_MV_BITS*2);
  51. if(c->map_generation==0){
  52. c->map_generation= 1<<(ME_MAP_MV_BITS*2);
  53. memset(c->map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
  54. }
  55. return c->map_generation;
  56. }
  57. /* shape adaptive search stuff */
  58. typedef struct Minima{
  59. int height;
  60. int x, y;
  61. int checked;
  62. }Minima;
  63. static int minima_cmp(const void *a, const void *b){
  64. const Minima *da = (const Minima *) a;
  65. const Minima *db = (const Minima *) b;
  66. return da->height - db->height;
  67. }
  68. #define FLAG_QPEL 1 //must be 1
  69. #define FLAG_CHROMA 2
  70. #define FLAG_DIRECT 4
  71. 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){
  72. const int offset[3]= {
  73. y*c-> stride + x,
  74. ((y*c->uvstride + x)>>1),
  75. ((y*c->uvstride + x)>>1),
  76. };
  77. int i;
  78. for(i=0; i<3; i++){
  79. c->src[0][i]= src [i] + offset[i];
  80. c->ref[0][i]= ref [i] + offset[i];
  81. }
  82. if(ref_index){
  83. for(i=0; i<3; i++){
  84. c->ref[ref_index][i]= ref2[i] + offset[i];
  85. }
  86. }
  87. }
  88. static int get_flags(MotionEstContext *c, int direct, int chroma){
  89. return ((c->avctx->flags&CODEC_FLAG_QPEL) ? FLAG_QPEL : 0)
  90. + (direct ? FLAG_DIRECT : 0)
  91. + (chroma ? FLAG_CHROMA : 0);
  92. }
  93. static av_always_inline int cmp_direct_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  94. const int size, const int h, int ref_index, int src_index,
  95. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel){
  96. MotionEstContext * const c= &s->me;
  97. const int stride= c->stride;
  98. const int hx= subx + (x<<(1+qpel));
  99. const int hy= suby + (y<<(1+qpel));
  100. uint8_t * const * const ref= c->ref[ref_index];
  101. uint8_t * const * const src= c->src[src_index];
  102. int d;
  103. //FIXME check chroma 4mv, (no crashes ...)
  104. assert(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1));
  105. if(x >= c->xmin && hx <= c->xmax<<(qpel+1) && y >= c->ymin && hy <= c->ymax<<(qpel+1)){
  106. const int time_pp= s->pp_time;
  107. const int time_pb= s->pb_time;
  108. const int mask= 2*qpel+1;
  109. if(s->mv_type==MV_TYPE_8X8){
  110. int i;
  111. for(i=0; i<4; i++){
  112. int fx = c->direct_basis_mv[i][0] + hx;
  113. int fy = c->direct_basis_mv[i][1] + hy;
  114. 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));
  115. 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));
  116. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  117. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  118. uint8_t *dst= c->temp + 8*(i&1) + 8*stride*(i>>1);
  119. if(qpel){
  120. c->qpel_put[1][fxy](dst, ref[0] + (fx>>2) + (fy>>2)*stride, stride);
  121. c->qpel_avg[1][bxy](dst, ref[8] + (bx>>2) + (by>>2)*stride, stride);
  122. }else{
  123. c->hpel_put[1][fxy](dst, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 8);
  124. c->hpel_avg[1][bxy](dst, ref[8] + (bx>>1) + (by>>1)*stride, stride, 8);
  125. }
  126. }
  127. }else{
  128. int fx = c->direct_basis_mv[0][0] + hx;
  129. int fy = c->direct_basis_mv[0][1] + hy;
  130. int bx = hx ? fx - c->co_located_mv[0][0] : (c->co_located_mv[0][0]*(time_pb - time_pp)/time_pp);
  131. int by = hy ? fy - c->co_located_mv[0][1] : (c->co_located_mv[0][1]*(time_pb - time_pp)/time_pp);
  132. int fxy= (fx&mask) + ((fy&mask)<<(qpel+1));
  133. int bxy= (bx&mask) + ((by&mask)<<(qpel+1));
  134. if(qpel){
  135. c->qpel_put[1][fxy](c->temp , ref[0] + (fx>>2) + (fy>>2)*stride , stride);
  136. c->qpel_put[1][fxy](c->temp + 8 , ref[0] + (fx>>2) + (fy>>2)*stride + 8 , stride);
  137. c->qpel_put[1][fxy](c->temp + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8*stride, stride);
  138. c->qpel_put[1][fxy](c->temp + 8 + 8*stride, ref[0] + (fx>>2) + (fy>>2)*stride + 8 + 8*stride, stride);
  139. c->qpel_avg[1][bxy](c->temp , ref[8] + (bx>>2) + (by>>2)*stride , stride);
  140. c->qpel_avg[1][bxy](c->temp + 8 , ref[8] + (bx>>2) + (by>>2)*stride + 8 , stride);
  141. c->qpel_avg[1][bxy](c->temp + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8*stride, stride);
  142. c->qpel_avg[1][bxy](c->temp + 8 + 8*stride, ref[8] + (bx>>2) + (by>>2)*stride + 8 + 8*stride, stride);
  143. }else{
  144. assert((fx>>1) + 16*s->mb_x >= -16);
  145. assert((fy>>1) + 16*s->mb_y >= -16);
  146. assert((fx>>1) + 16*s->mb_x <= s->width);
  147. assert((fy>>1) + 16*s->mb_y <= s->height);
  148. assert((bx>>1) + 16*s->mb_x >= -16);
  149. assert((by>>1) + 16*s->mb_y >= -16);
  150. assert((bx>>1) + 16*s->mb_x <= s->width);
  151. assert((by>>1) + 16*s->mb_y <= s->height);
  152. c->hpel_put[0][fxy](c->temp, ref[0] + (fx>>1) + (fy>>1)*stride, stride, 16);
  153. c->hpel_avg[0][bxy](c->temp, ref[8] + (bx>>1) + (by>>1)*stride, stride, 16);
  154. }
  155. }
  156. d = cmp_func(s, c->temp, src[0], stride, 16);
  157. }else
  158. d= 256*256*256*32;
  159. return d;
  160. }
  161. static av_always_inline int cmp_inline(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  162. const int size, const int h, int ref_index, int src_index,
  163. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, int qpel, int chroma){
  164. MotionEstContext * const c= &s->me;
  165. const int stride= c->stride;
  166. const int uvstride= c->uvstride;
  167. const int dxy= subx + (suby<<(1+qpel)); //FIXME log2_subpel?
  168. const int hx= subx + (x<<(1+qpel));
  169. const int hy= suby + (y<<(1+qpel));
  170. uint8_t * const * const ref= c->ref[ref_index];
  171. uint8_t * const * const src= c->src[src_index];
  172. int d;
  173. //FIXME check chroma 4mv, (no crashes ...)
  174. int uvdxy; /* no, it might not be used uninitialized */
  175. if(dxy){
  176. if(qpel){
  177. c->qpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride); //FIXME prototype (add h)
  178. if(chroma){
  179. int cx= hx/2;
  180. int cy= hy/2;
  181. cx= (cx>>1)|(cx&1);
  182. cy= (cy>>1)|(cy&1);
  183. uvdxy= (cx&1) + 2*(cy&1);
  184. //FIXME x/y wrong, but mpeg4 qpel is sick anyway, we should drop as much of it as possible in favor for h264
  185. }
  186. }else{
  187. c->hpel_put[size][dxy](c->temp, ref[0] + x + y*stride, stride, h);
  188. if(chroma)
  189. uvdxy= dxy | (x&1) | (2*(y&1));
  190. }
  191. d = cmp_func(s, c->temp, src[0], stride, h);
  192. }else{
  193. d = cmp_func(s, src[0], ref[0] + x + y*stride, stride, h);
  194. if(chroma)
  195. uvdxy= (x&1) + 2*(y&1);
  196. }
  197. if(chroma){
  198. uint8_t * const uvtemp= c->temp + 16*stride;
  199. c->hpel_put[size+1][uvdxy](uvtemp , ref[1] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  200. c->hpel_put[size+1][uvdxy](uvtemp+8, ref[2] + (x>>1) + (y>>1)*uvstride, uvstride, h>>1);
  201. d += chroma_cmp_func(s, uvtemp , src[1], uvstride, h>>1);
  202. d += chroma_cmp_func(s, uvtemp+8, src[2], uvstride, h>>1);
  203. }
  204. return d;
  205. }
  206. static int cmp_simple(MpegEncContext *s, const int x, const int y,
  207. int ref_index, int src_index,
  208. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func){
  209. return cmp_inline(s,x,y,0,0,0,16,ref_index,src_index, cmp_func, chroma_cmp_func, 0, 0);
  210. }
  211. static int cmp_fpel_internal(MpegEncContext *s, const int x, const int y,
  212. const int size, const int h, int ref_index, int src_index,
  213. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  214. if(flags&FLAG_DIRECT){
  215. return cmp_direct_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
  216. }else{
  217. return cmp_inline(s,x,y,0,0,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
  218. }
  219. }
  220. static int cmp_internal(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  221. const int size, const int h, int ref_index, int src_index,
  222. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  223. if(flags&FLAG_DIRECT){
  224. return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags&FLAG_QPEL);
  225. }else{
  226. 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);
  227. }
  228. }
  229. /** @brief compares a block (either a full macroblock or a partition thereof)
  230. against a proposed motion-compensated prediction of that block
  231. */
  232. static av_always_inline int cmp(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  233. const int size, const int h, int ref_index, int src_index,
  234. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  235. if(av_builtin_constant_p(flags) && av_builtin_constant_p(h) && av_builtin_constant_p(size)
  236. && av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
  237. && flags==0 && h==16 && size==0 && subx==0 && suby==0){
  238. return cmp_simple(s,x,y,ref_index,src_index, cmp_func, chroma_cmp_func);
  239. }else if(av_builtin_constant_p(subx) && av_builtin_constant_p(suby)
  240. && subx==0 && suby==0){
  241. return cmp_fpel_internal(s,x,y,size,h,ref_index,src_index, cmp_func, chroma_cmp_func,flags);
  242. }else{
  243. return cmp_internal(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, flags);
  244. }
  245. }
  246. static int cmp_hpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  247. const int size, const int h, int ref_index, int src_index,
  248. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  249. if(flags&FLAG_DIRECT){
  250. return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0);
  251. }else{
  252. return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 0, flags&FLAG_CHROMA);
  253. }
  254. }
  255. static int cmp_qpel(MpegEncContext *s, const int x, const int y, const int subx, const int suby,
  256. const int size, const int h, int ref_index, int src_index,
  257. me_cmp_func cmp_func, me_cmp_func chroma_cmp_func, const int flags){
  258. if(flags&FLAG_DIRECT){
  259. return cmp_direct_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1);
  260. }else{
  261. return cmp_inline(s,x,y,subx,suby,size,h,ref_index,src_index, cmp_func, chroma_cmp_func, 1, flags&FLAG_CHROMA);
  262. }
  263. }
  264. #include "motion_est_template.c"
  265. static int zero_cmp(void *s, uint8_t *a, uint8_t *b, int stride, int h){
  266. return 0;
  267. }
  268. static void zero_hpel(uint8_t *a, const uint8_t *b, int stride, int h){
  269. }
  270. int ff_init_me(MpegEncContext *s){
  271. MotionEstContext * const c= &s->me;
  272. int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
  273. int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
  274. if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -ME_MAP_SIZE){
  275. av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
  276. return -1;
  277. }
  278. //special case of snow is needed because snow uses its own iterative ME code
  279. if(s->me_method!=ME_ZERO && s->me_method!=ME_EPZS && s->me_method!=ME_X1 && s->avctx->codec_id != CODEC_ID_SNOW){
  280. 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");
  281. return -1;
  282. }
  283. c->avctx= s->avctx;
  284. if(cache_size < 2*dia_size && !c->stride){
  285. av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
  286. }
  287. ff_set_cmp(&s->dsp, s->dsp.me_pre_cmp, c->avctx->me_pre_cmp);
  288. ff_set_cmp(&s->dsp, s->dsp.me_cmp, c->avctx->me_cmp);
  289. ff_set_cmp(&s->dsp, s->dsp.me_sub_cmp, c->avctx->me_sub_cmp);
  290. ff_set_cmp(&s->dsp, s->dsp.mb_cmp, c->avctx->mb_cmp);
  291. c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
  292. c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
  293. c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
  294. /*FIXME s->no_rounding b_type*/
  295. if(s->flags&CODEC_FLAG_QPEL){
  296. c->sub_motion_search= qpel_motion_search;
  297. c->qpel_avg= s->dsp.avg_qpel_pixels_tab;
  298. if(s->no_rounding) c->qpel_put= s->dsp.put_no_rnd_qpel_pixels_tab;
  299. else c->qpel_put= s->dsp.put_qpel_pixels_tab;
  300. }else{
  301. if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
  302. c->sub_motion_search= hpel_motion_search;
  303. else if( c->avctx->me_sub_cmp == FF_CMP_SAD
  304. && c->avctx-> me_cmp == FF_CMP_SAD
  305. && c->avctx-> mb_cmp == FF_CMP_SAD)
  306. c->sub_motion_search= sad_hpel_motion_search; // 2050 vs. 2450 cycles
  307. else
  308. c->sub_motion_search= hpel_motion_search;
  309. }
  310. c->hpel_avg= s->dsp.avg_pixels_tab;
  311. if(s->no_rounding) c->hpel_put= s->dsp.put_no_rnd_pixels_tab;
  312. else c->hpel_put= s->dsp.put_pixels_tab;
  313. if(s->linesize){
  314. c->stride = s->linesize;
  315. c->uvstride= s->uvlinesize;
  316. }else{
  317. c->stride = 16*s->mb_width + 32;
  318. c->uvstride= 8*s->mb_width + 16;
  319. }
  320. /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
  321. * not have yet, and even if we had, the motion estimation code
  322. * does not expect it. */
  323. if(s->codec_id != CODEC_ID_SNOW){
  324. if((c->avctx->me_cmp&FF_CMP_CHROMA)/* && !s->dsp.me_cmp[2]*/){
  325. s->dsp.me_cmp[2]= zero_cmp;
  326. }
  327. if((c->avctx->me_sub_cmp&FF_CMP_CHROMA) && !s->dsp.me_sub_cmp[2]){
  328. s->dsp.me_sub_cmp[2]= zero_cmp;
  329. }
  330. c->hpel_put[2][0]= c->hpel_put[2][1]=
  331. c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
  332. }
  333. if(s->codec_id == CODEC_ID_H261){
  334. c->sub_motion_search= no_sub_motion_search;
  335. }
  336. return 0;
  337. }
  338. static inline void no_motion_search(MpegEncContext * s,
  339. int *mx_ptr, int *my_ptr)
  340. {
  341. *mx_ptr = 16 * s->mb_x;
  342. *my_ptr = 16 * s->mb_y;
  343. }
  344. #define Z_THRESHOLD 256
  345. #define CHECK_SAD_HALF_MV(suffix, x, y) \
  346. {\
  347. d= s->dsp.pix_abs[size][(x?1:0)+(y?2:0)](NULL, pix, ptr+((x)>>1), stride, h);\
  348. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*penalty_factor;\
  349. COPY3_IF_LT(dminh, d, dx, x, dy, y)\
  350. }
  351. static inline int sad_hpel_motion_search(MpegEncContext * s,
  352. int *mx_ptr, int *my_ptr, int dmin,
  353. int src_index, int ref_index,
  354. int size, int h)
  355. {
  356. MotionEstContext * const c= &s->me;
  357. const int penalty_factor= c->sub_penalty_factor;
  358. int mx, my, dminh;
  359. uint8_t *pix, *ptr;
  360. int stride= c->stride;
  361. const int flags= c->sub_flags;
  362. LOAD_COMMON
  363. assert(flags == 0);
  364. if(c->skip){
  365. // printf("S");
  366. *mx_ptr = 0;
  367. *my_ptr = 0;
  368. return dmin;
  369. }
  370. // printf("N");
  371. pix = c->src[src_index][0];
  372. mx = *mx_ptr;
  373. my = *my_ptr;
  374. ptr = c->ref[ref_index][0] + (my * stride) + mx;
  375. dminh = dmin;
  376. if (mx > xmin && mx < xmax &&
  377. my > ymin && my < ymax) {
  378. int dx=0, dy=0;
  379. int d, pen_x, pen_y;
  380. const int index= (my<<ME_MAP_SHIFT) + mx;
  381. const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  382. const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
  383. const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
  384. const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
  385. mx<<=1;
  386. my<<=1;
  387. pen_x= pred_x + mx;
  388. pen_y= pred_y + my;
  389. ptr-= stride;
  390. if(t<=b){
  391. CHECK_SAD_HALF_MV(y2 , 0, -1)
  392. if(l<=r){
  393. CHECK_SAD_HALF_MV(xy2, -1, -1)
  394. if(t+r<=b+l){
  395. CHECK_SAD_HALF_MV(xy2, +1, -1)
  396. ptr+= stride;
  397. }else{
  398. ptr+= stride;
  399. CHECK_SAD_HALF_MV(xy2, -1, +1)
  400. }
  401. CHECK_SAD_HALF_MV(x2 , -1, 0)
  402. }else{
  403. CHECK_SAD_HALF_MV(xy2, +1, -1)
  404. if(t+l<=b+r){
  405. CHECK_SAD_HALF_MV(xy2, -1, -1)
  406. ptr+= stride;
  407. }else{
  408. ptr+= stride;
  409. CHECK_SAD_HALF_MV(xy2, +1, +1)
  410. }
  411. CHECK_SAD_HALF_MV(x2 , +1, 0)
  412. }
  413. }else{
  414. if(l<=r){
  415. if(t+l<=b+r){
  416. CHECK_SAD_HALF_MV(xy2, -1, -1)
  417. ptr+= stride;
  418. }else{
  419. ptr+= stride;
  420. CHECK_SAD_HALF_MV(xy2, +1, +1)
  421. }
  422. CHECK_SAD_HALF_MV(x2 , -1, 0)
  423. CHECK_SAD_HALF_MV(xy2, -1, +1)
  424. }else{
  425. if(t+r<=b+l){
  426. CHECK_SAD_HALF_MV(xy2, +1, -1)
  427. ptr+= stride;
  428. }else{
  429. ptr+= stride;
  430. CHECK_SAD_HALF_MV(xy2, -1, +1)
  431. }
  432. CHECK_SAD_HALF_MV(x2 , +1, 0)
  433. CHECK_SAD_HALF_MV(xy2, +1, +1)
  434. }
  435. CHECK_SAD_HALF_MV(y2 , 0, +1)
  436. }
  437. mx+=dx;
  438. my+=dy;
  439. }else{
  440. mx<<=1;
  441. my<<=1;
  442. }
  443. *mx_ptr = mx;
  444. *my_ptr = my;
  445. return dminh;
  446. }
  447. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
  448. {
  449. const int xy= s->mb_x + s->mb_y*s->mb_stride;
  450. s->p_mv_table[xy][0] = mx;
  451. s->p_mv_table[xy][1] = my;
  452. /* has already been set to the 4 MV if 4MV is done */
  453. if(mv4){
  454. int mot_xy= s->block_index[0];
  455. s->current_picture.f.motion_val[0][mot_xy ][0] = mx;
  456. s->current_picture.f.motion_val[0][mot_xy ][1] = my;
  457. s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
  458. s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
  459. mot_xy += s->b8_stride;
  460. s->current_picture.f.motion_val[0][mot_xy ][0] = mx;
  461. s->current_picture.f.motion_val[0][mot_xy ][1] = my;
  462. s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
  463. s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
  464. }
  465. }
  466. /**
  467. * get fullpel ME search limits.
  468. */
  469. static inline void get_limits(MpegEncContext *s, int x, int y)
  470. {
  471. MotionEstContext * const c= &s->me;
  472. int range= c->avctx->me_range >> (1 + !!(c->flags&FLAG_QPEL));
  473. /*
  474. if(c->avctx->me_range) c->range= c->avctx->me_range >> 1;
  475. else c->range= 16;
  476. */
  477. if (s->unrestricted_mv) {
  478. c->xmin = - x - 16;
  479. c->ymin = - y - 16;
  480. c->xmax = - x + s->mb_width *16;
  481. c->ymax = - y + s->mb_height*16;
  482. } else if (s->out_format == FMT_H261){
  483. // Search range of H261 is different from other codec standards
  484. c->xmin = (x > 15) ? - 15 : 0;
  485. c->ymin = (y > 15) ? - 15 : 0;
  486. c->xmax = (x < s->mb_width * 16 - 16) ? 15 : 0;
  487. c->ymax = (y < s->mb_height * 16 - 16) ? 15 : 0;
  488. } else {
  489. c->xmin = - x;
  490. c->ymin = - y;
  491. c->xmax = - x + s->mb_width *16 - 16;
  492. c->ymax = - y + s->mb_height*16 - 16;
  493. }
  494. if(range){
  495. c->xmin = FFMAX(c->xmin,-range);
  496. c->xmax = FFMIN(c->xmax, range);
  497. c->ymin = FFMAX(c->ymin,-range);
  498. c->ymax = FFMIN(c->ymax, range);
  499. }
  500. }
  501. static inline void init_mv4_ref(MotionEstContext *c){
  502. const int stride= c->stride;
  503. c->ref[1][0] = c->ref[0][0] + 8;
  504. c->ref[2][0] = c->ref[0][0] + 8*stride;
  505. c->ref[3][0] = c->ref[2][0] + 8;
  506. c->src[1][0] = c->src[0][0] + 8;
  507. c->src[2][0] = c->src[0][0] + 8*stride;
  508. c->src[3][0] = c->src[2][0] + 8;
  509. }
  510. static inline int h263_mv4_search(MpegEncContext *s, int mx, int my, int shift)
  511. {
  512. MotionEstContext * const c= &s->me;
  513. const int size= 1;
  514. const int h=8;
  515. int block;
  516. int P[10][2];
  517. int dmin_sum=0, mx4_sum=0, my4_sum=0;
  518. int same=1;
  519. const int stride= c->stride;
  520. uint8_t *mv_penalty= c->current_mv_penalty;
  521. init_mv4_ref(c);
  522. for(block=0; block<4; block++){
  523. int mx4, my4;
  524. int pred_x4, pred_y4;
  525. int dmin4;
  526. static const int off[4]= {2, 1, 1, -1};
  527. const int mot_stride = s->b8_stride;
  528. const int mot_xy = s->block_index[block];
  529. P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
  530. P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
  531. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  532. /* special case for first line */
  533. if (s->first_slice_line && block<2) {
  534. c->pred_x= pred_x4= P_LEFT[0];
  535. c->pred_y= pred_y4= P_LEFT[1];
  536. } else {
  537. P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
  538. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  539. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
  540. P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][1];
  541. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  542. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  543. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  544. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  545. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  546. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  547. c->pred_x= pred_x4 = P_MEDIAN[0];
  548. c->pred_y= pred_y4 = P_MEDIAN[1];
  549. }
  550. P_MV1[0]= mx;
  551. P_MV1[1]= my;
  552. dmin4 = epzs_motion_search4(s, &mx4, &my4, P, block, block, s->p_mv_table, (1<<16)>>shift);
  553. dmin4= c->sub_motion_search(s, &mx4, &my4, dmin4, block, block, size, h);
  554. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  555. int dxy;
  556. const int offset= ((block&1) + (block>>1)*stride)*8;
  557. uint8_t *dest_y = c->scratchpad + offset;
  558. if(s->quarter_sample){
  559. uint8_t *ref= c->ref[block][0] + (mx4>>2) + (my4>>2)*stride;
  560. dxy = ((my4 & 3) << 2) | (mx4 & 3);
  561. if(s->no_rounding)
  562. s->dsp.put_no_rnd_qpel_pixels_tab[1][dxy](dest_y , ref , stride);
  563. else
  564. s->dsp.put_qpel_pixels_tab [1][dxy](dest_y , ref , stride);
  565. }else{
  566. uint8_t *ref= c->ref[block][0] + (mx4>>1) + (my4>>1)*stride;
  567. dxy = ((my4 & 1) << 1) | (mx4 & 1);
  568. if(s->no_rounding)
  569. s->dsp.put_no_rnd_pixels_tab[1][dxy](dest_y , ref , stride, h);
  570. else
  571. s->dsp.put_pixels_tab [1][dxy](dest_y , ref , stride, h);
  572. }
  573. dmin_sum+= (mv_penalty[mx4-pred_x4] + mv_penalty[my4-pred_y4])*c->mb_penalty_factor;
  574. }else
  575. dmin_sum+= dmin4;
  576. if(s->quarter_sample){
  577. mx4_sum+= mx4/2;
  578. my4_sum+= my4/2;
  579. }else{
  580. mx4_sum+= mx4;
  581. my4_sum+= my4;
  582. }
  583. s->current_picture.f.motion_val[0][s->block_index[block]][0] = mx4;
  584. s->current_picture.f.motion_val[0][s->block_index[block]][1] = my4;
  585. if(mx4 != mx || my4 != my) same=0;
  586. }
  587. if(same)
  588. return INT_MAX;
  589. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  590. 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);
  591. }
  592. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  593. int dxy;
  594. int mx, my;
  595. int offset;
  596. mx= ff_h263_round_chroma(mx4_sum);
  597. my= ff_h263_round_chroma(my4_sum);
  598. dxy = ((my & 1) << 1) | (mx & 1);
  599. offset= (s->mb_x*8 + (mx>>1)) + (s->mb_y*8 + (my>>1))*s->uvlinesize;
  600. if(s->no_rounding){
  601. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
  602. s->dsp.put_no_rnd_pixels_tab[1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
  603. }else{
  604. s->dsp.put_pixels_tab [1][dxy](c->scratchpad , s->last_picture.f.data[1] + offset, s->uvlinesize, 8);
  605. s->dsp.put_pixels_tab [1][dxy](c->scratchpad + 8, s->last_picture.f.data[2] + offset, s->uvlinesize, 8);
  606. }
  607. 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);
  608. 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);
  609. }
  610. c->pred_x= mx;
  611. c->pred_y= my;
  612. switch(c->avctx->mb_cmp&0xFF){
  613. /*case FF_CMP_SSE:
  614. return dmin_sum+ 32*s->qscale*s->qscale;*/
  615. case FF_CMP_RD:
  616. return dmin_sum;
  617. default:
  618. return dmin_sum+ 11*c->mb_penalty_factor;
  619. }
  620. }
  621. static inline void init_interlaced_ref(MpegEncContext *s, int ref_index){
  622. MotionEstContext * const c= &s->me;
  623. c->ref[1+ref_index][0] = c->ref[0+ref_index][0] + s->linesize;
  624. c->src[1][0] = c->src[0][0] + s->linesize;
  625. if(c->flags & FLAG_CHROMA){
  626. c->ref[1+ref_index][1] = c->ref[0+ref_index][1] + s->uvlinesize;
  627. c->ref[1+ref_index][2] = c->ref[0+ref_index][2] + s->uvlinesize;
  628. c->src[1][1] = c->src[0][1] + s->uvlinesize;
  629. c->src[1][2] = c->src[0][2] + s->uvlinesize;
  630. }
  631. }
  632. static int interlaced_search(MpegEncContext *s, int ref_index,
  633. int16_t (*mv_tables[2][2])[2], uint8_t *field_select_tables[2], int mx, int my, int user_field_select)
  634. {
  635. MotionEstContext * const c= &s->me;
  636. const int size=0;
  637. const int h=8;
  638. int block;
  639. int P[10][2];
  640. uint8_t * const mv_penalty= c->current_mv_penalty;
  641. int same=1;
  642. const int stride= 2*s->linesize;
  643. int dmin_sum= 0;
  644. const int mot_stride= s->mb_stride;
  645. const int xy= s->mb_x + s->mb_y*mot_stride;
  646. c->ymin>>=1;
  647. c->ymax>>=1;
  648. c->stride<<=1;
  649. c->uvstride<<=1;
  650. init_interlaced_ref(s, ref_index);
  651. for(block=0; block<2; block++){
  652. int field_select;
  653. int best_dmin= INT_MAX;
  654. int best_field= -1;
  655. for(field_select=0; field_select<2; field_select++){
  656. int dmin, mx_i, my_i;
  657. int16_t (*mv_table)[2]= mv_tables[block][field_select];
  658. if(user_field_select){
  659. assert(field_select==0 || field_select==1);
  660. assert(field_select_tables[block][xy]==0 || field_select_tables[block][xy]==1);
  661. if(field_select_tables[block][xy] != field_select)
  662. continue;
  663. }
  664. P_LEFT[0] = mv_table[xy - 1][0];
  665. P_LEFT[1] = mv_table[xy - 1][1];
  666. if(P_LEFT[0] > (c->xmax<<1)) P_LEFT[0] = (c->xmax<<1);
  667. c->pred_x= P_LEFT[0];
  668. c->pred_y= P_LEFT[1];
  669. if(!s->first_slice_line){
  670. P_TOP[0] = mv_table[xy - mot_stride][0];
  671. P_TOP[1] = mv_table[xy - mot_stride][1];
  672. P_TOPRIGHT[0] = mv_table[xy - mot_stride + 1][0];
  673. P_TOPRIGHT[1] = mv_table[xy - mot_stride + 1][1];
  674. if(P_TOP[1] > (c->ymax<<1)) P_TOP[1] = (c->ymax<<1);
  675. if(P_TOPRIGHT[0] < (c->xmin<<1)) P_TOPRIGHT[0]= (c->xmin<<1);
  676. if(P_TOPRIGHT[0] > (c->xmax<<1)) P_TOPRIGHT[0]= (c->xmax<<1);
  677. if(P_TOPRIGHT[1] > (c->ymax<<1)) P_TOPRIGHT[1]= (c->ymax<<1);
  678. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  679. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  680. }
  681. P_MV1[0]= mx; //FIXME not correct if block != field_select
  682. P_MV1[1]= my / 2;
  683. dmin = epzs_motion_search2(s, &mx_i, &my_i, P, block, field_select+ref_index, mv_table, (1<<16)>>1);
  684. dmin= c->sub_motion_search(s, &mx_i, &my_i, dmin, block, field_select+ref_index, size, h);
  685. mv_table[xy][0]= mx_i;
  686. mv_table[xy][1]= my_i;
  687. if(s->dsp.me_sub_cmp[0] != s->dsp.mb_cmp[0]){
  688. int dxy;
  689. //FIXME chroma ME
  690. uint8_t *ref= c->ref[field_select+ref_index][0] + (mx_i>>1) + (my_i>>1)*stride;
  691. dxy = ((my_i & 1) << 1) | (mx_i & 1);
  692. if(s->no_rounding){
  693. s->dsp.put_no_rnd_pixels_tab[size][dxy](c->scratchpad, ref , stride, h);
  694. }else{
  695. s->dsp.put_pixels_tab [size][dxy](c->scratchpad, ref , stride, h);
  696. }
  697. dmin= s->dsp.mb_cmp[size](s, c->src[block][0], c->scratchpad, stride, h);
  698. dmin+= (mv_penalty[mx_i-c->pred_x] + mv_penalty[my_i-c->pred_y] + 1)*c->mb_penalty_factor;
  699. }else
  700. dmin+= c->mb_penalty_factor; //field_select bits
  701. dmin += field_select != block; //slightly prefer same field
  702. if(dmin < best_dmin){
  703. best_dmin= dmin;
  704. best_field= field_select;
  705. }
  706. }
  707. {
  708. int16_t (*mv_table)[2]= mv_tables[block][best_field];
  709. if(mv_table[xy][0] != mx) same=0; //FIXME check if these checks work and are any good at all
  710. if(mv_table[xy][1]&1) same=0;
  711. if(mv_table[xy][1]*2 != my) same=0;
  712. if(best_field != block) same=0;
  713. }
  714. field_select_tables[block][xy]= best_field;
  715. dmin_sum += best_dmin;
  716. }
  717. c->ymin<<=1;
  718. c->ymax<<=1;
  719. c->stride>>=1;
  720. c->uvstride>>=1;
  721. if(same)
  722. return INT_MAX;
  723. switch(c->avctx->mb_cmp&0xFF){
  724. /*case FF_CMP_SSE:
  725. return dmin_sum+ 32*s->qscale*s->qscale;*/
  726. case FF_CMP_RD:
  727. return dmin_sum;
  728. default:
  729. return dmin_sum+ 11*c->mb_penalty_factor;
  730. }
  731. }
  732. static void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  733. int ymax= s->me.ymax>>interlaced;
  734. int ymin= s->me.ymin>>interlaced;
  735. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  736. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  737. if(mv[1] < ymin) mv[1] = ymin;
  738. if(mv[1] > ymax) mv[1] = ymax;
  739. }
  740. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  741. MotionEstContext * const c= &s->me;
  742. Picture *p= s->current_picture_ptr;
  743. int mb_xy= mb_x + mb_y*s->mb_stride;
  744. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  745. int mb_type= s->current_picture.f.mb_type[mb_xy];
  746. int flags= c->flags;
  747. int shift= (flags&FLAG_QPEL) + 1;
  748. int mask= (1<<shift)-1;
  749. int x, y, i;
  750. int d=0;
  751. me_cmp_func cmpf= s->dsp.sse[0];
  752. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  753. if(p_type && USES_LIST(mb_type, 1)){
  754. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  755. return INT_MAX/2;
  756. }
  757. assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  758. for(i=0; i<4; i++){
  759. int xy= s->block_index[i];
  760. clip_input_mv(s, p->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
  761. clip_input_mv(s, p->f.motion_val[1][xy], !!IS_INTERLACED(mb_type));
  762. }
  763. if(IS_INTERLACED(mb_type)){
  764. int xy2= xy + s->b8_stride;
  765. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  766. c->stride<<=1;
  767. c->uvstride<<=1;
  768. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  769. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  770. return INT_MAX/2;
  771. }
  772. if(USES_LIST(mb_type, 0)){
  773. int field_select0= p->f.ref_index[0][4*mb_xy ];
  774. int field_select1= p->f.ref_index[0][4*mb_xy+2];
  775. assert(field_select0==0 ||field_select0==1);
  776. assert(field_select1==0 ||field_select1==1);
  777. init_interlaced_ref(s, 0);
  778. if(p_type){
  779. s->p_field_select_table[0][mb_xy]= field_select0;
  780. s->p_field_select_table[1][mb_xy]= field_select1;
  781. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  782. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  783. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  784. }else{
  785. s->b_field_select_table[0][0][mb_xy]= field_select0;
  786. s->b_field_select_table[0][1][mb_xy]= field_select1;
  787. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  788. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  789. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  790. }
  791. x = p->f.motion_val[0][xy ][0];
  792. y = p->f.motion_val[0][xy ][1];
  793. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  794. x = p->f.motion_val[0][xy2][0];
  795. y = p->f.motion_val[0][xy2][1];
  796. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  797. }
  798. if(USES_LIST(mb_type, 1)){
  799. int field_select0 = p->f.ref_index[1][4 * mb_xy ];
  800. int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
  801. assert(field_select0==0 ||field_select0==1);
  802. assert(field_select1==0 ||field_select1==1);
  803. init_interlaced_ref(s, 2);
  804. s->b_field_select_table[1][0][mb_xy]= field_select0;
  805. s->b_field_select_table[1][1][mb_xy]= field_select1;
  806. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy ];
  807. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy2];
  808. if(USES_LIST(mb_type, 0)){
  809. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  810. }else{
  811. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  812. }
  813. x = p->f.motion_val[1][xy ][0];
  814. y = p->f.motion_val[1][xy ][1];
  815. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  816. x = p->f.motion_val[1][xy2][0];
  817. y = p->f.motion_val[1][xy2][1];
  818. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  819. //FIXME bidir scores
  820. }
  821. c->stride>>=1;
  822. c->uvstride>>=1;
  823. }else if(IS_8X8(mb_type)){
  824. if(!(s->flags & CODEC_FLAG_4MV)){
  825. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  826. return INT_MAX/2;
  827. }
  828. cmpf= s->dsp.sse[1];
  829. chroma_cmpf= s->dsp.sse[1];
  830. init_mv4_ref(c);
  831. for(i=0; i<4; i++){
  832. xy= s->block_index[i];
  833. x= p->f.motion_val[0][xy][0];
  834. y= p->f.motion_val[0][xy][1];
  835. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  836. }
  837. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  838. }else{
  839. if(USES_LIST(mb_type, 0)){
  840. if(p_type){
  841. *(uint32_t*)s->p_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  842. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  843. }else if(USES_LIST(mb_type, 1)){
  844. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  845. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  846. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  847. }else{
  848. *(uint32_t*)s->b_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  849. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  850. }
  851. x = p->f.motion_val[0][xy][0];
  852. y = p->f.motion_val[0][xy][1];
  853. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  854. }else if(USES_LIST(mb_type, 1)){
  855. *(uint32_t*)s->b_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  856. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  857. x = p->f.motion_val[1][xy][0];
  858. y = p->f.motion_val[1][xy][1];
  859. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  860. }else
  861. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  862. }
  863. return d;
  864. }
  865. void ff_estimate_p_frame_motion(MpegEncContext * s,
  866. int mb_x, int mb_y)
  867. {
  868. MotionEstContext * const c= &s->me;
  869. uint8_t *pix, *ppix;
  870. int sum, mx, my, dmin;
  871. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  872. int vard; ///< sum of squared differences with the estimated motion vector
  873. int P[10][2];
  874. const int shift= 1+s->quarter_sample;
  875. int mb_type=0;
  876. Picture * const pic= &s->current_picture;
  877. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  878. assert(s->quarter_sample==0 || s->quarter_sample==1);
  879. assert(s->linesize == c->stride);
  880. assert(s->uvlinesize == c->uvstride);
  881. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  882. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  883. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  884. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  885. get_limits(s, 16*mb_x, 16*mb_y);
  886. c->skip=0;
  887. /* intra / predictive decision */
  888. pix = c->src[0][0];
  889. sum = s->dsp.pix_sum(pix, s->linesize);
  890. varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500;
  891. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  892. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  893. c->mb_var_sum_temp += (varc+128)>>8;
  894. if(c->avctx->me_threshold){
  895. vard= check_input_motion(s, mb_x, mb_y, 1);
  896. if((vard+128)>>8 < c->avctx->me_threshold){
  897. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  898. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  899. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  900. c->mc_mb_var_sum_temp += (vard+128)>>8;
  901. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  902. return;
  903. }
  904. if((vard+128)>>8 < c->avctx->mb_threshold)
  905. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  906. }
  907. switch(s->me_method) {
  908. case ME_ZERO:
  909. default:
  910. no_motion_search(s, &mx, &my);
  911. mx-= mb_x*16;
  912. my-= mb_y*16;
  913. dmin = 0;
  914. break;
  915. case ME_X1:
  916. case ME_EPZS:
  917. {
  918. const int mot_stride = s->b8_stride;
  919. const int mot_xy = s->block_index[0];
  920. P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
  921. P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
  922. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  923. if(!s->first_slice_line) {
  924. P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
  925. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  926. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
  927. P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
  928. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  929. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  930. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  931. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  932. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  933. if(s->out_format == FMT_H263){
  934. c->pred_x = P_MEDIAN[0];
  935. c->pred_y = P_MEDIAN[1];
  936. }else { /* mpeg1 at least */
  937. c->pred_x= P_LEFT[0];
  938. c->pred_y= P_LEFT[1];
  939. }
  940. }else{
  941. c->pred_x= P_LEFT[0];
  942. c->pred_y= P_LEFT[1];
  943. }
  944. }
  945. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  946. break;
  947. }
  948. /* At this point (mx,my) are full-pell and the relative displacement */
  949. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  950. vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
  951. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  952. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  953. c->mc_mb_var_sum_temp += (vard+128)>>8;
  954. if(mb_type){
  955. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  956. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  957. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  958. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  959. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  960. set_p_mv_tables(s, mx, my, 1);
  961. }else{
  962. mx <<=shift;
  963. my <<=shift;
  964. }
  965. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  966. h263_mv4_search(s, mx, my, shift);
  967. set_p_mv_tables(s, mx, my, 0);
  968. }
  969. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  970. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  971. }
  972. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  973. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  974. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  975. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  976. if (vard*2 + 200*256 > varc)
  977. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  978. if (varc*2 + 200*256 > vard || s->qscale > 24){
  979. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  980. mb_type|= CANDIDATE_MB_TYPE_INTER;
  981. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  982. if(s->flags&CODEC_FLAG_MV0)
  983. if(mx || my)
  984. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  985. }else{
  986. mx <<=shift;
  987. my <<=shift;
  988. }
  989. if((s->flags&CODEC_FLAG_4MV)
  990. && !c->skip && varc>50<<8 && vard>10<<8){
  991. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  992. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  993. set_p_mv_tables(s, mx, my, 0);
  994. }else
  995. set_p_mv_tables(s, mx, my, 1);
  996. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  997. && !c->skip){ //FIXME varc/d checks
  998. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  999. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  1000. }
  1001. }else{
  1002. int intra_score, i;
  1003. mb_type= CANDIDATE_MB_TYPE_INTER;
  1004. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1005. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1006. dmin= ff_get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1007. if((s->flags&CODEC_FLAG_4MV)
  1008. && !c->skip && varc>50<<8 && vard>10<<8){
  1009. int dmin4= h263_mv4_search(s, mx, my, shift);
  1010. if(dmin4 < dmin){
  1011. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  1012. dmin=dmin4;
  1013. }
  1014. }
  1015. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1016. && !c->skip){ //FIXME varc/d checks
  1017. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  1018. if(dmin_i < dmin){
  1019. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  1020. dmin= dmin_i;
  1021. }
  1022. }
  1023. // pic->mb_cmp_score[s->mb_stride * mb_y + mb_x] = dmin;
  1024. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  1025. /* get intra luma score */
  1026. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  1027. intra_score= varc - 500;
  1028. }else{
  1029. unsigned mean = (sum+128)>>8;
  1030. mean*= 0x01010101;
  1031. for(i=0; i<16; i++){
  1032. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1033. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1034. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1035. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1036. }
  1037. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1038. }
  1039. intra_score += c->mb_penalty_factor*16;
  1040. if(intra_score < dmin){
  1041. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1042. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1043. }else
  1044. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
  1045. {
  1046. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1047. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1048. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1049. }
  1050. }
  1051. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1052. }
  1053. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1054. int mb_x, int mb_y)
  1055. {
  1056. MotionEstContext * const c= &s->me;
  1057. int mx, my, dmin;
  1058. int P[10][2];
  1059. const int shift= 1+s->quarter_sample;
  1060. const int xy= mb_x + mb_y*s->mb_stride;
  1061. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  1062. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1063. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1064. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1065. get_limits(s, 16*mb_x, 16*mb_y);
  1066. c->skip=0;
  1067. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1068. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1069. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1070. /* special case for first line */
  1071. if (s->first_slice_line) {
  1072. c->pred_x= P_LEFT[0];
  1073. c->pred_y= P_LEFT[1];
  1074. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1075. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1076. } else {
  1077. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1078. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1079. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1080. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1081. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1082. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1083. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1084. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1085. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1086. c->pred_x = P_MEDIAN[0];
  1087. c->pred_y = P_MEDIAN[1];
  1088. }
  1089. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1090. s->p_mv_table[xy][0] = mx<<shift;
  1091. s->p_mv_table[xy][1] = my<<shift;
  1092. return dmin;
  1093. }
  1094. static int ff_estimate_motion_b(MpegEncContext * s,
  1095. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1096. {
  1097. MotionEstContext * const c= &s->me;
  1098. int mx, my, dmin;
  1099. int P[10][2];
  1100. const int shift= 1+s->quarter_sample;
  1101. const int mot_stride = s->mb_stride;
  1102. const int mot_xy = mb_y*mot_stride + mb_x;
  1103. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1104. int mv_scale;
  1105. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1106. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1107. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1108. c->current_mv_penalty= mv_penalty;
  1109. get_limits(s, 16*mb_x, 16*mb_y);
  1110. switch(s->me_method) {
  1111. case ME_ZERO:
  1112. default:
  1113. no_motion_search(s, &mx, &my);
  1114. dmin = 0;
  1115. mx-= mb_x*16;
  1116. my-= mb_y*16;
  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 == 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 == 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 == 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 == 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. assert(s->pict_type==AV_PICTURE_TYPE_P);
  1616. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1617. assert(range <= 16 || !s->msmpeg4_version);
  1618. assert(range <=256 || !(s->codec_id == 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. }