bool CvCaptureAVI_VFW::open( const char* filename ) { close(); icvInitCapture_VFW(); if( !filename ) return false; HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL ); if( SUCCEEDED(hr)) { hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 ); if( SUCCEEDED(hr)) { hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo)); if( SUCCEEDED(hr)) { size.width = aviinfo.rcFrame.right - aviinfo.rcFrame.left; size.height = aviinfo.rcFrame.bottom - aviinfo.rcFrame.top; BITMAPINFOHEADER bmihdr = icvBitmapHeader( size.width, size.height, 24 ); film_range.start_index = (int)aviinfo.dwStart; film_range.end_index = film_range.start_index + (int)aviinfo.dwLength; fps = (double)aviinfo.dwRate/aviinfo.dwScale; pos = film_range.start_index; getframe = AVIStreamGetFrameOpen( avistream, &bmihdr ); if( getframe != 0 ) return true; } } } close(); return false; }
bool CvVideoWriter_VFW::createStreams( CvSize frameSize, bool isColor ) { if( !avifile ) return false; AVISTREAMINFO aviinfo; BITMAPINFO_8Bit bmih; bmih.bmiHeader = icvBitmapHeader( frameSize.width, frameSize.height, isColor ? 24 : 8 ); for( int i = 0; i < 256; i++ ) { bmih.bmiColors[i].rgbBlue = (BYTE)i; bmih.bmiColors[i].rgbGreen = (BYTE)i; bmih.bmiColors[i].rgbRed = (BYTE)i; bmih.bmiColors[i].rgbReserved = 0; } memset( &aviinfo, 0, sizeof(aviinfo)); aviinfo.fccType = streamtypeVIDEO; aviinfo.fccHandler = 0; // use highest possible accuracy for dwRate/dwScale aviinfo.dwScale = (DWORD)((double)0x7FFFFFFF / fps); aviinfo.dwRate = cvRound(fps * aviinfo.dwScale); aviinfo.rcFrame.top = aviinfo.rcFrame.left = 0; aviinfo.rcFrame.right = frameSize.width; aviinfo.rcFrame.bottom = frameSize.height; if( AVIFileCreateStream( avifile, &uncompressed, &aviinfo ) == AVIERR_OK ) { AVICOMPRESSOPTIONS copts, *pcopts = &copts; copts.fccType = streamtypeVIDEO; copts.fccHandler = fourcc != -1 ? fourcc : 0; copts.dwKeyFrameEvery = 1; copts.dwQuality = 10000; copts.dwBytesPerSecond = 0; copts.dwFlags = AVICOMPRESSF_VALID; copts.lpFormat = &bmih; copts.cbFormat = (isColor ? sizeof(BITMAPINFOHEADER) : sizeof(bmih)); copts.lpParms = 0; copts.cbParms = 0; copts.dwInterleaveEvery = 0; if( fourcc != -1 || AVISaveOptions( 0, 0, 1, &uncompressed, &pcopts ) == TRUE ) { if( AVIMakeCompressedStream( &compressed, uncompressed, pcopts, 0 ) == AVIERR_OK && AVIStreamSetFormat( compressed, 0, &bmih, sizeof(bmih)) == AVIERR_OK ) { fps = fps; fourcc = (int)copts.fccHandler; frameSize = frameSize; tempFrame = cvCreateImage( frameSize, 8, (isColor ? 3 : 1) ); return true; } } } return false; }
bool CAviLoader::open( const char* filename ) { close(); icvInitCapture_VFW(); if( !filename ) return false; HRESULT hr = AVIFileOpen( &avifile, filename, OF_READ, NULL ); if( SUCCEEDED(hr)) { hr = AVIFileGetStream( avifile, &avistream, streamtypeVIDEO, 0 ); if( SUCCEEDED(hr)) { hr = AVIStreamInfo( avistream, &aviinfo, sizeof(aviinfo)); if( SUCCEEDED(hr)) { //int fcc = aviinfo.fccHandler; data_offset = 0; size.width = aviinfo.rcFrame.right - aviinfo.rcFrame.left; size.height = aviinfo.rcFrame.bottom - aviinfo.rcFrame.top; BITMAPINFOHEADER bmih = icvBitmapHeader( size.width, size.height, 24 ); if(frame) delete []frame; frame = new unsigned char[size.width*size.height*3]; film_range.start_index = (int)aviinfo.dwStart; film_range.end_index = film_range.start_index + (int)aviinfo.dwLength; fps = (double)aviinfo.dwRate/aviinfo.dwScale; pos = film_range.start_index; getframe = AVIStreamGetFrameOpen( avistream, &bmih ); if( getframe != 0 ) return true; } } } close(); return false; }
IplImage* CvCaptureCAM_VFW::retrieveFrame(int) { BITMAPINFO vfmt; memset( &vfmt, 0, sizeof(vfmt)); BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader; if( !capWnd ) return 0; const DWORD sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt)); const int prevWidth = frame ? frame->width : 0; const int prevHeight = frame ? frame->height : 0; if( !hdr || hdr->lpData == 0 || sz == 0 ) return 0; if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight ) { cvReleaseImage( &frame ); frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 ); } if ( vfmt0.biCompression == MAKEFOURCC('N','V','1','2') ) { // Frame is in YUV 4:2:0 NV12 format, convert to BGR color space // See https://msdn.microsoft.com/en-us/library/windows/desktop/dd206750(v=vs.85).aspx#nv12) IplImage src; cvInitImageHeader( &src, cvSize( vfmt0.biWidth, vfmt0.biHeight * 3 / 2 ), IPL_DEPTH_8U, 1, IPL_ORIGIN_BL, 4 ); cvSetData( &src, hdr->lpData, src.widthStep ); cvCvtColor( &src, frame, CV_YUV2BGR_NV12 ); } else if( vfmt0.biCompression != BI_RGB || vfmt0.biBitCount != 24 ) { BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 ); if( hic == 0 || fourcc != vfmt0.biCompression || prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight ) { closeHIC(); hic = ICOpen( MAKEFOURCC('V','I','D','C'), vfmt0.biCompression, ICMODE_DECOMPRESS ); if( hic ) { if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK ) { closeHIC(); return 0; } } } if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData, &vfmt1, frame->imageData ) != ICERR_OK ) { closeHIC(); return 0; } cvFlip( frame, frame, 0 ); } else { IplImage src; cvInitImageHeader( &src, cvSize(vfmt0.biWidth, vfmt0.biHeight), IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 ); cvSetData( &src, hdr->lpData, src.widthStep ); cvFlip( &src, frame, 0 ); } return frame; }
IplImage* CvCaptureCAM_VFW::retrieveFrame(int) { BITMAPINFO vfmt; memset( &vfmt, 0, sizeof(vfmt)); BITMAPINFOHEADER& vfmt0 = vfmt.bmiHeader; if( !capWnd ) return 0; const DWORD sz = capGetVideoFormat( capWnd, &vfmt, sizeof(vfmt)); const int prevWidth = frame ? frame->width : 0; const int prevHeight = frame ? frame->height : 0; if( !hdr || hdr->lpData == 0 || sz == 0 ) return 0; if( !frame || frame->width != vfmt0.biWidth || frame->height != vfmt0.biHeight ) { cvReleaseImage( &frame ); frame = cvCreateImage( cvSize( vfmt0.biWidth, vfmt0.biHeight ), 8, 3 ); } if( vfmt0.biCompression != BI_RGB || vfmt0.biBitCount != 24 ) { BITMAPINFOHEADER vfmt1 = icvBitmapHeader( vfmt0.biWidth, vfmt0.biHeight, 24 ); if( hic == 0 || fourcc != vfmt0.biCompression || prevWidth != vfmt0.biWidth || prevHeight != vfmt0.biHeight ) { closeHIC(); hic = ICOpen( MAKEFOURCC('V','I','D','C'), vfmt0.biCompression, ICMODE_DECOMPRESS ); if( hic ) { if( ICDecompressBegin( hic, &vfmt0, &vfmt1 ) != ICERR_OK ) { closeHIC(); return 0; } } } if( !hic || ICDecompress( hic, 0, &vfmt0, hdr->lpData, &vfmt1, frame->imageData ) != ICERR_OK ) { closeHIC(); return 0; } cvFlip( frame, frame, 0 ); } else { IplImage src; cvInitImageHeader( &src, cvSize(vfmt0.biWidth, vfmt0.biHeight), IPL_DEPTH_8U, 3, IPL_ORIGIN_BL, 4 ); cvSetData( &src, hdr->lpData, src.widthStep ); cvFlip( &src, frame, 0 ); } return frame; }