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_dev(UINT8 * pix, int line_size, int mean)
49 for (i = 0; i < 16; i++) {
50 for (j = 0; j < 16; j += 8) {
51 s += ABS(pix[0]-mean);
52 s += ABS(pix[1]-mean);
53 s += ABS(pix[2]-mean);
54 s += ABS(pix[3]-mean);
55 s += ABS(pix[4]-mean);
56 s += ABS(pix[5]-mean);
57 s += ABS(pix[6]-mean);
58 s += ABS(pix[7]-mean);
61 pix += line_size - 16;
66 static int pix_norm(UINT8 * pix1, UINT8 * pix2, int line_size)
69 UINT32 *sq = squareTbl + 256;
72 for (i = 0; i < 16; i++) {
73 for (j = 0; j < 16; j += 8) {
74 s += sq[pix1[0] - pix2[0]];
75 s += sq[pix1[1] - pix2[1]];
76 s += sq[pix1[2] - pix2[2]];
77 s += sq[pix1[3] - pix2[3]];
78 s += sq[pix1[4] - pix2[4]];
79 s += sq[pix1[5] - pix2[5]];
80 s += sq[pix1[6] - pix2[6]];
81 s += sq[pix1[7] - pix2[7]];
85 pix1 += line_size - 16;
86 pix2 += line_size - 16;
91 static void no_motion_search(MpegEncContext * s,
92 int *mx_ptr, int *my_ptr)
94 *mx_ptr = 16 * s->mb_x;
95 *my_ptr = 16 * s->mb_y;
98 static int full_motion_search(MpegEncContext * s,
99 int *mx_ptr, int *my_ptr, int range,
100 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
102 int x1, y1, x2, y2, xx, yy, x, y;
108 x1 = xx - range + 1; /* we loose one pixel to avoid boundary pb with half pixel pred */
120 pix = s->new_picture[0] + (yy * s->linesize) + xx;
124 for (y = y1; y <= y2; y++) {
125 for (x = x1; x <= x2; x++) {
126 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x,
130 (abs(x - xx) + abs(y - yy)) <
131 (abs(mx - xx) + abs(my - yy)))) {
143 if (*mx_ptr < -(2 * range) || *mx_ptr >= (2 * range) ||
144 *my_ptr < -(2 * range) || *my_ptr >= (2 * range)) {
145 fprintf(stderr, "error %d %d\n", *mx_ptr, *my_ptr);
152 static int log_motion_search(MpegEncContext * s,
153 int *mx_ptr, int *my_ptr, int range,
154 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
156 int x1, y1, x2, y2, xx, yy, x, y;
183 pix = s->new_picture[0] + (yy * s->linesize) + xx;
189 for (y = y1; y <= y2; y += range) {
190 for (x = x1; x <= x2; x += range) {
191 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
192 if (d < dmin || (d == dmin && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
218 } while (range >= 1);
221 fprintf(stderr, "log - MX: %d\tMY: %d\n", mx, my);
228 static int phods_motion_search(MpegEncContext * s,
229 int *mx_ptr, int *my_ptr, int range,
230 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
232 int x1, y1, x2, y2, xx, yy, x, y, lastx, d;
233 int mx, my, dminx, dminy;
259 pix = s->new_picture[0] + (yy * s->linesize) + xx;
270 for (x = x1; x <= x2; x += range) {
271 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
272 if (d < dminx || (d == dminx && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
279 for (y = y1; y <= y2; y += range) {
280 d = pix_abs16x16(pix, ref_picture + (y * s->linesize) + x, s->linesize);
281 if (d < dminy || (d == dminy && (abs(x - xx) + abs(y - yy)) < (abs(mx - xx) + abs(my - yy)))) {
307 } while (range >= 1);
310 fprintf(stderr, "phods - MX: %d\tMY: %d\n", mx, my);
313 /* half pixel search */
320 #define Z_THRESHOLD 256
322 #define CHECK_MV(x,y)\
324 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
325 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
326 if(map[index]!=key){\
327 d = pix_abs16x16(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
328 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
329 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
331 score_map[index]= d;\
335 #define CHECK_MV_DIR(x,y,new_dir)\
337 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
338 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
339 if(map[index]!=key){\
340 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
341 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
349 score_map[index]= d;\
353 #define CHECK_MV4(x,y)\
355 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;\
356 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);\
357 if(map[index]!=key){\
358 d = pix_abs8x8(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);\
359 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;\
360 COPY3_IF_LT(dmin, d, best[0], x, best[1], y)\
362 score_map[index]= d;\
366 #define check(x,y,S,v)\
367 if( (x)<(xmin<<(S)) ) printf("%d %d %d %d %d xmin" #v, xmin, (x), (y), s->mb_x, s->mb_y);\
368 if( (x)>(xmax<<(S)) ) printf("%d %d %d %d %d xmax" #v, xmax, (x), (y), s->mb_x, s->mb_y);\
369 if( (y)<(ymin<<(S)) ) printf("%d %d %d %d %d ymin" #v, ymin, (x), (y), s->mb_x, s->mb_y);\
370 if( (y)>(ymax<<(S)) ) printf("%d %d %d %d %d ymax" #v, ymax, (x), (y), s->mb_x, s->mb_y);\
373 static inline int small_diamond_search(MpegEncContext * s, int *best, int dmin,
374 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
375 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
376 int xmin, int ymin, int xmax, int ymax, int shift,
377 uint32_t *map, uint16_t *score_map, int map_generation,
378 op_pixels_abs_func pix_abs)
384 const int dir= next_dir;
385 const int x= best[0];
386 const int y= best[1];
390 if(dir!=2 && x>xmin) CHECK_MV_DIR(x-1, y , 0)
391 if(dir!=3 && y>ymin) CHECK_MV_DIR(x , y-1, 1)
392 if(dir!=0 && x<xmax) CHECK_MV_DIR(x+1, y , 2)
393 if(dir!=1 && y<ymax) CHECK_MV_DIR(x , y+1, 3)
402 const int x= best[0];
403 const int y= best[1];
404 const int last_min=dmin;
405 if(x>xmin) CHECK_MV(x-1, y )
406 if(y>xmin) CHECK_MV(x , y-1)
407 if(x<xmax) CHECK_MV(x+1, y )
408 if(y<xmax) CHECK_MV(x , y+1)
409 if(x>xmin && y>ymin) CHECK_MV(x-1, y-1)
410 if(x>xmin && y<ymax) CHECK_MV(x-1, y+1)
411 if(x<xmax && y>ymin) CHECK_MV(x+1, y-1)
412 if(x<xmax && y<ymax) CHECK_MV(x+1, y+1)
413 if(x-1>xmin) CHECK_MV(x-2, y )
414 if(y-1>xmin) CHECK_MV(x , y-2)
415 if(x+1<xmax) CHECK_MV(x+2, y )
416 if(y+1<xmax) CHECK_MV(x , y+2)
417 if(x-1>xmin && y-1>ymin) CHECK_MV(x-2, y-2)
418 if(x-1>xmin && y+1<ymax) CHECK_MV(x-2, y+2)
419 if(x+1<xmax && y-1>ymin) CHECK_MV(x+2, y-2)
420 if(x+1<xmax && y+1<ymax) CHECK_MV(x+2, y+2)
421 if(dmin==last_min) return dmin;
433 static inline int snake_search(MpegEncContext * s, int *best, int dmin,
434 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
435 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
436 int xmin, int ymin, int xmax, int ymax, int shift,
437 uint32_t *map, uint16_t *score_map,int map_generation,
438 op_pixels_abs_func pix_abs)
442 static int x_dir[8]= {1,1,0,-1,-1,-1, 0, 1};
443 static int y_dir[8]= {0,1,1, 1, 0,-1,-1,-1};
445 int last_d[2]={dmin, dmin};
452 if(256*256*256*64%point==0)
454 printf("%d %d %d\n", good, bad, point);
463 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
464 const int key= ((y)<<ME_MAP_MV_BITS) + (x) + map_generation;
465 const int index= (((y)<<ME_MAP_SHIFT) + (x))&(ME_MAP_SIZE-1);
467 d = pix_abs(new_pic, old_pic + (x) + (y)*pic_stride, pic_stride);
468 d += (mv_penalty[((x)<<shift)-pred_x] + mv_penalty[((y)<<shift)-pred_y])*quant;
474 d = dmin + 10000; //FIXME smarter boundary handling
481 if(last_d[1] - last_d[0] > last_d[0] - d) c= -c;
491 if(fails>=SNAKE_1+1) return dmin;
504 static inline int cross_search(MpegEncContext * s, int *best, int dmin,
505 UINT8 *new_pic, UINT8 *old_pic, int pic_stride,
506 int pred_x, int pred_y, UINT16 *mv_penalty, int quant,
507 int xmin, int ymin, int xmax, int ymax, int shift,
508 uint32_t *map, uint16_t *score_map,int map_generation,
509 op_pixels_abs_func pix_abs)
511 static int x_dir[4]= {-1, 0, 1, 0};
512 static int y_dir[4]= { 0,-1, 0, 1};
513 int improvement[2]={100000, 100000};
519 dir= dirs[ improvement[0] > improvement[1] ? 0 : 1 ];
520 if(improvement[dir&1]==-1) return dmin;
523 const int x= best[0] + x_dir[dir];
524 const int y= best[1] + y_dir[dir];
525 const int key= (y<<ME_MAP_MV_BITS) + x + map_generation;
526 const int index= ((y<<ME_MAP_SHIFT) + x)&(ME_MAP_SIZE-1);
528 if(x>=xmin && x<=xmax && y>=ymin && y<=ymax){
530 d = pix_abs(new_pic, old_pic + x + y*pic_stride, pic_stride);
531 d += (mv_penalty[(x<<shift)-pred_x] + mv_penalty[(y<<shift)-pred_y])*quant;
535 improvement[dir&1]= dmin-d;
536 improvement[(dir&1)^1]++;
547 d= dmin + 1000; //FIXME is this a good idea?
549 /* evaluated point was cached or checked and worse */
552 improvement[dir&1]= -1;
554 improvement[dir&1]= d-dmin;
555 last_dir= dirs[dir&1]= dir^2;
561 static inline int update_map_generation(MpegEncContext * s)
563 s->me_map_generation+= 1<<(ME_MAP_MV_BITS*2);
564 if(s->me_map_generation==0){
565 s->me_map_generation= 1<<(ME_MAP_MV_BITS*2);
566 memset(s->me_map, 0, sizeof(uint32_t)*ME_MAP_SIZE);
568 return s->me_map_generation;
571 static int epzs_motion_search(MpegEncContext * s,
572 int *mx_ptr, int *my_ptr,
573 int P[10][2], int pred_x, int pred_y,
574 int xmin, int ymin, int xmax, int ymax, uint8_t * ref_picture)
578 UINT8 *new_pic, *old_pic;
579 const int pic_stride= s->linesize;
580 const int pic_xy= (s->mb_y*pic_stride + s->mb_x)*16;
581 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
582 int quant= s->qscale; // qscale of the prev frame
583 const int shift= 1+s->quarter_sample;
584 uint32_t *map= s->me_map;
585 uint16_t *score_map= s->me_score_map;
588 new_pic = s->new_picture[0] + pic_xy;
589 old_pic = ref_picture + pic_xy;
591 map_generation= update_map_generation(s);
593 dmin = pix_abs16x16(new_pic, old_pic, pic_stride);
594 map[0]= map_generation;
598 if ((s->mb_y == 0 || s->first_slice_line)) {
599 CHECK_MV(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
600 CHECK_MV(P_LAST[0]>>shift, P_LAST[1]>>shift)
602 if(dmin<256 && ( P_LEFT[0] |P_LEFT[1]
604 |P_TOPRIGHT[0]|P_TOPRIGHT[1])==0){
610 CHECK_MV(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
612 CHECK_MV(P_LAST[0] >>shift, P_LAST[1] >>shift)
613 CHECK_MV(P_LEFT[0] >>shift, P_LEFT[1] >>shift)
614 CHECK_MV(P_TOP[0] >>shift, P_TOP[1] >>shift)
615 CHECK_MV(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
619 CHECK_MV(P_LAST_RIGHT[0] >>shift, P_LAST_RIGHT[1] >>shift)
620 CHECK_MV(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
622 #if 0 //doest only slow things down
627 for(step=128; step>0; step>>=1){
628 const int step2= step;
630 for(y=-step2+best[1]; y<=step2+best[1]; y+=step){
632 if(y<ymin || y>ymax) continue;
634 for(x=-step2+best[0]; x<=step2+best[0]; x+=step){
635 if(x<xmin || x>xmax) continue;
636 if(x==best[0] && y==best[1]) continue;
643 //check(best[0],best[1],0, b0)
644 if(s->me_method==ME_EPZS)
645 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
646 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
647 shift, map, score_map, map_generation, pix_abs16x16);
649 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride,
650 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
651 shift, map, score_map, map_generation, pix_abs16x16);
652 //check(best[0],best[1],0, b1)
656 // printf("%d %d %d \n", best[0], best[1], dmin);
660 static int epzs_motion_search4(MpegEncContext * s, int block,
661 int *mx_ptr, int *my_ptr,
662 int P[10][2], int pred_x, int pred_y,
663 int xmin, int ymin, int xmax, int ymax, uint8_t *ref_picture)
667 UINT8 *new_pic, *old_pic;
668 const int pic_stride= s->linesize;
669 const int pic_xy= ((s->mb_y*2 + (block>>1))*pic_stride + s->mb_x*2 + (block&1))*8;
670 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
671 int quant= s->qscale; // qscale of the prev frame
672 const int shift= 1+s->quarter_sample;
673 uint32_t *map= s->me_map;
674 uint16_t *score_map= s->me_score_map;
677 new_pic = s->new_picture[0] + pic_xy;
678 old_pic = ref_picture + pic_xy;
680 map_generation= update_map_generation(s);
683 //printf("%d %d %d %d //",xmin, ymin, xmax, ymax);
685 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
686 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
687 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift)
688 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift)
690 CHECK_MV4(P_MV1[0]>>shift, P_MV1[1]>>shift)
691 //FIXME try some early stop
693 CHECK_MV4(P_MEDIAN[0]>>shift, P_MEDIAN[1]>>shift)
694 CHECK_MV4(P_LEFT[0]>>shift, P_LEFT[1]>>shift)
695 CHECK_MV4(P_TOP[0]>>shift, P_TOP[1]>>shift)
696 CHECK_MV4(P_TOPRIGHT[0]>>shift, P_TOPRIGHT[1]>>shift)
697 CHECK_MV4(P_LAST[0]>>shift, P_LAST[1]>>shift)
701 CHECK_MV4(P_LAST_RIGHT[0]>>shift, P_LAST_RIGHT[1]>>shift)
702 CHECK_MV4(P_LAST_BOTTOM[0]>>shift, P_LAST_BOTTOM[1]>>shift)
705 if(s->me_method==ME_EPZS)
706 dmin= small_diamond_search(s, best, dmin, new_pic, old_pic, pic_stride,
707 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
708 shift, map, score_map, map_generation, pix_abs8x8);
710 dmin= cross_search(s, best, dmin, new_pic, old_pic, pic_stride,
711 pred_x, pred_y, mv_penalty, quant, xmin, ymin, xmax, ymax,
712 shift, map, score_map, map_generation, pix_abs8x8);
717 // printf("%d %d %d \n", best[0], best[1], dmin);
721 #define CHECK_HALF_MV(suffix, x, y) \
723 d= pix_abs_ ## suffix(pix, ptr+((x)>>1), s->linesize);\
724 d += (mv_penalty[pen_x + x] + mv_penalty[pen_y + y])*quant;\
725 COPY3_IF_LT(dminh, d, dx, x, dy, y)\
729 /* The idea would be to make half pel ME after Inter/Intra decision to
731 static inline int halfpel_motion_search(MpegEncContext * s,
732 int *mx_ptr, int *my_ptr, int dmin,
733 int xmin, int ymin, int xmax, int ymax,
734 int pred_x, int pred_y, uint8_t *ref_picture,
735 op_pixels_abs_func pix_abs_x2,
736 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n)
738 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
739 const int quant= s->qscale;
740 int mx, my, xx, yy, dminh;
749 xx = 16 * s->mb_x + 8*(n&1);
750 yy = 16 * s->mb_y + 8*(n>>1);
751 pix = s->new_picture[0] + (yy * s->linesize) + xx;
755 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
759 if (mx > xmin && mx < xmax &&
760 my > ymin && my < ymax) {
771 CHECK_HALF_MV(xy2, -1, -1)
772 CHECK_HALF_MV(y2 , 0, -1)
773 CHECK_HALF_MV(xy2, +1, -1)
776 CHECK_HALF_MV(x2 , -1, 0)
777 CHECK_HALF_MV(x2 , +1, 0)
778 CHECK_HALF_MV(xy2, -1, +1)
779 CHECK_HALF_MV(y2 , 0, +1)
780 CHECK_HALF_MV(xy2, +1, +1)
794 static inline int fast_halfpel_motion_search(MpegEncContext * s,
795 int *mx_ptr, int *my_ptr, int dmin,
796 int xmin, int ymin, int xmax, int ymax,
797 int pred_x, int pred_y, uint8_t *ref_picture,
798 op_pixels_abs_func pix_abs_x2,
799 op_pixels_abs_func pix_abs_y2, op_pixels_abs_func pix_abs_xy2, int n)
801 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
802 uint16_t *score_map= s->me_score_map;
803 const int quant= s->qscale;
804 int mx, my, xx, yy, dminh;
815 xx = 16 * s->mb_x + 8*(n&1);
816 yy = 16 * s->mb_y + 8*(n>>1);
817 pix = s->new_picture[0] + (yy * s->linesize) + xx;
821 ptr = ref_picture + ((yy + my) * s->linesize) + (xx + mx);
825 if (mx > xmin && mx < xmax &&
826 my > ymin && my < ymax) {
829 const int index= (my<<ME_MAP_SHIFT) + mx;
830 const int t= score_map[(index-(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
831 const int l= score_map[(index- 1 )&(ME_MAP_SIZE-1)];
832 const int r= score_map[(index+ 1 )&(ME_MAP_SIZE-1)];
833 const int b= score_map[(index+(1<<ME_MAP_SHIFT))&(ME_MAP_SIZE-1)];
843 CHECK_HALF_MV(y2 , 0, -1)
845 CHECK_HALF_MV(xy2, -1, -1)
847 CHECK_HALF_MV(xy2, +1, -1)
851 CHECK_HALF_MV(xy2, -1, +1)
853 CHECK_HALF_MV(x2 , -1, 0)
855 CHECK_HALF_MV(xy2, +1, -1)
857 CHECK_HALF_MV(xy2, -1, -1)
861 CHECK_HALF_MV(xy2, +1, +1)
863 CHECK_HALF_MV(x2 , +1, 0)
868 CHECK_HALF_MV(xy2, -1, -1)
872 CHECK_HALF_MV(xy2, +1, +1)
874 CHECK_HALF_MV(x2 , -1, 0)
875 CHECK_HALF_MV(xy2, -1, +1)
878 CHECK_HALF_MV(xy2, +1, -1)
882 CHECK_HALF_MV(xy2, -1, +1)
884 CHECK_HALF_MV(x2 , +1, 0)
885 CHECK_HALF_MV(xy2, +1, +1)
887 CHECK_HALF_MV(y2 , 0, +1)
902 static inline void set_p_mv_tables(MpegEncContext * s, int mx, int my, int mv4)
904 const int xy= s->mb_x + 1 + (s->mb_y + 1)*(s->mb_width + 2);
906 s->p_mv_table[xy][0] = mx;
907 s->p_mv_table[xy][1] = my;
909 /* has allready been set to the 4 MV if 4MV is done */
911 int mot_xy= s->block_index[0];
913 s->motion_val[mot_xy ][0]= mx;
914 s->motion_val[mot_xy ][1]= my;
915 s->motion_val[mot_xy+1][0]= mx;
916 s->motion_val[mot_xy+1][1]= my;
918 mot_xy += s->block_wrap[0];
919 s->motion_val[mot_xy ][0]= mx;
920 s->motion_val[mot_xy ][1]= my;
921 s->motion_val[mot_xy+1][0]= mx;
922 s->motion_val[mot_xy+1][1]= my;
926 static inline void get_limits(MpegEncContext *s, int *range, int *xmin, int *ymin, int *xmax, int *ymax, int f_code)
928 *range = 8 * (1 << (f_code - 1));
929 /* XXX: temporary kludge to avoid overflow for msmpeg4 */
930 if (s->out_format == FMT_H263 && !s->h263_msmpeg4)
933 if (s->unrestricted_mv) {
938 if(s->avctx==NULL || s->avctx->codec->id!=CODEC_ID_MPEG4){
939 *xmax = s->mb_width*16;
940 *ymax = s->mb_height*16;
942 /* XXX: dunno if this is correct but ffmpeg4 decoder wont like it otherwise
943 (cuz the drawn edge isnt large enough))*/
950 *xmax = s->mb_width*16 - 16;
951 *ymax = s->mb_height*16 - 16;
955 static inline int mv4_search(MpegEncContext *s, int xmin, int ymin, int xmax, int ymax, int mx, int my, int shift)
959 uint8_t *ref_picture= s->last_picture[0];
962 for(block=0; block<4; block++){
964 int pred_x4, pred_y4;
966 static const int off[4]= {2, 1, 1, -1};
967 const int mot_stride = s->block_wrap[0];
968 const int mot_xy = s->block_index[block];
969 // const int block_x= (block&1);
970 // const int block_y= (block>>1);
971 #if 1 // this saves us a bit of cliping work and shouldnt affect compression in a negative way
972 const int rel_xmin4= xmin;
973 const int rel_xmax4= xmax;
974 const int rel_ymin4= ymin;
975 const int rel_ymax4= ymax;
977 const int rel_xmin4= xmin - block_x*8;
978 const int rel_xmax4= xmax - block_x*8 + 8;
979 const int rel_ymin4= ymin - block_y*8;
980 const int rel_ymax4= ymax - block_y*8 + 8;
982 P_LAST[0] = s->motion_val[mot_xy ][0];
983 P_LAST[1] = s->motion_val[mot_xy ][1];
984 P_LEFT[0] = s->motion_val[mot_xy - 1][0];
985 P_LEFT[1] = s->motion_val[mot_xy - 1][1];
986 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 1][0];
987 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 1][1];
988 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 1*mot_stride][0];
989 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 1*mot_stride][1];
991 if(P_LEFT[0] > (rel_xmax4<<shift)) P_LEFT[0] = (rel_xmax4<<shift);
992 if(P_LAST_RIGHT[0] < (rel_xmin4<<shift)) P_LAST_RIGHT[0] = (rel_xmin4<<shift);
993 if(P_LAST_BOTTOM[1]< (rel_ymin4<<shift)) P_LAST_BOTTOM[1]= (rel_ymin4<<shift);
995 /* special case for first line */
996 if ((s->mb_y == 0 || s->first_slice_line) && block<2) {
1000 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
1001 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
1002 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + off[block]][0];
1003 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + off[block]][1];
1004 if(P_TOP[1] > (rel_ymax4<<shift)) P_TOP[1] = (rel_ymax4<<shift);
1005 if(P_TOPRIGHT[0] < (rel_xmin4<<shift)) P_TOPRIGHT[0]= (rel_xmin4<<shift);
1006 if(P_TOPRIGHT[0] > (rel_xmax4<<shift)) P_TOPRIGHT[0]= (rel_xmax4<<shift);
1007 if(P_TOPRIGHT[1] > (rel_ymax4<<shift)) P_TOPRIGHT[1]= (rel_ymax4<<shift);
1009 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1010 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1012 if(s->out_format == FMT_H263){
1013 pred_x4 = P_MEDIAN[0];
1014 pred_y4 = P_MEDIAN[1];
1015 }else { /* mpeg1 at least */
1023 dmin4 = epzs_motion_search4(s, block, &mx4, &my4, P, pred_x4, pred_y4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4, ref_picture);
1025 dmin4= fast_halfpel_motion_search(s, &mx4, &my4, dmin4, rel_xmin4, rel_ymin4, rel_xmax4, rel_ymax4,
1026 pred_x4, pred_y4, ref_picture, pix_abs8x8_x2,
1027 pix_abs8x8_y2, pix_abs8x8_xy2, block);
1029 s->motion_val[ s->block_index[block] ][0]= mx4;
1030 s->motion_val[ s->block_index[block] ][1]= my4;
1036 void ff_estimate_p_frame_motion(MpegEncContext * s,
1040 int sum, varc, vard, mx, my, range, dmin, xx, yy;
1041 int xmin, ymin, xmax, ymax;
1042 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
1043 int pred_x=0, pred_y=0;
1045 const int shift= 1+s->quarter_sample;
1047 uint8_t *ref_picture= s->last_picture[0];
1049 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, s->f_code);
1050 rel_xmin= xmin - mb_x*16;
1051 rel_xmax= xmax - mb_x*16;
1052 rel_ymin= ymin - mb_y*16;
1053 rel_ymax= ymax - mb_y*16;
1056 switch(s->me_method) {
1059 no_motion_search(s, &mx, &my);
1065 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
1070 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1075 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1082 const int mot_stride = s->block_wrap[0];
1083 const int mot_xy = s->block_index[0];
1085 P_LAST[0] = s->motion_val[mot_xy ][0];
1086 P_LAST[1] = s->motion_val[mot_xy ][1];
1087 P_LEFT[0] = s->motion_val[mot_xy - 1][0];
1088 P_LEFT[1] = s->motion_val[mot_xy - 1][1];
1089 P_LAST_RIGHT[0] = s->motion_val[mot_xy + 2][0];
1090 P_LAST_RIGHT[1] = s->motion_val[mot_xy + 2][1];
1091 P_LAST_BOTTOM[0]= s->motion_val[mot_xy + 2*mot_stride][0];
1092 P_LAST_BOTTOM[1]= s->motion_val[mot_xy + 2*mot_stride][1];
1094 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
1095 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
1096 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
1098 /* special case for first line */
1099 if ((mb_y == 0 || s->first_slice_line)) {
1103 P_TOP[0] = s->motion_val[mot_xy - mot_stride ][0];
1104 P_TOP[1] = s->motion_val[mot_xy - mot_stride ][1];
1105 P_TOPRIGHT[0] = s->motion_val[mot_xy - mot_stride + 2][0];
1106 P_TOPRIGHT[1] = s->motion_val[mot_xy - mot_stride + 2][1];
1107 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1] = (rel_ymax<<shift);
1108 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
1109 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
1111 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1112 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1114 if(s->out_format == FMT_H263){
1115 pred_x = P_MEDIAN[0];
1116 pred_y = P_MEDIAN[1];
1117 }else { /* mpeg1 at least */
1123 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
1128 /* intra / predictive decision */
1132 pix = s->new_picture[0] + (yy * s->linesize) + xx;
1133 /* At this point (mx,my) are full-pell and the relative displacement */
1134 ppix = ref_picture + ((yy+my) * s->linesize) + (xx+mx);
1136 sum = pix_sum(pix, s->linesize);
1139 varc = (pix_norm1(pix, s->linesize) - sum*sum + 500 + 128)>>8;
1140 vard = (pix_norm(pix, ppix, s->linesize)+128)>>8;
1142 //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);
1143 s->mb_var [s->mb_width * mb_y + mb_x] = varc;
1144 s->mc_mb_var[s->mb_width * mb_y + mb_x] = vard;
1145 s->mb_var_sum += varc;
1146 s->mc_mb_var_sum += vard;
1147 //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);
1150 printf("varc=%4d avg_var=%4d (sum=%4d) vard=%4d mx=%2d my=%2d\n",
1151 varc, s->avg_mb_var, sum, vard, mx - xx, my - yy);
1153 if(s->flags&CODEC_FLAG_HQ){
1154 if (vard <= 64 || vard < varc)
1155 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1157 s->scene_change_score+= 20;
1159 if (vard*2 + 200 > varc)
1160 mb_type|= MB_TYPE_INTRA;
1161 if (varc*2 + 200 > vard){
1162 mb_type|= MB_TYPE_INTER;
1163 if(s->me_method >= ME_EPZS)
1164 fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1165 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1166 pix_abs16x16_xy2, 0);
1168 halfpel_motion_search( s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1169 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1170 pix_abs16x16_xy2, 0);
1175 if((s->flags&CODEC_FLAG_4MV)
1176 && !s->skip_me && varc>50 && vard>10){
1177 mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
1178 mb_type|=MB_TYPE_INTER4V;
1180 set_p_mv_tables(s, mx, my, 0);
1182 set_p_mv_tables(s, mx, my, 1);
1184 if (vard <= 64 || vard < varc) {
1185 s->scene_change_score+= ff_sqrt(vard) - ff_sqrt(varc);
1186 mb_type|= MB_TYPE_INTER;
1187 if (s->me_method != ME_ZERO) {
1188 if(s->me_method >= ME_EPZS)
1189 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1190 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1191 pix_abs16x16_xy2, 0);
1193 dmin= halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1194 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1195 pix_abs16x16_xy2, 0);
1196 if((s->flags&CODEC_FLAG_4MV)
1197 && !s->skip_me && varc>50 && vard>10){
1198 int dmin4= mv4_search(s, rel_xmin, rel_ymin, rel_xmax, rel_ymax, mx, my, shift);
1199 if(dmin4 + 128 <dmin)
1200 mb_type= MB_TYPE_INTER4V;
1202 set_p_mv_tables(s, mx, my, mb_type!=MB_TYPE_INTER4V);
1211 fprintf(stderr,"\nEarly skip: %d vard: %2d varc: %5d dmin: %d",
1212 skip, vard, varc, dmin);
1216 s->scene_change_score+= 20;
1217 mb_type|= MB_TYPE_INTRA;
1223 s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1226 int ff_estimate_motion_b(MpegEncContext * s,
1227 int mb_x, int mb_y, int16_t (*mv_table)[2], uint8_t *ref_picture, int f_code)
1229 int mx, my, range, dmin;
1230 int xmin, ymin, xmax, ymax;
1231 int rel_xmin, rel_ymin, rel_xmax, rel_ymax;
1232 int pred_x=0, pred_y=0;
1234 const int shift= 1+s->quarter_sample;
1235 const int mot_stride = s->mb_width + 2;
1236 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
1238 get_limits(s, &range, &xmin, &ymin, &xmax, &ymax, f_code);
1239 rel_xmin= xmin - mb_x*16;
1240 rel_xmax= xmax - mb_x*16;
1241 rel_ymin= ymin - mb_y*16;
1242 rel_ymax= ymax - mb_y*16;
1244 switch(s->me_method) {
1247 no_motion_search(s, &mx, &my);
1253 dmin = full_motion_search(s, &mx, &my, range, xmin, ymin, xmax, ymax, ref_picture);
1258 dmin = log_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1263 dmin = phods_motion_search(s, &mx, &my, range / 2, xmin, ymin, xmax, ymax, ref_picture);
1271 P_LAST[0] = mv_table[mot_xy ][0];
1272 P_LAST[1] = mv_table[mot_xy ][1];
1273 P_LEFT[0] = mv_table[mot_xy - 1][0];
1274 P_LEFT[1] = mv_table[mot_xy - 1][1];
1275 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0];
1276 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1];
1277 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0];
1278 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1];
1280 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
1281 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
1282 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
1284 /* special case for first line */
1285 if ((mb_y == 0 || s->first_slice_line)) {
1287 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1288 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1289 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
1290 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
1291 if(P_TOP[1] > (rel_ymax<<shift)) P_TOP[1]= (rel_ymax<<shift);
1292 if(P_TOPRIGHT[0] < (rel_xmin<<shift)) P_TOPRIGHT[0]= (rel_xmin<<shift);
1293 if(P_TOPRIGHT[1] > (rel_ymax<<shift)) P_TOPRIGHT[1]= (rel_ymax<<shift);
1295 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1296 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1301 dmin = epzs_motion_search(s, &mx, &my, P, pred_x, pred_y, rel_xmin, rel_ymin, rel_xmax, rel_ymax, ref_picture);
1306 dmin= fast_halfpel_motion_search(s, &mx, &my, dmin, rel_xmin, rel_ymin, rel_xmax, rel_ymax,
1307 pred_x, pred_y, ref_picture, pix_abs16x16_x2, pix_abs16x16_y2,
1308 pix_abs16x16_xy2, 0);
1309 //printf("%d %d %d %d//", s->mb_x, s->mb_y, mx, my);
1310 // s->mb_type[mb_y*s->mb_width + mb_x]= mb_type;
1311 mv_table[mot_xy][0]= mx;
1312 mv_table[mot_xy][1]= my;
1317 static inline int check_bidir_mv(MpegEncContext * s,
1319 int motion_fx, int motion_fy,
1320 int motion_bx, int motion_by,
1321 int pred_fx, int pred_fy,
1322 int pred_bx, int pred_by)
1325 //FIXME direct mode penalty
1326 UINT16 *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; // f_code of the prev frame
1327 uint8_t *dest_y = s->me_scratchpad;
1333 fbmin = (mv_penalty[motion_fx-pred_fx] + mv_penalty[motion_fy-pred_fy])*s->qscale;
1335 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1336 src_x = mb_x * 16 + (motion_fx >> 1);
1337 src_y = mb_y * 16 + (motion_fy >> 1);
1339 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
1340 put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
1342 fbmin += (mv_penalty[motion_bx-pred_bx] + mv_penalty[motion_by-pred_by])*s->qscale;
1344 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1345 src_x = mb_x * 16 + (motion_bx >> 1);
1346 src_y = mb_y * 16 + (motion_by >> 1);
1348 ptr = s->next_picture[0] + (src_y * s->linesize) + src_x;
1349 avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
1351 fbmin += pix_abs16x16(s->new_picture[0] + mb_x*16 + mb_y*16*s->linesize, dest_y, s->linesize);
1355 /* refine the bidir vectors in hq mode and return the score in both lq & hq mode*/
1356 static inline int bidir_refine(MpegEncContext * s,
1359 const int mot_stride = s->mb_width + 2;
1360 const int xy = (mb_y + 1)*mot_stride + mb_x + 1;
1362 int pred_fx= s->b_bidir_forw_mv_table[xy-1][0];
1363 int pred_fy= s->b_bidir_forw_mv_table[xy-1][1];
1364 int pred_bx= s->b_bidir_back_mv_table[xy-1][0];
1365 int pred_by= s->b_bidir_back_mv_table[xy-1][1];
1366 int motion_fx= s->b_bidir_forw_mv_table[xy][0]= s->b_forw_mv_table[xy][0];
1367 int motion_fy= s->b_bidir_forw_mv_table[xy][1]= s->b_forw_mv_table[xy][1];
1368 int motion_bx= s->b_bidir_back_mv_table[xy][0]= s->b_back_mv_table[xy][0];
1369 int motion_by= s->b_bidir_back_mv_table[xy][1]= s->b_back_mv_table[xy][1];
1371 //FIXME do refinement and add flag
1373 fbmin= check_bidir_mv(s, mb_x, mb_y,
1374 motion_fx, motion_fy,
1375 motion_bx, motion_by,
1382 static inline int direct_search(MpegEncContext * s,
1386 const int mot_stride = s->mb_width + 2;
1387 const int mot_xy = (mb_y + 1)*mot_stride + mb_x + 1;
1389 int motion_fx, motion_fy, motion_bx, motion_by, motion_bx0, motion_by0;
1390 int motion_dx, motion_dy;
1391 const int motion_px= s->p_mv_table[mot_xy][0];
1392 const int motion_py= s->p_mv_table[mot_xy][1];
1393 const int time_pp= s->pp_time;
1394 const int time_pb= s->pb_time;
1395 const int time_bp= time_pp - time_pb;
1397 int mx, my, mx2, my2;
1398 uint8_t *ref_picture= s->me_scratchpad - (mb_x - 1 + (mb_y - 1)*s->linesize)*16;
1399 int16_t (*mv_table)[2]= s->b_direct_mv_table;
1400 /* uint16_t *mv_penalty= s->mv_penalty[s->f_code] + MAX_MV; */ // f_code of the prev frame
1402 /* thanks to iso-mpeg the rounding is different for the zero vector, so we need to handle that ... */
1403 motion_fx= (motion_px*time_pb)/time_pp;
1404 motion_fy= (motion_py*time_pb)/time_pp;
1405 motion_bx0= (-motion_px*time_bp)/time_pp;
1406 motion_by0= (-motion_py*time_bp)/time_pp;
1407 motion_dx= motion_dy=0;
1408 dmin2= check_bidir_mv(s, mb_x, mb_y,
1409 motion_fx, motion_fy,
1410 motion_bx0, motion_by0,
1411 motion_fx, motion_fy,
1412 motion_bx0, motion_by0) - s->qscale;
1414 motion_bx= motion_fx - motion_px;
1415 motion_by= motion_fy - motion_py;
1416 for(by=-1; by<2; by++){
1417 for(bx=-1; bx<2; bx++){
1418 uint8_t *dest_y = s->me_scratchpad + (by+1)*s->linesize*16 + (bx+1)*16;
1422 const int width= s->width;
1423 const int height= s->height;
1425 dxy = ((motion_fy & 1) << 1) | (motion_fx & 1);
1426 src_x = (mb_x + bx) * 16 + (motion_fx >> 1);
1427 src_y = (mb_y + by) * 16 + (motion_fy >> 1);
1428 src_x = clip(src_x, -16, width);
1429 if (src_x == width) dxy &= ~1;
1430 src_y = clip(src_y, -16, height);
1431 if (src_y == height) dxy &= ~2;
1433 ptr = s->last_picture[0] + (src_y * s->linesize) + src_x;
1434 put_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
1436 dxy = ((motion_by & 1) << 1) | (motion_bx & 1);
1437 src_x = (mb_x + bx) * 16 + (motion_bx >> 1);
1438 src_y = (mb_y + by) * 16 + (motion_by >> 1);
1439 src_x = clip(src_x, -16, width);
1440 if (src_x == width) dxy &= ~1;
1441 src_y = clip(src_y, -16, height);
1442 if (src_y == height) dxy &= ~2;
1444 avg_pixels_tab[0][dxy](dest_y , ptr , s->linesize, 16);
1448 P_LAST[0] = mv_table[mot_xy ][0];
1449 P_LAST[1] = mv_table[mot_xy ][1];
1450 P_LEFT[0] = mv_table[mot_xy - 1][0];
1451 P_LEFT[1] = mv_table[mot_xy - 1][1];
1452 P_LAST_RIGHT[0] = mv_table[mot_xy + 1][0];
1453 P_LAST_RIGHT[1] = mv_table[mot_xy + 1][1];
1454 P_LAST_BOTTOM[0] = mv_table[mot_xy + mot_stride][0];
1455 P_LAST_BOTTOM[1] = mv_table[mot_xy + mot_stride][1];
1457 if(P_LEFT[0] > (rel_xmax<<shift)) P_LEFT[0] = (rel_xmax<<shift);
1458 if(P_LAST_RIGHT[0] < (rel_xmin<<shift)) P_LAST_RIGHT[0] = (rel_xmin<<shift);
1459 if(P_LAST_BOTTOM[1]< (rel_ymin<<shift)) P_LAST_BOTTOM[1]= (rel_ymin<<shift);
1461 /* special case for first line */
1462 if ((mb_y == 0 || s->first_slice_line)) {
1464 P_TOP[0] = mv_table[mot_xy - mot_stride ][0];
1465 P_TOP[1] = mv_table[mot_xy - mot_stride ][1];
1466 P_TOPRIGHT[0] = mv_table[mot_xy - mot_stride + 1 ][0];
1467 P_TOPRIGHT[1] = mv_table[mot_xy - mot_stride + 1 ][1];
1469 P_MEDIAN[0]= mid_pred(P_LEFT[0], P_TOP[0], P_TOPRIGHT[0]);
1470 P_MEDIAN[1]= mid_pred(P_LEFT[1], P_TOP[1], P_TOPRIGHT[1]);
1472 dmin = epzs_motion_search(s, &mx, &my, P, 0, 0, -16, -16, 15, 15, ref_picture);
1473 if(mx==0 && my==0) dmin=99999999; // not representable, due to rounding stuff
1482 for(by=-1; by<2; by++){
1483 if(my2+by < -32) continue;
1484 for(bx=-1; bx<2; bx++){
1485 if(bx==0 && by==0) continue;
1486 if(mx2+bx < -32) continue;
1487 dmin2= check_bidir_mv(s, mb_x, mb_y,
1488 mx2+bx+motion_fx, my2+by+motion_fy,
1489 mx2+bx+motion_bx, my2+by+motion_by,
1490 mx2+bx+motion_fx, my2+by+motion_fy,
1491 motion_bx, motion_by) - s->qscale;
1504 motion_bx= motion_bx0;
1505 motion_by= motion_by0;
1508 s->b_direct_mv_table[mot_xy][0]= mx;
1509 s->b_direct_mv_table[mot_xy][1]= my;
1510 s->b_direct_forw_mv_table[mot_xy][0]= motion_fx + mx;
1511 s->b_direct_forw_mv_table[mot_xy][1]= motion_fy + my;
1512 s->b_direct_back_mv_table[mot_xy][0]= motion_bx + mx;
1513 s->b_direct_back_mv_table[mot_xy][1]= motion_by + my;
1517 void ff_estimate_b_frame_motion(MpegEncContext * s,
1520 const int quant= s->qscale;
1521 int fmin, bmin, dmin, fbmin;
1524 dmin= direct_search(s, mb_x, mb_y);
1526 fmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_forw_mv_table, s->last_picture[0], s->f_code);
1527 bmin= ff_estimate_motion_b(s, mb_x, mb_y, s->b_back_mv_table, s->next_picture[0], s->b_code) - quant;
1528 //printf(" %d %d ", s->b_forw_mv_table[xy][0], s->b_forw_mv_table[xy][1]);
1530 fbmin= bidir_refine(s, mb_x, mb_y);
1534 type=MB_TYPE_DIRECT;
1538 type= MB_TYPE_FORWARD;
1542 type= MB_TYPE_BACKWARD;
1546 type= MB_TYPE_BIDIR;
1548 score= (score*score)>>8;
1549 s->mc_mb_var_sum += score;
1550 s->mc_mb_var[mb_y*s->mb_width + mb_x] = score; //FIXME use SSD
1553 if(s->flags&CODEC_FLAG_HQ){
1554 type= MB_TYPE_FORWARD | MB_TYPE_BACKWARD | MB_TYPE_BIDIR | MB_TYPE_DIRECT; //FIXME something smarter
1561 if(type==MB_TYPE_DIRECT){
1562 int diff= ABS(s->b_forw_mv_table)
1566 s->mb_type[mb_y*s->mb_width + mb_x]= type;
1567 /* if(mb_y==0 && mb_x==0) printf("\n");
1568 if(mb_x==0) printf("\n");
1569 printf("%d", av_log2(type));
1573 /* find best f_code for ME which do unlimited searches */
1574 int ff_get_best_fcode(MpegEncContext * s, int16_t (*mv_table)[2], int type)
1576 if(s->me_method>=ME_EPZS){
1579 UINT8 * fcode_tab= s->fcode_tab;
1581 int best_score=-10000000;
1583 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
1585 for(y=0; y<s->mb_height; y++){
1587 int xy= (y+1)* (s->mb_width+2) + 1;
1589 for(x=0; x<s->mb_width; x++){
1590 if(s->mb_type[i] & type){
1591 int fcode= MAX(fcode_tab[mv_table[xy][0] + MAX_MV],
1592 fcode_tab[mv_table[xy][1] + MAX_MV]);
1595 for(j=0; j<fcode && j<8; j++){
1596 if(s->pict_type==B_TYPE || s->mc_mb_var[i] < s->mb_var[i])
1606 if(score[i] > best_score){
1607 best_score= score[i];
1610 // printf("%d %d\n", i, score[i]);
1613 // printf("fcode: %d type: %d\n", i, s->pict_type);
1615 /* for(i=0; i<=MAX_FCODE; i++){
1616 printf("%d ", mv_num[i]);
1624 void ff_fix_long_p_mvs(MpegEncContext * s)
1626 const int f_code= s->f_code;
1628 UINT8 * fcode_tab= s->fcode_tab;
1631 /* clip / convert to intra 16x16 type MVs */
1632 for(y=0; y<s->mb_height; y++){
1634 int xy= (y+1)* (s->mb_width+2)+1;
1635 int i= y*s->mb_width;
1636 for(x=0; x<s->mb_width; x++){
1637 if(s->mb_type[i]&MB_TYPE_INTER){
1638 if( fcode_tab[s->p_mv_table[xy][0] + MAX_MV] > f_code
1639 || fcode_tab[s->p_mv_table[xy][0] + MAX_MV] == 0
1640 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] > f_code
1641 || fcode_tab[s->p_mv_table[xy][1] + MAX_MV] == 0 ){
1642 s->mb_type[i] &= ~MB_TYPE_INTER;
1643 s->mb_type[i] |= MB_TYPE_INTRA;
1644 s->p_mv_table[xy][0] = 0;
1645 s->p_mv_table[xy][1] = 0;
1655 //printf("%d no:%d %d//\n", clip, noclip, f_code);
1656 if(s->flags&CODEC_FLAG_4MV){
1657 const int wrap= 2+ s->mb_width*2;
1659 /* clip / convert to intra 8x8 type MVs */
1660 for(y=0; y<s->mb_height; y++){
1661 int xy= (y*2 + 1)*wrap + 1;
1662 int i= y*s->mb_width;
1665 for(x=0; x<s->mb_width; x++){
1666 if(s->mb_type[i]&MB_TYPE_INTER4V){
1668 for(block=0; block<4; block++){
1669 int off= (block& 1) + (block>>1)*wrap;
1670 int mx= s->motion_val[ xy + off ][0];
1671 int my= s->motion_val[ xy + off ][1];
1673 if( fcode_tab[mx + MAX_MV] > f_code
1674 || fcode_tab[mx + MAX_MV] == 0
1675 || fcode_tab[my + MAX_MV] > f_code
1676 || fcode_tab[my + MAX_MV] == 0 ){
1677 s->mb_type[i] &= ~MB_TYPE_INTER4V;
1678 s->mb_type[i] |= MB_TYPE_INTRA;
1689 void ff_fix_long_b_mvs(MpegEncContext * s, int16_t (*mv_table)[2], int f_code, int type)
1692 UINT8 * fcode_tab= s->fcode_tab;
1694 /* clip / convert to intra 16x16 type MVs */
1695 for(y=0; y<s->mb_height; y++){
1697 int xy= (y+1)* (s->mb_width+2)+1;
1698 int i= y*s->mb_width;
1699 for(x=0; x<s->mb_width; x++){
1700 if(s->mb_type[i]&type){
1701 if( fcode_tab[mv_table[xy][0] + MAX_MV] > f_code
1702 || fcode_tab[mv_table[xy][0] + MAX_MV] == 0
1703 || fcode_tab[mv_table[xy][1] + MAX_MV] > f_code
1704 || fcode_tab[mv_table[xy][1] + MAX_MV] == 0 ){
1705 if(s->mb_type[i]&(~type)) s->mb_type[i] &= ~type;
1707 mv_table[xy][0] = 0;
1708 mv_table[xy][1] = 0;
1709 //this is certainly bad FIXME