aboutsummaryrefslogtreecommitdiff
path: root/libpsn00b/psxpress/generate_lookup_table.py
blob: b40771fe84ce9714c77d3a345602231cda0a324a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env python3
# Huffman lookup table generator script for psxpress
# (C) 2022 spicyjpeg - MPL licensed

import sys, json
from array     import array
from itertools import repeat
from argparse  import ArgumentParser, FileType

HUFFMAN_TREE = {
	"10":			0xfe00, # End of block
	"11":			(  0,  1 ),
	"01": {
		"1":		(  1,  1 ),
		"00":		(  0,  2 ),
		"01":		(  2,  1 )
	},
	"001": {
		"01":		(  0,  3 ),
		"10":		(  4,  1 ),
		"11":		(  3,  1 ),
		"00000":	( 13,  1 ),
		"00001":	(  0,  6 ),
		"00010":	( 12,  1 ),
		"00011":	( 11,  1 ),
		"00100":	(  3,  2 ),
		"00101":	(  1,  3 ),
		"00110":	(  0,  5 ),
		"00111":	( 10,  1 )
	},
	"0001": {
		"00":		(  7,  1 ),
		"01":		(  6,  1 ),
		"10":		(  1,  2 ),
		"11":		(  5,  1 )
	},
	"00001": {
		"00":		(  2,  2 ),
		"01":		(  9,  1 ),
		"10":		(  0,  4 ),
		"11":		(  8,  1 )
	},
	"0000001": {
		"000":		( 16,  1 ),
		"001":		(  5,  2 ),
		"010":		(  0,  7 ),
		"011":		(  2,  3 ),
		"100":		(  1,  4 ),
		"101":		( 15,  1 ),
		"110":		( 14,  1 ),
		"111":		(  4,  2 )
	},
	"00000001": {
		"0000":		(  0, 11 ),
		"0001":		(  8,  2 ),
		"0010":		(  4,  3 ),
		"0011":		(  0, 10 ),
		"0100":		(  2,  4 ),
		"0101":		(  7,  2 ),
		"0110":		( 21,  2 ),
		"0111":		( 20,  1 ),
		"1000":		(  0,  9 ),
		"1001":		( 19,  1 ),
		"1010":		( 18,  1 ),
		"1011":		(  1,  5 ),
		"1100":		(  3,  3 ),
		"1101":		(  0,  8 ),
		"1110":		(  6,  2 ),
		"1111":		( 17,  1 )
	},
	"000000001": {
		"0000":		( 10,  2 ),
		"0001":		(  9,  2 ),
		"0010":		(  5,  3 ),
		"0011":		(  3,  4 ),
		"0100":		(  2,  5 ),
		"0101":		(  1,  7 ),
		"0110":		(  1,  6 ),
		"0111":		(  0, 15 ),
		"1000":		(  0, 14 ),
		"1001":		(  0, 13 ),
		"1010":		(  0, 12 ),
		"1011":		( 26,  1 ),
		"1100":		( 25,  1 ),
		"1101":		( 24,  1 ),
		"1110":		( 23,  1 ),
		"1111":		( 22,  1 )
	},
	"0000000001": {
		"0000":		(  0, 31 ),
		"0001":		(  0, 30 ),
		"0010":		(  0, 29 ),
		"0011":		(  0, 28 ),
		"0100":		(  0, 27 ),
		"0101":		(  0, 26 ),
		"0110":		(  0, 25 ),
		"0111":		(  0, 24 ),
		"1000":		(  0, 23 ),
		"1001":		(  0, 22 ),
		"1010":		(  0, 21 ),
		"1011":		(  0, 20 ),
		"1100":		(  0, 19 ),
		"1101":		(  0, 18 ),
		"1110":		(  0, 17 ),
		"1111":		(  0, 16 )
	},
	"00000000001": {
		"0000":		(  0, 40 ),
		"0001":		(  0, 39 ),
		"0010":		(  0, 38 ),
		"0011":		(  0, 37 ),
		"0100":		(  0, 36 ),
		"0101":		(  0, 35 ),
		"0110":		(  0, 34 ),
		"0111":		(  0, 33 ),
		"1000":		(  0, 32 ),
		"1001":		(  1, 14 ),
		"1010":		(  1, 13 ),
		"1011":		(  1, 12 ),
		"1100":		(  1, 11 ),
		"1101":		(  1, 10 ),
		"1110":		(  1,  9 ),
		"1111":		(  1,  8 )
	},
	"000000000001": {
		"0000":		(  1, 18 ),
		"0001":		(  1, 17 ),
		"0010":		(  1, 16 ),
		"0011":		(  1, 15 ),
		"0100":		(  6,  3 ),
		"0101":		( 16,  2 ),
		"0110":		( 15,  2 ),
		"0111":		( 14,  2 ),
		"1000":		( 13,  2 ),
		"1001":		( 12,  2 ),
		"1010":		( 11,  2 ),
		"1011":		( 31,  1 ),
		"1100":		( 30,  1 ),
		"1101":		( 29,  1 ),
		"1110":		( 28,  1 ),
		"1111":		( 27,  1 )
	}
}

