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.

2149 lines
76KB

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