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