## Utilities

def to_int10(value):
	clamped = min(max(int(value), -0x200), 0x1ff)
	return clamped + (0 if clamped >= 0 else 0x400)

def uint32_to_lines(data, indent = "\t", columns = 6):
	for offset in range(0, len(data), columns):
		line = f"{indent}0x{data[offset]:08x}"

		for item in data[(offset + 1):(offset + columns)]:
			line += f", 0x{item:08x}"

		yield line

## Table generation

def iterate_tree(tree):
	for code, value in tree.items():
		if type(value) is dict:
			# Iterate through any subtree recursively.
			for suffix, _value in iterate_tree(value):
				yield f"{code}{suffix}", _value

		elif type(value) is tuple:
			run_length, ac = value
			yield f"{code}0", (run_length << 10) | to_int10(ac)
			yield f"{code}1", (run_length << 10) | to_int10(-ac)

		else:
			yield code, value

def generate_table(codes, table_bits, prefix_bits = 0):
	table = array("I", repeat(0, 2 ** table_bits))

	for code, value in codes:
		used_bits = len(code)
		free_bits = table_bits - (used_bits - prefix_bits)
		index     = int(code[prefix_bits:], 2) << free_bits

		# Fill out every entry in the table whose index starts with the same
		# string of bits.
		for combo in range(2 ** free_bits):
			table[index | combo] = (used_bits << 16) | value

	return table

def compress_table(table):
	values     = []
	last_value = table[0]
	run_length = 0

	for value in table[1:]:
		if value == last_value and run_length < 0x7ff:
			run_length += 1
			continue

		# The run length is stored in the top 11 bits of each value, which are
		# otherwise unused.
		values.append((run_length << 21) | last_value)
		last_value = value
		run_length = 0

	values.append((run_length << 21) | last_value)
	return array("I", values)

## Main

UNCOMPRESSED_TEMPLATE = """static const DECDCTTAB {name} = {{
	.lut = {{
{short}
	}},
	.lut00 = {{
{long}
	}}
}};
"""
COMPRESSED_TEMPLATE = """static const uint32_t {name}[{length}] = {{
{table}
}};
"""

def get_args():
	parser = ArgumentParser(
		description = "Generates a Huffman lookup table structure, to be used by DecDCTvlc2()."
	)
	parser.add_argument(
		"-c", "--compress",
		action = "store_true",
		help   = "generate run-length compressed data instead of a DECDCTTAB struct"
	)
	parser.add_argument(
		"-n", "--name",
		type    = str,
		default = "_default_huffman_table",
		help    = "set the symbol name in the generated C source",
		metavar = "file"
	)
	parser.add_argument(
		"-t", "--tree",
		type    = FileType("rt"),
		help    = "use a custom Huffman tree from the specified JSON file",
		metavar = "json_file"
	)
	parser.add_argument(
		"-o", "--output",
		type    = FileType("wt"),
		default = sys.stdout,
		help    = "where to output generated table (stdout by default)",
		metavar = "file"
	)

	return parser.parse_args()

def main():
	args = get_args()
	tree = json.load(args.tree) if args.tree else HUFFMAN_TREE

	short_codes, short_bits = [], 0
	long_codes, long_bits   = [], 0

	for pair in iterate_tree(tree):
		if (code := pair[0]).startswith("00000000"):
			long_codes.append(pair)
			long_bits = max(long_bits, len(code) - 8)
		else:
			short_codes.append(pair)
			short_bits = max(short_bits, len(code))

	short_table = generate_table(short_codes, short_bits, 0)
	long_table  = generate_table(long_codes,  long_bits,  8)

	if args.compress:
		short_table.extend(long_table)
		table = compress_table(short_table)

		source = COMPRESSED_TEMPLATE.format(
			name   = args.name,
			length = len(table),
			table  = ",\n".join(uint32_to_lines(table, "\t"))
		)
	else:
		source = UNCOMPRESSED_TEMPLATE.format(
			name  = args.name,
			short = ",\n".join(uint32_to_lines(short_table, "\t\t")),
			long  = ",\n".join(uint32_to_lines(long_table,  "\t\t"))
		)

	with args.output as _file:
		_file.write(source)

if __name__ == "__main__":
	main()