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.

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