00001
00002
00003
00004
00005
00006
00007
00008 #include "aviplayer.h"
00009
00010
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
00018 AviPlayer::AviPlayer(char* video, int newWidth, int newHeight)
00019 {
00020
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();
00032
00033 OpenAVI(video);
00034 }
00035
00036
00037 AviPlayer::~AviPlayer()
00038 {
00039 CloseAVI();
00040 }
00041
00042
00043 void AviPlayer::OpenAVI(LPCSTR szFile)
00044 {
00045 TCHAR title[100];
00046
00047 AVIFileInit();
00048
00049
00050 if (AVIStreamOpenFromFile(&pavi, szFile, streamtypeVIDEO, 0, OF_READ, NULL) !=0)
00051 {
00052
00053 MessageBox (HWND_DESKTOP, "Failed To Open The AVI Stream", "Error", MB_OK | MB_ICONEXCLAMATION);
00054 }
00055
00056 AVIStreamInfo(pavi, &psi, sizeof(psi));
00057 width=psi.rcFrame.right-psi.rcFrame.left;
00058 height=psi.rcFrame.bottom-psi.rcFrame.top;
00059
00060 lastframe=AVIStreamLength(pavi);
00061
00062 mpf=AVIStreamSampleToTime(pavi,lastframe)/lastframe;
00063
00064 bmih.biSize = sizeof (BITMAPINFOHEADER);
00065 bmih.biPlanes = 1;
00066 bmih.biBitCount = 24;
00067 bmih.biWidth = textureWidth;
00068 bmih.biHeight = textureHeight;
00069 bmih.biCompression = BI_RGB;
00070
00071 hBitmap = CreateDIBSection (hdc, (BITMAPINFO*)(&bmih), DIB_RGB_COLORS, (void**)(&data), NULL, NULL);
00072 SelectObject (hdc, hBitmap);
00073
00074 pgf=AVIStreamGetFrameOpen(pavi, NULL);
00075 if (pgf==NULL)
00076 {
00077
00078 MessageBox (HWND_DESKTOP, "Failed To Open The AVI Frame", "Error", MB_OK | MB_ICONEXCLAMATION);
00079 }
00080 }
00081
00082
00083 void AviPlayer::GrabAVIFrame(int frame)
00084 {
00085 LPBITMAPINFOHEADER lpbi;
00086 lpbi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(pgf, frame);
00087 pdata=(char *)lpbi+lpbi->biSize+lpbi->biClrUsed * sizeof(RGBQUAD);
00088
00089
00090 DrawDibDraw (hdd, hdc, 0, 0, textureWidth, textureHeight, lpbi, pdata, 0, 0, width, height, 0);
00091
00092 flipIt(data);
00093
00094
00095 glTexSubImage2D (GL_TEXTURE_2D, 0, 0, 0, textureWidth, textureHeight, GL_RGB, GL_UNSIGNED_BYTE, data);
00096 }
00097
00098
00099 void AviPlayer::CloseAVI()
00100 {
00101 DeleteObject(hBitmap);
00102 DrawDibClose(hdd);
00103 AVIStreamGetFrameClose(pgf);
00104 AVIStreamRelease(pavi);
00105 AVIFileExit();
00106 }
00107
00108
00109 void AviPlayer::Update()
00110 {
00111 DWORD milliseconds;
00112
00113
00114 DWORD tickCount = GetTickCount();
00115 if (lastTickCount == -1) {
00116 milliseconds = 0;
00117 }
00118 else {
00119 milliseconds = tickCount - lastTickCount;
00120 }
00121 lastTickCount = tickCount;
00122
00123
00124 next+=milliseconds;
00125 frame=next/mpf;
00126
00127 if (frame >= lastframe)
00128 {
00129 if (repeatMovie) {
00130 frame=0;
00131 next=0;
00132 }
00133 else {
00134 frame=lastframe - 1;
00135 }
00136 }
00137 }
00138
00139
00140 void AviPlayer::Draw(float x, float y, float width, float height)
00141 {
00142 glEnable(GL_TEXTURE_2D);
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);
00149 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
00150 glEnable(GL_LIGHTING);
00151 glShadeModel(GL_SMOOTH);
00152
00153
00154 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, textureWidth, textureHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
00155
00156 GrabAVIFrame(frame);
00157
00158
00159 glBegin(GL_QUADS);
00160
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();
00166
00167 glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
00168
00169 glDisable(GL_TEXTURE_2D);
00170 }
00171
00172
00173 void AviPlayer::flipIt(void* buffer)
00174 {
00175 void* b = buffer;
00176 DWORD theSize = textureSize;
00177
00178 __asm
00179 {
00180 mov ecx, theSize
00181 mov ebx, b
00182 label:
00183 mov al,[ebx+0]
00184 mov ah,[ebx+2]
00185 mov [ebx+2],al
00186 mov [ebx+0],ah
00187
00188 add ebx,3
00189 dec ecx
00190 jnz label
00191 }
00192 }
00193
00194
00195 #else // _WIN32 end
00196
00197
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