Keep the movie deband planes per decoder, and drop the frame instead of ending the game when they cannot be allocated.

make_picture allocated the three 16-bit deband planes (6.2 MB at 1080p) for every picture and freed them after it,
60 times a second while a movie played, and in its usual path (8-bit, deband on, no overlay) a failed allocation
ended the game with "movie: out of memory for a WxH frame", while the slower path and the frame allocation itself
drop the frame through movie_oom_log. run_stripes' comment records that FATAL at 45.7 GB of commit on an RX 580.
A verifier's probe measured 0.34-0.36 ms and about 1000 page faults a picture for the per-picture allocation against
0.04 ms and 3 faults with kept planes, about 12% of the convert thread's time per picture.

Each Video now keeps its planes (y16, u16, v16, plane_room), used only by its convert thread and made again only for
a larger picture; planes_for frees what it has and allocates afresh, and on a failure frees all three and keeps none.
Both paths take their planes from it and drop the frame through movie_oom_log when it fails. finalize frees the
planes after stop_thread has joined the convert thread, which also covers the destructor. The path with the deband
off and no overlay no longer allocates planes it never used.

tests/movie_planes_check.py cuts the deband, the conversions, run_stripes, planes_for and make_picture from
ov_movie2.c with a stub dav1d picture and a malloc that fails on request: two pictures of one size allocate the
planes once, a larger one makes them again, the NV12 frame (and the overlay's RGBA frame) equals what fresh planes
give, also with an earlier picture left in the kept planes, and a failed plane allocation drops the frame with
nothing left allocated. The compile check passes with no new warnings.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
chrg6
2026-11-13 17:12:08 -05:00
parent 0672655c96
commit 0b217575d8
2 changed files with 252 additions and 15 deletions

View File

@@ -63,6 +63,11 @@ typedef struct {
PlatCond conv; // a decoded picture arrived, a slot freed, or quit
Decoded cq[CQMAX];
int cqh, cqn;
// The convert thread's deband planes (16-bit Y, U and V, plane_room samples of luma), kept from picture to picture
// and made again only for a larger one: made per picture they were 6.2 MB of fresh pages each at 1080p, 60 times a
// second. finalize frees them once the convert thread is joined.
uint16_t *y16, *u16, *v16;
size_t plane_room;
Dav1dContext *dav1d;
uint8_t in_ev[0x40], out_ev[0x40]; // nn::os::SystemEventType each
int in_bound, out_bound;
@@ -357,8 +362,23 @@ static void movie_oom_log(int w, int h) {
if (plat_xchg32(&drops, 1) == 0) rt_log("movie: out of memory for a %dx%d frame: dropped (later drops not logged)", w, h);
}
// One dav1d picture (8- or 10-bit 4:2:0) -> NV12 with 8-bit samples, and its RGBA; decoder thread
static int make_picture(const Dav1dPicture *pic, Picture *p) {
// The Video's deband planes for a w x h picture (convert thread). 0: no memory for them; the frame is then dropped
// as for any allocation of a picture, and no planes are kept.
static int planes_for(Video *v, int w, int h) {
size_t px = (size_t)w * h;
if (px <= v->plane_room) return 1;
free(v->y16), free(v->u16), free(v->v16);
v->y16 = malloc(px * 2), v->u16 = malloc(px / 2), v->v16 = malloc(px / 2);
v->plane_room = px;
if (v->y16 && v->u16 && v->v16) return 1;
free(v->y16), free(v->u16), free(v->v16);
v->y16 = v->u16 = v->v16 = NULL;
v->plane_room = 0;
return 0;
}
// One dav1d picture (8- or 10-bit 4:2:0) -> NV12 with 8-bit samples, and its RGBA; convert thread
static int make_picture(Video *v, const Dav1dPicture *pic, Picture *p) {
int w = pic->p.w, h = pic->p.h, hbd = pic->p.bpc > 8, sh = pic->p.bpc - 8;
if (pic->p.layout != DAV1D_PIXEL_LAYOUT_I420) {
static volatile int32_t logged;
@@ -395,11 +415,14 @@ static int make_picture(const Dav1dPicture *pic, Picture *p) {
// planes and writes the dithered NV12 rows. Reading the decoder's planes instead of an NV12 copy of them gives
// the same samples, and nothing reads the frame while it is written, so each stripe finishes its rows alone:
// one round of threads per frame instead of two, and no single-threaded copy before it.
uint16_t *Y16 = malloc((size_t)w * h * 2), *U16 = malloc((size_t)w * h / 2), *V16 = malloc((size_t)w * h / 2);
if (!Y16 || !U16 || !V16) rt_fatal("movie: out of memory for a %dx%d frame", w, h);
Stripe base = {2, 0, 0, w, h, o, Y16, U16, V16, NULL, pic->data[0], pic->data[1], pic->data[2], ys, cs};
if (!planes_for(v, w, h)) {
free(o);
movie_oom_log(w, h);
return 0;
}
Stripe base = {2, 0, 0, w, h, o, v->y16, v->u16, v->v16, NULL, pic->data[0], pic->data[1], pic->data[2],
ys, cs};
run_stripes(base);
free(Y16), free(U16), free(V16);
*p = (Picture){o, NULL, w, h, pic->m.timestamp, 0};
return 1;
}
@@ -417,27 +440,26 @@ static int make_picture(const Dav1dPicture *pic, Picture *p) {
}
}
// debanded planes (NX2NAVI_MOVIE_DEBAND=0 keeps the decoded ones), then the overlay's RGBA or the dithered NV12
if (!deband_on && !rgba) {
*p = (Picture){o, NULL, w, h, pic->m.timestamp, 0};
return 1;
}
{
uint16_t *Y16 = malloc((size_t)w * h * 2), *U16 = malloc((size_t)w * h / 2), *V16 = malloc((size_t)w * h / 2);
if (!Y16 || !U16 || !V16) {
free(Y16), free(U16), free(V16), free(o), free(rgba);
if (!planes_for(v, w, h)) {
free(o), free(rgba);
movie_oom_log(w, h);
return 0;
}
uint16_t *Y16 = v->y16, *U16 = v->u16, *V16 = v->v16;
Stripe base = {0, 0, 0, w, h, o, Y16, U16, V16, rgba};
if (deband_on) {
run_stripes(base);
} else if (!rgba) {
free(Y16), free(U16), free(V16);
*p = (Picture){o, rgba, w, h, pic->m.timestamp, 0};
return 1;
} else {
for (size_t i = 0; i < (size_t)w * h; i++) Y16[i] = (uint16_t)(o[i] * 16);
for (size_t i = 0; i < (size_t)w * h / 4; i++) U16[i] = (uint16_t)(uv[2 * i] * 16), V16[i] = (uint16_t)(uv[2 * i + 1] * 16);
}
base.phase = 1;
run_stripes(base);
free(Y16), free(U16), free(V16);
}
*p = (Picture){o, rgba, w, h, pic->m.timestamp, 0};
return 1;
@@ -505,7 +527,7 @@ static void convert_thread(void *arg) {
} else {
uint64_t t0 = plat_now_ns();
Picture p;
int ok = make_picture(&d.pic, &p);
int ok = make_picture(v, &d.pic, &p);
t_make_ns += plat_now_ns() - t0;
n_made++;
dav1d_picture_unref(&d.pic);
@@ -600,6 +622,9 @@ static void stop_thread(Video *v) {
static void finalize(Video *v) {
stop_thread(v);
free(v->y16), free(v->u16), free(v->v16); // the convert thread, their only user, has been joined
v->y16 = v->u16 = v->v16 = NULL;
v->plane_room = 0;
plat_mutex_lock(&v->codec);
if (v->dav1d) dav1d_close(&v->dav1d);
plat_mutex_unlock(&v->codec);