a0e56e3c1ac9badec66df27ae95eca5bd146259d
[platform/upstream/gstreamer.git] / gst / dvdspu / Notes.txt
1 \r
2           DVD subtitles\r
3          ---------------\r
4 \r
5 \r
6   0. Introduction\r
7   1. Basics\r
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
12   6. Thanks\r
13   7. Changes\r
14 \r
15 \r
16 \r
17 \r
18 \r
19 The latest version of this document can be found here:\r
20 http://www.via.ecp.fr/~sam/doc/dvd/\r
21 \r
22 \r
23 \r
24 \r
25 \r
26 0. Introduction\r
27 \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
31 \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
34 almost all of it.\r
35 \r
36 \r
37 \r
38 \r
39 \r
40 1. Basics\r
41 \r
42   DVD subtitles are hidden in private PS packets (0x000001ba), just like AC3\r
43 streams are.\r
44 \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
50 \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
54 \r
55 \r
56 \r
57 \r
58 \r
59 2. The data structure\r
60 \r
61 A subtitle packet, after its parts have been collected and appended, looks\r
62 like this :\r
63 \r
64    +----------------------------------------------------------+    \r
65    |                                                          |\r
66    |   0    2                                         size    |\r
67    |   +----+------------------------+-----------------+      |\r
68    |   |size|       data packet      |     control     |      |\r
69    |   +----+------------------------+-----------------+      |\r
70    |                                                          |\r
71    |                     a subtitle packet                    |\r
72    |                                                          |\r
73    +----------------------------------------------------------+    \r
74 \r
75 size is a 2 bytes word, and data packet and control may have any size.\r
76 \r
77 \r
78 Here is the structure of the data packet :\r
79 \r
80    +----------------------------------------------------------+    \r
81    |                                                          |\r
82    |   2    4                                        S0+2     |\r
83    |   +----+------------------------------------------+      |\r
84    |   | S0 |                  data                    |      |\r
85    |   +----+------------------------------------------+      |\r
86    |                                                          |\r
87    |                      the data packet                     |\r
88    |                                                          |\r
89    +----------------------------------------------------------+    \r
90 \r
91 S0, the data packet size, is a 2 bytes word.\r
92 \r
93 \r
94 Finally, here's the structure of the control packet :\r
95 \r
96    +----------------------------------------------------------+    \r
97    |                                                          |\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
102    |                                                          |\r
103    |                     the control packet                   |\r
104    |                                                          |\r
105    +----------------------------------------------------------+    \r
106 \r
107 To summarize :\r
108 \r
109  - S1, at offset S0+2, the position of the end sequence\r
110  - several control sequences\r
111  - the 'ff' byte\r
112  - the end sequence\r
113 \r
114 \r
115 \r
116 \r
117 \r
118 3. Reading the control header\r
119 \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
123 \r
124  * type 0x01 : '01' - 1 byte\r
125   it seems to be an empty control sequence.\r
126 \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 wth color of the palette, encoded color 1 is the xth color, aso.\r
130 \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 plausable.\r
134 \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
142 \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
146 \r
147 The end sequence has this structure:\r
148 \r
149  xxxx yyyy 02 ff (ff)\r
150 \r
151  it ends with 'ff' or 'ffff', to make the whole packet have an even length.\r
152 \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
155 \r
156  yyyy is equal to S1 (see picture).\r
157 \r
158 \r
159 Example of a control header :\r
160 ----\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
162 ----\r
163 Let's decode it. First of all, S1 = 0x0a0c.\r
164 \r
165 The control sequences are :\r
166  01\r
167    Nothing to say about this one\r
168  03 02 31\r
169    Color 0 is 0, color 1 is 2, color 2 is 3, and color 3 is 1.\r
170  04 0F F0\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
175  06 00 06 04 E9\r
176    The first encoded image starts at offset 0x006, and the second one starts at 0x04e9.\r
177 \r
178 And the end sequence is :\r
179   00 93 0A 0C 02 FF\r
180    Which means... well, not many things now. We can at least verify that S1 (0x0a0c) is\r
181    there.\r
182 \r
183 \r
184 \r
185 \r
186 \r
187 4. Decoding the graphics\r
188 \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
191 \r
192    The picture is interlaced, for instance for a 40 lines picture :\r
193 \r
194   line 0  ---------------#----------\r
195   line 2  ------#-------------------\r
196    ...\r
197   line 38 ------------#-------------\r
198   line 1  ------------------#-------\r
199   line 3  --------#-----------------\r
200    ...\r
201   line 39 -------------#------------\r
202 \r
203    When decoding you should get:\r
204 \r
205   line 0  ---------------#----------\r
206   line 1  ------------------#-------\r
207   line 2  ------#-------------------\r
208   line 3  --------#-----------------\r
209    ...\r
210   line 38 ------------#-------------\r
211   line 39 -------------#------------\r
212 \r
213    Computers with weak processors could choose only to decode even lines\r
214   in order to gain some time, for instance.\r
215 \r
216 \r
217    The encoding is run-length encoded, with the following alphabet:\r
218 \r
219    0xf\r
220    0xe\r
221    0xd\r
222    0xc\r
223    0xb\r
224    0xa\r
225    0x9\r
226    0x8\r
227    0x7\r
228    0x6\r
229    0x5\r
230    0x4\r
231    0x3-\r
232    0x2-\r
233    0x1-\r
234    0x0f-\r
235    0x0e-\r
236    0x0d-\r
237    0x0c-\r
238    0x0b-\r
239    0x0a-\r
240    0x09-\r
241    0x08-\r
242    0x07-\r
243    0x06-\r
244    0x05-\r
245    0x04-\r
246    0x03--\r
247    0x02--\r
248    0x01--\r
249    0x0000\r
250 \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
254 \r
255    For instance, 0x23 means "8 pixels of color 3".\r
256 \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
261   of a line).\r
262 \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
265 \r
266    Perhaps I don't explain this very well, so you'd better have a look at\r
267   the enclosed source.\r
268 \r
269 \r
270 \r
271 \r
272 \r
273 5. What I do not know yet / What I need\r
274 \r
275 I don't know what's in the end sequence yet.\r
276 \r
277 Also, I don't know exactly when to display subtitles, and when to remove them.\r
278 \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
282 \r
283 I don't know what the "official" color palette is.\r
284 \r
285 I don't know how to handle transparency information.\r
286 \r
287 I don't know if this document is generic enough.\r
288 \r
289 So what I need is you :\r
290 \r
291  - if you can, patch this document or my programs to fix strange behaviour with your subtitles.\r
292 \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
295   file per language.\r
296 \r
297 \r
298 \r
299 \r
300 \r
301 6. Thanks\r
302 \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
305 \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
308 \r
309 \r
310 \r
311 \r
312 \r
313 7. Changes\r
314 \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
318 \r
319 \r
320 \r
321 \r
322 -- \r
323 Paris, January 16th 2000\r
324 Samuel Hocevar <sam@via.ecp.fr>\r