cvent[256], 'v4' => cvent[256]]; // fb = list of W*H colours. function cv_rd16(string $s, int $p): int { return (ord($s[$p]) << 8) | ord($s[$p + 1]); } function cv_rd24(string $s, int $p): int { return (ord($s[$p]) << 16) | (ord($s[$p + 1]) << 8) | ord($s[$p + 2]); } function cv_rd32(string $s, int $p): int { return s32((ord($s[$p]) << 24) | (ord($s[$p + 1]) << 16) | (ord($s[$p + 2]) << 8) | ord($s[$p + 3])); } function cv_clamp8(int $v): int { return $v < 0 ? 0 : ($v > 255 ? 255 : $v); } function cv_yuv(int $y, int $u, int $v): int { return (cv_clamp8($y + $v * 2) << 16) | (cv_clamp8($y - intdiv($u, 2) - $v) << 8) | cv_clamp8($y + $u * 2); } /** A zeroed codebook set (calloc(32, sizeof(cvbook))). */ function cv_books(): array { $e = array_fill(0, 256, [0, 0, 0, 0]); return array_fill(0, 32, ['v1' => $e, 'v4' => $e]); } // Codebook chunk 0x20..0x27: +1 partial (flag words), +4 grey (no u/v) function cv_book(array &$cb, int $id, string $s, int $p, int $e): void { $n = ($id & 4) ? 4 : 6; $flag = 0; $mask = 0; for ($i = 0; $i < 256; $i++) { if ($id & 1) { $mask = ($mask >> 1) & 0x7fffffff; if (!$mask) { if ($p + 4 > $e) { return; } $flag = cv_rd32($s, $p); $p += 4; $mask = s32(1 << 31); } } if (($id & 1) && !($flag & $mask)) { continue; } if ($p + $n > $e) { return; } $u = $n == 6 ? s8(ord($s[$p + 4])) : 0; $v = $n == 6 ? s8(ord($s[$p + 5])) : 0; $cb[$i] = [cv_yuv(ord($s[$p]), $u, $v), cv_yuv(ord($s[$p + 1]), $u, $v), cv_yuv(ord($s[$p + 2]), $u, $v), cv_yuv(ord($s[$p + 3]), $u, $v)]; $p += $n; } } function cv_put_v1(array &$fb, int $W, int $H, int $x, int $y, array $e): void { for ($dy = 0; $dy < 4 && $y + $dy < $H; $dy++) { $row = ($y + $dy) * $W + $x; $c0 = $e[($dy >> 1) * 2]; $c1 = $e[($dy >> 1) * 2 + 1]; for ($dx = 0; $dx < 4 && $x + $dx < $W; $dx++) { $fb[$row + $dx] = $dx < 2 ? $c0 : $c1; } } } function cv_put_v4(array &$fb, int $W, int $H, int $x, int $y, array $q): void { for ($dy = 0; $dy < 4 && $y + $dy < $H; $dy++) { $row = ($y + $dy) * $W + $x; for ($dx = 0; $dx < 4 && $x + $dx < $W; $dx++) { $fb[$row + $dx] = $q[($dy >> 1) * 2 + ($dx >> 1)][($dy & 1) * 2 + ($dx & 1)]; } } } // Vector chunk 0x30 (key, V1/V4 flags), 0x31 (+skip flags), 0x32 (all V1) function cv_vec(array $b, int $id, string $s, int $p, int $e, array &$fb, int $W, int $H, int $x1, int $y1, int $x2, int $y2): void { $flag = 0; $mask = 0; for ($y = $y1; $y < $y2; $y += 4) { for ($x = $x1; $x < $x2; $x += 4) { if ($id & 1) { $mask = ($mask >> 1) & 0x7fffffff; if (!$mask) { if ($p + 4 > $e) { return; } $flag = cv_rd32($s, $p); $p += 4; $mask = s32(1 << 31); } } if (($id & 1) && !($flag & $mask)) { continue; } if (!($id & 2)) { $mask = ($mask >> 1) & 0x7fffffff; if (!$mask) { if ($p + 4 > $e) { return; } $flag = cv_rd32($s, $p); $p += 4; $mask = s32(1 << 31); } } if (($id & 2) || !($flag & $mask)) { if ($p >= $e) { return; } cv_put_v1($fb, $W, $H, $x, $y, $b['v1'][ord($s[$p++])]); } else { if ($p + 4 > $e) { return; } $v4 = $b['v4']; cv_put_v4($fb, $W, $H, $x, $y, [$v4[ord($s[$p])], $v4[ord($s[$p + 1])], $v4[ord($s[$p + 2])], $v4[ord($s[$p + 3])]]); $p += 4; } } } } /** One Cinepak frame ($len bytes at $p of $s) into $fb (W x H); $bk = 32 strip codebooks, kept between frames. */ function cinepak_frame(string $s, int $p, int $len, array &$fb, int $W, int $H, array &$bk): void { if ($len < 10) { return; } $e = $p + $len; $flags = ord($s[$p]); $ns = cv_rd16($s, $p + 8); $y0 = 0; $p += 10; for ($i = 0; $i < $ns && $i < 32 && $p + 12 <= $e;) { $id = ord($s[$p]); $sz = cv_rd24($s, $p + 1); if ($sz < 4 || $p + $sz > $e) { return; } if ($id != 0x10 && $id != 0x11) { $p += $sz; // e.g. the 3DO encoder's FE 000006 filler continue; } $y1 = cv_rd16($s, $p + 4); $x1 = cv_rd16($s, $p + 6); $y2 = cv_rd16($s, $p + 8); $x2 = cv_rd16($s, $p + 10); if ($y1 == 0) { $y1 = $y0; $y2 += $y0; } if ($i && !($flags & 1)) { $bk[$i] = $bk[$i - 1]; } for ($c = $p + 12, $se = $p + $sz; $c + 4 <= $se;) { $cid = ord($s[$c]); $csz = cv_rd24($s, $c + 1); if ($csz < 4 || $c + $csz > $se) { break; } if ($cid >= 0x20 && $cid <= 0x27) { if ($cid & 2) { cv_book($bk[$i]['v1'], $cid, $s, $c + 4, $c + $csz); } else { cv_book($bk[$i]['v4'], $cid, $s, $c + 4, $c + $csz); } } elseif ($cid >= 0x30 && $cid <= 0x32) { cv_vec($bk[$i], $cid, $s, $c + 4, $c + $csz, $fb, $W, $H, $x1, $y1, $x2, $y2); } $c += $csz; } $y0 = $y2; $p += $sz; $i++; } }