Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

AVICapture Class Reference

#include <avicapture.h>

Inheritance diagram for AVICapture:

Inheritance graph
Collaboration diagram for AVICapture:

Collaboration graph
List of all members.

Public Member Functions

 AVICapture ()
bool captureFrame ()
bool end ()
int getFrameCount () const
float getFrameRate () const
int getHeight () const
int getWidth () const
bool start (const std::string &filename, int w, int h, float fps)
virtual ~AVICapture ()

Private Member Functions

void cleanup ()

Private Attributes

PAVIFILE aviFile
PAVISTREAM aviStream
bool capturing
PAVISTREAM compAviStream
int frameCounter
float frameRate
int height
unsigned char * image
int width

Constructor & Destructor Documentation

AVICapture::AVICapture  ) 
 

Definition at line 22 of file avicapture.cpp.

00022                        :
00023     width(-1),
00024     height(-1),
00025     frameRate(30.0f),
00026     frameCounter(0),
00027     capturing(false),
00028     aviFile(NULL),
00029     aviStream(NULL),
00030     compAviStream(NULL),
00031     image(NULL)
00032 {
00033     AVIFileInit();
00034 }

AVICapture::~AVICapture  )  [virtual]
 

Definition at line 37 of file avicapture.cpp.

References cleanup().

00038 {
00039     cleanup();
00040     AVIFileExit();
00041 }


Member Function Documentation

bool AVICapture::captureFrame  )  [virtual]
 

Implements MovieCapture.

Definition at line 151 of file avicapture.cpp.

References capturing, compAviStream, DPRINTF, frameCounter, GL_BGR_EXT, height, image, and width.

00152 {
00153     if (!capturing)
00154         return false;
00155 
00156     // Get the dimensions of the current viewport
00157     int viewport[4];
00158     glGetIntegerv(GL_VIEWPORT, viewport);
00159 
00160     int x = viewport[0] + (viewport[2] - width) / 2;
00161     int y = viewport[1] + (viewport[3] - height) / 2;
00162     glReadPixels(x, y, width, height,
00163                  GL_BGR_EXT, GL_UNSIGNED_BYTE,
00164                  image);
00165 
00166     int rowBytes = (width * 3 + 3) & ~0x3;
00167 
00168     LONG samplesWritten = 0;
00169     LONG bytesWritten = 0;
00170     HRESULT hr = AVIStreamWrite(compAviStream,
00171                                 frameCounter,
00172                                 1,
00173                                 image,
00174                                 rowBytes * height,
00175                                 AVIIF_KEYFRAME,
00176                                 &samplesWritten,
00177                                 &bytesWritten);
00178     if (hr != AVIERR_OK)
00179     {
00180         DPRINTF(0, "AVIStreamWrite failed on frame %d\n", frameCounter);
00181         return false;
00182     }
00183 
00184     // printf("Writing frame: %d  %d => %d bytes\n",
00185     //        frameCounter, rowBytes * height, bytesWritten);
00186     frameCounter++;
00187 
00188     return true;
00189 }

void AVICapture::cleanup  )  [private]
 

Definition at line 192 of file avicapture.cpp.

References aviFile, aviStream, compAviStream, and image.

Referenced by end(), start(), and ~AVICapture().

00193 {
00194     if (aviStream != NULL)
00195     {
00196         AVIStreamRelease(aviStream);
00197         aviStream = NULL;
00198     }
00199     if (compAviStream != NULL)
00200     {
00201         AVIStreamRelease(compAviStream);
00202         compAviStream = NULL;
00203     }
00204     if (aviFile != NULL)
00205     {
00206         AVIFileRelease(aviFile);
00207         aviFile = NULL;
00208     }
00209     if (image != NULL)
00210     {
00211         delete[] image;
00212         image = NULL;
00213     }
00214 }

bool AVICapture::end  )  [virtual]
 

Implements MovieCapture.

Definition at line 142 of file avicapture.cpp.

References capturing, and cleanup().

00143 {
00144     capturing = false;
00145     cleanup();
00146 
00147     return true;
00148 }

int AVICapture::getFrameCount  )  const [virtual]
 

Implements MovieCapture.

Definition at line 232 of file avicapture.cpp.

References frameCounter.

00233 {
00234     return frameCounter;
00235 }

float AVICapture::getFrameRate  )  const [virtual]
 

Implements MovieCapture.

Definition at line 227 of file avicapture.cpp.

References frameRate.

00228 {
00229     return frameRate;
00230 }

int AVICapture::getHeight  )  const [virtual]
 

Implements MovieCapture.

Definition at line 222 of file avicapture.cpp.

References height.

00223 {
00224     return height;
00225 }

int AVICapture::getWidth  )  const [virtual]
 

Implements MovieCapture.

Definition at line 217 of file avicapture.cpp.

References width.

00218 {
00219     return width;
00220 }

bool AVICapture::start const std::string filename,
int  w,
int  h,
float  fps
[virtual]
 

Implements MovieCapture.

Definition at line 44 of file avicapture.cpp.

