|
| 1 | +# Writes src/codecs/vp8-enc-tables.ts: the tables of libwebp's lossy (VP8) encoder that aren't in the decoder's |
| 2 | +# tables, read from the C sources of a libwebp checkout (https://chromium.googlesource.com/webm/libwebp, BSD-3-Clause): |
| 3 | +# python scripts/vp8-enc-tables.py path/to/libwebp > src/codecs/vp8-enc-tables.ts |
| 4 | +import base64, re, struct, sys |
| 5 | + |
| 6 | +root = sys.argv[1].rstrip('/') + '/' |
| 7 | + |
| 8 | + |
| 9 | +def table(path, name): |
| 10 | + """the values of a C array, flattened""" |
| 11 | + s = open(root + path).read() |
| 12 | + m = re.search(r'\b' + re.escape(name) + r'\s*(\[[^=]*\])\s*=\s*\{', s) |
| 13 | + i = j = m.end() |
| 14 | + depth = 1 |
| 15 | + while depth: |
| 16 | + depth += {'{': 1, '}': -1}.get(s[j], 0) |
| 17 | + j += 1 |
| 18 | + body = re.sub(r'//[^\n]*', '', re.sub(r'/\*.*?\*/', '', s[i:j - 1], flags=re.S)) |
| 19 | + return [int(x, 0) for x in re.findall(r'-?(?:0x[0-9a-fA-F]+|\d+)', body)] |
| 20 | + |
| 21 | + |
| 22 | +def ints(name, comment, values): |
| 23 | + rows = [', '.join(str(v) for v in values[i:i + 32]) for i in range(0, len(values), 32)] |
| 24 | + print(f'/** {comment} */\nexport const {name} = [\n ' + ',\n '.join(rows) + '\n];\n') |
| 25 | + |
| 26 | + |
| 27 | +def words(name, comment, values): |
| 28 | + data = base64.b64encode(struct.pack(f'<{len(values)}H', *values)).decode() |
| 29 | + print(f'/** {comment} (16 bit, base64) */\nexport const {name} = \'{data}\';\n') |
| 30 | + |
| 31 | + |
| 32 | +print('''/*! |
| 33 | + * Generated by scripts/vp8-enc-tables.py from libwebp, don't edit. Copyright (c) 2010 Google Inc., BSD-3-Clause License |
| 34 | + * (see THIRD_PARTY_NOTICES.md) |
| 35 | + */ |
| 36 | +''') |
| 37 | +ints('ENTROPY_COST', 'VP8EntropyCost: the cost of a bit by probability (1/256 bits)', table('src/dsp/cost.c', 'VP8EntropyCost')) |
| 38 | +words('LEVEL_FIXED_COSTS', 'VP8LevelFixedCosts: the part of the cost of a level that doesn\'t depend on probabilities', |
| 39 | + table('src/dsp/cost.c', 'VP8LevelFixedCosts')) |
| 40 | +ints('LEVEL_CODES', 'VP8LevelCodes[67][2]: the contexts used to code a level (pattern) and their bits', |
| 41 | + table('src/enc/cost_enc.c', 'VP8LevelCodes')) |
| 42 | +words('FIXED_COSTS_I4', 'VP8FixedCostsI4[top][left][mode]: the costs of the intra 4x4 modes', |
| 43 | + table('src/enc/cost_enc.c', 'VP8FixedCostsI4')) |
| 44 | +ints('BMODES_PROBA', 'kBModesProba[top][left][9]: the probabilities of the intra 4x4 modes (encoder\'s mode order)', |
| 45 | + table('src/enc/tree_enc.c', 'kBModesProba')) |
| 46 | +ints('AC_TABLE2', 'kAcTable2: the AC quantizers of the luma DC (Y2) blocks', table('src/enc/quant_enc.c', 'kAcTable2')) |
| 47 | +ints('LEVELS_FROM_DELTA', 'kLevelsFromDelta[sharpness][delta]: the filter strength that smooths an edge step', |
| 48 | + table('src/enc/filter_enc.c', 'kLevelsFromDelta')) |
0 commit comments