[cvs] / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Diff of /xvidcore/src/motion/motion_est.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.44.2.51, Fri Feb 7 15:02:56 2003 UTC revision 1.58.2.7, Sat Mar 29 12:01:09 2003 UTC
# Line 44  Line 44 
44  #include "motion.h"  #include "motion.h"
45  #include "sad.h"  #include "sad.h"
46  #include "../utils/emms.h"  #include "../utils/emms.h"
47    #include "../dct/fdct.h"
48    
49    /*****************************************************************************
50     * Modified rounding tables -- declared in motion.h
51     * Original tables see ISO spec tables 7-6 -> 7-9
52     ****************************************************************************/
53    
54    const uint32_t roundtab[16] =
55    {0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
56    
57    /* K = 4 */
58    const uint32_t roundtab_76[16] =
59    { 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1 };
60    
61    /* K = 2 */
62    const uint32_t roundtab_78[8] =
63    { 0, 0, 1, 1, 0, 0, 0, 1  };
64    
65    /* K = 1 */
66    const uint32_t roundtab_79[4] =
67    { 0, 1, 0, 0 };
68    
69  #define INITIAL_SKIP_THRESH     (10)  #define INITIAL_SKIP_THRESH     (10)
70  #define FINAL_SKIP_THRESH       (50)  #define FINAL_SKIP_THRESH       (50)
# Line 51  Line 72 
72  #define MAX_CHROMA_SAD_FOR_SKIP (22)  #define MAX_CHROMA_SAD_FOR_SKIP (22)
73    
74  #define CHECK_CANDIDATE(X,Y,D) { \  #define CHECK_CANDIDATE(X,Y,D) { \
75  (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }  CheckCandidate((X),(Y), (D), &iDirection, data ); }
76    
77    /*****************************************************************************
78     * Code
79     ****************************************************************************/
80    
81  static __inline uint32_t  static __inline uint32_t
82  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)
# Line 224  Line 249 
249          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
250          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;          ref1 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
251          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
252          case 0: // pure halfpel position          case 3: // x and y in qpel resolution - the "corners" (top left/right and
253                  return (uint8_t *) ref1;                          // bottom left/right) during qpel refinement
254                    ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
255                    ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
256                    ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
257                    ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
258                    ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
259                    ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;
260                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
261                  break;                  break;
262    
263          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
# Line 240  Line 272 
272                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
273                  break;                  break;
274    
275          default: // x and y in qpel resolution - the "corners" (top left/right and          default: // pure halfpel position
276                           // bottom left/right) during qpel refinement                  return (uint8_t *) ref1;
277                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);  
                 ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);  
                 ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);  
                 ref2 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref3 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 ref4 += 8 * (block&1) + 8 * (block>>1) * iEdgedWidth;  
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 break;  
278          }          }
279          return Reference;          return Reference;
280  }  }
# Line 294  Line 319 
319                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, iEdgedWidth, rounding, 8);
320                  break;                  break;
321    
322          case 0: // pure halfpel position          default: // pure halfpel position
323                  return (uint8_t *) ref1;                  return (uint8_t *) ref1;
324          }          }
325          return Reference;          return Reference;
# Line 380  Line 405 
405          uint32_t t;          uint32_t t;
406          const uint8_t * Reference;          const uint8_t * Reference;
407    
408          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero even value
409                  (x > data->max_dx) || (x < data->min_dx)                  (x > data->max_dx) || (x < data->min_dx)
410                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
411    
# Line 415  Line 440 
440          uint32_t t;          uint32_t t;
441          VECTOR * current;          VECTOR * current;
442    
443          if ( (x > data->max_dx) | ( x < data->min_dx)          if ( (x > data->max_dx) || ( x < data->min_dx)
444                  | (y > data->max_dy) | (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
445    
446          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value          if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; //non-zero even value
447    
# Line 438  Line 463 
463          if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],          if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
464                                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);                                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);
465    
   
