Class ChdEncoder

Namespace
CHDSharp.Encoder
Assembly
CHDSharpLib.dll

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

sourcePath string

Path of the source CHD file (V1-V5, standalone or child).

chdPath string

Path of the output .chd file (created/overwritten).

codecTags IReadOnlyList<uint>

The codec tags for the output, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).

options ChdEncodeOptions

Optional encoding configuration. SourceParentPath supplies the parent of a child source; ParentPath creates the output as a delta child of a different parent (chdman -op).

cancellationToken CancellationToken

Cancels 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

chdPath string

Path of the output .chd file (created/overwritten).

totalBytes ulong

Total size of the blank disk in bytes.

hunkBytes uint

Hunk size in bytes (default 4096).

unitBytes uint

Unit size in bytes (default 512).

codecTags IReadOnlyList<uint>

The codec tags to use (default zlib; None for uncompressed).

options ChdEncodeOptions

Optional encoding configuration (see ChdEncodeOptions).

cancellationToken CancellationToken

Cancels the encode; OperationCanceledException is thrown when cancellation is requested.

Exceptions

ArgumentException

totalBytes is zero, or hunkBytes is not a multiple of unitBytes.

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

chdPath string

Path of the output .chd file (created/overwritten).

cylinders uint

Number of cylinders.

heads uint

Number of heads.

sectors uint

Sectors per track.

sectorSize uint

Bytes per sector (default 512).

hunkBytes uint

Hunk size in bytes (default 4096).

codecTags IReadOnlyList<uint>

The codec tags to use (default zlib).

options ChdEncodeOptions

Optional encoding configuration (see ChdEncodeOptions).

cancellationToken CancellationToken

Cancels 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

cuePath string

Path of the .cue file; referenced BIN/WAV files are resolved relative to it.

chdPath string

Path of the output .chd file (created/overwritten).

hunkBytes uint

Hunk size in bytes (default 19584 = 8 CD frames).

unitBytes uint

Unit size in bytes (default 2448 = CD frame with subcode).

codecTags IReadOnlyList<uint>

The codec tags to use, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).

options ChdEncodeOptions

Optional encoding configuration (see ChdEncodeOptions).

cancellationToken CancellationToken

Cancels the encode; OperationCanceledException is thrown when cancellation is requested.

Exceptions

ArgumentException

unitBytes is not the CD frame size, or hunkBytes is 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

aviPath string

Path of the source AVI file (YUY2/VYUY/UYVY video + PCM audio).

chdPath string

Path of the output .chd file (created/overwritten).

hunkBytes uint

Hunk size in bytes; must be a whole multiple of the frame size. Default 0 = one frame per hunk (chdman's default).

codecTags IReadOnlyList<uint>

The codec tags to use, tried per hunk in order (default Avhu; chdman rejects uncompressed for laserdiscs).

options ChdEncodeOptions

Optional encoding configuration (see ChdEncodeOptions).

inputStartFrame long

First input frame to encode (chdman -isf, in whole frames of the source; doubled with the field range for interlaced sources).

inputLengthFrames long?

Number of input frames to encode (chdman -if); null encodes through the last frame.

cancellationToken CancellationToken

Cancels the encode; OperationCanceledException is thrown when cancellation is requested.

Returns

LaserDiscEncodingInfo

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

hunkBytes is 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

sourceStream Stream

The raw source data; the full stream is consumed from its start.

chdPath string

Path of the output .chd file (created/overwritten).

hunkBytes uint

Hunk size in bytes (default 4096).

unitBytes uint

Unit size in bytes (default 512; 2048 when AutoClassify detects an ISO-9660 DVD image).

codecTags IReadOnlyList<uint>

The codec tags to use, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).

options ChdEncodeOptions

Optional encoding configuration (see ChdEncodeOptions).

cancellationToken CancellationToken

Cancels the encode; OperationCanceledException is thrown when cancellation is requested.

Exceptions

ArgumentException

hunkBytes is not a multiple of unitBytes.

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

sourcePath string

Path of the raw input file.

chdPath string

Path of the output .chd file (created/overwritten).

hunkBytes uint

Hunk size in bytes (default 4096).

unitBytes uint

Unit size in bytes (default 512).

codecTags IReadOnlyList<uint>

The codec tags to use, tried per hunk in order (default zlib; the single tag None produces an uncompressed CHD).

options ChdEncodeOptions

Optional encoding configuration (see ChdEncodeOptions).

cancellationToken CancellationToken

Cancels the encode; OperationCanceledException is thrown when cancellation is requested.

Exceptions

ArgumentException

hunkBytes is not a multiple of unitBytes.

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

chdPath string

Path to the input laserdisc CHD file.

aviPath string

Path for the output AVI file (created/overwritten).

startFrame long

First frame to extract (0-based).

lengthFrames long?

Number of frames to extract; null extracts all.

cancellationToken CancellationToken

Cancels 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

chdPath string

Path to the input laserdisc CHD file.

aviPath string

Path for the output AVI file (created/overwritten).

parentPath string

Optional path to the parent CHD. Pass null for standalone CHDs.

startFrame long

First frame to extract (0-based).

lengthFrames long?

Number of frames to extract; null extracts all.

cancellationToken CancellationToken

Cancels the extraction.