3 * Copyright (c) 2000,2001 Gerard Lantau.
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.
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.
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.
20 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
26 #include "mpegvideo.h"
28 #define ABS(a) ((a)>0 ? (a) : -(a))
29 #define INTER_BIAS 257
31 static void halfpel_motion_search(MpegEncContext * s,
32 int *mx_ptr, int *my_ptr, int dmin,
33 int xmin, int ymin, int xmax, int ymax,
34 int pred_x, int pred_y);
36 /* config it to test motion vector encoding (send random vectors) */
37 //#define CONFIG_TEST_MV_ENCODE
39 static int pix_sum(UINT8 * pix, int line_size)
44 for (i = 0; i < 16; i++) {
45 for (j = 0; j < 16; j += 8) {
56 pix += line_size - 16;
61 static int pix_dev(UINT8 * pix, int line_size, int mean)
66 for (i = 0; i < 16; i++) {
67 for (j = 0; j < 16; j += 8) {
68 s += ABS(pix[0]-mean);
69 s += ABS(pix[1]-mean);
70 s += ABS(pix[2]-mean);
71 s += ABS(pix[3]-mean);
72 s += ABS(pix[4]-mean);
73 s += ABS(pix[5]-mean);
74 s += ABS(pix[6]-mean);
75 s += ABS(pix[7]-mean);
78 pix += line_size - 16;
83 static int pix_norm1(UINT8 * pix, int line_size)
86 UINT32 *sq = squareTbl + 256;
89 for (i = 0; i < 16; i++) {
90 for (j = 0; j < 16; j += 8) {
101 pix += line_size - 16;
106 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
109 UINT32 *sq = squareTbl + 256;
112 for (i = 0; i < 16; i++) {
113 for (j = 0; j < 16; j += 8) {
114 s += sq[pix1[0] - pix2[0]];
115 s += sq[pix1[1] - pix2[1]];
116 s += sq[pix1[2] - pix2[2]];
117 s += sq[pix1[3] - pix2[3]];
118 s += sq[pix1[4] - pix2[4]];
119 s += sq[pix1[5] - pix2[5]];
120 s += sq[pix1[6] - pix2[6]];
121 s += sq[pix1[7] - pix2[7]];
125 pix1 += line_size - 16;
126 pix2 += line_size - 16;
131 static void no_motion_search(MpegEncContext * s,
132 int *mx_ptr, int *my_ptr)
134 *mx_ptr = 16 * s->mb_x;
135 *my_ptr = 16 * s->mb_y;
138 static int full_motion_search(MpegEncContext * s,
139 int *mx_ptr, int *my_ptr, int range,
140 int xmin, int ymin, int xmax, int ymax)
142 int x1, y1, x2, y2, xx, yy, x, y;
148 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
160 pix = s->new_picture[0] + (yy * s->linesize) + xx;
164 for (y = y1; y <= y2; y++) {
165 for (x = x1; x <= x2; x++) {
166 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x,
170 (abs(x - xx) + abs(y - yy)) <
171 (abs(mx - xx) + abs(my - yy)))) {
183 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
184 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
185 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
192 static int log_motion_search(MpegEncContext * s,
193 int *mx_ptr, int *my_ptr, int range,
194 int xmin, int ymin, int xmax, int ymax)
196 int x1, y1, x2, y2, xx, yy, x, y;
223 pix = s->new_picture[0] + (yy * s->linesize) + xx;
229 for (y = y1; y <= y2; y += range) {
230 for (x = x1; x <= x2; x += range) {
231 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16);
232 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
258 } while (range >= 1);
261 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
268 static int phods_motion_search(MpegEncContext * s,
269 int *mx_ptr, int *my_ptr, int range,
270 int xmin, int ymin, int xmax, int ymax)
272 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
273 int mx, my, dminx, dminy;
299 pix = s->new_picture[0] + (yy * s->linesize) + xx;
310 for (x = x1; x <= x2; x += range) {
311 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16);
312 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
319 for (y = y1; y <= y2; y += range) {
320 d = pix_abs16x16(pix, s->last_picture[0] + (y * s->linesize) + x, s->linesize, 16);
321 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
347 } while (range >= 1);
350 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
353 /* half pixel search */
360 #define Z_THRESHOLD 256
362 #define CHECK_MV(x,y)\
364 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);\
365 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
373 #define CHECK_MV_DIR(x,y,new_dir)\
375 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);\
376 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
385 #define check(x,y,S,v)\
386 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d xmin" #v, (x), (y), s->mb_x, s->mb_y);\
387 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d xmax" #v, (x), (y), s->mb_x, s->mb_y);\
388 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d ymin" #v, (x), (y), s->mb_x, s->mb_y);\
389 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d ymax" #v, (x), (y), s->mb_x, s->mb_y);\
392 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
393 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
394 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
395 int xmin, int ymin, int xmax, int ymax, int shift)
401 const int dir= next_dir;
402 const int x= best[0];
403 const int y= best[1];
407 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
408 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
409 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
410 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
419 const int x= best[0];
420 const int y= best[1];
421 const int last_min=dmin;
422 if(x>xmin) CHECK_MV(x-1, y )
423 if(y>xmin) CHECK_MV(x , y-1)
424 if(x<xmax) CHECK_MV(x+1, y )
425 if(y<xmax) CHECK_MV(x , y+1)
426 if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)
427 if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)
428 if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)
429 if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)
430 if(x-1>xmin) CHECK_MV(x-2, y )
431 if(y-1>xmin) CHECK_MV(x , y-2)
432 if(x+1<xmax) CHECK_MV(x+2, y )
433 if(y+1<xmax) CHECK_MV(x , y+2)
434 if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)
435 if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)
436 if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)
437 if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)
438 if(dmin==last_min) return dmin;
443 static inline int snake_search(MpegEncContext * s, int *best, int dmin,
444 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
445 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
446 int xmin, int ymin, int xmax, int ymax, int shift)
450 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1};
451 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1};
453 int last_d[2]={dmin, dmin};
460 if(256*256*256*64%point==0)
462 printf("%d %d %d\n", good, bad, point);
471 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
472 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride, 16);
473 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
475 d = dmin + 10000; //FIXME smarter boundary handling
482 if(last_d[1] - last_d[0] > last_d[0] - d) c= -c;
492 if(fails>=3) return dmin;
503 static int epzs_motion_search(MpegEncContext * s,
504 int *mx_ptr, int *my_ptr,
505 int P[5][2], int pred_x, int pred_y,
506 int xmin, int ymin, int xmax, int ymax)
510 UINT8 *new_pic, *old_pic;
511 const int pic_stride= s->linesize;
512 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16;
513 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
514 int quant= s->qscale; // qscale of the prev frame
515 const int shift= 1+s->quarter_sample;
517 new_pic = s->new_picture[0] + pic_xy;
518 old_pic = s->last_picture[0] + pic_xy;
520 dmin = pix_abs16x16(new_pic, old_pic, pic_stride, 16);
521 if(dmin<Z_THRESHOLD){
529 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
530 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
532 CHECK_MV(P[4][0]>>shift, P[4][1]>>shift)
533 if(dmin<Z_THRESHOLD){
534 *mx_ptr= P[4][0]>>shift;
535 *my_ptr= P[4][1]>>shift;
539 CHECK_MV(P[1][0]>>shift, P[1][1]>>shift)
540 CHECK_MV(P[2][0]>>shift, P[2][1]>>shift)
541 CHECK_MV(P[3][0]>>shift, P[3][1]>>shift)
543 CHECK_MV(P[0][0]>>shift, P[0][1]>>shift)
545 //check(best[0],best[1],0, b0)
546 if(s->full_search==ME_EPZS)
547 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
548 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
550 dmin= snake_search(s, best, dmin, new_pic, old_pic, pic_stride,
551 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax, shift);
552 //check(best[0],best[1],0, b1)
556 // printf("%d %d %d \n", best[0], best[1], dmin);
560 #define CHECK_HALF_MV(suffix, x, y) \
561 d= pix_abs16x16_ ## suffix(pix, ptr+((x)>>1), s->linesize, 16);\
562 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
569 /* The idea would be to make half pel ME after Inter/Intra decision to
571 static inline void halfpel_motion_search(MpegEncContext * s,
572 int *mx_ptr, int *my_ptr, int dmin,
573 int xmin, int ymin, int xmax, int ymax,
574 int pred_x, int pred_y)
576 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
577 const int quant= s->qscale;
579 int mx, my, mx1, my1, d, xx, yy, dminh;
585 ptr = s->last_picture[0] + (my * s->linesize) + mx;
589 pix = s->new_picture[0] + (yy * s->linesize) + xx;
593 if (mx > xmin && mx < xmax &&
594 my > ymin && my < ymax) {
596 mx= mx1= 2*(mx - xx);
597 my= my1= 2*(my - yy);
598 if(dmin < Z_THRESHOLD && mx==0 && my==0){
608 CHECK_HALF_MV(xy2, -1, -1)
609 CHECK_HALF_MV(y2 , 0, -1)
610 CHECK_HALF_MV(xy2, +1, -1)
613 CHECK_HALF_MV(x2 , -1, 0)
614 CHECK_HALF_MV(x2 , +1, 0)
615 CHECK_HALF_MV(xy2, -1, +1)
616 CHECK_HALF_MV(y2 , 0, +1)
617 CHECK_HALF_MV(xy2, +1, +1)
627 #ifndef CONFIG_TEST_MV_ENCODE
629 int estimate_motion(MpegEncContext * s,
631 int *mx_ptr, int *my_ptr)
634 int sum, varc, vard, mx, my, range, dmin, xx, yy;
635 int xmin, ymin, xmax, ymax;
636 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
637 int pred_x=0, pred_y=0;
639 const int shift= 1+s->quarter_sample;
641 range = 8 * (1 << (s->f_code - 1));
642 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
643 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
646 if (s->unrestricted_mv) {
651 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
652 xmax = s->mb_width*16;
653 ymax = s->mb_height*16;
655 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
656 (cuz the drawn edge isnt large enough))*/
663 xmax = s->mb_width*16 - 16;
664 ymax = s->mb_height*16 - 16;
666 switch(s->full_search) {
669 no_motion_search(s, &mx, &my);
673 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax);
676 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax);
679 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax);
681 case ME_X1: // just reserving some space for experiments ...
683 rel_xmin= xmin - s->mb_x*16;
684 rel_xmax= xmax - s->mb_x*16;
685 rel_ymin= ymin - s->mb_y*16;
686 rel_ymax= ymax - s->mb_y*16;
687 if(s->out_format == FMT_H263){
688 static const int off[4]= {2, 1, 1, -1};
689 const int mot_stride = s->mb_width*2 + 2;
690 const int mot_xy = (s->mb_y*2 + 1)*mot_stride + s->mb_x*2 + 1;
692 P[0][0] = s->motion_val[mot_xy ][0];
693 P[0][1] = s->motion_val[mot_xy ][1];
694 P[1][0] = s->motion_val[mot_xy - 1][0];
695 P[1][1] = s->motion_val[mot_xy - 1][1];
696 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
698 /* special case for first line */
699 if ((s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
703 P[2][0] = s->motion_val[mot_xy - mot_stride ][0];
704 P[2][1] = s->motion_val[mot_xy - mot_stride ][1];
705 P[3][0] = s->motion_val[mot_xy - mot_stride + off[0] ][0];
706 P[3][1] = s->motion_val[mot_xy - mot_stride + off[0] ][1];
707 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
708 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
709 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
711 P[4][0]= pred_x = mid_pred(P[1][0], P[2][0], P[3][0]);
712 P[4][1]= pred_y = mid_pred(P[1][1], P[2][1], P[3][1]);
715 const int xy= s->mb_y*s->mb_width + s->mb_x;
716 pred_x= s->last_mv[0][0][0];
717 pred_y= s->last_mv[0][0][1];
719 P[0][0]= s->mv_table[0][xy ];
720 P[0][1]= s->mv_table[1][xy ];
725 P[1][0]= s->mv_table[0][xy-1];
726 P[1][1]= s->mv_table[1][xy-1];
727 if(P[1][0] > (rel_xmax<<shift)) P[1][0]= (rel_xmax<<shift);
730 if (!(s->mb_y == 0 || s->first_slice_line || s->first_gob_line)) {
731 P[2][0] = s->mv_table[0][xy - s->mb_width];
732 P[2][1] = s->mv_table[1][xy - s->mb_width];
733 P[3][0] = s->mv_table[0][xy - s->mb_width+1];
734 P[3][1] = s->mv_table[1][xy - s->mb_width+1];
735 if(P[2][1] > (rel_ymax<<shift)) P[2][1]= (rel_ymax<<shift);
736 if(P[3][0] > (rel_xmax<<shift)) P[3][0]= (rel_xmax<<shift);
737 if(P[3][0] < (rel_xmin<<shift)) P[3][0]= (rel_xmin<<shift);
738 if(P[3][1] > (rel_ymax<<shift)) P[3][1]= (rel_ymax<<shift);
740 P[4][0]= mid_pred(P[1][0], P[2][0], P[3][0]);
741 P[4][1]= mid_pred(P[1][1], P[2][1], P[3][1]);
744 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax);
751 /* intra / predictive decision */
755 pix = s->new_picture[0] + (yy * s->linesize) + xx;
756 /* At this point (mx,my) are full-pell and the absolute displacement */
757 ppix = s->last_picture[0] + (my * s->linesize) + mx;
759 sum = pix_sum(pix, s->linesize);
760 varc = pix_dev(pix, s->linesize, (sum+128)>>8);
761 vard = pix_abs16x16(pix, ppix, s->linesize, 16);
763 s->mb_var[s->mb_width * mb_y + mb_x] = varc;
764 s->avg_mb_var += varc;
765 s->mc_mb_var += vard;
768 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
769 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
771 if (vard <= 64 || vard < varc + INTER_BIAS) {
772 if (s->full_search != ME_ZERO) {
773 halfpel_motion_search(s, &mx, &my, dmin, xmin, ymin, xmax, ymax, pred_x, pred_y);
778 // check(mx + 32*s->mb_x, my + 32*s->mb_y, 1, end)
792 /* test version which generates valid random vectors */
793 int estimate_motion(MpegEncContext * s,
795 int *mx_ptr, int *my_ptr)
797 int xx, yy, x1, y1, x2, y2, range;
799 if ((random() % 10) >= 5) {
800 range = 8 * (1 << (s->f_code - 1));
801 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
810 if (x2 > (s->width - 16))
816 if (y2 > (s->height - 16))
819 *mx_ptr = (random() % (2 * (x2 - x1 + 1))) + 2 * (x1 - xx);
820 *my_ptr = (random() % (2 * (y2 - y1 + 1))) + 2 * (y1 - yy);