Debian Codegraph @

lib/aviread/AviReadHandler.cpp

raw
1 #include "AviReadHandler.h"
2 #include "AviReadStream.h"
3 #include "utils.h"
4 #include "avm_fourcc.h"
5 #include "formats.h"
6 #include "avm_output.h"
7 #include <stdio.h>
8 #include <string.h>
9
10 //#define USE_HACK
11
12 AVM_BEGIN_NAMESPACE;
13
14 // reset profiler status - just for debuging
15 //extern "C" void kprof_reset_stats(void);
16 struct RiffInfoChunk {
17 fourcc_t riff;
18 //const char* text;
19 const char text[20];
20 };
21
22 static const RiffInfoChunk infos[] =
23 {
24 // here will follow various info about the movie file
25 // comments here are copied from OpenDML specs by Matrox
26
27 // Indicates where the subject of the file is archived
28 { RIFFINFO_IARL, "Archival Location" },
29 // Lists the artist of the original subject of the file;
30 // for example, "Michaelangelo."
31 { RIFFINFO_IART, "Artist" },
32 // Lists the name of the person or organization that commissioned
33 // the subject of the file; for example "Pope Julian II."
34 { RIFFINFO_ICMS, "Commissioned" },
35 // Provides general comments about the file or the subject
36 // of the file. If the comment is several sentences long, end each
37 // sentence with a period. Do not include new-line characters.
38 { RIFFINFO_ICMT, "Comments" },
39 // Records the copyright information for the file; for example,
40 // "Copyright Encyclopedia International 1991." If there are multiple
41 // copyrights, separate them by semicolon followed by a space.
42 { RIFFINFO_ICOP, "Copyright" },
43 // Specifies the date the subject of the file was created. List
44 // dates in year-month-day format, padding one-digit months and days with
45 // a zero on the left; for example, "1553-05-03" for May 3, 1553.
46 { RIFFINFO_ICRD, "Creation Date" },
47 // Describes whether an image has been cropped and, if so, how it
48 // was cropped; for example, "lower-right corner."
49 { RIFFINFO_ICRP, "Cropped" },
50 // Specifies the size of the original subject of the file; for
51 // example, "8.5 in h, 11 in w."
52 { RIFFINFO_IDIM, "Dimensions" },
53 // Stores dots per inch setting of the digitizer used to
54 // produce the file, such as "300."
55 { RIFFINFO_IDPI, "Dots Per Inch" },
56 // Stores the of the engineer who worked on the file. If there are
57 // multiple engineers, separate the names by a semicolon and a blank;
58 // for example, "Smith, John; Adams, Joe."
59 { RIFFINFO_IENG, "Engineer" },
60 // Describes the original work, such as "landscape,", "portrait,"
61 // "still liefe," etc.
62 { RIFFINFO_IGNR, "Genre" },
63 // Provides a list of keywords that refer to the file or subject of the
64 // file. Separate multiple keywords with a semicolon and a blank;
65 // for example, "Seattle, aerial view; scenery."
66 { RIFFINFO_IKEY, "Keywords" },
67 // ILGT - Describes the changes in the lightness settings on the digitizer
68 // required to produce the file. Note that the format of this information
69 // depends on the hardware used.
70 { RIFFINFO_ILGT, "Lightness" },
71 // IMED - Decribes the original subject of the file, such as
72 // "computer image," "drawing," "lithograph," and so on.
73 { RIFFINFO_IMED, "Medium" },
74 // INAM - Stores the title of the subject of the file, such as
75 // "Seattle from Above."
76 { RIFFINFO_INAM, "Name" },
77 // IPLT - Specifies the number of colors requested when digitizing
78 // an image, such as "256."
79 { RIFFINFO_IPLT, "Palette Setting" },
80 // IPRD - Specifies the name of title the file was originally intended
81 // for, such as "Encyclopedia of Pacific Northwest Geography."
82 { RIFFINFO_IPRD, "Product" },
83 // ISBJ - Decsribes the contents of the file, such as
84 // "Aerial view of Seattle."
85 { RIFFINFO_ISBJ, "Subject" },
86 // ISFT - Identifies the name of the software packages used to create the
87 // file, such as "Microsoft WaveEdit"
88 { RIFFINFO_ISFT, "Software" },
89 // ISHP - Identifies the change in sharpness for the digitizer
90 // required to produce the file (the format depends on the hardware used).
91 { RIFFINFO_ISHP, "Sharpness" },
92 // ISRC - Identifies the name of the person or organization who
93 // suplied the original subject of the file; for example, "Try Research."
94 { RIFFINFO_ISRC, "Source" },
95 // ISRF - Identifies the original form of the material that was digitized,
96 // such as "slide," "paper," "map," and so on. This is not necessarily
97 // the same as IMED
98 { RIFFINFO_ISRF, "Source Form" },
99 // ITCH - Identifies the technician who digitized the subject file;
100 // for example, "Smith, John."
101 { RIFFINFO_ITCH, "Technician" },
102
103 // ISMP
104 { RIFFINFO_ISMP, "Time Code" },
105 // IDIT
106 { RIFFINFO_IDIT, "Digitization Time" },
107
108 { 0, "" }
109 };
110
111 static inline AVIMainHeader* avm_get_leAVIMainHeader(void* header)
112 {
113 AVIMainHeader* h = (AVIMainHeader*) header;
114
115 h->dwMicroSecPerFrame = avm_get_le32(&h->dwMicroSecPerFrame);
116 h->dwMaxBytesPerSec = avm_get_le32(&h->dwMaxBytesPerSec);
117 h->dwPaddingGranularity = avm_get_le32(&h->dwPaddingGranularity);
118 h->dwFlags = avm_get_le32(&h->dwFlags);
119 h->dwTotalFrames = avm_get_le32(&h->dwTotalFrames);
120 h->dwInitialFrames = avm_get_le32(&h->dwInitialFrames);
121 h->dwStreams = avm_get_le32(&h->dwStreams);
122 h->dwSuggestedBufferSize = avm_get_le32(&h->dwSuggestedBufferSize);
123 h->dwWidth = avm_get_le32(&h->dwWidth);
124 h->dwHeight = avm_get_le32(&h->dwHeight);
125 h->dwScale = avm_get_le32(&h->dwScale);
126 h->dwRate = avm_get_le32(&h->dwRate);
127 h->dwStart = avm_get_le32(&h->dwStart);
128 h->dwLength = avm_get_le32(&h->dwLength);
129
130 return h;
131 }
132
133 static inline AVIStreamHeader* avm_get_leAVIStreamHeader(void* header)
134 {
135 AVIStreamHeader* h = (AVIStreamHeader*) header;
136
137 h->fccType = avm_get_le32(&h->fccType);
138 h->fccHandler = avm_get_le32(&h->fccHandler);
139 h->dwFlags = avm_get_le32(&h->dwFlags);
140 h->wPriority = avm_get_le16(&h->wPriority);
141 h->wLanguage = avm_get_le16(&h->wLanguage);
142 h->dwInitialFrames = avm_get_le32(&h->dwInitialFrames);
143 h->dwScale = avm_get_le32(&h->dwScale);
144 h->dwRate = avm_get_le32(&h->dwRate);
145 h->dwStart = avm_get_le32(&h->dwStart);
146 h->dwLength = avm_get_le32(&h->dwLength);
147 h->dwSuggestedBufferSize = avm_get_le32(&h->dwSuggestedBufferSize);
148 h->dwQuality = avm_get_le32(&h->dwQuality);
149 h->dwSampleSize = avm_get_le32(&h->dwSampleSize);
150 h->rcFrame.left = avm_get_le16(&h->rcFrame.left);
151 h->rcFrame.right = avm_get_le16(&h->rcFrame.right);
152 h->rcFrame.top = avm_get_le16(&h->rcFrame.top);
153 h->rcFrame.bottom = avm_get_le16(&h->rcFrame.bottom);
154
155 return h;
156 }
157
158
159 AviReadHandler::AviReadHandler(int flags)
160 :m_iFlags(flags)
161 {
162 }
163
164 int AviReadHandler::init(const char* pszFile)
165 {
166 if (m_Input.open(pszFile) < 0)
167 {
168 AVM_WRITE("AVI reader", "Stream: %s can't be opened!\n", pszFile);
169 return -1;
170 }
171
172 if (m_Input.readDword() != FOURCC_RIFF)
173 #ifndef USE_HACK
174 return -1
175 #endif
176 ;
177 m_Input.readDword();
178 if (m_Input.readDword() != formtypeAVI)
179 #ifndef USE_HACK
180 return -1
181 #endif
182 ;
183
184 off_t movie_chunk = 0;
185 uint_t type;
186 off_t nextpos;
187 size_t chunk_size;
188 uint32_t chunk_type;
189 bool index_chunk_found = false;
190 bool valid_list = false;
191
192 #ifdef USE_HACK
193 AVIStreamHeader ash;
194 ash.fccType = streamtypeVIDEO;
195 BitmapInfo bih(640,360,24);
196 bih.SetDirection(1);
197 bih.biCompression = ('3'<<24) | ('V'<<16) | ('I' << 8) | 'D';
198 ash.fccHandler = bih.biCompression;
199 if (AviReadStream* s = new AviReadStream(this, ash, 0, (char*)&bih, sizeof(bih)))
200 m_Streams.push_back(s);
201 #endif
202
203 for (;;)
204 {
205 chunk_type = m_Input.readDword();
206 if (m_Input.eof())
207 break;
208 if (!valid_list && chunk_type != FOURCC_LIST)
209 continue; // search for next valid LIST header
210
211 chunk_size = m_Input.readDword();
212 if (chunk_size < 0)
213 continue;
214
215 nextpos = m_Input.pos() + chunk_size + (chunk_size & 1);
216 //printf("CHUNK %.4s %d pos:%lld %d\n", (char*)&chunk_type, chunk_size, m_Input.pos(), m_Input.eof());
217 switch(chunk_type)
218 {
219 case FOURCC_LIST:
220 if (chunk_size < 4)
221 {
222 AVM_WRITE("AVI reader", "Damaged Avi with LIST chunk size %" PRIsz " detected...\n", chunk_size);
223 continue;
224 }
225 valid_list = true;
226 type = m_Input.readDword();
227 //printf("LISTTYPE %.4s %lld %d\n", (char*)&type, m_Input.pos(), m_Input.eof());
228 switch (type)
229 {
230 case listtypeAVIMOVIE:
231 movie_chunk = m_Input.pos();
232 m_Input.seek(nextpos);
233 // readMovieChunk();
234 //printf("MovieChunk %d\n", movie_chunk);
235 break;
236 case listtypeAVIHEADER:
237 continue;
238 case listtypeSTREAMHEADER:
239 if (m_Input.readDword() == ckidSTREAMHEADER)
240 readAVIStreamHeader(m_Input.readDword());
241 break;
242 case listtypeINFO:
243 readInfoChunk(chunk_size);
244 break;
245 }
246 break;
247 case ckidAVIMAINHDR:
248 readAVIMainHeader(chunk_size);
249 break;
250 case ckidAVINEWINDEX:
251 if (readIndexChunk(chunk_size, movie_chunk) == 0)
252 index_chunk_found = true;
253 break;
254 };
255 m_Input.seek(nextpos);
256 }
257
258 if (m_Streams.size() == 0)
259 {
260 AVM_WRITE("AVI reader", "No playable stream found in this AVI file!\n");
261 return -1;
262 }
263
264 if (!index_chunk_found)
265 reconstructIndexChunk(movie_chunk);
266
267 for (size_t i = 0; i < m_Streams.size(); ++i)
268 {
269 m_Streams[i]->fixHeader();
270 char b[100];
271 short wFormatTag;
272 if (m_Streams[i]->GetType() == IStream::Audio)
273 {
274 if(m_Streams[i]->m_pcFormat && (m_Streams[i]->m_uiFormatSize >= 2))
275 wFormatTag = ((WAVEFORMATEX*)(m_Streams[i]->m_pcFormat))->wFormatTag;
276 else
277 wFormatTag = (short)m_Streams[i]->m_Header.fccHandler;
278 strncpy(b, avm_wave_format_name(wFormatTag), sizeof(b));
279 b[sizeof(b) - 1] = 0;
280 }
281 else
282 {
283 wFormatTag = (short)m_Streams[i]->m_Header.fccHandler;
284 memcpy(b, &m_Streams[i]->m_Header.fccHandler, 4);
285 b[4] = 0;
286 }
287
288 // clip the size of allocated vectors
289 m_Streams[i]->m_Offsets.resize(m_Streams[i]->m_Offsets.size());
290 m_Streams[i]->m_Positions.resize(m_Streams[i]->m_Positions.size());
291
292 char fcctype[4];
293 avm_set_le32(fcctype, m_Streams[i]->m_Header.fccType);
294
295 AVM_WRITE("AVI reader", "Stream %d %.4s : %s (0x%hx) %" PRIsz " chunks (%.2fKB)\n",
296 (int)i, fcctype, b, wFormatTag, m_Streams[i]->m_Offsets.size(),
297 (double)(m_Streams[i]->m_Offsets.size() * sizeof(uint32_t)
298 + m_Streams[i]->m_Positions.size() * sizeof(uint32_t)) / 1024.);
299
300 #if 0
301 AVM_WRITE("AVI reader", " Idx Offset Timestamp Flags\n");
302 AviReadStream* stream = m_Streams[i];
303 for (unsigned j = 0 ; j < stream->m_Offsets.size(); j++)
304 AVM_WRITE("AVI reader", "%6d %10d %10d %d\n",
305 j, stream->m_Offsets[j] & ~0x01,
306 (j < stream->m_Positions.size()) ? stream->m_Positions[j] :
307 (int)(j / stream->m_dFrameRate * 1000),
308 stream->m_Offsets[j] & 0x01);
309 #endif
310
311 //hack interleaved format - iavs
312 //if (m_Streams[i]->m_Header.fccType == mmioFOURCC('i', 'a', 'v', 's'))
313 // m_Streams[i]->m_Header.fccType = streamtypeVIDEO;
314
315 //m_Input.addStream(m_Streams[i]->m_iId, m_Streams[i]->m_Index);
316 m_Input.addStream(m_Streams[i]->m_iId, m_Streams[i]->m_Offsets);
317 }
318 m_Input.async();
319 return 0;
320 }
321
322 AviReadHandler::~AviReadHandler()
323 {
324 m_Input.close();
325 for (size_t i = 0; i < m_Streams.size(); ++i)
326 delete m_Streams[i];
327 }
328
329 size_t AviReadHandler::GetHeader(void* pheader, size_t size)
330 {
331 if (pheader && (size >= sizeof(AVIMainHeader)))
332 {
333 memset(pheader, 0, size);
334 *(AVIMainHeader*)pheader = m_MainHeader;
335 }
336 return sizeof(AVIMainHeader);
337 }
338
339 IMediaReadStream* AviReadHandler::GetStream(uint_t stream_id,
340 IStream::StreamType type)
341 {
342 uint_t match = 0;
343 avm::vector<AviReadStream*>::iterator it;
344 for (it = m_Streams.begin(); it != m_Streams.end(); ++it)
345 if ((*it)->GetType() == type)
346 {
347 if (match == stream_id)
348 return (*it);
349 match++;
350 }
351 return 0;
352 }
353
354 size_t AviReadHandler::GetStreamCount(IStream::StreamType type)
355 {
356 size_t cnt = 0;
357 avm::vector<AviReadStream*>::const_iterator it;
358 for (it = m_Streams.begin(); it != m_Streams.end(); ++it)
359 if ((*it)->GetType() == type)
360 cnt++;
361
362 return cnt;
363 }
364
365 void AviReadHandler::readInfoChunk(size_t data_size)
366 {
367 size_t rs = 0;
368 while (rs < data_size && !m_Input.eof())
369 {
370 uint32_t type = m_Input.readDword();
371 size_t size = m_Input.readDword();
372 //printf("rs r:%" PRIsz " ds:%" PRIsz " t:%d s:%" PRIsz "\n", rs, data_size, type, size);
373
374 if ((rs + size) < data_size)
375 {
376 size += (size & 1);
377 char b[size + 10];
378 m_Input.read(b, size);
379
380 for (const RiffInfoChunk* pr = infos; pr->riff; pr++)
381 if (pr->riff == type)
382 {
383 AVM_WRITE("AVI reader", "InfoChunk %s: %s\n", pr->text, b);
384 break;
385 }
386 }
387 //printf("addd %" PRIsz " %" PRIsz "\n", rs, size);
388 rs += size;
389 }
390 }
391
392 void AviReadHandler::readAVIMainHeader(size_t data_size)
393 {
394 size_t read_size = sizeof(AVIMainHeader);
395 data_size += (data_size & 1);
396
397 if (read_size < data_size)
398 {
399 memset(&m_MainHeader, 0, read_size);
400 AVM_WRITE("AVI reader", "WARNING: unexpected main header size\n");
401 }
402 else
403 read_size = data_size;
404
405 m_Input.read(&m_MainHeader, read_size);
406
407 avm_get_leAVIMainHeader(&m_MainHeader);
408 PrintAVIMainHeader(&m_MainHeader);
409 }
410
411 void AviReadHandler::readAVIStreamHeader(size_t data_size)
412 {
413 AVIStreamHeader ash;
414 size_t read_size = sizeof(ash);
415
416 data_size += (data_size & 1);
417 if (read_size < data_size)
418 {
419 memset(&ash, 0, read_size);
420 AVM_WRITE("AVI reader", "WARNING: unexpected stream header size (%" PRIsz ")\n", data_size);
421 }
422 else
423 read_size = data_size;
424
425 m_Input.read(&ash, read_size);
426 if (read_size < data_size)
427 m_Input.seekCur(data_size - read_size);
428
429 avm_get_leAVIStreamHeader(&ash);
430 //if (m_Header.dwStart && m_Header.dwStart)
431 // cout << "Calc starttime " << m_Header.dwStart * (m_Header.dwRate / (double)m_Header.dwStart) << endl;
432
433 if (m_Input.readDword() != ckidSTREAMFORMAT)
434 {
435 AVM_WRITE("AVI Reader", "WARNING: StreamFormat not found, stream ignored\n");
436 return; //strf
437 }
438
439 data_size = m_Input.readDword();
440 size_t fsize = data_size;
441 data_size += (data_size & 1); // align
442
443 vector<uint8_t> format(data_size);
444 if (m_Input.read(format.begin(), data_size) < (ssize_t)data_size)
445 {
446 AVM_WRITE("AVI Reader", "WARNING: Can not read StreamFormat, stream ignored\n");
447 return;
448 }
449
450 if (ash.fccType == streamtypeVIDEO || ash.fccType == streamtypeIAVS)
451 {
452 const size_t bihs = sizeof(BITMAPINFOHEADER);
453 if (data_size < bihs)
454 {
455 format.resize(bihs);
456 memset(&format[data_size], 0, bihs - data_size);
457 fsize = data_size = bihs;
458 }
459
460 BITMAPINFOHEADER* bih = avm_get_leBITMAPINFOHEADER(format.begin());
461 //AVM_WRITE("AVI reader", "format %x %.4s %d\n",
462 // s.m_Header.fccHandler, (char*)&s.m_Header.fccHandler, data_size);
463 if (bih->biCompression != ash.fccHandler)
464 {
465 AVM_WRITE("AVI Reader", "Info: fccHandler differs from biCompression!\n");
466 ash.fccHandler = bih->biCompression;
467 }
468 ash.dwSampleSize = 0;
469 }
470 else if (ash.fccType == streamtypeAUDIO)
471 {
472 size_t wfs = sizeof(WAVEFORMATEX) + ((WAVEFORMATEX*)format.begin())->cbSize;
473 if (data_size < wfs)
474 {
475 format.resize(wfs);
476 memset(&format[data_size], 0, wfs - data_size);
477 fsize = data_size = wfs;
478 }
479 avm_get_leWAVEFORMATEX(format.begin());
480 if (!ash.dwSampleSize && ash.dwLength > 800000)
481 {
482 // ((*(ashort*)s.m_pcFormat == WAVE_FORMAT_MPEGLAYER3)
483 // only if we have a lot of chunks
484 // if the stream would be VBR then ther would be far
485 // less chunks
486 AVM_WRITE("AVI reader", "WARNING: setting SampleSize=1 (Length was %d)\n", ash.dwLength);
487 ash.dwSampleSize = 1;
488 }
489 }
490
491 PrintAVIStreamHeader(&ash);
492 m_Streams.push_back(new AviReadStream(this, ash, (uint_t)m_Streams.size(),
493 format.begin(), fsize));
494 }
495
496 int AviReadHandler::readIndexChunk(size_t index_size, off_t movie_chunk_offset)
497 {
498 bool zero_based_offsets = false;
499
500 AVM_WRITE("AVI reader", "Reading index from offset: %" PRId64 "\n", m_Input.pos());
501
502 for (size_t ip = 0; ip <= (index_size/sizeof(AVIIndexEntry))
503 && !m_Input.eof(); ip++)
504 {
505 uint_t ckid = m_Input.readDword();
506 uint_t cflags = m_Input.readDword();
507 off_t coffset = m_Input.readDword();
508 uint32_t clen = m_Input.readDword();
509
510 uint_t id = StreamFromFOURCC(ckid);
511 if (id >= m_Streams.size())
512 continue;
513 AviReadStream* stream = m_Streams[id];
514
515 //printf ("Id %u off: %u len:%u\n", ip, entry.dwChunkOffset, entry.dwChunkLength);
516 if (clen > StreamPacket::MAX_PACKET_SIZE)
517 {
518 AVM_WRITE("AVI reader", "WARNING: invalid index entry %" PRIsz " -- id: %u "
519 "offset: %" PRId64 " size: %ud (pos: %" PRId64 ")\n",
520 ip, ckid, (int64_t)coffset, clen, (int64_t)m_Input.pos());
521 return reconstructIndexChunk(stream->m_Offsets.back() & ~1);
522 }
523
524 if (coffset < movie_chunk_offset)
525 zero_based_offsets = true;
526
527 if (zero_based_offsets)
528 coffset += movie_chunk_offset - 4;
529
530 stream->addChunk(coffset, clen, cflags & AVIIF_KEYFRAME);
531 }
532 //AVM_WRITE("AVI reader", "Avi Index entries: %d\n", m_Streams[0].m_Index.size());
533 //kprof_reset_stats();
534 return 0;
535 }
536
537 int AviReadHandler::reconstructIndexChunk(off_t offset)
538 {
539 m_Input.seek(offset);
540 int64_t positions[m_Streams.size()];
541 int isdivx[m_Streams.size()];
542
543 AVM_WRITE("AVI reader", "Reconstructing index from offset: %" PRId64 " s=%" PRIsz "\n", (int64_t)offset, m_Streams.size());
544 // fixing KeyFrames for DivX movies by checking flag bit
545
546 for (size_t i = 0; i < m_Streams.size(); ++i)
547 {
548 isdivx[i] = 0;
549
550 if (m_Streams[i]->GetType() == IStream::Video)
551 switch (m_Streams[i]->m_Header.fccHandler)
552 {
553 case fccDIV3:
554 case fccdiv3:
555 case fccDIV4:
556 case fccdiv4:
557 case fccDIV5:
558 case fccdiv5:
559 case fccDIV6:
560 case fccdiv6:
561 case fccMP43:
562 case fccmp43:
563 case fccMP42:
564 case fccmp42:
565 case fccAP41:
566 isdivx[i] = 1;
567 break;
568 case fccDX50:
569 case fccdx50:
570 case fccDIVX:
571 case fccdivx:
572 case fccDIV1:
573 case fccdiv1:
574 case fccMP4S:
575 case fccmp4s:
576 case fccXVID:
577 case 0x4:
578 isdivx[i] = 2;
579 break;
580 case fccWMV1:
581 case fccwmv1:
582 isdivx[i] = 3;
583 break;
584 }
585 }
586
587 // set when trying to reconstruct even very broken .avi files
588 bool seriously_broken = false;
589 while (!m_Input.eof())
590 {
591 off_t coffset = m_Input.pos();
592 uint_t ckid = m_Input.readDword();
593
594 if (ckid == ckidAVINEWINDEX)
595 break;
596 //if (!ckid) continue;
597 uint_t id = StreamFromFOURCC(ckid);
598 //uint_t ckidt = TWOCCFromFOURCC(ckid);
599
600 //printf("pos: %llu %u %u %x %d\n", m_Input.pos(), id, m_Streams.size(), ckid, seriouslyBroken);
601 if (id >= m_Streams.size())
602 {
603 //printf("index type id:%d ss:%d 0x%x 0x%x ckid:0x%x\n", id, m_Streams.size(), (ckid & 0xffff), cktypeINDEX, ckid);
604 if ((ckid & 0xffff) == cktypeINDEX)
605 {
606 // skip indexes
607 size_t size = m_Input.readDword();
608 //printf("SKEEEEEP %u\n", size);
609 if (size < ((seriously_broken) ? MAX_CHUNK_SIZE : 0xfffffffU))
610 m_Input.seekCur(size);
611 continue;
612 }
613
614 if (ckid && !m_Input.eof())
615 m_Input.seekCur(-2);
616 if (!seriously_broken) {
617 AVM_WRITE("AVI reader", "Trying to reconstruct broken avi stream (could take a lot of time)\n");
618 seriously_broken = true;
619 }
620 continue;
621 }
622
623 size_t clen = m_Input.readDword();
624 if (clen > ((seriously_broken) ? MAX_CHUNK_SIZE : 0xfffffffU))
625 continue;
626
627 size_t size = clen + (clen & 1); // round
628 bool keyframe = (size > 0);
629
630 if (isdivx[id] && (size > 4))
631 {
632 uint_t a = m_Input.readDword();
633 size -= 4;
634 switch (isdivx[id])
635 {
636 case 1:
637 if (a & 0x40)
638 keyframe = false;
639 break;
640 case 2:
641 // DivX4 keyframe detection
642 //AVM_WRITE("AVI reader", "Dword 0x%x\n", a);
643 if (a == 0xb6010000 || a == 0xb0010000)
644 {
645 // looks like new DivX4/5 frame
646 int b = m_Input.readByte();
647 size--;
648 keyframe = ((b & 0xc0) == 0);
649 // 0x00 Key frame, 0x40 P-frame
650 //printf("Byte 0x%x %d", b, b);
651 // for b001 it gives false keyframes -
652 // parser from ffmpeg will be needed
653 // to do a better job :(...
654 }
655 //else if (a == 0xb0010000) // old OpenDivX
656 // keyframe = true;
657 else if (a != 0x00010000) // old OpenDivX
658 keyframe = false;
659 break;
660 case 3:
661 if (!(a & 0x1))
662 keyframe = false;
663 //printf("keyframe %d %d\n", keyframe, size);
664 break;
665 }
666 }
667 //printf("SEEK %d\n", size);
668 if (m_Input.seekCur(size) > ssize_t(coffset + size))
669 // do not insert incomplete chunks
670 m_Streams[id]->addChunk(coffset, clen, keyframe);
671 }
672
673 return 0;
674 }
675
676 #if 0
677 AviStreamPacket* AviReadHandler::readPacket(bool fill)
678 {
679 int ckid = m_Input.readDword();
680 uint_t id = StreamFromFOURCC(ckid);
681 uint_t ckidt = TWOCCFromFOURCC(ckid);
682
683 int len = m_Input.readDword();
684 if (fill)
685 {
686 }
687 return 0;
688 }
689 #endif
690
691 void AviReadHandler::PrintAVIMainHeader(const AVIMainHeader* h)
692 {
693 char buffer[200];
694 AVM_WRITE("AVI reader", "MainHeader: MicroSecPerFrame=%d MaxBytesPerSec=%d\n"
695 " PaddingGranularity=%d Flags=[%s] TotalFrames=%d\n"
696 " InitialFrames=%d Streams=%d SuggestedBufferSize=%d WxH=%dx%d\n"
697 " Scale=%d Rate=%d Start=%d Length=%d\n",
698 h->dwMicroSecPerFrame, h->dwMaxBytesPerSec,
699
700 h->dwPaddingGranularity, GetAviFlags(buffer, h->dwFlags),
701 h->dwTotalFrames,
702
703 h->dwInitialFrames, h->dwStreams,
704 h->dwSuggestedBufferSize, h->dwWidth, h->dwHeight,
705 h->dwScale, h->dwRate, h->dwStart, h->dwLength);
706 }
707
708 void AviReadHandler::PrintAVIStreamHeader(const AVIStreamHeader* h)
709 {
710 char buffer[200];
711
712 char sb[20];
713 if (h->fccType == streamtypeVIDEO || h->fccType == streamtypeIAVS)
714 {
715 avm_set_le32(sb, h->fccHandler);
716 sb[4] = 0;
717 }
718 else
719 sprintf(sb, "0x%x", h->fccHandler);
720 char ft[4];
721 AVM_WRITE("AVI reader", "StreamHeader: Type=%.4s Handler=%s Flags=[%s]\n"
722 " InitialFrames=%d Scale=%d Rate=%d Start=%d Length=%d\n"
723 " SuggestedBufferSize=%d Quality=%d SampleSize=%d"
724 " Rect l,r,t,b=%d,%d,%d,%d\n",
725 avm_set_le32(ft, h->fccType), sb,
726
727 GetAviFlags(buffer, h->dwFlags),
728 h->dwInitialFrames, h->dwScale,
729 h->dwRate, h->dwStart, h->dwLength,
730
731 h->dwSuggestedBufferSize, h->dwQuality, h->dwSampleSize,
732 h->rcFrame.left, h->rcFrame.right, h->rcFrame.top,
733 h->rcFrame.bottom);
734 }
735
736 char* AviReadHandler::GetAviFlags(char* buffer, int flags)
737 {
738 sprintf(buffer, "%s%s%s%s%s%s ",
739 (flags & AVIF_HASINDEX) ? " HAS_INDEX" : "",
740 (flags & AVIF_MUSTUSEINDEX) ? " MUST_USE_INDEX" : "",
741 (flags & AVIF_ISINTERLEAVED) ? " IS_INTERLEAVED" : "",
742 (flags & AVIF_TRUSTCKTYPE) ? " TRUST_CKTYPE" : "",
743 (flags & AVIF_COPYRIGHTED) ? " COPYRIGHTED" : "",
744 (flags & AVIF_WASCAPTUREFILE) ? " WAS_CAPTURED_FILE" : ""
745 );
746 return buffer;
747 }
748
749 IMediaReadHandler* CreateAviReadHandler(const char *pszFile, int flags)
750 {
751 AviReadHandler* r = new AviReadHandler(flags);
752 if (r && r->init(pszFile) == 0)
753 return r;
754
755 delete r;
756 return 0;
757 }
758
759 AVM_END_NAMESPACE;