[svn] / branches / dev-api-4 / xvidcore / src / motion / motion_est.c Repository:
ViewVC logotype

Diff of /branches/dev-api-4/xvidcore/src/motion/motion_est.c

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

revision 1075, Thu Jun 26 11:50:37 2003 UTC revision 1129, Mon Aug 25 15:10:30 2003 UTC
# Line 21  Line 21 
21   *  along with this program ; if not, write to the Free Software   *  along with this program ; if not, write to the Free Software
22   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
23   *   *
24   * $Id: motion_est.c,v 1.58.2.19 2003-06-26 11:50:37 syskin Exp $   * $Id: motion_est.c,v 1.58.2.28 2003-08-25 15:10:13 syskin Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 40  Line 40 
40  #include "motion_est.h"  #include "motion_est.h"
41  #include "motion.h"  #include "motion.h"
42  #include "sad.h"  #include "sad.h"
43    #include "gmc.h"
44  #include "../utils/emms.h"  #include "../utils/emms.h"
45  #include "../dct/fdct.h"  #include "../dct/fdct.h"
46    
# Line 71  Line 72 
72  #define CHECK_CANDIDATE(X,Y,D) { \  #define CHECK_CANDIDATE(X,Y,D) { \
73  CheckCandidate((X),(Y), (D), &iDirection, data ); }  CheckCandidate((X),(Y), (D), &iDirection, data ); }
74    
75    
76  /*****************************************************************************  /*****************************************************************************
77   * Code   * Code
78   ****************************************************************************/   ****************************************************************************/
# Line 107  Line 109 
109  {  {
110          int sad;          int sad;
111          const uint32_t stride = data->iEdgedWidth/2;          const uint32_t stride = data->iEdgedWidth/2;
112          uint8_t * f_refu = data->RefQ,          uint8_t *f_refu, *f_refv, *b_refu, *b_refv;
113                  * f_refv = data->RefQ + 8,  
114                  * b_refu = data->RefQ + 16,          const INTERPOLATE8X8_PTR interpolate8x8_halfpel[] = {
115                  * b_refv = data->RefQ + 24;                  NULL,
116                    interpolate8x8_halfpel_v,
117                    interpolate8x8_halfpel_h,
118                    interpolate8x8_halfpel_hv
119            };
120    
121          int offset = (fx>>1) + (fy>>1)*stride;          int offset = (fx>>1) + (fy>>1)*stride;
122            int filter = ((fx & 1) << 1) | (fy & 1);
123    
124          switch (((fx & 1) << 1) | (fy & 1))     {          if (filter != 0) {
125                  case 0:                  f_refu = data->RefQ;
126                    f_refv = data->RefQ + 8;
127                    interpolate8x8_halfpel[filter](f_refu, data->RefP[4] + offset, stride, data->rounding);
128                    interpolate8x8_halfpel[filter](f_refv, data->RefP[5] + offset, stride, data->rounding);
129            } else {
130                          f_refu = (uint8_t*)data->RefP[4] + offset;                          f_refu = (uint8_t*)data->RefP[4] + offset;
131                          f_refv = (uint8_t*)data->RefP[5] + offset;                          f_refv = (uint8_t*)data->RefP[5] + offset;
                         break;  
                 case 1:  
                         interpolate8x8_halfpel_v(f_refu, data->RefP[4] + offset, stride, data->rounding);  
                         interpolate8x8_halfpel_v(f_refv, data->RefP[5] + offset, stride, data->rounding);  
                         break;  
                 case 2:  
                         interpolate8x8_halfpel_h(f_refu, data->RefP[4] + offset, stride, data->rounding);  
                         interpolate8x8_halfpel_h(f_refv, data->RefP[5] + offset, stride, data->rounding);  
                         break;  
                 default:  
                         interpolate8x8_halfpel_hv(f_refu, data->RefP[4] + offset, stride, data->rounding);  
                         interpolate8x8_halfpel_hv(f_refv, data->RefP[5] + offset, stride, data->rounding);  
                         break;  
132          }          }
133    
134          offset = (bx>>1) + (by>>1)*stride;          offset = (bx>>1) + (by>>1)*stride;
135          switch (((bx & 1) << 1) | (by & 1))     {          filter = ((bx & 1) << 1) | (by & 1);
136                  case 0:  
137            if (filter != 0) {
138                    b_refu = data->RefQ + 16;
139                    b_refv = data->RefQ + 24;
140                    interpolate8x8_halfpel[filter](b_refu, data->b_RefP[4] + offset, stride, data->rounding);
141                    interpolate8x8_halfpel[filter](b_refv, data->b_RefP[5] + offset, stride, data->rounding);
142            } else {
143                          b_refu = (uint8_t*)data->b_RefP[4] + offset;                          b_refu = (uint8_t*)data->b_RefP[4] + offset;
144                          b_refv = (uint8_t*)data->b_RefP[5] + offset;                          b_refv = (uint8_t*)data->b_RefP[5] + offset;
                         break;  
                 case 1:  
                         interpolate8x8_halfpel_v(b_refu, data->b_RefP[4] + offset, stride, data->rounding);  
                         interpolate8x8_halfpel_v(b_refv, data->b_RefP[5] + offset, stride, data->rounding);  
                         break;  
                 case 2:  
                         interpolate8x8_halfpel_h(b_refu, data->b_RefP[4] + offset, stride, data->rounding);  
                         interpolate8x8_halfpel_h(b_refv, data->b_RefP[5] + offset, stride, data->rounding);  
                         break;  
                 default:  
                         interpolate8x8_halfpel_hv(b_refu, data->b_RefP[4] + offset, stride, data->rounding);  
                         interpolate8x8_halfpel_hv(b_refv, data->b_RefP[5] + offset, stride, data->rounding);  
                         break;  
145          }          }
146    
147          sad = sad8bi(data->CurU, b_refu, f_refu, stride);          sad = sad8bi(data->CurU, b_refu, f_refu, stride);
# Line 299  Line 291 
291                  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);
292                  break;                  break;
293    
294    
295          default: /* pure halfpel position */          default: /* pure halfpel position */
296                  return (uint8_t *) ref1;                  return (uint8_t *) ref1;
297          }          }
# Line 334  Line 327 
327          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t * sad)>>10;
328          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10;          data->temp[1] += (data->lambda8 * t * (data->temp[1] + NEIGH_8X8_BIAS))>>10;
329    
330          if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],          if (data->chroma && sad < data->iMinSAD[0])
331                    sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
332                                                                             (yc >> 1) + roundtab_79[yc & 0x3], data);                                                                             (yc >> 1) + roundtab_79[yc & 0x3], data);
333    
334          if (sad < data->iMinSAD[0]) {          if (sad < data->iMinSAD[0]) {
# Line 444  Line 438 
438          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
439          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t * sad)>>10;
440    
441          if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],          if (data->chroma && sad < *data->iMinSAD)
442                    sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
443                                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);                                                                                  (yc >> 1) + roundtab_79[yc & 0x3], data);
444    
445          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
# Line 455  Line 450 
450  }  }
451    
452  static void  static void
453    CheckCandidate16I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
454    {
455            int sad;
456    //      int xc, yc;
457            const uint8_t * Reference;
458    //      VECTOR * current;
459    
460            if ( (x > data->max_dx) || ( x < data->min_dx)
461                    || (y > data->max_dy) || (y < data->min_dy) ) return;
462    
463            Reference = GetReference(x, y, data);
464    //      xc = x; yc = y;
465    
466            sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
467    //      sad += d_mv_bits(x, y, data->predMV, data->iFcode, 0, 0);
468    
469    /*      if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
470                                                                                    (yc >> 1) + roundtab_79[yc & 0x3], data);
471    */
472    
473            if (sad < data->iMinSAD[0]) {
474                    data->iMinSAD[0] = sad;
475                    data->currentMV[0].x = x; data->currentMV[0].y = y;
476                    *dir = Direction;
477            }
478    }
479    
480    static void
481  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate32I(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
482  {  {
483          /* maximum speed - for P/B/I decision */          /* maximum speed - for P/B/I decision */
# Line 463  Line 486 
486          if ( (x > data->max_dx) || (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
487                  || (y > data->max_dy) || (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
488    
489          sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*(data->iEdgedWidth),          sad = sad32v_c(data->Cur, data->RefP[0] + (x>>1) + (y>>1)*((int)data->iEdgedWidth),
490                                          data->iEdgedWidth, data->temp+1);                                          data->iEdgedWidth, data->temp+1);
491    
492          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
# Line 516  Line 539 
539          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
540          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t * sad)>>10;
541    
542          if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],          if (data->chroma && sad < *data->iMinSAD)
543                    sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],
544                                                                                  (ycf >> 1) + roundtab_79[ycf & 0x3],                                                                                  (ycf >> 1) + roundtab_79[ycf & 0x3],
545                                                                                  (xcb >> 1) + roundtab_79[xcb & 0x3],                                                                                  (xcb >> 1) + roundtab_79[xcb & 0x3],
546                                                                                  (ycb >> 1) + roundtab_79[ycb & 0x3], data);                                                                                  (ycb >> 1) + roundtab_79[ycb & 0x3], data);
# Line 576  Line 600 
600    
601          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;
602    
603          if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],          if (data->chroma && sad < *data->iMinSAD)
604                    sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
605                                                                                  (ycf >> 3) + roundtab_76[ycf & 0xf],                                                                                  (ycf >> 3) + roundtab_76[ycf & 0xf],
606                                                                                  (xcb >> 3) + roundtab_76[xcb & 0xf],                                                                                  (xcb >> 3) + roundtab_76[xcb & 0xf],
607                                                                                  (ycb >> 3) + roundtab_76[ycb & 0xf], data);                                                                                  (ycb >> 3) + roundtab_76[ycb & 0xf], data);
# Line 628  Line 653 
653          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
654          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;          sad += (data->lambda16 * d_mv_bits(x, y, zeroMV, 1, 0, 0) * sad)>>10;
655    
656          if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],          if (data->chroma && sad < *data->iMinSAD)
657                    sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
658                                                                                  (ycf >> 3) + roundtab_76[ycf & 0xf],                                                                                  (ycf >> 3) + roundtab_76[ycf & 0xf],
659                                                                                  (xcb >> 3) + roundtab_76[xcb & 0xf],                                                                                  (xcb >> 3) + roundtab_76[xcb & 0xf],
660                                                                                  (ycb >> 3) + roundtab_76[ycb & 0xf], data);                                                                                  (ycb >> 3) + roundtab_76[ycb & 0xf], data);
# Line 642  Line 668 
668    
669    
670  static void  static void
671  CheckCandidateBits16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateRD16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
672  {  {
673    
674          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
675          int32_t bits = 0;          int32_t rd = 0;
676          VECTOR * current;          VECTOR * current;
677          const uint8_t * ptr;          const uint8_t * ptr;
678          int i, cbp = 0, t, xc, yc;          int i, cbp = 0, t, xc, yc;
# Line 667  Line 693 
693          for(i = 0; i < 4; i++) {          for(i = 0; i < 4; i++) {
694                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);                  int s = 8*((i&1) + (i>>1)*data->iEdgedWidth);
695                  transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);                  transfer_8to16subro(in, data->Cur + s, ptr + s, data->iEdgedWidth);
696                  bits += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i);                  rd += data->temp[i] = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, i);
697          }          }
698    
699          bits += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          rd += t = BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
700    
701          if (data->temp[0] + t < data->iMinSAD[1]) {          if (data->temp[0] + t < data->iMinSAD[1]) {
702                  data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; }                  data->iMinSAD[1] = data->temp[0] + t; current[1].x = x; current[1].y = y; data->cbp[1] = (data->cbp[1]&~32) | (cbp&32); }
703          if (data->temp[1] < data->iMinSAD[2]) {          if (data->temp[1] < data->iMinSAD[2]) {
704                  data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; }                  data->iMinSAD[2] = data->temp[1]; current[2].x = x; current[2].y = y; data->cbp[1] = (data->cbp[1]&~16) | (cbp&16); }
705          if (data->temp[2] < data->iMinSAD[3]) {          if (data->temp[2] < data->iMinSAD[3]) {
706                  data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; }                  data->iMinSAD[3] = data->temp[2]; current[3].x = x; current[3].y = y; data->cbp[1] = (data->cbp[1]&~8) | (cbp&8); }
707          if (data->temp[3] < data->iMinSAD[4]) {          if (data->temp[3] < data->iMinSAD[4]) {
708                  data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; }                  data->iMinSAD[4] = data->temp[3]; current[4].x = x; current[4].y = y; data->cbp[1] = (data->cbp[1]&~4) | (cbp&4); }
709    
710          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;          rd += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
711    
712          if (bits >= data->iMinSAD[0]) return;          if (rd >= data->iMinSAD[0]) return;
713    
714          /* chroma */          /* chroma */
715          xc = (xc >> 1) + roundtab_79[xc & 0x3];          xc = (xc >> 1) + roundtab_79[xc & 0x3];
716          yc = (yc >> 1) + roundtab_79[yc & 0x3];          yc = (yc >> 1) + roundtab_79[yc & 0x3];
717    
718          /* chroma U */          /* chroma U */
719          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[4], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);          ptr = interpolate8x8_switch2(data->RefQ, data->RefP[4], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding);
720          transfer_8to16subro(in, ptr, data->CurU, data->iEdgedWidth/2);          transfer_8to16subro(in, data->CurU, ptr, data->iEdgedWidth/2);
721          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4);          rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 4);
722          if (bits >= data->iMinSAD[0]) return;          if (rd >= data->iMinSAD[0]) return;
723    
724          /* chroma V */          /* chroma V */
725          ptr = interpolate8x8_switch2(data->RefQ + 64, data->RefP[5], 0, 0, xc, yc,  data->iEdgedWidth/2, data->rounding);          ptr = interpolate8x8_switch2(data->RefQ, data->RefP[5], 0, 0, xc, yc, data->iEdgedWidth/2, data->rounding);
726          transfer_8to16subro(in, ptr, data->CurV, data->iEdgedWidth/2);          transfer_8to16subro(in, data->CurV, ptr, data->iEdgedWidth/2);
727          bits += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);          rd += Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
728    
729          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;          rd += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
730    
731          if (bits < data->iMinSAD[0]) {          if (rd < data->iMinSAD[0]) {
732                  data->iMinSAD[0] = bits;                  data->iMinSAD[0] = rd;
733                  current[0].x = x; current[0].y = y;                  current[0].x = x; current[0].y = y;
734                  *dir = Direction;                  *dir = Direction;
735                    *data->cbp = cbp;
736          }          }
737  }  }
738    
739  static void  static void
740  CheckCandidateBits8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateRD8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
741  {  {
742    
743          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;          int16_t *in = data->dctSpace, *coeff = data->dctSpace + 64;
744          int32_t bits;          int32_t rd;
745          VECTOR * current;          VECTOR * current;
746          const uint8_t * ptr;          const uint8_t * ptr;
747          int cbp = 0;          int cbp = 0;
# Line 731  Line 758 
758          }          }
759    
760          transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);          transfer_8to16subro(in, data->Cur, ptr, data->iEdgedWidth);
761          bits = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);          rd = Block_CalcBits(coeff, in, data->dctSpace + 128, data->iQuant, data->quant_type, &cbp, 5);
762          bits += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);          rd += BITS_MULT*d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
763    
764          if (bits < data->iMinSAD[0]) {          if (rd < data->iMinSAD[0]) {
765                  data->temp[0] = cbp;                  *data->cbp = cbp;
766                  data->iMinSAD[0] = bits;                  data->iMinSAD[0] = rd;
767                  current[0].x = x; current[0].y = y;                  current[0].x = x; current[0].y = y;
768                  *dir = Direction;                  *dir = Direction;
769          }          }
# Line 934  Line 961 
961  }  }
962    
963  static __inline void  static __inline void
964  SkipMacroblockP(MACROBLOCK *pMB, const int32_t sad)  ZeroMacroblockP(MACROBLOCK *pMB, const int32_t sad)
965  {  {
966          pMB->mode = MODE_NOT_CODED;          pMB->mode = MODE_INTER;
967          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV;          pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = zeroMV;
968          pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV;          pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = zeroMV;
969          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;          pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
# Line 952  Line 979 
979                          const uint32_t VopFlags,                          const uint32_t VopFlags,
980                          const uint32_t VolFlags,                          const uint32_t VolFlags,
981                          const IMAGE * const pCurrent,                          const IMAGE * const pCurrent,
982                          const IMAGE * const pRef)                          const IMAGE * const pRef,
983                            const IMAGE * const vGMC,
984                            const int coding_type)
985  {  {
986          int mode = MODE_INTER;          int mode = MODE_INTER;
987            int mcsel = 0;
988          int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);          int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
989          const uint32_t iQuant = pMB->quant;          const uint32_t iQuant = pMB->quant;
990    
991          const int skip_possible = (!(VolFlags & XVID_VOL_GMC)) && (pMB->dquant == 0);          const int skip_possible = (coding_type == P_VOP) && (pMB->dquant == 0);
992    
993          if (!(VopFlags & XVID_VOP_MODEDECISION_BITS)) { /* normal, fast, SAD-based mode decision */          pMB->mcsel = 0;
994    
995            if (!(VopFlags & XVID_VOP_MODEDECISION_RD)) { /* normal, fast, SAD-based mode decision */
996                  int sad;                  int sad;
997                  int InterBias = MV16_INTER_BIAS;                  int InterBias = MV16_INTER_BIAS;
998                  if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +                  if (inter4v == 0 || Data->iMinSAD[0] < Data->iMinSAD[1] + Data->iMinSAD[2] +
# Line 982  Line 1014 
1014                                          sad = 0;                                          sad = 0;
1015                                  }                                  }
1016    
1017                    /* mcsel */
1018                    if (coding_type == S_VOP) {
1019    
1020                            int32_t iSAD = sad16(Data->Cur,
1021                                    vGMC->y + 16*y*Data->iEdgedWidth + 16*x, Data->iEdgedWidth, 65536);
1022    
1023                            if (Data->chroma) {
1024                                    iSAD += sad8(Data->CurU, vGMC->u + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
1025                                    iSAD += sad8(Data->CurV, vGMC->v + 8*y*(Data->iEdgedWidth/2) + 8*x, Data->iEdgedWidth/2);
1026                            }
1027    
1028                            if (iSAD <= sad) {              /* mode decision GMC */
1029                                    mode = MODE_INTER;
1030                                    mcsel = 1;
1031                                    sad = iSAD;
1032                            }
1033    
1034                    }
1035    
1036                  /* intra decision */                  /* intra decision */
1037    
1038                  if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */                  if (iQuant > 8) InterBias += 100 * (iQuant - 8); /* to make high quants work */
# Line 990  Line 1041 
1041                  if (x != 0)                  if (x != 0)
1042                          if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;                          if ((pMB - 1)->mode == MODE_INTRA ) InterBias -= 80;
1043    
1044                  if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? */                  if (Data->chroma) InterBias += 50; /* dev8(chroma) ??? <-- yes, we need dev8 (no big difference though) */
1045                  if (Data->rrv) InterBias *= 4;                  if (Data->rrv) InterBias *= 4;
1046    
1047                  if (InterBias < pMB->sad16) {                  if (InterBias < sad) {
1048                          int32_t deviation;                          int32_t deviation;
1049                          if (!Data->rrv) deviation = dev16(Data->Cur, Data->iEdgedWidth);                          if (!Data->rrv)
1050                          else deviation = dev16(Data->Cur, Data->iEdgedWidth) +                                  deviation = dev16(Data->Cur, Data->iEdgedWidth);
1051                            else
1052                                    deviation = dev16(Data->Cur, Data->iEdgedWidth) + /* dev32() */
1053                                  dev16(Data->Cur+16, Data->iEdgedWidth) +                                  dev16(Data->Cur+16, Data->iEdgedWidth) +
1054                                  dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +                                  dev16(Data->Cur + 16*Data->iEdgedWidth, Data->iEdgedWidth) +
1055                                  dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);                                  dev16(Data->Cur+16+16*Data->iEdgedWidth, Data->iEdgedWidth);
# Line 1004  Line 1057 
1057                          if (deviation < (sad - InterBias)) mode = MODE_INTRA;                          if (deviation < (sad - InterBias)) mode = MODE_INTRA;
1058                  }                  }
1059    
1060          } else { /* BITS */                  pMB->cbp = 63;
1061                    pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = sad;
1062    
1063            } else { /* Rate-Distortion */
1064    
1065                  int bits, intra, i;                  int min_rd, intra_rd, i, cbp, c[2] = {0, 0};
1066                  VECTOR backup[5], *v;                  VECTOR backup[5], *v;
1067                  Data->iQuant = iQuant;                  Data->iQuant = iQuant;
1068                    Data->cbp = c;
1069    
1070                  v = Data->qpel ? Data->currentQMV : Data->currentMV;                  v = Data->qpel ? Data->currentQMV : Data->currentMV;
1071                  for (i = 0; i < 5; i++) {                  for (i = 0; i < 5; i++) {
# Line 1016  Line 1073 
1073                          backup[i] = v[i];                          backup[i] = v[i];
1074                  }                  }
1075    
1076                  bits = CountMBBitsInter(Data, pMBs, x, y, pParam, MotionFlags);                  min_rd = findRDinter(Data, pMBs, x, y, pParam, MotionFlags);
1077                  if (bits == 0)                  cbp = *Data->cbp;
1078                          mode = MODE_INTER; /* quick stop */  
1079                  else {                  if (coding_type == S_VOP) {
1080                          if (inter4v) {                          int gmc_rd;
1081                                  int bits_inter4v = CountMBBitsInter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);                          *Data->iMinSAD = min_rd += BITS_MULT*1; /* mcsel */
1082                                  if (bits_inter4v < bits) { Data->iMinSAD[0] = bits = bits_inter4v; mode = MODE_INTER4V; }                          gmc_rd = findRDgmc(Data, vGMC, x, y);
1083                            if (gmc_rd < min_rd) {
1084                                    mcsel = 1;
1085                                    *Data->iMinSAD = min_rd = gmc_rd;
1086                                    mode = MODE_INTER;
1087                                    cbp = *Data->cbp;
1088                            }
1089                          }                          }
1090    
1091                          intra = CountMBBitsIntra(Data);                  if (inter4v) {
1092                            int v4_rd;
1093                            v4_rd = findRDinter4v(Data, pMB, pMBs, x, y, pParam, MotionFlags, backup);
1094                            if (v4_rd < min_rd) {
1095                                    Data->iMinSAD[0] = min_rd = v4_rd;
1096                                    mode = MODE_INTER4V;
1097                                    cbp = *Data->cbp;
1098                            }
1099                    }
1100    
1101                          if (intra < bits) { *Data->iMinSAD = bits = intra; mode = MODE_INTRA; }                  intra_rd = findRDintra(Data);
1102                    if (intra_rd < min_rd) {
1103                            *Data->iMinSAD = min_rd = intra_rd;
1104                            mode = MODE_INTRA;
1105                  }                  }
1106    
1107                    pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0;
1108                    pMB->cbp = cbp;
1109          }          }
1110    
1111          if (Data->rrv) {          if (Data->rrv) {
# Line 1036  Line 1113 
1113                          Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);                          Data->currentMV[0].y = RRV_MV_SCALEDOWN(Data->currentMV[0].y);
1114          }          }
1115    
1116          if (mode == MODE_INTER) {          if (mode == MODE_INTER && mcsel == 0) {
1117                  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];
                 pMB->sad16 = pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = Data->iMinSAD[0];  
1118    
1119                  if(Data->qpel) {                  if(Data->qpel) {
1120                          pMB->qmvs[0] = pMB->qmvs[1]                          pMB->qmvs[0] = pMB->qmvs[1]
# Line 1050  Line 1126 
1126                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;                          pMB->pmvs[0].y = Data->currentMV[0].y - Data->predMV.y;
1127                  }                  }
1128    
1129          } else if (mode == MODE_INTER4V)          } else if (mode == MODE_INTER ) { // but mcsel == 1
1130                  pMB->sad16 = Data->iMinSAD[0];  
1131                    pMB->mcsel = 1;
1132                    if (Data->qpel) {
1133                            pMB->qmvs[0] = pMB->qmvs[1] = pMB->qmvs[2] = pMB->qmvs[3] = pMB->amv;
1134                            pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = pMB->amv.x/2;
1135                            pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = pMB->amv.y/2;
1136                    } else
1137                            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = pMB->amv;
1138    
1139            } else
1140                    if (mode == MODE_INTER4V) ; /* anything here? */
1141          else /* INTRA, NOT_CODED */          else /* INTRA, NOT_CODED */
1142                  SkipMacroblockP(pMB, 0);                  ZeroMacroblockP(pMB, 0);
1143    
1144          pMB->mode = mode;          pMB->mode = mode;
1145  }  }
# Line 1065  Line 1151 
1151                                   const IMAGE * const pRefH,                                   const IMAGE * const pRefH,
1152                                   const IMAGE * const pRefV,                                   const IMAGE * const pRefV,
1153                                   const IMAGE * const pRefHV,                                   const IMAGE * const pRefHV,
1154                                    const IMAGE * const pGMC,
1155                                   const uint32_t iLimit)                                   const uint32_t iLimit)
1156  {  {
1157          MACROBLOCK *const pMBs = current->mbs;          MACROBLOCK *const pMBs = current->mbs;
# Line 1078  Line 1165 
1165    
1166          uint32_t x, y;          uint32_t x, y;
1167          uint32_t iIntra = 0;          uint32_t iIntra = 0;
1168          int32_t quant = current->quant, sad00;          int32_t sad00;
1169          int skip_thresh = \          int skip_thresh = INITIAL_SKIP_THRESH * \
                 INITIAL_SKIP_THRESH * \  
1170                  (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \                  (current->vop_flags & XVID_VOP_REDUCED ? 4:1) * \
1171                  (current->vop_flags & XVID_VOP_MODEDECISION_BITS ? 2:1);                  (current->vop_flags & XVID_VOP_MODEDECISION_RD ? 2:1);
1172    
1173          /* some pre-initialized thingies for SearchP */          /* some pre-initialized thingies for SearchP */
1174          int32_t temp[8];          int32_t temp[8];
# Line 1100  Line 1186 
1186          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
1187          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
1188          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);          Data.qpel = (current->vol_flags & XVID_VOL_QUARTERPEL ? 1:0);
1189          Data.chroma = MotionFlags & XVID_ME_CHROMA16;          Data.chroma = MotionFlags & XVID_ME_CHROMA_PVOP;
1190          Data.rrv = (current->vop_flags & XVID_VOP_REDUCED ? 1:0);          Data.rrv = (current->vop_flags & XVID_VOP_REDUCED) ? 1:0;
1191          Data.dctSpace = dct_space;          Data.dctSpace = dct_space;
1192          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);          Data.quant_type = !(pParam->vol_flags & XVID_VOL_MPEGQUANT);
1193    
# Line 1138  Line 1224 
1224    
1225                          sad00 = pMB->sad16;                          sad00 = pMB->sad16;
1226    
                         if (pMB->dquant != 0) {  
                                 quant += DQtab[pMB->dquant];  
                                 if (quant > 31) quant = 31;  
                                 else if (quant < 1) quant = 1;  
                         }  
                         pMB->quant = quant;  
   
1227                          /* initial skip decision */                          /* initial skip decision */
1228                          /* no early skip for GMC (global vector = skip vector is unknown!)  */                          /* no early skip for GMC (global vector = skip vector is unknown!)  */
1229                          if (!(current->vol_flags & XVID_VOL_GMC))       { /* no fast SKIP for S(GMC)-VOPs */                          if (current->coding_type != S_VOP)      { /* no fast SKIP for S(GMC)-VOPs */
1230                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)                                  if (pMB->dquant == 0 && sad00 < pMB->quant * skip_thresh)
1231                                          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)) {
1232                                                  SkipMacroblockP(pMB, sad00);                                                  ZeroMacroblockP(pMB, sad00);
1233                                                    pMB->mode = MODE_NOT_CODED;
1234                                                  continue;                                                  continue;
1235                                          }                                          }
1236                          }                          }
1237    
1238                            if ((current->vop_flags & XVID_VOP_CARTOON) &&
1239                                    (sad00 < pMB->quant * 4 * skip_thresh)) { /* favorize (0,0) vector for cartoons */
1240                                    ZeroMacroblockP(pMB, sad00);
1241                                    continue;
1242                            }
1243    
1244                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,                          SearchP(pRef, pRefH->y, pRefV->y, pRefHV->y, pCurrent, x,
1245                                          y, MotionFlags, current->vop_flags, current->vol_flags,                                          y, MotionFlags, current->vop_flags, current->vol_flags,
1246                                          &Data, pParam, pMBs, reference->mbs, pMB);                                          &Data, pParam, pMBs, reference->mbs, pMB);
1247    
1248                          ModeDecision(&Data, pMB, pMBs, x, y, pParam,                          ModeDecision(&Data, pMB, pMBs, x, y, pParam,
1249                                                   MotionFlags, current->vop_flags, current->vol_flags,                                                   MotionFlags, current->vop_flags, current->vol_flags,
1250                                                   pCurrent, pRef);                                                   pCurrent, pRef, pGMC, current->coding_type);
1251    
1252                          if (pMB->mode == MODE_INTRA)                          if (pMB->mode == MODE_INTRA)
1253                                  if (++iIntra > iLimit) return 1;                                  if (++iIntra > iLimit) return 1;
1254                  }                  }
1255          }          }
1256    
         if (current->vol_flags & XVID_VOL_GMC ) /* GMC only for S(GMC)-VOPs */  
         {  
                 current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);  
         }  
