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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
|
//=====================================================================
//
// Copyright (c) 1999-2003 On2 Technologies Inc. All Rights Reserved.
//
//---------------------------------------------------------------------
//
// File: $Workfile: AVI.hpp$
//
// Date: $Date: 2010/07/23 19:10:47 $
//
// Revision: $Revision: 1.1 $
//
//---------------------------------------------------------------------
#ifndef AVI_HPP
#define AVI_HPP
#pragma warning(disable:4786)
#include "FourCC.hpp"
#include <exception>
#include <iosfwd>
#include <list>
#include <deque>
#include <string>
#include <vector>
#if defined WIN32
#include <windows.h>
#endif
namespace AVI
{
#if defined WIN32
typedef unsigned __int64 size_type;
typedef DWORD dword;
typedef __int64 offset_t;
typedef unsigned __int32 length_t;
#elif defined LINUX
typedef unsigned long long size_type;
typedef unsigned long dword;
typedef long long offset_t;
typedef unsigned int length_t;
#endif
int asStreamId(const FourCC&);
enum ChunkType
{
waveform,
waveform_encrypted,
DIB_compressed,
DIB_uncompressed,
DIB_encrypted,
kChunkTypeUnknown
};
ChunkType asChunkType(const FourCC&);
const FourCC asChunkId(int stream, ChunkType type);
const FourCC asIndexChunkExId(int stream);
size_type estimatedFileSize(
int width,
int height,
int frameCount);
const std::string offtoa(offset_t);
class FileError : public std::exception
{
public:
FileError(dword messageId);
FileError(const char* message);
~FileError() throw();
const char* what() const throw();
dword id() const;
private:
std::string message;
dword m_id;
};
struct MainHeader
{
enum Flag
{
hasIndex = 0x00000010,
mustUseIndex = 0x00000020,
isInterleaved = 0x00000100,
indexIsAbsolute = 0x00000800, //? "trust cktype"
wasCaptureFile = 0x00010000,
copyrighted = 0x00020000
};
dword microSecPerFrame;
dword maxBytesPerSec;
dword paddingGranularity;
dword flags;
dword totalFrames;
dword initialFrames;
dword streams;
dword suggestedBufferSize;
dword width;
dword height;
dword reserved[4];
const std::string flagsAsStr() const;
};
std::ostream& operator<<(std::ostream&, const MainHeader&);
class Chunk
{
public:
Chunk(const FourCC, length_t, const unsigned char* data = 0);
const FourCC fcc() const;
length_t length() const;
const unsigned char* data() const;
unsigned char* data();
void data(const unsigned char* d);
private:
FourCC m_fcc;
// length_t m_length;
// unsigned char* m_data;
typedef std::vector<unsigned char> data_t;
data_t m_data;
};
std::ostream& operator<<(std::ostream& os, const Chunk&);
typedef std::vector<Chunk> ExtraHeaderVector;
struct Rectangle
{
typedef unsigned short T;
T left;
T top;
T right;
T bottom;
Rectangle()
: left(0), top(0), right(0), bottom(0)
{
}
Rectangle(T l, T t, T r, T b)
: left(l), top(t), right(r), bottom(b)
{
}
};
struct StreamHeader
{
enum Flag
{
disabled = 0x00000001,
formatChanges = 0x00010000
};
FourCC fccType;
FourCC fccHandler;
dword flags;
unsigned short priority;
unsigned short language;
dword initialFrames;
dword scale;
dword rate;
dword start;
dword length;
dword suggestedBufferSize;
long quality;
dword sampleSize;
Rectangle frame;
const std::string flagsAsStr() const;
};
std::ostream& operator<<(std::ostream&, const StreamHeader&);
struct BitmapInfoHeader
{
dword size;
long width;
long height;
unsigned short planes;
unsigned short bitCount;
FourCC compression;
dword sizeImage;
long xPelsPerMeter;
long yPelsPerMeter;
dword clrUsed;
dword clrImportant;
};
std::ostream& operator<<(std::ostream&, const BitmapInfoHeader&);
// namespace Compression
// {
// enum CompressionType
// {
// RGB,
// RLE8,
// RLE4,
// bitfields,
// unknown
// };
//
// bool operator==(CompressionType, const FourCC&);
// bool operator==(const FourCC&, CompressionType);
//
// CompressionType asCompression(const FourCC&);
// const FourCC asFourCC(CompressionType);
//
// std::ostream& operator<<(std::ostream&, CompressionType);
// }
struct PCMWaveFormat
{
unsigned short formatTag;
unsigned short nChannels;
dword samplesPerSec;
dword avgBytesPerSec;
unsigned short blockAlign;
unsigned short bitsPerSample;
};
struct WaveFormatEx : public PCMWaveFormat
{
typedef std::vector<unsigned char> ByteArray;
ByteArray extra;
};
std::ostream& operator<<(std::ostream&, const WaveFormatEx&);
//not currently used; it's for palette changes,
//which isn't implemented yet
struct RGBQuad
{
unsigned char blue;
unsigned char green;
unsigned char red;
unsigned char reserved;
};
struct IndexEntry
{
enum Flags
{
list = 0x00000001,
keyframe = 0x00000010,
notime = 0x00000100,
compuse = 0x0FFF0000
};
FourCC chunkId;
dword flags;
dword chunkOffset;
dword chunkLength;
const std::string flagsAsStr() const;
};
std::ostream& operator<<(std::ostream&, const IndexEntry&);
typedef std::vector<IndexEntry> IEVector;
struct FrameIndexEntry
{
union
{
offset_t offset;
struct
{
unsigned long offset_low;
unsigned long offset_high;
};
};
size_t size;
bool keyframe;
};
typedef std::vector<FrameIndexEntry> FrameIEVector;
typedef std::list<FrameIndexEntry> FrameIEList;
typedef std::deque<FrameIndexEntry> FrameIndex;
struct IndexChunkEx
{
FourCC code;
unsigned long length;
unsigned short longsPerEntry;
unsigned char subtype;
unsigned char type;
unsigned long entriesInUse;
FourCC chunkId;
unsigned long reserved[3];
};
std::ostream& operator<<(std::ostream&, const IndexChunkEx&);
struct StandardIndexChunk
{
FourCC code;
unsigned long length;
unsigned short longsPerEntry;
unsigned char subtype;
unsigned char type;
unsigned long entriesInUse;
FourCC chunkId;
unsigned long baseOffset_low;
unsigned long baseOffset_high;
unsigned long reserved;
struct Entry
{
unsigned long offset;
unsigned long size;
} index[1];
};
std::ostream& operator<<(std::ostream&, const StandardIndexChunk&);
std::ostream& operator<<(std::ostream&, const StandardIndexChunk::Entry&);
struct SuperIndexChunk
{
FourCC code;
unsigned long length;
unsigned short longsPerEntry;
unsigned char subtype;
unsigned char type;
unsigned long entriesInUse;
FourCC chunkId;
unsigned long reserved[3];
struct Entry
{
offset_t offset;
unsigned long size;
unsigned long duration;
} index[1];
};
std::ostream& operator<<(std::ostream&, const SuperIndexChunk&);
std::ostream& operator<<(std::ostream&, const SuperIndexChunk::Entry&);
class File
{
public:
enum mode_t {in, out, inout};
enum OutputType
{
OT_AVI,
OT_On2
};
File();
File(const char* name, mode_t mode);
~File();
void open(const char* name, mode_t mode, dword flags = 0);
void outputType(OutputType ot);
void close();
bool isOpen() const;
mode_t mode() const;
const char* name() const;
void mapinit();
void mapfinal();
unsigned long map(
offset_t offset,
length_t size,
unsigned char*& base,
length_t& baseSize,
length_t& offsetInView) const;
void unmap(unsigned char* base, length_t size) const;
const MainHeader& mainHeader() const;
MainHeader& mainHeader();
int extraHeaderCount() const;
const Chunk& extraHeader(int nChunk) const;
void extraHeader(FourCC fcc, length_t length, unsigned char* data);
void extraHeader(int nStream, FourCC fcc, length_t length, unsigned char* data);
dword totalFrames() const;
dword& totalFrames();
int streamCount() const;
int makeStreamHeaderVideo(int superIndexEntryCount = 0);
int makeStreamHeaderAudio(int superIndexEntryCount = 0);
const StreamHeader& streamHeader(int stream) const;
StreamHeader& streamHeader(int stream);
const unsigned char* strf(int nStream) const;
size_t strfSize(int nStream) const;
const BitmapInfoHeader& videoFormat(int stream) const;
BitmapInfoHeader& videoFormat(int stream);
const WaveFormatEx& audioFormat(int stream) const;
WaveFormatEx& audioFormat(int stream);
dword streamDataSize(int stream) const;
const unsigned char* streamData(int stream) const;
void setStreamData(int nStream, dword sds, const unsigned char* psd);
const char* streamName(int stream) const;
void setStreamName(int nStream, const char* psn);
SuperIndexChunk& superIndexChunk(int stream);
const SuperIndexChunk& superIndexChunk(int stream) const;
int superIndexEntryCount(int stream) const;
offset_t tell() const;
void seek(offset_t) const;
void seekCurrent(offset_t) const;
offset_t dataOffset() const;
offset_t idx1Offset() const;
length_t idx1Size() const;
void rewriteHeaders();
//For use by header updaters, throws if position of movi list
//changes, positions at beginning of data.
void seekMainHeader();
void writeMainHeader();
length_t seekStreamHeader(int stream);
void writeStreamHeader(int stream);
void seekVideoFormat(int stream);
void writeVideoFormat(int stream);
void seekAudioFormat(int stream);
void writeAudioFormat(int stream);
void seekStreamData(int stream);
void writeStreamData(int stream);
void seekSuperIndexChunk(int stream);
void writeSuperIndexChunk(int stream);
int indexCount() const;
void seekIndex() const;
void read(IndexEntry&) const;
void load(int stream, IEVector& index) const;
void load(int stream, FrameIndex& index) const;
void loadIndex(int, FrameIndex&) const;
void loadIndexEx(int, FrameIndex&) const;
void writeIndexChunkHeader(int number_of_index_entries);
void write(const IndexEntry&) const;
void writeIndexChunk(const IEVector& index);
void writeIndexChunk(
const FourCC& chunkId,
const FrameIndex& index);
void writeStandardIndexChunk(
int stream,
const FrameIndex& index,
offset_t baseOffset);
void writeStandardIndexChunk(
int stream,
const FrameIndex& index);
size_t makeSegment();
int segmentCount() const;
offset_t segmentOffset() const;
const FourCC readFourCC() const;
void writeFourCC(const FourCC&);
const FourCC testFourCC() const;
length_t readLength() const;
void writeLength(length_t length);
void read(void* buffer, size_t size) const;
void write(const void* data, size_t size, bool adjust = true);
void writeJunkChunk(length_t);
int countIndexEntries(int stream) const;
bool indexIsRelative() const;
offset_t size() const;
private:
File(const File& rhs);
File& operator=(const File& rhs);
//void readUnknownChunk() const;
void readJunkChunk() const;
bool readInit();
bool readHeaderList();
void readMainHeader();
void readExtraHeaders();
void readStreamHeaderList();
void readStreamHeader(StreamHeader& h);
void readStreamVideoFormat(BitmapInfoHeader& f);
struct StreamInfoVideo;
void readStreamVideoFormat(StreamInfoVideo* psiv);
void readStreamAudioFormat(WaveFormatEx& f);
void readStreamName(std::string& name);
void readExtendedAVIHeader();
void writeExtendedAVIHeader();
bool readDataList();
void readDataRecChunk() const;
void readDataChunk() const;
void readIndexList();
void writeInit();
void writeFinal();
void writeHeader();
void writeStreamHeaderList(int stream);
void writeStreamHeader(const StreamHeader& h);
void writeStreamVideoFormatChunk(const BitmapInfoHeader& f);
void writeStreamVideoFormat(const BitmapInfoHeader& f);
void writeStreamVideoFormatChunk(const unsigned char* pData, size_t sizeData);
void writeStreamVideoFormat(const unsigned char* pData, size_t sizeData);
void writeStreamAudioFormatChunk(const WaveFormatEx&);
void writeStreamAudioFormat(const WaveFormatEx&);
int headerLength() const;
int streamHeaderLength(int stream) const;
void load(const SuperIndexChunk::Entry&, FrameIndex&) const;
class handle_t
{
public:
handle_t();
void open(const char*, mode_t, dword) throw (FileError);
void close();
bool isOpen() const;
offset_t size() const;
void read(void*, size_t) const;
void write(const void*, size_t) const;
void truncate() const;
void seekCurrent(offset_t) const;
void seek(offset_t) const;
offset_t tell() const;
void mapinit();
void mapfinal();
unsigned long map(
offset_t offset,
length_t size,
unsigned char*& view,
length_t& viewSize,
length_t& offsetWithinView) const;
void unmap(unsigned char*, length_t) const;
private:
#if defined WIN32
HANDLE m_hFile;
HANDLE m_hFileMappingObject;
#elif defined LINUX
int m_fd;
#endif
};
handle_t m_handle;
mode_t m_mode;
std::string m_name;
OutputType m_ot;
MainHeader m_mainHeader;
ExtraHeaderVector m_extraHeaderVector;
class indx_t
{
public:
indx_t();
indx_t(int entryCount);
~indx_t();
int entryCount() const;
size_t size() const;
operator SuperIndexChunk&() const;
void read(File&);
void write(File&) const;
private:
indx_t(const indx_t&);
indx_t& operator=(const indx_t&);
dword* m_rep;
};
struct StreamInfo
{
virtual ~StreamInfo();
void write(File&) const;
int length() const;
StreamHeader header;
typedef std::vector<unsigned char> data_t;
data_t m_data;
std::string m_name;
ExtraHeaderVector m_extraHeaderVector;
indx_t m_indx;
protected:
StreamInfo(int);
StreamInfo(const StreamHeader&);
virtual int strf_length() const = 0;
virtual void strf_write(File&) const = 0;
private:
StreamInfo(const StreamInfo&);
StreamInfo& operator=(const StreamInfo&);
};
struct StreamInfoVideo : public StreamInfo
{
StreamInfoVideo(int entryCount);
StreamInfoVideo(const StreamHeader&);
~StreamInfoVideo();
// BitmapInfoHeader m_strf;
unsigned char* m_strf;
size_t m_strfSize;
protected:
int strf_length() const;
void strf_write(File&) const;
};
struct StreamInfoAudio : public StreamInfo
{
StreamInfoAudio(int entryCount);
StreamInfoAudio(const StreamHeader&);
WaveFormatEx m_strf;
protected:
int strf_length() const;
void strf_write(File&) const;
};
friend struct StreamInfo;
friend struct StreamInfoVideo;
friend struct StreamInfoAudio;
void readStreamData(StreamInfo::data_t&);
typedef std::vector<StreamInfo*> infoVector_t;
infoVector_t infoVector;
dword m_totalFrames;
offset_t m_dataPosn;
offset_t m_indexPosn;
int m_indexCount;
length_t m_idx1Size;
struct SegmentInfo
{
offset_t offset;
size_t size;
SegmentInfo() {}
SegmentInfo(offset_t offset_) : offset(offset_) {}
};
typedef std::vector<SegmentInfo> SegmentInfoVector;
SegmentInfoVector m_segmentInfo;
};
#if defined WIN32
class MappedFile
{
public:
enum { invalid_offset = -1 };
MappedFile();
MappedFile(const char* name);
~MappedFile();
void open(const char* name);
void close();
bool isOpen() const;
const char* name() const;
const MainHeader& mainHeader() const;
const StreamHeader& streamHeader(int stream) const;
const BitmapInfoHeader& videoFormat(int stream) const;
const WaveFormatEx& audioFormat(int stream) const;
const char* streamName(int stream) const;
dword totalFrames() const;
offset_t dataOffset() const;
offset_t indexOffset() const;
size_t indexSize() const;
offset_t indexChunkExOffset(int stream) const;
size_t indexChunkExSize(int stream) const;
const void* map(offset_t, size_t) const;
void unmap(const void*) const;
void load(int stream, FrameIEVector& index) const;
private:
MappedFile(const MappedFile&);
MappedFile& operator=(const MappedFile&);
void init();
void unmapAllViews();
offset_t offset() const;
void setFilePointerCurrent(LONG) const;
const FourCC readFourCC() const;
const FourCC queryFourCC() const;
dword readLength() const;
void readHeaderList();
void readDataList();
void readIndexList();
void readJunkChunk() const;
void readUnknownChunk() const;
void readMainHeaderChunk();
void readStreamHeaderList(int stream);
void readStreamHeaderChunk(StreamHeader&) const;
void readStreamVideoFormatChunk(BitmapInfoHeader&) const;
void readStreamAudioFormatChunk(WaveFormatEx&) const;
void readStreamNameChunk(std::string&) const;
void readIndexChunkEx(offset_t&, size_t&) const;
void readExtendedAVIHeaderList() const;
std::string m_name;
HANDLE m_file;
HANDLE m_fileMappingObject;
DWORD m_allocationGranularity;
MainHeader m_mainHeader;
struct StreamInfo
{
StreamHeader streamHeader;
union
{
BitmapInfoHeader* videoFormat;
WaveFormatEx* audioFormat;
};
std::string name;
offset_t indexChunkExOffset;
size_t indexChunkExSize;
};
typedef std::vector<StreamInfo> StreamInfoVector;
StreamInfoVector m_streamInfoVector;
size_t readChunkSize(offset_t) const;
int countEntries(int, const IndexEntry*, int) const;
int countEntries(const SuperIndexChunk&) const;
void loadIndex(int, FrameIEVector&) const;
void loadIndexEx(int, FrameIEVector&) const;
void load(const SuperIndexChunk::Entry&, FrameIEVector&) const;
mutable dword m_totalFrames;
offset_t m_dataOffset;
offset_t m_indexOffset;
size_t m_indexSize;
struct ViewInfo
{
unsigned char* pView;
unsigned char* pChunk;
};
typedef std::list<ViewInfo> Views;
mutable Views m_views;
Views::iterator findView(const void*) const;
};
#endif
}
//inline HANDLE AVI::File::handle() const
//{
// return m_handle;
//}
inline AVI::Chunk::Chunk(
const FourCC fcc,
length_t length,
const unsigned char* d)
: m_fcc(fcc)
{
// if (m_length > 0)
// {
// m_data = new unsigned char[m_length];
// if (m_data == 0)
// {
// throw FileError("Error allocating Chunk data.");
// }
// if (data != 0)
// {
// memcpy(m_data, data, m_length);
// }
// }
if (length)
{
if (d)
{
//typedef data_t::const_iterator iter_t;
//const iter_t first = iter_t(d);
//const data_t::size_type n = length;
//const iter_t last = first + n;
m_data.assign(d, d + length);
}
else
{
const data_t::size_type n = length;
m_data.assign(n, 0);
}
}
else
{
m_data.assign(data_t::size_type(0), 0);
}
}
inline const FourCC AVI::Chunk::fcc() const
{
return m_fcc;
}
inline AVI::length_t AVI::Chunk::length() const
{
const data_t::size_type n = m_data.size();
return n;
}
inline const unsigned char* AVI::Chunk::data() const
{
return &m_data[0];
}
inline unsigned char* AVI::Chunk::data()
{
return &m_data[0];
}
inline void AVI::Chunk::data(const unsigned char* d)
{
//typedef data_t::const_iterator iter_t;
//const iter_t first = iter_t(d);
//const data_t::size_type n = m_data.size();
//const iter_t last = first + n;
m_data.assign(d, d + m_data.size());
}
inline AVI::dword AVI::FileError::id() const
{
return m_id;
}
#endif
|