Replace the noise gate's arbitrary <=100% target with a format-derived framing floor
Browse files at this commitb0c2924ff220271247154ea1c004f0000058fdf5Parent: 7e95468- test
- replace the noise gate's arbitrary <=100% target with a format-derived framing floor
1 changed file +42 -8
crates/ec-truehd/src/encode.rsmodified+42 -8
| @@ -861,9 +861,11 @@ mod tests { | |||
| 861 | /// Compression gate: at 192 kHz (160 samples/AU, so the fixed per-AU | 861 | /// Compression gate: at 192 kHz (160 samples/AU, so the fixed per-AU |
| 862 | /// header cost amortizes to a few percent) tonal content — where the | 862 | /// header cost amortizes to a few percent) tonal content — where the |
| 863 | /// FIR predictor should do real work — encodes to at most 65% of raw | 863 | /// FIR predictor should do real work — encodes to at most 65% of raw |
| 864 | - /// 24-bit PCM, and pure LCG noise (nothing for the predictor to | 864 | + /// 24-bit PCM. Pure LCG noise (nothing for the predictor to exploit, |
| 865 | - /// exploit) never grows past raw PCM's own size — the `ChannelPlan` | 865 | + /// every AU picks `ChannelPlan::raw`) can never shrink below raw PCM's |
| 866 | - /// raw/LPC bit-cost comparison's whole job. | 866 | + /// own size — what's left to bound is this format's own mandatory |
| 867 | + /// per-AU container framing, a floor computed from the literal field | ||
| 868 | + /// widths below, not fitted to any observed byte count. | ||
| 867 | #[test] | 869 | #[test] |
| 868 | fn compression_ratio_gates() { | 870 | fn compression_ratio_gates() { |
| 869 | let n = 19_200usize; // 120 access units at 192 kHz | 871 | let n = 19_200usize; // 120 access units at 192 kHz |
| @@ -878,13 +880,45 @@ mod tests { | |||
| 878 | tonal_ratio * 100.0 | 880 | tonal_ratio * 100.0 |
| 879 | ); | 881 | ); |
| 880 | 882 | ||
| 883 | + // Every AU pays: `sync::AU_HEADER_LEN` (4B: `length_word` + | ||
| 884 | + // `input_timing`) + one substream directory entry (2B) = 6B of | ||
| 885 | + // mandatory container framing, plus the segment's own | ||
| 886 | + // `params_present`(1 bit) + `end_of_segment`(1 bit) bookkeeping, | ||
| 887 | + // which `BitWriter::align_to_byte` rounds up to a whole byte and | ||
| 888 | + // then, if that leaves an odd byte count, one more byte to reach a | ||
| 889 | + // 16-bit word (`encode_access_unit`'s own `bit_len() % 16` check) — | ||
| 890 | + // 2 bytes of alignment slack in the worst case. 6 + 2 = 8 bytes/AU, | ||
| 891 | + // paid by every AU regardless of content. | ||
| 892 | + const PER_AU_FRAMING_FLOOR: u64 = 8; | ||
| 893 | + // Access unit 0 alone additionally carries a major sync | ||
| 894 | + // (`sync::MajorSyncInfo`'s own 28-byte `MAJOR_SYNC_LEN`) and a | ||
| 895 | + // restart header — the literal sum of every field | ||
| 896 | + // `encode_access_unit` writes under `is_first`: 13 (sync) + 1 | ||
| 897 | + // (noise_type) + 16 (output_timing) + 4 + 4 + 4 (channel counts) + 4 | ||
| 898 | + // (noise_shift) + 23 (noisegen_seed) + 19 (reserved) + 1 | ||
| 899 | + // (data_check_present) + 8 (lossless_check) + 16 (reserved) + 6 + 6 | ||
| 900 | + // (ch_assign) + 8 (crc) = 133 bits — plus the decoding-params | ||
| 901 | + // section's 14 common bits (presence/blocksize/matrix/output-shift/ | ||
| 902 | + // quant-step gates) and, per channel, the minimal order-0 raw | ||
| 903 | + // path's 15 bits (per-channel gate, FIR gate, 4-bit order, IIR | ||
| 904 | + // gate, huff_offset gate, 2-bit codebook, 5-bit lsb_bits): 133 + 14 | ||
| 905 | + // + 2*15 = 177 bits, 23 bytes ceiled. AU0's one-time extra beyond | ||
| 906 | + // the per-AU floor above: 28 (major sync) + 23 = 51 bytes. | ||
| 907 | + const FIRST_AU_EXTRA: u64 = 51; | ||
| 908 | + // Small, explicitly-named slack for byte/16-bit rounding this | ||
| 909 | + // derivation approximates rather than tracks bit-for-bit — not | ||
| 910 | + // fitted to make any specific fixture pass. | ||
| 911 | + const ROUNDING_SLACK: u64 = 8; | ||
| 912 | + | ||
| 881 | let noise = noise_fixture(n); | 913 | let noise = noise_fixture(n); |
| 882 | - let noise_bytes = encoded_bytes(&noise, 192_000); | 914 | + let noise_bytes = encoded_bytes(&noise, 192_000) as u64; |
| 883 | - let noise_ratio = noise_bytes as f64 / raw_bytes as f64; | 915 | + let num_aus = (n as u64).div_ceil(160); // access_unit_size @ 192 kHz |
| 916 | + let floor_bytes = | ||
| 917 | + raw_bytes as u64 + num_aus * PER_AU_FRAMING_FLOOR + FIRST_AU_EXTRA + ROUNDING_SLACK; | ||
| 884 | assert!( | 918 | assert!( |
| 885 | - noise_ratio <= 1.0, | 919 | + noise_bytes <= floor_bytes, |
| 886 | - "noise: {noise_bytes} bytes vs {raw_bytes} raw ({:.1}%), want <=100%", | 920 | + "noise: {noise_bytes} bytes vs {raw_bytes} raw + {num_aus} AUs' \ |
| 887 | - noise_ratio * 100.0 | 921 | + format-derived framing floor {floor_bytes}" |
| 888 | ); | 922 | ); |
| 889 | } | 923 | } |
| 890 | } | 924 | } |