summaryrefslogtreecommitdiffstats
path: root/debian/transcode/transcode-1.1.7/avilib/avidump.c
blob: 498822d1b762583a042af43bccc4cdacac710702 (plain)
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
/*
 *  avidump.c
 *
 *  Copyright (C) Thomas Oestreich - June 2001
 *
 *  based on code:
 *  (c)94 UP-Vision Computergrafik for c't
 *  Extracts some infos from RIFF files, modified by Gerd Knorr.
 *
 *  This file is part of transcode, a video stream processing tool
 *
 *  transcode is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  transcode is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

#include "avilib.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "libtc/xio.h"

//#define AVI_DEBUG

#ifdef HAVE_ENDIAN_H
# include <endian.h>
#endif
#ifdef WORDS_BIGENDIAN
# define SWAP2(x) (((x>>8) & 0x00ff) |\
                   ((x<<8) & 0xff00))

# define SWAP4(x) (((x>>24) & 0x000000ff) |\
                   ((x>>8)  & 0x0000ff00) |\
                   ((x<<8)  & 0x00ff0000) |\
                   ((x<<24) & 0xff000000))
# define SWAP8(x) (( (SWAP4(x)<<32) & 0xffffffff00000000ULL) |\
                   ( SWAP4(x)))
#else
# define SWAP2(a) (a)
# define SWAP4(a) (a)
# define SWAP8(a) (a)
#endif

typedef uint32_t DWORD;
typedef uint16_t WORD;
typedef DWORD FOURCC;             /* Type of FOUR Character Codes */
typedef uint8_t boolean;
#define TRUE  1
#define FALSE 0
#define BUFSIZE 4096

/* Macro to convert expressions of form 'F','O','U','R' to
   numbers of type FOURCC: */

#if BYTE_ORDER == BIG_ENDIAN
# define MAKEFOURCC(a,b,c,d) ((((DWORD)a)<<24) | (((DWORD)b)<<16) | \
                              (((DWORD)c)<< 8) | ( (DWORD)d)      )
#else
# define MAKEFOURCC(a,b,c,d) ( ((DWORD)a)      | (((DWORD)b)<< 8) | \
                              (((DWORD)c)<<16) | (((DWORD)d)<<24)  )
#endif

/* The only FOURCCs interpreted by this program: */

#define RIFFtag MAKEFOURCC('R','I','F','F')
#define LISTtag MAKEFOURCC('L','I','S','T')
#define avihtag MAKEFOURCC('a','v','i','h')
#define strhtag MAKEFOURCC('s','t','r','h')
#define strftag MAKEFOURCC('s','t','r','f')
#define vidstag MAKEFOURCC('v','i','d','s')
#define audstag MAKEFOURCC('a','u','d','s')
#define dmlhtag MAKEFOURCC('d','m','l','h')
#define idx1tag MAKEFOURCC('i','d','x','1')
#define Tag00db MAKEFOURCC('0','0','d','b')
#define Tag00dc MAKEFOURCC('0','0','d','c')
#define Tag01db MAKEFOURCC('0','1','d','b')
#define Tag01dc MAKEFOURCC('0','1','d','c')
#define Tag00wb MAKEFOURCC('0','0','w','b')
#define Tag01wb MAKEFOURCC('0','1','w','b')
#define Tagwave MAKEFOURCC('f','m','t',' ')
#define Tagdata MAKEFOURCC('d','a','t','a')
#define TagDATA MAKEFOURCC('D','A','T','A')
#define indxtag MAKEFOURCC('i','n','d','x')
#define Tag00__ MAKEFOURCC('0','0','_','_')
#define Tagix00 MAKEFOURCC('i','x','0','0')
#define Tagix01 MAKEFOURCC('i','x','0','1')
#define Tagix02 MAKEFOURCC('i','x','0','2')
#define Tagix03 MAKEFOURCC('i','x','0','3')

/* Build a string from a FOURCC number
   (s must have room for at least 5 chars) */

