writeavi.c

Go to the documentation of this file.
00001 /* file WRITEAVI.c                                    */
00002 
00003 /* Code to create an AVI file (Open write each frame  */
00004 /* etc). given a pointer to a bitmap structure        */
00005 
00006 /* --
00007 OpenFX version 2.0  - Modelling, Animation and Rendering Package
00008 Copyright (C) 2000 - 2007 OpenFX Development Team
00009 -- */
00010 
00011 
00012 #ifndef STRICT
00013 #define STRICT
00014 #endif
00015 
00016 #define  INC_OLE2
00017 #include <windows.h>
00018 #include <windowsx.h>
00019 #include <mmsystem.h>
00020 #include <vfw.h>
00021 #include <stdlib.h>
00022 
00023 #define AVIIF_KEYFRAME  0x00000010L // this frame is a key frame.
00024 
00025 void configureAVIfile(HWND, char *, void *);
00026 void openAVIfile(char *Name, LPBITMAPINFOHEADER alpbi, int compress, long speed);
00027 void appendAVIfile(LPBITMAPINFOHEADER alpbi);
00028 void closeAVIfile(void);
00029 
00030 int AVI_frame_rate=25;
00031 
00032 static int fci=0;
00033 static PAVIFILE pfile = NULL;
00034 static PAVISTREAM ps = NULL, psCompressed = NULL;
00035 static BOOL compress_flag=TRUE, options=FALSE;
00036 static AVICOMPRESSOPTIONS  aviopt;
00037 static BOOL bConfigured=FALSE;
00038 
00039 static PSTR AVIFileInPath(PSTR pstrPath){
00040  PSTR pstr;
00041  pstr = pstrPath + strlen(pstrPath);
00042  while (pstr > pstrPath) {
00043      pstr = (AnsiPrev(pstrPath, pstr));
00044      if (*pstr == '\\' || *pstr == ':' || *pstr == '/') {
00045          pstr = (AnsiNext(pstr));
00046          break;
00047      }
00048  }
00049  return pstr;
00050 }
00051 
00052 void configureAVIfile(HWND hWnd, char *preview, void *arg){
00053  char filename[256];
00054  PAVIFILE pfile = NULL;
00055  PAVISTREAM ps = NULL;
00056  LPAVICOMPRESSOPTIONS lpaviopt;
00057  HRESULT hr;
00058  if(preview != NULL)strcpy(filename,preview);
00059  else{
00060    GetModuleFileName(NULL,filename,256);
00061    strcpy(AVIFileInPath(filename),"blank.avi");
00062  }
00063  AVIFileInit();
00064  hr = AVIFileOpen(&pfile,filename,OF_READ,NULL);
00065  if(hr == AVIERR_OK){
00066    int i;
00067    AVISTREAMINFO avis;
00068    for(i=0;i<10;i++){
00069      if(AVIFileGetStream(pfile,&ps,0,i) == AVIERR_OK){
00070        AVIStreamInfo(ps,&avis,sizeof(avis));
00071        if(avis.fccType == streamtypeVIDEO){
00072          lpaviopt=&aviopt;
00073          if(AVISaveOptions(hWnd,
00074                ICMF_CHOOSE_KEYFRAME|ICMF_CHOOSE_DATARATE|ICMF_CHOOSE_PREVIEW,
00075                1,&ps,&lpaviopt) == TRUE)options=TRUE;
00076          bConfigured=TRUE;
00077          break;
00078        }
00079      }
00080    }
00081    if(pfile != NULL)AVIFileRelease(pfile);
00082  }
00083  AVIFileExit();
00084  return;
00085 }
00086 
00087 void openAVIfile(char *Name, LPBITMAPINFOHEADER alpbi, int compress, long speed){
00088  AVISTREAMINFO strhdr;
00089  AVICOMPRESSOPTIONS opts;
00090  HRESULT hr;
00091  WORD wVer;
00092  fci=0;
00093  ps = NULL; psCompressed = NULL; pfile=NULL;
00094  wVer = HIWORD(VideoForWindowsVersion());
00095  if (wVer < 0x010a){
00096    MessageBeep(MB_ICONHAND);
00097    MessageBox(NULL, "Version",NULL,MB_OK|MB_ICONSTOP);
00098    return;
00099  }
00100  AVIFileInit();
00101  hr = AVIFileOpen(&pfile,             // returned file pointer
00102           Name,
00103           OF_WRITE | OF_CREATE,       // mode to open file with
00104           NULL);                      // use handler determined
00105                                       // from file extension....
00106  if (hr != AVIERR_OK)goto error;
00107  _fmemset(&strhdr, 0, sizeof(strhdr));
00108  strhdr.fccType                = streamtypeVIDEO;// stream type
00109  strhdr.fccHandler             = 0;
00110  strhdr.dwScale                = 1;
00111  strhdr.dwRate                 = AVI_frame_rate;
00112  strhdr.dwSuggestedBufferSize  = alpbi->biSizeImage;
00113  SetRect(&strhdr.rcFrame, 0, 0,              // rectangle for stream
00114      (int)alpbi->biWidth,
00115      (int)alpbi->biHeight);
00116  hr = AVIFileCreateStream(pfile,             // file pointer
00117         &ps,                                 // returned stream pointer
00118         &strhdr);                            // stream header
00119  if (hr != AVIERR_OK)goto error;
00120  _fmemset(&opts, 0, sizeof(opts));
00121 // opts.dwFlags = AVICOMPRESSF_VALID | AVICOMPRESSF_KEYFRAMES;
00122 // opts.fccHandler = mmioFOURCC('C', 'V', 'I', 'D');
00123  opts.dwFlags = AVICOMPRESSF_VALID;
00124  opts.fccType = streamtypeVIDEO;
00125  opts.fccHandler = mmioFOURCC('M', 'S', 'V', 'C');
00126  opts.dwQuality = 6500;
00127  opts.dwKeyFrameEvery = 10;
00128  if(options){
00129    compress=TRUE;
00130    memcpy(&opts,&aviopt,sizeof(AVICOMPRESSOPTIONS));
00131  }
00132  if(compress){
00133    compress_flag=TRUE;
00134    hr = AVIMakeCompressedStream(&psCompressed, ps, &opts, NULL);
00135    if (hr != AVIERR_OK)goto error;
00136  }
00137  else{
00138    compress_flag=FALSE;
00139    psCompressed=ps;
00140  }
00141  hr = AVIStreamSetFormat(psCompressed, 0,
00142              alpbi,            // stream format
00143              alpbi->biSize +   // format size
00144              alpbi->biClrUsed * sizeof(RGBQUAD));
00145  if (hr != AVIERR_OK)goto error;
00146  return;
00147  error:
00148  closeAVIfile();
00149 }
00150 
00151 void appendAVIfile(LPBITMAPINFOHEADER alpbi){
00152  HRESULT hr;
00153  if(!ps || !psCompressed || !pfile)return;
00154  hr = AVIStreamWrite(psCompressed, // stream pointer
00155   fci,                             // time of this frame
00156   1,                               // number to write
00157   (LPBYTE) alpbi +                 // pointer to data
00158    alpbi->biSize +
00159    alpbi->biClrUsed * sizeof(RGBQUAD),
00160    alpbi->biSizeImage,            // size of this frame
00161    AVIIF_KEYFRAME,                // flags....
00162    NULL,
00163    NULL);
00164  if (hr != AVIERR_OK)closeAVIfile();
00165  fci++;
00166 }
00167 
00168 void closeAVIfile(void){
00169  if(bConfigured){
00170    LPAVICOMPRESSOPTIONS lpaviopt;
00171    lpaviopt=&aviopt;
00172    AVISaveOptionsFree(1,&lpaviopt);
00173  }
00174  if (ps)AVIStreamClose(ps); ps=NULL;
00175  if(compress_flag){
00176    if(psCompressed)AVIStreamClose(psCompressed); psCompressed=NULL;
00177  }
00178  if (pfile)AVIFileClose(pfile); pfile=NULL;
00179  AVIFileExit();
00180  return;
00181 }
00182 
Generated on Tue Jan 28 06:18:27 2014 for OpenFX by  doxygen 1.6.3