FFmpeg.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using FFmpeg.AutoGen;
  2. using System;
  3. using System.Runtime.InteropServices;
  4. namespace Ryujinx.Graphics.VDec
  5. {
  6. static unsafe class FFmpegWrapper
  7. {
  8. private static AVCodec* _codec;
  9. private static AVCodecContext* _context;
  10. private static AVFrame* _frame;
  11. private static SwsContext* _scalerCtx;
  12. private static int _scalerWidth;
  13. private static int _scalerHeight;
  14. public static bool IsInitialized { get; private set; }
  15. public static void H264Initialize()
  16. {
  17. EnsureCodecInitialized(AVCodecID.AV_CODEC_ID_H264);
  18. }
  19. public static void Vp9Initialize()
  20. {
  21. EnsureCodecInitialized(AVCodecID.AV_CODEC_ID_VP9);
  22. }
  23. private static void EnsureCodecInitialized(AVCodecID codecId)
  24. {
  25. if (IsInitialized)
  26. {
  27. Uninitialize();
  28. }
  29. _codec = ffmpeg.avcodec_find_decoder(codecId);
  30. _context = ffmpeg.avcodec_alloc_context3(_codec);
  31. _frame = ffmpeg.av_frame_alloc();
  32. ffmpeg.avcodec_open2(_context, _codec, null);
  33. IsInitialized = true;
  34. }
  35. public static int DecodeFrame(byte[] data)
  36. {
  37. if (!IsInitialized)
  38. {
  39. throw new InvalidOperationException("Tried to use uninitialized codec!");
  40. }
  41. AVPacket packet;
  42. ffmpeg.av_init_packet(&packet);
  43. fixed (byte* ptr = data)
  44. {
  45. packet.data = ptr;
  46. packet.size = data.Length;
  47. ffmpeg.avcodec_send_packet(_context, &packet);
  48. }
  49. return ffmpeg.avcodec_receive_frame(_context, _frame);
  50. }
  51. public static FFmpegFrame GetFrame()
  52. {
  53. if (!IsInitialized)
  54. {
  55. throw new InvalidOperationException("Tried to use uninitialized codec!");
  56. }
  57. AVFrame managedFrame = Marshal.PtrToStructure<AVFrame>((IntPtr)_frame);
  58. byte*[] data = managedFrame.data.ToArray();
  59. return new FFmpegFrame()
  60. {
  61. Width = managedFrame.width,
  62. Height = managedFrame.height,
  63. LumaPtr = data[0],
  64. ChromaBPtr = data[1],
  65. ChromaRPtr = data[2]
  66. };
  67. }
  68. public static FFmpegFrame GetFrameRgba()
  69. {
  70. if (!IsInitialized)
  71. {
  72. throw new InvalidOperationException("Tried to use uninitialized codec!");
  73. }
  74. AVFrame managedFrame = Marshal.PtrToStructure<AVFrame>((IntPtr)_frame);
  75. EnsureScalerSetup(managedFrame.width, managedFrame.height);
  76. byte*[] data = managedFrame.data.ToArray();
  77. int[] lineSizes = managedFrame.linesize.ToArray();
  78. byte[] dst = new byte[managedFrame.width * managedFrame.height * 4];
  79. fixed (byte* ptr = dst)
  80. {
  81. byte*[] dstData = new byte*[] { ptr };
  82. int[] dstLineSizes = new int[] { managedFrame.width * 4 };
  83. ffmpeg.sws_scale(_scalerCtx, data, lineSizes, 0, managedFrame.height, dstData, dstLineSizes);
  84. }
  85. return new FFmpegFrame()
  86. {
  87. Width = managedFrame.width,
  88. Height = managedFrame.height,
  89. Data = dst
  90. };
  91. }
  92. private static void EnsureScalerSetup(int width, int height)
  93. {
  94. if (width == 0 || height == 0)
  95. {
  96. return;
  97. }
  98. if (_scalerCtx == null || _scalerWidth != width || _scalerHeight != height)
  99. {
  100. FreeScaler();
  101. _scalerCtx = ffmpeg.sws_getContext(
  102. width, height, AVPixelFormat.AV_PIX_FMT_YUV420P,
  103. width, height, AVPixelFormat.AV_PIX_FMT_RGBA, 0, null, null, null);
  104. _scalerWidth = width;
  105. _scalerHeight = height;
  106. }
  107. }
  108. public static void Uninitialize()
  109. {
  110. if (IsInitialized)
  111. {
  112. ffmpeg.av_frame_unref(_frame);
  113. ffmpeg.av_free(_frame);
  114. ffmpeg.avcodec_close(_context);
  115. FreeScaler();
  116. IsInitialized = false;
  117. }
  118. }
  119. private static void FreeScaler()
  120. {
  121. if (_scalerCtx != null)
  122. {
  123. ffmpeg.sws_freeContext(_scalerCtx);
  124. _scalerCtx = null;
  125. }
  126. }
  127. }
  128. }