Skip to content

Commit b7020ad

Browse files
committed
/16 rendering
1 parent d07ccb2 commit b7020ad

File tree

3 files changed

+23
-8
lines changed

3 files changed

+23
-8
lines changed

decode.py

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
except UnicodeDecodeError:
5151
raise ValueError('Invalid frame data, please check your input file')
5252

53+
print('fn', fn)
5354
data_len = frame.read_int(8)
5455
file_data = np.empty(data_len, dtype=np.uint8)
5556

encode.py

+13-7
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,24 @@
4949
file_name_bytes = FILE_NAME.encode()
5050

5151
raw_data = len(file_name_bytes).to_bytes(4) + file_name_bytes + len(file_data).to_bytes(8) + file_data
52-
n_bytes = len(raw_data) # we use bytes as each pixel (r/g/b) is 1 byte (0-255)
5352

54-
frame_count = math.ceil(n_bytes / CHOSEN_SIZE.frame_pixels)
55-
pad_val = frame_count * CHOSEN_SIZE.frame_pixels - n_bytes # how much to pad with black pixels (zeros)
5653

5754
# create the image data
5855
img_io = io.BytesIO()
5956

6057
parsed_data = np.frombuffer(raw_data, dtype=np.uint8)
61-
padded_data = np.pad(parsed_data, (0, pad_val)) # pad only at the end
62-
img_data = np.reshape(padded_data, (frame_count, CHOSEN_SIZE.dimension.height // 8, CHOSEN_SIZE.dimension.width // 8, 3))
6358

59+
ls = parsed_data >> 4 # shift right by 2 bits
60+
rs = parsed_data << 4 >> 4
61+
62+
final_data = np.insert(rs, np.arange(len(ls)), ls) * 16
63+
64+
n_bytes = len(final_data) # we use bytes as each pixel (r/g/b) is 1 byte (0-255)
65+
frame_count = math.ceil(n_bytes / CHOSEN_SIZE.frame_pixels)
66+
pad_val = frame_count * CHOSEN_SIZE.frame_pixels - n_bytes # how much to pad with black pixels (zeros)
67+
68+
padded_data = np.pad(final_data, (0, pad_val)) # pad only at the end
69+
img_data = np.reshape(padded_data, (frame_count, CHOSEN_SIZE.dimension.height // 8, CHOSEN_SIZE.dimension.width // 8, 3))
6470
# save images to buffer
6571
print(f'Video duration: {frame_count} frame(s) {frame_count / FPS} second(s)')
6672
print('Processing frames')
@@ -79,8 +85,8 @@
7985
else:
8086
debug_args = '-v warning'
8187
process = subprocess.run(
82-
# libaom-av1
83-
f'ffmpeg -y -hide_banner {debug_args} -stats -f image2pipe -r {FPS} -i pipe: -c:v libaom-av1 -cpu-used 8 -row-mt true -threads 8 -tile-columns 1 -tile-rows 0 -b:v {CHOSEN_SIZE.bitrate}M -pix_fmt gbrp -crf 0 "output/{new_fn}.mp4"',
88+
# libaom-av1, libsvtav1
89+
f'ffmpeg -y -hide_banner {debug_args} -stats -f image2pipe -r {FPS} -i pipe: -c:v libaom-av1 -preset 3 -svtav1-params tune=0:enable-overlays=1:scd=1:scm=0:crf=1:qp=1 -cpu-used 8 -row-mt true -threads 8 -tile-columns 1 -tile-rows 0 -b:v {CHOSEN_SIZE.bitrate}M -pix_fmt gbrp -crf 0 "output/{new_fn}.mp4"',
8490
shell=True, input=img_io.getvalue(), check=True
8591
)
8692

utils.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,15 @@ def _img_bytes_to_array(self, frame) -> np.ndarray[np.uint8]:
4949
data = io.BytesIO(frame)
5050
img = Image.open(data)
5151
img = img.resize((img.width // 8, img.height // 8), Image.Resampling.NEAREST)
52-
return np.asarray(img, dtype=np.uint8).flatten()
52+
vals = np.asarray(img, dtype=np.uint8).flatten()
53+
vals = np.rint(np.divide(vals, 16)).astype(np.uint8)
54+
final = (((vals[::2] << 4) | vals[1::2]))
55+
if self.id == 0:
56+
final.tofile('final_dec.bin')
57+
return final
58+
59+
60+
# np.left_shift(vals, 3, where=np.)
5361

5462
def read_buffer(self, size) -> np.ndarray[np.uint8]:
5563
val = self.frame[self.cursor:self.cursor + size]

0 commit comments

Comments
 (0)