Skip to content

Commit 1d57dbe

Browse files
committed
Add support for '_IR.ZIR' from G2Four/B2Four
1 parent d27c8d7 commit 1d57dbe

File tree

1 file changed

+133
-48
lines changed

1 file changed

+133
-48
lines changed

convert_zir.py

Lines changed: 133 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,19 @@
4545
"high" / Array(256, Float32l),
4646
))
4747

48+
ZIR_IR = Struct(
49+
"type" / Computed("IR"),
50+
51+
"low" / Array(1024, Float32l),
52+
"mid" / Array(1024, Float32l),
53+
"high" / Array(1024, Float32l),
54+
)
55+
4856
#--------------------------------------------------
4957

50-
def upscale_1U_to_ST(a):
58+
def downscale_1x5(a): # 768 -> 512
5159
o = []
52-
for i in range(0, len(a), 2):
60+
for i in range(0, len(a), 3):
5361
c = a[i]
5462
try:
5563
n = a[i+1]
@@ -59,12 +67,11 @@ def upscale_1U_to_ST(a):
5967
m = a[i+2]
6068
except:
6169
m = 0
62-
o.append(c)
63-
o.append((c+n+n)/3)
64-
o.append((n+n+m)/3)
70+
o.append((c+c+n)/3)
71+
o.append((n+m+m)/3)
6572
return o
6673

67-
def downscale_1U_to_LT(a):
74+
def downscale_2x(a): # 512 -> 256, or 1024 -> 512
6875
o = []
6976
for i in range(0, len(a), 2):
7077
c = a[i]
@@ -75,7 +82,7 @@ def downscale_1U_to_LT(a):
7582
o.append((c+n)/2)
7683
return o
7784

78-
def downscale_ST_to_1U(a):
85+
def downscale_3x(a): # 768 -> 256
7986
o = []
8087
for i in range(0, len(a), 3):
8188
c = a[i]
@@ -87,13 +94,12 @@ def downscale_ST_to_1U(a):
8794
m = a[i+2]
8895
except:
8996
m = 0
90-
o.append((c+c+n)/3)
91-
o.append((n+m+m)/3)
97+
o.append((c+n+m)/3)
9298
return o
9399

94-
def downscale_ST_to_LT(a):
100+
def downscale_4x(a): # 1024 -> 256
95101
o = []
96-
for i in range(0, len(a), 3):
102+
for i in range(0, len(a), 4):
97103
c = a[i]
98104
try:
99105
n = a[i+1]
@@ -103,10 +109,31 @@ def downscale_ST_to_LT(a):
103109
m = a[i+2]
104110
except:
105111
m = 0
106-
o.append((c+n+m)/3)
112+
try:
113+
l = a[i+3]
114+
except:
115+
l = 0
116+
o.append((c+n+m+l)/4)
117+
return o
118+
119+
def upscale_1x5(a): # 512 -> 768
120+
o = []
121+
for i in range(0, len(a), 2):
122+
c = a[i]
123+
try:
124+
n = a[i+1]
125+
except:
126+
n = 0
127+
try:
128+
m = a[i+2]
129+
except:
130+
m = 0
131+
o.append(c)
132+
o.append((c+n+n)/3)
133+
o.append((n+n+m)/3)
107134
return o
108135

109-
def upscale_LT_to_1U(a):
136+
def upscale_2x(a): # 256 -> 512, or 512 -> 1024
110137
o = []
111138
for i in range(len(a)):
112139
c = a[i]
@@ -118,7 +145,7 @@ def upscale_LT_to_1U(a):
118145
o.append((c+n)/2)
119146
return o
120147

121-
def upscale_LT_to_ST(a):
148+
def upscale_3x(a): # 256 -> 768
122149
o = []
123150
for i in range(len(a)):
124151
c = a[i]
@@ -131,6 +158,20 @@ def upscale_LT_to_ST(a):
131158
o.append((c+n+n)/3)
132159
return o
133160

