1 /* avi-fix v0.1 (C) A'rpi
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 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #define mp_debug(...) printf(__VA_ARGS__)
31 #define FCC(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))
33 static inline char xx(unsigned char c
){
34 if(c
>=32 && c
<128) return c
;
38 static inline unsigned int getid(FILE* f
){
47 int main(int argc
,char* argv
[]){
48 //FILE* f=fopen("edgar.avi","rb"); // readonly (report errors)
49 //FILE* f=fopen("edgar.avi","rb+"); // fix mode (fix chunk sizes)
50 unsigned int lastgood
=0;
52 unsigned int offset
=0;
57 printf("Usage: %s [-fix] badfile.avi\n",argv
[0]);
61 if(!strcmp(argv
[1],"-fix")){
63 f
=fopen(argv
[argc
-1],"rb+");
65 f
=fopen(argv
[argc
-1],"rb");
69 printf("couldnt open '%s'\n",argv
[argc
-1]);
81 // if(!lastgood && feof(f)) break;
84 mp_debug("%08X: %c%c%c%c\n",(int)ftell(f
)-4,xx(id
>>24),xx(id
>>16),xx(id
>>8),xx(id
));
86 case FCC('R','I','F','F'):
87 fread(&len
,4,1,f
); // filesize
89 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
));
91 case FCC('L','I','S','T'):
92 fread(&len
,4,1,f
); // size
94 mp_debug("LIST size=0x%X format=%c%c%c%c\n",len
,xx(id
>>24),xx(id
>>16),xx(id
>>8),xx(id
));
95 //case FCC('h','d','r','l'):
96 //case FCC('s','t','r','l'):
97 //case FCC('o','d','m','l'):
98 //case FCC('m','o','v','i'):
101 case FCC('a','v','i','h'): // avi header
102 case FCC('s','t','r','h'): // stream header
103 case FCC('s','t','r','f'): // stream format
104 case FCC('J','U','N','K'): // official shit
106 case FCC('i','d','x','1'): // main index??
107 case FCC('d','m','l','h'): // opendml header
108 case FCC('i','n','d','x'): // opendml main index??
109 case FCC('i','x','0','0'): // opendml sub index??
110 case FCC('i','x','0','1'): // opendml sub index??
112 case FCC('0','1','w','b'): // audio track #1
113 case FCC('0','2','w','b'): // audio track #2
114 case FCC('0','3','w','b'): // audio track #3
115 case FCC('0','0','d','b'): // uncompressed video
116 case FCC('0','0','d','c'): // compressed video
117 case FCC('0','0','_','_'): // A-V interleaved (type2 DV file)
119 case FCC('I','S','F','T'): // INFO: software
120 case FCC('I','S','R','C'): // INFO: source
121 case FCC('I','N','A','M'): // INFO: name
122 case FCC('I','S','B','J'): // INFO: subject
123 case FCC('I','A','R','T'): // INFO: artist
124 case FCC('I','C','O','P'): // INFO: copyright
125 case FCC('I','C','M','T'): // INFO: comment
127 if(fixat
&& fix_flag
){
128 // fix last chunk's size field:
129 fseek(f
,fixat
,SEEK_SET
);
130 len
=lastgood
-fixat
-8;
131 mp_debug("Correct len to 0x%X\n",len
);
133 fseek(f
,lastgood
,SEEK_SET
);
136 fread(&len
,4,1,f
); // size
137 mp_debug("ID ok, chunk len=0x%X\n",len
);
138 len
+=len
&1; // align at 2
139 fseek(f
,len
,SEEK_CUR
); // skip data
144 mp_debug("invalid ID, trying %d byte offset\n",offset
);
145 goto faszom
; // try again @ next post
147 mp_debug("invalid ID, parsing next chunk's data at 0x%X\n",lastgood
);
148 fseek(f
,lastgood
,SEEK_SET
);