bool MovieMaker::CreateCompressedStream(const string &codec, uint frameRate) { //Get the compression options HRESULT result = AVIERR_OK; AVICOMPRESSOPTIONS opts; AVICOMPRESSOPTIONS FAR * aopts[1] = {&opts}; memset(&opts, 0, sizeof(opts)); //If using the menu if(codec == "menu") { //Load the memu options if (!AVISaveOptions(GetActiveWindow(), 0, 1, &ps, (LPAVICOMPRESSOPTIONS FAR *)&aopts)) { AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); return false; } //Create a compressed stream using codec options. result = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL); if (result != AVIERR_OK) { AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); return false; } //Release save options result = AVISaveOptionsFree(1,(LPAVICOMPRESSOPTIONS FAR *) &aopts); if (result!=AVIERR_OK) { LOGERR(("CreateCompressedStream -Error releasing memory")); return false; } } else { opts.fccType=streamtypeVIDEO; opts.fccHandler= CreateCodecDWORD(codec); // mmioFOURCC('M','P','G','4'); opts.dwFlags=AVICOMPRESSF_KEYFRAMES | AVICOMPRESSF_VALID;//|AVICOMPRESSF_DATARATE; opts.dwKeyFrameEvery=frameRate; //Create a compressed stream using codec options. result = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL); if (result != AVIERR_OK) { return false; } } return true; }
void captureVideo(HDC hDC){ if(saves.size()>0){ SendMessage(hWnd,WM_COMMAND,MAKEWPARAM(IDC_LISTBOX,LBN_SELCHANGE),0); currentFrame = 0; GLvoid *imageData = 0; while(WWDPhysics->totaltime<10000){ while(!WWDPhysics->clientMoveAndDisplay(true, hDC)){} HDC hdcscreen=GetDC(0), hdc=CreateCompatibleDC(hdcscreen); ReleaseDC(0,hdcscreen); HBITMAP hData; hData = CreateDIBSection(hdc,(BITMAPINFO*) bi,DIB_RGB_COLORS,&imageData,NULL,NULL); GdiFlush(); //flush graphics operations batch to ensure memory contains the right pixels if(imageData!=0){ glReadPixels(0, 0, abs(simWidth), abs(simHeight), GL_BGRA, GL_UNSIGNED_BYTE, imageData); //Copy the image to the array imageData DIBSECTION dibs; int sbm = GetObject(hData,sizeof(dibs),&dibs); if (sbm!=sizeof(DIBSECTION)){ #ifdef _DEBUG printf("dibsection fault\n"); #endif }else{ DWORD keyframe = NULL; if(currentFrame==0){keyframe=AVIIF_KEYFRAME;} AVIStreamWrite(*pcompressedstream,currentFrame,1,dibs.dsBm.bmBits,dibs.dsBmih.biSizeImage,keyframe,NULL,NULL); currentFrame++; } DeleteObject(hData); } else{ #ifdef _DEBUG printf("frame skipped nr %d\n",currentFrame); #endif } } } //vfw cleanup if(VFWReturnVal==0){ AVIStreamRelease(*pcompressedstream); delete(pcompressedstream); AVIStreamRelease(*pstream); delete pstream; AVISaveOptionsFree(1,aopts); AVIFileRelease(*pfile); // closes the file } AVIFileExit(); // releases AVIFile library //\vfw cleanup }
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; }
static void test_AVISaveOptions(void) { AVICOMPRESSOPTIONS options[2]; LPAVICOMPRESSOPTIONS poptions[2]; PAVISTREAM streams[2] = {NULL, NULL}; HRESULT hres; DWORD res; LONG lres; poptions[0] = &options[0]; poptions[1] = &options[1]; ZeroMemory(options, sizeof(options)); SetLastError(0xdeadbeef); hres = CreateEditableStream(&streams[0], NULL); ok(hres == AVIERR_OK, "0: got 0x%x and %p (expected AVIERR_OK)\n", hres, streams[0]); SetLastError(0xdeadbeef); hres = CreateEditableStream(&streams[1], NULL); ok(hres == AVIERR_OK, "1: got 0x%x and %p (expected AVIERR_OK)\n", hres, streams[1]); SetLastError(0xdeadbeef); hres = EditStreamSetNameA(streams[0], winetest0); ok(hres == AVIERR_OK, "0: got 0x%x (expected AVIERR_OK)\n", hres); SetLastError(0xdeadbeef); hres = EditStreamSetNameA(streams[1], winetest1); ok(hres == AVIERR_OK, "1: got 0x%x (expected AVIERR_OK)\n", hres); if (winetest_interactive) { SetLastError(0xdeadbeef); res = AVISaveOptions(0, ICMF_CHOOSE_DATARATE |ICMF_CHOOSE_KEYFRAME | ICMF_CHOOSE_ALLCOMPRESSORS, 2, streams, poptions); trace("got %u with 0x%x/%u\n", res, GetLastError(), GetLastError()); } SetLastError(0xdeadbeef); lres = AVISaveOptionsFree(2, poptions); ok(lres == AVIERR_OK, "got 0x%x with 0x%x/%u\n", lres, GetLastError(), GetLastError()); SetLastError(0xdeadbeef); res = AVIStreamRelease(streams[0]); ok(res == 0, "0: got refcount %u (expected 0)\n", res); SetLastError(0xdeadbeef); res = AVIStreamRelease(streams[1]); ok(res == 0, "1: got refcount %u (expected 0)\n", res); }
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*/ }
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; }
boolean VFWInit(){ AVIFileInit(); CoInitialize(NULL); std::stringstream filename; filename << directory<< "TheVideo.avi\0"; pfile = new PAVIFILE(); VFWReturnVal = AVIFileOpen(pfile, filename.str().c_str(),OF_SHARE_DENY_WRITE|OF_CREATE,0L); if(VFWReturnVal !=0 ){ return false; } AVISTREAMINFO* strhdr = new AVISTREAMINFO();// ZeroMemory(&strhdr,sizeof(strhdr)); strhdr->fccType = streamtypeVIDEO;// stream type strhdr->fccHandler = 0; strhdr->dwScale = 1; strhdr->dwRate = 60;//10;//100;//*/1000; pstream = new PAVISTREAM(); VFWReturnVal = AVIFileCreateStream(*pfile,pstream,strhdr); if(VFWReturnVal !=0 ){ return false; } poptions = new AVICOMPRESSOPTIONS(); aopts[0] = poptions; AVISaveOptions(hWnd,0,1, pstream,aopts); DWORD flag=poptions->dwFlags &AVICOMPRESSF_VALID; if(!(poptions->dwFlags &AVICOMPRESSF_VALID)){ delete poptions; return false; } pcompressedstream = new PAVISTREAM(); VFWReturnVal = AVIMakeCompressedStream(pcompressedstream,*pstream,aopts[0],NULL); if(VFWReturnVal !=AVIERR_OK ){ AVIStreamRelease(*pcompressedstream); delete(pcompressedstream); AVIStreamRelease(*pstream); delete pstream; AVISaveOptionsFree(1,aopts); AVIFileRelease(*pfile); return false; } bi = new BITMAPINFOHEADER(); bi->biSize = sizeof (BITMAPINFOHEADER); bi->biWidth = simWidth; bi->biHeight = simHeight; bi->biPlanes = 1; bi->biBitCount = 32; bi->biCompression = BI_RGB; bi->biSizeImage = ((bi->biWidth*bi->biBitCount/8)*bi->biHeight); bi->biXPelsPerMeter = 14173; bi->biYPelsPerMeter = 14173; bi->biClrUsed = 0; bi->biClrImportant = 0; VFWReturnVal = AVIStreamSetFormat(*pcompressedstream,0,bi,bi->biSize); if(VFWReturnVal != 0 ){ AVIStreamRelease(*pcompressedstream); delete(pcompressedstream); AVIStreamRelease(*pstream); delete pstream; AVISaveOptionsFree(1,aopts); AVIFileRelease(*pfile); return false; } return true; }