FFmpegContext.cs 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. using FFmpeg.AutoGen;
  2. using Ryujinx.Common.Logging;
  3. using System;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Runtime.InteropServices;
  7. namespace Ryujinx.Graphics.Nvdec.H264
  8. {
  9. unsafe class FFmpegContext : IDisposable
  10. {
  11. private readonly AVCodec_decode _h264Decode;
  12. private static readonly av_log_set_callback_callback _logFunc;
  13. private readonly AVCodec* _codec;
  14. private AVPacket* _packet;
  15. private AVCodecContext* _context;
  16. public FFmpegContext()
  17. {
  18. _codec = ffmpeg.avcodec_find_decoder(AVCodecID.AV_CODEC_ID_H264);
  19. _context = ffmpeg.avcodec_alloc_context3(_codec);
  20. _context->debug |= ffmpeg.FF_DEBUG_MMCO;
  21. ffmpeg.avcodec_open2(_context, _codec, null);
  22. _packet = ffmpeg.av_packet_alloc();
  23. _h264Decode = Marshal.GetDelegateForFunctionPointer<AVCodec_decode>(_codec->decode.Pointer);
  24. }
  25. static FFmpegContext()
  26. {
  27. SetRootPath();
  28. _logFunc = Log;
  29. // Redirect log output.
  30. ffmpeg.av_log_set_level(ffmpeg.AV_LOG_MAX_OFFSET);
  31. ffmpeg.av_log_set_callback(_logFunc);
  32. }
  33. private static void SetRootPath()
  34. {
  35. if (OperatingSystem.IsLinux())
  36. {
  37. // Configure FFmpeg search path
  38. Process lddProcess = Process.Start(new ProcessStartInfo
  39. {
  40. FileName = "/bin/sh",
  41. Arguments = "-c \"ldd $(which ffmpeg 2>/dev/null) | grep libavfilter\" 2>/dev/null",
  42. UseShellExecute = false,
  43. RedirectStandardOutput = true
  44. });
  45. string lddOutput = lddProcess.StandardOutput.ReadToEnd();
  46. lddProcess.WaitForExit();
  47. lddProcess.Close();
  48. if (lddOutput.Contains(" => "))
  49. {
  50. ffmpeg.RootPath = Path.GetDirectoryName(lddOutput.Split(" => ")[1]);
  51. }
  52. else
  53. {
  54. Logger.Error?.PrintMsg(LogClass.FFmpeg, "FFmpeg wasn't found. Make sure that you have it installed and up to date.");
  55. }
  56. }
  57. }
  58. private static void Log(void* p0, int level, string format, byte* vl)
  59. {
  60. if (level > ffmpeg.av_log_get_level())
  61. {
  62. return;
  63. }
  64. int lineSize = 1024;
  65. byte* lineBuffer = stackalloc byte[lineSize];
  66. int printPrefix = 1;
  67. ffmpeg.av_log_format_line(p0, level, format, vl, lineBuffer, lineSize, &printPrefix);
  68. string line = Marshal.PtrToStringAnsi((IntPtr)lineBuffer).Trim();
  69. switch (level)
  70. {
  71. case ffmpeg.AV_LOG_PANIC:
  72. case ffmpeg.AV_LOG_FATAL:
  73. case ffmpeg.AV_LOG_ERROR:
  74. Logger.Error?.Print(LogClass.FFmpeg, line);
  75. break;
  76. case ffmpeg.AV_LOG_WARNING:
  77. Logger.Warning?.Print(LogClass.FFmpeg, line);
  78. break;
  79. case ffmpeg.AV_LOG_INFO:
  80. Logger.Info?.Print(LogClass.FFmpeg, line);
  81. break;
  82. case ffmpeg.AV_LOG_VERBOSE:
  83. case ffmpeg.AV_LOG_DEBUG:
  84. case ffmpeg.AV_LOG_TRACE:
  85. Logger.Debug?.Print(LogClass.FFmpeg, line);
  86. break;
  87. }
  88. }
  89. public int DecodeFrame(Surface output, ReadOnlySpan<byte> bitstream)
  90. {
  91. ffmpeg.av_frame_unref(output.Frame);
  92. int result;
  93. int gotFrame;
  94. fixed (byte* ptr = bitstream)
  95. {
  96. _packet->data = ptr;
  97. _packet->size = bitstream.Length;
  98. result = _h264Decode(_context, output.Frame, &gotFrame, _packet);
  99. }
  100. if (gotFrame == 0)
  101. {
  102. ffmpeg.av_frame_unref(output.Frame);
  103. // If the frame was not delivered, it was probably delayed.
  104. // Get the next delayed frame by passing a 0 length packet.
  105. _packet->data = null;
  106. _packet->size = 0;
  107. result = _h264Decode(_context, output.Frame, &gotFrame, _packet);
  108. // We need to set B frames to 0 as we already consumed all delayed frames.
  109. // This prevents the decoder from trying to return a delayed frame next time.
  110. _context->has_b_frames = 0;
  111. }
  112. ffmpeg.av_packet_unref(_packet);
  113. if (gotFrame == 0)
  114. {
  115. ffmpeg.av_frame_unref(output.Frame);
  116. return -1;
  117. }
  118. return result < 0 ? result : 0;
  119. }
  120. public void Dispose()
  121. {
  122. fixed (AVPacket** ppPacket = &_packet)
  123. {
  124. ffmpeg.av_packet_free(ppPacket);
  125. }
  126. ffmpeg.avcodec_close(_context);
  127. fixed (AVCodecContext** ppContext = &_context)
  128. {
  129. ffmpeg.avcodec_free_context(ppContext);
  130. }
  131. }
  132. }
  133. }