/** * Huffman coding on the encode side. * * The mirror of `huffman.ts`. Two jobs, and the second matters as much as the * first: writing the codewords, and *costing* them without writing anything. * * The quantiser's inner loop works by repeatedly asking "how many bits would * this granule take at this step size?" and adjusting until the answer fits the * frame. That question gets asked thousands of times per second of audio, so * {@link pairCost} and {@link regionCost} exist to answer it by table lookup * rather than by encoding into a throwaway buffer. */ import type { BitWriter } from '../io/bits.js'; /** * Bits needed to code one pair with `table`, or `Infinity` if it cannot. * * Includes the escape bits and the one sign bit each non-zero value carries. */ export declare function pairCost(table: number, x: number, y: number): number; /** Writes one pair: codeword, then any escape bits and sign bits. */ export declare function writePair(writer: BitWriter, table: number, x: number, y: number): void; /** * Largest magnitude each table can represent. * * Derived from the table dimensions rather than listed: a table without linbits * tops out at `xlen - 1`, and one with linbits escapes at 15 and adds up to * `2^linbits - 1` on top. This is what lets {@link chooseTable} skip candidates * without costing them, and it is also the ceiling the quantiser's gain search * has to respect — nothing above `TABLE_MAX[23]` is expressible at all. */ export declare const TABLE_MAX: Int32Array; /** The largest quantised magnitude any Layer III table can code. */ export declare const MAX_QUANT: number; /** * Total bits to code `values[start..end)` as pairs with `table`. * * `Infinity` when any pair is out of the table's range, which is what lets * {@link chooseTable} reject candidates cheaply. * * `ceiling` abandons the count as soon as it is exceeded. The caller is * searching for a minimum and does not care how much worse a losing candidate * is, and this runs inside the quantiser's inner loop. */ export declare function regionCost(values: Int32Array, start: number, end: number, table: number, ceiling?: number): number; /** Largest magnitude in `values[start..end)`. */ export declare function regionMax(values: Int32Array, start: number, end: number): number; /** * Picks the cheapest table for a region. * * Tables 4 and 14 are not used by the format — the specification leaves those * slots empty — so they are skipped rather than costed. Candidates that cannot * reach the region's largest magnitude are skipped too, and the escape tables * (16–31) are only considered when something actually needs escaping: their * whole point is coding values above 15, and below that they lose to the plain * tables on every real signal. */ export declare function chooseTable(values: Int32Array, start: number, end: number): number; /** * As {@link chooseTable}, but also returns what the winning table costs. * * The rate control needs both, and getting them from one pass rather than two * matters: this runs once per region per step-size trial. */ export declare function chooseTableAndCost(values: Int32Array, start: number, end: number): { table: number; bits: number; }; /** Bits to code one count1 quadruple with the selected table. */ export declare function count1Cost(tableSelect: number, v: number, w: number, x: number, y: number): number; /** * Writes one count1 quadruple. * * Values must already be limited to -1, 0 or +1 — the count1 region exists * precisely because the high-frequency tail quantises to nothing else, and a * larger value here means the quantiser placed the region boundary wrongly. */ export declare function writeCount1(writer: BitWriter, tableSelect: number, v: number, w: number, x: number, y: number): void; /** Whichever count1 table codes `values[start..end)` in fewer bits. */ export declare function chooseCount1Table(values: Int32Array, start: number, end: number): { table: number; bits: number; };