mixer: fix lowering hw volume while muted
[mplayer.git] / TOOLS / avisubdump.c
blobda886c433217c2f8b4361dee5e864136ec4f686a
1 /*
2 * avisubdump
4 * avi vobsub subtitle stream dumper (c) 2004 Tobias Diedrich
6 * The subtitles are dumped to stdout.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <unistd.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <fcntl.h>
28 #include <errno.h>
30 #define FCC(a,b,c,d) (((a))|((b)<<8)|((c)<<16)|((d)<<24))
32 #define FCC_RIFF FCC('R','I','F','F')
33 #define FCC_LIST FCC('L','I','S','T')
34 #define FCC_strh FCC('s','t','r','h')
35 #define FCC_txts FCC('t','x','t','s')
36 #define FCC_GAB2 FCC('G','A','B','2')
38 #define GAB_LANGUAGE 0
39 #define GAB_ENTRY 1
40 #define GAB_LANGUAGE_UNICODE 2
41 #define GAB_ENTRY_UNICODE 3
42 #define GAB_RAWTEXTSUBTITLE 4
44 static unsigned int getle16(FILE* f){
45 unsigned int res;
47 res = fgetc(f);
48 res |= fgetc(f) << 8;
50 return res;
53 static unsigned int getle(FILE* f){
54 unsigned int res;
56 res = fgetc(f);
57 res |= fgetc(f) << 8;
58 res |= fgetc(f) << 16;
59 res |= fgetc(f) << 24;
61 return res;
64 static void skip(FILE *f, int len)
66 if (f != stdin) {
67 fseek(f,len,SEEK_CUR);
68 } else {
69 void *buf = malloc(len);
70 fread(buf,len,1,f);
71 free(buf);
75 static int stream_id(unsigned int id)
77 char c1,c2;
78 c1 = (char)(id & 0xff);
79 c2 = (char)((id >> 8) & 0xff);
80 if (c1 >= '0' && c1 <= '9' &&
81 c2 >= '0' && c2 <= '9') {
82 c1 -= '0';
83 c2 -= '0';
84 return c1*10+c2;
86 return -1;
89 static int dumpsub_gab2(FILE *f, int size) {
90 int ret = 0;
92 while (ret + 6 <= size) {
93 unsigned int len, id;
94 char *buf;
95 int i;
97 id = getle16(f); ret += 2;
98 len = getle(f); ret += 4;
99 if (ret + len > size) break;
101 buf = malloc(len);
102 ret += fread(buf, 1, len, f);
104 switch (id) {
105 case GAB_LANGUAGE_UNICODE: /* FIXME: convert to utf-8; endianness */
106 for (i=0; i<len; i++) buf[i] = buf[i*2];
107 case GAB_LANGUAGE:
108 fprintf(stderr, "LANGUAGE: %s\n", buf);
109 break;
110 case GAB_ENTRY_UNICODE: /* FIXME: convert to utf-8; endianness */
111 for (i=0; i<len; i++) buf[i] = buf[i*2];
112 case GAB_ENTRY:
113 fprintf(stderr, "ENTRY: %s\n", buf);
114 break;
115 case GAB_RAWTEXTSUBTITLE:
116 printf("%s", buf);
117 break;
118 default:
119 fprintf(stderr, "Unknown type %d, len %d\n", id, len);
120 break;
122 free(buf);
125 return ret;
128 static void dump(FILE *f) {
129 unsigned int id, len;
130 int stream = 0;
131 int substream = -2;
133 while (1) {
134 id = getle(f);
135 len = getle(f);
137 if(feof(f)) break;
139 if (id == FCC_RIFF ||
140 id == FCC_LIST) {
141 getle(f);
142 continue;
143 } else if (id == FCC_strh) {
144 id = getle(f); len -= 4;
145 fprintf(stderr, "Stream %d is %c%c%c%c",
146 stream,
148 id >> 8,
149 id >> 16,
150 id >> 24);
151 if (id == FCC_txts) {
152 substream = stream;
153 fprintf(stderr, " (subtitle stream)");
155 fprintf(stderr, ".\n");
156 stream++;
157 } else if (stream_id(id) == substream) {
158 unsigned int subid;
159 subid = getle(f); len -= 4;
160 if (subid != FCC_GAB2) {
161 fprintf(stderr,
162 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
163 id, id >> 8, id >> 16, id >> 24, subid);
164 } else {
165 skip(f,1); len -= 1;
166 len -= dumpsub_gab2(f, len);
169 len+=len&1;
170 skip(f,len);
174 int main(int argc,char* argv[])
176 FILE* f;
178 if (argc != 2) {
179 fprintf(stderr, "Usage: %s <avi>\n", argv[0]);
180 exit(1);
183 if (strcmp(argv[argc-1], "-") == 0) {
184 dump(stdin);
185 return 0;
188 f=fopen(argv[argc-1],"rb");
190 if (!f) {
191 fprintf(stderr, "Could not open '%s': %s\n",
192 argv[argc-1], strerror(errno));
193 exit(-errno);
196 dump(f);
197 fclose(f);
199 return 0;