WEBVTT
Kind: captions
Language: en

00:00:00.080 --> 00:00:04.160
So this is a quick follow-up to my&nbsp;
last video on the Wii U gamepad clone,&nbsp;&nbsp;

00:00:04.160 --> 00:00:07.760
and this always seems to happen when&nbsp;
you're a YouTuber who does projects.

00:00:07.760 --> 00:00:10.560
You'll have some problem you've&nbsp;
been wrestling with for months...

00:00:10.560 --> 00:00:11.760
...if not years!

00:00:11.760 --> 00:00:14.320
You'll feel like you've&nbsp;
explored every single option,&nbsp;&nbsp;

00:00:14.320 --> 00:00:16.560
researched everything there is to know about it...

00:00:16.560 --> 00:00:19.360
You put it out on Patreon&nbsp;
and no one has any ideas...

00:00:19.360 --> 00:00:22.320
and then the moment you publish it on YouTube...

00:00:22.320 --> 00:00:25.200
...someone's like "oh, did you try this?"

00:00:25.200 --> 00:00:27.920
And... it's the solution to&nbsp;
every single problem you had.

00:00:27.920 --> 00:00:31.840
That's what happened to me with the Raspberry Pi.

00:00:31.840 --> 00:00:35.520
Yes, pretty much immediately people started&nbsp;
troubleshooting why the Raspberry Pi seemed&nbsp;&nbsp;

00:00:35.520 --> 00:00:40.400
to have a mandatory 3 frames of latency, and&nbsp;
making suggestions for what I could do about it.

00:00:40.400 --> 00:00:45.280
One of the most common was: couldn't we add&nbsp;
three blank frames in the beginning to offset it?

00:00:45.280 --> 00:00:48.400
I think what people thought would&nbsp;
happen was by adding blank frames,&nbsp;&nbsp;

00:00:48.400 --> 00:00:51.280
the sending side would line&nbsp;
up with the receiving side.

00:00:51.280 --> 00:00:54.080
But a frame is a frame, even if it's blank.

00:00:54.080 --> 00:00:57.360
By adding them to the queue, we're&nbsp;
just delaying the output even further.

00:00:57.360 --> 00:01:00.240
A 3-frame delay becomes a 6-frame delay.

00:01:00.240 --> 00:01:01.760
But a more interesting suggestion was:&nbsp;&nbsp;

00:01:01.760 --> 00:01:06.000
what if we tripled the frames going in&nbsp;
and just skipped the extra two coming out?

00:01:06.000 --> 00:01:09.360
Essentially forcing it to flush&nbsp;
out the real frame we just sent.

00:01:09.360 --> 00:01:13.360
In theory, this seemed plausible,&nbsp;
but I did have some concerns.

00:01:13.360 --> 00:01:15.200
First, as far as the decoder was concerned,&nbsp;&nbsp;

00:01:15.200 --> 00:01:20.560
we'd effectively be turning a&nbsp;
60FPS stream into a 180FPS stream,

00:01:20.560 --> 00:01:22.800
and not a single resource suggested that the Pi&nbsp;&nbsp;

00:01:22.800 --> 00:01:27.200
Zero would be even remotely capable&nbsp;
of decoding 180 frames per second.

00:01:27.200 --> 00:01:29.920
Secondly, just tripling the frames wouldn't work.

00:01:29.920 --> 00:01:34.640
As we established in the main video, most&nbsp;
H.264 frames only store the differences,&nbsp;&nbsp;

00:01:34.640 --> 00:01:38.000
or more specifically the motion,&nbsp;
between this frame and the last.

00:01:38.000 --> 00:01:41.120
For example, make these&nbsp;
pixels here move over here.

00:01:41.120 --> 00:01:45.600
It takes advantage of the fact that most&nbsp;
video is capturing some kind of smooth motion.

00:01:45.600 --> 00:01:50.240
Therefore, the only way to keep the video stream&nbsp;
faithful is to decode each frame in order.

00:01:50.240 --> 00:01:52.880
If you don't, then the motion data&nbsp;
starts getting applied to the wrong&nbsp;&nbsp;

00:01:52.880 --> 00:01:56.400
parts of the frame, leading to a&nbsp;
corruption of the original picture.

00:01:56.400 --> 00:02:00.160
This is what we saw when we tried to&nbsp;
decode on the Pi Zero 2's CPU and it&nbsp;&nbsp;

00:02:00.160 --> 00:02:03.040
couldn't quite keep up with&nbsp;
all of the incoming frames.

00:02:03.040 --> 00:02:05.680
Duplicating frames would produce&nbsp;
the same kind of problem:&nbsp;&nbsp;

00:02:05.680 --> 00:02:10.000
we'd end up applying motion to parts of the&nbsp;
frame that aren't supposed to move like that.

00:02:10.000 --> 00:02:12.560
That being said, we could fabricate a "do&nbsp;&nbsp;

