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.

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