References aviFile, aviStream, capturing, cleanup(), compAviStream, DPRINTF, frameCounter, frameRate, height, image, and width.

00047 {
00048     if (capturing)
00049         return false;
00050 
00051     width = w;
00052     height = h;
00053     frameRate = fps;
00054 
00055     if (HIWORD(VideoForWindowsVersion()) < 0x010a)
00056     {
00057         // We need to be running on version 1.1 or later
00058         return false;
00059     }
00060 
00061     // Compute the width of a row in bytes; pad so that rows are aligned on
00062     // 4 byte boundaries.
00063     int rowBytes = (width * 3 + 3) & ~0x3;
00064     image = new unsigned char[rowBytes * height]; 
00065 
00066     HRESULT hr = AVIFileOpen(&aviFile,
00067                              filename.c_str(),
00068                              OF_WRITE | OF_CREATE,
00069                              NULL);
00070     if (hr != AVIERR_OK)
00071     {
00072         DPRINTF(0, "Erroring creating avi file for capture.\n");
00073         return false;
00074     }
00075 
00076     AVISTREAMINFO info;
00077     ZeroMemory(&info, sizeof info);
00078     info.fccType = streamtypeVIDEO;
00079     info.fccHandler = 0;
00080     info.dwScale = 1;
00081     info.dwRate = (DWORD) floor(frameRate + 0.5f);
00082     info.dwSuggestedBufferSize = rowBytes * height;
00083     SetRect(&info.rcFrame, 0, 0, width, height);
00084     hr = AVIFileCreateStream(aviFile, &aviStream, &info);
00085     if (hr != AVIERR_OK)
00086     {
00087         DPRINTF(0, "Error %08x creating AVI stream.\n", hr);
00088         cleanup();
00089         return false;
00090     }
00091 
00092     // Display a dialog to allow the user to select compression options
00093     AVICOMPRESSOPTIONS options;
00094     AVICOMPRESSOPTIONS* arrOptions[1] = { &options };
00095     ZeroMemory(&options, sizeof options);
00096 
00097     if (!AVISaveOptions(NULL, 0, 1, &aviStream, 
00098                         (LPAVICOMPRESSOPTIONS*) &arrOptions))
00099     {
00100         // The user either clicked on cancel or there was an error
00101         cleanup();
00102         return false;
00103     }
00104 
00105     hr = AVIMakeCompressedStream(&compAviStream, aviStream, &options, NULL);
00106     if (hr != AVIERR_OK)
00107     {
00108         DPRINTF(0, "Error %08x creating compressed AVI stream.\n", hr);
00109         cleanup();
00110         return false;
00111     }
00112 
00113     BITMAPINFOHEADER bi;
00114     ZeroMemory(&bi, sizeof bi);
00115     bi.biSize = sizeof bi;
00116     bi.biWidth = width;
00117     bi.biHeight = height;
00118     bi.biPlanes = 1;
00119     bi.biBitCount = 24;
00120     bi.biCompression = BI_RGB;
00121     bi.biSizeImage = rowBytes * height;
00122     bi.biXPelsPerMeter = 0;
00123     bi.biYPelsPerMeter = 0;
00124     bi.biClrUsed = 0;
00125     bi.biClrImportant = 0;
00126 
00127     hr = AVIStreamSetFormat(compAviStream, 0, &bi, sizeof bi);
00128     if (hr != AVIERR_OK)
00129     {
00130         DPRINTF(0, "AVIStreamSetFormat failed: %08x\n", hr);
00131         cleanup();
00132         return false;
00133     }
00134 
00135     capturing = true;
00136     frameCounter = 0;
00137 
00138     return true;
00139 }


Member Data Documentation

PAVIFILE AVICapture::aviFile [private]
 

Definition at line 42 of file avicapture.h.

Referenced by cleanup(), and start().

PAVISTREAM AVICapture::aviStream [private]
 

Definition at line 43 of file avicapture.h.

Referenced by cleanup(), and start().

bool AVICapture::capturing [private]
 

Definition at line 41 of file avicapture.h.

Referenced by captureFrame(), end(), and start().

PAVISTREAM AVICapture::compAviStream [private]
 

Definition at line 44 of file avicapture.h.

Referenced by captureFrame(), cleanup(), and start().

int AVICapture::frameCounter [private]
 

Definition at line 40 of file avicapture.h.

Referenced by captureFrame(), getFrameCount(), and start().

float AVICapture::frameRate [private]
 

Definition at line 39 of file avicapture.h.

Referenced by getFrameRate(), and start().

int AVICapture::height [private]
 

Definition at line 38 of file avicapture.h.

Referenced by captureFrame(), getHeight(), and start().

unsigned char* AVICapture::image [private]
 

Definition at line 45 of file avicapture.h.

Referenced by captureFrame(), cleanup(), and start().

int AVICapture::width [private]
 

Definition at line 37 of file avicapture.h.

Referenced by captureFrame(), getWidth(), and start().


The documentation for this class was generated from the following files:
Generated on Sat Jan 14 22:33:06 2006 for Celestia by  doxygen 1.4.1