-
Notifications
You must be signed in to change notification settings - Fork 0
/
rdavi2.c
1386 lines (1111 loc) · 46.3 KB
/
rdavi2.c
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
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
RdAvi2.exe - Copyright (c) 2024 by Dennis Hawkins. All rights reserved.
Inspired by: ReadAvi.exe by Michael Kohn<[email protected]> (http://www.mikekohn.net/)
BSD License
Redistribution and use in source and binary forms are permitted provided
that the above copyright notice and this paragraph are duplicated in all
such forms and that any documentation, advertising materials, and other
materials related to such distribution and use acknowledge that the
software was developed by the copyright holder. The name of the copyright
holder may not be used to endorse or promote products derived from this
software without specific prior written permission. THIS SOFTWARE IS
PROVIDED `'AS IS? AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE.
Although not required, attribution is requested for any source code
used by others.
*/
#include "rdavi2.h"
static DWORD G_movi_offset = 0; // offset of the 'movi' tag
static int Level = 0;
static char indent[80] = {0};
#define CHARS_PER_TAB 2
// Take a numerical offset relative to the Base file address and return
// a pointer to an ascii string representing the full 64bit file location.
// If the base is 0, then only the 32bit file location is returned.
// For legacy files, this will always be 32bits.
// Because the string is returned as a pointer to a static buffer, this
// cannot be used more than once in a function call.
static char *GetOffsetStr(DWORD offset)
{
QWORD base = File64GetBase();
static char tmpstr[20];
if (base)
strcpy(tmpstr, QWORD2HEX(base + (QWORD) offset)); // Use 64 bit base
else sprintf(tmpstr, "%08X", offset); // 32 bit only
return(tmpstr);
}
// This function modifies the global string 'indent' by a making
// a spring of (CHARS_PER_TAB X Level) number of spaces. Level
// is also a global. The resulting string is used for output
// formatting purposes.
static char *MakeIndent(void)
{
int x;
memset(indent, ' ', sizeof(indent));
x = Level * CHARS_PER_TAB;
if (x >= sizeof(indent)) x = sizeof(indent) - 2;
indent[x] = 0;
return(indent);
}
// These functions are called before going deeper in the output nesting and
// after returning. They will print an opening and closing bracket as
// appropriate and adjust the nesting level.
static void OpenLevel(void)
{
printf("%s{\n", indent);
Level++;
MakeIndent();
}
static void CloseLevel(void)
{
Level--;
MakeIndent();
printf("%s}\n", indent);
}
// Produce a HEX dump for an output.
// Exactly chunk_len bytes are read and output.
// Return 0 on success and -1 if EOF.
// Output is suppressed after 16 lines.
#define HEXSTART (buf + 9)
#define CHARSTART (buf + 57)
#define ENDNULL 73
static int hex_dump_chunk(FILE *in, int chunk_len)
{
BYTE CharStr[17];
int n, i, linecnt = 0, bcnt, ret, printing = TRUE, poff;
DWORD offset = File64GetPos(in);
char buf[80], tmpstr[32];
/*
00000000 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 1234567890123456
*/
memset(buf, ' ', sizeof(buf));
buf[ENDNULL] = 0;
for (n = 0; n < chunk_len; )
{
// eject the previous line
if (linecnt++ == 16) printing = FALSE;
// start new line
memset(buf, ' ', ENDNULL); // clear print buffer
sprintf(tmpstr, "%08X ", offset & 0xFFFFFFF0); // print address
memcpy(buf, tmpstr, 8); // copy in address
poff = (offset & 0x0000000F);
bcnt = min(16 - poff, chunk_len - n);
offset += bcnt;
n += bcnt;
ret = File64Read(in, CharStr, bcnt);
if (ret != bcnt)
{
printf("*** Unexpected EOF ***\n");
return(-1);
}
if (poff) // fill in initial missing bytes
{
for (i = 0; i < 16; i++)
memcpy(HEXSTART + i * 3, "<>", 2);
}
// add hex and char bytes to buffer
for (i = 0; i < bcnt; i++)
{
BYTE ch = CharStr[i];
sprintf(tmpstr, "%02X", (DWORD) ch);
memcpy(HEXSTART + (i + poff) * 3, tmpstr, 2);
CHARSTART[i + poff] = (char)
((isprint(ch) && ch != 7 && ch < 128) ? ch : '.');
}
// print line
if (printing)
printf(" %s\n", buf); // print chars of previous line
}
// print anything left in buffer
if (!printing) printf("\n **TRUNCATED**\n");
return 0;
}
// Displays a legacy AVI index.
// Returns 0 on success and non-zero on failure.
// Output suppressed after 16 lines.
#define AVIIF_LIST 0x00000001L // chunk is a 'LIST'
#define AVIIF_KEYFRAME 0x00000010L // this frame is a key frame.
#define AVIIF_FIRSTPART 0x00000020L // this frame is the start of a partial frame.
#define AVIIF_LASTPART 0x00000040L // this frame is the end of a partial frame.
#define AVIIF_NO_TIME 0x00000100L // this frame doesn't take any time
#define AVIIF_COMPUSE 0x0FFF0000L
static int parse_idx1(FILE *in, int chunk_len)
{
AVIINDEXENTRY index_entry;
int t, br;
DWORD flags;
char buf[80];
printf("%sCkId Flags Location Length\n", indent);
printf("%s==== ============================== ========== ==========\n", indent);
for (t = 0; t < (int)(chunk_len / sizeof(AVIINDEXENTRY)); t++)
{
br = File64Read(in, &index_entry, sizeof(AVIINDEXENTRY));
if (br != sizeof(AVIINDEXENTRY))
{
printf("*** Unexpected EOF ***\n");
return(-1);
}
if (t < 16)
{
buf[0] = 0;
printf("%s%.4s ", indent, &index_entry.ckid);
flags = index_entry.dwFlags;
strcat(buf, (flags & AVIIF_KEYFRAME) ? "KEYFRM " : " ");
strcat(buf, (flags & AVIIF_LIST) ? "RECLIST " : " ");
strcat(buf, (flags & AVIIF_NO_TIME) ? "NOTIME " : " ");
strcat(buf, (flags & AVIIF_FIRSTPART) ? "1st " : " ");
strcat(buf, (flags & AVIIF_LASTPART) ? "LAST " : " ");
printf("%s 0x%08X 0x%08X\n", buf,
G_movi_offset - 4 + index_entry.dwChunkOffset,
index_entry.dwChunkLength);
}
}
if (t >= 16) printf("%s**Suppressed %d index entries**\n", indent, t - 16);
else printf("\n");
return 0;
}
// Read and print Main AVI Header.
// Return 0 on success or non-zero if not.
static int read_avi_header(FILE *in)
{
MainAVIHeader avi_header;
DWORD offset = File64GetPos(in);
DWORD flags, br;
char flagstr[64];
memset(flagstr, 0, sizeof(flagstr));
br = File64Read(in, &avi_header, sizeof(MainAVIHeader));
if (br != sizeof(MainAVIHeader)) return(-1);
// Format flag string
flags = avi_header.Flags;
if (flags & AVIF_HASINDEX) strcat(flagstr, "IDX "); //4
if (flags & AVIF_MUSTUSEINDEX) strcat(flagstr, "IDXREQ "); //7
if (flags & AVIF_ISINTERLEAVED) strcat(flagstr, "INTLV "); //6
if (flags & AVIF_WASCAPTUREFILE) strcat(flagstr, "CAPFILE ");//8
if (flags & AVIF_COPYRIGHTED) strcat(flagstr, "(c) "); //4
if (flags & AVIF_TRUSTCKTYPE) strcat(flagstr, "CKOK "); //5
if (flags == 0) strcpy(flagstr, "No Flags"); //9
printf(" offset=0x%lx\n", offset);
printf(" TimeBetweenFrames: %d\n", avi_header.MicroSecPerFrame);
printf(" MaximumDataRate: %d\n", avi_header.MaxBytesPerSec);
printf(" PaddingGranularity: %d\n", avi_header.PaddingGranularity);
printf(" Flags: %08x - %s\n", avi_header.Flags, flagstr);
printf(" TotalNumberOfFrames: %d\n", avi_header.TotalFrames);
printf(" NumberOfInitialFrames: %d\n", avi_header.InitialFrames);
printf(" NumberOfStreams: %d\n", avi_header.NumStreams);
printf(" SuggestedBufferSize: %d\n", avi_header.SuggestedBufferSize);
printf(" Width: %d\n", avi_header.Width);
printf(" Height: %d\n", avi_header.Height);
return 0;
}
// Stream Header.
// StrType is 0 for Video and 1 for audio streams.
// Return 0 if OK, or -1 on EOF.
static int read_stream_header(FILE *in, DWORD size)
{
AVIStreamHeader56 stream_header;
DWORD offset = File64GetPos(in);
DWORD br, flags;
char flagstr[32];
SMALL_RECT r;
// There are 3 different versions of AVIStreamHeader in use. Figure
// out which one based on length.
memset(&stream_header, 0, sizeof(AVIStreamHeader56));
if (size == sizeof(AVIStreamHeader48) || size == sizeof(AVIStreamHeader56))
{
br = File64Read(in, &stream_header, size);
if (br != size)
{
printf("**Unexpected End of File**\n");
return(-1);
}
}
else if (size == sizeof(AVIStreamHeader64))
{
AVIStreamHeader64 tHdr;
br = File64Read(in, &tHdr, sizeof(AVIStreamHeader64));
if (br != sizeof(AVIStreamHeader64))
{
printf("**Unexpected End of File**\n");
return(-1);
}
// convert to 56 byte version
memcpy(&stream_header, &tHdr, sizeof(AVIStreamHeader56));
stream_header.Frame.Top = (WORD) tHdr.Frame.top;
stream_header.Frame.Left = (WORD) tHdr.Frame.left;
stream_header.Frame.Bottom = (WORD) tHdr.Frame.bottom;
stream_header.Frame.Right = (WORD) tHdr.Frame.right;
}
else
{
printf("**Unknown structure type**\n");
return(-2); // unexpected structure size
}
r = stream_header.Frame;
flags = stream_header.Flags;
memset(flagstr, 0, sizeof(flagstr));
if (flags & AVISF_DISABLED) strcat(flagstr, "DISABLED ");
if (flags & AVISF_VIDEO_PALCHANGES) strcat(flagstr, "PALCHG");
if (flags == 0) strcpy(flagstr, "No Flags Set");
printf(" offset=0x%lx\n", offset);
printf(" Stream Header Version: %.4s (%d byte) version\n",
(char *)&stream_header.fccType, size);
printf(" FourCC Type: %.4s\n", (char *)&stream_header.fccType);
if (FIX_LIT(stream_header.fccType) == 'auds')
{
printf(" FourCC Handler: Not Used\n");
}
else
{
printf(" FourCC Handler: %.4s - %s\n",
(char *)&stream_header.fccHandler,
LookupFourCC(stream_header.fccHandler));
}
printf(" Flags: %08x - %s\n", stream_header.Flags, flagstr);
printf(" Priority: %d\n", stream_header.Priority);
printf(" InitialFrames: %d\n", stream_header.InitialFrames);
printf(" TimeScale: %d\n", stream_header.TimeScale);
printf(" DataRate: %d\n", stream_header.Rate);
printf(" StartTime: %d\n", stream_header.StartTime);
printf(" DataLength: %d\n", stream_header.Length);
printf(" SuggestedBufferSize: %d\n", stream_header.SuggestedBufferSize);
printf(" Quality: %d\n", stream_header.Quality);
printf(" SampleSize: %d\n", stream_header.SampleSize);
printf(" Frame: { Top: %d, Left: %d, Bottom: %d, Right: %d }\n",
r.Top, r.Left, r.Bottom, r.Right);
return 0;
}
// Read Video Stream Format
static int read_stream_format_vid(FILE *in, DWORD size)
{
STREAMFORMATVID stream_format;
VIDPALETTE pal[256];
DWORD offset = File64GetPos(in);
DWORD br, t;
// Read structure
memset(pal, 0, sizeof(pal));
if (size < sizeof(STREAMFORMATVID))
{
printf("*** Unexpected short chunk ***\n");
return(-1);
}
br = File64Read(in, &stream_format, sizeof(STREAMFORMATVID));
size -= br;
if (br != sizeof(STREAMFORMATVID))
{
printf("*** Unexpected End of File ***\n");
return(-1);
}
// Read Palette
if (stream_format.biClrUsed != 0)
{
// read palette but do not store
t = sizeof(VIDPALETTE) * stream_format.biClrUsed;
if (size < t)
{
printf("*** Unexpected short chunk ***\n");
return(-1);
}
br = File64Read(in, pal, t);
size -= br;
if (br != t)
{
printf("**Unexpected End of File**\n");
return(-1);
}
}
// Print structure
printf(" offset=0x%lx\n", offset);
printf(" header_size: %d\n", stream_format.header_size);
printf(" image_width: %d\n", stream_format.biWidth);
printf(" image_height: %d\n", stream_format.biHeight);
printf(" number_of_planes: %d\n", stream_format.biPlanes);
printf(" bits_per_pixel: %d\n", stream_format.bits_per_pixel);
printf(" compression_type: %.4s - %s\n",
(char *) &stream_format.biCompression,
LookupFourCC(stream_format.biCompression));
printf(" image_size_in_bytes: %d\n", stream_format.biSizeImage);
printf(" x_pels_per_meter: %d\n", stream_format.biXPelsPerMeter);
printf(" y_pels_per_meter: %d\n", stream_format.biYPelsPerMeter);
printf(" colors_used: %d\n", stream_format.biClrUsed);
printf(" colors_important: %d\n", stream_format.biClrImportant);
// print palette
if (stream_format.biClrUsed != 0)
{
int i;
printf("\n%sVideo Palette:\n%s", indent, indent);
printf("### RR:GG:BB ### RR:GG:BB ### RR:GG:BB ### RR:GG:BB\n");
for (i = 0; i < (int) stream_format.biClrUsed; i++)
{
printf("%3d %2X:%2X:%2X ", i,
pal[i].rgbRed, pal[i].rgbGreen, pal[i].rgbBlue);
if (i % 4 == 3) printf("\n");
}
printf("\n");
}
if (size) // error - extra stuff at end that we don't understand
{
printf("%sUnrecognized Bitmap Header Extension!!\n", indent);
hex_dump_chunk(in, size);
// File64SetPos(in, size, SEEK_CUR);
}
return 0;
}
// Read stream format structure for audio
static int read_stream_format_auds(FILE *in, int size)
{
STREAMFORMATAUD stream_format;
MP3EXT mp3fmt;
AUDIOEXTENSION extfmt;
DWORD offset = File64GetPos(in);
DWORD br, rl;
memset(&stream_format, 0, sizeof(STREAMFORMATAUD));
// structure could be either WAVEFORMATEX or WAVEFORMATEXTENSIBLE
// If the FormatTag field is 1 (PCM), then the cbSize field can be omitted.
// This results in a shorter structure and is why we go by the chunk
// size here instead of just the structure size. For a short structure,
// cbSize will read as zero due to being initialized that way
rl = min(sizeof(STREAMFORMATAUD), size);
br = File64Read(in, &stream_format, rl);
if (br != rl)
{
printf("*** Unexpected End of File ***\n");
return(-1);
}
printf(" offset=0x%lx\n", offset);
printf(" format: 0x%04X - %s\n",
stream_format.wFormatTag,
LookupFormat(stream_format.wFormatTag));
printf(" channels: %d\n", stream_format.nChannels);
printf(" samples_per_second: %d\n", stream_format.nSamplesPerSec);
printf(" bytes_per_second: %d\n", stream_format.nAvgBytesPerSec);
printf(" block_size_of_data: %d\n", stream_format.nBlockAlign);
printf(" bits_per_sample: %d\n", stream_format.wBitsPerSample);
printf(" Extensible bytes: %d\n", stream_format.cbSize);
if (stream_format.cbSize) // there are additional bytes that follow
{
size -= br;
if (stream_format.wFormatTag == 0x0055) // mp3
{
rl = min(sizeof(MP3EXT), size);
memset(&mp3fmt, 0, sizeof(mp3fmt));
br = File64Read(in, &mp3fmt, rl);
if (br != rl)
{
printf("*** Unexpected End of File ***\n");
return(-1);
}
printf(" wID: %d\n", mp3fmt.wID);
printf(" fdwFlags: 0x%08X\n", mp3fmt.fdwFlags);
printf(" nBlockSize: %d\n", mp3fmt.nBlockSize);
printf(" nFramesPerBlock: %d\n", mp3fmt.nFramesPerBlock);
printf(" nCodecDelay: %d\n", mp3fmt.nCodecDelay);
}
else if (stream_format.cbSize == sizeof(AUDIOEXTENSION))
{
// its just a guess, but if cbSize is 22 bytes,, then its likely
// a standard audio extension.
GUID t;
rl = min(sizeof(AUDIOEXTENSION), size);
memset(&extfmt, 0, sizeof(extfmt));
br = File64Read(in, &extfmt, rl);
if (br != rl)
{
printf("*** Unexpected End of File ***\n");
return(-1);
}
printf(" Valid_Bits_per_Sample: %d\n", extfmt.Samples.wReserved);
printf(" Samples_per_Block: %d\n", extfmt.Samples.wReserved);
printf(" Channel_Mask: %d\n", extfmt.dwChannelMask);
printf(" Subformat GUID: ");
// {00000000-0000-0000-0000-000000000000 }
t = extfmt.SubFormat;
printf("{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
t.Data1, t.Data2, t.Data3,
t.Data4[0], t.Data4[1],
t.Data4[2], t.Data4[3],
t.Data4[4], t.Data4[5],
t.Data4[6], t.Data4[7]);
}
else
{
hex_dump_chunk(in, size);
}
}
return 0;
}
// Read stream format for closed captioning
// Placeholder - not really supported
static int read_stream_format_txts(FILE *in, int size)
{
return(hex_dump_chunk(in, size));
}
// Display an open-DML index
static int ProcessIndx(FILE *in, int chunk_size)
{
INDX_CHUNK idx; // Generic open-dml index header
DWORD rb, irb, BytesLeft, br, pad, casetype;
int i, max;
// Read base structure if open-dml index
memset(&idx, 0, sizeof(idx));
rb = min(sizeof(idx), chunk_size);
BytesLeft = chunk_size - rb;
br = File64Read(in, &idx, rb);
if (br != rb)
{
printf("*** Unexpected End of File.\n");
return(-1);
}
pad = chunk_size - sizeof(idx) - (idx.wLongsPerEntry * 4 * idx.nEntriesInUse);
irb = idx.wLongsPerEntry * 4; // bytes to read for one index entry
// Modify indexType to include subtype for use in switch() below
casetype = idx.bIndexType;
if (casetype == AVI_INDEX_OF_CHUNKS)
casetype |= (idx.bIndexSubType << 8);
switch (casetype)
{
case AVI_INDEX_OF_INDEXES:
printf("%sThis is an Open-DML SuperIndex of Indexes", indent);
if (sizeof(SUPERINDEXENTRY) != irb)
{
printf("%swLongsPerEntry is not correct for this type "
"of index.\n", indent);
irb = min(sizeof(SUPERINDEXENTRY), irb);
}
printf(" for the stream '%.4s'.\n", (char *) &idx.dwChunkId);
printf("%sEach index entry has %d bytes ", indent, idx.wLongsPerEntry * 4);
printf("with %d entries in use.\n\n", idx.nEntriesInUse);
printf("%sAbsolute Location Size Duration\n", indent);
printf("%s================== ========== ==========\n", indent);
for (i = 0; i < (int) idx.nEntriesInUse; i++)
{
SUPERINDEXENTRY entry;
memset(&entry, 0, sizeof(entry));
BytesLeft -= irb;
br = File64Read(in, &entry, irb);
if (br != irb)
{
printf("*** Unexpected End of File.\n");
return(-1);
}
printf("%s0x%s 0x%08X 0x%08X\n", indent,
QWORD2HEX(entry.qwOffset),
entry.dwSize, entry.dwDuration);
}
break;
case AVI_INDEX_OF_CHUNKS: // standard index
if (irb != sizeof(STDINDEXENTRY))
{
printf("%swLongsPerEntry is not correct for this type "
"of index.\n", indent);
irb = min(sizeof(STDINDEXENTRY), irb);
}
printf("%sAbsolute Location Size Keyframe\n", indent);
printf("%s================== ========== ========\n", indent);
max = min(16, idx.nEntriesInUse);
for (i = 0; i < (int) max; i++)
{
STDINDEXENTRY entry;
memset(&entry, 0, sizeof(entry));
BytesLeft -= irb;
br = File64Read(in, &entry, irb);
if (br != irb)
{
printf("*** Unexpected End of File.\n");
return(-1);
}
printf("%s0x%s 0x%08X %s\n", indent,
QWORD2HEX(idx.qwBaseOffset + (QWORD) entry.dwOffset),
entry.dwSize & 0x7FFFFFFF,
(entry.dwSize & 0x80000000) ? "NO" : "YES");
}
if (max != (int) idx.nEntriesInUse)
printf("%s**Suppressed %d index entries**\n", indent, idx.nEntriesInUse - max);
break;
case AVI_INDEX_OF_CHUNKS | (AVI_INDEX_2FIELD << 8): // field index
if (irb != sizeof(FIELDINDEXENTRY))
{
printf("%swLongsPerEntry is not correct for this type "
"of index.\n", indent);
irb = min(sizeof(FIELDINDEXENTRY), irb);
}
printf("%sAbsolute Location 2nd Field Loc Size Keyframe\n", indent);
printf("%s================== ================== ========== ========\n", indent);
max = min(16, idx.nEntriesInUse);
for (i = 0; i < (int) max; i++)
{
FIELDINDEXENTRY entry;
memset(&entry, 0, sizeof(entry));
BytesLeft -= irb;
br = File64Read(in, &entry, irb);
if (br != irb)
{
printf("*** Unexpected End of File.\n");
return(-1);
}
// QWORD2HEX() cannot be called more than once at a time.
printf("%s0x%s ", indent,
QWORD2HEX(idx.qwBaseOffset + (QWORD) entry.dwOffset));
printf("0x%s 0x%08X %s\n",
QWORD2HEX(idx.qwBaseOffset + (QWORD) entry.dwOffsetField2),
entry.dwSize & 0x7FFFFFFF,
(entry.dwSize & 0x80000000) ? "NO" : "YES");
}
if (max != (int) idx.nEntriesInUse)
printf("%s**Suppressed %d index entries**\n", indent, idx.nEntriesInUse - max);
break;
case AVI_INDEX_IS_DATA: // not really supported
printf("Data containing Index\n");
break;
default:
printf("Index of an unknown type (0x%02X)", (DWORD) idx.bIndexType);
break;
}
printf("\n");
if (pad)
printf("%sThis index contains %d extra bytes of padding.\n", indent, pad);
if (BytesLeft)
File64SetPos(in, BytesLeft, SEEK_CUR);
return(0);
}
// Parse and display frames in the movi list.
// It will call itself recursively is a 'LIST rec' is encountered.
static int parse_movi(FILE *in, DWORD size)
{
FOURCC movi_fcc, NewListName;
DWORD movi_size, offset, file_movi_size;
QWORD AbsLoc, filebase;
int stream, adv, ret;
DWORD cnt = 4; // 4 for 'movi'
int dcCnt, txCnt, wbCnt, pcCnt, ix2Cnt, defCnt; // ix1Cnt,
char fccbuf[8], *fccptr, ChunkDesc[64];
dcCnt = txCnt = wbCnt = pcCnt = ix2Cnt = defCnt = 0; // ix1Cnt = 0;
filebase = File64GetBase();
// print header
printf("\n%sCkId Chunk Type Absolute Location Length\n", indent);
printf( "%s==== ======================== ================== ==========\n", indent);
while (cnt < size)
{
offset = File64GetPos(in);
movi_fcc = ReadFCC(in, &stream);
movi_size = read_long(in);
AbsLoc = filebase + (QWORD) offset;
// reconstitute fourcc
fccptr = (char *) &movi_fcc;
fccbuf[0] = 0;
if (stream != -1) // stream number included
{
if (FIX_LIT(movi_fcc) == 'ix##')
sprintf(fccbuf, "ix%02X", stream);
else
sprintf(fccbuf, "%02X%.2s", stream, fccptr + 2);
}
else // stream number not included
{
memcpy(fccbuf, &movi_fcc, 4);
fccbuf[4] = 0;
}
// There is a poorly documented fact regarding chunks that says they
// must be WORD aligned. So chunk_size refers to the size of the chunk
// and not to the number of bytes on the disk. Bump up the file
// pointer by one byte if the chunk_size is an odd number.
file_movi_size = movi_size;
if (file_movi_size & 0x00000001) file_movi_size++;
adv = file_movi_size + 8; // default advance
ChunkDesc[0] = 0;
switch (FIX_LIT(movi_fcc))
{
case 'LIST':
NewListName = ReadFCC(in, NULL); // should be 'rec ', but we handle them all
printf("%sLIST '%.4s' (Location=0x%s length=0x%08X)\n",
indent, (char *)&NewListName,
QWORD2HEX(File64GetBase() + (QWORD) offset), movi_size);
OpenLevel();
ret = parse_movi(in, file_movi_size);
CloseLevel();
adv = 0; // file advancing already done
cnt += file_movi_size + 8;
if (ret) return(ret);
break;
case '##db':
if (dcCnt++ < 16) strcpy(ChunkDesc, "Uncompressed Video");
break;
case '##dc':
if (dcCnt++ < 16) strcpy(ChunkDesc, "Compressed Video");
break;
case '##tx':
if (txCnt++ < 16) strcpy(ChunkDesc, "Subtitle Text");
break;
case '##wb':
if (wbCnt++ < 16) strcpy(ChunkDesc, "Audio");
break;
case '##pc':
if (pcCnt++ < 16) strcpy(ChunkDesc, "Palette Change");
break;
case 'ix##':
// peek at index type
{
INDX_CHUNK idx;
int rb;
char tmpstr[32];
// Read base structure if open-dml index
memset(&idx, 0, sizeof(idx));
rb = min(sizeof(idx), file_movi_size);
rb = File64Read(in, &idx, rb);
strcpy(tmpstr, "ODML Standard Index");
if (idx.bIndexSubType == AVI_INDEX_2FIELD)
strcpy(tmpstr, "ODML Frame Index");
printf("%s%s %.4s %-19s 0x%s 0x%08X\n",
indent, fccbuf, (char *)&idx.dwChunkId,
tmpstr, QWORD2HEX(AbsLoc), movi_size);
// return to previous position
File64SetPos(in, -rb, SEEK_CUR);
}
OpenLevel();
ret = ProcessIndx(in, file_movi_size);
// ret = hex_dump_chunk(in, movi_size);
CloseLevel();
adv = 0; // file advancing already done
cnt += file_movi_size + 8;
if (ret) return(ret);
break;
case '##ix':
if (ix2Cnt++ < 16) strcpy(ChunkDesc, "Data chunk for timecode stream");
break;
case 'JUNK':
strcpy(ChunkDesc, "Wasted Space");
break;
default:
if (defCnt++ < 16) strcpy(ChunkDesc, "Unsupported FourCC tag");
break;
}
if (ChunkDesc[0])
{
printf("%s%s %-24s 0x%s 0x%08X\n",
indent, fccbuf, ChunkDesc, QWORD2HEX(AbsLoc), movi_size);
}
if (adv)
{
cnt += adv;
File64SetPos(in, file_movi_size, SEEK_CUR);
}
}
if (dcCnt > 16) printf("%s**Suppressed %d video frames**\n", indent, dcCnt - 16);
if (wbCnt > 16) printf("%s**Suppressed %d audio frames**\n", indent, wbCnt - 16);
printf("\n");
return(0);
}
// Display the VPRP Video Property Header
static int ProcessVPRP(FILE *in, int chunk_size)
{
DWORD i, s, t;
VideoPropHeader vprp;
VIDEO_FIELD_DESC vfld;
static char *VidTokStr[] =
{
"FORMAT_UNKNOWN", "FORMAT_PAL_SQUARE", "FORMAT_PAL_CCIR_601",
"FORMAT_NTSC_SQUARE", "FORMAT_NTSC_CCIR_601", NULL
};
static char *VidStdStr[] =
{
"STANDARD_UNKNOWN", "STANDARD_PAL", "STANDARD_NTSC",
"STANDARD_SECAM", NULL
};
File64Read(in, (char *) &vprp, sizeof(VideoPropHeader));
// File64SetPos(in, sizeof(VideoPropHeader), SEEK_CUR); vprp.nbFieldPerFrame=1;
t = vprp.VideoFormatToken;
printf(" Video Format Token: %d - %s\n", t, (t < 5) ? VidTokStr[t] : "INVALID");
t = vprp.VideoStandard;
printf(" Video standard: %d - %s\n", t, (t < 4) ? VidStdStr[t] : "INVALID");
printf(" Vertical refresh rate: %d\n", vprp.dwVerticalRefreshRate);
printf(" Horizontal Total in T: %d\n", vprp.dwHTotalInT);
printf(" Vertical Total in Lines: %d\n", vprp.dwVTotalInLines);
t = vprp.dwFrameAspectRatio;
printf(" FrameAspect Ratio: %d:%d\n", (t & 0xFFFF0000) >> 16, t & 0x0000FFFF);
printf(" Active Frame Width in Pixels : %d\n", vprp.dwFrameWidthInPixels);
printf(" Active Frame Height in Lines : %d\n", vprp.dwFrameHeightInLines);
printf(" Number of Fields Per Frame : %d\n", vprp.nbFieldPerFrame);
// Number of fields is usually one or two
s = sizeof(VideoPropHeader);
for (i = 0; i < vprp.nbFieldPerFrame && s < (DWORD) chunk_size; i++)
{
File64Read(in, (char *) &vfld, sizeof(VIDEO_FIELD_DESC));
// File64SetPos(in, sizeof(VIDEO_FIELD_DESC), SEEK_CUR);
s += sizeof(VIDEO_FIELD_DESC);
printf("\n Video Field #%d Description\n", i);
printf(" Compressed Bitmap Size (WxH): %d X %d\n", vfld.CompressedBMWidth, vfld.CompressedBMHeight);
printf(" Valid Bitmap Size (WxH) : %d X %d\n", vfld.ValidBMWidth, vfld.ValidBMHeight);
printf(" Valid Bitmap Offet (X,Y): %d, %d\n", vfld.ValidBMXOffset, vfld.ValidBMYOffset);
printf(" Valid X-Offset In T : %d\n", vfld.VideoXOffsetInT);
printf(" Valid Y Start Line : %d\n", vfld.VideoYValidStartLine);
}
return(0);
}
// Read a null terminated string from the file and then display it.
// Non-printable characters are converted to spaces.
static int ProcessString(FILE *in, int chunk_size)
{
char buffer[32];
int BytesLeft = chunk_size;
int br, rt, i;
printf("\"");
while (BytesLeft)
{
memset(buffer, 0, sizeof(buffer));
br = min(sizeof(buffer) - 1, BytesLeft);
BytesLeft -= br;
rt = File64Read(in, buffer, br);
if (rt != br) // EOF
{
printf("*** Unexpected EOF\n");
return(-1);
}
// get rid of unprintable characters
for (i = 0; buffer[i]; i++)
if (!isprint(buffer[i])) buffer[i] = ' ';
printf("%s", buffer);
}
printf("\"\n");
return(0);
}
// Special INFO List Processor
// As far as I can tell, there are several possible elements for the INFO
// list and all of them are null terminated strings.
static int ProcessINFO(FILE *in, int chunk_size)
{
DWORD offset = File64GetPos(in);
DWORD end_of_chunk = offset + chunk_size - 4;
DWORD InfoName, InfoSize;
int ret;
while (offset < end_of_chunk)
{
InfoName = ReadFCC(in, NULL);
InfoSize = read_long(in); // length of list element
if (InfoSize & 0x00000001) InfoSize++;
printf("%s%s(%.4s): ", indent, LookupINFO(InfoName), (char *)&InfoName);
ret = ProcessString(in, InfoSize);
if (ret) return(ret);
offset = File64GetPos(in); // current offset
}
return(0);
}
// Read the DMLH header info
// The DML specs say that the DMLH structure contains only one DWORD, but
// every implementation also adds a 61 DWORD "reserved" field. This function
// will handle all sizes.
static int ProcessDmlh(FILE *in, int chunk_size)
{