HRESULT AddAviFrame(HAVI avi, HBITMAP hbm) { if (avi==NULL) return AVIERR_BADHANDLE; if (hbm==NULL) return AVIERR_BADPARAM; DIBSECTION dibs; int sbm = GetObject(hbm,sizeof(dibs),&dibs); if (sbm!=sizeof(DIBSECTION)) return AVIERR_BADPARAM; TAviUtil *au = (TAviUtil*)avi; if (au->iserr) return AVIERR_ERROR; // if (au->ps==0) // create the stream, if it wasn't there before { AVISTREAMINFO strhdr; ZeroMemory(&strhdr,sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO;// stream type strhdr.fccHandler = 0; strhdr.dwScale = au->period; strhdr.dwRate = 1000; strhdr.dwSuggestedBufferSize = dibs.dsBmih.biSizeImage; SetRect(&strhdr.rcFrame, 0, 0, dibs.dsBmih.biWidth, dibs.dsBmih.biHeight); HRESULT hr=AVIFileCreateStream(au->pfile, &au->ps, &strhdr); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} } // // create an empty compression, if the user hasn't set any if (au->psCompressed==0) { AVICOMPRESSOPTIONS opts; ZeroMemory(&opts,sizeof(opts)); opts.fccHandler=mmioFOURCC('D','I','B',' '); HRESULT hr = AVIMakeCompressedStream(&au->psCompressed, au->ps, &opts, NULL); if (hr != AVIERR_OK) {au->iserr=true; return hr;} hr = AVIStreamSetFormat(au->psCompressed, 0, &dibs.dsBmih, dibs.dsBmih.biSize+dibs.dsBmih.biClrUsed*sizeof(RGBQUAD)); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} } // //Now we can add the frame HRESULT hr = AVIStreamWrite(au->psCompressed, au->nframe, 1, dibs.dsBm.bmBits, dibs.dsBmih.biSizeImage, AVIIF_KEYFRAME, NULL, NULL); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} au->nframe++; return S_OK; }
void CMainFrame::InitAVIWriteOpt() { CString filename; CFileDialog FileDlg(FALSE,_T("avi")); if (FileDlg.DoModal()==IDOK) { filename = FileDlg.GetPathName(); //capGetVideoFormat(m_hWndCap,&m_InInfo,sizeof(m_InInfo)); m_Frame = 0 ; //AVI文件初始化 AVIFileInit() ; bSaveAVI = TRUE; //打开文件 AVIFileOpen(&m_pFile,filename,OF_WRITE | OF_CREATE,NULL); memset(&strhdr, 0, sizeof(strhdr)) ; strhdr.fccType = streamtypeVIDEO; strhdr.fccHandler = 0 ; strhdr.dwScale = 1 ; strhdr.dwRate = 25 ; strhdr.dwSuggestedBufferSize = lpbiIn->bmiHeader.biSizeImage; SetRect(&strhdr.rcFrame, 0, 0, lpbiIn->bmiHeader.biWidth, lpbiIn->bmiHeader.biHeight); ps = NULL; //文件文件流 AVIFileCreateStream(m_pFile,&ps,&strhdr); //开始捕捉 capCaptureSequenceNoFile(m_hWndCap); } }
HRESULT AddAviWav(HAVI avi, const char *src, DWORD flags) { if (avi==NULL) return AVIERR_BADHANDLE; if (flags!=SND_MEMORY && flags!=SND_FILENAME) return AVIERR_BADFLAGS; if (src==0) return AVIERR_BADPARAM; TAviUtil *au = (TAviUtil*)avi; if (au->iserr) return AVIERR_ERROR; // char *buf=0; WavChunk *wav = (WavChunk*)src; if (flags==SND_FILENAME) { HANDLE hf=CreateFile(src,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL); if (hf==INVALID_HANDLE_VALUE) {au->iserr=true; return AVIERR_FILEOPEN;} DWORD size = GetFileSize(hf,NULL); buf = new char[size]; DWORD red; ReadFile(hf,buf,size,&red,NULL); CloseHandle(hf); wav = (WavChunk*)buf; } // // check that format doesn't clash bool badformat=false; if (au->wfx.nChannels==0) { au->wfx.wFormatTag=wav->fmt.wFormatTag; au->wfx.cbSize=0; au->wfx.nAvgBytesPerSec=wav->fmt.dwAvgBytesPerSec; au->wfx.nBlockAlign=wav->fmt.wBlockAlign; au->wfx.nChannels=wav->fmt.wChannels; au->wfx.nSamplesPerSec=wav->fmt.dwSamplesPerSec; au->wfx.wBitsPerSample=wav->fmt.wBitsPerSample; } else { if (au->wfx.wFormatTag!=wav->fmt.wFormatTag) badformat=true; if (au->wfx.nAvgBytesPerSec!=wav->fmt.dwAvgBytesPerSec) badformat=true; if (au->wfx.nBlockAlign!=wav->fmt.wBlockAlign) badformat=true; if (au->wfx.nChannels!=wav->fmt.wChannels) badformat=true; if (au->wfx.nSamplesPerSec!=wav->fmt.dwSamplesPerSec) badformat=true; if (au->wfx.wBitsPerSample!=wav->fmt.wBitsPerSample) badformat=true; } if (badformat) {if (buf!=0) delete[] buf; return AVIERR_BADFORMAT;} // if (au->as==0) // create the stream if necessary { AVISTREAMINFO ahdr; ZeroMemory(&ahdr,sizeof(ahdr)); ahdr.fccType=streamtypeAUDIO; ahdr.dwScale=au->wfx.nBlockAlign; ahdr.dwRate=au->wfx.nSamplesPerSec*au->wfx.nBlockAlign; ahdr.dwSampleSize=au->wfx.nBlockAlign; ahdr.dwQuality=(DWORD)-1; HRESULT hr = AVIFileCreateStream(au->pfile, &au->as, &ahdr); if (hr!=AVIERR_OK) {if (buf!=0) delete[] buf; au->iserr=true; return hr;} hr = AVIStreamSetFormat(au->as,0,&au->wfx,sizeof(WAVEFORMATEX)); if (hr!=AVIERR_OK) {if (buf!=0) delete[] buf; au->iserr=true; return hr;} } // // now we can write the data unsigned long numbytes = wav->dat.size; unsigned long numsamps = numbytes*8 / au->wfx.wBitsPerSample; HRESULT hr = AVIStreamWrite(au->as,au->nsamp,numsamps,wav->dat.data,numbytes,0,NULL,NULL); if (buf!=0) delete[] buf; if (hr!=AVIERR_OK) {au->iserr=true; return hr;} au->nsamp+=numsamps; return S_OK; }
HRESULT AddAviAudio(HAVI avi, void *dat, unsigned long numbytes) { if (avi==NULL) return AVIERR_BADHANDLE; if (dat==NULL || numbytes==0) return AVIERR_BADPARAM; TAviUtil *au = (TAviUtil*)avi; if (au->iserr) return AVIERR_ERROR; if (au->wfx.nChannels==0) return AVIERR_BADFORMAT; unsigned long numsamps = numbytes*8 / au->wfx.wBitsPerSample; if ((numsamps*au->wfx.wBitsPerSample/8)!=numbytes) return AVIERR_BADPARAM; // if (au->as==0) // create the stream if necessary { AVISTREAMINFO ahdr; ZeroMemory(&ahdr,sizeof(ahdr)); ahdr.fccType=streamtypeAUDIO; ahdr.dwScale=au->wfx.nBlockAlign; ahdr.dwRate=au->wfx.nSamplesPerSec*au->wfx.nBlockAlign; ahdr.dwSampleSize=au->wfx.nBlockAlign; ahdr.dwQuality=(DWORD)-1; HRESULT hr = AVIFileCreateStream(au->pfile, &au->as, &ahdr); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} hr = AVIStreamSetFormat(au->as,0,&au->wfx,sizeof(WAVEFORMATEX)); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} } // // now we can write the data HRESULT hr = AVIStreamWrite(au->as,au->nsamp,numsamps,dat,numbytes,0,NULL,NULL); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} au->nsamp+=numsamps; return S_OK; }
void CAVIFile::SetSoundFormat(WAVEFORMATEX *f) { memcpy(&soundFormat, f, sizeof(soundFormat)); memset(&soundhdr, 0, sizeof(soundhdr)); soundhdr.fccType = streamtypeAUDIO;// stream type soundhdr.dwQuality = (DWORD)-1; soundhdr.dwScale = f->nBlockAlign; soundhdr.dwInitialFrames = 1; soundhdr.dwRate = f->nAvgBytesPerSec; soundhdr.dwSampleSize = f->nBlockAlign; HRESULT hr = AVIFileCreateStream(pfile, &psound, &soundhdr); if(hr != AVIERR_OK) { bOK = false; return; } hr = AVIStreamSetFormat(psound, 0, (void *)&soundFormat, sizeof(soundFormat)); if(hr != AVIERR_OK) { bOK = false; return; } soundAdded = true; }
void AVIWrite::SetSoundFormat(WAVEFORMATEX *format) { memcpy(&m_soundFormat, format, sizeof(WAVEFORMATEX)); ZeroMemory(&m_soundHeader, sizeof(AVISTREAMINFO)); // setup the sound stream header m_soundHeader.fccType = streamtypeAUDIO; m_soundHeader.dwQuality = (DWORD)-1; m_soundHeader.dwScale = format->nBlockAlign; m_soundHeader.dwInitialFrames = 1; m_soundHeader.dwRate = format->nAvgBytesPerSec; m_soundHeader.dwSampleSize = format->nBlockAlign; // create the sound stream if(FAILED(AVIFileCreateStream(m_file, &m_streamSound, &m_soundHeader))) { m_failed = true; return; } // setup the sound stream format if(FAILED(AVIStreamSetFormat(m_streamSound, 0 , (void *)&m_soundFormat, sizeof(WAVEFORMATEX)))) { m_failed = true; return; } }
bool CBmpToAvi::Open( LPCTSTR szFile, LPBITMAPINFO lpbmi ) { if (szFile == NULL) return false; m_nFrames = 0; if (AVIFileOpen(&m_pfile, szFile, OF_WRITE | OF_CREATE, NULL)) return false; m_si.fccType = streamtypeVIDEO; m_si.fccHandler = BI_RGB; m_si.dwScale = 1; m_si.dwRate = 5; // 每秒5帧 SetRect(&m_si.rcFrame, 0, 0, lpbmi->bmiHeader.biWidth, lpbmi->bmiHeader.biHeight); m_si.dwSuggestedBufferSize = lpbmi->bmiHeader.biSizeImage; if (AVIFileCreateStream(m_pfile, &m_pavi, &m_si)) return false; if (AVIStreamSetFormat(m_pavi, 0, lpbmi, sizeof(BITMAPINFO)) != AVIERR_OK) return false; return true; }
bool CAVIFile::Open(const char *filename) { HRESULT hr = AVIFileOpen(&pfile, // returned file pointer filename, // file name OF_WRITE | OF_CREATE, // mode to open file with NULL); // use handler determined // from file extension.... if (hr != AVIERR_OK) { bOK = false; return false; } memset(&strhdr, 0, sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO;// stream type strhdr.fccHandler = 0; strhdr.dwScale = 1; strhdr.dwRate = rate; strhdr.dwSuggestedBufferSize = bitmap.biSizeImage; SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream (int) bitmap.biWidth, (int) bitmap.biHeight); // And create the stream; hr = AVIFileCreateStream(pfile, // file pointer &ps, // returned stream pointer &strhdr); // stream header if (hr != AVIERR_OK) { bOK = false; return false; } memset(&opts, 0, sizeof(opts)); if(!AVISaveOptions(hWindow, 0, 1, &ps, aopts)) { bOK = false; return false; } hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL); if (hr != AVIERR_OK) { bOK = false; return false; } hr = AVIStreamSetFormat(psCompressed, 0, &bitmap, // stream format bitmap.biSize + // format size bitmap.biClrUsed * sizeof(RGBQUAD)); if (hr != AVIERR_OK) { bOK = false; return false; } return true; }
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 AVIDump::SetVideoFormat() { memset(&s_header, 0, sizeof(s_header)); s_header.fccType = streamtypeVIDEO; s_header.dwScale = 1; s_header.dwRate = s_frame_rate; s_header.dwSuggestedBufferSize = s_bitmap.biSizeImage; return SUCCEEDED(AVIFileCreateStream(s_file, &s_stream, &s_header)); }
bool AVIDump::SetVideoFormat() { memset(&m_header, 0, sizeof(m_header)); m_header.fccType = streamtypeVIDEO; m_header.dwScale = 1; m_header.dwRate = VideoInterface::TargetRefreshRate; m_header.dwSuggestedBufferSize = m_bitmap.biSizeImage; return SUCCEEDED(AVIFileCreateStream(m_file, &m_stream, &m_header)); }
HRESULT AddAviAudio (HAVI avi, void *dat, unsigned long numbytes) { TAviUtil *au; unsigned long numsamps; HRESULT hr; if (!avi) return AVIERR_BADHANDLE; if ((!dat) || (!numbytes)) return AVIERR_BADPARAM; au = (TAviUtil*)avi; if (au->iserr) return AVIERR_ERROR; if (!au->wfx.nChannels) return AVIERR_BADFORMAT; // make sure it's a whole number of samples numsamps = numbytes * 8 / au->wfx.wBitsPerSample; if ((numsamps * au->wfx.wBitsPerSample / 8) != numbytes) return AVIERR_BADPARAM; if (!au->audStream) // create the stream if necessary { AVISTREAMINFO ahdr; ZeroMemory(&ahdr, sizeof(ahdr)); ahdr.fccType = streamtypeAUDIO; ahdr.dwScale = au->wfx.nBlockAlign; ahdr.dwRate = au->wfx.nSamplesPerSec * au->wfx.nBlockAlign; ahdr.dwSampleSize = au->wfx.nBlockAlign; ahdr.dwQuality = (DWORD)-1; hr = AVIFileCreateStream(au->pfile, &au->audStream, &ahdr); if (hr) { au->iserr = TRUE; return hr; } hr = AVIStreamSetFormat(au->audStream, 0, &au->wfx, sizeof(WAVEFORMATEX)); if (hr) { au->iserr = TRUE; return hr; } } // now we can write the data hr = AVIStreamWrite(au->audStream, au->nsamp, numsamps, dat, numbytes, 0, NULL, NULL); if (hr) { au->iserr = TRUE; return hr; } au->nsamp += numsamps; return S_OK; }
static bool CreateVideoStream(DWORD dwFrameRate, size_t WidthFrame, size_t HeightFrame, PAVIFILE *pOutputFile, PAVISTREAM *ppStreamVideo) { AVISTREAMINFO sStreamInfo; ZeroMemory(&sStreamInfo, sizeof(sStreamInfo)); sStreamInfo.fccType = streamtypeVIDEO; sStreamInfo.fccHandler = 0; sStreamInfo.dwScale = 1; sStreamInfo.dwRate = dwFrameRate; sStreamInfo.dwSuggestedBufferSize = 0; SetRect(&sStreamInfo.rcFrame, 0, 0, WidthFrame, HeightFrame); return (AVIERR_OK==AVIFileCreateStream(*pOutputFile, ppStreamVideo, &sStreamInfo)); }
bool AVIWrite::Open(const char *filename) { // create the AVI file if(FAILED(AVIFileOpen(&m_file, filename, OF_WRITE | OF_CREATE, NULL))) { m_failed = true; return false; } // setup the video stream information ZeroMemory(&m_header, sizeof(AVISTREAMINFO)); m_header.fccType = streamtypeVIDEO; m_header.dwScale = 1; m_header.dwRate = m_fps; m_header.dwSuggestedBufferSize = m_bitmap.biSizeImage; // create the video stream if(FAILED(AVIFileCreateStream(m_file, &m_stream, &m_header))) { m_failed = true; return false; } ZeroMemory(&m_options, sizeof(AVICOMPRESSOPTIONS)); m_arrayOptions[0] = &m_options; // call the dialog to setup the compress options to be used if(!AVISaveOptions(AfxGetApp()->m_pMainWnd->GetSafeHwnd(), 0, 1, &m_stream, m_arrayOptions)) { m_failed = true; return false; } // create the compressed stream if(FAILED(AVIMakeCompressedStream(&m_streamCompressed, m_stream, &m_options, NULL))) { m_failed = true; return false; } // setup the video stream format if(FAILED( AVIStreamSetFormat(m_streamCompressed, 0, &m_bitmap, m_bitmap.biSize + m_bitmap.biClrUsed * sizeof(RGBQUAD)))) { m_failed = true; return false; } return true; }
HRESULT CAviFile::AppendAudioData(WAVEFORMATEX *wfx, void *dat, unsigned long numbytes) { if (dat == NULL || numbytes == 0) { return AVIERR_BADPARAM; } if (wfx->nChannels == 0) { return AVIERR_BADFORMAT; } unsigned long numsamps = numbytes * 8 / wfx->wBitsPerSample; if ((numsamps * wfx->wBitsPerSample / 8) != numbytes) { return AVIERR_BADPARAM; } if (m_pAviAudioStream == NULL) // create the stream if necessary { if (m_pAviFile == NULL) { return S_OK; } AVISTREAMINFO ahdr; ZeroMemory(&ahdr, sizeof(ahdr)); ahdr.fccType = streamtypeAUDIO; ahdr.dwScale = wfx->nBlockAlign; ahdr.dwRate = wfx->nSamplesPerSec * wfx->nBlockAlign; ahdr.dwSampleSize = wfx->nBlockAlign; ahdr.dwQuality = (DWORD)-1; HRESULT hr = AVIFileCreateStream(m_pAviFile, &m_pAviAudioStream, &ahdr); if (hr != AVIERR_OK) { SetErrorMessage(_T("Unable to create audio stream")); return E_FAIL; } hr = AVIStreamSetFormat(m_pAviAudioStream, m_lVSample, wfx, sizeof(WAVEFORMATEX) + wfx->cbSize); if (hr != AVIERR_OK) { SetErrorMessage(_T("Unable to set audio stream format")); return E_FAIL; } } HRESULT hr = AVIStreamWrite(m_pAviAudioStream, m_lASample, numsamps, dat, numbytes, 0, NULL, NULL); if (hr != AVIERR_OK) { SetErrorMessage(_T("Unable to Write Audio Stream to the output Movie File")); return E_FAIL; } m_lASample += numsamps; return S_OK; }
avi_str *avi_init(char *out, void *bih, LONG bihl) { avi_str *r = malloc(sizeof(avi_str)); AVIFileInit(); AVIFileOpenA(&r->fp, out, OF_CREATE | OF_WRITE, NULL); AVISTREAMINFO si; ZeroMemory(&si, sizeof(AVISTREAMINFO)); si.fccType = streamtypeVIDEO; si.fccHandler = mmioFOURCC('U', 'L', 'R', 'G'); si.dwScale = 1; si.dwRate = GLC_FPS; si.dwQuality = -1; SetRect(&si.rcFrame, 0, 0, GLC_MAX_WIDTH, GLC_MAX_HEIGHT); AVIFileCreateStream(r->fp, &r->sp, &si); AVIStreamSetFormat(r->sp, 0, bih, bihl); return r; }
HRESULT SetAviVideoCompression(HAVI avi, HBITMAP hbm, AVICOMPRESSOPTIONS *opts, bool ShowDialog, HWND hparent) { if (avi==NULL) return AVIERR_BADHANDLE; if (hbm==NULL) return AVIERR_BADPARAM; DIBSECTION dibs; int sbm = GetObject(hbm,sizeof(dibs),&dibs); if (sbm!=sizeof(DIBSECTION)) return AVIERR_BADPARAM; TAviUtil *au = (TAviUtil*)avi; if (au->iserr) return AVIERR_ERROR; if (au->psCompressed!=0) return AVIERR_COMPRESSOR; // if (au->ps==0) // create the stream, if it wasn't there before { AVISTREAMINFO strhdr; ZeroMemory(&strhdr,sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO;// stream type strhdr.fccHandler = 0; strhdr.dwScale = au->period; strhdr.dwRate = 1000; strhdr.dwSuggestedBufferSize = dibs.dsBmih.biSizeImage; SetRect(&strhdr.rcFrame, 0, 0, dibs.dsBmih.biWidth, dibs.dsBmih.biHeight); HRESULT hr=AVIFileCreateStream(au->pfile, &au->ps, &strhdr); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} } // if (au->psCompressed==0) // set the compression, prompting dialog if necessary { AVICOMPRESSOPTIONS myopts; ZeroMemory(&myopts,sizeof(myopts)); AVICOMPRESSOPTIONS *aopts[1]; if (opts!=NULL) aopts[0]=opts; else aopts[0]=&myopts; if (ShowDialog) { BOOL res = (BOOL)AVISaveOptions(hparent,0,1,&au->ps,aopts); if (!res) {AVISaveOptionsFree(1,aopts); au->iserr=true; return AVIERR_USERABORT;} } HRESULT hr = AVIMakeCompressedStream(&au->psCompressed, au->ps, aopts[0], NULL); AVISaveOptionsFree(1,aopts); if (hr != AVIERR_OK) {au->iserr=true; return hr;} DIBSECTION dibs; GetObject(hbm,sizeof(dibs),&dibs); hr = AVIStreamSetFormat(au->psCompressed, 0, &dibs.dsBmih, dibs.dsBmih.biSize+dibs.dsBmih.biClrUsed*sizeof(RGBQUAD)); if (hr!=AVIERR_OK) {au->iserr=true; return hr;} } // return AVIERR_OK; }
bool CAVIFile::AddFrame(CBitmap& bmp) { HRESULT hr; // char szMessage[BUFSIZE]; if (!bOK) return false; LPBITMAPINFOHEADER alpbi = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 8)); if (alpbi == NULL) return false; if (xDim>=0 && xDim != alpbi->biWidth) { GlobalFreePtr(alpbi); return false; } if (yDim>=0 && yDim != alpbi->biHeight) { GlobalFreePtr(alpbi); return false; } xDim = alpbi->biWidth; yDim = alpbi->biHeight; if (nFrames == 0) { hr = AVIFileOpen(&pfile, // returned file pointer FName, // file name OF_WRITE | OF_CREATE, // mode to open file with NULL); // use handler determined // from file extension.... if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } _fmemset(&strhdr, 0, sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO;// stream type strhdr.fccHandler = 0; strhdr.dwScale = 1; strhdr.dwRate = 15; // 15 fps strhdr.dwSuggestedBufferSize = alpbi->biSizeImage; SetRect(&strhdr.rcFrame, 0, 0, // rectangle for stream (int) alpbi->biWidth, (int) alpbi->biHeight); // And create the stream; hr = AVIFileCreateStream(pfile, // file pointer &ps, // returned stream pointer &strhdr); // stream header if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } _fmemset(&opts, 0, sizeof(opts)); if (!AVISaveOptions(NULL, 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *) &aopts)) { GlobalFreePtr(alpbi); bOK = false; return false; } hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL); if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } hr = AVIStreamSetFormat(psCompressed, 0, alpbi, // stream format alpbi->biSize + // format size alpbi->biClrUsed * sizeof(RGBQUAD)); if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } // Fill in the stream header for the text stream.... // The text stream is in 60ths of a second.... /* _fmemset(&strhdr, 0, sizeof(strhdr)); strhdr.fccType = streamtypeTEXT; strhdr.fccHandler = mmioFOURCC('D', 'R', 'A', 'W'); strhdr.dwScale = 1; strhdr.dwRate = 60; strhdr.dwSuggestedBufferSize = sizeof(szText); SetRect(&strhdr.rcFrame, 0, (int) alpbi->biHeight, (int) alpbi->biWidth, (int) alpbi->biHeight + TEXT_HEIGHT); // ....and create the stream. hr = AVIFileCreateStream(pfile, &psText, &strhdr); if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } dwTextFormat = sizeof(dwTextFormat); hr = AVIStreamSetFormat(psText, 0, &dwTextFormat, sizeof(dwTextFormat)); if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } */ } // Jetzt eigentliches Schreiben hr = AVIStreamWrite(psCompressed, // stream pointer nFrames * 10, // time of this frame 1, // number to write (LPBYTE) alpbi + // pointer to data alpbi->biSize + alpbi->biClrUsed * sizeof(RGBQUAD), alpbi->biSizeImage, // size of this frame AVIIF_KEYFRAME, // flags.... NULL, NULL); if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } // Make some text to put in the file ... //LoadString(hInstance, IDS_TEXTFORMAT, szMessage, BUFSIZE ); /* strcpy(szMessage, "This is frame #%d"); int iLen = wsprintf(szText, szMessage, (int)(nFrames + 1)); // ... and write it as well. hr = AVIStreamWrite(psText, nFrames * 40, 1, szText, iLen + 1, AVIIF_KEYFRAME, NULL, NULL); if (hr != AVIERR_OK) { GlobalFreePtr(alpbi); bOK = false; return false; } */ GlobalFreePtr(alpbi); nFrames++; return true; }
HRESULT CAviFile::AppendFrameFirstTime(int nWidth, int nHeight, LPVOID pBits,int nBitsPerPixel) { int nMaxWidth=GetSystemMetrics(SM_CXSCREEN),nMaxHeight=GetSystemMetrics(SM_CYSCREEN); BITMAPINFO bmpInfo; m_hAviDC=CreateCompatibleDC(NULL); if(m_hAviDC==NULL) { //MessageBox(NULL,"Unable to Create Compatible DC","Error",MB_OK|MB_ICONERROR); goto TerminateInitBits; } ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biPlanes=1; bmpInfo.bmiHeader.biWidth=nWidth; bmpInfo.bmiHeader.biHeight=nHeight; bmpInfo.bmiHeader.biCompression=BI_RGB; bmpInfo.bmiHeader.biBitCount=nBitsPerPixel; bmpInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER); bmpInfo.bmiHeader.biSizeImage=bmpInfo.bmiHeader.biWidth*bmpInfo.bmiHeader.biHeight*bmpInfo.bmiHeader.biBitCount/8; if(bmpInfo.bmiHeader.biHeight>nMaxHeight) nMaxHeight=bmpInfo.bmiHeader.biHeight; if(bmpInfo.bmiHeader.biWidth>nMaxWidth) nMaxWidth=bmpInfo.bmiHeader.biWidth; m_hHeap=HeapCreate(HEAP_NO_SERIALIZE,nMaxWidth*nMaxHeight*4,0); if(m_hHeap==NULL) { //MessageBox(NULL,"Unable to Allocate Memory","Error",MB_OK); goto TerminateInitBits; } m_lpBits=HeapAlloc(m_hHeap,HEAP_ZERO_MEMORY|HEAP_NO_SERIALIZE,nMaxWidth*nMaxHeight*4); if(m_lpBits==NULL) { //MessageBox(NULL,"Unable to Allocate Memory","Error",MB_OK); goto TerminateInitBits; } if(FAILED(AVIFileOpen(&m_pAviFile,m_szFileName,OF_CREATE|OF_WRITE,NULL))) { //MessageBox(NULL,"Unable to Create the Movie File","Error",MB_OK|MB_ICONERROR); goto TerminateInitBits; } ZeroMemory(&m_AviStreamInfo,sizeof(AVISTREAMINFO)); m_AviStreamInfo.fccType=streamtypeVIDEO; m_AviStreamInfo.fccHandler=VIDEOCODEC; m_AviStreamInfo.dwScale=1; m_AviStreamInfo.dwRate= FPS; //Frames Per Second; m_AviStreamInfo.dwQuality=-1; //Default Quality m_AviStreamInfo.dwSuggestedBufferSize=nMaxWidth*nMaxHeight*4; SetRect(&m_AviStreamInfo.rcFrame,0,0,bmpInfo.bmiHeader.biWidth,bmpInfo.bmiHeader.biHeight); strcpy(m_AviStreamInfo.szName,"Video Stream"); if(FAILED(AVIFileCreateStream(m_pAviFile,&m_pAviStream,&m_AviStreamInfo))) { //MessageBox(NULL,"Unable to Create Stream","Error",MB_OK|MB_ICONERROR); goto TerminateInitBits; } ZeroMemory(&m_AviCompressOptions,sizeof(AVICOMPRESSOPTIONS)); m_AviCompressOptions.fccType=streamtypeVIDEO; m_AviCompressOptions.fccHandler=m_AviStreamInfo.fccHandler; m_AviCompressOptions.dwFlags=AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;//|AVICOMPRESSF_DATARATE; m_AviCompressOptions.dwKeyFrameEvery=15; //m_AviCompressOptions.dwBytesPerSecond=1000/8; //m_AviCompressOptions.dwQuality=100; if(FAILED(AVIMakeCompressedStream(&m_pAviCompressedStream,m_pAviStream,&m_AviCompressOptions,NULL))) { //MessageBox(NULL,"Unable to Create Compressed Stream","Error",MB_OK); goto TerminateInitBits; } if(FAILED(AVIStreamSetFormat(m_pAviCompressedStream,0,(LPVOID)&bmpInfo,bmpInfo.bmiHeader.biSize))) { //MessageBox(NULL,"Unable to Set Format","Error",MB_OK); goto TerminateInitBits; } nAppendFuncSelector=2; //Point to UsualAppend Function return AppendFrameUsual(nWidth,nHeight,pBits,nBitsPerPixel); TerminateInitBits: ReleaseMemory(); MessageBox(NULL,"Error Occured While Rendering the Movie","Error",MB_OK|MB_ICONERROR); return E_FAIL; }
bool MovieMaker::Init(const char *newFileName, uint newWidth, uint newHeight, uint frameRate, const vector<string> &codecs) { //Test if init if(initSuccess) { LOGERR(("Init - Already init")); return false; } // Check VFW version. WORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010A) { LOGERR(("Init - VFW version 0x%x is too old.\n",wVer)); return false; } //Open the AVI system AVIFileInit(); aviSysOpen = true; //Open the file HRESULT result = AVIFileOpen(&pfile, // Returned file pointer newFileName, // File name OF_WRITE | OF_CREATE,// Mode to open file with NULL); // Use handler determined if(result != AVIERR_OK) { LOGERR(("Init - Error creating file %s",newFileName)); return false; } //Create the avi info structure AVISTREAMINFO aviInfo; memset(&aviInfo, 0, sizeof(aviInfo)); aviInfo.fccType = streamtypeVIDEO; aviInfo.fccHandler = 0; aviInfo.dwQuality = -1; aviInfo.dwLength = 0; aviInfo.dwScale = 1; aviInfo.dwRate = frameRate; aviInfo.dwSuggestedBufferSize = newWidth * newHeight * 4; SetRect(&aviInfo.rcFrame, 0, 0, (int) newWidth, (int) newHeight); // And create the stream; result = AVIFileCreateStream(pfile, // File pointer &ps, // Returned stream pointer &aviInfo); // Stream header if (result != AVIERR_OK) { LOGERR(("Init - Error creating stream for file %s",newFileName)); return false; } //Loop for all codecs bool codecSuccess = false; for(uint i=0; i<codecs.size(); i++) { //Attempt to create the codec and stop on the first successful codec if(CreateCompressedStream(codecs[i], frameRate)) { codecSuccess = true; break; } } if(!codecSuccess) { LOGERR(("Init - No valid compression codecs for %s",newFileName)); return false; } //Initialize bitmapInfo BITMAPINFOHEADER bitmapInfo; memset(&bitmapInfo,0, sizeof(BITMAPINFOHEADER)); //Fill the bitmap info structure. bitmapInfo.biSize=sizeof(BITMAPINFOHEADER); bitmapInfo.biWidth =newWidth; bitmapInfo.biHeight=newHeight; bitmapInfo.biPlanes=1; bitmapInfo.biBitCount=32; bitmapInfo.biSizeImage=newWidth*newHeight*4; bitmapInfo.biCompression=BI_RGB; //BI_RGB means BGR in reality //Set the format of a stream at the specified position result = AVIStreamSetFormat(psCompressed, 0, // position &bitmapInfo, // stream format bitmapInfo.biSize + // format size bitmapInfo.biClrUsed * sizeof(RGBQUAD)); if (result != AVIERR_OK) { LOGERR(("Init - AVI Compressed Stream format setting failed.")); return false; } //Assign init success width = newWidth; height = newHeight; initSuccess = true; return true; }
void * avi_begin_encode(const char *filename, int width, int height, int fps, const char *preferences_filename) { #ifdef HAVE_VFW avi_encode_context *context; HRESULT hr; BOOL ret; AVISTREAMINFO strhdr; BITMAPINFO bi; AVICOMPRESSOPTIONS opts; AVICOMPRESSOPTIONS * aopts[1]; int rowsize; int imagesize; int numbits; int prefsReadFromFile; if ( (width % 4 != 0) || (height % 4 != 0) ) return NULL; /* width and height must be divisible by 4 (several codecs crashes if this is not true) */ context = (avi_encode_context *) malloc(sizeof(avi_encode_context)); avi_init_context(context); context->width = width; context->height = height; AVIFileInit(); /* Open file */ hr = AVIFileOpen(&context->pfile , filename, OF_WRITE | OF_CREATE, NULL); if (hr != AVIERR_OK) { avi_cleanup_context(context); free(context); return NULL; } /* fixme 20020304 thammer: Investigate what happens if the file allready exists. Preliminary tests indicate that the new stream is just added to the existing file (increasing the file size), instead of truncating the file first, as the documentation for AVIFileOpen states. */ numbits = 24; rowsize = (width * numbits + 31) / 32 * 4; /* aligned to 32 bits */ imagesize = rowsize * height; memset(&strhdr, 0, sizeof(strhdr)); strhdr.fccType = streamtypeVIDEO; strhdr.fccHandler = 0; strhdr.dwScale = 1; strhdr.dwRate = fps; strhdr.dwSuggestedBufferSize = imagesize; strhdr.rcFrame.left = 0; strhdr.rcFrame.top = 0; strhdr.rcFrame.right = width; strhdr.rcFrame.bottom = height; /* Create stream */ hr = AVIFileCreateStream(context->pfile, &context->ps, &strhdr); if (hr != AVIERR_OK) { avi_cleanup_context(context); free(context); return NULL; } aopts[0] = &opts; memset(&opts, 0, sizeof(opts)); prefsReadFromFile = 0; if ( (preferences_filename != NULL) && (strlen(preferences_filename)>0) ) { FILE *file; int size; file = fopen(preferences_filename, "rb"); if (file==NULL) { /* file doesn't exist, must pop up GUI to get options */ ret = AVISaveOptions(NULL, ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE, 1, &context->ps, (LPAVICOMPRESSOPTIONS *) &aopts); if (!ret) { /* User pressed [Cancel] */ avi_cleanup_context(context); free(context); return NULL; } /* Save options to file*/ file = fopen(preferences_filename, "wb"); if (file == NULL) { avi_cleanup_context(context); free(context); return NULL; } /* write AVICOMPRESSOPTIONS struct */ size = fwrite(&opts, sizeof(AVICOMPRESSOPTIONS), 1, file); /* write AVICOMPRESSOPTIONS.cbFormat */ size = fwrite(&opts.cbFormat, 4, 1, file); /* write AVICOMPRESSOPTIONS.lpFormat */ size = fwrite(opts.lpFormat, opts.cbFormat, 1, file); /* write AVICOMPRESSOPTIONS.cbParms */ size = fwrite(&opts.cbParms, 4, 1, file); /* write AVICOMPRESSOPTIONS.lpParms */ size = fwrite(opts.lpParms, opts.cbParms, 1, file); fclose(file); } else { /* Read options from file */ file = fopen(preferences_filename, "rb"); if (file == NULL) { avi_cleanup_context(context); free(context); return NULL; } /* read AVICOMPRESSOPTIONS struct */ size = fread(&opts, sizeof(AVICOMPRESSOPTIONS), 1, file); /* read AVICOMPRESSOPTIONS.cbFormat */ size = fread(&opts.cbFormat, 4, 1, file); /* read AVICOMPRESSOPTIONS.lpFormat */ opts.lpFormat = (void *) malloc(opts.cbFormat); size = fread(opts.lpFormat, opts.cbFormat, 1, file); /* read AVICOMPRESSOPTIONS.cbParms */ size = fread(&opts.cbParms, 4, 1, file); /* read AVICOMPRESSOPTIONS.lpParms */ opts.lpParms = (void *) malloc(opts.cbParms); size = fread(opts.lpParms, opts.cbParms, 1, file); fclose(file); prefsReadFromFile = 1; } } else { ret = AVISaveOptions(NULL, ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_DATARATE, 1, &context->ps, (LPAVICOMPRESSOPTIONS *) &aopts); if (!ret) { /* User pressed [Cancel] */ avi_cleanup_context(context); free(context); return NULL; } }; hr = AVIMakeCompressedStream( &context->pscomp, context->ps, &opts, NULL); if (hr != AVIERR_OK) { avi_cleanup_context(context); free(context); return NULL; } if (prefsReadFromFile) { /* Since we don't know if our method of allocating memory (malloc) differs from whatever AVISaveOptions() uses, we free what we created ourselves. */ free(opts.lpFormat); opts.lpFormat = NULL; free(opts.lpParms); opts.lpParms = NULL; } else AVISaveOptionsFree(1, (LPAVICOMPRESSOPTIONS *) &aopts); memset(&bi, 0, sizeof(BITMAPINFO)); bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER) ; bi.bmiHeader.biWidth = width ; bi.bmiHeader.biHeight = height ; bi.bmiHeader.biPlanes = 1 ; bi.bmiHeader.biBitCount = numbits ; bi.bmiHeader.biCompression = BI_RGB ; bi.bmiHeader.biSizeImage = imagesize ; bi.bmiHeader.biXPelsPerMeter = 0 ; bi.bmiHeader.biYPelsPerMeter = 0 ; bi.bmiHeader.biClrUsed = (numbits <= 8) ? 1 << numbits : 0; bi.bmiHeader.biClrImportant = 0 ; hr = AVIStreamSetFormat(context->pscomp, 0, &bi, bi.bmiHeader.biSize + bi.bmiHeader.biClrUsed * sizeof(RGBQUAD)); if (hr != AVIERR_OK) { avi_cleanup_context(context); free(context); return NULL; } return (void *)context; #else /* HAVE_VFW */ return NULL; #endif /* HAVE_VFW*/ }
void VideoHelper::OpenVideo(CString strFilePath, FrameData& data) { AVIFileInit(); LONG hr; hr = AVIStreamOpenFromFile(&m_pAviStream, strFilePath, streamtypeVIDEO, 0, OF_READ, NULL); if (hr != 0){ // Handle failure. AfxMessageBox(L"Failed to open file."); } else { PAVIFILE pf; PAVISTREAM psSmall; HRESULT hr; AVISTREAMINFO strhdr; BITMAPINFOHEADER bi; BITMAPINFOHEADER biNew; LONG lStreamSize; LPVOID lpOld; LPVOID lpNew; // Determine the size of the format data using // AVIStreamFormatSize. AVIStreamFormatSize(m_pAviStream, 0, &lStreamSize); if (lStreamSize > sizeof(bi)) // Format too large? return; lStreamSize = sizeof(bi); hr = AVIStreamReadFormat(m_pAviStream, 0, &bi, &lStreamSize); // Read format if (bi.biCompression != BI_RGB) // Wrong compression format? return; hr = AVIStreamInfo(m_pAviStream, &strhdr, sizeof(strhdr)); // Create new AVI file using AVIFileOpen. hr = AVIFileOpen(&pf, strFilePath + L".Processed.avi", OF_WRITE | OF_CREATE, NULL); if (hr != 0) return; // Set parameters for the new stream. biNew = bi; SetRect(&strhdr.rcFrame, 0, 0, (int) biNew.biWidth, (int) biNew.biHeight); // Create a stream using AVIFileCreateStream. hr = AVIFileCreateStream(pf, &psSmall, &strhdr); if (hr != 0) { //Stream created OK? If not, close file. AVIFileRelease(pf); return; } // Set format of new stream using AVIStreamSetFormat. hr = AVIStreamSetFormat(psSmall, 0, &biNew, sizeof(biNew)); if (hr != 0) { AVIStreamRelease(psSmall); AVIFileRelease(pf); return; } // Allocate memory for the bitmaps. lpOld = malloc(bi.biSizeImage); // Read the stream data using AVIStreamRead. for (lStreamSize = AVIStreamStart(m_pAviStream); lStreamSize < AVIStreamEnd(m_pAviStream)/*1500*/; lStreamSize++) { //Context::Oversubscribe(true); hr = AVIStreamRead(m_pAviStream, lStreamSize, 1, lpOld, bi.biSizeImage, NULL, NULL); //Context::Oversubscribe(false); //memcpy_s(lpNew, bi.biSizeImage, lpOld, bi.biSizeImage); data.m_BBP = bi.biBitCount; data.m_ColorPlanes = bi.biPlanes; data.m_EndHeight = bi.biHeight; data.m_EndWidth = bi.biWidth; data.m_pFrame = (BYTE*)lpOld; data.m_Pitch = bi.biWidth * (bi.biBitCount / 8); data.m_Size = bi.biSizeImage; data.m_StartHeight = 0; data.m_StartWidth = 0; lpNew = m_pVideoAgent->ProcessFrame(data); if(NULL != lpNew) { // Save the compressed data using AVIStreamWrite. hr = AVIStreamWrite(psSmall, lStreamSize, 1, lpNew, biNew.biSizeImage, AVIIF_KEYFRAME, NULL, NULL); } } free(lpOld); // Close the stream and file. AVIStreamRelease(psSmall); AVIFileRelease(pf); } AVIFileExit(); }
static int avi_open(const char* filename, const BITMAPINFOHEADER* pbmih, const WAVEFORMATEX* pwfex) { int error = 1; int result = 0; do { // close existing first DRV_AviEnd(); if(!truncate_existing(filename)) break; if(!pbmih) break; // create the object avi_create(&avi_file); // set video size and framerate /*avi_file->start_scanline = vsi->start_scanline; avi_file->end_scanline = vsi->end_scanline; avi_file->fps = vsi->fps; avi_file->fps_scale = 16777216-1; avi_file->convert_buffer = new u8[256*384*3];*/ // open the file if(FAILED(AVIFileOpen(&avi_file->avi_file, filename, OF_CREATE | OF_WRITE, NULL))) break; // create the video stream set_video_format(pbmih, avi_file); memset(&avi_file->avi_video_header, 0, sizeof(AVISTREAMINFO)); avi_file->avi_video_header.fccType = streamtypeVIDEO; avi_file->avi_video_header.dwScale = 6*355*263; avi_file->avi_video_header.dwRate = 33513982; avi_file->avi_video_header.dwSuggestedBufferSize = avi_file->bitmap_format.biSizeImage; if(FAILED(AVIFileCreateStream(avi_file->avi_file, &avi_file->streams[VIDEO_STREAM], &avi_file->avi_video_header))) break; if(use_prev_options) { avi_file->compress_options[VIDEO_STREAM] = saved_avi_info.compress_options[VIDEO_STREAM]; avi_file->compress_options_ptr[VIDEO_STREAM] = &avi_file->compress_options[0]; } else { // get compression options memset(&avi_file->compress_options[VIDEO_STREAM], 0, sizeof(AVICOMPRESSOPTIONS)); avi_file->compress_options_ptr[VIDEO_STREAM] = &avi_file->compress_options[0]; //retryAviSaveOptions: //mbg merge 7/17/06 removed error = 0; if(!AVISaveOptions(MainWindow->getHWnd(), 0, 1, &avi_file->streams[VIDEO_STREAM], &avi_file->compress_options_ptr[VIDEO_STREAM])) break; error = 1; } // create compressed stream if(FAILED(AVIMakeCompressedStream(&avi_file->compressed_streams[VIDEO_STREAM], avi_file->streams[VIDEO_STREAM], &avi_file->compress_options[VIDEO_STREAM], NULL))) break; // set the stream format if(FAILED(AVIStreamSetFormat(avi_file->compressed_streams[VIDEO_STREAM], 0, (void*)&avi_file->bitmap_format, avi_file->bitmap_format.biSize))) break; // add sound (if requested) if(pwfex) { // add audio format set_sound_format(pwfex, avi_file); // create the audio stream memset(&avi_file->avi_sound_header, 0, sizeof(AVISTREAMINFO)); avi_file->avi_sound_header.fccType = streamtypeAUDIO; avi_file->avi_sound_header.dwQuality = (DWORD)-1; avi_file->avi_sound_header.dwScale = avi_file->wave_format.nBlockAlign; avi_file->avi_sound_header.dwRate = avi_file->wave_format.nAvgBytesPerSec; avi_file->avi_sound_header.dwSampleSize = avi_file->wave_format.nBlockAlign; avi_file->avi_sound_header.dwInitialFrames = 1; if(FAILED(AVIFileCreateStream(avi_file->avi_file, &avi_file->streams[AUDIO_STREAM], &avi_file->avi_sound_header))) break; // AVISaveOptions doesn't seem to work for audio streams // so here we just copy the pointer for the compressed stream avi_file->compressed_streams[AUDIO_STREAM] = avi_file->streams[AUDIO_STREAM]; // set the stream format if(FAILED(AVIStreamSetFormat(avi_file->compressed_streams[AUDIO_STREAM], 0, (void*)&avi_file->wave_format, sizeof(WAVEFORMATEX)))) break; } // initialize counters avi_file->video_frames = 0; avi_file->sound_samples = 0; avi_file->tBytes = 0; avi_file->ByteBuffer = 0; avi_file->audio_buffer_pos = 0; // success error = 0; result = 1; avi_file->valid = 1; } while(0); if(!result) { avi_destroy(&avi_file); if(error) EMU_PrintError("Error writing AVI file"); } return result; }
HRESULT CAviFile::InitMovieCreation(int nFrameWidth, int nFrameHeight, int nBitsPerPixel) { int nMaxWidth=GetSystemMetrics(SM_CXSCREEN),nMaxHeight=GetSystemMetrics(SM_CYSCREEN); m_hAviDC = CreateCompatibleDC(NULL); if(m_hAviDC==NULL) { SetErrorMessage("Unable to Create Compatible DC"); m_LastError = E_FAIL; return E_FAIL; } if(nFrameWidth > nMaxWidth) nMaxWidth= nFrameWidth; if(nFrameHeight > nMaxHeight) nMaxHeight = nFrameHeight; m_hHeap=HeapCreate(HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4, 0); if(m_hHeap==NULL) { SetErrorMessage("Unable to Create Heap"); m_LastError = E_FAIL; return E_FAIL; } m_lpBits=HeapAlloc(m_hHeap, HEAP_ZERO_MEMORY|HEAP_NO_SERIALIZE, nMaxWidth*nMaxHeight*4); if(m_lpBits==NULL) { SetErrorMessage("Unable to Allocate Memory on Heap"); m_LastError = E_FAIL; return E_FAIL; } // Make sure that the file doesn't exist before acting on it. _unlink( m_szFileName ); DWORD Result = AVIFileOpen( &m_pAviFile, m_szFileName, OF_CREATE|OF_WRITE, NULL ); if( FAILED( Result ) ) { SetErrorMessage("Unable to Create the Movie File"); m_LastError = E_FAIL; return E_FAIL; } ZeroMemory(&m_AviStreamInfo,sizeof(AVISTREAMINFO)); m_AviStreamInfo.fccType = streamtypeVIDEO; m_AviStreamInfo.fccHandler = m_dwFCCHandler; m_AviStreamInfo.dwScale = 1; m_AviStreamInfo.dwRate = m_dwFrameRate; // Frames Per Second; m_AviStreamInfo.dwQuality = -1; // Default Quality m_AviStreamInfo.dwSuggestedBufferSize = nMaxWidth*nMaxHeight*4; SetRect(&m_AviStreamInfo.rcFrame, 0, 0, nFrameWidth, nFrameHeight); strcpy_s( m_AviStreamInfo.szName, sizeof( m_AviStreamInfo.szName ), _T("Video Stream")); if(FAILED(AVIFileCreateStream(m_pAviFile,&m_pAviStream,&m_AviStreamInfo))) { SetErrorMessage("Unable to Create Video Stream in the Movie File"); m_LastError = E_FAIL; return E_FAIL; } ZeroMemory(&m_AviCompressOptions,sizeof(AVICOMPRESSOPTIONS)); m_AviCompressOptions.fccType=streamtypeVIDEO; m_AviCompressOptions.fccHandler=m_AviStreamInfo.fccHandler; m_AviCompressOptions.dwFlags=AVICOMPRESSF_VALID; // m_AviCompressOptions.dwKeyFrameEvery=100; // m_AviCompressOptions.dwBytesPerSecond=1000; this is ignored for xvid recording // m_AviCompressOptions.dwQuality=500; this is ignored for xvid recording HRESULT hResult = AVIMakeCompressedStream( &m_pAviCompressedStream, m_pAviStream, &m_AviCompressOptions, NULL); if(FAILED(hResult)) { // One reason this error might occur is if you are using a Codec that is not // available on your system. Check the mmioFOURCC() code you are using and make // sure you have that codec installed properly on your machine. SetErrorMessage("Unable to Create Compressed Stream: Check your CODEC options"); m_LastError = E_FAIL; return E_FAIL; } BITMAPINFO bmpInfo; ZeroMemory(&bmpInfo,sizeof(BITMAPINFO)); bmpInfo.bmiHeader.biPlanes = 1; bmpInfo.bmiHeader.biWidth = nFrameWidth; bmpInfo.bmiHeader.biHeight = nFrameHeight; bmpInfo.bmiHeader.biCompression = BI_RGB; bmpInfo.bmiHeader.biBitCount = nBitsPerPixel; bmpInfo.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); int LineByteWidth = ( ( bmpInfo.bmiHeader.biWidth * 3 ) + 3 ) & ~3; bmpInfo.bmiHeader.biSizeImage = LineByteWidth*bmpInfo.bmiHeader.biHeight; if(FAILED(AVIStreamSetFormat(m_pAviCompressedStream,0,(LPVOID)&bmpInfo, bmpInfo.bmiHeader.biSize))) { // One reason this error might occur is if your bitmap does not meet the Codec requirements. // For example, // your bitmap is 32bpp while the Codec supports only 16 or 24 bpp; Or // your bitmap is 274 * 258 size, while the Codec supports only sizes that are powers of 2; etc... // Possible solution to avoid this is: make your bitmap suit the codec requirements, // or Choose a codec that is suitable for your bitmap. SetErrorMessage("Unable to Set Video Stream Format"); m_LastError = E_FAIL; return E_FAIL; } return S_OK; // Everything went Fine }
int AVIBegin(const char* filename, struct AVIFile* _avi_out) { AVIFile& avi = *_avi_out; int result = 0; do { if(!avi.video_added) break; if(!truncate_existing(filename)) break; // open the file if(FAILED(AVIFileOpen(&avi.avi_file, filename, OF_CREATE | OF_WRITE, NULL))) break; // create the video stream memset(&avi.avi_video_header, 0, sizeof(AVISTREAMINFO)); avi.avi_video_header.fccType = streamtypeVIDEO; avi.avi_video_header.dwScale = ONE_DOT_CYCLE*SNES_HCOUNTER_MAX*SNES_MAX_NTSC_VCOUNTER; avi.avi_video_header.dwRate = (int)NTSC_MASTER_CLOCK; avi.avi_video_header.dwSuggestedBufferSize = avi.bitmap_format.biSizeImage; if(FAILED(AVIFileCreateStream(avi.avi_file, &avi.streams[VIDEO_STREAM], &avi.avi_video_header))) break; if(use_prev_options) { avi.compress_options[VIDEO_STREAM] = saved_avi_info.compress_options[VIDEO_STREAM]; avi.compress_options_ptr[VIDEO_STREAM] = &avi.compress_options[0]; } else { // get compression options memset(&avi.compress_options[VIDEO_STREAM], 0, sizeof(AVICOMPRESSOPTIONS)); avi.compress_options_ptr[VIDEO_STREAM] = &avi.compress_options[0]; if(!AVISaveOptions(GUI.hWnd, 0, 1, &avi.streams[VIDEO_STREAM], &avi.compress_options_ptr[VIDEO_STREAM])) break; } // create compressed stream if(FAILED(AVIMakeCompressedStream(&avi.compressed_streams[VIDEO_STREAM], avi.streams[VIDEO_STREAM], &avi.compress_options[VIDEO_STREAM], NULL))) break; // set the stream format if(FAILED(AVIStreamSetFormat(avi.compressed_streams[VIDEO_STREAM], 0, (void*)&avi.bitmap_format, avi.bitmap_format.biSize))) break; // add sound (if requested) if(avi.sound_added) { // create the audio stream memset(&avi.avi_sound_header, 0, sizeof(AVISTREAMINFO)); avi.avi_sound_header.fccType = streamtypeAUDIO; avi.avi_sound_header.dwQuality = (DWORD)-1; avi.avi_sound_header.dwScale = avi.wave_format.nBlockAlign; avi.avi_sound_header.dwRate = avi.wave_format.nAvgBytesPerSec; avi.avi_sound_header.dwSampleSize = avi.wave_format.nBlockAlign; avi.avi_sound_header.dwInitialFrames = 1; if(FAILED(AVIFileCreateStream(avi.avi_file, &avi.streams[AUDIO_STREAM], &avi.avi_sound_header))) break; // AVISaveOptions doesn't seem to work for audio streams // so here we just copy the pointer for the compressed stream avi.compressed_streams[AUDIO_STREAM] = avi.streams[AUDIO_STREAM]; // set the stream format if(FAILED(AVIStreamSetFormat(avi.compressed_streams[AUDIO_STREAM], 0, (void*)&avi.wave_format, sizeof(WAVEFORMATEX)))) break; } // initialize counters avi.video_frames = 0; avi.sound_samples = 0; avi.tBytes = 0; avi.ByteBuffer = 0; avi.audio_buffer_pos = 0; strncpy(saved_cur_avi_fnameandext,filename,MAX_PATH); strncpy(saved_avi_fname,filename,MAX_PATH); char* dot = strrchr(saved_avi_fname, '.'); if(dot && dot > strrchr(saved_avi_fname, '/') && dot > strrchr(saved_avi_fname, '\\')) { strcpy(saved_avi_ext,dot); dot[0]='\0'; } // success result = 1; avi.valid = true; } while(false); if(!result) { clean_up(&avi); avi.valid = false; } return result; }
// --[ Method ]--------------------------------------------------------------- // // - Class : CAviRenderer // - prototype : bool StartRender(CWindowGL *pWindowGL, std::string strAviFile, int nFramerate) // // - Purpose : Prepares the object for capturing the opengl window to an AVI. // // ----------------------------------------------------------------------------- bool CAviRenderer::StartRender(CWindowGL *pWindowGL, std::string strAviFile, int nFramerate) { assert(pWindowGL); m_pWindowGL = NULL; m_bIsWorking = false; // Check VFW version. WORD wVer = HIWORD(VideoForWindowsVersion()); if(wVer < 0x010A) { CLogger::ErrorWindow("Video For Windows outdated version"); return false; } // Init library AVIFileInit(); // Get an image and stuff it into a bitmap. HRESULT hr; HBITMAP bmp; if((bmp = LoadBMPFromFB(pWindowGL)) == NULL) { return false; } LPBITMAPINFOHEADER lpInfoHeader = (LPBITMAPINFOHEADER)GlobalLock(MakeDib(bmp, 32)); DeleteObject(bmp); if(lpInfoHeader == NULL) { LOG.Write("\nERROR - CAviRenderer::StartRender(): GlobalLock() failed."); return false; } m_nWidth = (int)lpInfoHeader->biWidth; m_nHeight = (int)lpInfoHeader->biHeight; // Open an avi file for writing hr = AVIFileOpen( &m_pAviFile, // returned file pointer strAviFile.data(), // file name OF_WRITE | OF_CREATE, // mode to open file with NULL); // use handler determined // from file extension.... if (hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIFileOpen() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Configure the stream _fmemset(&m_aviStreamInfo, 0, sizeof(m_aviStreamInfo)); m_aviStreamInfo.fccType = streamtypeVIDEO; // stream type m_aviStreamInfo.fccHandler = 0; m_aviStreamInfo.dwScale = 1; m_aviStreamInfo.dwRate = nFramerate; m_aviStreamInfo.dwSuggestedBufferSize = lpInfoHeader->biSizeImage; SetRect(&m_aviStreamInfo.rcFrame, 0, 0, // rectangle for stream (int)lpInfoHeader->biWidth, (int)lpInfoHeader->biHeight); // And create the stream hr = AVIFileCreateStream(m_pAviFile, // file pointer &m_pAviStream, // returned stream pointer &m_aviStreamInfo); // stream header if(hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIFileCreateStream() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Get save options (prompt dialog) _fmemset(&m_aviOptions, 0, sizeof(m_aviOptions)); if(!AVISaveOptions(NULL, 0, 1, &m_pAviStream, (LPAVICOMPRESSOPTIONS FAR *) &m_pAviOptions)) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVISaveOptions() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Create compressed stream hr = AVIMakeCompressedStream(&m_pAviStreamCompressed, m_pAviStream, &m_aviOptions, NULL); if(hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIMakeCompressedStream() failed."); GlobalFreePtr(lpInfoHeader); return false; } // Set it's format hr = AVIStreamSetFormat(m_pAviStreamCompressed, 0, lpInfoHeader, // stream format lpInfoHeader->biSize + // format size lpInfoHeader->biClrUsed * sizeof(RGBQUAD)); if(hr != AVIERR_OK) { LOG.Write("\nERROR - CAviRenderer::StartRender(): AVIStreamSetFormat() failed."); GlobalFreePtr(lpInfoHeader); return false; } m_pWindowGL = pWindowGL; m_bIsWorking = true; UpdateWindow(pWindowGL->GetHWND()); return true; }
bool CAVIGenerator::AddWav(LPCTSTR wavFileName) { _bstr_t wavName = wavFileName; if( _tcsstr((const char *) wavName,_T("wav"))==NULL ) { wavName +=_T(".wav"); } wavFileName = wavName; struct FmtChunk { char id[4]; //="fmt " unsigned long size; //=16 short wFormatTag; //=WAVE_FORMAT_PCM=1 unsigned short wChannels; //=1 or 2 for mono or stereo unsigned long dwSamplesPerSec; //=11025 or 22050 or 44100 unsigned long dwAvgBytesPerSec; //=wBlockAlign * dwSamplesPerSec unsigned short wBlockAlign; //=wChannels * (wBitsPerSample==8?1:2) unsigned short wBitsPerSample; //=8 or 16, for bits per sample }; struct DataChunk { char id[4]; //="data" unsigned long size; //=datsize, size of the following array unsigned char data[1]; //=the raw data goes here }; struct WavChunk { char id[4]; //="RIFF" unsigned long size; //=datsize+8+16+4 char type[4]; //="WAVE" FmtChunk fmt; DataChunk dat; }; char *buf=0; HANDLE hf=CreateFile(wavFileName, GENERIC_READ, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); if (hf==INVALID_HANDLE_VALUE) return false; DWORD size = GetFileSize(hf,0); buf = new char[size]; DWORD red; ReadFile(hf,buf,size,&red,0); CloseHandle(hf); WavChunk *wav = (WavChunk*)buf; // WAVEFORMATEX wfx; wfx.wFormatTag = wav->fmt.wFormatTag; wfx.cbSize = 0; wfx.nAvgBytesPerSec = wav->fmt.dwAvgBytesPerSec; wfx.nBlockAlign = wav->fmt.wBlockAlign; wfx.nChannels = wav->fmt.wChannels; wfx.nSamplesPerSec = wav->fmt.dwSamplesPerSec; wfx.wBitsPerSample = wav->fmt.wBitsPerSample; AVISTREAMINFO ahdr; ZeroMemory(&ahdr,sizeof(ahdr)); ahdr.fccType = streamtypeAUDIO; ahdr.dwScale = wfx.nBlockAlign; ahdr.dwRate = wfx.nSamplesPerSec*wfx.nBlockAlign; ahdr.dwSampleSize =wfx.nBlockAlign; ahdr.dwQuality = (DWORD)-1; HRESULT hr = AVIFileCreateStream(m_pAVIFile, &m_pWavStream, &ahdr); if (hr!=AVIERR_OK) { if (buf!=0) delete[] buf; return false; } hr = AVIStreamSetFormat(m_pWavStream, 0, &wfx, sizeof(WAVEFORMATEX)); if (hr!=AVIERR_OK) { if (buf!=0) delete[] buf; return false; } // now we can write the data unsigned long numbytes = wav->size;//wav->dat.size; unsigned long numsamps = numbytes*8 / wfx.wBitsPerSample; hr = AVIStreamWrite(m_pWavStream, 0, numsamps, wav->dat.data, numbytes, 0, 0, 0); if (buf != 0) delete[] buf; AVIStreamRelease(m_pWavStream); return true; // au->nsamp+=numsamps; }
HRESULT CAVIGenerator::InitEngine() { AVISTREAMINFO strHdr; // information for a single stream static AVICOMPRESSOPTIONS opts; static AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts}; static bool first = true; TCHAR szBuffer[1024]; HRESULT hr; m_sError=_T("Ok"); // Step 0 : Let's make sure we are running on 1.1 DWORD wVer = HIWORD(VideoForWindowsVersion()); if (wVer < 0x010a) { // oops, we are too old, blow out of here m_sError=_T("Version of Video for Windows too old. Come on, join the 21th century!"); return S_FALSE; } // Step 1 : initialize AVI engine AVIFileInit(); // Step 2 : Open the movie file for writing.... hr = AVIFileOpen(&m_pAVIFile, // Address to contain the new file interface pointer (LPCSTR)m_sFile, // Null-terminated string containing the name of the file to open OF_WRITE | OF_CREATE, // Access mode to use when opening the file. NULL); // use handler determined from file extension. // Name your file .avi -> very important if (hr != AVIERR_OK) { // sprintf(szBuffer,_T("AVI Engine failed to initialize. Check filename %s."),m_sFile); m_sError=szBuffer; // Check it succeded. switch(hr) { case AVIERR_BADFORMAT: m_sError+=_T("The file couldn't be read, indicating a corrupt file or an unrecognized format."); break; case AVIERR_MEMORY: m_sError+=_T("The file could not be opened because of insufficient memory."); break; case AVIERR_FILEREAD: m_sError+=_T("A disk error occurred while reading the file."); break; case AVIERR_FILEOPEN: m_sError+=_T("A disk error occurred while opening the file."); break; case REGDB_E_CLASSNOTREG: m_sError+=_T("According to the registry, the type of file specified in AVIFileOpen does not have a handler to process it"); break; } return hr; } // Fill in the header for the video stream.... memset(&strHdr, 0, sizeof(strHdr)); strHdr.fccType = streamtypeVIDEO; // video stream type strHdr.fccHandler = 0; strHdr.dwScale = 1; // should be one for video strHdr.dwRate = m_dwRate; // fps strHdr.dwSuggestedBufferSize = m_bih.biSizeImage; // Recommended buffer size, in bytes, for the stream. SetRect(&strHdr.rcFrame, 0, 0, // rectangle for stream (int) m_bih.biWidth, (int) m_bih.biHeight); // Step 3 : Create the stream; hr = AVIFileCreateStream(m_pAVIFile, // file pointer &m_pStream, // returned stream pointer &strHdr); // stream header // Check it succeded. if (hr != AVIERR_OK) { m_sError=_T("AVI Stream creation failed. Check Bitmap info."); if (hr==AVIERR_READONLY) { m_sError+=_T(" Read only file."); } return hr; } // if (first) { // Step 4: Get codec and infos about codec memset(&opts, 0, sizeof(opts)); // Poping codec dialog if (!AVISaveOptions(NULL, 0, 1, &m_pStream, (LPAVICOMPRESSOPTIONS FAR *) &aopts)) { AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); return S_FALSE; } // first = false; } // Step 5: Create a compressed stream using codec options. hr = AVIMakeCompressedStream(&m_pStreamCompressed, m_pStream, &opts, NULL); if (hr != AVIERR_OK) { m_sError=_T("AVI Compressed Stream creation failed."); switch(hr) { case AVIERR_NOCOMPRESSOR: m_sError+=_T(" A suitable compressor cannot be found."); break; case AVIERR_MEMORY: m_sError+=_T(" There is not enough memory to complete the operation."); break; case AVIERR_UNSUPPORTED: m_sError+=_T("Compression is not supported for this type of data. This error might be returned if you try to compress data that is not audio or video."); break; } return hr; } // releasing memory allocated by AVISaveOptionFree hr = AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); if (hr!=AVIERR_OK) { m_sError=_T("Error releasing memory"); return hr; } // Step 6 : sets the format of a stream at the specified position hr = AVIStreamSetFormat(m_pStreamCompressed, 0, // position &m_bih, // stream format m_bih.biSize + // format size m_bih.biClrUsed * sizeof(RGBQUAD)); if (hr != AVIERR_OK) { m_sError=_T("AVI Compressed Stream format setting failed."); return hr; } // Step 6 : Initialize step counter m_lFrame=0; return hr; }
static int avi_open(const char* filename, const BITMAPINFOHEADER* pbmih, const WAVEFORMATEX* pwfex, const struct VideoSystemInfo* vsi) { int error = 1; int result = 0; do { // close existing first FCEUI_AviEnd(); if(!truncate_existing(filename)) break; if(!pbmih) break; // create the object avi_create(&avi_file); // set video size and framerate avi_file->start_scanline = vsi->start_scanline; avi_file->end_scanline = vsi->end_scanline; //zero 20-oct-2012 - AVIFileClose has bugs in it which cause overflows in the calculation of dwTotalFrames, so some programs are unhappy with the resulting files. //so I reduced the precision here by the minimum number of shifts necessary to make it not overflow avi_file->fps = vsi->fps >> 3; avi_file->fps_scale = (16 * 1024 * 1024) >> 3; avi_file->convert_buffer = (uint8*)malloc(VIDEO_WIDTH*(vsi->end_scanline-vsi->start_scanline)*3); // open the file if(FAILED(AVIFileOpen(&avi_file->avi_file, filename, OF_CREATE | OF_WRITE, NULL))) break; // create the video stream set_video_format(pbmih, avi_file); memset(&avi_file->avi_video_header, 0, sizeof(AVISTREAMINFO)); avi_file->avi_video_header.fccType = streamtypeVIDEO; avi_file->avi_video_header.dwScale = avi_file->fps_scale; avi_file->avi_video_header.dwRate = avi_file->fps; avi_file->avi_video_header.dwSuggestedBufferSize = avi_file->bitmap_format.biSizeImage; if(FAILED(AVIFileCreateStream(avi_file->avi_file, &avi_file->streams[VIDEO_STREAM], &avi_file->avi_video_header))) break; if(use_prev_options) { avi_file->compress_options[VIDEO_STREAM] = saved_avi_info.compress_options[VIDEO_STREAM]; avi_file->compress_options_ptr[VIDEO_STREAM] = &avi_file->compress_options[0]; } else { // get compression options memset(&avi_file->compress_options[VIDEO_STREAM], 0, sizeof(AVICOMPRESSOPTIONS)); avi_file->compress_options_ptr[VIDEO_STREAM] = &avi_file->compress_options[0]; //retryAviSaveOptions: //mbg merge 7/17/06 removed error = 0; if(!AVISaveOptions(hAppWnd, 0, 1, &avi_file->streams[VIDEO_STREAM], &avi_file->compress_options_ptr[VIDEO_STREAM])) break; error = 1; } // create compressed stream if(FAILED(AVIMakeCompressedStream(&avi_file->compressed_streams[VIDEO_STREAM], avi_file->streams[VIDEO_STREAM], &avi_file->compress_options[VIDEO_STREAM], NULL))) break; // set the stream format if(FAILED(AVIStreamSetFormat(avi_file->compressed_streams[VIDEO_STREAM], 0, (void*)&avi_file->bitmap_format, avi_file->bitmap_format.biSize))) break; // add sound (if requested) if(pwfex) { // add audio format set_sound_format(pwfex, avi_file); // create the audio stream memset(&avi_file->avi_sound_header, 0, sizeof(AVISTREAMINFO)); avi_file->avi_sound_header.fccType = streamtypeAUDIO; avi_file->avi_sound_header.dwQuality = (DWORD)-1; avi_file->avi_sound_header.dwScale = avi_file->wave_format.nBlockAlign; avi_file->avi_sound_header.dwRate = avi_file->wave_format.nAvgBytesPerSec; avi_file->avi_sound_header.dwSampleSize = avi_file->wave_format.nBlockAlign; avi_file->avi_sound_header.dwInitialFrames = 1; if(FAILED(AVIFileCreateStream(avi_file->avi_file, &avi_file->streams[AUDIO_STREAM], &avi_file->avi_sound_header))) break; // AVISaveOptions doesn't seem to work for audio streams // so here we just copy the pointer for the compressed stream avi_file->compressed_streams[AUDIO_STREAM] = avi_file->streams[AUDIO_STREAM]; // set the stream format if(FAILED(AVIStreamSetFormat(avi_file->compressed_streams[AUDIO_STREAM], 0, (void*)&avi_file->wave_format, sizeof(WAVEFORMATEX)))) break; } // initialize counters avi_file->video_frames = 0; avi_file->sound_samples = 0; avi_file->tBytes = 0; avi_file->ByteBuffer = 0; avi_file->audio_buffer_pos = 0; // success error = 0; result = 1; avi_file->valid = 1; } while(0); if(!result) { avi_destroy(&avi_file); if(error) FCEUD_PrintError("Error writing AVI file"); } return result; }
int imFileFormatAVI::WriteImageInfo() { if (dib) { if (dib->bmih->biWidth != width || dib->bmih->biHeight != height || imColorModeSpace(file_color_mode) != imColorModeSpace(user_color_mode)) return IM_ERR_DATA; return IM_ERR_NONE; // parameters can be set only once } // force bottom up orientation this->file_data_type = IM_BYTE; this->file_color_mode = imColorModeSpace(this->user_color_mode); int bpp; if (this->file_color_mode == IM_RGB) { this->file_color_mode |= IM_PACKED; bpp = 24; if (imColorModeHasAlpha(this->user_color_mode)) { this->file_color_mode |= IM_ALPHA; bpp = 32; this->rmask = 0x00FF0000; this->roff = 16; this->gmask = 0x0000FF00; this->goff = 8; this->bmask = 0x000000FF; this->boff = 0; } } else bpp = 8; this->line_buffer_extra = 4; // room enough for padding imAttribTable* attrib_table = AttribTable(); const void* attrib_data = attrib_table->Get("FPS"); if (attrib_data) fps = *(float*)attrib_data; else fps = 15; if (this->compression[0] == 0 || imStrEqual(this->compression, "NONE")) this->use_compressor = 0; else this->use_compressor = 1; dib = imDibCreate(width, height, bpp); if (use_compressor) { memset(&compvars, 0, sizeof(COMPVARS)); compvars.cbSize = sizeof(COMPVARS); if (imStrEqual(this->compression, "CUSTOM")) { if (ICCompressorChoose(NULL, ICMF_CHOOSE_DATARATE | ICMF_CHOOSE_KEYFRAME, dib->dib, NULL, &compvars, "Choose Compression") == FALSE) return IM_ERR_COMPRESS; } else { compvars.dwFlags = ICMF_COMPVARS_VALID; compvars.fccType = ICTYPE_VIDEO; int* attrib = (int*)attrib_table->Get("KeyFrameRate"); if (attrib) compvars.lKey = *attrib; else compvars.lKey = 15; // same defaults of the dialog attrib = (int*)attrib_table->Get("DataRate"); if (attrib) compvars.lDataRate = *attrib / 8; else compvars.lDataRate = 300; // same defaults of the dialog attrib = (int*)attrib_table->Get("AVIQuality"); if (attrib) compvars.lQ = *attrib; else compvars.lQ = (DWORD)ICQUALITY_DEFAULT; if (imStrEqual(this->compression, "RLE")) compvars.fccHandler = mmioFOURCC('M','R','L','E'); else if (imStrEqual(this->compression, "CINEPACK")) compvars.fccHandler = mmioFOURCC('c','v','i','d'); else compvars.fccHandler = mmioFOURCC(compression[0],compression[1],compression[2],compression[3]); compvars.hic = ICOpen(ICTYPE_VIDEO, compvars.fccHandler, ICMODE_COMPRESS); } if (compvars.hic == NULL) use_compressor = 0; } AVISTREAMINFO streaminfo; memset(&streaminfo, 0, sizeof(AVISTREAMINFO)); streaminfo.fccType = streamtypeVIDEO; streaminfo.dwScale = 1000; streaminfo.dwRate = (DWORD)(fps*1000); SetRect(&streaminfo.rcFrame, 0, 0, width, height); if (use_compressor) { streaminfo.fccHandler = compvars.fccHandler; streaminfo.dwQuality = compvars.lQ; } else { streaminfo.fccHandler = mmioFOURCC('D','I','B',' '); streaminfo.dwQuality = (DWORD)ICQUALITY_DEFAULT; } /* creates a new stream in the new file */ HRESULT hr = AVIFileCreateStream(file, &stream, &streaminfo); if (hr != 0) return IM_ERR_ACCESS; /* set stream format */ if (use_compressor) { if (!ICSeqCompressFrameStart(&compvars, dib->bmi)) return IM_ERR_COMPRESS; hr = AVIStreamSetFormat(stream, 0, compvars.lpbiOut, dib->size - dib->bits_size); } else hr = AVIStreamSetFormat(stream, 0, dib->dib, dib->size - dib->bits_size); if (hr != 0) return IM_ERR_ACCESS; return IM_ERR_NONE; }