00:02:12.560 --> 00:02:15.760
nothing" frame and insert two&nbsp;
of those for every real frame.

00:02:15.760 --> 00:02:19.760
There was still no guarantee that the Pi could&nbsp;
keep up with this, but in theory if it could,&nbsp;&nbsp;

00:02:19.760 --> 00:02:24.080
that would keep the video stream faithful,&nbsp;
and workaround the 3 frames of latency.

00:02:24.080 --> 00:02:27.520
I was pessimistic about our chances&nbsp;
but I figured I'd give it a try anyway.

00:02:27.520 --> 00:02:29.200
Why leave any stone unturned?

00:02:29.200 --> 00:02:32.240
But what actually happened&nbsp;
was not what I expected.

00:02:32.240 --> 00:02:36.800
I tested it out expecting to see at least&nbsp;
some kind of difference, but... I didn't.

00:02:36.800 --> 00:02:41.040
The feed was the same as before, including&nbsp;
the exact same amount of latency.

00:02:41.040 --> 00:02:42.640
But... how is that possible?

00:02:42.640 --> 00:02:45.840
If we're inserting a bunch of&nbsp;
frames, where did they all go?

00:02:45.840 --> 00:02:48.560
Well I confirmed from the logs that it was indeed&nbsp;&nbsp;

00:02:48.560 --> 00:02:51.920
sending those null frames&nbsp;
and skipping them correctly.

00:02:51.920 --> 00:02:56.080
So astonishingly, the Pi could keep&nbsp;
up with this preposterous frame rate,

00:02:56.080 --> 00:02:58.240
as long as two-thirds of&nbsp;
the frames are dummy frames,

00:02:58.240 --> 00:03:02.160
but it still ended up imposing&nbsp;
the exact same amount of delay.

00:03:02.160 --> 00:03:07.680
So the only thing that could mean was the delay&nbsp;
is not actually a per-frame delay like I thought.

00:03:07.680 --> 00:03:11.200
It's a time delay - as in&nbsp;
real world, wall clock time.

00:03:11.200 --> 00:03:16.160
It doesn't matter if it's 3 frames&nbsp;
at 60 FPS or 9 frames at 180 FPS,&nbsp;&nbsp;

00:03:16.160 --> 00:03:20.320
apparently they all just take a certain amount&nbsp;
of time to work their way through the pipeline,&nbsp;&nbsp;

00:03:20.320 --> 00:03:24.720
and that just happens to be long enough to&nbsp;
cause a couple of frames of delay at 60 FPS.

00:03:24.720 --> 00:03:28.080
To confirm, I modified my latency&nbsp;
test tool to wait a certain amount&nbsp;&nbsp;

00:03:28.080 --> 00:03:32.480
of time between sending and receiving&nbsp;
a frame, and indeed if we just wait,&nbsp;&nbsp;

00:03:32.480 --> 00:03:37.520
we get the same frame we just sent without&nbsp;
having to push any more frames into the pipeline.

00:03:37.520 --> 00:03:42.880
So yeah, it's not about flushing the frames out,&nbsp;
it's just a matter of waiting for them to finish.

00:03:42.880 --> 00:03:44.880
It seemed like there was no way around it.

00:03:44.880 --> 00:03:48.400
I was ready to call it there, and was&nbsp;
even preparing a GitHub comment announcing&nbsp;&nbsp;

00:03:48.400 --> 00:03:56.720
to the world that it was a lost cause, when&nbsp;
suddenly I got the following Discord message...

00:03:56.720 --> 00:04:00.560
Now, at first I have to admit,&nbsp;
I was a little skeptical.

00:04:00.560 --> 00:04:03.840
I had literally just proven this&nbsp;
thing had an impenetrable time delay.

00:04:03.840 --> 00:04:06.240
What could someone else have found that I didn't?

00:04:06.240 --> 00:04:09.520
Well, earlier that day, GitHub user EinTim23 had&nbsp;&nbsp;

00:04:09.520 --> 00:04:12.800
reached out offering some suggestions&nbsp;
for reducing the three-frame delay.

00:04:12.800 --> 00:04:17.040
And while they didn't end up making a difference,&nbsp;
we both kept investigating the issue separately.

00:04:17.040 --> 00:04:20.800
Most of his findings lined up with mine that&nbsp;
it was a time delay rather than a frame delay,&nbsp;&nbsp;

00:04:20.800 --> 00:04:24.640
however he had managed to discover&nbsp;
a breakthrough that I hadn't.

00:04:24.640 --> 00:04:28.560
It wasn't *all* of the frames that took that long.

00:04:28.560 --> 00:04:32.160
See, I was right that the first&nbsp;
frame took about 50 milliseconds,&nbsp;&nbsp;

00:04:32.160 --> 00:04:35.920
which is way too long and sets us&nbsp;
back by a little over three frames.

00:04:35.920 --> 00:04:37.360
But look at the next one.

00:04:37.360 --> 00:04:39.440
It only took 5 milliseconds.

00:04:39.440 --> 00:04:40.640
Same as the next one.

00:04:40.640 --> 00:04:41.920
And the next one.

00:04:41.920 --> 00:04:44.560
So even if the first frame&nbsp;
does take a really long time,&nbsp;&nbsp;

00:04:44.560 --> 00:04:47.600
it would only take a few more frames&nbsp;
before we were caught up again,&nbsp;&nbsp;

00:04:47.600 --> 00:04:51.920
as long as we allow ourselves to skip the&nbsp;
frames that the really long frame displaced.

00:04:51.920 --> 00:04:56.480
And that was the big problem: Vanilla simply&nbsp;
wasn't designed for a decoder like this.

00:04:56.480 --> 00:04:59.360
Vanilla was designed around&nbsp;
FFmpeg's software decoder,&nbsp;&nbsp;

00:04:59.360 --> 00:05:02.400
which is a simple synchronous&nbsp;
frame-in-frame-out affair.

00:05:02.400 --> 00:05:04.800
If it ever does say it isn't&nbsp;
ready to produce a frame yet,&nbsp;&nbsp;

00:05:04.800 --> 00:05:07.920
it's almost always because it needs&nbsp;
another input frame, like when it's&nbsp;&nbsp;

00:05:07.920 --> 00:05:11.360
dealing with those pesky bidirectional&nbsp;
frames I mentioned in the main video.

00:05:11.360 --> 00:05:14.080
But even though I knew the Wii&nbsp;
U's video feed didn't use these,&nbsp;&nbsp;

00:05:14.080 --> 00:05:17.600
I still wrote Vanilla's decode loop&nbsp;
to, if the decoder wasn't ready yet,&nbsp;&nbsp;

00:05:17.600 --> 00:05:22.240
send another frame before trying again because&nbsp;
that's just how you usually write them.

00:05:22.240 --> 00:05:25.600
And I mean it worked, just&nbsp;
not for the reasons I thought.

00:05:25.600 --> 00:05:28.560
I thought it was because it&nbsp;
needed those additional frames.

00:05:28.560 --> 00:05:32.880
In reality, it only appeared to work because&nbsp;
the slow frame happened to be done by then,&nbsp;&nbsp;

00:05:32.880 --> 00:05:36.880
and I was unwittingly creating a&nbsp;
backlog that never got drained.

00:05:36.880 --> 00:05:41.840
Yes, it turns out I was complicit&nbsp;
in this three frames of latency.

00:05:41.840 --> 00:05:43.440
Knowing that, the fix is simple.

00:05:43.440 --> 00:05:47.280
Rather than just shrug and send another frame&nbsp;
if the decoder hasn't produced anything yet,&nbsp;&nbsp;

00:05:47.280 --> 00:05:52.000
we keep polling it at regular intervals so we&nbsp;
can grab the latest frame as soon as it's ready.

00:05:52.000 --> 00:05:56.080
The actual plumbing was a little more complicated,&nbsp;
I ended up moving this polling to a new thread&nbsp;&nbsp;

00:05:56.080 --> 00:05:59.120
so it could do it at all times without&nbsp;
interfering with the main thread, which...

00:05:59.120 --> 00:06:00.400
...you know how it is.

00:06:00.400 --> 00:06:04.400
But here's the important part: I am&nbsp;
very pleased to announce that now&nbsp;&nbsp;

00:06:04.400 --> 00:06:10.320
the Raspberry Pi Zero has the same amount&nbsp;
of latency as the original Wii U gamepad.

00:06:10.320 --> 00:06:13.680
So all of that discouragement&nbsp;
and non-recommending I did of&nbsp;&nbsp;

00:06:13.680 --> 00:06:16.960
the Raspberry Pi Zero, feel free to disregard it.

00:06:16.960 --> 00:06:22.000
Just about any Raspberry Pi should&nbsp;
now make for a viable Wii U gamepad.

00:06:22.000 --> 00:06:23.600
Where does that leave the Orange Pi?

00:06:23.600 --> 00:06:26.960
Well, on paper it probably is&nbsp;
still the superior computer,&nbsp;&nbsp;

00:06:26.960 --> 00:06:30.000
what with its slightly faster CPU and decoder,&nbsp;&nbsp;

00:06:30.000 --> 00:06:34.640
and if you can ever get one cheaper than the&nbsp;
Raspberry Pi again, it's probably a no-brainer.

00:06:34.640 --> 00:06:38.720
However you are much more likely to find&nbsp;
the Raspberry Pi in stores or buried in a&nbsp;&nbsp;

00:06:38.720 --> 00:06:43.200
drawer somewhere, and if you do, in&nbsp;
practice it should be just as good.

00:06:43.200 --> 00:06:46.080
Thanks to EinTim23 for finding the missing piece.

00:06:46.080 --> 00:06:47.840
I'll see you all in the next one.

00:06:47.840 --> 00:07:06.400
Bye guys.

