Color Palettes - mjuhasz/BDSup2Sub GitHub Wiki

The subtitle data on a Blu-Ray consists of run length encoded images up to 1920x1080 pixels with a color/alpha palette of up to 256 entries (where each undefined entry and the 256th entry are completely transparent by definition). Each single subpicture frame can have a separate palette. The subpicture data on HD-DVD is similar, the main difference is that the palette for each frame has always 256 entries.
The subpicture data on a DVD consists of run length encoded images up to 720x576 (PAL) or 720x480 (NTSC) pixels, where each image can use up to 4 colors from a common palette consisting of 16 colors. This global palette is defined in the IFO files of a DVD. For SUP/IFO streams, this info is read from an IFO file was well; for subtitles in SUB/IDX format, the information is stored in the IDX file.
All bitmap based subtitle formats are able to define transparency in a way. For DVD based subtitles, each of the colors per frame can have a transparency between 0..15. The HD formats can define an alpha value between 0..255 for each palette entry. Transparency is usually used for a transparent background, but it can be also used for fading. BDSup2Sub tries to detect fading and remove it to be able to display and convert each subtitle.

Palette Modes

So to convert from SUP (BD/HD-DVD) to SUB/IDX (or SUP/IFO), not only the pictures and display offsets have to be scaled, but also the color/alpha information has to be reduced. This is the reason why only "create new" palette mode is available when converting a 256 color format like BD-SUP to a DVD format like SUB/IDX.

In "create palette" mode for SUB/IDX (or SUP/IFO) output, BDSup2Sub uses the following approach for each subpicture frame:

  1. the dominant color in the original picture is identified ((non-)transparency and luminance are used as weight factors)
  2. the dominant color is matched with the default primary colors in the global palette (white, red, green, blue, yellow, cyan, magenta)
  3. The closest match is chosen as main color. A darker tone of the same color is chosen as 2nd color. The two other colors are always black and a transparent color.
  4. the subpicture is downsized to either PAL or NTSC, depending on the setting of "Output Format" Scaling up/down is done with a selectable scaling filter. However, only transparency (alpha) and luminance are considered: if the mean value of the alpha channel is below "Alpha Threshold" , the target pixel is considered to be transparent (palette entry 0). Else it is set to either the primary color (palette entry 1), the darker primary color (palette entry) or black (palette entry 3) depending on whether the pixels's luminance is above/below "Hi/Med Threshold" or "Med/Low Threshold". Note that the luminance thresholds are automatically detected (when loading a SUP), while the alpha threshold is set to a default value.

In SUP or BDN XML export mode, three different palette creation modes can be selected when scaling SUPs: "keep existing" doesn't change the palette, but uses the nearest existing color for each scaled pixel. This leads usually to good results, but if the palette lacks alpha/brightness/color gradations, the result may be suboptimal. The mode "create new" creates a new palette for each caption and "dithered" creates a new palette and applies a Floyd-Steinberg dithering matrix. This last approach is the slowest, but usually there is no visual difference to "create new". The default setting is "create new".

In VobSub or SUP/IFO export mode, the palette creation can be only changed if the imported caption stream was a VobSub or SUP/IFO as well. Then you may choose to keep the existing (i.e. imported) palette. In any other case, the palette has to be created anyway so the option is grayed out.

##Interpreting the SUP palette

BDSup2Sub assumes that the palette entries in SUP streams (BD as well as HD-DVD) are stored as Y, Cr, Cb (three bytes in this order). I can't validate this for HD-DVDs, but for BDs, the resulting colors displayed in BDSup2Sub match exactly what my PS3 displays. I'm aware though that some other programs like SupRip and MPC-HC caption renderers seems to assume a different color order than BDSup2Sub or the PS3 (Y, Cb, Cr instead of Y, Cr, Cb). This means of course that they will display colors differently (e.g. Cyan becomes Yellow and vice versa).
While I'm pretty sure that BDSup2Sub uses the correct order, just for the case that the byte order Y, Cb, Cr is needed instead, there's an an menu item "Settings->Swap Cr/Cb" to swap the Cr and Cb components when reading a SUP. When exporting a SUP with the swap option enabled, this also means that the Cr/Cb components are swapped then compared to those in the imported SUP. So while BDSup2Sub shows the colors like e.g. SupRip for the imported SUP then, SupRip won't show the same colors for the exported SUP. Note that with the swap option disabled, the exported SUP has exactly the same palette as the imported SUP.
When exporting SUB/IDX though, the colors displayed by BDSup2Sub do matter, as the color matching is done in RGB space. Also the palette colors written to the IDX file are stored as RGB values. As long as other tools respect this definition, the colors should appear as they are displayed by BDSup2Sub.

