2022-07-14 13:13:23 +00:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.Nvdec.FFmpeg.Native;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
using System;
|
2021-05-05 21:30:29 +00:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
|
2021-10-12 20:55:57 +00:00
|
|
|
|
namespace Ryujinx.Graphics.Nvdec.FFmpeg
|
2020-07-12 03:07:01 +00:00
|
|
|
|
{
|
|
|
|
|
unsafe class FFmpegContext : IDisposable
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
private readonly FFCodec.AVCodec_decode _decodeFrame;
|
|
|
|
|
private static readonly FFmpegApi.av_log_set_callback_callback _logFunc;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
private readonly AVCodec* _codec;
|
2021-05-02 20:08:35 +00:00
|
|
|
|
private AVPacket* _packet;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
private AVCodecContext* _context;
|
|
|
|
|
|
2021-10-12 20:55:57 +00:00
|
|
|
|
public FFmpegContext(AVCodecID codecId)
|
2020-07-12 03:07:01 +00:00
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
_codec = FFmpegApi.avcodec_find_decoder(codecId);
|
2022-01-03 11:38:21 +00:00
|
|
|
|
if (_codec == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, $"Codec wasn't found. Make sure you have the {codecId} codec present in your FFmpeg installation.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
_context = FFmpegApi.avcodec_alloc_context3(_codec);
|
2022-01-03 11:38:21 +00:00
|
|
|
|
if (_context == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, "Codec context couldn't be allocated.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-12 03:07:01 +00:00
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
if (FFmpegApi.avcodec_open2(_context, _codec, null) != 0)
|
2022-01-03 11:38:21 +00:00
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, "Codec couldn't be opened.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-02 20:08:35 +00:00
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
_packet = FFmpegApi.av_packet_alloc();
|
2022-01-03 11:38:21 +00:00
|
|
|
|
if (_packet == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, "Packet couldn't be allocated.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-28 22:43:40 +00:00
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
int avCodecRawVersion = FFmpegApi.avcodec_version();
|
|
|
|
|
int avCodecMajorVersion = avCodecRawVersion >> 16;
|
|
|
|
|
int avCodecMinorVersion = (avCodecRawVersion >> 8) & 0xFF;
|
|
|
|
|
|
|
|
|
|
// libavcodec 59.24 changed AvCodec to move its private API and also move the codec function to an union.
|
|
|
|
|
if (avCodecMajorVersion > 59 || (avCodecMajorVersion == 59 && avCodecMinorVersion > 24))
|
|
|
|
|
{
|
|
|
|
|
_decodeFrame = Marshal.GetDelegateForFunctionPointer<FFCodec.AVCodec_decode>(((FFCodec*)_codec)->CodecCallback);
|
|
|
|
|
}
|
|
|
|
|
// libavcodec 59.x changed AvCodec private API layout.
|
|
|
|
|
else if (avCodecMajorVersion == 59)
|
|
|
|
|
{
|
|
|
|
|
_decodeFrame = Marshal.GetDelegateForFunctionPointer<FFCodec.AVCodec_decode>(((FFCodecLegacy<AVCodec>*)_codec)->Decode);
|
|
|
|
|
}
|
|
|
|
|
// libavcodec 58.x and lower
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_decodeFrame = Marshal.GetDelegateForFunctionPointer<FFCodec.AVCodec_decode>(((FFCodecLegacy<AVCodecLegacy>*)_codec)->Decode);
|
|
|
|
|
}
|
2020-07-12 03:07:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 16:28:18 +00:00
|
|
|
|
static FFmpegContext()
|
|
|
|
|
{
|
2021-09-28 22:43:40 +00:00
|
|
|
|
_logFunc = Log;
|
|
|
|
|
|
|
|
|
|
// Redirect log output.
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_log_set_level(AVLog.MaxOffset);
|
|
|
|
|
FFmpegApi.av_log_set_callback(_logFunc);
|
2021-05-20 16:28:18 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
private static void Log(void* ptr, AVLog level, string format, byte* vl)
|
2021-05-20 16:28:18 +00:00
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
if (level > FFmpegApi.av_log_get_level())
|
2021-05-05 21:30:29 +00:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lineSize = 1024;
|
|
|
|
|
byte* lineBuffer = stackalloc byte[lineSize];
|
|
|
|
|
int printPrefix = 1;
|
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_log_format_line(ptr, level, format, vl, lineBuffer, lineSize, &printPrefix);
|
2021-05-05 21:30:29 +00:00
|
|
|
|
|
|
|
|
|
string line = Marshal.PtrToStringAnsi((IntPtr)lineBuffer).Trim();
|
|
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
case AVLog.Panic:
|
|
|
|
|
case AVLog.Fatal:
|
|
|
|
|
case AVLog.Error:
|
2021-05-05 21:30:29 +00:00
|
|
|
|
Logger.Error?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 13:13:23 +00:00
|
|
|
|
case AVLog.Warning:
|
2021-05-05 21:30:29 +00:00
|
|
|
|
Logger.Warning?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 13:13:23 +00:00
|
|
|
|
case AVLog.Info:
|
2021-05-05 21:30:29 +00:00
|
|
|
|
Logger.Info?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 13:13:23 +00:00
|
|
|
|
case AVLog.Verbose:
|
|
|
|
|
case AVLog.Debug:
|
2021-05-05 21:30:29 +00:00
|
|
|
|
Logger.Debug?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 13:13:23 +00:00
|
|
|
|
case AVLog.Trace:
|
|
|
|
|
Logger.Trace?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2021-05-05 21:30:29 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 03:07:01 +00:00
|
|
|
|
public int DecodeFrame(Surface output, ReadOnlySpan<byte> bitstream)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_frame_unref(output.Frame);
|
2021-10-04 17:12:24 +00:00
|
|
|
|
|
2021-09-28 22:43:40 +00:00
|
|
|
|
int result;
|
|
|
|
|
int gotFrame;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
|
|
|
|
|
fixed (byte* ptr = bitstream)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
_packet->Data = ptr;
|
|
|
|
|
_packet->Size = bitstream.Length;
|
2021-10-12 20:55:57 +00:00
|
|
|
|
result = _decodeFrame(_context, output.Frame, &gotFrame, _packet);
|
2021-09-28 22:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gotFrame == 0)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_frame_unref(output.Frame);
|
2020-07-12 03:07:01 +00:00
|
|
|
|
|
2021-09-28 22:43:40 +00:00
|
|
|
|
// If the frame was not delivered, it was probably delayed.
|
|
|
|
|
// Get the next delayed frame by passing a 0 length packet.
|
2022-07-14 13:13:23 +00:00
|
|
|
|
_packet->Data = null;
|
|
|
|
|
_packet->Size = 0;
|
2021-10-12 20:55:57 +00:00
|
|
|
|
result = _decodeFrame(_context, output.Frame, &gotFrame, _packet);
|
2020-07-12 03:07:01 +00:00
|
|
|
|
|
2021-09-28 22:43:40 +00:00
|
|
|
|
// We need to set B frames to 0 as we already consumed all delayed frames.
|
|
|
|
|
// This prevents the decoder from trying to return a delayed frame next time.
|
2022-07-14 13:13:23 +00:00
|
|
|
|
_context->HasBFrames = 0;
|
2021-09-28 22:43:40 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_packet_unref(_packet);
|
2021-09-28 22:43:40 +00:00
|
|
|
|
|
|
|
|
|
if (gotFrame == 0)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_frame_unref(output.Frame);
|
2021-10-12 20:55:57 +00:00
|
|
|
|
|
2021-09-28 22:43:40 +00:00
|
|
|
|
return -1;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-28 22:43:40 +00:00
|
|
|
|
return result < 0 ? result : 0;
|
2020-07-12 03:07:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-05-02 20:08:35 +00:00
|
|
|
|
fixed (AVPacket** ppPacket = &_packet)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.av_packet_free(ppPacket);
|
2021-05-02 20:08:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.avcodec_close(_context);
|
2020-07-12 03:07:01 +00:00
|
|
|
|
|
|
|
|
|
fixed (AVCodecContext** ppContext = &_context)
|
|
|
|
|
{
|
2022-07-14 13:13:23 +00:00
|
|
|
|
FFmpegApi.avcodec_free_context(ppContext);
|
2020-07-12 03:07:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|