466          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
467                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
468                  current->x = x; current->y = y;                  current->x = x; current->y = y;
# Line 482  Line 506 
506          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
507          VECTOR *current;          VECTOR *current;
508    
509          if ( (xf > data->max_dx) | (xf < data->min_dx)          if ((xf > data->max_dx) || (xf < data->min_dx) ||
510                  | (yf > data->max_dy) | (yf < data->min_dy) ) return;                  (yf > data->max_dy) || (yf < data->min_dy))
511                    return;
512    
513          if (!data->qpel_precision) {          if (!data->qpel_precision) {
514                  ReferenceF = GetReference(xf, yf, data);                  ReferenceF = GetReference(xf, yf, data);
# Line 528  Line 553 
553          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
554          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
555    
556          if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
557    
558          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
559                  mvs.x = data->directmvF[k].x + x;                  mvs.x = data->directmvF[k].x + x;
# Line 541  Line 566 
566                          data->directmvB[k].y                          data->directmvB[k].y
567                          : mvs.y - data->referencemv[k].y);                          : mvs.y - data->referencemv[k].y);
568    
569                  if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)                  if ((mvs.x > data->max_dx)   || (mvs.x < data->min_dx)   ||
570                          | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)                          (mvs.y > data->max_dy)   || (mvs.y < data->min_dy)   ||
571                          | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)                          (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx) ||
572                          | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;                          (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) )
573                            return;
574    
575                  if (data->qpel) {                  if (data->qpel) {
576                          xcf += mvs.x/2; ycf += mvs.y/2;                          xcf += mvs.x/2; ycf += mvs.y/2;
# Line 586  Line 612 
612          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
613          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
614    
615          if (( x > 31) | ( x < -32) | ( y > 31) | (y < -32)) return;          if (( x > 31) || ( x < -32) || ( y > 31) || (y < -32)) return;
616    
617          mvs.x = data->directmvF[0].x + x;          mvs.x = data->directmvF[0].x + x;
618          b_mvs.x = ((x == 0) ?          b_mvs.x = ((x == 0) ?
# Line 598  Line 624 
624                  data->directmvB[0].y                  data->directmvB[0].y
625                  : mvs.y - data->referencemv[0].y);                  : mvs.y - data->referencemv[0].y);
626    
627          if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)          if ( (mvs.x > data->max_dx) || (mvs.x < data->min_dx)
628                  | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)                  || (mvs.y > data->max_dy) || (mvs.y < data->min_dy)
629                  | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)                  || (b_mvs.x > data->max_dx) || (b_mvs.x < data->min_dx)
630                  | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;                  || (b_mvs.y > data->max_dy) || (b_mvs.y < data->min_dy) ) return;
631    
632          if (data->qpel) {          if (data->qpel) {
633                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
# Line 630  Line 656 
656          }          }
657  }  }
658    
659    
660    static void
661    CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
662    {
663    
664            int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
665            int32_t bits = 0, sum;
666            VECTOR * current;
667            const uint8_t * ptr;
668            int i, cbp = 0, t, xc, yc;
669    
670            if ( (x > data->max_dx) || (x < data->min_dx)
671                    || (y > data->max_dy) || (y < data->min_dy) ) return;
672    
673            if (!data->qpel_precision) {
674                    ptr = GetReference(x, y, data);
675                    current = data->currentMV;
676                    xc = x; yc = y;
677            } else { // x and y are in 1/4 precision
678                    ptr = Interpolate16x16qpel(x, y, 0, data);
679                    current = data->currentQMV;
680                    xc = x/2; yc = y/2;
681            }
682    
683            for(i = 0; i < 4; i++) {
684                    int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
685                    transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
686                    fdct(in);
687                    if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
688                    else sum = quant4_inter(coeff, in, data->lambda16);
689                    if (sum > 0) {
690                            cbp |= 1 << (5 - i);
691                            bits += data->temp[i] = CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
692                    } else data->temp[i] = 0;
693            }
694    
695            bits += t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
696    
697            if (bits < data->iMinSAD[0]) { // there is still a chance, adding chroma
698                    xc = (xc >> 1) + roundtab_79[xc & 0x3];
699                    yc = (yc >> 1) + roundtab_79[yc & 0x3];
700    
701                    //chroma U
702                    ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCU, 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
703                    transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);
704                    fdct(in);
705                    if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
706                    else sum = quant4_inter(coeff, in, data->lambda16);
707                    if (sum > 0) {
708                            cbp |= 1 << (5 - 4);
709                            bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
710                    }
711    
712                    if (bits < data->iMinSAD[0]) {
713                            //chroma V
714                            ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefCV, 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);
715                            transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);
716                            fdct(in);
717                            if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
718                            else sum = quant4_inter(coeff, in, data->lambda16);
719                            if (sum > 0) {
720                                    cbp |= 1 << (5 - 5);
721                                    bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
722                            }
723                    }
724            }
725    
726            bits += xvid_cbpy_tab[15-(cbp>>2)].len;
727            bits += mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
728    
729            if (bits < data->iMinSAD[0]) {
730                    data->iMinSAD[0] = bits;
731                    current[0].x = x; current[0].y = y;
732                    *dir = Direction;
733            }
734    
735            if (data->temp[0] + t < data->iMinSAD[1]) {
736                    data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }
737            if (data->temp[1] < data->iMinSAD[2]) {
738                    data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; }
739            if (data->temp[2] < data->iMinSAD[3]) {
740                    data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }
741            if (data->temp[3] < data->iMinSAD[4]) {
742                    data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }
743    
744    }
745    static void
746    CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
747    {
748    
749            int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
750            int32_t sum, bits;
751            VECTOR * current;
752            const uint8_t * ptr;
753            int cbp;
754    
755            if ( (x > data->max_dx) || (x < data->min_dx)
756                    || (y > data->max_dy) || (y < data->min_dy) ) return;
757    
758            if (!data->qpel_precision) {
759                    ptr = GetReference(x, y, data);
760                    current = data->currentMV;
761            } else { // x and y are in 1/4 precision
762                    ptr = Interpolate8x8qpel(x, y, 0, 0, data);
763                    current = data->currentQMV;
764            }
765    
766            transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);
767            fdct(in);
768            if (data->lambda8 == 0) sum = quant_inter(coeff, in, data->lambda16);
769            else sum = quant4_inter(coeff, in, data->lambda16);
770            if (sum > 0) {
771                    bits = CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
772                    cbp = 1;
773            } else cbp = bits = 0;
774    
775            bits += sum = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
776    
777            if (bits < data->iMinSAD[0]) {
778                    data->temp[0] = cbp;
779                    data->iMinSAD[0] = bits;
780                    current[0].x = x; current[0].y = y;
781                    *dir = Direction;
782            }
783    }
784    
785  /* CHECK_CANDIATE FUNCTIONS END */  /* CHECK_CANDIATE FUNCTIONS END */
786    
787  /* MAINSEARCH FUNCTIONS START */  /* MAINSEARCH FUNCTIONS START */
# Line 800  Line 952 
952                                                          const uint32_t stride, const uint32_t iQuant, int rrv)                                                          const uint32_t stride, const uint32_t iQuant, int rrv)
953    
954  {  {
955            int offset = (x + y*stride)*8;
956          if(!rrv) {          if(!rrv) {
957                  uint32_t sadC = sad8(current->u + x*8 + y*stride*8,                  uint32_t sadC = sad8(current->u + offset,
958                                                  reference->u + x*8 + y*stride*8, stride);                                                  reference->u + offset, stride);
959                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
960                  sadC += sad8(current->v + (x + y*stride)*8,                  sadC += sad8(current->v + offset,
961                                                  reference->v + (x + y*stride)*8, stride);                                                  reference->v + offset, stride);
962                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP) return 0;
963                  return 1;                  return 1;
964    
965          } else {          } else {
966                  uint32_t sadC = sad16(current->u + x*16 + y*stride*16,                  uint32_t sadC = sad16(current->u + 2*offset,
967                                                  reference->u + x*16 + y*stride*16, stride, 256*4096);                                                  reference->u + 2*offset, stride, 256*4096);
968                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
969                  sadC += sad16(current->v + (x + y*stride)*16,                  sadC += sad16(current->v + 2*offset,
970                                                  reference->v + (x + y*stride)*16, stride, 256*4096);                                                  reference->v + 2*offset, stride, 256*4096);
971                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;                  if (sadC > iQuant * MAX_CHROMA_SAD_FOR_SKIP*4) return 0;
972                  return 1;                  return 1;
973          }          }
# Line 845  Line 998 
998          uint32_t mb_width = pParam->mb_width;          uint32_t mb_width = pParam->mb_width;
999          uint32_t mb_height = pParam->mb_height;          uint32_t mb_height = pParam->mb_height;
1000          const uint32_t iEdgedWidth = pParam->edged_width;          const uint32_t iEdgedWidth = pParam->edged_width;
1001            const uint32_t MotionFlags = MakeGoodMotionFlags(current->motion_flags, current->vop_flags, current->vol_flags);
1002    
1003          uint32_t x, y;          uint32_t x, y;
1004          uint32_t iIntra = 0;          uint32_t iIntra = 0;
1005          int32_t InterBias, quant = current->quant, sad00;          int32_t quant = current->quant, sad00;
1006            int skip_thresh = \
1007                    INITIAL_SKIP_THRESH * \
1008                    (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
1009                    (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1);
1010    
1011          // some pre-initialized thingies for SearchP          // some pre-initialized thingies for SearchP
1012          int32_t temp[8];          int32_t temp[8];
1013          VECTOR currentMV[5];          VECTOR currentMV[5];
1014          VECTOR currentQMV[5];          VECTOR currentQMV[5];
1015          int32_t iMinSAD[5];          int32_t iMinSAD[5];
1016            DECLARE_ALIGNED_MATRIX(dct_space, 2, 64, int16_t, CACHE_LINE);
1017          SearchData Data;          SearchData Data;
1018          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
1019          Data.iEdgedWidth = iEdgedWidth;          Data.iEdgedWidth = iEdgedWidth;
# Line 864  Line 1023 
1023          Data.temp = temp;          Data.temp = temp;
1024          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1025          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1026          Data.qpel = pParam->m_quarterpel;          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1027          Data.chroma = current->motion_flags & PMV_CHROMA16;          Data.chroma = MotionFlags & XVID_ME_CHROMA16;
1028          Data.rrv = current->global_flags & XVID_REDUCED;          Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0);
1029            Data.dctSpace = dct_space;
1030    
1031          if ((current->global_flags & XVID_REDUCED)) {          if ((current->vop_flags & XVID_VOP_REDUCED)) {
1032                  mb_width = (pParam->width + 31) / 32;                  mb_width = (pParam->width + 31) / 32;
1033                  mb_height = (pParam->height + 31) / 32;                  mb_height = (pParam->height + 31) / 32;
1034                  Data.qpel = Data.chroma = 0;                  Data.qpel = 0;
1035          }          }
1036    
1037          Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose)          Data.RefQ = pRefV->u; // a good place, also used in MC (for similar purpose)
# Line 901  Line 1061 
1061    
1062                          sad00 = pMB->sad16;                          sad00 = pMB->sad16;
1063    
1064                          if (!(current->global_flags & XVID_LUMIMASKING)) {                          if (pMB->dquant != 0) {
                                 pMB->dquant = NO_CHANGE;  
                         } else {  
                                 if (pMB->dquant != NO_CHANGE) {  
1065                                          quant += DQtab[pMB->dquant];                                          quant += DQtab[pMB->dquant];
1066                                          if (quant > 31) quant = 31;                                          if (quant > 31) quant = 31;
1067                                          else if (quant < 1) quant = 1;                                          else if (quant < 1) quant = 1;
1068                                  }                                  }
1069                          }  
1070                          pMB->quant = current->quant;                          pMB->quant = current->quant;
1071    
1072  //initial skip decision  //initial skip decision
1073  /* no early skip for GMC (global vector = skip vector is unknown!)  */  /* no early skip for GMC (global vector = skip vector is unknown!)  */
1074                          if (!(current->global_flags & XVID_GMC))        { /* no fast SKIP for S(GMC)-VOPs */                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */
1075                                  if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) )                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
1076                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {
1077                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
1078                                                  continue;                                                  continue;
# Line 923  Line 1080 
1080                          }                          }
1081    
1082                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1083                                                  y, current->motion_flags, pMB->quant,                                                  y, MotionFlags, current->vol_flags, pMB->quant,
1084                                                  &Data, pParam, pMBs, reference->mbs,                                                  &Data, pParam, pMBs, reference->mbs,
1085                                                  current->global_flags & XVID_INTER4V, pMB);                                                  current->vop_flags & XVID_VOP_INTER4V, pMB);
1086    
1087  /* final skip decision, a.k.a. "the vector you found, really that good?" */  /* final skip decision, a.k.a. "the vector you found, really that good?" */
1088                          if (!(current->global_flags & XVID_GMC))        {                          if (!(current->vol_flags & XVID_VOL_GMC || current->vop_flags & XVID_VOP_MODEDECISION_BITS)) {
1089                                  if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)                                  if ( pMB->dquant == 0 && sad00 < pMB->quant * MAX_SAD00_FOR_SKIP) {
1090                                          && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1)) )                                          if ( (100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1) )
1091                                          if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv)) {                                                  if (Data.chroma || SkipDecisionP(pCurrent, pRef, x, y, iEdgedWidth/2, pMB->quant, Data.rrv))
1092                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
                                                 continue;  
                                         }  
                         }  
   
 /* finally, intra decision */  
   
                         InterBias = MV16_INTER_BIAS;  
                         if (pMB->quant > 8) InterBias += 100 * (pMB->quant - 8); // to make high quants work  
                         if (y != 0)  
                                 if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;  
                         if (x != 0)  
                                 if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;  
   
                         if (Data.chroma) InterBias += 50; // to compensate bigger SAD  
                         if (Data.rrv) InterBias *= 4;  
   
                         if (InterBias < pMB->sad16) {  
                                 int32_t deviation;  
                                 if (!Data.rrv)  
                                         deviation = dev16(pCurrent->y + (x + y * iEdgedWidth) * 16, iEdgedWidth);  
                                 else {  
                                         deviation = dev16(pCurrent->y + (x + y * iEdgedWidth) * 32, iEdgedWidth)  
                                                 + dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16, iEdgedWidth)  
                                                 + dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16 * iEdgedWidth, iEdgedWidth)  
                                                 + dev16(pCurrent->y + (x + y * iEdgedWidth) * 32 + 16 * (iEdgedWidth+1), iEdgedWidth);  
                                 }  
                                 if (deviation < (pMB->sad16 - InterBias)) {  
                                         if (++iIntra >= iLimit) return 1;  
                                         SkipMacroblockP(pMB, 0); //same thing  
                                         pMB->mode = MODE_INTRA;  
1093                                  }                                  }
1094                          }                          }
1095                            if (pMB->mode == MODE_INTRA)
1096                                    if (++iIntra > iLimit) return 1;
1097                  }                  }
1098          }          }
1099    
1100          if (current->global_flags & XVID_GMC )  /* GMC only for S(GMC)-VOPs */          if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */
         {  
1101                  current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);                  current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);
         }  
