forked from ChrisVeigl/BrainBay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathob_avi.cpp
364 lines (286 loc) · 9.61 KB
/
ob_avi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* -----------------------------------------------------------------------------
BrainBay Version 1.7, GPL 2003-2010, contact: [email protected]
MODULE: OB_AVI.CPP: functions for the AVI-Player-Object
Author: Chris Veigl
This Object can open a standard avi-file, grab a frame
and display it in a sizeable window. the frame number is given to the object's
input port. Sound of the AVI-File is not processed.
Inspired by Jeff Molofee's Lesson 35: Playing AVI-Files with OpenGl
http://nehe.gamedev.net
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; See the
GNU General Public License for more details.
-----------------------------------------------------------------------------*/
#include "brainBay.h"
#include "ob_avi.h"
LRESULT CALLBACK AVIDlgHandler(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
char szFileName[MAX_PATH];
AVIOBJ * st;
st = (AVIOBJ *) actobject;
if ((st==NULL)||(st->type!=OB_AVI)) return(FALSE);
switch( message )
{
case WM_INITDIALOG:
SetDlgItemText(hDlg, IDC_AVIFILE, st->avifile);
return TRUE;
case WM_CLOSE:
EndDialog(hDlg, LOWORD(wParam));
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case IDC_LOADAVI:
strcpy(szFileName,GLOBAL.resourcepath);
strcat(szFileName,"MOVIES\\*.avi");
if (open_file_dlg(hDlg, szFileName, FT_AVI, OPEN_LOAD))
{
if (!st->OpenAVI(szFileName))
report_error("Could not load AVI File");
else
{
// reduce_filepath(szFileName,szFileName);
strcpy(st->avifile,szFileName);
SetDlgItemText(hDlg, IDC_AVIFILE, st->avifile);
if (st->displayWnd) InvalidateRect(st->displayWnd,NULL,TRUE);
}
}
InvalidateRect(hDlg,NULL,FALSE);
break;
}
break;
case WM_SIZE:
case WM_MOVE: update_toolbox_position(hDlg);
break;
}
return FALSE;
}
LRESULT CALLBACK AVIWndHandler(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int t;
AVIOBJ * st;
st=NULL;
for (t=0;(t<GLOBAL.objects)&&(st==NULL);t++)
if (objects[t]!=NULL)
if (objects[t]->type==OB_AVI)
{ st=(AVIOBJ *)objects[t];
if (st!=NULL) if (st->displayWnd!=hWnd) st=NULL;
}
if (st!=NULL)
switch( message )
{
case WM_DESTROY:
Shutdown_GL(st->GLRC);
break;
case WM_KEYDOWN:
SendMessage(ghWndMain, message,wParam,lParam);
break;
case WM_ACTIVATE:
if (wParam==WA_CLICKACTIVE)
{
close_toolbox();
actobject=st;
// actobject->make_dialog();
SetActiveWindow(hWnd);
SetFocus(hWnd);
}
break;
case WM_COMMAND:
break;
case WM_SIZE:
// Size_GL(hWnd, st->GLRC,1);
case WM_MOVE:
{
WINDOWPLACEMENT wndpl;
GetWindowPlacement(st->displayWnd, &wndpl);
st->top=wndpl.rcNormalPosition.top;
st->left=wndpl.rcNormalPosition.left;
st->right=wndpl.rcNormalPosition.right;
st->bottom=wndpl.rcNormalPosition.bottom;
}
InvalidateRect(st->displayWnd,NULL,FALSE);
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hDC = BeginPaint(hWnd, &ps);
if (strcmp(st->avifile,"none"))
{
wglMakeCurrent(hDC, st->GLRC);
//glLoadIdentity (); // Reset The Modelview Matrix
//glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
st->GrabAVIFrame(st->frame,hDC); // Grab A Frame From The AVI
wglMakeCurrent(0, 0);
}
EndPaint(hWnd, &ps);
}
break;
default:
return DefWindowProc( hWnd, message, wParam, lParam );
} else return DefWindowProc( hWnd, message, wParam, lParam );
return 0;
}
//
// Object Implementation
//
AVIOBJ::AVIOBJ(int num) : BASE_CL()
{
inports = 1;
outports = 0;
strcpy(in_ports[0].in_name,"in");
left=50;right=300;top=330;bottom=580;
input=0;data=0;
frame=0;lastframe=0;
strcpy(avifile,"none");
displayWnd=create_AVI_Window(left,right,top,bottom,&GLRC);
strcpy(avifile,"none");
}
HWND AVIOBJ::create_AVI_Window(int left,int right, int top, int bottom,HGLRC * GLRC)
{
HWND Wnd;
char temp[50];
HDC hDC;
HGLRC m_hRC;
PIXELFORMATDESCRIPTOR pfd;
int nPixelFormat;
wsprintf(temp, "Avi - Movie");
if (!(Wnd= CreateWindow( "AVIClass", temp, WS_CLIPSIBLINGS| WS_CHILD | WS_CAPTION | WS_THICKFRAME | WS_VISIBLE , left, top,right-left,bottom-top, ghWndMain, NULL, hInst, NULL)))
{ report_error("Can't Create AVI Window");
return(0);
}
memset(&pfd, 0, sizeof(pfd));
pfd.nSize = sizeof(pfd);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 24;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;
// Set pixel format
hDC = GetDC(Wnd);
nPixelFormat = ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, nPixelFormat, &pfd);
m_hRC = wglCreateContext(hDC);
wglMakeCurrent(hDC, m_hRC);
hdc = hDC;
hdd = DrawDibOpen(); // Grab A Device Context For Our Dib
glClearColor (0.0f, 0.0f, 0.0f, 0.5f); // Black Background
// Select Smooth Shading
glHint (GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Set Perspective Calculations To Most Accurate
glViewport (0, 0, (GLsizei)(width), (GLsizei)(height)); // Reset The Current Viewport
glMatrixMode (GL_PROJECTION); // Select The Projection Matrix
glLoadIdentity (); // Reset The Projection Matrix
gluPerspective (45.0f, (GLfloat)(width)/(GLfloat)(height), // Calculate The Aspect Ratio Of The Window
1.0f, 100.0f);
glMatrixMode (GL_MODELVIEW); // Select The Modelview Matrix
*GLRC=m_hRC;
ReleaseDC(Wnd, hDC);
ShowWindow(Wnd,1);
UpdateWindow(Wnd);
InvalidateRect(Wnd,NULL,TRUE);
return(Wnd);
}
int AVIOBJ::OpenAVI(LPCSTR szFile) // Opens An AVI File (szFile)
{
AVIFileInit(); // Opens The AVIFile Library
pgf=NULL;
if (AVIStreamOpenFromFile(&pavi, szFile, streamtypeVIDEO, 0, OF_READ, NULL) !=0)
return(0);
AVIStreamInfo(pavi, &psi, sizeof(psi)); // Reads Information About The Stream Into psi
width=psi.rcFrame.right-psi.rcFrame.left; // Width Is Right Side Of Frame Minus Left
height=psi.rcFrame.bottom-psi.rcFrame.top; // Height Is Bottom Of Frame Minus Top
lastframe=AVIStreamLength(pavi); // The Last Frame Of The Stream
mpf=AVIStreamSampleToTime(pavi,lastframe)/lastframe; // Calculate Rough Milliseconds Per Frame
bmih.biSize = sizeof (BITMAPINFOHEADER); // Size Of The BitmapInfoHeader
bmih.biPlanes = 1; // Bitplanes
bmih.biBitCount = 24; // Bits Format We Want (24 Bit, 3 Bytes)
bmih.biWidth = width; // Width We Want
bmih.biHeight = height; // Height We Want
bmih.biCompression = BI_RGB; // Requested Mode = RGB
hBitmap = CreateDIBSection (hdc, (BITMAPINFO*)(&bmih), DIB_RGB_COLORS, (void**)(&data), NULL, 0);
SelectObject (hdc, hBitmap); // Select hBitmap Into Our Device Context (hdc)
pgf=AVIStreamGetFrameOpen(pavi, NULL); // Create The PGETFRAME Using Our Request Mode
if (pgf==NULL) return(0);
return(1);
}
void AVIOBJ::GrabAVIFrame(int frame,HDC nhdc) // Grabs A Frame From The Stream
{
LPBITMAPINFOHEADER lpbi; // Holds The Bitmap Header Information
if (pgf)
{
lpbi = (LPBITMAPINFOHEADER)AVIStreamGetFrame(pgf, frame); // Grab Data From The AVI Stream
if (lpbi)
{
pdata=(char *)lpbi+lpbi->biSize+lpbi->biClrUsed * sizeof(RGBQUAD); // Pointer To Data Returned By AVIStreamGetFrame
DrawDibDraw (hdd, nhdc, 0, 0, right-left,bottom-top, lpbi, pdata, 0, 0, width, height, 0);
}
}
}
void AVIOBJ::CloseAVI(void) // Properly Closes The Avi File
{
DeleteObject(hBitmap); // Delete The Device Dependant Bitmap Object
if (hdd) DrawDibClose(hdd); // Closes The DrawDib Device Context
if (pgf)
{
AVIStreamGetFrameClose(pgf); // Deallocates The GetFrame Resources
AVIStreamRelease(pavi); // Release The Stream
AVIFileExit(); // Release The File
}
}
void AVIOBJ::make_dialog(void)
{
display_toolbox(hDlg=CreateDialog(hInst, (LPCTSTR)IDD_AVIPROPBOX, ghWndStatusbox, (DLGPROC)AVIDlgHandler));
}
void AVIOBJ::load(HANDLE hFile)
{
load_object_basics(this);
load_property("avi-file",P_STRING,avifile);
load_property("wnd-top",P_INT,&top);
load_property("wnd-bottom",P_INT,&bottom);
load_property("wnd-left",P_INT,&left);
load_property("wnd-right",P_INT,&right);
if (strcmp(avifile,"none"))
{
char szFileName[MAX_PATH] = "";
strcpy(szFileName,avifile);
if (!OpenAVI (szFileName))
{
strcpy(szFileName,"could not Open AVI-File: ");
strcat(szFileName,avifile);
report_error ( szFileName);
}
}
MoveWindow(displayWnd,left,top,right-left,bottom-top,TRUE);
}
void AVIOBJ::save(HANDLE hFile)
{
save_object_basics(hFile,this);
save_property(hFile,"avi-file",P_STRING,avifile);
save_property(hFile,"wnd-top",P_INT,&top);
save_property(hFile,"wnd-bottom",P_INT,&bottom);
save_property(hFile,"wnd-left",P_INT,&left);
save_property(hFile,"wnd-right",P_INT,&right);
}
void AVIOBJ::incoming_data(int port, float value)
{
input=value;
}
void AVIOBJ::work(void)
{
if (GLOBAL.fly) return;
frame=(long) (lastframe*((input-in_ports[0].in_min)/(in_ports[0].in_max-in_ports[0].in_min))); // Calculate The Current Frame
if (frame<0) frame=0;
if (frame>=lastframe) // Are We At Or Past The Last Frame?
frame=lastframe; // Reset The Frame Back To Zero (Start Of Video)
if (frame!=old_frame )
{
old_frame=frame;
InvalidateRect(displayWnd,NULL,FALSE);
}
}
AVIOBJ::~AVIOBJ()
{
CloseAVI(); // Close The AVI File
DestroyWindow(displayWnd); displayWnd=NULL;
}