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