THP (File Format)

From Wexos's Wiki
Revision as of 18:05, 1 May 2024 by Wexos (talk | contribs) (Imported from avsys wiki)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

THP files are video files used in many Wii and GameCube games.

File Format

Header

The file starts with a header.

Offset Type Description
0x00 Char[4] File magic. Always THP\0 in ASCII (0x54485000).
0x04 UInt32 Version number. Mario Kart Wii uses 0x11000.
0x08 UInt32 Max buffer size. Maximum buffer size needed for one frame, which includes header, video and audio data.
0x0C UInt32 Max audio samples. Maximum number of samples used in one frame.
0x10 Float Frames per second. Frame rates between 1.0 and 59.94 are supported.
0x14 UInt32 Number of frames in this file.
0x18 UInt32 Length of the first frame in bytes.
0x1C UInt32 Length of all frames in bytes.
0x20 UInt32 Component Container offset.
0x24 UInt32 Frame table offset. Only used if a THP file is intended to not start on the first frame.
0x28 UInt32 First frame offset.
0x2C UInt32 Last frame offset.

Component Container

The component container specifies what component the file contains for each frame. Following this structure, comes info structures which specifies data for each component. It has the following structure:

Offset Type Description
0x00 UInt32 Number of used components.
0x04 SByte[16] Component types.
Value Description
0x00 Video
0x01 Audio
0xFF Empty

Video Info

The video info structure specifies info about the video.

Offset Type Description
0x00 UInt32 Width of the video in pixels.
0x04 UInt32 Height of the video in pixels.
If version ≥ 0x11000
0x08 UInt32 Video format.
Value Description
0x00 Non-interlaced
0x01 Odd-interlaced
0x02 Even-interlaced

Audio Info

The audio info structure specifies info about the audio.

Offset Type Description
0x00 UInt32 Number of audio channels.
0x04 UInt32 Sample rate in hertz.
0x08 UInt32 Number of samples.
If version ≥ 0x11000
0x0C UInt32 Number of tracks. Allows for more than 2 audio channels in a THP file.

Frame

A frame consist of a frame header, video data and optionally audio data, defined by the component container. The size of a frame must be a multiple of 32. A frame starts with the following structure:

Offset Type Description
0x00 UInt32 Next frame size. For the last frame this value is set to first frame size.
0x04 UInt32 Previous frame size. For the first frame this value is set to last frame size.

After the data for each component, which is specified in the component container, is stored. First an UInt32 is stored for each component, which is the size of the component data. The data is padded by 4. Afterwards the actual data is stored.

Video Data

The video data structure contains the actual still image. The size of the image is specified above, but since the video data is padded to be a multiple of 4, it may contain padding. The video data will always end with FF D9 (EndOfImage).

The image data is basically a JPEG. An JPEG consist of different markers, which always start with a FF byte, followed by a non-zero byte. Following the 2-byte marker ID is some data, which differs between marker. Some markers will store no data, some will store a 2-byte length, which specifies the length of the data which follows (including the length bytes), and some markers store other data. JPEG data will always start with a StartOfImage (SOI) marker and end with an EndOfImage (EOI) marker. It will also contain a StartOfScan (SOS) marker. The JPEG specification says that if the value FF needs to be stored, it needs to be stored as FF 00. This is not to confuse the byte with a marker. However, in Nintendo's implementation, this is not the case within the StartOfScan marker's data. In order for other tools to decode the JPEG, all FF needs to be replaced with FF 00 in the StartOfScan's data.

Offset Type Description
0x00 Byte[S] Image data, where S is the non-padded size of the image.

Audio Data

The audio data structure contains the actual audio data. The audio data is stored as DSP ADPCM data.

If version ≥ 0x11000, multiple audio datas are allowed to be stored. This allows for more than 2 channels to be stored in a file. The number of audio data structures are defined by number of tracks. The size of the audio data structure stored in the frame header stores the size of one and not the total size.

Offset Type Description
0x00 UInt32 Channel size in bytes. Specifies the size of each audio channel.
0x04 UInt32 Number of samples.
0x08 Int16[8][2] Channel 1 coefficients.
0x28 Int16[8][2] Channel 2 coefficients.
0x48 Int16 Channel 1 yn 1.
0x4A Int16 Channel 1 yn 2.
0x4C Int16 Channel 2 yn 1.
0x4E Int16 Channel 2 yn 2.

The raw audio data follows.

Offset Type Description
0x00 Byte[N][S] Audio channel data, where N is the number of channels and S is the size of each channel.

Tools

The following tools can handle THP files: