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.

1755 lines
56KB

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