static void FOURCC2Str(FOURCC fcc, char* s)
{
#if BYTE_ORDER == BIG_ENDIAN
    s[0]=(fcc >> 24) & 0xFF;
    s[1]=(fcc >> 16) & 0xFF;
    s[2]=(fcc >>  8) & 0xFF;
    s[3]=(fcc      ) & 0xFF;
#else
    s[0]=(fcc      ) & 0xFF;
    s[1]=(fcc >>  8) & 0xFF;
    s[2]=(fcc >> 16) & 0xFF;
    s[3]=(fcc >> 24) & 0xFF;
#endif
    s[4]=0;
}

static DWORD fcc_type;

#define EoLST  0
#define INT32  1
#define INT16  2
#define FLAGS  3
#define CCODE  4
#define HEX16  5
#define _TAG   6
#define INT8   7
#define INT64  8

struct FLAGLIST {
    int  bit;
    char *name;
};

struct VAL {
    int  type;
    char *name;
    struct FLAGLIST *flags;
};

struct FLAGLIST flags_avih[] = {
	{ 0x00000010, "hasindex" },
	{ 0x00000020, "useindex" },
	{ 0x00000100, "interleaved" },
	{ 0x00010000, "for_capture" },
	{ 0x00020000, "copyrighted" },
	{ 0, NULL }
};

struct VAL names_avih[] = {
    { INT32,  "us_frame" },
    { INT32,  "max_bps" },
    { INT32,  "pad_gran" },
    { FLAGS,  "flags", flags_avih },
    { INT32,  "tot_frames" },
    { INT32,  "init_frames" },
    { INT32,  "streams" },
    { INT32,  "sug_bsize" },
    { INT32,  "width" },
    { INT32,  "height" },
    { INT32,  "scale" },
    { INT32,  "rate" },
    { INT32,  "start" },
    { INT32,  "length" },
    { EoLST,  NULL }
};

struct VAL names_strh[] = {
    { CCODE,  "fcc_handler" },
    { FLAGS,  "flags" },
    { INT32,  "priority" },
    { INT32,  "init_frames" },
    { INT32,  "scale" },
    { INT32,  "rate" },
    { INT32,  "start" },
    { INT32,  "length" },
    { INT32,  "sug_bsize" },
    { INT32,  "quality" },
    { INT32,  "samp_size" },
    { EoLST,  NULL }
};

struct VAL names_strf_vids[] = {
    { INT32,  "size" },
    { INT32,  "width" },
    { INT32,  "height" },
    { INT16,  "planes" },
    { INT16,  "bit_cnt" },
    { CCODE,  "compression" },
    { INT32,  "image_size" },
    { INT32,  "xpels_meter" },
    { INT32,  "ypels_meter" },
    { INT32,  "num_colors" },
    { INT32,  "imp_colors" },
    { EoLST,  NULL }
};

struct VAL names_strf_auds[] = {
    { HEX16,  "format" },
    { INT16,  "channels" },
    { INT32,  "rate" },
    { INT32,  "av_bps" },
    { INT16,  "blockalign" },
    { INT16,  "bits" },
    { INT16,  "cbSize" },
    { INT16,  "wID" },
    { INT32,  "fdwFlags" },
    { INT16,  "nBlockSize" },
    { INT16,  "nFramesPerBlock" },
    { INT16,  "nCodecDelay" },
    { EoLST,  NULL }
};

struct VAL names_dmlh[] = {
    { INT32,  "frames" },
    { EoLST,  NULL }
};

struct VAL names_idx1[] = {
    { _TAG,  "tag" },
    { INT32,  "flags" },
    { INT32,  "pos" },
    { INT32,  "length" },
    { EoLST,  NULL }
};
struct VAL names_indx[] = {
    { INT16, "longs/entry"},
    { INT8, "index_sub_t" },
    { INT8, "index_t" },
    { INT32, "entries_used"},
    { CCODE, "fcc_handler"},
    { INT32, "reserved1"},
    { INT32, "reserved2"},
    { INT32, "reserved3"},
    { EoLST,  NULL }
};
struct VAL names_stdidx[] = {
    { INT16, "longs/entry"},
    { INT8, "index_sub_t" },
    { INT8, "index_t" },
    { INT32, "entries_used"},
    { CCODE, "fcc_handler"},
    { INT64, "base_offset"},
    { INT32, "reserved3"},
    { EoLST,  NULL }
};


