typo fix
[mplayer/greg.git] / TOOLS / avisubdump.c
blob9cc6d976eb91623c6fa7137b1fe734445c772207
1 /*
2 * avisubdump
4 * avi vobsub subtitle stream dumper (c) 2004 Tobias Diedrich
5 * Licensed under GNU GPLv2 or (at your option) any later version.
7 * The subtitles are dumped to stdout.
8 */
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>
19 #define FCC(a,b,c,d) (((a))|((b)<<8)|((c)<<16)|((d)<<24))
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')
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
33 static unsigned int getle16(FILE* f){
34 unsigned int res;
36 res = fgetc(f);
37 res |= fgetc(f) << 8;
39 return res;
42 static unsigned int getle(FILE* f){
43 unsigned int res;
45 res = fgetc(f);
46 res |= fgetc(f) << 8;
47 res |= fgetc(f) << 16;
48 res |= fgetc(f) << 24;
50 return res;
53 static void skip(FILE *f, int len)
55 if (f != stdin) {
56 fseek(f,len,SEEK_CUR);
57 } else {
58 void *buf = malloc(len);
59 fread(buf,len,1,f);
60 free(buf);
64 static int stream_id(unsigned int id)
66 char c1,c2;
67 c1 = (char)(id & 0xff);
68 c2 = (char)((id >> 8) & 0xff);
69 if (c1 >= '0' && c1 <= '9' &&
70 c2 >= '0' && c2 <= '9') {
71 c1 -= '0';
72 c2 -= '0';
73 return c1*10+c2;
75 return -1;
78 static int dumpsub_gab2(FILE *f, int size) {
79 int ret = 0;
81 while (ret + 6 <= size) {
82 unsigned int len, id;
83 char *buf;
84 int i;
86 id = getle16(f); ret += 2;
87 len = getle(f); ret += 4;
88 if (ret + len > size) break;
90 buf = malloc(len);
91 ret += fread(buf, 1, len, f);
93 switch (id) {
94 case GAB_LANGUAGE_UNICODE: /* FIXME: convert to utf-8; endianness */
95 for (i=0; i<len; i++) buf[i] = buf[i*2];
96 case GAB_LANGUAGE:
97 fprintf(stderr, "LANGUAGE: %s\n", buf);
98 break;
99 case GAB_ENTRY_UNICODE: /* FIXME: convert to utf-8; endianness */
100 for (i=0; i<len; i++) buf[i] = buf[i*2];
101 case GAB_ENTRY:
102 fprintf(stderr, "ENTRY: %s\n", buf);
103 break;
104 case GAB_RAWTEXTSUBTITLE:
105 printf("%s", buf);
106 break;
107 default:
108 fprintf(stderr, "Unknown type %d, len %d\n", id, len);
109 break;
111 free(buf);
114 return ret;
117 static void dump(FILE *f) {
118 unsigned int id, len;
119 int stream = 0;
120 int substream = -2;
122 while (1) {
123 id = getle(f);
124 len = getle(f);
126 if(feof(f)) break;
128 if (id == FCC_RIFF ||
129 id == FCC_LIST) {
130 getle(f);
131 continue;
132 } else if (id == FCC_strh) {
133 id = getle(f); len -= 4;
134 fprintf(stderr, "Stream %d is %c%c%c%c",
135 stream,
137 id >> 8,
138 id >> 16,
139 id >> 24);
140 if (id == FCC_txts) {
141 substream = stream;
142 fprintf(stderr, " (subtitle stream)");
144 fprintf(stderr, ".\n");
145 stream++;
146 } else if (stream_id(id) == substream) {
147 unsigned int subid;
148 subid = getle(f); len -= 4;
149 if (subid != FCC_GAB2) {
150 fprintf(stderr,
151 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
152 id, id >> 8, id >> 16, id >> 24, subid);
153 } else {
154 skip(f,1); len -= 1;
155 len -= dumpsub_gab2(f, len);
158 len+=len&1;
159 skip(f,len);
163 int main(int argc,char* argv[])
165 FILE* f;
167 if (argc != 2) {
168 fprintf(stderr, "Usage: %s <avi>\n", argv[0]);
169 exit(1);
172 if (strcmp(argv[argc-1], "-") == 0) f=stdin;
173 else f=fopen(argv[argc-1],"rb");
175 if (!f) {
176 fprintf(stderr, "Could not open '%s': %s\n",
177 argv[argc-1], strerror(errno));
178 exit(-errno);
181 dump(f);
183 return 0;