⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 video.cpp

📁 Vxworks 下的视频采集程序
💻 CPP
字号:
#include <vxworks.h>
#include<stdio.h>
#include<STDLIB.H>
#include<iostream.h>
#include<time.h>
#include"video.h"
#include"head.h"
#include"xdefine.h"
#include"data.h"
#include"w99200.h"
#define REL_MASTER_BUFFER_SIZE 4096
       
//extern int W99200WriteReg(int index, int value);
//extern int W99200ReadReg(int index);
//extern int W99200IMEM_Write(int Mem);
//extern int W99200FIFO_Thr(void);
//extern int W99200FIFO_End(void);
//extern int W99200I2CWrite(int Subadrs, int * Value, UINT Count);
//extern int W99200Inpdw(DWORD Port);

//extern DWORD  _ulAIR=0L;     // Address Index Register
//extern DWORD  _ulDPR=4L ;    // Data Port Register 
//extern DWORD  _ulODPR=8L ;   // the Output port of the Output-FIFO
//int _dMem_Data[64];// 4*64=256 Bytes
//int _dSDRAM_Data[2052]; // 2048*4=8192 Bytes
//int _dAUDIO_Data[32]; // 32*4=128 Bytes
//int _dDef_Regs[135];  // Default Value of Registers

int UTL_SET_I2CVIDEO(void)
{
  FILE *fp;
  int dIndex,dValue;
  int dCount,i;
  dIndex=0;
  dValue=0;
  dCount=0; 
  if ( _VideoStard == 0 ) // NTSC 
  {
    if( (fp = fopen( "I2C_114N.IN", "r+" )) == NULL )
    {
        printf( "The file 'I2C_114N.IN' was not opened\n" );
        return 0;
    }        
  } 
  else
  {
    if( (fp = fopen( "I2C_114P.IN", "r+" )) == NULL )
    {
        printf( "The file 'I2C_114P.IN' was not opened\n" );
        return 0;
    }
  }
    
  fscanf(fp,"%d",&dCount); // Read the Count of Data
  for ( i=0 ; i < dCount ; i++ )
  {
    fscanf(fp,"%x %x",&dIndex, &dValue); // Read the index and value  
    //printf(" Write I2C(114), index->%x,%x\n",dIndex,dValue);
    if(W99200I2CWrite(dIndex,&dValue,1)==FALSE)return 0;
  }
  fclose(fp);
  return 1;   
           
} 
int UTL_LVE(char *szFileName, int dwLimit, int iUseMaster)
{   

// ##### for Maste mode buffer
   unsigned int flag=0;
   char _cc0[REL_MASTER_BUFFER_SIZE+4], _cc1[REL_MASTER_BUFFER_SIZE+4];
   void *_pp0 = _cc0, *_pp1 = _cc1;
   char *_pcc0 = _cc0, *_pcc1 = _cc1;
   unsigned int _ulSeg, _ulOff, _ulAddr0, _ulAddr1;
// ##### for Maste mode buffer 
   int dRead=0; 
   int iMBufferIndex=0; 
   int key;
   int done=FALSE;
   int i;
   FILE *fp1; // Output file
   int *pRam;
   int j;  
   clock_t start_time,end_time;
   key=0;
   static int iFileCount = 0; 
   int nTemp=0;
   
   if (_LVE_Bitrate==0)
    _LVE_Bitrate=1024000;
   
   W99200WriteReg( PCR,0x61 ); 
   W99200WriteReg( PCR,0x60 );  
   for ( j=0 ; j < 4096 ; j++)
   {
      _cc0[j]= _cc1[j]=0;
   } 

    if (szFileName==NULL)
    {
//        printf("invalid filename!");
        return FALSE;
    }
   fp1=fopen(szFileName,"wb");
   if ( fp1==NULL)
   {
       printf(" \n encoding file open error !!! ");
       return FALSE;
   }

   // Set SAA7114
   if(UTL_SET_I2CVIDEO()==0)return 0;
   else printf("init ok!\n");

   // #### Video Reset          
   W99200WriteReg(Video_reset, 0xFF);//any data 

   // #### Initial the Internal memory:index 01~04 and 0c 

   BYTE DefaultRam[] = 
   {
     #include "Ram.h"
   };
   // index: 00~04  256 bytes
   for ( i=0 ; i < 5 ; i++)
   {
     pRam=(int *)(DefaultRam+(i*256));
     for ( j=0 ; j < 64 ; j++)
     {
        _dMem_Data[j]=*(pRam+j);
     }
     W99200IMEM_Write(i);
   } 
   // index: 0c  128 bytes
   pRam=(int *)(DefaultRam+(5*256));
   for ( j=0 ; j < 32 ; j++)
   {
        _dMem_Data[j]=*(pRam+j);
   }
   W99200IMEM_Write(0x0c); 
   
   // index:06    128 bytes 
   pRam=(int *)(DefaultRam+(6*256));
   for ( j=0 ; j < 32 ; j++)
   {
        _dMem_Data[j]=*(pRam+j);
   }
   W99200IMEM_Write(0x06);
     
   //5. Soft Reset          
   W99200WriteReg(Video_reset, 0xFF);//any data 

   //6. Configuration

   W99200WriteReg(Vint_enable, 0x00);
   W99200WriteReg(Vthreshold, 0x0f);// 60=15*4 int  
   W99200WriteReg(Vwork_mode,0x00); 
   if ( _LVE_PictureResoultion ==1 && _VideoStard==0) //NTSC,SIF 
      W99200WriteReg(Video_format,0x01); // SIF
   if ( _LVE_PictureResoultion ==2 && _VideoStard==0) //NTSC,QSIF
      W99200WriteReg(Video_format,0x02); // QSIF
   if ( _LVE_PictureResoultion ==1 && _VideoStard==1) //SQUARE,SIF 
      W99200WriteReg(Video_format,0x05); // SIF
   if ( _LVE_PictureResoultion ==2 && _VideoStard==1) //SQUARE,QSIF
      W99200WriteReg(Video_format,0x06); // QSIF      
   if ( _LVE_PictureResoultion ==1 && _VideoStard==2) //PAL,SIF 
      W99200WriteReg(Video_format,0x09); // SIF
   if ( _LVE_PictureResoultion ==2 && _VideoStard==2) //PAL,QSIF
      W99200WriteReg(Video_format,0x0a); // QSIF
   W99200WriteReg(Venc_cntl,0x6c);
          
   W99200WriteReg(Vframe_pattern,0x12);
   _LVE_Bitrate=_LVE_Bitrate/400; 
   int rate_m;
   int rate_l;
   rate_m= _LVE_Bitrate >> 8;
   rate_l= _LVE_Bitrate & 0x000000FF;
   W99200WriteReg(Vbit_rate_m,rate_m); // Bit-rate
   W99200WriteReg(Vbit_rate_l,rate_l); //
   W99200WriteReg(Vbv_size,0x32);//0x14);
   W99200WriteReg(Vbv_initial,0x31);//0x13);
   W99200WriteReg(Vquality,_Quality);    // 01(High) ~ 1F (Low) 
   W99200WriteReg(Vslice_header,0x01);
   W99200WriteReg(Vgop_header,0x01); 
   W99200WriteReg(Vit_hour,0x00);
   W99200WriteReg(Vit_minute,0x00);
   W99200WriteReg(Vit_second,0x00);
   W99200WriteReg(Vit_frame,0x00); 

   W99200WriteReg(Vin_offset,0x00);//00);
      
   if ( _LVE_RC_Mtehod == 0 ) // Constant Bitrate
      W99200WriteReg(Venc_cntl,0x4c);
    if ( _LVE_RC_Mtehod == 1 ) // Maximum Bitrate  
      W99200WriteReg(Venc_cntl,0x4d);
   if ( _LVE_RC_Mtehod == 2 ) // Variable Bitrate 
      W99200WriteReg(Venc_cntl,0x4e);  
   W99200WriteReg(Vin_picture,0x00);

   // #### for Master mode
   if ( iUseMaster == 1 )
   {
   // ##### Allocate Master Mode Buffer0  
//       _ulSeg = _FP_SEG(_pp0);
//       _ulOff = _FP_OFF(_pp0);
//       _ulAddr0 = (_ulSeg << 4) + _ulOff;
	_ulAddr0=_pp0;
       for( i = 1; i < 4; i ++ )    // Find the 4 address
       {
          if((_ulAddr0 & 0x0003L) == 0)
              break;
          _ulAddr0 ++;
          _pcc0 ++;
       }
   // ##### Allocate Master Mode Buffer1
//       _ulSeg = _FP_SEG(_pp1);
//       _ulOff = _FP_OFF(_pp1);
//       _ulAddr1 = (_ulSeg << 4) + _ulOff;
	_ulAddr1=_pp1;
       for( i = 1; i < 4; i ++ )    // Find the 4 address
       {
          if((_ulAddr1 & 0x0003L) == 0)
              break;
          _ulAddr1 ++;
          _pcc1 ++;
       }
      // ##### Set W99200 register for master mode
      W99200WriteReg(Vthreshold, 0x000F);// 15*4 double-word(60*4 bytes)
      W99200WriteReg(BBSR,0x03FB03FBL);  // Buffer Size -1 ( 1020-1 =1019=0x03FB ) uint is int 
                                         // Buffer Size must be multiple of threshold level int
                                         // So 1024/60=17.066 Buffer Size= 17*60=1020     
      // ##### Buffer 0
      W99200WriteReg(BBSAR0, _ulAddr0); // Main Memory address (Buffer0)
      W99200WriteReg(BBSTR0,0x03);      // Buffer ready
      // ##### Buffer 1
      W99200WriteReg(BBSAR1, _ulAddr1); // Main Memory address (Buffer1)
      W99200WriteReg(BBSTR1,0x03);      // Buffer ready
      // ##### Open Master Enable
      W99200WriteReg(PCICR,0x02);     
   }

     //7. VStart Command
   W99200WriteReg(Vstart,0xff); // any data
   start_time=clock();
   if ( dwLimit == 0) // No Time Limit=> until Press 'S'
   {
 /*       printf(" \n Press 's' or 'S' to stop live video encoding\n");
        do
        {
            
            key=_bios_keybrd(_KEYBRD_READY)&0xff;
            if (key!=0 ) // any key is pressed
            {
                  key=_bios_keybrd(_KEYBRD_READ)&0xff;
                  if((key=='S')||(key=='s') ) // "s" key is pressed
                  {
                        W99200WriteReg(Vstop,0xff);//9. Stop command
                  }
            }
            //8. Reverse Data transfer + handling interrupts
            if ( iUseMaster == 1 )
            {      
                  // Poll BSTR0 to check if Buffer 0 is full 
                  dRead=W99200ReadReg(BBSTR0);
                  if ( ((dRead & 0x02) == 0x02 )  && iMBufferIndex == 0 )
                  {
                        if ( fwrite(_pcc0, sizeof(BYTE),4080,fp1) != 4080 )
                                printf( " Buffer 0 write file error!\n" );
                        W99200WriteReg(BBSTR0,0x03);
                        iMBufferIndex=1;
                  } 
                  // Poll BSTR1 to check if Buffer 1 is full 
                  dRead=W99200ReadReg(BBSTR1);
                  if ( ((dRead & 0x02) == 0x02 ) && iMBufferIndex ==1 )
                  {
                        if ( fwrite(_pcc1, sizeof(BYTE),4080,fp1) != 4080 )
                             printf( " Buffer 1 write file error!\n" );
                        W99200WriteReg(BBSTR1,0x03);
                        iMBufferIndex=0;
                  }
            } //if ( iUseMaster == 1 )
            else
            {
                  if ( W99200FIFO_Thr()== TRUE )
                  {
                        for( i=0 ; i <60  ; i++)
                        {
                            _dMem_Data[i]=W99200Inpdw(_IOBase+_ulODPR);
                        }
                        fwrite(_dMem_Data,sizeof(int),60,fp1);
                        
                  }
            }
        }while((key!='s')&&(key!='S') && (W99200FIFO_End()!=TRUE));
   */}
   else
   {
        printf("Recording for %d seconds...", dwLimit);
//        printf(" \n Stop After %d Second",dwLimit);
        do
        {
           //8. Reverse Data transfer + handling interrupts
            if ( iUseMaster == 1 )
            {      
                  // Poll BSTR0 to check if Buffer 0 is full 
                  dRead=W99200ReadReg(BBSTR0);
                  if ( ((dRead & 0x02) == 0x02 )  && iMBufferIndex == 0 )
                  {
                        if ( fwrite(_pcc0, sizeof(BYTE),4080,fp1) != 4080 )
                                printf( " Buffer 0 write file error!\n" );
                        W99200WriteReg(BBSTR0,0x03);
                        iMBufferIndex=1;
                  } 
                  // Poll BSTR1 to check if Buffer 1 is full 
                  dRead=W99200ReadReg(BBSTR1);
                  if ( ((dRead & 0x02) == 0x02 ) && iMBufferIndex ==1 )
                  {
                        if ( fwrite(_pcc1, sizeof(BYTE),4080,fp1) != 4080 )
                             printf( " Buffer 1 write file error!\n" );
                        W99200WriteReg(BBSTR1,0x03);
                        iMBufferIndex=0;
                  }
            } //if ( iUseMaster == 1 )
            else
            {
                  if ( W99200FIFO_Thr()== TRUE )
                  {
                        for( i=0 ; i <60  ; i++)
                        {
                            _dMem_Data[i]=W99200Inpdw(_IOBase+_ulODPR);
                        }
                        fwrite(_dMem_Data,sizeof(int),60,fp1);
                        
                  }
            }
           end_time=clock(); 
           if((int)(end_time-start_time) > dwLimit*1000 ) // "s" key is pressed
                W99200WriteReg(Vstop,0xff);//9. Stop command  
        //printf(" %d ",int(end_time-start_time));     
	}//while(((int)(end_time-start_time) <=dwLimit*1000) && (W99200FIFO_End()!=TRUE));
	while((flag++<200000*dwLimit) && (W99200FIFO_End()!=TRUE));
     
   }   
    //10. Reverse Data transfer + handling interrupts 
    printf("iMBufferIndex=%d\niUseMaster=%d\n",iMBufferIndex,iUseMaster);
    if ( iUseMaster == 1 )
    {   
        
        if ( iMBufferIndex == 0 )
        {
            dRead=W99200ReadReg(BBFR);
            
            if ( fwrite(_pcc0, sizeof(BYTE),(unsigned int)dRead*4,fp1) != (unsigned int)(dRead*4) )
                   printf( " Buffer 0 write file error!\n" );
             else printf("write ok\n");
        }
        else
        {
            dRead=W99200ReadReg(BBFR);
            if ( fwrite(_pcc1, sizeof(BYTE),(unsigned int)(dRead*4),fp1) != (unsigned int)(dRead*4) )
                   printf( " Buffer 1 write file error!\n" );
                  else printf("write ok2\n");
        }
        W99200WriteReg(PCICR,0x00);     // Close Master Enable    
    }
    else
    {                    
      do 
      {
         if ( W99200FIFO_Thr()== TRUE )
         {
            printf("fifo");
            for( i=0 ; i < 60 ; i++)
               _dMem_Data[i]=W99200Inpdw(_IOBase+_ulODPR);
            fwrite(_dMem_Data,sizeof(int),60,fp1);
         }
      } while ( W99200FIFO_End()!=TRUE  );// Pooling the End of Data
    }
    if ( dwLimit == 0)
    {
      end_time=clock();
      printf("\n Time : %ld second", (end_time-start_time)/1000 );
    }
    printf("\n OutPutFile : %s\n",szFileName);   
    fclose(fp1);
    return TRUE;
}  
  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -