mirror of
https://github.com/valentderah/totk-pc-recomp.git
synced 2026-11-11 01:41:02 +00:00
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 <noreply@anthropic.com>
This commit is contained in:
1 parent
8a8ef05241
commit
3cd241071a
3 files changed
+124
-46
No files matched your search
+1
-1
@@ -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",
|
||||
|
||||
+82
-35
@@ -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) {
|
||||
|
||||
+41
-10
@@ -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;
|
||||
|
||||
Reference in new issue
Block a user