You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1937 lines
76KB

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