#define MAX_BUF (4096)
static char buffer[MAX_BUF];

static size_t avi_read(int fd, char *buf, size_t len)
{
   size_t n = 0;
   size_t r = 0;

   while (r < len) {
      n = xio_read (fd, buf + r, len - r);

      if (n <= 0)
	  return r;
      r += n;
   }

   return r;
}

static size_t avi_write (int fd, char *buf, size_t len)
{
   size_t n = 0;
   size_t r = 0;

   while (r < len) {
      n = xio_write (fd, buf + r, len - r);
      if (n < 0)
         return n;

      r += n;
   }
   return r;
}

static size_t avi_read_write (int fd_in, int fd_out, size_t len)
{

  size_t w=len, z=0;

  while(w>0) {

    if(w>MAX_BUF) {
      z=avi_read(fd_in, buffer, MAX_BUF);
      if(z<0) return(z);
      z=avi_write(fd_out, buffer, MAX_BUF);
      if(z<0) return(z);
      w -=MAX_BUF;
    } else {
      z=avi_read(fd_in, buffer, w);
      if(z<0) return(z);
      z=avi_write(fd_out, buffer, w);
      if(z<0) return(z);
      w = 0;
    }
  } //data > 0

  return(len);
}


static void dump_vals(int fd, int count, struct VAL *names)
{
    uint32_t i,j,val32;
    uint16_t val16;
    loff_t val64;
    char val8;

    for (i = 0; names[i].type != EoLST; i++) {
	switch (names[i].type) {
	case INT8:
	    xio_read(fd, &val8, 1);
	    printf("\t%-12s = %d\n",names[i].name,val8);
	    break;
	case INT64:
	    xio_read(fd, &val64, 8);
	    val64 = SWAP8(val64);
	    printf("\t%-12s = 0x%016llx\n",names[i].name, (unsigned long long)val64);
	    break;
	case INT32:
	    xio_read(fd, &val32, 4);
	    val32 = SWAP4(val32);
	    printf("\t%-12s = %ld\n",names[i].name,(long)val32);
	    break;
	case CCODE:
	    xio_read(fd, &val32,4);
	    val32 = SWAP4(val32);
	    if (val32) {
		printf("\t%-12s = %c%c%c%c (0x%lx)\n",names[i].name,
		       (int)( val32        & 0xff),
		       (int)((val32 >>  8) & 0xff),
		       (int)((val32 >> 16) & 0xff),
		       (int)((val32 >> 24) & 0xff),
		       (long)val32);
	    } else {
		printf("\t%-12s = unset (0)\n",names[i].name);
	    }
	    break;
	case _TAG:
	    xio_read(fd, &val32,4);
	    val32 = SWAP4(val32);
	    if (val32) {
		printf("\t%-12s = %c%c%c%c\n",names[i].name,
		       (int)( val32        & 0xff),
		       (int)((val32 >>  8) & 0xff),
		       (int)((val32 >> 16) & 0xff),
		       (int)((val32 >> 24) & 0xff));
	    } else {
		printf("\t%-12s = unset (0)\n",names[i].name);
	    }
	    break;
	case FLAGS:
	    xio_read(fd, &val32,4);
	    val32 = SWAP4(val32);
	    printf("\t%-12s = 0x%lx\n",names[i].name,(long)val32);
	    if (names[i].flags) {
		for (j = 0; names[i].flags[j].bit != 0; j++)
		    if (names[i].flags[j].bit & val32)
			printf("\t\t0x%x: %s\n",
			       names[i].flags[j].bit,names[i].flags[j].name);
	    }
	    break;
	case INT16:
	    xio_read(fd, &val16,2);
	    val16 = SWAP2(val16);
	    printf("\t%-12s = %u\n",names[i].name,(uint32_t)val16);
	    break;

	case HEX16:
	    xio_read(fd, &val16,2);
	    val16 = SWAP2(val16);
	    printf("\t%-12s = 0x%x\n",names[i].name,(uint32_t)val16);
	    break;
	}
    }
}

