Skip to content

Commit 4681053

Browse files
ArcaneNibblemarcan
authored andcommitted
jpeg: Implement encoding other RGB formats
Signed-off-by: R <[email protected]>
1 parent 970b278 commit 4681053

1 file changed

Lines changed: 76 additions & 20 deletions

File tree

proxyclient/experiments/jpeg.py

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ def yuv2rgb(y, u, v):
4949
ap.add_argument("--decode-pixelfmt", type=str, required=False, default='RGBA')
5050
ap.add_argument("--decode-rgba-alpha", type=int, required=False, default=255)
5151
ap.add_argument("--encode-subsampling", type=str, required=False, default='444')
52+
ap.add_argument("--encode-rst-interval", type=int, required=False)
53+
ap.add_argument("--encode-pixelfmt", type=str, required=False, default='RGB888')
5254
ap.add_argument("input", type=str)
5355
ap.add_argument("output", type=str)
5456
args = ap.parse_args()
@@ -197,16 +199,35 @@ def yuv2rgb(y, u, v):
197199
else:
198200
assert False
199201

202+
assert args.encode_pixelfmt in [
203+
'RGB888',
204+
'RGB101010',
205+
'RGB565',
206+
]
207+
pixfmt = args.encode_pixelfmt
208+
200209
image_data = b''
201210
with Image.open(args.input) as im:
202211
im_W, im_H = im.size
203212

204213
for y in range(im_H):
205214
for x in range(im_W):
206215
r, g, b = im.getpixel((x, y))
207-
image_data += struct.pack("BBBB", r, g, b, 255)
216+
if pixfmt == 'RGB888':
217+
image_data += struct.pack("BBBB", r, g, b, 255)
218+
elif pixfmt == 'RGB101010':
219+
image_data += struct.pack("<I", (r << 2) | (g << 12) | (b << 22))
220+
elif pixfmt == 'RGB565':
221+
image_data += struct.pack("<H", (r >> 3) | ((g >> 2) << 5) | ((b >> 3) << 11))
222+
else:
223+
assert False
208224

209-
BYTESPP = 4
225+
if pixfmt in ['RGB888', 'RGB101010']:
226+
BYTESPP = 4
227+
elif pixfmt == 'RGB565':
228+
BYTESPP = 2
229+
else:
230+
assert False
210231
surface_stride = im_W * BYTESPP
211232
surface_sz = surface_stride * im_H
212233
input_mem_sz = align_up(surface_sz)
@@ -790,21 +811,42 @@ def set_default_regs(param1=0):
790811

791812
jpeg.PX_TILES_W = divroundup(im_W, macroblock_W)
792813
jpeg.PX_TILES_H = divroundup(im_H, macroblock_H)
793-
if args.encode_subsampling == '444' or args.encode_subsampling == '400':
794-
jpeg.PX_PLANE0_TILING_H = 4
795-
jpeg.PX_PLANE0_TILING_V = 8
796-
jpeg.PX_PLANE1_TILING_H = 1
797-
jpeg.PX_PLANE1_TILING_V = 1
798-
elif args.encode_subsampling == '422':
799-
jpeg.PX_PLANE0_TILING_H = 8
800-
jpeg.PX_PLANE0_TILING_V = 8
801-
jpeg.PX_PLANE1_TILING_H = 1
802-
jpeg.PX_PLANE1_TILING_V = 1
803-
elif args.encode_subsampling == '420':
804-
jpeg.PX_PLANE0_TILING_H = 8
805-
jpeg.PX_PLANE0_TILING_V = 16
806-
jpeg.PX_PLANE1_TILING_H = 0
807-
jpeg.PX_PLANE1_TILING_V = 0
814+
if pixfmt in ['RGB888', 'RGB101010']:
815+
if args.encode_subsampling == '444' or args.encode_subsampling == '400':
816+
jpeg.PX_PLANE0_TILING_H = 4
817+
jpeg.PX_PLANE0_TILING_V = 8
818+
jpeg.PX_PLANE1_TILING_H = 1
819+
jpeg.PX_PLANE1_TILING_V = 1
820+
elif args.encode_subsampling == '422':
821+
jpeg.PX_PLANE0_TILING_H = 8
822+
jpeg.PX_PLANE0_TILING_V = 8
823+
jpeg.PX_PLANE1_TILING_H = 1
824+
jpeg.PX_PLANE1_TILING_V = 1
825+
elif args.encode_subsampling == '420':
826+
jpeg.PX_PLANE0_TILING_H = 8
827+
jpeg.PX_PLANE0_TILING_V = 16
828+
jpeg.PX_PLANE1_TILING_H = 0
829+
jpeg.PX_PLANE1_TILING_V = 0
830+
else:
831+
assert False
832+
elif pixfmt == 'RGB565':
833+
if args.encode_subsampling == '444' or args.encode_subsampling == '400':
834+
jpeg.PX_PLANE0_TILING_H = 2
835+
jpeg.PX_PLANE0_TILING_V = 8
836+
jpeg.PX_PLANE1_TILING_H = 1
837+
jpeg.PX_PLANE1_TILING_V = 1
838+
elif args.encode_subsampling == '422':
839+
jpeg.PX_PLANE0_TILING_H = 4
840+
jpeg.PX_PLANE0_TILING_V = 8
841+
jpeg.PX_PLANE1_TILING_H = 1
842+
jpeg.PX_PLANE1_TILING_V = 1
843+
elif args.encode_subsampling == '420':
844+
jpeg.PX_PLANE0_TILING_H = 4
845+
jpeg.PX_PLANE0_TILING_V = 16
846+
jpeg.PX_PLANE1_TILING_H = 0
847+
jpeg.PX_PLANE1_TILING_V = 0
848+
else:
849+
assert False
808850
else:
809851
assert False
810852
jpeg.PX_PLANE0_STRIDE = surface_stride
@@ -834,7 +876,14 @@ def set_default_regs(param1=0):
834876
jpeg.MATRIX_MULT[9].val = 0x0
835877
jpeg.MATRIX_MULT[10].val = 0x80
836878

