2022-07-14 15:13:23 +02:00
|
|
|
|
using Ryujinx.Common.Logging;
|
|
|
|
|
using Ryujinx.Graphics.Nvdec.FFmpeg.Native;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
using System;
|
2021-05-05 23:30:29 +02:00
|
|
|
|
using System.Runtime.InteropServices;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-10-12 22:55:57 +02:00
|
|
|
|
namespace Ryujinx.Graphics.Nvdec.FFmpeg
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
|
|
|
|
unsafe class FFmpegContext : IDisposable
|
|
|
|
|
{
|
2022-11-02 09:26:50 +01:00
|
|
|
|
private unsafe delegate int AVCodec_decode(AVCodecContext* avctx, void* outdata, int* got_frame_ptr, AVPacket* avpkt);
|
|
|
|
|
|
|
|
|
|
private readonly AVCodec_decode _decodeFrame;
|
2022-07-14 15:13:23 +02:00
|
|
|
|
private static readonly FFmpegApi.av_log_set_callback_callback _logFunc;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
private readonly AVCodec* _codec;
|
2021-05-02 22:08:35 +02:00
|
|
|
|
private AVPacket* _packet;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
private AVCodecContext* _context;
|
|
|
|
|
|
2021-10-12 22:55:57 +02:00
|
|
|
|
public FFmpegContext(AVCodecID codecId)
|
2020-07-12 05:07:01 +02:00
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
_codec = FFmpegApi.avcodec_find_decoder(codecId);
|
2022-01-03 12:38:21 +01: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 15:13:23 +02:00
|
|
|
|
_context = FFmpegApi.avcodec_alloc_context3(_codec);
|
2022-01-03 12:38:21 +01:00
|
|
|
|
if (_context == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, "Codec context couldn't be allocated.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2022-07-14 15:13:23 +02:00
|
|
|
|
if (FFmpegApi.avcodec_open2(_context, _codec, null) != 0)
|
2022-01-03 12:38:21 +01:00
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, "Codec couldn't be opened.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-05-02 22:08:35 +02:00
|
|
|
|
|
2022-07-14 15:13:23 +02:00
|
|
|
|
_packet = FFmpegApi.av_packet_alloc();
|
2022-01-03 12:38:21 +01:00
|
|
|
|
if (_packet == null)
|
|
|
|
|
{
|
|
|
|
|
Logger.Error?.PrintMsg(LogClass.FFmpeg, "Packet couldn't be allocated.");
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2021-09-29 00:43:40 +02:00
|
|
|
|
|
2022-07-14 15:13:23 +02: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))
|
|
|
|
|
{
|
2022-11-02 09:26:50 +01:00
|
|
|
|
_decodeFrame = Marshal.GetDelegateForFunctionPointer<AVCodec_decode>(((FFCodec<AVCodec>*)_codec)->CodecCallback);
|
2022-07-14 15:13:23 +02:00
|
|
|
|
}
|
|
|
|
|
// libavcodec 59.x changed AvCodec private API layout.
|
|
|
|
|
else if (avCodecMajorVersion == 59)
|
|
|
|
|
{
|
2022-11-02 09:26:50 +01:00
|
|
|
|
_decodeFrame = Marshal.GetDelegateForFunctionPointer<AVCodec_decode>(((FFCodecLegacy<AVCodec501>*)_codec)->Decode);
|
2022-07-14 15:13:23 +02:00
|
|
|
|
}
|
|
|
|
|
// libavcodec 58.x and lower
|
|
|
|
|
else
|
|
|
|
|
{
|
2022-11-02 09:26:50 +01:00
|
|
|
|
_decodeFrame = Marshal.GetDelegateForFunctionPointer<AVCodec_decode>(((FFCodecLegacy<AVCodec>*)_codec)->Decode);
|
2022-07-14 15:13:23 +02:00
|
|
|
|
}
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-05-20 18:28:18 +02:00
|
|
|
|
static FFmpegContext()
|
|
|
|
|
{
|
2021-09-29 00:43:40 +02:00
|
|
|
|
_logFunc = Log;
|
|
|
|
|
|
|
|
|
|
// Redirect log output.
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_log_set_level(AVLog.MaxOffset);
|
|
|
|
|
FFmpegApi.av_log_set_callback(_logFunc);
|
2021-05-20 18:28:18 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 15:13:23 +02:00
|
|
|
|
private static void Log(void* ptr, AVLog level, string format, byte* vl)
|
2021-05-20 18:28:18 +02:00
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
if (level > FFmpegApi.av_log_get_level())
|
2021-05-05 23:30:29 +02:00
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int lineSize = 1024;
|
|
|
|
|
byte* lineBuffer = stackalloc byte[lineSize];
|
|
|
|
|
int printPrefix = 1;
|
|
|
|
|
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_log_format_line(ptr, level, format, vl, lineBuffer, lineSize, &printPrefix);
|
2021-05-05 23:30:29 +02:00
|
|
|
|
|
|
|
|
|
string line = Marshal.PtrToStringAnsi((IntPtr)lineBuffer).Trim();
|
|
|
|
|
|
|
|
|
|
switch (level)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
case AVLog.Panic:
|
|
|
|
|
case AVLog.Fatal:
|
|
|
|
|
case AVLog.Error:
|
2021-05-05 23:30:29 +02:00
|
|
|
|
Logger.Error?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 15:13:23 +02:00
|
|
|
|
case AVLog.Warning:
|
2021-05-05 23:30:29 +02:00
|
|
|
|
Logger.Warning?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 15:13:23 +02:00
|
|
|
|
case AVLog.Info:
|
2021-05-05 23:30:29 +02:00
|
|
|
|
Logger.Info?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 15:13:23 +02:00
|
|
|
|
case AVLog.Verbose:
|
|
|
|
|
case AVLog.Debug:
|
2021-05-05 23:30:29 +02:00
|
|
|
|
Logger.Debug?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2022-07-14 15:13:23 +02:00
|
|
|
|
case AVLog.Trace:
|
|
|
|
|
Logger.Trace?.Print(LogClass.FFmpeg, line);
|
|
|
|
|
break;
|
2021-05-05 23:30:29 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-07-12 05:07:01 +02:00
|
|
|
|
public int DecodeFrame(Surface output, ReadOnlySpan<byte> bitstream)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_frame_unref(output.Frame);
|
2021-10-04 19:12:24 +02:00
|
|
|
|
|
2021-09-29 00:43:40 +02:00
|
|
|
|
int result;
|
|
|
|
|
int gotFrame;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
|
|
|
|
fixed (byte* ptr = bitstream)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
_packet->Data = ptr;
|
|
|
|
|
_packet->Size = bitstream.Length;
|
2021-10-12 22:55:57 +02:00
|
|
|
|
result = _decodeFrame(_context, output.Frame, &gotFrame, _packet);
|
2021-09-29 00:43:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (gotFrame == 0)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_frame_unref(output.Frame);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-09-29 00:43:40 +02: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 15:13:23 +02:00
|
|
|
|
_packet->Data = null;
|
|
|
|
|
_packet->Size = 0;
|
2021-10-12 22:55:57 +02:00
|
|
|
|
result = _decodeFrame(_context, output.Frame, &gotFrame, _packet);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
2021-09-29 00:43:40 +02: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 15:13:23 +02:00
|
|
|
|
_context->HasBFrames = 0;
|
2021-09-29 00:43:40 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_packet_unref(_packet);
|
2021-09-29 00:43:40 +02:00
|
|
|
|
|
|
|
|
|
if (gotFrame == 0)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_frame_unref(output.Frame);
|
2021-10-12 22:55:57 +02:00
|
|
|
|
|
2021-09-29 00:43:40 +02:00
|
|
|
|
return -1;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
2021-09-29 00:43:40 +02:00
|
|
|
|
return result < 0 ? result : 0;
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
|
{
|
2021-05-02 22:08:35 +02:00
|
|
|
|
fixed (AVPacket** ppPacket = &_packet)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.av_packet_free(ppPacket);
|
2021-05-02 22:08:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.avcodec_close(_context);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
|
|
|
|
|
fixed (AVCodecContext** ppContext = &_context)
|
|
|
|
|
{
|
2022-07-14 15:13:23 +02:00
|
|
|
|
FFmpegApi.avcodec_free_context(ppContext);
|
2020-07-12 05:07:01 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|