FFmpegContext.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 av_log_set_callback_callback _logFunc;
  12. private readonly AVCodec* _codec;
  13. private AVPacket* _packet;
  14. private AVCodecContext* _context;
  15. public FFmpegContext()
  16. {
  17. _logFunc = Log;
  18. // Redirect log output
  19. ffmpeg.av_log_set_level(ffmpeg.AV_LOG_MAX_OFFSET);
  20. ffmpeg.av_log_set_callback(_logFunc);
  21. _codec = ffmpeg.avcodec_find_decoder(AVCodecID.AV_CODEC_ID_H264);
  22. _context = ffmpeg.avcodec_alloc_context3(_codec);
  23. ffmpeg.avcodec_open2(_context, _codec, null);
  24. _packet = ffmpeg.av_packet_alloc();
  25. }
  26. static FFmpegContext()
  27. {
  28. SetRootPath();
  29. }
  30. private static void SetRootPath()
  31. {
  32. if (OperatingSystem.IsLinux())
  33. {
  34. // Configure FFmpeg search path
  35. Process lddProcess = Process.Start(new ProcessStartInfo
  36. {
  37. FileName = "/bin/sh",
  38. Arguments = "-c \"ldd $(which ffmpeg 2>/dev/null) | grep libavfilter\" 2>/dev/null",
  39. UseShellExecute = false,
  40. RedirectStandardOutput = true
  41. });
  42. string lddOutput = lddProcess.StandardOutput.ReadToEnd();
  43. lddProcess.WaitForExit();
  44. lddProcess.Close();
  45. if (lddOutput.Contains(" => "))
  46. {
  47. ffmpeg.RootPath = Path.GetDirectoryName(lddOutput.Split(" => ")[1]);
  48. }
  49. else
  50. {
  51. Logger.Error?.PrintMsg(LogClass.FFmpeg, "FFmpeg wasn't found. Make sure that you have it installed and up to date.");
  52. }
  53. }
  54. }
  55. private void Log(void* p0, int level, string format, byte* vl)
  56. {
  57. if (level > ffmpeg.av_log_get_level())
  58. {
  59. return;
  60. }
  61. int lineSize = 1024;
  62. byte* lineBuffer = stackalloc byte[lineSize];
  63. int printPrefix = 1;
  64. ffmpeg.av_log_format_line(p0, level, format, vl, lineBuffer, lineSize, &printPrefix);
  65. string line = Marshal.PtrToStringAnsi((IntPtr)lineBuffer).Trim();
  66. switch (level)
  67. {
  68. case ffmpeg.AV_LOG_PANIC:
  69. case ffmpeg.AV_LOG_FATAL:
  70. case ffmpeg.AV_LOG_ERROR:
  71. Logger.Error?.Print(LogClass.FFmpeg, line);
  72. break;
  73. case ffmpeg.AV_LOG_WARNING:
  74. Logger.Warning?.Print(LogClass.FFmpeg, line);
  75. break;
  76. case ffmpeg.AV_LOG_INFO:
  77. Logger.Info?.Print(LogClass.FFmpeg, line);
  78. break;
  79. case ffmpeg.AV_LOG_VERBOSE:
  80. case ffmpeg.AV_LOG_DEBUG:
  81. case ffmpeg.AV_LOG_TRACE:
  82. Logger.Debug?.Print(LogClass.FFmpeg, line);
  83. break;
  84. }
  85. }
  86. public int DecodeFrame(Surface output, ReadOnlySpan<byte> bitstream)
  87. {
  88. // Ensure the packet is clean before proceeding
  89. ffmpeg.av_packet_unref(_packet);
  90. fixed (byte* ptr = bitstream)
  91. {
  92. _packet->data = ptr;
  93. _packet->size = bitstream.Length;
  94. int rc = ffmpeg.avcodec_send_packet(_context, _packet);
  95. if (rc != 0)
  96. {
  97. return rc;
  98. }
  99. }
  100. return ffmpeg.avcodec_receive_frame(_context, output.Frame);
  101. }
  102. public void Dispose()
  103. {
  104. fixed (AVPacket** ppPacket = &_packet)
  105. {
  106. ffmpeg.av_packet_free(ppPacket);
  107. }
  108. ffmpeg.avcodec_close(_context);
  109. fixed (AVCodecContext** ppContext = &_context)
  110. {
  111. ffmpeg.avcodec_free_context(ppContext);
  112. }
  113. }
  114. }
  115. }