8 2. The data structure
\r
9 3. Reading the control header
\r
10 4. Decoding the graphics
\r
11 5. What I do not know yet / What I need
\r
19 The latest version of this document can be found here:
\r
20 http://www.via.ecp.fr/~sam/doc/dvd/
\r
28 One of the last things we missed in DVD decoding under my system was the
\r
29 decoding of subtitles. I found no information on the web or Usenet about them,
\r
30 apart from a few words on them being run-length encoded in the DVD FAQ.
\r
32 So we decided to reverse-engineer their format (it's completely legal in
\r
33 France, since we did it on interoperability purposes), and managed to get
\r
42 DVD subtitles are hidden in private PS packets (0x000001ba), just like AC3
\r
45 Within the PS packet, there are PES packets, and like AC3, the header for the
\r
46 ones containing subtitles have a 0x000001bd header.
\r
47 As for AC3, where there's an ID like (0x80 + x), there's a subtitle ID equal
\r
48 to (0x20 + x), where x is the subtitle ID. Thus there seems to be only
\r
49 16 possible different subtitles on a DVD (my Taxi Driver copy has 16).
\r
51 I'll suppose you know how to extract AC3 from a DVD, and jump to the
\r
52 interesting part of this documentation. Anyway you're unlikely to have
\r
53 understood what I said without already being familiar with MPEG2.
\r
59 2. The data structure
\r
61 A subtitle packet, after its parts have been collected and appended, looks
\r
64 +----------------------------------------------------------+
\r
67 | +----+------------------------+-----------------+ |
\r
68 | |size| data packet | control | |
\r
69 | +----+------------------------+-----------------+ |
\r
71 | a subtitle packet |
\r
73 +----------------------------------------------------------+
\r
75 size is a 2 bytes word, and data packet and control may have any size.
\r
78 Here is the structure of the data packet :
\r
80 +----------------------------------------------------------+
\r
83 | +----+------------------------------------------+ |
\r
85 | +----+------------------------------------------+ |
\r
89 +----------------------------------------------------------+
\r
91 S0, the data packet size, is a 2 bytes word.
\r
94 Finally, here's the structure of the control packet :
\r
96 +----------------------------------------------------------+
\r
98 | S0+2 S0+4 S1 size |
\r
99 | +----+---------+---------+--+---------+--+---------+ |
\r
100 | | S1 |ctrl seq |ctrl seq |..|ctrl seq |ff| end seq | |
\r
101 | +----+---------+---------+--+---------+--+---------+ |
\r
103 | the control packet |
\r
105 +----------------------------------------------------------+
\r
109 - S1, at offset S0+2, the position of the end sequence
\r
110 - several control sequences
\r
118 3. Reading the control header
\r
120 The first thing to read is the control sequences. There are several
\r
121 types of them, and each type is determined by its first byte. As far
\r
122 as I know, each type has a fixed length.
\r
124 * type 0x01 : '01' - 1 byte
\r
125 it seems to be an empty control sequence.
\r
127 * type 0x03 : '03wxyz' - 3 bytes
\r
128 this one has the palette information ; it basically says 'encoded color 0
\r
129 is the with color of the palette, encoded color 1 is the xth color, aso.
\r
131 * type 0x04 : '04wxyz' - 3 bytes
\r
132 I *think* this is the alpha channel information ; I only saw values of 0 or f
\r
133 for those nibbles, so I can't really be sure, but it seems plausible.
\r
135 * type 0x05 : '05xxxXXXyyyYYY' - 7 bytes
\r
136 the coordinates of the subtitle on the screen :
\r
137 xxx is the first column of the subtitle
\r
138 XXX is the last column of the subtitle
\r
139 yyy is the first line of the subtitle
\r
140 YYY is the last line of the subtitle
\r
141 thus the subtitle's size is (XXX-xxx+1) x (YYY-yyy+1)
\r
143 * type 0x06 : '06xxxxyyyy' - 5 bytes
\r
144 xxxx is the position of the first graphic line, and yyyy is the position of
\r
145 the second one (the graphics are interlaced, so it helps a lot :p)
\r
147 The end sequence has this structure:
\r
149 xxxx yyyy 02 ff (ff)
\r
151 it ends with 'ff' or 'ffff', to make the whole packet have an even length.
\r
153 FIXME: I absolutely don't know what xxxx is. I suppose it may be some date
\r
154 information since I found it nowhere else, but I can't be sure.
\r
156 yyyy is equal to S1 (see picture).
\r
159 Example of a control header :
\r
161 0A 0C 01 03 02 31 04 0F F0 05 00 02 CF 00 22 3E 06 00 06 04 E9 FF 00 93 0A 0C 02 FF
\r
163 Let's decode it. First of all, S1 = 0x0a0c.
\r
165 The control sequences are :
\r
167 Nothing to say about this one
\r
169 Color 0 is 0, color 1 is 2, color 2 is 3, and color 3 is 1.
\r
171 Colors 0 and 3 are transparent, and colors 2 and 3 are opaque (not sure of this one)
\r
172 05 00 02 CF 00 22 3E
\r
173 The first column is 0x000, the last one is 0x2cf, the first line is 0x002, and
\r
174 the last line is 0x23e. Thus the subtitle's size is 0x2d0 x 0x23d.
\r
176 The first encoded image starts at offset 0x006, and the second one starts at 0x04e9.
\r
178 And the end sequence is :
\r
180 Which means... well, not many things now. We can at least verify that S1 (0x0a0c) is
\r
187 4. Decoding the graphics
\r
189 The graphics are rather easy to decode (at least, when you know how to do it - it
\r
190 took us one whole week to figure out what the encoding was :p).
\r
192 The picture is interlaced, for instance for a 40 lines picture :
\r
194 line 0 ---------------#----------
\r
195 line 2 ------#-------------------
\r
197 line 38 ------------#-------------
\r
198 line 1 ------------------#-------
\r
199 line 3 --------#-----------------
\r
201 line 39 -------------#------------
\r
203 When decoding you should get:
\r
205 line 0 ---------------#----------
\r
206 line 1 ------------------#-------
\r
207 line 2 ------#-------------------
\r
208 line 3 --------#-----------------
\r
210 line 38 ------------#-------------
\r
211 line 39 -------------#------------
\r
213 Computers with weak processors could choose only to decode even lines
\r
214 in order to gain some time, for instance.
\r
217 The encoding is run-length encoded, with the following alphabet:
\r
251 '-' stands for any other nibble. Once a sequence X of this alphabet has
\r
252 been read, the pixels can be displayed : (X >> 2) is the number of pixels
\r
253 to display, and (X & 0x3) is the color of the pixel.
\r
255 For instance, 0x23 means "8 pixels of color 3".
\r
257 "0000" has a special meaning : it's a carriage return. The decoder should
\r
258 do a carriage return when reaching the end of the line, or when encountering
\r
259 this "0000" sequence. When doing a carriage return, the parser should be
\r
260 reset to the next even position (it cannot be nibble-aligned at the start
\r
263 After a carriage return, the parser should read a line on the other
\r
264 interlaced picture, and swap like this after each carriage return.
\r
266 Perhaps I don't explain this very well, so you'd better have a look at
\r
267 the enclosed source.
\r
273 5. What I do not know yet / What I need
\r
275 I don't know what's in the end sequence yet.
\r
277 Also, I don't know exactly when to display subtitles, and when to remove them.
\r
279 I don't know if there are other types of control sequences (in my programs I consider
\r
280 0xff as a control sequence type, as well as 0x02. I don't know if it's correct or not,
\r
281 so please comment on this).
\r
283 I don't know what the "official" color palette is.
\r
285 I don't know how to handle transparency information.
\r
287 I don't know if this document is generic enough.
\r
289 So what I need is you :
\r
291 - if you can, patch this document or my programs to fix strange behaviour with your subtitles.
\r
293 - send me your subtitles (there's a program to extract them enclosed) ; the first 10 KB
\r
294 of subtitles in a VOB should be enough, but it would be cool if you sent me one subtitle
\r
303 Thanks to Michel Lespinasse <walken@via.ecp.fr> for his great help on understanding
\r
304 the RLE stuff, and for all the ideas he had.
\r
306 Thanks to mass (David Waite) and taaz (David I. Lehn) from irc at
\r
307 openprojects.net for sending me their subtitles.
\r
315 20000116: added the 'changes' section.
\r
316 20000116: added David Waite's and David I. Lehn's name.
\r
317 20000116: changed "x0" and "x1" to "S0" and "S1" to make it less confusing.
\r
323 Paris, January 16th 2000
\r
324 Samuel Hocevar <sam@via.ecp.fr>
\r