[MN-dev] [mndiff]: r61 - in trunk/2010: 2010.cpp 2_crypt.cpp 2_file.cpp 2_nag.cpp 2_vc.cpp

michael subversion at mplayerhq.hu
Wed Jul 4 10:44:08 CEST 2007


Author: michael
Date: Wed Jul  4 10:44:08 2007
New Revision: 61

Log:
version from 1999


Modified:
   trunk/2010/2010.cpp
   trunk/2010/2_crypt.cpp
   trunk/2010/2_file.cpp
   trunk/2010/2_nag.cpp
   trunk/2010/2_vc.cpp

Modified: trunk/2010/2010.cpp
==============================================================================
--- trunk/2010/2010.cpp	(original)
+++ trunk/2010/2010.cpp	Wed Jul  4 10:44:08 2007
@@ -23,6 +23,7 @@
 #include "2_crypt.h"
 #include "2_71x6.h"
 #include "2_nag.h"
+#include "2_vc.h"
 
  extern volatile bool oneField;
  extern int iVSmooth;
@@ -38,7 +39,7 @@
  extern int showCont;
  extern CRYPTSTD cryptStd;
  extern TVSTD TVStd;
- extern bool iState;
+ extern int iState;
  extern bool helpState;
  extern bool allowDrop;
  extern int reqPage, reqSubPage, reqChanNum;
@@ -132,6 +133,7 @@ int error(error_code e){
                             "         -? / -h (This stuff)\n");
                      break;
   default          : printg_t("\nUnknown error!");
+                     printf("error %d\n", int(e));
                      break;
  }
 