1102    
1103          return 0;          return 0;
1104  }  }
# Line 1036  Line 1163 
1163          }          }
1164  }  }
1165    
1166    static int
1167    ModeDecision(const uint32_t iQuant, SearchData * const Data,
1168                    int inter4v,
1169                    MACROBLOCK * const pMB,
1170                    const MACROBLOCK * const pMBs,
1171                    const int x, const int y,
1172                    const MBParam * const pParam,
1173                    const uint32_t MotionFlags,
1174                    const uint32_t VopFlags)
1175    {
1176    
1177            int mode = MODE_INTER;
1178    
1179            if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { //normal, fast, SAD-based mode decision
1180                    int sad;
1181                    int InterBias = MV16_INTER_BIAS;
1182                    if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
1183                            Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant) {
1184                            mode = MODE_INTER;
1185                            sad = Data->iMinSAD[0];
1186                    } else {
1187                            mode = MODE_INTER4V;
1188                            sad = Data->iMinSAD[1] + Data->iMinSAD[2] +
1189                                                    Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant;
1190                            Data->iMinSAD[0] = sad;
1191                    }
1192    
1193                    /* intra decision */
1194    
1195                    if (iQuant > 8) InterBias += 100 * (iQuant - 8); // to make high quants work
1196                    if (y != 0)
1197                            if ((pMB - pParam->mb_width)->mode == MODE_INTRA ) InterBias -= 80;
1198                    if (x != 0)
1199                            if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
1200    
1201                    if (Data->chroma) InterBias += 50; // to compensate bigger SAD
1202                    if (Data->rrv) InterBias *= 4;
1203    
1204                    if (InterBias < pMB->sad16) {
1205                            int32_t deviation;
1206                            if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth);
1207                            else deviation = dev16(Data->Cur, Data->iEdgedWidth) +
1208                                    dev16(Data->Cur+8, Data->iEdgedWidth) +
1209                                    dev16(Data->Cur + 8*Data->iEdgedWidth, Data->iEdgedWidth) +
1210                                    dev16(Data->Cur+8+8*Data->iEdgedWidth, Data->iEdgedWidth);
1211    
1212                            if (deviation < (sad - InterBias)) return MODE_INTRA;
1213                    }
1214                    return mode;
1215    
1216            } else {
1217    
1218                    int bits, intra, i;
1219                    VECTOR backup[5], *v;
1220                    Data->lambda16 = iQuant;
1221            Data->lambda8 = (pParam->vol_flags & XVID_VOL_MPEGQUANT)?1:0;
1222    
1223                    v = Data->qpel ? Data->currentQMV : Data->currentMV;
1224                    for (i = 0; i < 5; i++) {
1225                            Data->iMinSAD[i] = 256*4096;
1226                            backup[i] = v[i];
1227                    }
1228    
1229                    bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);
1230                    if (bits == 0) return MODE_INTER; // quick stop
1231    
1232                    if (inter4v) {
1233                            int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
1234                            if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; }
1235                    }
1236    
1237    
1238                    intra = CountMBBitsIntra(Data);
1239    
1240                    if (intra < bits) { *Data->iMinSAD = bits = intra; return MODE_INTRA; }
1241    
1242                    return mode;
1243            }
1244    }
1245    
1246  static void  static void
1247  SearchP(const IMAGE * const pRef,  SearchP(const IMAGE * const pRef,
1248                  const uint8_t * const pRefH,                  const uint8_t * const pRefH,
# Line 1045  Line 1252 
1252                  const int x,                  const int x,
1253                  const int y,                  const int y,
1254                  const uint32_t MotionFlags,                  const uint32_t MotionFlags,
1255                    const uint32_t VopFlags,
1256                  const uint32_t iQuant,                  const uint32_t iQuant,
1257                  SearchData * const Data,                  SearchData * const Data,
1258                  const MBParam * const pParam,                  const MBParam * const pParam,
# Line 1079  Line 1287 
1287          Data->lambda8 = lambda_vec8[iQuant];          Data->lambda8 = lambda_vec8[iQuant];
1288          Data->qpel_precision = 0;          Data->qpel_precision = 0;
1289    
1290          if (pMB->dquant != NO_CHANGE) inter4v = 0;          if (pMB->dquant != 0) inter4v = 0;
1291    
1292          for(i = 0; i < 5; i++)          memset(Data->currentMV, 0, 5*sizeof(VECTOR));
                 Data->currentMV[i].x = Data->currentMV[i].y = 0;  
1293    
1294          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);          if (Data->qpel) Data->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, 0);
1295          else Data->predMV = pmv[0];          else Data->predMV = pmv[0];
# Line 1094  Line 1301 
1301          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
1302          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
1303    
1304          if (x | y) {          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) || (x | y)) {
1305                  threshA = Data->temp[0]; // that's when we keep this SAD atm                  threshA = Data->temp[0]; // that's where we keep this SAD atm
1306                  if (threshA < 512) threshA = 512;                  if (threshA < 512) threshA = 512;
1307                  else if (threshA > 1024) threshA = 1024;                  else if (threshA > 1024) threshA = 1024;
1308          } else threshA = 512;          } else
1309                    threshA = 512;
1310    
1311          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,          PreparePredictionsP(pmv, x, y, pParam->mb_width, pParam->mb_height,
1312                                          prevMBs + x + y * pParam->mb_width, Data->rrv);                                          prevMBs + x + y * pParam->mb_width, Data->rrv);
# Line 1112  Line 1320 
1320    
1321          for (i = 1; i < 7; i++) {          for (i = 1; i < 7; i++) {
1322                  if (!(mask = make_mask(pmv, i)) ) continue;                  if (!(mask = make_mask(pmv, i)) ) continue;
1323                  (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1324                  if (Data->iMinSAD[0] <= threshA) break;                  if (Data->iMinSAD[0] <= threshA) break;
1325          }          }
1326    
1327          if ((Data->iMinSAD[0] <= threshA) ||          if ((Data->iMinSAD[0] <= threshA) ||
1328                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&                          (MVequal(Data->currentMV[0], (prevMBs+x+y*pParam->mb_width)->mvs[0]) &&
1329                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16)))                          (Data->iMinSAD[0] < (prevMBs+x+y*pParam->mb_width)->sad16))) {
1330                  inter4v = 0;                  if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) inter4v = 0;      }
1331          else {          else {
1332    
1333                  MainSearchFunc * MainSearchPtr;                  MainSearchFunc * MainSearchPtr;
1334                  if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;                  if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1335                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1336                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1337    
1338                  (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1339    
1340  /* extended search, diamond starting in 0,0 and in prediction.  /* extended search, diamond starting in 0,0 and in prediction.
1341          note that this search is/might be done in halfpel positions,          note that this search is/might be done in halfpel positions,
1342          which makes it more different than the diamond above */          which makes it more different than the diamond above */
1343    
1344                  if (MotionFlags & PMV_EXTSEARCH16) {                  if (MotionFlags & XVID_ME_EXTSEARCH16) {
1345                          int32_t bSAD;                          int32_t bSAD;
1346                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];                          VECTOR startMV = Data->predMV, backupMV = Data->currentMV[0];
1347                          if (Data->rrv) {                          if (Data->rrv) {
1348                                  startMV.x = RRV_MV_SCALEUP(startMV.x);                                  startMV.x = RRV_MV_SCALEUP(startMV.x);
1349                                  startMV.y = RRV_MV_SCALEUP(startMV.y);                                  startMV.y = RRV_MV_SCALEUP(startMV.y);
1350                          } else                          }
                                 if (!(MotionFlags & PMV_HALFPELREFINE16)) // who's gonna use extsearch and no halfpel?  
                                         startMV.x = EVEN(startMV.x); startMV.y = EVEN(startMV.y);  
1351                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1352                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1353    
1354                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);                                  CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data);
1355                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  MainSearchPtr(startMV.x, startMV.y, Data, 255);
1356                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1357                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
1358                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD; }
1359                          }                          }
1360    
1361                          backupMV = Data->currentMV[0];                          backupMV = Data->currentMV[0];
1362                          if (MotionFlags & PMV_HALFPELREFINE16 && !Data->rrv) startMV.x = startMV.y = 1;                          startMV.x = startMV.y = 1;
                         else startMV.x = startMV.y = 0;  
