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.
10 #define _LARGEFILE_SOURCE
11 #define _FILE_OFFSET_BITS 64
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
29 #define GAB_LANGUAGE_UNICODE 2
30 #define GAB_ENTRY_UNICODE 3
31 #define GAB_RAWTEXTSUBTITLE 4
33 static unsigned int getle16(FILE* f
){
42 static unsigned int getle(FILE* f
){
47 res
|= fgetc(f
) << 16;
48 res
|= fgetc(f
) << 24;
53 static void skip(FILE *f
, int len
)
56 fseek(f
,len
,SEEK_CUR
);
58 void *buf
= malloc(len
);
64 static int stream_id(unsigned int id
)
67 c1
= (char)(id
& 0xff);
68 c2
= (char)((id
>> 8) & 0xff);
69 if (c1
>= '0' && c1
<= '9' &&
70 c2
>= '0' && c2
<= '9') {
78 static int dumpsub_gab2(FILE *f
, int size
) {
81 while (ret
+ 6 <= size
) {
86 id
= getle16(f
); ret
+= 2;
87 len
= getle(f
); ret
+= 4;
88 if (ret
+ len
> size
) break;
91 ret
+= fread(buf
, 1, len
, f
);
94 case GAB_LANGUAGE_UNICODE
: /* FIXME: convert to utf-8; endianness */
95 for (i
=0; i
<len
; i
++) buf
[i
] = buf
[i
*2];
97 fprintf(stderr
, "LANGUAGE: %s\n", buf
);
99 case GAB_ENTRY_UNICODE
: /* FIXME: convert to utf-8; endianness */
100 for (i
=0; i
<len
; i
++) buf
[i
] = buf
[i
*2];
102 fprintf(stderr
, "ENTRY: %s\n", buf
);
104 case GAB_RAWTEXTSUBTITLE
:
108 fprintf(stderr
, "Unknown type %d, len %d\n", id
, len
);
117 static void dump(FILE *f
) {
118 unsigned int id
, len
;
128 if (id
== FCC_RIFF
||
132 } else if (id
== FCC_strh
) {
133 id
= getle(f
); len
-= 4;
134 fprintf(stderr
, "Stream %d is %c%c%c%c",
140 if (id
== FCC_txts
) {
142 fprintf(stderr
, " (subtitle stream)");
144 fprintf(stderr
, ".\n");
146 } else if (stream_id(id
) == substream
) {
148 subid
= getle(f
); len
-= 4;
149 if (subid
!= FCC_GAB2
) {
151 "Unknown subtitle chunk %c%c%c%c (%08x).\n",
152 id
, id
>> 8, id
>> 16, id
>> 24, subid
);
155 len
-= dumpsub_gab2(f
, len
);
163 int main(int argc
,char* argv
[])
168 fprintf(stderr
, "Usage: %s <avi>\n", argv
[0]);
172 if (strcmp(argv
[argc
-1], "-") == 0) f
=stdin
;
173 else f
=fopen(argv
[argc
-1],"rb");
176 fprintf(stderr
, "Could not open '%s': %s\n",
177 argv
[argc
-1], strerror(errno
));