Files
nt-lite/code/user/apps/video/video.c
T
maksw 73abd6f90d isolate POSIX builds from the host UCRT, wire lwext4 and ntfs-3g as ext4.sys and ntfs.sys, split the FAT driver into directory/file/async/driver/mount units, add posixsrv and dwm plus input/console/desktop syscall clients and a pile of new smoke targets, rename serial.sys to cereal.sys, name HDA/USB/lwIP/AHCI workers, harden ATA probing/reset/removal around actual hardware deadlines, absent-channel detection, diagnostics and explicit worker lifetime, redo ACPI S5 shutdown around GAS/MMIO and HW-reduced firmware, scan SSDTs for _S5 and deal with Intel SMI sleep trapping, make serial and SMP timeouts use real monotonic deadlines, add thread names and fix terminal thread teardown, scheduler and APC prepare/commit races, make advisory file locks queryable and preserve split ranges and add mutable file append state, add shared-memory pipes with kernel-assisted wait/wake, introduce capability-scoped input and display managers, split surface map rights into read/write, add explicit unmap and replace present with commit/generation/damage tracking while keeping physical presentation under the display manager, decouple native exception classes from Win32 exception numbers and translate them in kernel32, add opaque process startup data, bump the resource protocol to v3 with namespace directories, symlinks and enumeration and service control to v2 with generic READY handle exports, add the new POSIX errno/status surface and rebuild fds around posixsrv-owned shared open descriptions, descriptor-local flags, exact inheritance snapshots, dup/fcntl/record locks and shared-ring pipes, add waitpid, POSIX credentials/groups/sessions/passwd/group lookups and move sockets onto stable open descriptions, port the shells and apps to the new console/input services and surface commit API, teach init to load services.ini and start/supervise RSSRV, DWM and generic services under restricted capabilities while publishing their endpoints through RSSRV, split RSSRV into namespace, volume, display, session, input, console and resource components and make /SystemRoot a real symlink to the independently published system volume, shift the syscall table again, expand lock/resource/surface/POSIX/service/thread smoke coverage, make docs scream ASK QUESTIONS and DISCUSS at LLMs, replace MIT No Attribution with WTFPL+.
2026-09-16 10:33:20 +02:00

374 lines
10 KiB
C

