Decoder.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using Ryujinx.Graphics.Nvdec.FFmpeg.Native;
  2. using Ryujinx.Graphics.Video;
  3. using System;
  4. namespace Ryujinx.Graphics.Nvdec.FFmpeg.Vp8
  5. {
  6. public sealed class Decoder : IDecoder
  7. {
  8. public bool IsHardwareAccelerated => false;
  9. private readonly FFmpegContext _context = new FFmpegContext(AVCodecID.AV_CODEC_ID_VP8);
  10. public ISurface CreateSurface(int width, int height)
  11. {
  12. return new Surface(width, height);
  13. }
  14. public bool Decode(ref Vp8PictureInfo pictureInfo, ISurface output, ReadOnlySpan<byte> bitstream)
  15. {
  16. Surface outSurf = (Surface)output;
  17. int uncompHeaderSize = pictureInfo.KeyFrame ? 10 : 3;
  18. byte[] frame = new byte[bitstream.Length + uncompHeaderSize];
  19. uint firstPartSizeShifted = pictureInfo.FirstPartSize << 5;
  20. frame[0] = (byte)(pictureInfo.KeyFrame ? 0 : 1);
  21. frame[0] |= (byte)((pictureInfo.Version & 7) << 1);
  22. frame[0] |= 1 << 4;
  23. frame[0] |= (byte)firstPartSizeShifted;
  24. frame[1] |= (byte)(firstPartSizeShifted >> 8);
  25. frame[2] |= (byte)(firstPartSizeShifted >> 16);
  26. if (pictureInfo.KeyFrame)
  27. {
  28. frame[3] = 0x9d;
  29. frame[4] = 0x01;
  30. frame[5] = 0x2a;
  31. frame[6] = (byte)pictureInfo.FrameWidth;
  32. frame[7] = (byte)((pictureInfo.FrameWidth >> 8) & 0x3F);
  33. frame[8] = (byte)pictureInfo.FrameHeight;
  34. frame[9] = (byte)((pictureInfo.FrameHeight >> 8) & 0x3F);
  35. }
  36. bitstream.CopyTo(new Span<byte>(frame).Slice(uncompHeaderSize));
  37. return _context.DecodeFrame(outSurf, frame) == 0;
  38. }
  39. public void Dispose() => _context.Dispose();
  40. }
  41. }