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.

2017 lines
78KB

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