Class 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.
public static class ChdEncoder
- Inheritance
-
ChdEncoder
- Inherited Members
Remarks
Encoding runs a producer→worker→consumer pipeline (CompressAll(uint, Func<uint, byte[], int>, Sha1, Action<HunkResult>, CancellationToken)):
hunks are read and hashed on one thread, compressed in parallel by TaskCount workers
(each with private, persistent codec instances), and written back strictly in hunk order by a
single consumer. The output is byte-identical to a single-threaded encode regardless of the
worker count, because codec outputs are deterministic and dedup/offset assignment stays
sequential. -c none (uncompressed CHD) uses a dedicated sequential path that writes the
V5 raw map (4-byte hunk-index entries, chdman-parity layout).
Methods
Copy(string, string, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
Re-compresses an existing CHD file into a new CHD (chdman copy / CHDlite
ChdArchiver::copy parity): every hunk of the source is read (through its parent
when the source is a child) and re-encoded with the target codec list. All metadata
entries of the source are cloned into the output. The output uses the source's hunk and
unit sizes. Runs through the same parallel producer→worker→consumer pipeline as
EncodeRaw(Stream, string, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
,
so output is byte-identical regardless of the worker count.
public static void Copy(string sourcePath, string chdPath, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, CancellationToken cancellationToken = default)
Parameters
sourcePathstringPath of the source CHD file (V1-V5, standalone or child).
chdPathstringPath of the output .chd file (created/overwritten).
codecTagsIReadOnlyList<uint>The codec tags for the output, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).
optionsChdEncodeOptionsOptional encoding configuration. SourceParentPath supplies the parent of a child source; ParentPath creates the output as a delta child of a different parent (chdman
-op).cancellationTokenCancellationTokenCancels the copy; OperationCanceledException is thrown when cancellation is requested.
Remarks
Legacy CD/GD-ROM metadata tags (CHCD, CHTR, CHGT) are automatically
upgraded to their modern equivalents (CHT2, CHGD) during the copy, matching
MAME chdman's copy command behavior. For legacy GD-ROMs (CHGT), CDDA audio
tracks are byte-swapped from little-endian to big-endian during the copy. Set
NoMetadataUpgrade to true to preserve legacy tags.
Exceptions
- IOException
The source (or its parent) cannot be opened.
CreateBlank(string, ulong, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
Creates a blank, zero-filled CHD v5 file without reading from an input stream.
Equivalent to chdman createhd --size. All hunks are written as zero-filled
data, and the file is verifiable by chdman verify.
public static void CreateBlank(string chdPath, ulong totalBytes, uint hunkBytes = 4096, uint unitBytes = 512, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, CancellationToken cancellationToken = default)
Parameters
chdPathstringPath of the output .chd file (created/overwritten).
totalBytesulongTotal size of the blank disk in bytes.
hunkBytesuintHunk size in bytes (default 4096).
unitBytesuintUnit size in bytes (default 512).
codecTagsIReadOnlyList<uint>The codec tags to use (default zlib; None for uncompressed).
optionsChdEncodeOptionsOptional encoding configuration (see ChdEncodeOptions).
cancellationTokenCancellationTokenCancels the encode; OperationCanceledException is thrown when cancellation is requested.
Exceptions
- ArgumentException
totalBytesis zero, orhunkBytesis not a multiple ofunitBytes.
CreateBlankWithChs(string, uint, uint, uint, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
Creates a blank, zero-filled CHD v5 file with explicit CHS geometry metadata.
Equivalent to chdman createhd --size --chs.
public static void CreateBlankWithChs(string chdPath, uint cylinders, uint heads, uint sectors, uint sectorSize = 512, uint hunkBytes = 4096, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, CancellationToken cancellationToken = default)
Parameters
chdPathstringPath of the output .chd file (created/overwritten).
cylindersuintNumber of cylinders.
headsuintNumber of heads.
sectorsuintSectors per track.
sectorSizeuintBytes per sector (default 512).
hunkBytesuintHunk size in bytes (default 4096).
codecTagsIReadOnlyList<uint>The codec tags to use (default zlib).
optionsChdEncodeOptionsOptional encoding configuration (see ChdEncodeOptions).
cancellationTokenCancellationTokenCancels the encode.
EncodeCd(string, string, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
Encodes a CD image from a CUE sheet into a compressed CHD v5 file. Tracks are padded to 4-frame boundaries, audio sectors are byte-swapped to big-endian (as on the physical disc), and one CHT2 metadata entry is written per track.
public static void EncodeCd(string cuePath, string chdPath, uint hunkBytes = 19584, uint unitBytes = 2448, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, CancellationToken cancellationToken = default)
Parameters
cuePathstringPath of the .cue file; referenced BIN/WAV files are resolved relative to it.
chdPathstringPath of the output .chd file (created/overwritten).
hunkBytesuintHunk size in bytes (default 19584 = 8 CD frames).
unitBytesuintUnit size in bytes (default 2448 = CD frame with subcode).
codecTagsIReadOnlyList<uint>The codec tags to use, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).
optionsChdEncodeOptionsOptional encoding configuration (see ChdEncodeOptions).
cancellationTokenCancellationTokenCancels the encode; OperationCanceledException is thrown when cancellation is requested.
Exceptions
- ArgumentException
unitBytesis not the CD frame size, orhunkBytesis not a multiple of it.- FileNotFoundException
The CUE file or a referenced data file does not exist.
- InvalidDataException
The CUE sheet is malformed or contains no tracks.
EncodeLaserDisc(string, string, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, long, long?, CancellationToken)
Encodes a laserdisc CHD from an AVI file (chdman createld parity). Each output
hunk holds one or more whole video frames; every frame is assembled into MAME's raw
'chav' layout and compressed with the 'avhu' codec (delta-RLE Huffman video + per-channel
mono 48 kHz FLAC audio). The file carries 'AVAV' A/V metadata, plus 'AVLD' VBI metadata
(16 packed bytes per frame) when the field height is 262 or 312 — appended after the
map exactly like chdman.
public static LaserDiscEncodingInfo EncodeLaserDisc(string aviPath, string chdPath, uint hunkBytes = 0, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, long inputStartFrame = 0, long? inputLengthFrames = null, CancellationToken cancellationToken = default)
Parameters
aviPathstringPath of the source AVI file (YUY2/VYUY/UYVY video + PCM audio).
chdPathstringPath of the output .chd file (created/overwritten).
hunkBytesuintHunk size in bytes; must be a whole multiple of the frame size. Default 0 = one frame per hunk (chdman's default).
codecTagsIReadOnlyList<uint>The codec tags to use, tried per hunk in order (default Avhu; chdman rejects uncompressed for laserdiscs).
optionsChdEncodeOptionsOptional encoding configuration (see ChdEncodeOptions).
inputStartFramelongFirst input frame to encode (chdman
-isf, in whole frames of the source; doubled with the field range for interlaced sources).inputLengthFrameslong?Number of input frames to encode (chdman
-if);nullencodes through the last frame.cancellationTokenCancellationTokenCancels the encode; OperationCanceledException is thrown when cancellation is requested.
Returns
Exceptions
- InvalidDataException
The AVI has no usable video stream, an unsupported format, or an out-of-range frame selection.
- NotSupportedException
The AVI video format is not YUY2/VYUY/UYVY.
- ArgumentException
hunkBytesis not a multiple of the frame size.
EncodeRaw(Stream, string, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
Encodes a raw binary stream into a compressed CHD v5 file. The last hunk is
zero-padded in the file when the source size is not a multiple of
hunkBytes; the stored raw SHA-1 covers only the actual source
bytes, so chdman verify succeeds for any input size.
public static void EncodeRaw(Stream sourceStream, string chdPath, uint hunkBytes = 4096, uint unitBytes = 512, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, CancellationToken cancellationToken = default)
Parameters
sourceStreamStreamThe raw source data; the full stream is consumed from its start.
chdPathstringPath of the output .chd file (created/overwritten).
hunkBytesuintHunk size in bytes (default 4096).
unitBytesuintUnit size in bytes (default 512; 2048 when AutoClassify detects an ISO-9660 DVD image).
codecTagsIReadOnlyList<uint>The codec tags to use, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).
optionsChdEncodeOptionsOptional encoding configuration (see ChdEncodeOptions).
cancellationTokenCancellationTokenCancels the encode; OperationCanceledException is thrown when cancellation is requested.
Exceptions
- ArgumentException
hunkBytesis not a multiple ofunitBytes.
EncodeRaw(string, string, uint, uint, IReadOnlyList<uint>?, ChdEncodeOptions?, CancellationToken)
Encodes a raw binary file into a compressed CHD v5 file.
public static void EncodeRaw(string sourcePath, string chdPath, uint hunkBytes = 4096, uint unitBytes = 512, IReadOnlyList<uint>? codecTags = null, ChdEncodeOptions? options = null, CancellationToken cancellationToken = default)
Parameters
sourcePathstringPath of the raw input file.
chdPathstringPath of the output .chd file (created/overwritten).
hunkBytesuintHunk size in bytes (default 4096).
unitBytesuintUnit size in bytes (default 512).
codecTagsIReadOnlyList<uint>The codec tags to use, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).
optionsChdEncodeOptionsOptional encoding configuration (see ChdEncodeOptions).
cancellationTokenCancellationTokenCancels the encode; OperationCanceledException is thrown when cancellation is requested.
Exceptions
- ArgumentException
hunkBytesis not a multiple ofunitBytes.
ExtractLaserDisc(string, string, long, long?, CancellationToken)
Extracts a laserdisc CHD back to an AVI file (chdman extractld parity). Reads
each AVHuff hunk, parses the raw 'chav' layout, byte-swaps audio from big-endian
planar to little-endian interleaved, and writes YUY2 video frames + PCM audio to
a standard AVI file.
public static void ExtractLaserDisc(string chdPath, string aviPath, long startFrame = 0, long? lengthFrames = null, CancellationToken cancellationToken = default)
Parameters
chdPathstringPath to the input laserdisc CHD file.
aviPathstringPath for the output AVI file (created/overwritten).
startFramelongFirst frame to extract (0-based).
lengthFrameslong?Number of frames to extract;
nullextracts all.cancellationTokenCancellationTokenCancels the extraction.
ExtractLaserDisc(string, string, string?, long, long?, CancellationToken)
Extracts a laserdisc CHD back to an AVI file, optionally resolving a parent CHD for differential (child) images.
public static void ExtractLaserDisc(string chdPath, string aviPath, string? parentPath, long startFrame = 0, long? lengthFrames = null, CancellationToken cancellationToken = default)
Parameters
chdPathstringPath to the input laserdisc CHD file.
aviPathstringPath for the output AVI file (created/overwritten).
parentPathstringOptional path to the parent CHD. Pass
nullfor standalone CHDs.startFramelongFirst frame to extract (0-based).
lengthFrameslong?Number of frames to extract;
nullextracts all.cancellationTokenCancellationTokenCancels the extraction.