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!
7 #define mp_debug(...) printf(__VA_ARGS__)
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
;
23 static inline unsigned int getid(FILE* f
){
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;
37 unsigned int offset
=0;
42 printf("Usage: %s [-fix] badfile.avi\n",argv
[0]);
46 if(!strcmp(argv
[1],"-fix")){
48 f
=fopen(argv
[argc
-1],"rb+");
50 f
=fopen(argv
[argc
-1],"rb");
54 printf("couldnt open '%s'\n",argv
[argc
-1]);
66 // if(!lastgood && feof(f)) break;
69 mp_debug("%08X: %c%c%c%c\n",(int)ftell(f
)-4,xx(id
>>24),xx(id
>>16),xx(id
>>8),xx(id
));
71 case FCC('R','I','F','F'):
72 fread(&len
,4,1,f
); // filesize
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
));
76 case FCC('L','I','S','T'):
77 fread(&len
,4,1,f
); // size
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'):
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
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??
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)
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
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
);
118 fseek(f
,lastgood
,SEEK_SET
);
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
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
);