Presentation Graphic Stream (SUP files) BluRay Subtitle Format

 

The Presentation Graphic Stream (PGS) specification is defined in the US Patent US 20090185789 A1. This graphic stream definition is used to show subtitles in BluRay movies. When a subtitle stream in PGS format is ripped from a BluRay disc is usually saved in a file with the SUP extension (Subtitle Presentation).

A Presentation Graphic Stream (PGS) is made of several functional segments one after another.  These segments have the following header:

NameSize in bytesDescription
Magic Number2"PG" (0x5047)
PTS4Presentation Timestamp
DTS4Decoding Timestamp
Segment Type10x14: PDS
0x15: ODS
0x16: PCS
0x17: WDS
0x80: END
Segment Size2Size of the segment

The DTS should indicate a time when the decoding of the sub picture starts, and the PTS indicates a time when the decoding ends and the sub picture is shown on the screen.  DTS is always zero in practice (at least from what I have found so far), so you can freely ignore this value. These timestamps have an accuracy of 90 kHz. This means that for example, if you have a PTS value of 0x0004C11C and you want to know the milliseconds from the start of the movie when the sub picture is shown, you have to divide the decimal value (311,580) by 90, and the result is the value you are looking for: 3,462 milliseconds (or 3.462 seconds).

As you can see, there are five types of segments used in PGS:

  • Presentation Composition Segment (PCS)
  • Window Definition Segment (WDS)
  • Palette Definition Segment (PDS)
  • Object Definition Segment (ODS)
  • End of Display Set Segment (END)

The Presentation Composition Segment (PCS) is also called the Control Segment because it indicates a new Display Set (DS) definition, composed by definition segments (WDS, PDS, ODS) until an END segment is found.

A Display Set (DS) is a sub picture definition that might look like this:

PCS WDS PDS ODS END

In a DS there can be several windows, palette and object definitions, and the composition objects define what is going to be shown on the screen.

Presentation Composition Segment

The Presentation Composition Segment is used for composing a sub picture. It is made of the following fields:

NameSize in bytesDescription
Width2Video width in pixels (ex. 0x780 = 1920)
Height2Video height in pixels (ex. 0x438 = 1080)
Frame Rate1Always 0x10. Can be ignored.
Composition Number2Number of this specific composition. It is incremented by one every time a graphics update occurs.
Composition State1Type of this composition. Allowed values are:
0x00: Normal
0x40: Acquisition Point
0x80: Epoch Start
Palette Update Flag1Indicates if this PCS describes a Palette only Display Update. Allowed values are:
0x00: False
0x80: True
Palette ID1ID of the palette to be used in the Palette only Display Update
Number of Composition Objects1Number of composition objects defined in this segment

The composition state can be one of three values:

  • Epoch Start: This defines a new display. The Epoch Start contains all functional segments needed to display a new composition on the screen.
  • Acquisition Point: This defines a display refresh. This is used to compose in the middle of the Epoch. It includes functional segments with new objects to be used in a new composition, replacing old objects with the same Object ID.
  • Normal: This defines a display update, and contains only functional segments with elements that are different from the preceding composition. It’s mostly used to stop displaying objects on the screen by defining a composition with no composition objects (a value of zero in the Number of Composition Objects flag) but also used to define a new composition with new objects and objects defined since the Epoch Start.

The composition objects, also known as window information objects, define the position on the screen of every image that will be shown. They have the following structure:

NameSize in bytesDescription
Object ID2ID of the ODS segment that defines the image to be shown
Window ID1Id of the WDS segment to which the image is allocated in the PCS. Up to two images may be assigned to one window
Object Cropped Flag10x40: Force display of the cropped image object
0x00: Off
Object Horizontal Position2X offset from the top left pixel of the image on the screen
Object Vertical Position2Y offset from the top left pixel of the image on the screen
Object Cropping Horizontal Position2X offset from the top left pixel of the cropped object in the screen. Only used when the Object Cropped Flag is set to 0x40.
Object Cropping Vertical Position2Y offset from the top left pixel of the cropped object in the screen. Only used when the Object Cropped Flag is set to 0x40.
Object Cropping Width2Width of the cropped object in the screen. Only used when the Object Cropped Flag is set to 0x40.
Object Cropping Height Position2Heightl of the cropped object in the screen. Only used when the Object Cropped Flag is set to 0x40.

When the Object Cropped Flag is set to true (or actually 0x40), then the sub picture is cropped to show only a portion of it. This is used for example when you don’t want to show the whole subtitle at first, but just a few words first, and then the rest.

Window Definition Segment

This segment is used to define the rectangular area on the screen where the sub picture will be shown. This rectangular area is called a Window. Its structure is as follows:

NameSize in bytesDescription
Window ID1ID of this window
Window Horizontal Position2X offset from the top left pixel of the window in the screen.
Window Vertical Position2Y offset from the top left pixel of the window in the screen.
Window Width2Width of the window
Window Height2Height of the window

