Change some filename-handling code to use mp_basename()
[mplayer/greg.git] / TOOLS / avi-fix.c
blob2dfd48a955e3296ce81d03c55f04e0203ca0d568
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.
20 #include "config.h"
21 #ifdef MP_DEBUG
22 #define mp_debug(...) printf(__VA_ARGS__)
23 #else
24 #define mp_debug(...)
25 #endif
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
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;
35 return '?';
38 static inline unsigned int getid(FILE* f){
39 unsigned int id;
40 id=fgetc(f);
41 id=(id<<8)|fgetc(f);
42 id=(id<<8)|fgetc(f);
43 id=(id<<8)|fgetc(f);
44 return id;
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;
51 unsigned int fixat=0;
52 unsigned int offset=0;
53 int fix_flag=0;
54 FILE* f;
56 if(argc<=1){
57 printf("Usage: %s [-fix] badfile.avi\n",argv[0]);
58 exit(1);
61 if(!strcmp(argv[1],"-fix")){
62 fix_flag=1;
63 f=fopen(argv[argc-1],"rb+");
64 } else
65 f=fopen(argv[argc-1],"rb");
67 if(!f){
68 perror("error");
69 printf("couldnt open '%s'\n",argv[argc-1]);
70 exit(2);
73 while(1){
74 unsigned int id,len;
75 again:
76 id=fgetc(f);
77 id=(id<<8)|fgetc(f);
78 id=(id<<8)|fgetc(f);
79 faszom:
80 if(feof(f)) break;
81 // if(!lastgood && feof(f)) break;
82 id=(id<<8)|fgetc(f);
83 // lastgood=ftell(f);
84 mp_debug("%08X: %c%c%c%c\n",(int)ftell(f)-4,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
85 switch(id){
86 case FCC('R','I','F','F'):
87 fread(&len,4,1,f); // filesize
88 id=getid(f); // AVI
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));
90 break;
91 case FCC('L','I','S','T'):
92 fread(&len,4,1,f); // size
93 id=getid(f); // AVI
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'):
99 break;
100 // legal chunk IDs:
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
105 // index:
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??
111 // data:
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)
118 // info:
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
126 lastgood=ftell(f);
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);
132 fwrite(&len,4,1,f);
133 fseek(f,lastgood,SEEK_SET);
134 fixat=0;
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
140 break;
141 default:
142 if(!lastgood){
143 ++offset;
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);
149 fixat=lastgood;
150 lastgood=0;
151 goto again;
153 offset=0;
156 return 0;