Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include <new>
00047
00048 #include "VideoReader.h"
00049 #include "Image/ByteImage.h"
00050 #include "Helpers/helpers.h"
00051 #include <ctype.h>
00052
00053
00054
00055
00056
00057
00058 #define MAX_HEADER_LENGTH 8192
00059
00060
00061
00062
00063
00064
00065
00066 CVideoReader::CVideoReader()
00067 {
00068 m_file = 0;
00069 m_nBytesToRead = 0;
00070 m_nImageWidth = -1;
00071 m_nImageHeight = -1;
00072 m_pTempBuffer = new unsigned char[1];
00073 m_pImage = new CByteImage();
00074
00075 header = new unsigned char[MAX_HEADER_LENGTH];
00076 }
00077
00078 CVideoReader::~CVideoReader()
00079 {
00080 delete [] header;
00081 delete m_pImage;
00082 Close();
00083 }
00084
00085
00086
00087
00088
00089
00090 bool CVideoReader::OpenUncompressedAVI(const char *pFileName)
00091 {
00092
00093 Close();
00094
00095 m_file = fopen(pFileName, "rb");
00096 if (!m_file)
00097 return false;
00098
00099
00100 if (!ParseUncompressedAVIHeader())
00101 {
00102 Close();
00103 return false;
00104 }
00105
00106
00107 m_nBytesToRead = m_nImageWidth * m_nImageHeight * 3;
00108
00109
00110 delete m_pImage;
00111 delete [] m_pTempBuffer;
00112 m_pImage = new CByteImage(m_nImageWidth, m_nImageHeight, CByteImage::eRGB24);
00113 m_pTempBuffer = new unsigned char[m_nBytesToRead];
00114
00115 return true;
00116 }
00117
00118 CByteImage *CVideoReader::ReadNextFrame()
00119 {
00120 if (!m_file)
00121 return 0;
00122
00123 if (fread(m_pTempBuffer, m_nBytesToRead, 1, m_file) != 1)
00124 return 0;
00125
00126 const int width_bytes = 3 * m_nImageWidth;
00127
00128 unsigned char *pHelper1 = m_pImage->pixels;
00129 unsigned char *pHelper2 = m_pTempBuffer + m_nBytesToRead - width_bytes;
00130
00131
00132 for (int i = 0; i < m_nImageHeight; i++)
00133 {
00134 for (int j = 0; j < width_bytes; j += 3)
00135 {
00136 pHelper1[j] = pHelper2[j + 2];
00137 pHelper1[j + 1] = pHelper2[j + 1];
00138 pHelper1[j + 2] = pHelper2[j];
00139 }
00140
00141 pHelper1 += width_bytes;
00142 pHelper2 -= width_bytes;
00143 }
00144
00145 fseek(m_file, 8, SEEK_CUR);
00146
00147 return m_pImage;
00148 }
00149
00150 void CVideoReader::Close()
00151 {
00152 if (m_file)
00153 {
00154 fclose(m_file);
00155 m_file = 0;
00156 }
00157
00158 m_nImageWidth = -1;
00159 m_nImageHeight = -1;
00160 }
00161
00162
00163 static inline unsigned int ConvertFourByteCode(const char *pCode)
00164 {
00165 return *((unsigned int *) pCode);
00166 }
00167
00168 bool CVideoReader::ParseUncompressedAVIHeader()
00169 {
00170 if (fread(header, 8192, 1, m_file) != 1)
00171 return false;
00172
00173 unsigned int length, *p = (unsigned int *) header;
00174
00175 #ifdef IVT_BIG_ENDIAN
00176 if (*p++ != ConvertFourByteCode("RIFF"))
00177 return false;
00178
00179
00180 p++;
00181
00182 if (*p++ != ConvertFourByteCode("AVI "))
00183 return false;
00184
00185 if (*p++ != ConvertFourByteCode("LIST"))
00186 return false;
00187
00188
00189 p++;
00190
00191 if (*p++ != ConvertFourByteCode("hdrl"))
00192 return false;
00193
00194 if (*p++ != ConvertFourByteCode("avih"))
00195 return false;
00196
00197 m_nImageWidth = invert_byte_order_int(p[9]);
00198 m_nImageHeight = invert_byte_order_int(p[10]);
00199
00200 length = invert_byte_order_int(*p++);
00201 if (length > 256)
00202 return false;
00203
00204
00205 p = (unsigned int *) (((unsigned char *) p) + length);
00206
00207 if (*p++ != ConvertFourByteCode("LIST"))
00208 return false;
00209
00210
00211 p++;
00212
00213 if (*p++ != ConvertFourByteCode("strl"))
00214 return false;
00215
00216 if (*p++ != ConvertFourByteCode("strh"))
00217 return false;
00218
00219 length = invert_byte_order_int(*p++);
00220 if (length > 256)
00221 return false;
00222
00223
00224 p = (unsigned int *) (((unsigned char *) p) + length);
00225
00226 if (*p++ != ConvertFourByteCode("strf"))
00227 return false;
00228
00229 length = invert_byte_order_int(*p++);
00230
00231
00232 p = (unsigned int *) (((unsigned char *) p) + length);
00233 if (length > 4096)
00234 return false;
00235
00236
00237 unsigned int offset = (unsigned int) ((unsigned char *) p - header);
00238 fseek(m_file, offset, SEEK_SET);
00239
00240 if (*p++ == ConvertFourByteCode("JUNK"))
00241 {
00242
00243 length = invert_byte_order_int(*p);
00244 fseek(m_file, length + 8, SEEK_CUR);
00245 }
00246
00247 if (fread(header, 20, 1, m_file) != 1)
00248 return false;
00249
00250 p = (unsigned int *) header;
00251
00252 if (*p++ != ConvertFourByteCode("LIST"))
00253 return false;
00254
00255
00256 p++;
00257
00258 if (*p++ != ConvertFourByteCode("movi"))
00259 return false;
00260
00261 if (*p++ != ConvertFourByteCode("00db"))
00262 return false;
00263
00264 if (invert_byte_order_int(*p) != m_nImageWidth * m_nImageHeight * 3)
00265 return false;
00266 #else
00267 if (*p++ != ConvertFourByteCode("RIFF"))
00268 return false;
00269
00270
00271 p++;
00272
00273 if (*p++ != ConvertFourByteCode("AVI "))
00274 return false;
00275
00276 if (*p++ != ConvertFourByteCode("LIST"))
00277 return false;
00278
00279
00280 p++;
00281
00282 if (*p++ != ConvertFourByteCode("hdrl"))
00283 return false;
00284
00285 if (*p++ != ConvertFourByteCode("avih"))
00286 return false;
00287
00288 m_nImageWidth = p[9];
00289 m_nImageHeight = p[10];
00290
00291 length = *p++;
00292 if (length > 256)
00293 return false;
00294
00295
00296 p = (unsigned int *) (((unsigned char *) p) + length);
00297
00298 if (*p++ != ConvertFourByteCode("LIST"))
00299 return false;
00300
00301
00302 p++;
00303
00304 if (*p++ != ConvertFourByteCode("strl"))
00305 return false;
00306
00307 if (*p++ != ConvertFourByteCode("strh"))
00308 return false;
00309
00310 length = *p++;
00311 if (length > 256)
00312 return false;
00313
00314
00315 p = (unsigned int *) (((unsigned char *) p) + length);
00316
00317 if (*p++ != ConvertFourByteCode("strf"))
00318 return false;
00319
00320 length = *p++;
00321
00322
00323 p = (unsigned int *) (((unsigned char *) p) + length);
00324 if (length > 4096)
00325 return false;
00326
00327
00328 unsigned int offset = (unsigned int) ((unsigned char *) p - header);
00329 fseek(m_file, offset, SEEK_SET);
00330
00331 if (*p++ == ConvertFourByteCode("JUNK"))
00332 {
00333
00334 length = *p;
00335 fseek(m_file, length + 8, SEEK_CUR);
00336 }
00337
00338 if (fread(header, 20, 1, m_file) != 1)
00339 return false;
00340
00341 p = (unsigned int *) header;
00342
00343 if (*p++ != ConvertFourByteCode("LIST"))
00344 return false;
00345
00346
00347 p++;
00348
00349 if (*p++ != ConvertFourByteCode("movi"))
00350 return false;
00351
00352 if (*p++ != ConvertFourByteCode("00db"))
00353 return false;
00354
00355 if (*p != (unsigned int) (m_nImageWidth * m_nImageHeight * 3))
00356 return false;
00357 #endif
00358
00359 return true;
00360 }
asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:58