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