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.

1762 lines
68KB

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