2022-07-14 15:13:23 +02:00
|
|
|
|
using Ryujinx.Graphics.Nvdec.FFmpeg.Native;
|
2021-10-12 22:55:57 +02:00
|
|
|
|
using Ryujinx.Graphics.Video;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
2021-10-12 22:55:57 +02:00
|
|
|
|
namespace Ryujinx.Graphics.Nvdec.FFmpeg.H264
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2020-08-20 05:07:04 +02:00
|
|
|
|
public sealed class Decoder : IH264Decoder
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
|
|
|
|
public bool IsHardwareAccelerated => false;
|
|
|
|
|
|
|
|
|
|
private const int WorkBufferSize = 0x200;
|
|
|
|
|
|
|
|
|
|
private readonly byte[] _workBuffer = new byte[WorkBufferSize];
|
|
|
|
|
|
2021-10-12 22:55:57 +02:00
|
|
|
|
private FFmpegContext _context = new FFmpegContext(AVCodecID.AV_CODEC_ID_H264);
|
2020-10-11 11:09:38 +02:00
|
|
|
|
|
|
|
|
|
private int _oldOutputWidth;
|
|
|
|
|
private int _oldOutputHeight;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
|
|
|
|
public ISurface CreateSurface(int width, int height)
|
|
|
|
|
{
|
2020-10-11 11:09:38 +02:00
|
|
|
|
return new Surface(width, height);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool Decode(ref H264PictureInfo pictureInfo, ISurface output, ReadOnlySpan<byte> bitstream)
|
|
|
|
|
{
|
2020-10-11 11:09:38 +02:00
|
|
|
|
Surface outSurf = (Surface)output;
|
|
|
|
|
|
|
|
|
|
if (outSurf.RequestedWidth != _oldOutputWidth ||
|
|
|
|
|
outSurf.RequestedHeight != _oldOutputHeight)
|
|
|
|
|
{
|
|
|
|
|
_context.Dispose();
|
2021-10-12 22:55:57 +02:00
|
|
|
|
_context = new FFmpegContext(AVCodecID.AV_CODEC_ID_H264);
|
2020-10-11 11:09:38 +02:00
|
|
|
|
|
|
|
|
|
_oldOutputWidth = outSurf.RequestedWidth;
|
|
|
|
|
_oldOutputHeight = outSurf.RequestedHeight;
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 05:07:01 +02:00
|
|
|
|
Span<byte> bs = Prepend(bitstream, SpsAndPpsReconstruction.Reconstruct(ref pictureInfo, _workBuffer));
|
|
|
|
|
|
2020-10-11 11:09:38 +02:00
|
|
|
|
return _context.DecodeFrame(outSurf, bs) == 0;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static byte[] Prepend(ReadOnlySpan<byte> data, ReadOnlySpan<byte> prep)
|
|
|
|
|
{
|
|
|
|
|
byte[] output = new byte[data.Length + prep.Length];
|
|
|
|
|
|
|
|
|
|
prep.CopyTo(output);
|
|
|
|
|
data.CopyTo(new Span<byte>(output).Slice(prep.Length));
|
|
|
|
|
|
|
|
|
|
return output;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() => _context.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|