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>
59 lines
1.7 KiB
C#
59 lines
1.7 KiB
C#
using System;
|
|
using System.Diagnostics;
|
|
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
namespace Ryujinx.Graphics.Nvdec.Vp9.Common
|
|
{
|
|
internal static class BitUtils
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static byte ClipPixel(int val)
|
|
{
|
|
return (byte)(val > 255 ? 255 : val < 0 ? 0 : val);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static ushort ClipPixelHighbd(int val, int bd)
|
|
{
|
|
return bd switch
|
|
{
|
|
10 => (ushort)Math.Clamp(val, 0, 1023),
|
|
12 => (ushort)Math.Clamp(val, 0, 4095),
|
|
_ => (ushort)Math.Clamp(val, 0, 255),
|
|
};
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int RoundPowerOfTwo(int value, int n)
|
|
{
|
|
return (value + (1 << (n - 1))) >> n;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static long RoundPowerOfTwo(long value, int n)
|
|
{
|
|
return (value + (1L << (n - 1))) >> n;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int AlignPowerOfTwo(int value, int n)
|
|
{
|
|
return (value + ((1 << n) - 1)) & ~((1 << n) - 1);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static int GetMsb(uint n)
|
|
{
|
|
Debug.Assert(n != 0);
|
|
|
|
return 31 ^ BitOperations.LeadingZeroCount(n);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int GetUnsignedBits(uint numValues)
|
|
{
|
|
return numValues > 0 ? GetMsb(numValues) + 1 : 0;
|
|
}
|
|
}
|
|
} |