2023-12-04 13:17:13 +00:00
|
|
|
using Ryujinx.Graphics.Nvdec.FFmpeg.H264;
|
2020-07-12 04:07:01 +01:00
|
|
|
using Ryujinx.Graphics.Nvdec.Image;
|
|
|
|
using Ryujinx.Graphics.Nvdec.Types.H264;
|
|
|
|
using Ryujinx.Graphics.Video;
|
|
|
|
using System;
|
|
|
|
|
|
|
|
namespace Ryujinx.Graphics.Nvdec
|
|
|
|
{
|
|
|
|
static class H264Decoder
|
|
|
|
{
|
|
|
|
private const int MbSizeInPixels = 16;
|
|
|
|
|
2021-10-12 21:55:57 +01:00
|
|
|
public static void Decode(NvdecDecoderContext context, ResourceManager rm, ref NvdecRegisters state)
|
2020-07-12 04:07:01 +01:00
|
|
|
{
|
2023-01-02 14:48:46 +00:00
|
|
|
PictureInfo pictureInfo = rm.Gmm.DeviceRead<PictureInfo>(state.SetDrvPicSetupOffset);
|
2020-07-12 04:07:01 +01:00
|
|
|
H264PictureInfo info = pictureInfo.Convert();
|
|
|
|
|
2023-01-02 14:48:46 +00:00
|
|
|
ReadOnlySpan<byte> bitstream = rm.Gmm.DeviceGetSpan(state.SetInBufBaseOffset, (int)pictureInfo.BitstreamSize);
|
2020-07-12 04:07:01 +01:00
|
|
|
|
2023-06-25 20:44:42 +01:00
|
|
|
int width = (int)pictureInfo.PicWidthInMbs * MbSizeInPixels;
|
2020-07-12 04:07:01 +01:00
|
|
|
int height = (int)pictureInfo.PicHeightInMbs * MbSizeInPixels;
|
|
|
|
|
2021-09-28 23:43:40 +01:00
|
|
|
int surfaceIndex = (int)pictureInfo.OutputSurfaceIndex;
|
2020-07-12 04:07:01 +01:00
|
|
|
|
2023-06-25 20:44:42 +01:00
|
|
|
uint lumaOffset = state.SetPictureLumaOffset[surfaceIndex];
|
2023-01-02 14:48:46 +00:00
|
|
|
uint chromaOffset = state.SetPictureChromaOffset[surfaceIndex];
|
2020-07-12 04:07:01 +01:00
|
|
|
|
2021-10-12 21:55:57 +01:00
|
|
|
Decoder decoder = context.GetH264Decoder();
|
2020-07-12 04:07:01 +01:00
|
|
|
|
2021-09-28 23:43:40 +01:00
|
|
|
ISurface outputSurface = rm.Cache.Get(decoder, 0, 0, width, height);
|
2020-07-12 04:07:01 +01:00
|
|
|
|
2021-09-28 23:43:40 +01:00
|
|
|
if (decoder.Decode(ref info, outputSurface, bitstream))
|
|
|
|
{
|
2022-03-23 20:09:32 +00:00
|
|
|
if (outputSurface.Field == FrameField.Progressive)
|
|
|
|
{
|
|
|
|
SurfaceWriter.Write(
|
|
|
|
rm.Gmm,
|
|
|
|
outputSurface,
|
2023-06-25 20:44:42 +01:00
|
|
|
lumaOffset + pictureInfo.LumaFrameOffset,
|
2022-03-23 20:09:32 +00:00
|
|
|
chromaOffset + pictureInfo.ChromaFrameOffset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SurfaceWriter.WriteInterlaced(
|
|
|
|
rm.Gmm,
|
|
|
|
outputSurface,
|
2023-06-25 20:44:42 +01:00
|
|
|
lumaOffset + pictureInfo.LumaTopFieldOffset,
|
2022-03-23 20:09:32 +00:00
|
|
|
chromaOffset + pictureInfo.ChromaTopFieldOffset,
|
2023-06-25 20:44:42 +01:00
|
|
|
lumaOffset + pictureInfo.LumaBottomFieldOffset,
|
2022-03-23 20:09:32 +00:00
|
|
|
chromaOffset + pictureInfo.ChromaBottomFieldOffset);
|
|
|
|
}
|
2020-07-12 04:07:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rm.Cache.Put(outputSurface);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|