161+
def upscale_4x(a): # 256 -> 1024
162+
o = []
163+
for i in range(len(a)):
164+
c = a[i]
165+
try:
166+
n = a[i+1]
167+
except:
168+
n = 0
169+
o.append(c)
170+
o.append((c+c+c+n)/4)
171+
o.append((c+c+n+n)/4)
172+
o.append((c+n+n+n)/4)
173+
return o
174+
134175
#--------------------------------------------------
135176

136177
def main():
@@ -156,6 +197,9 @@ def main():
156197
tpx.add_argument("-3", "--LT",
157198
help="ZIR Type 3 '_LT' = 12288 bytes, low/mid/high",
158199
action="store_true", dest="type_lt")
200+
tpx.add_argument("-4", "--IR",
201+
help="ZIR Type 4 '_IR' = 12288 bytes, low/mid/high",
202+
action="store_true", dest="type_ir")
159203

160204
opx = parser.add_mutually_exclusive_group()
161205
opx.add_argument("-O", "--writeback",
@@ -206,21 +250,28 @@ def main():
206250
zir = ZIR_ST.parse(data)
207251
elif options.type_lt:
208252
zir = ZIR_LT.parse(data)
253+
elif options.type_ir:
254+
zir = ZIR_IR.parse(data)
209255
else:
210256
guess = True
211257

212258
if guess:
213-
if len(data) == 12288:
259+
filename, extension = os.path.splitext(options.files[0])
260+
if filename[-2:] == "ST":
261+
print("Guessing 'ST'")
262+
zir = ZIR_ST.parse(data)
263+
elif filename[-2:] == "1U":
264+
print("Guessing '1U'")
265+
zir = ZIR_1U.parse(data)
266+
elif filename[-2:] == "LT":
214267
print("Guessing 'LT'")
215268
zir = ZIR_LT.parse(data)
216-
else:
217-
filename, extension = os.path.splitext(options.files[0])
218-
if filename[-2:] == "ST":
219-
print("Guessing 'ST'")
220-
zir = ZIR_ST.parse(data)
221-
elif filename[-2:] == "1U":
222-
print("Guessing '1U'")
223-
zir = ZIR_1U.parse(data)
269+
elif filename[-2:] == "IR":
270+
print("Guessing 'IR'")
271+
zir = ZIR_IR.parse(data)
272+
elif filename[-2:] == "88": # B2 Four
273+
print("Guessing '88/IR'")
274+
zir = ZIR_IR.parse(data)
224275

225276
if not zir:
226277
sys.exit("unable to guess....")
@@ -232,37 +283,69 @@ def main():
232283

233284
if options.output or options.writeback:
234285
# up/down-sample data to change type
235-
if zir['type'] == "1U":
236-
if options.type_st:
237-
zir['left'] = upscale_1U_to_ST(zir['mid'])
238-
zir['right'] = upscale_1U_to_ST(zir['mid'])
286+
if zir['type'] == "1U": # 512
287+
if options.type_st: # -> 768
288+
zir['left'] = upscale_1x5(zir['mid'])
289+
zir['right'] = upscale_1x5(zir['mid'])
239290
zir['type'] = "ST"
240-
if options.type_lt:
241-
zir['low'] = downscale_1U_to_LT(zir['low'])
242-
zir['mid'] = downscale_1U_to_LT(zir['mid'])
243-
zir['high'] = downscale_1U_to_LT(zir['high'])
291+
elif options.type_lt: # -> 256
292+
zir['low'] = downscale_2x(zir['low'])
293+
zir['mid'] = downscale_2x(zir['mid'])
294+
zir['high'] = downscale_2x(zir['high'])
295+
zir['type'] = "LT"
296+
elif options.type_ir: # -> 1024
297+
zir['low'] = upscale_2x(zir['low'])
298+
zir['mid'] = upscale_2x(zir['mid'])
299+
zir['high'] = upscale_2x(zir['high'])
300+
zir['type'] = "IR"
301+
else:
302+
sys.exit("Conversion not supported yet")
303+
304+
if zir['type'] == "ST": # 768
305+
if options.type_1u: # -> 512
306+
zir['low'] = downscale_1x5(zir['left'])
307+
zir['mid'] = downscale_1x5(zir['left'])
308+
zir['high'] = downscale_1x5(zir['left'])
309+
zir['type'] = "1U"
310+
elif options.type_lt: # -> 256
311+
zir['low'] = downscale_3x(zir['left'])
312+
zir['mid'] = downscale_3x(zir['left'])
313+
zir['high'] = downscale_3x(zir['left'])
244314
zir['type'] = "LT"
245-
if zir['type'] == "LT":
246-
if options.type_1u:
247-
zir['low'] = upscale_LT_to_1U(zir['low'])
248-
zir['mid'] = upscale_LT_to_1U(zir['mid'])
249-
zir['high'] = upscale_LT_to_1U(zir['high'])
315+
else:
316+
sys.exit("Conversion not supported yet")
317+
318+
if zir['type'] == "LT": # 256
319+
if options.type_1u: # -> 512
320+
zir['low'] = upscale_2x(zir['low'])
321+
zir['mid'] = upscale_2x(zir['mid'])
322+
zir['high'] = upscale_2x(zir['high'])
250323
zir['type'] = "1U"
251-
if options.type_st:
252-
zir['left'] = upscale_LT_to_ST(zir['mid'])
253-
zir['right'] = upscale_LT_to_ST(zir['mid'])
324+
elif options.type_st: # -> 768
325+
zir['left'] = upscale_3x(zir['mid'])
326+
zir['right'] = upscale_3x(zir['mid'])
254327
zir['type'] = "ST"
255-
if zir['type'] == "ST":
256-
if options.type_1u:
257-
zir['low'] = downscale_ST_to_1U(zir['left'])
258-
zir['mid'] = downscale_ST_to_1U(zir['left'])
259-
zir['high'] = downscale_ST_to_1U(zir['left'])
328+
elif options.type_ir: # -> 1024
329+
zir['low'] = upscale_4x(zir['low'])
330+
zir['mid'] = upscale_4x(zir['mid'])
331+
zir['high'] = upscale_4x(zir['high'])
332+
zir['type'] = "IR"
333+
else:
334+
sys.exit("Conversion not supported yet")
335+
336+
if zir['type'] == "IR": # 1024
337+
if options.type_1u: # -> 512
338+
zir['low'] = downscale_2x(zir['low'])
339+
zir['mid'] = downscale_2x(zir['mid'])
340+
zir['high'] = downscale_2x(zir['high'])
260341
zir['type'] = "1U"
261-
if options.type_lt:
262-
zir['low'] = downscale_ST_to_LT(zir['left'])
263-
zir['mid'] = downscale_ST_to_LT(zir['left'])
264-
zir['high'] = downscale_ST_to_LT(zir['left'])
342+
elif options.type_lt: # -> 256
343+
zir['low'] = downscale_4x(zir['low'])
344+
zir['mid'] = downscale_4x(zir['mid'])
345+
zir['high'] = downscale_4x(zir['high'])
265346
zir['type'] = "LT"
347+
else:
348+
sys.exit("Conversion not supported yet")
266349

267350
if options.writeback:
268351
outfile = open(options.files[0], "wb")
@@ -279,6 +362,8 @@ def main():
279362
data = ZIR_ST.build(zir)
280363
elif zir['type'] == "LT":
281364
data = ZIR_LT.build(zir)
365+
elif zir['type'] == "IR":
366+
data = ZIR_IR.build(zir)
282367

283368
outfile.write(data)
284369
outfile.close()

0 commit comments

Comments
 (0)