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

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

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

revision 768, Thu Jan 9 11:36:33 2003 UTC revision 819, Sat Feb 8 03:49:47 2003 UTC
# Line 32  Line 32 
32  #include <stdio.h>  #include <stdio.h>
33  #include <stdlib.h>  #include <stdlib.h>
34  #include <string.h>     // memcpy  #include <string.h>     // memcpy
35    #include <math.h>       // lrint
36    
37  #include "../encoder.h"  #include "../encoder.h"
38  #include "../utils/mbfunctions.h"  #include "../utils/mbfunctions.h"
# Line 49  Line 50 
50  #define MAX_SAD00_FOR_SKIP      (20)  #define MAX_SAD00_FOR_SKIP      (20)
51  #define MAX_CHROMA_SAD_FOR_SKIP (22)  #define MAX_CHROMA_SAD_FOR_SKIP (22)
52    
53  #define CHECK_CANDIDATE(X,Y,D) { \  #define CHECK_CANDIDATE(X,Y,D) { CheckCandidate((X),(Y), (D), &iDirection, data ); }
 (*CheckCandidate)((const int)(X),(const int)(Y), (D), &iDirection, data ); }  
54    
55  static __inline uint32_t  static __inline uint32_t
56  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)  d_mv_bits(int x, int y, const VECTOR pred, const uint32_t iFcode, const int qpel, const int rrv)
57  {  {
58          int xb, yb;          int xb, yb;
59          x += x * qpel; y += y * qpel;          x = qpel ? x<<1 : x;
60            y = qpel ? y<<1 : y;
61          if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }          if (rrv) { x = RRV_MV_SCALEDOWN(x); y = RRV_MV_SCALEDOWN(y); }
62    
63          x -= pred.x;          x -= pred.x;
64          y -= pred.y;          y -= pred.y;
65    
# Line 79  Line 81 
81          return xb + yb;          return xb + yb;
82  }  }
83    
84    static int32_t ChromaSAD2(int fx, int fy, int bx, int by, const SearchData * const data)
85    {
86            int sad;
87            const uint32_t stride = data->iEdgedWidth/2;
88            uint8_t * f_refu = data->RefQ,
89                    * f_refv = data->RefQ + 8,
90                    * b_refu = data->RefQ + 16,
91                    * b_refv = data->RefQ + 24;
92    
93            switch (((fx & 1) << 1) | (fy & 1))     {
94                    case 0:
95                            fx = fx / 2; fy = fy / 2;
96                            f_refu = (uint8_t*)data->RefCU + fy * stride + fx, stride;
97                            f_refv = (uint8_t*)data->RefCV + fy * stride + fx, stride;
98                            break;
99                    case 1:
100                            fx = fx / 2; fy = (fy - 1) / 2;
101                            interpolate8x8_halfpel_v(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding);
102                            interpolate8x8_halfpel_v(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding);
103                            break;
104                    case 2:
105                            fx = (fx - 1) / 2; fy = fy / 2;
106                            interpolate8x8_halfpel_h(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding);
107                            interpolate8x8_halfpel_h(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding);
108                            break;
109                    default:
110                            fx = (fx - 1) / 2; fy = (fy - 1) / 2;
111                            interpolate8x8_halfpel_hv(f_refu, data->RefCU + fy * stride + fx, stride, data->rounding);
112                            interpolate8x8_halfpel_hv(f_refv, data->RefCV + fy * stride + fx, stride, data->rounding);
113                            break;
114            }
115    
116            switch (((bx & 1) << 1) | (by & 1))     {
117                    case 0:
118                            bx = bx / 2; by = by / 2;
119                            b_refu = (uint8_t*)data->b_RefCU + by * stride + bx, stride;
120                            b_refv = (uint8_t*)data->b_RefCV + by * stride + bx, stride;
121                            break;
122                    case 1:
123                            bx = bx / 2; by = (by - 1) / 2;
124                            interpolate8x8_halfpel_v(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding);
125                            interpolate8x8_halfpel_v(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding);
126                            break;
127                    case 2:
128                            bx = (bx - 1) / 2; by = by / 2;
129                            interpolate8x8_halfpel_h(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding);
130                            interpolate8x8_halfpel_h(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding);
131                            break;
132                    default:
133                            bx = (bx - 1) / 2; by = (by - 1) / 2;
134                            interpolate8x8_halfpel_hv(b_refu, data->b_RefCU + by * stride + bx, stride, data->rounding);
135                            interpolate8x8_halfpel_hv(b_refv, data->b_RefCV + by * stride + bx, stride, data->rounding);
136                            break;
137            }
138    
139            sad = sad8bi(data->CurU, b_refu, f_refu, stride);
140            sad += sad8bi(data->CurV, b_refv, f_refv, stride);
141    
142            return sad;
143    }
144    
145    
146  static int32_t  static int32_t
147  ChromaSAD(int dx, int dy, const SearchData * const data)  ChromaSAD(int dx, int dy, const SearchData * const data)
148  {  {
# Line 139  Line 203 
203  {  {
204          switch ( ((x&1)<<1) | (y&1) ) {          switch ( ((x&1)<<1) | (y&1) ) {
205                  case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);                  case 0 : return data->Ref + x/2 + (y/2)*(data->iEdgedWidth);
206                    case 3 : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);
207                  case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);                  case 1 : return data->RefV + x/2 + ((y-1)/2)*(data->iEdgedWidth);
208                  case 2 : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);                  default : return data->RefH + (x-1)/2 + (y/2)*(data->iEdgedWidth);      //case 2
                 default : return data->RefHV + (x-1)/2 + ((y-1)/2)*(data->iEdgedWidth);  
209          }          }
210  }  }
211    
# Line 202  Line 266 
266    
267          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);          ref1 = GetReferenceB(halfpel_x, halfpel_y, dir, data);
268          switch( ((x&1)<<1) + (y&1) ) {          switch( ((x&1)<<1) + (y&1) ) {
269          case 0: // pure halfpel position          case 3: // x and y in qpel resolution - the "corners" (top left/right and
270                  return (uint8_t *) ref1;                           // bottom left/right) during qpel refinement
271                    ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
272                    ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);
273                    ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);
274                    interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);
275                    interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);
276                    interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);
277                    interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);
278                    break;
279    
280          case 1: // x halfpel, y qpel - top or bottom during qpel refinement          case 1: // x halfpel, y qpel - top or bottom during qpel refinement
281                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);                  ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);
282                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);                  interpolate8x8_avg2(Reference, ref1, ref2, iEdgedWidth, rounding, 8);
# Line 220  Line 293 
293                  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);
294                  break;                  break;
295    
296          default: // x and y in qpel resolution - the "corners" (top left/right and          case 0: // pure halfpel position
297                           // bottom left/right) during qpel refinement                  return (uint8_t *) ref1;
                 ref2 = GetReferenceB(halfpel_x, y - halfpel_y, dir, data);  
                 ref3 = GetReferenceB(x - halfpel_x, halfpel_y, dir, data);  
                 ref4 = GetReferenceB(x - halfpel_x, y - halfpel_y, dir, data);  
                 interpolate8x8_avg4(Reference, ref1, ref2, ref3, ref4, iEdgedWidth, rounding);  
                 interpolate8x8_avg4(Reference+8, ref1+8, ref2+8, ref3+8, ref4+8, iEdgedWidth, rounding);  
                 interpolate8x8_avg4(Reference+8*iEdgedWidth, ref1+8*iEdgedWidth, ref2+8*iEdgedWidth, ref3+8*iEdgedWidth, ref4+8*iEdgedWidth, iEdgedWidth, rounding);  
                 interpolate8x8_avg4(Reference+8*iEdgedWidth+8, ref1+8*iEdgedWidth+8, ref2+8*iEdgedWidth+8, ref3+8*iEdgedWidth+8, ref4+8*iEdgedWidth+8, iEdgedWidth, rounding);  
                 break;  
