[MN-dev] [mndiff]: r63 - in trunk/2010: 2010.cpp 2_71x6.cpp 2_71x6.h 2_crypt.cpp 2_file.cpp 2_file.h 2_grafix.cpp 2_hw_mem.cpp 2_hw_mem.h 2_nag.cpp 2_txt.cpp 2_vc.cpp 2_vpaytv.cpp 2_vpaytv.h makefile

michael subversion at mplayerhq.hu
Wed Jul 4 10:58:24 CEST 2007


Author: michael
Date: Wed Jul  4 10:58:24 2007
New Revision: 63

Log:
version from 2000-02-03 17:54


Added:
   trunk/2010/2_vpaytv.cpp
   trunk/2010/2_vpaytv.h
Modified:
   trunk/2010/2010.cpp
   trunk/2010/2_71x6.cpp
   trunk/2010/2_71x6.h
   trunk/2010/2_crypt.cpp
   trunk/2010/2_file.cpp
   trunk/2010/2_file.h
   trunk/2010/2_grafix.cpp
   trunk/2010/2_hw_mem.cpp
   trunk/2010/2_hw_mem.h
   trunk/2010/2_nag.cpp
   trunk/2010/2_txt.cpp
   trunk/2010/2_vc.cpp
   trunk/2010/makefile

Modified: trunk/2010/2010.cpp
==============================================================================
--- trunk/2010/2010.cpp	(original)
+++ trunk/2010/2010.cpp	Wed Jul  4 10:58:24 2007
@@ -1,4 +1,3 @@
-
 //2010 0.1 Copyright (c) 1998 Michael Niedermayer
 
 #include <stdlib.h>
@@ -45,6 +44,7 @@
  extern int reqPage, reqSubPage, reqChanNum;
  extern char reqChanStr[];
  extern char findTxt[];
+ extern bool isVCPhaseTrick;
 
  int maxBuf=2;
  int some=0;
@@ -53,6 +53,7 @@
 int _crt0_startup_flags = _CRT0_FLAG_NEARPTR | _CRT0_FLAG_NONMOVE_SBRK;
  bool fShowRaw=false;
  int grabf;
+ int grabV=0;
  int fysize;
  byte font[4096];
  int vid=0;
@@ -137,6 +138,7 @@ int error(error_code e){
                      break;
  }
 
+// asm("int $3\n\t");
  exit(e);
 }
 
