aboutsummaryrefslogtreecommitdiffstats
path: root/console/showriff.c
blob: aa9d093915f8ac48631f11bfbd76bb417126ca4a (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
/*  SHOWRIFF.c
 *  Extracts some infos from RIFF files
 *  (c)94 UP-Vision Computergrafik for c't
 *  Written in ANSI-C. No special header files needed to compile.
 *
 *  modified by Gerd Knorr:
 *   - dos2unix :-)
 *   - fixed warnings
 *   - added some tags (checked xanim sources for info)
 *   - LFS + OpenDML + mjpeg
 *   - bytesex fixes
 */

#include "config.h"

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <sys/types.h>

#if BYTE_ORDER == BIG_ENDIAN
# define SWAP2(x) (((x>>8) & 0x00ff) |\
                   ((x<<8) & 0xff00))

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

#ifndef HAVE_FTELLO
# define ftello ftell
#endif
#ifndef HAVE_FSEEKO
# define fseeko fseek
#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 avi_tag MAKEFOURCC('A','V','I',' ')
#define wavetag MAKEFOURCC('W','A','V','E')
#define fmt_tag MAKEFOURCC('f','m','t',' ') 

#define MJPGtag MAKEFOURCC('M','J','P','G')
#define _00dbtag MAKEFOURCC('0','0','d','b')
#define _00dctag MAKEFOURCC('0','0','d','c')

/* 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;
static DWORD riff_type;
static int stop_on_errors = 1;
static int print_mjpeg = 0;

#define EoLST  0
#define INT32  1
#define INT16  2
#define FLAGS  3
#define CCODE  4

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[] = {
    { INT16,  "format" },
    { INT16,  "channels" },
    { INT32,  "rate" },
    { INT32,  "av_bps" },
    { INT16,  "blockalign" },
    { INT16,  "size" },
    { EoLST,  NULL }    
};

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

static void dump_vals(FILE *f, int count, struct VAL *names)
{
    DWORD i,j,val32;
    WORD  val16;
    
    for (i = 0; names[i].type != EoLST; i++) {
	switch (names[i].type) {
	case INT32:
	    fread(&val32,4,1,f);
	    val32 = SWAP4(val32);
	    printf("\t%-12s = %d\n",names[i].name,val32);
	    break;
	case CCODE:
	    fread(&val32,4,1,f);
	    val32 = SWAP4(val32);
	    if (val32) {
		printf("\t%-12s = %c%c%c%c (0x%x)\n",names[i].name,
		       (int)( val32        & 0xff),
		       (int)((val32 >>  8) & 0xff),
		       (int)((val32 >> 16) & 0xff),
		       (int)((val32 >> 24) & 0xff),
		       val32);
	    } else {
		printf("\t%-12s = unset (0)\n",names[i].name);
	    }
	    break;
	case FLAGS:
	    fread(&val32,4,1,f);
	    val32 = SWAP4(val32);
	    printf("\t%-12s = 0x%x\n",names[i].name,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:
	    fread(&val16,2,1,f);
	    val16 = SWAP2(val16);
	    printf("\t%-12s = %ld\n",names[i].name,(long)val16);
	    break;
	}
    }
}

struct JPEG_MARKER {
    unsigned char  val;
    char           *name;
    char           *desc;
} mark[] = {
    { 0xc0, "SOF0 ", "start of frame 0"   },
    { 0xc4, "DHT  ", "huffmann table(?)"  },
    { 0xd8, "SOI  ", "start of image"     },
    { 0xda, "SOS  ", "start of scan"      },
    { 0xdb, "DQT  ", "quantization table" },
    { 0xe0, "APP0 ", NULL                 },
    { 0xfe, "COM  ", "comment"            },
    { 0,    NULL,    NULL }
};

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");
    }
}

static void dump_jpeg(unsigned char *buf, int len)
{
    int i,j,type,skip;

    if (!print_mjpeg)
	return;

    for (i = 0; i < len;) {
	if (buf[i] != 0xff) {
	    printf("\tjpeg @ 0x%04x: oops 0x%02x 0x%02x 0x%02x 0x%02x\n",
		   i,buf[i],buf[i+1],buf[i+2],buf[i+3]);
	    return;
	}
	type = buf[i+1];
	for (j = 0; 0 != mark[j].val; j++)
	    if (mark[j].val == type)
		break;
	printf("\tjpeg @ 0x%04x: %s (0x%x)  %s\n",i,
	       mark[j].name ? mark[j].name : "???", type,
	       mark[j].desc ? mark[j].desc : "???");
	i+=2;
	skip = buf[i] << 8 | buf[i+1];
	switch (type) {
	case 0xd8:
	    skip=0;
	    break;
	case 0xc0: /* SOF0 */
	    printf("\t\tsize: %dx%d prec=%d comp=%d\n",
		   buf[i+6] | (buf[i+5] << 8),
		   buf[i+4] | (buf[i+3] << 8),
		   buf[i+2], buf[i+7]);
	    for (j = 0; j < buf[i+7]; j++) {
		printf("\t\tcomp%d: id=%d h=%d v=%d qt=%d\n",j,
		       buf[i+3*j+8],
		       (buf[i+3*j+9] >> 4) & 0x0f,
		       buf[i+3*j+9]        & 0x0f,
		       buf[i+3*j+10]);
	    }
	    break;
	case 0xc4: /* DHT */
	    printf("\t\tindex=%d\n",buf[i+2]);
	    break;
	case 0xdb: /* DQT */
	    printf("\t\tprec=%d, nr=%d\n",
		   (buf[i+2] >> 4) & 0x0f,
		   buf[i+2]        & 0x0f);
	    break;
	case 0xda:
	    return;
	}
	hexlog(buf+i+2,skip-2);
	i += skip;
    }
}

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 p;
}    

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

