FFmpegContext.cs 5.8 KB

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