Namespace CHDSharp.Encoder

Classes

AvHuffCodec

MAME A/V Huffman codec ('avhu'), matching chd_avhuff_compressor: each hunk is one raw 'chav' A/V frame (assembled by EncodeLaserDisc(string, string, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, long, long?, CancellationToken)) compressed as delta-RLE Huffman video + per-channel mono FLAC audio via CHDSharp.Encoder.AvHuffEncoder. Multi-frame hunks (hunkBytes > bytesPerFrame) are stored raw, matching MAME's codec-chain behavior where the avhuff compress fails on already-encoded data. Decodable by CHDSharpLib's ChdReaders.AvHuff and chdman.

AviReader

Minimal AVI container reader for laserdisc CHD creation (chdman's avi_file read path, src/lib/util/aviio.cpp). Parses RIFF/AVIX headers, stream descriptions ('strh'/ 'strf') and frame indexes ('idx1', or a sequential 'movi' scan when no index exists), and serves YUY-family video frames and PCM sound samples. Supported video: uncompressed YUY2/VYUY (bytes pass through unchanged) and UYVY (byte-swapped per pixel pair to YUY2 order), matching MAME's yuv_decompress_to_yuy16 output stored via put_u16be into the CHD video bitstream. On entry the encoder expects YUY2 byte order [Y0,Cb,Y1,Cr] per pixel pair so that Y occupies even byte offsets and Cb/Cr occupy the interleaved odd offsets. Supported audio: uncompressed PCM, 8 or 16 bits per sample.

AviReader.MovieInfo

Movie description (MAME's avi_file::movie_info subset).

CdCompoundCodec

Shared implementation for the CD compound codecs ('cdzl', 'cdlz', 'cdzs'), matching MAME's chd_cd_compressor<BaseCompressor, SubcodeCompressor>: frames are deinterleaved into a data portion (frames × 2352) and a subcode portion (frames × 96); sectors with a valid sync header and ECC get their sync header + ECC cleared and a bitmap bit set (the decompressor regenerates them). The result is [ecc bitmap][2/3-byte base length][base compressed][subcode compressed].

CdImageParser

Dispatches CD image descriptor parsing by file extension, mirroring MAME's cdrom_file::parse_toc: .cue, .gdi, .iso/.cdr/.toast, .toc, and a cdrdao-style fallback for unknown extensions.

CdflCodec

CD FLAC codec ('cdfl'), matching MAME's chd_cd_flac_compressor: the CD audio portion (frames × 2352 bytes) is encoded as raw FLAC frames (no stream header, little-endian samples, 2ch/16-bit/44100 Hz, 2352 samples per frame — MAME's cdfl blocksize) and the subcode portion (frames × 96 bytes) is deflated and appended. The result is [FLAC frames][deflated subcode], decodable by MAME/chdman and CHDSharpLib.

CdlzCodec

CD LZMA codec ('cdlz'): LZMA for sector data, deflate for subcode (MAME parity).

CdzlCodec

CD zlib codec ('cdzl'): deflate for sector data and subcode.

CdzsCodec

CD Zstandard codec ('cdzs'): zstd for sector data and subcode.

ChdCodecs

Creates codec instances from four-character tags.

ChdEncoder

Creates CHD v5 files from raw binary data ( EncodeRaw(Stream, string, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken) ), from CD CUE/BIN sources ( EncodeCd(string, string, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken) ), or by re-compressing an existing CHD ( Copy(string, string, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken) ). Uses the zlib codec by default, matching chdman's --compression zlib output; produced files pass chdman verify and extract byte-identically via chdman extractraw.

CodecTags

Defines CHD v5 codec tag constants and conversion utilities.

CueParser

Parses CUE sheets (CUE/BIN, CUE/ISO, CUE/WAV) into a CdToc. The parsing logic mirrors MAME's cdrom_file::parse_cue (src/lib/util/cdrom.cpp), including track length and file offset resolution.

FlacCodec

Raw FLAC codec ('flac'), matching MAME's chd_flac_compressor: the hunk is treated as interleaved 2-channel 16-bit 44100 Hz samples and encoded twice (as little-endian and big-endian samples); the smaller result wins and a leading marker byte ('L'/'B') records the stored endianness. The block size follows MAME's formula (hunk samples halved until ≤ 2048). Decodable by CHDSharpLib's ChdReaders.Flac and chdman.

GdiParser

Parses a Sega GD-ROM GDI descriptor into a table of contents, matching MAME's cdrom_file::parse_gdi. Track lines are <track> <lba> <type> <sector size> "<file>" <offset>; gaps between track LBAs become zero-filled pad frames at the end of the previous track.

HuffCodec

MAME generic Huffman codec ('huff'), matching chd_huffman_compressor / huffman_8bit_encoder: the hunk is histogrammed, a canonical tree with codes of at most 16 bits is built (weight-scaled), the tree is exported Huffman-encoded via a 24-symbol/6-bit small tree, and the data follows. Decodable by CHDSharpLib's ChdReaders.Huffman and chdman.

IsoParser

Parses a raw ISO image into a single-track table of contents, matching MAME's cdrom_file::parse_iso: MODE1/2048 when the size is divisible by 2048, MODE2/2336, or MODE2_RAW/2352.

LzmaCodec

LZMA compression matching MAME's chd_lzma_compressor: raw headerless LZMA with no end marker, properties lc=3/lp=0/pb=2, dictionary size = hunk bytes and numFastBytes=64 (the LZMA "level 8" profile that chdman configures via LzmaEncProps; the match-finder cycle count 16 + fb/2 = 48 follows automatically). Backed by Igor Pavlov's official LZMA SDK C# encoder (public domain, ported into this project); the encoder never writes the 5-byte property header, so the stream is already in CHD's raw format; the decoder synthesizes the properties (see CHDSharpLib's CHDReaders.Lzma). The port matches the C encoder's byte emission: the price table uses the C's kNumMoveReducingBits/kNumBitPriceShiftBits = 4/4 (the SDK C# line still uses 2/6, which flips near-tie optimal-parser decisions) and the BT4 match finder walks the btree with maxLen = 3 like MAME's Bt4_MatchFinder_GetMatches. Output is byte-identical to chdman on the battle-test corpus (verified per-hunk on 1,664 raw hunks and the cdlz CD hunks).

MapCompressor

Compresses a CHD v5 hunk map using RLE and Huffman encoding.

MetadataWriter

Writes CHD metadata entries (linked list at the end of the file, before the map). The on-disk format mirrors MAME's chd_file::write_metadata (src/lib/util/chd.cpp): each entry is a 16-byte header (tag, flags, 24-bit length, 64-bit next) followed by the payload. The first entry's file offset is stored in the CHD header's metaoffset field.

NrgParser

Parses Nero .NRG disc images (DAO/CUE layout) into a CdToc (CHDlite cdrom_file::parse_nero parity, cdrom.cpp:1839-2004). The image is a chain of big-endian chunks; the DAOX chunk holds the track table. Supported sector modes: 2048-byte Mode 1, 2352-byte Mode 2 raw, and 2352-byte audio (byte-swapped for CHD storage). Pregaps whose sectors are not physically stored are zero-filled when encoded.

ParentMap

Builds the parent-hunk hash map used to create differential (delta) CHDs. The parent CHD is decompressed once, and every unit-aligned window of its data (one hunk's worth of bytes starting at each unit boundary) is hashed; during encoding, a child hunk whose full-hunk (CRC-16, SHA-1) matches a window is emitted as a CompressionParent reference to that parent unit — mirroring MAME's chd_file_compressor parent walk (chd.cpp: hashes every parent unit window, then matches child hunks against it).

PlatformDetector

Detects the game platform of a raw disc image (CHDlite detect_input parity) and picks smart per-platform codec presets (CHDlite smart_compression_for, chd_archiver.cpp:124-165). Works on raw .bin/.iso/.img files and on CUE/GDI/NRG descriptors before archiving.

RawDeflate

Provides raw DEFLATE compression and decompression utilities.

Sha1

Computes SHA-1 (160-bit) hash digests, backed by the platform's native SHA1 implementation (the digest is identical to any conforming SHA-1 implementation).

TocParser

Parses a cdrdao-style .toc descriptor into a table of contents, matching MAME's fallback TOC parser in cdrom_file::parse_toc. Supports TRACK, DATAFILE/ AUDIOFILE/FILE (with SWAP, #decimal and MSF offsets/lengths), START, and the COPY/PRE_EMPHASIS/CHANNEL control lines.

VbiParse

Port of MAME's vbiparse (src/lib/util/vbiparse.cpp): parses Philips codes and the "white flag" from laserdisc VBI lines, and packs the per-frame results into the 16-byte form stored in 'AVLD' metadata. Input is a YUY2 frame in serialized byte order; each 16-bit pixel is read little-endian and shifted right by sourceShift (= 8 selects the luma byte), exactly like MAME reading its native bitmap.

ZlibCodec

Zlib compression (raw DEFLATE), matching chdman -c zlib.

ZstdCodec

Zstandard compression at zstd's maximum level, matching MAME's chd_zstd_compressor (ZSTD_maxCLevel()).

Structs

MetadataWriter.ChsGeometry

Guessed CHS geometry for a hard disk image (cylinders / heads / sectors).