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

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

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

revision 1215, Wed Nov 19 12:24:25 2003 UTC revision 1275, Thu Dec 18 13:26:48 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: estimation_bvop.c,v 1.1.2.5 2003-11-19 12:24:25 syskin Exp $   * $Id: estimation_bvop.c,v 1.1.2.10 2003-12-18 13:26:48 Isibaar Exp $
25   *   *
26   ****************************************************************************/   ****************************************************************************/
27    
# Line 39  Line 39 
39  #include "sad.h"  #include "sad.h"
40  #include "motion_inlines.h"  #include "motion_inlines.h"
41    
   
42  static int32_t  static int32_t
43  ChromaSAD2(const int fx, const int fy, const int bx, const int by,  ChromaSAD2(const int fx, const int fy, const int bx, const int by,
44                          SearchData * const data)                          SearchData * const data)
# Line 298  Line 297 
297          }          }
298  }  }
299    
300    void
301    CheckCandidate16no4v_qpel(const int x, const int y, SearchData * const data, const unsigned int Direction)
302    {
303            int32_t sad, xc, yc;
304            const uint8_t * Reference;
305            uint32_t t;
306    
307            if ( (x > data->max_dx) || ( x < data->min_dx)
308                    || (y > data->max_dy) || (y < data->min_dy) ) return;
309    
310            if (data->rrv && (!(x&1) && x !=0) | (!(y&1) && y !=0) ) return; /* non-zero even value */
311    
312            Reference = xvid_me_interpolate16x16qpel(x, y, 0, data);
313    
314            xc = x/2; yc = y/2;
315            t = d_mv_bits(x, y, data->predMV, data->iFcode,
316                                            data->qpel^data->qpel_precision, data->rrv);
317    
318            sad = sad16(data->Cur, Reference, data->iEdgedWidth, 256*4096);
319            sad += (data->lambda16 * t * sad)>>10;
320    
321            if (data->chroma && sad < *data->iMinSAD)
322                    sad += xvid_me_ChromaSAD((xc >> 1) + roundtab_79[xc & 0x3],
323                                                                    (yc >> 1) + roundtab_79[yc & 0x3], data);
324    
325            if (sad < *(data->iMinSAD)) {
326                    data->iMinSAD2 = *(data->iMinSAD);
327                    data->currentQMV2.x = data->currentQMV->x;
328                    data->currentQMV2.y = data->currentQMV->y;
329    
330                    data->iMinSAD[0] = sad;
331                    data->currentQMV[0].x = x; data->currentQMV[0].y = y;
332            } else if (sad < data->iMinSAD2) {
333                    data->iMinSAD2 = sad;
334                    data->currentQMV2.x = x; data->currentQMV2.y = y;
335            }
336    }
337    
338  static __inline VECTOR  static __inline VECTOR
339  ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)  ChoosePred(const MACROBLOCK * const pMB, const uint32_t mode)
340  {  {
# Line 361  Line 398 
398    
399          int i;          int i;
400          VECTOR pmv[7];          VECTOR pmv[7];
         MainSearchFunc *MainSearchPtr;  
401          *Data->iMinSAD = MV_MAX_ERROR;          *Data->iMinSAD = MV_MAX_ERROR;
402          Data->iFcode = iFcode;          Data->iFcode = iFcode;
403          Data->qpel_precision = 0;          Data->qpel_precision = 0;
404          Data->chromaX = Data->chromaX = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */          Data->chromaX = Data->chromaY = Data->chromaSAD = 256*4096; /* reset chroma-sad cache */
405    
406          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[0] = pRef->y + (x + Data->iEdgedWidth*y) * 16;
407          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;          Data->RefP[2] = pRefH + (x + Data->iEdgedWidth*y) * 16;
# Line 391  Line 427 
427                  if (!vector_repeats(pmv, i) )                  if (!vector_repeats(pmv, i) )
428                          CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i);                          CheckCandidate16no4v(pmv[i].x, pmv[i].y, Data, i);
429    
430            if (*Data->iMinSAD > 512) {
431                    unsigned int mask = make_mask(pmv, 7, Data->dir);
432    
433                    MainSearchFunc *MainSearchPtr;
434          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
435          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;          else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
436                  else MainSearchPtr = xvid_me_DiamondSearch;                  else MainSearchPtr = xvid_me_DiamondSearch;
437    
         if (*Data->iMinSAD > 512) {  
                 unsigned int mask = make_mask(pmv, 7, Data->dir);  
438                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v);                  MainSearchPtr(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate16no4v);
439          }          }
440    
# Line 408  Line 446 
446                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
447                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
448                                          pParam->width, pParam->height, iFcode, 2, 0);                                          pParam->width, pParam->height, iFcode, 2, 0);
449    
450                    if (MotionFlags & XVID_ME_QUARTERPELREFINE16) {
451                            if(MotionFlags & XVID_ME_FASTREFINE16)
452                                    SubpelRefine_Fast(Data, CheckCandidate16no4v_qpel);
453                            else
454                  xvid_me_SubpelRefine(Data, CheckCandidate16no4v);                  xvid_me_SubpelRefine(Data, CheckCandidate16no4v);
455          }          }
456            }
457    
458          /* three bits are needed to code backward mode. four for forward */          /* three bits are needed to code backward mode. four for forward */
459    
# Line 446  Line 490 
490                                  const uint32_t x, const uint32_t y,                                  const uint32_t x, const uint32_t y,
491                                  const SearchData * const Data)                                  const SearchData * const Data)
492  {  {
493            int k;
494    
495            if (!Data->chroma) {
496          int dx = 0, dy = 0, b_dx = 0, b_dy = 0;          int dx = 0, dy = 0, b_dx = 0, b_dy = 0;
497          int32_t sum;          int32_t sum;
         int k;  
498          const uint32_t stride = Data->iEdgedWidth/2;          const uint32_t stride = Data->iEdgedWidth/2;
499          /* this is not full chroma compensation, only it's fullpel approximation. should work though */          /* this is not full chroma compensation, only it's fullpel approximation. should work though */
500    
# Line 476  Line 522 
522                                          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,
523                                          stride);                                          stride);
524    
525          if (sum < MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) {                  if (sum >= MAX_CHROMA_SAD_FOR_SKIP * (int)Data->iQuant) return; /* no skip */
526            }
527    
528            /* skip */
529                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */                  pMB->mode = MODE_DIRECT_NONE_MV; /* skipped */
530                  for (k = 0; k < 4; k++) {                  for (k = 0; k < 4; k++) {
531                          pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k];                          pMB->qmvs[k] = pMB->mvs[k] = Data->directmvF[k];
532                          pMB->b_qmvs[k] = pMB->b_mvs[k] =  Data->directmvB[k];                          pMB->b_qmvs[k] = pMB->b_mvs[k] =  Data->directmvB[k];
533                  }                  }
534          }          }
 }  
