From 86f03f95659fe299a5ea5d59ce079d94c0019ac8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Wed, 19 Aug 2026 16:59:19 +0300 Subject: [PATCH] feat(ec-aac): close the eight envelope SBR Huffman codebook values The values-stage classifier misread the reference decoder's unconditional "channel element 0.1 is not allocated" complaint (fires on every mono sce probe regardless of range) as a crash signal, hiding in-range codewords as gaps; the discriminator is now whether the parser's byte count matches the full predicted frame length. ENV30_F/ENVB30_F needed their own two-envelope probe config since a lone envelope always forces the 1.5 dB resolution. All twelve SBR codebooks now close with Kraft sum 1 and full values. --- crates/ec-aac/src/sbr_tables.rs | 596 +++++++++++++++++++++++++++++++- scripts/aac-tables/sbrtables.py | 29 +- 2 files changed, 617 insertions(+), 8 deletions(-) diff --git a/crates/ec-aac/src/sbr_tables.rs b/crates/ec-aac/src/sbr_tables.rs index cd7142e2..3f13ea10 100644 --- a/crates/ec-aac/src/sbr_tables.rs +++ b/crates/ec-aac/src/sbr_tables.rs @@ -9,7 +9,389 @@ //! a Kraft sum of exactly 1 (asserted below). #![allow(dead_code)] -// Not emitted (values incomplete or book did not close): ['ENV15_F', 'ENV15_T', 'ENV30_T', 'ENV30_F', 'ENVB15_F', 'ENVB15_T', 'ENVB30_T', 'ENVB30_F'] +/// SBR Huffman book `ENV15_F`: (length, code, delta) by codeword. +pub(crate) static ENV15_F: [(u8, u32, i32); 121] = [ + (2, 0, 0), + (2, 1, -1), + (3, 4, 1), + (3, 5, -2), + (4, 12, -3), + (4, 13, 2), + (5, 28, -4), + (5, 29, 3), + (6, 60, -5), + (6, 61, 4), + (7, 124, -6), + (8, 250, 5), + (8, 251, -7), + (8, 252, 6), + (9, 506, -8), + (9, 507, 7), + (9, 508, -9), + (10, 1018, 8), + (10, 1019, -10), + (11, 2040, 9), + (11, 2041, -11), + (11, 2042, 10), + (11, 2043, 11), + (12, 4088, -12), + (12, 4089, 12), + (12, 4090, -13), + (12, 4091, 13), + (13, 8184, 14), + (13, 8185, -14), + (13, 8186, -15), + (13, 8187, 15), + (14, 16376, 16), + (14, 16377, 17), + (14, 16378, -16), + (15, 32758, -17), + (15, 32759, -18), + (16, 65520, -19), + (16, 65521, 18), + (16, 65522, 19), + (16, 65523, -20), + (16, 65524, -21), + (17, 131050, 20), + (17, 131051, 21), + (17, 131052, -24), + (17, 131053, -23), + (17, 131054, -22), + (17, 131055, -26), + (18, 262112, -28), + (18, 262113, 22), + (18, 262114, 23), + (18, 262115, 25), + (18, 262116, -41), + (18, 262117, -25), + (18, 262118, 26), + (18, 262119, 27), + (18, 262120, -30), + (18, 262121, -27), + (18, 262122, 24), + (18, 262123, 28), + (18, 262124, 44), + (19, 524250, -51), + (19, 524251, -46), + (19, 524252, -44), + (19, 524253, -43), + (19, 524254, -37), + (19, 524255, -33), + (19, 524256, -31), + (19, 524257, -29), + (19, 524258, 30), + (19, 524259, 37), + (19, 524260, 42), + (19, 524261, 47), + (19, 524262, 48), + (19, 524263, -60), + (19, 524264, -59), + (20, 1048530, -58), + (20, 1048531, -57), + (20, 1048532, -56), + (20, 1048533, -55), + (20, 1048534, -54), + (20, 1048535, -53), + (20, 1048536, -52), + (20, 1048537, -50), + (20, 1048538, -49), + (20, 1048539, -48), + (20, 1048540, -47), + (20, 1048541, -45), + (20, 1048542, -42), + (20, 1048543, -40), + (20, 1048544, -39), + (20, 1048545, -38), + (20, 1048546, -36), + (20, 1048547, -35), + (20, 1048548, -34), + (20, 1048549, -32), + (20, 1048550, 29), + (20, 1048551, 31), + (20, 1048552, 32), + (20, 1048553, 33), + (20, 1048554, 34), + (20, 1048555, 35), + (20, 1048556, 36), + (20, 1048557, 38), + (20, 1048558, 39), + (20, 1048559, 40), + (20, 1048560, 41), + (20, 1048561, 43), + (20, 1048562, 45), + (20, 1048563, 46), + (20, 1048564, 49), + (20, 1048565, 50), + (20, 1048566, 51), + (20, 1048567, 52), + (20, 1048568, 53), + (20, 1048569, 54), + (20, 1048570, 55), + (20, 1048571, 56), + (20, 1048572, 57), + (20, 1048573, 58), + (20, 1048574, 59), + (20, 1048575, 60), +]; + +/// SBR Huffman book `ENV15_T`: (length, code, delta) by codeword. +pub(crate) static ENV15_T: [(u8, u32, i32); 121] = [ + (2, 0, 0), + (2, 1, -1), + (3, 4, 1), + (3, 5, -2), + (4, 12, 2), + (4, 13, -3), + (5, 28, 3), + (5, 29, -4), + (6, 60, 4), + (6, 61, -5), + (7, 124, 5), + (7, 125, -6), + (8, 252, 6), + (8, 253, -7), + (9, 508, 7), + (9, 509, -8), + (10, 1020, -9), + (10, 1021, 8), + (11, 2044, -10), + (12, 4090, 9), + (12, 4091, -11), + (13, 8184, 10), + (13, 8185, -12), + (13, 8186, -13), + (14, 16374, 11), + (14, 16375, -14), + (14, 16376, 12), + (14, 16377, -15), + (15, 32756, -16), + (15, 32757, 13), + (16, 65516, -19), + (16, 65517, -18), + (16, 65518, -17), + (16, 65519, 14), + (16, 65520, -24), + (16, 65521, -20), + (16, 65522, 16), + (17, 131046, -26), + (17, 131047, -21), + (17, 131048, 15), + (17, 131049, -23), + (18, 262100, -25), + (18, 262101, -22), + (18, 262102, -60), + (18, 262103, -59), + (18, 262104, -58), + (18, 262105, -57), + (18, 262106, -56), + (18, 262107, -55), + (19, 524216, -54), + (19, 524217, -53), + (19, 524218, -52), + (19, 524219, -51), + (19, 524220, -50), + (19, 524221, -49), + (19, 524222, -48), + (19, 524223, -47), + (19, 524224, -46), + (19, 524225, -45), + (19, 524226, -44), + (19, 524227, -43), + (19, 524228, -42), + (19, 524229, -41), + (19, 524230, -40), + (19, 524231, -39), + (19, 524232, -38), + (19, 524233, -37), + (19, 524234, -36), + (19, 524235, -35), + (19, 524236, -34), + (19, 524237, -33), + (19, 524238, -32), + (19, 524239, -31), + (19, 524240, -30), + (19, 524241, -29), + (19, 524242, -28), + (19, 524243, -27), + (19, 524244, 17), + (19, 524245, 18), + (19, 524246, 19), + (19, 524247, 20), + (19, 524248, 21), + (19, 524249, 22), + (19, 524250, 23), + (19, 524251, 24), + (19, 524252, 25), + (19, 524253, 26), + (19, 524254, 27), + (19, 524255, 28), + (19, 524256, 29), + (19, 524257, 30), + (19, 524258, 31), + (19, 524259, 32), + (19, 524260, 33), + (19, 524261, 34), + (19, 524262, 35), + (19, 524263, 36), + (19, 524264, 37), + (19, 524265, 38), + (19, 524266, 39), + (19, 524267, 40), + (19, 524268, 41), + (19, 524269, 42), + (19, 524270, 43), + (19, 524271, 44), + (19, 524272, 45), + (19, 524273, 46), + (19, 524274, 47), + (19, 524275, 48), + (19, 524276, 49), + (19, 524277, 50), + (19, 524278, 51), + (19, 524279, 52), + (19, 524280, 53), + (19, 524281, 54), + (19, 524282, 55), + (19, 524283, 56), + (19, 524284, 57), + (19, 524285, 58), + (19, 524286, 59), + (19, 524287, 60), +]; + +/// SBR Huffman book `ENV30_T`: (length, code, delta) by codeword. +pub(crate) static ENV30_T: [(u8, u32, i32); 63] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 0), + (4, 14, -2), + (5, 30, 0), + (6, 62, -3), + (7, 126, 0), + (8, 254, -4), + (9, 510, 0), + (11, 2044, -5), + (11, 2045, 0), + (12, 4092, -6), + (13, 8186, -7), + (13, 8187, 0), + (14, 16376, -8), + (14, 16377, 0), + (14, 16378, -9), + (14, 16379, -10), + (14, 16380, 0), + (15, 32762, 0), + (16, 65526, 0), + (16, 65527, -13), + (16, 65528, -11), + (16, 65529, -12), + (17, 131060, -14), + (17, 131061, 0), + (18, 262124, 0), + (18, 262125, -31), + (18, 262126, -30), + (19, 524254, -29), + (19, 524255, -28), + (19, 524256, -27), + (19, 524257, -26), + (19, 524258, -25), + (19, 524259, -24), + (19, 524260, -23), + (19, 524261, -22), + (19, 524262, -21), + (19, 524263, -20), + (19, 524264, -19), + (19, 524265, -18), + (19, 524266, -17), + (19, 524267, -16), + (19, 524268, -15), + (19, 524269, 0), + (19, 524270, 0), + (19, 524271, 0), + (19, 524272, 0), + (19, 524273, 0), + (19, 524274, 0), + (19, 524275, 0), + (19, 524276, 0), + (19, 524277, 0), + (19, 524278, 0), + (19, 524279, 0), + (19, 524280, 0), + (19, 524281, 0), + (19, 524282, 0), + (19, 524283, 0), + (19, 524284, 0), + (19, 524285, 0), + (19, 524286, 0), + (19, 524287, 0), +]; + +/// SBR Huffman book `ENV30_F`: (length, code, delta) by codeword. +pub(crate) static ENV30_F: [(u8, u32, i32); 63] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 0), + (4, 14, -2), + (5, 30, 0), + (6, 62, -3), + (8, 252, 0), + (8, 253, -4), + (9, 508, 0), + (9, 509, -5), + (10, 1020, 0), + (10, 1021, -6), + (11, 2044, 0), + (11, 2045, -7), + (12, 4092, 0), + (12, 4093, -8), + (13, 8188, 0), + (14, 16378, 0), + (14, 16379, -9), + (15, 32760, -10), + (15, 32761, 0), + (15, 32762, 0), + (16, 65526, -11), + (16, 65527, -12), + (16, 65528, 0), + (16, 65529, 0), + (17, 131060, -13), + (17, 131061, -15), + (17, 131062, 0), + (17, 131063, 0), + (18, 262128, -14), + (18, 262129, 0), + (18, 262130, -18), + (18, 262131, -24), + (18, 262132, -19), + (18, 262133, 0), + (18, 262134, 0), + (19, 524270, -22), + (19, 524271, -21), + (19, 524272, -16), + (19, 524273, 0), + (19, 524274, 0), + (19, 524275, 0), + (19, 524276, 0), + (19, 524277, -23), + (19, 524278, -20), + (19, 524279, 0), + (20, 1048560, -31), + (20, 1048561, -30), + (20, 1048562, -29), + (20, 1048563, -28), + (20, 1048564, -27), + (20, 1048565, -26), + (20, 1048566, -25), + (20, 1048567, -17), + (20, 1048568, 0), + (20, 1048569, 0), + (20, 1048570, 0), + (20, 1048571, 0), + (20, 1048572, 0), + (20, 1048573, 0), + (20, 1048574, 0), + (20, 1048575, 0), +]; /// SBR Huffman book `NOISE_T`: (length, code, delta) by codeword. pub(crate) static NOISE_T: [(u8, u32, i32); 63] = [ @@ -145,6 +527,170 @@ pub(crate) static NOISE_F: [(u8, u32, i32); 63] = [ (20, 1048575, 31), ]; +/// SBR Huffman book `ENVB15_F`: (length, code, delta) by codeword. +pub(crate) static ENVB15_F: [(u8, u32, i32); 49] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 1), + (4, 14, -2), + (5, 30, 2), + (6, 62, 3), + (7, 126, -3), + (8, 254, -4), + (9, 510, 4), + (11, 2044, -5), + (11, 2045, 5), + (11, 2046, -6), + (12, 4094, 6), + (14, 16380, -7), + (15, 32762, 7), + (16, 65526, 8), + (16, 65527, -9), + (17, 131056, -8), + (18, 262114, -24), + (18, 262115, -23), + (18, 262116, -22), + (18, 262117, -21), + (18, 262118, -20), + (18, 262119, -19), + (18, 262120, -18), + (18, 262121, -17), + (18, 262122, -16), + (18, 262123, -15), + (18, 262124, -14), + (18, 262125, -13), + (18, 262126, -12), + (18, 262127, -11), + (18, 262128, -10), + (18, 262129, 9), + (18, 262130, 10), + (18, 262131, 11), + (18, 262132, 12), + (18, 262133, 13), + (18, 262134, 14), + (18, 262135, 15), + (18, 262136, 16), + (18, 262137, 17), + (18, 262138, 18), + (18, 262139, 19), + (18, 262140, 20), + (18, 262141, 21), + (18, 262142, 22), + (19, 524286, 23), + (19, 524287, 24), +]; + +/// SBR Huffman book `ENVB15_T`: (length, code, delta) by codeword. +pub(crate) static ENVB15_T: [(u8, u32, i32); 49] = [ + (1, 0, 0), + (2, 2, 1), + (3, 6, -1), + (4, 14, 2), + (5, 30, -2), + (6, 62, 3), + (7, 126, -3), + (8, 254, 4), + (9, 510, -4), + (11, 2044, -5), + (11, 2045, 5), + (12, 4092, -6), + (12, 4093, 6), + (15, 32752, 7), + (16, 65506, -7), + (16, 65507, 8), + (16, 65508, -24), + (16, 65509, -23), + (16, 65510, -22), + (16, 65511, -21), + (16, 65512, -20), + (16, 65513, -19), + (16, 65514, -18), + (16, 65515, -17), + (16, 65516, -16), + (16, 65517, -15), + (16, 65518, -14), + (16, 65519, -13), + (16, 65520, -12), + (16, 65521, -11), + (16, 65522, -10), + (16, 65523, -9), + (16, 65524, -8), + (16, 65525, 9), + (16, 65526, 10), + (16, 65527, 11), + (16, 65528, 12), + (16, 65529, 13), + (16, 65530, 14), + (17, 131062, 15), + (17, 131063, 16), + (17, 131064, 17), + (17, 131065, 18), + (17, 131066, 19), + (17, 131067, 20), + (17, 131068, 21), + (17, 131069, 22), + (17, 131070, 23), + (17, 131071, 24), +]; + +/// SBR Huffman book `ENVB30_T`: (length, code, delta) by codeword. +pub(crate) static ENVB30_T: [(u8, u32, i32); 25] = [ + (1, 0, 0), + (2, 2, 0), + (3, 6, -1), + (4, 14, -2), + (5, 30, 0), + (6, 62, 0), + (7, 126, -3), + (8, 254, -4), + (9, 510, 0), + (12, 4088, -5), + (13, 8178, -12), + (13, 8179, -11), + (13, 8180, -10), + (13, 8181, -9), + (13, 8182, -8), + (13, 8183, -7), + (13, 8184, -6), + (13, 8185, 0), + (13, 8186, 0), + (13, 8187, 0), + (13, 8188, 0), + (13, 8189, 0), + (13, 8190, 0), + (14, 16382, 0), + (14, 16383, 0), +]; + +/// SBR Huffman book `ENVB30_F`: (length, code, delta) by codeword. +pub(crate) static ENVB30_F: [(u8, u32, i32); 25] = [ + (1, 0, 0), + (2, 2, -1), + (3, 6, 0), + (4, 14, -2), + (5, 30, 0), + (6, 62, 0), + (7, 126, -3), + (8, 254, -4), + (9, 510, 0), + (11, 2044, -5), + (12, 4090, 0), + (13, 8182, 0), + (13, 8183, -12), + (13, 8184, -11), + (13, 8185, -10), + (13, 8186, -9), + (13, 8187, -8), + (14, 16376, -7), + (14, 16377, -6), + (14, 16378, 0), + (14, 16379, 0), + (14, 16380, 0), + (14, 16381, 0), + (14, 16382, 0), + (14, 16383, 0), +]; + /// SBR Huffman book `NOISEB_T`: (length, code, delta) by codeword. pub(crate) static NOISEB_T: [(u8, u32, i32); 25] = [ (1, 0, 0), @@ -222,6 +768,30 @@ mod tests { } } + #[test] + fn env15_f_is_a_complete_prefix_code() { + kraft_is_one(&ENV15_F); + is_prefix_free(&ENV15_F); + } + + #[test] + fn env15_t_is_a_complete_prefix_code() { + kraft_is_one(&ENV15_T); + is_prefix_free(&ENV15_T); + } + + #[test] + fn env30_t_is_a_complete_prefix_code() { + kraft_is_one(&ENV30_T); + is_prefix_free(&ENV30_T); + } + + #[test] + fn env30_f_is_a_complete_prefix_code() { + kraft_is_one(&ENV30_F); + is_prefix_free(&ENV30_F); + } + #[test] fn noise_t_is_a_complete_prefix_code() { kraft_is_one(&NOISE_T); @@ -234,6 +804,30 @@ mod tests { is_prefix_free(&NOISE_F); } + #[test] + fn envb15_f_is_a_complete_prefix_code() { + kraft_is_one(&ENVB15_F); + is_prefix_free(&ENVB15_F); + } + + #[test] + fn envb15_t_is_a_complete_prefix_code() { + kraft_is_one(&ENVB15_T); + is_prefix_free(&ENVB15_T); + } + + #[test] + fn envb30_t_is_a_complete_prefix_code() { + kraft_is_one(&ENVB30_T); + is_prefix_free(&ENVB30_T); + } + + #[test] + fn envb30_f_is_a_complete_prefix_code() { + kraft_is_one(&ENVB30_F); + is_prefix_free(&ENVB30_F); + } + #[test] fn noiseb_t_is_a_complete_prefix_code() { kraft_is_one(&NOISEB_T); diff --git a/scripts/aac-tables/sbrtables.py b/scripts/aac-tables/sbrtables.py index 18b935f5..6e9503e8 100644 --- a/scripts/aac-tables/sbrtables.py +++ b/scripts/aac-tables/sbrtables.py @@ -891,8 +891,16 @@ def values_config(kind, sf_index=7): element = "cpe" if balance else "sce" if kind.endswith("_F"): if is_env: - over = dict(element=element, coupling=1, num_env=1, amp_res=amp_res, - df_env=[0] * 8, df_noise=[0, 0], freq_res=1) + if amp_res: + # `amp_res_of` forces the 1.5 dB resolution whenever there is + # only one envelope, so a lone envelope can never carry the + # 3 dB book: a second envelope (T-coded, already closed) has + # to ride along to keep the resolution at 3 dB. + over = dict(element=element, coupling=1, num_env=2, amp_res=1, + df_env=[0, 1] + [0] * 6, df_noise=[0, 0], freq_res=1) + else: + over = dict(element=element, coupling=1, num_env=1, amp_res=amp_res, + df_env=[0] * 8, df_noise=[0, 0], freq_res=1) found = find_config(sf_index, over, n_high=2, n_q=1) key, width = ("env0b" if balance else "env0"), RAW_ENV[(1 if balance else 0, amp_res)] else: @@ -945,17 +953,24 @@ def book_values(oracle, kind, cfg, tables, key, width, scale, books): seen = {} crashed = set() clean = set() - for (length, code, raw), (_n, err) in zip(meta, got): + for (length, code, raw), (v, t, w), (n, err) in zip(meta, cases, got): m = pat.search(err) if not m: # Not a range-check complaint: either a clean decode (only the # routine byte-count line) or the parser aborting the element for # an unrelated reason -- only the former licenses the "both ends - # valid" inference below, so the two are told apart here. - if "is not allocated" in err or "Invalid data found" in err: - crashed.add((length, code)) - else: + # valid" inference below, so the two are told apart here. The + # "channel element 0.1 is not allocated" complaint fires on every + # mono probe regardless of whether the codeword itself was in + # range (the reference decoder treats HE-AAC mono as implicit + # stereo and always misses the second channel element), so it + # carries no information; the byte count the parser actually + # consumed against the full predicted frame length is the real + # discriminator -- an undershoot is a genuine early abort. + if n == predicted(v, t, w): clean.add((length, code)) + else: + crashed.add((length, code)) continue val = int(m.group(1)) if raw == 0: