Unmerged PR from OG Ryujinx (#4367). From @gdkchan: > The main goal of this change is porting the loop filtering from libvpx, which should fix the block artifacts on some VP9 videos on games using NVDEC to decode them. In addition to that, there are two other changes: > > - The remaining decoder code required to decode a VP9 video (with headers included) has been added. That was done because it's much better to test the decoder standalone with a video file. I decided to keep that code on the emulator, even if some of it is unused, since it makes standalone testing easier in the future too, and we can include unit tests with video files. > - Large refactoring of both new and existing code to conform with our conding [sic] styles, done by @TSRBerry (thanks!) Some of it has been automated. > > Since we had no loop filtering before, this change will make video decoding slower. That may cause frame drop etc if the decoder is not fast enough in some games. I plan to optimize the decoder more in the future to make up for that, but if possible I'd prefer to not do it as part of this PR, but if the perf loss is too severe I might consider. > > This will need to be tested on games that had the block artifacts, it would be nice to confirm if they match hardware now, and get some before/after screenshots etc. Comment from @Bjorn29512: > Significantly improves the block artifacts in FE: Engage. > > Before: >  > > After: >  --------- Co-authored-by: gdkchan <gab.dark.100@gmail.com> Co-authored-by: TSR Berry <20988865+TSRBerry@users.noreply.github.com>
84 lines
2.0 KiB
C#
84 lines
2.0 KiB
C#
using Ryujinx.Graphics.Nvdec.Vp9.Common;
|
|
using Ryujinx.Graphics.Nvdec.Vp9.Types;
|
|
using System;
|
|
|
|
namespace Ryujinx.Graphics.Nvdec.Vp9
|
|
{
|
|
public ref struct ReadBitBuffer
|
|
{
|
|
public ReadOnlySpan<byte> BitBuffer;
|
|
public ulong BitOffset;
|
|
public object ErrorHandlerData;
|
|
|
|
public int DecodeUnsignedMax(int max)
|
|
{
|
|
int data = ReadLiteral(BitUtils.GetUnsignedBits((uint)max));
|
|
return data > max ? max : data;
|
|
}
|
|
|
|
public ulong BytesRead()
|
|
{
|
|
return (BitOffset + 7) >> 3;
|
|
}
|
|
|
|
public int ReadBit()
|
|
{
|
|
ulong off = BitOffset;
|
|
ulong p = off >> 3;
|
|
int q = 7 - (int)(off & 0x7);
|
|
if (p < (ulong)BitBuffer.Length)
|
|
{
|
|
int bit = (BitBuffer[(int)p] >> q) & 1;
|
|
BitOffset = off + 1;
|
|
return bit;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
public int ReadLiteral(int bits)
|
|
{
|
|
int value = 0, bit;
|
|
for (bit = bits - 1; bit >= 0; bit--)
|
|
{
|
|
value |= ReadBit() << bit;
|
|
}
|
|
|
|
return value;
|
|
}
|
|
|
|
public int ReadSignedLiteral(int bits)
|
|
{
|
|
int value = ReadLiteral(bits);
|
|
return ReadBit() != 0 ? -value : value;
|
|
}
|
|
|
|
public int ReadInvSignedLiteral(int bits)
|
|
{
|
|
return ReadSignedLiteral(bits);
|
|
}
|
|
|
|
public int ReadDeltaQ()
|
|
{
|
|
return ReadBit() != 0 ? ReadSignedLiteral(4) : 0;
|
|
}
|
|
|
|
public void ReadFrameSize(out int width, out int height)
|
|
{
|
|
width = ReadLiteral(16) + 1;
|
|
height = ReadLiteral(16) + 1;
|
|
}
|
|
|
|
public BitstreamProfile ReadProfile()
|
|
{
|
|
int profile = ReadBit();
|
|
profile |= ReadBit() << 1;
|
|
if (profile > 2)
|
|
{
|
|
profile += ReadBit();
|
|
}
|
|
|
|
return (BitstreamProfile)profile;
|
|
}
|
|
}
|
|
} |