// Native NTLite Bink video player.
//
// This application intentionally uses the native syscall ABI directly. Do not
// include <windows.h> here; Win32 compatibility belongs in the Win32 personality
// layer, not in native apps.
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include <intrin.h>
#include <ntlite/syscall.h>
#include <ntlite/console_service.h>
#include <ntlite/path.h>
#include "bink.h"
#define VIDEO_USAGE "usage: video <file.bk2>\n" \
" video --help\n" \
"Plays a Bink video file from disk.\n"
static void print_span(const char* s, size_t len) {
if (!s || !len) return;
(void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, len, NULL);
}
static void print(const char* s) {
if (!s) return;
(void)NtlConsoleWrite(SYS_HANDLE_INVALID, s, strlen(s), NULL);
}
static void print_u64(uint64_t v) {
char buf[32];
int i = 0;
if (v == 0) {
print("0");
return;
}
while (v && i < (int)sizeof(buf)) {
buf[i++] = (char)('0' + (v % 10));
v /= 10;
}
while (i) {
char c = buf[--i];
print_span(&c, 1);
}
}
static void fail_status(const char* what, uint32_t status) {
print("video: ");
print(what);
print(" failed status=");
print_u64(status);
print("\n");
while (true) SysExitProcess(1);
}
static void exit_now(int code) {
while (true) SysExitProcess(code);
}
static SYS_STRING_VIEW command_line_view(void) {
SYS_PEB* peb = SysGetPeb();
if (!peb || peb->Magic != SYS_PEB_MAGIC)
return SYS_SV_NULL;
return peb->CommandLine;
}
static int sv_is_space(char c) {
return c == ' ' || c == '\t';
}
static SYS_STRING_VIEW skip_one_token(SYS_STRING_VIEW s) {
size_t i = 0;
while (i < s.Length && sv_is_space(s.Data[i]))
i++;
if (i < s.Length && s.Data[i] == '"') {
i++;
while (i < s.Length && s.Data[i] != '"')
i++;
if (i < s.Length && s.Data[i] == '"')
i++;
} else {
while (i < s.Length && !sv_is_space(s.Data[i]))
i++;
}
while (i < s.Length && sv_is_space(s.Data[i]))
i++;
return SysSvFromParts(s.Data + i, s.Length - i);
}
static SYS_STRING_VIEW command_line_first_argument(SYS_STRING_VIEW cmdline) {
SYS_STRING_VIEW rest = skip_one_token(cmdline);
if (!rest.Data || rest.Length == 0)
return SYS_SV_NULL;
size_t begin = 0;
size_t end = 0;
if (rest.Data[0] == '"') {
begin = 1;
end = begin;
while (end < rest.Length && rest.Data[end] != '"')
end++;
} else {
begin = 0;
end = 0;
while (end < rest.Length && !sv_is_space(rest.Data[end]))
end++;
}
if (end <= begin)
return SYS_SV_NULL;
return SysSvFromParts(rest.Data + begin, end - begin);
}
static int sv_equals_cstr(SYS_STRING_VIEW s, const char* cstr) {
size_t len = 0;
if (!s.Data || !cstr)
return 0;
while (cstr[len])
len++;
if (s.Length != len)
return 0;
return memcmp(s.Data, cstr, len) == 0;
}
static int video_argument_is_help(SYS_STRING_VIEW arg) {
return sv_equals_cstr(arg, "-h") ||
sv_equals_cstr(arg, "--help") ||
sv_equals_cstr(arg, "/?");
}
static void print_usage(void) {
print(VIDEO_USAGE);
}
static const char* path_to_cstr(SYS_STRING_VIEW path, char* out, size_t outSize) {
if (!out || outSize == 0)
return "";
if (!path.Data || path.Length == 0) {
out[0] = '\0';
return out;
}
size_t n = path.Length;
if (n + 1 > outSize)
n = outSize - 1;
memcpy(out, path.Data, n);
out[n] = '\0';
return out;
}
static uint32_t open_file_view(SYS_STRING_VIEW path, uint32_t* handleOut) {
return NtlOpenFile(path, SYS_FILE_ACCESS_READ | SYS_FILE_ACCESS_QUERY,
SYS_FILE_OPEN_EXISTING, SYS_FILE_FLAG_NONE, handleOut);
}
int main(void) {
SYS_STRING_VIEW cmdline = command_line_view();
SYS_STRING_VIEW pathView = command_line_first_argument(cmdline);
if (!pathView.Data || pathView.Length == 0 || video_argument_is_help(pathView)) {
print_usage();
exit_now(0);
}
print("video: starting\n");
if (cmdline.Data && cmdline.Length) {
print_span(cmdline.Data, cmdline.Length);
print("\n");
}
uint32_t file = 0;
uint32_t status = open_file_view(pathView, &file);
if (status != NTLSTATUS_SUCCESS) {
char pathBuf[260];
print("video: failed to open input file: ");
print(path_to_cstr(pathView, pathBuf, sizeof(pathBuf)));
print("\n");
fail_status("NtlOpenFile", status);
}
uint64_t fileSize = 0;
status = SysGetFileSize(file, &fileSize);
if (status != NTLSTATUS_SUCCESS) {
SysCloseHandle(file);
fail_status("SysGetFileSize", status);
}
if (fileSize == 0) {
SysCloseHandle(file);
print("video: input file is empty\n");
exit_now(1);
}
uint32_t mapping = 0;
SYS_CREATE_FILE_MAPPING_REQUEST createMapping = {
sizeof(SYS_CREATE_FILE_MAPPING_REQUEST), file, SYS_VM_READ,
SYS_MAP_FLAG_NONE, fileSize, &mapping
};
status = SysCreateFileMapping(&createMapping);
if (status != NTLSTATUS_SUCCESS || !mapping) {
SysCloseHandle(file);
fail_status("SysCreateFileMapping", status);
}
void* buf = 0;
SYS_MAP_FILE_REQUEST mapFile = {
sizeof(SYS_MAP_FILE_REQUEST), mapping, SYS_MAP_ACCESS_READ,
SYS_MAP_FLAG_NONE, 0, (size_t)fileSize, &buf
};
status = SysMapFile(&mapFile);
if (status != NTLSTATUS_SUCCESS || !buf) {
SysCloseHandle(mapping);
SysCloseHandle(file);
fail_status("SysMapFile", status);
}
// The mapped VAD owns the file reference now; keep only the mapped pointer
// alive while Bink is open. This avoids a second whole-file heap allocation
// and lets the VM fault file pages in on demand.
SysCloseHandle(mapping);
SysCloseHandle(file);
print("video: input file mapped\n");
if (!BinkSoundUseWaveOut()) {
print("video: BinkSoundUseWaveOut failed: ");
const char* err = BinkGetError();
print(err ? err : "unknown");
print("\n");
SysFreeVm(buf);
exit_now(1);
}
print("video: WaveOut sound system ready\n");
HBINK b = BinkOpen((const char*)buf, BINKFROMMEMORY);
if (!b) {
print("video: BinkOpen failed: ");
const char* err = BinkGetError();
print(err ? err : "unknown");
print("\n");
SysFreeVm(buf);
exit_now(1);
}
print("video: Bink open tracks=");
print_u64((uint64_t)(b->NumTracks < 0 ? 0 : b->NumTracks));
print(" playing=");
print_u64(b->playingtracks);
print(" soundon=");
print_u64(b->soundon);
print("\n");
for (int32_t track = 0; track < b->NumTracks; track++) {
print("video: track ");
print_u64((uint64_t)track);
print(" id=");
print_u64(BinkGetTrackID(b, (uint32_t)track));
print(" type=");
print_u64(BinkGetTrackType(b, (uint32_t)track));
print(" maxBytes=");
print_u64(BinkGetTrackMaxSize(b, (uint32_t)track));
print("\n");
}
uint32_t width = b->Width;
uint32_t height = b->Height;
if (!width || !height) {
print("video: invalid video dimensions\n");
BinkClose(b);
SysFreeVm(buf);
exit_now(1);
}
uint32_t surface = 0;
SYS_CREATE_SURFACE_REQUEST surfaceReq;
memset(&surfaceReq, 0, sizeof(surfaceReq));
surfaceReq.Size = sizeof(surfaceReq);
surfaceReq.Width = width;
surfaceReq.Height = height;
surfaceReq.Format = SYS_SURFACE_FORMAT_XRGB8888;
surfaceReq.Flags = SYS_SURFACE_FLAG_NONE;
surfaceReq.HandleOut = &surface;
status = SysCreateSurface(&surfaceReq);
if (status != NTLSTATUS_SUCCESS || !surface) {
BinkClose(b);
SysFreeVm(buf);
fail_status("SysCreateSurface", status);
}
void* surfaceBase = 0;
uint32_t pitchBytes = 0;
status = SysMapSurface(surface, SYS_SURFACE_MAP_READ | SYS_SURFACE_MAP_WRITE,
&surfaceBase, &pitchBytes);
if (status != NTLSTATUS_SUCCESS || !surfaceBase || pitchBytes < width * sizeof(uint32_t)) {
SysCloseHandle(surface);
BinkClose(b);
SysFreeVm(buf);
fail_status("SysMapSurface", status);
}
print("video: native surface ready\n");
print("video: decode loop entered\n");
DISPLAY_RECT fullFrame = { 0, 0, (int32_t)width, (int32_t)height };
int firstFrameCommitted = 0;
while (1) {
if (BinkWait(b)) {
// Bink can be waiting on its decode, I/O, or WaveOut worker. Yield
// a millisecond instead of monopolizing the CPU with a hot poll.
SysSleep(1);
continue;
}
BinkDoFrame(b);
if (BinkShouldSkip(b)) {
BinkNextFrame(b);
BinkDoFrame(b);
continue;
}
BinkCopyToBuffer(b, surfaceBase, pitchBytes, height, 0, 0, BINKSURFACE32);
status = SysCommitSurface(surface, &fullFrame, 1, 0);
if (status != NTLSTATUS_SUCCESS) {
(void)SysUnmapSurface(surfaceBase);
SysCloseHandle(surface);
BinkClose(b);
SysFreeVm(buf);
fail_status("SysCommitSurface", status);
}
if (!firstFrameCommitted) {
firstFrameCommitted = 1;
print("video: first frame committed sndBytes=");
print_u64(b->SndSize);
print(" soundon=");
print_u64(b->soundon);
print(" playing=");
print_u64(b->playingtracks);
print("\n");
}
if (b->FrameNum == b->Frames)
break;
BinkNextFrame(b);
}
print("video: playback complete frame=");
print_u64(b->FrameNum);
print(" sndBytes=");
print_u64(b->SndSize);
print("\n");
print("video: closing surface\n");
(void)SysUnmapSurface(surfaceBase);
SysCloseHandle(surface);
print("video: closing Bink\n");
BinkClose(b);
print("video: Bink closed\n");
print("video: freeing input mapping\n");
SysFreeVm(buf);
print("video: goodbye\n");
exit_now(0);
}