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.
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
40 #define GAB_LANGUAGE_UNICODE 2
41 #define GAB_ENTRY_UNICODE 3
42 #define GAB_RAWTEXTSUBTITLE 4
44 static unsigned int getle16(FILE* f
){
53 static unsigned int getle(FILE* f
){
58 res
|= fgetc(f
) << 16;
59 res
|= fgetc(f
) << 24;
64 static void skip(FILE *f
, int len
)
67 fseek(f
,len
,SEEK_CUR
);
69 void *buf
= malloc(len
);
75 static int stream_id(unsigned int id
)
78 c1
= (char)(id
& 0xff);
79 c2
= (char)((id
>> 8) & 0xff);
80 if (c1
>= '0' && c1
<= '9' &&
81 c2
>= '0' && c2
<= '9') {
89 static int dumpsub_gab2(FILE *f
, int size
) {
92 while (ret
+ 6 <= size
) {
97 id
= getle16(f
); ret
+= 2;
98 len
= getle(f
); ret
+= 4;
99 if (ret
+ len
> size
) break;
102 ret
+= fread(buf
, 1, len
, f
);
105 case GAB_LANGUAGE_UNICODE
: /* FIXME: convert to utf-8; endianness */
106 for (i
=0; i
<len
; i
++) buf
[i
] = buf
[i
*2];
108 fprintf(stderr
, "LANGUAGE: %s\n", buf
);
110 case GAB_ENTRY_UNICODE
: /* FIXME: convert to utf-8; endianness */
111 for (i
=0; i
<len
; i
++) buf
[i
] = buf
[i
*2];
113 fprintf(stderr
, "ENTRY: %s\n", buf
);
115 case GAB_RAWTEXTSUBTITLE
:
119 fprintf(stderr
, "Unknown type %d, len %d\n", id
, len
);
128 static void dump(FILE *f
) {
129 unsigned int id
, len
;
139 if (id
== FCC_RIFF
||
143 } else if (id
== FCC_strh
) {
144 id
= getle(f
); len
-= 4;
145 fprintf(stderr
, "Stream %d is %c%c%c%c",
151 if (id
== FCC_txts
) {
153 fprintf(stderr
, " (subtitle stream)");
155 fprintf(stderr
, ".\n");
157 } else if (stream_id(id
) == substream
) {
159 subid
= getle(f
); len
-= 4;
160 if (subid
!= FCC_GAB2
) {
162 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
163 id
, id
>> 8, id
>> 16, id
>> 24, subid
);
166 len
-= dumpsub_gab2(f
, len
);
174 int main(int argc
,char* argv
[])
179 fprintf(stderr
, "Usage: %s <avi>\n", argv
[0]);
183 if (strcmp(argv
[argc
-1], "-") == 0) {
188 f
=fopen(argv
[argc
-1],"rb");
191 fprintf(stderr
, "Could not open '%s': %s\n",
192 argv
[argc
-1], strerror(errno
));