Jump to content
Setting up an AI agent? ReverseEngineering.wiki skill (works from the command line, no configuration): https://reverseengineering.wiki/skill/rewiki/SKILL.md · setup for every client, including MCP: https://reverseengineering.wiki/llms.txt · REWiki:Connect

Format:MDEC (PlayStation macroblock decoder)

From ReverseEngineering.wiki
Format
On this wiki Facts · Code · Notes · Topics

The PlayStation's hardware image decoder: a 32-bit command word (decode macroblocks, set quantization tables, set the IDCT scale table), run-length coded 16-bit halfwords with a 6-bit quantizer scale and a 10-bit coefficient, six 8x8 blocks per colour macroblock and output depths of 4, 8, 15 or 24 bits; it decodes the BS data of STR movies.

Overview

The MDEC is the JPEG-like block decoder next to the CPU. Games feed it a halfword stream (the BS data cut out of Format:STR (PlayStation video) sectors, after the sector header has been stripped and the variable-length codes expanded by the CPU into fixed 16-bit codes) by DMA, and read back decoded macroblocks. This page lists what a reimplementation or a tool such as a frame decoder needs; the emulator source of DuckStation is the reference used.

Command word

A command is one 32-bit word written to MDEC register 0 (register 4 is control on write and status on read): bits 29-31 command (1 decode macroblock(s), 2 set quantization table, 3 set scale table; 0 and 4-7 do nothing but still consume a parameter word count), bits 27-28 output depth (0 4-bit, 1 8-bit, 2 24-bit, 3 15-bit), bit 26 signed output, bit 25 sets bit 15 for 15-bit output, bits 0-15 number of parameter words. Command 2 takes 16 words (the luminance table, 64 bytes) or 32 words when bit 0 is set (luminance and colour tables). Command 3 takes 32 words (64 halfwords, the IDCT scale matrix).

Block data

Each colour macroblock is six 8x8 blocks (NUM_BLOCKS = 6 in DuckStation). Unverified: the order is Cr, Cb, then four luminance blocks Y1-Y4, as psx-spx describes. Output depths 4 and 8 bit are monochrome and decode only one block per macroblock. Each 16-bit input code holds the quantizer scale in bits 10-15 and a signed 10-bit coefficient in bits 0-9; the value 0xFE00 is padding at the start of a block and the end-of-block marker. Coefficients are placed through the zig-zag table and multiplied by the quantization table entry and the scale; a scale of 0 stores the coefficient times 2 without the table. Unverified: the DC term of a block is treated differently from the other coefficients.

Registers

Status bits: 31 output FIFO empty, 30 input FIFO full, 29 busy, 28 data-in request, 27 data-out request, 25-26 output depth, 24 signed output, 23 bit 15, 16-18 current block, 0-15 remaining parameter words (Unverified: hardware stores the count minus one). Control: bit 31 reset, bit 30 enable DMA in, bit 29 enable DMA out.

Code & repositories

  • https://github.com/stenzek/duckstation @ 8a5b99b8b3ec82712292fea6355438eed85dd79e · license: CC-BY-NC-ND-4.0 (declared by the contributor; GitHub's detector did not recognize LICENSE)
    PS1 emulator: src/core/mdec.cpp (command word, RLE decoding, zigzag) and src/core/cdrom.cpp (XA subheader, XA-ADPCM sectors). Check the repository licence before reusing code. PS1 emulator (licence CC BY-NC-ND 4.0, so read for facts only, do not copy code): src/core/mdec.cpp (command word, RLE decoding, zigzag) and src/core/cdrom.cpp (XA subheader, XA-ADPCM sectors).
    Tested on: source read, not run: source read, not run or built · checked 2026-10-19; last activity 2026-10-18; Note: the latest release ships binaries; the wiki links only the source, never the binaries

Pitfalls

  • The scale matrix is stored transposed in DuckStation's table (index x*8+y).
  • Unverified: details of the DC-only path and the exact rounding of the fixed-point IDCT; compare with the actual source before relying on them.