1363                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1364                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1365    
1366                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);                                  CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data);
1367                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  MainSearchPtr(startMV.x, startMV.y, Data, 255);
1368                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1369                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
1370                                          Data->iMinSAD[0] = bSAD; }                                          Data->iMinSAD[0] = bSAD; }
# Line 1167  Line 1372 
1372                  }                  }
1373          }          }
1374    
1375          if (MotionFlags & PMV_HALFPELREFINE16) SubpelRefine(Data);          if (MotionFlags & XVID_ME_HALFPELREFINE16)
1376                    if ((!(MotionFlags & XVID_ME_HALFPELREFINE16_BITS)) || Data->iMinSAD[0] < 200*(int)iQuant)
1377                            SubpelRefine(Data);
1378    
1379          for(i = 0; i < 5; i++) {          for(i = 0; i < 5; i++) {
1380                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // initialize qpel vectors
1381                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
1382          }          }
1383    
1384          if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE16) {          if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
1385                  Data->qpel_precision = 1;  
1386                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
1387                                  pParam->width, pParam->height, Data->iFcode, 1, 0);                                  pParam->width, pParam->height, Data->iFcode, 1, 0);
1388    
1389                    if ((!(MotionFlags & XVID_ME_QUARTERPELREFINE16_BITS)) || (Data->iMinSAD[0] < 200*(int)iQuant)) {
1390                            Data->qpel_precision = 1;
1391                  SubpelRefine(Data);                  SubpelRefine(Data);
1392          }          }
1393            }
1394    
1395            if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (Data->iMinSAD[0] < (int32_t)iQuant * 30)) inter4v = 0;
1396    
1397            if (inter4v && (!(VopFlags & XVID_VOP_MODEDECISION_BITS) ||
1398                            (!(MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS)) || (!(MotionFlags & XVID_ME_HALFPELREFINE8_BITS)) ||
1399                            ((!(MotionFlags & XVID_ME_EXTSEARCH_BITS)) && (!(MotionFlags&XVID_ME_EXTSEARCH8)) ))) {
1400                    // if decision is BITS-based and all refinement steps will be done in BITS domain, there is no reason to call this loop
1401    
         if (Data->iMinSAD[0] < (int32_t)iQuant * 30) inter4v = 0;  
         if (inter4v) {  
1402                  SearchData Data8;                  SearchData Data8;
1403                  memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data                  memcpy(&Data8, Data, sizeof(SearchData)); //quick copy of common data
1404    
# Line 1192  Line 1407 
1407                  Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);                  Search8(Data, 2*x, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 2, &Data8);
1408                  Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);                  Search8(Data, 2*x + 1, 2*y + 1, MotionFlags, pParam, pMB, pMBs, 3, &Data8);
1409    
1410                  if (Data->chroma) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {
1411                            // chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, there is no reason to compute it
1412                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
1413                          const int div = 1 + Data->qpel;                          const int div = 1 + Data->qpel;
1414                          const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs;                          const VECTOR * const mv = Data->qpel ? pMB->qmvs : pMB->mvs;
# Line 1207  Line 1423 
1423                  }                  }
1424          }          }
1425    
1426            inter4v = ModeDecision(iQuant, Data, inter4v, pMB, pMBs, x, y, pParam, MotionFlags, VopFlags);
1427    
1428          if (Data->rrv) {          if (Data->rrv) {
1429                          Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);                          Data->currentMV[0].x = RRV_MV_SCALEDOWN(Data->currentMV[0].x);
1430                          Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);                          Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
1431          }          }
1432    
1433          if (!(inter4v) ||          if (inter4v == MODE_INTER) {
                 (Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +  
                         Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * (int32_t)iQuant )) {  
 // INTER MODE  
1434                  pMB->mode = MODE_INTER;                  pMB->mode = MODE_INTER;
1435                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];                  pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
1436                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];                  pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];
# Line 1229  Line 1444 
1444                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;                          pMB->pmvs[0].x = Data->currentMV[0].x - Data->predMV.x;
1445                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1446                  }                  }
1447          } else {  
1448  // INTER4V MODE; all other things are already set in Search8          } else if (inter4v == MODE_INTER4V) {
1449                  pMB->mode = MODE_INTER4V;                  pMB->mode = MODE_INTER4V;
1450                  pMB->sad16 = Data->iMinSAD[1] + Data->iMinSAD[2] +                  pMB->sad16 = Data->iMinSAD[0];
1451                          Data->iMinSAD[3] + Data->iMinSAD[4] + IMV16X16 * iQuant;          } else { // INTRA mode
1452                    SkipMacroblockP(pMB, 0); // not skip, but similar enough
1453                    pMB->mode = MODE_INTRA;
1454          }          }
1455    
1456  }  }
1457    
1458  static void  static void
# Line 1264  Line 1482 
1482    
1483          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;          *(Data->iMinSAD) += (Data->lambda8 * i * (*Data->iMinSAD + NEIGH_8X8_BIAS))>>10;
1484    
1485          if (MotionFlags & (PMV_EXTSEARCH8|PMV_HALFPELREFINE8|PMV_QUARTERPELREFINE8)) {          if (MotionFlags & (XVID_ME_EXTSEARCH8|XVID_ME_HALFPELREFINE8|XVID_ME_QUARTERPELREFINE8)) {
1486                  if (Data->rrv) i = 2; else i = 1;                  if (Data->rrv) i = 2; else i = 1;
1487    
1488                  Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->Ref = OldData->Ref + i * 8 * ((block&1) + Data->iEdgedWidth*(block>>1));
# Line 1281  Line 1499 
1499                  if (!Data->rrv) CheckCandidate = CheckCandidate8;                  if (!Data->rrv) CheckCandidate = CheckCandidate8;
1500                  else CheckCandidate = CheckCandidate16no4v;                  else CheckCandidate = CheckCandidate16no4v;
1501    
1502                  if (MotionFlags & PMV_EXTSEARCH8) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {
1503                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1504    
1505                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
1506                          if (MotionFlags & PMV_USESQUARES8) MainSearchPtr = SquareSearch;                          if (MotionFlags & XVID_ME_USESQUARES8) MainSearchPtr = SquareSearch;
1507                                  else if (MotionFlags & PMV_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;                                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND8) MainSearchPtr = AdvDiamondSearch;
1508                                          else MainSearchPtr = DiamondSearch;                                          else MainSearchPtr = DiamondSearch;
1509    
1510                          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, 255);                          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, 255);
1511    
1512                          if(*(Data->iMinSAD) < temp_sad) {                          if(*(Data->iMinSAD) < temp_sad) {
1513                                          Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector                                          Data->currentQMV->x = 2 * Data->currentMV->x; // update our qpel vector
# Line 1297  Line 1515 
1515                          }                          }
1516                  }                  }
1517    
1518                  if (MotionFlags & PMV_HALFPELREFINE8) {                  if (MotionFlags & XVID_ME_HALFPELREFINE8) {
1519                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD                          int32_t temp_sad = *(Data->iMinSAD); // store current MinSAD
1520    
1521                          SubpelRefine(Data); // perform halfpel refine of current best vector                          SubpelRefine(Data); // perform halfpel refine of current best vector
# Line 1308  Line 1526 
1526                          }                          }
1527                  }                  }
1528    
1529                  if (Data->qpel && MotionFlags & PMV_QUARTERPELREFINE8) {                  if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {
1530                                  Data->qpel_precision = 1;                                  Data->qpel_precision = 1;
1531                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,                                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 8,
1532                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 1, 0);
# Line 1432  Line 1650 
1650                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate16no4v(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1651          }          }
1652    
1653          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1654          else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1655                  else MainSearchPtr = DiamondSearch;                  else MainSearchPtr = DiamondSearch;
1656    
1657          (*MainSearchPtr)(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
1658    
1659          SubpelRefine(Data);          SubpelRefine(Data);
1660    
# Line 1493  Line 1711 
1711    
1712          for (k = 0; k < 4; k++) {          for (k = 0; k < 4; k++) {
1713                  dy += Data->directmvF[k].y / div;                  dy += Data->directmvF[k].y / div;
1714                  dx += Data->directmvF[0].x / div;                  dx += Data->directmvF[k].x / div;
1715                  b_dy += Data->directmvB[0].y / div;                  b_dy += Data->directmvB[k].y / div;
1716                  b_dx += Data->directmvB[0].x / div;                  b_dx += Data->directmvB[k].x / div;
1717          }          }
1718    
1719          dy = (dy >> 3) + roundtab_76[dy & 0xf];          dy = (dy >> 3) + roundtab_76[dy & 0xf];
# Line 1515  Line 1733 
1733                                          b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,                                          b_Ref->v + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1734                                          stride);                                          stride);
1735    
1736          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) pMB->mode = MODE_DIRECT_NONE_MV; //skipped          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1737                    pMB->mode = MODE_DIRECT_NONE_MV; //skipped
1738                    for (k = 0; k < 4; k++) {
1739                            pMB->qmvs[k] = pMB->mvs[k];
1740                            pMB->b_qmvs[k] = pMB->b_mvs[k];
1741                    }
1742            }
1743  }  }
1744    
1745  static __inline uint32_t  static __inline uint32_t
# Line 1590  Line 1814 
1814    
1815          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;
1816    
1817          (*CheckCandidate)(0, 0, 255, &k, Data);          CheckCandidate(0, 0, 255, &k, Data);
1818    
1819  // initial (fast) skip decision  // initial (fast) skip decision
1820          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) {          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) {
# Line 1604  Line 1828 
1828                  }                  }
1829          }          }
1830    
1831            *Data->iMinSAD += Data->lambda16;
1832          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
1833    
1834  //      DIRECT MODE DELTA VECTOR SEARCH.  //      DIRECT MODE DELTA VECTOR SEARCH.
1835  //      This has to be made more effective, but at the moment I'm happy it's running at all  //      This has to be made more effective, but at the moment I'm happy it's running at all
1836    
1837          if (MotionFlags & PMV_USESQUARES16) MainSearchPtr = SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = SquareSearch;
1838                  else if (MotionFlags & PMV_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = AdvDiamondSearch;
1839                          else MainSearchPtr = DiamondSearch;                          else MainSearchPtr = DiamondSearch;
1840    
1841          (*MainSearchPtr)(0, 0, Data, 255);          MainSearchPtr(0, 0, Data, 255);
1842    
1843          SubpelRefine(Data);          SubpelRefine(Data);
1844    
# Line 1819  Line 2044 
2044          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
2045          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
2046          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant];
2047          Data.qpel = pParam->m_quarterpel;          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;
2048          Data.rounding = 0;          Data.rounding = 0;
2049          Data.chroma = frame->motion_flags & PMV_CHROMA8;          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;
2050          Data.temp = temp;          Data.temp = temp;
2051    
2052          Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose)          Data.RefQ = f_refV->u; // a good place, also used in MC (for similar purpose)
# Line 1929  Line 2154 
2154  {  {
2155    
2156          int i, mask;          int i, mask;
2157            int quarterpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0;
2158          VECTOR pmv[3];          VECTOR pmv[3];
2159          MACROBLOCK * pMB = &pMBs[x + y * pParam->mb_width];          MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
2160    
2161          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;          for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
2162    
# Line 1944  Line 2170 
2170                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median                          else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); //else median
2171    
2172          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,          get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2173                                  pParam->width, pParam->height, Data->iFcode - pParam->m_quarterpel, 0, Data->rrv);          pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0);
2174    
2175          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2176          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;          Data->Ref = pRef + (x + y * pParam->edged_width) * 16;
# Line 1956  Line 2182 
2182          pmv[0].x = pmv[0].y = 0;          pmv[0].x = pmv[0].y = 0;
2183    
2184          CheckCandidate32I(0, 0, 255, &i, Data);          CheckCandidate32I(0, 0, 255, &i, Data);
2185            Data->iMinSAD[1] -= 50;
2186            Data->iMinSAD[2] -= 50;
2187            Data->iMinSAD[3] -= 50;
2188            Data->iMinSAD[4] -= 50;
2189    
2190          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) {          if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) {
2191    
2192                  if (!(mask = make_mask(pmv, 1)))                  if (!(mask = make_mask(pmv, 1)))
2193                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);                          CheckCandidate32I(pmv[1].x, pmv[1].y, mask, &i, Data);
2194                  if (!(mask = make_mask(pmv, 2)))                  if (!(mask = make_mask(pmv, 2)))
2195                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);                          CheckCandidate32I(pmv[2].x, pmv[2].y, mask, &i, Data);
2196    
2197                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP * 4) // diamond only if needed                  if (*Data->iMinSAD > 4 * MAX_SAD00_FOR_SKIP) // diamond only if needed
2198                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);                          DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, i);
2199            }
2200    
2201                  for (i = 0; i < 4; i++) {                  for (i = 0; i < 4; i++) {
2202                          MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];                          MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];
# Line 1974  Line 2205 
2205                          MB->sad16 = Data->iMinSAD[i+1];                          MB->sad16 = Data->iMinSAD[i+1];
2206                  }                  }
2207          }          }
 }  
