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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
|
#if !defined(NSV_HPP)
#define NSV_HPP
//______________________________________________________________________________
//
// NSV.hpp
// NSV File I/O Classes
#include <FourCC.hpp>
#include <exception>
#include <string>
#include <vector>
#include <fstream>
#include <iosfwd>
namespace NSV
{
#if (defined(__POWERPC__) || defined(__APPLE__))
typedef long long INT64;
typedef long long POS_TYPE;
typedef std::ios IOS_BASE;
#elif (defined(__linux) || defined(linux))
typedef long long INT64; // Assumes WIN32
typedef std::ios::pos_type POS_TYPE;
typedef std::ios_base IOS_BASE;
#else
typedef __int64 INT64; // Assumes WIN32
typedef std::ios::pos_type POS_TYPE;
typedef std::ios_base IOS_BASE;
#endif
enum
{
FrameRates = 256
};
double frameRate(int nFrameRate);
int frameRate(unsigned int uiRateNum, unsigned int uiRateDenom);
bool video(FourCC fccNSV);
bool audio(FourCC fccNSV);
FourCC videoFormat(FourCC fccCompression);
FourCC audioFormat(unsigned short wFormatTag);
FourCC videoCompression(FourCC fccNSV);
int videoBitCount(FourCC fccNSV);
unsigned short audioFormatTag(FourCC fccNSV);
//--------------------------------------
class Exception : public std::exception
{
public:
Exception();
Exception(const std::string& strException);
Exception(const Exception& e);
~Exception() throw();
Exception& operator=(const Exception& e);
const char* what() const throw();
private:
std::string m_strException;
};
//--------------------------------------
class IndexEntry
{
public:
IndexEntry(POS_TYPE posOffset = 0, size_t sizeData = -1, bool bKeyFrame = false) :
m_posOffset(posOffset),
m_sizeData(sizeData),
m_bKeyFrame(bKeyFrame)
{
}
//private:
POS_TYPE m_posOffset;
size_t m_sizeData;
bool m_bKeyFrame;
};
typedef std::vector<IndexEntry> Index;
//--------------------------------------
class FileHeader
{
public:
friend std::ostream& operator<<(std::ostream& os, const FileHeader& fh);
FileHeader() :
m_fccSignature(0UL),
m_sizeHeader(0),
m_sizeFile(0),
m_iFileSize_ms(0),
m_sizeMetaData(0),
m_nTOCAlloc(0),
m_nTOCSize(0),
m_strMetaData(),
m_index()
{
}
//private:
FourCC m_fccSignature;
size_t m_sizeHeader;
size_t m_sizeFile;
int m_iFileSize_ms;
size_t m_sizeMetaData;
int m_nTOCAlloc;
int m_nTOCSize;
std::string m_strMetaData;
std::vector<size_t> m_index;
};
//--------------------------------------
class FrameHeader
{
public:
friend std::ostream& operator<<(std::ostream& os, const FrameHeader& fh);
FrameHeader() :
m_fccSignature(0UL),
m_fccVideo(0UL),
m_fccAudio(0UL),
m_iWidth(0),
m_iHeight(0),
m_iFrameRate(0),
m_iSyncOffset_ms(0),
m_bKeyFrame(false)
{
}
//private:
FourCC m_fccSignature;
FourCC m_fccVideo;
FourCC m_fccAudio;
int m_iWidth;
int m_iHeight;
int m_iFrameRate;
int m_iSyncOffset_ms;
bool m_bKeyFrame;
};
class Stream;
//--------------------------------------
class File
{
friend std::ostream& operator<<(std::ostream& os, const File& f);
public:
File();
~File();
// File header
void header(const std::string& strMetaData, int nIndexEntries);
void metaData(const std::string& strMetaData);
const std::string& metaData() const;
void indexEntry(int nEntry, POS_TYPE posOffset, size_t sizeData = -1, bool bKeyFrame = false);
void appendIndexEntry(POS_TYPE posOffset, size_t sizeData = -1, bool bKeyFrame = false);
Index& index(); // Relative index
int indexEntriesUsed() const;
// Synchronization frame header
FourCC videoFormat() const;
FourCC audioFormat() const;
void size(int iWidth, int iHeight);
int width()const;
int height() const;
void frameRate(unsigned char ucFrameRate);
unsigned char frameRate() const;
void rate(unsigned int uiRateNum, unsigned int uiRateDenom);
unsigned int rateNum() const;
unsigned int rateDenom() const;
void syncOffset(int iSyncOffset_ms);
int syncOffset() const;
void finalizeSyncOffset();
void frames(INT64 nFrames);
INT64 frames() const;
Stream& newStream(FourCC fccDataType, unsigned int uiRateNum = 0, unsigned int uiRateDenom = 0, INT64 nSamples = -1);
int streams() const;
int streamsAux() const;
Stream& stream(int nStream);
const Stream& stream(int nStream) const;
int streamVideo() const;
int streamAudio() const;
int streamAux(int nAux) const;
void dataOffset(POS_TYPE posDataOffset);
POS_TYPE dataOffset();
private:
File(const File& f); // Not implemented
File& operator=(const File& f); // Not implemented
std::string m_strMetaData;
Index m_index;
int m_nIndexEntriesUsed;
int m_iWidth;
int m_iHeight;
unsigned int m_uiRateNum;
unsigned int m_uiRateDenom;
unsigned char m_ucFrameRate;
int m_iSyncOffset_ms;
std::vector<Stream> m_vStream;
POS_TYPE m_posDataOffset;
int m_nStreamVideo;
int m_nStreamAudio;
INT64 m_nFrames;
};
//--------------------------------------
class Stream
{
friend std::ostream& operator<<(std::ostream& os, const Stream& s);
public:
Stream(File* pFile, int nStream, FourCC fccDataType, unsigned int uiRateNum, unsigned int uiRateDenom, INT64 nSamples);
Stream(const Stream& s);
~Stream();
Stream& operator=(const Stream& s);
void open();
void close();
int stream() const;
FourCC type() const; // Returns AVI types, vids, auds, ...
FourCC dataType() const;
void samples(INT64 nSamples);
INT64 samples() const;
void sample(INT64 nSample);
INT64 sample() const;
void rate(unsigned int uiRateNum, unsigned int uiRateDenom);
unsigned int rateNum() const;
unsigned int rateDenom() const;
void dataSize(size_t sizeData);
void data(const unsigned char* pData, size_t sizeData);
unsigned char* data();
size_t dataSize() const;
void keyFrame(bool bKeyFrame);
bool keyFrame() const;
private:
File* m_pFile;
int m_nStream;
FourCC m_fccDataType;
INT64 m_nSamples;
INT64 m_nSample;
unsigned int m_uiRateNum;
unsigned int m_uiRateDenom;
unsigned char* m_pBuffer;
size_t m_sizeBuffer;
size_t m_sizeData;
bool m_bKeyFrame;
};
/*
//--------------------------------------
class Reader
{
public:
Reader(File& f);
Reader(File& f, const std::string& strFile);
~Reader();
void open(const std::string& strFile);
void open();
void close();
File& file();
const std::string& fileName() const;
void readFileHeader();
void readFrame();
void readFrameInfo();
void readFrameHeader();
void readPayload();
void readPayloadInfo();
void readPayloadHeader(int& nAux, int& iAuxPlusVideo, int& iAudio);
INT64 frames() const;
INT64 frame() const;
void seek(INT64 nFrame);
void readSample(int nStream, unsigned char* pData, size_t sizeDataMax, size_t& sizeData, bool& bKeyFrame);
bool eof();
const FileHeader& fileHeader() const;
const FrameHeader& frameHeader() const;
private:
Reader(const Reader& r); // Not implemented
Reader& operator=(const Reader& r); // Not implemented
short read_i16();
unsigned short read_ui16();
int read_i32();
unsigned int read_ui32();
File& m_file;
std::string m_strFile;
std::ifstream m_ifs;
FileHeader m_fileHeader;
FrameHeader m_frameHeader;
bool m_bFrameHeader;
INT64 m_nFrame;
};
*/
//--------------------------------------
class Writer
{
public:
Writer(File& f);
Writer(File& f, const std::string& strFile);
~Writer();
void open(const std::string& strFile);
void open();
void close();
File& file();
const std::string& fileName() const;
INT64 frame() const;
void seek(POS_TYPE posOffset);
void header(const std::string& strMetaData, int nIndexEntries);
void sample(int nStream, const unsigned char* pData, size_t sizeData, bool bKeyframe);
void writeFrame();
private:
Writer(const Writer& w); // Not implemented
Writer& operator=(const Writer& w); // Not implemented
void writeFileHeader();
void writeFrameHeader(bool bKeyFrame);
void writePayloadHeader();
void write_i16(short i16);
void write_ui16(unsigned short ui16);
void write_i32(int i32);
void write_ui32(unsigned int ui32);
File& m_file;
std::string m_strFile;
std::ofstream m_ofs;
INT64 m_nFrame;
bool m_bKeyFrame;
};
} // namespace NSV
#endif // NSV_HPP
|