| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | import re |
| 10 | import sys |
| 11 | |
| 12 | src, out = sys.argv[1], sys.argv[2] |
| 13 | text = open(src).read() |
| 14 | |
| 15 | sizes = [int(x) for x in re.search(r"huff_sizes\[\] = \{([^}]*)\}", text).group(1).split(",")] |
| 16 | body = re.search(r"aacps_huff_tabs\[\]\[2\] = \{(.*?)\n\};", text, re.S).group(1) |
| 17 | pairs = [(int(a), int(b)) for a, b in re.findall(r"\{\s*(\d+),\s*(\d+)\s*\}", body)] |
| 18 | offsets = [int(x) for x in re.search(r"huff_offset\[\] = \{([^}]*)\}", text).group(1).replace("\n", " ").split(",") if x.strip()] |
| 19 | |
| 20 | assert len(sizes) == 10 and len(offsets) == 10, (sizes, offsets) |
| 21 | assert sum(sizes) == len(pairs), (sum(sizes), len(pairs)) |
| 22 | names = [ |
| 23 | "IID_DF1", "IID_DT1", "IID_DF0", "IID_DT0", |
| 24 | "ICC_DF", "ICC_DT", "IPD_DF", "IPD_DT", "OPD_DF", "OPD_DT", |
| 25 | ] |
| 26 | lines = [ |
| 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 | ] |
| 37 | pos = 0 |
| 38 | for 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("];") |
| 46 | lines.append("/// The ten tables indexed by the `T_*` order above.") |
| 47 | lines.append("pub const TABLES: [&[(u8, u8)]; 10] = [%s];" % ", ".join("&T_%s" % n for n in names)) |
| 48 | open(out, "w").write("\n".join(lines) + "\n") |
| 49 | print("wrote", out, "tables", len(sizes), "codewords", len(pairs)) |
| 50 | |
| 51 | |
| 52 | def 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))] |
| 55 | k20, k34 = kmap("ff_k_to_i_20"), kmap("ff_k_to_i_34") |
| 56 | assert len(k20) == 71 and len(k34) == 91, (len(k20), len(k34)) |
| 57 | extra = [ |
| 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 | ] |
| 63 | open(out, "a").write("\n".join(extra) + "\n") |