int EncoderMFC::Initialize(int width, int height,int codec,float rate,int bitrate,int in_bufs,int out_bufs, int stream_b_size){ if(state!=MFC_ST_OPEN) return -1; if(SetFormat(width,height)<0) return -1; if(SetCodec(codec,stream_b_size)<0) return -1; SetRate(rate); if(bitrate>0) SetBitRate(bitrate); if(InitBuffers(DIR_IN,in_bufs)<0) return -1; if(InitBuffers(DIR_OUT,out_bufs)<0) return -1; for (int bi=0;bi<out_bufs;bi++){ struct v4l2_buffer buf; struct v4l2_plane planes[MFC_MAX_PLANES]; buf.type = io_dir_to_type(DIR_OUT); buf.memory = V4L2_MEMORY_MMAP; buf.m.planes = planes; buf.length = NPPBuf[DIR_OUT]; buf.index=bi; for (u_int i = 0; i < NPPBuf[DIR_OUT]; i++){ planes[i].length=PlanesLen[DIR_OUT][i]; planes[i].bytesused=0; planes[i].m.userptr=(u_long)BufAddr[DIR_OUT][bi][i]; } int ret = ioctl(fd, VIDIOC_QBUF, &buf); if (ret != 0) { printf("\nEncoderMFC: Init, Queue buffer %d error! %s\n",buf.index,strerror(errno)); } } BufInx[DIR_OUT]=0; state=MFC_ST_OK; return 0; }
BOOL ISndStreamMPEG::OpenStream(CString strFileName) { DWORD dwID3TagOffset = 0; INT nXingTagOffset = 0; // Set buffer to hold maximum MP3 frame SetBufferSize( MAXSIZE_MP3FRAME ); // Get the DLL functions m_hDLL=LoadLibrary("MpgLib.dll"); // just for development purposes, try to load the DLL from the devo path #ifdef _DEBUG if (m_hDLL==NULL) m_hDLL=LoadLibrary(g_config.GetAppPath()+"\\MpgLibDLL\\Debug\\MpgLib.dll"); #else if (m_hDLL==NULL) m_hDLL=LoadLibrary(g_config.GetAppPath()+"\\MpgLibDLL\\Release\\MpgLib.dll"); #endif if (m_hDLL==NULL) { ASSERT(FALSE); return FALSE; } //Get pointer to functions m_pOpenStream =(MPGLIB_OPENSTREAM)GetProcAddress(m_hDLL,TEXT_MPGLIB_OPENSTREAM); m_pDecode =(MPGLIB_DECODECHUNK)GetProcAddress(m_hDLL,TEXT_MPGLIB_DECODECHUNK); m_pCloseStream =(MPGLIB_CLOSESTREAM)GetProcAddress(m_hDLL,TEXT_MPGLIB_CLOSESTREAM); m_pVersion =(MPGLIB_VERSION)GetProcAddress(m_hDLL,TEXT_MPGLIB_VERSION); m_pGetBitRate =(MPGLIB_GETBITRATE)GetProcAddress(m_hDLL,TEXT_MPGLIB_GETBITRATE); m_pFlush =(MPGLIB_GETBITRATE)GetProcAddress(m_hDLL,TEXT_MPGLIB_FLUSH); // Check if all functions are available if ( (m_pOpenStream==NULL) || (m_pDecode==NULL) || (m_pCloseStream==NULL)|| (m_pVersion==NULL)) { ASSERT(FALSE); return FALSE; } // Register the file name SetFileName(strFileName); // Get MP3 ID3V2 Tag offset CID3Tag ID3Tag; ID3Tag.Init(); ID3Tag.OpenFile( GetFileName() ); ID3Tag.LoadTag(); if ( ID3Tag.IsV2Tag() ) { dwID3TagOffset = ID3Tag.GetTagSize(); } // Open MPEG stream if (! (m_pFile = fopen( GetFileName(), "rb" ) ) ) { ASSERT(FALSE); return FALSE; } m_dwCurrentFilePos = 0; if ( dwID3TagOffset ) { fseek(m_pFile, dwID3TagOffset, SEEK_SET ); } fread( m_pbDataInBuffer, 1024, 1, m_pFile ); // Clear header information m_Header.Init(); // get Xing VBR header if available if ( m_Header.ReadHeader( m_pbDataInBuffer, 1024, &nXingTagOffset ) == FALSE ) { return FALSE; } // Extract properties from header information SetSampleRate( m_Header.GetSampleRate() ); SetBitRate( m_Header.GetSampleRate() ); // Get file size fseek( m_pFile, 0, SEEK_END ); m_dwTotalFileSize= ftell( m_pFile ); // seek back to beginning of stream fseek( m_pFile, dwID3TagOffset + nXingTagOffset, SEEK_SET ); m_dwBytesToDo=m_dwTotalFileSize-nXingTagOffset-dwID3TagOffset; // Initialize MPEG structure m_pOpenStream(&m_hStream); SetChannels(m_Header.GetChannels()); m_dwSkipBytes=1105*sizeof(SHORT)*m_Header.GetChannels(); // Return Success return TRUE; }
DWORD ISndStreamMPEG::Read(PBYTE pbData,DWORD dwNumBytes) { BOOL bEos=FALSE; static int nFrame=0; // check if file pointer is valid ASSERT(m_pFile); // check size of output buffer, must be larger than largest frame ASSERT(dwNumBytes>=4096); int nErrorCode=!MPGLIB_OK; int nDataOutBuffer=0; // while ( (nErrorCode!=MPGLIB_OK) && (m_nDataInBuffer>0)) // while ( (m_nDataInBuffer>0) && (nDataOutBuffer==0)) // Try to decode a block, until were not at the end of the input stream // end till we have a decoded frame while ( (bEos==FALSE) && (nDataOutBuffer==0) ) { // Try to decode the data nErrorCode= m_pDecode( m_hStream, NULL, 0, pbData, dwNumBytes, &nDataOutBuffer); if ( nErrorCode==MPGLIB_NEED_MORE ) { DWORD dwRead= (m_dwBytesToDo>MPEG_WAV_BUFFERSIZE)?MPEG_WAV_BUFFERSIZE:m_dwBytesToDo; // Read MPEG data from disk dwRead=fread(m_pbDataInBuffer,1,dwRead,m_pFile); m_dwCurrentFilePos+= dwRead; if (dwRead!=0) { m_dwBytesToDo-=dwRead; // CString strTmp; // strTmp.Format("******* Read %d bytes at frame %d\n",dwRead,nFrame); // OutputDebugString(strTmp); // Fed new data to Decode library, and decode a frame nErrorCode = m_pDecode(m_hStream, m_pbDataInBuffer, dwRead, pbData, dwNumBytes, &nDataOutBuffer); } else { // Were stuck, no more data, and the decoder needs more bEos=TRUE; } // Get bitrate of this frame SetBitRate(m_pGetBitRate()); } if ( m_dwTotalFileSize ) { SetPercent( (INT)( (double) m_dwCurrentFilePos * 100.0 / m_dwTotalFileSize ) ); } else { SetPercent( 0 ); } } nFrame++; return nDataOutBuffer; }
BOOL ISndStreamWAV::OpenStream( const CUString& strFileName ) { SF_INFO wfInfo; LTRACE( _T( "ISndStreamWAV::OpenStream( %s )" ), strFileName ); // Set the buffer size SetBufferSize(12000); memset(&wfInfo,0,sizeof(SF_INFO)); SetFileName(strFileName); // Open stream CUStringConvert strCnv; #ifdef _UNICODE m_pSndFile = sf_open((const tchar*)strCnv.ToT( GetFileName() ), SFM_READ, &wfInfo ); #else m_pSndFile = sf_open( strCnv.ToT( GetFileName() ), SFM_READ, &wfInfo ); #endif if ( NULL == m_pSndFile ) { LTRACE( _T( "ISndStreamWAV::OpenStream( %s ) failed" ), strFileName ); ASSERT(FALSE); return FALSE; } SetChannels( wfInfo.channels ); SetSampleRate(wfInfo.samplerate); WORD wBitsPerSample = 0; switch ( wfInfo.format & SF_FORMAT_SUBMASK ) { case SF_FORMAT_PCM_S8: wBitsPerSample = 8; break; case SF_FORMAT_PCM_16: wBitsPerSample = 16; break; case SF_FORMAT_PCM_24: wBitsPerSample = 24; break; case SF_FORMAT_PCM_32: wBitsPerSample = 32; break; default: wBitsPerSample = 16; ASSERT( FALSE ); } SetBitRate( wfInfo.samplerate * wfInfo.channels * wBitsPerSample ); m_dwSamples = (DWORD)wfInfo.frames; m_ddwTotalFileSize = sf_seek( m_pSndFile, 0, SEEK_END ) * wfInfo.channels * ( wBitsPerSample / 8 ); m_ddwCurrentFilePos = 0; sf_seek( m_pSndFile, 0, SEEK_SET ); // return Success return TRUE; }
DWORD ISndStreamWinAmp::Read(PBYTE pbData,DWORD dwNumBytes) { int nReturn = 0; LONG lastcount = 0; LTRACE3( _T( "ISndStreamWinAmp::Read dwNumBytes %d CurrentFilePos %d TotalFileSize %d " ), dwNumBytes, m_dwCurrentFilePos, m_dwTotalFileSize ); m_dwCurrentFilePos+= dwNumBytes; int nCurrentTime = m_WinAmpProp.pModule->GetOutputTime(); int nTotalTime = m_WinAmpProp.pModule->GetLength(); if ( nTotalTime ) { int t = g_nOutSampleRate*g_nNumChannels; int l = 0; int ms = g_nFifoTotalBytes; if ( t ) { l=ms%t; ms /= t; ms *= 1000; ms += (l*1000)/t; if (g_nBitsPerSample == 16) ms/=2; } else { ms = 0; } SetPercent( (INT)( (double) (nCurrentTime - ms) * 100.0 / nTotalTime ) ); } else { SetPercent( 0 ); } // wait for incoming data HANDLE pWaitEvents[2] = { (HANDLE)g_eFifoNotEmpty, (HANDLE)g_eFinished }; int nEventIdx = WaitForMultipleObjects( 2, pWaitEvents, FALSE, INFINITE ) - WAIT_OBJECT_0 ; if ( 1 == nEventIdx ) { nReturn = 0; } else { nReturn = g_pFifo[g_nFifoReadIdx].nBytesWritten; memcpy( pbData, g_pFifo[g_nFifoReadIdx].pbtBuffer, nReturn ); LTRACE2( _T( "ISndStreamWinAmp::Read getting %d bytes from buffer %d (freebuffers = %d)" ), g_pFifo[g_nFifoReadIdx].nBytesWritten, g_nFifoReadIdx, g_nFifoFreeBuffers ); // lock access to FIFO parameters g_mFifoAcces.Lock(); if ( g_nFifoFreeBuffers < WAPI_FIFO_NUM_BUFFERS ) { g_nFifoFreeBuffers++; } if ( WAPI_FIFO_NUM_BUFFERS == g_nFifoFreeBuffers ) { LTRACE( _T( "Reset g_eFifoNotEmpty Event" ) ); g_eFifoNotEmpty.ResetEvent(); } if ( 2 == g_nFifoFreeBuffers ) { LTRACE( _T( "Set g_eFifoNotFull Event" ) ); g_eFifoNotFull.SetEvent(); } g_nFifoTotalBytes-= nReturn; // unlock access to FIFO parameters g_mFifoAcces.Unlock(); // increase FIFO read index pointer g_nFifoReadIdx = ( ( g_nFifoReadIdx + 1 ) % WAPI_FIFO_NUM_BUFFERS ); LTRACE( _T( "FreeBuffers on Read :%d, bytes in fifo %d" ), g_nFifoFreeBuffers, g_nFifoTotalBytes ); } // update the bitrate info SetBitRate( g_nBitRate ); LTRACE2( _T( "ISndStreamWinAmp::Read returns %d" ), nReturn ); return nReturn; }
BOOL ISndStreamWinAmp::OpenStream( const CUString& strFileName ) { DWORD i = 0; BOOL bFound = FALSE; LTRACE2( _T( "ISndStreamWinAmp::OpenStream( %s )" ), strFileName ); int nPos = 0; CUString strExt( _T( "wav" ) ); g_bFinished = FALSE; g_bInitialized = FALSE; g_bOutOpened = FALSE; g_eFinished.ResetEvent(); FlushFIFO( ); nPos = strFileName.ReverseFind('.'); if ( nPos >= 0 ) { strExt = strFileName.Right( strFileName.GetLength() - nPos - 1 ); strExt.MakeLower(); } // Set the buffer size SetBufferSize( 9000 ); SetFileName( strFileName ); int nRet = 0; // loop through the available plugings for ( i=0; i < gs_vWinAmpProps.size(); i++ ) { m_WinAmpProp = gs_vWinAmpProps[i]; // m_WinAmpProp.pModule->Init( ); // check if the module can handle the file format if ( ( nRet = m_WinAmpProp.pModule->IsOurFile( (LPSTR)GetDosFileName( strFileName ) ) ) ) { bFound = TRUE; break; } /* if ( ( 0 == strExt.CompareNoCase( _T( "flac" ) ) ) && ( 0 == strExt.CompareNoCase( m_WinAmpProp.strExt ) ) ) { bFound = TRUE; break; } */ CUString strPluginExt( m_WinAmpProp.pModule->FileExtensions, CP_UTF8 ); strPluginExt.MakeLower(); // check first on extention if ( strPluginExt.Find( strExt ) >=0 ) { bFound = TRUE; break; } // m_WinAmpProp.pModule->Quit( ); } if ( !bFound ) { LTRACE2( _T( "ISndStreamWinAmp::OpenStream Failed, no proper plugin found!" ) ); return FALSE; } if ( 0 == m_WinAmpProp.pModule->UsesOutputPlug ) { LTRACE2( _T( "ISndStreamWinAmp::OpenStream; Failed, does not use Output Plugin " ) ); return FALSE; } LTRACE2( _T( "ISndStreamWinAmp::OpenStream found proper plugin ( %s )" ) , m_WinAmpProp.pModule->description ); // setup proper function calls m_WinAmpProp.pModule->SetInfo = SetInfo; m_WinAmpProp.pModule->SAAddPCMData = SAAddPCMData; m_WinAmpProp.pModule->SAGetMode = SAGetMode; m_WinAmpProp.pModule->SAAdd = SAAdd; m_WinAmpProp.pModule->VSAAddPCMData = VSAAddPCMData; m_WinAmpProp.pModule->VSAGetMode = VSAGetMode; m_WinAmpProp.pModule->VSAAdd = VSAAdd; m_WinAmpProp.pModule->VSASetInfo = VSASetInfo; m_WinAmpProp.pModule->dsp_isactive = dsp_isactive; m_WinAmpProp.pModule->dsp_dosamples = dsp_dosamples; m_WinAmpProp.pModule->EQSet = EQSet; m_WinAmpProp.pModule->SAVSADeInit = WMIN_SAVSADeInit; m_WinAmpProp.pModule->SAVSAInit = WMIN_SAVSAInit; m_WinAmpProp.pModule->outMod = &g_OutModule; g_OutModule.version = OUT_VER; g_OutModule.description = g_lpszOutModuleName; g_OutModule.id = 33; g_OutModule.Config = OutConfig; g_OutModule.About = OutAbout; g_OutModule.Init = OutInit; g_OutModule.Quit = OutQuit; g_OutModule.Open = OutOpen; g_OutModule.Close = OutClose; g_OutModule.Write = OutWrite; g_OutModule.CanWrite = OutCanWrite; g_OutModule.IsPlaying = OutIsPlaying; g_OutModule.Pause = OutPause; g_OutModule.SetVolume = OutSetVolume; g_OutModule.SetPan = OutSetPan; g_OutModule.Flush = OutFlush; g_OutModule.GetOutputTime = OutGetOutputTime; g_OutModule.GetWrittenTime = OutGetWrittenTime; SetChannels( 2 ); SetSampleRate( 44100 ); SetBitRate( 176000 * 8 ); // m_dwSamples=wfInfo.samples; m_dwTotalFileSize = 1000; m_dwCurrentFilePos = 0; // start the decoding thread if ( 0 != m_WinAmpProp.pModule->Play( (LPSTR)GetDosFileName( strFileName ) ) ) { return FALSE; } // Wait till output device has been opened by the // input plugin while ( FALSE == g_bOutOpened ) { ::Sleep( 5 ); } SetChannels( g_nNumChannels ); SetSampleRate( g_nOutSampleRate ); SetBitRate( g_nBitRate ); g_bInitialized = TRUE; // return Success return TRUE; }
/* Main procedure */ void main(int argc, char * argv[]) { int newState = 0; HFILE com; ULONG action; DCBparam DCB; if (argc != 2) { fprintf(stderr, "Incorrect number of parameters!\n"); help(); exit(1); } if ((strcmpi(argv[1], "COM1")<0) || (strcmpi(argv[1], "COM4")>0)) { fprintf(stderr, "Serial port name required!\n"); help(); exit(2); } unsigned long datasize = sizeof(DCB); if (DosOpen(argv[1], &com, &action, 0L, // logical size 0L, // file attributes 0x01, // action on existence 0x0012, // openbits 0L)) exit(1); RaiseDTR(com); if (!CheckDSR(com)) DosBeep(1000, 1000L); DosDevIOCtl(com, 0x01, 0x73, NULL, 0, NULL, &DCB, datasize, &datasize); DCB.writeTimeout = 1000; DCB.readTimeout = 100; DCB.flags1 = 0x00; DCB.flags3 = (DCB.flags3 & 0xF9) + 0x04; SetBitRate(com, 110); while (CheckDSR(com)) { newState = CheckState(com); if (newState != state) Powerfail(newState, com); switch(state) { case 1: DosBeep(500, 10L); break; case 2: DosBeep(4000, 10L); break; default: break; } switch (doShutdown) { case 1: shutdown(com); break; case 2: { DosShutdown(0); UPSPowerOff(com); }; break; default: break; } DosSleep(1000); } DosClose(com); exit(-1); }