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.

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