-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathencoder.py
More file actions
executable file
·458 lines (407 loc) · 14.2 KB
/
Copy pathencoder.py
File metadata and controls
executable file
·458 lines (407 loc) · 14.2 KB
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
#!/usr/bin/env python3
import sys
import gzip
from itertools import zip_longest, repeat, cycle, islice
SRC_FPS = 30
EEPROM_SIZE = 32768 - 4 # init
NUM_LOOKAHEAD_FRAMES = 3
CLOSE_ENOUGH_PIXELS = 4
TRIM_START_FRAMES = 29 # allowing room for intro text
TRIM_END_FRAMES = 51
PIXELS = 5 # horizontal pixels per cgram character
LINES = 8 # vertical pixels per cgram character
COLS = 8
ROWS = 4
CGRAM = 8
MAX_DELTA = PIXELS * LINES * COLS * ROWS
ALL_0 = b'\x00' * 8
ALL_1 = b'\x1f' * 8
# 1-9, a-w order for display updates
order = """
6sekbpi8
q91mw4ug
j3hv7rdn
co5ft2la
"""
# convert to a list of positions
pos_order = [None] * (ROWS * COLS)
for y, ln in enumerate(order.strip().split('\n')):
for x, ch in enumerate(ln):
pos = y * 10 + x
idx = ord(ch) - ord('1') if ch.isdigit() else ord(ch) - ord('a') + 9
pos_order[idx] = pos
assert None not in pos_order
pos_iter = cycle(pos_order)
def ovs(s):
'convert bytes to array of single-byte reprs for output'
return [repr(bytes([b])) for b in s]
output_override = {}
output_override.update({i: b for (i, b) in enumerate(
['E13'] + ovs(b'Bad') +
['D32'] + ovs(b'Apple'),
start=35,
)})
output_override.update({i: b for (i, b) in enumerate(
['D08'] + ovs(b'~1.2 kbit/s\x7f') + # ~ is → and \xf7 is ←
['E08'] + ovs(b'40x32 pixels') +
['D28'] + ovs(b'8 cgram chrs') +
['E28'] + ovs(b'HD44780 LCD'),
start=13696,
)})
output_override.update({i: b for (i, b) in enumerate(
['D08'] + ovs(b'Bad Apple on') +
['E08'] + ovs(b'32K EEPROM ') +
['D28'] + ovs(b' excess.org/') +
['E28'] + ovs(b' bad-apple'),
start=29586,
)})
print('''#!/usr/bin/env python3
from baconsts import *
with open('video.bin', 'wb') as f:
f.write(
INI + # function set: initial setup
HID + # hidden cursor
EIN + # entry incrementing, no shift
CLR
)''')
def w(x, comment=None):
print(f' f.write({x})' + (f' # {comment}' if comment else ''))
display_pos = 0 # 0-7 row 1, 10-17 row 2, 20-27 row 3, 30-37 row 4, 40+ cgram
display_pixels = bytearray(COLS * ROWS * LINES)
cg_pixels = bytearray([0x80] * CGRAM * LINES) # 0x80 to force replacement of data
cg_assign = {} # position where cgram character appears -> cgram number, ordered
bytes_sent = 0
file_frame = 0
frame_pixels = None
input_file = gzip.open(sys.argv[1], 'rb')
def read_frame():
return input_file.read(COLS * ROWS * LINES)
def intpixels(pixels):
"return [[0/1, ...],...] pixel values from top left to bottom right"
ipx = []
for y in range(LINES * ROWS + ROWS - 1):
if y % (LINES + 1) == LINES:
ipx.append([0] * (PIXELS * COLS + COLS - 1))
continue
cell_y = y // (LINES + 1)
line = y % (LINES + 1)
row = []
ipx.append([int(b) for b in
'0'.join(
'{:05b}'.format(pixels[
cell_y * (LINES * COLS) + x * LINES + line
])
for x in range(COLS)
)
])
return ipx
def braillepixels(ipx):
"return braille text version of intpixel matrix"
braille = []
# padded intpixel matrix to avoid IndexErrors
pipx = [r + [0] for r in ipx] + [[0] * (len(ipx[0]) + 1)] * 7
for y in range(0, len(ipx), 4):
braille.append(''.join(
chr(0x2800
+ 1 * pipx[y][x]
+ 2 * pipx[y + 1][x]
+ 4 * pipx[y + 2][x]
+ 8 * pipx[y][x + 1]
+ 16 * pipx[y + 1][x + 1]
+ 32 * pipx[y + 2][x + 1]
+ 64 * pipx[y + 3][x]
+ 128 * pipx[y + 3][x + 1]
) for x in range(0, len(ipx[0]), 2)
))
return braille
def pixeldelta(a, b):
return bin(
int.from_bytes(a, 'little') ^ int.from_bytes(b, 'little')
).count('1')
def pixel1s(a):
return bin(int.from_bytes(a, 'little')).count('1')
def solid(a):
n = pixel1s(a)
if n <= CLOSE_ENOUGH_PIXELS:
return b' '
if n >= PIXELS * LINES - CLOSE_ENOUGH_PIXELS:
return b'\xff'
def solid_exact(a):
n = pixel1s(a)
if n == 0:
return b' '
if n == PIXELS * LINES:
return b'\xff'
def print_state():
delta = pixeldelta(frame_pixels, display_pixels)
for f, d, i in zip_longest(
braillepixels(intpixels(frame_pixels)),
braillepixels(intpixels(display_pixels)),
[
f'frame {file_frame}',
f'bytes sent {bytes_sent}',
f'position {position_mnemonic(display_pos)}',
f'delta {delta}',
'▴' * min(50, int(delta * 80 / MAX_DELTA)),
f'cgram {len(cg_assign)}/{CGRAM}',
' '.join(
f'{position_mnemonic(p)}:CG{c}'
for p, c in cg_assign.items()
),
],
fillvalue = '.',
):
print('#', f, '»', d, i)
def cell(p, pixels, cgram=b''):
"Return 8 pixel-bytes at position p"
if p >= 40:
if p > 103:
raise IndexError()
return cgram[p - 40:][:8]
if p >= 30:
if p > 37:
raise IndexError()
return pixels[LINES * COLS * 3 + (p - 30) * LINES:][:8]
if p >= 20:
if p > 27:
raise IndexError()
return pixels[LINES * COLS * 2 + (p - 20) * LINES:][:8]
if p >= 10:
if p > 17:
raise IndexError()
return pixels[LINES * COLS * 1 + (p - 10) * LINES:][:8]
if p > 7 or p < 0:
raise IndexError()
return pixels[p * LINES:][:8]
def writecell(pat, p, pixels):
"Set 8 pixel-bytes at position p to pat"
assert len(pat) == 8
if p >= 30:
if p > 37:
raise IndexError()
off = LINES * COLS * 3 + (p - 30) * LINES
elif p >= 20:
if p > 27:
raise IndexError()
off = LINES * COLS * 2 + (p - 20) * LINES
elif p >= 10:
if p > 17:
raise IndexError()
off = LINES * COLS * 1 + (p - 10) * LINES
elif p > 7:
raise IndexError()
else:
off = p * LINES
pixels[off:off + 8] = (b & 0x1f for b in pat)
def sim(b, comment=None):
global display_pos, display_pixels, bytes_sent
if bytes_sent in output_override:
assert b == 'INI', (bytes_sent, b, output_override[bytes_sent])
w(output_override[bytes_sent], comment)
bytes_sent += 1
elif isinstance(b, bytes): # literal byte
w(f'{repr(b)}', comment)
if b == b'\xff':
writecell(ALL_1, display_pos, display_pixels)
elif b == b' ':
writecell(ALL_0, display_pos, display_pixels)
elif display_pos >= 40:
cg_pixels[display_pos - 40] = ord(b)
n = (display_pos - 40) // LINES
for k, v in cg_assign.items():
if v == n:
writecell(cg_pixels[n * LINES:][:LINES], k, display_pixels)
break
bytes_sent += 1
display_pos += 1
elif isinstance(b, str): # mnemonic
w(f'{b}', comment)
bytes_sent += 1
if b.startswith('CG'):
n = int(b[2:])
writecell(cg_pixels[n * LINES:][:LINES], display_pos, display_pixels)
display_pos += 1
if b == 'CLR':
display_pos = 0
display_pixels[:] = b'\x00' * len(display_pixels)
elif isinstance(b, int): # position (output mnemonic)
w(position_mnemonic(b), comment)
bytes_sent += 1
display_pos = b
def position_mnemonic(pos):
if pos >= 40:
return f'C{(pos - 40) // LINES:01d}{(pos - 40) % LINES:01d}'
elif pos >= 30:
return f'E{pos - 30 + 20:02d}'
elif pos >= 20:
return f'D{pos - 20 + 20:02d}'
elif pos >= 10:
return f'E{pos - 10:02d}'
return f'D{pos:02d}'
def minimal_update(cgnum, arr, comment=None, sim_fn=sim):
"""
yield minimal steps for update of cgram position (cgnum)
with bytearray (arr)
"""
cgpos = cgnum * LINES
pos = None
for i, (cg, b) in enumerate(zip(cg_pixels[cgpos:], arr)):
if cg == b + 0x40:
continue
if pos != i:
yield sim_fn(cgpos + i + 40, comment)
comment = None
yield sim_fn(bytes([0x40 + b]))
pos = i + 1
def encode():
while True:
# if clear screen is better match than current display, clear it
delta = pixeldelta(frame_pixels, display_pixels)
if delta > MAX_DELTA / 2 and delta > 1.5 * pixel1s(frame_pixels):
cg_assign.clear()
yield sim('CLR')
continue
# if cursor already on cell that needs to be all 0s or all 1s
# - (advance 1): ' ' or '\xff'
try:
here = cell(display_pos, frame_pixels)
except IndexError:
pass
else:
c = cell(display_pos, display_pixels)
if (solid(here) and solid(here) != solid(c)
) or (solid_exact(here) and not solid_exact(c)):
if display_pos in cg_assign:
freed = cg_assign.pop(display_pos)
yield sim(solid(here), f'free CG{freed} at {position_mnemonic(display_pos)}')
else:
yield sim(solid(here))
continue
# choose the next cell that needs to be all 0s or all 1s next in order (leftmost applicable)
# - (advance 2): position, ' ' or '\xff'
for pos in islice(pos_iter, COLS * ROWS):
c = cell(pos, display_pixels)
here = cell(pos, frame_pixels)
if (solid(here) and solid(here) != solid(c)
) or (solid_exact(here) and not solid_exact(c)):
break
else:
pos = None
if pos is not None:
# found one, now scan left
while True:
p = pos - 1
try:
left = cell(p, frame_pixels)
except IndexError:
break
if not solid(left) or solid(left) == solid(cell(p, display_pixels)):
break
pos = p
yield sim(pos)
continue
# if none choose the cell with >delta next in order
future = frame_at_bytes(bytes_sent + 10) # estimate of update cost
future_pixels = all_frames[future]
for pos in islice(pos_iter, COLS * ROWS):
here = cell(pos, future_pixels)
if pixeldelta(here, cell(pos, display_pixels)) > CLOSE_ENOUGH_PIXELS:
# check that this cell doesn't go solid very soon afterwards
if not any(
solid(cell(pos, all_frames[f]))
for f in range(future + 1, future + 1 + NUM_LOOKAHEAD_FRAMES)
):
break
else:
# if none choose the cell delta > 0 next in order
# - if assigned (advance 7):
# cgposition, 8 * bit pattern
# - if unassigned, 1+ available (advance 9):
# cgposition, 8 * bit pattern, position, cgchar
for pos in islice(pos_iter, COLS * ROWS):
here = cell(pos, future_pixels)
if pixeldelta(here, cell(pos, display_pixels)):
# check that this cell doesn't go solid very soon afterwards
if not any(
solid_exact(cell(pos, all_frames[f]))
for f in range(future + 1, future + 1 + NUM_LOOKAHEAD_FRAMES)
):
break
else:
# if none emit NOP (advance 1)
yield sim('INI') # stand-in for "NOP"
continue
# - if assigned (advance 7): * or update-in-place (advance <7)
# cgposition, 8 * bit pattern
if pos in cg_assign:
reorder = cg_assign.pop(pos)
cg_assign[pos] = reorder # move to last
yield from minimal_update(
reorder,
cell(pos, future_pixels),
f'update assigned CG{reorder} at {position_mnemonic(pos)}',
)
continue
# - if unassigned, 1+ available (advance 9):
# cgposition, 8 * bit pattern, position, cgchar
if len(cg_assign) < CGRAM:
best = None
shortest = None
for i in range(CGRAM):
if i in cg_assign.values():
continue
steps = sum(1 for e in minimal_update(
i,
cell(pos, future_pixels),
sim_fn=(lambda x,y=0:x),
))
if best is None or steps < shortest:
best = i
shortest = steps
assert best is not None
yield from minimal_update(
best,
cell(pos, future_pixels),
f'assign CG{best} to {position_mnemonic(pos)} ({shortest} steps)',
)
yield sim(pos)
cg_assign[pos] = best
yield sim(f'CG{best}')
# - else (advance 13):
# reorder oldest assigned to last
# position of oldest assigned, ' ' or '\xff'
# cgposition, 8 * bit pattern, position, cgchar
else:
oldpos, oldest = next(iter(cg_assign.items()))
cg_assign.pop(oldpos)
yield sim(oldpos, f'evict CG{oldest} at {position_mnemonic(oldpos)}')
yield sim(
b'\xff' if pixeldelta(
cell(oldpos, future_pixels), ALL_0) > 20 else b' '
)
yield from minimal_update(
oldest,
cell(pos, future_pixels),
f'reassign CG{oldest} to {position_mnemonic(pos)}'
)
yield sim(pos)
cg_assign[pos] = oldest
yield sim(f'CG{oldest}')
def frame_at_bytes(bsent):
return num_src_frames * bsent // EEPROM_SIZE
encoder = encode()
all_frames = []
while True:
frame_pixels = read_frame()
if not frame_pixels:
break
all_frames.append(frame_pixels)
all_frames = all_frames[TRIM_START_FRAMES:-TRIM_END_FRAMES]
num_src_frames = len(all_frames)
all_frames.extend([frame_pixels] * NUM_LOOKAHEAD_FRAMES)
while file_frame < num_src_frames:
frame_pixels = all_frames[file_frame]
next(encoder)
if frame_at_bytes(bytes_sent) > file_frame:
print_state()
file_frame = frame_at_bytes(bytes_sent)