2208    
2209  #define INTRA_BIAS              2500  #define INTRA_THRESH    2400
2210  #define INTRA_THRESH    1500  #define INTER_THRESH    1100
 #define INTER_THRESH    1400  
2211    
2212  int  int
2213  MEanalysis(     const IMAGE * const pRef,  MEanalysis(     const IMAGE * const pRef,
2214                          FRAMEINFO * const Current,                          const FRAMEINFO * const Current,
2215                          MBParam * const pParam,                          const MBParam * const pParam,
2216                          int maxIntra, //maximum number if non-I frames                          const int maxIntra, //maximum number if non-I frames
2217                          int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame                          const int intraCount, //number of non-I frames after last I frame; 0 if we force P/B frame
2218                          int bCount) // number of B frames in a row                          const int bCount,  // number of B frames in a row
2219                            const int b_thresh)
2220  {  {
2221          uint32_t x, y, intra = 0;          uint32_t x, y, intra = 0;
2222          int sSAD = 0;          int sSAD = 0;
2223          MACROBLOCK * const pMBs = Current->mbs;          MACROBLOCK * const pMBs = Current->mbs;
2224          const IMAGE * const pCurrent = &Current->image;          const IMAGE * const pCurrent = &Current->image;
2225          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH;          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + 10*b_thresh;
2226            int s = 0, blocks = 0;
2227    
2228          int32_t iMinSAD[5], temp[5];          int32_t iMinSAD[5], temp[5];
2229          VECTOR currentMV[5];          VECTOR currentMV[5];
# Line 2001  Line 2232 
2232          Data.currentMV = currentMV;          Data.currentMV = currentMV;
2233          Data.iMinSAD = iMinSAD;          Data.iMinSAD = iMinSAD;
2234          Data.iFcode = Current->fcode;          Data.iFcode = Current->fcode;
         Data.rrv = Current->global_flags & XVID_REDUCED;  
2235          Data.temp = temp;          Data.temp = temp;
2236          CheckCandidate = CheckCandidate32I;          CheckCandidate = CheckCandidate32I;
2237    
2238          if (intraCount != 0 && intraCount < 10) // we're right after an I frame          if (intraCount != 0 && intraCount < 10) // we're right after an I frame
2239                  IntraThresh += 4 * (intraCount - 10) * (intraCount - 10);                  IntraThresh += 8 * (intraCount - 10) * (intraCount - 10);
2240          else          else
2241                  if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec                  if ( 5*(maxIntra - intraCount) < maxIntra) // we're close to maximum. 2 sec when max is 10 sec
2242                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;                          IntraThresh -= (IntraThresh * (maxIntra - 5*(maxIntra - intraCount)))/maxIntra;
2243    
2244          InterThresh += 400 * (1 - bCount);          InterThresh -= (350 - 8*b_thresh) * bCount;
2245          if (InterThresh < 300) InterThresh = 300;          if (InterThresh < 300 + 5*b_thresh) InterThresh = 300 + 5*b_thresh;
2246    
2247          if (sadInit) (*sadInit) ();          if (sadInit) (*sadInit) ();
2248    
2249          for (y = 1; y < pParam->mb_height-1; y += 2) {          for (y = 1; y < pParam->mb_height-1; y += 2) {
2250                  for (x = 1; x < pParam->mb_width-1; x += 2) {                  for (x = 1; x < pParam->mb_width-1; x += 2) {
2251                          int i;                          int i;
2252                            blocks += 4;
2253    
2254                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;                          if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;
2255                            else { //extrapolation of the vector found for last frame
2256                                    pMBs[x + y * pParam->mb_width].mvs[0].x =
2257                                            (pMBs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;
2258                                    pMBs[x + y * pParam->mb_width].mvs[0].y =
2259                                            (pMBs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount;
2260                            }
2261    
2262                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);                          MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);
2263    
# Line 2032  Line 2269 
2269                                                                          pParam->edged_width);                                                                          pParam->edged_width);
2270                                          if (dev + IntraThresh < pMB->sad16) {                                          if (dev + IntraThresh < pMB->sad16) {
2271                                                  pMB->mode = MODE_INTRA;                                                  pMB->mode = MODE_INTRA;
2272                                                  if (++intra > (pParam->mb_height-2)*(pParam->mb_width-2)/2) return I_VOP;                                                  if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP;
2273                                          }                                          }
2274                                  }                                  }
2275                                    if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0) s++;
2276    
2277                                  sSAD += pMB->sad16;                                  sSAD += pMB->sad16;
2278                          }                          }
2279                  }                  }
2280          }          }
2281          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);  
2282  //      if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP;          sSAD /= blocks;
2283            s = (10*s) / blocks;
2284    
2285            if (s > 5) sSAD += (s - 4) * (180 - 2*b_thresh); //static block - looks bad when in bframe...
2286    
2287          if (sSAD > InterThresh ) return P_VOP;          if (sSAD > InterThresh ) return P_VOP;
2288          emms();          emms();
2289          return B_VOP;          return B_VOP;
   