#if 0
static void hexlog(unsigned char *buf, int count)
{
    int l,i;

    for (l = 0; l < count; l+= 16) {
	printf("\t  ");
	for (i = l; i < l+16; i++) {
	    if (i < count)
		printf("%02x ",buf[i]);
	    else
		printf("   ");
	    if ((i%4) == 3)
		printf(" ");
	}
	for (i = l; i < l+16; i++) {
	    if (i < count)
		printf("%c",isprint(buf[i]) ? buf[i] : '.');
	}
	printf("\n");
    }
}
#endif
static unsigned char*
off_t_to_char(off_t val, int base, int len)
{
    static const char digit[] = "0123456789abcdef";
    static char outbuf[32];
    char *p = outbuf + sizeof(outbuf);
    int i;

    *(--p) = 0;
    for (i = 0; i < len || val > 0; i++) {
	*(--p) = digit[ val % base ];
	val = val / base;
    }
    return (unsigned char *)p;
}

/* Reads a chunk ID and the chunk's size from file f at actual
   file position : */

static boolean ReadChunkHead(int fd, FOURCC* ID, DWORD* size)
{
    if (!xio_read(fd, ID, sizeof(FOURCC))) return(FALSE);
    if (!xio_read(fd, size, sizeof(DWORD))) return(FALSE);

    *size = SWAP4(*size);

#ifdef AVI_DEBUG
    printf("ReadChunkHead size = %lu\n", *size);
#endif

    return(TRUE);
}

/* Processing of a chunk. (Will be called recursively!).
   Processes file f starting at position filepos.
   f contains filesize bytes.
   If DesiredTag!=0, ProcessChunk tests, whether the chunk begins
   with the DesiredTag. If the read chunk is not identical to
   DesiredTag, an error message is printed.
   RekDepth determines the recursion depth of the chunk.
   chunksize is set to the length of the chunk's data (excluding
   header and padding byte).
   ProcessChunk prints out information of the chunk to stdout
   and returns FALSE, if an error occured. */

static off_t datapos_tmp[8];

