forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

gen-ps-huff.py63 lines, 3.0 KiBHistoryRawDownload
1#!/usr/bin/env python3
2"""Generates crates/ec-aac/src/ps_huff.rs from the Parametric Stereo Huffman
3code-length tables as tabulated in FFmpeg's libavcodec/aacpsdata.c (the
4`aacps_huff_tabs` array: one (symbol, code length) pair per codeword, in the
5order ISO/IEC 14496-3 subpart 8 lists them). Usage:
6
7 gen-ps-huff.py <aacpsdata.c> <out.rs>
8"""
9import re
10import sys
11
12src, out = sys.argv[1], sys.argv[2]
13text = open(src).read()
14
15sizes = [int(x) for x in re.search(r"huff_sizes\[\] = \{([^}]*)\}", text).group(1).split(",")]
16body = re.search(r"aacps_huff_tabs\[\]\[2\] = \{(.*?)\n\};", text, re.S).group(1)
17pairs = [(int(a), int(b)) for a, b in re.findall(r"\{\s*(\d+),\s*(\d+)\s*\}", body)]
18offsets = [int(x) for x in re.search(r"huff_offset\[\] = \{([^}]*)\}", text).group(1).replace("\n", " ").split(",") if x.strip()]
19
20assert len(sizes) == 10 and len(offsets) == 10, (sizes, offsets)
21assert sum(sizes) == len(pairs), (sum(sizes), len(pairs))
22names = [
23 "IID_DF1", "IID_DT1", "IID_DF0", "IID_DT0",
24 "ICC_DF", "ICC_DT", "IPD_DF", "IPD_DT", "OPD_DF", "OPD_DT",
25]
26lines = [
27 "//! Parametric Stereo Huffman code-length tables (ISO/IEC 14496-3 subpart 8).",
28 "//!",
29 "//! Generated by `scripts/aac-tables/gen-ps-huff.py` from the `aacps_huff_tabs`",
30 "//! array of FFmpeg's `libavcodec/aacpsdata.c`: one `(symbol, code length)` pair",
31 "//! per codeword, in table order. Codes are canonical: within each length they",
32 "//! are assigned in table order (see `ps_data::build_codes`). Do not edit by hand.",
33 "",
34 "/// Added to a decoded symbol to give the parameter value, per table.",
35 "pub const OFFSETS: [i32; 10] = [%s];" % ", ".join(str(o) for o in offsets),
36]
37pos = 0
38for i, (name, n) in enumerate(zip(names, sizes)):
39 chunk = pairs[pos:pos + n]
40 pos += n
41 lines.append("/// `%s`: %d codewords." % (name, n))
42 lines.append("pub const T_%s: [(u8, u8); %d] = [" % (name, n))
43 for j in range(0, len(chunk), 6):
44 lines.append(" " + " ".join("(%d, %d)," % p for p in chunk[j:j + 6]))
45 lines.append("];")
46lines.append("/// The ten tables indexed by the `T_*` order above.")
47lines.append("pub const TABLES: [&[(u8, u8)]; 10] = [%s];" % ", ".join("&T_%s" % n for n in names))
48open(out, "w").write("\n".join(lines) + "\n")
49print("wrote", out, "tables", len(sizes), "codewords", len(pairs))
50
51# The two stereo-band index maps (ISO/IEC 14496-3 tables 8.48/8.49), from the same file.
52def kmap(name):
53 m = re.search(r"%s\[\] = \{(.*?)\};" % name, text, re.S)
54 return [int(x) for x in re.findall(r"-?\d+", m.group(1))]
55k20, k34 = kmap("ff_k_to_i_20"), kmap("ff_k_to_i_34")
56assert len(k20) == 71 and len(k34) == 91, (len(k20), len(k34))
57extra = [
58 "/// Table 8.48: sub-subband `k` (0..71) to the 20-band parameter index.",
59 "pub const K_TO_I_20: [u8; 71] = [%s];" % ", ".join(str(v) for v in k20),
60 "/// Table 8.49: sub-subband `k` (0..91) to the 34-band parameter index.",
61 "pub const K_TO_I_34: [u8; 91] = [%s];" % ", ".join(str(v) for v in k34),
62]
63open(out, "a").write("\n".join(extra) + "\n")