Skip to content

Commit fbb2cab

Browse files
committed
Use fixed size types in AVIContainer structs
1 parent 3fd2384 commit fbb2cab

File tree

1 file changed

+87
-94
lines changed

1 file changed

+87
-94
lines changed

modules/videoio/src/container_avi.cpp

Lines changed: 87 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -7,66 +7,57 @@
77
namespace cv
88
{
99

10-
const unsigned int RIFF_CC = CV_FOURCC('R','I','F','F');
11-
const unsigned int LIST_CC = CV_FOURCC('L','I','S','T');
12-
const unsigned int HDRL_CC = CV_FOURCC('h','d','r','l');
13-
const unsigned int AVIH_CC = CV_FOURCC('a','v','i','h');
14-
const unsigned int STRL_CC = CV_FOURCC('s','t','r','l');
15-
const unsigned int STRH_CC = CV_FOURCC('s','t','r','h');
16-
const unsigned int STRF_CC = CV_FOURCC('s','t','r','f');
17-
const unsigned int VIDS_CC = CV_FOURCC('v','i','d','s');
18-
const unsigned int MJPG_CC = CV_FOURCC('M','J','P','G');
19-
const unsigned int MOVI_CC = CV_FOURCC('m','o','v','i');
20-
const unsigned int IDX1_CC = CV_FOURCC('i','d','x','1');
21-
const unsigned int AVI_CC = CV_FOURCC('A','V','I',' ');
22-
const unsigned int AVIX_CC = CV_FOURCC('A','V','I','X');
23-
const unsigned int JUNK_CC = CV_FOURCC('J','U','N','K');
24-
const unsigned int INFO_CC = CV_FOURCC('I','N','F','O');
25-
const unsigned int ODML_CC = CV_FOURCC('o','d','m','l');
26-
const unsigned int DMLH_CC = CV_FOURCC('d','m','l','h');
27-
28-
String fourccToString(unsigned int fourcc);
29-
30-
#ifndef DWORD
31-
typedef unsigned int DWORD;
32-
#endif
33-
#ifndef WORD
34-
typedef unsigned short int WORD;
35-
#endif
36-
#ifndef LONG
37-
typedef int LONG;
38-
#endif
10+
const uint32_t RIFF_CC = CV_FOURCC('R','I','F','F');
11+
const uint32_t LIST_CC = CV_FOURCC('L','I','S','T');
12+
const uint32_t HDRL_CC = CV_FOURCC('h','d','r','l');
13+
const uint32_t AVIH_CC = CV_FOURCC('a','v','i','h');
14+
const uint32_t STRL_CC = CV_FOURCC('s','t','r','l');
15+
const uint32_t STRH_CC = CV_FOURCC('s','t','r','h');
16+
const uint32_t STRF_CC = CV_FOURCC('s','t','r','f');
17+
const uint32_t VIDS_CC = CV_FOURCC('v','i','d','s');
18+
const uint32_t MJPG_CC = CV_FOURCC('M','J','P','G');
19+
const uint32_t MOVI_CC = CV_FOURCC('m','o','v','i');
20+
const uint32_t IDX1_CC = CV_FOURCC('i','d','x','1');
21+
const uint32_t AVI_CC = CV_FOURCC('A','V','I',' ');
22+
const uint32_t AVIX_CC = CV_FOURCC('A','V','I','X');
23+
const uint32_t JUNK_CC = CV_FOURCC('J','U','N','K');
24+
const uint32_t INFO_CC = CV_FOURCC('I','N','F','O');
25+
const uint32_t ODML_CC = CV_FOURCC('o','d','m','l');
26+
const uint32_t DMLH_CC = CV_FOURCC('d','m','l','h');
27+
28+
String fourccToString(uint32_t fourcc);
29+
3930

4031
#pragma pack(push, 1)
4132
struct AviMainHeader
4233
{
43-
DWORD dwMicroSecPerFrame; // The period between video frames
44-
DWORD dwMaxBytesPerSec; // Maximum data rate of the file
45-
DWORD dwReserved1; // 0
46-
DWORD dwFlags; // 0x10 AVIF_HASINDEX: The AVI file has an idx1 chunk containing an index at the end of the file.
47-
DWORD dwTotalFrames; // Field of the main header specifies the total number of frames of data in file.
48-
DWORD dwInitialFrames; // Is used for interleaved files
49-
DWORD dwStreams; // Specifies the number of streams in the file.
50-
DWORD dwSuggestedBufferSize; // Field specifies the suggested buffer size forreading the file
51-
DWORD dwWidth; // Fields specify the width of the AVIfile in pixels.
52-
DWORD dwHeight; // Fields specify the height of the AVIfile in pixels.
53-
DWORD dwReserved[4]; // 0, 0, 0, 0
34+
uint32_t dwMicroSecPerFrame; // The period between video frames
35+
uint32_t dwMaxBytesPerSec; // Maximum data rate of the file
36+
uint32_t dwReserved1; // 0
37+
uint32_t dwFlags; // 0x10 AVIF_HASINDEX: The AVI file has an idx1 chunk containing an index at the end of the file.
38+
uint32_t dwTotalFrames; // Field of the main header specifies the total number of frames of data in file.
39+
uint32_t dwInitialFrames; // Is used for interleaved files
40+
uint32_t dwStreams; // Specifies the number of streams in the file.
41+
uint32_t dwSuggestedBufferSize; // Field specifies the suggested buffer size forreading the file
42+
uint32_t dwWidth; // Fields specify the width of the AVIfile in pixels.
43+
uint32_t dwHeight; // Fields specify the height of the AVIfile in pixels.
44+
uint32_t dwReserved[4]; // 0, 0, 0, 0
5445
};
5546

5647
struct AviStreamHeader
5748
{
58-
unsigned int fccType; // 'vids', 'auds', 'txts'...
59-
unsigned int fccHandler; // "cvid", "DIB "
60-
DWORD dwFlags; // 0
61-
DWORD dwPriority; // 0
62-
DWORD dwInitialFrames; // 0
63-
DWORD dwScale; // 1
64-
DWORD dwRate; // Fps (dwRate - frame rate for video streams)
65-
DWORD dwStart; // 0
66-
DWORD dwLength; // Frames number (playing time of AVI file as defined by scale and rate)
67-
DWORD dwSuggestedBufferSize; // For reading the stream
68-
DWORD dwQuality; // -1 (encoding quality. If set to -1, drivers use the default quality value)
69-
DWORD dwSampleSize; // 0 means that each frame is in its own chunk
49+
uint32_t fccType; // 'vids', 'auds', 'txts'...
50+
uint32_t fccHandler; // "cvid", "DIB "
51+
uint32_t dwFlags; // 0
52+
uint32_t dwPriority; // 0
53+
uint32_t dwInitialFrames; // 0
54+
uint32_t dwScale; // 1
55+
uint32_t dwRate; // Fps (dwRate - frame rate for video streams)
56+
uint32_t dwStart; // 0
57+
uint32_t dwLength; // Frames number (playing time of AVI file as defined by scale and rate)
58+
uint32_t dwSuggestedBufferSize; // For reading the stream
59+
uint32_t dwQuality; // -1 (encoding quality. If set to -1, drivers use the default quality value)
60+
uint32_t dwSampleSize; // 0 means that each frame is in its own chunk
7061
struct {
7162
short int left;
7263
short int top;
@@ -77,39 +68,39 @@ struct AviStreamHeader
7768

7869
struct AviIndex
7970
{
80-
DWORD ckid;
81-
DWORD dwFlags;
82-
DWORD dwChunkOffset;
83-
DWORD dwChunkLength;
71+
uint32_t ckid;
72+
uint32_t dwFlags;
73+
uint32_t dwChunkOffset;
74+
uint32_t dwChunkLength;
8475
};
8576

8677
struct BitmapInfoHeader
8778
{
88-
DWORD biSize; // Write header size of BITMAPINFO header structure
89-
LONG biWidth; // width in pixels
90-
LONG biHeight; // height in pixels
91-
WORD biPlanes; // Number of color planes in which the data is stored
92-
WORD biBitCount; // Number of bits per pixel
93-
DWORD biCompression; // Type of compression used (uncompressed: NO_COMPRESSION=0)
94-
DWORD biSizeImage; // Image Buffer. Quicktime needs 3 bytes also for 8-bit png
79+
uint32_t biSize; // Write header size of BITMAPINFO header structure
80+
int32_t biWidth; // width in pixels
81+
int32_t biHeight; // height in pixels
82+
uint16_t biPlanes; // Number of color planes in which the data is stored
83+
uint16_t biBitCount; // Number of bits per pixel
84+
uint32_t biCompression; // Type of compression used (uncompressed: NO_COMPRESSION=0)
85+
uint32_t biSizeImage; // Image Buffer. Quicktime needs 3 bytes also for 8-bit png
9586
// (biCompression==NO_COMPRESSION)?0:xDim*yDim*bytesPerPixel;
96-
LONG biXPelsPerMeter; // Horizontal resolution in pixels per meter
97-
LONG biYPelsPerMeter; // Vertical resolution in pixels per meter
98-
DWORD biClrUsed; // 256 (color table size; for 8-bit only)
99-
DWORD biClrImportant; // Specifies that the first x colors of the color table. Are important to the DIB.
87+
int32_t biXPelsPerMeter; // Horizontal resolution in pixels per meter
88+
int32_t biYPelsPerMeter; // Vertical resolution in pixels per meter
89+
uint32_t biClrUsed; // 256 (color table size; for 8-bit only)
90+
uint32_t biClrImportant; // Specifies that the first x colors of the color table. Are important to the DIB.
10091
};
10192

10293
struct RiffChunk
10394
{
104-
unsigned int m_four_cc;
105-
unsigned int m_size;
95+
uint32_t m_four_cc;
96+
uint32_t m_size;
10697
};
10798

10899
struct RiffList
109100
{
110-
unsigned int m_riff_or_list_cc;
111-
unsigned int m_size;
112-
unsigned int m_list_type_cc;
101+
uint32_t m_riff_or_list_cc;
102+
uint32_t m_size;
103+
uint32_t m_list_type_cc;
113104
};
114105

115106
class VideoInputStream
@@ -118,9 +109,9 @@ class VideoInputStream
118109
VideoInputStream();
119110
VideoInputStream(const String& filename);
120111
~VideoInputStream();
121-
VideoInputStream& read(char*, unsigned long long int);
122-
VideoInputStream& seekg(unsigned long long int);
123-
unsigned long long int tellg();
112+
VideoInputStream& read(char*, uint64_t);
113+
VideoInputStream& seekg(uint64_t);
114+
uint64_t tellg();
124115
bool isOpened() const;
125116
bool open(const String& filename);
126117
void close();
@@ -178,7 +169,7 @@ static const int AVIIF_KEYFRAME = 0x10;
178169
static const int MAX_BYTES_PER_SEC = 99999999;
179170
static const int SUG_BUFFER_SIZE = 1048576;
180171

181-
String fourccToString(unsigned int fourcc)
172+
String fourccToString(uint32_t fourcc)
182173
{
183174
return format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
184175
}
@@ -221,7 +212,7 @@ void VideoInputStream::close()
221212
}
222213
}
223214

224-
VideoInputStream& VideoInputStream::read(char* buf, unsigned long long int count)
215+
VideoInputStream& VideoInputStream::read(char* buf, uint64_t count)
225216
{
226217
if(isOpened())
227218
{
@@ -231,14 +222,14 @@ VideoInputStream& VideoInputStream::read(char* buf, unsigned long long int count
231222
return *this;
232223
}
233224

234-
VideoInputStream& VideoInputStream::seekg(unsigned long long int pos)
225+
VideoInputStream& VideoInputStream::seekg(uint64_t pos)
235226
{
236-
m_is_valid = (fseek(m_f, (long)pos, SEEK_SET) == 0);
227+
m_is_valid = (fseek(m_f, (int32_t)pos, SEEK_SET) == 0);
237228

238229
return *this;
239230
}
240231

241-
unsigned long long int VideoInputStream::tellg()
232+
uint64_t VideoInputStream::tellg()
242233
{
243234
return ftell(m_f);
244235
}
@@ -283,9 +274,9 @@ void AVIReadContainer::close()
283274
m_file_stream->close();
284275
}
285276

286-
bool AVIReadContainer::parseIndex(unsigned int index_size, frame_list& in_frame_list)
277+
bool AVIReadContainer::parseIndex(uint32_t index_size, frame_list& in_frame_list)
287278
{
288-
unsigned long long int index_end = m_file_stream->tellg();
279+
uint64_t index_end = m_file_stream->tellg();
289280
index_end += index_size;
290281
bool result = false;
291282

@@ -296,7 +287,7 @@ bool AVIReadContainer::parseIndex(unsigned int index_size, frame_list& in_frame_
296287

297288
if(idx1.ckid == m_stream_id)
298289
{
299-
unsigned long long int absolute_pos = m_movi_start + idx1.dwChunkOffset;
290+
uint64_t absolute_pos = m_movi_start + idx1.dwChunkOffset;
300291

301292
if(absolute_pos < m_movi_end)
302293
{
@@ -322,7 +313,7 @@ bool AVIReadContainer::parseStrl(char stream_id, Codecs codec_)
322313

323314
if(m_file_stream && strh.m_four_cc == STRH_CC)
324315
{
325-
unsigned long long int next_strl_list = m_file_stream->tellg();
316+
uint64_t next_strl_list = m_file_stream->tellg();
326317
next_strl_list += strh.m_size;
327318

328319
AviStreamHeader strm_hdr;
@@ -332,8 +323,8 @@ bool AVIReadContainer::parseStrl(char stream_id, Codecs codec_)
332323
{
333324
if(strm_hdr.fccType == VIDS_CC && strm_hdr.fccHandler == MJPG_CC)
334325
{
335-
char first_digit = (stream_id/10) + '0';
336-
char second_digit = (stream_id%10) + '0';
326+
uint8_t first_digit = (stream_id/10) + '0';
327+
uint8_t second_digit = (stream_id%10) + '0';
337328

338329
if(m_stream_id == 0)
339330
{
@@ -382,7 +373,7 @@ bool AVIReadContainer::parseHdrlList(Codecs codec_)
382373

383374
if(m_file_stream && avih.m_four_cc == AVIH_CC)
384375
{
385-
unsigned long long int next_strl_list = m_file_stream->tellg();
376+
uint64_t next_strl_list = m_file_stream->tellg();
386377
next_strl_list += avih.m_size;
387378

388379
AviMainHeader avi_hdr;
@@ -391,13 +382,13 @@ bool AVIReadContainer::parseHdrlList(Codecs codec_)
391382
if(m_file_stream)
392383
{
393384
m_is_indx_present = ((avi_hdr.dwFlags & 0x10) != 0);
394-
DWORD number_of_streams = avi_hdr.dwStreams;
385+
uint32_t number_of_streams = avi_hdr.dwStreams;
395386
CV_Assert(number_of_streams < 0xFF);
396387
m_width = avi_hdr.dwWidth;
397388
m_height = avi_hdr.dwHeight;
398389

399390
//the number of strl lists must be equal to number of streams specified in main avi header
400-
for(DWORD i = 0; i < number_of_streams; ++i)
391+
for(uint32_t i = 0; i < number_of_streams; ++i)
401392
{
402393
m_file_stream->seekg(next_strl_list);
403394
RiffList strl_list;
@@ -433,7 +424,7 @@ bool AVIReadContainer::parseAviWithFrameList(frame_list& in_frame_list, Codecs c
433424

434425
if( m_file_stream && hdrl_list.m_riff_or_list_cc == LIST_CC && hdrl_list.m_list_type_cc == HDRL_CC )
435426
{
436-
unsigned long long int next_list = m_file_stream->tellg();
427+
uint64_t next_list = m_file_stream->tellg();
437428
//RiffList::m_size includes fourCC field which we have already read
438429
next_list += (hdrl_list.m_size - 4);
439430
//parseHdrlList sets m_is_indx_present flag which would be used later
@@ -472,7 +463,7 @@ bool AVIReadContainer::parseAviWithFrameList(frame_list& in_frame_list, Codecs c
472463
if(m_is_indx_present)
473464
{
474465
//we are expecting to find index section after movi list
475-
unsigned int indx_pos = (unsigned int)m_movi_start + 4;
466+
uint32_t indx_pos = (uint32_t)m_movi_start + 4;
476467
indx_pos += (some_list.m_size - 4);
477468
m_file_stream->seekg(indx_pos);
478469

@@ -520,6 +511,7 @@ std::vector<char> AVIReadContainer::readFrame(frame_iterator it)
520511

521512
RiffChunk chunk;
522513
*(m_file_stream) >> chunk;
514+
CV_Assert(chunk.m_size <= 0xFFFF);
523515

524516
std::vector<char> result;
525517

@@ -543,7 +535,7 @@ bool AVIReadContainer::parseRiff(frame_list &m_mjpeg_frames_)
543535
if( *m_file_stream && riff_list.m_riff_or_list_cc == RIFF_CC &&
544536
((riff_list.m_list_type_cc == AVI_CC) | (riff_list.m_list_type_cc == AVIX_CC)) )
545537
{
546-
unsigned long long int next_riff = m_file_stream->tellg();
538+
uint64_t next_riff = m_file_stream->tellg();
547539
//RiffList::m_size includes fourCC field which we have already read
548540
next_riff += (riff_list.m_size - 4);
549541

@@ -559,7 +551,7 @@ bool AVIReadContainer::parseRiff(frame_list &m_mjpeg_frames_)
559551
return result;
560552
}
561553

562-
void AVIReadContainer::printError(RiffList &list, unsigned int expected_fourcc)
554+
void AVIReadContainer::printError(RiffList &list, uint32_t expected_fourcc)
563555
{
564556
if(!m_file_stream)
565557
{
@@ -575,7 +567,7 @@ void AVIReadContainer::printError(RiffList &list, unsigned int expected_fourcc)
575567
}
576568
}
577569

578-
void AVIReadContainer::printError(RiffChunk &chunk, unsigned int expected_fourcc)
570+
void AVIReadContainer::printError(RiffChunk &chunk, uint32_t expected_fourcc)
579571
{
580572
if(!m_file_stream)
581573
{
@@ -742,6 +734,7 @@ void BitStream::patchInt(int val, size_t pos)
742734
}
743735
else
744736
{
737+
CV_Assert(pos < (1u<<31));
745738
long fpos = ftell(m_f);
746739
fseek(m_f, (long)pos, SEEK_SET);
747740
uchar buf[] = { (uchar)val, (uchar)(val >> 8), (uchar)(val >> 16), (uchar)(val >> 24) };

0 commit comments

Comments
 (0)