1257          return 0;          return 0;
1258  }  }
1259    
# Line 1256  Line 1338 
1338          VECTOR pmv[7];          VECTOR pmv[7];
1339          int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);          int inter4v = (VopFlags & XVID_VOP_INTER4V) && (pMB->dquant == 0);
1340    
1341          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, 4,
1342                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
1343    
1344          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);          get_pmvdata2(pMBs, pParam->mb_width, 0, x, y, 0, pmv, Data->temp);
1345    
# Line 1290  Line 1372 
1372          Data->iMinSAD[3] = pMB->sad8[2];          Data->iMinSAD[3] = pMB->sad8[2];
1373          Data->iMinSAD[4] = pMB->sad8[3];          Data->iMinSAD[4] = pMB->sad8[3];
1374    
1375          if ((!(VopFlags & XVID_VOP_MODEDECISION_BITS)) && (x | y)) {          if ((!(VopFlags & XVID_VOP_MODEDECISION_RD)) && (x | y)) {
1376                  threshA = Data->temp[0]; /* that's where we keep this SAD atm */                  threshA = Data->temp[0]; /* that's where we keep this SAD atm */
1377                  if (threshA < 512) threshA = 512;                  if (threshA < 512) threshA = 512;
1378                  else if (threshA > 1024) threshA = 1024;                  else if (threshA > 1024) threshA = 1024;
# Line 1370  Line 1452 
1452          }          }
1453    
1454          if (Data->qpel) {          if (Data->qpel) {
1455                  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, 4,
1456                                  pParam->width, pParam->height, Data->iFcode, 1, 0);                                  pParam->width, pParam->height, Data->iFcode, 2, 0);
1457                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
1458                  if (MotionFlags & XVID_ME_QUARTERPELREFINE16)                  if (MotionFlags & XVID_ME_QUARTERPELREFINE16)
1459                          SubpelRefine(Data);                          SubpelRefine(Data);
# Line 1389  Line 1471 
1471                  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);
1472                  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);
1473    
1474                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_BITS))) {                  if ((Data->chroma) && (!(VopFlags & XVID_VOP_MODEDECISION_RD))) {
1475                          /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */                          /* chroma is only used for comparsion to INTER. if the comparsion will be done in BITS domain, it will not be used */
1476                          int sumx = 0, sumy = 0;                          int sumx = 0, sumy = 0;
1477    
# Line 1449  Line 1531 
1531                  Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));                  Data->Cur = OldData->Cur + i * ((block&1) + Data->iEdgedWidth*(block>>1));
1532                  Data->qpel_precision = 0;                  Data->qpel_precision = 0;
1533    
1534                  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, 3,
1535                                          pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                          pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
1536    
1537                  if (!Data->rrv) CheckCandidate = CheckCandidate8;                  if (!Data->rrv) CheckCandidate = CheckCandidate8;
1538                  else CheckCandidate = CheckCandidate16no4v;                  else CheckCandidate = CheckCandidate16no4v;
1539    
1540                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_BITS))) {                  if (MotionFlags & XVID_ME_EXTSEARCH8 && (!(MotionFlags & XVID_ME_EXTSEARCH_RD))) {
1541                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */                          int32_t temp_sad = *(Data->iMinSAD); /* store current MinSAD */
1542    
1543                          MainSearchFunc *MainSearchPtr;                          MainSearchFunc *MainSearchPtr;
# Line 1484  Line 1566 
1566    
1567                  if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {                  if (Data->qpel && MotionFlags & XVID_ME_QUARTERPELREFINE8) {
1568                                  Data->qpel_precision = 1;                                  Data->qpel_precision = 1;
1569                                  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, 3,
1570                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 2, 0);
1571                                  SubpelRefine(Data);                                  SubpelRefine(Data);
1572                  }                  }
1573          }          }
# Line 1589  Line 1671 
1671    
1672          Data->predMV = *predMV;          Data->predMV = *predMV;
1673    
1674          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, 4,
1675                                  pParam->width, pParam->height, iFcode - Data->qpel, 0, 0);                                  pParam->width, pParam->height, iFcode - Data->qpel, 1, 0);
1676    
1677          pmv[0] = Data->predMV;          pmv[0] = Data->predMV;
1678          if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }          if (Data->qpel) { pmv[0].x /= 2; pmv[0].y /= 2; }
# Line 1618  Line 1700 
1700                  Data->currentQMV->x = 2*Data->currentMV->x;                  Data->currentQMV->x = 2*Data->currentMV->x;
1701                  Data->currentQMV->y = 2*Data->currentMV->y;                  Data->currentQMV->y = 2*Data->currentMV->y;
1702                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
1703                  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, 4,
1704                                          pParam->width, pParam->height, iFcode, 1, 0);                                          pParam->width, pParam->height, iFcode, 2, 0);
1705                  SubpelRefine(Data);                  SubpelRefine(Data);
1706          }          }
1707    
# Line 1682  Line 1764 
1764                                          b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,                                          b_Ref->u + (y*8 + b_dy/2) * stride + x*8 + b_dx/2,
1765                                          stride);                                          stride);
1766    
1767          if (sum >= 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */          if (sum >= MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) return; /* no skip */
1768    
1769          sum += sad8bi(pCur->v + 8*x + 8 * y * stride,          sum += sad8bi(pCur->v + 8*x + 8 * y * stride,
1770                                          f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,                                          f_Ref->v + (y*8 + dy/2) * stride + x*8 + dx/2,
1771                                          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,
1772                                          stride);                                          stride);
1773    
1774          if (sum < 2 * MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {          if (sum < MAX_CHROMA_SAD_FOR_SKIP * pMB->quant) {
1775                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */
1776                  for (k = 0; k < 4; k++) {                  for (k = 0; k < 4; k++) {
1777                          pMB->qmvs[k] = pMB->mvs[k];                          pMB->qmvs[k] = pMB->mvs[k];
# Line 1885  Line 1967 
1967          fData->bpredMV = bData.predMV = *b_predMV;          fData->bpredMV = bData.predMV = *b_predMV;
1968          fData->currentMV[0] = fData->currentMV[2];          fData->currentMV[0] = fData->currentMV[2];
1969    
1970          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode - fData->qpel, 0, 0);          get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode - fData->qpel, 1, 0);
1971          get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode - fData->qpel, 0, 0);          get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode - fData->qpel, 1, 0);
1972    
1973          if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;          if (fData->currentMV[0].x > fData->max_dx) fData->currentMV[0].x = fData->max_dx;
1974          if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx;          if (fData->currentMV[0].x < fData->min_dx) fData->currentMV[0].x = fData->min_dx;
# Line 1926  Line 2008 
2008                  if (*fData->iMinSAD > *best_sad + 500) return;                  if (*fData->iMinSAD > *best_sad + 500) return;
2009                  CheckCandidate = CheckCandidateInt;                  CheckCandidate = CheckCandidateInt;
2010                  fData->qpel_precision = bData.qpel_precision = 1;                  fData->qpel_precision = bData.qpel_precision = 1;
2011                  get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 16, pParam->width, pParam->height, fcode, 1, 0);                  get_range(&fData->min_dx, &fData->max_dx, &fData->min_dy, &fData->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0);
2012                  get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 16, pParam->width, pParam->height, bcode, 1, 0);                  get_range(&bData.min_dx, &bData.max_dx, &bData.min_dy, &bData.max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0);
2013                  fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;                  fData->currentQMV[2].x = fData->currentQMV[0].x = 2 * fData->currentMV[0].x;
2014                  fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;                  fData->currentQMV[2].y = fData->currentQMV[0].y = 2 * fData->currentMV[0].y;
2015                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;                  fData->currentQMV[1].x = 2 * fData->currentMV[1].x;
# Line 2002  Line 2084 
2084          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
2085          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
2086          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant];
2087          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL;          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
2088          Data.rounding = 0;          Data.rounding = 0;
2089          Data.chroma = frame->motion_flags & XVID_ME_CHROMA8;          Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
2090          Data.temp = temp;          Data.temp = temp;
2091    
2092          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 2128  Line 2210 
2210                          Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */                          Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */
2211                          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 */
2212    
2213          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, 4,
2214          pParam->width, pParam->height, Data->iFcode - quarterpel, 0, 0);                          pParam->width, pParam->height, Data->iFcode - quarterpel, 1, 0);
2215    
2216          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;          Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
2217          Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;          Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;
# Line 2179  Line 2261 
2261          MACROBLOCK * const pMBs = Current->mbs;          MACROBLOCK * const pMBs = Current->mbs;
2262          const IMAGE * const pCurrent = &Current->image;          const IMAGE * const pCurrent = &Current->image;
2263          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + b_thresh;          int IntraThresh = INTRA_THRESH, InterThresh = INTER_THRESH + b_thresh;
2264          int s = 0, blocks = 0;          int blocks = 0;
2265          int complexity = 0;          int complexity = 0;
2266    
2267          int32_t iMinSAD[5], temp[5];          int32_t iMinSAD[5], temp[5];
# Line 2192  Line 2274 
2274          Data.temp = temp;          Data.temp = temp;
2275          CheckCandidate = CheckCandidate32I;          CheckCandidate = CheckCandidate32I;
2276    
   