837-
jpeg.ENCODE_PIXEL_FORMAT = 2
879+
if pixfmt == 'RGB888':
880+
jpeg.ENCODE_PIXEL_FORMAT.set(FORMAT=E_ENCODE_PIXEL_FORMAT.RGB888)
881+
elif pixfmt == 'RGB101010':
882+
jpeg.ENCODE_PIXEL_FORMAT.set(FORMAT=E_ENCODE_PIXEL_FORMAT.RGB101010)
883+
elif pixfmt == 'RGB565':
884+
jpeg.ENCODE_PIXEL_FORMAT.set(FORMAT=E_ENCODE_PIXEL_FORMAT.RGB565)
885+
else:
886+
assert False
838887
jpeg.ENCODE_COMPONENT0_POS = 0
839888
jpeg.ENCODE_COMPONENT1_POS = 1
840889
jpeg.ENCODE_COMPONENT2_POS = 2
@@ -850,7 +899,7 @@ def set_default_regs(param1=0):
850899
jpeg.REG_0x118 = 0x1
851900
jpeg.REG_0x11c = 0x0
852901

853-
jpeg.ENABLE_RST_LOGGING = 1
902+
jpeg.ENABLE_RST_LOGGING = args.encode_rst_interval is not None
854903

855904
jpeg.MODE = 0x16f
856905
if args.encode_subsampling == '444':
@@ -870,7 +919,10 @@ def set_default_regs(param1=0):
870919
)
871920
jpeg.JPEG_WIDTH = im_W
872921
jpeg.JPEG_HEIGHT = im_H
873-
jpeg.RST_INTERVAL = 0
922+
if args.encode_rst_interval is not None:
923+
jpeg.RST_INTERVAL = args.encode_rst_interval
924+
else:
925+
jpeg.RST_INTERVAL = 0
874926
jpeg.JPEG_OUTPUT_FLAGS = 0
875927

876928
jpeg.QTBL[0].val = 0xa06e64a0
@@ -954,6 +1006,10 @@ def set_default_regs(param1=0):
9541006
jpeg_out_sz = jpeg.COMPRESSED_BYTES.val
9551007
print(f"JPEG output is {jpeg_out_sz} bytes")
9561008

1009+
rst_log_n = jpeg.RST_LOG_ENTRIES.val
1010+
for i in range(rst_log_n):
1011+
print(f"RST log[{i}] = 0x{jpeg.RSTLOG[i].val:X}")
1012+
9571013
output_data = iface.readmem(output_buf_phys, output_mem_sz)
9581014
if args.raw_output is not None:
9591015
with open(args.raw_output, 'wb') as f:

0 commit comments

Comments
 (0)