annotate TOOLS/avisubdump.c @ 15886:e44fe4cf44f9

avi subtitle stream dumper
author ranma
date Sat, 02 Jul 2005 15:22:49 +0000
parents
children 451c9ca8fd47
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
1 /*
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
2 * avisubdump
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
3 *
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
4 * avi vobsub subtitle stream dumper (c) 2004 Tobias Diedrich
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
5 * Licensed under GNU GPLv2 or (at your option) any later version.
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
6 *
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
7 * Compile with "make avisubdump"
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
8 */
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
9
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
10 #define _LARGEFILE_SOURCE
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
11 #define _FILE_OFFSET_BITS 64
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
12 #include <unistd.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
13 #include <stdio.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
14 #include <stdlib.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
15 #include <string.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
16 #include <fcntl.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
17 #include <errno.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
18
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
19 #define FCC(a,b,c,d) (((a))|((b)<<8)|((c)<<16)|((d)<<24))
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
20
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
21 #define FCC_RIFF FCC('R','I','F','F')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
22 #define FCC_LIST FCC('L','I','S','T')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
23 #define FCC_strh FCC('s','t','r','h')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
24 #define FCC_txts FCC('t','x','t','s')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
25 #define FCC_GAB2 FCC('G','A','B','2')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
26
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
27 #define GAB_LANGUAGE 0
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
28 #define GAB_ENTRY 1
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
29 #define GAB_LANGUAGE_UNICODE 2
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
30 #define GAB_ENTRY_UNICODE 3
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
31 #define GAB_RAWTEXTSUBTITLE 4
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
32
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
33 static char *subfile;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
34
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
35 static unsigned int getle16(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
36 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
37
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
38 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
39 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
40
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
41 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
42 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
43
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
44 static unsigned int getle(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
45 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
46
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
47 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
48 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
49 res |= fgetc(f) << 16;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
50 res |= fgetc(f) << 24;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
51
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
52 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
53 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
54
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
55 static void skip(FILE *f, int len)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
56 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
57 if (f != stdin) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
58 fseek(f,len,SEEK_CUR);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
59 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
60 void *buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
61 fread(buf,len,1,f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
62 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
63 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
64 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
65
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
66 static int stream_id(unsigned int id)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
67 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
68 char c1,c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
69 c1 = (char)(id & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
70 c2 = (char)((id >> 8) & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
71 if (c1 >= '0' && c1 <= '9' &&
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
72 c2 >= '0' && c2 <= '9') {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
73 c1 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
74 c2 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
75 return c1*10+c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
76 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
77 return -1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
78 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
79
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
80 static int dumpsub_gab2(FILE *f, int size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
81 int ret = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
82
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
83 while (ret + 6 <= size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
84 unsigned int len, id;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
85 char *buf;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
86 int i, fd;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
87
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
88 id = getle16(f); ret += 2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
89 len = getle(f); ret += 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
90 if (ret + len > size) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
91
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
92 buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
93 ret += fread(buf, 1, len, f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
94
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
95 switch (id) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
96 case GAB_LANGUAGE_UNICODE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
97 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
98 case GAB_LANGUAGE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
99 fprintf(stderr, "LANGUAGE: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
100 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
101 case GAB_ENTRY_UNICODE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
102 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
103 case GAB_ENTRY:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
104 fprintf(stderr, "ENTRY: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
105 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
106 case GAB_RAWTEXTSUBTITLE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
107 printf("%s", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
108 fd = open(subfile, O_CREAT|O_APPEND|O_WRONLY, 0644);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
109 write(fd, buf, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
110 close(fd);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
111 fprintf(stderr, "Dumped subtitles to %s.\n", subfile);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
112 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
113 default:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
114 fprintf(stderr, "Unknown type %d, len %d\n", id, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
115 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
116 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
117 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
118 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
119
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
120 return ret;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
121 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
122
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
123 static void dump(FILE *f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
124 unsigned int id, len;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
125 int stream = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
126 int substream = -2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
127
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
128 while (1) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
129 id = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
130 len = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
131
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
132 if(feof(f)) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
133
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
134 if (id == FCC_RIFF ||
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
135 id == FCC_LIST) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
136 getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
137 continue;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
138 } else if (id == FCC_strh) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
139 id = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
140 fprintf(stderr, "Stream %d is %c%c%c%c",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
141 stream,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
142 id,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
143 id >> 8,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
144 id >> 16,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
145 id >> 24);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
146 if (id == FCC_txts) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
147 substream = stream;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
148 fprintf(stderr, " (subtitle stream)");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
149 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
150 fprintf(stderr, ".\n");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
151 stream++;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
152 } else if (stream_id(id) == substream) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
153 unsigned int subid;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
154 subid = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
155 if (subid != FCC_GAB2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
156 fprintf(stderr,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
157 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
158 id, id >> 8, id >> 16, id >> 24, subid);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
159 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
160 skip(f,1); len -= 1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
161 len -= dumpsub_gab2(f, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
162 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
163 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
164 len+=len&1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
165 skip(f,len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
166 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
167 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
168
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
169 int main(int argc,char* argv[])
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
170 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
171 FILE* f;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
172 int i;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
173
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
174 if (argc != 2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
175 fprintf(stderr, "Usage: %s <avi>\n", argv[0]);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
176 exit(1);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
177 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
178
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
179 f=fopen(argv[argc-1],"rb");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
180
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
181 if (!f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
182 fprintf(stderr, "Could not open '%s': %s\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
183 argv[argc-1], strerror(errno));
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
184 exit(-errno);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
185 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
186
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
187 subfile = malloc(strlen(argv[1]) + 4);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
188 strcpy(subfile, argv[1]);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
189 for (i=strlen(subfile); i>0 && subfile[i] != '.'; i--);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
190 subfile[i] = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
191 strcat(subfile, ".ssa");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
192
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
193 dump(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
194
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
195 free(subfile);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
196
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
197 return 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
198 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
199