Attempt to determine DVD structure by reading sectors
=====================================================

Sector size is 2048 bytes.

Basically for each title IFO is followed by VOBUs of menu (if present), VOBUs ot title
(if present) and BUP. Usually they just follow each others but some recording rules
may force gaps between them. This emans that parsing the ISO filesystem structure
is required to find the start and length of each elements.

Read VIDEO_TS/IFO (VMG):
------------------------

Offset 0x0C, size 4: last LB of BUP, i.e. size - 1 of IFO + VOB + BUP.
Offset 0x1C, size 4: last LB of IFO, i.e. size - 1 of IFO.
Offset 0x3E, size 2: nr of VTS
Offset 0xC0, size 4: offset from start of IFO (unit = 1 LB) to VIDEO_TS/VOB.
Offset 0xC4, size 4: offset from start of IFO (unit = 1 LB) of table of titles.
Offset 0xD8, size 4: offset from start of IFO (unit = 1 LB) of table of menu
                     cell adresses.

Table of titles:
Offset 0x00, size 2: nr of titles
Offset 0x08, list of titles (12 bytes each)

Title:
Offset 0x02, size 2: nr of chapters
Offset 0x06, size 1: vts nr (the XX of VTS_XX_... files)
Offset 0x07, size 1: vts title nr in vts XX table of title parts
Offset 0x08, size 1: first LB of IFO of vts XX

-> Size of VIDEO_TS/IFO, start and size of VIDEO_TS/BUP.
-> Start of VIDEO_TS/VOB, start of VIDEO_TS/BUP gives max possible size
   of VIDEO_TS/VOB, but on test I had a difference of 10 sectors.
   -> Scan menu cell adresses for highest end sector. 
-> Start of each VTS_XX_0/IFO.

Read each VTS_XX_0/IFO (VTS):
-----------------------------
Offset 0x0C, size 4: last LB of BUP, i.e. size - 1 of IFO + VOB + BUP.
Offset 0x1C, size 4: last LB of IFO, i.e. size - 1 of IFO.
Offset 0xC0, size 4: offset from start of IFO (unit = 1 LB) to VTS_XX_0/VOB.
Offset 0xC4, size 4: offset from start of IFO (unit = 1 LB) of VTS_XX_1/VOB.
Offset 0xC8, size 4: offset from start of IFO (unit = 1 LB) of table of title parts.
Offset 0xCC, size 4: offset from start of IFO (unit = 1 LB) of table of title program chains.
Offset 0xD8, size 4: offset from start of IFO (unit = 1 LB) of table of menu
                     cell adresses.
Offset 0xE0, size 4: offset from start of IFO (unit = 1 LB) of table of title
                     cell adresses.
Offset 0xE4 gives title list of LB offsets to each VOBU.

-> Size of VTS_XX_0/IFO, start and size of VTS_XX_0/BUP.
-> Start of VTS_XX_1/VOB.
   Start of VIDEO_TS/BUP gives max possible size of VTS_XX_Y/VOB,
   but on test I had a difference of 10 sectors.
   -> Scan menu cell adresses for highest end sector. 
-> Start of VTS_XX_0/VOB, start of VTS_XX_1/BUP gives max possible size.
   -> Scan title cell adresses for highest end sector. 
-> Use VOBUS to split VOBS at largest offsets such as
   (offset - start offset) <= 0x8000?

Title + Chapter:
----------------

From VMG's table of titles you get the vts nr to use, the number of chapters
in the title and the vts title nr.

VTS table of title parts:
Offset 0x00 size 2: nr of titles in vts.
Offset 0x08 size 4*nr: offsets from start of table to first chapter of title x
                       (beware indexes as first offset is for title 1)
Offset 0x08+4*nr, chapter list

Chapter list:
Offset 0x00, size 2: program chain number of first chapter (pgcn)
Offset 0x02, size 2: program number of first chapter (pgn)
Offset 0x04, size 2: program chain number of second chapter
Offset 0x06, size 2: program number of second chapter
...

Table of title program chains:
Offset 0x00, size 2: nr of proram chains
Offset 0x08, size 4*nr: offsets from start of table to program chain lookup

Program chain lookup:
Offset 0x08, size 4: offset from start of table to grogram chain

Program chain (pgc):
Offset 0x02, size 1: nr of prorams
Offset 0x03, size 1: nr of cells
Offset 0x04, size 4: BCD time
Offset 0xA4, size 4*16: 16 palette entries (0, Y, U, V) for subtitles
Offset 0xE4, size 2: offset from start of pgc to commands table
Offset 0xE6, size 2: offset from start of pgc to program map
Offset 0xE8, size 2: offset from start of pgc to cells playback table
Offset 0xEA, size 2: offset from start of pgc to cells positions

PGC program map:
1 byte * nr of programs: cell nr

PGC cells playback table:
Offset 0x04, size 4: BCD time of cell 1
Offset 0x08, size 4: start LB of first VOBU (relative to vts title VOB LB)
Offset 0x0C, size 4: last  LB of first ILVU (...)
Offset 0x10, size 4: start LB of last VOBU (...)
Offset 0x14, size 4: last  LB of last VOBU (...)
 

From VMG table of tiles -> VTS table of title parts -> (pgcn, pgn)
From VTS table of title program chains -> pgc
From PGC program map and pgn -> cell nr
From PGC cells playback table and cell nr -> sectors to play
Beware: pgcn, pgn, and cell nr numbering starts with 1

Special meanings in IFOs
------------------------

- if PGC is non sequential, the cells/programs of the PGC must be shuffled or randomised
  before being played.
- For interleaved angles the PGC contains a program for each angle and must be taken
  into account for finding the next/previous one and for determining the total duration.
- Interleaved VOBUs do not always map to a set of selectable angles, they may correspond
  to visuals modified to match the language in use (like the scrolling text that
  starts each episode of Star Wars).
- Aspect setting from the IFO tilte/menu attributes overwrites the aspect ratio
  in the MPEG stream.

Subtitles and menus
===================

IFOs may contain different subtitles for normal, wide, letterboxed or pan & scan.
Normal subtitles can be overlaid directly on the MPEG image but the wide version
seems to be overlaid on the squashed output.


Menus are stored as subtiles with a forced display command but beware that not all
forced subtitles are menus. What makes a forced subtitle a real menu is the presence
of button definitions in the PCI of the displayed VOBU.

The PCI contains button definitions which defines the rectangle within the subtitle
they occupy and the color/transparency mapping to use when the buttton is selected
or acted upon.

You would think that you only need to display the selected button with its color/transparency
mapping, but actually the rest of the subtitle should be displaued with the subtitle's own
color/transparency mapping. This makes no difference for usual cases where the subtitle uses
a completely transparent mapping, however there are reverse uses where the subtitle is used
to hide everything and transparent buttons are used to unveil a particular area (example:
an image from a given chapter or a given trailer from a list).

Preferred language
==================

There are 3 system registers controlling languages, one for the interface (menus),
one for audio and one for subtitles.

Most of the time multilingual DVDs propose a menu to select the language of the menus
but not always. In suchs cases the menu definitions in the IFOs come in multiple
languages and one matching the default language (if possible) should be selected.

The situation of menus is simple, they have either no audio or a single audio stream
and 0 or 1 forced subtitle stream for the menu definition.

For titles, the default audio stream for titles is "none" and subtitles are off.
The correct streams are usually set by commands within the pre/post commands of PGCs,
but not always (especially if there is only a single possible audio stream).
In such cases one matching the default language (if possible) should be selected.