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.

1511 lines
45KB

  1. /*
  2. * Motion estimation
  3. * Copyright (c) 2000,2001 Gerard Lantau.
  4. *
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
  21. */
  22. #include <stdlib.h>
  23. #include <stdio.h>
  24. #include "avcodec.h"
  25. #include "dsputil.h"
  26. #include "mpegvideo.h"
  27. //#define ABS(a) ((a)>0 ? (a) : -(a))
  28. #define MAX(a,b) ((a) > (b) ? (a) : (b))
  29. #define INTER_BIAS 257
  30. static int halfpel_motion_search(MpegEncContext * s,
  31. int *mx_ptr, int *my_ptr, int dmin,
  32. int xmin, int ymin, int xmax, int ymax,
  33. int pred_x, int pred_y, uint8_t *ref_picture);
  34. static int pix_sum(UINT8 * pix, int line_size)
  35. {
  36. int s, i, j;
  37. s = 0;
  38. for (i = 0; i < 16; i++) {
  39. for (j = 0; j < 16; j += 8) {
  40. s += pix[0];
  41. s += pix[1];
  42. s += pix[2];
  43. s += pix[3];
  44. s += pix[4];
  45. s += pix[5];
  46. s += pix[6];
  47. s += pix[7];
  48. pix += 8;
  49. }
  50. pix += line_size - 16;
  51. }
  52. return s;
  53. }
  54. static int pix_dev(UINT8 * pix, int line_size, int mean)
  55. {
  56. int s, i, j;
  57. s = 0;
  58. for (i = 0; i < 16; i++) {
  59. for (j = 0; j < 16; j += 8) {
  60. s += ABS(pix[0]-mean);
  61. s += ABS(pix[1]-mean);
  62. s += ABS(pix[2]-mean);
  63. s += ABS(pix[3]-mean);
  64. s += ABS(pix[4]-mean);
  65. s += ABS(pix[5]-mean);
  66. s += ABS(pix[6]-mean);
  67. s += ABS(pix[7]-mean);
  68. pix += 8;
  69. }
  70. pix += line_size - 16;
  71. }
  72. return s;
  73. }
  74. static int pix_norm1(UINT8 * pix, int line_size)
  75. {
  76. int s, i, j;
  77. UINT32 *sq = squareTbl + 256;
  78. s = 0;
  79. for (i = 0; i < 16; i++) {
  80. for (j = 0; j < 16; j += 8) {
  81. s += sq[pix[0]];
  82. s += sq[pix[1]];
  83. s += sq[pix[2]];
  84. s += sq[pix[3]];
  85. s += sq[pix[4]];
  86. s += sq[pix[5]];
  87. s += sq[pix[6]];
  88. s += sq[pix[7]];
  89. pix += 8;
  90. }
  91. pix += line_size - 16;
  92. }
  93. return s;
  94. }
  95. static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
  96. {
  97. int s, i, j;
  98. UINT32 *sq = squareTbl + 256;
  99. s = 0;
  100. for (i = 0; i < 16; i++) {
  101. for (j = 0; j < 16; j += 8) {
  102. s += sq[pix1[0] - pix2[0]];
  103. s += sq[pix1[1] - pix2[1]];
  104. s += sq[pix1[2] - pix2[2]];
  105. s += sq[pix1[3] - pix2[3]];
  106. s += sq[pix1[4] - pix2[4]];
  107. s += sq[pix1[5] - pix2[5]];
  108. s += sq[pix1[6] - pix2[6]];
  109. s += sq[pix1[7] - pix2[7]];
  110. pix1 += 8;
  111. pix2 += 8;
  112. }
  113. pix1 += line_size - 16;
  114. pix2 += line_size - 16;
  115. }
  116. return s;
  117. }
  118. static void no_motion_search(MpegEncContext * s,
  119. int *mx_ptr, int *my_ptr)
  120. {
  121. *mx_ptr = 16 * s->mb_x;
  122. *my_ptr = 16 * s->mb_y;
  123. }
  124. static int full_motion_search(MpegEncContext * s,
  125. int *mx_ptr, int *my_ptr, int range,
  126. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  127. {
  128. int x1, y1, x2, y2, xx, yy, x, y;
  129. int mx, my, dmin, d;
  130. UINT8 *pix;
  131. xx = 16 * s->mb_x;
  132. yy = 16 * s->mb_y;
  133. x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
  134. if (x1 < xmin)
  135. x1 = xmin;
  136. x2 = xx + range - 1;
  137. if (x2 > xmax)
  138. x2 = xmax;
  139. y1 = yy - range + 1;
  140. if (y1 < ymin)
  141. y1 = ymin;
  142. y2 = yy + range - 1;
  143. if (y2 > ymax)
  144. y2 = ymax;
  145. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  146. dmin = 0x7fffffff;
  147. mx = 0;
  148. my = 0;
  149. for (y = y1; y <= y2; y++) {
  150. for (x = x1; x <= x2; x++) {
  151. d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
  152. s->linesize);
  153. if (d < dmin ||
  154. (d == dmin &&
  155. (abs(x - xx) + abs(y - yy)) <
  156. (abs(mx - xx) + abs(my - yy)))) {
  157. dmin = d;
  158. mx = x;
  159. my = y;
  160. }
  161. }
  162. }
  163. *mx_ptr = mx;
  164. *my_ptr = my;
  165. #if 0
  166. if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
  167. *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
  168. fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
  169. }
  170. #endif
  171. return dmin;
  172. }
  173. static int log_motion_search(MpegEncContext * s,
  174. int *mx_ptr, int *my_ptr, int range,
  175. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  176. {
  177. int x1, y1, x2, y2, xx, yy, x, y;
  178. int mx, my, dmin, d;
  179. UINT8 *pix;
  180. xx = s->mb_x << 4;
  181. yy = s->mb_y << 4;
  182. /* Left limit */
  183. x1 = xx - range;
  184. if (x1 < xmin)
  185. x1 = xmin;
  186. /* Right limit */
  187. x2 = xx + range;
  188. if (x2 > xmax)
  189. x2 = xmax;
  190. /* Upper limit */
  191. y1 = yy - range;
  192. if (y1 < ymin)
  193. y1 = ymin;
  194. /* Lower limit */
  195. y2 = yy + range;
  196. if (y2 > ymax)
  197. y2 = ymax;
  198. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  199. dmin = 0x7fffffff;
  200. mx = 0;
  201. my = 0;
  202. do {
  203. for (y = y1; y <= y2; y += range) {
  204. for (x = x1; x <= x2; x += range) {
  205. d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
  206. if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  207. dmin = d;
  208. mx = x;
  209. my = y;
  210. }
  211. }
  212. }
  213. range = range >> 1;
  214. x1 = mx - range;
  215. if (x1 < xmin)
  216. x1 = xmin;
  217. x2 = mx + range;
  218. if (x2 > xmax)
  219. x2 = xmax;
  220. y1 = my - range;
  221. if (y1 < ymin)
  222. y1 = ymin;
  223. y2 = my + range;
  224. if (y2 > ymax)
  225. y2 = ymax;
  226. } while (range >= 1);
  227. #ifdef DEBUG
  228. fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
  229. #endif
  230. *mx_ptr = mx;
  231. *my_ptr = my;
  232. return dmin;
  233. }
  234. static int phods_motion_search(MpegEncContext * s,
  235. int *mx_ptr, int *my_ptr, int range,
  236. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  237. {
  238. int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
  239. int mx, my, dminx, dminy;
  240. UINT8 *pix;
  241. xx = s->mb_x << 4;
  242. yy = s->mb_y << 4;
  243. /* Left limit */
  244. x1 = xx - range;
  245. if (x1 < xmin)
  246. x1 = xmin;
  247. /* Right limit */
  248. x2 = xx + range;
  249. if (x2 > xmax)
  250. x2 = xmax;
  251. /* Upper limit */
  252. y1 = yy - range;
  253. if (y1 < ymin)
  254. y1 = ymin;
  255. /* Lower limit */
  256. y2 = yy + range;
  257. if (y2 > ymax)
  258. y2 = ymax;
  259. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  260. mx = 0;
  261. my = 0;
  262. x = xx;
  263. y = yy;
  264. do {
  265. dminx = 0x7fffffff;
  266. dminy = 0x7fffffff;
  267. lastx = x;
  268. for (x = x1; x <= x2; x += range) {
  269. d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
  270. if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  271. dminx = d;
  272. mx = x;
  273. }
  274. }
  275. x = lastx;
  276. for (y = y1; y <= y2; y += range) {
  277. d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
  278. if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
  279. dminy = d;
  280. my = y;
  281. }
  282. }
  283. range = range >> 1;
  284. x = mx;
  285. y = my;
  286. x1 = mx - range;
  287. if (x1 < xmin)
  288. x1 = xmin;
  289. x2 = mx + range;
  290. if (x2 > xmax)
  291. x2 = xmax;
  292. y1 = my - range;
  293. if (y1 < ymin)
  294. y1 = ymin;
  295. y2 = my + range;
  296. if (y2 > ymax)
  297. y2 = ymax;
  298. } while (range >= 1);
  299. #ifdef DEBUG
  300. fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
  301. #endif
  302. /* half pixel search */
  303. *mx_ptr = mx;
  304. *my_ptr = my;
  305. return dminy;
  306. }
  307. #define Z_THRESHOLD 256
  308. #define CHECK_MV(x,y)\
  309. {\
  310. d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
  311. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
  312. if(d<dmin){\
  313. best[0]=x;\
  314. best[1]=y;\
  315. dmin=d;\
  316. }\
  317. }
  318. #define CHECK_MV_DIR(x,y,new_dir)\
  319. {\
  320. d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
  321. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
  322. if(d<dmin){\
  323. best[0]=x;\
  324. best[1]=y;\
  325. dmin=d;\
  326. next_dir= new_dir;\
  327. }\
  328. }
  329. #define CHECK_MV4(x,y)\
  330. {\
  331. d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
  332. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
  333. if(d<dmin){\
  334. best[0]=x;\
  335. best[1]=y;\
  336. dmin=d;\
  337. }\
  338. }
  339. #define CHECK_MV4_DIR(x,y,new_dir)\
  340. {\
  341. d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
  342. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
  343. if(d<dmin){\
  344. best[0]=x;\
  345. best[1]=y;\
  346. dmin=d;\
  347. next_dir= new_dir;\
  348. }\
  349. }
  350. #define check(x,y,S,v)\
  351. if( (x)<(xmin<<(S)) ) printf("%d %d %d %d xmin" #v, (x), (y), s->mb_x, s->mb_y);\
  352. if( (x)>(xmax<<(S)) ) printf("%d %d %d %d xmax" #v, (x), (y), s->mb_x, s->mb_y);\
  353. if( (y)<(ymin<<(S)) ) printf("%d %d %d %d ymin" #v, (x), (y), s->mb_x, s->mb_y);\
  354. if( (y)>(ymax<<(S)) ) printf("%d %d %d %d ymax" #v, (x), (y), s->mb_x, s->mb_y);\
  355. static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
  356. UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
  357. int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
  358. int xmin, int ymin, int xmax, int ymax, int shift)
  359. {
  360. int next_dir=-1;
  361. for(;;){
  362. int d;
  363. const int dir= next_dir;
  364. const int x= best[0];
  365. const int y= best[1];
  366. next_dir=-1;
  367. //printf("%d", dir);
  368. if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
  369. if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
  370. if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
  371. if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
  372. if(next_dir==-1){
  373. return dmin;
  374. }
  375. }
  376. /* for(;;){
  377. int d;
  378. const int x= best[0];
  379. const int y= best[1];
  380. const int last_min=dmin;
  381. if(x>xmin) CHECK_MV(x-1, y )
  382. if(y>xmin) CHECK_MV(x , y-1)
  383. if(x<xmax) CHECK_MV(x+1, y )
  384. if(y<xmax) CHECK_MV(x , y+1)
  385. if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)
  386. if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)
  387. if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)
  388. if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)
  389. if(x-1>xmin) CHECK_MV(x-2, y )
  390. if(y-1>xmin) CHECK_MV(x , y-2)
  391. if(x+1<xmax) CHECK_MV(x+2, y )
  392. if(y+1<xmax) CHECK_MV(x , y+2)
  393. if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)
  394. if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)
  395. if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)
  396. if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)
  397. if(dmin==last_min) return dmin;
  398. }
  399. */
  400. }
  401. static inline int small_diamond_search4MV(MpegEncContext * s, int *best, int dmin,
  402. UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
  403. int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
  404. int xmin, int ymin, int xmax, int ymax, int shift)
  405. {
  406. int next_dir=-1;
  407. for(;;){
  408. int d;
  409. const int dir= next_dir;
  410. const int x= best[0];
  411. const int y= best[1];
  412. next_dir=-1;
  413. //printf("%d", dir);
  414. if(dir!=2 && x>xmin) CHECK_MV4_DIR(x-1, y , 0)
  415. if(dir!=3 && y>ymin) CHECK_MV4_DIR(x , y-1, 1)
  416. if(dir!=0 && x<xmax) CHECK_MV4_DIR(x+1, y , 2)
  417. if(dir!=1 && y<ymax) CHECK_MV4_DIR(x , y+1, 3)
  418. if(next_dir==-1){
  419. return dmin;
  420. }
  421. }
  422. }
  423. static inline int snake_search(MpegEncContext * s, int *best, int dmin,
  424. UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
  425. int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
  426. int xmin, int ymin, int xmax, int ymax, int shift)
  427. {
  428. int dir=0;
  429. int c=1;
  430. static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1};
  431. static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1};
  432. int fails=0;
  433. int last_d[2]={dmin, dmin};
  434. /*static int good=0;
  435. static int bad=0;
  436. static int point=0;
  437. point++;
  438. if(256*256*256*64%point==0)
  439. {
  440. printf("%d %d %d\n", good, bad, point);
  441. }*/
  442. for(;;){
  443. int x= best[0];
  444. int y= best[1];
  445. int d;
  446. x+=x_dir[dir];
  447. y+=y_dir[dir];
  448. if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
  449. d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);
  450. d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
  451. }else{
  452. d = dmin + 10000; //FIXME smarter boundary handling
  453. }
  454. if(d<dmin){
  455. best[0]=x;
  456. best[1]=y;
  457. dmin=d;
  458. if(last_d[1] - last_d[0] > last_d[0] - d) c= -c;
  459. dir+=c;
  460. fails=0;
  461. //good++;
  462. last_d[1]=last_d[0];
  463. last_d[0]=d;
  464. }else{
  465. //bad++;
  466. if(fails){
  467. if(fails>=3) return dmin;
  468. }else{
  469. c= -c;
  470. }
  471. dir+=c*2;
  472. fails++;
  473. }
  474. dir&=7;
  475. }
  476. }
  477. static int epzs_motion_search(MpegEncContext * s,
  478. int *mx_ptr, int *my_ptr,
  479. int P[5][2], int pred_x, int pred_y,
  480. int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture)
  481. {
  482. int best[2]={0, 0};
  483. int d, dmin;
  484. UINT8 *new_pic, *old_pic;
  485. const int pic_stride= s->linesize;
  486. const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16;
  487. UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  488. int quant= s->qscale; // qscale of the prev frame
  489. const int shift= 1+s->quarter_sample;
  490. new_pic = s->new_picture[0] + pic_xy;
  491. old_pic = ref_picture + pic_xy;
  492. dmin = pix_abs16x16(new_pic, old_pic, pic_stride);
  493. if(dmin<Z_THRESHOLD){
  494. *mx_ptr= 0;
  495. *my_ptr= 0;
  496. //printf("Z");
  497. return dmin;
  498. }
  499. /* first line */
  500. if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
  501. CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
  502. }else{
  503. CHECK_MV(P[4][0]>>shift, P[4][1]>>shift)
  504. if(dmin<Z_THRESHOLD){
  505. *mx_ptr= P[4][0]>>shift;
  506. *my_ptr= P[4][1]>>shift;
  507. //printf("M\n");
  508. return dmin;
  509. }
  510. CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
  511. CHECK_MV(P[2][0]>>shift, P[2][1]>>shift)
  512. CHECK_MV(P[3][0]>>shift, P[3][1]>>shift)
  513. }
  514. CHECK_MV(P[0][0]>>shift, P[0][1]>>shift)
  515. //check(best[0],best[1],0, b0)
  516. if(s->me_method==ME_EPZS)
  517. dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
  518. pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
  519. else
  520. dmin= snake_search(s, best, dmin, new_pic, old_pic, pic_stride,
  521. pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
  522. //check(best[0],best[1],0, b1)
  523. *mx_ptr= best[0];
  524. *my_ptr= best[1];
  525. // printf("%d %d %d \n", best[0], best[1], dmin);
  526. return dmin;
  527. }
  528. static int epzs_motion_search4(MpegEncContext * s, int block,
  529. int *mx_ptr, int *my_ptr,
  530. int P[6][2], int pred_x, int pred_y,
  531. int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
  532. {
  533. int best[2]={0, 0};
  534. int d, dmin;
  535. UINT8 *new_pic, *old_pic;
  536. const int pic_stride= s->linesize;
  537. const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8;
  538. UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  539. int quant= s->qscale; // qscale of the prev frame
  540. const int shift= 1+s->quarter_sample;
  541. new_pic = s->new_picture[0] + pic_xy;
  542. old_pic = ref_picture + pic_xy;
  543. dmin = pix_abs8x8(new_pic, old_pic, pic_stride);
  544. /* first line */
  545. if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
  546. CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift)
  547. }else{
  548. CHECK_MV4(P[4][0]>>shift, P[4][1]>>shift)
  549. if(dmin<Z_THRESHOLD){
  550. *mx_ptr= P[4][0]>>shift;
  551. *my_ptr= P[4][1]>>shift;
  552. //printf("M\n");
  553. return dmin;
  554. }
  555. CHECK_MV4(P[1][0]>>shift, P[1][1]>>shift)
  556. CHECK_MV4(P[2][0]>>shift, P[2][1]>>shift)
  557. CHECK_MV4(P[3][0]>>shift, P[3][1]>>shift)
  558. }
  559. CHECK_MV4(P[0][0]>>shift, P[0][1]>>shift)
  560. CHECK_MV4(P[5][0]>>shift, P[5][1]>>shift)
  561. //check(best[0],best[1],0, b0)
  562. dmin= small_diamond_search4MV(s, best, dmin, new_pic, old_pic, pic_stride,
  563. pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
  564. //check(best[0],best[1],0, b1)
  565. *mx_ptr= best[0];
  566. *my_ptr= best[1];
  567. // printf("%d %d %d \n", best[0], best[1], dmin);
  568. return dmin;
  569. }
  570. #define CHECK_HALF_MV(suffix, x, y) \
  571. d= pix_abs16x16_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
  572. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
  573. if(d<dminh){\
  574. dminh= d;\
  575. mx= mx1 + x;\
  576. my= my1 + y;\
  577. }
  578. #define CHECK_HALF_MV4(suffix, x, y) \
  579. d= pix_abs8x8_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
  580. d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
  581. if(d<dminh){\
  582. dminh= d;\
  583. mx= mx1 + x;\
  584. my= my1 + y;\
  585. }
  586. /* The idea would be to make half pel ME after Inter/Intra decision to
  587. save time. */
  588. static inline int halfpel_motion_search(MpegEncContext * s,
  589. int *mx_ptr, int *my_ptr, int dmin,
  590. int xmin, int ymin, int xmax, int ymax,
  591. int pred_x, int pred_y, uint8_t *ref_picture)
  592. {
  593. UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  594. const int quant= s->qscale;
  595. int pen_x, pen_y;
  596. int mx, my, mx1, my1, d, xx, yy, dminh;
  597. UINT8 *pix, *ptr;
  598. mx = *mx_ptr;
  599. my = *my_ptr;
  600. ptr = ref_picture + (my * s->linesize) + mx;
  601. xx = 16 * s->mb_x;
  602. yy = 16 * s->mb_y;
  603. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  604. dminh = dmin;
  605. if (mx > xmin && mx < xmax &&
  606. my > ymin && my < ymax) {
  607. mx= mx1= 2*(mx - xx);
  608. my= my1= 2*(my - yy);
  609. if(dmin < Z_THRESHOLD && mx==0 && my==0){
  610. *mx_ptr = 0;
  611. *my_ptr = 0;
  612. return dmin;
  613. }
  614. pen_x= pred_x + mx;
  615. pen_y= pred_y + my;
  616. ptr-= s->linesize;
  617. CHECK_HALF_MV(xy2, -1, -1)
  618. CHECK_HALF_MV(y2 , 0, -1)
  619. CHECK_HALF_MV(xy2, +1, -1)
  620. ptr+= s->linesize;
  621. CHECK_HALF_MV(x2 , -1, 0)
  622. CHECK_HALF_MV(x2 , +1, 0)
  623. CHECK_HALF_MV(xy2, -1, +1)
  624. CHECK_HALF_MV(y2 , 0, +1)
  625. CHECK_HALF_MV(xy2, +1, +1)
  626. }else{
  627. mx= 2*(mx - xx);
  628. my= 2*(my - yy);
  629. }
  630. *mx_ptr = mx;
  631. *my_ptr = my;
  632. return dminh;
  633. }
  634. static inline void halfpel_motion_search4(MpegEncContext * s,
  635. int *mx_ptr, int *my_ptr, int dmin,
  636. int xmin, int ymin, int xmax, int ymax,
  637. int pred_x, int pred_y, int block_x, int block_y,
  638. uint8_t *ref_picture)
  639. {
  640. UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  641. const int quant= s->qscale;
  642. int pen_x, pen_y;
  643. int mx, my, mx1, my1, d, xx, yy, dminh;
  644. UINT8 *pix, *ptr;
  645. xx = 8 * block_x;
  646. yy = 8 * block_y;
  647. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  648. mx = *mx_ptr;
  649. my = *my_ptr;
  650. ptr = ref_picture + ((yy+my) * s->linesize) + xx + mx;
  651. dminh = dmin;
  652. if (mx > xmin && mx < xmax &&
  653. my > ymin && my < ymax) {
  654. mx= mx1= 2*mx;
  655. my= my1= 2*my;
  656. if(dmin < Z_THRESHOLD && mx==0 && my==0){
  657. *mx_ptr = 0;
  658. *my_ptr = 0;
  659. return;
  660. }
  661. pen_x= pred_x + mx;
  662. pen_y= pred_y + my;
  663. ptr-= s->linesize;
  664. CHECK_HALF_MV4(xy2, -1, -1)
  665. CHECK_HALF_MV4(y2 , 0, -1)
  666. CHECK_HALF_MV4(xy2, +1, -1)
  667. ptr+= s->linesize;
  668. CHECK_HALF_MV4(x2 , -1, 0)
  669. CHECK_HALF_MV4(x2 , +1, 0)
  670. CHECK_HALF_MV4(xy2, -1, +1)
  671. CHECK_HALF_MV4(y2 , 0, +1)
  672. CHECK_HALF_MV4(xy2, +1, +1)
  673. }else{
  674. mx*=2;
  675. my*=2;
  676. }
  677. *mx_ptr = mx;
  678. *my_ptr = my;
  679. }
  680. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my)
  681. {
  682. const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
  683. s->p_mv_table[xy][0] = mx;
  684. s->p_mv_table[xy][1] = my;
  685. /* has allready been set to the 4 MV if 4MV is done */
  686. if(!(s->flags&CODEC_FLAG_4MV)){
  687. int mot_xy= s->block_index[0];
  688. s->motion_val[mot_xy ][0]= mx;
  689. s->motion_val[mot_xy ][1]= my;
  690. s->motion_val[mot_xy+1][0]= mx;
  691. s->motion_val[mot_xy+1][1]= my;
  692. mot_xy += s->block_wrap[0];
  693. s->motion_val[mot_xy ][0]= mx;
  694. s->motion_val[mot_xy ][1]= my;
  695. s->motion_val[mot_xy+1][0]= mx;
  696. s->motion_val[mot_xy+1][1]= my;
  697. }
  698. }
  699. static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
  700. {
  701. *range = 8 * (1 << (f_code - 1));
  702. /* XXX: temporary kludge to avoid overflow for msmpeg4 */
  703. if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
  704. *range *= 2;
  705. if (s->unrestricted_mv) {
  706. *xmin = -16;
  707. *ymin = -16;
  708. if (s->h263_plus)
  709. *range *= 2;
  710. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
  711. *xmax = s->mb_width*16;
  712. *ymax = s->mb_height*16;
  713. }else {
  714. /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
  715. (cuz the drawn edge isnt large enough))*/
  716. *xmax = s->width;
  717. *ymax = s->height;
  718. }
  719. } else {
  720. *xmin = 0;
  721. *ymin = 0;
  722. *xmax = s->mb_width*16 - 16;
  723. *ymax = s->mb_height*16 - 16;
  724. }
  725. }
  726. void ff_estimate_p_frame_motion(MpegEncContext * s,
  727. int mb_x, int mb_y)
  728. {
  729. UINT8 *pix, *ppix;
  730. int sum, varc, vard, mx, my, range, dmin, xx, yy;
  731. int xmin, ymin, xmax, ymax;
  732. int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
  733. int pred_x=0, pred_y=0;
  734. int P[6][2];
  735. const int shift= 1+s->quarter_sample;
  736. int mb_type=0;
  737. uint8_t *ref_picture= s->last_picture[0];
  738. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
  739. switch(s->me_method) {
  740. case ME_ZERO:
  741. default:
  742. no_motion_search(s, &mx, &my);
  743. dmin = 0;
  744. break;
  745. case ME_FULL:
  746. dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
  747. break;
  748. case ME_LOG:
  749. dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  750. break;
  751. case ME_PHODS:
  752. dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  753. break;
  754. case ME_X1:
  755. case ME_EPZS:
  756. {
  757. const int mot_stride = s->block_wrap[0];
  758. const int mot_xy = s->block_index[0];
  759. rel_xmin= xmin - mb_x*16;
  760. rel_xmax= xmax - mb_x*16;
  761. rel_ymin= ymin - mb_y*16;
  762. rel_ymax= ymax - mb_y*16;
  763. P[0][0] = s->motion_val[mot_xy ][0];
  764. P[0][1] = s->motion_val[mot_xy ][1];
  765. P[1][0] = s->motion_val[mot_xy - 1][0];
  766. P[1][1] = s->motion_val[mot_xy - 1][1];
  767. if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
  768. /* special case for first line */
  769. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
  770. P[4][0] = P[1][0];
  771. P[4][1] = P[1][1];
  772. } else {
  773. P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
  774. P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
  775. P[3][0] = s->motion_val[mot_xy - mot_stride + 2 ][0];
  776. P[3][1] = s->motion_val[mot_xy - mot_stride + 2 ][1];
  777. if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
  778. if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
  779. if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
  780. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  781. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  782. }
  783. if(s->out_format == FMT_H263){
  784. pred_x = P[4][0];
  785. pred_y = P[4][1];
  786. }else { /* mpeg1 at least */
  787. pred_x= P[1][0];
  788. pred_y= P[1][1];
  789. }
  790. }
  791. dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
  792. mx+= mb_x*16;
  793. my+= mb_y*16;
  794. break;
  795. }
  796. if(s->flags&CODEC_FLAG_4MV){
  797. int block;
  798. mb_type|= MB_TYPE_INTER4V;
  799. for(block=0; block<4; block++){
  800. int mx4, my4;
  801. int pred_x4, pred_y4;
  802. int dmin4;
  803. static const int off[4]= {2, 1, 1, -1};
  804. const int mot_stride = s->block_wrap[0];
  805. const int mot_xy = s->block_index[block];
  806. const int block_x= mb_x*2 + (block&1);
  807. const int block_y= mb_y*2 + (block>>1);
  808. const int rel_xmin4= xmin - block_x*8;
  809. const int rel_xmax4= xmax - block_x*8 + 8;
  810. const int rel_ymin4= ymin - block_y*8;
  811. const int rel_ymax4= ymax - block_y*8 + 8;
  812. P[0][0] = s->motion_val[mot_xy ][0];
  813. P[0][1] = s->motion_val[mot_xy ][1];
  814. P[1][0] = s->motion_val[mot_xy - 1][0];
  815. P[1][1] = s->motion_val[mot_xy - 1][1];
  816. if(P[1][0] > (rel_xmax4<<shift)) P[1][0]= (rel_xmax4<<shift);
  817. /* special case for first line */
  818. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
  819. P[4][0] = P[1][0];
  820. P[4][1] = P[1][1];
  821. } else {
  822. P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
  823. P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
  824. P[3][0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
  825. P[3][1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
  826. if(P[2][1] > (rel_ymax4<<shift)) P[2][1]= (rel_ymax4<<shift);
  827. if(P[3][0] < (rel_xmin4<<shift)) P[3][0]= (rel_xmin4<<shift);
  828. if(P[3][0] > (rel_xmax4<<shift)) P[3][0]= (rel_xmax4<<shift);
  829. if(P[3][1] > (rel_ymax4<<shift)) P[3][1]= (rel_ymax4<<shift);
  830. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  831. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  832. }
  833. if(s->out_format == FMT_H263){
  834. pred_x4 = P[4][0];
  835. pred_y4 = P[4][1];
  836. }else { /* mpeg1 at least */
  837. pred_x4= P[1][0];
  838. pred_y4= P[1][1];
  839. }
  840. P[5][0]= mx - mb_x*16;
  841. P[5][1]= my - mb_y*16;
  842. dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture);
  843. halfpel_motion_search4(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
  844. pred_x4, pred_y4, block_x, block_y, ref_picture);
  845. s->motion_val[ s->block_index[block] ][0]= mx4;
  846. s->motion_val[ s->block_index[block] ][1]= my4;
  847. }
  848. }
  849. /* intra / predictive decision */
  850. xx = mb_x * 16;
  851. yy = mb_y * 16;
  852. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  853. /* At this point (mx,my) are full-pell and the absolute displacement */
  854. ppix = ref_picture + (my * s->linesize) + mx;
  855. sum = pix_sum(pix, s->linesize);
  856. #if 0
  857. varc = pix_dev(pix, s->linesize, (sum+128)>>8) + INTER_BIAS;
  858. vard = pix_abs16x16(pix, ppix, s->linesize);
  859. #else
  860. sum= (sum+8)>>4;
  861. varc = ((pix_norm1(pix, s->linesize) - sum*sum + 128 + 500)>>8);
  862. vard = (pix_norm(pix, ppix, s->linesize)+128)>>8;
  863. #endif
  864. s->mb_var[s->mb_width * mb_y + mb_x] = varc;
  865. s->avg_mb_var+= varc;
  866. s->mc_mb_var += vard;
  867. #if 0
  868. printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
  869. varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
  870. #endif
  871. if(s->flags&CODEC_FLAG_HQ){
  872. if (vard*2 + 200 > varc)
  873. mb_type|= MB_TYPE_INTRA;
  874. if (varc*2 + 200 > vard){
  875. mb_type|= MB_TYPE_INTER;
  876. halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
  877. }else{
  878. mx = mx*2 - mb_x*32;
  879. my = my*2 - mb_y*32;
  880. }
  881. }else{
  882. if (vard <= 64 || vard < varc) {
  883. mb_type|= MB_TYPE_INTER;
  884. if (s->me_method != ME_ZERO) {
  885. halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
  886. } else {
  887. mx -= 16 * mb_x;
  888. my -= 16 * mb_y;
  889. }
  890. #if 0
  891. if (vard < 10) {
  892. skip++;
  893. fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d",
  894. skip, vard, varc, dmin);
  895. }
  896. #endif
  897. }else{
  898. mb_type|= MB_TYPE_INTRA;
  899. mx = 0;//mx*2 - 32 * mb_x;
  900. my = 0;//my*2 - 32 * mb_y;
  901. }
  902. }
  903. s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  904. set_p_mv_tables(s, mx, my);
  905. }
  906. int ff_estimate_motion_b(MpegEncContext * s,
  907. int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code)
  908. {
  909. int mx, my, range, dmin;
  910. int xmin, ymin, xmax, ymax;
  911. int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
  912. int pred_x=0, pred_y=0;
  913. int P[6][2];
  914. const int shift= 1+s->quarter_sample;
  915. const int mot_stride = s->mb_width + 2;
  916. const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
  917. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
  918. switch(s->me_method) {
  919. case ME_ZERO:
  920. default:
  921. no_motion_search(s, &mx, &my);
  922. dmin = 0;
  923. break;
  924. case ME_FULL:
  925. dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
  926. break;
  927. case ME_LOG:
  928. dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  929. break;
  930. case ME_PHODS:
  931. dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  932. break;
  933. case ME_X1:
  934. case ME_EPZS:
  935. {
  936. rel_xmin= xmin - mb_x*16;
  937. rel_xmax= xmax - mb_x*16;
  938. rel_ymin= ymin - mb_y*16;
  939. rel_ymax= ymax - mb_y*16;
  940. P[0][0] = mv_table[mot_xy ][0];
  941. P[0][1] = mv_table[mot_xy ][1];
  942. P[1][0] = mv_table[mot_xy - 1][0];
  943. P[1][1] = mv_table[mot_xy - 1][1];
  944. if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
  945. /* special case for first line */
  946. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
  947. P[4][0] = P[1][0];
  948. P[4][1] = P[1][1];
  949. } else {
  950. P[2][0] = mv_table[mot_xy - mot_stride ][0];
  951. P[2][1] = mv_table[mot_xy - mot_stride ][1];
  952. P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0];
  953. P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1];
  954. if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
  955. if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
  956. if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
  957. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  958. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  959. }
  960. pred_x= P[1][0];
  961. pred_y= P[1][1];
  962. }
  963. dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
  964. mx+= mb_x*16;
  965. my+= mb_y*16;
  966. break;
  967. }
  968. /* intra / predictive decision */
  969. // xx = mb_x * 16;
  970. // yy = mb_y * 16;
  971. // pix = s->new_picture[0] + (yy * s->linesize) + xx;
  972. /* At this point (mx,my) are full-pell and the absolute displacement */
  973. // ppix = ref_picture + (my * s->linesize) + mx;
  974. dmin= halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
  975. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  976. mv_table[mot_xy][0]= mx;
  977. mv_table[mot_xy][1]= my;
  978. return dmin;
  979. }
  980. static inline int check_bidir_mv(MpegEncContext * s,
  981. int mb_x, int mb_y,
  982. int motion_fx, int motion_fy,
  983. int motion_bx, int motion_by,
  984. int pred_fx, int pred_fy,
  985. int pred_bx, int pred_by)
  986. {
  987. //FIXME optimize?
  988. UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  989. uint8_t *dest_y = s->me_scratchpad;
  990. uint8_t *ptr;
  991. int dxy;
  992. int src_x, src_y;
  993. int fbmin;
  994. fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->qscale;
  995. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  996. src_x = mb_x * 16 + (motion_fx >> 1);
  997. src_y = mb_y * 16 + (motion_fy >> 1);
  998. ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
  999. put_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
  1000. put_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
  1001. fbmin += (mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->qscale;
  1002. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1003. src_x = mb_x * 16 + (motion_bx >> 1);
  1004. src_y = mb_y * 16 + (motion_by >> 1);
  1005. ptr = s->next_picture[0] + (src_y * s->linesize) + src_x;
  1006. avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
  1007. avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
  1008. fbmin += pix_abs16x16(s->new_picture[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
  1009. return fbmin;
  1010. }
  1011. /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
  1012. static inline int bidir_refine(MpegEncContext * s,
  1013. int mb_x, int mb_y)
  1014. {
  1015. const int mot_stride = s->mb_width + 2;
  1016. const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
  1017. int fbmin;
  1018. int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
  1019. int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
  1020. int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
  1021. int pred_by= s->b_bidir_back_mv_table[xy-1][1];
  1022. int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  1023. int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  1024. int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  1025. int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  1026. //FIXME do refinement and add flag
  1027. fbmin= check_bidir_mv(s, mb_x, mb_y,
  1028. motion_fx, motion_fy,
  1029. motion_bx, motion_by,
  1030. pred_fx, pred_fy,
  1031. pred_bx, pred_by);
  1032. return fbmin;
  1033. }
  1034. static inline int direct_search(MpegEncContext * s,
  1035. int mb_x, int mb_y)
  1036. {
  1037. int P[6][2];
  1038. const int mot_stride = s->mb_width + 2;
  1039. const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
  1040. int dmin, dmin2;
  1041. int motion_fx, motion_fy, motion_bx, motion_by, motion_bx0, motion_by0;
  1042. int motion_dx, motion_dy;
  1043. const int motion_px= s->p_mv_table[mot_xy][0];
  1044. const int motion_py= s->p_mv_table[mot_xy][1];
  1045. const int time_pp= s->pp_time;
  1046. const int time_bp= s->bp_time;
  1047. const int time_pb= time_pp - time_bp;
  1048. int bx, by;
  1049. int mx, my, mx2, my2;
  1050. uint8_t *ref_picture= s->me_scratchpad - (mb_x + 1 + (mb_y + 1)*s->linesize)*16;
  1051. int16_t (*mv_table)[2]= s->b_direct_mv_table;
  1052. uint16_t *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  1053. /* thanks to iso-mpeg the rounding is different for the zero vector, so we need to handle that ... */
  1054. motion_fx= (motion_px*time_pb)/time_pp;
  1055. motion_fy= (motion_py*time_pb)/time_pp;
  1056. motion_bx0= (-motion_px*time_bp)/time_pp;
  1057. motion_by0= (-motion_py*time_bp)/time_pp;
  1058. motion_dx= motion_dy=0;
  1059. dmin2= check_bidir_mv(s, mb_x, mb_y,
  1060. motion_fx, motion_fy,
  1061. motion_bx0, motion_by0,
  1062. motion_fx, motion_fy,
  1063. motion_bx0, motion_by0) - s->qscale;
  1064. motion_bx= motion_fx - motion_px;
  1065. motion_by= motion_fy - motion_py;
  1066. for(by=-1; by<2; by++){
  1067. for(bx=-1; bx<2; bx++){
  1068. uint8_t *dest_y = s->me_scratchpad + (by+1)*s->linesize*16 + (bx+1)*16;
  1069. uint8_t *ptr;
  1070. int dxy;
  1071. int src_x, src_y;
  1072. const int width= s->width;
  1073. const int height= s->height;
  1074. dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
  1075. src_x = (mb_x + bx) * 16 + (motion_fx >> 1);
  1076. src_y = (mb_y + by) * 16 + (motion_fy >> 1);
  1077. src_x = clip(src_x, -16, width);
  1078. if (src_x == width) dxy &= ~1;
  1079. src_y = clip(src_y, -16, height);
  1080. if (src_y == height) dxy &= ~2;
  1081. ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
  1082. put_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
  1083. put_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
  1084. dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
  1085. src_x = (mb_x + bx) * 16 + (motion_bx >> 1);
  1086. src_y = (mb_y + by) * 16 + (motion_by >> 1);
  1087. src_x = clip(src_x, -16, width);
  1088. if (src_x == width) dxy &= ~1;
  1089. src_y = clip(src_y, -16, height);
  1090. if (src_y == height) dxy &= ~2;
  1091. avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
  1092. avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
  1093. }
  1094. }
  1095. P[0][0] = mv_table[mot_xy ][0];
  1096. P[0][1] = mv_table[mot_xy ][1];
  1097. P[1][0] = mv_table[mot_xy - 1][0];
  1098. P[1][1] = mv_table[mot_xy - 1][1];
  1099. /* special case for first line */
  1100. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
  1101. P[4][0] = P[1][0];
  1102. P[4][1] = P[1][1];
  1103. } else {
  1104. P[2][0] = mv_table[mot_xy - mot_stride ][0];
  1105. P[2][1] = mv_table[mot_xy - mot_stride ][1];
  1106. P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0];
  1107. P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1];
  1108. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  1109. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  1110. }
  1111. dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, -16, -16, 15, 15, ref_picture);
  1112. if(mx==0 && my==0) dmin=99999999; // not representable, due to rounding stuff
  1113. if(dmin2<dmin){
  1114. dmin= dmin2;
  1115. mx=0;
  1116. my=0;
  1117. }
  1118. #if 1
  1119. mx2= mx= mx*2;
  1120. my2= my= my*2;
  1121. for(by=-1; by<2; by++){
  1122. if(my2+by < -32) continue;
  1123. for(bx=-1; bx<2; bx++){
  1124. if(bx==0 && by==0) continue;
  1125. if(mx2+bx < -32) continue;
  1126. dmin2= check_bidir_mv(s, mb_x, mb_y,
  1127. mx2+bx+motion_fx, my2+by+motion_fy,
  1128. mx2+bx+motion_bx, my2+by+motion_by,
  1129. mx2+bx+motion_fx, my2+by+motion_fy,
  1130. motion_bx, motion_by) - s->qscale;
  1131. if(dmin2<dmin){
  1132. dmin=dmin2;
  1133. mx= mx2 + bx;
  1134. my= my2 + by;
  1135. }
  1136. }
  1137. }
  1138. #else
  1139. mx*=2; my*=2;
  1140. #endif
  1141. if(mx==0 && my==0){
  1142. motion_bx= motion_bx0;
  1143. motion_by= motion_by0;
  1144. }
  1145. s->b_direct_mv_table[mot_xy][0]= mx;
  1146. s->b_direct_mv_table[mot_xy][1]= my;
  1147. s->b_direct_forw_mv_table[mot_xy][0]= motion_fx + mx;
  1148. s->b_direct_forw_mv_table[mot_xy][1]= motion_fy + my;
  1149. s->b_direct_back_mv_table[mot_xy][0]= motion_bx + mx;
  1150. s->b_direct_back_mv_table[mot_xy][1]= motion_by + my;
  1151. return dmin;
  1152. }
  1153. void ff_estimate_b_frame_motion(MpegEncContext * s,
  1154. int mb_x, int mb_y)
  1155. {
  1156. const int mot_stride = s->mb_width + 2;
  1157. const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
  1158. const int quant= s->qscale;
  1159. int fmin, bmin, dmin, fbmin;
  1160. int type=0;
  1161. int motion_fx, motion_fy, motion_bx, motion_by;
  1162. dmin= direct_search(s, mb_x, mb_y);
  1163. fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code);
  1164. bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code) - quant;
  1165. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  1166. fbmin= bidir_refine(s, mb_x, mb_y);
  1167. if(s->flags&CODEC_FLAG_HQ){
  1168. type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT;
  1169. }else{
  1170. int score= dmin;
  1171. type=MB_TYPE_DIRECT;
  1172. if(fmin<score){
  1173. score=fmin;
  1174. type= MB_TYPE_FORWARD;
  1175. }
  1176. if(bmin<score){
  1177. score=bmin;
  1178. type= MB_TYPE_BACKWARD;
  1179. }
  1180. if(fbmin<score){
  1181. score=fbmin;
  1182. type= MB_TYPE_BIDIR;
  1183. }
  1184. s->mc_mb_var += score;
  1185. }
  1186. s->mb_type[mb_y*s->mb_width + mb_x]= type;
  1187. }
  1188. /* find best f_code for ME which do unlimited searches */
  1189. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1190. {
  1191. int f_code;
  1192. if(s->me_method>=ME_EPZS){
  1193. int mv_num[8];
  1194. int i, y;
  1195. int loose=0;
  1196. UINT8 * fcode_tab= s->fcode_tab;
  1197. for(i=0; i<8; i++) mv_num[i]=0;
  1198. for(y=0; y<s->mb_height; y++){
  1199. int x;
  1200. int xy= (y+1)* (s->mb_width+2) + 1;
  1201. i= y*s->mb_width;
  1202. for(x=0; x<s->mb_width; x++){
  1203. if(s->mb_type[i] & type){
  1204. mv_num[ fcode_tab[mv_table[xy][0] + MAX_MV] ]++;
  1205. mv_num[ fcode_tab[mv_table[xy][1] + MAX_MV] ]++;
  1206. //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i);
  1207. }
  1208. i++;
  1209. xy++;
  1210. }
  1211. }
  1212. for(i=MAX_FCODE; i>1; i--){
  1213. int threshold;
  1214. loose+= mv_num[i];
  1215. if(s->pict_type==B_TYPE) threshold= 0;
  1216. else threshold= s->mb_num/20; //FIXME
  1217. if(loose > threshold) break;
  1218. }
  1219. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1220. return i;
  1221. /* for(i=0; i<=MAX_FCODE; i++){
  1222. printf("%d ", mv_num[i]);
  1223. }
  1224. printf("\n");*/
  1225. }else{
  1226. return 1;
  1227. }
  1228. }
  1229. void ff_fix_long_p_mvs(MpegEncContext * s)
  1230. {
  1231. const int f_code= s->f_code;
  1232. int y;
  1233. UINT8 * fcode_tab= s->fcode_tab;
  1234. /* clip / convert to intra 16x16 type MVs */
  1235. for(y=0; y<s->mb_height; y++){
  1236. int x;
  1237. int xy= (y+1)* (s->mb_width+2)+1;
  1238. int i= y*s->mb_width;
  1239. for(x=0; x<s->mb_width; x++){
  1240. if(s->mb_type[i]&MB_TYPE_INTER){
  1241. if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
  1242. || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
  1243. || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
  1244. || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
  1245. s->mb_type[i] &= ~MB_TYPE_INTER;
  1246. s->mb_type[i] |= MB_TYPE_INTRA;
  1247. s->p_mv_table[xy][0] = 0;
  1248. s->p_mv_table[xy][1] = 0;
  1249. }
  1250. }
  1251. xy++;
  1252. i++;
  1253. }
  1254. }
  1255. if(s->flags&CODEC_FLAG_4MV){
  1256. const int wrap= 2+ s->mb_width*2;
  1257. /* clip / convert to intra 8x8 type MVs */
  1258. for(y=0; y<s->mb_height; y++){
  1259. int xy= (y*2 + 1)*wrap + 1;
  1260. int i= y*s->mb_width;
  1261. int x;
  1262. for(x=0; x<s->mb_width; x++){
  1263. if(s->mb_type[i]&MB_TYPE_INTER4V){
  1264. int block;
  1265. for(block=0; block<4; block++){
  1266. int off= (block& 1) + (block>>1)*wrap;
  1267. int mx= s->motion_val[ xy + off ][0];
  1268. int my= s->motion_val[ xy + off ][1];
  1269. if( fcode_tab[mx + MAX_MV] > f_code
  1270. || fcode_tab[mx + MAX_MV] == 0
  1271. || fcode_tab[my + MAX_MV] > f_code
  1272. || fcode_tab[my + MAX_MV] == 0 ){
  1273. s->mb_type[i] &= ~MB_TYPE_INTER4V;
  1274. s->mb_type[i] |= MB_TYPE_INTRA;
  1275. }
  1276. }
  1277. xy+=2;
  1278. i++;
  1279. }
  1280. }
  1281. }
  1282. }
  1283. }
  1284. void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
  1285. {
  1286. int y;
  1287. UINT8 * fcode_tab= s->fcode_tab;
  1288. /* clip / convert to intra 16x16 type MVs */
  1289. for(y=0; y<s->mb_height; y++){
  1290. int x;
  1291. int xy= (y+1)* (s->mb_width+2)+1;
  1292. int i= y*s->mb_width;
  1293. for(x=0; x<s->mb_width; x++){
  1294. if(s->mb_type[i]&type){
  1295. if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
  1296. || fcode_tab[mv_table[xy][0] + MAX_MV] == 0
  1297. || fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
  1298. || fcode_tab[mv_table[xy][1] + MAX_MV] == 0 ){
  1299. if(s->mb_type[i]&(~type)) s->mb_type[i] &= ~type;
  1300. else{
  1301. mv_table[xy][0] = 0;
  1302. mv_table[xy][1] = 0;
  1303. //this is certainly bad FIXME
  1304. }
  1305. }
  1306. }
  1307. xy++;
  1308. i++;
  1309. }
  1310. }
  1311. }