annotate TOOLS/avisubdump.c @ 24185:a5288270d416

Remove ugly Russian language support hack. Currently support for only Latin-1 languages is left. Proper solution for another languages will be added later.
author voroshil
date Sun, 26 Aug 2007 05:15:53 +0000
parents 451c9ca8fd47
children 3f3d90d27549
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 #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 unsigned int getle16(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
34 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
35
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
36 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
37 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
38
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
39 return res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
40 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
41
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
42 static unsigned int getle(FILE* f){
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
43 unsigned int res;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
44
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
45 res = fgetc(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
46 res |= fgetc(f) << 8;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
47 res |= fgetc(f) << 16;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
48 res |= fgetc(f) << 24;
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 void skip(FILE *f, int len)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
54 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
55 if (f != stdin) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
56 fseek(f,len,SEEK_CUR);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
57 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
58 void *buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
59 fread(buf,len,1,f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
60 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
61 }
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 int stream_id(unsigned int id)
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
65 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
66 char c1,c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
67 c1 = (char)(id & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
68 c2 = (char)((id >> 8) & 0xff);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
69 if (c1 >= '0' && c1 <= '9' &&
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
70 c2 >= '0' && c2 <= '9') {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
71 c1 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
72 c2 -= '0';
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
73 return c1*10+c2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
74 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
75 return -1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
76 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
77
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
78 static int dumpsub_gab2(FILE *f, int size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
79 int ret = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
80
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
81 while (ret + 6 <= size) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
82 unsigned int len, id;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
83 char *buf;
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
84 int i;
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
85
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
86 id = getle16(f); ret += 2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
87 len = getle(f); ret += 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
88 if (ret + len > size) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
89
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
90 buf = malloc(len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
91 ret += fread(buf, 1, len, f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
92
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
93 switch (id) {
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
94 case GAB_LANGUAGE_UNICODE: /* FIXME: convert to utf-8; endianness */
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
95 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
96 case GAB_LANGUAGE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
97 fprintf(stderr, "LANGUAGE: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
98 break;
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
99 case GAB_ENTRY_UNICODE: /* FIXME: convert to utf-8; endianness */
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
100 for (i=0; i<len; i++) buf[i] = buf[i*2];
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
101 case GAB_ENTRY:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
102 fprintf(stderr, "ENTRY: %s\n", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
103 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
104 case GAB_RAWTEXTSUBTITLE:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
105 printf("%s", buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
106 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
107 default:
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
108 fprintf(stderr, "Unknown type %d, len %d\n", id, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
109 break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
110 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
111 free(buf);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
112 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
113
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
114 return ret;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
115 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
116
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
117 static void dump(FILE *f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
118 unsigned int id, len;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
119 int stream = 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
120 int substream = -2;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
121
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
122 while (1) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
123 id = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
124 len = getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
125
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
126 if(feof(f)) break;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
127
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
128 if (id == FCC_RIFF ||
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
129 id == FCC_LIST) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
130 getle(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
131 continue;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
132 } else if (id == FCC_strh) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
133 id = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
134 fprintf(stderr, "Stream %d is %c%c%c%c",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
135 stream,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
136 id,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
137 id >> 8,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
138 id >> 16,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
139 id >> 24);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
140 if (id == FCC_txts) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
141 substream = stream;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
142 fprintf(stderr, " (subtitle stream)");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
143 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
144 fprintf(stderr, ".\n");
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
145 stream++;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
146 } else if (stream_id(id) == substream) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
147 unsigned int subid;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
148 subid = getle(f); len -= 4;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
149 if (subid != FCC_GAB2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
150 fprintf(stderr,
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
151 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
152 id, id >> 8, id >> 16, id >> 24, subid);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
153 } else {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
154 skip(f,1); len -= 1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
155 len -= dumpsub_gab2(f, len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
156 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
157 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
158 len+=len&1;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
159 skip(f,len);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
160 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
161 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
162
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
163 int main(int argc,char* argv[])
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
164 {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
165 FILE* f;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
166
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
167 if (argc != 2) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
168 fprintf(stderr, "Usage: %s <avi>\n", argv[0]);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
169 exit(1);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
170 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
171
15891
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
172 if (strcmp(argv[argc-1], "-") == 0) f=stdin;
451c9ca8fd47 Only dump to stdout
ranma
parents: 15886
diff changeset
173 else f=fopen(argv[argc-1],"rb");
15886
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
174
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
175 if (!f) {
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
176 fprintf(stderr, "Could not open '%s': %s\n",
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
177 argv[argc-1], strerror(errno));
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
178 exit(-errno);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
179 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
180
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
181 dump(f);
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
182
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
183 return 0;
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
184 }
e44fe4cf44f9 avi subtitle stream dumper
ranma
parents:
diff changeset
185