2290  }  }
2291    
2292    
# Line 2076  Line 2318 
2318          double meanx,meany;          double meanx,meany;
2319          int num,oldnum;          int num,oldnum;
2320    
2321          if (!MBmask) { fprintf(stderr,"Mem error\n"); return gmc;}          if (!MBmask) { fprintf(stderr,"Mem error\n");
2322                                   gmc.duv[0].x= gmc.duv[0].y =
2323                                                    gmc.duv[1].x= gmc.duv[1].y =
2324                                                    gmc.duv[2].x= gmc.duv[2].y = 0;
2325                                            return gmc; }
2326    
2327  // filter mask of all blocks  // filter mask of all blocks
2328    
2329          for (my = 1; my < MBh-1; my++)          for (my = 1; my < (uint32_t)MBh-1; my++)
2330          for (mx = 1; mx < MBw-1; mx++)          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2331          {          {
2332                  const int mbnum = mx + my * MBw;                  const int mbnum = mx + my * MBw;
2333                  const MACROBLOCK *pMB = &pMBs[mbnum];                  const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2097  Line 2343 
2343                          MBmask[mbnum]=1;                          MBmask[mbnum]=1;
2344          }          }
2345    
2346          for (my = 1; my < MBh-1; my++)          for (my = 1; my < (uint32_t)MBh-1; my++)
2347          for (mx = 1; mx < MBw-1; mx++)          for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2348          {          {
2349                  const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;                  const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;
2350    
# Line 2106  Line 2352 
2352                  if (!MBmask[mbnum])                  if (!MBmask[mbnum])
2353                          continue;                          continue;
2354    
2355                  if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= grad )                  if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= (uint32_t)grad )
2356                          MBmask[mbnum] = 0;                          MBmask[mbnum] = 0;
2357                  if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= grad )                  if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= (uint32_t)grad )
2358                          MBmask[mbnum] = 0;                          MBmask[mbnum] = 0;
2359    
2360          }          }
# Line 2119  Line 2365 
2365    
2366          a = b = c = n = 0;          a = b = c = n = 0;
2367          DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;          DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;
2368          for (my = 0; my < MBh; my++)          for (my = 0; my < (uint32_t)MBh; my++)
2369                  for (mx = 0; mx < MBw; mx++)                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2370                  {                  {
2371                          const int mbnum = mx + my * MBw;                          const int mbnum = mx + my * MBw;
2372                          const MACROBLOCK *pMB = &pMBs[mbnum];                          const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2157  Line 2403 
2403    
2404          meanx = meany = 0.;          meanx = meany = 0.;
2405          oldnum = 0;          oldnum = 0;
2406          for (my = 0; my < MBh; my++)          for (my = 0; my < (uint32_t)MBh; my++)
2407                  for (mx = 0; mx < MBw; mx++)                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2408                  {                  {
2409                          const int mbnum = mx + my * MBw;                          const int mbnum = mx + my * MBw;
2410                          const MACROBLOCK *pMB = &pMBs[mbnum];                          const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2186  Line 2432 
2432          fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);          fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);
2433  */  */
2434          num = 0;          num = 0;
2435          for (my = 0; my < MBh; my++)          for (my = 0; my < (uint32_t)MBh; my++)
2436                  for (mx = 0; mx < MBw; mx++)                  for (mx = 0; mx < (uint32_t)MBw; mx++)
2437                  {                  {
2438                          const int mbnum = mx + my * MBw;                          const int mbnum = mx + my * MBw;
2439                          const MACROBLOCK *pMB = &pMBs[mbnum];                          const MACROBLOCK *pMB = &pMBs[mbnum];
# Line 2225  Line 2471 
2471    
2472          return gmc;          return gmc;
2473  }  }
2474    
2475    // functions which perform BITS-based search/bitcount
2476    
2477    static int
2478    CountMBBitsInter(SearchData * const Data,
2479                                    const MACROBLOCK * const pMBs, const int x, const int y,
2480                                    const MBParam * const pParam,
2481                                    const uint32_t MotionFlags)
2482    {
2483            int i, iDirection;
2484            int32_t bsad[5];
2485    
2486            CheckCandidate = CheckCandidateBits16;
2487    
2488            if (Data->qpel) {
2489                    for(i = 0; i < 5; i++) {
2490                            Data->currentMV[i].x = Data->currentQMV[i].x/2;
2491                            Data->currentMV[i].y = Data->currentQMV[i].y/2;
2492                    }
2493                    Data->qpel_precision = 1;
2494                    CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);
2495    
2496                    //checking if this vector is perfect. if it is, we stop.
2497                    if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0)
2498                            return 0; //quick stop
2499    
2500                    if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { //we have to prepare for halfpixel-precision search
2501                            for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
2502                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2503                                                    pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);
2504                            Data->qpel_precision = 0;
2505                            if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1)
2506                                    CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2507                    }
2508    
2509            } else { // not qpel
2510    
2511                    CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2512                    //checking if this vector is perfect. if it is, we stop.
2513                    if (Data->temp[0] == 0 && Data->temp[1] == 0 && Data->temp[2] == 0 && Data->temp[3] == 0) {
2514                            return 0; //inter
2515                    }
2516            }
2517    
2518            if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
2519    
2520            if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);
2521    
2522            if (Data->qpel) {
2523                    if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { // there was halfpel-precision search
2524                            for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
2525                                    Data->currentQMV[i].x = 2 * Data->currentMV[i].x; // we have found a better match
2526                                    Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
2527                            }
2528    
2529                            // preparing for qpel-precision search
2530                            Data->qpel_precision = 1;
2531                            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 16,
2532                                            pParam->width, pParam->height, Data->iFcode, 1, 0);
2533                    }
2534                    if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);
2535            }
2536    
2537            if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { //let's check vector equal to prediction
2538                    VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
2539                    if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))
2540                            CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);
2541            }
2542            return Data->iMinSAD[0];
2543    }
2544    
2545    
2546    static int
2547    CountMBBitsInter4v(const SearchData * const Data,
2548                                            MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,
2549                                            const int x, const int y,
2550                                            const MBParam * const pParam, const uint32_t MotionFlags,
2551                                            const VECTOR * const backup)
2552    {
2553    
2554            int cbp = 0, bits = 0, t = 0, i, iDirection;
2555            SearchData Data2, *Data8 = &Data2;
2556            int sumx = 0, sumy = 0;
2557            int16_t *in = Data->dctSpace, *coeff = Data->dctSpace + 64;
2558    
2559            memcpy(Data8, Data, sizeof(SearchData));
2560            CheckCandidate = CheckCandidateBits8;
2561    
2562            for (i = 0; i < 4; i++) {
2563                    Data8->iMinSAD = Data->iMinSAD + i + 1;
2564                    Data8->currentMV = Data->currentMV + i + 1;
2565                    Data8->currentQMV = Data->currentQMV + i + 1;
2566                    Data8->Cur = Data->Cur + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2567                    Data8->Ref = Data->Ref + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2568                    Data8->RefH = Data->RefH + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2569                    Data8->RefV = Data->RefV + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2570                    Data8->RefHV = Data->RefHV + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2571    
2572                    if(Data->qpel) {
2573                            Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);
2574                            if (i != 0)     t = d_mv_bits(  Data8->currentQMV->x, Data8->currentQMV->y,
2575                                                                                    Data8->predMV, Data8->iFcode, 0, 0);
2576                    } else {
2577                            Data8->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, i);
2578                            if (i != 0)     t = d_mv_bits(  Data8->currentMV->x, Data8->currentMV->y,
2579                                                                                    Data8->predMV, Data8->iFcode, 0, 0);
2580                    }
2581    
2582                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2583                                            pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0);
2584    
2585                    *Data8->iMinSAD += t;
2586    
2587                    Data8->qpel_precision = Data8->qpel;
2588                    // checking the vector which has been found by SAD-based 8x8 search (if it's different than the one found so far)
2589                    if (Data8->qpel) {
2590                            if (!(Data8->currentQMV->x == backup[i+1].x && Data8->currentQMV->y == backup[i+1].y))
2591                                    CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);
2592                    } else {
2593                            if (!(Data8->currentMV->x == backup[i+1].x && Data8->currentMV->y == backup[i+1].y))
2594                                    CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);
2595                    }
2596    
2597                    if (Data8->qpel) {
2598                            if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { // halfpixel motion search follows
2599                                    int32_t s = *Data8->iMinSAD;
2600                                    Data8->currentMV->x = Data8->currentQMV->x/2;
2601                                    Data8->currentMV->y = Data8->currentQMV->y/2;
2602                                    Data8->qpel_precision = 0;
2603                                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2604                                                            pParam->width, pParam->height, Data8->iFcode - 1, 0, 0);
2605    
2606                                    if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)
2607                                            CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);
2608    
2609                                    if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)
2610                                            SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2611    
2612                                    if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8);
2613    
2614                                    if(s > *Data8->iMinSAD) { //we have found a better match
2615                                            Data8->currentQMV->x = 2*Data8->currentMV->x;
2616                                            Data8->currentQMV->y = 2*Data8->currentMV->y;
2617                                    }
2618    
2619                                    Data8->qpel_precision = 1;
2620                                    get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,
2621                                                            pParam->width, pParam->height, Data8->iFcode, 1, 0);
2622    
2623                            }
2624                            if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);
2625    
2626                    } else // not qpel
2627                            if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS) SubpelRefine(Data8); //halfpel mode, halfpel refinement
2628    
2629                    //checking vector equal to predicion
2630                    if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {
2631                            const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
2632                            if (!(Data8->predMV.x == v->x && Data8->predMV.y == v->y))
2633                                    CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);
2634                    }
2635    
2636                    bits += *Data8->iMinSAD;
2637                    if (bits >= Data->iMinSAD[0]) break; // no chances for INTER4V
2638    
2639                    // MB structures for INTER4V mode; we have to set them here, we don't have predictor anywhere else
2640                    if(Data->qpel) {
2641                            pMB->pmvs[i].x = Data8->currentQMV->x - Data8->predMV.x;
2642                            pMB->pmvs[i].y = Data8->currentQMV->y - Data8->predMV.y;
2643                            pMB->qmvs[i] = *Data8->currentQMV;
2644                            sumx += Data8->currentQMV->x/2;
2645                            sumy += Data8->currentQMV->y/2;
2646                    } else {
2647                            pMB->pmvs[i].x = Data8->currentMV->x - Data8->predMV.x;
2648                            pMB->pmvs[i].y = Data8->currentMV->y - Data8->predMV.y;
2649                            sumx += Data8->currentMV->x;
2650                            sumy += Data8->currentMV->y;
2651                    }
2652                    pMB->mvs[i] = *Data8->currentMV;
2653                    pMB->sad8[i] = 4 * *Data8->iMinSAD;
2654                    if (Data8->temp[0]) cbp |= 1 << (5 - i);
2655            }
2656    
2657            if (bits < *Data->iMinSAD) { // there is still a chance for inter4v mode. let's check chroma
2658                    const uint8_t * ptr;
2659                    sumx = (sumx >> 3) + roundtab_76[sumx & 0xf];
2660                    sumy = (sumy >> 3) + roundtab_76[sumy & 0xf];
2661    
2662                    //chroma U
2663                    ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCU, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2664                    transfer_8to16subro(in, Data->CurU, ptr, Data->iEdgedWidth/2);
2665                    fdct(in);
2666                    if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16);
2667                    else i = quant4_inter(coeff, in, Data->lambda16);
2668                    if (i > 0) {
2669                            bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
2670                            cbp |= 1 << (5 - 4);
2671                    }
2672    
2673                    if (bits < *Data->iMinSAD) { // still possible
2674                            //chroma V
2675                            ptr = interpolate8x8_switch2(Data->RefQ + 64, Data->RefCV, 0, 0, sumx, sumy, Data->iEdgedWidth/2, Data->rounding);
2676                            transfer_8to16subro(in, Data->CurV, ptr, Data->iEdgedWidth/2);
2677                            fdct(in);
2678                            if (Data->lambda8 == 0) i = quant_inter(coeff, in, Data->lambda16);
2679                            else i = quant4_inter(coeff, in, Data->lambda16);
2680                            if (i > 0) {
2681                                    bits += CodeCoeffInter_CalcBits(coeff, scan_tables[0]);
2682                                    cbp |= 1 << (5 - 5);
2683                            }
2684                            bits += xvid_cbpy_tab[15-(cbp>>2)].len;
2685                            bits += mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
2686                    }
2687            }
2688    
2689            return bits;
2690    }
2691    
2692    
2693    static int
2694    CountMBBitsIntra(const SearchData * const Data)
2695    {
2696            int bits = 1; //this one is ac/dc prediction flag. always 1.
2697            int cbp = 0, i, t, dc = 0, b_dc = 1024;
2698            const uint32_t iQuant = Data->lambda16;
2699            int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
2700    
2701            for(i = 0; i < 4; i++) {
2702                    uint32_t iDcScaler = get_dc_scaler(iQuant, 1);
2703    
2704                    int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2705                    transfer_8to16copy(in, Data->Cur + s, Data->iEdgedWidth);
2706                    fdct(in);
2707                    b_dc = dc;
2708                    dc = in[0];
2709                    in[0] -= b_dc;
2710                    if (Data->lambda8 == 0) quant_intra_c(coeff, in, iQuant, iDcScaler);
2711                    else quant4_intra_c(coeff, in, iQuant, iDcScaler);
2712    
2713                    b_dc = dc;
2714                    dc = coeff[0];
2715                    if (i != 0) coeff[0] -= b_dc;
2716    
2717                    bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcy_tab[coeff[0] + 255].len;;
2718                    Data->temp[i] = t;
2719                    if (t != 0)  cbp |= 1 << (5 - i);
2720                    if (bits >= Data->iMinSAD[0]) break;
2721            }
2722    
2723            if (bits < Data->iMinSAD[0]) { // INTRA still looks good, let's add chroma
2724                    uint32_t iDcScaler = get_dc_scaler(iQuant, 0);
2725                    //chroma U
2726                    transfer_8to16copy(in, Data->CurU, Data->iEdgedWidth/2);
2727                    fdct(in);
2728                    in[0] -= 1024;
2729                    if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);
2730                    else quant4_intra(coeff, in, iQuant, iDcScaler);
2731    
2732                    bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;
2733                    if (t != 0) cbp |= 1 << (5 - 4);
2734    
2735                    if (bits < Data->iMinSAD[0]) {
2736                            iDcScaler = get_dc_scaler(iQuant, 1);
2737                            //chroma V
2738                            transfer_8to16copy(in, Data->CurV, Data->iEdgedWidth/2);
2739                            fdct(in);
2740                            in[0] -= 1024;
2741                            if (Data->lambda8 == 0) quant_intra(coeff, in, iQuant, iDcScaler);
2742                            else quant4_intra(coeff, in, iQuant, iDcScaler);
2743    
2744                            bits += t = CodeCoeffIntra_CalcBits(coeff, scan_tables[0]) + dcc_tab[coeff[0] + 255].len;
2745                            if (t != 0) cbp |= 1 << (5 - 5);
2746    
2747                            bits += xvid_cbpy_tab[cbp>>2].len;
2748                            bits += mcbpc_inter_tab[(MODE_INTRA & 7) | ((cbp & 3) << 3)].len;
2749                    }
2750            }
2751            return bits;
2752    }

Legend:
Removed from v.1.44.2.51  
changed lines
  Added in v.1.58.2.7

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4