//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 15/08/2001 // PROJECT: // SUBSYSTEM: // FILE: DSVideoProcessor.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of DSVideoProcessor.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "DSVideoProcessor.h" #include "DSConfiguration.h" CDSVideoProcessor::CDSVideoProcessor() { m_pVideoWindow = NULL; m_pAMCrossbar = NULL; m_pAMVideoProcAmp = NULL; m_pSampleGrabber = NULL; m_pSampleGrabberCB = NULL; m_pVideoHeader = NULL; m_pInputPin = NULL; m_nInputPinCount = 0; m_nOutputPin = -1; m_nVideoType = VT_NONE_VIDEO; m_ppPseudoVideoWnd = NULL; m_pbPseudoVideoWnd = NULL; m_nPseudoVideoWnd = 0; m_nPseudoCaptureLoop = 0; m_nPseudoLastInput = -1; m_dwPseudoLastTime = 0; m_pAMAnalogVideoDecoder = NULL; m_hEventVideoSample = NULL; ::CoInitialize( NULL ); } CDSVideoProcessor::~CDSVideoProcessor() { Destroy(); ::CoUninitialize(); } BOOL CDSVideoProcessor::Create( CDSConfiguration* pDirectShow ) { Destroy(); if( !pDirectShow ) return FALSE; IAMCrossbar *pCrossbar = pDirectShow->GetCrossbar(); if( !pCrossbar ) return FALSE; HRESULT hr = pCrossbar->QueryInterface( IID_IAMCrossbar, (void**)&m_pAMCrossbar ); if( FAILED( hr ) ) return FALSE; IBaseFilter *pBaseFilter = NULL; hr = m_pAMCrossbar->QueryInterface( IID_IBaseFilter, (void**)&pBaseFilter ); if( FAILED( hr ) ) return FALSE; FindMembers( pBaseFilter ); pBaseFilter->Release(); InitSwitch(); if( !m_pSampleGrabber ) return FALSE; if( m_pVideoWindow ) { m_pVideoWindow->put_Visible( OAFALSE ); m_pVideoWindow->put_AutoShow( OAFALSE ); } if( m_pAMVideoProcAmp ) { m_pAMVideoProcAmp->Set( VideoProcAmp_Saturation, 0, 0 ); } m_hEventVideoSample = ::CreateEvent( NULL, FALSE, FALSE, NULL ); if( !m_hEventVideoSample ) return FALSE; m_pSampleGrabberCB = new CDSSampleGrabberCB( this ); if( !m_pSampleGrabberCB ) return FALSE; hr = m_pSampleGrabber->SetOneShot(FALSE); if( FAILED( hr ) ) return FALSE; hr = m_pSampleGrabber->SetBufferSamples(FALSE); if( FAILED( hr ) ) return FALSE; hr = m_pSampleGrabber->SetCallback( m_pSampleGrabberCB, 1 ); if( FAILED( hr ) ) return FALSE; AM_MEDIA_TYPE mt; if( FAILED( m_pSampleGrabber->GetConnectedMediaType(&mt) ) ) return FALSE; if( mt.formattype != FORMAT_VideoInfo ) return FALSE; m_pVideoHeader = (VIDEOINFOHEADER*)mt.pbFormat; if( !m_pVideoHeader ) return FALSE; return Start( FALSE ); } void CDSVideoProcessor::FindMembers( IBaseFilter *pMemberFilter ) { IEnumPins *pEnumPins = NULL; HRESULT hr = pMemberFilter->EnumPins( &pEnumPins ); if( FAILED(hr) ) return; IPin *pPinOut = NULL; while( pEnumPins->Next( 1, &pPinOut, NULL ) == S_OK ) { PIN_INFO PinInfo; hr = pPinOut->QueryPinInfo( &PinInfo ); if( FAILED( hr ) ) { pPinOut->Release(); pPinOut = NULL; continue; } if( PinInfo.dir == PINDIR_INPUT ) { pPinOut->Release(); pPinOut = NULL; continue; } IPin *pPinIn = NULL; hr = pPinOut->ConnectedTo( &pPinIn ); if( FAILED( hr ) ) { pPinOut->Release(); pPinOut = NULL; continue; } hr = pPinIn->QueryPinInfo( &PinInfo ); if( FAILED( hr ) ) { pPinOut->Release(); pPinOut = NULL; pPinIn->Release(); continue; } if( !m_pVideoWindow ) PinInfo.pFilter->QueryInterface( IID_IVideoWindow, (void**)&m_pVideoWindow ); if( !m_pAMVideoProcAmp ) PinInfo.pFilter->QueryInterface( IID_IAMVideoProcAmp, (void**)&m_pAMVideoProcAmp ); if( !m_pSampleGrabber ) PinInfo.pFilter->QueryInterface( IID_ISampleGrabber, (void**)&m_pSampleGrabber ); if( !m_pAMAnalogVideoDecoder ) PinInfo.pFilter->QueryInterface( IID_IAMAnalogVideoDecoder, (void**)&m_pAMAnalogVideoDecoder ); FindMembers( PinInfo.pFilter ); PinInfo.pFilter->Release(); pPinOut->Release(); pPinIn->Release(); } pEnumPins->Release(); } void CDSVideoProcessor::Destroy() { TRACE( "INFO: CDSVideoProcessor::Destroy\n" ); Stop( TRUE ); if( m_pSampleGrabber ) { m_pSampleGrabber->SetCallback( NULL, 1 ); m_pSampleGrabber->Release(); m_pSampleGrabber = NULL; } // m_pSampleGrabberCB = NULL; if( m_pAMCrossbar ) { m_pAMCrossbar->Release(); m_pAMCrossbar = NULL; } if( m_pVideoWindow ) { m_pVideoWindow->Release(); m_pVideoWindow = NULL; } if( m_pAMVideoProcAmp ) { m_pAMVideoProcAmp->Release(); m_pAMVideoProcAmp = NULL; } if( m_pSampleGrabberCB ) { m_pSampleGrabberCB->Release(); m_pSampleGrabberCB = NULL; } m_pVideoHeader = NULL; if( m_pInputPin ) { delete [] m_pInputPin; m_pInputPin = NULL; } m_nInputPinCount = 0; m_nOutputPin = -1; m_nPseudoLastInput = -1; if( m_ppPseudoVideoWnd ) { delete [] m_ppPseudoVideoWnd; m_ppPseudoVideoWnd = NULL; } m_nPseudoVideoWnd = 0; if( m_pAMAnalogVideoDecoder ) { m_pAMAnalogVideoDecoder->Release(); m_pAMAnalogVideoDecoder = NULL; } if( m_hEventVideoSample ) { ::CloseHandle( m_hEventVideoSample ); m_hEventVideoSample = NULL; } if( m_pbPseudoVideoWnd ) { delete [] m_pbPseudoVideoWnd; m_pbPseudoVideoWnd = NULL; } } BOOL CDSVideoProcessor::SwitchVideo( int nInputPin ) { if( m_nVideoType != VT_LIVE_VIDEO ) return FALSE; if( m_nInputPinCount > nInputPin ) { HRESULT hr = m_pAMCrossbar->Route( m_nOutputPin, m_pInputPin[ nInputPin ] ); if( FAILED( hr ) ) return FALSE; } return TRUE; } BOOL CDSVideoProcessor::GetDirectInput( PLONG pInput ) { if( !m_pAMVideoProcAmp ) return FALSE; LONG lData = 0; return SUCCEEDED( m_pAMVideoProcAmp->Get( VideoProcAmp_Gamma, pInput, &lData ) ); } BOOL CDSVideoProcessor::SetDirectOutput( LONG nIndex, BOOL bOn ) { if( !m_pAMVideoProcAmp ) return FALSE; LONG lData = 0; return SUCCEEDED( m_pAMVideoProcAmp->Set( bOn ? VideoProcAmp_Sharpness : VideoProcAmp_Gamma, nIndex, lData ) ); } BOOL CDSVideoProcessor::Capture( LPBITMAPINFO pBitmapInfo, LPBYTE pBytes ) { if( !pBytes ) return FALSE; LPBYTE pSrcBuffer = NULL; LONG nLength = 0; if( !m_pSampleGrabberCB->LockBuffer( &pSrcBuffer, &nLength ) ) return FALSE; ::CopyMemory( pBytes, pSrcBuffer, nLength ); m_pSampleGrabberCB->UnlockBuffer(); return TRUE; } HBITMAP CDSVideoProcessor::CaptureDIB() { if( !m_pVideoHeader ) return NULL; if( !m_pSampleGrabberCB ) return NULL; LPBYTE pDstBuffer = NULL; HBITMAP hBitmap = ::CreateDIBSection( 0, (LPBITMAPINFO)&m_pVideoHeader->bmiHeader, DIB_RGB_COLORS, (void**)&pDstBuffer, NULL, 0UL ); if( !hBitmap ) return NULL; LPBYTE pSrcBuffer = NULL; LONG nLength = 0; if( !m_pSampleGrabberCB->LockBuffer( &pSrcBuffer, &nLength ) ) { DeleteObject( hBitmap ); return NULL; } ::CopyMemory( pDstBuffer, pSrcBuffer, nLength ); m_pSampleGrabberCB->UnlockBuffer(); return hBitmap; } BOOL CDSVideoProcessor::InitSwitch() { if( !m_pAMCrossbar ) return FALSE; long OutputPinCount; long InputPinCount; HRESULT hr = m_pAMCrossbar->get_PinCounts( &OutputPinCount, &InputPinCount ); if( FAILED( hr ) ) return FALSE; for( long i = 0; i < OutputPinCount && m_nOutputPin < 0; i++ ) { long PinIndexRelated; long PhysicalType; HRESULT hr = m_pAMCrossbar->get_CrossbarPinInfo( FALSE, i, &PinIndexRelated, &PhysicalType ); if( FAILED( hr ) ) return FALSE; switch( PhysicalType ) { case PhysConn_Video_Tuner: case PhysConn_Video_Composite: case PhysConn_Video_SVideo: case PhysConn_Video_RGB: case PhysConn_Video_YRYBY: case PhysConn_Video_SerialDigital: case PhysConn_Video_ParallelDigital: case PhysConn_Video_SCSI: case PhysConn_Video_AUX: case PhysConn_Video_1394: case PhysConn_Video_USB: case PhysConn_Video_VideoDecoder: case PhysConn_Video_VideoEncoder: case PhysConn_Video_SCART: m_nOutputPin = i; break; } } PLONG pInputPin = new LONG[ InputPinCount ]; if( !pInputPin ) return FALSE; long nInput = 0; for( i = 0; i < InputPinCount; i++ ) { long PinIndexRelated = 0; long PhysicalType = -1; HRESULT hr = m_pAMCrossbar->get_CrossbarPinInfo( TRUE, i, &PinIndexRelated, &PhysicalType ); if( FAILED( hr ) ) { delete [] pInputPin; return FALSE; } switch( PhysicalType ) { case PhysConn_Video_Tuner: case PhysConn_Video_Composite: case PhysConn_Video_SVideo: case PhysConn_Video_RGB: case PhysConn_Video_YRYBY: case PhysConn_Video_SerialDigital: case PhysConn_Video_ParallelDigital: case PhysConn_Video_SCSI: case PhysConn_Video_AUX: case PhysConn_Video_1394: case PhysConn_Video_USB: case PhysConn_Video_VideoDecoder: case PhysConn_Video_VideoEncoder: case PhysConn_Video_SCART: pInputPin[ nInput++ ] = i; break; } } m_nInputPinCount = nInput; m_pInputPin = new LONG[ m_nInputPinCount ]; if( !m_pInputPin ) { delete [] pInputPin; return FALSE; } ::CopyMemory( m_pInputPin, pInputPin, sizeof(LONG) * m_nInputPinCount ); delete [] pInputPin; m_ppPseudoVideoWnd = new CDDVideoWnd* [ m_nInputPinCount ]; if( !m_ppPseudoVideoWnd ) return FALSE; m_pbPseudoVideoWnd = new BOOL[ m_nInputPinCount ]; if( !m_pbPseudoVideoWnd ) return FALSE; ::ZeroMemory( m_pbPseudoVideoWnd, sizeof(BOOL) * m_nInputPinCount ); ::ZeroMemory( m_ppPseudoVideoWnd, sizeof(CDDVideoWnd*) * m_nInputPinCount ); m_nPseudoVideoWnd = 0; return TRUE; } BOOL CDSVideoProcessor::ConnectVideoWnd( IVideoWindow** ppVideoWindow ) { if( m_nVideoType != VT_NONE_VIDEO ) return FALSE; if( !ppVideoWindow ) return FALSE; if( !m_pVideoWindow ) { *ppVideoWindow = NULL; return FALSE; } HRESULT hr = m_pVideoWindow->QueryInterface( IID_IVideoWindow, (void**)ppVideoWindow ); if( FAILED( hr ) ) { *ppVideoWindow = NULL; return FALSE; } m_nVideoType = VT_LIVE_VIDEO; return TRUE; } BOOL CDSVideoProcessor::ConnectVideoWnd( CDDVideoWnd *pVideoWnd, int nInput ) { if( m_nVideoType != VT_NONE_VIDEO && m_nVideoType != VT_MULTIPLEX_VIDEO ) return FALSE; if( !m_ppPseudoVideoWnd ) return FALSE; if( nInput >= m_nInputPinCount ) return FALSE; if( nInput > 0 ) { if( m_ppPseudoVideoWnd[ nInput ] ) return FALSE; m_ppPseudoVideoWnd[ nInput ] = pVideoWnd; m_ppPseudoVideoWnd[ nInput ]->SetImageFormat( (LPBITMAPINFO)&m_pVideoHeader->bmiHeader, sizeof(BITMAPINFO) ); m_nVideoType = VT_MULTIPLEX_VIDEO; return TRUE; } for( nInput = 0; nInput < m_nInputPinCount; nInput++ ) { if( !m_ppPseudoVideoWnd[ nInput ] ) { m_ppPseudoVideoWnd[ nInput ] = pVideoWnd; m_ppPseudoVideoWnd[ nInput ]->SetImageFormat( (LPBITMAPINFO)&m_pVideoHeader->bmiHeader, sizeof(BITMAPINFO) ); m_nVideoType = VT_MULTIPLEX_VIDEO; return TRUE; } } return FALSE; } void CDSVideoProcessor::ActivateWnd( LONG nIndex ) { if( nIndex < 0 && nIndex >= m_nInputPinCount ) return; if( !m_pbPseudoVideoWnd ) return; m_pbPseudoVideoWnd[ nIndex ] = TRUE; } DWORD CDSVideoProcessor::ThreadProc() { HANDLE pEvent[] = { CThread::GetThreadStopEvent(), m_hEventVideoSample }; BOOL bRun = TRUE; while( bRun ) { switch( WaitForMultipleObjects( 2, pEvent, FALSE, INFINITE ) ) { case WAIT_OBJECT_0: bRun = FALSE; continue; case WAIT_OBJECT_0+1: // Sleep(100); ProcessVideoSample(); break; default: ASSERT( NULL ); bRun = FALSE; continue; } } return 0UL; } void CDSVideoProcessor::ProcessVideoSample() { // TRACE( "INFO: CDSVideoProcessor::ProcessVideoSample\n" ); __try { if( m_nVideoType != VT_MULTIPLEX_VIDEO ) return; if( !m_pAMCrossbar ) return; if( m_nPseudoCaptureLoop % 2 == 0 ) // if( (GetTickCount() - m_dwPseudoLastTime) > 100UL ) { m_dwPseudoLastTime = GetTickCount(); if( m_pbPseudoVideoWnd[ m_nPseudoVideoWnd ] && m_ppPseudoVideoWnd[ m_nPseudoVideoWnd ] && m_ppPseudoVideoWnd[ m_nPseudoVideoWnd ]->GetSafeHwnd() ) { LPBITMAPINFO pBitmapInfo = NULL; LPBYTE pBytes = NULL; if( m_ppPseudoVideoWnd[ m_nPseudoVideoWnd ]->GetImageInfo( &pBitmapInfo, &pBytes ) ) { if( Capture( pBitmapInfo, pBytes ) ) m_ppPseudoVideoWnd[ m_nPseudoVideoWnd ]->PostMessage( WM_UPDATE_BITMAP ); m_ppPseudoVideoWnd[ m_nPseudoVideoWnd ]->ReleaseImageInfo(); } } for( int i = 0; i < m_nInputPinCount; i++ ) { m_nPseudoVideoWnd = (m_nPseudoVideoWnd + 1) % m_nInputPinCount; if( m_pbPseudoVideoWnd[ m_nPseudoVideoWnd ] && m_ppPseudoVideoWnd[ m_nPseudoVideoWnd ] ) { if( m_nPseudoLastInput != m_pInputPin[ m_nPseudoVideoWnd ] ) { m_nPseudoLastInput = m_pInputPin[ m_nPseudoVideoWnd ]; m_pAMCrossbar->Route( m_nOutputPin, m_pInputPin[ m_nPseudoVideoWnd ] ); TRACE( "-----Route %d <-> %d\n", m_nOutputPin, m_pInputPin[ m_nPseudoVideoWnd ] ); } break; } } } m_nPseudoCaptureLoop++; } __except( 1 ) { } } void CDSVideoProcessor::OnVideoSample() { // return; // NOTE TEST ONLY if( m_hEventVideoSample ) ::SetEvent( m_hEventVideoSample ); } BOOL CDSVideoProcessor::SetVideoStandart( long lVideoStandard ) { if( !m_pAMAnalogVideoDecoder ) return FALSE; return SUCCEEDED( m_pAMAnalogVideoDecoder->put_TVFormat( lVideoStandard ) ); } /* BOOL CDSVideoProcessor::DrawText( LPCTSTR pText, LPBITMAPINFO pBitmapInfo, LPBYTE pBytes, COLORREF cColor ) { if( !pText || !pBitmapInfo || !pBytes ) return FALSE; if( pBitmapInfo->bmiHeader.biBitCount % 8 != 0 ) return FALSE; int nStrPixelSize = _tcslen( pText ) * m_SymbolData.GetWidth(); int nStrShift = ( pBitmapInfo->bmiHeader.biWidth - nStrPixelSize ) >> 1; int nCharRowBegin = ( m_SymbolData.GetHeigth() - 1 ) * m_SymbolData.GetWidth(); PBYTE pPixel08Ptr; PWORD pPixel16Ptr; RGBTRIPLE* pPixel24Ptr; BYTE color08; WORD color16; RGBTRIPLE color24; int x, y; switch( pBitmapInfo->bmiHeader.biBitCount ) { case 8: color08 = (BYTE)cColor; pPixel08Ptr = (PBYTE)pBytes; pPixel08Ptr += nStrShift; for( y = 0; y < m_SymbolData.GetHeigth(); y++ ) { int nChar = 0; int nCharColumn = 0; for( x = 0; x < nStrPixelSize; x++ ) { if( *( m_SymbolData.Get( pText[ nChar ] ) + nCharRowBegin + nCharColumn ) ) pPixel08Ptr[ x ] = color08; if( ++nCharColumn == m_SymbolData.GetWidth() ) { nCharColumn = 0; nChar++; } } pPixel08Ptr += pBitmapInfo->bmiHeader.biWidth; nCharRowBegin -= m_SymbolData.GetWidth(); } break; case 16: color16 = ( ( GetRValue( cColor ) >> 3 ) << 11 ) | ( ( GetGValue( cColor ) >> 2 ) << 5 ) | ( ( GetBValue( cColor ) >> 3 ) << 0 ); pPixel16Ptr = (PWORD)pBytes; pPixel16Ptr += nStrShift; for( y = 0; y < m_SymbolData.GetHeigth(); y++ ) { int nChar = 0; int nCharColumn = 0; for( x = 0; x < nStrPixelSize; x++ ) { if( *( m_SymbolData.Get( pText[ nChar ] ) + nCharRowBegin + nCharColumn ) ) pPixel16Ptr[ x ] = color16; if( ++nCharColumn == m_SymbolData.GetWidth() ) { nCharColumn = 0; nChar++; } } pPixel16Ptr += pBitmapInfo->bmiHeader.biWidth; nCharRowBegin -= m_SymbolData.GetWidth(); } break; case 24: color24.rgbtRed = GetRValue( cColor ); color24.rgbtGreen = GetGValue( cColor ); color24.rgbtBlue = GetBValue( cColor ); pPixel24Ptr = (RGBTRIPLE*)pBytes; pPixel24Ptr += nStrShift; for( y = 0; y < m_SymbolData.GetHeigth(); y++ ) { int nChar = 0; int nCharColumn = 0; for( x = 0; x < nStrPixelSize; x++ ) { if( *( m_SymbolData.Get( pText[ nChar ] ) + nCharRowBegin + nCharColumn ) ) pPixel24Ptr[ x ] = color24; if( ++nCharColumn == m_SymbolData.GetWidth() ) { nCharColumn = 0; nChar++; } } pPixel24Ptr += pBitmapInfo->bmiHeader.biWidth; nCharRowBegin -= m_SymbolData.GetWidth(); } break; } return TRUE; } */