Editing the "default" DVD palette (for "create new" palette mode)

To allow a at least a minimum of anti-aliasing during scaling down/up with SUB/IDX (or SUP/IFO) as output format, the 16 color palette used by BDSup2Sup in "create palette" mode has one light and one dark tone for each predefined color. E.g. there is a dark blue and a light blue. White is a special case, since it's the most common subtitle color: in addition to white, there is also a light and a dark gray. So for lighter subtitles, the conversion can select white and dark gray, while it can select light gray and dark gray for darker subtitles.
If you're unhappy with the default palette, you can edit the default palette used for conversion to SUB/IDX via the according menu item "Edit default DVD palette" in the "Preferences" menu. Just keep in mind that if you edit a light color, you should also change the dark color accordingly (and vice versa). Also "white, "light gray" and "dark gray" must be different tones of the same color or else you'll get erratic results. Also note that when you use VobSub or SUP/IFO as import and export format, you may as well choose to keep the existing palette. Then BDSup2Sub's default palette is completely ignored. Note that when you're scaling, this might lead to worse results than the "create new" palette mode if no intermediate color is present (in the frame palette) for antialiasing.

Editing the "imported" DVD palette

When the import format is either VobSub or SUP/IFO, the menu item ""Edit imported DVD palette" is enabled, which let's you edit the 16 color palette imported from the IDX or IFO. You can change any of the 16 colors as you wish. Just keep in mind that each caption is still limited to a 4 color frame palette which can be edited via a separate menu item ("Edit DVD Frame Palette" described in the next chapter).
Note that editing the imported palette and editing the frame palette/alpha values are the only edit features in BDSup2Sub that actually change the source image. Though this might seem a little inconsistent (in that all other edit features only influence the target subtitle), I decided that these features make more sense this way since else they would only work when converting from SUB/IDX to SUP/IFO or vice versa and using the "keep palette" mode.

Editing the Frame Palette and Alpha values (DVD input only)

As already mentioned, DVD subtitle formats (SUB/IDX or SUP/IFO) use a 4 color palette and 4 alpha values for each frame. All the values can vary from 0 to 15, where the number means the palette entry from the global 16 color palette for the palette while it defines the absolute transparency for the alpha values: 0 is completely transparent, 15 is completely opaque. Editing the frame palette/alpha is only available when the import format is either VobSub or SUP/IFO and it also influences the source image displayed in the upper half of the main screen.
Note that apart from changing the palette/alpha values for the current caption, you can also apply the same setting to all subtitles ("Set All"). This is true for restoring the default values (as defined in the original stream): you can either restore only the values for the current caption ("Reset") or for the whole stream ("Reset All").

Automatic fixing of invisible subtitles due to zero alpha frame palette (DVD input only)

In some DVD subtitle streams (SUB/IDX and SUP/IFO) single subtitles are invisible since all four alpha values are zero (due to an authoring fault). In some cases, this hides a useful subtitle, in some cases it hides a garbage bitmap. Anyway, apart from manually setting the according alpha values via the "Edit DVD Frame Palette" menu item, BDSup2Sub can also try to fix this problem automatically by keeping the last non-zero frame palette (i.e. the one from the previous subtitle). This feature can be switched on/off either via the command line (/fixinv) or with the menu item "Fix invisible frames" from the settings menu.
Note that changing this setting in the menu has no influence on the currently loaded subtitle since the alpha fixing already took place then. You need to reload the current subtitle file to see an effect.