535    
536  static uint32_t  static uint32_t
537  SearchDirect(const IMAGE * const f_Ref,  SearchDirect(const IMAGE * const f_Ref,
# Line 563  Line 611 
611          /* initial (fast) skip decision */          /* initial (fast) skip decision */
612          if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {          if (*Data->iMinSAD < (int)Data->iQuant * INITIAL_SKIP_THRESH * (Data->chroma?3:2)) {
613                  /* possible skip */                  /* possible skip */
                 if (Data->chroma) {  
                         pMB->mode = MODE_DIRECT_NONE_MV;  
                         return *Data->iMinSAD; /* skip. */  
                 } else {  
614                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);                          SkipDecisionB(pCur, f_Ref, b_Ref, pMB, x, y, Data);
615                          if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skip. */                  if (pMB->mode == MODE_DIRECT_NONE_MV) return *Data->iMinSAD; /* skipped */
                 }  
616          }          }
617    
618          *Data->iMinSAD += Data->lambda16;          *Data->iMinSAD += Data->lambda16;
619          skip_sad = *Data->iMinSAD;          skip_sad = *Data->iMinSAD;
620    
621          /*          if (!(MotionFlags & XVID_ME_SKIP_DELTASEARCH)) {
          * DIRECT MODE DELTA VECTOR SEARCH.  
          * This has to be made more effective, but at the moment I'm happy it's running at all  
          */  
   
622          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;          if (MotionFlags & XVID_ME_USESQUARES16) MainSearchPtr = xvid_me_SquareSearch;
623                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;                  else if (MotionFlags & XVID_ME_ADVANCEDDIAMOND16) MainSearchPtr = xvid_me_AdvDiamondSearch;
624                          else MainSearchPtr = xvid_me_DiamondSearch;                          else MainSearchPtr = xvid_me_DiamondSearch;
# Line 587  Line 626 
626          MainSearchPtr(0, 0, Data, 255, CheckCandidate);          MainSearchPtr(0, 0, Data, 255, CheckCandidate);
627    
628          xvid_me_SubpelRefine(Data, CheckCandidate);          xvid_me_SubpelRefine(Data, CheckCandidate);
629            }
630    
631          *best_sad = *Data->iMinSAD;          *best_sad = *Data->iMinSAD;
632    
# Line 671  Line 711 
711  {  {
712          int i, j;          int i, j;
713          int b_range[4], f_range[4];          int b_range[4], f_range[4];
714            int threshA = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 0 : 500;
715            int threshB = (MotionFlags & XVID_ME_FAST_MODEINTERPOLATE) ? 0 : 300;
716    
717          Data->qpel_precision = 0;          Data->qpel_precision = 0;
718          *Data->iMinSAD = 4096*256;          *Data->iMinSAD = 4096*256;
# Line 739  Line 781 
781    
782          /* qpel refinement */          /* qpel refinement */
783          if (Data->qpel) {          if (Data->qpel) {
784                  if (*Data->iMinSAD > *best_sad + 500) return;                  if (*Data->iMinSAD > *best_sad + threshA) return;
785                  Data->qpel_precision = 1;                  Data->qpel_precision = 1;
786                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0);                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, fcode, 2, 0);
787    
# Line 748  Line 790 
790                  Data->currentQMV[1].x = 2 * Data->currentMV[1].x;                  Data->currentQMV[1].x = 2 * Data->currentMV[1].x;
791                  Data->currentQMV[1].y = 2 * Data->currentMV[1].y;                  Data->currentQMV[1].y = 2 * Data->currentMV[1].y;
792                  SubpelRefine_dir(Data, CheckCandidateInt, 1);                  SubpelRefine_dir(Data, CheckCandidateInt, 1);
793                  if (*Data->iMinSAD > *best_sad + 300) return;                  if (*Data->iMinSAD > *best_sad + threshB) return;
794                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0);                  get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4, pParam->width, pParam->height, bcode, 2, 0);
795                  SubpelRefine_dir(Data, CheckCandidateInt, 2);                  SubpelRefine_dir(Data, CheckCandidateInt, 2);
796          }          }
797    
798          *Data->iMinSAD += (2+3) * Data->lambda16; /* two bits are needed to code interpolate mode. */          *Data->iMinSAD += 2 * Data->lambda16; /* two bits are needed to code interpolate mode. */
799    
800          if (*Data->iMinSAD < *best_sad) {          if (*Data->iMinSAD < *best_sad) {
801                  *best_sad = *Data->iMinSAD;                  *best_sad = *Data->iMinSAD;
# Line 799  Line 841 
841          uint32_t skip_sad;          uint32_t skip_sad;
842    
843          const MACROBLOCK * const b_mbs = b_reference->mbs;          const MACROBLOCK * const b_mbs = b_reference->mbs;
844            MACROBLOCK *const pMBs = frame->mbs;
845    
846          VECTOR f_predMV, b_predMV;          VECTOR f_predMV, b_predMV;
847    
# Line 811  Line 854 
854          memset(&Data, 0, sizeof(SearchData));          memset(&Data, 0, sizeof(SearchData));
855    
856          Data.iEdgedWidth = pParam->edged_width;          Data.iEdgedWidth = pParam->edged_width;
         Data.lambda16 = xvid_me_lambda_vec16[MAX(frame->quant-2, 2)];  
857          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;          Data.qpel = pParam->vol_flags & XVID_VOL_QUARTERPEL ? 1 : 0;
858          Data.rounding = 0;          Data.rounding = 0;
859          Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;          Data.chroma = frame->motion_flags & XVID_ME_CHROMA_BVOP;
# Line 827  Line 869 
869                  for (i = 0; i < pParam->mb_width; i++) {                  for (i = 0; i < pParam->mb_width; i++) {
870                          MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;                          MACROBLOCK * const pMB = frame->mbs + i + j * pParam->mb_width;
871                          const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;                          const MACROBLOCK * const b_mb = b_mbs + i + j * pParam->mb_width;
872                            int interpol_search;
873                            int bf_search = 0;
874                            int bf_thresh1, bf_thresh2;
875    
876  /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */  /* special case, if collocated block is SKIPed in P-VOP: encoding is forward (0,0), cpb=0 without further ado */
877                          if (b_reference->coding_type != S_VOP)                          if (b_reference->coding_type != S_VOP)
878                                  if (b_mb->mode == MODE_NOT_CODED) {                                  if (b_mb->mode == MODE_NOT_CODED) {
879                                          pMB->mode = MODE_NOT_CODED;                                          pMB->mode = MODE_NOT_CODED;
880                                          pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;                                          pMB->mvs[0] = pMB->b_mvs[0] = zeroMV;
881                                            pMB->sad16 = 0;
882                                          continue;                                          continue;
883                                  }                                  }
884    
885                            Data.lambda16 = xvid_me_lambda_vec16[b_mb->quant];
886    
887                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;                          Data.Cur = frame->image.y + (j * Data.iEdgedWidth + i) * 16;
888                          Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;                          Data.CurU = frame->image.u + (j * Data.iEdgedWidth/2 + i) * 8;
889                          Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;                          Data.CurV = frame->image.v + (j * Data.iEdgedWidth/2 + i) * 8;
# Line 853  Line 901 
901                                                                          &best_sad,                                                                          &best_sad,
902                                                                          &Data);                                                                          &Data);
903    
904                          if (pMB->mode == MODE_DIRECT_NONE_MV) continue;                          if (pMB->mode == MODE_DIRECT_NONE_MV) {
905                                    pMB->sad16 = best_sad;
906                                    continue;
907                            }
908    
909                            if (frame->motion_flags & XVID_ME_BFRAME_EARLYSTOP) {
910    
911                                    if(i > 0 && j > 0 && i < pParam->mb_width) {
912                                            bf_thresh1 = ((&pMBs[(i-1) + j * pParam->mb_width])->sad16 +
913                                                                    (&pMBs[i + (j-1) * pParam->mb_width])->sad16 +
914                                                                    (&pMBs[(i+1) + (j-1) * pParam->mb_width])->sad16) / 3;
915    
916                                            if (((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_FORWARD) &&
917                                                    ((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_BACKWARD) &&
918                                                    ((&pMBs[(i-1) + j * pParam->mb_width])->mode != MODE_INTERPOLATE))
919                                                            bf_search++;
920    
921                                            if (((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) &&
922                                                    ((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) &&
923                                                    ((&pMBs[i + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE))
924                                                            bf_search++;
925    
926                                            if (((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_FORWARD) &&
927                                                    ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_BACKWARD) &&
928                                                    ((&pMBs[(i + 1) + (j - 1) * pParam->mb_width])->mode != MODE_INTERPOLATE))
929                                                    bf_search++;
930                                    }
931    
932                                    if ((best_sad < bf_thresh1) && (bf_search == 3))
933                                            continue;
934                            }
935    
936                          /* forward search */                          /* forward search */
937                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchBF(f_ref, f_refH->y, f_refV->y, f_refHV->y,
# Line 872  Line 950 
950                                                  MODE_BACKWARD, &Data);                                                  MODE_BACKWARD, &Data);
951    
952                          /* 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 */
953                            if (frame->motion_flags & XVID_ME_FAST_MODEINTERPOLATE)
954                                    interpol_search = (best_sad > Data.iQuant * 3 * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2));
955                            else
956                                    interpol_search = 1;
957    
958                            if (interpol_search) {
959                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,                          SearchInterpolate(f_ref, f_refH->y, f_refV->y, f_refHV->y,
960                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,                                                  b_ref, b_refH->y, b_refV->y, b_refHV->y,
961                                                  i, j,                                                  i, j,
# Line 881  Line 965 
965                                                  &f_predMV, &b_predMV,                                                  &f_predMV, &b_predMV,
966                                                  pMB, &best_sad,                                                  pMB, &best_sad,
967                                                  &Data);                                                  &Data);
968                            }
969    
970                          /* final skip decision */                          /* final skip decision */
971                          if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * 2)                          if ( (skip_sad < Data.iQuant * MAX_SAD00_FOR_SKIP * (Data.chroma ? 3:2) )
972                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )                                          && ((100*best_sad)/(skip_sad+1) > FINAL_SKIP_THRESH) )
973    
974                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);                                  SkipDecisionB(&frame->image, f_ref, b_ref, pMB, i, j, &Data);
975    
976                          switch (pMB->mode) {                          switch (pMB->mode) {
977                                  case MODE_FORWARD:                                  case MODE_FORWARD:
978                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
979                                            pMB->sad16 = best_sad;
980                                          break;                                          break;
981                                  case MODE_BACKWARD:                                  case MODE_BACKWARD:
982                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
983                                            pMB->sad16 = best_sad;
984                                          break;                                          break;
985                                  case MODE_INTERPOLATE:                                  case MODE_INTERPOLATE:
986                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];                                          f_predMV = Data.qpel ? pMB->qmvs[0] : pMB->mvs[0];
987                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];                                          b_predMV = Data.qpel ? pMB->b_qmvs[0] : pMB->b_mvs[0];
988                                            pMB->sad16 = best_sad;
989                                          break;                                          break;
990                                  default:                                  default:
991                                            pMB->sad16 = best_sad;
992                                          break;                                          break;
993                          }                          }
994                  }                  }

Legend:
Removed from v.1215  
changed lines
  Added in v.1275

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