diff --git a/docs/mods.md b/docs/mods.md index 72f9eca6..d26553db 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -48,7 +48,10 @@ Two mods that edit the same game file do not combine: the higher one wins the wh (`.tkcl` files) go the same way. The port merges the resource size table on its own: at every start with mods on, it combines the tables the mods ship -and raises the entries for files a mod made larger or added. A separate RESTBL mod is not needed. +and raises the entries for files a mod made larger or added, the files inside a mod's packs (`Pack\**\*.pack.zs`) +included. A separate RESTBL mod is not needed. The table tells the game how much memory each file needs: a file that +outgrew its entry crashes the game when it loads, which can be long after the start, when the game first needs it. The +log line `RomFS: resource size table: ...` says how many entries were raised and names the first files inside packs. ## Code patches (ExeFS) diff --git a/runtime/cfg.c b/runtime/cfg.c index 487665a2..afdafbf8 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -45,11 +45,11 @@ static const char *const known[] = { "FULLSCREEN", "FXAA", "GPL", "GPL_DRAW_BUILD", "GPU", "GPU_INDEX", "GPU_NAME", "HDR", "HDR_GAME_NITS", "HDR_HUE", "HDR_PEAK_NITS", "HDR_UI_NITS", "HIGHFPS_FIX", "INPUT", "INPUT_RECORD", "INPUT_SCRIPT", "LANG", "LIB_PREBUILD", "LIB_PREBUILD_KEEP", "LIB_PREBUILD_THREADS", "LOCALE", "LOD_GUARD", "LSTICK_DEADZONE", "MEMORY", "MENU_LANG", "MENU_TEST", - "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", + "MENU_CURSOR", "MENU_CURSOR_LOG", "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_AIM_SCALE", "MOUSE_MODE", "MOUSE_DIRECT_PATCH", "MOUSE_SENS", "MOVIE_ASPECT", "MOVIE_HOST", "MOVIE_PACE", "MVOBJ_ALL", "MVOBJ_BANKS", "MVOBJ_EQUAL", "MVOBJ_EXACT", "MVOBJ_EXEC", "MVOBJ_NOC4", "MVOBJ_ONLY", "MVOBJ_TIMES", "MVPREV_CHECK", "MVPREV_GROW", "MVPREV_SKIP", "MVPREV_TRUST", "MV_CHECK", "MV_SURVEY", "ONE_PER_CORE", "OPERATION_MODE", "OUTPUT_RES", "PAD_LAYOUT", "PASS_CRUMBS", "PATCH_OUTPUT", "PATCH_SCENE", "PCSS", "PCSS_K", "PERF_OVERLAY", "PERF_SLOW_MS", "PIPELINE_CACHE", "PIPE_MEMO", "PIPE_PREBUILD", "PIPE_SKIP", "PIPE_SYNC", "PIPE_WAIT", - "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", + "PREWARM_XLATE", "PROBE_MOVIE", "PROBE_MOVIE_BENCH", "PROBE_MOVIE_SHOT", "PROFILE", "PROFILE_CHAINS", "PROFILE_DIR", "PROFILE_FRAMES", "PROFILE_MAIN", "PROFILE_RENDER", "PROMPTS", "PUSH_SKIP", "PUSH_STAGE", "RDOC", "RDOC_DIR", "RDOC_DLL", "REALTIME", "REC_END", "REEXEC", "RENDER_SCALE", "REPLAY_ASYNC", "REPLAY_CHECK", "RESOLVE_CHECK", "RESTART_WAIT", "ROMFS", "ROMFS_INDEX", "ROMFS_MMAP", "ROMFS_MODS", "ROMFS_PATCHES", @@ -69,7 +69,7 @@ static const struct { const char *key, *section; } sections[] = { {"REALTIME", "frame rate"}, {"CLOTH_REALTIME", "frame rate"}, {"HIGHFPS_FIX", "frame rate"}, {"FULLSCREEN", "display"}, {"WINDOW_SIZE", "display"}, {"PERF_OVERLAY", "display"}, {"VK_PRESENT", "display"}, {"GPU_INDEX", "display"}, {"OUTPUT_RES", "display"}, {"RENDER_SCALE", "display"}, {"UPSCALER", "display"}, - {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"}, + {"SHARPNESS", "display"}, {"ASPECT", "display"}, {"MOVIE_ASPECT", "display"}, {"FOV", "display"}, {"ANISO", "display"}, {"COLOR_PRESET", "display"}, {"COLOR_BRIGHTNESS", "display"}, {"COLOR_CONTRAST", "display"}, {"COLOR_SATURATION", "display"}, {"COLOR_GAMMA", "display"}, {"HDR", "display"}, {"HDR_PEAK_NITS", "display"}, {"HDR_GAME_NITS", "display"}, @@ -120,7 +120,7 @@ static const CfgMeta metas[] = { {"DLAA", "0", "0|1", RESTART}, {"DLSS", "dlaa", "dlaa|quality|balanced|performance|ultra_performance", LIVE}, {"DLSS_PRESET", "default", "default|k|j", LIVE}, {"DLSS_CHAR_MASK", "65", "0|35|65|100", LIVE}, {"FXAA", "1", "0|1", LIVE}, - {"ASPECT", "fit", "fit|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, + {"ASPECT", "fit", "fit|stretch", LIVE}, {"MOVIE_ASPECT", "keep", "keep|stretch", LIVE}, {"FOV", "100", "70..140", LIVE}, {"ANISO", "game", "game|2|4|8|16", RESTART}, // vk_texture.c sampler_features // colour adjustment (vk_color.c); the preset is the menu's, the four values are what the present uses {"COLOR_PRESET", "default", "default|vivid|clarity|custom", LIVE}, {"COLOR_BRIGHTNESS", "0", "-30..30", LIVE}, diff --git a/runtime/main.c b/runtime/main.c index c23fc49c..720896f8 100644 --- a/runtime/main.c +++ b/runtime/main.c @@ -2,7 +2,8 @@ // // totk.exe [--exefs DIR] [--romfs DIR] [--profile DIR] // --selftest layout, dispatch table and host-function registry; needs no game files -// --probe-mods the mods folder scan, order and switches (mods.c); needs no game files +// --probe-mods the mods folder scan, order and switches (mods.c), the size table entries of the members of +// mods' packs (restbl_probe.c); needs no game files // --probe-loader load and relocate the modules, check them against the measured 1.2.1 values // --probe-kernel, --probe-ipc, --probe-services, --probe-fs host probes // --probe-discord the Rich Presence IPC exchange; skips without a Discord client; no game files diff --git a/runtime/mods_probe.c b/runtime/mods_probe.c index 221cb2d7..e576ee84 100644 --- a/runtime/mods_probe.c +++ b/runtime/mods_probe.c @@ -1,5 +1,6 @@ // --probe-mods: the mods folder scan, its order and switches (mods.c). Builds a mods folder of its own next to totk.exe, -// scans it and removes it. Needs no game files. +// scans it and removes it. Also the resource size table's entries of the members of the mods' packs (restbl_probe.c). +// Needs no game files. #include #include @@ -7,6 +8,7 @@ #include "host.h" #include "mods.h" #include "platform/platform.h" +#include "restbl.h" static int fails; #define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL mods: %s\n", what); } } while (0) @@ -126,5 +128,6 @@ int probe_mods(void) { } remove_tree(root); printf("mods: %s\n", fails ? "FAIL" : "ok"); - return fails ? 1 : 0; + int table = restbl_probe(); + return fails || table ? 1 : 0; } diff --git a/runtime/movie.h b/runtime/movie.h index 96745e9c..43fb427e 100644 --- a/runtime/movie.h +++ b/runtime/movie.h @@ -29,10 +29,18 @@ typedef struct { enum { MOVIE_EV_INPUT, MOVIE_EV_OUTPUT, MOVIE_EV_FORMAT, MOVIE_EV_ERROR }; // DecoderEvents: EventType * per event enum { MOVIE_TYPE_VP9 = 3, MOVIE_MODE_BUFFER = 1, MOVIE_FORMAT_NV12 = 4, MOVIE_FLAG_EOS = 4 }; -typedef struct { uint64_t frames, decode_ns, max_ns, errors, dropped, handed; } MovieStats; +typedef struct { uint64_t frames, decode_ns, max_ns, errors, dropped, handed, fit_ns; } MovieStats; +// fit_ns: the part of decode_ns spent writing the NV12 frame void movie_set_guest(const MovieGuest *g); // probe: stand-ins instead of the modules void movie_set_clock(uint64_t (*clock)(void)); // probe: a fake clock (ns) for the pacing; NULL = real void movie_set_pace(int on); // probe: 0 hands every decoded frame over at the next tick int movie_stats(uint64_t obj, MovieStats *out); // counters of the decoder at guest object obj; 0 if none int movie_quiet(uint64_t obj); // 1 when its worker is idle and cannot take more input now + +// Keeping a movie's shape on an output of another shape (ov_movie.c): the picture is shrunk inside the frame, with +// black (Y 16, U = V 128) around it. movie_fit_new returns NULL when the shapes agree within 1%: copy the frame. +typedef struct MovieFit MovieFit; +MovieFit *movie_fit_new(int w, int h, unsigned out_w, unsigned out_h); +void movie_fit_apply(const MovieFit *f, const uint8_t *const plane[3], const int stride[3], uint8_t *dst); +void movie_fit_free(MovieFit *f); diff --git a/runtime/movie_probe.c b/runtime/movie_probe.c index 7580b739..a944d569 100644 --- a/runtime/movie_probe.c +++ b/runtime/movie_probe.c @@ -9,6 +9,7 @@ // Without the RomFS (no game data) it prints why and exits 0. TOTK_PROBE_MOVIE_SHOT= saves the middle frame. #include #include +#include #include #include @@ -267,7 +268,104 @@ static int paced(uint64_t obj, const uint8_t *data, const Track *t, int limit, u return 0; } +// The shape-keeping resampler (ov_movie.c movie_fit_new / movie_fit_apply) on a synthetic 1920x1080 I420 picture, which +// needs no game data. The planes are gradients, which a box filter keeps linear, so every output sample has an expected +// value: luma rises left to right and top to bottom, U rises left to right, V falls top to bottom. A 16:9 output must give +// no plan; the others must give bars (Y 16, U = V 128) that are symmetric, a picture of the movie's shape once the game +// stretches the frame, and samples of the picture where the gradients say. TOTK_PROBE_MOVIE_BENCH=1 also times the frame. +#define FIT_CHECK(cond, ...) \ + do { \ + if (!(cond)) { \ + printf("probe-movie: FAILED: "); \ + printf(__VA_ARGS__); \ + printf("\n"); \ + rc = 1; \ + goto done; \ + } \ + } while (0) +static int check_fit(void) { + enum { W = 1920, H = 1080, CW = W / 2, CH = H / 2 }; + int rc = 0; + MovieFit *f = NULL; + uint8_t *y = malloc((size_t)W * H), *u = malloc((size_t)CW * CH), *v = malloc((size_t)CW * CH); + uint8_t *out = malloc((size_t)W * H * 3 / 2); + FIT_CHECK(y && u && v && out, "fit test buffers"); + for (int j = 0; j < H; j++) + for (int i = 0; i < W; i++) y[j * W + i] = (uint8_t)(32 + 150.0 * (i + 0.5) / W + 60.0 * (j + 0.5) / H + 0.5); + for (int j = 0; j < CH; j++) + for (int i = 0; i < CW; i++) { + u[j * CW + i] = (uint8_t)(40 + 150.0 * (i + 0.5) / CW + 0.5); + v[j * CW + i] = (uint8_t)(220 - 150.0 * (j + 0.5) / CH + 0.5); + } + const uint8_t *plane[3] = {y, u, v}; + const int stride[3] = {W, CW, CW}; + FIT_CHECK(!movie_fit_new(W, H, 1920, 1080) && !movie_fit_new(W, H, 2560, 1440), "16:9 outputs need no plan"); + static const unsigned outs[][2] = {{5120, 1440}, {3440, 1440}, {1280, 800}, {1920, 1200}}; + for (int k = 0; k < 4; k++) { + unsigned ow = outs[k][0], oh = outs[k][1]; + f = movie_fit_new(W, H, ow, oh); + FIT_CHECK(f, "%ux%u: no plan", ow, oh); + memset(out, 0xAA, (size_t)W * H * 3 / 2); // a byte nothing else produces: every pixel must be written + movie_fit_apply(f, plane, stride, out); + const uint8_t *uv = out + (size_t)W * H; + int l = 0, r = W - 1, t = 0, b = H - 1; // the picture's extent: where luma leaves 16, on the middle row and column + while (l < W && out[(H / 2) * W + l] == 16) l++; + while (r >= 0 && out[(H / 2) * W + r] == 16) r--; + while (t < H && out[t * W + W / 2] == 16) t++; + while (b >= 0 && out[b * W + W / 2] == 16) b--; + int pw = r - l + 1, ph = b - t + 1; + FIT_CHECK(pw > 2 && ph > 2, "%ux%u: no picture found", ow, oh); + FIT_CHECK(abs(l - (W - 1 - r)) <= 2 && abs(t - (H - 1 - b)) <= 2, + "%ux%u: bars not symmetric: left %d right %d, top %d bottom %d", ow, oh, l, W - 1 - r, t, H - 1 - b); + // On screen the picture covers pw/W of the width and ph/H of the height of an ow x oh canvas; 1 = the movie's shape. + double shape = ((double)pw / W * ow) / ((double)ph / H * oh) / ((double)W / H); + FIT_CHECK(fabs(shape - 1) < 0.01, "%ux%u: picture %dx%d shows at %.3f of the movie's shape", ow, oh, pw, ph, shape); + FIT_CHECK(l == 0 || ph == H, "%ux%u: sideways bars with a squeezed height", ow, oh); + for (int j = 0; j < H; j++) // everything outside the picture is black in luma and chroma, left and right alike + for (int i = 0; i < W; i++) + if (j < t || j > b || i < l || i > r) + FIT_CHECK(out[j * W + i] == 16, "%ux%u: luma (%d,%d) is %d, not black", ow, oh, i, j, out[j * W + i]); + for (int j = 0; j < CH; j++) + for (int i = 0; i < CW; i++) { + int outside = 2 * j + 1 < t || 2 * j > b || 2 * i + 1 < l || 2 * i > r; + const uint8_t *c = uv + (size_t)j * W + 2 * i; + if (outside) FIT_CHECK(c[0] == 128 && c[1] == 128, "%ux%u: chroma (%d,%d) is %d/%d, not black", ow, oh, i, j, + c[0], c[1]); + } + static const double fr[] = {0.1, 0.35, 0.5, 0.8, 0.95}; // samples of the picture, as fractions of it + for (int a = 0; a < 5; a++) + for (int c = 0; c < 5; c++) { + int px = (int)(pw * fr[a]), py = (int)(ph * fr[c]); // picture pixel + double sx = (px + 0.5) * W / pw, sy = (py + 0.5) * H / ph; // where it comes from in the source + double ey = 32 + 150.0 * sx / W + 60.0 * sy / H; + int got = out[(t + py) * W + l + px]; + FIT_CHECK(fabs(got - ey) <= 2, "%ux%u: luma at picture (%d,%d) is %d, want %.1f", ow, oh, px, py, got, ey); + int cx = px / 2, cy = py / 2, cpw = pw / 2, cph = ph / 2; + double sux = (cx + 0.5) * CW / cpw, svy = (cy + 0.5) * CH / cph; + const uint8_t *q = uv + (size_t)(t / 2 + cy) * W + 2 * (l / 2 + cx); + double eu = 40 + 150.0 * sux / CW, ev = 220 - 150.0 * svy / CH; + FIT_CHECK(fabs(q[0] - eu) <= 3 && fabs(q[1] - ev) <= 3, + "%ux%u: chroma at picture (%d,%d) is U %d V %d, want U %.1f V %.1f", ow, oh, cx, cy, q[0], q[1], eu, + ev); + } + if (cfg_get("TOTK_PROBE_MOVIE_BENCH")) { + uint64_t t0 = plat_now_ns(); + for (int i = 0; i < 100; i++) movie_fit_apply(f, plane, stride, out); + printf("probe-movie: fit %ux%u: %.2f ms per frame\n", ow, oh, (plat_now_ns() - t0) / 1e8); + } + printf("probe-movie: fit %ux%u: picture %dx%d at (%d,%d) of the frame, bars Y 16 / UV 128, gradients as expected\n", + ow, oh, pw, ph, l, t); + movie_fit_free(f); + f = NULL; + } +done: + movie_fit_free(f); + free(y), free(u), free(v), free(out); + return rc; +} + int probe_movie(void) { + if (check_fit()) return 1; const char *dir = rt_config()->romfs; char image[PLAT_PATH_MAX]; snprintf(image, sizeof image, "%s/romfs.bin", dir); diff --git a/runtime/ov_movie.c b/runtime/ov_movie.c index acf52586..10599d31 100644 --- a/runtime/ov_movie.c +++ b/runtime/ov_movie.c @@ -73,6 +73,7 @@ typedef struct { unsigned owed; // events to signal on the next guest thread (bit per MOVIE_EV_*) uint64_t t0; // host time of the first frame handed over (pacing) int64_t pts0; // its presentation time (us) + MovieFit *fit; // picture shrunk inside the frame; NULL = the shapes agree, copy MovieStats st; PlatMutex mu; PlatCond work, idle; @@ -207,6 +208,173 @@ static void to_nv12(const vpx_image_t *img, uint8_t *dst, int w, int h) { } } +// ---- Keeping the movie's shape. The game draws a decoded frame over the whole canvas, so on an output that is not 16:9 +// (output_res in totk.ini) the 1920x1080 movies would be stretched. The frame keeps its size; the picture is shrunk +// inside it and the rest is black, so that after the game's stretch the picture has the movie's own shape again: a wider +// output gets a squeezed picture with black at the left and right, a taller one (16:10) black above and below. +// The shrink is a box filter (each output pixel averages the source pixels it covers, weights in 1/4096), separable, with +// the per-column and per-row source spans and weights built once per movie (movie_fit_new), so the frame loop only +// multiplies and adds. +enum { FIT_ONE = 4096, FIT_Y_BLACK = 16, FIT_UV_BLACK = 128 }; // black of limited-range video: Y 16, U = V 128 +typedef struct { int n, taps; int *start; uint16_t *wt; } FitAxis; // output i: taps weights from source start[i] +struct MovieFit { + int w, h, x0, y0, pw, ph; // frame, and the picture's corner and size in it (all even) + FitAxis lx, ly, cx, cy; // luma, chroma; columns, rows; n = 0 where the axis is kept + uint8_t *tmp; // horizontally shrunk rows, before the vertical pass + uint32_t *acc; // one row of the vertical sum +}; + +// Box filter from src_n to dst_n (dst_n < src_n): output i covers [i, i + 1) * src_n / dst_n of the source. +static int axis_build(FitAxis *a, int src_n, int dst_n) { + double s = (double)src_n / dst_n; + a->n = dst_n; + a->taps = (int)s + 2 > src_n ? src_n : (int)s + 2; + a->start = malloc((size_t)dst_n * sizeof *a->start); + a->wt = malloc((size_t)dst_n * (size_t)a->taps * sizeof *a->wt); + if (!a->start || !a->wt) return 0; + for (int i = 0; i < dst_n; i++) { + double lo = i * s, hi = (i + 1) * s; + int st = (int)lo; + if (st > src_n - a->taps) st = src_n - a->taps; + a->start[i] = st; + int sum = 0, big = 0; + uint16_t *w = a->wt + (size_t)i * a->taps; + for (int t = 0; t < a->taps; t++) { + double ov = (st + t + 1 < hi ? st + t + 1 : hi) - (st + t > lo ? st + t : lo); + w[t] = ov > 0 ? (uint16_t)(ov / s * FIT_ONE + 0.5) : 0; + sum += w[t]; + if (w[t] > w[big]) big = t; + } + w[big] = (uint16_t)(w[big] + FIT_ONE - sum); // the weights add up to exactly 1 + } + return 1; +} +static void axis_free(FitAxis *a) { + free(a->start); + free(a->wt); +} + +void movie_fit_free(MovieFit *f) { + if (!f) return; + axis_free(&f->lx), axis_free(&f->ly), axis_free(&f->cx), axis_free(&f->cy); + free(f->tmp); + free(f->acc); + free(f); +} + +// The plan for a w x h movie shown on an ow x oh output; NULL when the shapes agree within 1% (the frame is copied). +MovieFit *movie_fit_new(int w, int h, unsigned ow, unsigned oh) { + if (w < 16 || h < 16 || (w | h) & 1 || !ow || !oh) return NULL; + double movie = (double)w / h, out = (double)ow / oh, r = out / movie; + if (r > 0.99 && r < 1.01) return NULL; + MovieFit *f = calloc(1, sizeof *f); + if (!f) { + rt_log("movie: out of memory for the picture-shape tables; this movie is stretched"); + return NULL; + } + f->w = w, f->h = h, f->pw = w, f->ph = h; + if (r > 1) f->pw = (int)(w / r / 2 + 0.5) * 2; // wider output: squeeze sideways + else f->ph = (int)(h * r / 2 + 0.5) * 2; // taller output: squeeze vertically + if (f->pw < 2) f->pw = 2; + if (f->ph < 2) f->ph = 2; + f->x0 = (w - f->pw) / 2 & ~1; + f->y0 = (h - f->ph) / 2 & ~1; + int ok = 1; + if (f->pw < w) ok = axis_build(&f->lx, w, f->pw) && axis_build(&f->cx, w / 2, f->pw / 2); + if (f->ph < h) ok = ok && axis_build(&f->ly, h, f->ph) && axis_build(&f->cy, h / 2, f->ph / 2); + f->tmp = malloc((size_t)f->pw * h); + f->acc = malloc((size_t)f->pw * sizeof *f->acc); + if (!ok || !f->tmp || !f->acc) { + rt_log("movie: out of memory for the picture-shape tables; this movie is stretched"); + movie_fit_free(f); + return NULL; + } + return f; +} + +// One output row of the horizontal pass: dst gets the shrunk src row (dstep 2 writes every other byte, for U and V). +// The tap count is a constant in the 3 and 4 tap copies (what 2x and below squeezes need), so the compiler unrolls the sum. +static inline __attribute__((always_inline)) void fit_row_taps(const FitAxis *a, const uint8_t *src, uint8_t *dst, + int dstep, int taps) { + for (int i = 0; i < a->n; i++) { + const uint8_t *p = src + a->start[i]; + const uint16_t *w = a->wt + (size_t)i * taps; + uint32_t v = FIT_ONE / 2; + for (int t = 0; t < taps; t++) v += w[t] * p[t]; + dst[(size_t)i * dstep] = (uint8_t)(v / FIT_ONE); + } +} +static void fit_row_h(const FitAxis *a, const uint8_t *src, uint8_t *dst, int dstep) { + if (a->taps == 3) fit_row_taps(a, src, dst, dstep, 3); + else if (a->taps == 4) fit_row_taps(a, src, dst, dstep, 4); + else fit_row_taps(a, src, dst, dstep, a->taps); +} + +// One plane (sw x sh) shrunk into the pw x ph picture whose corner (ox, oy) is in dst; ax/ay NULL = that axis is kept. +static void fit_plane(const MovieFit *f, const uint8_t *src, int ss, int sw, int sh, uint8_t *dst, int ds, int dstep, + int ox, int oy, const FitAxis *ax, const FitAxis *ay, int pw, int ph) { + uint8_t *base = dst + (size_t)oy * ds + (size_t)ox * dstep; + if (!ay) { // sideways only: straight into the frame + for (int y = 0; y < sh; y++) { + const uint8_t *row = src + (size_t)y * ss; + uint8_t *o = base + (size_t)y * ds; + if (ax) fit_row_h(ax, row, o, dstep); + else if (dstep == 1) memcpy(o, row, (size_t)sw); + else for (int x = 0; x < sw; x++) o[(size_t)x * dstep] = row[x]; + } + return; + } + const uint8_t *in = src; + int is = ss; + if (ax) { + for (int y = 0; y < sh; y++) fit_row_h(ax, src + (size_t)y * ss, f->tmp + (size_t)y * pw, 1); + in = f->tmp, is = pw; + } + for (int y = 0; y < ph; y++) { + const uint16_t *w = ay->wt + (size_t)y * ay->taps; + const uint8_t *r0 = in + (size_t)ay->start[y] * is; + uint8_t *o = base + (size_t)y * ds; + if (ay->taps == 3) { // the common case, summed in registers + const uint8_t *r1 = r0 + is, *r2 = r1 + is; + for (int x = 0; x < pw; x++) + o[(size_t)x * dstep] = (uint8_t)((FIT_ONE / 2 + w[0] * r0[x] + w[1] * r1[x] + w[2] * r2[x]) / FIT_ONE); + continue; + } + memset(f->acc, 0, (size_t)pw * sizeof *f->acc); + for (int t = 0; t < ay->taps; t++) { + const uint8_t *row = r0 + (size_t)t * is; + uint32_t k = w[t]; + for (int x = 0; x < pw; x++) f->acc[x] += k * row[x]; + } + for (int x = 0; x < pw; x++) o[(size_t)x * dstep] = (uint8_t)((f->acc[x] + FIT_ONE / 2) / FIT_ONE); + } +} + +// I420 planes -> packed NV12 with the picture shrunk inside black bars: w x h of Y, then w x h/2 of interleaved U, V. +void movie_fit_apply(const MovieFit *f, const uint8_t *const plane[3], const int stride[3], uint8_t *dst) { + int w = f->w, h = f->h, cw = w / 2, ch = h / 2; + uint8_t *uv = dst + (size_t)w * h; + // Black only where the picture does not go: whole rows above and below it, the strips left and right of it. + int rx = f->x0 + f->pw, ry = f->y0 + f->ph; + memset(dst, FIT_Y_BLACK, (size_t)f->y0 * w); + memset(dst + (size_t)ry * w, FIT_Y_BLACK, (size_t)(h - ry) * w); + memset(uv, FIT_UV_BLACK, (size_t)(f->y0 / 2) * w); + memset(uv + (size_t)(ry / 2) * w, FIT_UV_BLACK, (size_t)(ch - ry / 2) * w); + for (int y = f->y0; y < ry; y++) { + memset(dst + (size_t)y * w, FIT_Y_BLACK, (size_t)f->x0); + memset(dst + (size_t)y * w + rx, FIT_Y_BLACK, (size_t)(w - rx)); + } + for (int y = f->y0 / 2; y < ry / 2; y++) { + memset(uv + (size_t)y * w, FIT_UV_BLACK, (size_t)f->x0); + memset(uv + (size_t)y * w + rx, FIT_UV_BLACK, (size_t)(w - rx)); + } + fit_plane(f, plane[0], stride[0], w, h, dst, w, 1, f->x0, f->y0, f->lx.n ? &f->lx : NULL, f->ly.n ? &f->ly : NULL, + f->pw, f->ph); + for (int c = 0; c < 2; c++) + fit_plane(f, plane[1 + c], stride[1 + c], cw, ch, uv + c, w, 2, f->x0 / 2, f->y0 / 2, + f->cx.n ? &f->cx : NULL, f->cy.n ? &f->cy : NULL, f->pw / 2, f->ph / 2); +} + static int free_out(const Movie *m) { for (int i = 0; i < OUT_BUFS; i++) if (!m->out_held[i]) return i; @@ -250,7 +418,9 @@ static void worker(void *arg) { slot = free_out(m); m->out_held[slot] = 1; // reserved: in no queue, so the game cannot release it } - uint64_t gen = m->gen, dt = 0; + uint64_t gen = m->gen, dt = 0, fit_ns = 0; + // A plan made for another frame size (a start that found the worker busy) is not used: it would overrun dst. + const MovieFit *fit = m->fit && m->fit->w == m->w && m->fit->h == m->h ? m->fit : NULL; int got = 0, w = m->w, h = m->h; uint8_t *dst = slot >= 0 ? m->out[slot] : NULL; vpx_codec_err_t e = VPX_CODEC_OK; @@ -265,7 +435,12 @@ static void worker(void *arg) { const vpx_image_t *img; while ((img = vpx_codec_get_frame(&m->vpx, &it)) != NULL) { if (img->fmt != VPX_IMG_FMT_I420 || (int)img->d_w != w || (int)img->d_h != h) bad = "layout"; - else if (!got) to_nv12(img, dst, w, h); + else if (!got) { + uint64_t t1 = kern_now_ns(); + if (fit) movie_fit_apply(fit, (const uint8_t *const *)img->planes, img->stride, dst); + else to_nv12(img, dst, w, h); + fit_ns = kern_now_ns() - t1; + } got++; } } @@ -279,6 +454,7 @@ static void worker(void *arg) { m->owed |= 1u << MOVIE_EV_INPUT; if (p.size > 0) { m->st.decode_ns += dt; + m->st.fit_ns += fit_ns; if (dt > m->st.max_ns) m->st.max_ns = dt; if (bad) rt_fatal("movie: VP9 frame layout differs from the configured %dx%d I420", w, h); if (e != VPX_CODEC_OK || !got) { @@ -302,8 +478,10 @@ static void worker(void *arg) { push_decoded(m, slot, 0, p.time, frame_bytes(m)); m->st.frames++; if (m->st.frames % 600 == 0) - rt_log("movie: %llu frames, decode %.2f ms per frame (max %.2f)", (unsigned long long)m->st.frames, - m->st.decode_ns / 1e6 / (double)m->st.frames, m->st.max_ns / 1e6); + rt_log("movie: %llu frames, decode %.2f ms per frame (max %.2f), of it %.2f ms %s", + (unsigned long long)m->st.frames, m->st.decode_ns / 1e6 / (double)m->st.frames, + m->st.max_ns / 1e6, m->st.fit_ns / 1e6 / (double)m->st.frames, + m->fit ? "fitting the picture" : "converting"); } } if (p.flags & MOVIE_FLAG_EOS && !m->eos_in) { @@ -355,6 +533,8 @@ static void release_codec(Movie *m) { // m->mu held, worker quiesced free(m->out[i]); m->out[i] = NULL; } + movie_fit_free(m->fit); + m->fit = NULL; m->configured = m->started = 0; reset_queues(m); } @@ -597,6 +777,18 @@ static void ov_start(Cpu *c) { plat_mutex_lock(&m->mu); int st = ST_NOT_CONFIGURED; if (m->configured) { + if (!m->started && !m->busy) { // the worker is idle: it reads m->fit only when it takes an input + movie_fit_free(m->fit); + m->fit = NULL; + unsigned out[2]; + res_wanted(out); + if (strcmp(cfg_value("TOTK_MOVIE_ASPECT"), "stretch")) { + m->fit = movie_fit_new(m->w, m->h, out[0], out[1]); + if (m->fit) + rt_log("movie: %dx%d picture kept in shape on the %ux%u output (%dx%d inside the frame)", m->w, + m->h, out[0], out[1], m->fit->pw, m->fit->ph); + } + } m->started = 1; m->owed |= 1u << MOVIE_EV_INPUT; plat_cond_signal(&m->work); diff --git a/runtime/restbl.c b/runtime/restbl.c index 7436ddd1..3530f1d7 100644 --- a/runtime/restbl.c +++ b/runtime/restbl.c @@ -234,3 +234,102 @@ uint32_t restbl_grow(const char *key, uint32_t entry, uint64_t old, uint64_t new uint64_t e = entry + ((new_size - old) * f + 999) / 1000; return clamp32((e + 31) & ~(uint64_t)31); } + +// ---- packs +// SARC: "SARC", u16 header size, u16 byte order mark 0xfeff, u32 archive size, u32 data offset, u16 version, u16 pad; +// SFAT: "SFAT", u16 header size, u16 node count, u32 hash key, then per node {u32 name hash, u32 attributes (bit 24: it +// has a name, bits 0-23: the name's offset in SFNT / 4), u32 data start, u32 data end} with the data offsets relative +// to the archive's data offset; SFNT: "SFNT", u16 header size, u16 pad, then the NUL-terminated names. +static uint32_t rd16(const uint8_t *p) { return p[0] | p[1] << 8; } + +int restbl_sarc_each(const uint8_t *p, size_t n, RestblMember *each, void *ctx) { + if (n < 0x14 || memcmp(p, "SARC", 4) || rd16(p + 6) != 0xfeff) return 0; + size_t head = rd16(p + 4), data = rd32(p + 12); + if (head + 12 > n || memcmp(p + head, "SFAT", 4) || data > n) return 0; + size_t count = rd16(p + head + 6), nodes = head + rd16(p + head + 4), names = nodes + count * 16; + if (names + 8 > n || memcmp(p + names, "SFNT", 4) || rd16(p + names + 4) < 8) return 0; + names += rd16(p + names + 4); + for (size_t i = 0; i < count; i++) { + const uint8_t *e = p + nodes + i * 16; + uint32_t attr = rd32(e + 4), lo = rd32(e + 8), hi = rd32(e + 12); + if (!(attr >> 24)) continue; // without a name: nothing the game looks up by path + size_t s = names + (size_t)(attr & 0xffffff) * 4; + if (s >= n || !memchr(p + s, 0, n - s) || lo > hi || hi > n - data) return 0; + each((const char *)p + s, p + data + lo, hi - lo, ctx); + } + return 1; +} + +// The decompressed size a zstd frame p[0..n) records (Frame_Content_Size), or 4 times n when it records none, as +// romfs.c content_size takes a loose file's. +static uint64_t frame_content_size(const uint8_t *p, size_t n) { + static const uint8_t did_bytes[4] = {0, 1, 2, 4}; + if (n < 6 || rd32(p) != 0xFD2FB528u) return n; + uint8_t fhd = p[4]; + int single = fhd >> 5 & 1, fcs = fhd >> 6; + size_t at = 5 + !single + did_bytes[fhd & 3], len = fcs ? 1u << fcs : single; + if (!len || at + len > n) return (uint64_t)n * 4; + uint64_t v = 0; + for (size_t i = 0; i < len; i++) v |= (uint64_t)p[at + i] << 8 * i; + return len == 2 ? v + 256 : v; +} + +typedef struct { const char *name; uint64_t size; } Member; +typedef struct { Member *m; size_t n, cap; int full; } Members; + +static int ends_with(const char *s, const char *tail) { + size_t n = strlen(s), k = strlen(tail); + return n >= k && !strcmp(s + n - k, tail); +} + +// A member's size as the game unpacks it: a .zs member's content size, else its bytes (a .mc member's compressed ones). +static void member_add(const char *name, const uint8_t *data, size_t size, void *ctx) { + Members *ms = ctx; + if (ms->n == ms->cap) { + size_t cap = ms->cap ? ms->cap * 2 : 64; + Member *grown = realloc(ms->m, cap * sizeof *grown); + if (!grown) { + ms->full = 1; + return; + } + ms->m = grown, ms->cap = cap; + } + ms->m[ms->n++] = (Member){name, ends_with(name, ".zs") ? frame_content_size(data, size) : size}; +} + +static int by_name(const void *a, const void *b) { + return strcmp(((const Member *)a)->name, ((const Member *)b)->name); +} + +int restbl_pack_raise(Restbl *t, const Restbl *base, const uint8_t *mod, size_t mod_n, const uint8_t *orig, + size_t orig_n, RestblNote *note, void *ctx) { + Members m = {0}, o = {0}; + int raised = -1; + if (!restbl_sarc_each(mod, mod_n, member_add, &m) || m.full || + (orig && (!restbl_sarc_each(orig, orig_n, member_add, &o) || o.full))) + goto done; + if (o.n) qsort(o.m, o.n, sizeof *o.m, by_name); + raised = 0; + for (size_t i = 0; i < m.n; i++) { + char key[1024]; + uint32_t entry, before = 0, want; + restbl_key(m.m[i].name, key, sizeof key); + int mc = ends_with(m.m[i].name, ".mc"); + const Member *was = o.n ? bsearch(&m.m[i], o.m, o.n, sizeof *o.m, by_name) : NULL; + if (was) { + if (!restbl_get(base, key, &entry)) continue; // the game sizes it without the table (streamed) + want = restbl_grow(key, entry, was->size, m.m[i].size, mc); + } else { // new to this pack: no copy of the game's to grow from + want = restbl_estimate(key, mc ? m.m[i].size * 8 : m.m[i].size); + } + restbl_get(t, key, &before); + if (want && restbl_raise(t, key, want)) { + raised++; + if (note) note(key, before, want, ctx); + } + } +done: + free(m.m); + free(o.m); + return raised; +} diff --git a/runtime/restbl.h b/runtime/restbl.h index 430480f5..359d1713 100644 --- a/runtime/restbl.h +++ b/runtime/restbl.h @@ -31,3 +31,20 @@ uint32_t restbl_estimate(const char *key, uint64_t size); // times the type's factor, never lowered. scaled: the sizes are compressed ones (a mesh codec .mc file), whose entry // grows in proportion. uint32_t restbl_grow(const char *key, uint32_t entry, uint64_t old, uint64_t new_size, int scaled); + +// The members of the SARC archive p[0..n): each(name, data, size, ctx) for every named member, in the archive's order. +// 0 when p is not a SARC archive or a member lies outside it. +typedef void RestblMember(const char *name, const uint8_t *data, size_t size, void *ctx); +int restbl_sarc_each(const uint8_t *p, size_t n, RestblMember *each, void *ctx); + +// A pack (Pack/**/*.pack.zs, decompressed: a SARC archive) whose members the game loads as resources of their own, each +// sized by its own entry. The entries of a mod's pack `mod` raised in t against the game's table `base` and the game's +// copy of the pack `orig` (NULL for a pack the game lacks): a member that grew by restbl_grow, a member the game's copy +// lacks by restbl_estimate, the others left as they are. note(key, entry before, entry after, ctx) for each raised +// entry. The number raised; -1 when either pack is not a SARC archive. +typedef void RestblNote(const char *key, uint32_t was, uint32_t now, void *ctx); +int restbl_pack_raise(Restbl *t, const Restbl *base, const uint8_t *mod, size_t mod_n, const uint8_t *orig, + size_t orig_n, RestblNote *note, void *ctx); + +// restbl_probe.c, part of --probe-mods: the number of failed checks. +int restbl_probe(void); diff --git a/runtime/restbl_probe.c b/runtime/restbl_probe.c new file mode 100644 index 00000000..3fdd9b17 --- /dev/null +++ b/runtime/restbl_probe.c @@ -0,0 +1,133 @@ +// The entries of the members of the mods' packs (restbl.c restbl_pack_raise), on archives and tables of its own; part +// of --probe-mods (mods_probe.c). Needs no game files. +#include +#include +#include + +#include "restbl.h" + +static int fails; +#define CHECK(c, what) do { if (!(c)) { fails++; fprintf(stderr, "FAIL restbl: %s\n", what); } } while (0) + +// A member of a probe archive: its name and size; a .zs member is a zstd frame header recording `content` bytes: +// fcs 4, a frame with a window descriptor and a 4-byte field; 2, a single-segment frame with a 2-byte field holding +// content - 256. +typedef struct { const char *name; size_t size, content; int fcs; } Member; + +static void wr16(uint8_t *p, unsigned v) { p[0] = (uint8_t)v, p[1] = (uint8_t)(v >> 8); } +static void wr32(uint8_t *p, uint32_t v) { wr16(p, v & 0xffff), wr16(p + 2, v >> 16); } + +// A SARC archive of the members m[0..n); unnamed: the index of a member without a name, -1 for none. malloc'd. +static uint8_t *sarc(const Member *m, int n, int unnamed, size_t *out_n) { + size_t names = 0, data = 0; + for (int i = 0; i < n; i++) names += (strlen(m[i].name) + 4) & ~(size_t)3, data += (m[i].size + 7) & ~(size_t)7; + size_t sfat = 0x14, sfnt = sfat + 12 + 16 * (size_t)n, start = (sfnt + 8 + names + 15) & ~(size_t)15; + size_t total = start + data; + uint8_t *p = calloc(1, total); + if (!p) return NULL; + memcpy(p, "SARC", 4), wr16(p + 4, 0x14), wr16(p + 6, 0xfeff); + wr32(p + 8, (uint32_t)total), wr32(p + 12, (uint32_t)start); + memcpy(p + sfat, "SFAT", 4), wr16(p + sfat + 4, 12), wr16(p + sfat + 6, (unsigned)n), wr32(p + sfat + 8, 0x65); + memcpy(p + sfnt, "SFNT", 4), wr16(p + sfnt + 4, 8); + size_t name_at = 0, data_at = 0; + for (int i = 0; i < n; i++) { + uint8_t *node = p + sfat + 12 + 16 * (size_t)i, *d = p + start + data_at; + wr32(node + 4, i == unnamed ? 0 : 0x01000000u | (uint32_t)(name_at / 4)); + wr32(node + 8, (uint32_t)data_at), wr32(node + 12, (uint32_t)(data_at + m[i].size)); + memcpy(p + sfnt + 8 + name_at, m[i].name, strlen(m[i].name)); + memset(d, 0x5a, m[i].size); + if (m[i].fcs == 4) wr32(d, 0xFD2FB528u), d[4] = 0x80, d[5] = 0x58, wr32(d + 6, (uint32_t)m[i].content); + if (m[i].fcs == 2) wr32(d, 0xFD2FB528u), d[4] = 0x60, wr16(d + 5, (unsigned)(m[i].content - 256)); + name_at += (strlen(m[i].name) + 4) & ~(size_t)3; + data_at += (m[i].size + 7) & ~(size_t)7; + } + *out_n = total; + return p; +} + +// An empty table with the entries keys[i] = sizes[i]. +static Restbl *table(const char *const *keys, const uint32_t *sizes, int n) { + uint8_t head[22] = "RESTBL"; + wr32(head + 6, 1), wr32(head + 10, 160), wr32(head + 14, 0), wr32(head + 18, 0); + Restbl *t = restbl_parse(head, sizeof head); + for (int i = 0; t && i < n; i++) restbl_raise(t, keys[i], sizes[i]); + return t; +} + +static uint32_t get(const Restbl *t, const char *key) { + uint32_t v = 0; + return restbl_get(t, key, &v) ? v : 0; +} + +static int visited; +static void count(const char *name, const uint8_t *data, size_t size, void *ctx) { + (void)name, (void)data, (void)size, (void)ctx; + visited++; +} + +static int notes; +static void note(const char *key, uint32_t was, uint32_t now, void *ctx) { + (void)key, (void)ctx; + notes += now > was; +} + +int restbl_probe(void) { + fails = 0; + static const char *const keys[] = {"AI/Grow.root.ainb", "AI/Same.root.ainb", "AI/Shrink.root.ainb", + "Component/Shared.bgyml", "Model/Mesh.bfres", "AI/Zs.ainb", "AI/Zs2.ainb"}; + static const uint32_t sizes[] = {13512, 5000, 6000, 9000, 30000, 4400, 3500}; + // The game's copy: an .ainb member a mod grows (by 288 bytes, past its entry's slack), one it keeps, one it + // shrinks, one the table does not size, a mesh codec member and two compressed ones. + static const Member orig[] = {{"AI/Grow.root.ainb", 13088, 0, 0}, {"AI/Same.root.ainb", 4600, 0, 0}, + {"AI/Shrink.root.ainb", 5600, 0, 0}, {"AI/NotSized.ainb", 100, 0, 0}, + {"Model/Mesh.bfres.mc", 1000, 0, 0}, {"AI/Zs.ainb.zs", 64, 4000, 4}, + {"AI/Zs2.ainb.zs", 64, 3900, 2}}; + // The mod's copy: those, plus members new to the pack (one the game sizes elsewhere, one it lacks, a streamed one). + static const Member mod[] = {{"AI/Grow.root.ainb", 13376, 0, 0}, {"AI/Same.root.ainb", 4600, 0, 0}, + {"AI/Shrink.root.ainb", 5000, 0, 0}, {"AI/NotSized.ainb", 300, 0, 0}, + {"Component/New/X.bgyml", 1000, 0, 0}, {"Component/Shared.bgyml", 2000, 0, 0}, + {"Sound/Stream.bwav", 500, 0, 0}, {"Model/Mesh.bfres.mc", 1500, 0, 0}, + {"AI/Zs.ainb.zs", 64, 4500, 4}, {"AI/Zs2.ainb.zs", 64, 4200, 2}}; + size_t on = 0, mn = 0; + uint8_t *op = sarc(orig, 7, -1, &on), *mp = sarc(mod, 10, 4, &mn); // the mod's New/X has no name: left alone + Restbl *base = table(keys, sizes, 7), *t = table(keys, sizes, 7); + CHECK(op && mp && base && t, "probe archives and tables"); + if (op && mp && base && t) { + visited = 0; + CHECK(restbl_sarc_each(mp, mn, count, NULL) && visited == 9, "SARC members: every named one"); + int r = restbl_pack_raise(t, base, mp, mn, op, on, note, NULL); + CHECK(r == 5 && notes == 5, "five entries raised, each noted"); + CHECK(get(t, "AI/Grow.root.ainb") == 13824, "a member that grew: its entry plus the growth, 32-byte steps"); + CHECK(get(t, "AI/Same.root.ainb") == 5000 && get(t, "AI/Shrink.root.ainb") == 6000, + "members that kept or lost bytes keep their entries"); + CHECK(!get(t, "AI/NotSized.ainb"), "a member the game sizes without the table gets no entry"); + CHECK(!get(t, "Component/New/X.bgyml"), "a member without a name is not looked up"); + CHECK(get(t, "Component/Shared.bgyml") == 16384, "a member new to the pack: the estimate of its type"); + CHECK(!get(t, "Sound/Stream.bwav"), "a streamed member new to the pack gets no entry"); + CHECK(get(t, "Model/Mesh.bfres") == 45000, "a mesh codec member's entry grows in proportion"); + CHECK(get(t, "AI/Zs.ainb") == 4928 && get(t, "AI/Zs2.ainb") == 3808, + "a compressed member: the growth of the size its frame records (4- and 2-byte fields)"); + notes = 0; + CHECK(restbl_pack_raise(t, base, mp, mn, op, on, note, NULL) == 0 && !notes, "a second pass raises nothing"); + Restbl *shipped = table(keys, (const uint32_t[]){20000, 5000, 6000, 9000, 30000, 4400, 3500}, 7); + CHECK(shipped && restbl_pack_raise(shipped, base, mp, mn, op, on, NULL, NULL) == 4 && + get(shipped, "AI/Grow.root.ainb") == 20000, + "an entry a mod's own table raised further stays"); + restbl_free(shipped); + Restbl *added = table(keys, sizes, 7); + CHECK(added && restbl_pack_raise(added, base, mp, mn, NULL, 0, NULL, NULL) == 7 && + get(added, "AI/Grow.root.ainb") == 24576 && get(added, "AI/NotSized.ainb") == 12288 && + get(added, "AI/Zs2.ainb") == 16384 && get(added, "Model/Mesh.bfres") == 30000, + "a pack the game lacks: every member gets the estimate of its type, an entry above it stays"); + restbl_free(added); + CHECK(restbl_pack_raise(t, base, (const uint8_t *)"SARC", 4, NULL, 0, NULL, NULL) == -1 && + restbl_pack_raise(t, base, mp, mn, mp, 0x20, NULL, NULL) == -1, + "an archive that is not one, or whose game copy is cut short, raises nothing"); + } + free(op); + free(mp); + restbl_free(base); + restbl_free(t); + printf("restbl: %s\n", fails ? "FAIL" : "ok"); + return fails; +} diff --git a/runtime/romfs.c b/runtime/romfs.c index 1a0d5b71..c062c4b2 100644 --- a/runtime/romfs.c +++ b/runtime/romfs.c @@ -24,8 +24,8 @@ // matches a RomFS path only without case is served under the RomFS spelling. // // The resource size table: with a player mod enabled, the game's table is rebuilt at start with the tables the mods -// ship merged in and the entries of the mods' files raised to what they need (restbl_overlay), and served from -// /mods/ in place of the game's or a mod's table. +// ship merged in and the entries of the mods' files raised to what they need (restbl_overlay), the members of their +// packs too (restbl_packs), and served from /mods/ in place of the game's or a mod's table. #include #include #include @@ -621,10 +621,122 @@ static Restbl *restbl_unpack(const void *z, size_t n) { return t; } +// ---- the members of the mods' packs (restbl_pack_raise). The game loads each member of a pack (Pack/**/*.pack.zs, a +// SARC archive compressed with a dictionary of Pack/ZsDic.pack.zs) as a resource of its own, in a heap sized by the +// member's own entry, so a mod that changes a member and ships no table for it (or an older one) leaves the game's +// entry. A member that outgrew its entry's slack loads, then its heap runs out: TotK 1.4.3 writes through a null +// pointer at main+0x21794c when the 0x68-byte object an .ainb member builds after its file no longer fits. +#define PACK_MAX (256u << 20) // the largest pack taken, decompressed (AI.Global.Product.140.pack: 24 MiB) +typedef struct { ZSTD_DDict *dict[4]; unsigned id[4]; int n; ZSTD_DCtx *dctx; } Packs; + +// The bytes of zstd frame z[0..n), with the dictionary of ps its header names; malloc'd. NULL when it is not such a +// frame with a recorded size of at most PACK_MAX. +static uint8_t *pack_unpack(Packs *ps, const void *z, size_t n, size_t *out_n) { + unsigned long long raw = ZSTD_getFrameContentSize(z, n); + unsigned id = ZSTD_getDictID_fromFrame(z, n); + ZSTD_DDict *dd = NULL; + for (int i = 0; i < ps->n; i++) + if (ps->id[i] == id) dd = ps->dict[i]; + if (raw == ZSTD_CONTENTSIZE_UNKNOWN || raw == ZSTD_CONTENTSIZE_ERROR || raw > PACK_MAX || (id && !dd) || !ps->dctx) + return NULL; + uint8_t *d = malloc(raw ? (size_t)raw : 1); + size_t got = !d ? 0 : dd ? ZSTD_decompress_usingDDict(ps->dctx, d, (size_t)raw, z, n, dd) + : ZSTD_decompressDCtx(ps->dctx, d, (size_t)raw, z, n); + if (!d || ZSTD_isError(got) || got != raw) { + free(d); + return NULL; + } + *out_n = (size_t)raw; + return d; +} + +static void pack_dict_add(const char *name, const uint8_t *data, size_t size, void *ctx) { + Packs *ps = ctx; + ZSTD_DDict *dd = ps->n < 4 && ends_with(name, ".zsdic") ? ZSTD_createDDict(data, size) : NULL; // a copy + if (!dd) return; + ps->dict[ps->n] = dd; + ps->id[ps->n++] = ZSTD_getDictID_fromDDict(dd); +} + +// The dictionaries of Pack/ZsDic.pack.zs as served (zs.zsdic, pack.zsdic, bcett.byml.zsdic): a SARC compressed +// without one. 0 when it holds none. +static int packs_open(Packs *ps) { + Overlay *base; + size_t n = 0, rn = 0; + ps->dctx = ZSTD_createDCtx(); + void *z = read_served("Pack/ZsDic.pack.zs", &base, &n, "the members of the mods' packs keep their entries"); + uint8_t *raw = z ? pack_unpack(ps, z, n, &rn) : NULL; + int ok = raw && restbl_sarc_each(raw, rn, pack_dict_add, ps) && ps->n; + free(z); + free(raw); + return ok; +} + +static void packs_close(Packs *ps) { + for (int i = 0; i < ps->n; i++) ZSTD_freeDDict(ps->dict[i]); + ZSTD_freeDCtx(ps->dctx); +} + +// Overlay o (a pack) and the game's copy of it, decompressed; NULL when o cannot be read or unpacked. *orig stays NULL +// for a pack the game lacks or whose copy cannot be read. +static uint8_t *pack_pair(Packs *ps, const Overlay *o, size_t *n, uint8_t **orig, size_t *orig_n) { + uint64_t zn = 0; + void *z = plat_file_read(o->path, &zn); + uint8_t *mod = z ? pack_unpack(ps, z, (size_t)zn, n) : NULL; + free(z); + *orig = NULL; + if (!mod || o->index == ADDED) return mod; + Entry e = entry(o->index); + void *g = e.size < READ_CHUNK ? malloc(e.size ? (size_t)e.size : 1) : NULL; + if (g && read_at(image, g, (uint32_t)e.size, e.offset) == (int64_t)e.size) + *orig = pack_unpack(ps, g, (size_t)e.size, orig_n); + free(g); + return mod; +} + +typedef struct { const char *pack, *mod; int lines; } PackNote; + +static void pack_note(const char *key, uint32_t was, uint32_t now, void *ctx) { + PackNote *pn = ctx; + if (pn->lines++ < 16) + rt_log("RomFS: resource size table: %s in %s (%s): entry %u -> %u", key, pn->pack, pn->mod, was, now); +} + +// The entries of the members of every pack a mod serves, raised in out against the game's table base; the number +// raised, *packs the number of packs with one. +static int restbl_packs(Restbl *out, const Restbl *base, int *packs, int *lines) { + Packs ps = {0}; + int raised = 0, opened = 0, bad = 0; + *packs = 0; + for (size_t k = 0; k < noverlay; k++) { + const Overlay *o = &overlays[k]; + if ((!o->mod && o->prio != 1) || !ends_with(o->name, ".pack.zs")) continue; + if (!opened++ && !packs_open(&ps)) { + rt_log("RomFS: no zstd dictionary in Pack/ZsDic.pack.zs; the members of mods' packs keep their entries"); + break; + } + // A pack whose game copy cannot be read counts as new: each member gets an estimate, which only raises entries. + size_t n = 0, orig_n = 0; + uint8_t *orig, *mod = pack_pair(&ps, o, &n, &orig, &orig_n); + PackNote pn = {o->name, owner_name(o), *lines}; + int r = mod ? restbl_pack_raise(out, base, mod, n, orig, orig_n, pack_note, &pn) : -1; + if (r < 0 && bad++ < 4) rt_log("RomFS: %s (%s) is not a pack this build reads; its members keep their entries", + o->name, owner_name(o)); + *lines = pn.lines; + raised += r > 0 ? r : 0; + *packs += r > 0; + free(mod); + free(orig); + } + packs_close(&ps); + return raised; +} + // With a player mod (or the TOTK_ROMFS_MODS manifest) serving files: the game's table with every shipped table of this // version merged in (an entry larger than the game's wins), the entries a mod's table for another version gives for // that mod's files, and each served mod file's entry raised to what its size needs (restbl_grow for a file the game -// has, restbl_estimate for an added one). Never lowers the game's entries. +// has, restbl_estimate for an added one), the members of the packs mods serve too (restbl_packs). Never lowers the +// game's entries. static void restbl_overlay(void) { int player = 0; for (size_t k = 0; k < noverlay; k++) player |= overlays[k].mod || overlays[k].prio == 1; @@ -691,8 +803,10 @@ static void restbl_overlay(void) { } raised += want && restbl_raise(out, key, want); } + int packs = 0, lines = 0, members = restbl_packs(out, base, &packs, &lines); + if (lines > 16) rt_log("RomFS: resource size table: %d more entries of pack members raised", lines - 16); restbl_free(base); - if (!merged && !raised && !served) { + if (!merged && !raised && !members && !served) { restbl_free(out); rt_log("RomFS: resource size table: the mods' files fit the game's entries (%.0f ms)", (now_s() - t0) * 1000); return; @@ -711,7 +825,8 @@ static void restbl_overlay(void) { } if (serve_generated(restbl_rel, served, z, zn, "mods", "merged resource sizes", keeps)) rt_log("RomFS: resource size table: %d tables merged (%d entries), %d entries raised for mod files (%d " - "added files estimated), %.0f ms", tables, merged, raised, estimated, (now_s() - t0) * 1000); + "added files estimated), %d for members of %d mod packs, %.0f ms", tables, merged, raised, estimated, + members, packs, (now_s() - t0) * 1000); } // The HeapDef table a bigger game resolution needs (resolution.c): its texture heap and that heap's parent grow. The diff --git a/runtime/ui/menu.cpp b/runtime/ui/menu.cpp index b8468bae..d7a7a32f 100644 --- a/runtime/ui/menu.cpp +++ b/runtime/ui/menu.cpp @@ -668,6 +668,8 @@ static void tab_screen(float scale) { opt_slider("TOTK_SHARPNESS", UI_MENU_SHARPNESS, 0, 100, UI_MENU_SHARPNESS_DESC, 5, "%.0f %%"); static const UiTextId aspect[] = {UI_MENU_ASPECT_FIT, UI_MENU_ASPECT_STRETCH}; opt_choice("TOTK_ASPECT", UI_MENU_ASPECT, aspect, 2, UI_MENU_ASPECT_DESC); + static const UiTextId movie_aspect[] = {UI_MENU_MOVIE_ASPECT_KEEP, UI_MENU_MOVIE_ASPECT_STRETCH}; + opt_choice("TOTK_MOVIE_ASPECT", UI_MENU_MOVIE_ASPECT, movie_aspect, 2, UI_MENU_MOVIE_ASPECT_DESC); opt_slider("TOTK_FOV", UI_MENU_FOV, 70, 140, UI_MENU_FOV_DESC, 5, "%.0f %%"); static const char *const aniso[] = {"game", "2", "4", "8", "16"}; const char *aniso_names[] = {menu_text(UI_MENU_ANISO_GAME), "2x", "4x", "8x", "16x"}; diff --git a/runtime/ui_text.c b/runtime/ui_text.c index 3c2a2756..cf3cad13 100644 --- a/runtime/ui_text.c +++ b/runtime/ui_text.c @@ -79,6 +79,10 @@ static const char *const text_en[UI_TEXT_COUNT] = { [UI_MENU_ASPECT_DESC] = "When the window's shape differs from the game resolution's (a 16:9 picture on a 21:9 " "screen): keep the picture's shape with black bars, or stretch it over the window.", [UI_MENU_ASPECT_FIT] = "Black bars", [UI_MENU_ASPECT_STRETCH] = "Stretch", + [UI_MENU_MOVIE_ASPECT] = "Movie shape", + [UI_MENU_MOVIE_ASPECT_DESC] = "When the game resolution is not 16:9, keep the cutscene movies' own shape with black " + "bars, or stretch them over the whole picture. Applies from the next movie.", + [UI_MENU_MOVIE_ASPECT_KEEP] = "Black bars", [UI_MENU_MOVIE_ASPECT_STRETCH] = "Stretch", [UI_MENU_FOV] = "Field of view", [UI_MENU_FOV_DESC] = "How much of the world the camera shows, in percent of the game's own. Game version 1.4.3 only.", [UI_MENU_SHADOW_RES] = "Shadow map size", @@ -542,6 +546,10 @@ static const char *const text_ru[UI_TEXT_COUNT] = { [UI_MENU_ASPECT_DESC] = "Когда форма окна отличается от разрешения игры (картинка 16:9 на экране 21:9): сохранить " "форму картинки с чёрными полосами или растянуть её на всё окно.", [UI_MENU_ASPECT_FIT] = "Чёрные полосы", [UI_MENU_ASPECT_STRETCH] = "Растянуть", + [UI_MENU_MOVIE_ASPECT] = "Форма видеороликов", + [UI_MENU_MOVIE_ASPECT_DESC] = "Когда разрешение игры не 16:9: сохранить форму роликов с чёрными полосами или " + "растянуть их на всю картинку. Действует со следующего ролика.", + [UI_MENU_MOVIE_ASPECT_KEEP] = "Чёрные полосы", [UI_MENU_MOVIE_ASPECT_STRETCH] = "Растянуть", [UI_MENU_FOV] = "Поле зрения", [UI_MENU_FOV_DESC] = "Сколько мира показывает камера, в процентах от игрового. Только для версии игры 1.4.3.", [UI_MENU_SHADOW_RES] = "Размер карты теней", @@ -1040,6 +1048,10 @@ static const char *const text_pt_br[UI_TEXT_COUNT] = { [UI_MENU_ASPECT_DESC] = "Quando o formato da janela difere do da resolução do jogo (uma imagem 16:9 numa tela " "21:9): manter o formato da imagem com barras pretas ou esticá-la sobre a janela.", [UI_MENU_ASPECT_FIT] = "Barras pretas", [UI_MENU_ASPECT_STRETCH] = "Esticar", + [UI_MENU_MOVIE_ASPECT] = "Formato dos vídeos", + [UI_MENU_MOVIE_ASPECT_DESC] = "Quando a resolução do jogo não é 16:9: manter o formato dos vídeos das cenas com " + "barras pretas ou esticá-los sobre a imagem toda. Vale a partir do próximo vídeo.", + [UI_MENU_MOVIE_ASPECT_KEEP] = "Barras pretas", [UI_MENU_MOVIE_ASPECT_STRETCH] = "Esticar", [UI_MENU_SHARPNESS] = "Nitidez do FSR", [UI_MENU_SHARPNESS_DESC] = "Aumento de nitidez após a ampliação do FSR; 0 desliga.", @@ -1549,6 +1561,11 @@ static const char *const text_zh_hans[UI_TEXT_COUNT] = { "用黑边保持画面比例,或拉伸铺满窗口。", [UI_MENU_ASPECT_FIT] = "黑边", [UI_MENU_ASPECT_STRETCH] = "拉伸", + [UI_MENU_MOVIE_ASPECT] = "视频画面比例", + [UI_MENU_MOVIE_ASPECT_DESC] = "当游戏分辨率不是 16:9 时:用黑边保持过场视频的原有比例,或拉伸铺满整个画面。" + "从下一段视频起生效。", + [UI_MENU_MOVIE_ASPECT_KEEP] = "黑边", + [UI_MENU_MOVIE_ASPECT_STRETCH] = "拉伸", [UI_MENU_FOV] = "视野", [UI_MENU_FOV_DESC] = "镜头能看到多大范围,以游戏自身视野的百分比表示。" "仅限游戏版本 1.4.3。", diff --git a/runtime/ui_text.h b/runtime/ui_text.h index bfe43966..a9811e26 100644 --- a/runtime/ui_text.h +++ b/runtime/ui_text.h @@ -37,6 +37,7 @@ typedef enum { UI_MENU_RENDER_SCALE, UI_MENU_RENDER_SCALE_DESC, UI_MENU_RENDER_SCALE_GAME, UI_MENU_UPSCALER, UI_MENU_UPSCALER_DESC, UI_MENU_UPSCALER_FSR, UI_MENU_UPSCALER_BILINEAR, UI_MENU_ASPECT, UI_MENU_ASPECT_DESC, UI_MENU_ASPECT_FIT, UI_MENU_ASPECT_STRETCH, + UI_MENU_MOVIE_ASPECT, UI_MENU_MOVIE_ASPECT_DESC, UI_MENU_MOVIE_ASPECT_KEEP, UI_MENU_MOVIE_ASPECT_STRETCH, UI_MENU_FOV, UI_MENU_FOV_DESC, UI_MENU_SHADOW_RES, UI_MENU_SHADOW_RES_DESC, UI_MENU_SHADOW_RES_GAME, UI_MENU_ANISO, UI_MENU_ANISO_DESC, UI_MENU_ANISO_GAME, diff --git a/totk.ini.example b/totk.ini.example index 0f514cf5..744bdd3c 100644 --- a/totk.ini.example +++ b/totk.ini.example @@ -158,6 +158,10 @@ # window is no longer stretched as it used to be; stretch fills the window. Changes while the game runs take effect at # once. # aspect = fit +# Shape of the pre-rendered movies (16:9) when the game resolution (output_res) is not 16:9: keep, the default, draws +# them in their own shape with black bars at the sides or above and below; stretch fills the whole picture, as the game +# does. Read when a movie starts, so a change takes effect at the next movie. +# movie_aspect = keep # Field of view, in percent of the game's own: 70 to 140, 100 (the default) leaves the camera as it is. Higher shows # more of the world around Link. Game version 1.4.3 only. Changes while the game runs take effect at once. # fov = 100