@@ -330,6 +332,7 @@ int main(int argc, char **argv){
    infoPosX=10;
 
    if(grabf==2 && TVStd!=TXTPAL) GrabRaw(), grabf=0;
+//   if(grabf==2) GrabRaw(), grabf=0; //REMOVE
 
    if(fShowRaw) ShowRaw();
 
@@ -337,6 +340,8 @@ int main(int argc, char **argv){
    showStuff();
 
    if(grabf==1 && TVStd!=TXTPAL) GrabBmp(), grabf=0;
+   
+   	if(grabV==1) RecordVideo();
 
    for(int k=0; k<delay*10000000; k++);
 
@@ -375,11 +380,17 @@ int main(int argc, char **argv){
      }
 
      switch(menuLevel){
-       case mLNorm : switch(key){
-                       case 'p'  :
-                       case 'P'  : getkey();
-                       break;
-                       case 'g'  : key=0, grabf=1;
+     	case mLNorm : switch(key){
+				case 'p'  :
+				case 'P'  : getkey();
+				break;
+				
+				case 'r'  : key=0, 
+							grabV&=1;
+							grabV^=1;
+				break;			
+                       
+					   case 'g'  : key=0, grabf=1;
                        break;
                        case 'G'  : key=0, grabf=2;
                        break;
@@ -420,6 +431,9 @@ int main(int argc, char **argv){
                        break;
                        case 's'  : menuLevel=mLTVStd, helpState=false;
                        break;
+                       case 't'  :
+                       case 'T'  : isVCPhaseTrick=true;
+                       break;
                        case 'i'  :
                        case 'I'  : iState++; iState%=ISTATES;
                        break;
@@ -531,6 +545,14 @@ int main(int argc, char **argv){
                                    setStdScale();
                                    contGrab();
                        break;
+                       case '7'  : TVStd= PAL;
+                                   cryptStd= vpaytv;
+                                   if(yuvMode==0) yuvMode=1;
+                                   grabYuvMode=yuvMode;
+//                                   iVSmooth=2;
+                                   setStdScale();
+                                   contGrab();
+                       break;
 #endif
                      }
                      menuLevel=mLNorm;

Modified: trunk/2010/2_71x6.cpp
==============================================================================
--- trunk/2010/2_71x6.cpp	(original)
+++ trunk/2010/2_71x6.cpp	Wed Jul  4 10:58:24 2007
@@ -307,7 +307,7 @@ void meteor_int_handler_end(void){
 
 void setCont(void){
 
- write_saa7196(0x12, satur);/*7:0  Chrominance saturation control for VRAM port */
+ write_saa7196(0x12, satur);/*7:0  Chrominance saturation control for VRAM port (Signed (: Negativ Saturation) */
  write_saa7196(0x13, contr);/*7:0  Luminance contract control for VRAM port */
  write_saa7196(0x19,bright);/*7:0  Luminance brightness control for VRAM port */
 
@@ -385,24 +385,60 @@ void setStdScale(void){
                               1:0  Aperture bandpass filter weights        */
 
  if(TVStd==SECAM) write_saa7196(0x0D, 0x85);
- else             write_saa7196(0x0D, 0x84);  
+ else             write_saa7196(0x0D, 0x84); //0x84 
                            /*   7  VTR/TV mode bit = 1->VTR mode
                                 3  Realtime output mode select bit
                                 2  HREF position select
                                 1  Status byte select
                                 0  SECAM mode bit                          */
+
  if(TVStd==SECAM) write_saa7196(0x0F, 0xB0);
- else             write_saa7196(0x0F, 0x90);        
+ else             write_saa7196(0x0F, 0x90); //90        
                            /*   7  Automatic Field detection
                                 6  Field Select 0 = 50hz, 1=60hz
                                 5  SECAM cross-colour reduction
                                 4  Enable sync and clamping pulse
                               3:1  Luminance delay compensation            */
-// write_saa7196(0x10, 0x04);
- write_saa7196(0x10, 0x00);
+ if(cryptStd==vpaytv)
+    write_saa7196(0x10, 0x02);
+ else
+    write_saa7196(0x10, 0x00); //0
                            /*   2  Select HREF Position
                               1:0  Vertical noise reduction                */
 
+ if(cryptStd==vpaytv)
+    write_saa7196(0x0C, 0xE0);  //0xE0
+ else
+    write_saa7196(0x0C, 0xC0);  //0xC0
+                           /*   7  Colour-on bit
+                              6:5  AGC filter                              */
+
+ if(cryptStd==vpaytv)
+    write_saa7196(0x0E, 0xB8); //B8
+ else
+    write_saa7196(0x0E, 0x38); //38
+
+
+                           /*   7  Horizontal clock PLL //38 / B8
+                                5  Select interal/external clock source
+                                4  Output enable of Horizontal/Vertical sync
+                                3  Data output YUV enable
+                                2  S-VHS bit
+                                1  GPSW2
+                                0  GPSW1                                   */
+
+
+// if(some-0x60>=124 ) some=123+0x60;
+ write_saa7196(0x01, 0x30);/* 7:0  Horizontal Sync Begin for 50hz          */
+ write_saa7196(0x02, 0x00);/* 7:0  Horizontal Sync Stop for 50hz           */
+ write_saa7196(0x03, 0xE8);/* 7:0  Horizontal Sync Clamp Start for 50hz    */
+ write_saa7196(0x04, 0xB6);/* 7:0  Horizontal Sync Clamp Stop for 50hz     */
+ write_saa7196(0x05, 0xF4);/* 7:0  Horizontal Sync Start after PH1 for 50hz */
+
+ //TEST
+// write_saa7196(0x11, some);/* 7:0  Chrominance gain conrtol for QAM        */
+
+
  if(grabYuvMode==0){
 /*   if(oneField) write_saa7196(0x20, 0xF2);
    else         write_saa7196(0x20, 0x92);
@@ -549,15 +585,6 @@ void init_meteor(void){
  write_saa7196(0x09, 0x7F);/* 7:3  Colour-killer threshold SECAM           */
  write_saa7196(0x0A, 0x7F);/* 7:0  PAL switch sensitivity                  */
  write_saa7196(0x0B, 0x7F);/* 7:0  SECAM switch sensitivity                */
- write_saa7196(0x0C, 0xC0);/*   7  Colour-on bit
-                              6:5  AGC filter                              */
- write_saa7196(0x0E, 0x38);/*   7  Horizontal clock PLL
-                                5  Select interal/external clock source
-                                4  Output enable of Horizontal/Vertical sync
-                                3  Data output YUV enable
-                                2  S-VHS bit
-                                1  GPSW2
-                                0  GPSW1                                   */
  write_saa7196(0x11, 0x2C);/* 7:0  Chrominance gain conrtol for QAM        */
  write_saa7196(0x14, 0x34);/* 7:0  Horizontal sync begin for 60hz          */
 //#ifdef notdef
@@ -680,6 +707,7 @@ void contGrab(void){
 }
 
 void doTxtHack(int txtHackState){
+//  return; //REMOVE
   switch(txtHackState){
     case 8 ... 100:
                  write_saa7196(0x10, 0x00);
@@ -717,3 +745,75 @@ void doTxtHack(int txtHackState){
     break;
   }
 }
+
+void vPayTvShiftHack(int amount)
+    {
+    write_saa7196(0x0F, 0x50);
+
+    for(int i=0; i<10000*amount; i++);
+
+    write_saa7196(0x0F, 0x10);
+    }
+
+
+void vPayTvBrightHack(bool b, int posInLCC)
+    {
+
+/*	int hSyncBegin= 		- 96 +posInLCC;
+	int hSyncStop= 			   0 +posInLCC;
+	int hClampBegin=		  48 +posInLCC;
+	int hClampStop=			 148 +posInLCC;*/
+
+    int hSyncBegin=         - 96 +posInLCC;
+	int hSyncStop= 			- 70 +posInLCC;
+	int hClampBegin=		- 60 +posInLCC;
+    int hClampStop=          148 +posInLCC-some;
+	int hSyncStartAfterPH1=	  96;//+posInLCC;
+
+	hSyncBegin = minmax(-382, hSyncBegin,  126);
+	hSyncStop  = minmax(-380, hSyncStop,   128);
+	hClampBegin= minmax(-254, hClampBegin, 254);
+	hClampStop = minmax(-252, hClampStop,  256);
+
+	hSyncBegin/=2; 
+	hSyncStop/=2; 	
+	hClampBegin/=2;
+	hClampStop/=2;
+	hSyncStartAfterPH1/=8;
+
+	if(hSyncStop <= hSyncBegin) hSyncStop= hSyncBegin+1;
+	if(hClampBegin <= hSyncStop) hClampBegin= hSyncStop+1;
+	if(hClampStop <= hClampBegin) hClampStop= hClampBegin+1;
+
+
+	//  write_saa7196(0x01, 0x30);/* 7:0  Horizontal Sync Begin for 50hz          */
+    //	write_saa7196(0x02, 0x00);/* 7:0  Horizontal Sync Stop for 50hz           */
+	//  write_saa7196(0x03, 0xE8);/* 7:0  Horizontal Sync Clamp Start for 50hz    */
+    //	write_saa7196(0x04, 0xB6);/* 7:0  Horizontal Sync Clamp Stop for 50hz     */
+	//  write_saa7196(0x05, 0xF4);/* 7:0  Horizontal Sync Start after PH1 for 50hz */
+
+    if(b)
+        {
+        write_saa7196(0x01, (-hSyncBegin        )&0xFF);/* 7:0  Horizontal Sync Begin for 50hz          */
+        write_saa7196(0x02, (-hSyncBegin+10     )&0xFF);/* 7:0  Horizontal Sync Stop for 50hz           */
+        write_saa7196(0x03, (-hClampBegin       )&0xFF);/* 7:0  Horizontal Sync Clamp Start for 50hz    */
+        write_saa7196(0x04, (-hClampStop        )&0xFF);/* 7:0  Horizontal Sync Clamp Stop for 50hz     */
+        write_saa7196(0x05, (-hSyncStartAfterPH1)&0xFF);/* 7:0  Horizontal Sync Start after PH1 for 50hz */
+        }
+    else
+        {
+        write_saa7196(0x01, (-hSyncBegin        )&0xFF);/* 7:0  Horizontal Sync Begin for 50hz          */
+        write_saa7196(0x02, (-hSyncStop         )&0xFF);/* 7:0  Horizontal Sync Stop for 50hz           */
+        write_saa7196(0x03, (-hClampBegin       )&0xFF);/* 7:0  Horizontal Sync Clamp Start for 50hz    */
+        write_saa7196(0x04, (-hClampStop        )&0xFF);/* 7:0  Horizontal Sync Clamp Stop for 50hz     */
+        write_saa7196(0x05, (-hSyncStartAfterPH1)&0xFF);/* 7:0  Horizontal Sync Start after PH1 for 50hz */
+        }
+    printf("T %d %d %d %d %d\n"
+		,(-hSyncBegin        )&0xFF
+		,(-hSyncStop         )&0xFF
+		,(-hClampBegin       )&0xFF
+		,(-hClampStop        )&0xFF
+		,(-hSyncStartAfterPH1)&0xFF);
+
+    }
+

Modified: trunk/2010/2_71x6.h
==============================================================================
--- trunk/2010/2_71x6.h	(original)
+++ trunk/2010/2_71x6.h	Wed Jul  4 10:58:24 2007
@@ -20,6 +20,7 @@ enum CRYPTSTD{
  nix,
  vc,
  nag,
+ vpaytv
 };
 
 /*
@@ -219,5 +220,7 @@ void setStdScale(void);
 void grab_next(VID2MEMBUF v2mb);
 void grab_nix();
 void doTxtHack(int);
+void vPayTvShiftHack(int);
+void vPayTvBrightHack(bool, int);
 
 #endif

Modified: trunk/2010/2_crypt.cpp
==============================================================================
--- trunk/2010/2_crypt.cpp	(original)
+++ trunk/2010/2_crypt.cpp	Wed Jul  4 10:58:24 2007
@@ -12,6 +12,7 @@
 #include "2_txt.h"
 #include "2_71x6.h"
 #include "2_gfunc.h"
+#include "2_vpaytv.h"
 
 #define COMB_FIX 256
 
@@ -45,7 +46,7 @@ void decrypt(void){
  }
 
  if( cryptStd == nag && satur!=0 ) Decomb(0);
- if( cryptStd == vc  && satur!=0 ) Decomb(1);
+ if( cryptStd == vc  && satur!=0 ) Decomb(1); 
 
  if(iState){
    T2=uclock();
@@ -56,6 +57,7 @@ void decrypt(void){
 #ifdef CRYPT
  if(cryptStd==vc)       vc_decrypt();
  else if(cryptStd==nag) nag_decrypt();
+ else if(cryptStd==vpaytv) vPayTvDecrypt();
 #endif
 
  if(TVStd==TXTPAL) txt_decrypt();

Modified: trunk/2010/2_file.cpp
==============================================================================
--- trunk/2010/2_file.cpp	(original)
+++ trunk/2010/2_file.cpp	Wed Jul  4 10:58:24 2007
@@ -2,10 +2,14 @@
 
 #include <stdio.h>
 #include <dos.h>
+#include <time.h>
 #include "2_all.h"
 #include "2010.h"
 #include "2_file.h"
 #include "2_hw.h"
+#include "2_hw_mem.h"
+#include "2_gfunc.h"
+#include <jpeglib.h>
 
  extern int vgax, vgay, wndx, wndy, outy;
  extern int fysize;
@@ -14,6 +18,7 @@
  extern int g_mode;
  extern int yuvMode;
  extern char *yuv2rgb_lut;
+extern int infoPosX, infoPosY;
 
  static byte *ppbRawDat[64];
  static int nRawDats=0;
@@ -132,6 +137,113 @@ void ShowRaw(void){
     i%=nRawDats;
     memcpy(actVid2MemBufp->b, ppbRawDat[i], vgax*outy*2);
 }
+/*
+static byte *jpegStorage= null;
+static int nJpegs=0;
+static byte **apJpeg= null;
+static int *aJpegSizes= null;
+static int jpegStorageSize=0;
+*/
+void RecordVideo(void)
+{
+	if(yuvMode==0) return;
+	long T0= uclock();
+	
+	static FILE * outFile= NULL;		
+ 	if(outFile == NULL)
+	{
+		char fileName[16];
+ 		struct time ti;
+
+ 		gettime(&ti);
+ 		sprintf(fileName, "V%02d%02d%02d.jpg", ti.ti_hour, ti.ti_min, ti.ti_sec);
+		
+		outFile = fopen(fileName, "wb");
+		if (outFile == NULL) 
+		{
+    		fprintf(stderr, "can't open %s\n", fileName);
+    		exit(1);
+  		}
+		
+	}
+	
+	int image_width= wndx/2;
+	int image_height= wndy/2;
+	int quality= 70;
+	
+	static byte * image_buffer= new byte[wndx*wndy*3];
+	byte * pYUV= &actVid2MemBufp->b[ 0 ];
+	byte * pYCbCr= &image_buffer[0];
+	
+	for(int y=0; y<(wndy>>1); y++)
+	{
+		for(int x=0; x<(wndx>>1); x++)
+		{
+			pYCbCr[0] = (pYUV[1] + pYUV[3]) >> 1;
+			pYCbCr[1] = pYUV[0]+127;
+			pYCbCr[2] = pYUV[2]+127;
+			pYCbCr+=3;
+			pYUV+=4;
+		}
+		pYUV+=(vgax*2 - wndx)*2;
+	}			
+
+	static struct jpeg_compress_struct cinfo;
+	
+	static struct jpeg_error_mgr jerr;
+
+	JSAMPROW row_pointer[1];	/* pointer to JSAMPLE row[s] */
+  	const int row_stride= image_width * 3;;		/* physical row width in image buffer */
+
+	static bool isInit=true;
+	if(isInit)
+	{
+		isInit=false;
+		cinfo.err = jpeg_std_error(&jerr);
+
+		/* Now we can initialize the JPEG compression object. */
+  		jpeg_create_compress(&cinfo);
+		
+	  	jpeg_stdio_dest(&cinfo, outFile);
+	}
+	
+   	cinfo.image_width = image_width; 	/* image width and height, in pixels */
+  	cinfo.image_height = image_height;
+  	cinfo.input_components = 3;		/* # of color components per pixel */
+  	cinfo.in_color_space = JCS_YCbCr; 	/* colorspace of input image */
+  	jpeg_set_defaults(&cinfo);
+	
+	cinfo.dct_method= JDCT_ISLOW; // JDCT_IFAST is 30% faster
+	cinfo.optimize_coding= TRUE; // almost no differenc in speed
+	
+  	jpeg_set_quality(&cinfo, quality, TRUE /* limit to baseline-JPEG values */);
+
+   	jpeg_start_compress(&cinfo, TRUE);
+
+  	while (cinfo.next_scanline < cinfo.image_height) {
+    	row_pointer[0] = & image_buffer[cinfo.next_scanline * row_stride];
+    	(void) jpeg_write_scanlines(&cinfo, row_pointer, 1);
+  	}
+
+ 
+  	jpeg_finish_compress(&cinfo);
+	
+	fflush(outFile);
+
+//   	jpeg_destroy_compress(&cinfo);
+
+	long T1= uclock();
+	char textBuffer[20];
+	sprintf(textBuffer, "Recording %f", float(T1-T0)/float(UCLOCKS_PER_SEC));
+	
+	color c;
+	c.init(255, 0, 0, false);
+   	
+	gprint(infoPosX, infoPosY+=10,  c.col, textBuffer);
+	
+}
+
+
 
 long fsize(FILE *f){
  long i,j;

Modified: trunk/2010/2_file.h
==============================================================================
--- trunk/2010/2_file.h	(original)
+++ trunk/2010/2_file.h	Wed Jul  4 10:58:24 2007
@@ -25,5 +25,6 @@ long fsize(FILE *f);
 void LoadRaws(int, char **);
 void GrabRaw(void);
 void ShowRaw(void);
+void RecordVideo(void);
 
 #endif

Modified: trunk/2010/2_grafix.cpp
==============================================================================
--- trunk/2010/2_grafix.cpp	(original)
+++ trunk/2010/2_grafix.cpp	Wed Jul  4 10:58:24 2007
@@ -8,9 +8,12 @@
 #include "2010.h"
 #include "2_gfunc.h"
 #include "2_hw.h"
+#include "2_hw_mem.h"
 #include "2_file.h"
 #include "2_crypt.h"
 
+#include <dpmi.h> // remove
+
  extern int xres, yres, xresc;
  extern int fysize;
  extern byte font[4096];
@@ -27,7 +30,7 @@
  extern bool allowDrop;
 
  bool helpState=true;
- bool iState=false;
+ int iState=0;
  int showCont=0;
 
 void showStuff(void){
@@ -41,7 +44,7 @@ void showStuff(void){
                         "g     ScreenShot     G SaveRaw",
                         "v     smoothIterlace p Pause",
                         "d     allowDrop      q Quit",
-                        "S     ShowRaw", 
+                        "S     ShowRaw        r Record", 
                         "            TELETXT:", 
                         "+     Page+      -     Page-",
                         "right subPage+   left  subPage-",
@@ -73,28 +76,35 @@ void showStuff(void){
 
  if(menuLevel==mLTVStd){
 #ifndef CRYPT
-   char text[3][256]={ "1. PAL   4. PAL-TeleText",
+   char text[4][256]={ "1. PAL   4. PAL-TeleText",
                        "2. NTSC",
-                       "3. SECAM"};
+                       "3. SECAM",
+                       ""};
 #else
-   char text[3][256]={ "1. PAL   4. PAL-TeleText",
+   char text[4][256]={ "1. PAL   4. PAL-TeleText",
                        "2. NTSC  5. VideoCrypt (PAL)",
-                       "3. SECAM 6. NagraVision (PAL/secam)"};
+                       "3. SECAM 6. NagraVision (PAL/secam)",
+                       "7. "};
 #endif
-   for(int i=0; i<3; i++){
+   for(int i=0; i<4; i++){
      c.init(255, 0, 0, false);
      gprint(infoPosX, infoPosY+=10, c.col, text[i]);
    }
    infoPosY+=5;
  }
 
- static long T2=uclock();
- long T1=uclock();
-
  if(iState){
    c.init(255, 0, 0, false);
 
-   sprintf(textbuf,"%2.2f Fps",1/((float)(T1-T2)/UCLOCKS_PER_SEC));
+
+   static long T[10];
+   for(int i=8; i>=0; i--)
+   		{
+		T[i+1] = T[i];
+		}
+   T[0] = uclock();
+
+   sprintf(textbuf,"%2.2f Fps",1/((float)(T[0]-T[9])/(UCLOCKS_PER_SEC*9)));
    gprint(infoPosX, infoPosY+=10, c.col, textbuf);
 
    sprintf(textbuf,"%d Buf", vid2MemBuf[0].num);
@@ -119,8 +129,15 @@ void showStuff(void){
    gprint(infoPosX, infoPosY+=10, c.col, textbuf);
    infoPosY+=5;
  }
+ if(iState==2){
+   c.init(255, 0, 0, false);
+ 
+   int nMbLeft= getFreeMemory() / (1024 * 1024);
 
- T2=T1;
+   sprintf(textbuf,"%dMb left", nMbLeft);
+   gprint(infoPosX, infoPosY+=10, c.col, textbuf);
+   infoPosY+=5;
+ }
 
 }
 

Modified: trunk/2010/2_hw_mem.cpp
==============================================================================
--- trunk/2010/2_hw_mem.cpp	(original)
+++ trunk/2010/2_hw_mem.cpp	Wed Jul  4 10:58:24 2007
@@ -7,6 +7,7 @@
 #include <string.h>
 #include <sys/nearptr.h>
 #include <sys/farptr.h>
+#include <math.h>
 #include "2_all.h"
 #include "2_hw_mem.h"
 #include "2010.h"
@@ -23,7 +24,7 @@ bool checkAlign(void *p, int align){    
   else                   return true;
 }
 
-void *newAlign(int size, int align){        
+void *newAlign(int size, int align){
   int v= int(new char[size + align + 3]);
 #ifdef debug
   printf("anew %d\n", v);
@@ -455,3 +456,26 @@ void unmapall(void){
    delete llp2;
  }
 }
+
+int getFreeMemory(void)
+{
+	int h[3000];
+ 	int x;
+   	__dpmi_meminfo dmi;
+	
+   	for(x=0; ; x++)
+	{
+    	dmi.size= 1024*1024;
+        int e=__dpmi_allocate_memory(&dmi);
+        if(e==-1) break;
+        h[x]=dmi.handle;
+	}
+	
+
+	for(int i = x-1; i>=0; i--)
+    {
+		__dpmi_free_memory(h[i]);
+    }
+	
+	return x * 1024 * 1024 ;
+}

Modified: trunk/2010/2_hw_mem.h
==============================================================================
--- trunk/2010/2_hw_mem.h	(original)
+++ trunk/2010/2_hw_mem.h	Wed Jul  4 10:58:24 2007
@@ -56,5 +56,6 @@ void vds_freeall(void);
 void *map(u_long start, u_long end);
 void unmap(void *start);
 void unmapall(void);
+int getFreeMemory(void);
 
 #endif

Modified: trunk/2010/2_nag.cpp
==============================================================================
--- trunk/2010/2_nag.cpp	(original)
+++ trunk/2010/2_nag.cpp	Wed Jul  4 10:58:24 2007
@@ -20,6 +20,8 @@
 
 #define dwnSmp 3
 
+#define SAVE_SEEDS
+
 #define FREQ_CHROM     4433618.75
 #define FREQ_HS        15625.0
 #define PHASE_DRIFT_PER_LINE (-fmod(FREQ_CHROM/FREQ_HS*4.0, 1.0)/4.0*PI*2.0)
@@ -255,7 +257,15 @@ void nag_decrypt(void){
  static long long *sinPerLine=new long long[NAG_LINES+32];
  static long long *cosPerLine=new long long[NAG_LINES+32];
 
+ static FILE *seedsFile=NULL;
+
  if(first){
+
+#ifdef SAVE_SEEDS 	
+ 	seedsFile= fopen("nagSeeds.dat", "w");
+	if(seedsFile==NULL) error(error_code(-987));
+#endif 
+ 	
    decoLines=32;
    decoLine=new int[decoLines];
    decoLine[ 0]= 14;
@@ -738,6 +748,11 @@ void nag_decrypt(void){
  printf("%X\n", bestKey);
 #endif
 
+#ifdef SAVE_SEEDS
+	if(drop)	fprintf(seedsFile, "FFFF\n"); 
+	else 		fprintf(seedsFile, "%04X\n", bestKey);
+#endif
+
  delete [] bestCoeffs;   // kill this
 
 

Modified: trunk/2010/2_txt.cpp
==============================================================================
--- trunk/2010/2_txt.cpp	(original)
+++ trunk/2010/2_txt.cpp	Wed Jul  4 10:58:24 2007
@@ -12,7 +12,7 @@
 #include "2_txt.h"
 #include "2_71x6.h"
 
-#define FORCE_ORDER true
+#define FORCE_ORDER false
 
 extern int vgax, vgay, wndx, wndy, mc, x_field;
 extern bool iState, helpState;
@@ -118,13 +118,15 @@ static inline int sample(byte *b, int fi
   return (a1+a2) >> FIXP_SH;
 }
 
-
 void txt_decrypt(void){
   char textbuf[9][128];
 
+  int cross= (some-20)*1000;
+
   static int hammErr=0;
   static int pages=0;
-  static int parities=0;
+  static double parities= 0;
+  static double magics= 0;
 
   const double freqPix2= double(wndx) / double(x_field) * FREQ_PIX;
   const int delta= int(freqPix2/FREQ_TXT*ONE_FIXP);
@@ -250,6 +252,7 @@ void txt_decrypt(void){
     }
 
     if(x>=70) continue;
+    magics++;
    
 /*  
     actVid2MemBufp->b[linep + (x<<1)    ]= 128;
@@ -465,10 +468,11 @@ void txt_decrypt(void){
   }
 
   memset(actVid2MemBufp->b, 0, vgax*vgay<<mc);
-
-//  if(yuvMode<2) memset(actVid2MemBufp->b, 0, vgax*vgay<<mc);
-//  yuvMode++;
-//  if(yuvMode>2) yuvMode=2;
+/*
+  if(yuvMode<2) memset(actVid2MemBufp->b, 0, vgax*vgay<<mc);
+  yuvMode++;
+  if(yuvMode>2) yuvMode=2;
+  */
 
   if(txtHack>0 && txtHackState==0) txtHackState=14;
 
@@ -635,7 +639,7 @@ void txt_decrypt(void){
     }
 
     if(line==0){
-      sprintf(textbuf[0], " %d %d", pages, parities);
+      sprintf(textbuf[0], " %d %f", pages, parities);
       gprint(10+32*9, yPos, txtColor[7].col, textbuf[0]);
     }
 
@@ -698,8 +702,8 @@ void txt_decrypt(void){
 
   infoPosY+=5;
 
-  if(parities>9999) parities-=9000;
-
+  parities*= 0.9;
+  magics*=0.9;
 }
       
 

Modified: trunk/2010/2_vc.cpp
==============================================================================
--- trunk/2010/2_vc.cpp	(original)
+++ trunk/2010/2_vc.cpp	Wed Jul  4 10:58:24 2007
@@ -39,12 +39,12 @@
 
 
 #define MIN_LINES_FOR_EDGE_AREA 50
-#define EDGE_CHANGE_THRESHOLD 0.3
-#define PHASE_CORRECT_THRESHOLD (1.0/128*PI)
+#define EDGE_CHANGE_THRESHOLD 0.2
+#define PHASE_CORRECT_THRESHOLD (0.5/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 FIELDS_PER_PHASE_CHECK 50
+//#define EDGE_AREA_TRUNC_DIV 6
 #define EDGE_AREA_START -4
 #define EDGE_AREA_END 4
 #define DEFINAL_WAIT 30
@@ -56,8 +56,8 @@
 #define SCORE_THRESHOLD 3
 #define MAX_EQ_SETS 14
 #define nTEST_LINES 10
-#define MAX_BAD_CACHES 30000
-#define MAX_GOOD_CACHES 70000
+#define MAX_BAD_CACHES 50000
+#define MAX_GOOD_CACHES 25000
 
 #define MAGIC_ID "VCC0"
 #define STEP_START 16
@@ -74,7 +74,8 @@
 #define PHASE_EXP 4
 #define PHASE_LIMIT 40
 #define MAX_PHASE_PENALTY 1000
-#define AMP_THRESHOLD 200
+#define AMP_THRESHOLD1 250
+#define AMP_THRESHOLD2 600
 
 #define PHASE_COEFF (MAX_PHASE_PENALTY/pow(PHASE_LIMIT, PHASE_EXP))
 #define EDGE_COEFF (MAX_EDGE/log(EDGE_LIMIT))
@@ -95,6 +96,8 @@ extern int infoPosY;
 extern long uclockWaste;
 extern bool drop;
 
+bool isVCPhaseTrick=false;
+
 static void loadVCCache(void);
 
 
@@ -501,7 +504,7 @@ static CutPCache *findInLutX(int *piCutP
                     else
                         {
                         if(   pCutPCache->score>=SCORE_THRESHOLD
-                           && pCutPCache!=pMergeCutPCache && 0)
+                           && pCutPCache!=pMergeCutPCache && 0) //FIX ?! 
                             {
 
                             addCutPsToCache(
@@ -724,16 +727,18 @@ static void loadVCCache(void){
         tpCutPCache= new CutPCache;
 
         fread(tpCutPCache, sizeof(CutPCache), 1, pFCache);
+		if( feof(pFCache) ) break;						    // FIX LOOKS UGLY
 
 //        printf("test %d %d\n", tpCutPCache->iScore, tpCutPCache->nUsed);
 
 
         tpCutPCache->pbCCutP= new byte[tpCutPCache->nbCCutP];
+		if(tpCutPCache->nbCCutP > 600) printf("%d\n", tpCutPCache->nbCCutP);
         fread(tpCutPCache->pbCCutP, sizeof(byte),
               tpCutPCache->nbCCutP, pFCache);
 
         if(tpCutPCache->isDeFinal==0) badFinalsLeft++;
-		addToCache(tpCutPCache);
+        addToCache(tpCutPCache);
 //        printf("B\n");
         addToLut(tpCutPCache);
 //        printf("A\n");
@@ -751,15 +756,10 @@ void vc_decrypt(void){
  long T1=0, T2;
  color c;
  
+ int pChromAmp[max_x];
+
  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);
 
@@ -843,7 +843,13 @@ void vc_decrypt(void){
  static bool findPhases=true;
  static double phaseCorrect=0;
  static double lastPhaseCorrect=0;
- if(mabs(phaseCorrect-lastPhaseCorrect) > PHASE_CORRECT_THRESHOLD)
+ if(isVCPhaseTrick)
+ 	{
+	phaseCorrect+=12.0/180.0*PI;
+	if(phaseCorrect > MAX_PHASE_CORRECT) phaseCorrect= MIN_PHASE_CORRECT;
+	isVCPhaseTrick=false;
+	}
+ if(mabs(phaseCorrect-lastPhaseCorrect) > PHASE_CORRECT_THRESHOLD && some==0)
  	{
 	lastPhaseCorrect= phaseCorrect;
 	findPhases=true;
@@ -861,6 +867,7 @@ void vc_decrypt(void){
      	sin2PerCutP[cutP]= char( round( sin( -phase )*127 ) );
      	cos2PerCutP[cutP]= char( round( cos( -phase )*127 ) );
    		}
+	findPhases=false;
 	}
 
  if(first){
@@ -1177,6 +1184,7 @@ void vc_decrypt(void){
             chromAmp+= mabs(un) + mabs(vn);
         }
         if(line<2) chromAmp=1;
+		pChromAmp[line]= chromAmp;
    
 //   linep[ ((vcStartX +  0)<<1) + 1]=255;
  //  linep[ ((vcStartX +  relCutPX)<<1) + 1]=200;
@@ -1290,7 +1298,7 @@ void vc_decrypt(void){
                 const int phase1= (a + phasePerCutP[ lastCutP ]) & 0xFF;
                 const int lastPhaseErrLastCutP= pPhaseErr[(line-1)*256 + lastCutP] + 128;
           // FIX (ASM_OPTIMIZE)
-                if(chromAmp>AMP_THRESHOLD){
+                if(chromAmp>AMP_THRESHOLD1){
                     for(int newCutP= newCutPStart; newCutP <= newCutPEnd; newCutP++){
                         const int phaseErr= (phase1 + phasePerCutP[ newCutP ]) & 0xFF;
                         const int val=  lastVal[lastCutP] - *(++piDrift)
@@ -1458,7 +1466,7 @@ void vc_decrypt(void){
  int phaseErrSum=0;
  for(int line=10; line<wndy-10; line++)
  	{
-	if(black[line] || black[line-1]) continue;
+	if(black[line] || black[line-1] || pChromAmp[line]<AMP_THRESHOLD2) continue;
 	int phaseErr= pPhaseErr[ line*256 + piBestCutP[line] ];
 	if(phaseErr>128) phaseErr-=256;
 	phaseErrSum+= phaseErr;

Added: trunk/2010/2_vpaytv.cpp
==============================================================================
--- (empty file)
+++ trunk/2010/2_vpaytv.cpp	Wed Jul  4 10:58:24 2007
@@ -0,0 +1,689 @@
+//2010 0.1 Copyright (C) Michael Niedermayer 2000
+
+#include <string.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <time.h>
+#include <math.h>
+#include "2_all.h"
+#include "2_crypt.h"
+#include "2_vc.h"
+#include "2_nag.h"
+#include "2_71x6.h"
+#include "2_gfunc.h"
+#include "2_mmx.h"
+#include "2_nag_a.h"
+#include "2_hw_mem.h"
+#include "2_vpaytv.h"
+#include "2010.h"
+
+#define FREQ_CHROM     4433618.75
+#define FREQ_HS        15625.0
+#define PHASE_DRIFT_PER_LINE (-fmod(FREQ_CHROM/FREQ_HS*4.0, 1.0)/4.0*PI*2.0)
+#define Q_2_HSYNC_DISTANCE 6		//HACK (only 400x300)
+#define VSYNC_SIZE 26
+
+#define REAL_QLINE_POS (wndx-10)
+
+#define QLINE_COEFFICIENT_THRESHOLD 0.20
+//#define QLINE_OVERBRIGHT_THRESHOLD 220
+#define QLINE_OVERBRIGHT_THRESHOLD 240
+#define OVERBRIGHT_TEST_COLUMN 4
+#define HSYNC_THRESHOLD 30
+#define HSYNC_SEARCH_START (Q_2_HSYNC_DISTANCE-3)
+#define HSYNC_SEARCH_END 22
+#define VSYNC_SEARCH_COLUMN (-2)
+#define VSYNC_OVERBRIGHT_THRESHOLD 230
+#define MIN_VSYNC_DISTANCE 20
+
+#define LEFT_QLINE_BOUNDRY (wndx-20)
+#define RIGHT_QLINE_BOUNDRY (wndx-7)
+
+#define TOP_VSYNC_BOUNDRY (wndy-50)
+#define BOTTOM_VSYNC_BOUNDRY (wndy+50)
+
+#define BRIGHTNESS_REF_POS 3
+#define BRIGHTNESS_DELTA 180
+
+
+extern vgax, vgay, wndx, wndy, outy, y_field, x_field;
+extern VID2MEMBUF *actVid2MemBufp;
+extern volatile int scales_x, scalee_x;
+extern volatile int scales_y, scalee_y;
+extern u_long asm_temp, asm_temp4;
+extern bool iState;
+extern int yuvMode;
+extern bool mmx;
+extern int infoPosX;
+extern int infoPosY;
+extern int some;
+extern bool drop;
+extern bool isVCPhaseTrick;
+
+static inline float atan3(const float f1, const float f2){
+  float out;
+                     // ?! atan2 crashes 
+  asm(//"int $3\n\t"
+      "fpatan \n\t"
+      : "=t"(out)
+      : "0"(f2), "u"(f1)
+      : "st(1)"
+                          );
+
+  return out;
+}
+
+static int qPhase=0;
+int findQLine(void)
+{
+	long T1=0, T2;
+	color c;
+    c.init(255, 0, 0, false);
+	char textbuf[100];
+    
+	if(iState==2)
+		T1=uclock();
+
+	const int xSizeOfCorrelVector= 4;
+	float correlVector[4][4]= { 	{1,1,0   ,0   },
+									{0,0,0.33,0.33},
+									{0,0,0.33,0.33},
+									{0,0,0.33,0.33} };
+
+	float meanOfCorrelVector= 0;
+	for(int y=0; y<4; y++)
+		for(int x=0; x<xSizeOfCorrelVector; x++)
+			meanOfCorrelVector+= correlVector[y][x];
+
+	meanOfCorrelVector/= xSizeOfCorrelVector*4;
+		
+	float varianceOfCorrelVector= 0;
+	for(int y=0; y<4; y++)
+		for(int x=0; x<xSizeOfCorrelVector; x++)
+			{
+			correlVector[y][x] -= meanOfCorrelVector;
+			varianceOfCorrelVector+= correlVector[y][x] * correlVector[y][x];
+			}
+
+	varianceOfCorrelVector/= xSizeOfCorrelVector*4;
+
+	byte const * const p= &actVid2MemBufp->b[0];
+
+	if(iState==2)
+		{
+	    T2=uclock();
+    	sprintf(textbuf,"%f X", (float)(T2-T1)/UCLOCKS_PER_SEC);
+    	gprint(infoPosX, infoPosY+=10, c.col, textbuf);
+		T1=T2;
+		}
+	
+	int aSumsOfEquivalentPoints[4][1000];
+	for(int i=0; i<4; i++)
+		for(int x=0; x<wndx; x++)
+			aSumsOfEquivalentPoints[i][x]= 0;
+
+	int aSumsOfSqrs[1000];
+	for(int x=0; x<wndx; x++)
+		aSumsOfSqrs[x]=0;
+
+	for(int line=0; line<wndy; line++)
+		for(int x=0; x<wndx; x++)
+			{
+			const int index= (line*vgax + x)*2;
+			const int a= 
+				       int(p[index + 1]) 
+				+ mabs(int((char)(p[index + 2])))
+				+ mabs(int((char)(p[index])));
+			aSumsOfEquivalentPoints[line & 3][x]+= a;
+			aSumsOfSqrs[x]+= a*a;
+			}
+
+	if(iState==2)
+		{
+	    T2=uclock();
+    	sprintf(textbuf,"%f EquivSum & SumSqr", (float)(T2-T1)/UCLOCKS_PER_SEC);
+    	gprint(infoPosX, infoPosY+=10, c.col, textbuf);
+		T1=T2;
+		}
+			
+	int bestQLine=-1;
+	float bestCoefficient=-1;
+	for(int qLine=0; qLine<wndx-xSizeOfCorrelVector+1; qLine++)
+		{
+		int sum=0;
+		for(int line=0; line<4; line++)
+			for(int x=qLine; x<qLine + xSizeOfCorrelVector; x++)
+				sum+= aSumsOfEquivalentPoints[line][x];
+
+		const double mean= double(sum) / (xSizeOfCorrelVector*wndy);
+
+		int sumOfSqrs=0;
+		for(int x=qLine; x<qLine + xSizeOfCorrelVector; x++)
+			sumOfSqrs+= aSumsOfSqrs[x];
+	
+		float variance= (sumOfSqrs - sum*mean) / (xSizeOfCorrelVector*wndy); 
+//		printf("%f = %d - %d * %f / ...\n", variance, sumOfSqrs, sum, mean);
+		if(variance<0.0) variance=0;
+
+		float coefficient[4]={0,0,0,0};
+		for(int line=0; line<4; line++)
+			for(int x=qLine; x<qLine + xSizeOfCorrelVector; x++)
+				{
+				coefficient[0]+= (aSumsOfEquivalentPoints[line][x]-mean*wndy/4) 
+									* correlVector[(line+0) &3][x-qLine];
+				coefficient[1]+= (aSumsOfEquivalentPoints[line][x]-mean*wndy/4) 
+									* correlVector[(line+1) &3][x-qLine];
+				coefficient[2]+= (aSumsOfEquivalentPoints[line][x]-mean*wndy/4) 
+									* correlVector[(line+2) &3][x-qLine];
+				coefficient[3]+= (aSumsOfEquivalentPoints[line][x]-mean*wndy/4) 
+									* correlVector[(line+3) &3][x-qLine];
+				}
+
+		for(int i=0; i<4; i++)
+			{
+			coefficient[i]/= xSizeOfCorrelVector*wndy* sqrt(variance * varianceOfCorrelVector + 0.000001);
+
+			if(coefficient[i]>bestCoefficient)
+				{
+				bestCoefficient= coefficient[i];
+				bestQLine= qLine;		
+				qPhase= (4-i) &3;
+//				some= int(minmax(-10000, bestCoefficient*100, 10000));
+				}
+			}
+		}
+
+	if(iState==2)
+		{
+	    T2=uclock();
+    	sprintf(textbuf,"%f coeff", (float)(T2-T1)/UCLOCKS_PER_SEC);
+    	gprint(infoPosX, infoPosY+=10, c.col, textbuf);
+		T1=T2;
+		}
+
+
+	if(bestCoefficient < QLINE_COEFFICIENT_THRESHOLD) 	return -1;
+
+
+	// try to find the Sync pulse 
+
+	int score[1000];
+	for(int i=0; i<1000; i++)
+		score[i]=0;
+
+	for(int x= min(bestQLine+HSYNC_SEARCH_END, wndx-1); x >= bestQLine + HSYNC_SEARCH_START; x--)
+		{
+		for(int line=0; line<wndy; line++)
+			{
+			score[x] += p[(line*vgax + x)*2 + 1];
+			}
+
+		if( x+2 <= min(bestQLine+HSYNC_SEARCH_END, wndx-1) 
+			&& (score[x] - score[x+2])/wndy > HSYNC_THRESHOLD) 
+			{
+			bestQLine= x - Q_2_HSYNC_DISTANCE;
+			if(bestQLine<0) bestQLine=0;
+			return bestQLine;
+			}
+		}
+
+	if(bestQLine + OVERBRIGHT_TEST_COLUMN >= wndx) return -1;
+	int sum=0;
+	for(int line=0; line<wndy; line++)
+		sum+= p[(line*vgax + bestQLine + OVERBRIGHT_TEST_COLUMN)*2 + 1];
+
+	if(sum/wndy > QLINE_OVERBRIGHT_THRESHOLD) return -1; // Sync Pulse not found and Pic to Bright
+
+	return bestQLine; //no Sync Pulse Found (Picture perhaps too dark) 
+	//perhaps -1 better
+}
+
+int findVSync(int qLine)
+{
+	if(qLine==-1) return -1;
+	if(qLine<2 || qLine+1>=wndx) return -1;
+
+    byte const * const p= &actVid2MemBufp->b[0];
+
+	int aRunningMeanQ[600];
+	int aRunningMeanNotQ[600];
+
+	int qSum=0;
+	int notQSum=0;
+	int runningQSum=0;
+	int runningNotQSum=0;
+	int nRunningQSum=0;
+	int nRunningNotQSum=0;
+	for(int vSyncStart= -VSYNC_SIZE; vSyncStart<wndy; vSyncStart++)
+		{
+		const vSyncEnd= vSyncStart + VSYNC_SIZE;
+		int index= vSyncStart;
+		if(index<0) index= vSyncStart + wndy + VSYNC_SIZE;
+
+		if(vSyncStart>=0)
+			{
+			const int xP= (vSyncStart*vgax + qLine + VSYNC_SEARCH_COLUMN) * 2;
+			const int scoreStart=      int(p[xP + 1]) 
+								+ mabs(int((char)(p[xP + 2])))
+								+ mabs(int((char)(p[xP])));
+
+			if((vSyncStart&3) == qPhase)
+				{
+				runningQSum-= scoreStart;
+				nRunningQSum--;
+				}
+			else
+				{
+				runningNotQSum-= scoreStart;
+				nRunningNotQSum--;
+				}
+			}
+		if(vSyncEnd<wndy)
+			{
+			const int xP= (vSyncEnd*vgax + qLine + VSYNC_SEARCH_COLUMN) * 2;
+			const int scoreEnd=        int(p[xP + 1]) 
+								+ mabs(int((char)(p[xP + 2])))
+								+ mabs(int((char)(p[xP])));
+
+			if((vSyncEnd&3) == qPhase)
+				{
+				qSum+=scoreEnd;
+				runningQSum+= scoreEnd;
+				nRunningQSum++;
+				}
+			else
+				{
+				notQSum+= scoreEnd;
+				runningNotQSum+= scoreEnd;
+				nRunningNotQSum++;
+				}
+			}
+
+		if(nRunningQSum) 	aRunningMeanQ[index]= runningQSum / nRunningQSum;
+		if(nRunningNotQSum) aRunningMeanNotQ[index]= runningNotQSum / nRunningNotQSum;
+		}
+
+	
+	const int qMean= qSum * 4 / (wndy*3);
+	const int notQMean= notQSum * 4 / wndy;
+
+	int bestScore= -1000000;
+	int bestVSync= -1;
+
+	for(int vSync= 0; vSync<wndy+VSYNC_SIZE; vSync++)
+		{
+		if(vSync < wndy && vSync > wndy-MIN_VSYNC_DISTANCE) continue;   
+		if(vSync >= wndy && vSync-wndy < MIN_VSYNC_DISTANCE) continue;
+
+		int score= aRunningMeanNotQ[vSync] - aRunningMeanQ[vSync];
+
+//		if(aRunningMeanNotQ[vSync] > VSYNC_OVERBRIGHT_THRESHOLD) score=-1000000;
+
+		if(score>bestScore)
+			{
+			bestScore= score;
+			bestVSync= vSync;
+			}
+		}
+	return bestVSync;
+}
+
+// Should be writen as an Object...
+//6015X 10960D 994498XX 3224315DD 1789858XD 55n
+static int hSyncDelaySumX=  6015;
+static int hSyncDelaySumD=  10960;
+static int hSyncDelaySumXX= 994498;
+static int hSyncDelaySumDD= 3224315;
+static int hSyncDelaySumDX= 1789858;
+static int hSyncDelays=55;
+static int lastHSyncDelay=0;
+static int lastQLine=0; //from last call to doHSyncDelay
+void doHSyncDelay(int hDistance, int qLine)
+{
+	double sigmaXD=   double(hSyncDelaySumDX) * double(hSyncDelays) 
+					- double(hSyncDelaySumX) * double(hSyncDelaySumD);
+	sigmaXD/= double(hSyncDelays) * double(hSyncDelays);
+
+	double sigmaX2=   double(hSyncDelaySumXX) * double(hSyncDelays) 
+					- double(hSyncDelaySumX) * double(hSyncDelaySumX);
+	sigmaX2/= double(hSyncDelays) * double(hSyncDelays);
+	if(sigmaX2 <= 0.0) sigmaX2 = 1e-50;
+
+	double diffFromMeanX= double(hDistance) - double(hSyncDelaySumX) / double(hSyncDelays);
+	double diffFromMeanD= diffFromMeanX * sigmaXD / sigmaX2;
+/*	printf("%d %d\n", hDistance, qLine);
+	printf("%dX %dD %dXX %dDD %dXD %dn\n", 
+		hSyncDelaySumX, hSyncDelaySumD,
+		hSyncDelaySumXX, hSyncDelaySumDD,
+		hSyncDelaySumDX, hSyncDelays);
+	printf("%fSXD %fSX2\n",sigmaXD, sigmaX2);
+	printf("%fdfmx %fdfmd\n", diffFromMeanX, diffFromMeanD);
+
+	printf("%f\n", diffFromMeanD + double(hSyncDelaySumD) / double(hSyncDelays) + 0.5);*/
+	int delay= int(diffFromMeanD + double(hSyncDelaySumD) / double(hSyncDelays) + 0.5);
+	if(delay<0) delay=0;
+
+//	delay= random() % 511;
+//	printf("%d Delay\n", delay);
+
+	lastHSyncDelay= delay;
+	lastQLine= qLine;
+
+	vPayTvShiftHack(delay);
+}
+
+static nStat=0;
+static int *statX= new int[10000];
+static int *statY= new int[10000];
+void analyzeHSyncDelay(int newQLine)
+{	
+	int realHDistance= newQLine - lastQLine;
+	if(newQLine!=-1 && realHDistance>0)
+		{
+//		statX[nStat]= realHDistance;
+//		statY[nStat++]= lastHSyncDelay;
+
+		hSyncDelaySumX+= realHDistance;
+		hSyncDelaySumD+= lastHSyncDelay;
+		hSyncDelaySumXX+= realHDistance * realHDistance;
+		hSyncDelaySumDD+= lastHSyncDelay * lastHSyncDelay;
+		hSyncDelaySumDX+= lastHSyncDelay * realHDistance;
+		hSyncDelays++;
+
+		if( mabs(hSyncDelaySumDX) + mabs(hSyncDelaySumDD) + mabs(hSyncDelaySumXX) > 100000000 
+				&& hSyncDelays%2==0)
+			{
+			hSyncDelaySumX/=2;
+			hSyncDelaySumD/=2;
+			hSyncDelaySumXX/=2;
+			hSyncDelaySumDD/=2;
+			hSyncDelaySumDX/=2;
+			hSyncDelays/=2;
+			}
+		}
+
+	lastHSyncDelay=0;
+	printf("%dX %dD %dXX %dDD %dXD %dn\n",
+			hSyncDelaySumX,
+			hSyncDelaySumD,
+			hSyncDelaySumXX,
+			hSyncDelaySumDD,
+			hSyncDelaySumDX,
+			hSyncDelays);
+
+}
+
+// Should be writen as an Object...
+static int vSyncDelaySumY=  3;
+static int vSyncDelaySumD=  3;
+static int vSyncDelaySumYY= 10;
+static int vSyncDelaySumDD= 10;
+static int vSyncDelaySumDY= 10;
+static int vSyncDelays=2;
+static int lastVSyncDelay=0;
+static int lastVSync=0; //from last call to doVSyncDelay
+void doVSyncDelay(int vDistance, int vSync)
+{
+	double sigmaYD=   double(vSyncDelaySumDY) * double(vSyncDelays) 
+					- double(vSyncDelaySumY) * double(vSyncDelaySumD);
+	sigmaYD/= double(vSyncDelays) * double(vSyncDelays);
+
+	double sigmaY2=   double(vSyncDelaySumYY) * double(vSyncDelays) 
+					- double(vSyncDelaySumY) * double(vSyncDelaySumY);
+	sigmaY2/= double(vSyncDelays) * double(vSyncDelays);
+	if(sigmaY2 <= 0.0) sigmaY2 = 1e-50;
+
+	double diffFromMeanY= double(vDistance) - double(vSyncDelaySumY) / double(vSyncDelays);
+	double diffFromMeanD= diffFromMeanY * sigmaYD / sigmaY2;
+
+	int delay= int(diffFromMeanD + double(vSyncDelaySumD) / double(vSyncDelays) + 0.5);
+	if(delay<0) delay=0;
+
+	delay/=2;
+	if(delay>500) delay=500;
+	delay= random() % 151;
+	lastVSyncDelay= delay;
+	lastVSync= vSync;
+
+	vPayTvShiftHack(delay*10);
+}
+
+void analyzeVSyncDelay(int newVSync)
+{
+	int realVDistance= newVSync - lastVSync;
+	if(newVSync!=-1 && realVDistance>0)
+		{
+
+		statX[nStat]= lastVSyncDelay;
+		statY[nStat++]= realVDistance;
+
+		vSyncDelaySumY+= realVDistance;
+		vSyncDelaySumD+= lastVSyncDelay;
+		vSyncDelaySumYY+= realVDistance * realVDistance;
+		vSyncDelaySumDD+= lastVSyncDelay * lastVSyncDelay;
+		vSyncDelaySumDY+= lastVSyncDelay * realVDistance;
+		vSyncDelays++;
+
+		if( mabs(vSyncDelaySumDY) + mabs(vSyncDelaySumDD) + mabs(vSyncDelaySumYY) > 100000000 
+				&& vSyncDelays%2==0)
+			{
+			vSyncDelaySumY/=2;
+			vSyncDelaySumD/=2;
+			vSyncDelaySumYY/=2;
+			vSyncDelaySumDD/=2;
+			vSyncDelaySumDY/=2;
+			vSyncDelays/=2;
+			}
+		}
+
+	lastVSyncDelay=0;
+	printf("%dY %dD %dYY %dDD %dYD %dn\n",
+			vSyncDelaySumY,
+			vSyncDelaySumD,
+			vSyncDelaySumYY,
+			vSyncDelaySumDD,
+			vSyncDelaySumDY,
+			vSyncDelays);
+
+}
+/*
+void fixColor(int qLine)
+{
+	qLine&= ~1;
+    byte * const p= &actVid2MemBufp->b[0];
+
+	for(int line=qPhase; line<wndy-3; line+=4)
+		{
+		int u=char(p[(line*vgax + qLine-2)*2    ]);
+		int v=char(p[(line*vgax + qLine-2)*2 + 2]);
+		printf("%d %d %f\n", u, v, atan3(u,v)/PI*180);
+		for(int x=0; x<wndx-1; x+=2)
+			{
+			}
+
+		}
+}
+*/
+
+void fixBrightness(int qLine)
+{
+    byte * const p= &actVid2MemBufp->b[0];
+
+	for(int line=0; line<wndy; line++)
+        {
+
+        if(qLine + BRIGHTNESS_REF_POS>=wndx) break;
+
+        int brightnessRef=0;
+        int nBrightnessRef=0;
+        for(int refLine=max(line-2, 0); refLine<min(line+2, vgay); refLine++)
+            {
+            brightnessRef+= p[(refLine*vgax + qLine + BRIGHTNESS_REF_POS)*2 + 1];
+            nBrightnessRef++;
+            }
+        brightnessRef/= nBrightnessRef;
+
+        for(int x=0; x<min(qLine, wndx); x++)
+            {
+            p[(line*vgax + x)*2 + 1]=
+                minmax(0, p[(line*vgax + x)*2 + 1] - brightnessRef + BRIGHTNESS_DELTA, 255);
+            }
+        }
+}
+
+void showStat(void)
+{
+    byte * const p= &actVid2MemBufp->b[0];
+
+	for(int i=0; i<200; i+=2)
+		{
+		p[(100*vgax + i)*2 + 1]=128;
+		p[(i*vgax + 100)*2 + 1]=128;
+		}
+
+	for(int i=0; i<nStat; i++)
+		{
+		int x= statX[i] + 100;
+		int y= statY[i] + 100;
+		if(y<0 || x<0) continue;
+		if(y>=wndy || x>=wndx) continue;
+
+		int xP= (y*vgax + x)*2;
+		p[xP + 1]=255;
+		}
+}
+
+void vPayTvDecrypt(void)
+{
+	static int dropList[10]; //0 -> nothing, 1 -> Drop, 2 -> Drop & Return
+	for(int i=0; i<9; i++)
+		dropList[i]= dropList[i+1];
+	dropList[9]= 0;
+
+	static int lastQLineInLCC=0;
+	static bool isBrightHack=true;
+	vPayTvBrightHack(isBrightHack, lastQLineInLCC);
+	isBrightHack = !isBrightHack;
+    if(!isBrightHack) dropList[1]|=2;
+
+	if(dropList[0])
+		{
+		drop=true;
+        if(dropList[0]&2) return;
+		}
+
+	int qLine= findQLine();
+	int vSync= -1;//findVSync(qLine);
+
+	if(vSync!= -1)
+		{
+		lastQLineInLCC= (qLine-wndx-6)*1888 / wndx;
+		}
+
+    byte * p= &actVid2MemBufp->b[0];
+    for(int line=0; line<wndy; line++)
+        {
+        if((line&3) == qPhase)
+            {
+            p[2*qLine+1]=0;
+            p[2*qLine+3]=255;
+            }
+        p+= vgax<<1;
+        }
+
+    p= &actVid2MemBufp->b[0];
+	for(int x=0; x<wndx; x++)
+        {
+        if((x&3)==0)
+            {
+//            p[(vSync*vgax + x)*2 + 1]=0;
+ //           p[(vSync*vgax + x)*2 + 3]=255;
+            }
+        }
+
+
+	if(lastHSyncDelay!=0)
+		{
+		analyzeHSyncDelay(qLine);
+		}
+
+	if(lastVSyncDelay!=0)
+		{
+		analyzeVSyncDelay(vSync);
+		}
+
+	static bool isVSyncBad= false;
+	if(vSync!=-1 && (vSync<TOP_VSYNC_BOUNDRY || vSync>BOTTOM_VSYNC_BOUNDRY) && 0) 
+		{
+		if(isVSyncBad)
+			{
+			doVSyncDelay(BOTTOM_VSYNC_BOUNDRY - vSync, vSync);
+            dropList[1]|=2;
+			isVSyncBad= false;
+			}
+		else
+			{
+			isVSyncBad= true;
+			}
+		}
+    else if(qLine!=-1 && qLine<LEFT_QLINE_BOUNDRY)
+		{
+		doHSyncDelay((RIGHT_QLINE_BOUNDRY + LEFT_QLINE_BOUNDRY)/2 - qLine, qLine);
+        dropList[1]|=2;
+		}
+	else if(qLine==-1 && 0)
+		{
+		doHSyncDelay(wndx/2, 1);
+		lastHSyncDelay=0;
+        dropList[1]|=2;
+		}
+	else if(isVCPhaseTrick)
+		{
+		isVCPhaseTrick= false;
+        vPayTvShiftHack(1000);
+        dropList[1]|=2;
+		}
+	else
+		{
+		isVSyncBad= false;
+		}
+
+    if(qLine==-1) dropList[1]|=2;
+
+
+	fixBrightness(qLine);
+
+//	showStat();
+  
+    int nPixMove= REAL_QLINE_POS - qLine;
+
+    if(nPixMove>wndx) nPixMove=0;
+    if(nPixMove<-wndx) nPixMove=0;
+
+
+    p= &actVid2MemBufp->b[0];
+    for(int line=1; line<wndy; line++)
+        {
+        if(nPixMove>0)
+            {
+            memmove(&p[nPixMove*2], &p[0], (wndx-nPixMove)*2);
+            }
+        else if(nPixMove<0)
+            {
+            memmove(&p[0], &p[-nPixMove*2], (wndx+nPixMove)*2);
+            }
+        if(nPixMove & 1)
+            for(int x=0; x<wndx-2; x++)
+                {
+                p[x*2]= p[x*2+2];
+                }
+		for(int x=0; x<wndx-2; x++)
+			{
+//			p[x*2]=0;
+			}
+        p+= vgax<<1;
+        }
+
+
+	lastQLine= qLine;        
+}

Added: trunk/2010/2_vpaytv.h
==============================================================================
--- (empty file)
+++ trunk/2010/2_vpaytv.h	Wed Jul  4 10:58:24 2007
@@ -0,0 +1,7 @@
+//2010 0.1 Copyright (C) Michael Niedermayer 1999
+#ifndef n2_vpaytv_h
+#define n2_vpaytv_h
+
+void vPayTvDecrypt(void);
+
+#endif

Modified: trunk/2010/makefile
==============================================================================
--- trunk/2010/makefile	(original)
+++ trunk/2010/makefile	Wed Jul  4 10:58:24 2007
@@ -22,15 +22,15 @@ O = 2_glob_a.o 2_cryp_a.o 2_hw_a.o\
 
 OC = 2_nag_a.o 2_vc_a.o 2_glob_a.o 2_cryp_a.o 2_hw_a.o\
      2010.o 2_file.o 2_grafix.o 2_hw.o 2_gfunc.o \
-     2_hw_asm.o 2_71x6.o 2_hw_mem.o 2_crypt.o 2_vc.o 2_nag.o 2_txt.o 
+     2_hw_asm.o 2_71x6.o 2_hw_mem.o 2_crypt.o 2_vc.o 2_nag.o 2_txt.o 2_vpaytv.o
      
 
 default: ;$(MAKE) 2010 CFLAGS="$(OPTS)"
 
 crypt:   ;$(MAKE) 2010C CFLAGS="$(OPTS) -DCRYPT"
 
-2010  : $(O)  ; gcc $(CFLAGS) -o 2010  $(O)
-2010C : $(OC) ; gcc $(CFLAGS) -o 2010C $(OC)
+2010  : $(O)  ; gcc $(CFLAGS) -o 2010  $(O) -ljpeg
+2010C : $(OC) ; gcc $(CFLAGS) -o 2010C $(OC) -ljpeg
 clean: ; del $(O) 2010
 
 2010.o     : 2010.cpp     2_all.h 2010.h 2_file.h 2_hw.h 2_grafix.h\
@@ -42,11 +42,12 @@ clean: ; del $(O) 2010
                              2_71x6.h 2_mmx.h 2_hw_a.h
 2_hw_mem.o : 2_hw_mem.cpp 2_all.h 2_hw_mem.h 2010.h 
 2_71x6.o   : 2_71x6.cpp   2_all.h 2_71x6.h 2_hw.h 2_hw_mem.h 2010.h 2_hw_asm.h
-2_crypt.o  : 2_crypt.cpp  2_all.h 2_crypt.h 2_71x6.h 2_vc.h 2_nag.h 2_txt.h
+2_crypt.o  : 2_crypt.cpp  2_all.h 2_crypt.h 2_71x6.h 2_vc.h 2_nag.h 2_txt.h 2_vpaytv.h
 2_vc.o     : 2_vc.cpp     2_all.h 2_vc.h 2_crypt.h 2_gfunc.h 2_vc_a.h
 2_nag.o    : 2_nag.cpp    2_all.h 2_nag.h 2_crypt.h 2_gfunc.h 2_mmx.h 2_nag_a.h\
                              2010.h 2_hw_mem.h
 2_txt.o    : 2_txt.cpp    2_all.h 2_txt.h 2_crypt.h 2_gfunc.h 2010.h 2_71x6.h
+2_vpaytv.o : 2_vpaytv.cpp 2_all.h 2_crypt.h 2_vpaytv.h 2_mmx.h
 
 2_hw_asm.o : 2_hw_asm.s
 



More information about the Mndiff-dev mailing list