mirror of
https://git.triforceproject.com/triforce/source
synced 2026-11-29 04:18:49 -04:00
Triforce v0.8.5
A Randomizer page in the launcher, a new title screen with Mods and Save Select tabs, scaling changes applied in game, the Jabu-Jabu bars fixed, renderer frame-time work and the brightness-flash fix, and fixes from the 0.8.4 reports.
This commit is contained in:
442
host/nn_video.c
442
host/nn_video.c
@@ -8,7 +8,8 @@
|
||||
pictures by buffer index (AcquireXDecoderOutputBuffer, GetXDecoderOutputBufferProperties), reads them back to
|
||||
its own memory as RGBA or NV12 (ReadDecoderOutputImageRgba / Nv12) and gives the buffer back (Release).
|
||||
|
||||
Here a submission is decoded at once; a finished picture waits in a queue until an Acquire puts it in the first
|
||||
Here a submission is kept and decoded later (ahead of the game by a host thread of the decoder's, see
|
||||
decode_ahead_main; else in the Acquire); a finished picture waits in a queue until an Acquire puts it in the first
|
||||
free registered buffer (host memory: the pool's guest memory is not used). The video service itself (nn::gsv) is
|
||||
not emulated: nn::codec::video::Initialize does nothing. */
|
||||
#include "hos.h"
|
||||
@@ -34,7 +35,7 @@
|
||||
#define DEC_MAGIC 0x43454456u /* 'VDEC' */
|
||||
#define PREF_MAGIC 0x46455250u /* 'PREF' */
|
||||
#define MAX_SLOTS 16
|
||||
#define MAX_QUEUE 8
|
||||
#define MAX_QUEUE 16 /* ((Triforce) 8 before the pictures were decoded ahead: room for the most HOS_VIDEO_DECODE_AHEAD takes, 14) */
|
||||
|
||||
static int video_log(void) { /* HOS_VIDEO_LOG=1: submissions and acquisitions */
|
||||
static int on = -1;
|
||||
@@ -46,6 +47,7 @@ void hos_system_event_init(uint64_t key, int autoclear); /* nn_os_sync.c */
|
||||
void hos_system_event_signal(uint64_t key);
|
||||
void hos_system_event_clear(uint64_t key); /* ((Triforce, 11-22, IO-34) these two under a host lock: no checkpoint) */
|
||||
void hos_system_event_signal_held(uint64_t key);
|
||||
void hos_host_thread_register_self(const char *name); /* hos_prof.c */
|
||||
|
||||
/* an image pool: its pictures, w x h, as RGBA and as NV12 (both kept: the reader asks for either) */
|
||||
typedef struct {
|
||||
@@ -91,17 +93,51 @@ void mf_av1_decode(void *m, const uint8_t *data, size_t n, int64_t pts, mf_av1_p
|
||||
void mf_av1_flush(void *m);
|
||||
void mf_av1_close(void *m);
|
||||
|
||||
/* (Triforce, the AMD limits work) The rules of decoding ahead (decode_ahead_main), without the decoder: cut out and run by
|
||||
tests/test_video_decode_ahead.py.
|
||||
HOS_VIDEO_DECODE_AHEAD into the pictures kept decoded ahead of the game: unset or 1, VIDEO_AHEAD_PICTURES; 0 (or no
|
||||
number), none: decoded in the game's call, as before; 2 and more, that many, with two places of the queue left over.
|
||||
A 1080p picture is 3.1 MB as NV12 (Media Foundation's), so twelve ahead are ~37 MB; libavcodec's are kept as RGBA too,
|
||||
11.4 MB each, ~137 MB. Twelve are 200 ms of a movie at 60 pictures a second: the title's movie has single pictures
|
||||
that take 80-200 ms to decode, and in the lab six ahead still left an acquire waiting 30 ms twice in 600 (the title
|
||||
with the menu loading over it), twelve none */
|
||||
enum { VIDEO_AHEAD_PICTURES = 12, VIDEO_LATE_NS = 25000000 };
|
||||
static inline int video_ahead_count(const char *e, int max_queue) {
|
||||
const int n = e ? atoi(e) : 1, most = max_queue - 2;
|
||||
if (n <= 0 || most < 1) return 0;
|
||||
const int want = n == 1 ? VIDEO_AHEAD_PICTURES : n;
|
||||
return want < most ? want : most;
|
||||
}
|
||||
/* 1 while the worker decodes the next submission now: the stream's end as soon as it is next, pictures queued or not
|
||||
(the last picture must get its flag before it goes out, see decode_pending), a picture while fewer than `ahead` wait.
|
||||
It never decodes into a queue that has its count, so nothing decoded is ever dropped for room */
|
||||
static inline int video_ahead_decode(int has_next, int next_is_end, int queued, int ahead) {
|
||||
return has_next && (next_is_end || queued < ahead);
|
||||
}
|
||||
/* the catch-up's rule: a picture more than `late` behind the movie's clock (the picture at clock_pts was due at clock_ns) */
|
||||
static inline int video_picture_late(int64_t pts, int64_t clock_pts, int64_t clock_ns, int64_t now, int64_t late) {
|
||||
return pts + late < clock_pts + (now - clock_ns);
|
||||
}
|
||||
|
||||
typedef struct {
|
||||
/* ((Triforce) The decoder: ctx or mf, frame, sws, and out, the pictures of the submission being decoded. One thread
|
||||
at a time uses it, and it has no lock of its own: with a worker (decode ahead), the worker while `decoding` is set,
|
||||
d->lock not held, and a flush under d->lock once `decoding` is clear (ahead_stop); out is the worker's alone.
|
||||
Without a worker, whoever holds d->lock. ctx and mf themselves are set at the start and never change) */
|
||||
AVCodecContext *ctx;
|
||||
void *mf; /* (hookshot) an AV1 decoder on Media Foundation instead of ctx */
|
||||
AVPacket *pkt;
|
||||
AVFrame *frame;
|
||||
struct SwsContext *sws;
|
||||
int sws_w, sws_h, sws_fmt;
|
||||
Picture *out; /* (Triforce) pictures decoded, not in the queue yet (out_push, decode_next, the worker) */
|
||||
int nout, cap_out;
|
||||
/* (the buffers: the game's calls only, never the worker) */
|
||||
uint64_t slots[MAX_SLOTS]; /* registered pools (guest pointers), by buffer index */
|
||||
int busy[MAX_SLOTS]; /* acquired, not released */
|
||||
int64_t slot_pts[MAX_SLOTS];
|
||||
int slot_last[MAX_SLOTS]; /* the stream's last picture (the submission marked last, all decoded) */
|
||||
/* (from here on: under d->lock, the worker's reads and writes too) */
|
||||
Picture queue[MAX_QUEUE];
|
||||
int qhead, qn;
|
||||
int eos; /* the last submission is in and decoded: an empty queue is the end */
|
||||
@@ -122,6 +158,20 @@ typedef struct {
|
||||
uint64_t ready; /* ((Triforce, 11-22, IO-34) its "ready for a submission" event, a guest address: set,
|
||||
and clear from a refused submission until there is room again) */
|
||||
int ready_clear; /* (... whether it is clear now; changed only under the lock, with the event) */
|
||||
/* (Triforce) what the decoding cost since the last "video: ... pictures taken" line: submissions decoded, their time,
|
||||
the longest one, the longest time one acquire waited for its picture (for the worker; without one, decoding in the
|
||||
call, where the catch-up decodes several), pictures skipped, acquires that found their picture ready and that waited */
|
||||
int64_t st_since, st_taken, st_decoded, st_ns, st_max_ns, st_call_max_ns, st_skipped, st_ready, st_waited;
|
||||
/* (Triforce) decode ahead (decode_ahead_main). worker and ahead are set at the start and never change */
|
||||
HANDLE worker; /* the decoder's host thread; NULL: none (the switch, or no thread): decoded in the acquire */
|
||||
int ahead; /* the pictures it keeps decoded ahead */
|
||||
CONDITION_VARIABLE work; /* the worker sleeps here: a submission, a picture taken, a flush and the close wake it */
|
||||
CONDITION_VARIABLE idle; /* the game's calls wait here for the worker: woken when a decode ends, a picture is queued */
|
||||
int decoding, ending; /* the worker is in the decoder (d->lock let go); and with the stream's end */
|
||||
int uncommitted; /* decoded pictures the worker has yet to queue (the queue was full) */
|
||||
int hold; /* flushes that wait for the decoder: no decode begins */
|
||||
int closing; /* the decoder is being finalized: the worker ends */
|
||||
uint32_t gen; /* counts the flushes: pictures of a decode begun before one are thrown away */
|
||||
SRWLOCK lock;
|
||||
} Dec;
|
||||
|
||||
@@ -302,6 +352,8 @@ HLE(_ZN2nn5codec26ReadDecoderOutputImageNv12EPKNS0_25VideoDecoderImagePoolTypeEj
|
||||
}
|
||||
|
||||
/* ---- decoders ---- */
|
||||
static void ahead_start(Dec *d);
|
||||
static void ahead_close(Dec *d);
|
||||
static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, int h);
|
||||
static void dec_init(Ctx *c, enum AVCodecID id, const char *what) { dec_init_sized(c, id, what, 0, 0); }
|
||||
#ifdef _WIN32
|
||||
@@ -376,11 +428,13 @@ static void dec_init_sized(Ctx *c, enum AVCodecID id, const char *what, int w, i
|
||||
d->pkt = av_packet_alloc();
|
||||
d->frame = av_frame_alloc();
|
||||
d->ready = ready_new(); /* ((Triforce, 11-22, IO-34) before the game can ask for it or submit) */
|
||||
if (d->ctx || d->mf) ahead_start(d); /* ((Triforce) before the game can find the decoder: no call sees it change) */
|
||||
set_host(ARG(0), DEC_MAGIC, d);
|
||||
static int logged;
|
||||
if (logged++ < 4)
|
||||
hos_log("video: %s decoder %s%s (%dx%d)", what, d->ctx ? "ready: " : d->mf ? "ready on Media Foundation" :
|
||||
"not available in this FFmpeg", d->ctx ? d->ctx->codec->name : "", w, h);
|
||||
hos_log("video: %s decoder %s%s (%dx%d)%s", what, d->ctx ? "ready: " : d->mf ? "ready on Media Foundation" :
|
||||
"not available in this FFmpeg", d->ctx ? d->ctx->codec->name : "", w, h,
|
||||
d->worker ? ", its pictures decoded ahead by a host thread (HOS_VIDEO_DECODE_AHEAD)" : "");
|
||||
RET(0);
|
||||
}
|
||||
static void picture_free(Picture *p) {
|
||||
@@ -413,27 +467,56 @@ static void movie_end(Dec *d) {
|
||||
static void dec_finalize(Ctx *c) {
|
||||
Dec *d = (Dec *)host_of(ARG(0), DEC_MAGIC);
|
||||
if (!d) return;
|
||||
set_host(ARG(0), DEC_MAGIC, NULL); /* ((Triforce) first: no later call finds the decoder while its worker is waited for) */
|
||||
ahead_close(d); /* (the worker out of the decoder and ended, before any of it is freed) */
|
||||
movie_end(d);
|
||||
/* ((Triforce, 11-22, IO-34) its ready event is left set: a thread that still waits for it is not left waiting. No
|
||||
lock: nothing else has this decoder any more) */
|
||||
if (d->ready_clear) hos_system_event_signal(d->ready);
|
||||
pending_free(d);
|
||||
for (int i = 0; i < d->qn; i++) picture_free(&d->queue[(d->qhead + i) % MAX_QUEUE]);
|
||||
for (int i = 0; i < d->nout; i++) picture_free(&d->out[i]);
|
||||
free(d->out);
|
||||
if (d->sws) sws_freeContext(d->sws);
|
||||
avcodec_free_context(&d->ctx);
|
||||
mf_av1_close(d->mf);
|
||||
av_packet_free(&d->pkt);
|
||||
av_frame_free(&d->frame);
|
||||
free(d);
|
||||
set_host(ARG(0), DEC_MAGIC, NULL);
|
||||
}
|
||||
static int rgba_eager(void) { /* ((Triforce, 11-22, IO-36) HOS_VIDEO_RGBA=1, read once: planar pictures made RGBA at once) */
|
||||
static int on = -1;
|
||||
if (on < 0) on = getenv("HOS_VIDEO_RGBA") != NULL && atoi(getenv("HOS_VIDEO_RGBA")) != 0;
|
||||
return on;
|
||||
}
|
||||
/* a decoded frame into the queue, as RGBA and NV12 (the oldest dropped when the reader falls behind).
|
||||
((Triforce, 11-22, the performance audit's IO-36, after Source A's) As NV12 alone for libdav1d's planar 4:2:0 too, as
|
||||
/* ((Triforce) a decoded picture kept for the queue, in the decoding thread's own list (see Dec): no lock, and not the
|
||||
queue, which the game's calls read meanwhile. 0: no memory for the list, the picture is freed) */
|
||||
static int out_push(Dec *d, Picture *p) {
|
||||
if (d->nout == d->cap_out) {
|
||||
const int cap = d->cap_out ? d->cap_out * 2 : 4;
|
||||
Picture *more = (Picture *)realloc(d->out, (size_t)cap * sizeof *more);
|
||||
if (!more) {
|
||||
picture_free(p);
|
||||
return 0;
|
||||
}
|
||||
d->out = more, d->cap_out = cap;
|
||||
}
|
||||
d->out[d->nout++] = *p;
|
||||
return 1;
|
||||
}
|
||||
/* ((Triforce) a decoded picture into the queue, d->lock held. Without a worker the oldest is dropped when the reader falls
|
||||
behind, as before; the worker never comes here with a full queue: it waits for room, decode_ahead_main) */
|
||||
static void queue_push(Dec *d, const Picture *p) {
|
||||
if (d->qn == MAX_QUEUE) {
|
||||
picture_free(&d->queue[d->qhead]);
|
||||
d->qhead = (d->qhead + 1) % MAX_QUEUE, d->qn--;
|
||||
}
|
||||
d->queue[(d->qhead + d->qn) % MAX_QUEUE] = *p;
|
||||
d->qn++;
|
||||
}
|
||||
/* a decoded frame as RGBA and NV12, for the queue (out_push: this runs on the thread that decodes, which with a worker
|
||||
does not hold d->lock; it touches the decoder alone) */
|
||||
/* ((Triforce, 11-22, the performance audit's IO-36, after Source A's) As NV12 alone for libdav1d's planar 4:2:0 too, as
|
||||
Media Foundation's NV12 already was: no swscale context, no RGBA buffer and no conversion, the pool making the RGBA
|
||||
only if it is read (pool_rgba; dec_acquire copies no RGBA rows for such a picture). Each 1080p picture was converted
|
||||
on the movie thread for a game that reads only the NV12. The NV12 buffer is zeroed only where a copy leaves bytes
|
||||
@@ -475,14 +558,7 @@ static void queue_frame(Dec *d, const AVFrame *f) {
|
||||
video_uv_interleave(uv + (size_t)y * w, f->data[1] + (size_t)y * f->linesize[1],
|
||||
f->data[2] + (size_t)y * f->linesize[2], w / 2);
|
||||
}
|
||||
if (d->qn == MAX_QUEUE) {
|
||||
picture_free(&d->queue[d->qhead]);
|
||||
d->qhead = (d->qhead + 1) % MAX_QUEUE, d->qn--;
|
||||
}
|
||||
d->queue[(d->qhead + d->qn) % MAX_QUEUE] = p;
|
||||
d->qn++;
|
||||
d->movie.pictures++;
|
||||
video_pace_picture(&d->pace, f->pts); /* ((Triforce, 11-22, THREAD-82) the gap between pictures, from their own times) */
|
||||
if (!out_push(d, &p)) hos_log("video: no memory to keep a %dx%d frame: dropped", w, h);
|
||||
}
|
||||
static void mf_picture(void *user, const uint8_t *y, const uint8_t *uv, int w, int h, int stride, int64_t pts) {
|
||||
AVFrame f;
|
||||
@@ -506,47 +582,67 @@ static void decode_next(Dec *d);
|
||||
static void decode_pending(Dec *d) {
|
||||
while (d->phead && (!d->qn || !d->phead->pkt)) decode_next(d);
|
||||
}
|
||||
/* one submission decoded (its pictures into the queue, if any) */
|
||||
/* (d->lock held) the oldest submission off the list; data after the end starts a stream again */
|
||||
static Pending *pending_pop(Dec *d) {
|
||||
Pending *n = d->phead;
|
||||
d->phead = n->next;
|
||||
if (!d->phead) d->ptail = NULL;
|
||||
d->npending--; /* ((Triforce, 11-22, IO-34) no longer waiting to be decoded) */
|
||||
if (n->pkt) d->eos = 0;
|
||||
return n;
|
||||
}
|
||||
/* ((Triforce) one submission through the decoder, its pictures into d->out (mf_picture, drain: queue_frame), and the
|
||||
submission freed. For the thread that has the decoder (see Dec): the worker comes here without d->lock, so nothing
|
||||
under that lock is read or written here, and no guest memory (the submission's data was copied by dec_submit)) */
|
||||
static void decode_one(Dec *d, Pending *n) {
|
||||
if (d->mf) { /* (hookshot) */
|
||||
if (n->pkt) mf_av1_decode(d->mf, n->pkt->data, (size_t)n->pkt->size, n->pkt->pts, mf_picture, d);
|
||||
else mf_av1_decode(d->mf, NULL, 0, 0, mf_picture, d);
|
||||
} else if (n->pkt) {
|
||||
if (avcodec_send_packet(d->ctx, n->pkt) == AVERROR(EAGAIN)) { /* (full: its pictures out first) */
|
||||
drain(d);
|
||||
avcodec_send_packet(d->ctx, n->pkt);
|
||||
}
|
||||
drain(d);
|
||||
} else { /* (the end: the rest out) */
|
||||
avcodec_send_packet(d->ctx, NULL);
|
||||
drain(d);
|
||||
avcodec_flush_buffers(d->ctx); /* (a stream after it starts again) */
|
||||
}
|
||||
av_packet_free(&n->pkt);
|
||||
free(n);
|
||||
}
|
||||
/* (d->lock held) after decode_one: its time counted; at the stream's end the last picture marked, the last one this
|
||||
submission gave (d->out, queued next) or else the last one queued.
|
||||
((Triforce, 11-22) The movie's own counts (v0.7.0's IO-32 line and THREAD-82 pace) are taken here, under the lock, from
|
||||
the pictures in d->out and their times: queue_frame, where v0.7.0 counted them, runs on the worker without the lock.
|
||||
live 0: a decode a flush overtook, of a movie whose line is already out: not counted into the next one) */
|
||||
static void decode_done(Dec *d, int end, int64_t ns, int live) {
|
||||
d->st_decoded++, d->st_ns += ns;
|
||||
if (ns > d->st_max_ns) d->st_max_ns = ns;
|
||||
if (live) {
|
||||
video_movie_decoded(&d->movie, ns);
|
||||
for (int i = 0; i < d->nout; i++) d->movie.pictures++, video_pace_picture(&d->pace, d->out[i].pts);
|
||||
/* ((Triforce, 11-22, THREAD-82) and what a decode that gives a picture takes lately, for the catch-up's bound) */
|
||||
if (d->nout) video_pace_decoded(&d->pace, ns);
|
||||
}
|
||||
if (end) {
|
||||
d->eos = 1;
|
||||
d->log_submits = 8; /* (data after the end: a restart, worth seeing) */
|
||||
if (d->nout) d->out[d->nout - 1].last = 1;
|
||||
else if (d->qn) d->queue[(d->qhead + d->qn - 1) % MAX_QUEUE].last = 1;
|
||||
}
|
||||
}
|
||||
/* one submission decoded in the caller's thread, d->lock held all through (no worker): its pictures into the queue, if any */
|
||||
static void decode_next(Dec *d) {
|
||||
if (d->phead) {
|
||||
const int64_t began = hos_now_ns(); /* ((Triforce, 11-22, IO-32) each submission timed, for the movie's line) */
|
||||
const int64_t pictures = d->movie.pictures;
|
||||
Pending *n = d->phead;
|
||||
d->phead = n->next;
|
||||
if (!d->phead) d->ptail = NULL;
|
||||
d->npending--;
|
||||
if (d->mf) { /* (hookshot) */
|
||||
if (n->pkt) {
|
||||
d->eos = 0;
|
||||
mf_av1_decode(d->mf, n->pkt->data, (size_t)n->pkt->size, n->pkt->pts, mf_picture, d);
|
||||
av_packet_free(&n->pkt);
|
||||
} else {
|
||||
mf_av1_decode(d->mf, NULL, 0, 0, mf_picture, d);
|
||||
d->eos = 1;
|
||||
d->log_submits = 8; /* (data after the end: a restart, worth seeing) */
|
||||
if (d->qn) d->queue[(d->qhead + d->qn - 1) % MAX_QUEUE].last = 1;
|
||||
}
|
||||
} else if (n->pkt) {
|
||||
d->eos = 0;
|
||||
if (avcodec_send_packet(d->ctx, n->pkt) == AVERROR(EAGAIN)) { /* (full: its pictures out first) */
|
||||
drain(d);
|
||||
avcodec_send_packet(d->ctx, n->pkt);
|
||||
}
|
||||
drain(d);
|
||||
av_packet_free(&n->pkt);
|
||||
} else { /* (the end: the rest out, the last picture marked) */
|
||||
avcodec_send_packet(d->ctx, NULL);
|
||||
drain(d);
|
||||
d->eos = 1;
|
||||
d->log_submits = 8;
|
||||
if (d->qn) d->queue[(d->qhead + d->qn - 1) % MAX_QUEUE].last = 1;
|
||||
avcodec_flush_buffers(d->ctx); /* (a stream after it starts again) */
|
||||
}
|
||||
free(n);
|
||||
const int64_t took = hos_now_ns() - began;
|
||||
video_movie_decoded(&d->movie, took);
|
||||
/* ((Triforce, 11-22, THREAD-82) and what a decode that gives a picture takes lately, for the catch-up's bound) */
|
||||
if (d->movie.pictures != pictures) video_pace_decoded(&d->pace, took);
|
||||
const int64_t t0 = hos_now_ns();
|
||||
Pending *n = pending_pop(d);
|
||||
const int end = !n->pkt;
|
||||
decode_one(d, n);
|
||||
decode_done(d, end, hos_now_ns() - t0, 1);
|
||||
for (int i = 0; i < d->nout; i++) queue_push(d, &d->out[i]);
|
||||
d->nout = 0;
|
||||
}
|
||||
}
|
||||
static void pending_push(Dec *d, AVPacket *pkt) {
|
||||
@@ -557,8 +653,147 @@ static void pending_push(Dec *d, AVPacket *pkt) {
|
||||
d->ptail = n;
|
||||
d->npending++;
|
||||
}
|
||||
/* SubmitXDecoderInputBuffer(u64* id, decoder, data, size, TimeSpan pts, bool last): kept, decoded when a picture is
|
||||
acquired (the library submits a whole movie at once: its pictures decoded then would not fit any queue) */
|
||||
|
||||
/* ---- decode ahead ---- */
|
||||
/* (Triforce, the AMD limits work) The pictures were decoded in the game's Acquire call: 3-5 ms each for the title screen's
|
||||
1080p AV1 movie in its quiet parts, 6-13 ms in busy ones, single ones 80-200 ms, and after a long frame several in a row
|
||||
(the catch-up: one call decoded for 728 ms, and 47-250 of every ~650 pictures were thrown away to keep up with the
|
||||
sound). Giving the virtual core up for the call (dec_acquire, HOS_VIDEO_KEEP_CORE) ended the sound's stutter; the movie still stuttered
|
||||
wherever a scene loads over it. Now each decoder has a host thread of its own (no guest thread: it holds no virtual
|
||||
core, touches no guest memory, runs at normal priority) that decodes the submissions ahead of the game and keeps
|
||||
`ahead` pictures in the queue (video_ahead_count), so an acquire takes a picture that is there.
|
||||
|
||||
The locking. d->lock guards the lists, the queue and the counts, and is never held across a decode: the worker takes a
|
||||
submission off the list under it, sets `decoding`, lets it go, decodes into its own list (d->out), takes it again,
|
||||
clears `decoding` and queues the pictures. So a submit, an acquire, a flush wait for the lock no longer than a list
|
||||
operation. The decoder itself has no lock: it is the worker's while `decoding` is set. A flush needs it too
|
||||
(avcodec_flush_buffers, mf_av1_flush): ahead_stop counts the flush (gen: what a decode begun before it gives is thrown
|
||||
away, also by a worker that waits for room in the queue), holds new decodes off (hold) and waits on `idle` until
|
||||
`decoding` is clear; ahead_go lets the worker on. The close (ahead_close) sets `closing` and waits for the thread to
|
||||
end before anything is freed. The worker sleeps on `work` with nothing to decode or the queue at its count, and with a
|
||||
full queue (MAX_QUEUE: a submission that gave several pictures) it waits for room: it never drops a picture.
|
||||
|
||||
HOS_VIDEO_DECODE_AHEAD=0: no worker, decoded in the acquire as before; =<2..14>: that many pictures ahead (6) */
|
||||
static int decode_ahead(void) {
|
||||
static int n = -1;
|
||||
if (n < 0) n = video_ahead_count(getenv("HOS_VIDEO_DECODE_AHEAD"), MAX_QUEUE);
|
||||
return n;
|
||||
}
|
||||
/* (d->lock held) 1 while the last picture queued may yet turn out to be the stream's last: the stream's end is in the
|
||||
decoder, or is next with nothing between that picture and it. Such a picture does not go out before the worker has
|
||||
marked it (the rule above decode_pending); the end is decoded as soon as it is next, so this is short */
|
||||
static int tail_unsettled(const Dec *d) {
|
||||
return d->ending || (!d->decoding && !d->uncommitted && d->phead && !d->phead->pkt);
|
||||
}
|
||||
static DWORD WINAPI decode_ahead_main(LPVOID arg) {
|
||||
Dec *d = (Dec *)arg;
|
||||
hos_host_thread_register_self("video decode ahead");
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
while (!d->closing) {
|
||||
if (d->hold || !video_ahead_decode(d->phead != NULL, d->phead && !d->phead->pkt, d->qn, d->ahead)) {
|
||||
SleepConditionVariableSRW(&d->work, &d->lock, INFINITE, 0);
|
||||
continue;
|
||||
}
|
||||
Pending *n = pending_pop(d);
|
||||
const int end = !n->pkt;
|
||||
const uint32_t gen = d->gen;
|
||||
d->decoding = 1, d->ending = end;
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
const int64_t t0 = hos_now_ns();
|
||||
decode_one(d, n); /* (the decoder is this thread's; no call of the game waits for this) */
|
||||
const int64_t ns = hos_now_ns() - t0;
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
decode_done(d, end && gen == d->gen, ns, gen == d->gen); /* (after a flush it was the end of a stream that is gone) */
|
||||
/* ((Triforce, 11-22, v0.7.0's IO-34) the submission left the list: the ready event a refused one cleared is set
|
||||
again from here too, not only at the game's next call (hos_system_event_signal_held takes no checkpoint) */
|
||||
input_room(d);
|
||||
d->decoding = d->ending = 0;
|
||||
d->uncommitted = d->nout;
|
||||
WakeAllConditionVariable(&d->idle); /* (a flush that waits for the decoder; an acquire that waits for the end) */
|
||||
int i = 0;
|
||||
while (i < d->nout && gen == d->gen && !d->closing) {
|
||||
if (d->qn == MAX_QUEUE) { /* (full: the oldest is not dropped; the next acquire makes room and wakes this) */
|
||||
SleepConditionVariableSRW(&d->work, &d->lock, INFINITE, 0);
|
||||
continue;
|
||||
}
|
||||
queue_push(d, &d->out[i++]);
|
||||
d->uncommitted--;
|
||||
WakeAllConditionVariable(&d->idle); /* (an acquire that waits for a picture) */
|
||||
}
|
||||
const int dropped = i < d->nout;
|
||||
for (; i < d->nout; i++) picture_free(&d->out[i]); /* (a flush, the close: pictures of the stream before it) */
|
||||
d->nout = d->uncommitted = 0;
|
||||
/* (an acquire that counted on those pictures: woken, not left to its 2 ms look) */
|
||||
if (dropped) WakeAllConditionVariable(&d->idle);
|
||||
}
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
return 0;
|
||||
}
|
||||
/* the worker started, before the game has the decoder (dec_init_sized). None with the switch off, or when the thread
|
||||
cannot be made: d->worker stays NULL for the decoder's life and the acquire decodes */
|
||||
static void ahead_start(Dec *d) {
|
||||
InitializeConditionVariable(&d->work);
|
||||
InitializeConditionVariable(&d->idle);
|
||||
d->ahead = decode_ahead();
|
||||
if (!d->ahead) return;
|
||||
d->worker = CreateThread(NULL, 0, decode_ahead_main, d, 0, NULL);
|
||||
if (!d->worker) hos_log("video: no thread to decode a movie's pictures ahead: they are decoded in the game's calls");
|
||||
/* (placed as the game's threads are: the decoding ran on the movie's game thread before, off the core kept for the
|
||||
replay; left where Windows puts it, a busy decode could sit on that core. HOS_PIN names it "video decode ahead") */
|
||||
else hos_thread_pin(d->worker, "video decode ahead");
|
||||
}
|
||||
/* the worker ended and waited for (the close, no other call has the decoder): it is in the decoder for one submission at
|
||||
most, and that may take 100 ms and more, so not on a virtual core */
|
||||
static void ahead_close(Dec *d) {
|
||||
if (!d->worker) return;
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
d->closing = 1;
|
||||
WakeAllConditionVariable(&d->work);
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
const int away = hos_vcore_away();
|
||||
WaitForSingleObject(d->worker, INFINITE);
|
||||
if (away) hos_vcore_back();
|
||||
CloseHandle(d->worker);
|
||||
d->worker = NULL;
|
||||
}
|
||||
/* (d->lock held, let go while it waits) the decoder taken from the worker for a flush, and given back. hos_cv_sleep
|
||||
gives the caller's virtual core up for the wait; it may let the lock go once more after `decoding` is clear, and
|
||||
`hold` keeps the worker out of the decoder then */
|
||||
static void ahead_stop(Dec *d) {
|
||||
if (!d->worker) return;
|
||||
d->gen++, d->hold++;
|
||||
WakeAllConditionVariable(&d->work); /* (a worker that waits for room: its pictures are of the stream before) */
|
||||
while (d->decoding) hos_cv_sleep(&d->idle, &d->lock, 20);
|
||||
}
|
||||
static void ahead_go(Dec *d) {
|
||||
if (!d->worker) return;
|
||||
d->hold--;
|
||||
WakeAllConditionVariable(&d->work);
|
||||
WakeAllConditionVariable(&d->idle); /* (an acquire that waited for what the flush threw away) */
|
||||
}
|
||||
/* (d->lock held, let go while it waits) What decode_pending and the wait after it give an acquire, from the worker: it
|
||||
comes back with a picture queued whose last flag is settled, or with none when none will come: at the stream's end,
|
||||
or after ~100 ms with nothing submitted (as before: the buffer goes out as it was). While the worker has something to
|
||||
decode or to queue the wait goes on, as the acquire's own decoding did: the game is never handed an old buffer for a
|
||||
picture that is on its way. The time waited for the worker (ns) comes back, for the pictures-taken line; 0 is the
|
||||
rule: the picture was there */
|
||||
static int64_t ahead_wait(Dec *d) {
|
||||
const int64_t idle_until = hos_now_ns() + 100000000;
|
||||
int64_t waited = 0;
|
||||
for (;;) {
|
||||
if (d->qn > 1 || (d->qn == 1 && !tail_unsettled(d))) break;
|
||||
const int coming = d->qn || d->phead || d->decoding || d->uncommitted;
|
||||
const int64_t t0 = hos_now_ns();
|
||||
if (!coming && (d->eos || d->stopped || t0 >= idle_until)) break; /* (stopped: v0.7.0's rule, no wait after a Stop) */
|
||||
WakeAllConditionVariable(&d->work);
|
||||
hos_cv_sleep(&d->idle, &d->lock, 2);
|
||||
if (coming) waited += hos_now_ns() - t0;
|
||||
}
|
||||
return waited;
|
||||
}
|
||||
/* SubmitXDecoderInputBuffer(u64* id, decoder, data, size, TimeSpan pts, bool last): kept, decoded a few pictures ahead
|
||||
of the acquires by the worker, or without one when a picture is acquired (the library submits a whole movie at once:
|
||||
its pictures decoded then would not fit any queue) */
|
||||
static volatile LONG64 submissions;
|
||||
|
||||
/* (hookshot) A buffer handed out with nothing decoded into it, by a decoder with nothing left to decode, gets a sync
|
||||
@@ -671,10 +906,27 @@ static void dec_submit(Ctx *c) {
|
||||
if (video_log() && (d->submits <= 8 || (ARG(5) & 1) || logged_one))
|
||||
hos_log("video: submit %lld (%llu bytes, pts %lld%s)", (long long)d->submits, (unsigned long long)ARG(3),
|
||||
(long long)ARG(4), (ARG(5) & 1) ? ", last" : "");
|
||||
if (d->worker) WakeAllConditionVariable(&d->work); /* ((Triforce) decoded ahead, from now) */
|
||||
input_room(d); /* ((Triforce, 11-22, IO-34) as at every unlock that ends a call: the event never stays clear with room) */
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
RET(0);
|
||||
}
|
||||
/* (d->lock held) the flush itself. With a worker the decoder is taken from it first (ahead_stop waits for the decode in
|
||||
flight, the lock let go meanwhile: a submission that comes in then is of the stream before, and goes with it); from
|
||||
there to ahead_go the lock is held, so the decoder, the lists and the queue are emptied as one step */
|
||||
static void flush_locked(Dec *d) {
|
||||
ahead_stop(d);
|
||||
d->log_submits = 8;
|
||||
if (d->ctx) avcodec_flush_buffers(d->ctx);
|
||||
else mf_av1_flush(d->mf);
|
||||
pending_free(d);
|
||||
for (int i = 0; i < d->qn; i++) picture_free(&d->queue[(d->qhead + i) % MAX_QUEUE]);
|
||||
d->qhead = d->qn = 0;
|
||||
d->eos = 0;
|
||||
d->clock_set = 0;
|
||||
for (int i = 0; i < MAX_SLOTS; i++) d->slot_last[i] = 0;
|
||||
ahead_go(d);
|
||||
}
|
||||
static void dec_flush(Ctx *c, const char *what) {
|
||||
Dec *d = (Dec *)host_of(ARG(0), DEC_MAGIC);
|
||||
if (d && (d->ctx || d->mf)) {
|
||||
@@ -683,17 +935,10 @@ static void dec_flush(Ctx *c, const char *what) {
|
||||
hos_log("video: %s after %lld submits and %lld acquires (%d queued, %s, lr %#llx)", what, (long long)d->submits,
|
||||
(long long)d->acquires, d->qn, d->eos ? "at the end" : d->phead ? "submissions left" : "none left",
|
||||
(unsigned long long)c->x[30]);
|
||||
d->log_submits = 8;
|
||||
movie_end(d); /* ((Triforce, 11-22, IO-32) what the movie cost to decode, once) */
|
||||
if (d->ctx) avcodec_flush_buffers(d->ctx);
|
||||
else mf_av1_flush(d->mf);
|
||||
pending_free(d);
|
||||
for (int i = 0; i < d->qn; i++) picture_free(&d->queue[(d->qhead + i) % MAX_QUEUE]);
|
||||
d->qhead = d->qn = 0;
|
||||
d->eos = 0;
|
||||
movie_end(d); /* ((Triforce, 11-22, IO-32) what the movie cost to decode, once; before the flush counts itself
|
||||
(gen): what a decode still in flight gives is not counted into the next movie) */
|
||||
flush_locked(d);
|
||||
d->stopped = 1;
|
||||
d->clock_set = 0;
|
||||
for (int i = 0; i < MAX_SLOTS; i++) d->slot_last[i] = 0;
|
||||
input_room(d); /* ((Triforce, 11-22, IO-34) nothing waits any more: ready for submissions) */
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
}
|
||||
@@ -710,7 +955,11 @@ static void dec_unregister(Ctx *c) {
|
||||
if (d && (uint32_t)ARG(1) < MAX_SLOTS) d->slots[(uint32_t)ARG(1)] = 0;
|
||||
RET(0);
|
||||
}
|
||||
/* AcquireXDecoderOutputBuffer(u32* index, NativeSyncHandle*, decoder): the oldest picture into a free buffer */
|
||||
/* AcquireXDecoderOutputBuffer(u32* index, NativeSyncHandle*, decoder): the oldest picture into a free buffer.
|
||||
((Triforce, 11-22) The v0.6.0 branch gave the thread's virtual core up around this call (HOS_VIDEO_DECODE_AWAY); v0.7.0
|
||||
does so itself below (HOS_VIDEO_KEEP_CORE=1: kept), so that wrapper is gone. With the pictures decoded ahead
|
||||
("decode ahead" above) the call decodes nothing and mostly waits for nothing; where it does wait for the worker,
|
||||
hos_cv_sleep gives the core up, with or without the switch) */
|
||||
static void dec_acquire(Ctx *c) {
|
||||
Dec *d = (Dec *)host_of(ARG(2), DEC_MAGIC);
|
||||
if (ARG(1)) G64(ARG(1)) = 0;
|
||||
@@ -728,13 +977,20 @@ static void dec_acquire(Ctx *c) {
|
||||
if (keep_core < 0) keep_core = getenv("HOS_VIDEO_KEEP_CORE") && atoi(getenv("HOS_VIDEO_KEEP_CORE")) == 1;
|
||||
const int away = keep_core ? 0 : hos_vcore_away();
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
decode_pending(d);
|
||||
const int64_t wait_until = hos_now_ns() + 100000000;
|
||||
for (int tries = 0; !d->qn && !d->eos && (by_tries ? tries < 50 : !d->stopped && hos_now_ns() < wait_until); tries++) {
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
hos_sleep_ns(2000000);
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
const int64_t call_ns0 = d->st_ns;
|
||||
int64_t waited_ns = 0;
|
||||
if (!d->st_since) d->st_since = hos_now_ns();
|
||||
if (d->worker) { /* ((Triforce) decoded ahead: the worker's picture, waited for only when none is there) */
|
||||
waited_ns = ahead_wait(d);
|
||||
} else {
|
||||
decode_pending(d);
|
||||
const int64_t wait_until = hos_now_ns() + 100000000;
|
||||
for (int tries = 0; !d->qn && !d->eos && (by_tries ? tries < 50 : !d->stopped && hos_now_ns() < wait_until); tries++) {
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
hos_sleep_ns(2000000);
|
||||
AcquireSRWLockExclusive(&d->lock);
|
||||
decode_pending(d);
|
||||
}
|
||||
}
|
||||
int slot = -1;
|
||||
for (int i = 0; i < MAX_SLOTS && slot < 0; i++)
|
||||
@@ -750,7 +1006,9 @@ static void dec_acquire(Ctx *c) {
|
||||
if (!d->qn) { /* (nothing decoded: the buffer's last picture again; after the last submission, as the last) */
|
||||
d->busy[slot] = 1;
|
||||
if (d->eos) d->slot_last[slot] = 1, d->slot_pts[slot] = d->last_pts; /* (the end at the last picture's time) */
|
||||
const int idle = !d->eos && !d->phead; /* (nothing will come into this buffer: its sync is not reached) */
|
||||
/* (nothing will come into this buffer: its sync is not reached. Nothing submitted, and with a worker nothing in
|
||||
the decoder or on its way to the queue either, which ahead_wait has waited out) */
|
||||
const int idle = !d->eos && !d->phead && !d->decoding && !d->uncommitted;
|
||||
if (idle && ARG(1))
|
||||
G64(ARG(1)) = SYNC_IDLE_TAG | ((uint64_t)InterlockedCompareExchange64(&submissions, 0, 0) & SYNC_IDLE_MASK);
|
||||
if (video_log()) hos_log("video: acquire %lld: nothing decoded%s, buffer %d again", (long long)d->acquires,
|
||||
@@ -766,7 +1024,11 @@ static void dec_acquire(Ctx *c) {
|
||||
}
|
||||
{ /* (hookshot) The game takes one picture a frame, and the sound plays on: under 60 fps the movie fell behind its
|
||||
sound (the intro "slow motion, audio ahead", issues 146 and 179). Pictures behind the movie's clock are skipped:
|
||||
the next ones decoded, as the whole movie is submitted at once. HOS_VIDEO_NO_CATCHUP=1: as before */
|
||||
the next ones decoded, as the whole movie is submitted at once. HOS_VIDEO_NO_CATCHUP=1: as before.
|
||||
(Triforce) With the pictures decoded ahead only queued ones are skipped, down to the newest: nothing is decoded
|
||||
in the call, the worker decodes on into the room this makes and the next acquires skip what is still behind.
|
||||
The picture before the last queued one stays while that one's last flag is not settled (tail_unsettled): the
|
||||
one that goes out is never one the worker has yet to mark */
|
||||
/* ((Triforce, 11-22, the performance audit's THREAD-82, after Source A's) Decoding ahead to skip had no bound but
|
||||
the guard below. With a decoder slower than the movie every picture decoded to catch up puts the clock
|
||||
further ahead, so an acquire decoded hundreds of pictures with the lock held, threw them away, and the next
|
||||
@@ -777,16 +1039,16 @@ static void dec_acquire(Ctx *c) {
|
||||
if (off < 0) off = getenv("HOS_VIDEO_NO_CATCHUP") != NULL;
|
||||
if (unbounded < 0)
|
||||
unbounded = getenv("HOS_VIDEO_UNBOUNDED_CATCHUP") != NULL && atoi(getenv("HOS_VIDEO_UNBOUNDED_CATCHUP")) != 0;
|
||||
const int64_t now = hos_now_ns(), late = 25000000; /* (a frame and a half at 60 fps) */
|
||||
const int64_t now = hos_now_ns(), late = VIDEO_LATE_NS; /* (a frame and a half at 60 fps) */
|
||||
Picture *h = &d->queue[d->qhead];
|
||||
if (!d->clock_set || h->pts < d->clock_pts) d->clock_set = 1, d->clock_ns = now, d->clock_pts = h->pts; /* (a start, a loop) */
|
||||
int skipped = 0;
|
||||
for (int guard = 0; !off && guard < 600 && !d->queue[d->qhead].last; guard++) {
|
||||
const int64_t due = d->clock_pts + (now - d->clock_ns);
|
||||
if (d->queue[d->qhead].pts + late >= due) break;
|
||||
if (!video_picture_late(d->queue[d->qhead].pts, d->clock_pts, d->clock_ns, now, late)) break;
|
||||
if (d->qn < 2) {
|
||||
if (!d->phead) break;
|
||||
if (d->worker || !d->phead) break;
|
||||
if (video_catchup_stop(unbounded, video_pace_spacing(&d->pace), d->pace.recent_ns, hos_now_ns() - now)) {
|
||||
const int64_t due = d->clock_pts + (now - d->clock_ns);
|
||||
if (video_log())
|
||||
hos_log("video: the catch-up stops %.2f s behind: a decode takes %.1f ms lately, a picture is due "
|
||||
"every %.1f ms, and this acquire has decoded for %.1f ms", (double)(due - d->queue[d->qhead].pts) / 1e9,
|
||||
@@ -797,13 +1059,22 @@ static void dec_acquire(Ctx *c) {
|
||||
decode_next(d);
|
||||
continue;
|
||||
}
|
||||
if (d->worker && d->qn == 2 && tail_unsettled(d)) break;
|
||||
picture_free(&d->queue[d->qhead]);
|
||||
d->qhead = (d->qhead + 1) % MAX_QUEUE, d->qn--;
|
||||
skipped++;
|
||||
}
|
||||
if (skipped && video_log()) hos_log("video: %d pictures skipped to catch up with the movie's clock", skipped);
|
||||
d->movie.skipped += skipped; /* ((Triforce, 11-22, IO-32) and in the movie's line) */
|
||||
d->st_skipped += skipped;
|
||||
}
|
||||
{ /* ((Triforce) what this acquire waited for its picture: for the worker, or without one its own decoding) */
|
||||
const int64_t call_ns = d->worker ? waited_ns : d->st_ns - call_ns0;
|
||||
if (call_ns > d->st_call_max_ns) d->st_call_max_ns = call_ns;
|
||||
if (call_ns > 0) d->st_waited++;
|
||||
else d->st_ready++;
|
||||
}
|
||||
d->st_taken++;
|
||||
{ /* (Triforce) and none ahead of it. The game takes the next picture when its count of the movie's 60ths says so
|
||||
(hos_movie_frames, host/nn_vi.c), and while a movie plays at the 60 cap the frames are paced at 240
|
||||
(movie_pace.h) and the count follows their time, not the clock: at ~226 fps an opening's picture ran ahead,
|
||||
@@ -894,8 +1165,21 @@ static void dec_acquire(Ctx *c) {
|
||||
(unsigned long long)(n ? sum / n : 0), (unsigned long long)(n ? rgb[0] / n : 0),
|
||||
(unsigned long long)(n ? rgb[1] / n : 0), (unsigned long long)(n ? rgb[2] / n : 0));
|
||||
}
|
||||
if (d->st_taken >= 600 || (p->last && d->st_taken)) { /* ((Triforce) what the decoding cost: one line per 600 pictures) */
|
||||
hos_log("video: %lld pictures taken in %.1f s: %lld decoded in %.1f ms each (the longest %.1f ms), %s; %lld acquires "
|
||||
"found their picture ready, %lld waited for it, %.1f ms at most in one acquire; %lld skipped to catch up with "
|
||||
"the movie's clock; the thread's virtual core %s for the call (HOS_VIDEO_KEEP_CORE)",
|
||||
(long long)d->st_taken, (double)(hos_now_ns() - d->st_since) / 1e9, (long long)d->st_decoded,
|
||||
d->st_decoded ? (double)d->st_ns / 1e6 / (double)d->st_decoded : 0.0, (double)d->st_max_ns / 1e6,
|
||||
d->worker ? "decoded ahead by a host thread (HOS_VIDEO_DECODE_AHEAD)" : "in the game's call",
|
||||
(long long)d->st_ready, (long long)d->st_waited, (double)d->st_call_max_ns / 1e6, (long long)d->st_skipped,
|
||||
keep_core ? "kept" : "given up");
|
||||
d->st_since = d->st_taken = d->st_decoded = d->st_ns = d->st_max_ns = d->st_call_max_ns = d->st_skipped = 0;
|
||||
d->st_ready = d->st_waited = 0;
|
||||
}
|
||||
picture_free(p);
|
||||
d->qhead = (d->qhead + 1) % MAX_QUEUE, d->qn--;
|
||||
if (d->worker) WakeAllConditionVariable(&d->work); /* ((Triforce) room in the queue: the worker decodes on) */
|
||||
input_room(d);
|
||||
ReleaseSRWLockExclusive(&d->lock);
|
||||
if (away) hos_vcore_back();
|
||||
|
||||
Reference in New Issue
Block a user