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.

1290 lines
37KB

  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 void 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 void 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;
  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. }
  633. static inline void halfpel_motion_search4(MpegEncContext * s,
  634. int *mx_ptr, int *my_ptr, int dmin,
  635. int xmin, int ymin, int xmax, int ymax,
  636. int pred_x, int pred_y, int block_x, int block_y,
  637. uint8_t *ref_picture)
  638. {
  639. UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
  640. const int quant= s->qscale;
  641. int pen_x, pen_y;
  642. int mx, my, mx1, my1, d, xx, yy, dminh;
  643. UINT8 *pix, *ptr;
  644. xx = 8 * block_x;
  645. yy = 8 * block_y;
  646. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  647. mx = *mx_ptr;
  648. my = *my_ptr;
  649. ptr = ref_picture + ((yy+my) * s->linesize) + xx + mx;
  650. dminh = dmin;
  651. if (mx > xmin && mx < xmax &&
  652. my > ymin && my < ymax) {
  653. mx= mx1= 2*mx;
  654. my= my1= 2*my;
  655. if(dmin < Z_THRESHOLD && mx==0 && my==0){
  656. *mx_ptr = 0;
  657. *my_ptr = 0;
  658. return;
  659. }
  660. pen_x= pred_x + mx;
  661. pen_y= pred_y + my;
  662. ptr-= s->linesize;
  663. CHECK_HALF_MV4(xy2, -1, -1)
  664. CHECK_HALF_MV4(y2 , 0, -1)
  665. CHECK_HALF_MV4(xy2, +1, -1)
  666. ptr+= s->linesize;
  667. CHECK_HALF_MV4(x2 , -1, 0)
  668. CHECK_HALF_MV4(x2 , +1, 0)
  669. CHECK_HALF_MV4(xy2, -1, +1)
  670. CHECK_HALF_MV4(y2 , 0, +1)
  671. CHECK_HALF_MV4(xy2, +1, +1)
  672. }else{
  673. mx*=2;
  674. my*=2;
  675. }
  676. *mx_ptr = mx;
  677. *my_ptr = my;
  678. }
  679. static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my)
  680. {
  681. const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
  682. s->p_mv_table[xy][0] = mx;
  683. s->p_mv_table[xy][1] = my;
  684. /* has allready been set to the 4 MV if 4MV is done */
  685. if(!(s->flags&CODEC_FLAG_4MV)){
  686. int mot_xy= s->block_index[0];
  687. s->motion_val[mot_xy ][0]= mx;
  688. s->motion_val[mot_xy ][1]= my;
  689. s->motion_val[mot_xy+1][0]= mx;
  690. s->motion_val[mot_xy+1][1]= my;
  691. mot_xy += s->block_wrap[0];
  692. s->motion_val[mot_xy ][0]= mx;
  693. s->motion_val[mot_xy ][1]= my;
  694. s->motion_val[mot_xy+1][0]= mx;
  695. s->motion_val[mot_xy+1][1]= my;
  696. }
  697. }
  698. static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
  699. {
  700. *range = 8 * (1 << (f_code - 1));
  701. /* XXX: temporary kludge to avoid overflow for msmpeg4 */
  702. if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
  703. *range *= 2;
  704. if (s->unrestricted_mv) {
  705. *xmin = -16;
  706. *ymin = -16;
  707. if (s->h263_plus)
  708. *range *= 2;
  709. if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
  710. *xmax = s->mb_width*16;
  711. *ymax = s->mb_height*16;
  712. }else {
  713. /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
  714. (cuz the drawn edge isnt large enough))*/
  715. *xmax = s->width;
  716. *ymax = s->height;
  717. }
  718. } else {
  719. *xmin = 0;
  720. *ymin = 0;
  721. *xmax = s->mb_width*16 - 16;
  722. *ymax = s->mb_height*16 - 16;
  723. }
  724. }
  725. void ff_estimate_p_frame_motion(MpegEncContext * s,
  726. int mb_x, int mb_y)
  727. {
  728. UINT8 *pix, *ppix;
  729. int sum, varc, vard, mx, my, range, dmin, xx, yy;
  730. int xmin, ymin, xmax, ymax;
  731. int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
  732. int pred_x=0, pred_y=0;
  733. int P[6][2];
  734. const int shift= 1+s->quarter_sample;
  735. int mb_type=0;
  736. uint8_t *ref_picture= s->last_picture[0];
  737. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
  738. switch(s->me_method) {
  739. case ME_ZERO:
  740. default:
  741. no_motion_search(s, &mx, &my);
  742. dmin = 0;
  743. break;
  744. case ME_FULL:
  745. dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
  746. break;
  747. case ME_LOG:
  748. dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  749. break;
  750. case ME_PHODS:
  751. dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  752. break;
  753. case ME_X1:
  754. case ME_EPZS:
  755. {
  756. const int mot_stride = s->block_wrap[0];
  757. const int mot_xy = s->block_index[0];
  758. rel_xmin= xmin - mb_x*16;
  759. rel_xmax= xmax - mb_x*16;
  760. rel_ymin= ymin - mb_y*16;
  761. rel_ymax= ymax - mb_y*16;
  762. P[0][0] = s->motion_val[mot_xy ][0];
  763. P[0][1] = s->motion_val[mot_xy ][1];
  764. P[1][0] = s->motion_val[mot_xy - 1][0];
  765. P[1][1] = s->motion_val[mot_xy - 1][1];
  766. if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
  767. /* special case for first line */
  768. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
  769. P[4][0] = P[1][0];
  770. P[4][1] = P[1][1];
  771. } else {
  772. P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
  773. P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
  774. P[3][0] = s->motion_val[mot_xy - mot_stride + 2 ][0];
  775. P[3][1] = s->motion_val[mot_xy - mot_stride + 2 ][1];
  776. if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
  777. if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
  778. if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
  779. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  780. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  781. }
  782. if(s->out_format == FMT_H263){
  783. pred_x = P[4][0];
  784. pred_y = P[4][1];
  785. }else { /* mpeg1 at least */
  786. pred_x= P[1][0];
  787. pred_y= P[1][1];
  788. }
  789. }
  790. dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
  791. mx+= mb_x*16;
  792. my+= mb_y*16;
  793. break;
  794. }
  795. if(s->flags&CODEC_FLAG_4MV){
  796. int block;
  797. mb_type|= MB_TYPE_INTER4V;
  798. for(block=0; block<4; block++){
  799. int mx4, my4;
  800. int pred_x4, pred_y4;
  801. int dmin4;
  802. static const int off[4]= {2, 1, 1, -1};
  803. const int mot_stride = s->block_wrap[0];
  804. const int mot_xy = s->block_index[block];
  805. const int block_x= mb_x*2 + (block&1);
  806. const int block_y= mb_y*2 + (block>>1);
  807. const int rel_xmin4= xmin - block_x*8;
  808. const int rel_xmax4= xmax - block_x*8 + 8;
  809. const int rel_ymin4= ymin - block_y*8;
  810. const int rel_ymax4= ymax - block_y*8 + 8;
  811. P[0][0] = s->motion_val[mot_xy ][0];
  812. P[0][1] = s->motion_val[mot_xy ][1];
  813. P[1][0] = s->motion_val[mot_xy - 1][0];
  814. P[1][1] = s->motion_val[mot_xy - 1][1];
  815. if(P[1][0] > (rel_xmax4<<shift)) P[1][0]= (rel_xmax4<<shift);
  816. /* special case for first line */
  817. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line) && block<2) {
  818. P[4][0] = P[1][0];
  819. P[4][1] = P[1][1];
  820. } else {
  821. P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
  822. P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
  823. P[3][0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
  824. P[3][1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
  825. if(P[2][1] > (rel_ymax4<<shift)) P[2][1]= (rel_ymax4<<shift);
  826. if(P[3][0] < (rel_xmin4<<shift)) P[3][0]= (rel_xmin4<<shift);
  827. if(P[3][0] > (rel_xmax4<<shift)) P[3][0]= (rel_xmax4<<shift);
  828. if(P[3][1] > (rel_ymax4<<shift)) P[3][1]= (rel_ymax4<<shift);
  829. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  830. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  831. }
  832. if(s->out_format == FMT_H263){
  833. pred_x4 = P[4][0];
  834. pred_y4 = P[4][1];
  835. }else { /* mpeg1 at least */
  836. pred_x4= P[1][0];
  837. pred_y4= P[1][1];
  838. }
  839. P[5][0]= mx - mb_x*16;
  840. P[5][1]= my - mb_y*16;
  841. dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture);
  842. halfpel_motion_search4(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
  843. pred_x4, pred_y4, block_x, block_y, ref_picture);
  844. s->motion_val[ s->block_index[block] ][0]= mx4;
  845. s->motion_val[ s->block_index[block] ][1]= my4;
  846. }
  847. }
  848. /* intra / predictive decision */
  849. xx = mb_x * 16;
  850. yy = mb_y * 16;
  851. pix = s->new_picture[0] + (yy * s->linesize) + xx;
  852. /* At this point (mx,my) are full-pell and the absolute displacement */
  853. ppix = ref_picture + (my * s->linesize) + mx;
  854. sum = pix_sum(pix, s->linesize);
  855. #if 0
  856. varc = pix_dev(pix, s->linesize, (sum+128)>>8) + INTER_BIAS;
  857. vard = pix_abs16x16(pix, ppix, s->linesize);
  858. #else
  859. sum= (sum+8)>>4;
  860. varc = ((pix_norm1(pix, s->linesize) - sum*sum + 128 + 500)>>8);
  861. vard = (pix_norm(pix, ppix, s->linesize)+128)>>8;
  862. #endif
  863. s->mb_var[s->mb_width * mb_y + mb_x] = varc;
  864. s->avg_mb_var+= varc;
  865. s->mc_mb_var += vard;
  866. #if 0
  867. printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
  868. varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
  869. #endif
  870. if(s->flags&CODEC_FLAG_HQ){
  871. if (vard*2 + 200 > varc)
  872. mb_type|= MB_TYPE_INTRA;
  873. if (varc*2 + 200 > vard){
  874. mb_type|= MB_TYPE_INTER;
  875. halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
  876. }else{
  877. mx = mx*2 - mb_x*32;
  878. my = my*2 - mb_y*32;
  879. }
  880. }else{
  881. if (vard <= 64 || vard < varc) {
  882. mb_type|= MB_TYPE_INTER;
  883. if (s->me_method != ME_ZERO) {
  884. halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
  885. } else {
  886. mx -= 16 * mb_x;
  887. my -= 16 * mb_y;
  888. }
  889. #if 0
  890. if (vard < 10) {
  891. skip++;
  892. fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d",
  893. skip, vard, varc, dmin);
  894. }
  895. #endif
  896. }else{
  897. mb_type|= MB_TYPE_INTRA;
  898. mx = 0;//mx*2 - 32 * mb_x;
  899. my = 0;//my*2 - 32 * mb_y;
  900. }
  901. }
  902. s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  903. set_p_mv_tables(s, mx, my);
  904. }
  905. void ff_estimate_motion_b(MpegEncContext * s,
  906. int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code)
  907. {
  908. UINT8 *pix, *ppix;
  909. int sum, varc, vard, mx, my, range, dmin, xx, yy;
  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. int mb_type=0;
  916. const int mot_stride = s->mb_width + 2;
  917. const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
  918. get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
  919. switch(s->me_method) {
  920. case ME_ZERO:
  921. default:
  922. no_motion_search(s, &mx, &my);
  923. dmin = 0;
  924. break;
  925. case ME_FULL:
  926. dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
  927. break;
  928. case ME_LOG:
  929. dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  930. break;
  931. case ME_PHODS:
  932. dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
  933. break;
  934. case ME_X1:
  935. case ME_EPZS:
  936. {
  937. rel_xmin= xmin - mb_x*16;
  938. rel_xmax= xmax - mb_x*16;
  939. rel_ymin= ymin - mb_y*16;
  940. rel_ymax= ymax - mb_y*16;
  941. P[0][0] = mv_table[mot_xy ][0];
  942. P[0][1] = mv_table[mot_xy ][1];
  943. P[1][0] = mv_table[mot_xy - 1][0];
  944. P[1][1] = mv_table[mot_xy - 1][1];
  945. if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
  946. /* special case for first line */
  947. if ((mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
  948. P[4][0] = P[1][0];
  949. P[4][1] = P[1][1];
  950. } else {
  951. P[2][0] = mv_table[mot_xy - mot_stride ][0];
  952. P[2][1] = mv_table[mot_xy - mot_stride ][1];
  953. P[3][0] = mv_table[mot_xy - mot_stride + 1 ][0];
  954. P[3][1] = mv_table[mot_xy - mot_stride + 1 ][1];
  955. if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
  956. if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
  957. if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
  958. P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
  959. P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
  960. }
  961. pred_x= P[1][0];
  962. pred_y= P[1][1];
  963. }
  964. dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
  965. mx+= mb_x*16;
  966. my+= mb_y*16;
  967. break;
  968. }
  969. /* intra / predictive decision */
  970. // xx = mb_x * 16;
  971. // yy = mb_y * 16;
  972. // pix = s->new_picture[0] + (yy * s->linesize) + xx;
  973. /* At this point (mx,my) are full-pell and the absolute displacement */
  974. // ppix = ref_picture + (my * s->linesize) + mx;
  975. halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y, ref_picture);
  976. // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
  977. mv_table[mot_xy][0]= mx;
  978. mv_table[mot_xy][1]= my;
  979. }
  980. int ff_decide_type(MpegEncContext * s,
  981. int mb_x, int mb_y)
  982. {
  983. }
  984. void ff_estimate_b_frame_motion(MpegEncContext * s,
  985. int mb_x, int mb_y)
  986. {
  987. const int mot_stride = s->mb_width + 2;
  988. const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
  989. ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code);
  990. ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code);
  991. //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
  992. s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
  993. s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
  994. s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
  995. s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
  996. s->mb_type[mb_y*s->mb_width + mb_x]= MB_TYPE_FORWARD; //FIXME
  997. }
  998. /* find best f_code for ME which do unlimited searches */
  999. int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
  1000. {
  1001. int f_code;
  1002. if(s->me_method>=ME_EPZS){
  1003. int mv_num[8];
  1004. int i, y;
  1005. int loose=0;
  1006. UINT8 * fcode_tab= s->fcode_tab;
  1007. for(i=0; i<8; i++) mv_num[i]=0;
  1008. for(y=0; y<s->mb_height; y++){
  1009. int x;
  1010. int xy= (y+1)* (s->mb_width+2) + 1;
  1011. i= y*s->mb_width;
  1012. for(x=0; x<s->mb_width; x++){
  1013. if(s->mb_type[i] & type){
  1014. mv_num[ fcode_tab[mv_table[xy][0] + MAX_MV] ]++;
  1015. mv_num[ fcode_tab[mv_table[xy][1] + MAX_MV] ]++;
  1016. //printf("%d %d %d\n", s->mv_table[0][i], fcode_tab[s->mv_table[0][i] + MAX_MV], i);
  1017. }
  1018. i++;
  1019. xy++;
  1020. }
  1021. }
  1022. for(i=MAX_FCODE; i>1; i--){
  1023. loose+= mv_num[i];
  1024. if(loose > s->mb_num/20) break; //FIXME this is pretty ineffective
  1025. }
  1026. // printf("fcode: %d type: %d\n", i, s->pict_type);
  1027. return i;
  1028. /* for(i=0; i<=MAX_FCODE; i++){
  1029. printf("%d ", mv_num[i]);
  1030. }
  1031. printf("\n");*/
  1032. }else{
  1033. return 1;
  1034. }
  1035. }
  1036. void ff_fix_long_p_mvs(MpegEncContext * s)
  1037. {
  1038. const int f_code= s->f_code;
  1039. int y;
  1040. UINT8 * fcode_tab= s->fcode_tab;
  1041. /* clip / convert to intra 16x16 type MVs */
  1042. for(y=0; y<s->mb_height; y++){
  1043. int x;
  1044. int xy= (y+1)* (s->mb_width+2)+1;
  1045. int i= y*s->mb_width;
  1046. for(x=0; x<s->mb_width; x++){
  1047. if(s->mb_type[i]&MB_TYPE_INTER){
  1048. if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
  1049. || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
  1050. || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
  1051. || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
  1052. s->mb_type[i] &= ~MB_TYPE_INTER;
  1053. s->mb_type[i] |= MB_TYPE_INTRA;
  1054. s->p_mv_table[xy][0] = 0;
  1055. s->p_mv_table[xy][1] = 0;
  1056. }
  1057. }
  1058. xy++;
  1059. i++;
  1060. }
  1061. }
  1062. if(s->flags&CODEC_FLAG_4MV){
  1063. const int wrap= 2+ s->mb_width*2;
  1064. /* clip / convert to intra 8x8 type MVs */
  1065. for(y=0; y<s->mb_height; y++){
  1066. int xy= (y*2 + 1)*wrap + 1;
  1067. int i= y*s->mb_width;
  1068. int x;
  1069. for(x=0; x<s->mb_width; x++){
  1070. if(s->mb_type[i]&MB_TYPE_INTER4V){
  1071. int block;
  1072. for(block=0; block<4; block++){
  1073. int off= (block& 1) + (block>>1)*wrap;
  1074. int mx= s->motion_val[ xy + off ][0];
  1075. int my= s->motion_val[ xy + off ][1];
  1076. if( fcode_tab[mx + MAX_MV] > f_code
  1077. || fcode_tab[mx + MAX_MV] == 0
  1078. || fcode_tab[my + MAX_MV] > f_code
  1079. || fcode_tab[my + MAX_MV] == 0 ){
  1080. s->mb_type[i] &= ~MB_TYPE_INTER4V;
  1081. s->mb_type[i] |= MB_TYPE_INTRA;
  1082. }
  1083. }
  1084. xy+=2;
  1085. i++;
  1086. }
  1087. }
  1088. }
  1089. }
  1090. }
  1091. void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
  1092. {
  1093. int y;
  1094. UINT8 * fcode_tab= s->fcode_tab;
  1095. /* clip / convert to intra 16x16 type MVs */
  1096. for(y=0; y<s->mb_height; y++){
  1097. int x;
  1098. int xy= (y+1)* (s->mb_width+2)+1;
  1099. int i= y*s->mb_width;
  1100. for(x=0; x<s->mb_width; x++){
  1101. if(s->mb_type[i]&type){
  1102. if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
  1103. || fcode_tab[mv_table[xy][0] + MAX_MV] == 0
  1104. || fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
  1105. || fcode_tab[mv_table[xy][1] + MAX_MV] == 0 ){
  1106. s->mb_type[i] &= ~type;
  1107. if(s->mb_type[i]==0) s->mb_type[i]= MB_TYPE_FORWARD; //FIXME
  1108. mv_table[xy][0] = 0;
  1109. mv_table[xy][1] = 0;
  1110. //this is certainly bad FIXME
  1111. }
  1112. }
  1113. xy++;
  1114. i++;
  1115. }
  1116. }
  1117. }