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.

1936 lines
76KB

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