tsvm mov encoder and decoder

This commit is contained in:
minjaesong
2022-04-12 16:44:14 +09:00
parent 41761289d3
commit 95bfaae1da
8 changed files with 219 additions and 37 deletions

54
assets/disk0/decodemov.js Normal file
View File

@@ -0,0 +1,54 @@
let filename = exec_args[1]
const FBUF_SIZE = 560*448
let status = filesystem.open("A", filename, "R")
if (status) return status
println("Reading...")
let bytes = filesystem.readAllBytes("A")
con.clear()
let readCount = 0
function readBytes(length) {
let ret = new Int8Array(length)
for (let k = 0; k < length; k++) {
ret[k] = bytes[readCount]
readCount += 1
}
return ret
}
function readInt() {
let b = readBytes(4)
return (b[0] & 255) | ((b[1] & 255) << 8) | ((b[2] & 255) << 16) | ((b[3] & 255) << 24)
}
function readShort() {
let b = readBytes(2)
return (b[0] & 255) | ((b[1] & 255) << 8)
}
let magic = readBytes(8)
if (String.fromCharCode.apply(null, magic) != '\x1fTSVMMOV') return 1
let width = readShort()
let height = readShort()
let fps = readShort()
let frameCount = readInt() % 16777216
let fbuf = sys.malloc(FBUF_SIZE)
for (let f = 0; f < frameCount; f++) {
let payloadLen = readInt()
let gzipped = readBytes(payloadLen)
gzip.decompTo(gzipped, fbuf)
dma.ramToFrame(fbuf, 0, FBUF_SIZE)
}
sys.free(fbuf)

64
assets/disk0/encodemov.js Normal file
View File

@@ -0,0 +1,64 @@
const FBUF_SIZE = 560*448
let infile = sys.malloc(120000) // somewhat arbitrary
let imagearea = sys.malloc(FBUF_SIZE*3)
let decodearea = sys.malloc(FBUF_SIZE)
let gzippedImage = sys.malloc(180000) // somewhat arbitrary
let outfilename = exec_args[1]
if (!outfilename) return 1
function appendToOutfile(bytes) {
filesystem.open("A", outfilename, "A")
filesystem.writeBytes("A", bytes)
}
function appendToOutfilePtr(ptr, len) {
filesystem.open("A", outfilename, "A")
dma.ramToCom(ptr, 0, len)
}
// write header to the file
let headerBytes = [
0x1F, 0x54, 0x53, 0x56, 0x4D, 0x4D, 0x4F, 0x56,
0x30, 0x02,
0xC0, 0x01,
0x1E, 0x00,
0x34, 0x00, 0x00, 0x7C
]
filesystem.open("A", outfilename, "W")
filesystem.writeBytes("A", headerBytes)
for (let f = 1; f <=52; f++) {
let fname = `/movtestimg/${(''+f).padStart(3,'0')}.jpg`
filesystem.open("A", fname, "R")
let fileLen = filesystem.getFileLen("A")
dma.comToRam(0, 0, infile, fileLen)
graphics.decodeImageTo(infile, fileLen, imagearea)
print(`Encoding frame ${f}...`)
graphics.imageToDisplayableFormat(imagearea, decodearea, 560, 448, 3, 1)
let gzlen = gzip.compFromTo(decodearea, FBUF_SIZE, gzippedImage)
let frameSize = [
(gzlen >>> 0) & 255,
(gzlen >>> 8) & 255,
(gzlen >>> 16) & 255,
(gzlen >>> 24) & 255
]
appendToOutfile(frameSize)
appendToOutfilePtr(gzippedImage, gzlen)
print(` ${gzlen} bytes\n`)
}
sys.free(infile)
sys.free(imagearea)
sys.free(decodearea)
sys.free(gzippedImage)

View File

@@ -141,7 +141,7 @@ filesystem.write = (driveLetter, string) => {
filesystem._flush(port[0]); filesystem._close(port[0]);
};
filesystem.writeBytes = (driveLetter, bytes) => {
var string = String.fromCharCode(...bytes);
var string = String.fromCharCode.apply(null, bytes); // no spreading: has length limit
filesystem.write(driveLetter, string);
};
filesystem.isDirectory = (driveLetter) => {