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.

2157 lines
77KB

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