Repository navigation
Expand file tree
/
Copy pathVideoMaskPack.java
More file actions
92 lines (90 loc) · 5.12 KB
/
Copy pathVideoMaskPack.java
File metadata and controls
92 lines (90 loc) · 5.12 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
package dev.denver.dressroomadvanced;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.util.Properties;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/** Pure Java, bounded matte-pack reader. All file access is on the depth worker. */
final class VideoMaskPack implements Closeable {
final String sha256;
final int width, height, unique;
final VideoMaskTimeline timeline;
private final int[] refs;
private final ZipFile zip;
static final int MAX_PNG = 4_194_304;
VideoMaskPack(File path) throws IOException {
if (!path.isFile() || path.length() > 140_000_000L) throw new IOException("Invalid mask pack size");
zip = new ZipFile(path);
try {
Properties p = new Properties();
p.load(new ByteArrayInputStream(read("manifest.properties", 8192)));
if (!"1".equals(p.getProperty("schema"))) throw new IOException("Unsupported pack schema");
sha256 = p.getProperty("sha256", "");
if (!sha256.matches("[0-9a-f]{64}")) throw new IOException("Invalid video hash");
width = number(p, "width", 1, 4096);
height = number(p, "height", 1, 4096);
if ((long) width * height > 4_194_304L) throw new IOException("Mask too large");
int count = number(p, "count", 1, 21600);
unique = number(p, "unique", 1, count);
long duration = number(p, "durationUs", 1, 720_000_000);
long[] times = new long[count]; refs = new int[count];
String[] lines = new String(read("timeline.tsv", 600_000), StandardCharsets.UTF_8).split("\\n");
if (lines.length != count) throw new IOException("Timeline length mismatch");
for (int i = 0; i < count; i++) {
String[] row = lines[i].split("\\t", -1);
if (row.length != 2) throw new IOException("Invalid timeline row");
times[i] = Long.parseLong(row[0]); refs[i] = Integer.parseInt(row[1]);
if (refs[i] < 0 || refs[i] >= unique) throw new IOException("Invalid mask reference");
}
timeline = new VideoMaskTimeline(times, duration);
long total = 0;
for (int i = 0; i < unique; i++) {
ZipEntry e = zip.getEntry("masks/" + i + ".png");
if (e == null || e.getSize() < 33 || e.getSize() > MAX_PNG) throw new IOException("Invalid mask entry");
total += e.getSize();
}
if (total > 134_217_728L || zip.size() != unique + 2) throw new IOException("Invalid pack contents");
} catch (RuntimeException | IOException e) {
zip.close(); throw new IOException("Invalid matte pack", e);
}
}
static int number(Properties p, String key, int min, int max) throws IOException {
try { int x = Integer.parseInt(p.getProperty(key, "")); if (x >= min && x <= max) return x; }
catch (NumberFormatException ignored) {}
throw new IOException("Invalid " + key);
}
int maskAt(int ms) { int i = timeline.frameAtMs(ms); return i < 0 ? -1 : refs[i]; }
int maskNearestAt(int ms) { int i=timeline.nearestFrameAtMs(ms); return i<0?-1:refs[i]; }
byte[] png(int id) throws IOException {
if (id < 0 || id >= unique) throw new IOException("Invalid mask index");
byte[] b = read("masks/" + id + ".png", MAX_PNG);
if (b.length < 33 || b[0] != (byte) 137 || b[1] != 80 || b[2] != 78 || b[3] != 71
|| b[12] != 73 || b[13] != 72 || b[14] != 68 || b[15] != 82
|| intAt(b, 16) != width || intAt(b, 20) != height || b[24] != 8 || b[25] != 6)
throw new IOException("Expected 8-bit RGBA PNG of source geometry");
return b;
}
private static int intAt(byte[] b, int i) {return ((b[i]&255)<<24)|((b[i+1]&255)<<16)|((b[i+2]&255)<<8)|(b[i+3]&255);}
private byte[] read(String name, int max) throws IOException {
ZipEntry e = zip.getEntry(name);
if (e == null || e.isDirectory() || e.getSize() < 0 || e.getSize() > max) throw new IOException("Invalid " + name);
try (InputStream in = zip.getInputStream(e)) {
byte[] data = in.readNBytes(max + 1);
if (data.length != e.getSize() || data.length > max) throw new IOException("Entry size mismatch");
return data;
}
}
static String hash(File file) throws Exception {
if (!file.isFile() || file.length() > 268_435_456L) throw new IOException("Invalid source file");
MessageDigest md = MessageDigest.getInstance("SHA-256");
try (InputStream in = new FileInputStream(file)) {
byte[] b = new byte[65536]; long total = 0; int n;
while ((n = in.read(b)) != -1) { total += n; if (total > 268_435_456L) throw new IOException("Source too large"); md.update(b, 0, n); }
}
StringBuilder hex = new StringBuilder(64);
for (byte b : md.digest()) { hex.append(Character.forDigit((b >>> 4) & 15, 16)); hex.append(Character.forDigit(b & 15, 16)); }
return hex.toString();
}
@Override public void close() throws IOException { zip.close(); }
}