/Users/craigcornelius/Projects/SPRING Mac Release 0.2/aviplayer.cpp

Go to the documentation of this file.
00001 // $Id: aviplayer.cpp,v 1.8 2006/03/01 19:54:18 hawaii Exp $ 
00002 // $Copyright: (c)2001 National Biocomputation Center, Stanford University $
00003 
00004 //
00005 // SEE aviplayer.h for further info! 
00006 //
00007 
00008 #include "aviplayer.h"
00009 
00010 // globals
00011 int AviPlayer::debug = 0;
00012 const char* AviPlayer::rcsid = "@(#) $Id: aviplayer.cpp,v 1.8 2006/03/01 19:54:18 hawaii Exp $ $Copyright: (c)2001 National Biocomputation Center, Stanford University $";
00013 
00014 
00015 #ifdef _WIN32
00016 
00017 // Initializes this object
00018 AviPlayer::AviPlayer(char* video, int newWidth, int newHeight)          // Any GL Init Code & User Initialiazation Goes Here
00019 {
00020         // Start Of User Initialization
00021         repeatMovie = false;
00022         textureWidth = newWidth;
00023         textureHeight = newHeight;
00024         textureSize = textureWidth*textureHeight;
00025 
00026         lastTickCount = -1;
00027         frame = 0;
00028         next = 0;
00029         data = 0;
00030         hdc = CreateCompatibleDC(0);
00031         hdd = DrawDibOpen();                                                                            // Grab A Device Context For Our Dib
00032 
00033         OpenAVI(video);
00034 }
00035 
00036 // Destroys this object
00037 AviPlayer::~AviPlayer()                                                                                 // Any User DeInitialization Goes Here
00038 {
00039         CloseAVI();                                                                                                     // Close The AVI File
00040 }
00041 
00042 // Opens an AVI file
00043 void AviPlayer::OpenAVI(LPCSTR szFile)                                                  // Opens An AVI File (szFile)
00044 {
00045         TCHAR   title[100];                                                                                     // Will Hold The Modified Window Title
00046 
00047         AVIFileInit();                                                                                          // Opens The AVIFile Library
00048 
00049         // Opens The AVI Stream
00050         if (AVIStreamOpenFromFile(&pavi, szFile, streamtypeVIDEO, 0, OF_READ, NULL) !=0)
00051         {
00052                 // An Error Occurred Opening The Stream
00053                 MessageBox (HWND_DESKTOP, "Failed To Open The AVI Stream", "Error", MB_OK | MB_ICONEXCLAMATION);
00054         }
00055 
00056         AVIStreamInfo(pavi, &psi, sizeof(psi));                                         // Reads Information About The Stream Into psi
00057         width=psi.rcFrame.right-psi.rcFrame.left;                                       // Width Is Right Side Of Frame Minus Left
00058         height=psi.rcFrame.bottom-psi.rcFrame.top;                                      // Height Is Bottom Of Frame Minus Top
00059 
00060         lastframe=AVIStreamLength(pavi);                                                        // The Last Frame Of The Stream
00061 
00062         mpf=AVIStreamSampleToTime(pavi,lastframe)/lastframe;            // Calculate Rough Milliseconds Per Frame
00063 
00064         bmih.biSize = sizeof (BITMAPINFOHEADER);                                        // Size Of The BitmapInfoHeader
00065         bmih.biPlanes = 1;                                                                                      // Bitplanes    
00066         bmih.biBitCount = 24;                                                                           // Bits Format We Want (24 Bit, 3 Bytes)
00067         bmih.biWidth = textureWidth;                                                            // Width We Want (256 Pixels)
00068         bmih.biHeight = textureHeight;                                                          // Height We Want (256 Pixels)
00069         bmih.biCompression = BI_RGB;                                                            // Requested Mode = RGB
00070 
00071         hBitmap = CreateDIBSection (hdc, (BITMAPINFO*)(&bmih), DIB_RGB_COLORS, (void**)(&data), NULL, NULL);
00072         SelectObject (hdc, hBitmap);                                                            // Select hBitmap Into Our Device Context (hdc)
00073 
00074         pgf=AVIStreamGetFrameOpen(pavi, NULL);                                          // Create The PGETFRAME Using Our Request Mode
00075         if (pgf==NULL)
00076         {
00077                 // An Error Occurred Opening The Frame
00078                 MessageBox (HWND_DESKTOP, "Failed To Open The AVI Frame", "Error", MB_OK | MB_ICONEXCLAMATION);
00079         }
00080 }
00081 
00082 // Grabs an AVI frame
00083 void AviPlayer::GrabAVIFrame(int frame)                                                                 // Grabs A Frame From The Stream
00084 {
00085         LPBITMAPINFOHEADER lpbi;                                                                        // Holds The Bitmap Header Information
00086         lpbi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(pgf, frame);       // Grab Data From The AVI Stream
00087         pdata=(char *)lpbi+lpbi->biSize+lpbi->biClrUsed * sizeof(RGBQUAD);      // Pointer To Data Returned By AVIStreamGetFrame
00088 
00089         // Convert Data To Requested Bitmap Format
00090         DrawDibDraw (hdd, hdc, 0, 0, textureWidth, textureHeight, lpbi, pdata, 0, 0, width, height, 0);
00091 
00092         flipIt(data);                                                                                           // Swap The Red And Blue Bytes (GL Compatability)
00093 
00094         // Update The Texture
00095         glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, GL_RGB, GL_UNSIGNED_BYTE, data);
00096 }
00097 
00098 // Closes an AVI video
00099 void AviPlayer::CloseAVI()                                                                              // Properly Closes The Avi File
00100 {
00101         DeleteObject(hBitmap);                                                                          // Delete The Device Dependant Bitmap Object
00102         DrawDibClose(hdd);                                                                                      // Closes The DrawDib Device Context
00103         AVIStreamGetFrameClose(pgf);                                                            // Deallocates The GetFrame Resources
00104         AVIStreamRelease(pavi);                                                                         // Release The Stream
00105         AVIFileExit();                                                                                          // Release The File
00106 }
00107 
00108 // Internal update for frame
00109 void AviPlayer::Update()                                                                                // Perform Motion Updates Here
00110 {
00111         DWORD milliseconds;
00112 
00113         // update tick count and calculate milliseconds that passed
00114         DWORD tickCount = GetTickCount();
00115         if (lastTickCount == -1) {
00116                 milliseconds = 0;
00117         }
00118         else {
00119                 milliseconds = tickCount - lastTickCount;
00120         }
00121         lastTickCount = tickCount;
00122 
00123         // update frame to show
00124         next+=milliseconds;                                                                                     // Increase next Based On The Timer
00125         frame=next/mpf;                                                                                         // Calculate The Current Frame
00126 
00127         if (frame >= lastframe)                                                                         // Are We At Or Past The Last Frame?
00128         {
00129                 if (repeatMovie) {
00130                         frame=0;                                                                                                // Reset The Frame Back To Zero (Start Of Video)
00131                         next=0;                                                                                                 // Reset The Animation Timer (next)
00132                 }
00133                 else {
00134                         frame=lastframe - 1;
00135                 }
00136         }
00137 }
00138 
00139 // Draws to the screen: x & y are lower-left corner
00140 void AviPlayer::Draw(float x, float y, float width, float height)   // Draw Our Scene
00141 {
00142         glEnable(GL_TEXTURE_2D);                                                                        // Enable Texture Mapping
00143 
00144     glPixelStorei(GL_UNPACK_ALIGNMENT, 4); 
00145     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
00146     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
00147     glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
00148         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);       // Linear Filtering
00149         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);       // Linear Filtering
00150         glEnable(GL_LIGHTING);  
00151         glShadeModel(GL_SMOOTH); 
00152 
00153         // Create The Texture
00154         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00155 
00156         GrabAVIFrame(frame);                                                                            // Grab A Frame From The AVI
00157 
00158         // print onto screen
00159         glBegin(GL_QUADS);                                                                                      // Begin Drawing The Background (One Quad)
00160                 // Front Face
00161                 glTexCoord2f(1.0f, 1.0f); glVertex3f(x + width, y + height, 0.0f);
00162                 glTexCoord2f(0.0f, 1.0f); glVertex3f(x        , y + height, 0.0f);
00163                 glTexCoord2f(0.0f, 0.0f); glVertex3f(x        , y         , 0.0f);
00164                 glTexCoord2f(1.0f, 0.0f); glVertex3f(x + width, y         , 0.0f);
00165         glEnd();                                                                                                    // Done Drawing The Background
00166 
00167     glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00168 
00169         glDisable(GL_TEXTURE_2D);
00170 }
00171 
00172 // Flips buffer for Red<->Blue
00173 void AviPlayer::flipIt(void* buffer)                                                    // Flips The Red And Blue Bytes (256x256)
00174 {
00175         void* b = buffer;                                                                                       // Pointer To The Buffer
00176         DWORD theSize = textureSize;
00177 
00178         __asm                                                                                                           // Assembler Code To Follow
00179         {
00180                 mov ecx, theSize                                                                        // Counter Set To Dimensions Of Our Memory Block
00181                 mov ebx, b                                                                                              // Points ebx To Our Data (b)
00182                 label:                                                                                                  // Label Used For Looping
00183                         mov al,[ebx+0]                                                                          // Loads Value At ebx Into al
00184                         mov ah,[ebx+2]                                                                          // Loads Value At ebx+2 Into ah
00185                         mov [ebx+2],al                                                                          // Stores Value In al At ebx+2
00186                         mov [ebx+0],ah                                                                          // Stores Value In ah At ebx
00187                         
00188                         add ebx,3                                                                                       // Moves Through The Data By 3 Bytes
00189                         dec ecx                                                                                         // Decreases Our Loop Counter
00190                         jnz label                                                                                       // If Not Zero Jump Back To Label
00191         }
00192 }
00193 
00194 
00195 #else // _WIN32 end
00196 
00197 // for everthing else except _WIN32, somebody has to actually implement it!
00198 
00199 AviPlayer::AviPlayer(char* video, int width, int height) { }
00200 
00201 AviPlayer::~AviPlayer() { }
00202 
00203 void AviPlayer::Update() { }
00204 
00205 void AviPlayer::Draw(float x, float y, float width, float height) { }
00206 
00207 
00208 #endif

Generated on Thu Aug 30 11:03:13 2007 for SPRING Mac by  doxygen 1.5.3