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.

1745 lines
67KB

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