annotate TOOLS/avisubdump.c @ 28108:df0f7d52de91

Merge two identical NetBSD/OpenBSD conditions.
author diego
date Sat, 13 Dec 2008 12:17:00 +0000
parents 3f3d90d27549
children b573c7c7173b
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 *
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
7 * The subtitles are dumped to stdout.
15886
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 #include <unistd.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
11 #include <stdio.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
12 #include <stdlib.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
13 #include <string.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
14 #include <fcntl.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
15 #include <errno.h>
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
16
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
17 #define FCC(a,b,c,d) (((a))|((b)<<8)|((c)<<16)|((d)<<24))
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
18
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
19 #define FCC_RIFF FCC('R','I','F','F')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
20 #define FCC_LIST FCC('L','I','S','T')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
21 #define FCC_strh FCC('s','t','r','h')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
22 #define FCC_txts FCC('t','x','t','s')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
23 #define FCC_GAB2 FCC('G','A','B','2')
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
24
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
25 #define GAB_LANGUAGE 0
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
26 #define GAB_ENTRY 1
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
27 #define GAB_LANGUAGE_UNICODE 2
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
28 #define GAB_ENTRY_UNICODE 3
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
29 #define GAB_RAWTEXTSUBTITLE 4
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
30
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
31 static unsigned int getle16(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
32 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
33
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
34 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
35 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
36
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
37 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
38 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
39
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
40 static unsigned int getle(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
41 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
42
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
43 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
44 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
45 res |= fgetc(f) << 16;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
46 res |= fgetc(f) << 24;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
47
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
48 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
49 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
50
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
51 static void skip(FILE *f, int len)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
52 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
53 if (f != stdin) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
54 fseek(f,len,SEEK_CUR);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
55 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
56 void *buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
57 fread(buf,len,1,f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
58 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
59 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
60 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
61
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
62 static int stream_id(unsigned int id)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
63 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
64 char c1,c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
65 c1 = (char)(id & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
66 c2 = (char)((id >> 8) & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
67 if (c1 >= '0' && c1 <= '9' &&
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
68 c2 >= '0' && c2 <= '9') {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
69 c1 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
70 c2 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
71 return c1*10+c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
72 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
73 return -1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
74 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
75
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
76 static int dumpsub_gab2(FILE *f, int size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
77 int ret = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
78
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
79 while (ret + 6 <= size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
80 unsigned int len, id;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
81 char *buf;
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
82 int i;
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
83
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
84 id = getle16(f); ret += 2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
85 len = getle(f); ret += 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
86 if (ret + len > size) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
87
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
88 buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
89 ret += fread(buf, 1, len, f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
90
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
91 switch (id) {
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
92 case GAB_LANGUAGE_UNICODE: /* FIXME: convert to utf-8; endianness */
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
93 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
94 case GAB_LANGUAGE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
95 fprintf(stderr, "LANGUAGE: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
96 break;
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
97 case GAB_ENTRY_UNICODE: /* FIXME: convert to utf-8; endianness */
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
98 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
99 case GAB_ENTRY:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
100 fprintf(stderr, "ENTRY: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
101 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
102 case GAB_RAWTEXTSUBTITLE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
103 printf("%s", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
104 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
105 default:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
106 fprintf(stderr, "Unknown type %d, len %d\n", id, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
107 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
108 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
109 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
110 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
111
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
112 return ret;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
113 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
114
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
115 static void dump(FILE *f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
116 unsigned int id, len;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
117 int stream = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
118 int substream = -2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
119
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
120 while (1) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
121 id = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
122 len = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
123
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
124 if(feof(f)) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
125
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
126 if (id == FCC_RIFF ||
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
127 id == FCC_LIST) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
128 getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
129 continue;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
130 } else if (id == FCC_strh) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
131 id = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
132 fprintf(stderr, "Stream %d is %c%c%c%c",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
133 stream,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
134 id,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
135 id >> 8,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
136 id >> 16,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
137 id >> 24);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
138 if (id == FCC_txts) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
139 substream = stream;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
140 fprintf(stderr, " (subtitle stream)");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
141 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
142 fprintf(stderr, ".\n");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
143 stream++;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
144 } else if (stream_id(id) == substream) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
145 unsigned int subid;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
146 subid = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
147 if (subid != FCC_GAB2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
148 fprintf(stderr,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
149 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
150 id, id >> 8, id >> 16, id >> 24, subid);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
151 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
152 skip(f,1); len -= 1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
153 len -= dumpsub_gab2(f, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
154 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
155 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
156 len+=len&1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
157 skip(f,len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
158 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
159 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
160
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
161 int main(int argc,char* argv[])
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
162 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
163 FILE* f;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
164
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
165 if (argc != 2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
166 fprintf(stderr, "Usage: %s <avi>\n", argv[0]);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
167 exit(1);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
168 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
169
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
170 if (strcmp(argv[argc-1], "-") == 0) f=stdin;
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
171 else f=fopen(argv[argc-1],"rb");
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
172
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
173 if (!f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
174 fprintf(stderr, "Could not open '%s': %s\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
175 argv[argc-1], strerror(errno));
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
176 exit(-errno);
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 dump(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
180
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
181 return 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
182 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
183