audio/video: delete buggy "dynamic plugin" code
[mplayer.git] / TOOLS / avi-fix.c
blobe4d6600ad6f79d535857a8868ca5949a645f41a3
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
4 * copy -vcodec copy
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.
21 #include "config.h"
22 #ifdef MP_DEBUG
23 #define mp_debug(...) printf(__VA_ARGS__)
24 #else
25 #define mp_debug(...)
26 #endif
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
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;
36 return '?';
39 static inline unsigned int getid(FILE* f){
40 unsigned int id;
41 id=fgetc(f);
42 id=(id<<8)|fgetc(f);
43 id=(id<<8)|fgetc(f);
44 id=(id<<8)|fgetc(f);
45 return id;
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;
52 unsigned int fixat=0;
53 unsigned int offset=0;
54 int fix_flag=0;
55 FILE* f;
57 if(argc<=1){
58 printf("Usage: %s [-fix] badfile.avi\n",argv[0]);
59 exit(1);
62 if(!strcmp(argv[1],"-fix")){
63 fix_flag=1;
64 f=fopen(argv[argc-1],"rb+");
65 } else
66 f=fopen(argv[argc-1],"rb");
68 if(!f){
69 perror("error");
70 printf("couldnt open '%s'\n",argv[argc-1]);
71 exit(2);
74 while(1){
75 unsigned int id,len;
76 again:
77 id=fgetc(f);
78 id=(id<<8)|fgetc(f);
79 id=(id<<8)|fgetc(f);
80 faszom:
81 if(feof(f)) break;
82 // if(!lastgood && feof(f)) break;
83 id=(id<<8)|fgetc(f);
84 // lastgood=ftell(f);
85 mp_debug("%08X: %c%c%c%c\n",(int)ftell(f)-4,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
86 switch(id){
87 case FCC('R','I','F','F'):
88 fread(&len,4,1,f); // filesize
89 id=getid(f); // AVI
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));
91 break;
92 case FCC('L','I','S','T'):
93 fread(&len,4,1,f); // size
94 id=getid(f); // AVI
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'):
100 break;
101 // legal chunk IDs:
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
106 // index:
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??
112 // data:
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)
119 // info:
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
127 lastgood=ftell(f);
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);
133 fwrite(&len,4,1,f);
134 fseek(f,lastgood,SEEK_SET);
135 fixat=0;
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
141 break;
142 default:
143 if(!lastgood){
144 ++offset;
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);
150 fixat=lastgood;
151 lastgood=0;
152 goto again;
154 offset=0;
157 return 0;