Merge svn changes up to r28310
[mplayer.git] / TOOLS / avi-fix.c
blob19aa55384a4607b83e6923215a4a728550061aec
1 // avi-fix v0.1 (C) A'rpi, license GPL
2 // simple tool to fix chunk sizes in a RIFF AVI file
3 // it doesn't check/fix index, use mencoder -forceidx -oac copy -ovc copy to fix index!
5 #include "config.h"
6 #ifdef MP_DEBUG
7 #define mp_debug(...) printf(__VA_ARGS__)
8 #else
9 #define mp_debug(...)
10 #endif
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
16 #define FCC(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))
18 static inline char xx(unsigned char c){
19 if(c>=32 && c<128) return c;
20 return '?';
23 static inline unsigned int getid(FILE* f){
24 unsigned int id;
25 id=fgetc(f);
26 id=(id<<8)|fgetc(f);
27 id=(id<<8)|fgetc(f);
28 id=(id<<8)|fgetc(f);
29 return id;
32 int main(int argc,char* argv[]){
33 //FILE* f=fopen("edgar.avi","rb"); // readonly (report errors)
34 //FILE* f=fopen("edgar.avi","rb+"); // fix mode (fix chunk sizes)
35 unsigned int lastgood=0;
36 unsigned int fixat=0;
37 unsigned int offset=0;
38 int fix_flag=0;
39 FILE* f;
41 if(argc<=1){
42 printf("Usage: %s [-fix] badfile.avi\n",argv[0]);
43 exit(1);
46 if(!strcmp(argv[1],"-fix")){
47 fix_flag=1;
48 f=fopen(argv[argc-1],"rb+");
49 } else
50 f=fopen(argv[argc-1],"rb");
52 if(!f){
53 perror("error");
54 printf("couldnt open '%s'\n",argv[argc-1]);
55 exit(2);
58 while(1){
59 unsigned int id,len;
60 again:
61 id=fgetc(f);
62 id=(id<<8)|fgetc(f);
63 id=(id<<8)|fgetc(f);
64 faszom:
65 if(feof(f)) break;
66 // if(!lastgood && feof(f)) break;
67 id=(id<<8)|fgetc(f);
68 // lastgood=ftell(f);
69 mp_debug("%08X: %c%c%c%c\n",(int)ftell(f)-4,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
70 switch(id){
71 case FCC('R','I','F','F'):
72 fread(&len,4,1,f); // filesize
73 id=getid(f); // AVI
74 mp_debug("RIFF header, filesize=0x%X format=%c%c%c%c\n",len,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
75 break;
76 case FCC('L','I','S','T'):
77 fread(&len,4,1,f); // size
78 id=getid(f); // AVI
79 mp_debug("LIST size=0x%X format=%c%c%c%c\n",len,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
80 //case FCC('h','d','r','l'):
81 //case FCC('s','t','r','l'):
82 //case FCC('o','d','m','l'):
83 //case FCC('m','o','v','i'):
84 break;
85 // legal chunk IDs:
86 case FCC('a','v','i','h'): // avi header
87 case FCC('s','t','r','h'): // stream header
88 case FCC('s','t','r','f'): // stream format
89 case FCC('J','U','N','K'): // official shit
90 // index:
91 case FCC('i','d','x','1'): // main index??
92 case FCC('d','m','l','h'): // opendml header
93 case FCC('i','n','d','x'): // opendml main index??
94 case FCC('i','x','0','0'): // opendml sub index??
95 case FCC('i','x','0','1'): // opendml sub index??
96 // data:
97 case FCC('0','1','w','b'): // audio track #1
98 case FCC('0','2','w','b'): // audio track #2
99 case FCC('0','3','w','b'): // audio track #3
100 case FCC('0','0','d','b'): // uncompressed video
101 case FCC('0','0','d','c'): // compressed video
102 case FCC('0','0','_','_'): // A-V interleaved (type2 DV file)
103 // info:
104 case FCC('I','S','F','T'): // INFO: software
105 case FCC('I','S','R','C'): // INFO: source
106 case FCC('I','N','A','M'): // INFO: name
107 case FCC('I','S','B','J'): // INFO: subject
108 case FCC('I','A','R','T'): // INFO: artist
109 case FCC('I','C','O','P'): // INFO: copyright
110 case FCC('I','C','M','T'): // INFO: comment
111 lastgood=ftell(f);
112 if(fixat && fix_flag){
113 // fix last chunk's size field:
114 fseek(f,fixat,SEEK_SET);
115 len=lastgood-fixat-8;
116 mp_debug("Correct len to 0x%X\n",len);
117 fwrite(&len,4,1,f);
118 fseek(f,lastgood,SEEK_SET);
119 fixat=0;
121 fread(&len,4,1,f); // size
122 mp_debug("ID ok, chunk len=0x%X\n",len);
123 len+=len&1; // align at 2
124 fseek(f,len,SEEK_CUR); // skip data
125 break;
126 default:
127 if(!lastgood){
128 ++offset;
129 mp_debug("invalid ID, trying %d byte offset\n",offset);
130 goto faszom; // try again @ next post
132 mp_debug("invalid ID, parsing next chunk's data at 0x%X\n",lastgood);
133 fseek(f,lastgood,SEEK_SET);
134 fixat=lastgood;
135 lastgood=0;
136 goto again;
138 offset=0;
141 return 0;