@@ -140,6 +142,7 @@ int error(error_code e){
 
 void exitt(void){
  close_hw();
+ saveVCCache();   
 }
 
 int main(int argc, char **argv){
@@ -335,7 +338,7 @@ int main(int argc, char **argv){
 
    if(grabf==1 && TVStd!=TXTPAL) GrabBmp(), grabf=0;
 
-   for(int k=0; k<delay*1000000; k++);
+   for(int k=0; k<delay*10000000; k++);
 
    copy_vidbuffer();
 
@@ -418,7 +421,7 @@ int main(int argc, char **argv){
                        case 's'  : menuLevel=mLTVStd, helpState=false;
                        break;
                        case 'i'  :
-                       case 'I'  : iState= !iState;
+                       case 'I'  : iState++; iState%=ISTATES;
                        break;
                        case 'v'  :
                        case 'V'  : iVSmooth++; iVSmooth%=3;
@@ -536,6 +539,6 @@ int main(int argc, char **argv){
      }
    }
  }
- 
+
  exit(0);
 }

Modified: trunk/2010/2_crypt.cpp
==============================================================================
--- trunk/2010/2_crypt.cpp	(original)
+++ trunk/2010/2_crypt.cpp	Wed Jul  4 10:44:08 2007
@@ -93,6 +93,9 @@ static void Decomb(int q){
 // for(int i=0; i<wndx; i++) temp[i]=128<<8;
  for(int i=0; i<wndx; i++) temp[i]=0;
 
+// SetDecombInc((some<<4) + (some<<20));
+ SetDecombInc( 128 + (128<<16) );
+
  if(q==1){
   int linep= stride*256;
   if(o) linep+=vgax<<1;

Modified: trunk/2010/2_file.cpp
==============================================================================
--- trunk/2010/2_file.cpp	(original)
+++ trunk/2010/2_file.cpp	Wed Jul  4 10:44:08 2007
@@ -7,7 +7,7 @@
 #include "2_file.h"
 #include "2_hw.h"
 
- extern int vgax, vgay, wndx, wndy;
+ extern int vgax, vgay, wndx, wndy, outy;
  extern int fysize;
  extern byte font[4096];
  extern VID2MEMBUF *actVid2MemBufp;
@@ -73,11 +73,10 @@ void GrabBmp(void){
        const int v = char(actVid2MemBufp->b[ ((j + i*vgax)<<1) + 2 ]);
        const int y2=      actVid2MemBufp->b[ ((j + i*vgax)<<1) + 3 ];
        color c[2];
-       c[0].init(y1, u, v, 1);
-       c[1].init(y2, u, v, 1);
+       c[0].init(y1, u, v, 0);
+       c[1].init(y2, u, v, 0);
        byte bgr2[6]={ c[0].col.b, c[0].col.g, c[0].col.r,
                       c[1].col.b, c[1].col.g, c[1].col.r };
-
        fwrite(bgr2, 1, 6, fg);
      }
    } 
@@ -92,12 +91,15 @@ void GrabRaw(void){
  char grab_name[16];
  struct time ti;
 
+ static int iCounter= ti.ti_hour;
+
  gettime(&ti);
- sprintf(grab_name, "G%02d%02d%02d.RAW", ti.ti_hour, ti.ti_min, ti.ti_sec);
+ sprintf(grab_name, "G%02d%02d%02d.RAW", iCounter%100, ti.ti_min, ti.ti_sec);
+ iCounter++;
 
  if((fg=fopen(grab_name, "ab"))==NULL && g_mode==0) error(FileWrite);
 
- fwrite(actVid2MemBufp->b, 1, vgax*wndy*2, fg);
+ fwrite(actVid2MemBufp->b, 1, vgax*outy*2, fg);
 
  fclose(fg);
 }
@@ -128,7 +130,7 @@ void ShowRaw(void){
 
     i++;
     i%=nRawDats;
-    memcpy(actVid2MemBufp->b, ppbRawDat[i], vgax*vgay*2);
+    memcpy(actVid2MemBufp->b, ppbRawDat[i], vgax*outy*2);
 }
 
 long fsize(FILE *f){

Modified: trunk/2010/2_nag.cpp
==============================================================================
--- trunk/2010/2_nag.cpp	(original)
+++ trunk/2010/2_nag.cpp	Wed Jul  4 10:44:08 2007
@@ -1,4 +1,4 @@
-//2010 0.1 Copyright (C) Michael Niedermayer 1998
+//2010 0.1 Copyright (C) Michael Niedermayer 1999
 
 //FIX search multiply (use deltas)
 

Modified: trunk/2010/2_vc.cpp
==============================================================================
--- trunk/2010/2_vc.cpp	(original)
+++ trunk/2010/2_vc.cpp	Wed Jul  4 10:44:08 2007
@@ -1,9 +1,10 @@
-//2010 0.1 Copyright (C) Michael Niedermayer 1998
+//2010 0.1 Copyright (C) Michael Niedermayer 1999
 
 #include <stdio.h>
 #include <time.h>
 #include <string.h>
 #include <math.h>
+#include <stdlib.h>
 #include "2_all.h"
 #include "2_crypt.h"
 #include "2_vc.h"
@@ -12,7 +13,11 @@
 #include "2_vc_a.h"
 #include "2_hw_mem.h"
 #include "2010.h"
-                      
+
+//#define DEBUG_CUTP
+
+//#define DEBUG
+
 #define FREQ_SMP       2.1e7
 #define POINT_LAND     765.0                         
 #define NO_POINT_LAND1 150.0             // GUESSED and ok 
@@ -28,12 +33,24 @@
                           * (POINT_LAND + NO_POINT_LAND), 1.0) * PI * 2.0)
 
 
+#define NOT_VC_THRESHOLD 20   // below 10 and above 90 bad
+#define HEADER_EMPTY 0
+#define HEADER_FINAL 15
+#define GOOD_CACHE_THRESHOLD 5
+#define FINAL_CACHE_THRESHOLD 4
+#define SCORE_THRESHOLD 3
+#define MAX_EQ_SETS 14
+#define nTEST_LINES 10
+#define MAX_BAD_CACHES 30000
+#define MAX_GOOD_CACHES 70000
+
 #define STEP_START 16
 #define STEP_END 2
 #define DWN_SMP 1
 #define MAX_DRIFT 2
 #define DRIFT_TAB {1200, 400, 0, 400, 1200}
 #define PHASE_CMP_STEP 3 
+#define BAD_MEAN_THRESHOLD 17
 #define BAD_THRESHOLD1 0.8
 #define BAD_THRESHOLD2 200
 #define EDGE_LIMIT 70
@@ -53,14 +70,16 @@ extern VID2MEMBUF *actVid2MemBufp;
 extern volatile int scales_x, scalee_x;
 extern volatile int scales_y, scalee_y;
 extern bool showPoints;
-extern bool iState;
+extern int iState;
 extern int yuvMode;
 extern int some;
 extern bool mmx;
 extern int infoPosX;
 extern int infoPosY;
 extern long uclockWaste;
+extern bool drop;
 
+static void loadVCCache(void);
 
 static inline float atan3(const float f1, const float f2){
   float out;
@@ -75,6 +94,526 @@ static inline float atan3(const float f1
   return out;
 }
 
+static pTestLine[nTEST_LINES]= {43, 62, 84, 109, 122, 147, 160, 181, 205, 238};
+static CutPLut *ppCutPLut[nTEST_LINES];
+static int nGoodCaches=0;
+static int nBadCaches=0;
+static int nFieldsCached=0;
+static int nFieldsUnCached=0;
+static int nFields=0;
+static int iCurrentLUTScore=0;
+static int iCurrentUsed=0;
+static CutPCache *pGoodCutPCache=NULL;
+static CutPCache *pBadCutPCache=NULL;
+
+static void delFromCache(CutPCache *pOld){
+
+    if(pOld->nUsed<GOOD_CACHE_THRESHOLD) nBadCaches--;
+    else                                 nGoodCaches--;
+
+    if(pOld==pBadCutPCache)
+        pBadCutPCache=pOld->pNext;
+    if(pOld==pGoodCutPCache)
+        pGoodCutPCache=pOld->pNext;
+
+    pOld->pNext->pPrev= pOld->pPrev;
+    pOld->pPrev->pNext= pOld->pNext;
+
+    if(pOld->pNext == pOld)
+        {
+        if( pBadCutPCache == pOld )
+            pBadCutPCache= NULL;
+        else if( pGoodCutPCache == pOld )
+            pGoodCutPCache= NULL;
+        else error(error_code(-76));               
+        }
+}
+
+static void addCutPsToCache(CutPCache *pCutPCache, int *piCutP){
+
+    static byte *pbCScratchPad= new byte[ (MAX_EQ_SETS+1)*wndy+10 ];
+
+    int xIn=0, xOut=0;
+    int nInBytes=0;
+    int nInHeaders=0;
+    int xLastOutHeader=-1;
+    int nOutHeaders=-1;
+    int iLastOutHeader=-1;
+    for(int line=0; line<wndy; line++)
+        {
+        if(nInHeaders==0 && pCutPCache->pbCCutP!=NULL)
+            {
+            nInHeaders= (pCutPCache->pbCCutP[xIn]>>4) + 1;
+            nInBytes= pCutPCache->pbCCutP[xIn] & 15;
+            xIn++;
+            }
+
+        bool bOldGood=true;
+        bool bGood=true;
+        if(nInBytes!=HEADER_FINAL)
+            {
+            bOldGood=false;
+            bGood=false;
+            int nCurrentCutPs=1;
+            for(int i=0; i<nInBytes; i++)
+                {
+                if(int(pCutPCache->pbCCutP[xIn+i]) == piCutP[line])
+                    {
+                    nCurrentCutPs++;
+                    if(nCurrentCutPs>=FINAL_CACHE_THRESHOLD)
+                        {
+                        bGood=true;
+                        }
+                    }
+                }
+            }
+        int nOutBytes=nInBytes;
+        if(!bGood && piCutP[line]!=-1) nOutBytes++;
+        int xKill=-1;
+        if(!bGood && nOutBytes>MAX_EQ_SETS)
+            {
+            nOutBytes=MAX_EQ_SETS;
+            xKill=random() % MAX_EQ_SETS;
+            }
+        if(bGood) nOutBytes=1;
+
+        int iOutHeader=nOutBytes;
+        if(bGood) iOutHeader=HEADER_FINAL;
+
+        if(iLastOutHeader == iOutHeader && nOutHeaders<16)
+            nOutHeaders++;
+        else
+            {
+            if(xLastOutHeader>=0)
+                pbCScratchPad[xLastOutHeader]=
+                    ((nOutHeaders-1)<<4) + iLastOutHeader;
+            iLastOutHeader= iOutHeader;
+            nOutHeaders=1;
+            xLastOutHeader=xOut;
+            xOut++;
+            }
+
+
+        if(bOldGood)
+            {
+            pbCScratchPad[xOut]= pCutPCache->pbCCutP[xIn];
+            xOut++;
+            }
+        else if(bGood)
+            {
+            pbCScratchPad[xOut]= piCutP[line];
+            xOut++;
+            }
+        else
+            {
+            bool bOut=false;
+            if(piCutP[line]==-1) bOut=true;
+            for(int i=0; i<nInBytes; i++)
+                {
+                if(i==xKill) continue;
+                if(piCutP[line] < pCutPCache->pbCCutP[xIn+i] && !bOut)
+                    {
+                    pbCScratchPad[xOut]= piCutP[line];
+                    xOut++;
+                    bOut=true;
+                    }
+                pbCScratchPad[xOut]= pCutPCache->pbCCutP[xIn+i];
+                xOut++;
+                }
+            if(!bOut)
+                {
+                pbCScratchPad[xOut]= piCutP[line];
+                xOut++;
+                }
+            }
+
+
+        if(nInBytes!=HEADER_FINAL) xIn+=nInBytes;
+        else                       xIn++;
+
+        nInHeaders--;
+        }
+    pbCScratchPad[xLastOutHeader]= ((nOutHeaders-1)<<4) + iLastOutHeader;
+
+    delete [] pCutPCache->pbCCutP;
+    pCutPCache->pbCCutP= new byte[xOut];
+    pCutPCache->nbCCutP= xOut;
+    memcpy(pCutPCache->pbCCutP, pbCScratchPad, xOut);
+
+}
+
+static int *getBestCutPs(CutPCache *pCutPCache){
+    static int piCutP[600];
+    int x=0;
+    int nBytes=-1;
+    int nHeaders=0;
+    for(int line=0; line<wndy; line++)
+        {
+        if(nHeaders==0)
+            {
+            nHeaders= (pCutPCache->pbCCutP[x]>>4) + 1;
+            nBytes= pCutPCache->pbCCutP[x] & 15;
+            if(nBytes==HEADER_FINAL) nBytes=1;
+            x++;
+            }
+//        printf("x %d %d %d\n", nHeaders, nBytes, pCutPCache->nbCCutP);
+
+
+        int iCurrentCutP=0;     
+        int nCurrentCutPs=0;
+        int iBestCutP=0;
+        int nBestCutPs=0;
+        for(int i=0; i<nBytes; i++)
+            {
+//            printf("y %d %d\n", x, pCutPCache->pbCCutP[x]);
+            if(pCutPCache->pbCCutP[x] == iCurrentCutP)
+                {
+                nCurrentCutPs++;
+                }
+            else
+                {
+                nCurrentCutPs=1;
+                iCurrentCutP=pCutPCache->pbCCutP[x];
+                }
+
+            if(nCurrentCutPs>nBestCutPs)
+                {
+                nBestCutPs= nCurrentCutPs;
+                iBestCutP=  iCurrentCutP;
+                }
+            x++;
+            }
+//        printf("z %d %d\n", x, bBestCutP);
+        piCutP[line] =iBestCutP;
+
+        nHeaders--;
+        }
+
+    return piCutP;
+}
+
+static int addToLut(CutPCache *pNewCutPCache){
+    int *piCutP= getBestCutPs(pNewCutPCache);
+    int nTimesAdded=0;
+
+    for(int xTestLine=0; xTestLine<nTEST_LINES; xTestLine++)
+        {
+        int iTestLine= pTestLine[ xTestLine ];
+        int iDiff1= (byte)(piCutP[ iTestLine   ] - piCutP[ iTestLine-1 ]);
+        int iDiff2= (byte)(piCutP[ iTestLine+1 ] - piCutP[ iTestLine   ]);
+        if(iDiff1==0 && iDiff2==0) continue;  
+
+        nTimesAdded++;
+
+        int xDiff= (iDiff1<<8) + iDiff2;
+        CutPLut *pCutPLut= &ppCutPLut[ xTestLine ][ xDiff ];
+        while( pCutPLut->pNext!=NULL ) pCutPLut= pCutPLut->pNext;
+
+        if(pCutPLut->pCutPCache!=NULL)
+            {
+            pCutPLut->pNext= new CutPLut;
+            pCutPLut= pCutPLut->pNext;
+            }
+        pCutPLut->pNext=NULL;
+        pCutPLut->pCutPCache= pNewCutPCache;
+        }
+    return nTimesAdded;
+}
+
+static void delFromLut(CutPCache *pOldCutPCache){
+    int *piCutP= getBestCutPs(pOldCutPCache);
+    for(int xTestLine=0; xTestLine<nTEST_LINES; xTestLine++)
+        {
+        int iTestLine= pTestLine[ xTestLine ];
+        int iDiff1= (byte)(piCutP[ iTestLine   ] - piCutP[ iTestLine-1 ]);
+        int iDiff2= (byte)(piCutP[ iTestLine+1 ] - piCutP[ iTestLine   ]);
+//        if(iDiff1==0 && iDiff2==0) continue;  
+
+        int xDiff= (iDiff1<<8) + iDiff2;
+        CutPLut *pCutPLut= &ppCutPLut[ xTestLine ][ xDiff ];
+        CutPLut *pPrev=NULL;
+        while( pCutPLut!=NULL )
+            {
+            if(pCutPLut->pCutPCache==pOldCutPCache)
+                {
+                if(pPrev!=NULL)
+                    {
+                    pPrev->pNext=pCutPLut->pNext;
+                    delete pCutPLut;
+                    }
+                else
+                    {
+                    pCutPLut->pCutPCache=NULL;
+                    }
+                break;
+                }
+            pPrev=pCutPLut;
+            pCutPLut= pCutPLut->pNext;
+            }
+        }
+}
+
+static CutPCache *findInLutX(int *piCutP, CutPCache *pMergeCutPCache, bool bInc){
+    int iBestScore=0;
+    CutPCache *pBestCutPCache=NULL;
+    for(int xTestLine=0; xTestLine<nTEST_LINES; xTestLine++)
+        {
+        int iTestLine= pTestLine[ xTestLine ];
+
+        if(  piCutP[ iTestLine-1 ]==-1 || piCutP[ iTestLine  ]==-1
+           ||piCutP[ iTestLine+1 ]==-1                            ) continue;
+
+        int iDiff1= (byte)(piCutP[ iTestLine   ] - piCutP[ iTestLine-1 ]);
+        int iDiff2= (byte)(piCutP[ iTestLine+1 ] - piCutP[ iTestLine   ]);
+        for(int iDrift1=-1; iDrift1<2; iDrift1++)
+            {
+            int iTDiff1= (byte)(iDiff1 + iDrift1);
+            for(int iDrift2=-1; iDrift2<2; iDrift2++)
+                {
+                int iTDiff2= (byte)(iDiff2 + iDrift2);
+
+                int xDiff= (int(iTDiff1)<<8) + int(iTDiff2);
+                CutPLut *pCutPLut= &ppCutPLut[ xTestLine ][ xDiff ];
+                while( pCutPLut!=NULL && pCutPLut->pCutPCache!=NULL)
+                    {
+                    CutPLut *pNextCutPLut= pCutPLut->pNext;
+                    CutPCache *pCutPCache= pCutPLut->pCutPCache;
+
+                    if(bInc)
+                        {
+                        pCutPCache->iScore++;
+                        if(pCutPCache->iScore > iBestScore)
+                            {
+                            iBestScore=pCutPCache->iScore;
+                            pBestCutPCache=pCutPCache;
+                            }
+                        }
+                    else
+                        {
+                        if(   pCutPCache->iScore>=SCORE_THRESHOLD
+                           && pCutPCache!=pMergeCutPCache && 0)
+                            {
+
+                            addCutPsToCache(
+                                pMergeCutPCache,
+                                getBestCutPs(pCutPCache));
+
+                            delFromLut(pCutPCache);
+                            delFromCache(pCutPCache);
+                            delete [] pCutPCache->pbCCutP;
+                            delete pCutPCache;
+                            }
+                        else
+                            {
+                            pCutPCache->iScore=0;
+                            }
+                        }
+                    pCutPLut= pNextCutPLut;
+                    }
+                }
+            }
+        }
+    return pBestCutPCache;
+}
+
+static void mergeAndClean(int *piCutP, CutPCache *pBestCutPCache){
+    findInLutX(piCutP, pBestCutPCache, false);
+}
+
+static CutPCache *findInLut(int *piCutP){
+    return findInLutX(piCutP, NULL, true);
+}
+
+static void addToCache(CutPCache *pNew){
+                                 // ? FIX too many pointers
+    CutPCache **ppPlace;
+    if(pNew->nUsed < GOOD_CACHE_THRESHOLD)
+        {
+        if(nBadCaches>MAX_BAD_CACHES)
+            {
+            CutPCache *pDel= pBadCutPCache->pPrev;
+            delFromLut(pDel);
+            delFromCache(pDel);
+            delete [] pDel->pbCCutP;
+            delete pDel;
+            }
+        ppPlace= &pBadCutPCache;
+        nBadCaches++;
+        }
+    else
+        {
+        if(nGoodCaches>MAX_GOOD_CACHES)
+            {
+            CutPCache *pDel= pGoodCutPCache->pPrev;
+            delFromLut(pDel);
+            delFromCache(pDel);
+            delete [] pDel->pbCCutP;
+            delete pDel;
+            }
+        ppPlace= &pGoodCutPCache;
+        nGoodCaches++;
+        }
+
+    if(*ppPlace==NULL)
+        {
+        *ppPlace=          pNew;
+        (*ppPlace)->pNext= pNew;
+        (*ppPlace)->pPrev= pNew;
+        }
+
+    pNew->pNext= *ppPlace;
+    pNew->pPrev= (*ppPlace)->pPrev;
+
+    pNew->pNext->pPrev=pNew;
+    pNew->pPrev->pNext=pNew;
+    *ppPlace= pNew;
+}
+
+static void cache(int *piCurrentCutP){
+
+    static bool bInit=true;
+    if(bInit)
+        {
+        for(int i=0; i<nTEST_LINES; i++)
+            {
+            ppCutPLut[i]=new CutPLut[1<<16];
+            memset( ppCutPLut[i], 0, sizeof(CutPLut)*(1<<16) );
+            }
+        bInit=false;
+        loadVCCache();
+        }
+
+    CutPCache *pBestCutPCache= findInLut(piCurrentCutP);
+
+
+    int iBestScore;
+    if(pBestCutPCache!=NULL) iBestScore=pBestCutPCache->iScore;
+    else                     iBestScore=0;
+
+    mergeAndClean(piCurrentCutP, pBestCutPCache);
+
+    if(pBestCutPCache!=NULL && iBestScore>=SCORE_THRESHOLD)
+        {
+
+         /* LRU Stuff */
+        delFromCache(pBestCutPCache);
+        pBestCutPCache->nUsed++;
+        addToCache(pBestCutPCache);
+
+
+        delFromLut(pBestCutPCache);
+
+        addCutPsToCache(pBestCutPCache, piCurrentCutP);
+
+        addToLut(pBestCutPCache);
+
+        }
+    else if(iBestScore<SCORE_THRESHOLD)
+        {
+        CutPCache *pNewCutPCache= new CutPCache;
+
+        pNewCutPCache->nUsed=0;
+        pNewCutPCache->iScore=0;
+        pNewCutPCache->pbCCutP=NULL;
+        addCutPsToCache(pNewCutPCache, piCurrentCutP);
+
+        addToCache(pNewCutPCache);
+
+        // add and check if we could ever use it, if not thrash it
+        if( addToLut(pNewCutPCache)<SCORE_THRESHOLD )
+            {
+            delFromLut(pNewCutPCache);
+            delFromCache(pNewCutPCache);
+            delete [] pNewCutPCache->pbCCutP;
+            delete pNewCutPCache;
+            }
+        }
+
+    drop=true;
+    nFieldsUnCached++;
+    iCurrentLUTScore=iBestScore;
+    iCurrentUsed=pBestCutPCache->nUsed;
+
+    if(iBestScore<SCORE_THRESHOLD || some==0) return;
+    nFieldsUnCached--;
+    nFieldsCached++;
+
+#ifdef DEBUG
+    printf("Field Cached %d\n", nFields);
+#endif
+
+    if(pBestCutPCache->nUsed<2) return;
+
+    drop=false;
+
+    int *piCutP= getBestCutPs(pBestCutPCache);
+    memcpy(piCurrentCutP, piCutP, sizeof(int)*wndy);
+}
+
+static bool bWasLoaded=false;
+void saveVCCache(void){
+    if(!bWasLoaded) return;
+
+    static FILE *pFCache=fopen("vc_cache.dat", "wb");
+    if(pFCache==NULL) error(error_code(-91));
+    fwrite(&nGoodCaches, sizeof(int), 1, pFCache);
+    fwrite(&nBadCaches, sizeof(int), 1, pFCache);
+    CutPCache *tpCutPCache= pGoodCutPCache->pPrev;
+
+    for(int i=0; i<nGoodCaches; i++)
+        {
+        fwrite(tpCutPCache, sizeof(CutPCache), 1, pFCache);
+        fwrite(tpCutPCache->pbCCutP, sizeof(byte),
+               tpCutPCache->nbCCutP, pFCache);
+
+        tpCutPCache= tpCutPCache->pPrev;
+        }
+
+    tpCutPCache=pBadCutPCache->pPrev;
+    for(int i=0; i<nBadCaches; i++)
+        {
+        fwrite(tpCutPCache, sizeof(CutPCache), 1, pFCache);
+        fwrite(tpCutPCache->pbCCutP, sizeof(byte),
+               tpCutPCache->nbCCutP, pFCache);
+
+        tpCutPCache= tpCutPCache->pPrev;
+        }
+
+    fclose(pFCache);
+}
+
+static void loadVCCache(void){
+
+    bWasLoaded=true;
+    static FILE *pFCache=fopen("vc_cache.dat", "rb");
+    if(pFCache==NULL) return;
+    int nEntGood, nEntBad;
+    fread(&nEntGood, sizeof(int), 1, pFCache);       
+    fread(&nEntBad,  sizeof(int), 1, pFCache);        
+    CutPCache *tpCutPCache;
+
+    for(int i=0; i<nEntGood+nEntBad; i++)
+        {
+        tpCutPCache= new CutPCache;
+
+        fread(tpCutPCache, sizeof(CutPCache), 1, pFCache);
+
+//        printf("test %d %d\n", tpCutPCache->iScore, tpCutPCache->nUsed);
+
+
+        tpCutPCache->pbCCutP= new byte[tpCutPCache->nbCCutP];
+        fread(tpCutPCache->pbCCutP, sizeof(byte),
+              tpCutPCache->nbCCutP, pFCache);
+
+        addToCache(tpCutPCache);
+//        printf("B\n");
+        addToLut(tpCutPCache);
+//        printf("A\n");
+        }
+
+    fclose(pFCache);
+}
+
 void vc_decrypt(void){
  int cutPointX[max_y];
  int addapBuff[max_x];
@@ -82,18 +621,19 @@ void vc_decrypt(void){
  long T1=0, T2;
  color c;
 
- const int lowResSize= mmx ? DWN_SMP : (DWN_SMP-1);
+// const int lowResSize= mmx ? DWN_SMP : (DWN_SMP-1);
 
-#ifdef debug
+#ifdef DEBUG2
  printf("xx%f %f %f\n", PHASE_POINT/PI*180, PHASE_SEG/PI*180, PHASE_SMP/PI*180);
 #endif
 
  int vScore=0;
+ nFields++;
 
 // showPoints=true;
 
  const double freqPix2= double(wndx) / double(x_field) * FREQ_PIX;
-#ifdef debug
+#ifdef DEBUG2
  printf("xy%d %d\n", wndx, x_field);
 #endif
                            //  18
@@ -123,13 +663,13 @@ void vc_decrypt(void){
  const int vcEndX= vcStartX + int( double(POINT_LAND + NO_POINT_LAND)
                                                * freqPix2 / FREQ_SMP + .5);
 
-#ifdef debug
+#ifdef DEBUG2
  printf("vcEndX %d\n",vcEndX);
 #endif
  if(vcEndX>wndx) error(error_code(-123));
  const int vcDiff=vcEndX - vcStartX;
 
- const int minDist= cutP2X[0];
+// const int minDist= cutP2X[0];
 
  cutPointX[0]=0;
 
@@ -141,9 +681,9 @@ void vc_decrypt(void){
  int *convVec= new int[vcDiff>>(PHASE_CMP_STEP-1)];
 
  long TCorr=0, TReSmp=0, TUVDetect=0, TPhaseDiff1=0,
-      TPhaseDiff2_Drift=0, TEdgeDetect=0;
+      TPhaseDiff2_Drift=0, TEdgeDetect=0 ,TCache=0;
 
- if(iState){
+ if(iState==2){
    T1=uclock();
  }
 
@@ -157,7 +697,7 @@ void vc_decrypt(void){
  static char sin2PerCutP[256];
  static char cos2PerCutP[256];
 
- static int cutPs=0;
+// static int cutPs=0;
  static int fields=0;
  fields++;
 
@@ -205,18 +745,43 @@ void vc_decrypt(void){
    lowRes[0] = lowRes[1];
    lowRes[1] = dTemp;
 
-   if(mmx){
+   black[line]=false; //fix this
+
+   if(mmx && DWN_SMP==1)
+        {
+        int iMean;
+        asm(//"int $3 \n\t"
+
+         "xorl %%ecx, %%ecx                   \n\t"
+         "1:                                  \n\t"
+         "movzbl 2(%%esi, %%edx, 4), %%eax    \n\t"  
+         "movzbl 2(%%esi, %%edx, 4), %%ebx    \n\t"  
+         "addl %%eax, %%ebx                   \n\t"  
+         "shrl $1, %%ebx                      \n\t"
+         "addl %%ebx, %%ecx                   \n\t"
+         "movb %%bl, (%%edi, %%edx)           \n\t"  
+         "incl %%edx                          \n\t"  
+         " jnz 1b                             \n\t"  
+  
+          : "=c" (iMean)
+          : "S" ( linep + 1 + (vcStartX<<1) + (vcDiff>>1)*4 ) ,
+            "d" ( -(vcDiff>>1) ) ,
+            "D" ( lowRes[1] + (vcDiff>>1) ) 
+          : "%eax", "%ebx", "%edx", "%esi", "%edi");
+
+        if(double(iMean) / double(vcDiff>>1) < BAD_MEAN_THRESHOLD)
+            black[line]=true;
+        }
+   else if(mmx){
      asm(//"int $3 \n\t"
          "pushl %%ebp          \n\t"
 
          "1:                   \n\t"
-         "movb (%%edi), %%al   \n\t"   // U        load 32 byte in L1-cache
-         "xorl %%ebx, %%ebx    \n\t"   //  V 1
+         "xorl %%ebx, %%ebx    \n\t"   
          "movl %1, %%ebp       \n\t"   // U
          "xorl %%eax, %%eax    \n\t"   //  V 1
          "2:                   \n\t"
          "addl %%eax, %%ebx    \n\t"   // U
-         "xorl %%eax, %%eax    \n\t"   //  V 1
          "movb (%%esi), %%al   \n\t"   // U
          "addl $2, %%esi       \n\t"   //  V 1
          "decl %%ebp           \n\t"   // U
@@ -270,9 +835,8 @@ void vc_decrypt(void){
    }
 
 
-   black[line]=false; //fix this
 
-   if(iState){
+   if(iState==2){
      T2=uclock();
      TReSmp+=T2-T1-uclockWaste;
      T1=T2;
@@ -282,63 +846,66 @@ void vc_decrypt(void){
 
    int bestDiff=0x1FFFFFFF;
    int avgDiff=0;             // for detecting bad lines
-   int avgDiffNum=0;          // for detecting bad lines
+   int avgDiffNum=1;          // for detecting bad lines
    int bestCutPX=0;
 
-   bool firstPass=true;
-   for(int step=STEP_START; step>STEP_END; step>>=1){
-
-     const int cutPStart= firstPass ? 0 : step>>1;
+   if(!black[line])
+        {
+        bool firstPass=true;
+        for(int step=STEP_START; step>STEP_END; step>>=1){
 
-     for(int cutP=cutPStart; cutP<vcDiff; cutP+=step){
-       if(firstPass || cutP+step>=vcDiff || cutP==step>>1 
-          || bestDiff+(bestDiff>>1) > addapBuff[ cutP - (step>>1) ]
-          || bestDiff+(bestDiff>>1) > addapBuff[ cutP + (step>>1) ]){
+            const int cutPStart= firstPass ? 0 : step>>1;
 
+            for(int cutP=cutPStart; cutP<vcDiff; cutP+=step){
+                if(    firstPass || cutP+step>=vcDiff || cutP==step>>1 
+                    || bestDiff+(bestDiff>>1) > addapBuff[ cutP - (step>>1) ]
+                    || bestDiff+(bestDiff>>1) > addapBuff[ cutP + (step>>1) ]){
 
-         int diff;
-         if(mmx){
-           diff = vc_corr_mmx(
-                  int(lowRes[1]                                ),
-                  int(lowRes[0] + (( vcDiff - cutP) >> DWN_SMP)),
-                      cutP >> DWN_SMP ,
-                  int(lowRes[1] + (( cutP         ) >> DWN_SMP)),
-                  int(lowRes[0]                                ),
-                      (vcDiff-cutP) >> DWN_SMP                  );
-           asm("emms\n\t");
-         }
-         else{
-           diff = vc_corr(
-                  int(lowRes[1] + (((        + cutP) >> (DWN_SMP-1))&~1)),
-                  int(lowRes[0] + ((( vcDiff       ) >> (DWN_SMP-1))&~1)),
-                     -(( cutP >> (DWN_SMP-1) ) & ~1),
-                  int(lowRes[1] + ((( vcDiff       ) >> (DWN_SMP-1))&~1)),
-                  int(lowRes[0] + ((( vcDiff - cutP) >> (DWN_SMP-1))&~1)),
-                     -(( (vcDiff-cutP) >> (DWN_SMP-1) ) & ~1)             );
+                    int diff;
+                    if(mmx){
+                        diff = vc_corr_mmx(
+                            int(lowRes[1]                                ),
+                            int(lowRes[0] + (( vcDiff - cutP) >> DWN_SMP)),
+                                cutP >> DWN_SMP ,
+                            int(lowRes[1] + (( cutP         ) >> DWN_SMP)),
+                            int(lowRes[0]                                ),
+                                (vcDiff-cutP) >> DWN_SMP                  );
+                    }
+                    else{
+                        diff = vc_corr(
+                            int(lowRes[1] + (((        + cutP) >> (DWN_SMP-1))&~1)),
+                            int(lowRes[0] + ((( vcDiff       ) >> (DWN_SMP-1))&~1)),
+                                -(( cutP >> (DWN_SMP-1) ) & ~1),
+                            int(lowRes[1] + ((( vcDiff       ) >> (DWN_SMP-1))&~1)),
+                            int(lowRes[0] + ((( vcDiff - cutP) >> (DWN_SMP-1))&~1)),
+                                -(( (vcDiff-cutP) >> (DWN_SMP-1) ) & ~1)             );
               
-         }
+                    }
 
-         if(bestDiff>diff){
-           bestDiff=diff;
-           bestCutPX=cutP;
-         }
+                    if(bestDiff>diff){
+                        bestDiff=diff;
+                        bestCutPX=cutP;
+                    }
 
-         avgDiff+=diff;
-         avgDiffNum++;
+                    avgDiff+=diff;
+                    avgDiffNum++;
 
-         addapBuff[cutP]=diff;
-       }
-       else addapBuff[cutP]=0x1FFFFFFF;
-     }
-     if(firstPass) step<<=1;
-     firstPass=false;
-   }
+                    addapBuff[cutP]=diff;
+                }
+                else addapBuff[cutP]=0x1FFFFFFF;
+            }
+            if(firstPass) step<<=1;
+            firstPass=false;
+        }
+    }
+
+   asm("emms\n\t");
 
    if( int( avgDiff*BAD_THRESHOLD1/float(avgDiffNum) ) - BAD_THRESHOLD2
                                                                  < bestDiff )
        black[line]=true;
 
-   if(iState){
+   if(iState==2){
      T2=uclock();
      TCorr+=T2-T1-uclockWaste;
      T1=T2;
@@ -372,7 +939,7 @@ void vc_decrypt(void){
    if(bestCutPX>=vcDiff) bestCutPX-=vcDiff;
    cutPointX[line]=bestCutPX;
 
-   if(iState){
+   if(iState==2){
      T2=uclock();
      TUVDetect+=T2-T1-uclockWaste;
      T1=T2;
@@ -382,375 +949,422 @@ void vc_decrypt(void){
  deleteAlign(lowRes[0]);
  deleteAlign(lowRes[1]);
 
+ int iSumOfAbsDiff=0;
+ int iCount=1;
+ for(int xLine=1; xLine<wndy; xLine++)
+    {
+    if( black[xLine] || black[xLine-1]) continue;
+
+    iCount++;
+
+    int iCutP1= x2CutP[ cutPointX[xLine-1] ];
+    int iCutP2= x2CutP[ cutPointX[xLine  ] ];
+
+    iSumOfAbsDiff+= mabs(iCutP2 - iCutP1);
+    }
+ bool bVC=true;
+ if(iSumOfAbsDiff/iCount < NOT_VC_THRESHOLD) bVC=false;
+
  byte *dir=new byte[(wndy + 1)<<8];
+
  int *bestDir=new int[wndy + 1];
+ for(int i=0; i<wndy+1; i++) bestDir[i]=-1;
 
  const int driftPenalty[MAX_DRIFT*2+1]= DRIFT_TAB;
 
- int *newVal=new int[256], *lastVal=new int[256];
- memset(lastVal, 0, 256<<2);
-
- byte *newPhaseErr=new byte[256], *lastPhaseErr=new byte[256];
- memset(lastPhaseErr, 0, 256);
-
  llinep=&actVid2MemBufp->b[ -(vgax<<2) ];
  linep= &actVid2MemBufp->b[ -(vgax<<1) ];
- for(int line=0; line<wndy; line++){
-   llinep+=vgax<<1;
-   linep+=vgax<<1;
+ if(bVC){
+    int *newVal=new int[256], *lastVal=new int[256];
+    memset(lastVal, 0, 256<<2);
 
-   memset(newVal, 0, 256<<2);
-   memset(newPhaseErr, 0, 256);
-   bool noLeft=true;
+    byte *newPhaseErr=new byte[256], *lastPhaseErr=new byte[256];
+    memset(lastPhaseErr, 0, 256);
 
-   bestDir[line]=-1;
+    for(int line=0; line<wndy; line++){
+        llinep+=vgax<<1;
+        linep+=vgax<<1;
 
-   if(line==0) continue;
+        memset(newVal, 0, 256<<2);
+        memset(newPhaseErr, 0, 256);
+        bool noLeft=true;
 
-   int relCutPX= cutPointX[line] - cutPointX[line-1];
-   if(relCutPX<0) relCutPX+=vcDiff;
+        bestDir[line]=-1;
 
-   int chromAmp=1;
-   for(int x=0; x<vcDiff; x+= 1<<PHASE_CMP_STEP){
-     int nx= x + (relCutPX & ~1);
-     if(nx>=vcDiff) nx-= (vcDiff & ~1);
-     const int ul= char(llinep[ (((vcStartX & ~1) +  x)<<1) + 0]);
-     const int vl= char(llinep[ (((vcStartX & ~1) +  x)<<1) + 2]);
-     const int un= char( linep[ (((vcStartX & ~1) + nx)<<1) + 0]);
-     const int vn= char( linep[ (((vcStartX & ~1) + nx)<<1) + 2]);
-     const int p= x>>(PHASE_CMP_STEP-1);
-     convVec[p  ]= ul*un + vl*vn;
-     convVec[p+1]= ul*vn - vl*un;
+        if(line==0) continue;
+
+        int relCutPX= cutPointX[line] - cutPointX[line-1];
+        if(relCutPX<0) relCutPX+=vcDiff;
+
+        int chromAmp=1;
+        for(int x=0; x<vcDiff; x+= 1<<PHASE_CMP_STEP){
+            int nx= x + (relCutPX & ~1);
+            if(nx>=vcDiff) nx-= (vcDiff & ~1);
+            const int ul= char(llinep[ (((vcStartX & ~1) +  x)<<1) + 0]);
+            const int vl= char(llinep[ (((vcStartX & ~1) +  x)<<1) + 2]);
+            const int un= char( linep[ (((vcStartX & ~1) + nx)<<1) + 0]);
+            const int vn= char( linep[ (((vcStartX & ~1) + nx)<<1) + 2]);
+            const int p= x>>(PHASE_CMP_STEP-1);
+            convVec[p  ]= ul*un + vl*vn;
+            convVec[p+1]= ul*vn - vl*un;
 /*
      const float fsScale= sqrt(convVec[p  ]*convVec[p  ] +
                                convVec[p+1]*convVec[p+1]  );
      convVec[p  ]= int(convVec[p  ]/pow(fsScale + .001, .9));
      convVec[p+1]= int(convVec[p+1]/pow(fsScale + .001, .9));
 */
-     chromAmp+= mabs(un) + mabs(vn);
-   }
-   if(line<4) chromAmp=1;
+            chromAmp+= mabs(un) + mabs(vn);
+        }
+        if(line<4) chromAmp=1;
    
 //   linep[ ((vcStartX +  0)<<1) + 1]=255;
 //   linep[ ((vcStartX +  relCutPX)<<1) + 1]=200;
 
-   if(iState){
-     T2=uclock();
-     TPhaseDiff1+=T2-T1-uclockWaste;
-     T1=T2;
-   }
+        if(iState==2){
+            T2=uclock();
+            TPhaseDiff1+=T2-T1-uclockWaste;
+            T1=T2;
+        }
 
-   int sumR0=0, sumI0=0;
-   int sumR1=0, sumI1=0;
+        int sumR0=0, sumI0=0;
+        int sumR1=0, sumI1=0;
 
-   {                 
-     int x=0;
-     for(; x<vcDiff-relCutPX; x+= 1<<PHASE_CMP_STEP){
-       const int p= x>>(PHASE_CMP_STEP-1);
-       sumR0+= convVec[p  ];
-       sumI0+= convVec[p+1];
-     }
-     for(; x<vcDiff; x+= 1<<PHASE_CMP_STEP){
-       const int p= x>>(PHASE_CMP_STEP-1);
-       sumR1+= convVec[p  ];
-       sumI1+= convVec[p+1];
-     }
-   }
+        {                 
+            int x=0;
+            for(; x<vcDiff-relCutPX; x+= 1<<PHASE_CMP_STEP){
+                const int p= x>>(PHASE_CMP_STEP-1);
+                sumR0+= convVec[p  ];
+                sumI0+= convVec[p+1];
+            }
+            for(; x<vcDiff; x+= 1<<PHASE_CMP_STEP){
+                const int p= x>>(PHASE_CMP_STEP-1);
+                sumR1+= convVec[p  ];
+                sumI1+= convVec[p+1];
+            }
+        }
 
-   const float s= ( (line & 1) ^ ( vScore>0 ) ) ? -1 : 1;
+        const float s= ( (line & 1) ^ ( vScore>0 ) ) ? -1 : 1;
 
-   int sumR0C= int( sumR0*cosSegPhase   + sumI0*sinSegPhase*s);
-   int sumI0C= int(-sumR0*sinSegPhase*s + sumI0*cosSegPhase);
-   int sumR1C= int( sumR1*cosSegPhase   + sumI1*sinSegPhase*s);
-   int sumI1C= int(-sumR1*sinSegPhase*s + sumI1*cosSegPhase);
+        int sumR0C= int( sumR0*cosSegPhase   + sumI0*sinSegPhase*s);
+        int sumI0C= int(-sumR0*sinSegPhase*s + sumI0*cosSegPhase);
+        int sumR1C= int( sumR1*cosSegPhase   + sumI1*sinSegPhase*s);
+        int sumI1C= int(-sumR1*sinSegPhase*s + sumI1*cosSegPhase);
 
 
-   int ffR0=0    , ffI0=0;
-   int ssR0=sumR0, ssI0=sumI0;
-   int ffR1=0    , ffI1=0;
-   int ssR1=sumR1, ssI1=sumI1;
+        int ffR0=0    , ffI0=0;
+        int ssR0=sumR0, ssI0=sumI0;
+        int ffR1=0    , ffI1=0;
+        int ssR1=sumR1, ssI1=sumI1;
 
-   int lastPhasePos=0;
+        int lastPhasePos=0;
             
-   byte ang=0;
-   if(!black[line]){
-     for(int lastCutP = 0; lastCutP < 256; lastCutP++){
-       if(lastVal[lastCutP]==0) continue;
+        byte ang=0;
+        if(!black[line]){
+            for(int lastCutP = 0; lastCutP < 256; lastCutP++){
+                if(lastVal[lastCutP]==0) continue;
 
-       const int newPhasePos= cutP2X[ lastCutP ] & ((-1) << PHASE_CMP_STEP);
+                const int newPhasePos= cutP2X[ lastCutP ] & ((-1) << PHASE_CMP_STEP);
 
-       if( newPhasePos >= lastPhasePos ){
+                if( newPhasePos >= lastPhasePos ){
 
-         for(; lastPhasePos <= newPhasePos; lastPhasePos += (1<<PHASE_CMP_STEP)){
-           const bool isLast= lastPhasePos == newPhasePos;
+                    for(; lastPhasePos <= newPhasePos; lastPhasePos += (1<<PHASE_CMP_STEP)){
+                        const bool isLast= lastPhasePos == newPhasePos;
 
-           const int p= lastPhasePos>>(PHASE_CMP_STEP-1);
+                        const int p= lastPhasePos>>(PHASE_CMP_STEP-1);
 
-           if(lastPhasePos+relCutPX<vcDiff){
-             ssR0-=convVec[p  ];
-             ssI0-=convVec[p+1];
+                        if(lastPhasePos+relCutPX<vcDiff){
+                            ssR0-=convVec[p  ];
+                            ssI0-=convVec[p+1];
          
-             if(isLast){
-               const int r= ssR0 + sumR1C + int( ffR0*cosSegPhase2   + ffI0*sinSegPhase2*s);
-               const int i= ssI0 + sumI1C + int(-ffR0*sinSegPhase2*s + ffI0*cosSegPhase2);
+                            if(isLast){
+                                const int r= ssR0 + sumR1C
+                                    + int(  ffR0*cosSegPhase2
+                                          + ffI0*sinSegPhase2*s);
+                                const int i= ssI0 + sumI1C
+                                    + int( -ffR0*sinSegPhase2*s
+                                          + ffI0*cosSegPhase2);
             
-               ang= (byte)( atan3(i, r)/PI*128 );
-             }
+                                ang= (byte)( atan3(i, r)/PI*128 );
+                            }
 
-             ffR0+=convVec[p  ];
-             ffI0+=convVec[p+1];
-           }
-           else{
-             ssR1-=convVec[p  ];
-             ssI1-=convVec[p+1];
+                            ffR0+=convVec[p  ];
+                            ffI0+=convVec[p+1];
+                        }
+                        else{
+                            ssR1-=convVec[p  ];
+                            ssI1-=convVec[p+1];
      
-             if(isLast){
-               const int r= ssR1 + sumR0C + int( ffR0*cosSegPhase2   + ffI0*sinSegPhase2*s);
-               const int i= ssI1 + sumI0C + int(-ffR0*sinSegPhase2*s + ffI0*cosSegPhase2);
+                            if(isLast){
+                                const int r= ssR1 + sumR0C
+                                    + int(  ffR0*cosSegPhase2
+                                          + ffI0*sinSegPhase2*s);
+                                const int i= ssI1 + sumI0C
+                                    + int( -ffR0*sinSegPhase2*s
+                                          + ffI0*cosSegPhase2);
 
-               ang= (byte)( atan3(i, r)/PI*128 );
-             }
+                                ang= (byte)( atan3(i, r)/PI*128 );
+                            }
 
-             ffR1+=convVec[p  ];
-             ffI1+=convVec[p+1];
-           }
+                            ffR1+=convVec[p  ];
+                            ffI1+=convVec[p+1];
+                        }
 
-         }
-       }
+                    }
+                }
 
-       int newXTemp= cutPointX[line] - cutPointX[line-1] + cutP2X[lastCutP];
-       if(newXTemp <  0     ) newXTemp+= vcDiff;
-       if(newXTemp >= vcDiff) newXTemp-= vcDiff;
-       if(newXTemp >= vcDiff) newXTemp-= vcDiff;
+                int newXTemp= cutPointX[line] - cutPointX[line-1] + cutP2X[lastCutP];
+                if(newXTemp <  0     ) newXTemp+= vcDiff;
+                if(newXTemp >= vcDiff) newXTemp-= vcDiff;
+                if(newXTemp >= vcDiff) newXTemp-= vcDiff;
 
-       const int newCutPStart= max(x2CutP[ newXTemp ] - MAX_DRIFT, 0);
-       const int newCutPEnd=   min(x2CutP[ newXTemp ] + MAX_DRIFT, 256);
+                const int newCutPStart= max(x2CutP[ newXTemp ] - MAX_DRIFT, 0);
+                const int newCutPEnd=   min(x2CutP[ newXTemp ] + MAX_DRIFT, 256);
 
-       int drift= newCutPStart - (x2CutP[ newXTemp ] - MAX_DRIFT);
+//       const int drift= newCutPStart - (x2CutP[ newXTemp ] - MAX_DRIFT);
+                const int *piDrift=
+                    &driftPenalty[newCutPStart - (x2CutP[ newXTemp ] - MAX_DRIFT)-1];
 
-       const byte a= (line & 1) ^ ( vScore>0 ) ? -ang: ang;
+                const byte a= (line & 1) ^ ( vScore>0 ) ? -ang: ang;
 
-       const int phase1= (a + phasePerCutP[ lastCutP ]) & 0xFF;
-       const int lastPhaseErrLastCutP= lastPhaseErr[lastCutP] + 128;
+                const int phase1= (a + phasePerCutP[ lastCutP ]) & 0xFF;
+                const int lastPhaseErrLastCutP= lastPhaseErr[lastCutP] + 128;
           // FIX (ASM_OPTIMIZE)
-       if(chromAmp>AMP_THRESHOLD){
-         for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
-           const int phaseErr= (phase1 + phasePerCutP[ newCutP ]) & 0xFF;
-           const int val=  lastVal[lastCutP] - driftPenalty[drift]
+                if(chromAmp>AMP_THRESHOLD){
+                    for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
+                        const int phaseErr= (phase1 + phasePerCutP[ newCutP ]) & 0xFF;
+                        const int val=  lastVal[lastCutP] - *(++piDrift)
                          - phaseLut[ phaseErr ]
                          - phaseLut[ (phaseErr - lastPhaseErrLastCutP) & 0xFF];// useless without phase errors from decomb-filter
      
-           if(newVal[newCutP] < val){
-             newVal[newCutP]= val;
-             dir[(line<<8) + newCutP]= lastCutP;
-             newPhaseErr[newCutP]= phaseErr;
+                        if(newVal[newCutP] < val){
+                            newVal[newCutP]= val;
+                            dir[(line<<8) + newCutP]= lastCutP;
+                            newPhaseErr[newCutP]= phaseErr;
         
-             noLeft=false;
-           }
-           drift++;
-         }
-       }
-       else{
-         linep[ ((vcStartX -  1)<<1) + 1]=255;
-         for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
-           const int phaseErr= (phase1 + phasePerCutP[ newCutP ]) & 0xFF;
-           const int val=  lastVal[lastCutP] - driftPenalty[drift];
+                            noLeft=false;
+                        }
+                    }
+                }
+                else{
+                    linep[ ((vcStartX -  1)<<1) + 1]=255;
+                    for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
+                        const int phaseErr= (phase1 + phasePerCutP[ newCutP ]) & 0xFF;
+                        const int val=  lastVal[lastCutP] - *(++piDrift);
      
-           if(newVal[newCutP] < val){
-             newVal[newCutP]= val;
-             dir[(line<<8) + newCutP]= lastCutP;
-             newPhaseErr[newCutP]= phaseErr;
+                        if(newVal[newCutP] < val){
+                            newVal[newCutP]= val;
+                            dir[(line<<8) + newCutP]= lastCutP;
+                            newPhaseErr[newCutP]= phaseErr;
         
-             noLeft=false;
-           }
-           drift++;
-         }
-       }
-     }
-   }
+                            noLeft=false;
+                        }
+                    }
+                }
+            }
+        }
 
-   if(iState){
-     T2=uclock();
-     TPhaseDiff2_Drift+=T2-T1-uclockWaste;
-     T1=T2;
-   }
+        if(iState==2){
+            T2=uclock();
+            TPhaseDiff2_Drift+=T2-T1-uclockWaste;
+            T1=T2;
+        }
 
-   if(!black[line]){
-     int iBestVal=0;
-     for(int newCutP = 0; newCutP < 256; newCutP++){
-       if(!noLeft && newVal[newCutP]==0) continue;
+        if(!black[line]){
+            int iBestVal=0;
+            for(int newCutP = 0; newCutP < 256; newCutP++){
+                if(!noLeft && newVal[newCutP]==0) continue;
 
-       const byte * const p= &linep[ ((cutP2X[ newCutP ] + vcStartX)<<1) + 1 - 4];
+                const byte * const p=
+                    &linep[ ((cutP2X[ newCutP ] + vcStartX)<<1) + 1 - 4];
 
-       const int diff=edgeLut[ mabs( + int(* p   ) + int(*(p+2))
-                                     - int(*(p+4)) - int(*(p+6)) ) ];
+                const int diff=edgeLut[ mabs( + int(* p   ) + int(*(p+2))
+                                              - int(*(p+4)) - int(*(p+6)) ) ];
   
-       if(newVal[newCutP]==0) newVal[newCutP] =  diff + 1000000000;
-       else                   newVal[newCutP] += diff;
+                if(newVal[newCutP]==0) newVal[newCutP] =  diff + 1000000000;
+                else                   newVal[newCutP] += diff;
 
-       if(iBestVal < newVal[newCutP]){
-         iBestVal= newVal[newCutP];
-         bestDir[line]= newCutP;
-       }
+                if(iBestVal < newVal[newCutP]){
+                    iBestVal= newVal[newCutP];
+                    bestDir[line]= newCutP;
+                }
 
-     }
-   }
+            }
+        }
    
-   if(iState){
-     T2=uclock();
-     TEdgeDetect+=T2-T1-uclockWaste;
-     T1=T2;
-   }
+        if(iState==2){
+            T2=uclock();
+            TEdgeDetect+=T2-T1-uclockWaste;
+            T1=T2;
+        }
 
-   int *tempVal=lastVal;
-   lastVal=newVal;
-   newVal=tempVal;
+        int *tempVal=lastVal;
+        lastVal=newVal;
+        newVal=tempVal;
+
+        byte *tempErr=lastPhaseErr;
+        lastPhaseErr=newPhaseErr;
+        newPhaseErr=tempErr;
+    }
 
-   byte *tempErr=lastPhaseErr;
-   lastPhaseErr=newPhaseErr;
-   newPhaseErr=tempErr;
- }
 
+    delete [] newVal;
+    delete [] lastVal;
+
+    delete [] newPhaseErr;
+    delete [] lastPhaseErr;
+ }
  delete [] convVec;
 
- delete [] newVal;
- delete [] lastVal;
 
- delete [] newPhaseErr;
- delete [] lastPhaseErr;
- 
- if(showPoints){
-   int bestCutP=-1;
-   for(int line=wndy-2; line>0; line--){
-     if(bestDir[line]==-1){
-       bestCutP=-1;
-       continue;
-     }
-     if(bestCutP==-1) bestCutP= bestDir[line];
-     if(bestCutP<0 || bestCutP>=256){
-       printf("one%d\n", bestCutP);
-       bestCutP=-1;
-       continue;
-     }
+ int piBestCutP[600];
+ int iBestCutP=-1;
+ for(int line=wndy-2; line>0; line--)
+    {
+    piBestCutP[line]=-1;
 
-//     const int bestX= cutP2X[ bestCutP ];
-     int bestX=cutP2X[ bestCutP ] + cutPointX[line-1] - cutPointX[line];
-     if(bestX<0) bestX+= vcDiff;
-     if(bestX>=vcDiff) bestX-= vcDiff;
-     actVid2MemBufp->b[((line-1) * vgax<<1) + ((vcStartX + bestX)<<1)+1]=255;
-     actVid2MemBufp->b[((line-1) * vgax<<1) + ((vcStartX + bestX)<<1)+3]=0;
+    if(bestDir[line]==-1)
+        {
+        iBestCutP=-1;
+        continue;
+        }
 
-     bestCutP= dir[(line<<8) + bestCutP];
-   }
- }
+    if(iBestCutP==-1) iBestCutP= bestDir[line];
+    if(iBestCutP<0 || iBestCutP>=256)
+        {
+        printf("one%d\n", iBestCutP);
+        iBestCutP=-1;
+        continue;
+        }
+    piBestCutP[line]= iBestCutP;
 
- byte cutpp[1000];
- byte copy_buff[max_x<<1];
+    iBestCutP= dir[(line<<8) + iBestCutP];
+    }
 
- int bestCutP=-1;
- linep= &actVid2MemBufp->b[ (wndy-1) * (vgax<<1) ];
- llinep=&actVid2MemBufp->b[  wndy    * (vgax<<1) ];
- for(int line=wndy-2; line>0; line--){
-   linep-= vgax<<1;
-   llinep-= vgax<<1;
+#ifdef DEBUG_CUTP
+ for(int line=1; line<wndy-1; line++)
+    {
+    fprintf(pF, "%d\n", piBestCutP[line]);
+    }
+#endif
 
-   cutpp[line]=0;
+ if(bVC) cache(piBestCutP);
 
-   if(black[line]){
-     linep[ ((vcStartX - 4)<<1) + 1 ]=255;
-     linep[ ((vcStartX - 4)<<1) + 3 ]=255;
-   }
+ if(iState==2)
+    {
+    T2=uclock();
+    TCache+=T2-T1-uclockWaste;
+    T1=T2;
+    }
 
-   if(showPoints){
-     linep[ (vcStartX<<1)+1 ]=255;
-     linep[ (vcStartX<<1)+3 ]=0;
-   }
-   if(bestDir[line]==-1){
-     bestCutP=-1;
-     continue;
-   }
-   if(bestCutP==-1) bestCutP= bestDir[line]; 
 
-   if(bestCutP<0 || bestCutP>=256){
-     printf("two%d\n", bestCutP);
-     bestCutP=-1;
-     continue;
-   }
+ if(showPoints)
+    {
+    for(int line=wndy-2; line>0; line--)
+        {
+        int bestCutP=piBestCutP[line];
+        if(bestCutP==-1) continue;
 
-   cutpp[line]=bestCutP;
+//     const int bestX= cutP2X[ bestCutP ];
+         int bestX=cutP2X[ bestCutP ] + cutPointX[line-1] - cutPointX[line];
+         if(bestX<0) bestX+= vcDiff;
+        if(bestX>=vcDiff) bestX-= vcDiff;
+        actVid2MemBufp->b[((line-1) * vgax<<1) + ((vcStartX + bestX)<<1)+1]=255;
+        actVid2MemBufp->b[((line-1) * vgax<<1) + ((vcStartX + bestX)<<1)+3]=0;
 
-   const int bestX= cutP2X[ bestCutP ];
+        }
+    }
 
-   int cutX= ((showPoints && 0) ? cutPointX[line] : bestX );
+ if(bVC){
+    byte copy_buff[max_x<<1];
 
-   const int cutP= max(x2CutP[cutX], 0);          // fix ? ness, min?
- 
-   const char s1= sin1PerCutP[ cutP ];
-   const char c1= cos1PerCutP[ cutP ];
-   const char s2= sin2PerCutP[ cutP ];
-   const char c2= cos2PerCutP[ cutP ];
+    linep= &actVid2MemBufp->b[ (wndy-1) * (vgax<<1) ];
+    llinep=&actVid2MemBufp->b[  wndy    * (vgax<<1) ];
+    for(int line=wndy-2; line>0; line--){
+        linep-= vgax<<1;
+        llinep-= vgax<<1;
 
-   if( (line & 1) ^ ( vScore>0 ) ){
-     char *p= (char*)( &linep[ ((vcStartX & ~1)<<1) ] );
-     int x=0;
-     for(; x<cutX; x+=4){
-       const char u= * p;
-       const char v= *(p+2);
+        if(black[line]){
+            linep[ ((vcStartX - 4)<<1) + 1 ]=255;
+            linep[ ((vcStartX - 4)<<1) + 3 ]=255;
+        }
 
-       const char u1=(char)(( - v*c1 - u*s1)>>7);
-       const char v1=(char)(( + v*s1 - u*c1)>>7);
+        if(showPoints){
+            linep[ (vcStartX<<1)+1 ]=255;
+            linep[ (vcStartX<<1)+3 ]=0;
+        }       
+        int bestCutP=piBestCutP[line];
+        if(bestCutP==-1) continue;
 
-       if( (vcDiff - cutX) & 1 ){
-         * p   = v1;
-         *(p+2)= u1;
-         *(p+4)= v1;
-         *(p+6)= u1;
-       }
-       else{
-         * p   = u1;
-         *(p+2)= v1;
-         *(p+4)= u1;
-         *(p+6)= v1;
-       }
-       p+=8;
-     }
+        const int bestX= cutP2X[ bestCutP ];
 
-     for(; x<vcDiff; x+=4){
-       const char u= * p;
-       const char v= *(p+2);
+        int cutX= ((showPoints && 0) ? cutPointX[line] : bestX );
 
-       const char u1=(char)(( - v*c2 - u*s2)>>7);
-       const char v1=(char)(( + v*s2 - u*c2)>>7);
-       
-       if( cutX & 1 ){
-         * p   = v1;
-         *(p+2)= u1;
-         *(p+4)= v1;
-         *(p+6)= u1;
-       }
-       else{
-         * p   = u1;
-         *(p+2)= v1;
-         *(p+4)= u1;
-         *(p+6)= v1;
-       }
-       p+=8;
-     }
-   }
+        const int cutP= max(x2CutP[cutX], 0);          // fix ? ness, min?
+ 
+        const char s1= sin1PerCutP[ cutP ];
+        const char c1= cos1PerCutP[ cutP ];
+        const char s2= sin2PerCutP[ cutP ];
+        const char c2= cos2PerCutP[ cutP ];
+
+        if( (line & 1) ^ ( vScore>0 ) ){
+            char *p= (char*)( &linep[ ((vcStartX & ~1)<<1) ] );
+            int x=0;
+            for(; x<cutX; x+=4){
+                const char u= * p;
+                const char v= *(p+2);
+
+                const char u1=(char)(( - v*c1 - u*s1)>>7);
+                const char v1=(char)(( + v*s1 - u*c1)>>7);
 
+                if( (vcDiff - cutX) & 1 ){
+                    * p   = v1;
+                    *(p+2)= u1;
+                    *(p+4)= v1;
+                    *(p+6)= u1;
+                }
+                else{
+                    * p   = u1;
+                    *(p+2)= v1;
+                    *(p+4)= u1;
+                    *(p+6)= v1;
+                }
+                p+=8;
+            }
 
+            for(; x<vcDiff; x+=4){
+                const char u= * p;
+                const char v= *(p+2);
+
+                const char u1=(char)(( - v*c2 - u*s2)>>7);
+                const char v1=(char)(( + v*s2 - u*c2)>>7);
+       
+                if( cutX & 1 ){
+                    * p   = v1;
+                    *(p+2)= u1;
+                    *(p+4)= v1;
+                    *(p+6)= u1;
+                }
+                else{
+                    * p   = u1;
+                    *(p+2)= v1;
+                    *(p+4)= u1;
+                    *(p+6)= v1;
+                }
+                p+=8;
+            }
+        }
 
  
 //   linep[ ((vcStartX)<<1)+1 ]= 255;
    
-   memcpy(copy_buff, linep + (vcStartX<<1), vcDiff<<1);
+        memcpy(copy_buff, linep + (vcStartX<<1), vcDiff<<1);
 
-   memcpy(linep + (vcStartX<<1), copy_buff + (cutX<<1), (vcDiff-cutX)<<1);
-   memcpy(linep + (vcStartX<<1) + ((vcDiff-cutX)<<1), copy_buff, cutX<<1);
+        memcpy(linep + (vcStartX<<1), copy_buff + (cutX<<1), (vcDiff-cutX)<<1);
+        memcpy(linep + (vcStartX<<1) + ((vcDiff-cutX)<<1), copy_buff, cutX<<1);
 
-   if( !( (line & 1) ^ ( vScore>0 ) ) ){
-     char *pl= (char*)( &llinep[ ((vcStartX<<1) & ~3) ] );
-     char *pn= (char*)( &linep[  ((vcStartX<<1) & ~3) ] );
+        if( !( (line & 1) ^ ( vScore>0 ) ) ){
+            char *pl= (char*)( &llinep[ ((vcStartX<<1) & ~3) ] );
+            char *pn= (char*)( &linep[  ((vcStartX<<1) & ~3) ] );
 /*     for(int x=0; x<vcDiff; x+=4){
        * pn   = *(pn+4)= * pl;
        *(pn+2)= *(pn+6)= *(pl+2);
@@ -774,17 +1388,10 @@ void vc_decrypt(void){
             "S" (int(pl) + (vcDiff<<1)) ,
             "c" (-vcDiff<<1) 
           : "%eax", "%ebx", "%ecx");
-   }
-
-   bestCutP= dir[(line<<8) + bestCutP];
- }
+        }
 
-#ifdef debug
- for(int line=1; line<wndy-1; line++)
-    {
-    fprintf(pF, "%d\n", cutpp[line]);
     }
-#endif
+ }
 
 /*
  float avgCutP=0;
@@ -796,35 +1403,71 @@ void vc_decrypt(void){
  printf("avg%f\n", avgCutP-float(vcDiff)/2.0);
   */
   
-#ifdef debug
+#ifdef DEBUG2
  printf("vScore %d\n", vScore);
 #endif
 
- if(iState){
-   T2=uclock();
-   sprintf(textbuf[0],"%f ReSample", (float)(TReSmp)/UCLOCKS_PER_SEC);
-   sprintf(textbuf[1],"%f Correlate", (float)(TCorr)/UCLOCKS_PER_SEC);
-   sprintf(textbuf[2],"%f UVDetect", (float)(TUVDetect)/UCLOCKS_PER_SEC);
-   sprintf(textbuf[3],"%f FindPhaseDiff1", (float)(TPhaseDiff1)/UCLOCKS_PER_SEC);
-   sprintf(textbuf[4],"%f FindPhaseDiff2 / Drift", (float)(TPhaseDiff2_Drift)/UCLOCKS_PER_SEC);
-   sprintf(textbuf[5],"%f EdgeDetect", (float)(TEdgeDetect)/UCLOCKS_PER_SEC);
-   sprintf(textbuf[6],"%f Rotate / ColorFix", (float)(T2-T1-uclockWaste)/UCLOCKS_PER_SEC);
-   c.init(255, 0, 0, false);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[0]);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[2]);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[3]);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[4]);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[5]);
-   gprint(infoPosX, infoPosY+=10, c.col, textbuf[6]);
-   infoPosY+=5;
-   T1=T2;                           
- }
-/*
- c.init(255, 0, 0, false);
- sprintf(textbuf[6],"%d l %d f", cutPs, fields);
- gprint(infoPosX, infoPosY+=10, c.col, textbuf[6]);
-  */
+    if(iState==2)
+        {
+        T2=uclock();
+        sprintf(textbuf[0],"%f ReSample", (float)(TReSmp)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[1],"%f Correlate", (float)(TCorr)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[2],"%f UVDetect", (float)(TUVDetect)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[3],"%f FindPhaseDiff1", (float)(TPhaseDiff1)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[4],"%f FindPhaseDiff2 / Drift", (float)(TPhaseDiff2_Drift)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[5],"%f EdgeDetect", (float)(TEdgeDetect)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[6],"%f Cache", (float)(TCache)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[7],"%f Rotate / ColorFix", (float)(T2-T1-uclockWaste)/UCLOCKS_PER_SEC);
+        c.init(255, 0, 0, false);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[0]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[2]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[3]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[4]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[5]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[6]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[7]);
+        infoPosY+=5;
+        T1=T2;                           
+    }
+    if(iState>=1)
+        {
+        sprintf(textbuf[0],"%d/%d Caches, %d/%d Cached",
+            nBadCaches,
+            nGoodCaches,
+            nFieldsUnCached,
+            nFieldsCached);
+
+        c.init(255, 0, 0, false);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[0]);
+
+        byte pbScore[100];
+        for(int i=0; i<iCurrentLUTScore; i++)
+            pbScore[i]='*';
+        for(int i=iCurrentLUTScore; i<nTEST_LINES; i++)
+            pbScore[i]='.';
+        pbScore[ nTEST_LINES ]=0;
+
+        sprintf(textbuf[1],"LUT Score %s",
+            pbScore);
+
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
+
+
+        byte pbUsed[100];
+        iCurrentUsed= min(iCurrentUsed, 20);
+        for(int i=0; i<iCurrentUsed; i++)
+            pbUsed[i]='*';
+        for(int i=iCurrentUsed; i<20; i++)
+            pbUsed[i]='.';
+        pbUsed[ 20 ]=0;
+
+        sprintf(textbuf[1],"Used %s",
+            pbUsed);
+
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
+
+        }
 
   delete [] bestDir;
   delete [] dir;



More information about the Mndiff-dev mailing list