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.

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