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.

2150 lines
76KB

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