2277          if (intraCount != 0) {          if (intraCount != 0) {
2278                  if (intraCount < 10) // we're right after an I frame                  if (intraCount < 10) // we're right after an I frame
2279                          IntraThresh += 15* (intraCount - 10) * (intraCount - 10);                          IntraThresh += 15* (intraCount - 10) * (intraCount - 10);
# Line 2227  Line 2308 
2308                                  dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16,                                  dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16,
2309                                                                  pParam->edged_width);                                                                  pParam->edged_width);
2310    
2311                                  complexity += dev;                                  complexity += MAX(dev, 300);
2312                                  if (dev + IntraThresh < pMB->sad16) {                                  if (dev + IntraThresh < pMB->sad16) {
2313                                          pMB->mode = MODE_INTRA;                                          pMB->mode = MODE_INTRA;
2314                                          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;
# Line 2237  Line 2318 
2318                                          if (dev > 500 && pMB->sad16 < 1000)                                          if (dev > 500 && pMB->sad16 < 1000)
2319                                                  sSAD += 1000;                                                  sSAD += 1000;
2320    
2321                                  sSAD += pMB->sad16;                                  sSAD += (dev < 3000) ? pMB->sad16 : pMB->sad16/2; /* blocks with big contrast differences usually have large SAD - while they look very good in b-frames */
2322                          }                          }
2323                  }                  }
2324          }          }
# Line 2245  Line 2326 
2326    
2327          sSAD /= complexity + 4*blocks;          sSAD /= complexity + 4*blocks;
2328    
2329          if (intraCount > 12 && sSAD > INTRA_THRESH2 ) return I_VOP;          if (intraCount > 80 && sSAD > INTRA_THRESH2 ) return I_VOP;
2330          if (sSAD > InterThresh ) return P_VOP;          if (sSAD > InterThresh ) return P_VOP;
2331          emms();          emms();
2332          return B_VOP;          return B_VOP;
2333  }  }
2334    
 static WARPPOINTS  
 GlobalMotionEst(const MACROBLOCK * const pMBs,  
                                 const MBParam * const pParam,  
                                 const FRAMEINFO * const current,  
                                 const FRAMEINFO * const reference,  
                                 const IMAGE * const pRefH,  
                                 const IMAGE * const pRefV,  
                                 const IMAGE * const pRefHV      )  
 {  
   
         const int deltax=8;             /* upper bound for difference between a MV and it's neighbour MVs */  
         const int deltay=8;  
         const int grad=512;             /* lower bound for deviation in MB */  
   
         WARPPOINTS gmc;  
   
         uint32_t mx, my;  
   
         int MBh = pParam->mb_height;  
         int MBw = pParam->mb_width;  
   
         int *MBmask= calloc(MBh*MBw,sizeof(int));  
         double DtimesF[4] = { 0.,0., 0., 0. };  
         double sol[4] = { 0., 0., 0., 0. };  
         double a,b,c,n,denom;  
         double meanx,meany;  
         int num,oldnum;  
   
         if (!MBmask) {  fprintf(stderr,"Mem error\n");  
                                         gmc.duv[0].x= gmc.duv[0].y =  
                                                 gmc.duv[1].x= gmc.duv[1].y =  
                                                 gmc.duv[2].x= gmc.duv[2].y = 0;  
                                         return gmc; }  
   
         /* filter mask of all blocks */  
   
         for (my = 1; my < (uint32_t)MBh-1; my++)  
         for (mx = 1; mx < (uint32_t)MBw-1; mx++)  
         {  
                 const int mbnum = mx + my * MBw;  
                 const MACROBLOCK *pMB = &pMBs[mbnum];  
                 const VECTOR mv = pMB->mvs[0];  
   
                 if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)  
                         continue;  
   
                 if ( ( (abs(mv.x -   (pMB-1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB-1)->mvs[0].y) < deltay) )  
                 &&   ( (abs(mv.x -   (pMB+1)->mvs[0].x) < deltax) && (abs(mv.y -   (pMB+1)->mvs[0].y) < deltay) )  
                 &&   ( (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay) )  
                 &&   ( (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) )  
                         MBmask[mbnum]=1;  
         }  
   
         for (my = 1; my < (uint32_t)MBh-1; my++)  
         for (mx = 1; mx < (uint32_t)MBw-1; mx++)  
         {  
                 const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;  
   
                 const int mbnum = mx + my * MBw;  
                 if (!MBmask[mbnum])  
                         continue;  
   
                 if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= (uint32_t)grad )  
                         MBmask[mbnum] = 0;  
                 if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= (uint32_t)grad )  
                         MBmask[mbnum] = 0;  
   
         }  
   
         emms();  
   
         do {            /* until convergence */  
   
         a = b = c = n = 0;  
         DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;  
         for (my = 0; my < (uint32_t)MBh; my++)  
                 for (mx = 0; mx < (uint32_t)MBw; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         const MACROBLOCK *pMB = &pMBs[mbnum];  
                         const VECTOR mv = pMB->mvs[0];  
   
                         if (!MBmask[mbnum])  
                                 continue;  
   
                         n++;  
                         a += 16*mx+8;  
                         b += 16*my+8;  
                         c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8);  
   
                         DtimesF[0] += (double)mv.x;  
                         DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8);  
                         DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8);  
                         DtimesF[3] += (double)mv.y;  
                 }  
   
         denom = a*a+b*b-c*n;  
   
 /* Solve the system:    sol = (D'*E*D)^{-1} D'*E*F   */  
 /* D'*E*F has been calculated in the same loop as matrix */  
   
         sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2];  
         sol[1] =  a*DtimesF[0] - n*DtimesF[1]                + b*DtimesF[3];  
         sol[2] =  b*DtimesF[0]                - n*DtimesF[2] - a*DtimesF[3];  
         sol[3] =                 b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3];  
   
         sol[0] /= denom;  
         sol[1] /= denom;  
         sol[2] /= denom;  
         sol[3] /= denom;  
   
         meanx = meany = 0.;  
         oldnum = 0;  
         for (my = 0; my < (uint32_t)MBh; my++)  
                 for (mx = 0; mx < (uint32_t)MBw; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         const MACROBLOCK *pMB = &pMBs[mbnum];  
                         const VECTOR mv = pMB->mvs[0];  
   
                         if (!MBmask[mbnum])  
                                 continue;  
   
                         oldnum++;  
                         meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x );  
                         meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y );  
                 }  
   
         if (4*meanx > oldnum)   /* better fit than 0.25 is useless */  
                 meanx /= oldnum;  
         else  
                 meanx = 0.25;  
   
         if (4*meany > oldnum)  
                 meany /= oldnum;  
         else  
                 meany = 0.25;  
   
 /*      fprintf(stderr,"sol = (%8.5f, %8.5f, %8.5f, %8.5f)\n",sol[0],sol[1],sol[2],sol[3]);  
         fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);  
 */  
         num = 0;  
         for (my = 0; my < (uint32_t)MBh; my++)  
                 for (mx = 0; mx < (uint32_t)MBw; mx++)  
                 {  
                         const int mbnum = mx + my * MBw;  
                         const MACROBLOCK *pMB = &pMBs[mbnum];  
                         const VECTOR mv = pMB->mvs[0];  
   
                         if (!MBmask[mbnum])  
                                 continue;  
   
                         if  ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx )  
                                 || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) )  
                                 MBmask[mbnum]=0;  
                         else  
                                 num++;  
                 }  
   
         } while ( (oldnum != num) && (num>=4) );  
   
         if (num < 4)  
         {  
                 gmc.duv[0].x= gmc.duv[0].y= gmc.duv[1].x= gmc.duv[1].y= gmc.duv[2].x= gmc.duv[2].y=0;  
         } else {  
   
                 gmc.duv[0].x=(int)(sol[0]+0.5);  
                 gmc.duv[0].y=(int)(sol[3]+0.5);  
   
                 gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5);  
                 gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5);  
   
                 gmc.duv[2].x=0;  
                 gmc.duv[2].y=0;  
         }  
 /*      fprintf(stderr,"wp1 = ( %4d, %4d)  wp2 = ( %4d, %4d) \n", gmc.duv[0].x, gmc.duv[0].y, gmc.duv[1].x, gmc.duv[1].y); */  
   
         free(MBmask);  
   
         return gmc;  
 }  
