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