static boolean ProcessChunk(int fd, off_t filepos, off_t filesize,
			    FOURCC DesiredTag, int RekDepth,
			    DWORD* chunksize)
{
    char   tagstr[5];          /* FOURCC of chunk converted to string */
    FOURCC chunkid;            /* read FOURCC of chunk                */
    off_t  datapos;     /* position of data in file to process */

    off_t tt;

    if (filepos>filesize-1) {  /* Oops. Must be something wrong!      */
	printf("  *****  Error: Data would be behind end of file!\n");
    }

    tt=xio_lseek(fd, filepos, SEEK_SET); /* Go to desired file position!     */

#ifdef AVI_DEBUG
    printf("lseek64=%Lu/%Lu (%Lu)\n", tt, filepos, filesize);
#endif

    if (!ReadChunkHead(fd, &chunkid, chunksize)) {  /* read chunk header */
	printf("  *****  Error reading chunk at filepos 0x%s\n",
	       off_t_to_char(filepos,16,1));
	return(FALSE);
    }
    FOURCC2Str(chunkid,tagstr);       /* now we can PRINT the chunkid */
    if (DesiredTag) {                 /* do we have to test identity? */
	if (DesiredTag!=chunkid) {
	    char ds[5];
	    FOURCC2Str(DesiredTag,ds);
	    printf("\n\n *** Error: Expected chunk '%s', found '%s'\n",
		   ds,tagstr);
	    return(FALSE);
	}
    }

    datapos=filepos+sizeof(FOURCC)+sizeof(DWORD); /* here is the data */

    /* print out header: */
#ifdef AVI_DEBUG
    printf("(%Lu) %*c  ID:<%s>   Size: %lu\n",
	    filepos ,(RekDepth+1)*4,' ',tagstr, *chunksize);
#else
    printf("(0x%s) %*c  ID:<%s>   Size: 0x%08x %8u\n",
	   off_t_to_char(filepos,16,8),(RekDepth+1)*4,' ',tagstr, *chunksize, *chunksize);
#endif


    if (datapos + ((*chunksize+1)&~1) > filesize) {      /* too long? */
	printf("  *****  Error: Chunk exceeds file\n");
    }

    switch (chunkid) {

  /* Depending on the ID of the chunk and the internal state, the
     different IDs can be interpreted. At the moment the only
     interpreted chunks are RIFF- and LIST-chunks. For all other
     chunks only their header is printed out. */

    case RIFFtag:
    case LISTtag: {

	DWORD  datashowed=0;
	FOURCC formtype;         /* format of chunk                     */
	char   formstr[5];       /* format of chunk converted to string */
	DWORD  subchunksize=0;   /* size of a read subchunk             */

	if (!read(fd, &formtype, sizeof(FOURCC))) printf("ERROR\n");     /* read the form type */
	FOURCC2Str(formtype,formstr);            /* make it printable  */


	/* print out the indented form of the chunk: */
	if (chunkid==RIFFtag)
	    printf("%12c %*c  Form Type = <%s>\n",
		   ' ',(RekDepth+1)*4,' ',formstr);
	else
	    printf("%12c %*c  List Type = <%s>\n",
		   ' ',(RekDepth+1)*4,' ',formstr);

	datashowed=sizeof(FOURCC);    /* we showed the form type      */
	datapos+=(off_t) datashowed;  /* for the rest of the routine  */

	while (datashowed<*chunksize) {      /* while not showed all: */
	
	  uint32_t subchunklen;           /* complete size of a subchunk  */
	
	  datapos_tmp[RekDepth]=datapos;

	    /* recurse for subchunks of RIFF and LIST chunks: */
#ifdef AVI_DEBUG
	    printf("enter [%d] size=%lu pos=%Lu left=%lu\n", RekDepth, subchunksize, datapos, *chunksize-datashowed);
#endif
	    if (!ProcessChunk(fd, datapos, filesize,  0,
			      RekDepth+1, &subchunksize)) return(FALSE);
	
	    subchunklen = sizeof(FOURCC) +  /* this is the complete..   */
		sizeof(DWORD)  +  /* .. size of the subchunk  */
		((subchunksize+1) & ~1);
	
	    datashowed += subchunklen;      /* we showed the subchunk   */
	    datapos    = datapos_tmp[RekDepth] + (off_t) subchunklen;      /* for the rest of the loop */
	
#ifdef AVI_DEBUG
	    printf(" exit [%d] size=%lu/%lu pos=%Lu left=%lu\n", RekDepth, subchunksize, subchunklen, datapos, *chunksize-datashowed);
#endif
	}
    } break;

    /* Feel free to put your extensions here! */

    case avihtag:
	dump_vals(fd,sizeof(names_avih)/sizeof(struct VAL),names_avih);
	break;
    case strhtag:
    {
	char   typestr[5];
	xio_read(fd, &fcc_type,sizeof(FOURCC));
	FOURCC2Str(fcc_type,typestr);
	printf("\tfcc_type     = %s\n",typestr);
	dump_vals(fd,sizeof(names_strh)/sizeof(struct VAL),names_strh);
	break;
    }
    case strftag:

	switch (fcc_type) {
	case vidstag:
	    dump_vals(fd,sizeof(names_strf_vids)/sizeof(struct VAL),names_strf_vids);
	    break;
	case audstag:
	    dump_vals(fd,sizeof(names_strf_auds)/sizeof(char*),names_strf_auds);
	    break;
	default:
	    printf("unknown\n");
	    break;
	}
	break;

    case Tagwave:
	dump_vals(fd,sizeof(names_strf_auds)/sizeof(char*),names_strf_auds);
	break;

    case indxtag: {
	uint32_t chunks=*chunksize-sizeof(names_indx)/sizeof(char*);
	loff_t offset;
	uint32_t size, duration;
	uint32_t u=0;
	off_t indxend = datapos + chunks;
	dump_vals(fd,sizeof(names_indx)/sizeof(char*),names_indx);

	while (datapos < indxend) {
	    xio_read(fd, &offset,8);
	    xio_read(fd, &size, 4);
	    xio_read(fd, &duration,4);
	    offset = SWAP8(offset);
	    size = SWAP4(size);
	    duration = SWAP4(duration);
	    if (size!=0)
	    printf("\t\t [%6d] 0x%016llx 0x%08lx %8d\n", u++,
		    (unsigned long long)offset, (long)size, (int)duration);
	    datapos += 16;
	}
	break;
	}
    case Tagix00:
    case Tagix01:
    case Tagix02:
    case Tagix03: {
	uint32_t chunks=*chunksize-sizeof(names_stdidx)/sizeof(char*);
	uint32_t offset, size, key;
	uint32_t u=0;
	off_t indxend = datapos + chunks;
	dump_vals(fd,sizeof(names_stdidx)/sizeof(char*),names_stdidx);

	while (datapos < indxend) {
	    xio_read(fd, &offset,4);
	    xio_read(fd, &size, 4);
	    offset = SWAP4(offset);
	    size = SWAP4(size);
	    key  = size;
	    key  = key&0x80000000?0:1;
	    size &= 0x7fffffff;
	    if (size!=0)
	    printf("\t\t [%6d] 0x%08x 0x%08x key=%s\n", u++, offset, size,
		    key?"yes":"no");
	    datapos += 8;
	}
		  }
	break;

    case idx1tag: {
	off_t idxend = datapos+*chunksize;

	while (datapos<idxend) {

	    DWORD val32;
	    static uint32_t u=0;

	    //tag:
	    xio_read(fd, &val32,4);
	    val32 = SWAP4(val32);
	    if(val32==Tag00db) {
		printf("\t\t [%6d] %s=%c%c%c%c ", u++, "tag",
		       (int)( val32        & 0xff),
		       (int)((val32 >>  8) & 0xff),
		       (int)((val32 >> 16) & 0xff),
		       (int)((val32 >> 24) & 0xff));
	    } else printf("\t\t          %s=%c%c%c%c ", "tag",
		       (int)( val32        & 0xff),
		       (int)((val32 >>  8) & 0xff),
		       (int)((val32 >> 16) & 0xff),
		       (int)((val32 >> 24) & 0xff));
	
	
	    //flag
	    xio_read(fd, &val32, 4);
	    val32 = SWAP4(val32);
	    printf("flags=%02ld ",(long)val32);

	    //pos
	    xio_read(fd, &val32, 4);
	    val32 = SWAP4(val32);
	    printf("0x%08lx",(long)val32);

	    //size
	    xio_read(fd, &val32, 4);
	    val32 = SWAP4(val32);
	    printf("%8ld\n",(long)val32);

	    datapos+=16;
	}
	}
	break;

    case dmlhtag:
	dump_vals(fd,sizeof(names_dmlh)/sizeof(struct VAL),names_dmlh);
	break;
    }

    return(TRUE);
}

