mkvtoolnix/avilib/avidump.c
2003-07-09 18:53:25 +00:00

802 lines
20 KiB
C

/*
* avidump.c
*
* Copyright (C) Thomas Östreich - 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 linux 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 "os.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef COMP_MSC
#include <io.h>
#define STDOUT_FILENO fileno(stdout)
#else
#include <unistd.h>
#endif
#include <ctype.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#if defined(SYS_BSD) || defined(COMP_MSC)
typedef off_t off64_t;
#define lseek64 lseek
#endif
//#define AVI_DEBUG
#ifdef HAVE_ENDIAN_H
# include <endian.h>
#endif
#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
typedef unsigned long DWORD;
typedef unsigned short WORD;
typedef DWORD FOURCC; /* Type of FOUR Character Codes */
typedef unsigned char 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 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 Tag00__ MAKEFOURCC('0','0','_','_')
/* 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
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" },
{ EoLST, NULL }
};
struct VAL names_dmlh[] = {
{ INT32, "frames" },
{ EoLST, NULL }
};
struct VAL names_idx1[] = {
{ _TAG, "tag" },
{ INT32, "flags" },
{ INT32, "pos" },
{ INT32, "length" },
{ 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 = 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 = 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)
{
DWORD i,j,val32;
WORD val16;
for (i = 0; names[i].type != EoLST; i++) {
switch (names[i].type) {
case INT32:
read(fd, &val32, 4);
val32 = SWAP4(val32);
printf("\t%-12s = %ld\n",names[i].name,val32);
break;
case CCODE:
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),
val32);
} else {
printf("\t%-12s = unset (0)\n",names[i].name);
}
break;
case _TAG:
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:
read(fd, &val32,4);
val32 = SWAP4(val32);
printf("\t%-12s = 0x%lx\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:
read(fd, &val16,2);
val16 = SWAP2(val16);
printf("\t%-12s = %ld\n",names[i].name,(long)val16);
break;
case HEX16:
read(fd, &val16,2);
val16 = SWAP2(val16);
printf("\t%-12s = 0x%lx\n",names[i].name,(long)val16);
break;
}
}
}
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 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 (!read(fd, ID, sizeof(FOURCC))) return(FALSE);
if (!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=lseek64(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%08lx %8lu\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: */
long 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, (off_t) 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];
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 idx1tag:
while (datapos<filesize) {
DWORD val32;
static long u=0;
//tag:
read(fd, &val32,4);
val32 = SWAP4(val32);
if(val32==Tag00db) {
printf("\t\t [%6ld] %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
read(fd, &val32, 4);
val32 = SWAP4(val32);
printf("flags=%02ld ",val32);
//pos
read(fd, &val32, 4);
val32 = SWAP4(val32);
printf("0x%08lx",val32);
//size
read(fd, &val32, 4);
val32 = SWAP4(val32);
printf("%8ld\n",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);
lseek64(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 */
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: */
long 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:
if(mode==1) {
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 Tag00__:
if(mode==0) {
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) {
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(char *file_name)
{
off_t filesize; /* its size */
off_t filepos;
int fd;
DWORD chunksize; /* size of the RIFF chunk data */
#if defined(SYS_WINDOWS)
if (!(fd=open(file_name, O_RDONLY|O_BINARY))) {
#else
if (!(fd=open(file_name, O_RDONLY))) {
#endif
printf("\n\n *** Error opening file %s. Program aborted!\n",
file_name);
return(1);
}
filesize = lseek64(fd, 0, SEEK_END);
lseek64(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");
}
close(fd);
return(0);
}
int AVI_dump(char *file_name, int mode)
{
off_t filesize; /* its size */
off_t filepos;
int fd;
DWORD chunksize; /* size of the RIFF chunk data */
#if defined(SYS_WINDOWS)
if (!(fd=open(file_name,O_RDONLY|O_BINARY))) return(1);
#else
if (!(fd=open(file_name,O_RDONLY))) return(1);
#endif
filesize = lseek64(fd, 0, SEEK_END);
lseek64(fd, 0, SEEK_SET);
for (filepos = 0; filepos < filesize;) {
chunksize = 0;
if (!DumpChunk(fd,filepos,filesize,RIFFtag,0,&chunksize,mode))
break;
filepos += chunksize + 8;
}
close(fd);
return(0);
}