From 8a8ef05241f2fd02659ed98af8a3e67d055808bc Mon Sep 17 00:00:00 2001 From: ProSep <9l0mhe7e@prosep.anonaddy.me> Date: Tue, 3 Nov 2026 23:27:39 +0100 Subject: [PATCH 1/2] Keep the shape of the pre-rendered movies on outputs that are not 16:9 The game stretches each decoded movie frame over the whole canvas, so the 16:9 movies were stretched on 32:9, 21:9 and 16:10 outputs. The host decoder now shrinks the picture inside the frame with a box filter and fills the rest with black, so that the game's stretch gives the picture its own shape back. The new setting movie_aspect (TOTK_MOVIE_ASPECT, keep or stretch, read when a movie starts) turns it off, and the menu has a row for it next to the picture shape. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 6 +- runtime/movie.h | 10 ++- runtime/movie_probe.c | 51 +++++++++++++ runtime/ov_movie.c | 169 +++++++++++++++++++++++++++++++++++++++++- runtime/ui/menu.cpp | 2 + runtime/ui_text.c | 17 +++++ runtime/ui_text.h | 1 + totk.ini.example | 4 + 8 files changed, 252 insertions(+), 8 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index de418f3c..eac290d5 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -43,7 +43,7 @@ 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_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "MOUSE_SENS", "MOVIE_HOST", "MOVIE_PACE", + "MENU_TEST_DIR", "MENU_TEST_SET", "MINIMAL_LAYOUT", "MIRROR_GAP", "MODS_DIR", "MODS_DISABLED", "MODS_ORDER", "MODS_REBUILD", "MOTION", "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", @@ -67,7 +67,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"}, @@ -118,7 +118,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/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..3d8e5ef1 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,57 @@ 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: a matching shape is no plan at all; a wider output gives black (Y 16, U = V 128) left and right +// and a flat picture stays flat; a taller one gives black above and below; and the cost of a frame is printed. +static int check_fit(void) { + enum { W = 1920, H = 1080 }; + uint8_t *y = malloc((size_t)W * H), *u = malloc((size_t)W * H / 4), *v = malloc((size_t)W * H / 4); + uint8_t *out = malloc((size_t)W * H * 3 / 2); + CHECK(y && u && v && out, "fit test buffers"); + memset(y, 120, (size_t)W * H); + memset(u, 90, (size_t)W * H / 4); + memset(v, 200, (size_t)W * H / 4); + const uint8_t *plane[3] = {y, u, v}; + const int stride[3] = {W, W / 2, W / 2}; + 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++) { + MovieFit *f = movie_fit_new(W, H, outs[k][0], outs[k][1]); + CHECK(f, "%ux%u: no plan", outs[k][0], outs[k][1]); + memset(out, 0, (size_t)W * H * 3 / 2); + movie_fit_apply(f, plane, stride, out); + const uint8_t *uv = out + (size_t)W * H; + int bars = 0, pic = 0, bad = 0; // luma pixels that are bar / picture / neither + for (int i = 0; i < W * H; i++) { + if (out[i] == 16) bars++; + else if (out[i] == 120) pic++; + else bad++; + } + int cbars = 0, cpic = 0, cbad = 0; + for (int i = 0; i < W * H / 2; i += 2) { + if (uv[i] == 128 && uv[i + 1] == 128) cbars++; + else if (uv[i] == 90 && uv[i + 1] == 200) cpic++; + else cbad++; + } + double shown = (double)pic / ((double)W * H); // picture's share of the frame, which must be shape / out shape + double want = outs[k][0] * H > (unsigned)W * outs[k][1] ? (double)W * outs[k][1] / ((double)outs[k][0] * H) + : (double)outs[k][0] * H / ((double)W * outs[k][1]); + CHECK(!bad && !cbad && bars && cbars && cpic && fabs(shown - want) < 0.01, + "%ux%u: %d luma pixels neither bar nor picture, %d chroma; picture %.3f of the frame, want %.3f", outs[k][0], + outs[k][1], bad, cbad, shown, want); + 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: picture %.1f%% of the frame, bars Y 16 / UV 128, %.2f ms per frame\n", outs[k][0], + outs[k][1], shown * 100, (plat_now_ns() - t0) / 1e8); + movie_fit_free(f); + } + free(y), free(u), free(v), free(out); + return 0; +} + 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..a2f968bd 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,143 @@ 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) 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) { + 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). +static void fit_row_h(const FitAxis *a, const uint8_t *src, uint8_t *dst, int dstep) { + 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 * a->taps; + uint32_t v = FIT_ONE / 2; + for (int t = 0; t < a->taps; t++) v += w[t] * p[t]; + dst[(size_t)i * dstep] = (uint8_t)(v / FIT_ONE); + } +} + +// 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++) { + memset(f->acc, 0, (size_t)pw * sizeof *f->acc); + const uint16_t *w = ay->wt + (size_t)y * ay->taps; + for (int t = 0; t < ay->taps; t++) { + const uint8_t *row = in + (size_t)(ay->start[y] + t) * is; + uint32_t k = w[t]; + for (int x = 0; x < pw; x++) f->acc[x] += k * row[x]; + } + uint8_t *o = base + (size_t)y * ds; + 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; + memset(dst, FIT_Y_BLACK, (size_t)w * h); + memset(uv, FIT_UV_BLACK, (size_t)w * ch); + 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 +388,8 @@ 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; + const MovieFit *fit = m->fit; 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 +404,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 +423,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 +447,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 +502,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 +746,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/ui/menu.cpp b/runtime/ui/menu.cpp index d76bdde5..a02c6f52 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 28490bdc..2feec9f1 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", @@ -532,6 +536,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] = "Размер карты теней", @@ -1020,6 +1028,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.", @@ -1517,6 +1529,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 edf669fb..e4ca4b5b 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 f5c28b7e..1e2b9d4b 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 From 3cd241071a8061c2cd41eb5f680d619c590776be Mon Sep 17 00:00:00 2001 From: ProSep <9l0mhe7e@prosep.anonaddy.me> Date: Tue, 3 Nov 2026 23:41:10 +0100 Subject: [PATCH 2/2] Check the movie picture fit against gradients and make it cheaper The probe now feeds gradient planes and checks bar symmetry, the picture's shape on screen, black outside the picture and the expected luma and separate U and V samples, with one cleanup path. The timing loop runs only with TOTK_PROBE_MOVIE_BENCH. In the decoder, a plan made for another frame size is never used, an allocation failure is logged, only the bar strips are filled with black, and the 3 and 4 tap filters are unrolled, which halves the cost. Co-Authored-By: Claude Opus 5.5 --- runtime/cfg.c | 2 +- runtime/movie_probe.c | 117 +++++++++++++++++++++++++++++------------- runtime/ov_movie.c | 51 ++++++++++++++---- 3 files changed, 124 insertions(+), 46 deletions(-) diff --git a/runtime/cfg.c b/runtime/cfg.c index eac290d5..64b3cb56 100644 --- a/runtime/cfg.c +++ b/runtime/cfg.c @@ -47,7 +47,7 @@ static const char *const known[] = { "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", diff --git a/runtime/movie_probe.c b/runtime/movie_probe.c index 3d8e5ef1..a944d569 100644 --- a/runtime/movie_probe.c +++ b/runtime/movie_probe.c @@ -269,52 +269,99 @@ static int paced(uint64_t obj, const uint8_t *data, const Track *t, int limit, u } // The shape-keeping resampler (ov_movie.c movie_fit_new / movie_fit_apply) on a synthetic 1920x1080 I420 picture, which -// needs no game data: a matching shape is no plan at all; a wider output gives black (Y 16, U = V 128) left and right -// and a flat picture stays flat; a taller one gives black above and below; and the cost of a frame is printed. +// 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 }; - uint8_t *y = malloc((size_t)W * H), *u = malloc((size_t)W * H / 4), *v = malloc((size_t)W * H / 4); + 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); - CHECK(y && u && v && out, "fit test buffers"); - memset(y, 120, (size_t)W * H); - memset(u, 90, (size_t)W * H / 4); - memset(v, 200, (size_t)W * H / 4); + 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, W / 2, W / 2}; - CHECK(!movie_fit_new(W, H, 1920, 1080) && !movie_fit_new(W, H, 2560, 1440), "16:9 outputs need no plan"); + 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++) { - MovieFit *f = movie_fit_new(W, H, outs[k][0], outs[k][1]); - CHECK(f, "%ux%u: no plan", outs[k][0], outs[k][1]); - memset(out, 0, (size_t)W * H * 3 / 2); + 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 bars = 0, pic = 0, bad = 0; // luma pixels that are bar / picture / neither - for (int i = 0; i < W * H; i++) { - if (out[i] == 16) bars++; - else if (out[i] == 120) pic++; - else bad++; + 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); } - int cbars = 0, cpic = 0, cbad = 0; - for (int i = 0; i < W * H / 2; i += 2) { - if (uv[i] == 128 && uv[i + 1] == 128) cbars++; - else if (uv[i] == 90 && uv[i + 1] == 200) cpic++; - else cbad++; - } - double shown = (double)pic / ((double)W * H); // picture's share of the frame, which must be shape / out shape - double want = outs[k][0] * H > (unsigned)W * outs[k][1] ? (double)W * outs[k][1] / ((double)outs[k][0] * H) - : (double)outs[k][0] * H / ((double)W * outs[k][1]); - CHECK(!bad && !cbad && bars && cbars && cpic && fabs(shown - want) < 0.01, - "%ux%u: %d luma pixels neither bar nor picture, %d chroma; picture %.3f of the frame, want %.3f", outs[k][0], - outs[k][1], bad, cbad, shown, want); - 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: picture %.1f%% of the frame, bars Y 16 / UV 128, %.2f ms per frame\n", outs[k][0], - outs[k][1], shown * 100, (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 0; + return rc; } int probe_movie(void) { diff --git a/runtime/ov_movie.c b/runtime/ov_movie.c index a2f968bd..10599d31 100644 --- a/runtime/ov_movie.c +++ b/runtime/ov_movie.c @@ -268,7 +268,10 @@ MovieFit *movie_fit_new(int w, int h, unsigned ow, unsigned oh) { 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) return NULL; + 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 @@ -282,6 +285,7 @@ MovieFit *movie_fit_new(int w, int h, unsigned ow, unsigned oh) { 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; } @@ -289,15 +293,22 @@ MovieFit *movie_fit_new(int w, int h, unsigned ow, unsigned oh) { } // One output row of the horizontal pass: dst gets the shrunk src row (dstep 2 writes every other byte, for U and V). -static void fit_row_h(const FitAxis *a, const uint8_t *src, uint8_t *dst, int dstep) { +// 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 * a->taps; + const uint16_t *w = a->wt + (size_t)i * taps; uint32_t v = FIT_ONE / 2; - for (int t = 0; t < a->taps; t++) v += w[t] * p[t]; + 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, @@ -320,14 +331,21 @@ static void fit_plane(const MovieFit *f, const uint8_t *src, int ss, int sw, int in = f->tmp, is = pw; } for (int y = 0; y < ph; y++) { - memset(f->acc, 0, (size_t)pw * sizeof *f->acc); 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 = in + (size_t)(ay->start[y] + t) * is; + 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]; } - uint8_t *o = base + (size_t)y * ds; for (int x = 0; x < pw; x++) o[(size_t)x * dstep] = (uint8_t)((f->acc[x] + FIT_ONE / 2) / FIT_ONE); } } @@ -336,8 +354,20 @@ static void fit_plane(const MovieFit *f, const uint8_t *src, int ss, int sw, int 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; - memset(dst, FIT_Y_BLACK, (size_t)w * h); - memset(uv, FIT_UV_BLACK, (size_t)w * ch); + // 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++) @@ -389,7 +419,8 @@ static void worker(void *arg) { m->out_held[slot] = 1; // reserved: in no queue, so the game cannot release it } uint64_t gen = m->gen, dt = 0, fit_ns = 0; - const MovieFit *fit = m->fit; + // 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;