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.

1953 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 "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.f.motion_val[0][mot_xy ][0] = mx;
  443. s->current_picture.f.motion_val[0][mot_xy ][1] = my;
  444. s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
  445. s->current_picture.f.motion_val[0][mot_xy + 1][1] = my;
  446. mot_xy += s->b8_stride;
  447. s->current_picture.f.motion_val[0][mot_xy ][0] = mx;
  448. s->current_picture.f.motion_val[0][mot_xy ][1] = my;
  449. s->current_picture.f.motion_val[0][mot_xy + 1][0] = mx;
  450. s->current_picture.f.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.f.motion_val[0][mot_xy - 1][0];
  517. P_LEFT[1] = s->current_picture.f.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.f.motion_val[0][mot_xy - mot_stride ][0];
  525. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  526. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + off[block]][0];
  527. P_TOPRIGHT[1] = s->current_picture.f.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.f.motion_val[0][s->block_index[block]][0] = mx4;
  571. s->current_picture.f.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 void clip_input_mv(MpegEncContext * s, int16_t *mv, int interlaced){
  720. int ymax= s->me.ymax>>interlaced;
  721. int ymin= s->me.ymin>>interlaced;
  722. if(mv[0] < s->me.xmin) mv[0] = s->me.xmin;
  723. if(mv[0] > s->me.xmax) mv[0] = s->me.xmax;
  724. if(mv[1] < ymin) mv[1] = ymin;
  725. if(mv[1] > ymax) mv[1] = ymax;
  726. }
  727. static inline int check_input_motion(MpegEncContext * s, int mb_x, int mb_y, int p_type){
  728. MotionEstContext * const c= &s->me;
  729. Picture *p= s->current_picture_ptr;
  730. int mb_xy= mb_x + mb_y*s->mb_stride;
  731. int xy= 2*mb_x + 2*mb_y*s->b8_stride;
  732. int mb_type= s->current_picture.f.mb_type[mb_xy];
  733. int flags= c->flags;
  734. int shift= (flags&FLAG_QPEL) + 1;
  735. int mask= (1<<shift)-1;
  736. int x, y, i;
  737. int d=0;
  738. me_cmp_func cmpf= s->dsp.sse[0];
  739. me_cmp_func chroma_cmpf= s->dsp.sse[1];
  740. if(p_type && USES_LIST(mb_type, 1)){
  741. av_log(c->avctx, AV_LOG_ERROR, "backward motion vector in P frame\n");
  742. return INT_MAX/2;
  743. }
  744. assert(IS_INTRA(mb_type) || USES_LIST(mb_type,0) || USES_LIST(mb_type,1));
  745. for(i=0; i<4; i++){
  746. int xy= s->block_index[i];
  747. clip_input_mv(s, p->f.motion_val[0][xy], !!IS_INTERLACED(mb_type));
  748. clip_input_mv(s, p->f.motion_val[1][xy], !!IS_INTERLACED(mb_type));
  749. }
  750. if(IS_INTERLACED(mb_type)){
  751. int xy2= xy + s->b8_stride;
  752. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  753. c->stride<<=1;
  754. c->uvstride<<=1;
  755. if(!(s->flags & CODEC_FLAG_INTERLACED_ME)){
  756. av_log(c->avctx, AV_LOG_ERROR, "Interlaced macroblock selected but interlaced motion estimation disabled\n");
  757. return INT_MAX/2;
  758. }
  759. if(USES_LIST(mb_type, 0)){
  760. int field_select0= p->f.ref_index[0][4*mb_xy ];
  761. int field_select1= p->f.ref_index[0][4*mb_xy+2];
  762. assert(field_select0==0 ||field_select0==1);
  763. assert(field_select1==0 ||field_select1==1);
  764. init_interlaced_ref(s, 0);
  765. if(p_type){
  766. s->p_field_select_table[0][mb_xy]= field_select0;
  767. s->p_field_select_table[1][mb_xy]= field_select1;
  768. *(uint32_t*)s->p_field_mv_table[0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  769. *(uint32_t*)s->p_field_mv_table[1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  770. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER_I;
  771. }else{
  772. s->b_field_select_table[0][0][mb_xy]= field_select0;
  773. s->b_field_select_table[0][1][mb_xy]= field_select1;
  774. *(uint32_t*)s->b_field_mv_table[0][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy ];
  775. *(uint32_t*)s->b_field_mv_table[0][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[0][xy2];
  776. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_FORWARD_I;
  777. }
  778. x = p->f.motion_val[0][xy ][0];
  779. y = p->f.motion_val[0][xy ][1];
  780. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0, 0, cmpf, chroma_cmpf, flags);
  781. x = p->f.motion_val[0][xy2][0];
  782. y = p->f.motion_val[0][xy2][1];
  783. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1, 1, cmpf, chroma_cmpf, flags);
  784. }
  785. if(USES_LIST(mb_type, 1)){
  786. int field_select0 = p->f.ref_index[1][4 * mb_xy ];
  787. int field_select1 = p->f.ref_index[1][4 * mb_xy + 2];
  788. assert(field_select0==0 ||field_select0==1);
  789. assert(field_select1==0 ||field_select1==1);
  790. init_interlaced_ref(s, 2);
  791. s->b_field_select_table[1][0][mb_xy]= field_select0;
  792. s->b_field_select_table[1][1][mb_xy]= field_select1;
  793. *(uint32_t*)s->b_field_mv_table[1][0][field_select0][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy ];
  794. *(uint32_t*)s->b_field_mv_table[1][1][field_select1][mb_xy] = *(uint32_t*)p->f.motion_val[1][xy2];
  795. if(USES_LIST(mb_type, 0)){
  796. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BIDIR_I;
  797. }else{
  798. s->mb_type[mb_xy]= CANDIDATE_MB_TYPE_BACKWARD_I;
  799. }
  800. x = p->f.motion_val[1][xy ][0];
  801. y = p->f.motion_val[1][xy ][1];
  802. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select0+2, 0, cmpf, chroma_cmpf, flags);
  803. x = p->f.motion_val[1][xy2][0];
  804. y = p->f.motion_val[1][xy2][1];
  805. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 8, field_select1+2, 1, cmpf, chroma_cmpf, flags);
  806. //FIXME bidir scores
  807. }
  808. c->stride>>=1;
  809. c->uvstride>>=1;
  810. }else if(IS_8X8(mb_type)){
  811. if(!(s->flags & CODEC_FLAG_4MV)){
  812. av_log(c->avctx, AV_LOG_ERROR, "4MV macroblock selected but 4MV encoding disabled\n");
  813. return INT_MAX/2;
  814. }
  815. cmpf= s->dsp.sse[1];
  816. chroma_cmpf= s->dsp.sse[1];
  817. init_mv4_ref(c);
  818. for(i=0; i<4; i++){
  819. xy= s->block_index[i];
  820. x= p->f.motion_val[0][xy][0];
  821. y= p->f.motion_val[0][xy][1];
  822. d+= cmp(s, x>>shift, y>>shift, x&mask, y&mask, 1, 8, i, i, cmpf, chroma_cmpf, flags);
  823. }
  824. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER4V;
  825. }else{
  826. if(USES_LIST(mb_type, 0)){
  827. if(p_type){
  828. *(uint32_t*)s->p_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  829. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTER;
  830. }else if(USES_LIST(mb_type, 1)){
  831. *(uint32_t*)s->b_bidir_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  832. *(uint32_t*)s->b_bidir_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  833. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BIDIR;
  834. }else{
  835. *(uint32_t*)s->b_forw_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[0][xy];
  836. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_FORWARD;
  837. }
  838. x = p->f.motion_val[0][xy][0];
  839. y = p->f.motion_val[0][xy][1];
  840. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 0, 0, cmpf, chroma_cmpf, flags);
  841. }else if(USES_LIST(mb_type, 1)){
  842. *(uint32_t*)s->b_back_mv_table[mb_xy] = *(uint32_t*)p->f.motion_val[1][xy];
  843. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_BACKWARD;
  844. x = p->f.motion_val[1][xy][0];
  845. y = p->f.motion_val[1][xy][1];
  846. d = cmp(s, x>>shift, y>>shift, x&mask, y&mask, 0, 16, 2, 0, cmpf, chroma_cmpf, flags);
  847. }else
  848. s->mb_type[mb_xy]=CANDIDATE_MB_TYPE_INTRA;
  849. }
  850. return d;
  851. }
  852. static inline int get_penalty_factor(int lambda, int lambda2, int type){
  853. switch(type&0xFF){
  854. default:
  855. case FF_CMP_SAD:
  856. return lambda>>FF_LAMBDA_SHIFT;
  857. case FF_CMP_DCT:
  858. return (3*lambda)>>(FF_LAMBDA_SHIFT+1);
  859. case FF_CMP_SATD:
  860. case FF_CMP_DCT264:
  861. return (2*lambda)>>FF_LAMBDA_SHIFT;
  862. case FF_CMP_RD:
  863. case FF_CMP_PSNR:
  864. case FF_CMP_SSE:
  865. case FF_CMP_NSSE:
  866. return lambda2>>FF_LAMBDA_SHIFT;
  867. case FF_CMP_BIT:
  868. return 1;
  869. }
  870. }
  871. void ff_estimate_p_frame_motion(MpegEncContext * s,
  872. int mb_x, int mb_y)
  873. {
  874. MotionEstContext * const c= &s->me;
  875. uint8_t *pix, *ppix;
  876. int sum, mx, my, dmin;
  877. int varc; ///< the variance of the block (sum of squared (p[y][x]-average))
  878. int vard; ///< sum of squared differences with the estimated motion vector
  879. int P[10][2];
  880. const int shift= 1+s->quarter_sample;
  881. int mb_type=0;
  882. Picture * const pic= &s->current_picture;
  883. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  884. assert(s->quarter_sample==0 || s->quarter_sample==1);
  885. assert(s->linesize == c->stride);
  886. assert(s->uvlinesize == c->uvstride);
  887. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  888. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  889. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  890. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  891. get_limits(s, 16*mb_x, 16*mb_y);
  892. c->skip=0;
  893. /* intra / predictive decision */
  894. pix = c->src[0][0];
  895. sum = s->dsp.pix_sum(pix, s->linesize);
  896. varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)sum*sum)>>8) + 500;
  897. pic->mb_mean[s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  898. pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  899. c->mb_var_sum_temp += (varc+128)>>8;
  900. if(c->avctx->me_threshold){
  901. vard= check_input_motion(s, mb_x, mb_y, 1);
  902. if((vard+128)>>8 < c->avctx->me_threshold){
  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. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  906. c->mc_mb_var_sum_temp += (vard+128)>>8;
  907. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  908. return;
  909. }
  910. if((vard+128)>>8 < c->avctx->mb_threshold)
  911. mb_type= s->mb_type[mb_x + mb_y*s->mb_stride];
  912. }
  913. switch(s->me_method) {
  914. case ME_ZERO:
  915. default:
  916. mx = 0;
  917. my = 0;
  918. dmin = 0;
  919. break;
  920. case ME_X1:
  921. case ME_EPZS:
  922. {
  923. const int mot_stride = s->b8_stride;
  924. const int mot_xy = s->block_index[0];
  925. P_LEFT[0] = s->current_picture.f.motion_val[0][mot_xy - 1][0];
  926. P_LEFT[1] = s->current_picture.f.motion_val[0][mot_xy - 1][1];
  927. if(P_LEFT[0] > (c->xmax<<shift)) P_LEFT[0] = (c->xmax<<shift);
  928. if(!s->first_slice_line) {
  929. P_TOP[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][0];
  930. P_TOP[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride ][1];
  931. P_TOPRIGHT[0] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][0];
  932. P_TOPRIGHT[1] = s->current_picture.f.motion_val[0][mot_xy - mot_stride + 2][1];
  933. if(P_TOP[1] > (c->ymax<<shift)) P_TOP[1] = (c->ymax<<shift);
  934. if(P_TOPRIGHT[0] < (c->xmin<<shift)) P_TOPRIGHT[0]= (c->xmin<<shift);
  935. if(P_TOPRIGHT[1] > (c->ymax<<shift)) P_TOPRIGHT[1]= (c->ymax<<shift);
  936. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  937. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  938. if(s->out_format == FMT_H263){
  939. c->pred_x = P_MEDIAN[0];
  940. c->pred_y = P_MEDIAN[1];
  941. }else { /* mpeg1 at least */
  942. c->pred_x= P_LEFT[0];
  943. c->pred_y= P_LEFT[1];
  944. }
  945. }else{
  946. c->pred_x= P_LEFT[0];
  947. c->pred_y= P_LEFT[1];
  948. }
  949. }
  950. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  951. break;
  952. }
  953. /* At this point (mx,my) are full-pell and the relative displacement */
  954. ppix = c->ref[0][0] + (my * s->linesize) + mx;
  955. vard = s->dsp.sse[0](NULL, pix, ppix, s->linesize, 16);
  956. pic->mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  957. c->mc_mb_var_sum_temp += (vard+128)>>8;
  958. if(mb_type){
  959. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  960. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  961. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  962. if(mb_type == CANDIDATE_MB_TYPE_INTER){
  963. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  964. set_p_mv_tables(s, mx, my, 1);
  965. }else{
  966. mx <<=shift;
  967. my <<=shift;
  968. }
  969. if(mb_type == CANDIDATE_MB_TYPE_INTER4V){
  970. h263_mv4_search(s, mx, my, shift);
  971. set_p_mv_tables(s, mx, my, 0);
  972. }
  973. if(mb_type == CANDIDATE_MB_TYPE_INTER_I){
  974. interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 1);
  975. }
  976. }else if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  977. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  978. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  979. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  980. if (vard*2 + 200*256 > varc)
  981. mb_type|= CANDIDATE_MB_TYPE_INTRA;
  982. if (varc*2 + 200*256 > vard || s->qscale > 24){
  983. // if (varc*2 + 200*256 + 50*(s->lambda2>>FF_LAMBDA_SHIFT) > vard){
  984. mb_type|= CANDIDATE_MB_TYPE_INTER;
  985. c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  986. if(s->flags&CODEC_FLAG_MV0)
  987. if(mx || my)
  988. mb_type |= CANDIDATE_MB_TYPE_SKIPPED; //FIXME check difference
  989. }else{
  990. mx <<=shift;
  991. my <<=shift;
  992. }
  993. if((s->flags&CODEC_FLAG_4MV)
  994. && !c->skip && varc>50<<8 && vard>10<<8){
  995. if(h263_mv4_search(s, mx, my, shift) < INT_MAX)
  996. mb_type|=CANDIDATE_MB_TYPE_INTER4V;
  997. set_p_mv_tables(s, mx, my, 0);
  998. }else
  999. set_p_mv_tables(s, mx, my, 1);
  1000. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1001. && !c->skip){ //FIXME varc/d checks
  1002. if(interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0) < INT_MAX)
  1003. mb_type |= CANDIDATE_MB_TYPE_INTER_I;
  1004. }
  1005. }else{
  1006. int intra_score, i;
  1007. mb_type= CANDIDATE_MB_TYPE_INTER;
  1008. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1009. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1010. dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1011. if((s->flags&CODEC_FLAG_4MV)
  1012. && !c->skip && varc>50<<8 && vard>10<<8){
  1013. int dmin4= h263_mv4_search(s, mx, my, shift);
  1014. if(dmin4 < dmin){
  1015. mb_type= CANDIDATE_MB_TYPE_INTER4V;
  1016. dmin=dmin4;
  1017. }
  1018. }
  1019. if((s->flags&CODEC_FLAG_INTERLACED_ME)
  1020. && !c->skip){ //FIXME varc/d checks
  1021. int dmin_i= interlaced_search(s, 0, s->p_field_mv_table, s->p_field_select_table, mx, my, 0);
  1022. if(dmin_i < dmin){
  1023. mb_type = CANDIDATE_MB_TYPE_INTER_I;
  1024. dmin= dmin_i;
  1025. }
  1026. }
  1027. set_p_mv_tables(s, mx, my, mb_type!=CANDIDATE_MB_TYPE_INTER4V);
  1028. /* get intra luma score */
  1029. if((c->avctx->mb_cmp&0xFF)==FF_CMP_SSE){
  1030. intra_score= varc - 500;
  1031. }else{
  1032. unsigned mean = (sum+128)>>8;
  1033. mean*= 0x01010101;
  1034. for(i=0; i<16; i++){
  1035. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 0]) = mean;
  1036. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 4]) = mean;
  1037. *(uint32_t*)(&c->scratchpad[i*s->linesize+ 8]) = mean;
  1038. *(uint32_t*)(&c->scratchpad[i*s->linesize+12]) = mean;
  1039. }
  1040. intra_score= s->dsp.mb_cmp[0](s, c->scratchpad, pix, s->linesize, 16);
  1041. }
  1042. intra_score += c->mb_penalty_factor*16;
  1043. if(intra_score < dmin){
  1044. mb_type= CANDIDATE_MB_TYPE_INTRA;
  1045. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = CANDIDATE_MB_TYPE_INTRA; //FIXME cleanup
  1046. }else
  1047. s->current_picture.f.mb_type[mb_y*s->mb_stride + mb_x] = 0;
  1048. {
  1049. int p_score= FFMIN(vard, varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*100);
  1050. int i_score= varc-500+(s->lambda2>>FF_LAMBDA_SHIFT)*20;
  1051. c->scene_change_score+= ff_sqrt(p_score) - ff_sqrt(i_score);
  1052. }
  1053. }
  1054. s->mb_type[mb_y*s->mb_stride + mb_x]= mb_type;
  1055. }
  1056. int ff_pre_estimate_p_frame_motion(MpegEncContext * s,
  1057. int mb_x, int mb_y)
  1058. {
  1059. MotionEstContext * const c= &s->me;
  1060. int mx, my, dmin;
  1061. int P[10][2];
  1062. const int shift= 1+s->quarter_sample;
  1063. const int xy= mb_x + mb_y*s->mb_stride;
  1064. init_ref(c, s->new_picture.f.data, s->last_picture.f.data, NULL, 16*mb_x, 16*mb_y, 0);
  1065. assert(s->quarter_sample==0 || s->quarter_sample==1);
  1066. c->pre_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_pre_cmp);
  1067. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1068. get_limits(s, 16*mb_x, 16*mb_y);
  1069. c->skip=0;
  1070. P_LEFT[0] = s->p_mv_table[xy + 1][0];
  1071. P_LEFT[1] = s->p_mv_table[xy + 1][1];
  1072. if(P_LEFT[0] < (c->xmin<<shift)) P_LEFT[0] = (c->xmin<<shift);
  1073. /* special case for first line */
  1074. if (s->first_slice_line) {
  1075. c->pred_x= P_LEFT[0];
  1076. c->pred_y= P_LEFT[1];
  1077. P_TOP[0]= P_TOPRIGHT[0]= P_MEDIAN[0]=
  1078. P_TOP[1]= P_TOPRIGHT[1]= P_MEDIAN[1]= 0; //FIXME
  1079. } else {
  1080. P_TOP[0] = s->p_mv_table[xy + s->mb_stride ][0];
  1081. P_TOP[1] = s->p_mv_table[xy + s->mb_stride ][1];
  1082. P_TOPRIGHT[0] = s->p_mv_table[xy + s->mb_stride - 1][0];
  1083. P_TOPRIGHT[1] = s->p_mv_table[xy + s->mb_stride - 1][1];
  1084. if(P_TOP[1] < (c->ymin<<shift)) P_TOP[1] = (c->ymin<<shift);
  1085. if(P_TOPRIGHT[0] > (c->xmax<<shift)) P_TOPRIGHT[0]= (c->xmax<<shift);
  1086. if(P_TOPRIGHT[1] < (c->ymin<<shift)) P_TOPRIGHT[1]= (c->ymin<<shift);
  1087. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1088. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1089. c->pred_x = P_MEDIAN[0];
  1090. c->pred_y = P_MEDIAN[1];
  1091. }
  1092. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, s->p_mv_table, (1<<16)>>shift, 0, 16);
  1093. s->p_mv_table[xy][0] = mx<<shift;
  1094. s->p_mv_table[xy][1] = my<<shift;
  1095. return dmin;
  1096. }
  1097. static int ff_estimate_motion_b(MpegEncContext * s,
  1098. int mb_x, int mb_y, int16_t (*mv_table)[2], int ref_index, int f_code)
  1099. {
  1100. MotionEstContext * const c= &s->me;
  1101. int mx, my, dmin;
  1102. int P[10][2];
  1103. const int shift= 1+s->quarter_sample;
  1104. const int mot_stride = s->mb_stride;
  1105. const int mot_xy = mb_y*mot_stride + mb_x;
  1106. uint8_t * const mv_penalty= c->mv_penalty[f_code] + MAX_MV;
  1107. int mv_scale;
  1108. c->penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_cmp);
  1109. c->sub_penalty_factor= get_penalty_factor(s->lambda, s->lambda2, c->avctx->me_sub_cmp);
  1110. c->mb_penalty_factor = get_penalty_factor(s->lambda, s->lambda2, c->avctx->mb_cmp);
  1111. c->current_mv_penalty= mv_penalty;
  1112. get_limits(s, 16*mb_x, 16*mb_y);
  1113. switch(s->me_method) {
  1114. case ME_ZERO:
  1115. default:
  1116. mx = 0;
  1117. my = 0;
  1118. dmin = 0;
  1119. break;
  1120. case ME_X1:
  1121. case ME_EPZS:
  1122. P_LEFT[0] = mv_table[mot_xy - 1][0];
  1123. P_LEFT[1] = mv_table[mot_xy - 1][1];
  1124. if (P_LEFT[0] > (c->xmax << shift)) P_LEFT[0] = (c->xmax << shift);
  1125. /* special case for first line */
  1126. if (!s->first_slice_line) {
  1127. P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
  1128. P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
  1129. P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1][0];
  1130. P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1][1];
  1131. if (P_TOP[1] > (c->ymax << shift)) P_TOP[1] = (c->ymax << shift);
  1132. if (P_TOPRIGHT[0] < (c->xmin << shift)) P_TOPRIGHT[0] = (c->xmin << shift);
  1133. if (P_TOPRIGHT[1] > (c->ymax << shift)) P_TOPRIGHT[1] = (c->ymax << shift);
  1134. P_MEDIAN[0] = mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1135. P_MEDIAN[1] = mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1136. }
  1137. c->pred_x = P_LEFT[0];
  1138. c->pred_y = P_LEFT[1];
  1139. if(mv_table == s->b_forw_mv_table){
  1140. mv_scale= (s->pb_time<<16) / (s->pp_time<<shift);
  1141. }else{
  1142. mv_scale= ((s->pb_time - s->pp_time)<<16) / (s->pp_time<<shift);
  1143. }
  1144. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, ref_index, s->p_mv_table, mv_scale, 0, 16);
  1145. break;
  1146. }
  1147. dmin= c->sub_motion_search(s, &mx, &my, dmin, 0, ref_index, 0, 16);
  1148. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1149. dmin= get_mb_score(s, mx, my, 0, ref_index, 0, 16, 1);
  1150. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  1151. mv_table[mot_xy][0]= mx;
  1152. mv_table[mot_xy][1]= my;
  1153. return dmin;
  1154. }
  1155. static inline int check_bidir_mv(MpegEncContext * s,
  1156. int motion_fx, int motion_fy,
  1157. int motion_bx, int motion_by,
  1158. int pred_fx, int pred_fy,
  1159. int pred_bx, int pred_by,
  1160. int size, int h)
  1161. {
  1162. //FIXME optimize?
  1163. //FIXME better f_code prediction (max mv & distance)
  1164. //FIXME pointers
  1165. MotionEstContext * const c= &s->me;
  1166. uint8_t * const mv_penalty_f= c->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1167. uint8_t * const mv_penalty_b= c->mv_penalty[s->b_code] + MAX_MV; // f_code of the prev frame
  1168. int stride= c->stride;
  1169. uint8_t *dest_y = c->scratchpad;
  1170. uint8_t *ptr;
  1171. int dxy;
  1172. int src_x, src_y;
  1173. int fbmin;
  1174. uint8_t **src_data= c->src[0];
  1175. uint8_t **ref_data= c->ref[0];
  1176. uint8_t **ref2_data= c->ref[2];
  1177. if(s->quarter_sample){
  1178. dxy = ((motion_fy & 3) << 2) | (motion_fx & 3);
  1179. src_x = motion_fx >> 2;
  1180. src_y = motion_fy >> 2;
  1181. ptr = ref_data[0] + (src_y * stride) + src_x;
  1182. s->dsp.put_qpel_pixels_tab[0][dxy](dest_y , ptr , stride);
  1183. dxy = ((motion_by & 3) << 2) | (motion_bx & 3);
  1184. src_x = motion_bx >> 2;
  1185. src_y = motion_by >> 2;
  1186. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1187. s->dsp.avg_qpel_pixels_tab[size][dxy](dest_y , ptr , stride);
  1188. }else{
  1189. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1190. src_x = motion_fx >> 1;
  1191. src_y = motion_fy >> 1;
  1192. ptr = ref_data[0] + (src_y * stride) + src_x;
  1193. s->dsp.put_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1194. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1195. src_x = motion_bx >> 1;
  1196. src_y = motion_by >> 1;
  1197. ptr = ref2_data[0] + (src_y * stride) + src_x;
  1198. s->dsp.avg_pixels_tab[size][dxy](dest_y , ptr , stride, h);
  1199. }
  1200. fbmin = (mv_penalty_f[motion_fx-pred_fx] + mv_penalty_f[motion_fy-pred_fy])*c->mb_penalty_factor
  1201. +(mv_penalty_b[motion_bx-pred_bx] + mv_penalty_b[motion_by-pred_by])*c->mb_penalty_factor
  1202. + s->dsp.mb_cmp[size](s, src_data[0], dest_y, stride, h); //FIXME new_pic
  1203. if(c->avctx->mb_cmp&FF_CMP_CHROMA){
  1204. }
  1205. //FIXME CHROMA !!!
  1206. return fbmin;
  1207. }
  1208. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1209. static inline int bidir_refine(MpegEncContext * s, int mb_x, int mb_y)
  1210. {
  1211. MotionEstContext * const c= &s->me;
  1212. const int mot_stride = s->mb_stride;
  1213. const int xy = mb_y *mot_stride + mb_x;
  1214. int fbmin;
  1215. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1216. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1217. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1218. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1219. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1220. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1221. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1222. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1223. const int flags= c->sub_flags;
  1224. const int qpel= flags&FLAG_QPEL;
  1225. const int shift= 1+qpel;
  1226. const int xmin= c->xmin<<shift;
  1227. const int ymin= c->ymin<<shift;
  1228. const int xmax= c->xmax<<shift;
  1229. const int ymax= c->ymax<<shift;
  1230. #define HASH(fx,fy,bx,by) ((fx)+17*(fy)+63*(bx)+117*(by))
  1231. #define HASH8(fx,fy,bx,by) ((uint8_t)HASH(fx,fy,bx,by))
  1232. int hashidx= HASH(motion_fx,motion_fy, motion_bx, motion_by);
  1233. uint8_t map[256] = { 0 };
  1234. map[hashidx&255] = 1;
  1235. fbmin= check_bidir_mv(s, motion_fx, motion_fy,
  1236. motion_bx, motion_by,
  1237. pred_fx, pred_fy,
  1238. pred_bx, pred_by,
  1239. 0, 16);
  1240. if(s->avctx->bidir_refine){
  1241. int end;
  1242. static const uint8_t limittab[5]={0,8,32,64,80};
  1243. const int limit= limittab[s->avctx->bidir_refine];
  1244. static const int8_t vect[][4]={
  1245. { 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},
  1246. { 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},
  1247. { 0, 1, 0, 1}, { 0,-1, 0,-1}, { 1, 0, 1, 0}, {-1, 0,-1, 0},
  1248. { 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},
  1249. { 0,-1, 0, 1}, { 0, 1, 0,-1}, {-1, 0, 1, 0}, { 1, 0,-1, 0},
  1250. { 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},
  1251. { 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},
  1252. { 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},
  1253. { 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},
  1254. { 1, 1, 1, 1}, {-1,-1,-1,-1},
  1255. { 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},
  1256. { 1, 1,-1,-1}, {-1,-1, 1, 1}, { 1,-1,-1, 1}, {-1, 1, 1,-1}, { 1,-1, 1,-1}, {-1, 1,-1, 1},
  1257. };
  1258. static const uint8_t hash[]={
  1259. 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),
  1260. 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),
  1261. HASH8( 0, 1, 0, 1), HASH8( 0,-1, 0,-1), HASH8( 1, 0, 1, 0), HASH8(-1, 0,-1, 0),
  1262. 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),
  1263. HASH8( 0,-1, 0, 1), HASH8( 0, 1, 0,-1), HASH8(-1, 0, 1, 0), HASH8( 1, 0,-1, 0),
  1264. 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),
  1265. 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),
  1266. 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),
  1267. 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),
  1268. HASH8( 1, 1, 1, 1), HASH8(-1,-1,-1,-1),
  1269. 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),
  1270. 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),
  1271. };
  1272. #define CHECK_BIDIR(fx,fy,bx,by)\
  1273. if( !map[(hashidx+HASH(fx,fy,bx,by))&255]\
  1274. &&(fx<=0 || motion_fx+fx<=xmax) && (fy<=0 || motion_fy+fy<=ymax) && (bx<=0 || motion_bx+bx<=xmax) && (by<=0 || motion_by+by<=ymax)\
  1275. &&(fx>=0 || motion_fx+fx>=xmin) && (fy>=0 || motion_fy+fy>=ymin) && (bx>=0 || motion_bx+bx>=xmin) && (by>=0 || motion_by+by>=ymin)){\
  1276. int score;\
  1277. map[(hashidx+HASH(fx,fy,bx,by))&255] = 1;\
  1278. 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);\
  1279. if(score < fbmin){\
  1280. hashidx += HASH(fx,fy,bx,by);\
  1281. fbmin= score;\
  1282. motion_fx+=fx;\
  1283. motion_fy+=fy;\
  1284. motion_bx+=bx;\
  1285. motion_by+=by;\
  1286. end=0;\
  1287. }\
  1288. }
  1289. #define CHECK_BIDIR2(a,b,c,d)\
  1290. CHECK_BIDIR(a,b,c,d)\
  1291. CHECK_BIDIR(-(a),-(b),-(c),-(d))
  1292. do{
  1293. int i;
  1294. int borderdist=0;
  1295. end=1;
  1296. CHECK_BIDIR2(0,0,0,1)
  1297. CHECK_BIDIR2(0,0,1,0)
  1298. CHECK_BIDIR2(0,1,0,0)
  1299. CHECK_BIDIR2(1,0,0,0)
  1300. for(i=8; i<limit; i++){
  1301. int fx= motion_fx+vect[i][0];
  1302. int fy= motion_fy+vect[i][1];
  1303. int bx= motion_bx+vect[i][2];
  1304. int by= motion_by+vect[i][3];
  1305. if(borderdist<=0){
  1306. int a= (xmax - FFMAX(fx,bx))|(FFMIN(fx,bx) - xmin);
  1307. int b= (ymax - FFMAX(fy,by))|(FFMIN(fy,by) - ymin);
  1308. if((a|b) < 0)
  1309. map[(hashidx+hash[i])&255] = 1;
  1310. }
  1311. if(!map[(hashidx+hash[i])&255]){
  1312. int score;
  1313. map[(hashidx+hash[i])&255] = 1;
  1314. score= check_bidir_mv(s, fx, fy, bx, by, pred_fx, pred_fy, pred_bx, pred_by, 0, 16);
  1315. if(score < fbmin){
  1316. hashidx += hash[i];
  1317. fbmin= score;
  1318. motion_fx=fx;
  1319. motion_fy=fy;
  1320. motion_bx=bx;
  1321. motion_by=by;
  1322. end=0;
  1323. borderdist--;
  1324. if(borderdist<=0){
  1325. int a= FFMIN(xmax - FFMAX(fx,bx), FFMIN(fx,bx) - xmin);
  1326. int b= FFMIN(ymax - FFMAX(fy,by), FFMIN(fy,by) - ymin);
  1327. borderdist= FFMIN(a,b);
  1328. }
  1329. }
  1330. }
  1331. }
  1332. }while(!end);
  1333. }
  1334. s->b_bidir_forw_mv_table[xy][0]= motion_fx;
  1335. s->b_bidir_forw_mv_table[xy][1]= motion_fy;
  1336. s->b_bidir_back_mv_table[xy][0]= motion_bx;
  1337. s->b_bidir_back_mv_table[xy][1]= motion_by;
  1338. return fbmin;
  1339. }
  1340. static inline int direct_search(MpegEncContext * s, int mb_x, int mb_y)
  1341. {
  1342. MotionEstContext * const c= &s->me;
  1343. int P[10][2];
  1344. const int mot_stride = s->mb_stride;
  1345. const int mot_xy = mb_y*mot_stride + mb_x;
  1346. const int shift= 1+s->quarter_sample;
  1347. int dmin, i;
  1348. const int time_pp= s->pp_time;
  1349. const int time_pb= s->pb_time;
  1350. int mx, my, xmin, xmax, ymin, ymax;
  1351. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1352. c->current_mv_penalty= c->mv_penalty[1] + MAX_MV;
  1353. ymin= xmin=(-32)>>shift;
  1354. ymax= xmax= 31>>shift;
  1355. if (IS_8X8(s->next_picture.f.mb_type[mot_xy])) {
  1356. s->mv_type= MV_TYPE_8X8;
  1357. }else{
  1358. s->mv_type= MV_TYPE_16X16;
  1359. }
  1360. for(i=0; i<4; i++){
  1361. int index= s->block_index[i];
  1362. int min, max;
  1363. c->co_located_mv[i][0] = s->next_picture.f.motion_val[0][index][0];
  1364. c->co_located_mv[i][1] = s->next_picture.f.motion_val[0][index][1];
  1365. c->direct_basis_mv[i][0]= c->co_located_mv[i][0]*time_pb/time_pp + ((i& 1)<<(shift+3));
  1366. c->direct_basis_mv[i][1]= c->co_located_mv[i][1]*time_pb/time_pp + ((i>>1)<<(shift+3));
  1367. // c->direct_basis_mv[1][i][0]= c->co_located_mv[i][0]*(time_pb - time_pp)/time_pp + ((i &1)<<(shift+3);
  1368. // c->direct_basis_mv[1][i][1]= c->co_located_mv[i][1]*(time_pb - time_pp)/time_pp + ((i>>1)<<(shift+3);
  1369. max= FFMAX(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1370. min= FFMIN(c->direct_basis_mv[i][0], c->direct_basis_mv[i][0] - c->co_located_mv[i][0])>>shift;
  1371. max+= 16*mb_x + 1; // +-1 is for the simpler rounding
  1372. min+= 16*mb_x - 1;
  1373. xmax= FFMIN(xmax, s->width - max);
  1374. xmin= FFMAX(xmin, - 16 - min);
  1375. max= FFMAX(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1376. min= FFMIN(c->direct_basis_mv[i][1], c->direct_basis_mv[i][1] - c->co_located_mv[i][1])>>shift;
  1377. max+= 16*mb_y + 1; // +-1 is for the simpler rounding
  1378. min+= 16*mb_y - 1;
  1379. ymax= FFMIN(ymax, s->height - max);
  1380. ymin= FFMAX(ymin, - 16 - min);
  1381. if(s->mv_type == MV_TYPE_16X16) break;
  1382. }
  1383. assert(xmax <= 15 && ymax <= 15 && xmin >= -16 && ymin >= -16);
  1384. if(xmax < 0 || xmin >0 || ymax < 0 || ymin > 0){
  1385. s->b_direct_mv_table[mot_xy][0]= 0;
  1386. s->b_direct_mv_table[mot_xy][1]= 0;
  1387. return 256*256*256*64;
  1388. }
  1389. c->xmin= xmin;
  1390. c->ymin= ymin;
  1391. c->xmax= xmax;
  1392. c->ymax= ymax;
  1393. c->flags |= FLAG_DIRECT;
  1394. c->sub_flags |= FLAG_DIRECT;
  1395. c->pred_x=0;
  1396. c->pred_y=0;
  1397. P_LEFT[0] = av_clip(mv_table[mot_xy - 1][0], xmin<<shift, xmax<<shift);
  1398. P_LEFT[1] = av_clip(mv_table[mot_xy - 1][1], ymin<<shift, ymax<<shift);
  1399. /* special case for first line */
  1400. if (!s->first_slice_line) { //FIXME maybe allow this over thread boundary as it is clipped
  1401. P_TOP[0] = av_clip(mv_table[mot_xy - mot_stride ][0], xmin<<shift, xmax<<shift);
  1402. P_TOP[1] = av_clip(mv_table[mot_xy - mot_stride ][1], ymin<<shift, ymax<<shift);
  1403. P_TOPRIGHT[0] = av_clip(mv_table[mot_xy - mot_stride + 1 ][0], xmin<<shift, xmax<<shift);
  1404. P_TOPRIGHT[1] = av_clip(mv_table[mot_xy - mot_stride + 1 ][1], ymin<<shift, ymax<<shift);
  1405. P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
  1406. P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
  1407. }
  1408. dmin = ff_epzs_motion_search(s, &mx, &my, P, 0, 0, mv_table, 1<<(16-shift), 0, 16);
  1409. if(c->sub_flags&FLAG_QPEL)
  1410. dmin = qpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1411. else
  1412. dmin = hpel_motion_search(s, &mx, &my, dmin, 0, 0, 0, 16);
  1413. if(c->avctx->me_sub_cmp != c->avctx->mb_cmp && !c->skip)
  1414. dmin= get_mb_score(s, mx, my, 0, 0, 0, 16, 1);
  1415. get_limits(s, 16*mb_x, 16*mb_y); //restore c->?min/max, maybe not needed
  1416. mv_table[mot_xy][0]= mx;
  1417. mv_table[mot_xy][1]= my;
  1418. c->flags &= ~FLAG_DIRECT;
  1419. c->sub_flags &= ~FLAG_DIRECT;
  1420. return dmin;
  1421. }
  1422. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1423. int mb_x, int mb_y)
  1424. {
  1425. MotionEstContext * const c= &s->me;
  1426. const int penalty_factor= c->mb_penalty_factor;
  1427. int fmin, bmin, dmin, fbmin, bimin, fimin;
  1428. int type=0;
  1429. const int xy = mb_y*s->mb_stride + mb_x;
  1430. init_ref(c, s->new_picture.f.data, s->last_picture.f.data,
  1431. s->next_picture.f.data, 16 * mb_x, 16 * mb_y, 2);
  1432. get_limits(s, 16*mb_x, 16*mb_y);
  1433. c->skip=0;
  1434. if (s->codec_id == AV_CODEC_ID_MPEG4 && s->next_picture.f.mbskip_table[xy]) {
  1435. int score= direct_search(s, mb_x, mb_y); //FIXME just check 0,0
  1436. score= ((unsigned)(score*score + 128*256))>>16;
  1437. c->mc_mb_var_sum_temp += score;
  1438. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1439. s->mb_type[mb_y*s->mb_stride + mb_x]= CANDIDATE_MB_TYPE_DIRECT0;
  1440. return;
  1441. }
  1442. if(c->avctx->me_threshold){
  1443. int vard= check_input_motion(s, mb_x, mb_y, 0);
  1444. if((vard+128)>>8 < c->avctx->me_threshold){
  1445. // pix = c->src[0][0];
  1446. // sum = s->dsp.pix_sum(pix, s->linesize);
  1447. // varc = s->dsp.pix_norm1(pix, s->linesize) - (((unsigned)(sum*sum))>>8) + 500;
  1448. // pic->mb_var [s->mb_stride * mb_y + mb_x] = (varc+128)>>8;
  1449. s->current_picture.mc_mb_var[s->mb_stride * mb_y + mb_x] = (vard+128)>>8;
  1450. /* pic->mb_mean [s->mb_stride * mb_y + mb_x] = (sum+128)>>8;
  1451. c->mb_var_sum_temp += (varc+128)>>8;*/
  1452. c->mc_mb_var_sum_temp += (vard+128)>>8;
  1453. /* if (vard <= 64<<8 || vard < varc) {
  1454. c->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
  1455. }else{
  1456. c->scene_change_score+= s->qscale * s->avctx->scenechange_factor;
  1457. }*/
  1458. return;
  1459. }
  1460. if((vard+128)>>8 < c->avctx->mb_threshold){
  1461. type= s->mb_type[mb_y*s->mb_stride + mb_x];
  1462. if(type == CANDIDATE_MB_TYPE_DIRECT){
  1463. direct_search(s, mb_x, mb_y);
  1464. }
  1465. if(type == CANDIDATE_MB_TYPE_FORWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1466. c->skip=0;
  1467. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code);
  1468. }
  1469. if(type == CANDIDATE_MB_TYPE_BACKWARD || type == CANDIDATE_MB_TYPE_BIDIR){
  1470. c->skip=0;
  1471. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code);
  1472. }
  1473. if(type == CANDIDATE_MB_TYPE_FORWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1474. c->skip=0;
  1475. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1476. interlaced_search(s, 0,
  1477. s->b_field_mv_table[0], s->b_field_select_table[0],
  1478. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 1);
  1479. }
  1480. if(type == CANDIDATE_MB_TYPE_BACKWARD_I || type == CANDIDATE_MB_TYPE_BIDIR_I){
  1481. c->skip=0;
  1482. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1483. interlaced_search(s, 2,
  1484. s->b_field_mv_table[1], s->b_field_select_table[1],
  1485. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 1);
  1486. }
  1487. return;
  1488. }
  1489. }
  1490. if (s->codec_id == AV_CODEC_ID_MPEG4)
  1491. dmin= direct_search(s, mb_x, mb_y);
  1492. else
  1493. dmin= INT_MAX;
  1494. //FIXME penalty stuff for non mpeg4
  1495. c->skip=0;
  1496. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, 0, s->f_code) + 3*penalty_factor;
  1497. c->skip=0;
  1498. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, 2, s->b_code) + 2*penalty_factor;
  1499. av_dlog(s, " %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1500. c->skip=0;
  1501. fbmin= bidir_refine(s, mb_x, mb_y) + penalty_factor;
  1502. av_dlog(s, "%d %d %d %d\n", dmin, fmin, bmin, fbmin);
  1503. if(s->flags & CODEC_FLAG_INTERLACED_ME){
  1504. //FIXME mb type penalty
  1505. c->skip=0;
  1506. c->current_mv_penalty= c->mv_penalty[s->f_code] + MAX_MV;
  1507. fimin= interlaced_search(s, 0,
  1508. s->b_field_mv_table[0], s->b_field_select_table[0],
  1509. s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1], 0);
  1510. c->current_mv_penalty= c->mv_penalty[s->b_code] + MAX_MV;
  1511. bimin= interlaced_search(s, 2,
  1512. s->b_field_mv_table[1], s->b_field_select_table[1],
  1513. s->b_back_mv_table[xy][0], s->b_back_mv_table[xy][1], 0);
  1514. }else
  1515. fimin= bimin= INT_MAX;
  1516. {
  1517. int score= fmin;
  1518. type = CANDIDATE_MB_TYPE_FORWARD;
  1519. if (dmin <= score){
  1520. score = dmin;
  1521. type = CANDIDATE_MB_TYPE_DIRECT;
  1522. }
  1523. if(bmin<score){
  1524. score=bmin;
  1525. type= CANDIDATE_MB_TYPE_BACKWARD;
  1526. }
  1527. if(fbmin<score){
  1528. score=fbmin;
  1529. type= CANDIDATE_MB_TYPE_BIDIR;
  1530. }
  1531. if(fimin<score){
  1532. score=fimin;
  1533. type= CANDIDATE_MB_TYPE_FORWARD_I;
  1534. }
  1535. if(bimin<score){
  1536. score=bimin;
  1537. type= CANDIDATE_MB_TYPE_BACKWARD_I;
  1538. }
  1539. score= ((unsigned)(score*score + 128*256))>>16;
  1540. c->mc_mb_var_sum_temp += score;
  1541. s->current_picture.mc_mb_var[mb_y*s->mb_stride + mb_x] = score; //FIXME use SSE
  1542. }
  1543. if(c->avctx->mb_decision > FF_MB_DECISION_SIMPLE){
  1544. type= CANDIDATE_MB_TYPE_FORWARD | CANDIDATE_MB_TYPE_BACKWARD | CANDIDATE_MB_TYPE_BIDIR | CANDIDATE_MB_TYPE_DIRECT;
  1545. if(fimin < INT_MAX)
  1546. type |= CANDIDATE_MB_TYPE_FORWARD_I;
  1547. if(bimin < INT_MAX)
  1548. type |= CANDIDATE_MB_TYPE_BACKWARD_I;
  1549. if(fimin < INT_MAX && bimin < INT_MAX){
  1550. type |= CANDIDATE_MB_TYPE_BIDIR_I;
  1551. }
  1552. //FIXME something smarter
  1553. if(dmin>256*256*16) type&= ~CANDIDATE_MB_TYPE_DIRECT; //do not try direct mode if it is invalid for this MB
  1554. 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])
  1555. type |= CANDIDATE_MB_TYPE_DIRECT0;
  1556. }
  1557. s->mb_type[mb_y*s->mb_stride + mb_x]= type;
  1558. }
  1559. /* find best f_code for ME which do unlimited searches */
  1560. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1561. {
  1562. if(s->me_method>=ME_EPZS){
  1563. int score[8];
  1564. int i, y, range= s->avctx->me_range ? s->avctx->me_range : (INT_MAX/2);
  1565. uint8_t * fcode_tab= s->fcode_tab;
  1566. int best_fcode=-1;
  1567. int best_score=-10000000;
  1568. if(s->msmpeg4_version)
  1569. range= FFMIN(range, 16);
  1570. else if(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL)
  1571. range= FFMIN(range, 256);
  1572. for(i=0; i<8; i++) score[i]= s->mb_num*(8-i);
  1573. for(y=0; y<s->mb_height; y++){
  1574. int x;
  1575. int xy= y*s->mb_stride;
  1576. for(x=0; x<s->mb_width; x++){
  1577. if(s->mb_type[xy] & type){
  1578. int mx= mv_table[xy][0];
  1579. int my= mv_table[xy][1];
  1580. int fcode= FFMAX(fcode_tab[mx + MAX_MV],
  1581. fcode_tab[my + MAX_MV]);
  1582. int j;
  1583. if(mx >= range || mx < -range ||
  1584. my >= range || my < -range)
  1585. continue;
  1586. for(j=0; j<fcode && j<8; j++){
  1587. if(s->pict_type==AV_PICTURE_TYPE_B || s->current_picture.mc_mb_var[xy] < s->current_picture.mb_var[xy])
  1588. score[j]-= 170;
  1589. }
  1590. }
  1591. xy++;
  1592. }
  1593. }
  1594. for(i=1; i<8; i++){
  1595. if(score[i] > best_score){
  1596. best_score= score[i];
  1597. best_fcode= i;
  1598. }
  1599. }
  1600. return best_fcode;
  1601. }else{
  1602. return 1;
  1603. }
  1604. }
  1605. void ff_fix_long_p_mvs(MpegEncContext * s)
  1606. {
  1607. MotionEstContext * const c= &s->me;
  1608. const int f_code= s->f_code;
  1609. int y, range;
  1610. assert(s->pict_type==AV_PICTURE_TYPE_P);
  1611. range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1612. assert(range <= 16 || !s->msmpeg4_version);
  1613. assert(range <=256 || !(s->codec_id == AV_CODEC_ID_MPEG2VIDEO && s->avctx->strict_std_compliance >= FF_COMPLIANCE_NORMAL));
  1614. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1615. if(s->flags&CODEC_FLAG_4MV){
  1616. const int wrap= s->b8_stride;
  1617. /* clip / convert to intra 8x8 type MVs */
  1618. for(y=0; y<s->mb_height; y++){
  1619. int xy= y*2*wrap;
  1620. int i= y*s->mb_stride;
  1621. int x;
  1622. for(x=0; x<s->mb_width; x++){
  1623. if(s->mb_type[i]&CANDIDATE_MB_TYPE_INTER4V){
  1624. int block;
  1625. for(block=0; block<4; block++){
  1626. int off= (block& 1) + (block>>1)*wrap;
  1627. int mx = s->current_picture.f.motion_val[0][ xy + off ][0];
  1628. int my = s->current_picture.f.motion_val[0][ xy + off ][1];
  1629. if( mx >=range || mx <-range
  1630. || my >=range || my <-range){
  1631. s->mb_type[i] &= ~CANDIDATE_MB_TYPE_INTER4V;
  1632. s->mb_type[i] |= CANDIDATE_MB_TYPE_INTRA;
  1633. s->current_picture.f.mb_type[i] = CANDIDATE_MB_TYPE_INTRA;
  1634. }
  1635. }
  1636. }
  1637. xy+=2;
  1638. i++;
  1639. }
  1640. }
  1641. }
  1642. }
  1643. /**
  1644. *
  1645. * @param truncate 1 for truncation, 0 for using intra
  1646. */
  1647. void ff_fix_long_mvs(MpegEncContext * s, uint8_t *field_select_table, int field_select,
  1648. int16_t (*mv_table)[2], int f_code, int type, int truncate)
  1649. {
  1650. MotionEstContext * const c= &s->me;
  1651. int y, h_range, v_range;
  1652. // RAL: 8 in MPEG-1, 16 in MPEG-4
  1653. int range = (((s->out_format == FMT_MPEG1 || s->msmpeg4_version) ? 8 : 16) << f_code);
  1654. if(c->avctx->me_range && range > c->avctx->me_range) range= c->avctx->me_range;
  1655. h_range= range;
  1656. v_range= field_select_table ? range>>1 : range;
  1657. /* clip / convert to intra 16x16 type MVs */
  1658. for(y=0; y<s->mb_height; y++){
  1659. int x;
  1660. int xy= y*s->mb_stride;
  1661. for(x=0; x<s->mb_width; x++){
  1662. if (s->mb_type[xy] & type){ // RAL: "type" test added...
  1663. if(field_select_table==NULL || field_select_table[xy] == field_select){
  1664. if( mv_table[xy][0] >=h_range || mv_table[xy][0] <-h_range
  1665. || mv_table[xy][1] >=v_range || mv_table[xy][1] <-v_range){
  1666. if(truncate){
  1667. if (mv_table[xy][0] > h_range-1) mv_table[xy][0]= h_range-1;
  1668. else if(mv_table[xy][0] < -h_range ) mv_table[xy][0]= -h_range;
  1669. if (mv_table[xy][1] > v_range-1) mv_table[xy][1]= v_range-1;
  1670. else if(mv_table[xy][1] < -v_range ) mv_table[xy][1]= -v_range;
  1671. }else{
  1672. s->mb_type[xy] &= ~type;
  1673. s->mb_type[xy] |= CANDIDATE_MB_TYPE_INTRA;
  1674. mv_table[xy][0]=
  1675. mv_table[xy][1]= 0;
  1676. }
  1677. }
  1678. }
  1679. }
  1680. xy++;
  1681. }
  1682. }
  1683. }