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

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

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

revision 1.1.2.1, Wed Sep 10 22:18:59 2003 UTC revision 1.6, Sun Oct 3 14:25:01 2004 UTC
# Line 78  Line 78 
78   ****************************************************************************/   ****************************************************************************/
79    
80  int32_t  int32_t
81  xvid_me_ChromaSAD(const int dx, const int dy, const SearchData * const data)  xvid_me_ChromaSAD(const int dx, const int dy, SearchData * const data)
82  {  {
83          int sad;          int sad;
84          const uint32_t stride = data->iEdgedWidth/2;          const uint32_t stride = data->iEdgedWidth/2;
85          int offset = (dx>>1) + (dy>>1)*stride;          int offset = (dx>>1) + (dy>>1)*stride;
86          int next = 1;          int next = 1;
87    
88          if (dx == data->temp[5] && dy == data->temp[6]) return data->temp[7]; /* it has been checked recently */          if (dx == data->chromaX && dy == data->chromaY)
89          data->temp[5] = dx; data->temp[6] = dy; /* backup */                  return data->chromaSAD; /* it has been checked recently */
90            data->chromaX = dx; data->chromaY = dy; /* backup */
91    
92          switch (((dx & 1) << 1) | (dy & 1))     {          switch (((dx & 1) << 1) | (dy & 1))     {
93                  case 0:                  case 0:
# Line 107  Line 108 
108                          sad += sad8(data->CurV, data->RefQ, stride);                          sad += sad8(data->CurV, data->RefQ, stride);
109                          break;                          break;
110          }          }
111          data->temp[7] = sad; /* backup, part 2 */          data->chromaSAD = sad; /* backup, part 2 */
112          return sad;          return sad;
113  }  }
114    
# Line 206  Line 207 
207  }  }
208    
209  void  void
210  xvid_me_AdvDiamondSearch(int x, int y, const SearchData * const data,  xvid_me_AdvDiamondSearch(int x, int y, SearchData * const data,
211                                                   int bDirection, CheckFunc * const CheckCandidate)                                                   int bDirection, CheckFunc * const CheckCandidate)
212  {  {
213    
214  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
215    
216          unsigned int * const iDirection = data->dir;          unsigned int * const iDirection = &data->dir;
217    
218          for(;;) { /* forever */          for(;;) { /* forever */
219                  *iDirection = 0;                  *iDirection = 0;
# Line 292  Line 293 
293  }  }
294    
295  void  void
296  xvid_me_SquareSearch(int x, int y, const SearchData * const data,  xvid_me_SquareSearch(int x, int y, SearchData * const data,
297                                           int bDirection, CheckFunc * const CheckCandidate)                                           int bDirection, CheckFunc * const CheckCandidate)
298  {  {
299          unsigned int * const iDirection = data->dir;          unsigned int * const iDirection = &data->dir;
300    
301          do {          do {
302                  *iDirection = 0;                  *iDirection = 0;
# Line 314  Line 315 
315  }  }
316    
317  void  void
318  xvid_me_DiamondSearch(int x, int y, const SearchData * const data,  xvid_me_DiamondSearch(int x, int y, SearchData * const data,
319                                            int bDirection, CheckFunc * const CheckCandidate)                                            int bDirection, CheckFunc * const CheckCandidate)
320  {  {
321    
322  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */  /* directions: 1 - left (x-1); 2 - right (x+1), 4 - up (y-1); 8 - down (y+1) */
323    
324          unsigned int * const iDirection = data->dir;          unsigned int * const iDirection = &data->dir;
325    
326          do {          do {
327                  *iDirection = 0;                  *iDirection = 0;
# Line 330  Line 331 
331                  if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);                  if (bDirection & 8) CHECK_CANDIDATE(x, y + iDiamondSize, 8);
332    
333                  /* now we're doing diagonal checks near our candidate */                  /* now we're doing diagonal checks near our candidate */
   
                 if (*iDirection) {              /* checking if anything found */  
334                          bDirection = *iDirection;                          bDirection = *iDirection;
335                    if (*iDirection) {              /* checking if anything found */
336                          *iDirection = 0;                          *iDirection = 0;
337                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
338                          if (bDirection & 3) {   /* our candidate is left or right */                          if (bDirection & 3) {   /* our candidate is left or right */
# Line 346  Line 346 
346                          x = data->currentMV->x; y = data->currentMV->y;                          x = data->currentMV->x; y = data->currentMV->y;
347                  }                  }
348          }          }
349          while (*iDirection);          while (bDirection);
350  }  }
351    
352  void  void
353  xvid_me_SubpelRefine(const SearchData * const data, CheckFunc * const CheckCandidate)  xvid_me_SubpelRefine(VECTOR centerMV, SearchData * const data, CheckFunc * const CheckCandidate, int dir)
354  {  {
355  /* Do a half-pel or q-pel refinement */  /* Do a half-pel or q-pel refinement */
         const VECTOR centerMV = data->qpel_precision ? *data->currentQMV : *data->currentMV;  
356    
357          CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x, centerMV.y - 1, dir);
358          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y - 1, dir);
359          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y, dir);
360          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, 0);          CHECK_CANDIDATE(centerMV.x + 1, centerMV.y + 1, dir);
361          CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, 0);          CHECK_CANDIDATE(centerMV.x, centerMV.y + 1, dir);
362          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, 0);          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y + 1, dir);
363          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, 0);          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y, dir);
364          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, 0);          CHECK_CANDIDATE(centerMV.x - 1, centerMV.y - 1, dir);
365    }
366    
367    #define CHECK_CANDIDATE_2ndBEST(X, Y, DIR) { \
368            *data->iMinSAD = s_best2; \
369            CheckCandidate((X),(Y), data, direction); \
370            if (data->iMinSAD[0] < s_best) { \
371                    s_best2 = s_best; \
372                    s_best = data->iMinSAD[0]; \
373                    v_best2 = v_best; \
374                    v_best.x = X; v_best.y = Y; \
375                    dir = DIR; \
376            } else if (data->iMinSAD[0] < s_best2) { \
377                    s_best2 = data->iMinSAD[0]; \
378                    v_best2.x = X; v_best2.y = Y; \
379            } \
380    }
381    
382    void
383    FullRefine_Fast(SearchData * data, CheckFunc * CheckCandidate, int direction)
384    {
385    /* Do a fast h-pel and then q-pel refinement */
386    
387            int32_t s_best = data->iMinSAD[0], s_best2 = 256*4096;
388            VECTOR v_best, v_best2;
389            int dir = 0, xo2, yo2, best_halfpel, b_cbp;
390    
391            int xo = 2*data->currentMV[0].x, yo = 2*data->currentMV[0].y;
392    
393            data->currentQMV[0].x = v_best.x = xo;
394            data->currentQMV[0].y = v_best.y = yo;
395    
396            data->qpel_precision = 1;
397    
398            /* halfpel refinement: check 8 neighbours, but keep the second best SAD as well */
399            CHECK_CANDIDATE_2ndBEST(xo - 2, yo,             1+16+64);
400            CHECK_CANDIDATE_2ndBEST(xo + 2, yo,             2+32+128);
401            CHECK_CANDIDATE_2ndBEST(xo,             yo - 2, 4+16+32);
402            CHECK_CANDIDATE_2ndBEST(xo,             yo + 2, 8+64+128);
403            CHECK_CANDIDATE_2ndBEST(xo - 2, yo - 2, 1+4+16+32+64);
404            CHECK_CANDIDATE_2ndBEST(xo + 2, yo - 2, 2+4+16+32+128);
405            CHECK_CANDIDATE_2ndBEST(xo - 2, yo + 2, 1+8+16+64+128);
406            CHECK_CANDIDATE_2ndBEST(xo + 2, yo + 2, 2+8+32+64+128);
407    
408            xo = v_best.x; yo = v_best.y, b_cbp = data->cbp[0];
409    
410            /* we need all 8 neighbours *of best hpel position found above* checked for 2nd best
411                    let's check the missing ones */
412    
413            /* on rare occasions, 1st best and 2nd best are far away, and 2nd best is not 1st best's neighbour.
414                    to simplify stuff, we'll forget that evil 2nd best and make a full search for a new 2nd best */
415            /* todo. we should check the missing neighbours first, maybe they'll give us 2nd best which is even better
416                    than the infamous one. in that case, we will not have to re-check the other neighbours */
417    
418            if (abs(v_best.x - v_best2.x) > 2 || abs(v_best.y - v_best2.y) > 2) { /* v_best2 is useless */
419                    data->iMinSAD[0] = 256*4096;
420                    dir = ~0; /* all */
421            } else {
422                    data->iMinSAD[0] = s_best2;
423                    data->currentQMV[0] = v_best2;
424            }
425    
426            if (dir & 1) CHECK_CANDIDATE(   xo - 2, yo,             direction);
427            if (dir & 2) CHECK_CANDIDATE(   xo + 2, yo,             direction);
428            if (dir & 4) CHECK_CANDIDATE(   xo,             yo - 2, direction);
429            if (dir & 8) CHECK_CANDIDATE(   xo,             yo + 2, direction);
430            if (dir & 16) CHECK_CANDIDATE(  xo - 2, yo - 2, direction);
431            if (dir & 32) CHECK_CANDIDATE(  xo + 2, yo - 2, direction);
432            if (dir & 64) CHECK_CANDIDATE(  xo - 2, yo + 2, direction);
433            if (dir & 128) CHECK_CANDIDATE( xo + 2, yo + 2, direction);
434    
435            /* read the position of 2nd best */
436            v_best2 = data->currentQMV[0];
437    
438            /* after second_best has been found, go back to best vector */
439    
440            data->currentQMV[0].x = xo;
441            data->currentQMV[0].y = yo;
442            data->cbp[0] = b_cbp;
443    
444            data->currentMV[0].x = xo/2;
445            data->currentMV[0].y = yo/2;
446            data->iMinSAD[0] = best_halfpel = s_best;
447    
448            xo2 = v_best2.x;
449            yo2 = v_best2.y;
450            s_best2 = 256*4096;
451    
452            if (yo == yo2) {
453                    CHECK_CANDIDATE_2ndBEST((xo+xo2)>>1, yo, 0);
454                    CHECK_CANDIDATE_2ndBEST(xo, yo-1, 0);
455                    CHECK_CANDIDATE_2ndBEST(xo, yo+1, 0);
456                    data->currentQMV[0] = v_best;
457                    data->iMinSAD[0] = s_best;
458    
459                    if(best_halfpel <= s_best2) return;
460    
461                    if(data->currentQMV[0].x == v_best2.x) {
462                            CHECK_CANDIDATE((xo+xo2)>>1, yo-1, direction);
463                            CHECK_CANDIDATE((xo+xo2)>>1, yo+1, direction);
464                    } else {
465                            CHECK_CANDIDATE((xo+xo2)>>1,
466                                    (data->currentQMV[0].x == xo) ? data->currentQMV[0].y : v_best2.y, direction);
467                    }
468                    return;
469  }  }
470    
471            if (xo == xo2) {
472                    CHECK_CANDIDATE_2ndBEST(xo, (yo+yo2)>>1, 0);
473                    CHECK_CANDIDATE_2ndBEST(xo-1, yo, 0);
474                    CHECK_CANDIDATE_2ndBEST(xo+1, yo, 0);
475                    data->currentQMV[0] = v_best;
476                    data->iMinSAD[0] = s_best;
477    
478                    if(best_halfpel <= s_best2) return;
479    
480                    if(data->currentQMV[0].y == v_best2.y) {
481                            CHECK_CANDIDATE(xo-1, (yo+yo2)>>1, direction);
482                            CHECK_CANDIDATE(xo+1, (yo+yo2)>>1, direction);
483                    } else {
484                            CHECK_CANDIDATE((data->currentQMV[0].y == yo) ? data->currentQMV[0].x : v_best2.x, (yo+yo2)>>1, direction);
485                    }
486                    return;
487            }
488    
489            CHECK_CANDIDATE_2ndBEST(xo, (yo+yo2)>>1, 0);
490            CHECK_CANDIDATE_2ndBEST((xo+xo2)>>1, yo, 0);
491            data->currentQMV[0] = v_best;
492            data->iMinSAD[0] = s_best;
493    
494            if(best_halfpel <= s_best2) return;
495    
496            CHECK_CANDIDATE((xo+xo2)>>1, (yo+yo2)>>1, direction);
497    
498    }

Legend:
Removed from v.1.1.2.1  
changed lines
  Added in v.1.6

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