From 4d02a2d2c0451b4de1f6de3bbce54c457cacebe2 Mon Sep 17 00:00:00 2001 From: gdkchan Date: Sun, 12 Jul 2020 00:07:01 -0300 Subject: New NVDEC and VIC implementation (#1384) * Initial NVDEC and VIC implementation * Update FFmpeg.AutoGen to 4.3.0 * Add nvdec dependencies for Windows * Unify some VP9 structures * Rename VP9 structure fields * Improvements to Video API * XML docs for Common.Memory * Remove now unused or redundant overloads from MemoryAccessor * NVDEC UV surface read/write scalar paths * Add FIXME comments about hacky things/stuff that will need to be fixed in the future * Cleaned up VP9 memory allocation * Remove some debug logs * Rename some VP9 structs * Remove unused struct * No need to compile Ryujinx.Graphics.Host1x with unsafe anymore * Name AsyncWorkQueue threads to make debugging easier * Make Vp9PictureInfo a ref struct * LayoutConverter no longer needs the depth argument (broken by rebase) * Pooling of VP9 buffers, plus fix a memory leak on VP9 * Really wish VS could rename projects properly... * Address feedback * Remove using * Catch OperationCanceledException * Add licensing informations * Add THIRDPARTY.md to release too Co-authored-by: Thog --- Ryujinx.Graphics.Nvdec/Vp9Decoder.cs | 92 ++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 Ryujinx.Graphics.Nvdec/Vp9Decoder.cs (limited to 'Ryujinx.Graphics.Nvdec/Vp9Decoder.cs') diff --git a/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs b/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs new file mode 100644 index 00000000..f05555c6 --- /dev/null +++ b/Ryujinx.Graphics.Nvdec/Vp9Decoder.cs @@ -0,0 +1,92 @@ +using Ryujinx.Common; +using Ryujinx.Graphics.Gpu.Memory; +using Ryujinx.Graphics.Nvdec.Image; +using Ryujinx.Graphics.Nvdec.Types.Vp9; +using Ryujinx.Graphics.Nvdec.Vp9; +using Ryujinx.Graphics.Video; +using System; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; +using static Ryujinx.Graphics.Nvdec.MemoryExtensions; + +namespace Ryujinx.Graphics.Nvdec +{ + static class Vp9Decoder + { + private static Decoder _decoder = new Decoder(); + + public unsafe static void Decode(NvdecDevice device, ResourceManager rm, ref NvdecRegisters state) + { + PictureInfo pictureInfo = rm.Gmm.DeviceRead(state.SetPictureInfoOffset); + EntropyProbs entropy = rm.Gmm.DeviceRead(state.SetVp9EntropyProbsOffset); + + ISurface Rent(uint lumaOffset, uint chromaOffset, FrameSize size) + { + return rm.Cache.Get(_decoder, CodecId.Vp9, lumaOffset, chromaOffset, size.Width, size.Height); + } + + ISurface lastSurface = Rent(state.SetSurfaceLumaOffset[0], state.SetSurfaceChromaOffset[0], pictureInfo.LastFrameSize); + ISurface goldenSurface = Rent(state.SetSurfaceLumaOffset[1], state.SetSurfaceChromaOffset[1], pictureInfo.GoldenFrameSize); + ISurface altSurface = Rent(state.SetSurfaceLumaOffset[2], state.SetSurfaceChromaOffset[2], pictureInfo.AltFrameSize); + ISurface currentSurface = Rent(state.SetSurfaceLumaOffset[3], state.SetSurfaceChromaOffset[3], pictureInfo.CurrentFrameSize); + + Vp9PictureInfo info = pictureInfo.Convert(); + + info.LastReference = lastSurface; + info.GoldenReference = goldenSurface; + info.AltReference = altSurface; + + entropy.Convert(ref info.Entropy); + + ReadOnlySpan bitstream = rm.Gmm.DeviceGetSpan(state.SetBitstreamOffset, (int)pictureInfo.BitstreamSize); + + ReadOnlySpan mvsIn = ReadOnlySpan.Empty; + + if (info.UsePrevInFindMvRefs) + { + mvsIn = GetMvsInput(rm.Gmm, pictureInfo.CurrentFrameSize, state.SetVp9LastFrameMvsOffset); + } + + int miCols = BitUtils.DivRoundUp(pictureInfo.CurrentFrameSize.Width, 8); + int miRows = BitUtils.DivRoundUp(pictureInfo.CurrentFrameSize.Height, 8); + + using var mvsRegion = rm.Gmm.GetWritableRegion(ExtendOffset(state.SetVp9CurrFrameMvsOffset), miRows * miCols * 16); + + Span mvsOut = MemoryMarshal.Cast(mvsRegion.Memory.Span); + + uint lumaOffset = state.SetSurfaceLumaOffset[3]; + uint chromaOffset = state.SetSurfaceChromaOffset[3]; + + if (_decoder.Decode(ref info, currentSurface, bitstream, mvsIn, mvsOut)) + { + SurfaceWriter.Write(rm.Gmm, currentSurface, lumaOffset, chromaOffset); + + device.OnFrameDecoded(CodecId.Vp9, lumaOffset, chromaOffset); + } + + WriteBackwardUpdates(rm.Gmm, state.SetVp9BackwardUpdatesOffset, ref info.BackwardUpdateCounts); + + rm.Cache.Put(lastSurface); + rm.Cache.Put(goldenSurface); + rm.Cache.Put(altSurface); + rm.Cache.Put(currentSurface); + } + + private static ReadOnlySpan GetMvsInput(MemoryManager gmm, FrameSize size, uint offset) + { + int miCols = BitUtils.DivRoundUp(size.Width, 8); + int miRows = BitUtils.DivRoundUp(size.Height, 8); + + return MemoryMarshal.Cast(gmm.DeviceGetSpan(offset, miRows * miCols * 16)); + } + + private static void WriteBackwardUpdates(MemoryManager gmm, uint offset, ref Vp9BackwardUpdates counts) + { + using var backwardUpdatesRegion = gmm.GetWritableRegion(ExtendOffset(offset), Unsafe.SizeOf()); + + ref var backwardUpdates = ref MemoryMarshal.Cast(backwardUpdatesRegion.Memory.Span)[0]; + + backwardUpdates = new BackwardUpdates(ref counts); + } + } +} -- cgit v1.2.3