static boolean ReadChunkHead(FILE* f, FOURCC* ID, DWORD* size)
{
    if (!fread(ID,sizeof(FOURCC),1,f)) return(FALSE);
    if (!fread(size,sizeof(DWORD),1,f)) return(FALSE);
    *size = SWAP4(*size);
    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 boolean ProcessChunk(FILE* f, size_t filepos, size_t filesize,
			    FOURCC DesiredTag, int RekDepth,
			    DWORD* chunksize)
{
    char   buf[BUFSIZE];
    int    buflen;
    char   tagstr[5];          /* FOURCC of chunk converted to string */
    FOURCC chunkid;            /* read FOURCC of chunk                */
    size_t datapos;            /* position of data in file to process */
    
    if (filepos>filesize-1) {  /* Oops. Must be something wrong!      */
	printf("  *****  Error: Data would be behind end of file!\n");
	if (stop_on_errors)
	    return(FALSE);
    }
    fseeko(f,filepos,SEEK_SET);    /* Go to desired file position!     */
    
    if (!ReadChunkHead(f,&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: */
    printf("(0x%s) %*c  ID:<%s>   Size: 0x%08x\n",
	   off_t_to_char(filepos,16,8),(RekDepth+1)*4,' ',tagstr,*chunksize);

    if (datapos + ((*chunksize+1)&~1) > filesize) {      /* too long? */
	printf("  *****  Error: Chunk exceeds file\n");
	if (stop_on_errors)
	    return(FALSE);
    }
    
    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             */
	
	fread(&formtype,sizeof(FOURCC),1,f);    /* 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);
	    riff_type = formtype;
	} else
	    printf("%12c %*c  List Type = <%s>\n",
		   ' ',(RekDepth+1)*4,' ',formstr);
	
	datashowed=sizeof(FOURCC);    /* we showed the form type      */
	datapos+=datashowed;          /* for the rest of the routine  */
	
	while (datashowed<*chunksize) {      /* while not showed all: */
	    
	    long subchunklen;           /* complete size of a subchunk  */
	    
	    /* recurse for subchunks of RIFF and LIST chunks: */
	    if (!ProcessChunk(f,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    += subchunklen;      /* for the rest of the loop */
	}
    } break;
    
    /* Feel free to put your extensions here! */
    
    case avihtag:
	dump_vals(f,sizeof(names_avih)/sizeof(struct VAL),names_avih);
	break;
    case strhtag:
    {
	char   typestr[5];
	fread(&fcc_type,sizeof(FOURCC),1,f);
	FOURCC2Str(fcc_type,typestr);
	printf("\tfcc_type     = %s\n",typestr);
	dump_vals(f,sizeof(names_strh)/sizeof(struct VAL),names_strh);
	break;
    }
    case strftag:
	switch (fcc_type) {
	case vidstag:
	    dump_vals(f,sizeof(names_strf_vids)/sizeof(struct VAL),names_strf_vids);
	    break;
	case audstag:
	    dump_vals(f,sizeof(names_strf_auds)/sizeof(char*),names_strf_auds);
	    break;
	default:
	    printf("unknown\n");
	    break;
	}
	break;
    case fmt_tag:
	if (riff_type == wavetag)
	    dump_vals(f,sizeof(names_strf_auds)/sizeof(char*),names_strf_auds);
	break;
    case dmlhtag:
	dump_vals(f,sizeof(names_dmlh)/sizeof(struct VAL),names_dmlh);
	break;
    case _00dbtag:
    case _00dctag:
	buflen = (*chunksize > BUFSIZE) ? BUFSIZE : *chunksize;
	fread(buf, buflen, 1, f);
	dump_jpeg(buf,buflen);
	break;
    }
    
    return(TRUE);
}

static void
usage(char *prog)
{
    char *h;

    if (NULL != (h = strrchr(prog,'/')))
	prog = h+1;
    fprintf(stderr,
	    "\n"
	    "%s shows contents of RIFF files (AVI,WAVE...).\n"
	    "(c) 1994 UP-Vision Computergrafik for c't\n"
	    "unix port and some extentions (for avi) by Gerd Knorr\n"
	    "\n"
	    "usage:  %s [ -j ] [ -e ] filename\n"
	    "options:\n"
	    "  -j  try to decode some mjpeg headers\n"
	    "  -e  don't stop on errors\n"
	    "\n",
	    prog,prog);
}

int main (int argc, char **argv)
{
    FILE*  f;            /* the input file              */
    off_t  filesize;     /* its size                    */
    off_t  filepos;
    DWORD  chunksize;    /* size of the RIFF chunk data */
    int c;

    /* parse options */
    for (;;) {
	if (-1 == (c = getopt(argc, argv, "jeh")))
	    break;
	switch (c) {
	case 'j':
	    print_mjpeg = 1;
	    break;
	case 'e':
	    stop_on_errors = 0;
	    break;
	case 'h':
	default:
	    usage(argv[0]);
	    exit(1);
	}
    }
    
    if (optind == argc) {
	usage(argv[0]);
	exit(1);
    }
    
    if (!(f=fopen(argv[optind],"rb"))) {
	printf("\n\n *** Error opening file %s. Program aborted!\n",
	       argv[optind]);
	return(1);
    }
    
    fseeko(f, 0, SEEK_END);
    filesize = ftello(f);
    fseeko(f, 0, SEEK_SET);

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

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

Privacy Policy