static DWORD size;
static int eos=0;

static boolean DumpChunk(int fd, off_t filepos, off_t filesize,
			 FOURCC DesiredTag, int RekDepth,
			 DWORD* chunksize, int mode)
{
    char   tagstr[5];          /* FOURCC of chunk converted to string */
    FOURCC chunkid;            /* read FOURCC of chunk                */
    off_t  datapos;            /* position of data in file to process */

    if (filepos>filesize-1)  /* Oops. Must be something wrong!      */
	return(FALSE);

    xio_lseek(fd, filepos, SEEK_SET);

    if (!ReadChunkHead(fd, &chunkid,chunksize)) {  /* read chunk header */
      return(FALSE);
    }

    FOURCC2Str(chunkid,tagstr);       /* now we can PRINT the chunkid */

    if (DesiredTag) {                 /* do we have to test identity? */
      if (DesiredTag!=chunkid) {
	char ds[5];
	FOURCC2Str(DesiredTag,ds);
	return(FALSE);
      }
    }

    datapos=filepos+sizeof(FOURCC)+sizeof(DWORD); /* here is the data */

    //support for broken files
    if (datapos + ((*chunksize+1)&~1) > filesize) {
      size = filesize-datapos;
      eos=1;
    } else {
      size = *chunksize;
    }

    switch (chunkid) {

  /* Depending on the ID of the chunk and the internal state, the
     different IDs can be interpreted. At the moment the only
     interpreted chunks are RIFF- and LIST-chunks. For all other
     chunks only their header is printed out. */

    case RIFFtag:
    case LISTtag: {

      DWORD datashowed;
      FOURCC formtype;       /* format of chunk                     */
      char   formstr[5];     /* format of chunk converted to string */
      DWORD  subchunksize;   /* size of a read subchunk             */

      xio_read(fd, &formtype,sizeof(FOURCC));    /* read the form type */
      FOURCC2Str(formtype,formstr);           /* make it printable  */

      datashowed=sizeof(FOURCC);    /* we showed the form type      */
      datapos+=(off_t)datashowed;   /* for the rest of the routine  */

      while (datashowed<*chunksize) {      /* while not showed all: */

	uint32_t subchunklen;           /* complete size of a subchunk  */

	/* recurse for subchunks of RIFF and LIST chunks: */
	if (!DumpChunk(fd, datapos,filesize,0,
		       RekDepth+1,&subchunksize,mode)) return(FALSE);

	subchunklen = sizeof(FOURCC) +  /* this is the complete..   */
	  sizeof(DWORD)  +  /* .. size of the subchunk  */
	  ((subchunksize+1) & ~1);

	datashowed += subchunklen;      /* we showed the subchunk   */
	datapos    += (off_t) subchunklen;      /* for the rest of the loop */
      }
    }

    break;

    case Tag01wb:
    case Tag00wb:

      if(mode==1) {
	xio_lseek(fd, datapos, SEEK_SET);
	if(avi_read_write(fd, STDOUT_FILENO, size)!=size) return(FALSE);
      }
      if(eos) return(FALSE);

      break;

    case Tag00db:
    case Tag00dc:
    case Tag01db:
    case Tag01dc:
    case Tag00__:

      if(mode==0) {
	xio_lseek(fd, datapos, SEEK_SET);
	if(avi_read_write(fd, STDOUT_FILENO, size)!=size) return(FALSE);
      }

      if(eos) return(FALSE);

      break;

    case Tagdata:
    case TagDATA:

      if(mode==2) {
	xio_lseek(fd, datapos, SEEK_SET);
	if(avi_read_write(fd, STDOUT_FILENO, size)!=size) return(FALSE);
      }

      if(eos) exit(1);

      break;
    }

    return(TRUE);
}


