-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAviReader.cpp
More file actions
251 lines (203 loc) · 4.98 KB
/
AviReader.cpp
File metadata and controls
251 lines (203 loc) · 4.98 KB
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
// $Id: AviReader.cpp,v 1.3 2008/04/24 23:41:24 samn Exp $
#include "stdafx.h"
#include "AviReader.h"
#include "Log.h"
bool AVIReader::bInitLib = false;
int AVIReader::iReaders = 0;
AVIReader::AVIReader()
{
iReaders++;
if(!bInitLib)
{
AVIFileInit();// Opens The AVIFile Library
bInitLib = true;
}
m_iCurrFrame = 0;
m_iLastFrame = 0;
m_iFirstFrame = 0;
m_iHeight = 0;
m_iWidth = 0;
m_dMPF = 0.0;
m_pAVIStream = 0;
m_pAVIFile = 0;
m_pGetFrame = 0;
m_bOpen = false;
}
AVIReader::~AVIReader()
{
Close();
iReaders--;
if(iReaders==0)
{
AVIFileExit(); // Release The File
}
}
bool AVIReader::IsOpen()
{
return m_bOpen;
}
bool AVIReader::Open(const char* fname)
{
m_bOpen = false;
int res = 0;
// Opens The AVI Stream
res = AVIStreamOpenFromFile(&m_pAVIStream, fname, streamtypeVIDEO, 0, OF_READ, NULL);
if(res!=AVIERR_OK)
{ //an error occurs
Close();
return false;
}
// Reads Information About The Stream Into psi
AVIStreamInfo(m_pAVIStream, &m_oStreamInfo, sizeof(m_oStreamInfo));
// Width Is Right Side Of Frame Minus Left
m_iWidth=m_oStreamInfo.rcFrame.right-m_oStreamInfo.rcFrame.left;
// Height Is Bottom Of Frame Minus Top
m_iHeight=m_oStreamInfo.rcFrame.bottom-m_oStreamInfo.rcFrame.top;
// The First Frame Of The Stream
m_iFirstFrame=AVIStreamStart(m_pAVIStream);
if(m_iFirstFrame==-1)
{ //Error getting the frame inside the stream
Close();
return false;
}
// The Last Frame Of The Stream
m_iLastFrame=AVIStreamLength(m_pAVIStream);
if(m_iLastFrame == -1)
{
Close();
return false;
}
// Calculate Rough Milliseconds Per Frame
m_dMPF=AVIStreamSampleToTime(m_pAVIStream,m_iLastFrame)/static_cast<double>(m_iLastFrame);
//m_dMPF = 1000.0 / (m_oStreamInfo.dwRate / (double)(m_oStreamInfo.dwScale?m_oStreamInfo.dwScale:1.0));
try
{
// Create The PGETFRAME Using Our Request Mode
m_pGetFrame=AVIStreamGetFrameOpen(m_pAVIStream, NULL);
if (m_pGetFrame==NULL)
{ // An Error Occurred Opening The Frame
//MessageBox (HWND_DESKTOP, "Failed To Open The AVI Frame", "Error", MB_OK | MB_ICONEXCLAMATION);
return false;
}
}
catch(...)
{
Write2Log("AVIReader::Open: Exception !!");
return false;
}
m_bOpen = true;
return true;
}
bool AVIReader::Close()
{
if(m_pAVIFile)
AVIFileRelease(m_pAVIFile);
// Deallocates The GetFrame Resources
if(m_pGetFrame)
AVIStreamGetFrameClose(m_pGetFrame);
// Release The Stream
if(m_pAVIStream)
AVIStreamRelease(m_pAVIStream);
m_iCurrFrame = 0;
m_iFirstFrame = 0;
m_iLastFrame = 0;
m_iHeight = 0;
m_iWidth = 0;
m_dMPF = 0.0;
m_pAVIStream = 0;
m_pGetFrame = 0;
m_bOpen = false;
return true;
}
BOOL CreateFromPackedDIBPointer(LPBYTE pDIB, int iFrame)
{
ASSERT(pDIB!=NULL);
//Creates a full-color (no palette) DIB from a pointer to a
//full-color memory DIB
//get the BitmapInfoHeader
BITMAPINFOHEADER bih;
RtlMoveMemory(&bih.biSize, pDIB, sizeof(BITMAPINFOHEADER));
//now get the bitmap bits
if (bih.biSizeImage < 1)
{
return FALSE;
}
BYTE* Bits=new BYTE;
RtlMoveMemory(Bits, pDIB + sizeof(BITMAPINFOHEADER), bih.biSizeImage);
//and BitmapInfo variable-length UDT
BYTE memBitmapInfo[40];
RtlMoveMemory(memBitmapInfo, &bih, sizeof(bih));
BITMAPFILEHEADER bfh;
bfh.bfType=19778; //BM header
bfh.bfSize=55 + bih.biSizeImage;
bfh.bfReserved1=0;
bfh.bfReserved2=0;
bfh.bfOffBits=sizeof(BITMAPINFOHEADER) + sizeof(BITMAPFILEHEADER); //54
CString FileName;
FileName.Format("Frame-%05d.bmp", iFrame);
FILE* fp=fopen(FileName, "wb");
if (fp!=NULL)
{
fwrite(&bfh, sizeof(bfh), 1, fp);
fwrite(&memBitmapInfo, sizeof(memBitmapInfo), 1, fp);
fwrite(Bits, bih.biSizeImage, 1, fp);
fclose(fp);
}
else
{
TRACE0(_T("Error writing the bitmap file"));
return FALSE;
}
delete [] Bits;
return TRUE;
}
LPBITMAPINFOHEADER AVIReader::GetFrame(int iFrame)
{
if(iFrame < m_iFirstFrame || iFrame >= m_iLastFrame)
return 0;
// Grab Data From The AVI Stream
return (LPBITMAPINFOHEADER)AVIStreamGetFrame(m_pGetFrame, iFrame);
}
LPBITMAPINFOHEADER AVIReader::GetCurrentFrame()
{
return GetFrame(m_iCurrFrame);
}
int AVIReader::FirstFrame()
{
return m_iFirstFrame;
}
int AVIReader::LastFrame()
{
return m_iLastFrame;
}
int AVIReader::CurrentFrame()
{
return m_iCurrFrame;
}
bool AVIReader::GoToFrame(int iFrame)
{
if(iFrame < m_iFirstFrame || iFrame > m_iLastFrame)
return false;
m_iCurrFrame = iFrame;
return true;
}
bool AVIReader::FreeFrame(byte*& pFrame)
{
if(!pFrame)
return false;
delete [] pFrame;
pFrame = 0;
return true;
}
int AVIReader::Width()
{
return m_iWidth;
}
int AVIReader::Height()
{
return m_iHeight;
}
double AVIReader::MSPerFrame()
{
return m_dMPF;
}