3 * Copyright (c) 2000,2001 Fabrice Bellard.
4 * Copyright (c) 2002 Michael Niedermayer
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.
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.
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
21 * new Motion Estimation (X1/EPZS) by Michael Niedermayer <michaelni@gmx.at>
27 #include "mpegvideo.h"
29 #define SQ(a) ((a)*(a))
30 #define INTER_BIAS 257
35 #define P_TOPRIGHT P[3]
37 #define P_LAST_LEFT P[5]
38 #define P_LAST_RIGHT P[6]
39 #define P_LAST_TOP P[7]
40 #define P_LAST_BOTTOM P[8]
44 static int pix_sum(UINT8 * pix, int line_size)
49 for (i = 0; i < 16; i++) {
50 for (j = 0; j < 16; j += 8) {
61 pix += line_size - 16;
66 static int pix_dev(UINT8 * pix, int line_size, int mean)
71 for (i = 0; i < 16; i++) {
72 for (j = 0; j < 16; j += 8) {
73 s += ABS(pix[0]-mean);
74 s += ABS(pix[1]-mean);
75 s += ABS(pix[2]-mean);
76 s += ABS(pix[3]-mean);
77 s += ABS(pix[4]-mean);
78 s += ABS(pix[5]-mean);
79 s += ABS(pix[6]-mean);
80 s += ABS(pix[7]-mean);
83 pix += line_size - 16;
88 static int pix_norm1(UINT8 * pix, int line_size)
91 UINT32 *sq = squareTbl + 256;
94 for (i = 0; i < 16; i++) {
95 for (j = 0; j < 16; j += 8) {
106 pix += line_size - 16;
111 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
114 UINT32 *sq = squareTbl + 256;
117 for (i = 0; i < 16; i++) {
118 for (j = 0; j < 16; j += 8) {
119 s += sq[pix1[0] - pix2[0]];
120 s += sq[pix1[1] - pix2[1]];
121 s += sq[pix1[2] - pix2[2]];
122 s += sq[pix1[3] - pix2[3]];
123 s += sq[pix1[4] - pix2[4]];
124 s += sq[pix1[5] - pix2[5]];
125 s += sq[pix1[6] - pix2[6]];
126 s += sq[pix1[7] - pix2[7]];
130 pix1 += line_size - 16;
131 pix2 += line_size - 16;
136 static void no_motion_search(MpegEncContext * s,
137 int *mx_ptr, int *my_ptr)
139 *mx_ptr = 16 * s->mb_x;
140 *my_ptr = 16 * s->mb_y;
143 static int full_motion_search(MpegEncContext * s,
144 int *mx_ptr, int *my_ptr, int range,
145 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
147 int x1, y1, x2, y2, xx, yy, x, y;
153 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
165 pix = s->new_picture[0] + (yy * s->linesize) + xx;
169 for (y = y1; y <= y2; y++) {
170 for (x = x1; x <= x2; x++) {
171 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
175 (abs(x - xx) + abs(y - yy)) <
176 (abs(mx - xx) + abs(my - yy)))) {
188 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
189 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
190 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
197 static int log_motion_search(MpegEncContext * s,
198 int *mx_ptr, int *my_ptr, int range,
199 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
201 int x1, y1, x2, y2, xx, yy, x, y;
228 pix = s->new_picture[0] + (yy * s->linesize) + xx;
234 for (y = y1; y <= y2; y += range) {
235 for (x = x1; x <= x2; x += range) {
236 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
237 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
263 } while (range >= 1);
266 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
273 static int phods_motion_search(MpegEncContext * s,
274 int *mx_ptr, int *my_ptr, int range,
275 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
277 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
278 int mx, my, dminx, dminy;
304 pix = s->new_picture[0] + (yy * s->linesize) + xx;
315 for (x = x1; x <= x2; x += range) {
316 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
317 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
324 for (y = y1; y <= y2; y += range) {
325 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
326 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
352 } while (range >= 1);
355 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
358 /* half pixel search */
365 #define Z_THRESHOLD 256
367 #define CHECK_MV(x,y)\
369 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
370 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
371 if(map[index]!=key){\
372 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
373 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
374 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
376 score_map[index]= d;\
380 #define CHECK_MV_DIR(x,y,new_dir)\
382 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
383 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
384 if(map[index]!=key){\
385 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
386 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
394 score_map[index]= d;\
398 #define CHECK_MV4(x,y)\
400 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
401 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
402 if(map[index]!=key){\
403 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
404 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
405 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
407 score_map[index]= d;\
411 #define check(x,y,S,v)\
412 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
413 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
414 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
415 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
418 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
419 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
420 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
421 int xmin, int ymin, int xmax, int ymax, int shift,
422 uint32_t *map, uint16_t *score_map, int map_generation,
423 op_pixels_abs_func pix_abs)
429 const int dir= next_dir;
430 const int x= best[0];
431 const int y= best[1];
435 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
436 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
437 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
438 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
447 const int x= best[0];
448 const int y= best[1];
449 const int last_min=dmin;
450 if(x>xmin) CHECK_MV(x-1, y )
451 if(y>xmin) CHECK_MV(x , y-1)
452 if(x<xmax) CHECK_MV(x+1, y )
453 if(y<xmax) CHECK_MV(x , y+1)
454 if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)
455 if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)
456 if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)
457 if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)
458 if(x-1>xmin) CHECK_MV(x-2, y )
459 if(y-1>xmin) CHECK_MV(x , y-2)
460 if(x+1<xmax) CHECK_MV(x+2, y )
461 if(y+1<xmax) CHECK_MV(x , y+2)
462 if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)
463 if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)
464 if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)
465 if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)
466 if(dmin==last_min) return dmin;
478 static inline int snake_search(MpegEncContext * s, int *best, int dmin,
479 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
480 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
481 int xmin, int ymin, int xmax, int ymax, int shift,
482 uint32_t *map, uint16_t *score_map,int map_generation,
483 op_pixels_abs_func pix_abs)
487 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1};
488 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1};
490 int last_d[2]={dmin, dmin};
497 if(256*256*256*64%point==0)
499 printf("%d %d %d\n", good, bad, point);
508 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
509 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;
510 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
512 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);
513 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
519 d = dmin + 10000; //FIXME smarter boundary handling
526 if(last_d[1] - last_d[0] > last_d[0] - d) c= -c;
536 if(fails>=SNAKE_1+1) return dmin;
549 static inline int cross_search(MpegEncContext * s, int *best, int dmin,
550 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
551 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
552 int xmin, int ymin, int xmax, int ymax, int shift,
553 uint32_t *map, uint16_t *score_map,int map_generation,
554 op_pixels_abs_func pix_abs)
556 static int x_dir[4]= {-1, 0, 1, 0};
557 static int y_dir[4]= { 0,-1, 0, 1};
558 int improvement[2]={100000, 100000};
564 dir= dirs[ improvement[0] > improvement[1] ? 0 : 1 ];
565 if(improvement[dir&1]==-1) return dmin;
568 const int x= best[0] + x_dir[dir];
569 const int y= best[1] + y_dir[dir];
570 const int key= (y<<ME_MAP_MV_BITS) + x + map_generation;
571 const int index= ((y<<ME_MAP_SHIFT) + x)&(ME_MAP_SIZE-1);
573 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
575 d = pix_abs(new_pic, old_pic + x + y*pic_stride, pic_stride);
576 d += (mv_penalty[(x<<shift)-pred_x] + mv_penalty[(y<<shift)-pred_y])*quant;
580 improvement[dir&1]= dmin-d;
581 improvement[(dir&1)^1]++;
592 d= dmin + 1000; //FIXME is this a good idea?
594 /* evaluated point was cached or checked and worse */
597 improvement[dir&1]= -1;
599 improvement[dir&1]= d-dmin;
600 last_dir= dirs[dir&1]= dir^2;
606 static inline int update_map_generation(MpegEncContext * s)
608 s->me_map_generation+= 1<<(ME_MAP_MV_BITS*2);
609 if(s->me_map_generation==0){
610 s->me_map_generation= 1<<(ME_MAP_MV_BITS*2);
611 memset(s->me_map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
613 return s->me_map_generation;
616 static int epzs_motion_search(MpegEncContext * s,
617 int *mx_ptr, int *my_ptr,
618 int P[10][2], int pred_x, int pred_y,
619 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture)
623 UINT8 *new_pic, *old_pic;
624 const int pic_stride= s->linesize;
625 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16;
626 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
627 int quant= s->qscale; // qscale of the prev frame
628 const int shift= 1+s->quarter_sample;
629 uint32_t *map= s->me_map;
630 uint16_t *score_map= s->me_score_map;
633 new_pic = s->new_picture[0] + pic_xy;
634 old_pic = ref_picture + pic_xy;
636 map_generation= update_map_generation(s);
638 dmin = pix_abs16x16(new_pic, old_pic, pic_stride);
639 map[0]= map_generation;
643 if ((s->mb_y == 0 || s->first_slice_line)) {
644 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
645 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift)
647 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
649 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
655 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
657 CHECK_MV(P_LAST[0] >>shift, P_LAST[1] >>shift)
658 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
659 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
660 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
664 CHECK_MV(P_LAST_RIGHT[0] >>shift, P_LAST_RIGHT[1] >>shift)
665 CHECK_MV(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
667 #if 0 //doest only slow things down
672 for(step=128; step>0; step>>=1){
673 const int step2= step;
675 for(y=-step2+best[1]; y<=step2+best[1]; y+=step){
677 if(y<ymin || y>ymax) continue;
679 for(x=-step2+best[0]; x<=step2+best[0]; x+=step){
680 if(x<xmin || x>xmax) continue;
681 if(x==best[0] && y==best[1]) continue;
688 //check(best[0],best[1],0, b0)
689 if(s->me_method==ME_EPZS)
690 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
691 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
692 shift, map, score_map, map_generation, pix_abs16x16);
694 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride,
695 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
696 shift, map, score_map, map_generation, pix_abs16x16);
697 //check(best[0],best[1],0, b1)
701 // printf("%d %d %d \n", best[0], best[1], dmin);
705 static int epzs_motion_search4(MpegEncContext * s, int block,
706 int *mx_ptr, int *my_ptr,
707 int P[10][2], int pred_x, int pred_y,
708 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
712 UINT8 *new_pic, *old_pic;
713 const int pic_stride= s->linesize;
714 const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8;
715 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
716 int quant= s->qscale; // qscale of the prev frame
717 const int shift= 1+s->quarter_sample;
718 uint32_t *map= s->me_map;
719 uint16_t *score_map= s->me_score_map;
722 new_pic = s->new_picture[0] + pic_xy;
723 old_pic = ref_picture + pic_xy;
725 map_generation= update_map_generation(s);
728 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
730 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
731 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
732 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift)
733 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift)
735 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift)
736 //FIXME try some early stop
738 CHECK_MV4(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
739 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
740 CHECK_MV4(P_TOP[0]>>shift, P_TOP[1]>>shift)
741 CHECK_MV4(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
742 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift)
746 CHECK_MV4(P_LAST_RIGHT[0]>>shift, P_LAST_RIGHT[1]>>shift)
747 CHECK_MV4(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
750 if(s->me_method==ME_EPZS)
751 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
752 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
753 shift, map, score_map, map_generation, pix_abs8x8);
755 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride,
756 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
757 shift, map, score_map, map_generation, pix_abs8x8);
762 // printf("%d %d %d \n", best[0], best[1], dmin);
766 #define CHECK_HALF_MV(suffix, x, y) \
768 d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
769 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
770 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
774 /* The idea would be to make half pel ME after Inter/Intra decision to
776 static inline int halfpel_motion_search(MpegEncContext * s,
777 int *mx_ptr, int *my_ptr, int dmin,
778 int xmin, int ymin, int xmax, int ymax,
779 int pred_x, int pred_y, uint8_t *ref_picture,
780 op_pixels_abs_func pix_abs_x2,
781 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n)
783 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
784 const int quant= s->qscale;
785 int mx, my, xx, yy, dminh;
794 xx = 16 * s->mb_x + 8*(n&1);
795 yy = 16 * s->mb_y + 8*(n>>1);
796 pix = s->new_picture[0] + (yy * s->linesize) + xx;
800 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
804 if (mx > xmin && mx < xmax &&
805 my > ymin && my < ymax) {
816 CHECK_HALF_MV(xy2, -1, -1)
817 CHECK_HALF_MV(y2 , 0, -1)
818 CHECK_HALF_MV(xy2, +1, -1)
821 CHECK_HALF_MV(x2 , -1, 0)
822 CHECK_HALF_MV(x2 , +1, 0)
823 CHECK_HALF_MV(xy2, -1, +1)
824 CHECK_HALF_MV(y2 , 0, +1)
825 CHECK_HALF_MV(xy2, +1, +1)
839 static inline int fast_halfpel_motion_search(MpegEncContext * s,
840 int *mx_ptr, int *my_ptr, int dmin,
841 int xmin, int ymin, int xmax, int ymax,
842 int pred_x, int pred_y, uint8_t *ref_picture,
843 op_pixels_abs_func pix_abs_x2,
844 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n)
846 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
847 uint16_t *score_map= s->me_score_map;
848 const int quant= s->qscale;
849 int mx, my, xx, yy, dminh;
860 xx = 16 * s->mb_x + 8*(n&1);
861 yy = 16 * s->mb_y + 8*(n>>1);
862 pix = s->new_picture[0] + (yy * s->linesize) + xx;
866 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
870 if (mx > xmin && mx < xmax &&
871 my > ymin && my < ymax) {
874 const int index= (my<<ME_MAP_SHIFT) + mx;
875 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
876 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
877 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
878 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
888 CHECK_HALF_MV(y2 , 0, -1)
890 CHECK_HALF_MV(xy2, -1, -1)
892 CHECK_HALF_MV(xy2, +1, -1)
896 CHECK_HALF_MV(xy2, -1, +1)
898 CHECK_HALF_MV(x2 , -1, 0)
900 CHECK_HALF_MV(xy2, +1, -1)
902 CHECK_HALF_MV(xy2, -1, -1)
906 CHECK_HALF_MV(xy2, +1, +1)
908 CHECK_HALF_MV(x2 , +1, 0)
913 CHECK_HALF_MV(xy2, -1, -1)
917 CHECK_HALF_MV(xy2, +1, +1)
919 CHECK_HALF_MV(x2 , -1, 0)
920 CHECK_HALF_MV(xy2, -1, +1)
923 CHECK_HALF_MV(xy2, +1, -1)
927 CHECK_HALF_MV(xy2, -1, +1)
929 CHECK_HALF_MV(x2 , +1, 0)
930 CHECK_HALF_MV(xy2, +1, +1)
932 CHECK_HALF_MV(y2 , 0, +1)
947 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
949 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
951 s->p_mv_table[xy][0] = mx;
952 s->p_mv_table[xy][1] = my;
954 /* has allready been set to the 4 MV if 4MV is done */
956 int mot_xy= s->block_index[0];
958 s->motion_val[mot_xy ][0]= mx;
959 s->motion_val[mot_xy ][1]= my;
960 s->motion_val[mot_xy+1][0]= mx;
961 s->motion_val[mot_xy+1][1]= my;
963 mot_xy += s->block_wrap[0];
964 s->motion_val[mot_xy ][0]= mx;
965 s->motion_val[mot_xy ][1]= my;
966 s->motion_val[mot_xy+1][0]= mx;
967 s->motion_val[mot_xy+1][1]= my;
971 static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
973 *range = 8 * (1 << (f_code - 1));
974 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
975 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
978 if (s->unrestricted_mv) {
983 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
984 *xmax = s->mb_width*16;
985 *ymax = s->mb_height*16;
987 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
988 (cuz the drawn edge isnt large enough))*/
995 *xmax = s->mb_width*16 - 16;
996 *ymax = s->mb_height*16 - 16;
1000 static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift)
1004 uint8_t *ref_picture= s->last_picture[0];
1007 for(block=0; block<4; block++){
1009 int pred_x4, pred_y4;
1011 static const int off[4]= {2, 1, 1, -1};
1012 const int mot_stride = s->block_wrap[0];
1013 const int mot_xy = s->block_index[block];
1014 // const int block_x= (block&1);
1015 // const int block_y= (block>>1);
1016 #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way
1017 const int rel_xmin4= xmin;
1018 const int rel_xmax4= xmax;
1019 const int rel_ymin4= ymin;
1020 const int rel_ymax4= ymax;
1022 const int rel_xmin4= xmin - block_x*8;
1023 const int rel_xmax4= xmax - block_x*8 + 8;
1024 const int rel_ymin4= ymin - block_y*8;
1025 const int rel_ymax4= ymax - block_y*8 + 8;
1027 P_LAST[0] = s->motion_val[mot_xy ][0];
1028 P_LAST[1] = s->motion_val[mot_xy ][1];
1029 P_LEFT[0] = s->motion_val[mot_xy - 1][0];
1030 P_LEFT[1] = s->motion_val[mot_xy - 1][1];
1031 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 1][0];
1032 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 1][1];
1033 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 1*mot_stride][0];
1034 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 1*mot_stride][1];
1036 if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift);
1037 if(P_LAST_RIGHT[0] < (rel_xmin4<<shift)) P_LAST_RIGHT[0] = (rel_xmin4<<shift);
1038 if(P_LAST_BOTTOM[1]< (rel_ymin4<<shift)) P_LAST_BOTTOM[1]= (rel_ymin4<<shift);
1040 /* special case for first line */
1041 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
1045 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
1046 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
1047 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
1048 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
1049 if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift);
1050 if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift);
1051 if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift);
1052 if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift);
1054 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1055 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1057 if(s->out_format == FMT_H263){
1058 pred_x4 = P_MEDIAN[0];
1059 pred_y4 = P_MEDIAN[1];
1060 }else { /* mpeg1 at least */
1068 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture);
1070 dmin4= fast_halfpel_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
1071 pred_x4, pred_y4, ref_picture, pix_abs8x8_x2,
1072 pix_abs8x8_y2, pix_abs8x8_xy2, block);
1074 s->motion_val[ s->block_index[block] ][0]= mx4;
1075 s->motion_val[ s->block_index[block] ][1]= my4;
1081 void ff_estimate_p_frame_motion(MpegEncContext * s,
1085 int sum, varc, vard, mx, my, range, dmin, xx, yy;
1086 int xmin, ymin, xmax, ymax;
1087 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
1088 int pred_x=0, pred_y=0;
1090 const int shift= 1+s->quarter_sample;
1092 uint8_t *ref_picture= s->last_picture[0];
1094 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
1095 rel_xmin= xmin - mb_x*16;
1096 rel_xmax= xmax - mb_x*16;
1097 rel_ymin= ymin - mb_y*16;
1098 rel_ymax= ymax - mb_y*16;
1101 switch(s->me_method) {
1104 no_motion_search(s, &mx, &my);
1110 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
1115 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1120 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1127 const int mot_stride = s->block_wrap[0];
1128 const int mot_xy = s->block_index[0];
1130 P_LAST[0] = s->motion_val[mot_xy ][0];
1131 P_LAST[1] = s->motion_val[mot_xy ][1];
1132 P_LEFT[0] = s->motion_val[mot_xy - 1][0];
1133 P_LEFT[1] = s->motion_val[mot_xy - 1][1];
1134 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 2][0];
1135 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 2][1];
1136 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 2*mot_stride][0];
1137 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 2*mot_stride][1];
1139 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
1140 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
1141 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
1143 /* special case for first line */
1144 if ((mb_y == 0 || s->first_slice_line)) {
1148 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
1149 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
1150 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0];
1151 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1];
1152 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift);
1153 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
1154 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
1156 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1157 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1159 if(s->out_format == FMT_H263){
1160 pred_x = P_MEDIAN[0];
1161 pred_y = P_MEDIAN[1];
1162 }else { /* mpeg1 at least */
1168 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
1173 /* intra / predictive decision */
1177 pix = s->new_picture[0] + (yy * s->linesize) + xx;
1178 /* At this point (mx,my) are full-pell and the relative displacement */
1179 ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx);
1181 sum = pix_sum(pix, s->linesize);
1184 varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8;
1185 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8;
1186 //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);
1187 s->mb_var [s->mb_width * mb_y + mb_x] = varc;
1188 s->mc_mb_var[s->mb_width * mb_y + mb_x] = vard;
1189 s->mb_var_sum += varc;
1190 s->mc_mb_var_sum += vard;
1191 //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);
1194 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
1195 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
1197 if(s->flags&CODEC_FLAG_HQ){
1198 if (vard*2 + 200 > varc)
1199 mb_type|= MB_TYPE_INTRA;
1200 if (varc*2 + 200 > vard){
1201 mb_type|= MB_TYPE_INTER;
1202 if(s->me_method >= ME_EPZS)
1203 fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1204 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1205 pix_abs16x16_xy2, 0);
1207 halfpel_motion_search( s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1208 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1209 pix_abs16x16_xy2, 0);
1214 if((s->flags&CODEC_FLAG_4MV)
1215 && !s->skip_me && varc>50 && vard>10){
1216 mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
1217 mb_type|=MB_TYPE_INTER4V;
1219 set_p_mv_tables(s, mx, my, 0);
1221 set_p_mv_tables(s, mx, my, 1);
1223 if (vard <= 64 || vard < varc) {
1224 mb_type|= MB_TYPE_INTER;
1225 if (s->me_method != ME_ZERO) {
1226 if(s->me_method >= ME_EPZS)
1227 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1228 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1229 pix_abs16x16_xy2, 0);
1231 dmin= halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1232 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1233 pix_abs16x16_xy2, 0);
1234 if((s->flags&CODEC_FLAG_4MV)
1235 && !s->skip_me && varc>50 && vard>10){
1236 int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
1237 if(dmin4 + 128 <dmin)
1238 mb_type= MB_TYPE_INTER4V;
1240 set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V);
1249 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d",
1250 skip, vard, varc, dmin);
1254 mb_type|= MB_TYPE_INTRA;
1260 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1263 int ff_estimate_motion_b(MpegEncContext * s,
1264 int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code)
1266 int mx, my, range, dmin;
1267 int xmin, ymin, xmax, ymax;
1268 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
1269 int pred_x=0, pred_y=0;
1271 const int shift= 1+s->quarter_sample;
1272 const int mot_stride = s->mb_width + 2;
1273 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
1275 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
1276 rel_xmin= xmin - mb_x*16;
1277 rel_xmax= xmax - mb_x*16;
1278 rel_ymin= ymin - mb_y*16;
1279 rel_ymax= ymax - mb_y*16;
1281 switch(s->me_method) {
1284 no_motion_search(s, &mx, &my);
1290 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
1295 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1300 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1308 P_LAST[0] = mv_table[mot_xy ][0];
1309 P_LAST[1] = mv_table[mot_xy ][1];
1310 P_LEFT[0] = mv_table[mot_xy - 1][0];
1311 P_LEFT[1] = mv_table[mot_xy - 1][1];
1312 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0];
1313 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1];
1314 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0];
1315 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1];
1317 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
1318 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
1319 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
1321 /* special case for first line */
1322 if ((mb_y == 0 || s->first_slice_line)) {
1324 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1325 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1326 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
1327 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
1328 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift);
1329 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
1330 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
1332 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1333 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1338 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
1343 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1344 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1345 pix_abs16x16_xy2, 0);
1346 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1347 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1348 mv_table[mot_xy][0]= mx;
1349 mv_table[mot_xy][1]= my;
1354 static inline int check_bidir_mv(MpegEncContext * s,
1356 int motion_fx, int motion_fy,
1357 int motion_bx, int motion_by,
1358 int pred_fx, int pred_fy,
1359 int pred_bx, int pred_by)
1362 //FIXME direct mode penalty
1363 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1364 uint8_t *dest_y = s->me_scratchpad;
1370 fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->qscale;
1372 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1373 src_x = mb_x * 16 + (motion_fx >> 1);
1374 src_y = mb_y * 16 + (motion_fy >> 1);
1376 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
1377 put_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
1378 put_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
1380 fbmin += (mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->qscale;
1382 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1383 src_x = mb_x * 16 + (motion_bx >> 1);
1384 src_y = mb_y * 16 + (motion_by >> 1);
1386 ptr = s->next_picture[0] + (src_y * s->linesize) + src_x;
1387 avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
1388 avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
1390 fbmin += pix_abs16x16(s->new_picture[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
1394 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1395 static inline int bidir_refine(MpegEncContext * s,
1398 const int mot_stride = s->mb_width + 2;
1399 const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
1401 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1402 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1403 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1404 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1405 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1406 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1407 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1408 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1410 //FIXME do refinement and add flag
1412 fbmin= check_bidir_mv(s, mb_x, mb_y,
1413 motion_fx, motion_fy,
1414 motion_bx, motion_by,
1421 static inline int direct_search(MpegEncContext * s,
1425 const int mot_stride = s->mb_width + 2;
1426 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
1428 int motion_fx, motion_fy, motion_bx, motion_by, motion_bx0, motion_by0;
1429 int motion_dx, motion_dy;
1430 const int motion_px= s->p_mv_table[mot_xy][0];
1431 const int motion_py= s->p_mv_table[mot_xy][1];
1432 const int time_pp= s->pp_time;
1433 const int time_bp= s->bp_time;
1434 const int time_pb= time_pp - time_bp;
1436 int mx, my, mx2, my2;
1437 uint8_t *ref_picture= s->me_scratchpad - (mb_x + 1 + (mb_y + 1)*s->linesize)*16;
1438 int16_t (*mv_table)[2]= s->b_direct_mv_table;
1439 uint16_t *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1441 /* thanks to iso-mpeg the rounding is different for the zero vector, so we need to handle that ... */
1442 motion_fx= (motion_px*time_pb)/time_pp;
1443 motion_fy= (motion_py*time_pb)/time_pp;
1444 motion_bx0= (-motion_px*time_bp)/time_pp;
1445 motion_by0= (-motion_py*time_bp)/time_pp;
1446 motion_dx= motion_dy=0;
1447 dmin2= check_bidir_mv(s, mb_x, mb_y,
1448 motion_fx, motion_fy,
1449 motion_bx0, motion_by0,
1450 motion_fx, motion_fy,
1451 motion_bx0, motion_by0) - s->qscale;
1453 motion_bx= motion_fx - motion_px;
1454 motion_by= motion_fy - motion_py;
1455 for(by=-1; by<2; by++){
1456 for(bx=-1; bx<2; bx++){
1457 uint8_t *dest_y = s->me_scratchpad + (by+1)*s->linesize*16 + (bx+1)*16;
1461 const int width= s->width;
1462 const int height= s->height;
1464 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1465 src_x = (mb_x + bx) * 16 + (motion_fx >> 1);
1466 src_y = (mb_y + by) * 16 + (motion_fy >> 1);
1467 src_x = clip(src_x, -16, width);
1468 if (src_x == width) dxy &= ~1;
1469 src_y = clip(src_y, -16, height);
1470 if (src_y == height) dxy &= ~2;
1472 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
1473 put_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
1474 put_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
1476 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1477 src_x = (mb_x + bx) * 16 + (motion_bx >> 1);
1478 src_y = (mb_y + by) * 16 + (motion_by >> 1);
1479 src_x = clip(src_x, -16, width);
1480 if (src_x == width) dxy &= ~1;
1481 src_y = clip(src_y, -16, height);
1482 if (src_y == height) dxy &= ~2;
1484 avg_pixels_tab[dxy](dest_y , ptr , s->linesize, 16);
1485 avg_pixels_tab[dxy](dest_y + 8, ptr + 8, s->linesize, 16);
1489 P_LAST[0] = mv_table[mot_xy ][0];
1490 P_LAST[1] = mv_table[mot_xy ][1];
1491 P_LEFT[0] = mv_table[mot_xy - 1][0];
1492 P_LEFT[1] = mv_table[mot_xy - 1][1];
1493 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0];
1494 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1];
1495 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0];
1496 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1];
1498 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
1499 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
1500 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
1502 /* special case for first line */
1503 if ((mb_y == 0 || s->first_slice_line)) {
1505 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1506 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1507 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
1508 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
1510 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1511 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1513 dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, -16, -16, 15, 15, ref_picture);
1514 if(mx==0 && my==0) dmin=99999999; // not representable, due to rounding stuff
1523 for(by=-1; by<2; by++){
1524 if(my2+by < -32) continue;
1525 for(bx=-1; bx<2; bx++){
1526 if(bx==0 && by==0) continue;
1527 if(mx2+bx < -32) continue;
1528 dmin2= check_bidir_mv(s, mb_x, mb_y,
1529 mx2+bx+motion_fx, my2+by+motion_fy,
1530 mx2+bx+motion_bx, my2+by+motion_by,
1531 mx2+bx+motion_fx, my2+by+motion_fy,
1532 motion_bx, motion_by) - s->qscale;
1545 motion_bx= motion_bx0;
1546 motion_by= motion_by0;
1549 s->b_direct_mv_table[mot_xy][0]= mx;
1550 s->b_direct_mv_table[mot_xy][1]= my;
1551 s->b_direct_forw_mv_table[mot_xy][0]= motion_fx + mx;
1552 s->b_direct_forw_mv_table[mot_xy][1]= motion_fy + my;
1553 s->b_direct_back_mv_table[mot_xy][0]= motion_bx + mx;
1554 s->b_direct_back_mv_table[mot_xy][1]= motion_by + my;
1558 void ff_estimate_b_frame_motion(MpegEncContext * s,
1561 const int quant= s->qscale;
1562 int fmin, bmin, dmin, fbmin;
1565 dmin= direct_search(s, mb_x, mb_y);
1567 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code);
1568 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code) - quant;
1569 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1571 fbmin= bidir_refine(s, mb_x, mb_y);
1573 if(s->flags&CODEC_FLAG_HQ){
1574 type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT;
1577 type=MB_TYPE_DIRECT;
1581 type= MB_TYPE_FORWARD;
1585 type= MB_TYPE_BACKWARD;
1589 type= MB_TYPE_BIDIR;
1591 s->mc_mb_var_sum += score;
1592 s->mc_mb_var[mb_y*s->mb_width + mb_x] = score;
1598 if(type==MB_TYPE_DIRECT){
1599 int diff= ABS(s->b_forw_mv_table)
1603 s->mb_type[mb_y*s->mb_width + mb_x]= type;
1604 /* if(mb_y==0 && mb_x==0) printf("\n");
1605 if(mb_x==0) printf("\n");
1606 printf("%d", av_log2(type));
1610 /* find best f_code for ME which do unlimited searches */
1611 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1613 if(s->me_method>=ME_EPZS){
1616 UINT8 * fcode_tab= s->fcode_tab;
1618 int best_score=-10000000;
1620 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
1622 for(y=0; y<s->mb_height; y++){
1624 int xy= (y+1)* (s->mb_width+2) + 1;
1626 for(x=0; x<s->mb_width; x++){
1627 if(s->mb_type[i] & type){
1628 int fcode= MAX(fcode_tab[mv_table[xy][0] + MAX_MV],
1629 fcode_tab[mv_table[xy][1] + MAX_MV]);
1632 for(j=0; j<fcode && j<8; j++){
1633 if(s->pict_type==B_TYPE || s->mc_mb_var[i] < s->mb_var[i])
1643 if(score[i] > best_score){
1644 best_score= score[i];
1647 // printf("%d %d\n", i, score[i]);
1650 // printf("fcode: %d type: %d\n", i, s->pict_type);
1652 /* for(i=0; i<=MAX_FCODE; i++){
1653 printf("%d ", mv_num[i]);
1661 void ff_fix_long_p_mvs(MpegEncContext * s)
1663 const int f_code= s->f_code;
1665 UINT8 * fcode_tab= s->fcode_tab;
1668 /* clip / convert to intra 16x16 type MVs */
1669 for(y=0; y<s->mb_height; y++){
1671 int xy= (y+1)* (s->mb_width+2)+1;
1672 int i= y*s->mb_width;
1673 for(x=0; x<s->mb_width; x++){
1674 if(s->mb_type[i]&MB_TYPE_INTER){
1675 if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
1676 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
1677 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
1678 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
1679 s->mb_type[i] &= ~MB_TYPE_INTER;
1680 s->mb_type[i] |= MB_TYPE_INTRA;
1681 s->p_mv_table[xy][0] = 0;
1682 s->p_mv_table[xy][1] = 0;
1692 //printf("%d no:%d %d//\n", clip, noclip, f_code);
1693 if(s->flags&CODEC_FLAG_4MV){
1694 const int wrap= 2+ s->mb_width*2;
1696 /* clip / convert to intra 8x8 type MVs */
1697 for(y=0; y<s->mb_height; y++){
1698 int xy= (y*2 + 1)*wrap + 1;
1699 int i= y*s->mb_width;
1702 for(x=0; x<s->mb_width; x++){
1703 if(s->mb_type[i]&MB_TYPE_INTER4V){
1705 for(block=0; block<4; block++){
1706 int off= (block& 1) + (block>>1)*wrap;
1707 int mx= s->motion_val[ xy + off ][0];
1708 int my= s->motion_val[ xy + off ][1];
1710 if( fcode_tab[mx + MAX_MV] > f_code
1711 || fcode_tab[mx + MAX_MV] == 0
1712 || fcode_tab[my + MAX_MV] > f_code
1713 || fcode_tab[my + MAX_MV] == 0 ){
1714 s->mb_type[i] &= ~MB_TYPE_INTER4V;
1715 s->mb_type[i] |= MB_TYPE_INTRA;
1726 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
1729 UINT8 * fcode_tab= s->fcode_tab;
1731 /* clip / convert to intra 16x16 type MVs */
1732 for(y=0; y<s->mb_height; y++){
1734 int xy= (y+1)* (s->mb_width+2)+1;
1735 int i= y*s->mb_width;
1736 for(x=0; x<s->mb_width; x++){
1737 if(s->mb_type[i]&type){
1738 if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
1739 || fcode_tab[mv_table[xy][0] + MAX_MV] == 0
1740 || fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
1741 || fcode_tab[mv_table[xy][1] + MAX_MV] == 0 ){
1742 if(s->mb_type[i]&(~type)) s->mb_type[i] &= ~type;
1744 mv_table[xy][0] = 0;
1745 mv_table[xy][1] = 0;
1746 //this is certainly bad FIXME