298          }          }
299          return Reference;          return Reference;
300  }  }
# Line 239  Line 304 
304  static void  static void
305  CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate16(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
306  {  {
307          int t, xc, yc;          int xc, yc;
308          const uint8_t * Reference;          const uint8_t * Reference;
309          VECTOR * current;          VECTOR * current;
310            int32_t sad; uint32_t t;
311    
312          if ( (x > data->max_dx) | (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
313                  | (y > data->max_dy) | (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
314    
315          if (data->qpel_precision) { // x and y are in 1/4 precision          if (!data->qpel_precision) {
                 Reference = Interpolate16x16qpel(x, y, 0, data);  
                 xc = x/2; yc = y/2; //for chroma sad  
                 current = data->currentQMV;  
         } else {  
316                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
317                  current = data->currentMV;                  current = data->currentMV;
318                  xc = x; yc = y;                  xc = x; yc = y;
319            } else { // x and y are in 1/4 precision
320                    Reference = Interpolate16x16qpel(x, y, 0, data);
321                    xc = x/2; yc = y/2; //for chroma sad
322                    current = data->currentQMV;
323          }          }
         t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);  
324    
325          data->temp[0] = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);          sad = sad16v(data->Cur, Reference, data->iEdgedWidth, data->temp + 1);
326            t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
327    
328          data->temp[0] += (data->lambda16 * t * data->temp[0])>>10;          sad += (data->lambda16 * t * sad)>>10;
329          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;
330    
331          if (data->chroma) data->temp[0] += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],          if (data->chroma) 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 (data->temp[0] < data->iMinSAD[0]) {          if (sad < data->iMinSAD[0]) {
335                  data->iMinSAD[0] = data->temp[0];                  data->iMinSAD[0] = sad;
336                  current[0].x = x; current[0].y = y;                  current[0].x = x; current[0].y = y;
337                  *dir = Direction; }                  *dir = Direction;
338            }
339    
340          if (data->temp[1] < data->iMinSAD[1]) {          if (data->temp[1] < data->iMinSAD[1]) {
341                  data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; }                  data->iMinSAD[1] = data->temp[1]; current[1].x = x; current[1].y= y; }
# Line 282  Line 349 
349  }  }
350    
351  static void  static void
352    CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
353    {
354            int32_t sad; uint32_t t;
355            const uint8_t * Reference;
356    
357            if ( (x > data->max_dx) || (x < data->min_dx)
358                    || (y > data->max_dy) || (y < data->min_dy) ) return;
359    
360            if (!data->qpel_precision) Reference = GetReference(x, y, data);
361            else Reference = Interpolate8x8qpel(x, y, 0, 0, data);
362    
363            sad = sad8(data->Cur, Reference, data->iEdgedWidth);
364            t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0);
365    
366            sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;
367    
368            if (sad < *(data->iMinSAD)) {
369                    *(data->iMinSAD) = sad;
370                    data->currentMV->x = x; data->currentMV->y = y;
371                    *dir = Direction;
372            }
373    }
374    
375    
376    static void
377  CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate32(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
378  {  {
379          uint32_t t;          uint32_t t;
380          const uint8_t * Reference;          const uint8_t * Reference;
381    
382          if ( (!(x&1) && x !=0) | (!(y&1) && y !=0) || //non-zero integer value          if ( (!(x&1) && x !=0) || (!(y&1) && y !=0) || //non-zero integer value
383                  (x > data->max_dx) | (x < data->min_dx)                  (x > data->max_dx) || (x < data->min_dx)
384                  | (y > data->max_dy) | (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
385    
386          Reference = GetReference(x, y, data);          Reference = GetReference(x, y, data);
387          t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1);          t = d_mv_bits(x, y, data->predMV, data->iFcode, 0, 1);
# Line 317  Line 409 
409  static void  static void
410  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidate16no4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
411  {  {
412          int32_t sad;          int32_t sad, xc, yc;
413          const uint8_t * Reference;          const uint8_t * Reference;
414          uint32_t t;          uint32_t t;
415          VECTOR * current;          VECTOR * current;
# Line 330  Line 422 
422          if (data->qpel_precision) { // x and y are in 1/4 precision          if (data->qpel_precision) { // x and y are in 1/4 precision
423                  Reference = Interpolate16x16qpel(x, y, 0, data);                  Reference = Interpolate16x16qpel(x, y, 0, data);
424                  current = data->currentQMV;                  current = data->currentQMV;
425                    xc = x/2; yc = y/2;
426          } else {          } else {
427                  Reference = GetReference(x, y, data);                  Reference = GetReference(x, y, data);
428                  current = data->currentMV;                  current = data->currentMV;
429                    xc = x; yc = y;
430          }          }
431          t = d_mv_bits(x, y, data->predMV, data->iFcode,          t = d_mv_bits(x, y, data->predMV, data->iFcode,
432                                          data->qpel^data->qpel_precision, data->rrv);                                          data->qpel^data->qpel_precision, data->rrv);
# Line 340  Line 434 
434          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);          sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
435          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t * sad)>>10;
436    
437            if (data->chroma) sad += ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
438                                                                                    (yc >> 1) + roundtab_79[yc & 0x3], data);
439    
440    
441          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
442                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
443                  current->x = x; current->y = y;                  current->x = x; current->y = y;
444                  *dir = Direction; }                  *dir = Direction;
445            }
446  }  }
447    
448  static void  static void
449  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)
450  {  {
451  // maximum speed - for P/B/I decision  // maximum speed - for P/B/I decision
452            int32_t sad;
453    
454          if ( (x > data->max_dx) | (x < data->min_dx)          if ( (x > data->max_dx) || (x < data->min_dx)
455                  | (y > data->max_dy) | (y < data->min_dy) ) return;                  || (y > data->max_dy) || (y < data->min_dy) ) return;
456    
457          data->temp[0] = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),          sad = sad32v_c(data->Cur, data->Ref + x/2 + (y/2)*(data->iEdgedWidth),
458                                                          data->iEdgedWidth, data->temp+1);                                                          data->iEdgedWidth, data->temp+1);
459    
460          if (data->temp[0] < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
461                  *(data->iMinSAD) = data->temp[0];                  *(data->iMinSAD) = sad;
462                  data->currentMV[0].x = x; data->currentMV[0].y = y;                  data->currentMV[0].x = x; data->currentMV[0].y = y;
463                  *dir = Direction; }                  *dir = Direction;
464            }
465          if (data->temp[1] < data->iMinSAD[1]) {          if (data->temp[1] < data->iMinSAD[1]) {
466                  data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }                  data->iMinSAD[1] = data->temp[1]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
467          if (data->temp[2] < data->iMinSAD[2]) {          if (data->temp[2] < data->iMinSAD[2]) {
# Line 375  Line 476 
476  static void  static void
477  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateInt(const int xf, const int yf, const int Direction, int * const dir, const SearchData * const data)
478  {  {
479          int32_t sad, xb, yb;          int32_t sad, xb, yb, xcf, ycf, xcb, ycb;
480          uint32_t t;          uint32_t t;
481          const uint8_t *ReferenceF, *ReferenceB;          const uint8_t *ReferenceF, *ReferenceB;
482          VECTOR *current;          VECTOR *current;
# Line 388  Line 489 
489                  xb = data->currentMV[1].x; yb = data->currentMV[1].y;                  xb = data->currentMV[1].x; yb = data->currentMV[1].y;
490                  ReferenceB = GetReferenceB(xb, yb, 1, data);                  ReferenceB = GetReferenceB(xb, yb, 1, data);
491                  current = data->currentMV;                  current = data->currentMV;
492                    xcf = xf; ycf = yf;
493                    xcb = xb; ycb = yb;
494          } else {          } else {
495                  ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);                  ReferenceF = Interpolate16x16qpel(xf, yf, 0, data);
496                  xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;                  xb = data->currentQMV[1].x; yb = data->currentQMV[1].y;
497                  current = data->currentQMV;                  current = data->currentQMV;
498                  ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);                  ReferenceB = Interpolate16x16qpel(xb, yb, 1, data);
499                    xcf = xf/2; ycf = yf/2;
500                    xcb = xb/2; ycb = yb/2;
501          }          }
502    
503          t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0)          t = d_mv_bits(xf, yf, data->predMV, data->iFcode, data->qpel^data->qpel_precision, 0)
# Line 401  Line 506 
506          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
507          sad += (data->lambda16 * t * sad)>>10;          sad += (data->lambda16 * t * sad)>>10;
508    
509            if (data->chroma) sad += ChromaSAD2((xcf >> 1) + roundtab_79[xcf & 0x3],
510                                                                                    (ycf >> 1) + roundtab_79[ycf & 0x3],
511                                                                                    (xcb >> 1) + roundtab_79[xcb & 0x3],
512                                                                                    (ycb >> 1) + roundtab_79[ycb & 0x3], data);
513    
514          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
515                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
516                  current->x = xf; current->y = yf;                  current->x = xf; current->y = yf;
517                  *dir = Direction; }                  *dir = Direction;
518            }
519  }  }
520    
521  static void  static void
522  CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateDirect(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
523  {  {
524          int32_t sad = 0;          int32_t sad = 0, xcf = 0, ycf = 0, xcb = 0, ycb = 0;
525          uint32_t k;          uint32_t k;
526          const uint8_t *ReferenceF;          const uint8_t *ReferenceF;
527          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
# Line 429  Line 540 
540                          data->directmvB[k].y                          data->directmvB[k].y
541                          : mvs.y - data->referencemv[k].y);                          : mvs.y - data->referencemv[k].y);
542    
543                  if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )                  if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)
544                          || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )                          | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)
545                          || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                          | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)
546                          || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;                          | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;
547    
548                    if (data->qpel) {
549                  mvs.x *= 2 - data->qpel; mvs.y *= 2 - data->qpel;                          xcf += mvs.x/2; ycf += mvs.y/2;
550                  b_mvs.x *= 2 - data->qpel; b_mvs.y *= 2 - data->qpel; //we move to qpel precision anyway                          xcb += b_mvs.x/2; ycb += b_mvs.y/2;
551                    } else {
552                            xcf += mvs.x; ycf += mvs.y;
553                            xcb += b_mvs.x; ycb += b_mvs.y;
554                            mvs.x *= 2; mvs.y *= 2; //we move to qpel precision anyway
555                            b_mvs.x *= 2; b_mvs.y *= 2;
556                    }
557    
558                  ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);                  ReferenceF = Interpolate8x8qpel(mvs.x, mvs.y, k, 0, data);
559                  ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);                  ReferenceB = Interpolate8x8qpel(b_mvs.x, b_mvs.y, k, 1, data);
# Line 448  Line 565 
565    
566          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;
567    
568            if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
569                                                                                    (ycf >> 3) + roundtab_76[ycf & 0xf],
570                                                                                    (xcb >> 3) + roundtab_76[xcb & 0xf],
571                                                                                    (ycb >> 3) + roundtab_76[ycb & 0xf], data);
572    
573          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
574                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
575                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
576                  *dir = Direction; }                  *dir = Direction;
577            }
578  }  }
579    
580  static void  static void
581  CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  CheckCandidateDirectno4v(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)
582  {  {
583          int32_t sad;          int32_t sad, xcf, ycf, xcb, ycb;
584          const uint8_t *ReferenceF;          const uint8_t *ReferenceF;
585          const uint8_t *ReferenceB;          const uint8_t *ReferenceB;
586          VECTOR mvs, b_mvs;          VECTOR mvs, b_mvs;
# Line 474  Line 597 
597                  data->directmvB[0].y                  data->directmvB[0].y
598                  : mvs.y - data->referencemv[0].y);                  : mvs.y - data->referencemv[0].y);
599    
600          if (( mvs.x > data->max_dx ) || ( mvs.x < data->min_dx )          if ( (mvs.x > data->max_dx) | (mvs.x < data->min_dx)
601                  || ( mvs.y > data->max_dy ) || ( mvs.y < data->min_dy )                  | (mvs.y > data->max_dy) | (mvs.y < data->min_dy)
602                  || ( b_mvs.x > data->max_dx ) || ( b_mvs.x < data->min_dx )                  | (b_mvs.x > data->max_dx) | (b_mvs.x < data->min_dx)
603                  || ( b_mvs.y > data->max_dy ) || ( b_mvs.y < data->min_dy )) return;                  | (b_mvs.y > data->max_dy) | (b_mvs.y < data->min_dy) ) return;
604    
605          mvs.x *= 2 - data->qpel; mvs.y *= 2 - data->qpel;          if (data->qpel) {
606          b_mvs.x *= 2 - data->qpel; b_mvs.y *= 2 - data->qpel; //we move to qpel precision anyway                  xcf = 4*(mvs.x/2); ycf = 4*(mvs.y/2);
607                    xcb = 4*(b_mvs.x/2); ycb = 4*(b_mvs.y/2);
608          ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);          ReferenceF = Interpolate16x16qpel(mvs.x, mvs.y, 0, data);
609          ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);          ReferenceB = Interpolate16x16qpel(b_mvs.x, b_mvs.y, 1, data);
610            } else {
611                    xcf = 4*mvs.x; ycf = 4*mvs.y;
612                    xcb = 4*b_mvs.x; ycb = 4*b_mvs.y;
613                    ReferenceF = GetReference(mvs.x, mvs.y, data);
614                    ReferenceB = GetReferenceB(b_mvs.x, b_mvs.y, 1, data);
615            }
616    
617          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);          sad = sad16bi(data->Cur, ReferenceF, ReferenceB, data->iEdgedWidth);
618          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;
619    
620          if (sad < *(data->iMinSAD)) {          if (data->chroma) sad += ChromaSAD2((xcf >> 3) + roundtab_76[xcf & 0xf],
621                  *(data->iMinSAD) = sad;                                                                                  (ycf >> 3) + roundtab_76[ycf & 0xf],
622                  data->currentMV->x = x; data->currentMV->y = y;                                                                                  (xcb >> 3) + roundtab_76[xcb & 0xf],
623                  *dir = Direction; }                                                                                  (ycb >> 3) + roundtab_76[ycb & 0xf], data);
 }  
   
 static void  
 CheckCandidate8(const int x, const int y, const int Direction, int * const dir, const SearchData * const data)  
 {  
         int32_t sad; uint32_t t;  
         const uint8_t * Reference;  
   
         if ( (x > data->max_dx) | (x < data->min_dx)  
                 | (y > data->max_dy) | (y < data->min_dy) ) return;  
   
         if (data->qpel) Reference = Interpolate16x16qpel(x, y, 0, data);  
         else Reference = GetReference(x, y, data);  
   
         sad = sad8(data->Cur, Reference, data->iEdgedWidth);  
         t = d_mv_bits(x, y, data->predMV, data->iFcode, data->qpel && !data->qpel_precision, 0);  
   
         sad += (data->lambda8 * t * (sad+NEIGH_8X8_BIAS))>>10;  
624    
625          if (sad < *(data->iMinSAD)) {          if (sad < *(data->iMinSAD)) {
626                  *(data->iMinSAD) = sad;                  *(data->iMinSAD) = sad;
627                  data->currentMV->x = x; data->currentMV->y = y;                  data->currentMV->x = x; data->currentMV->y = y;
628                  *dir = Direction; }                  *dir = Direction;
629            }
630  }  }
631    
632  /* CHECK_CANDIATE FUNCTIONS END */  /* CHECK_CANDIATE FUNCTIONS END */
# Line 752  Line 864 
864          Data.iFcode = current->fcode;          Data.iFcode = current->fcode;
865          Data.rounding = pParam->m_rounding_type;          Data.rounding = pParam->m_rounding_type;
866          Data.qpel = pParam->m_quarterpel;          Data.qpel = pParam->m_quarterpel;
867          Data.chroma = current->global_flags & XVID_ME_COLOUR;          Data.chroma = current->motion_flags & PMV_CHROMA16;
868          Data.rrv = current->global_flags & XVID_REDUCED;          Data.rrv = current->global_flags & XVID_REDUCED;
869    
870          if ((current->global_flags & XVID_REDUCED)) {          if ((current->global_flags & XVID_REDUCED)) {
# Line 801  Line 913 
913    
914  //initial skip decision  //initial skip decision
915  /* no early skip for GMC (global vector = skip vector is unknown!)      */  /* no early skip for GMC (global vector = skip vector is unknown!)      */
916                          if (current->coding_type == P_VOP)      { /* no fast SKIP for S(GMC)-VOPs */                          if (!(current->global_flags & XVID_GMC))        { /* no fast SKIP for S(GMC)-VOPs */
917                                  if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) )                                  if (pMB->dquant == NO_CHANGE && sad00 < pMB->quant * INITIAL_SKIP_THRESH * (Data.rrv ? 4:1) )
918                                          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)) {
919                                                  SkipMacroblockP(pMB, sad00);                                                  SkipMacroblockP(pMB, sad00);
# Line 815  Line 927 
927                                                  current->global_flags & XVID_INTER4V, pMB);                                                  current->global_flags & XVID_INTER4V, pMB);
928    
929  /* final skip decision, a.k.a. "the vector you found, really that good?" */  /* final skip decision, a.k.a. "the vector you found, really that good?" */
930                          if (current->coding_type == P_VOP)      {                          if (!(current->global_flags & XVID_GMC))        {
931                                  if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)                                  if ( (pMB->dquant == NO_CHANGE) && (sad00 < pMB->quant * MAX_SAD00_FOR_SKIP)
932                                          && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1)) )                                          && ((100*pMB->sad16)/(sad00+1) > FINAL_SKIP_THRESH * (Data.rrv ? 4:1)) )
933                                          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)) {
# Line 855  Line 967 
967                  }                  }
968          }          }
969    
970          if (current->coding_type == S_VOP)      /* first GMC step only for S(GMC)-VOPs */          if (current->global_flags & XVID_GMC )  /* GMC only for S(GMC)-VOPs */
971                  current->GMC_MV = GlobalMotionEst( pMBs, pParam, current->fcode );          {
972          else                  current->warp = GlobalMotionEst( pMBs, pParam, current, reference, pRefH, pRefV, pRefHV);
973                  current->GMC_MV = zeroMV;          }
974    
975          return 0;          return 0;
976  }  }
# Line 999  Line 1111 
1111    
1112          for (i = 1; i < 7; i++) {          for (i = 1; i < 7; i++) {
1113                  if (!(mask = make_mask(pmv, i)) ) continue;                  if (!(mask = make_mask(pmv, i)) ) continue;
1114                  (*CheckCandidate)(pmv[i].x, pmv[i].y, mask, &iDirection, Data);                  CheckCandidate(pmv[i].x, pmv[i].y, mask, &iDirection, Data);
1115                  if (Data->iMinSAD[0] <= threshA) break;                  if (Data->iMinSAD[0] <= threshA) break;
1116          }          }
1117    
# Line 1032  Line 1144 
1144                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1145                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1146    
1147                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);                                  CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data);
1148                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1149                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1150                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
# Line 1045  Line 1157 
1157                          if (!(MVequal(startMV, backupMV))) {                          if (!(MVequal(startMV, backupMV))) {
1158                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;                                  bSAD = Data->iMinSAD[0]; Data->iMinSAD[0] = MV_MAX_ERROR;
1159    
1160                                  (*CheckCandidate)(startMV.x, startMV.y, 255, &iDirection, Data);                                  CheckCandidate(startMV.x, startMV.y, 255, &iDirection, Data);
1161                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);                                  (*MainSearchPtr)(startMV.x, startMV.y, Data, 255);
1162                                  if (bSAD < Data->iMinSAD[0]) {                                  if (bSAD < Data->iMinSAD[0]) {
1163                                          Data->currentMV[0] = backupMV;                                          Data->currentMV[0] = backupMV;
# Line 1269  Line 1381 
1381    
1382  /* search backward or forward */  /* search backward or forward */
1383  static void  static void
1384  SearchBF(       const uint8_t * const pRef,  SearchBF(       const IMAGE * const pRef,
1385                          const uint8_t * const pRefH,                          const uint8_t * const pRefH,
1386                          const uint8_t * const pRefV,                          const uint8_t * const pRefV,
1387                          const uint8_t * const pRefHV,                          const uint8_t * const pRefHV,
# Line 1291  Line 1403 
1403          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
1404          Data->iFcode = iFcode;          Data->iFcode = iFcode;
1405          Data->qpel_precision = 0;          Data->qpel_precision = 0;
1406            Data->temp[5] = Data->temp[6] = Data->temp[7] = 256*4096; // reset chroma-sad cache
1407    
1408          Data->Ref = pRef + (x + y * Data->iEdgedWidth) * 16;          Data->Ref = pRef->y + (x + y * Data->iEdgedWidth) * 16;
1409          Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16;          Data->RefH = pRefH + (x + y * Data->iEdgedWidth) * 16;
1410          Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16;          Data->RefV = pRefV + (x + y * Data->iEdgedWidth) * 16;
1411          Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16;          Data->RefHV = pRefHV + (x + y * Data->iEdgedWidth) * 16;
1412            Data->RefCU = pRef->u + (x + y * Data->iEdgedWidth/2) * 8;
1413            Data->RefCV = pRef->v + (x + y * Data->iEdgedWidth/2) * 8;
1414    
1415          Data->predMV = *predMV;          Data->predMV = *predMV;
1416    
# Line 1435  Line 1550 
1550          Data->bRefH = b_RefH + k;          Data->bRefH = b_RefH + k;
1551          Data->bRefV = b_RefV + k;          Data->bRefV = b_RefV + k;
1552          Data->bRefHV = b_RefHV + k;          Data->bRefHV = b_RefHV + k;
1553            Data->RefCU = f_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
1554            Data->RefCV = f_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
1555            Data->b_RefCU = b_Ref->u + (x + (Data->iEdgedWidth/2) * y) * 8;
1556            Data->b_RefCV = b_Ref->v + (x + (Data->iEdgedWidth/2) * y) * 8;
1557    
1558          k = Data->qpel ? 4 : 2;          k = Data->qpel ? 4 : 2;
1559          Data->max_dx = k * (pParam->width - x * 16);          Data->max_dx = k * (pParam->width - x * 16);
# Line 1470  Line 1589 
1589    
1590          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;          CheckCandidate = b_mb->mode == MODE_INTER4V ? CheckCandidateDirect : CheckCandidateDirectno4v;
1591    
1592          (*CheckCandidate)(0, 0, 255, &k, Data);          CheckCandidate(0, 0, 255, &k, Data);
1593    
1594  // initial (fast) skip decision  // initial (fast) skip decision
1595          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * 2) {          if (*Data->iMinSAD < pMB->quant * INITIAL_SKIP_THRESH * (2 + Data->chroma?1:0)) {
1596                  //possible skip - checking chroma                  //possible skip
1597                    if (Data->chroma) {
1598                            pMB->mode = MODE_DIRECT_NONE_MV;
1599                            return *Data->iMinSAD; // skip.
1600                    } else {
1601                  SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);                  SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
1602                  if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip.                  if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; // skip.
1603          }          }
1604            }
1605    
1606          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
1607    
# Line 1527  Line 1651 
1651  }  }
1652    
1653  static void  static void
1654  SearchInterpolate(const uint8_t * const f_Ref,  SearchInterpolate(const IMAGE * const f_Ref,
1655                                  const uint8_t * const f_RefH,                                  const uint8_t * const f_RefH,
1656                                  const uint8_t * const f_RefV,                                  const uint8_t * const f_RefV,
1657                                  const uint8_t * const f_RefHV,                                  const uint8_t * const f_RefHV,
1658                                  const uint8_t * const b_Ref,                                  const IMAGE * const b_Ref,
1659                                  const uint8_t * const b_RefH,                                  const uint8_t * const b_RefH,
1660                                  const uint8_t * const b_RefV,                                  const uint8_t * const b_RefV,
1661                                  const uint8_t * const b_RefHV,                                  const uint8_t * const b_RefHV,
# Line 1559  Line 1683 
1683          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;          fData->iFcode = bData.bFcode = fcode; fData->bFcode = bData.iFcode = bcode;
1684    
1685          i = (x + y * fData->iEdgedWidth) * 16;          i = (x + y * fData->iEdgedWidth) * 16;
1686          bData.bRef = fData->Ref = f_Ref + i;          bData.bRef = fData->Ref = f_Ref->y + i;
1687          bData.bRefH = fData->RefH = f_RefH + i;          bData.bRefH = fData->RefH = f_RefH + i;
1688          bData.bRefV = fData->RefV = f_RefV + i;          bData.bRefV = fData->RefV = f_RefV + i;
1689          bData.bRefHV = fData->RefHV = f_RefHV + i;          bData.bRefHV = fData->RefHV = f_RefHV + i;
1690          bData.Ref = fData->bRef = b_Ref + i;          bData.Ref = fData->bRef = b_Ref->y + i;
1691          bData.RefH = fData->bRefH = b_RefH + i;          bData.RefH = fData->bRefH = b_RefH + i;
1692          bData.RefV = fData->bRefV = b_RefV + i;          bData.RefV = fData->bRefV = b_RefV + i;
1693          bData.RefHV = fData->bRefHV = b_RefHV + i;          bData.RefHV = fData->bRefHV = b_RefHV + i;
1694            bData.b_RefCU = fData->RefCU = f_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;
1695            bData.b_RefCV = fData->RefCV = f_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;
1696            bData.RefCU = fData->b_RefCU = b_Ref->u + (x + (fData->iEdgedWidth/2) * y) * 8;
1697            bData.RefCV = fData->b_RefCV = b_Ref->v + (x + (fData->iEdgedWidth/2) * y) * 8;
1698    
1699    
1700          bData.bpredMV = fData->predMV = *f_predMV;          bData.bpredMV = fData->predMV = *f_predMV;
1701          fData->bpredMV = bData.predMV = *b_predMV;          fData->bpredMV = bData.predMV = *b_predMV;
# Line 1683  Line 1812 
1812          int32_t iMinSAD;          int32_t iMinSAD;
1813          VECTOR currentMV[3];          VECTOR currentMV[3];
1814          VECTOR currentQMV[3];          VECTOR currentQMV[3];
1815            int32_t temp[8];
1816          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
1817          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
1818          Data.currentMV = currentMV; Data.currentQMV = currentQMV;          Data.currentMV = currentMV; Data.currentQMV = currentQMV;
1819          Data.iMinSAD = &iMinSAD;          Data.iMinSAD = &iMinSAD;
1820          Data.lambda16 = lambda_vec16[frame->quant];          Data.lambda16 = lambda_vec16[frame->quant];
         Data.chroma = frame->quant;  
1821          Data.qpel = pParam->m_quarterpel;          Data.qpel = pParam->m_quarterpel;
1822          Data.rounding = 0;          Data.rounding = 0;
1823            Data.chroma = frame->motion_flags & PMV_CHROMA8;
1824            Data.temp = temp;
1825    
1826          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)
1827          // note: i==horizontal, j==vertical          // note: i==horizontal, j==vertical
# Line 1710  Line 1841 
1841                                  }                                  }
1842    
1843                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
1844                            Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;
1845                            Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;
1846                          pMB->quant = frame->quant;                          pMB->quant = frame->quant;
1847    
1848  /* direct search comes first, because it (1) checks for SKIP-mode  /* direct search comes first, because it (1) checks for SKIP-mode
# Line 1728  Line 1861 
1861                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }                          if (pMB->mode == MODE_DIRECT_NONE_MV) { n_count++; continue; }
1862    
1863                          // forward search                          // forward search
1864                          SearchBF(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
1865                                                  &frame->image, i, j,                                                  &frame->image, i, j,
1866                                                  frame->motion_flags,                                                  frame->motion_flags,
1867                                                  frame->fcode, pParam,                                                  frame->fcode, pParam,
# Line 1736  Line 1869 
1869                                                  MODE_FORWARD, &Data);                                                  MODE_FORWARD, &Data);
1870    
1871                          // backward search                          // backward search
1872                          SearchBF(b_ref->y, b_refH->y, b_refV->y, b_refHV->y,                          SearchBF(b_ref, b_refH->y, b_refV->y, b_refHV->y,
1873                                                  &frame->image, i, j,                                                  &frame->image, i, j,
1874                                                  frame->motion_flags,                                                  frame->motion_flags,
1875                                                  frame->bcode, pParam,                                                  frame->bcode, pParam,
# Line 1744  Line 1877 
1877                                                  MODE_BACKWARD, &Data);                                                  MODE_BACKWARD, &Data);
1878    
1879                          // interpolate search comes last, because it uses data from forward and backward as prediction                          // interpolate search comes last, because it uses data from forward and backward as prediction
1880                          SearchInterpolate(f_ref->y, f_refH->y, f_refV->y, f_refHV->y,                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
1881                                                  b_ref->y, b_refH->y, b_refV->y, b_refHV->y,                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,
1882                                                  &frame->image,                                                  &frame->image,
1883                                                  i, j,                                                  i, j,
1884                                                  frame->fcode, frame->bcode,                                                  frame->fcode, frame->bcode,
# Line 1906  Line 2039 
2039                  }                  }
2040          }          }
2041          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);          sSAD /= (pParam->mb_height-2)*(pParam->mb_width-2);
2042          if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP;  //      if (sSAD > IntraThresh + INTRA_BIAS) return I_VOP;
2043          if (sSAD > InterThresh ) return P_VOP;          if (sSAD > InterThresh ) return P_VOP;
2044          emms();          emms();
2045          return B_VOP;          return B_VOP;
2046    
2047  }  }
2048    
2049  static void  
2050  CheckGMC(int x, int y, const int dir, int * iDirection,  static WARPPOINTS
2051                  const MACROBLOCK * const pMBs, uint32_t * bestcount, VECTOR * GMC,  GlobalMotionEst(const MACROBLOCK * const pMBs,
2052                  const MBParam * const pParam)                                  const MBParam * const pParam,
2053                                    const FRAMEINFO * const current,
2054                                    const FRAMEINFO * const reference,
2055                                    const IMAGE * const pRefH,
2056                                    const IMAGE * const pRefV,
2057                                    const IMAGE * const pRefHV      )
2058  {  {
         uint32_t mx, my, a, count = 0;  
2059    
2060          for (my = 1; my < pParam->mb_height-1; my++)          const int deltax=8;             // upper bound for difference between a MV and it's neighbour MVs
2061                  for (mx = 1; mx < pParam->mb_width-1; mx++) {          const int deltay=8;
2062                          VECTOR mv;          const int grad=512;             // lower bound for deviation in MB
2063                          const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];  
2064                          if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED) continue;          WARPPOINTS gmc;
2065                          mv = pMB->mvs[0];  
2066                          a = ABS(mv.x - x) + ABS(mv.y - y);          uint32_t mx, my;
2067                          if (a < 6) count += 6 - a;  
2068                  }          int MBh = pParam->mb_height;
2069            int MBw = pParam->mb_width;
2070    
2071            int *MBmask= calloc(MBh*MBw,sizeof(int));
2072            double DtimesF[4] = { 0.,0., 0., 0. };
2073            double sol[4] = { 0., 0., 0., 0. };
2074            double a,b,c,n,denom;
2075            double meanx,meany;
2076            int num,oldnum;
2077    
2078            if (!MBmask) { fprintf(stderr,"Mem error\n"); return gmc;}
2079    
2080    // filter mask of all blocks
2081    
2082            for (my = 1; my < MBh-1; my++)
2083            for (mx = 1; mx < MBw-1; mx++)
2084            {
2085                    const int mbnum = mx + my * MBw;
2086                    const MACROBLOCK *pMB = &pMBs[mbnum];
2087                    const VECTOR mv = pMB->mvs[0];
2088    
2089          if (count > *bestcount) {                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)
2090                  *bestcount = count;                          continue;
2091                  *iDirection = dir;  
2092                  GMC->x = x; GMC->y = y;                  if ( ( (ABS(mv.x -   (pMB-1)->mvs[0].x) < deltax) && (ABS(mv.y -   (pMB-1)->mvs[0].y) < deltay) )
2093                    &&   ( (ABS(mv.x -   (pMB+1)->mvs[0].x) < deltax) && (ABS(mv.y -   (pMB+1)->mvs[0].y) < deltay) )
2094                    &&   ( (ABS(mv.x - (pMB-MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB-MBw)->mvs[0].y) < deltay) )
2095                    &&   ( (ABS(mv.x - (pMB+MBw)->mvs[0].x) < deltax) && (ABS(mv.y - (pMB+MBw)->mvs[0].y) < deltay) ) )
2096                            MBmask[mbnum]=1;
2097          }          }
2098    
2099            for (my = 1; my < MBh-1; my++)
2100            for (mx = 1; mx < MBw-1; mx++)
2101            {
2102                    const uint8_t *const pCur = current->image.y + 16*my*pParam->edged_width + 16*mx;
2103    
2104                    const int mbnum = mx + my * MBw;
2105                    if (!MBmask[mbnum])
2106                            continue;
2107    
2108                    if (sad16 ( pCur, pCur+1 , pParam->edged_width, 65536) <= grad )
2109                            MBmask[mbnum] = 0;
2110                    if (sad16 ( pCur, pCur+pParam->edged_width, pParam->edged_width, 65536) <= grad )
2111                            MBmask[mbnum] = 0;
2112    
2113  }  }
2114    
2115            emms();
2116    
2117  static VECTOR          do {            /* until convergence */
 GlobalMotionEst(const MACROBLOCK * const pMBs, const MBParam * const pParam, const uint32_t iFcode)  
 {  
2118    
2119          uint32_t count, bestcount = 0;          a = b = c = n = 0;
2120          int x, y;          DtimesF[0] = DtimesF[1] = DtimesF[2] = DtimesF[3] = 0.;
2121          VECTOR gmc = {0,0};          for (my = 0; my < MBh; my++)
2122          int step, min_x, max_x, min_y, max_y;                  for (mx = 0; mx < MBw; mx++)
2123          uint32_t mx, my;                  {
2124          int iDirection, bDirection;                          const int mbnum = mx + my * MBw;
2125                            const MACROBLOCK *pMB = &pMBs[mbnum];
2126                            const VECTOR mv = pMB->mvs[0];
2127    
2128          min_x = min_y = -32<<iFcode;                          if (!MBmask[mbnum])
2129          max_x = max_y = 32<<iFcode;                                  continue;
2130    
2131  //step1: let's find a rough camera panning                          n++;
2132          for (step = 32; step >= 2; step /= 2) {                          a += 16*mx+8;
2133                  bestcount = 0;                          b += 16*my+8;
2134                  for (y = min_y; y <= max_y; y += step)                          c += (16*mx+8)*(16*mx+8)+(16*my+8)*(16*my+8);
2135                          for (x = min_x ; x <= max_x; x += step) {  
2136                                  count = 0;                          DtimesF[0] += (double)mv.x;
2137                                  //for all macroblocks                          DtimesF[1] += (double)mv.x*(16*mx+8) + (double)mv.y*(16*my+8);
2138                                  for (my = 1; my < pParam->mb_height-1; my++)                          DtimesF[2] += (double)mv.x*(16*my+8) - (double)mv.y*(16*mx+8);
2139                                          for (mx = 1; mx < pParam->mb_width-1; mx++) {                          DtimesF[3] += (double)mv.y;
2140                                                  const MACROBLOCK *pMB = &pMBs[mx + my * pParam->mb_width];                  }
2141                                                  VECTOR mv;  
2142            denom = a*a+b*b-c*n;
2143    
2144    /* Solve the system:     sol = (D'*E*D)^{-1} D'*E*F   */
2145    /* D'*E*F has been calculated in the same loop as matrix */
2146    
2147            sol[0] = -c*DtimesF[0] + a*DtimesF[1] + b*DtimesF[2];
2148            sol[1] =  a*DtimesF[0] - n*DtimesF[1]                + b*DtimesF[3];
2149            sol[2] =  b*DtimesF[0]                - n*DtimesF[2] - a*DtimesF[3];
2150            sol[3] =                 b*DtimesF[1] - a*DtimesF[2] - c*DtimesF[3];
2151    
2152            sol[0] /= denom;
2153            sol[1] /= denom;
2154            sol[2] /= denom;
2155            sol[3] /= denom;
2156    
2157            meanx = meany = 0.;
2158            oldnum = 0;
2159            for (my = 0; my < MBh; my++)
2160                    for (mx = 0; mx < MBw; mx++)
2161                    {
2162                            const int mbnum = mx + my * MBw;
2163                            const MACROBLOCK *pMB = &pMBs[mbnum];
2164                            const VECTOR mv = pMB->mvs[0];
2165    
2166                                                  if (pMB->mode == MODE_INTRA || pMB->mode == MODE_NOT_CODED)                          if (!MBmask[mbnum])
2167                                                          continue;                                                          continue;
2168    
2169                                                  mv = pMB->mvs[0];                          oldnum++;
2170                                                  if ( ABS(mv.x - x) <= step && ABS(mv.y - y) <= step )   /* GMC translation is always halfpel-res */                          meanx += ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x );
2171                                                          count++;                          meany += ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y );
                                         }  
                                 if (count >= bestcount) { bestcount = count; gmc.x = x; gmc.y = y; }  
2172                          }                          }
2173                  min_x = gmc.x - step;  
2174                  max_x = gmc.x + step;          if (4*meanx > oldnum)   /* better fit than 0.25 is useless */
2175                  min_y = gmc.y - step;                  meanx /= oldnum;
2176                  max_y = gmc.y + step;          else
2177                    meanx = 0.25;
2178    
2179            if (4*meany > oldnum)
2180                    meany /= oldnum;
2181            else
2182                    meany = 0.25;
2183    
2184    /*      fprintf(stderr,"sol = (%8.5f, %8.5f, %8.5f, %8.5f)\n",sol[0],sol[1],sol[2],sol[3]);
2185            fprintf(stderr,"meanx = %8.5f  meany = %8.5f   %d\n",meanx,meany, oldnum);
2186    */
2187            num = 0;
2188            for (my = 0; my < MBh; my++)
2189                    for (mx = 0; mx < MBw; mx++)
2190                    {
2191                            const int mbnum = mx + my * MBw;
2192                            const MACROBLOCK *pMB = &pMBs[mbnum];
2193                            const VECTOR mv = pMB->mvs[0];
2194    
2195                            if (!MBmask[mbnum])
2196                                    continue;
2197    
2198                            if  ( ( ABS(( sol[0] + (16*mx+8)*sol[1] + (16*my+8)*sol[2] ) - mv.x ) > meanx )
2199                               || ( ABS(( sol[3] - (16*mx+8)*sol[2] + (16*my+8)*sol[1] ) - mv.y ) > meany ) )
2200                                    MBmask[mbnum]=0;
2201                            else
2202                                    num++;
2203          }          }
2204    
2205          if (bestcount < (pParam->mb_height-2)*(pParam->mb_width-2)/10)          } while ( (oldnum != num) && (num>=4) );
                 gmc.x = gmc.y = 0; //no camara pan, no GMC  
