annotate TOOLS/avisubdump.c @ 31638:e4cab1a16c0d

Simplify alpha conversion code.
author reimar
date Sun, 11 Jul 2010 13:05:45 +0000
parents 32725ca88fed
children 5f505a260e9c
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 *
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
6 * The subtitles are dumped to stdout.
30416
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
7 *
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
8 * This program is free software; you can redistribute it and/or modify
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
9 * it under the terms of the GNU General Public License as published by
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
10 * the Free Software Foundation; either version 2 of the License, or
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
11 * (at your option) any later version.
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
12 *
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
13 * This program is distributed in the hope that it will be useful,
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
16 * GNU General Public License for more details.
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
17 *
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
18 * You should have received a copy of the GNU General Public License along
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
19 * with this program; if not, write to the Free Software Foundation, Inc.,
b573c7c7173b Add standard license header to C tools missing them.
diego
parents: 26456
diff changeset
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
21 */
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
22
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
23 #include <unistd.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
24 #include <stdio.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
25 #include <stdlib.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
26 #include <string.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
27 #include <fcntl.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
28 #include <errno.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
29
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
30 #define FCC(a,b,c,d) (((a))|((b)<<8)|((c)<<16)|((d)<<24))
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
31
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
32 #define FCC_RIFF FCC('R','I','F','F')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
33 #define FCC_LIST FCC('L','I','S','T')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
34 #define FCC_strh FCC('s','t','r','h')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
35 #define FCC_txts FCC('t','x','t','s')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
36 #define FCC_GAB2 FCC('G','A','B','2')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
37
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
38 #define GAB_LANGUAGE 0
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
39 #define GAB_ENTRY 1
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
40 #define GAB_LANGUAGE_UNICODE 2
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
41 #define GAB_ENTRY_UNICODE 3
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
42 #define GAB_RAWTEXTSUBTITLE 4
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
43
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
44 static unsigned int getle16(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
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
50 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
51 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
52
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
53 static unsigned int getle(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
54 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
55
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
56 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
57 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
58 res |= fgetc(f) << 16;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
59 res |= fgetc(f) << 24;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
60
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
61 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
62 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
63
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
64 static void skip(FILE *f, int len)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
65 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
66 if (f != stdin) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
67 fseek(f,len,SEEK_CUR);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
68 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
69 void *buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
70 fread(buf,len,1,f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
71 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
72 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
73 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
74
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
75 static int stream_id(unsigned int id)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
76 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
77 char c1,c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
78 c1 = (char)(id & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
79 c2 = (char)((id >> 8) & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
80 if (c1 >= '0' && c1 <= '9' &&
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
81 c2 >= '0' && c2 <= '9') {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
82 c1 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
83 c2 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
84 return c1*10+c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
85 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
86 return -1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
87 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
88
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
89 static int dumpsub_gab2(FILE *f, int size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
90 int ret = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
91
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
92 while (ret + 6 <= size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
93 unsigned int len, id;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
94 char *buf;
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
95 int i;
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
96
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
97 id = getle16(f); ret += 2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
98 len = getle(f); ret += 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
99 if (ret + len > size) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
100
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
101 buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
102 ret += fread(buf, 1, len, f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
103
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
104 switch (id) {
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
105 case GAB_LANGUAGE_UNICODE: /* FIXME: convert to utf-8; endianness */
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
106 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
107 case GAB_LANGUAGE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
108 fprintf(stderr, "LANGUAGE: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
109 break;
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
110 case GAB_ENTRY_UNICODE: /* FIXME: convert to utf-8; endianness */
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
111 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
112 case GAB_ENTRY:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
113 fprintf(stderr, "ENTRY: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
114 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
115 case GAB_RAWTEXTSUBTITLE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
116 printf("%s", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
117 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
118 default:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
119 fprintf(stderr, "Unknown type %d, len %d\n", id, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
120 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
121 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
122 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
123 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
124
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
125 return ret;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
126 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
127
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
128 static void dump(FILE *f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
129 unsigned int id, len;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
130 int stream = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
131 int substream = -2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
132
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
133 while (1) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
134 id = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
135 len = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
136
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
137 if(feof(f)) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
138
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
139 if (id == FCC_RIFF ||
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
140 id == FCC_LIST) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
141 getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
142 continue;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
143 } else if (id == FCC_strh) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
144 id = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
145 fprintf(stderr, "Stream %d is %c%c%c%c",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
146 stream,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
147 id,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
148 id >> 8,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
149 id >> 16,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
150 id >> 24);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
151 if (id == FCC_txts) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
152 substream = stream;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
153 fprintf(stderr, " (subtitle stream)");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
154 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
155 fprintf(stderr, ".\n");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
156 stream++;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
157 } else if (stream_id(id) == substream) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
158 unsigned int subid;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
159 subid = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
160 if (subid != FCC_GAB2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
161 fprintf(stderr,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
162 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
163 id, id >> 8, id >> 16, id >> 24, subid);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
164 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
165 skip(f,1); len -= 1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
166 len -= dumpsub_gab2(f, len);
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 len+=len&1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
170 skip(f,len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
171 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
172 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
173
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
174 int main(int argc,char* argv[])
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
175 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
176 FILE* f;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
177
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
178 if (argc != 2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
179 fprintf(stderr, "Usage: %s <avi>\n", argv[0]);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
180 exit(1);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
181 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
182
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
183 if (strcmp(argv[argc-1], "-") == 0) f=stdin;
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
184 else f=fopen(argv[argc-1],"rb");
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
185
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
186 if (!f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
187 fprintf(stderr, "Could not open '%s': %s\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
188 argv[argc-1], strerror(errno));
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
189 exit(-errno);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
190 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
191
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
192 dump(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
193
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
194 return 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
195 }