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.

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