2206    
2207  // step2: let's refine camera panning using gradiend-descent approach          if (num < 4)
2208  // TODO: more warping points may be evaluated here (like in interpolate mode search - two vectors in one diamond)          {
2209          bestcount = 0;                  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;
2210          CheckGMC(gmc.x, gmc.y, 255, &iDirection, pMBs, &bestcount, &gmc, pParam);          } else {
         do {  
                 x = gmc.x; y = gmc.y;  
                 bDirection = iDirection; iDirection = 0;  
                 if (bDirection & 1) CheckGMC(x - 1, y, 1+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);  
                 if (bDirection & 2) CheckGMC(x + 1, y, 2+4+8, &iDirection, pMBs, &bestcount, &gmc, pParam);  
                 if (bDirection & 4) CheckGMC(x, y - 1, 1+2+4, &iDirection, pMBs, &bestcount, &gmc, pParam);  
                 if (bDirection & 8) CheckGMC(x, y + 1, 1+2+8, &iDirection, pMBs, &bestcount, &gmc, pParam);  
2211    
2212          } while (iDirection);                  gmc.duv[0].x=(int)(sol[0]+0.5);
2213                    gmc.duv[0].y=(int)(sol[3]+0.5);
2214    
2215                    gmc.duv[1].x=(int)(sol[1]*pParam->width+0.5);
2216                    gmc.duv[1].y=(int)(-sol[2]*pParam->width+0.5);
2217    
2218          if (pParam->m_quarterpel) {                  gmc.duv[2].x=0;
2219                  gmc.x *= 2;                  gmc.duv[2].y=0;
                 gmc.y *= 2;     /* we store the halfpel value as pseudo-qpel to make comparison easier */  
2220          }          }
2221    //      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);
2222    
2223            free(MBmask);
2224    
2225          return gmc;          return gmc;
2226  }  }

Legend:
Removed from v.768  
changed lines
  Added in v.819

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