annotate TOOLS/avi-fix.c @ 13983:47ebd214fe19

synced to 1.21 (URL update)
author gabrov
date Fri, 19 Nov 2004 19:35:20 +0000
parents 217d10ec0e18
children 31660114d885
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11924
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
1 // avi-fix v0.1 (C) A'rpi, license GPL
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
2 // simple tool to fix chunk sizes in a RIFF AVI file
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
3 // it doesn't check/fix index, use mencoder -forceidx -oac copy -ovc copy to fix index!
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
4
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
5 #include <stdio.h>
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
6 #include <stdlib.h>
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
7 //#include <string.h>
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
8
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
9 #define FCC(a,b,c,d) (((a)<<24)|((b)<<16)|((c)<<8)|(d))
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
10
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
11 static inline xx(unsigned char c){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
12 if(c>=32 && c<128) return c;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
13 return '?';
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
14 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
15
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
16 static inline unsigned int getid(FILE* f){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
17 unsigned int id;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
18 id=fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
19 id=(id<<8)|fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
20 id=(id<<8)|fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
21 id=(id<<8)|fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
22 return id;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
23 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
24
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
25 int main(int argc,char* argv[]){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
26 //FILE* f=fopen("edgar.avi","rb"); // readonly (report errors)
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
27 //FILE* f=fopen("edgar.avi","rb+"); // fix mode (fix chunk sizes)
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
28 unsigned int lastgood=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
29 unsigned int fixat=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
30 unsigned int offset=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
31 int fix_flag=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
32 FILE* f;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
33
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
34 if(argc<=1){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
35 printf("Usage: %s [-fix] badfile.avi\n",argv[0]);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
36 exit(1);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
37 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
38
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
39 if(!strcmp(argv[1],"-fix")){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
40 fix_flag=1;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
41 f=fopen(argv[argc-1],"rb+");
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
42 } else
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
43 f=fopen(argv[argc-1],"rb");
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
44
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
45 if(!f){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
46 perror("error");
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
47 printf("couldnt open '%s'\n",argv[argc-1]);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
48 exit(2);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
49 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
50
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
51 while(1){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
52 unsigned int id,len;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
53 again:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
54 id=fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
55 id=(id<<8)|fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
56 id=(id<<8)|fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
57 faszom:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
58 if(feof(f)) break;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
59 // if(!lastgood && feof(f)) break;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
60 id=(id<<8)|fgetc(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
61 // lastgood=ftell(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
62 printf("%08X: %c%c%c%c\n",(int)ftell(f)-4,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
63 switch(id){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
64 case FCC('R','I','F','F'):
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
65 fread(&len,4,1,f); // filesize
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
66 id=getid(f); // AVI
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
67 printf("RIFF header, filesize=0x%X format=%c%c%c%c\n",len,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
68 break;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
69 case FCC('L','I','S','T'):
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
70 fread(&len,4,1,f); // size
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
71 id=getid(f); // AVI
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
72 printf("LIST size=0x%X format=%c%c%c%c\n",len,xx(id>>24),xx(id>>16),xx(id>>8),xx(id));
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
73 //case FCC('h','d','r','l'):
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
74 //case FCC('s','t','r','l'):
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
75 //case FCC('o','d','m','l'):
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
76 //case FCC('m','o','v','i'):
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
77 break;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
78 // legal chunk IDs:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
79 case FCC('a','v','i','h'): // avi header
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
80 case FCC('s','t','r','h'): // stream header
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
81 case FCC('s','t','r','f'): // stream format
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
82 case FCC('J','U','N','K'): // official shit
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
83 // index:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
84 case FCC('i','d','x','1'): // main index??
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
85 case FCC('d','m','l','h'): // opendml header
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
86 case FCC('i','n','d','x'): // opendml main index??
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
87 case FCC('i','x','0','0'): // opendml sub index??
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
88 case FCC('i','x','0','1'): // opendml sub index??
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
89 // data:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
90 case FCC('0','1','w','b'): // audio track #1
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
91 case FCC('0','2','w','b'): // audio track #2
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
92 case FCC('0','3','w','b'): // audio track #3
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
93 case FCC('0','0','d','b'): // uncompressed video
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
94 case FCC('0','0','d','c'): // compressed video
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
95 case FCC('0','0','_','_'): // A-V interleaved (type2 DV file)
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
96 // info:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
97 case FCC('I','S','F','T'): // INFO: software
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
98 case FCC('I','S','R','C'): // INFO: source
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
99 case FCC('I','N','A','M'): // INFO: name
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
100 case FCC('I','S','B','J'): // INFO: subject
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
101 case FCC('I','A','R','T'): // INFO: artist
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
102 case FCC('I','C','O','P'): // INFO: copyright
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
103 case FCC('I','C','M','T'): // INFO: comment
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
104 lastgood=ftell(f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
105 if(fixat && fix_flag){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
106 // fix last chunk's size field:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
107 fseek(f,fixat,SEEK_SET);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
108 len=lastgood-fixat-8;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
109 printf("Correct len to 0x%X\n",len);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
110 fwrite(&len,4,1,f);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
111 fseek(f,lastgood,SEEK_SET);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
112 fixat=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
113 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
114 fread(&len,4,1,f); // size
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
115 printf("ID ok, chunk len=0x%X\n",len);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
116 len+=len&1; // align at 2
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
117 fseek(f,len,SEEK_CUR); // skip data
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
118 break;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
119 default:
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
120 if(!lastgood){
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
121 ++offset;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
122 printf("invalid ID, trying %d byte offset\n",offset);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
123 goto faszom; // try again @ next post
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
124 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
125 printf("invalid ID, parsing next chunk's data at 0x%X\n",lastgood);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
126 fseek(f,lastgood,SEEK_SET);
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
127 fixat=lastgood;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
128 lastgood=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
129 goto again;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
130 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
131 offset=0;
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
132 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
133
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
134
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
135 }
217d10ec0e18 avi-fix v0.1: simple tool to fix chunk sizes in a RIFF AVI file
arpi
parents:
diff changeset
136