int AVI_scan(const char *file_name)
{
    off_t  filesize;     /* its size                    */
    off_t  filepos;

    int fd;

    DWORD  chunksize;    /* size of the RIFF chunk data */

    if (!(fd=xio_open(file_name, O_RDONLY))) {
	printf("\n\n *** Error opening file %s. Program aborted!\n",
	       file_name);
	return(1);
    }

    filesize = xio_lseek(fd, 0, SEEK_END);
    xio_lseek(fd, 0, SEEK_SET);

    printf("Contents of file %s (%s/", file_name,
	   off_t_to_char(filesize,10,1));
    printf("0x%s bytes):\n\n",off_t_to_char(filesize,16,1));

    for (filepos = 0; filepos < filesize;) {
	chunksize = 0;
	if (!ProcessChunk(fd, filepos,filesize,RIFFtag,0,&chunksize))
	    break;
	filepos += chunksize + 8;
	printf("\n");
    }

    xio_close(fd);

    return(0);
}


int AVI_dump(const char *file_name, int mode)
{
    off_t  filesize;     /* its size                    */
    off_t  filepos;
    int fd;

    DWORD  chunksize;    /* size of the RIFF chunk data */

    if (!(fd=xio_open(file_name,O_RDONLY))) return(1);

    filesize = xio_lseek(fd, 0, SEEK_END);
    xio_lseek(fd, 0, SEEK_SET);

    for (filepos = 0; filepos < filesize;) {
	chunksize = 0;
	if (!DumpChunk(fd,filepos,filesize,RIFFtag,0,&chunksize,mode))
	    break;
	filepos += chunksize + 8;
    }

    xio_close(fd);

    return(0);
}