diff options
| author | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 18:21:44 +0000 |
|---|---|---|
| committer | Dominik <55330408+Dom-in@users.noreply.github.com> | 2026-11-22 18:48:52 +0000 |
| commit | fb6f143da9ac89e50bed0be0650ea42e2474e5c3 (patch) | |
| tree | b51c57112ef4009948acc77b76b411f1f85255a3 /runtime/test/bink_play.cpp | |
| parent | 77444fa0fbbcfe99d56e44a89f21330b669ccc51 (diff) | |
bink_play: play a game movie in a window, straight from its archive
runtime/test/bink_play.cpp maps the package's Bink2Scarlett.dll with rt::Loader
and plays a .bk2 at its frame rate (GDI window; pause, seek, loop). A movie
inside an archive plays where it is: archive.rpf@OFFSET goes to BinkOpen with
BinkSetFileOffset and the BINKFILEOFFSET flag, so nothing is extracted.
tools/bink_play.py finds the offset from the movie's name (bink_names.py).
CTest runs the headless smoke, which skips without a package and movie.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'runtime/test/bink_play.cpp')
| -rw-r--r-- | runtime/test/bink_play.cpp | 353 |
1 files changed, 353 insertions, 0 deletions
diff --git a/runtime/test/bink_play.cpp b/runtime/test/bink_play.cpp new file mode 100644 index 000000000..9f915f0cb --- /dev/null +++ b/runtime/test/bink_play.cpp | |||
| @@ -0,0 +1,353 @@ | |||
| 1 | // bink_play: play a Bink 2 stream in a window through the package's own Bink2Scarlett.dll, mapped by rt::Loader (the | ||
| 2 | // same path bink_decode_test checks). A local viewing tool for movies carved from your own copy; nothing it reads or | ||
| 3 | // shows is written anywhere. | ||
| 4 | // | ||
| 5 | // bink_play <movie.bk2 | archive.rpf@OFFSET> [--game-dir DIR] [--frames N] [--headless] [--loop] | ||
| 6 | // | ||
| 7 | // A movie inside an archive plays where it is: `movies.rpf@OFFSET` hands BinkOpen the archive with | ||
| 8 | // BinkSetFileOffset, so nothing is extracted (tools/bink_play.py finds the offset from the movie's name). A relative | ||
| 9 | // archive path is taken from the game folder. | ||
| 10 | // | ||
| 11 | // --game-dir defaults to LEONIDA_GAME_DIR, the movie to LEONIDA_MOVIE. Without a package or a movie it prints | ||
| 12 | // "SKIP (no package/movie)" and exits 77 (CTest skip). --headless decodes without a window (as fast as it can) and | ||
| 13 | // prints the decode rate, so CTest can run it; --frames stops after N frames. | ||
| 14 | // | ||
| 15 | // Keys: Space pause/resume, Left/Right one second back/forward, Home restart, L loop on/off, Esc or closing the | ||
| 16 | // window quits. The frame is scaled to the window, keeping its aspect ratio. | ||
| 17 | #include <windows.h> | ||
| 18 | |||
| 19 | #include <algorithm> | ||
| 20 | #include <chrono> | ||
| 21 | #include <cstdint> | ||
| 22 | #include <cstdio> | ||
| 23 | #include <cstdlib> | ||
| 24 | #include <cstring> | ||
| 25 | #include <filesystem> | ||
| 26 | #include <fstream> | ||
| 27 | #include <stdexcept> | ||
| 28 | #include <string> | ||
| 29 | #include <thread> | ||
| 30 | #include <vector> | ||
| 31 | |||
| 32 | #include "../shims/shims.h" | ||
| 33 | #include "../src/pe_loader.h" | ||
| 34 | |||
| 35 | namespace { | ||
| 36 | |||
| 37 | [[noreturn]] void fatal(const std::string& what) { | ||
| 38 | std::fprintf(stderr, "FAIL %s\n", what.c_str()); | ||
| 39 | std::exit(1); | ||
| 40 | } | ||
| 41 | |||
| 42 | std::string env(const char* name) { | ||
| 43 | const char* v = std::getenv(name); | ||
| 44 | return v ? v : ""; | ||
| 45 | } | ||
| 46 | |||
| 47 | uint32_t read_u32(const uint8_t* p) { | ||
| 48 | return (uint32_t)p[0] | ((uint32_t)p[1] << 8) | ((uint32_t)p[2] << 16) | ((uint32_t)p[3] << 24); | ||
| 49 | } | ||
| 50 | |||
| 51 | struct MovieHeader { | ||
| 52 | uint32_t frames = 0, width = 0, height = 0, fps_num = 0, fps_den = 0; | ||
| 53 | }; | ||
| 54 | |||
| 55 | // The fixed Bink 2 header (docs/movies.md): frame count at 0x08, size at 0x14/0x18, frame rate at 0x1C/0x20. | ||
| 56 | MovieHeader read_movie_header(const std::filesystem::path& path, uint64_t offset) { | ||
| 57 | std::ifstream f(path, std::ios::binary); | ||
| 58 | if (!f) fatal("cannot open movie " + path.string()); | ||
| 59 | f.seekg((std::streamoff)offset); | ||
| 60 | uint8_t h[0x2C]{}; | ||
| 61 | f.read(reinterpret_cast<char*>(h), sizeof(h)); | ||
| 62 | if (f.gcount() != (std::streamsize)sizeof(h)) fatal("movie is shorter than the 44-byte Bink 2 header"); | ||
| 63 | if (h[0] != 'K' || h[1] != 'B' || h[2] != '2' || h[3] < 'a' || h[3] > 'z') | ||
| 64 | fatal("movie does not start with KB2[a-z]"); | ||
| 65 | MovieHeader out; | ||
| 66 | out.frames = read_u32(h + 0x08); | ||
| 67 | out.width = read_u32(h + 0x14); | ||
| 68 | out.height = read_u32(h + 0x18); | ||
| 69 | out.fps_num = read_u32(h + 0x1C); | ||
| 70 | out.fps_den = read_u32(h + 0x20); | ||
| 71 | if (!out.frames || !out.width || !out.height || !out.fps_num || !out.fps_den) | ||
| 72 | fatal("movie header has a zero frame count, size or fps"); | ||
| 73 | if (out.width > 16384 || out.height > 16384) fatal("movie header has an implausible frame size"); | ||
| 74 | return out; | ||
| 75 | } | ||
| 76 | |||
| 77 | struct BinkApi { | ||
| 78 | void* (*open)(const char*, uint32_t) = nullptr; | ||
| 79 | uint32_t (*do_frame)(void*) = nullptr; | ||
| 80 | uint32_t (*copy)(void*, void*, uint32_t, uint32_t, uint32_t, uint32_t, uint32_t) = nullptr; | ||
| 81 | void (*next)(void*) = nullptr; | ||
| 82 | void (*go_to)(void*, uint32_t, uint32_t) = nullptr; | ||
| 83 | void (*close)(void*) = nullptr; | ||
| 84 | const char* (*get_error)() = nullptr; | ||
| 85 | void (*set_file_offset)(uint64_t) = nullptr; // applies to the next BinkOpen | ||
| 86 | }; | ||
| 87 | |||
| 88 | template <class F> void bind(rt::Module& m, const char* name, F& out) { | ||
| 89 | auto it = m.exports.find(name); | ||
| 90 | if (it == m.exports.end()) fatal(std::string("Bink2Scarlett.dll does not export ") + name); | ||
| 91 | out = reinterpret_cast<F>(it->second); | ||
| 92 | } | ||
| 93 | |||
| 94 | constexpr uint32_t kCopyBgra32 = 3; // the BinkCopyToBuffer format bink_decode_test measured as 32-bit BGRA | ||
| 95 | constexpr uint32_t kFileOffset = 0x20; // BinkOpen flag: start at the offset BinkSetFileOffset gave | ||
| 96 | |||
| 97 | struct Player { | ||
| 98 | BinkApi api; | ||
| 99 | void* bink = nullptr; | ||
| 100 | MovieHeader header; | ||
| 101 | std::vector<uint8_t> frame; | ||
| 102 | uint32_t index = 0; // the frame in `frame` | ||
| 103 | bool paused = false; | ||
| 104 | bool loop = false; | ||
| 105 | bool quit = false; | ||
| 106 | bool seek_pending = false; | ||
| 107 | uint32_t seek_to = 0; | ||
| 108 | |||
| 109 | void decode() { | ||
| 110 | api.do_frame(bink); | ||
| 111 | api.copy(bink, frame.data(), header.width * 4, header.height, 0, 0, kCopyBgra32); | ||
| 112 | } | ||
| 113 | // Bink frame numbers in BinkGoto are 1-based. | ||
| 114 | void go_to(uint32_t target) { | ||
| 115 | index = std::min(target, header.frames - 1); | ||
| 116 | api.go_to(bink, index + 1, 0); | ||
| 117 | decode(); | ||
| 118 | } | ||
| 119 | uint32_t fps_frames(double seconds) const { | ||
| 120 | return (uint32_t)(seconds * header.fps_num / header.fps_den); | ||
| 121 | } | ||
| 122 | }; | ||
| 123 | |||
| 124 | Player* g_player = nullptr; | ||
| 125 | |||
| 126 | void paint(HWND hwnd) { | ||
| 127 | PAINTSTRUCT ps; | ||
| 128 | HDC dc = BeginPaint(hwnd, &ps); | ||
| 129 | RECT rc; | ||
| 130 | GetClientRect(hwnd, &rc); | ||
| 131 | const int cw = rc.right - rc.left, ch = rc.bottom - rc.top; | ||
| 132 | const Player& p = *g_player; | ||
| 133 | // Fit the frame in the client area, keeping its aspect ratio, and black out the bars. | ||
| 134 | int w = cw, h = (int)((int64_t)cw * p.header.height / p.header.width); | ||
| 135 | if (h > ch) { | ||
| 136 | h = ch; | ||
| 137 | w = (int)((int64_t)ch * p.header.width / p.header.height); | ||
| 138 | } | ||
| 139 | const int x = (cw - w) / 2, y = (ch - h) / 2; | ||
| 140 | HBRUSH black = (HBRUSH)GetStockObject(BLACK_BRUSH); | ||
| 141 | RECT bars[] = {{0, 0, cw, y}, {0, y + h, cw, ch}, {0, y, x, y + h}, {x + w, y, cw, y + h}}; | ||
| 142 | for (const RECT& b : bars) FillRect(dc, &b, black); | ||
| 143 | BITMAPINFO bi{}; | ||
| 144 | bi.bmiHeader.biSize = sizeof(bi.bmiHeader); | ||
| 145 | bi.bmiHeader.biWidth = (LONG)p.header.width; | ||
| 146 | bi.bmiHeader.biHeight = -(LONG)p.header.height; // top-down | ||
| 147 | bi.bmiHeader.biPlanes = 1; | ||
| 148 | bi.bmiHeader.biBitCount = 32; | ||
| 149 | bi.bmiHeader.biCompression = BI_RGB; | ||
| 150 | SetStretchBltMode(dc, COLORONCOLOR); | ||
| 151 | StretchDIBits(dc, x, y, w, h, 0, 0, (int)p.header.width, (int)p.header.height, p.frame.data(), &bi, | ||
| 152 | DIB_RGB_COLORS, SRCCOPY); | ||
| 153 | EndPaint(hwnd, &ps); | ||
| 154 | } | ||
| 155 | |||
| 156 | LRESULT CALLBACK window_proc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp) { | ||
| 157 | Player& p = *g_player; | ||
| 158 | switch (msg) { | ||
| 159 | case WM_PAINT: paint(hwnd); return 0; | ||
| 160 | case WM_ERASEBKGND: return 1; | ||
| 161 | case WM_KEYDOWN: | ||
| 162 | switch (wp) { | ||
| 163 | case VK_ESCAPE: DestroyWindow(hwnd); return 0; | ||
| 164 | case VK_SPACE: p.paused = !p.paused; return 0; | ||
| 165 | case 'L': p.loop = !p.loop; return 0; | ||
| 166 | case VK_HOME: p.seek_pending = true; p.seek_to = 0; return 0; | ||
| 167 | case VK_LEFT: { | ||
| 168 | const uint32_t back = p.fps_frames(1.0); | ||
| 169 | p.seek_pending = true; | ||
| 170 | p.seek_to = p.index > back ? p.index - back : 0; | ||
| 171 | return 0; | ||
| 172 | } | ||
| 173 | case VK_RIGHT: | ||
| 174 | p.seek_pending = true; | ||
| 175 | p.seek_to = std::min(p.index + p.fps_frames(1.0), p.header.frames - 1); | ||
| 176 | return 0; | ||
| 177 | } | ||
| 178 | break; | ||
| 179 | case WM_DESTROY: p.quit = true; PostQuitMessage(0); return 0; | ||
| 180 | } | ||
| 181 | return DefWindowProcW(hwnd, msg, wp, lp); | ||
| 182 | } | ||
| 183 | |||
| 184 | void set_title(HWND hwnd, const Player& p, const std::wstring& name, double rate) { | ||
| 185 | wchar_t title[256]; | ||
| 186 | swprintf(title, 256, L"bink_play - %s - frame %u/%u - decode %.1f fps%s%s", name.c_str(), p.index + 1, | ||
| 187 | p.header.frames, rate, p.paused ? L" - paused" : L"", p.loop ? L" - loop" : L""); | ||
| 188 | SetWindowTextW(hwnd, title); | ||
| 189 | } | ||
| 190 | |||
| 191 | } // namespace | ||
| 192 | |||
| 193 | int main(int argc, char** argv) { | ||
| 194 | std::string game_dir_arg = env("LEONIDA_GAME_DIR"), movie_arg = env("LEONIDA_MOVIE"); | ||
| 195 | long max_frames = -1; | ||
| 196 | bool headless = false, loop = false; | ||
| 197 | for (int i = 1; i < argc; ++i) { | ||
| 198 | const std::string a = argv[i]; | ||
| 199 | if (a == "--game-dir" && i + 1 < argc) game_dir_arg = argv[++i]; | ||
| 200 | else if (a == "--frames" && i + 1 < argc) { | ||
| 201 | char* end = nullptr; | ||
| 202 | max_frames = std::strtol(argv[++i], &end, 10); | ||
| 203 | if (!end || *end || max_frames <= 0) fatal("--frames takes a positive count"); | ||
| 204 | } else if (a == "--headless") headless = true; | ||
| 205 | else if (a == "--loop") loop = true; | ||
| 206 | else if (!a.empty() && a[0] != '-') movie_arg = a; | ||
| 207 | else { | ||
| 208 | std::fprintf(stderr, "usage: bink_play <movie.bk2> [--game-dir DIR] [--frames N] [--headless] [--loop]\n"); | ||
| 209 | return 2; | ||
| 210 | } | ||
| 211 | } | ||
| 212 | if (game_dir_arg.empty() || movie_arg.empty()) { | ||
| 213 | std::printf("SKIP (no package/movie)\n"); | ||
| 214 | return 77; | ||
| 215 | } | ||
| 216 | const std::filesystem::path game_dir = game_dir_arg; | ||
| 217 | std::filesystem::path movie = movie_arg; | ||
| 218 | uint64_t offset = 0; | ||
| 219 | if (const size_t at = movie_arg.rfind('@'); at != std::string::npos) { | ||
| 220 | char* end = nullptr; | ||
| 221 | offset = std::strtoull(movie_arg.c_str() + at + 1, &end, 0); | ||
| 222 | if (at + 1 == movie_arg.size() || !end || *end) fatal("the movie's @OFFSET is not a number"); | ||
| 223 | movie = movie_arg.substr(0, at); | ||
| 224 | if (movie.is_relative() && !std::filesystem::exists(movie)) movie = game_dir / movie; | ||
| 225 | } | ||
| 226 | std::error_code ec; | ||
| 227 | if (!std::filesystem::is_regular_file(game_dir / "Bink2Scarlett.dll", ec)) | ||
| 228 | fatal("the game folder does not contain Bink2Scarlett.dll"); | ||
| 229 | if (!std::filesystem::is_regular_file(movie, ec)) fatal("the movie is not a file"); | ||
| 230 | |||
| 231 | Player player; | ||
| 232 | g_player = &player; | ||
| 233 | player.header = read_movie_header(movie, offset); | ||
| 234 | player.loop = loop; | ||
| 235 | const MovieHeader& h = player.header; | ||
| 236 | std::printf("movie: %ux%u, %u frames, %u/%u fps\n", h.width, h.height, h.frames, h.fps_num, h.fps_den); | ||
| 237 | |||
| 238 | try { | ||
| 239 | rt::Loader loader(game_dir); | ||
| 240 | rt::Module& dll = loader.load(game_dir / "Bink2Scarlett.dll"); | ||
| 241 | for (auto& r : dll.imports) | ||
| 242 | if (r.binding == rt::Binding::Unimplemented && rt::lower(r.dll).find("xaudio") == std::string::npos) | ||
| 243 | fatal("Bink2Scarlett.dll import " + r.dll + "!" + r.name + " is unimplemented"); | ||
| 244 | BinkApi& api = player.api; | ||
| 245 | bind(dll, "BinkOpen", api.open); | ||
| 246 | bind(dll, "BinkDoFrame", api.do_frame); | ||
| 247 | bind(dll, "BinkCopyToBuffer", api.copy); | ||
| 248 | bind(dll, "BinkNextFrame", api.next); | ||
| 249 | bind(dll, "BinkGoto", api.go_to); | ||
| 250 | bind(dll, "BinkClose", api.close); | ||
| 251 | bind(dll, "BinkGetError", api.get_error); | ||
| 252 | if (offset) { | ||
| 253 | bind(dll, "BinkSetFileOffset", api.set_file_offset); | ||
| 254 | api.set_file_offset(offset); | ||
| 255 | } | ||
| 256 | |||
| 257 | player.bink = api.open(movie.string().c_str(), offset ? kFileOffset : 0); | ||
| 258 | if (!player.bink) { | ||
| 259 | const char* e = api.get_error ? api.get_error() : nullptr; | ||
| 260 | fatal(std::string("BinkOpen failed: ") + (e && *e ? e : "(no BinkGetError text)")); | ||
| 261 | } | ||
| 262 | player.frame.assign((size_t)h.width * 4 * h.height, 0); | ||
| 263 | player.decode(); | ||
| 264 | |||
| 265 | using clock = std::chrono::steady_clock; | ||
| 266 | const auto frame_time = std::chrono::duration<double>((double)h.fps_den / h.fps_num); | ||
| 267 | const uint32_t last = max_frames > 0 ? (uint32_t)std::min<long>(max_frames, (long)h.frames) : h.frames; | ||
| 268 | |||
| 269 | if (headless) { | ||
| 270 | const auto start = clock::now(); | ||
| 271 | uint32_t decoded = 1; | ||
| 272 | while (decoded < last) { | ||
| 273 | api.next(player.bink); | ||
| 274 | player.decode(); | ||
| 275 | ++decoded; | ||
| 276 | } | ||
| 277 | const double s = std::chrono::duration<double>(clock::now() - start).count(); | ||
| 278 | std::printf("decoded %u frames in %.2f s (%.1f fps; the movie runs at %.2f fps)\n", decoded, s, | ||
| 279 | s > 0 ? decoded / s : 0.0, (double)h.fps_num / h.fps_den); | ||
| 280 | api.close(player.bink); | ||
| 281 | std::printf("RESULT PASS\n"); | ||
| 282 | return 0; | ||
| 283 | } | ||
| 284 | |||
| 285 | WNDCLASSW wc{}; | ||
| 286 | wc.lpfnWndProc = window_proc; | ||
| 287 | wc.hInstance = GetModuleHandleW(nullptr); | ||
| 288 | wc.hCursor = LoadCursor(nullptr, IDC_ARROW); | ||
| 289 | wc.lpszClassName = L"LeonidaBinkPlay"; | ||
| 290 | RegisterClassW(&wc); | ||
| 291 | // Start at up to 1280 wide (or the movie's size if smaller), the movie's aspect ratio. | ||
| 292 | const int start_w = (int)std::min<uint32_t>(h.width, 1280); | ||
| 293 | RECT r{0, 0, start_w, (int)((int64_t)start_w * h.height / h.width)}; | ||
| 294 | AdjustWindowRect(&r, WS_OVERLAPPEDWINDOW, FALSE); | ||
| 295 | HWND hwnd = CreateWindowW(wc.lpszClassName, L"bink_play", WS_OVERLAPPEDWINDOW | WS_VISIBLE, CW_USEDEFAULT, | ||
| 296 | CW_USEDEFAULT, r.right - r.left, r.bottom - r.top, nullptr, nullptr, wc.hInstance, | ||
| 297 | nullptr); | ||
| 298 | if (!hwnd) fatal("CreateWindow failed"); | ||
| 299 | const std::wstring name = movie.filename().wstring() + (offset ? L"@" + std::to_wstring(offset) : L""); | ||
| 300 | |||
| 301 | auto due = clock::now() + frame_time; | ||
| 302 | auto rate_start = clock::now(); | ||
| 303 | uint32_t rate_frames = 0, shown = 1; | ||
| 304 | double rate = 0; | ||
| 305 | while (!player.quit) { | ||
| 306 | MSG msg; | ||
| 307 | while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { | ||
| 308 | TranslateMessage(&msg); | ||
| 309 | DispatchMessageW(&msg); | ||
| 310 | } | ||
| 311 | if (player.quit) break; | ||
| 312 | if (player.seek_pending) { | ||
| 313 | player.seek_pending = false; | ||
| 314 | player.go_to(player.seek_to); | ||
| 315 | InvalidateRect(hwnd, nullptr, FALSE); | ||
| 316 | due = clock::now() + frame_time; | ||
| 317 | } | ||
| 318 | if (player.paused || clock::now() < due) { | ||
| 319 | set_title(hwnd, player, name, rate); | ||
| 320 | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||
| 321 | continue; | ||
| 322 | } | ||
| 323 | if (player.index + 1 >= h.frames || (max_frames > 0 && shown >= last)) { | ||
| 324 | if (!player.loop) break; | ||
| 325 | player.go_to(0); | ||
| 326 | } else { | ||
| 327 | api.next(player.bink); | ||
| 328 | player.decode(); | ||
| 329 | ++player.index; | ||
| 330 | } | ||
| 331 | ++shown; | ||
| 332 | InvalidateRect(hwnd, nullptr, FALSE); | ||
| 333 | UpdateWindow(hwnd); | ||
| 334 | ++rate_frames; | ||
| 335 | const double elapsed = std::chrono::duration<double>(clock::now() - rate_start).count(); | ||
| 336 | if (elapsed >= 1.0) { | ||
| 337 | rate = rate_frames / elapsed; | ||
| 338 | rate_frames = 0; | ||
| 339 | rate_start = clock::now(); | ||
| 340 | } | ||
| 341 | set_title(hwnd, player, name, rate); | ||
| 342 | // Hold the movie's frame rate; when decoding falls behind, don't try to catch up in a burst. | ||
| 343 | due += std::chrono::duration_cast<clock::duration>(frame_time); | ||
| 344 | if (clock::now() > due + std::chrono::duration_cast<clock::duration>(frame_time * 4)) due = clock::now(); | ||
| 345 | } | ||
| 346 | if (IsWindow(hwnd)) DestroyWindow(hwnd); | ||
| 347 | api.close(player.bink); | ||
| 348 | std::printf("played %u frames\n", shown); | ||
| 349 | return 0; | ||
| 350 | } catch (const std::exception& e) { | ||
| 351 | fatal(e.what()); | ||
| 352 | } | ||
| 353 | } | ||