2335    
2336  /* functions which perform BITS-based search/bitcount */  /* functions which perform BITS-based search/bitcount */
2337    
2338  static int  static int
2339  CountMBBitsInter(SearchData * const Data,  findRDinter(SearchData * const Data,
2340                                  const MACROBLOCK * const pMBs, const int x, const int y,                                  const MACROBLOCK * const pMBs, const int x, const int y,
2341                                  const MBParam * const pParam,                                  const MBParam * const pParam,
2342                                  const uint32_t MotionFlags)                                  const uint32_t MotionFlags)
# Line 2444  Line 2344 
2344          int i, iDirection;          int i, iDirection;
2345          int32_t bsad[5];          int32_t bsad[5];
2346    
2347          CheckCandidate = CheckCandidateBits16;          CheckCandidate = CheckCandidateRD16;
2348    
2349          if (Data->qpel) {          if (Data->qpel) {
2350                  for(i = 0; i < 5; i++) {                  for(i = 0; i < 5; i++) {
# Line 2452  Line 2352 
2352                          Data->currentMV[i].y = Data->currentQMV[i].y/2;                          Data->currentMV[i].y = Data->currentQMV[i].y/2;
2353                  }                  }
2354                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
2355                  CheckCandidateBits16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);                  CheckCandidateRD16(Data->currentQMV[0].x, Data->currentQMV[0].y, 255, &iDirection, Data);
2356    
2357                  if (MotionFlags & (XVID_ME_HALFPELREFINE16_BITS | XVID_ME_EXTSEARCH_BITS)) { /* we have to prepare for halfpixel-precision search */                  if (MotionFlags & (XVID_ME_HALFPELREFINE16_RD | XVID_ME_EXTSEARCH_RD)) { /* we have to prepare for halfpixel-precision search */
2358                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];                          for(i = 0; i < 5; i++) bsad[i] = Data->iMinSAD[i];
2359                          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, 4,
2360                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 0, Data->rrv);                                                  pParam->width, pParam->height, Data->iFcode - Data->qpel, 1, Data->rrv);
2361                          Data->qpel_precision = 0;                          Data->qpel_precision = 0;
2362                          if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1)                          if (Data->currentQMV->x & 1 || Data->currentQMV->y & 1)
2363                                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);                                  CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2364                  }                  }
2365    
2366          } else { /* not qpel */          } else { /* not qpel */
2367    
2368                  CheckCandidateBits16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);                  CheckCandidateRD16(Data->currentMV[0].x, Data->currentMV[0].y, 255, &iDirection, Data);
2369          }          }
2370    
2371          if (MotionFlags&XVID_ME_EXTSEARCH_BITS) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);          if (MotionFlags&XVID_ME_EXTSEARCH_RD) SquareSearch(Data->currentMV->x, Data->currentMV->y, Data, iDirection);
2372    
2373          if (MotionFlags&XVID_ME_HALFPELREFINE16_BITS) SubpelRefine(Data);          if (MotionFlags&XVID_ME_HALFPELREFINE16_RD) SubpelRefine(Data);
2374    
2375          if (Data->qpel) {          if (Data->qpel) {
2376                  if (MotionFlags&(XVID_ME_EXTSEARCH_BITS | XVID_ME_HALFPELREFINE16_BITS)) { /* there was halfpel-precision search */                  if (MotionFlags&(XVID_ME_EXTSEARCH_RD | XVID_ME_HALFPELREFINE16_RD)) { /* there was halfpel-precision search */
2377                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {                          for(i = 0; i < 5; i++) if (bsad[i] > Data->iMinSAD[i]) {
2378                                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */                                  Data->currentQMV[i].x = 2 * Data->currentMV[i].x; /* we have found a better match */
2379                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;                                  Data->currentQMV[i].y = 2 * Data->currentMV[i].y;
# Line 2481  Line 2381 
2381    
2382                          /* preparing for qpel-precision search */                          /* preparing for qpel-precision search */
2383                          Data->qpel_precision = 1;                          Data->qpel_precision = 1;
2384                          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, 4,
2385                                          pParam->width, pParam->height, Data->iFcode, 1, 0);                                          pParam->width, pParam->height, Data->iFcode, 2, 0);
2386                  }                  }
2387                  if (MotionFlags&XVID_ME_QUARTERPELREFINE16_BITS) SubpelRefine(Data);                  if (MotionFlags&XVID_ME_QUARTERPELREFINE16_RD) SubpelRefine(Data);
2388          }          }
2389    
2390          if (MotionFlags&XVID_ME_CHECKPREDICTION_BITS) { /* let's check vector equal to prediction */          if (MotionFlags&XVID_ME_CHECKPREDICTION_RD) { /* let's check vector equal to prediction */
2391                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;                  VECTOR * v = Data->qpel ? Data->currentQMV : Data->currentMV;
2392                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))                  if (!(Data->predMV.x == v->x && Data->predMV.y == v->y))
2393                          CheckCandidateBits16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);                          CheckCandidateRD16(Data->predMV.x, Data->predMV.y, 255, &iDirection, Data);
2394          }          }
2395          return Data->iMinSAD[0];          return Data->iMinSAD[0];
2396  }  }
2397    
2398  static int  static int
2399  CountMBBitsInter4v(const SearchData * const Data,  findRDinter4v(const SearchData * const Data,
2400                                          MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,                                          MACROBLOCK * const pMB, const MACROBLOCK * const pMBs,
2401                                          const int x, const int y,                                          const int x, const int y,
2402                                          const MBParam * const pParam, const uint32_t MotionFlags,                                          const MBParam * const pParam, const uint32_t MotionFlags,
# Line 2510  Line 2410 
2410          uint8_t * ptr;          uint8_t * ptr;
2411    
2412          memcpy(Data8, Data, sizeof(SearchData));          memcpy(Data8, Data, sizeof(SearchData));
2413          CheckCandidate = CheckCandidateBits8;          CheckCandidate = CheckCandidateRD8;
2414    
2415          for (i = 0; i < 4; i++) { /* for all luma blocks */          for (i = 0; i < 4; i++) { /* for all luma blocks */
2416    
# Line 2522  Line 2422 
2422                  Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[2] = Data->RefP[2] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2423                  Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[1] = Data->RefP[1] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2424                  Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);                  Data8->RefP[3] = Data->RefP[3] + 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2425                    *Data8->cbp = (Data->cbp[1] & (1<<(5-i))) ? 1:0; // copy corresponding cbp bit
2426    
2427                  if(Data->qpel) {                  if(Data->qpel) {
2428                          Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);                          Data8->predMV = get_qpmv2(pMBs, pParam->mb_width, 0, x, y, i);
# Line 2533  Line 2434 
2434                                                                                  Data8->predMV, Data8->iFcode, 0, 0);                                                                                  Data8->predMV, Data8->iFcode, 0, 0);
2435                  }                  }
2436    
2437                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3,
2438                                          pParam->width, pParam->height, Data8->iFcode, Data8->qpel, 0);                                          pParam->width, pParam->height, Data8->iFcode, Data8->qpel+1, 0);
2439    
2440                  *Data8->iMinSAD += BITS_MULT*t;                  *Data8->iMinSAD += BITS_MULT*t;
2441    
# Line 2543  Line 2444 
2444                  {                  {
2445                          VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;                          VECTOR *v = Data8->qpel ? Data8->currentQMV : Data8->currentMV;
2446                          if (!MVequal (*v, backup[i+1]) )                          if (!MVequal (*v, backup[i+1]) )
2447                                  CheckCandidateBits8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);                                  CheckCandidateRD8(backup[i+1].x, backup[i+1].y, 255, &iDirection, Data8);
2448                  }                  }
2449    
2450                  if (Data8->qpel) {                  if (Data8->qpel) {
2451                          if (MotionFlags&XVID_ME_HALFPELREFINE8_BITS || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_BITS)) { /* halfpixel motion search follows */                          if (MotionFlags&XVID_ME_HALFPELREFINE8_RD || (MotionFlags&XVID_ME_EXTSEARCH8 && MotionFlags&XVID_ME_EXTSEARCH_RD)) { /* halfpixel motion search follows */
2452                                  int32_t s = *Data8->iMinSAD;                                  int32_t s = *Data8->iMinSAD;
2453                                  Data8->currentMV->x = Data8->currentQMV->x/2;                                  Data8->currentMV->x = Data8->currentQMV->x/2;
2454                                  Data8->currentMV->y = Data8->currentQMV->y/2;                                  Data8->currentMV->y = Data8->currentQMV->y/2;
2455                                  Data8->qpel_precision = 0;                                  Data8->qpel_precision = 0;
2456                                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,                                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3,
2457                                                          pParam->width, pParam->height, Data8->iFcode - 1, 0, 0);                                                          pParam->width, pParam->height, Data8->iFcode - 1, 1, 0);
2458    
2459                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)                                  if (Data8->currentQMV->x & 1 || Data8->currentQMV->y & 1)
2460                                          CheckCandidateBits8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);                                          CheckCandidateRD8(Data8->currentMV->x, Data8->currentMV->y, 255, &iDirection, Data8);
2461    
2462                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS)                                  if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD)
2463                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);                                          SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2464    
2465                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS)                                  if (MotionFlags & XVID_ME_HALFPELREFINE8_RD)
2466                                          SubpelRefine(Data8);                                          SubpelRefine(Data8);
2467    
2468                                  if(s > *Data8->iMinSAD) { /* we have found a better match */                                  if(s > *Data8->iMinSAD) { /* we have found a better match */
# Line 2570  Line 2471 
2471                                  }                                  }
2472    
2473                                  Data8->qpel_precision = 1;                                  Data8->qpel_precision = 1;
2474                                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 8,                                  get_range(&Data8->min_dx, &Data8->max_dx, &Data8->min_dy, &Data8->max_dy, 2*x + (i&1), 2*y + (i>>1), 3,
2475                                                          pParam->width, pParam->height, Data8->iFcode, 1, 0);                                                          pParam->width, pParam->height, Data8->iFcode, 2, 0);
2476    
2477                          }                          }
2478                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_BITS) SubpelRefine(Data8);                          if (MotionFlags & XVID_ME_QUARTERPELREFINE8_RD) SubpelRefine(Data8);
2479    
2480                  } else { /* not qpel */                  } else { /* not qpel */
2481    
2482                          if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_BITS) /* extsearch */                          if (MotionFlags & XVID_ME_EXTSEARCH8 && MotionFlags & XVID_ME_EXTSEARCH_RD) /* extsearch */
2483                                  SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);                                  SquareSearch(Data8->currentMV->x, Data8->currentMV->x, Data8, 255);
2484    
2485                          if (MotionFlags & XVID_ME_HALFPELREFINE8_BITS)                          if (MotionFlags & XVID_ME_HALFPELREFINE8_RD)
2486                                  SubpelRefine(Data8); /* halfpel refinement */                                  SubpelRefine(Data8); /* halfpel refinement */
2487                  }                  }
2488    
2489                  /* checking vector equal to predicion */                  /* checking vector equal to predicion */
2490                  if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_BITS) {                  if (i != 0 && MotionFlags & XVID_ME_CHECKPREDICTION_RD) {
2491                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;                          const VECTOR * v = Data->qpel ? Data8->currentQMV : Data8->currentMV;
2492                          if (!MVequal(*v, Data8->predMV))                          if (!MVequal(*v, Data8->predMV))
2493                                  CheckCandidateBits8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);                                  CheckCandidateRD8(Data8->predMV.x, Data8->predMV.y, 255, &iDirection, Data8);
2494                  }                  }
2495    
2496                  bits += *Data8->iMinSAD;                  bits += *Data8->iMinSAD;
# Line 2610  Line 2511 
2511                  }                  }
2512                  pMB->mvs[i] = *Data8->currentMV;                  pMB->mvs[i] = *Data8->currentMV;
2513                  pMB->sad8[i] = 4 * *Data8->iMinSAD;                  pMB->sad8[i] = 4 * *Data8->iMinSAD;
2514                  if (Data8->temp[0]) cbp |= 1 << (5 - i);                  if (Data8->cbp[0]) cbp |= 1 << (5 - i);
2515    
2516          } /* /for all luma blocks */          } /* end - for all luma blocks */
2517    
2518          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;          bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
2519    
# Line 2634  Line 2535 
2535    
2536          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;          bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER4V & 7) | ((cbp & 3) << 3)].len;
2537    
2538            *Data->cbp = cbp;
2539          return bits;          return bits;
2540  }  }
2541    
2542  static int  static int
2543  CountMBBitsIntra(const SearchData * const Data)  findRDintra(const SearchData * const Data)
2544  {  {
2545          int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */          int bits = BITS_MULT*1; /* this one is ac/dc prediction flag bit */
2546          int cbp = 0, i, dc = 0;          int cbp = 0, i, dc = 0;
# Line 2668  Line 2570 
2570    
2571          return bits;          return bits;
2572  }  }
2573    
2574    static int
2575    findRDgmc(const SearchData * const Data, const IMAGE * const vGMC, const int x, const int y)
2576    {
2577            int bits = BITS_MULT*1; /* this one is mcsel */
2578            int cbp = 0, i;
2579            int16_t *in = Data->dctSpace, * coeff = Data->dctSpace + 64;
2580    
2581            for(i = 0; i < 4; i++) {
2582                    int s = 8*((i&1) + (i>>1)*Data->iEdgedWidth);
2583                    transfer_8to16subro(in, Data->Cur + s, vGMC->y + s + 16*(x+y*Data->iEdgedWidth), Data->iEdgedWidth);
2584                    bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, i);
2585                    if (bits >= Data->iMinSAD[0]) return bits;
2586            }
2587    
2588            bits += BITS_MULT*xvid_cbpy_tab[15-(cbp>>2)].len;
2589    
2590            /*chroma U */
2591            transfer_8to16subro(in, Data->CurU, vGMC->u + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2);
2592            bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 4);
2593    
2594            if (bits >= Data->iMinSAD[0]) return bits;
2595    
2596            /* chroma V */
2597            transfer_8to16subro(in, Data->CurV , vGMC->v + 8*(x+y*(Data->iEdgedWidth/2)), Data->iEdgedWidth/2);
2598            bits += Block_CalcBits(coeff, in, Data->dctSpace + 128, Data->iQuant, Data->quant_type, &cbp, 5);
2599    
2600            bits += BITS_MULT*mcbpc_inter_tab[(MODE_INTER & 7) | ((cbp & 3) << 3)].len;
2601    
2602            *Data->cbp = cbp;
2603    
2604            return bits;
2605    }
2606    
2607    
2608    
2609    
2610    static __inline void
2611    GMEanalyzeMB (  const uint8_t * const pCur,
2612                                    const uint8_t * const pRef,
2613                                    const uint8_t * const pRefH,
2614                                    const uint8_t * const pRefV,
2615                                    const uint8_t * const pRefHV,
2616                                    const int x,
2617                                    const int y,
2618                                    const MBParam * const pParam,
2619                                    MACROBLOCK * const pMBs,
2620                                    SearchData * const Data)
2621    {
2622    
2623            int i=0;
2624            MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
2625    
2626            Data->iMinSAD[0] = MV_MAX_ERROR;
2627    
2628            Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0);
2629    
2630            get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
2631                                    pParam->width, pParam->height, 16, 1, 0);
2632    
2633            Data->Cur = pCur + 16*(x + y * pParam->edged_width);
2634            Data->RefP[0] = pRef + 16*(x + y * pParam->edged_width);
2635            Data->RefP[1] = pRefV + 16*(x + y * pParam->edged_width);
2636            Data->RefP[2] = pRefH + 16*(x + y * pParam->edged_width);
2637            Data->RefP[3] = pRefHV + 16*(x + y * pParam->edged_width);
2638    
2639            Data->currentMV[0].x = Data->currentMV[0].y = 0;
2640            CheckCandidate16I(0, 0, 255, &i, Data);
2641    
2642            if ( (Data->predMV.x !=0) || (Data->predMV.y != 0) )
2643                    CheckCandidate16I(Data->predMV.x, Data->predMV.y, 255, &i, Data);
2644    
2645            AdvDiamondSearch(Data->currentMV[0].x, Data->currentMV[0].y, Data, 255);
2646    
2647            SubpelRefine(Data);
2648    
2649    
2650            /* for QPel halfpel positions are worse than in halfpel mode :( */
2651    /*      if (Data->qpel) {
2652                    Data->currentQMV->x = 2*Data->currentMV->x;
2653                    Data->currentQMV->y = 2*Data->currentMV->y;
2654                    Data->qpel_precision = 1;
2655                    get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
2656                                            pParam->width, pParam->height, iFcode, 2, 0);
2657                    SubpelRefine(Data);
2658            }
2659    */
2660    
2661            pMB->mvs[0] = pMB->mvs[1] = pMB->mvs[2] = pMB->mvs[3] = Data->currentMV[0];
2662            pMB->sad16 = Data->iMinSAD[0];
2663            pMB->mode = MODE_INTER;
2664            pMB->sad16 += 10*d_mv_bits(pMB->mvs[0].x, pMB->mvs[0].y, Data->predMV, Data->iFcode, 0, 0);
2665            return;
2666    }
2667    
2668    void
2669    GMEanalysis(const MBParam * const pParam,
2670                            const FRAMEINFO * const current,
2671                            const FRAMEINFO * const reference,
2672                            const IMAGE * const pRefH,
2673                            const IMAGE * const pRefV,
2674                            const IMAGE * const pRefHV)
2675    {
2676            uint32_t x, y;
2677            MACROBLOCK * const pMBs = current->mbs;
2678            const IMAGE * const pCurrent = &current->image;
2679            const IMAGE * const pReference = &reference->image;
2680    
2681            int32_t iMinSAD[5], temp[5];
2682            VECTOR currentMV[5];
2683            SearchData Data;
2684            memset(&Data, 0, sizeof(SearchData));
2685    
2686            Data.iEdgedWidth = pParam->edged_width;
2687            Data.rounding = pParam->m_rounding_type;
2688    
2689            Data.currentMV = &currentMV[0];
2690            Data.iMinSAD = &iMinSAD[0];
2691            Data.iFcode = current->fcode;
2692            Data.temp = temp;
2693    
2694            CheckCandidate = CheckCandidate16I;
2695    
2696            if (sadInit) (*sadInit) ();
2697    
2698            for (y = 0; y < pParam->mb_height; y ++) {
2699                    for (x = 0; x < pParam->mb_width; x ++) {
2700    
2701                            GMEanalyzeMB(pCurrent->y, pReference->y, pRefH->y, pRefV->y, pRefHV->y, x, y, pParam, pMBs, &Data);
2702                    }
2703            }
2704            return;
2705    }
2706    
2707    
2708    WARPPOINTS
2709    GlobalMotionEst(MACROBLOCK * const pMBs,
2710                                    const MBParam * const pParam,
2711                                    const FRAMEINFO * const current,
2712                                    const FRAMEINFO * const reference,
2713                                    const IMAGE * const pRefH,
2714                                    const IMAGE * const pRefV,
2715                                    const IMAGE * const pRefHV)
2716    {
2717    
2718            const int deltax=8;             // upper bound for difference between a MV and it's neighbour MVs
2719            const int deltay=8;
2720            const unsigned int gradx=512;           // lower bound for gradient in MB (ignore "flat" blocks)
2721            const unsigned int grady=512;
2722    
2723            double sol[4] = { 0., 0., 0., 0. };
2724    
2725            WARPPOINTS gmc;
2726    
2727            uint32_t mx, my;
2728    
2729            int MBh = pParam->mb_height;
2730            int MBw = pParam->mb_width;
2731            const int minblocks = 9; //MBh*MBw/32+3;                /* just some reasonable number 3% + 3 */
2732            const int maxblocks = MBh*MBw/4;                /* just some reasonable number 3% + 3 */
2733    
2734            int num=0;
2735            int oldnum;
2736    
2737            gmc.duv[0].x = gmc.duv[0].y = gmc.duv[1].x = gmc.duv[1].y = gmc.duv[2].x = gmc.duv[2].y = 0;
2738    
2739            GMEanalysis(pParam,current, reference, pRefH, pRefV, pRefHV);
2740    
2741            /* block based ME isn't done, yet, so do a quick presearch */
2742    
2743    // filter mask of all blocks
2744    
2745            for (my = 0; my < (uint32_t)MBh; my++)
2746            for (mx = 0; mx < (uint32_t)MBw; mx++)
2747            {
2748                    const int mbnum = mx + my * MBw;
2749                            pMBs[mbnum].mcsel = 0;
2750            }
2751    
2752    
2753            for (my = 1; my < (uint32_t)MBh-1; my++) /* ignore boundary blocks */
2754            for (mx = 1; mx < (uint32_t)MBw-1; mx++) /* theirs MVs are often wrong */
2755            {
2756                    const int mbnum = mx + my * MBw;
2757                    MACROBLOCK *const pMB = &pMBs[mbnum];
2758                    const VECTOR mv = pMB->mvs[0];
2759    
2760                    /* don't use object boundaries */
2761                    if   ( (abs(mv.x -   (pMB-1)->mvs[0].x) < deltax)
2762                            && (abs(mv.y -   (pMB-1)->mvs[0].y) < deltay)
2763                            && (abs(mv.x -   (pMB+1)->mvs[0].x) < deltax)
2764                            && (abs(mv.y -   (pMB+1)->mvs[0].y) < deltay)
2765                            && (abs(mv.x - (pMB-MBw)->mvs[0].x) < deltax)
2766                            && (abs(mv.y - (pMB-MBw)->mvs[0].y) < deltay)
2767                            && (abs(mv.x - (pMB+MBw)->mvs[0].x) < deltax)
2768                            && (abs(mv.y - (pMB+MBw)->mvs[0].y) < deltay) )
2769                    {       const int iEdgedWidth = pParam->edged_width;
2770                            const uint8_t *const pCur = current->image.y + 16*(my*iEdgedWidth + mx);
2771                            if ( (sad16 ( pCur, pCur+1 , iEdgedWidth, 65536) >= gradx )
2772                             &&  (sad16 ( pCur, pCur+iEdgedWidth, iEdgedWidth, 65536) >= grady ) )
2773                             {      pMB->mcsel = 1;
2774                                    num++;
2775                             }
2776    
2777                    /* only use "structured" blocks */
2778                    }
2779            }
2780            emms();
2781    
2782            /*      further filtering would be possible, but during iteration, remaining
2783                    outliers usually are removed, too */
2784    
2785            if (num>= minblocks)
2786            do {            /* until convergence */
2787                    double DtimesF[4];
2788                    double a,b,c,n,invdenom;
2789                    double meanx,meany;
2790    
2791                    a = b = c = n = 0;
2792                    DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;
2793                    for (my = 1; my < (uint32_t)MBh-1; my++)
2794                    for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2795                    {
2796                            const int mbnum = mx + my * MBw;
2797                            const VECTOR mv = pMBs[mbnum].mvs[0];
2798    
2799                            if (!pMBs[mbnum].mcsel)
2800                                    continue;
2801    
2802                            n++;
2803                            a += 16*mx+8;
2804                            b += 16*my+8;
2805                            c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8);
2806    
2807                            DtimesF[0] += (double)mv.x;
2808                            DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8);
2809                            DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8);
2810                            DtimesF[3] += (double)mv.y;
2811                    }
2812    
2813            invdenom = a*a+b*b-c*n;
2814    
2815    /* Solve the system:    sol = (D'*E*D)^{-1} D'*E*F   */
2816    /* D'*E*F has been calculated in the same loop as matrix */
2817    
2818            sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2];
2819            sol[1] =  a*DtimesF[0] - n*DtimesF[1]                           + b*DtimesF[3];
2820            sol[2] =  b*DtimesF[0]                          - n*DtimesF[2] - a*DtimesF[3];
2821            sol[3] =                                 b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3];
2822    
2823            sol[0] /= invdenom;
2824            sol[1] /= invdenom;
2825            sol[2] /= invdenom;
2826            sol[3] /= invdenom;
2827    
2828            meanx = meany = 0.;
2829            oldnum = 0;
2830            for (my = 1; my < (uint32_t)MBh-1; my++)
2831                    for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2832                    {
2833                            const int mbnum = mx + my * MBw;
2834                            const VECTOR mv = pMBs[mbnum].mvs[0];
2835    
2836                            if (!pMBs[mbnum].mcsel)
2837                                    continue;
2838    
2839                            oldnum++;
2840                            meanx += fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - (double)mv.x );
2841                            meany += fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - (double)mv.y );
2842                    }
2843    
2844            if (4*meanx > oldnum)   /* better fit than 0.25 (=1/4pel) is useless */
2845                    meanx /= oldnum;
2846            else
2847                    meanx = 0.25;
2848    
2849            if (4*meany > oldnum)
2850                    meany /= oldnum;
2851            else
2852                    meany = 0.25;
2853    
2854            num = 0;
2855            for (my = 0; my < (uint32_t)MBh; my++)
2856                    for (mx = 0; mx < (uint32_t)MBw; mx++)
2857                    {
2858                            const int mbnum = mx + my * MBw;
2859                            const VECTOR mv = pMBs[mbnum].mvs[0];
2860    
2861                            if (!pMBs[mbnum].mcsel)
2862                                    continue;
2863    
2864                            if  ( ( fabs(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - (double)mv.x ) > meanx )
2865                                    || ( fabs(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - (double)mv.y ) > meany ) )
2866                                    pMBs[mbnum].mcsel=0;
2867                            else
2868                                    num++;
2869                    }
2870    
2871            } while ( (oldnum != num) && (num>= minblocks) );
2872    
2873            if (num < minblocks)
2874            {
2875                    const int iEdgedWidth = pParam->edged_width;
2876                    num = 0;
2877    
2878    /*              fprintf(stderr,"Warning! Unreliable GME (%d/%d blocks), falling back to translation.\n",num,MBh*MBw);
2879    */
2880                    gmc.duv[0].x= gmc.duv[0].y= gmc.duv[1].x= gmc.duv[1].y= gmc.duv[2].x= gmc.duv[2].y=0;
2881    
2882                    if (!(current->motion_flags & XVID_ME_GME_REFINE))
2883                            return gmc;
2884    
2885                    for (my = 1; my < (uint32_t)MBh-1; my++) /* ignore boundary blocks */
2886                    for (mx = 1; mx < (uint32_t)MBw-1; mx++) /* theirs MVs are often wrong */
2887                    {
2888                            const int mbnum = mx + my * MBw;
2889                            MACROBLOCK *const pMB = &pMBs[mbnum];
2890                            const uint8_t *const pCur = current->image.y + 16*(my*iEdgedWidth + mx);
2891                            if ( (sad16 ( pCur, pCur+1 , iEdgedWidth, 65536) >= gradx )
2892                             &&  (sad16 ( pCur, pCur+iEdgedWidth, iEdgedWidth, 65536) >= grady ) )
2893                             {      pMB->mcsel = 1;
2894                                    gmc.duv[0].x += pMB->mvs[0].x;
2895                                    gmc.duv[0].y += pMB->mvs[0].y;
2896                                    num++;
2897                             }
2898                    }
2899    
2900                    if (gmc.duv[0].x)
2901                            gmc.duv[0].x /= num;
2902                    if (gmc.duv[0].y)
2903                            gmc.duv[0].y /= num;
2904            } else {
2905    
2906                    gmc.duv[0].x=(int)(sol[0]+0.5);
2907                    gmc.duv[0].y=(int)(sol[3]+0.5);
2908    
2909                    gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5);
2910                    gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5);
2911    
2912                    gmc.duv[2].x=-gmc.duv[1].y;             /* two warp points only */
2913                    gmc.duv[2].y=gmc.duv[1].x;
2914            }
2915            if (num>maxblocks)
2916            {       for (my = 1; my < (uint32_t)MBh-1; my++)
2917                    for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2918                    {
2919                            const int mbnum = mx + my * MBw;
2920                            if (pMBs[mbnum-1].mcsel)
2921                                    pMBs[mbnum].mcsel=0;
2922                            else
2923                                    if (pMBs[mbnum-MBw].mcsel)
2924                                            pMBs[mbnum].mcsel=0;
2925                    }
2926            }
2927            return gmc;
2928    }
2929    
2930    int
2931    GlobalMotionEstRefine(
2932                                    WARPPOINTS *const startwp,
2933                                    MACROBLOCK * const pMBs,
2934                                    const MBParam * const pParam,
2935                                    const FRAMEINFO * const current,
2936                                    const FRAMEINFO * const reference,
2937                                    const IMAGE * const pCurr,
2938                                    const IMAGE * const pRef,
2939                                    const IMAGE * const pRefH,
2940                                    const IMAGE * const pRefV,
2941                                    const IMAGE * const pRefHV)
2942    {
2943            uint8_t* GMCblock = (uint8_t*)malloc(16*pParam->edged_width);
2944            WARPPOINTS bestwp=*startwp;
2945            WARPPOINTS centerwp,currwp;
2946            int gmcminSAD=0;
2947            int gmcSAD=0;
2948            int direction;
2949    //      int mx,my;
2950    
2951    /* use many blocks... */
2952    /*              for (my = 0; my < (uint32_t)pParam->mb_height; my++)
2953                    for (mx = 0; mx < (uint32_t)pParam->mb_width; mx++)
2954                    {
2955                            const int mbnum = mx + my * pParam->mb_width;
2956                            pMBs[mbnum].mcsel=1;
2957                    }
2958    */
2959    
2960    /* or rather don't use too many blocks... */
2961    /*
2962                    for (my = 1; my < (uint32_t)MBh-1; my++)
2963                    for (mx = 1; mx < (uint32_t)MBw-1; mx++)
2964                    {
2965                            const int mbnum = mx + my * MBw;
2966                            if (MBmask[mbnum-1])
2967                                    MBmask[mbnum-1]=0;
2968                            else
2969                                    if (MBmask[mbnum-MBw])
2970                                            MBmask[mbnum-1]=0;
2971    
2972                    }
2973    */
2974                    gmcminSAD = globalSAD(&bestwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
2975    
2976                    if ( (reference->coding_type == S_VOP)
2977                            && ( (reference->warp.duv[1].x != bestwp.duv[1].x)
2978                              || (reference->warp.duv[1].y != bestwp.duv[1].y)
2979                              || (reference->warp.duv[0].x != bestwp.duv[0].x)
2980                              || (reference->warp.duv[0].y != bestwp.duv[0].y)
2981                              || (reference->warp.duv[2].x != bestwp.duv[2].x)
2982                              || (reference->warp.duv[2].y != bestwp.duv[2].y) ) )
2983                    {
2984                            gmcSAD = globalSAD(&reference->warp, pParam, pMBs,
2985                                                                    current, pRef, pCurr, GMCblock);
2986    
2987                            if (gmcSAD < gmcminSAD)
2988                            {       bestwp = reference->warp;
2989                                    gmcminSAD = gmcSAD;
2990                            }
2991                    }
2992    
2993            do {
2994                    direction = 0;
2995                    centerwp = bestwp;
2996    
2997                    currwp = centerwp;
2998    
2999                    currwp.duv[0].x--;
3000                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3001                    if (gmcSAD < gmcminSAD)
3002                    {       bestwp = currwp;
3003                            gmcminSAD = gmcSAD;
3004                            direction = 1;
3005                    }
3006                    else
3007                    {
3008                    currwp = centerwp; currwp.duv[0].x++;
3009                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3010                    if (gmcSAD < gmcminSAD)
3011                    {       bestwp = currwp;
3012                            gmcminSAD = gmcSAD;
3013                            direction = 2;
3014                    }
3015                    }
3016                    if (direction) continue;
3017    
3018                    currwp = centerwp; currwp.duv[0].y--;
3019                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3020                    if (gmcSAD < gmcminSAD)
3021                    {       bestwp = currwp;
3022                            gmcminSAD = gmcSAD;
3023                            direction = 4;
3024                    }
3025                    else
3026                    {
3027                    currwp = centerwp; currwp.duv[0].y++;
3028                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3029                    if (gmcSAD < gmcminSAD)
3030                    {       bestwp = currwp;
3031                            gmcminSAD = gmcSAD;
3032                            direction = 8;
3033                    }
3034                    }
3035                    if (direction) continue;
3036    
3037                    currwp = centerwp; currwp.duv[1].x++;
3038                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3039                    if (gmcSAD < gmcminSAD)
3040                    {       bestwp = currwp;
3041                            gmcminSAD = gmcSAD;
3042                            direction = 32;
3043                    }
3044                    currwp.duv[2].y++;
3045                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3046                    if (gmcSAD < gmcminSAD)
3047                    {       bestwp = currwp;
3048                            gmcminSAD = gmcSAD;
3049                            direction = 1024;
3050                    }
3051    
3052                    currwp = centerwp; currwp.duv[1].x--;
3053                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3054                    if (gmcSAD < gmcminSAD)
3055                    {       bestwp = currwp;
3056                            gmcminSAD = gmcSAD;
3057                            direction = 16;
3058                    }
3059                    else
3060                    {
3061                    currwp = centerwp; currwp.duv[1].x++;
3062                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3063                    if (gmcSAD < gmcminSAD)
3064                    {       bestwp = currwp;
3065                            gmcminSAD = gmcSAD;
3066                            direction = 32;
3067                    }
3068                    }
3069                    if (direction) continue;
3070    
3071    
3072                    currwp = centerwp; currwp.duv[1].y--;
3073                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3074                    if (gmcSAD < gmcminSAD)
3075                    {       bestwp = currwp;
3076                            gmcminSAD = gmcSAD;
3077                            direction = 64;
3078                    }
3079                    else
3080                    {
3081                    currwp = centerwp; currwp.duv[1].y++;
3082                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3083                    if (gmcSAD < gmcminSAD)
3084                    {       bestwp = currwp;
3085                            gmcminSAD = gmcSAD;
3086                            direction = 128;
3087                    }
3088                    }
3089                    if (direction) continue;
3090    
3091                    currwp = centerwp; currwp.duv[2].x--;
3092                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3093                    if (gmcSAD < gmcminSAD)
3094                    {       bestwp = currwp;
3095                            gmcminSAD = gmcSAD;
3096                            direction = 256;
3097                    }
3098                    else
3099                    {
3100                    currwp = centerwp; currwp.duv[2].x++;
3101                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3102                    if (gmcSAD < gmcminSAD)
3103                    {       bestwp = currwp;
3104                            gmcminSAD = gmcSAD;
3105                            direction = 512;
3106                    }
3107                    }
3108                    if (direction) continue;
3109    
3110                    currwp = centerwp; currwp.duv[2].y--;
3111                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3112                    if (gmcSAD < gmcminSAD)
3113                    {       bestwp = currwp;
3114                            gmcminSAD = gmcSAD;
3115                            direction = 1024;
3116                    }
3117                    else
3118                    {
3119                    currwp = centerwp; currwp.duv[2].y++;
3120                    gmcSAD = globalSAD(&currwp, pParam, pMBs, current, pRef, pCurr, GMCblock);
3121                    if (gmcSAD < gmcminSAD)
3122                    {       bestwp = currwp;
3123                            gmcminSAD = gmcSAD;
3124                            direction = 2048;
3125                    }
3126                    }
3127            } while (direction);
3128            free(GMCblock);
3129    
3130            *startwp = bestwp;
3131    
3132            return gmcminSAD;
3133    }
3134    
3135    int
3136    globalSAD(const WARPPOINTS *const wp,
3137                      const MBParam * const pParam,
3138                      const MACROBLOCK * const pMBs,
3139                      const FRAMEINFO * const current,
3140                      const IMAGE * const pRef,
3141                      const IMAGE * const pCurr,
3142                      uint8_t *const GMCblock)
3143    {
3144            NEW_GMC_DATA gmc_data;
3145            int iSAD, gmcSAD=0;
3146            int num=0;
3147            unsigned int mx, my;
3148    
3149            generate_GMCparameters( 3, 3, wp, pParam->width, pParam->height, &gmc_data);
3150    
3151            for (my = 0; my < (uint32_t)pParam->mb_height; my++)
3152                    for (mx = 0; mx < (uint32_t)pParam->mb_width; mx++) {
3153    
3154                    const int mbnum = mx + my * pParam->mb_width;
3155                    const int iEdgedWidth = pParam->edged_width;
3156    
3157                    if (!pMBs[mbnum].mcsel)
3158                            continue;
3159    
3160                    gmc_data.predict_16x16(&gmc_data, GMCblock,
3161                                                    pRef->y,
3162                                                    iEdgedWidth,
3163                                                    iEdgedWidth,
3164                                                    mx, my,
3165                                                    pParam->m_rounding_type);
3166    
3167                    iSAD = sad16 ( pCurr->y + 16*(my*iEdgedWidth + mx),
3168                                                    GMCblock , iEdgedWidth, 65536);
3169                    iSAD -= pMBs[mbnum].sad16;
3170    
3171                    if (iSAD<0)
3172                            gmcSAD += iSAD;
3173                    num++;
3174            }
3175            return gmcSAD;
3176    }
3177    

Legend:
Removed from v.1075  
changed lines
  Added in v.1129

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