Palette Definition Segment

This segment is used to define a palette for color conversion. It’s composed of the following fields:

NameSize in bytesDefinition
Palette ID1ID of the palette
Palette Version Number1Version of this palette within the Epoch
Palette Entry ID1Entry number of the palette
Luminance (Y)1Luminance (Y value)
Color Difference Red (Cr)1Color Difference Red (Cr value)
Color Difference Blue (Cb)1Color Difference Blue (Cb value)
Transparency (Alpha)1Transparency (Alpha value)

There can be several palette entries, with different palette IDs so the last five fields can repeat.

Object Definition Segment

This segment defines the graphics object. These are images with rendered text on a transparent background. Its structure is explained in the following table:

NameSize in bytesDescription
Object ID2ID of this object
Object Version Number1Version of this object
Last in Sequence Flag1If the image is split into a series of consecutive fragments, the last fragment has this flag set. Possible values:
0x40: Last in sequence
0x80: First in sequence
0xC0: First and last in sequence (0x40 | 0x80)
Object Data Length3The length of the Run-length Encoding (RLE) data buffer with the compressed image data.
Width2Width of the image
Height2Height of the image
Object DatavariableThis is the image data compressed using Run-length Encoding (RLE). The size of the data is defined in the Object Data Length field.

The Run-length encoding method is defined in the US 7912305 B1 patent. Here’s a quick and dirty definition to this method:

CodeMeaning
CCCCCCCCOne pixel in color C
00000000 00LLLLLLL pixels in color 0 (L between 1 and 63)
00000000 01LLLLLL LLLLLLLLL pixels in color 0 (L between 64 and 16383)
00000000 10LLLLLL CCCCCCCCL pixels in color C (L between 3 and 63)
00000000 11LLLLLL LLLLLLLL CCCCCCCCL pixels in color C (L between 64 and 16383)
00000000 00000000End of line

End Segment

The end segment always has a segment size of zero and indicates the end of a Display Set (DS) definition. It appears immediately after the last ODS in one DS.

Example

Let’s see a real world example. Take a look at this section of a SUP file:

This is a complete Display Set. These are the segments:

  • First Segment: PCS, offset 0x00348a10
    • Magic Number: “PG” (0x5047)
    • Presentation Time: 17:11.822 (92,863,980 / 90)
    • Decoding Time: 0
    • Segment Type: PCS (0x16)
    • Segment Size: 0x13 bytes
    • Width: 1920 (0x780)
    • Height: 1080 (0x438)
    • Frame rate: 0x10
    • Composition Number: 430 (0x1ae)
    • Composition State: Epoch Start (0x80)
    • Palette Update Flag: false
    • Palette ID: 0
    • Number of Composition Objects: 1
    • Object ID: 0
    • Window ID: 0
    • Object Cropped Flag: false
    • Object Horizontal Position: 773 (0x305)
    • Object Vertical Position: 108 (0x06c)
  • Second Segment: WDS (0x17), offset 0x00348a30
    • Magic Number: “PG” (0x5047)
    • Presentation Time: 17:11.822 (92,863,980 / 90)
    • Decoding Time: 0
    • Segment Type: WDS (0x17)
    • Segment Size: 0x13 bytes
    • Window ID: 0
    • Window Horizontal Position: 773 (0x305)
    • Window Vertical Position: 108 (0x06c)
    • Window Width: 377 (0x179)
    • Window Height 43 (0x02b)
  • Third Segment: PDS (0x14), offset 0x00348a50
    • Magic Number: “PG” (0x5047)
    • Presentation Time: 17:11.822 (92,863,980 / 90)
    • Decoding Time: 0
    • Segment Type: PDS (0x14)
    • Segment Size: 0x9d bytes
    • Palette ID: 0
    • Palette Version: 0
    • 31 palette entries
  • Fourth Segment: ODS (0x15), offset 0x00348afa
    • Magic Number: “PG” (0x5047)
    • Presentation Time: 17:11.822 (92,863,980 / 90)
    • Decoding Time: 0
    • Segment Type: PDS (0x15)
    • Segment Size: 0x21c2 bytes
    • Object ID: 0
    • Object Version Number: 0
    • Last in sequence flag: First and last sequence (0xC0)
    • Object Data Length: 0x0021bb bytes
    • Width: 377 (0x179)
    • Height: 43 (0x02b)
  • Fifth Segment: END (0x80), offset 0x0034acc9
    • Magic Number: “PG” (0x5047)
    • Presentation Time: 17:11.822 (92,863,980 / 90)
    • Decoding Time: 0
    • Segment Type: END (0x80)
    • Segment Size: 0 bytes

This Display Set will show an image of 377×43 in size, starting at 17 minutes, 11.822 seconds on the screen at offset 773×108.

Leave a Reply