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.

1737 lines
66KB

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