[MN-dev] [mndiff]: r62 - in trunk/2010: 2010.cpp 2_all.h 2_cryp_a.asm 2_crypt.cpp 2_hw_a.asm 2_nag.cpp 2_vc.cpp 2_vc.h 2d makefile

michael subversion at mplayerhq.hu
Wed Jul 4 10:47:18 CEST 2007


Author: michael
Date: Wed Jul  4 10:47:18 2007
New Revision: 62

Log:
version from 1999-06-18 22:00


Modified:
   trunk/2010/2010.cpp
   trunk/2010/2_all.h
   trunk/2010/2_cryp_a.asm
   trunk/2010/2_crypt.cpp
   trunk/2010/2_hw_a.asm
   trunk/2010/2_nag.cpp
   trunk/2010/2_vc.cpp
   trunk/2010/2_vc.h
   trunk/2010/2d
   trunk/2010/makefile

Modified: trunk/2010/2010.cpp
==============================================================================
--- trunk/2010/2010.cpp	(original)
+++ trunk/2010/2010.cpp	Wed Jul  4 10:47:18 2007
@@ -240,9 +240,9 @@ int main(int argc, char **argv){
         {
         for(int y=0; y<=31; y++)
             {
-            int r= minmax(0, int( y + double(v) * 1.375                       ), 31); 
-            int g= minmax(0, int((y - double(v) * 0.703 - double(u) * 0.343)*2), 63); 
-            int b= minmax(0, int( y                     + double(u) * 1.734   ), 31); 
+            int r= minmax(0, int( y + double(v) * 1.375                       +.5), 31); 
+            int g= minmax(0, int((y - double(v) * 0.703 - double(u) * 0.343)*2+.5), 63); 
+            int b= minmax(0, int( y                     + double(u) * 1.734   +.5), 31); 
 
             const int xLut= y + ((u&0x1F)<<5) + ((v&0x1F)<<10);
             yuv2RgbLut[ xLut ]= b + (g<<5) + (r<<11);

Modified: trunk/2010/2_all.h
==============================================================================
--- trunk/2010/2_all.h	(original)
+++ trunk/2010/2_all.h	Wed Jul  4 10:47:18 2007
@@ -6,6 +6,7 @@
 #define sign(a)   ((a)<0 ? -1 :  1)
 #define is_neg(a) ((a)<0 ?  0 : -1)
 #define is_pos(a) ((a)<0 ? -1 :  0)
+#define round(a) ( (a)<0.0 ? (a)-.5 : (a)+.5 )
 
 #define max(a,b)   ((a)>(b) ? (a) : (b))
 #define min(a,b)   ((a)<(b) ? (a) : (b))

Modified: trunk/2010/2_cryp_a.asm
==============================================================================
--- trunk/2010/2_cryp_a.asm	(original)
+++ trunk/2010/2_cryp_a.asm	Wed Jul  4 10:47:18 2007
@@ -93,6 +93,7 @@ _Decomb2__Fiii:
  xor ecx, byte -1
  inc ecx
 
+; int3
  movq mm6, [decombYMask]
  movq mm7, [decombInc]
 align16

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

Modified: trunk/2010/2_hw_a.asm
==============================================================================
--- trunk/2010/2_hw_a.asm	(original)
+++ trunk/2010/2_hw_a.asm	Wed Jul  4 10:47:18 2007
@@ -40,8 +40,10 @@ _CopyYuv2Rgb1__Fiiii:
 align16
 CopyLoop1:
  movq mm0, [esi+ecx]               ;4p2 wMM0rESIECX  1    0 3
- movq mm1, [esi+ecx]               ;4p2 wMM1rESIECX  10   1 4
- movq mm2, [esi+ecx]               ;4p2 wMM2rESIECX  10   2 5
+; movq mm1, [esi+ecx]               ;4p2 wMM1rESIECX  10   1 4 // Hotspot ?2%
+; movq mm2, [esi+ecx]               ;4p2 wMM2rESIECX  10   2 5
+movq mm1, mm0
+movq mm2, mm0
 
  psrlw mm0, 11                     ;4p1 wMM0rMM0     0    1
  ;iFetch

Modified: trunk/2010/2_nag.cpp
==============================================================================
--- trunk/2010/2_nag.cpp	(original)
+++ trunk/2010/2_nag.cpp	Wed Jul  4 10:47:18 2007
@@ -310,7 +310,7 @@ void nag_decrypt(void){
    }
 
 
-   f=fopen("2010.dat", "rb");
+   f=fopen("nagra.dat", "rb");
    if(f!=NULL){
      int s=0;
      int decoLines2;
@@ -533,7 +533,7 @@ void nag_decrypt(void){
      */
 
 
-     f=fopen("2010.dat", "wb");
+     f=fopen("nagra.dat", "wb");
      if(f==NULL) error(DatWrite);
 
      fwrite(&decoLines,    sizeof(int), 1, f);

Modified: trunk/2010/2_vc.cpp
==============================================================================
--- trunk/2010/2_vc.cpp	(original)
+++ trunk/2010/2_vc.cpp	Wed Jul  4 10:47:18 2007
@@ -15,41 +15,57 @@
 #include "2010.h"
 
 //#define DEBUG_CUTP
-
+#define SHOW_EDGE_AREA
 //#define DEBUG
+#define SHOW_FINALS
+//#define NO_CACHE
 
 #define FREQ_SMP       2.1e7
 #define POINT_LAND     765.0                         
-#define NO_POINT_LAND1 150.0             // GUESSED and ok 
-#define NO_POINT_LAND2 150.0             // GUESSED and ok
-#define PHASE_PATCH    (-28)             // GUESSED ? -9, ...
+#define NO_POINT_LAND1 (148)  // GUESSED 148/153
+#define NO_POINT_LAND2 (152)  // GUESSED 152/147
  // ? POINT_LAND + NO_POINT_LAND(1|2) = 910   (FIFO SIZE)
- // ? NO_POINT_LAND + POINT_LAND = 1065       (PHASE ~+-60deg)
+ // ? NO_POINT_LAND + POINT_LAND = 1065, (1060) 
+ // ? NO_POINT_LAND1 not 150 ?
+ // PHASE_SEG 268-306 Grad
 
 #define NO_POINT_LAND  (NO_POINT_LAND1 + NO_POINT_LAND2)
 #define PHASE_POINT    (fmod(FREQ_CHROM / (FREQ_SMP/3.0), 1.0) * PI * 2.0)
 #define PHASE_SMP      (fmod(FREQ_CHROM / (FREQ_SMP    ), 1.0) * PI * 2.0)
+#define PHASE_LINE     (fmod(FREQ_CHROM / FREQ_HS       , 1.0) * PI * 2.0) // + some/180.0*PI)
 #define PHASE_SEG      (fmod(FREQ_CHROM / FREQ_SMP \
-                          * (POINT_LAND + NO_POINT_LAND), 1.0) * PI * 2.0)
+                          * (POINT_LAND + NO_POINT_LAND), 1.0) * PI * 2.0)// + some/180.0*PI)
 
 
+
+#define MIN_LINES_FOR_EDGE_AREA 50
+#define EDGE_CHANGE_THRESHOLD 0.3
+#define PHASE_CORRECT_THRESHOLD (1.0/128*PI)
+#define MAX_PHASE_CORRECT ( 22.0/180.0*PI)
+#define MIN_PHASE_CORRECT (-22.0/180.0*PI)
+#define FIELDS_PER_PHASE_CHECK 30
+#define EDGE_AREA_TRUNC_DIV 6
+#define EDGE_AREA_START -4
+#define EDGE_AREA_END 4
+#define DEFINAL_WAIT 30
 #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 GOOD_CACHE_THRESHOLD 3
+#define FINAL_CACHE_DIFF_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 MAGIC_ID "VCC0"
 #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 PHASE_CMP_STEP 3
 #define BAD_MEAN_THRESHOLD 17
 #define BAD_THRESHOLD1 0.8
 #define BAD_THRESHOLD2 200
@@ -81,6 +97,7 @@ extern bool drop;
 
 static void loadVCCache(void);
 
+
 static inline float atan3(const float f1, const float f2){
   float out;
                      // ?! atan2 crashes 
@@ -105,6 +122,7 @@ static int iCurrentLUTScore=0;
 static int iCurrentUsed=0;
 static CutPCache *pGoodCutPCache=NULL;
 static CutPCache *pBadCutPCache=NULL;
+static int badFinalsLeft=0;
 
 static void delFromCache(CutPCache *pOld){
 
@@ -148,37 +166,58 @@ static void addCutPsToCache(CutPCache *p
             xIn++;
             }
 
-        bool bOldGood=true;
-        bool bGood=true;
+        bool isOldFinal=true;
+        bool isFinal=true;
+		int bestCutP= piCutP[line];
+		int bestCutPs=1;
+		if(bestCutP==-1) bestCutPs=0;
+
+		int nextBestCutP=-1;
+		int nextBestCutPs=0;
         if(nInBytes!=HEADER_FINAL)
             {
-            bOldGood=false;
-            bGood=false;
-            int nCurrentCutPs=1;
+            isOldFinal=false;
+            isFinal=false;
+			int currentCutP=-1;
+			int currentCutPs=0;
             for(int i=0; i<nInBytes; i++)
                 {
-                if(int(pCutPCache->pbCCutP[xIn+i]) == piCutP[line])
-                    {
-                    nCurrentCutPs++;
-                    if(nCurrentCutPs>=FINAL_CACHE_THRESHOLD)
-                        {
-                        bGood=true;
-                        }
-                    }
-                }
+				if(int(pCutPCache->pbCCutP[xIn+i]) != currentCutP)
+					{
+					currentCutP= pCutPCache->pbCCutP[xIn+i];
+					currentCutPs=0;
+					if(currentCutP == piCutP[line]) currentCutPs++;
+					}
+				currentCutPs++;
+
+				if(currentCutPs >= bestCutPs)
+					{
+					if(bestCutP != currentCutP)
+						{
+						nextBestCutP=  bestCutP;
+						nextBestCutPs= bestCutPs;
+						}
+					bestCutP=  currentCutP;
+					bestCutPs= currentCutPs;
+					}
+				}
+			
+			if(bestCutPs-nextBestCutPs >= FINAL_CACHE_DIFF_THRESHOLD) isFinal=true; 
             }
-        int nOutBytes=nInBytes;
-        if(!bGood && piCutP[line]!=-1) nOutBytes++;
+
+	    int nOutBytes=nInBytes;
+        if(!isFinal && piCutP[line]!=-1) nOutBytes++;
         int xKill=-1;
-        if(!bGood && nOutBytes>MAX_EQ_SETS)
+        if(!isFinal && nOutBytes>MAX_EQ_SETS)
             {
             nOutBytes=MAX_EQ_SETS;
             xKill=random() % MAX_EQ_SETS;
             }
-        if(bGood) nOutBytes=1;
+        if(isFinal) nOutBytes=1;
 
-        int iOutHeader=nOutBytes;
-        if(bGood) iOutHeader=HEADER_FINAL;
+        int iOutHeader;
+        if(isFinal) iOutHeader= HEADER_FINAL;
+        else        iOutHeader= nOutBytes;
 
         if(iLastOutHeader == iOutHeader && nOutHeaders<16)
             nOutHeaders++;
@@ -194,20 +233,22 @@ static void addCutPsToCache(CutPCache *p
             }
 
 
-        if(bOldGood)
+        if(isOldFinal)
             {
             pbCScratchPad[xOut]= pCutPCache->pbCCutP[xIn];
             xOut++;
             }
-        else if(bGood)
+        else if(isFinal)
             {
-            pbCScratchPad[xOut]= piCutP[line];
+            pbCScratchPad[xOut]= bestCutP;
             xOut++;
             }
         else
             {
-            bool bOut=false;
+            bool bOut;
             if(piCutP[line]==-1) bOut=true;
+            else                 bOut=false;
+
             for(int i=0; i<nInBytes; i++)
                 {
                 if(i==xKill) continue;
@@ -247,15 +288,30 @@ static int *getBestCutPs(CutPCache *pCut
     int x=0;
     int nBytes=-1;
     int nHeaders=0;
+    bool isFinal=false;
     for(int line=0; line<wndy; line++)
         {
         if(nHeaders==0)
             {
+            isFinal=false;
             nHeaders= (pCutPCache->pbCCutP[x]>>4) + 1;
             nBytes= pCutPCache->pbCCutP[x] & 15;
-            if(nBytes==HEADER_FINAL) nBytes=1;
+            if(nBytes==HEADER_FINAL)
+                {
+                nBytes=1;
+                isFinal= true;
+                }
             x++;
             }
+
+#ifdef SHOW_FINALS
+        if(isFinal)
+            {
+            actVid2MemBufp->b[((line) * vgax<<1) +1]=255;
+            actVid2MemBufp->b[((line) * vgax<<1) +3]=0;
+            }
+#endif
+
 //        printf("x %d %d %d\n", nHeaders, nBytes, pCutPCache->nbCCutP);
 
 
@@ -292,6 +348,60 @@ static int *getBestCutPs(CutPCache *pCut
     return piCutP;
 }
 
+static void deFinalize(CutPCache *pCutPCache){
+    static byte *pbCScratchPad= new byte[ (MAX_EQ_SETS+1)*wndy+10 ];
+
+    badFinalsLeft--;
+
+	int xIn=0;
+    int xOut=0;
+    int nInBytes=-1;
+    int nInHeaders=0;
+    bool isFinal=false;
+    do
+        {
+        if(nInHeaders==0)
+            {
+            isFinal=false;
+            nInHeaders= (pCutPCache->pbCCutP[xIn]>>4) + 1;
+            nInBytes= pCutPCache->pbCCutP[xIn] & 15;
+            if(nInBytes==HEADER_FINAL)
+                {
+                nInBytes=1;
+                isFinal= true;
+				pbCScratchPad[xOut]= ((nInHeaders-1)<<4) | 2;
+                }
+			else
+				{
+				pbCScratchPad[xOut]= pCutPCache->pbCCutP[xIn];
+				}
+			xOut++;
+            xIn++;
+            }
+
+		for(int i=0; i<nInBytes; i++)
+			{
+			if(isFinal)
+				{
+				pbCScratchPad[xOut]= pCutPCache->pbCCutP[xIn];
+				xOut++;
+				}
+			pbCScratchPad[xOut]= pCutPCache->pbCCutP[xIn];
+			xOut++;
+			xIn++;
+			}
+
+        nInHeaders--;
+        }while(xIn < pCutPCache->nbCCutP);
+
+    delete [] pCutPCache->pbCCutP;
+    pCutPCache->pbCCutP= new byte[xOut];
+    pCutPCache->nbCCutP= xOut;
+    memcpy(pCutPCache->pbCCutP, pbCScratchPad, xOut);
+
+   	pCutPCache->isDeFinal= 1;
+}
+
 static int addToLut(CutPCache *pNewCutPCache){
     int *piCutP= getBestCutPs(pNewCutPCache);
     int nTimesAdded=0;
@@ -381,16 +491,16 @@ static CutPCache *findInLutX(int *piCutP
 
                     if(bInc)
                         {
-                        pCutPCache->iScore++;
-                        if(pCutPCache->iScore > iBestScore)
+                        pCutPCache->score++;
+                        if(pCutPCache->score > iBestScore)
                             {
-                            iBestScore=pCutPCache->iScore;
+                            iBestScore=pCutPCache->score;
                             pBestCutPCache=pCutPCache;
                             }
                         }
                     else
                         {
-                        if(   pCutPCache->iScore>=SCORE_THRESHOLD
+                        if(   pCutPCache->score>=SCORE_THRESHOLD
                            && pCutPCache!=pMergeCutPCache && 0)
                             {
 
@@ -405,7 +515,7 @@ static CutPCache *findInLutX(int *piCutP
                             }
                         else
                             {
-                            pCutPCache->iScore=0;
+                            pCutPCache->score=0;
                             }
                         }
                     pCutPLut= pNextCutPLut;
@@ -487,10 +597,18 @@ static void cache(int *piCurrentCutP){
 
 
     int iBestScore;
-    if(pBestCutPCache!=NULL) iBestScore=pBestCutPCache->iScore;
+    if(pBestCutPCache!=NULL) iBestScore=pBestCutPCache->score;
     else                     iBestScore=0;
 
-    mergeAndClean(piCurrentCutP, pBestCutPCache);
+	static int deFinalWait= DEFINAL_WAIT;
+    if(iBestScore>=SCORE_THRESHOLD && deFinalWait<=0 && pBestCutPCache->isDeFinal==0 )
+		{
+		deFinalWait+= DEFINAL_WAIT;
+		deFinalize(pBestCutPCache);
+		}
+	if(iBestScore>=SCORE_THRESHOLD) deFinalWait--;
+
+	mergeAndClean(piCurrentCutP, pBestCutPCache);
 
     if(pBestCutPCache!=NULL && iBestScore>=SCORE_THRESHOLD)
         {
@@ -513,7 +631,8 @@ static void cache(int *piCurrentCutP){
         CutPCache *pNewCutPCache= new CutPCache;
 
         pNewCutPCache->nUsed=0;
-        pNewCutPCache->iScore=0;
+        pNewCutPCache->score=0;
+		pNewCutPCache->isDeFinal=1;
         pNewCutPCache->pbCCutP=NULL;
         addCutPsToCache(pNewCutPCache, piCurrentCutP);
 
@@ -532,9 +651,14 @@ static void cache(int *piCurrentCutP){
     drop=true;
     nFieldsUnCached++;
     iCurrentLUTScore=iBestScore;
-    iCurrentUsed=pBestCutPCache->nUsed;
+    iCurrentUsed=0;
 
-    if(iBestScore<SCORE_THRESHOLD || some==0) return;
+#ifdef NO_CACHE
+	return;
+#endif
+    if(iBestScore<SCORE_THRESHOLD) return;
+
+    iCurrentUsed=pBestCutPCache->nUsed;
     nFieldsUnCached--;
     nFieldsCached++;
 
@@ -556,8 +680,11 @@ void saveVCCache(void){
 
     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);
+
+   	char magicId[5]=MAGIC_ID;
+    fwrite(&magicId, 4*sizeof(char), 1, pFCache);
+	int reserved=0;
+    fwrite(&reserved, sizeof(int), 1, pFCache);
     CutPCache *tpCutPCache= pGoodCutPCache->pPrev;
 
     for(int i=0; i<nGoodCaches; i++)
@@ -587,12 +714,12 @@ 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);        
+    int reserved;
+    fread(&reserved, sizeof(int), 1, pFCache);       
+    fread(&reserved, sizeof(int), 1, pFCache);        
     CutPCache *tpCutPCache;
 
-    for(int i=0; i<nEntGood+nEntBad; i++)
+    while( !feof(pFCache) )
         {
         tpCutPCache= new CutPCache;
 
@@ -605,7 +732,8 @@ static void loadVCCache(void){
         fread(tpCutPCache->pbCCutP, sizeof(byte),
               tpCutPCache->nbCCutP, pFCache);
 
-        addToCache(tpCutPCache);
+        if(tpCutPCache->isDeFinal==0) badFinalsLeft++;
+		addToCache(tpCutPCache);
 //        printf("B\n");
         addToLut(tpCutPCache);
 //        printf("A\n");
@@ -616,10 +744,22 @@ static void loadVCCache(void){
 
 void vc_decrypt(void){
  int cutPointX[max_y];
+ cutPointX[0]=
+ cutPointX[1]=0;
  int addapBuff[max_x];
  char textbuf[9][128];
  long T1=0, T2;
  color c;
+ 
+ static int edgePos=1;
+ static int edgeAlgo=0;
+/* static int *pEdgeArea=NULL;
+ if(pEdgeArea==NULL)
+ 	{
+	pEdgeArea= new int[ (EDGE_AREA_END - EDGE_AREA_START) ];
+	memset( pEdgeArea, 0, (EDGE_AREA_END - EDGE_AREA_START)*sizeof(int));  
+	}
+*/
 
 // const int lowResSize= mmx ? DWN_SMP : (DWN_SMP-1);
 
@@ -632,13 +772,13 @@ void vc_decrypt(void){
 
 // showPoints=true;
 
- const double freqPix2= double(wndx) / double(x_field) * FREQ_PIX;
+ const double freqPix2= double(wndx) / double(x_field) * FREQ_PIX; //FIX CHECK (xfield...)
 #ifdef DEBUG2
  printf("xy%d %d\n", wndx, x_field);
 #endif
                            //  18
 // const int vcStartX=int(double(16 -scales_x+3)/x_field*wndx + .5) + some - 10;
- const int vcStartX=int(double(16 -scales_x+3)/x_field*wndx + .5);
+ const int vcStartX=int(double(16 -scales_x+3)/x_field*wndx + .5);    //FIX CHECK
 
  static FILE *pF= fopen("out.dat", "wb");
 
@@ -649,13 +789,13 @@ void vc_decrypt(void){
  if(wndx != wndxBak){
    for(int x=0; x<max_x; x++){       
 
-     const int cutP=int( double(x) / ( 3.0 * freqPix2) * FREQ_SMP
-                             - (NO_POINT_LAND1)/3.0 + .5);
+     const int cutP=int( round(
+        double(x) / ( 3.0 * freqPix2) * FREQ_SMP - (NO_POINT_LAND1)/3.0 ) );
      x2CutP[x]= cutP;
    }
    for(int cutP=0; cutP<256; cutP++){
-     const int x=int( double(cutP*3 + NO_POINT_LAND1)
-                                                 * freqPix2 / FREQ_SMP + .5);
+     const int x=int( round(
+        double(cutP*3 + NO_POINT_LAND1) * freqPix2 / FREQ_SMP ) );
      cutP2X[cutP]= x;
    }
    wndxBak=wndx;
@@ -671,23 +811,22 @@ void vc_decrypt(void){
 
 // const int minDist= cutP2X[0];
 
- cutPointX[0]=0;
-
  char *lowRes[2];
 
  lowRes[0]= (char*)newAlign(vcDiff, 8);
  lowRes[1]= (char*)newAlign(vcDiff, 8);
 
- int *convVec= new int[vcDiff>>(PHASE_CMP_STEP-1)];
+ int *convVec= new int[ (vcDiff>>(PHASE_CMP_STEP-1)) + 1 ];
+ memset(convVec, 0, ( (vcDiff>>(PHASE_CMP_STEP-1)) + 1 )*sizeof(int) );
 
- long TCorr=0, TReSmp=0, TUVDetect=0, TPhaseDiff1=0,
-      TPhaseDiff2_Drift=0, TEdgeDetect=0 ,TCache=0;
+ long TCorr=0, TReSmp=0, TUVDetect=0, TPhaseDiff1=0, TPhaseCorrect=0,
+      TPhaseDiff2_Drift=0, TEdgeDetect=0 ,TEdgeArea=0, TCache=0;
 
  if(iState==2){
    T1=uclock();
  }
 
- static int edgeLut[512];
+ static int edgeLut[2048];
  static int phaseLut[256];
  static bool first= true;
 
@@ -701,26 +840,40 @@ void vc_decrypt(void){
  static int fields=0;
  fields++;
 
+ static bool findPhases=true;
+ static double phaseCorrect=0;
+ static double lastPhaseCorrect=0;
+ if(mabs(phaseCorrect-lastPhaseCorrect) > PHASE_CORRECT_THRESHOLD)
+ 	{
+	lastPhaseCorrect= phaseCorrect;
+	findPhases=true;
+	}
+
+ if(findPhases)
+ 	{
+   	for(int cutP=0; cutP<256; cutP++)
+		{
+     	const double phase=   PHASE_SMP*NO_POINT_LAND1 - PHASE_LINE 
+	 	 					+ PHASE_POINT*cutP - phaseCorrect;
+     	phasePerCutP[cutP]= (byte)( round( phase * 128 / PI) );
+     	sin1PerCutP[cutP]= char( round( sin( -phase + PHASE_SEG )*127 ) );
+     	cos1PerCutP[cutP]= char( round( cos( -phase + PHASE_SEG )*127 ) );
+     	sin2PerCutP[cutP]= char( round( sin( -phase )*127 ) );
+     	cos2PerCutP[cutP]= char( round( cos( -phase )*127 ) );
+   		}
+	}
+
  if(first){
-   if(first) some=10;
+//   if(first) some=10;
 
-   for(int i=0; i<512; i++){
+  for(int i=0; i<2048; i++){
      const int j= min(i+1, EDGE_LIMIT);
-     edgeLut[i]=int( log(j)*EDGE_COEFF );
+     edgeLut[i]=int( log(j)*EDGE_COEFF +.5 );
    }
 
    for(int i=0; i<256; i++){                           
      const int j= max(min(mabs(i-128), PHASE_LIMIT), 1);
-     phaseLut[i]=int( pow(j, PHASE_EXP)*PHASE_COEFF );
-   }
-
-   for(int cutP=0; cutP<256; cutP++){
-     const double phase= PHASE_SMP*(NO_POINT_LAND1 + PHASE_PATCH) + PHASE_POINT*cutP; // - some/360.0*2.0*PI;
-     phasePerCutP[cutP]= (byte)( phase * 128 / PI );
-     sin1PerCutP[cutP]= char(sin( -phase + PHASE_SEG )*127);
-     cos1PerCutP[cutP]= char(cos( -phase + PHASE_SEG )*127);
-     sin2PerCutP[cutP]= char(sin( -phase )*127);
-     cos2PerCutP[cutP]= char(cos( -phase )*127);
+     phaseLut[i]=int( pow(j, PHASE_EXP)*PHASE_COEFF +.5);
    }
 
    first=false;
@@ -734,10 +887,11 @@ void vc_decrypt(void){
  const float cosSegPhase2= cos(PHASE_SEG*2);
 
  bool *black= new bool[wndy];
+ black[0]= true;
 
- byte * llinep=&actVid2MemBufp->b[ -(vgax<<2) ];
- byte * linep= &actVid2MemBufp->b[ -(vgax<<1) ];
- for(int line=0; line<wndy; line++){
+ byte * llinep=&actVid2MemBufp->b[ -(vgax<<1) ];
+ byte * linep= &actVid2MemBufp->b[ 0 ];
+ for(int line=1; line<wndy-1; line++){
    llinep+=vgax<<1;
    linep+=vgax<<1;
 
@@ -751,10 +905,10 @@ void vc_decrypt(void){
         {
         int iMean;
         asm(//"int $3 \n\t"
-
          "xorl %%ecx, %%ecx                   \n\t"
+         ".align 3                            \n\t"
          "1:                                  \n\t"
-         "movzbl 2(%%esi, %%edx, 4), %%eax    \n\t"  
+         "movzbl 0(%%esi, %%edx, 4), %%eax    \n\t"  
          "movzbl 2(%%esi, %%edx, 4), %%ebx    \n\t"  
          "addl %%eax, %%ebx                   \n\t"  
          "shrl $1, %%ebx                      \n\t"
@@ -770,7 +924,9 @@ void vc_decrypt(void){
           : "%eax", "%ebx", "%edx", "%esi", "%edi");
 
         if(double(iMean) / double(vcDiff>>1) < BAD_MEAN_THRESHOLD)
-            black[line]=true;
+            {
+            black[    line           ]=true;
+            }
         }
    else if(mmx){
      asm(//"int $3 \n\t"
@@ -842,7 +998,7 @@ void vc_decrypt(void){
      T1=T2;
    }
 
-   if(line==0) continue;
+   if(line<2) continue;
 
    int bestDiff=0x1FFFFFFF;
    int avgDiff=0;             // for detecting bad lines
@@ -900,10 +1056,17 @@ void vc_decrypt(void){
     }
 
    asm("emms\n\t");
-
-   if( int( avgDiff*BAD_THRESHOLD1/float(avgDiffNum) ) - BAD_THRESHOLD2
-                                                                 < bestDiff )
-       black[line]=true;
+/*
+   bestCutPX= bestCutPX+some-10;
+   if(bestCutPX<0) bestCutPX+=vcDiff;
+   if(bestCutPX>=vcDiff) bestCutPX-=vcDiff;
+  */
+   if( int( avgDiff*BAD_THRESHOLD1/float(avgDiffNum) ) - BAD_THRESHOLD2 < bestDiff 
+   		&& !black[line-1] && !black[line])
+        {
+        black[line-1]=true;
+        black[line  ]=true;
+        }
 
    if(iState==2){
      T2=uclock();
@@ -911,10 +1074,9 @@ void vc_decrypt(void){
      T1=T2;
    }
 
-   if(line>2){
-
-     int sumI=0;
-     for(int x=-2; x<2; x+=2){
+//   bestCutPX+=some-10;
+   int sumI=0;
+   for(int x=-2; x<2; x+=2){
        const int nx= x + (vcDiff & ~1);
        const int ul= char(linep[ (((vcStartX & ~1) +  x)<<1) + 0]);
        const int vl= char(linep[ (((vcStartX & ~1) +  x)<<1) + 2]);
@@ -923,17 +1085,16 @@ void vc_decrypt(void){
        sumI+= ul*vn - vl*un;
 //       linep[ (((vcStartX & ~1) - x)<<1) + 1]=255;
 //       linep[ (((vcStartX & ~1) - nx)<<1) + 1]=255;
-     }
+   }
   
-     if(line>10 && line<250){
-       if(line & 1){
+   if(line>10 && line<250){
+   	   if(line & 1){
          vScore+= sumI;
        }else{
          vScore-= sumI;
        }
-     }
-              
    }
+              
 
    bestCutPX+= cutPointX[line-1];
    if(bestCutPX>=vcDiff) bestCutPX-=vcDiff;
@@ -951,7 +1112,7 @@ void vc_decrypt(void){
 
  int iSumOfAbsDiff=0;
  int iCount=1;
- for(int xLine=1; xLine<wndy; xLine++)
+ for(int xLine=2; xLine<wndy-1; xLine++)
     {
     if( black[xLine] || black[xLine-1]) continue;
 
@@ -972,53 +1133,53 @@ void vc_decrypt(void){
 
  const int driftPenalty[MAX_DRIFT*2+1]= DRIFT_TAB;
 
+ byte *pPhaseErr= new byte[256*wndy];
+
  llinep=&actVid2MemBufp->b[ -(vgax<<2) ];
  linep= &actVid2MemBufp->b[ -(vgax<<1) ];
  if(bVC){
-    int *newVal=new int[256], *lastVal=new int[256];
+    int *newVal=new int[257], *lastVal=new int[257];
     memset(lastVal, 0, 256<<2);
+	lastVal[256]=1; //for edge detect (faster at skiping zeros)
 
-    byte *newPhaseErr=new byte[256], *lastPhaseErr=new byte[256];
-    memset(lastPhaseErr, 0, 256);
+//    byte *newPhaseErr=new byte[256], *lastPhaseErr=new byte[256];
+//    memset(lastPhaseErr, 0, 256);
 
-    for(int line=0; line<wndy; line++){
+    for(int line=0; line<wndy-1; line++){
         llinep+=vgax<<1;
         linep+=vgax<<1;
 
         memset(newVal, 0, 256<<2);
-        memset(newPhaseErr, 0, 256);
+		newVal[256]=1; //for edge detect (faster at skiping zeros)
+//        memset(newPhaseErr, 0, 256);
+//	    memset(pPhaseErr+line*256, 0, 256);
         bool noLeft=true;
 
         bestDir[line]=-1;
 
-        if(line==0) continue;
+        if(line<1) 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);
+        for(int x=8; x<vcDiff-8; x+= 1<<PHASE_CMP_STEP){
+            int nx= x + relCutPX;
+            if(nx>=vcDiff) nx-= vcDiff;
+            nx&= ~1;              //Changed
             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 un= char( linep[ (((vcStartX & ~1) + nx)<<1) + 0]); //HOTSPOT 2.7%
             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;
+        if(line<2) chromAmp=1;
    
 //   linep[ ((vcStartX +  0)<<1) + 1]=255;
-//   linep[ ((vcStartX +  relCutPX)<<1) + 1]=200;
+ //  linep[ ((vcStartX +  relCutPX)<<1) + 1]=200;
 
         if(iState==2){
             T2=uclock();
@@ -1026,91 +1187,90 @@ void vc_decrypt(void){
             T1=T2;
         }
 
-        int sumR0=0, sumI0=0;
-        int sumR1=0, sumI1=0;
+        int rrR=0, rrI=0;
+        int rlR=0, rlI=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];
+                rrR+= convVec[p  ];
+                rrI+= 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];
+                rlR+= convVec[p  ];
+                rlI+= convVec[p+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 ffR0=0    , ffI0=0;
-        int ssR0=sumR0, ssI0=sumI0;
-        int ffR1=0    , ffI1=0;
-        int ssR1=sumR1, ssI1=sumI1;
+		int llR=0, llI=0;
+		int lrR=0, lrI=0;
+        int rlRC= int( round( rlR*cosSegPhase   + rlI*sinSegPhase*s) );
+        int rlIC= int( round(-rlR*sinSegPhase*s + rlI*cosSegPhase  ) ); 
+		int lrRC=0;
+		int lrIC=0;
 
         int lastPhasePos=0;
             
         byte ang=0;
-        if(!black[line]){
+        if(!black[line] && !black[line-1]){
             for(int lastCutP = 0; lastCutP < 256; lastCutP++){
-                if(lastVal[lastCutP]==0) continue;
+				while(lastVal[lastCutP]==0) lastCutP++;
+				if(lastCutP>=256) break;
 
-                const int newPhasePos= cutP2X[ lastCutP ] & ((-1) << PHASE_CMP_STEP);
+                const int newPhasePos=
+                    min(
+                        (cutP2X[ lastCutP ]+(1 << (PHASE_CMP_STEP-1)) )
+                            & ((-1) << PHASE_CMP_STEP),
+                        (vcDiff-1) & ((-1) << PHASE_CMP_STEP)
+                       );
 
-                if( newPhasePos >= lastPhasePos ){
+	            if( newPhasePos >= lastPhasePos )
+					{
 
-                    for(; lastPhasePos <= newPhasePos; lastPhasePos += (1<<PHASE_CMP_STEP)){
+                    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(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 );
-                            }
+						if(   lastPhasePos+relCutPX >= vcDiff 
+						   && lastPhasePos+relCutPX < vcDiff+(1<<PHASE_CMP_STEP) )
+							{
+							rrR=rlR, rrI=rlI;
+							lrR=llR, lrI=llI;
+							llR=0  , llI=0;
+							rlR=0  , rlI=0;
+					        lrRC= int( round( lrR*cosSegPhase   + lrI*sinSegPhase*s) );
+     					    lrIC= int( round(-lrR*sinSegPhase*s + lrI*cosSegPhase  ) ); 
+							rlRC=0;
+							rlIC=0;
+							}
 
-                            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);
+                      	rrR-=convVec[p  ];
+                        rrI-=convVec[p+1];
+         
+                        if(isLast)
+							{
 
-                                ang= (byte)( atan3(i, r)/PI*128 );
+                        	const float r= rrR + rlRC + lrRC
+                                    + llR*cosSegPhase2
+                                    + llI*sinSegPhase2*s;
+                            const float i= rrI + rlIC + lrIC
+                                    - llR*sinSegPhase2*s
+                                    + llI*cosSegPhase2;                                    
+            
+                            ang= (byte)round( atan3(i, r)/PI*128 );
                             }
 
-                            ffR1+=convVec[p  ];
-                            ffI1+=convVec[p+1];
-                        }
-
+                        llR+=convVec[p  ];
+                        llI+=convVec[p+1];
+	                    }
+	
                     }
-                }
 
                 int newXTemp= cutPointX[line] - cutPointX[line-1] + cutP2X[lastCutP];
                 if(newXTemp <  0     ) newXTemp+= vcDiff;
@@ -1124,10 +1284,11 @@ void vc_decrypt(void){
                 const int *piDrift=
                     &driftPenalty[newCutPStart - (x2CutP[ newXTemp ] - MAX_DRIFT)-1];
 
-                const byte a= (line & 1) ^ ( vScore>0 ) ? -ang: ang;
+                byte a= (line & 1) ^ ( vScore>0 ) ? -ang: ang;
+                a+=256/2;
 
                 const int phase1= (a + phasePerCutP[ lastCutP ]) & 0xFF;
-                const int lastPhaseErrLastCutP= lastPhaseErr[lastCutP] + 128;
+                const int lastPhaseErrLastCutP= pPhaseErr[(line-1)*256 + lastCutP] + 128;
           // FIX (ASM_OPTIMIZE)
                 if(chromAmp>AMP_THRESHOLD){
                     for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
@@ -1139,7 +1300,7 @@ void vc_decrypt(void){
                         if(newVal[newCutP] < val){
                             newVal[newCutP]= val;
                             dir[(line<<8) + newCutP]= lastCutP;
-                            newPhaseErr[newCutP]= phaseErr;
+                            pPhaseErr[line*256 + newCutP]= phaseErr;
         
                             noLeft=false;
                         }
@@ -1148,13 +1309,13 @@ void vc_decrypt(void){
                 else{
                     linep[ ((vcStartX -  1)<<1) + 1]=255;
                     for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
-                        const int phaseErr= (phase1 + phasePerCutP[ newCutP ]) & 0xFF;
+//                        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;
+                            pPhaseErr[line*256 + newCutP]= 0; //phaseErr;
         
                             noLeft=false;
                         }
@@ -1169,27 +1330,67 @@ void vc_decrypt(void){
             T1=T2;
         }
 
-        if(!black[line]){
+        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];
+            if(edgeAlgo==0)
+				{
+	            for(int newCutP = 0; newCutP < 256; newCutP++)
+					{
+					if(!noLeft)
+						{
+						while(newVal[newCutP]==0) newCutP++;
+						}
+					if(newCutP>=256) break;
 
-                const int diff=edgeLut[ mabs( + int(* p   ) + int(*(p+2))
-                                              - int(*(p+4)) - int(*(p+6)) ) ];
+					
+    	            const byte * const p=
+        	            &linep[ ((cutP2X[ newCutP ] + vcStartX + edgePos)<<1) + 1];  // (-0) - (-4) 
+
+            	    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;
+    	            	}
+			
+            		}
+        		}
+            else 
+				{
+	            for(int newCutP = 0; newCutP < 256; newCutP++)
+					{
+					if(!noLeft)
+						{
+						while(newVal[newCutP]==0) newCutP++;
+						}
+					if(newCutP>=256) break;
 
-                if(iBestVal < newVal[newCutP]){
-                    iBestVal= newVal[newCutP];
-                    bestDir[line]= newCutP;
-                }
+					
+    	            const byte * const p=
+        	            &linep[ ((cutP2X[ newCutP ] + vcStartX + edgePos)<<1) + 1];  
 
-            }
-        }
+            	    const int diff=edgeLut[ mabs( + int(* p   ) + int(*(p+2))
+	                                              - int(*(p+6)) - int(*(p+8)) ) ]; //no converting factor!
+  
+	                if(newVal[newCutP]==0) newVal[newCutP] =  diff + 1000000000;
+    	            else                   newVal[newCutP] += diff;
+			
+            	    if(iBestVal < newVal[newCutP])
+						{
+                    	iBestVal= newVal[newCutP];
+	                    bestDir[line]= newCutP;
+    	            	}
+			
+            		}
+       			}
+			}
    
         if(iState==2){
             T2=uclock();
@@ -1201,22 +1402,19 @@ void vc_decrypt(void){
         lastVal=newVal;
         newVal=tempVal;
 
-        byte *tempErr=lastPhaseErr;
-        lastPhaseErr=newPhaseErr;
-        newPhaseErr=tempErr;
     }
 
 
     delete [] newVal;
     delete [] lastVal;
-
-    delete [] newPhaseErr;
-    delete [] lastPhaseErr;
  }
  delete [] convVec;
 
 
  int piBestCutP[600];
+ piBestCutP[0     ]=
+ piBestCutP[wndy-1]=-1;
+
  int iBestCutP=-1;
  for(int line=wndy-2; line>0; line--)
     {
@@ -1248,14 +1446,166 @@ void vc_decrypt(void){
 #endif
 
  if(bVC) cache(piBestCutP);
+	
 
  if(iState==2)
     {
     T2=uclock();
     TCache+=T2-T1-uclockWaste;
     T1=T2;
-    }
+    }							 
+ 
+ int phaseErrSum=0;
+ for(int line=10; line<wndy-10; line++)
+ 	{
+	if(black[line] || black[line-1]) continue;
+	int phaseErr= pPhaseErr[ line*256 + piBestCutP[line] ];
+	if(phaseErr>128) phaseErr-=256;
+	phaseErrSum+= phaseErr;
+	}
+
+ if(bVC) phaseCorrect-= double(phaseErrSum) / FIELDS_PER_PHASE_CHECK / wndy / 2 /128.0*PI;
+
+ if(phaseCorrect>MAX_PHASE_CORRECT) phaseCorrect= MAX_PHASE_CORRECT;
+ if(phaseCorrect<MIN_PHASE_CORRECT) phaseCorrect= MIN_PHASE_CORRECT;
+
+ 	
+ delete [] pPhaseErr;
+
+ if(iState==2)
+    {
+    T2=uclock();
+    TPhaseCorrect+=T2-T1-uclockWaste;
+    T1=T2;
+    }							 
+	
+
+ int ppEdgeAreaScore[2][ (EDGE_AREA_END - EDGE_AREA_START) ];
+ memset( ppEdgeAreaScore[0], 0, (EDGE_AREA_END - EDGE_AREA_START)*sizeof(int));
+ memset( ppEdgeAreaScore[1], 0, (EDGE_AREA_END - EDGE_AREA_START)*sizeof(int));
+ for(int line=10; line<wndy-10; line+=3)
+ 	{   
+	if(!black[line] && piBestCutP[line]!=-1)
+		{
+        for(int x = EDGE_AREA_START; x<EDGE_AREA_END; x++)
+			{
+            const byte * const p=
+			 	&actVid2MemBufp->b
+					[
+					+(line * (vgax<<1))
+			 		+((cutP2X[ piBestCutP[line] ] + vcStartX + x)<<1) + 1
+					];
+
+            int pDiff[2];
+			pDiff[0]= edgeLut[ mabs( + int(* p   ) + int(*(p+2))
+                                     - int(*(p+4)) - int(*(p+6)) )     ];
+            pDiff[1]= edgeLut[ mabs( + int(* p   ) + int(*(p+2))
+                                     - int(*(p+6)) - int(*(p+8)) )*7/8 ];
+
+  		    ppEdgeAreaScore[0][x-EDGE_AREA_START]+= pDiff[0];
+  		    ppEdgeAreaScore[1][x-EDGE_AREA_START]+= pDiff[1];
+            }
+        }
+	}
+
+ int pBestEdgeScore[2]={0,0};
+ int pBestEdgePos[2]={0,0};
+ for(int a=0; a<2; a++)
+ 	{
+ 	for(int x = EDGE_AREA_START; x<EDGE_AREA_END; x++)
+		{
+		if(ppEdgeAreaScore[a][x-EDGE_AREA_START] > pBestEdgeScore[a])
+			{
+			pBestEdgeScore[a]= ppEdgeAreaScore[a][x-EDGE_AREA_START];
+			pBestEdgePos[a]= x-EDGE_AREA_START;
+			}
+		}
+	}
+
+ double bestFit=0;
+ int bestEdgeAlgo=0;
+ int bestEdgePos=0;
+ for(int a=0; a<2; a++)
+ 	{
+	int larger= max( 
+		ppEdgeAreaScore[a][ (pBestEdgePos[a]-1<0) ? (pBestEdgePos[a]+1) : (pBestEdgePos[a]-1) ],
+		ppEdgeAreaScore[a][ (pBestEdgePos[a]+1>=(EDGE_AREA_END - EDGE_AREA_START)) ? 
+  					 							   (pBestEdgePos[a]-1) : (pBestEdgePos[a]+1) ]
+		);
+
+	double fit= double(pBestEdgeScore[a]) / double(larger+.0001);
+	if(fit>bestFit)
+		{
+		bestFit=fit;
+		bestEdgeAlgo=a;
+		bestEdgePos= pBestEdgePos[a] + EDGE_AREA_START;
+		}
+	}
 
+ int linesOkEdge=0;
+ int lines=0;
+ for(int line=10; line<wndy-10; line+=3)
+ 	{   
+	if(!black[line] && piBestCutP[line]!=-1)
+		{
+		int goodPos= 0;
+		int goodScore= 0;
+		lines++;
+
+        for(int x = EDGE_AREA_START; x<EDGE_AREA_END; x++)
+			{
+            const byte * const p=
+			 	&actVid2MemBufp->b
+					[
+					+(line * (vgax<<1))
+			 		+((cutP2X[ piBestCutP[line] ] + vcStartX + x)<<1) + 1
+					];
+
+            int pDiff[2];
+			pDiff[0]= edgeLut[ mabs( + int(* p   ) + int(*(p+2))
+                                     - int(*(p+4)) - int(*(p+6)) )     ];
+            pDiff[1]= edgeLut[ mabs( + int(* p   ) + int(*(p+2))
+                                     - int(*(p+6)) - int(*(p+8)) )*7/8 ];
+
+			if(pDiff[bestEdgeAlgo] > goodScore)
+				{
+				goodScore= pDiff[bestEdgeAlgo];
+				goodPos= x;
+				}
+			}
+		if(goodPos == bestEdgePos)
+			linesOkEdge++;
+        }
+	}
+
+if(lines>MIN_LINES_FOR_EDGE_AREA && lines*EDGE_CHANGE_THRESHOLD<linesOkEdge)
+	{
+	edgePos= bestEdgePos;
+	edgeAlgo= bestEdgeAlgo;
+	}
+
+
+#ifdef SHOW_EDGE_AREA
+	for(int line=10; line<wndy-10; line+=3)
+		{
+		actVid2MemBufp->b
+			[
+			+(line * (vgax<<1))
+ 			+((cutP2X[ piBestCutP[line] ] + vcStartX + edgePos)<<1) + 1
+ 			]= 255;
+		actVid2MemBufp->b
+			[
+			+(line * (vgax<<1))
+ 			+((cutP2X[ piBestCutP[line] ] + vcStartX + edgePos)<<1) + 7
+ 			]= 255;
+		}
+#endif
+ if(iState==2)
+    {
+    T2=uclock();
+    TEdgeArea+=T2-T1-uclockWaste;
+    T1=T2;
+    }
 
  if(showPoints)
     {
@@ -1313,14 +1663,19 @@ void vc_decrypt(void){
                 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);
+//                const char u1=(char)(( + v*c1 + u*s1)>>7);
+//                const char v1=(char)(( - v*s1 + u*c1)>>7);
+
+                const char u1=(char)(( + u*c1 - v*s1)>>7);
+                const char v1=(char)(( - u*s1 - v*c1)>>7);
+
 
                 if( (vcDiff - cutX) & 1 ){
                     * p   = v1;
                     *(p+2)= u1;
                     *(p+4)= v1;
                     *(p+6)= u1;
+
                 }
                 else{
                     * p   = u1;
@@ -1335,8 +1690,12 @@ void vc_decrypt(void){
                 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);
+//                const char u1=(char)(( + v*c2 + u*s2)>>7);
+//                const char v1=(char)(( - v*s2 + u*c2)>>7);
+
+                const char u1=(char)(( + u*c2 - v*s2)>>7);
+                const char v1=(char)(( - u*s2 - v*c2)>>7);
+
        
                 if( cutX & 1 ){
                     * p   = v1;
@@ -1417,7 +1776,9 @@ void vc_decrypt(void){
         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);
+        sprintf(textbuf[7],"%f FindPhaseCorrect", (float)(TPhaseCorrect)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[7],"%f EdgeArea", (float)(TEdgeArea)/UCLOCKS_PER_SEC);
+        sprintf(textbuf[8],"%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]);
@@ -1427,6 +1788,7 @@ void vc_decrypt(void){
         gprint(infoPosX, infoPosY+=10, c.col, textbuf[5]);
         gprint(infoPosX, infoPosY+=10, c.col, textbuf[6]);
         gprint(infoPosX, infoPosY+=10, c.col, textbuf[7]);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[8]);
         infoPosY+=5;
         T1=T2;                           
     }
@@ -1467,6 +1829,14 @@ void vc_decrypt(void){
 
         gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
 
+
+        sprintf(textbuf[1],"Bad Final Left %d", badFinalsLeft);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
+
+
+        sprintf(textbuf[1],"phaseCorrect %3.1f", phaseCorrect/PI*180);
+        gprint(infoPosX, infoPosY+=10, c.col, textbuf[1]);
+
         }
 
   delete [] bestDir;

Modified: trunk/2010/2_vc.h
==============================================================================
--- trunk/2010/2_vc.h	(original)
+++ trunk/2010/2_vc.h	Wed Jul  4 10:47:18 2007
@@ -9,7 +9,10 @@ struct CutPCache{
     byte *pbCCutP;
     CutPCache *pNext;
     CutPCache *pPrev;
-    int iScore;
+    byte score;
+	byte isDeFinal;
+	byte reserved1;
+	byte reserved2;
     int nUsed;
     int nbCCutP;
 };

Modified: trunk/2010/2d
==============================================================================
--- trunk/2010/2d	(original)
+++ trunk/2010/2d	Wed Jul  4 10:47:18 2007
@@ -1,10 +1,31 @@
-load ?
-vsmooth
-vc color
-vc rightdrift ?
+txt restart!?
+rgb565 / 1555 stuff not realy correct but works
+add rgb1555 support
+add better support for hi-res (800x600) or so
+
+decomb 2 pass in nagra ?
+
+double buffer code sucks (field droping is wrong)
+
+vtune detected hotspot at decomb(1,2)+4 and CopyLoop1+4    ?
 
 vc (black like)
 vc exact start/end lines (last one not in 300x400)
-vc use amp to reject phase
-crypt decomb (math proofs and fixPoint Arith)
-double2float
+vc use amp to reject phase (?)
+vc integral phase test
+vc use MulTab in color deco
+vc !oneField
+vc optimize 2algo
+
+vc color deco mmx
+
+nag last line ?
+
+yuv2rgb mmx
+
+NAG_LINES ?
+
+teletext:  
+ BETTER decode
+ SAVE
+ fix fractional digits

Modified: trunk/2010/makefile
==============================================================================
--- trunk/2010/makefile	(original)
+++ trunk/2010/makefile	Wed Jul  4 10:47:18 2007
@@ -1,7 +1,8 @@
 .SUFFIXES: .c .cc .cpp .h .o .asm .S .s
 
 OPTS = -g
-OPTS = -O5 -mpentiumpro -fomit-frame-pointer -g -Wall -Wstrict-prototypes -malign-double -ffast-math -funroll-loops -fno-default-inline
+OPTS = -O5 -mpentiumpro -fomit-frame-pointer -g -Wall -Wstrict-prototypes -malign-double -ffast-math -funroll-all-loops -fno-default-inline 
+OPTS = -O5 -mpentiumpro -fomit-frame-pointer -g -Wall -Wstrict-prototypes -malign-double -ffast-math -funroll-all-loops -fno-default-inline 
 AFLAGS = -f coff
 
 .c.o:   ; gcc $(CFLAGS) -c $<



More information about the Mndiff-dev mailing list