2015-05-24 04:55:12 +00:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2021-07-05 01:22:19 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2023-01-28 00:50:57 +00:00
|
|
|
#include "VideoCommon/FrameDumpFFMpeg.h"
|
2020-10-21 14:37:16 +00:00
|
|
|
|
2011-08-27 18:42:11 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
|
|
|
#endif
|
|
|
|
|
2022-03-24 11:47:43 +00:00
|
|
|
#include <array>
|
2017-02-25 03:56:33 +00:00
|
|
|
#include <sstream>
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
#include <fmt/chrono.h>
|
2019-11-22 22:10:41 +00:00
|
|
|
#include <fmt/format.h>
|
|
|
|
|
2016-01-02 18:07:08 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
2022-03-24 11:47:43 +00:00
|
|
|
#include <libavutil/error.h>
|
|
|
|
#include <libavutil/log.h>
|
2016-01-02 18:07:08 +00:00
|
|
|
#include <libavutil/mathematics.h>
|
2022-03-23 19:26:22 +00:00
|
|
|
#include <libavutil/opt.h>
|
2022-04-08 22:45:49 +00:00
|
|
|
#include <libavutil/pixdesc.h>
|
2016-01-02 18:07:08 +00:00
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
#include "Common/ChunkFile.h"
|
2014-06-03 05:08:54 +00:00
|
|
|
#include "Common/FileUtil.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
2022-05-19 18:55:27 +00:00
|
|
|
#include "Common/Logging/LogManager.h"
|
2016-01-17 21:54:31 +00:00
|
|
|
#include "Common/MsgHandler.h"
|
2022-05-19 18:55:27 +00:00
|
|
|
#include "Common/StringUtil.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
|
2021-12-27 18:39:35 +00:00
|
|
|
#include "Core/Config/MainSettings.h"
|
2016-01-03 22:20:42 +00:00
|
|
|
#include "Core/ConfigManager.h"
|
2014-10-20 09:11:56 +00:00
|
|
|
#include "Core/HW/SystemTimers.h"
|
2020-10-21 14:37:16 +00:00
|
|
|
#include "Core/HW/VideoInterface.h"
|
2023-03-10 21:14:54 +00:00
|
|
|
#include "Core/System.h"
|
2016-10-16 11:26:37 +00:00
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
#include "VideoCommon/FrameDumper.h"
|
2016-10-16 11:26:37 +00:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
struct FrameDumpContext
|
|
|
|
{
|
|
|
|
AVFormatContext* format = nullptr;
|
|
|
|
AVStream* stream = nullptr;
|
|
|
|
AVCodecContext* codec = nullptr;
|
|
|
|
AVFrame* src_frame = nullptr;
|
|
|
|
AVFrame* scaled_frame = nullptr;
|
|
|
|
SwsContext* sws = nullptr;
|
|
|
|
|
|
|
|
s64 last_pts = AV_NOPTS_VALUE;
|
|
|
|
|
|
|
|
int width = 0;
|
|
|
|
int height = 0;
|
|
|
|
|
2020-11-21 21:25:57 +00:00
|
|
|
u64 start_ticks = 0;
|
2020-10-21 14:37:16 +00:00
|
|
|
u32 savestate_index = 0;
|
|
|
|
|
|
|
|
bool gave_vfr_warning = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
AVRational GetTimeBaseForCurrentRefreshRate()
|
|
|
|
{
|
2023-03-10 21:14:54 +00:00
|
|
|
auto& vi = Core::System::GetInstance().GetVideoInterface();
|
2020-10-21 14:37:16 +00:00
|
|
|
int num;
|
|
|
|
int den;
|
2023-03-10 21:14:54 +00:00
|
|
|
av_reduce(&num, &den, int(vi.GetTargetRefreshRateDenominator()),
|
|
|
|
int(vi.GetTargetRefreshRateNumerator()), std::numeric_limits<int>::max());
|
2020-10-21 14:37:16 +00:00
|
|
|
return AVRational{num, den};
|
|
|
|
}
|
|
|
|
|
|
|
|
void InitAVCodec()
|
2010-11-14 21:14:26 +00:00
|
|
|
{
|
|
|
|
static bool first_run = true;
|
|
|
|
if (first_run)
|
|
|
|
{
|
2022-03-24 11:47:43 +00:00
|
|
|
av_log_set_level(AV_LOG_DEBUG);
|
|
|
|
av_log_set_callback([](void* ptr, int level, const char* fmt, va_list vl) {
|
|
|
|
if (level < 0)
|
|
|
|
level = AV_LOG_DEBUG;
|
|
|
|
if (level >= 0)
|
|
|
|
level &= 0xff;
|
|
|
|
|
|
|
|
if (level > av_log_get_level())
|
|
|
|
return;
|
|
|
|
|
|
|
|
auto log_level = Common::Log::LogLevel::LNOTICE;
|
|
|
|
if (level >= AV_LOG_ERROR && level < AV_LOG_WARNING)
|
|
|
|
log_level = Common::Log::LogLevel::LERROR;
|
|
|
|
else if (level >= AV_LOG_WARNING && level < AV_LOG_INFO)
|
|
|
|
log_level = Common::Log::LogLevel::LWARNING;
|
|
|
|
else if (level >= AV_LOG_INFO && level < AV_LOG_DEBUG)
|
|
|
|
log_level = Common::Log::LogLevel::LINFO;
|
|
|
|
else if (level >= AV_LOG_DEBUG)
|
|
|
|
// keep libav debug messages visible in release build of dolphin
|
|
|
|
log_level = Common::Log::LogLevel::LINFO;
|
|
|
|
|
2022-05-19 18:55:27 +00:00
|
|
|
// Don't perform this formatting if the log level is disabled
|
|
|
|
auto* log_manager = Common::Log::LogManager::GetInstance();
|
|
|
|
if (log_manager != nullptr &&
|
|
|
|
log_manager->IsEnabled(Common::Log::LogType::FRAMEDUMP, log_level))
|
|
|
|
{
|
|
|
|
constexpr size_t MAX_MSGLEN = 1024;
|
|
|
|
char message[MAX_MSGLEN];
|
|
|
|
CharArrayFromFormatV(message, MAX_MSGLEN, fmt, vl);
|
|
|
|
|
|
|
|
GENERIC_LOG_FMT(Common::Log::LogType::FRAMEDUMP, log_level, "{}", message);
|
|
|
|
}
|
2022-03-24 11:47:43 +00:00
|
|
|
});
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
// TODO: We never call avformat_network_deinit.
|
2017-02-21 19:37:36 +00:00
|
|
|
avformat_network_init();
|
2022-03-24 11:47:43 +00:00
|
|
|
|
2010-11-14 21:14:26 +00:00
|
|
|
first_run = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
std::string GetDumpPath(const std::string& extension, std::time_t time, u32 index)
|
2010-11-14 21:14:26 +00:00
|
|
|
{
|
2017-02-25 09:02:20 +00:00
|
|
|
if (!g_Config.sDumpPath.empty())
|
|
|
|
return g_Config.sDumpPath;
|
2017-02-21 09:43:31 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
const std::string path_prefix =
|
|
|
|
File::GetUserPath(D_DUMPFRAMES_IDX) + SConfig::GetInstance().GetGameID();
|
|
|
|
|
|
|
|
const std::string base_name =
|
2021-10-15 20:49:13 +00:00
|
|
|
fmt::format("{}_{:%Y-%m-%d_%H-%M-%S}_{}", path_prefix, fmt::localtime(time), index);
|
2020-10-21 14:37:16 +00:00
|
|
|
|
|
|
|
const std::string path = fmt::format("{}.{}", base_name, extension);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
// Ask to delete file.
|
|
|
|
if (File::Exists(path))
|
2016-01-03 22:20:42 +00:00
|
|
|
{
|
2021-12-27 18:39:35 +00:00
|
|
|
if (Config::Get(Config::MAIN_MOVIE_DUMP_FRAMES_SILENT) ||
|
2020-12-02 18:17:27 +00:00
|
|
|
AskYesNoFmtT("Delete the existing file '{0}'?", path))
|
2017-02-25 09:02:20 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
File::Delete(path);
|
2017-02-25 09:02:20 +00:00
|
|
|
}
|
|
|
|
else
|
2016-01-03 22:20:42 +00:00
|
|
|
{
|
2017-02-25 09:02:20 +00:00
|
|
|
// Stop and cancel dumping the video
|
|
|
|
return "";
|
2016-06-24 08:43:46 +00:00
|
|
|
}
|
2016-01-03 22:20:42 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2022-03-24 11:47:43 +00:00
|
|
|
std::string AVErrorString(int error)
|
|
|
|
{
|
|
|
|
std::array<char, AV_ERROR_MAX_STRING_SIZE> msg;
|
|
|
|
av_make_error_string(&msg[0], msg.size(), error);
|
|
|
|
return fmt::format("{:8x} {}", (u32)error, &msg[0]);
|
|
|
|
}
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
} // namespace
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
bool FFMpegFrameDump::Start(int w, int h, u64 start_ticks)
|
2020-10-21 14:37:16 +00:00
|
|
|
{
|
|
|
|
if (IsStarted())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
m_savestate_index = 0;
|
|
|
|
m_start_time = std::time(nullptr);
|
|
|
|
m_file_index = 0;
|
|
|
|
|
2020-11-21 21:25:57 +00:00
|
|
|
return PrepareEncoding(w, h, start_ticks, m_savestate_index);
|
2020-10-21 14:37:16 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
bool FFMpegFrameDump::PrepareEncoding(int w, int h, u64 start_ticks, u32 savestate_index)
|
2020-10-21 14:37:16 +00:00
|
|
|
{
|
|
|
|
m_context = std::make_unique<FrameDumpContext>();
|
|
|
|
|
|
|
|
m_context->width = w;
|
|
|
|
m_context->height = h;
|
|
|
|
|
2020-11-21 21:25:57 +00:00
|
|
|
m_context->start_ticks = start_ticks;
|
|
|
|
m_context->savestate_index = savestate_index;
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
InitAVCodec();
|
|
|
|
const bool success = CreateVideoFile();
|
|
|
|
if (!success)
|
|
|
|
{
|
|
|
|
CloseVideoFile();
|
|
|
|
OSD::AddMessage("FrameDump Start failed");
|
|
|
|
}
|
|
|
|
return success;
|
2017-02-25 09:02:20 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
bool FFMpegFrameDump::CreateVideoFile()
|
2017-02-25 09:02:20 +00:00
|
|
|
{
|
2018-01-17 21:11:44 +00:00
|
|
|
const std::string& format = g_Config.sDumpFormat;
|
2017-02-25 09:02:20 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
const std::string dump_path = GetDumpPath(format, m_start_time, m_file_index);
|
2017-02-25 09:02:20 +00:00
|
|
|
|
2018-01-17 21:11:44 +00:00
|
|
|
if (dump_path.empty())
|
2017-02-25 09:02:20 +00:00
|
|
|
return false;
|
|
|
|
|
2018-01-17 21:11:44 +00:00
|
|
|
File::CreateFullPath(dump_path);
|
2018-01-17 12:10:04 +00:00
|
|
|
|
2022-01-30 19:16:51 +00:00
|
|
|
auto* const output_format = av_guess_format(format.c_str(), dump_path.c_str(), nullptr);
|
2017-02-21 10:43:49 +00:00
|
|
|
if (!output_format)
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Invalid format {}", format);
|
2017-02-21 22:24:15 +00:00
|
|
|
return false;
|
|
|
|
}
|
2017-03-06 01:29:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
if (avformat_alloc_output_context2(&m_context->format, output_format, nullptr,
|
|
|
|
dump_path.c_str()) < 0)
|
2017-02-21 22:24:15 +00:00
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Could not allocate output context");
|
2017-02-21 10:43:49 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2017-02-25 09:01:21 +00:00
|
|
|
const std::string& codec_name = g_Config.bUseFFV1 ? "ffv1" : g_Config.sDumpCodec;
|
2017-02-25 09:28:51 +00:00
|
|
|
|
|
|
|
AVCodecID codec_id = output_format->video_codec;
|
|
|
|
|
|
|
|
if (!codec_name.empty())
|
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
const AVCodecDescriptor* const codec_desc = avcodec_descriptor_get_by_name(codec_name.c_str());
|
2017-02-25 09:28:51 +00:00
|
|
|
if (codec_desc)
|
|
|
|
codec_id = codec_desc->id;
|
|
|
|
else
|
2020-11-09 18:45:04 +00:00
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "Invalid codec {}", codec_name);
|
2017-02-25 09:28:51 +00:00
|
|
|
}
|
2017-02-21 19:04:22 +00:00
|
|
|
|
|
|
|
const AVCodec* codec = nullptr;
|
2017-02-21 09:43:31 +00:00
|
|
|
|
2017-12-29 14:00:55 +00:00
|
|
|
if (!g_Config.sDumpEncoder.empty())
|
|
|
|
{
|
|
|
|
codec = avcodec_find_encoder_by_name(g_Config.sDumpEncoder.c_str());
|
|
|
|
if (!codec)
|
2020-11-09 18:45:04 +00:00
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "Invalid encoder {}", g_Config.sDumpEncoder);
|
2017-12-29 14:00:55 +00:00
|
|
|
}
|
|
|
|
if (!codec)
|
|
|
|
codec = avcodec_find_encoder(codec_id);
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->codec = avcodec_alloc_context3(codec);
|
|
|
|
if (!codec || !m_context->codec)
|
2017-01-03 23:50:17 +00:00
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Could not find encoder or allocate codec context");
|
2017-01-03 23:50:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-03-18 04:50:53 +00:00
|
|
|
// Force XVID FourCC for better compatibility when using H.263
|
2017-02-21 18:06:46 +00:00
|
|
|
if (codec->id == AV_CODEC_ID_MPEG4)
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->codec->codec_tag = MKTAG('X', 'V', 'I', 'D');
|
|
|
|
|
|
|
|
const auto time_base = GetTimeBaseForCurrentRefreshRate();
|
|
|
|
|
2020-11-09 18:45:04 +00:00
|
|
|
INFO_LOG_FMT(FRAMEDUMP, "Creating video file: {} x {} @ {}/{} fps", m_context->width,
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->height, time_base.den, time_base.num);
|
|
|
|
|
|
|
|
m_context->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
m_context->codec->bit_rate = static_cast<int64_t>(g_Config.iBitrateKbps) * 1000;
|
|
|
|
m_context->codec->width = m_context->width;
|
|
|
|
m_context->codec->height = m_context->height;
|
|
|
|
m_context->codec->time_base = time_base;
|
|
|
|
m_context->codec->gop_size = 1;
|
|
|
|
m_context->codec->level = 1;
|
2022-03-23 19:26:22 +00:00
|
|
|
|
2022-04-08 22:45:49 +00:00
|
|
|
AVPixelFormat pix_fmt = AV_PIX_FMT_NONE;
|
|
|
|
|
|
|
|
const std::string& pixel_format_string = g_Config.sDumpPixelFormat;
|
|
|
|
if (!pixel_format_string.empty())
|
2022-03-23 19:26:22 +00:00
|
|
|
{
|
2022-04-08 22:45:49 +00:00
|
|
|
pix_fmt = av_get_pix_fmt(pixel_format_string.c_str());
|
|
|
|
if (pix_fmt == AV_PIX_FMT_NONE)
|
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "Invalid pixel format {}", pixel_format_string);
|
2022-03-23 19:26:22 +00:00
|
|
|
}
|
2022-04-08 22:45:49 +00:00
|
|
|
|
|
|
|
if (pix_fmt == AV_PIX_FMT_NONE)
|
2022-03-23 19:26:22 +00:00
|
|
|
{
|
2022-04-08 22:45:49 +00:00
|
|
|
if (m_context->codec->codec_id == AV_CODEC_ID_FFV1)
|
|
|
|
pix_fmt = AV_PIX_FMT_BGR0;
|
|
|
|
else if (m_context->codec->codec_id == AV_CODEC_ID_UTVIDEO)
|
|
|
|
pix_fmt = AV_PIX_FMT_GBRP;
|
|
|
|
else
|
|
|
|
pix_fmt = AV_PIX_FMT_YUV420P;
|
2022-03-23 19:26:22 +00:00
|
|
|
}
|
2017-01-03 01:43:47 +00:00
|
|
|
|
2022-04-08 22:45:49 +00:00
|
|
|
m_context->codec->pix_fmt = pix_fmt;
|
|
|
|
|
|
|
|
if (m_context->codec->codec_id == AV_CODEC_ID_UTVIDEO)
|
|
|
|
av_opt_set_int(m_context->codec->priv_data, "pred", 3, 0); // median
|
|
|
|
|
2017-02-21 19:49:37 +00:00
|
|
|
if (output_format->flags & AVFMT_GLOBALHEADER)
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->codec->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
2017-02-21 19:49:37 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
if (avcodec_open2(m_context->codec, codec, nullptr) < 0)
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Could not open codec");
|
2010-11-18 16:46:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->src_frame = av_frame_alloc();
|
|
|
|
m_context->scaled_frame = av_frame_alloc();
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->scaled_frame->format = m_context->codec->pix_fmt;
|
|
|
|
m_context->scaled_frame->width = m_context->width;
|
|
|
|
m_context->scaled_frame->height = m_context->height;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
if (av_frame_get_buffer(m_context->scaled_frame, 1))
|
2016-07-11 01:36:18 +00:00
|
|
|
return false;
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->stream = avformat_new_stream(m_context->format, codec);
|
2020-10-23 19:32:10 +00:00
|
|
|
if (!m_context->stream ||
|
|
|
|
avcodec_parameters_from_context(m_context->stream->codecpar, m_context->codec) < 0)
|
2017-01-03 01:43:47 +00:00
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Could not create stream");
|
2017-01-03 01:43:47 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-10-23 19:32:10 +00:00
|
|
|
m_context->stream->time_base = m_context->codec->time_base;
|
|
|
|
|
2020-11-09 18:45:04 +00:00
|
|
|
NOTICE_LOG_FMT(FRAMEDUMP, "Opening file {} for dumping", dump_path);
|
2020-10-21 14:37:16 +00:00
|
|
|
if (avio_open(&m_context->format->pb, dump_path.c_str(), AVIO_FLAG_WRITE) < 0 ||
|
|
|
|
avformat_write_header(m_context->format, nullptr))
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Could not open {}", dump_path);
|
2010-11-18 16:46:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
if (av_cmp_q(m_context->stream->time_base, time_base) != 0)
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "Stream time base differs at {}/{}", m_context->stream->time_base.den,
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->stream->time_base.num);
|
|
|
|
}
|
|
|
|
|
|
|
|
OSD::AddMessage(fmt::format("Dumping Frames to \"{}\" ({}x{})", dump_path, m_context->width,
|
|
|
|
m_context->height));
|
2010-11-14 21:14:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
bool FFMpegFrameDump::IsFirstFrameInCurrentFile() const
|
2014-06-15 03:16:58 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
return m_context->last_pts == AV_NOPTS_VALUE;
|
2014-06-15 03:16:58 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
void FFMpegFrameDump::AddFrame(const FrameData& frame)
|
2017-01-03 00:45:20 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
// Are we even dumping?
|
|
|
|
if (!IsStarted())
|
|
|
|
return;
|
2017-01-03 00:45:20 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
CheckForConfigChange(frame);
|
2017-01-03 00:45:20 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
// Handle failure after a config change.
|
|
|
|
if (!IsStarted())
|
|
|
|
return;
|
2017-01-03 00:45:20 +00:00
|
|
|
|
2020-11-21 21:25:57 +00:00
|
|
|
// Calculate presentation timestamp from ticks since start.
|
2024-01-04 22:07:50 +00:00
|
|
|
const s64 pts = av_rescale_q(
|
|
|
|
frame.state.ticks - m_context->start_ticks,
|
|
|
|
AVRational{1, int(Core::System::GetInstance().GetSystemTimers().GetTicksPerSecond())},
|
|
|
|
m_context->codec->time_base);
|
2020-08-11 09:16:33 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
if (!IsFirstFrameInCurrentFile())
|
2016-11-04 17:24:03 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
if (pts <= m_context->last_pts)
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "PTS delta < 1. Current frame will not be dumped.");
|
2020-10-21 14:37:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (pts > m_context->last_pts + 1 && !m_context->gave_vfr_warning)
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "PTS delta > 1. Resulting file will have variable frame rate. "
|
2020-11-21 21:25:57 +00:00
|
|
|
"Subsequent occurrences will not be reported.");
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->gave_vfr_warning = true;
|
|
|
|
}
|
2016-11-04 17:24:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
constexpr AVPixelFormat pix_fmt = AV_PIX_FMT_RGBA;
|
|
|
|
|
|
|
|
m_context->src_frame->data[0] = const_cast<u8*>(frame.data);
|
|
|
|
m_context->src_frame->linesize[0] = frame.stride;
|
|
|
|
m_context->src_frame->format = pix_fmt;
|
|
|
|
m_context->src_frame->width = m_context->width;
|
|
|
|
m_context->src_frame->height = m_context->height;
|
|
|
|
|
|
|
|
// Convert image from RGBA to desired pixel format.
|
|
|
|
m_context->sws = sws_getCachedContext(
|
|
|
|
m_context->sws, frame.width, frame.height, pix_fmt, m_context->width, m_context->height,
|
|
|
|
m_context->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr);
|
|
|
|
if (m_context->sws)
|
2011-03-25 18:12:40 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
sws_scale(m_context->sws, m_context->src_frame->data, m_context->src_frame->linesize, 0,
|
|
|
|
frame.height, m_context->scaled_frame->data, m_context->scaled_frame->linesize);
|
2011-03-25 18:12:40 +00:00
|
|
|
}
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->last_pts = pts;
|
|
|
|
m_context->scaled_frame->pts = pts;
|
|
|
|
|
2020-10-23 19:32:10 +00:00
|
|
|
if (const int error = avcodec_send_frame(m_context->codec, m_context->scaled_frame))
|
2020-10-21 14:37:16 +00:00
|
|
|
{
|
2022-03-24 11:47:43 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Error while encoding video: {}", AVErrorString(error));
|
2020-10-21 14:37:16 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-10-23 19:32:10 +00:00
|
|
|
ProcessPackets();
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
void FFMpegFrameDump::ProcessPackets()
|
2017-02-21 08:24:06 +00:00
|
|
|
{
|
2021-05-17 18:38:39 +00:00
|
|
|
auto pkt = std::unique_ptr<AVPacket, std::function<void(AVPacket*)>>(
|
|
|
|
av_packet_alloc(), [](AVPacket* packet) { av_packet_free(&packet); });
|
|
|
|
|
|
|
|
if (!pkt)
|
2017-02-21 08:24:06 +00:00
|
|
|
{
|
2021-05-17 18:38:39 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Could not allocate packet");
|
|
|
|
return;
|
|
|
|
}
|
2020-10-21 14:37:16 +00:00
|
|
|
|
2021-05-17 18:38:39 +00:00
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
const int receive_error = avcodec_receive_packet(m_context->codec, pkt.get());
|
2020-10-23 19:32:10 +00:00
|
|
|
|
|
|
|
if (receive_error == AVERROR(EAGAIN) || receive_error == AVERROR_EOF)
|
2017-02-21 08:24:06 +00:00
|
|
|
{
|
2020-10-23 19:32:10 +00:00
|
|
|
// We have processed all available packets.
|
2017-02-21 08:24:06 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-03-06 01:29:46 +00:00
|
|
|
|
2020-10-23 19:32:10 +00:00
|
|
|
if (receive_error)
|
|
|
|
{
|
2022-03-24 11:47:43 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Error receiving packet: {}", AVErrorString(receive_error));
|
2017-02-21 08:24:06 +00:00
|
|
|
break;
|
2020-10-23 19:32:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-17 18:38:39 +00:00
|
|
|
av_packet_rescale_ts(pkt.get(), m_context->codec->time_base, m_context->stream->time_base);
|
|
|
|
pkt->stream_index = m_context->stream->index;
|
2017-03-06 01:29:46 +00:00
|
|
|
|
2021-05-17 18:38:39 +00:00
|
|
|
if (const int write_error = av_interleaved_write_frame(m_context->format, pkt.get()))
|
2020-10-23 19:32:10 +00:00
|
|
|
{
|
2022-03-24 11:47:43 +00:00
|
|
|
ERROR_LOG_FMT(FRAMEDUMP, "Error writing packet: {}", AVErrorString(write_error));
|
2020-10-23 19:32:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2017-02-21 08:24:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
void FFMpegFrameDump::Stop()
|
2010-11-14 21:14:26 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
if (!IsStarted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
// Signal end of stream to encoder.
|
|
|
|
if (const int flush_error = avcodec_send_frame(m_context->codec, nullptr))
|
2022-03-24 11:47:43 +00:00
|
|
|
WARN_LOG_FMT(FRAMEDUMP, "Error sending flush packet: {}", AVErrorString(flush_error));
|
2020-10-21 14:37:16 +00:00
|
|
|
|
2020-10-23 19:32:10 +00:00
|
|
|
ProcessPackets();
|
2020-10-21 14:37:16 +00:00
|
|
|
av_write_trailer(m_context->format);
|
2017-01-30 03:01:05 +00:00
|
|
|
CloseVideoFile();
|
2020-10-23 19:32:10 +00:00
|
|
|
|
2020-11-09 18:45:04 +00:00
|
|
|
NOTICE_LOG_FMT(FRAMEDUMP, "Stopping frame dump");
|
2016-10-08 01:11:37 +00:00
|
|
|
OSD::AddMessage("Stopped dumping frames");
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
bool FFMpegFrameDump::IsStarted() const
|
2020-10-21 14:37:16 +00:00
|
|
|
{
|
|
|
|
return m_context != nullptr;
|
|
|
|
}
|
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
void FFMpegFrameDump::CloseVideoFile()
|
2010-11-14 21:14:26 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
av_frame_free(&m_context->src_frame);
|
|
|
|
av_frame_free(&m_context->scaled_frame);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
avcodec_free_context(&m_context->codec);
|
2017-02-27 04:14:02 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
if (m_context->format)
|
|
|
|
avio_closep(&m_context->format->pb);
|
2016-06-24 08:43:46 +00:00
|
|
|
|
2020-10-21 14:37:16 +00:00
|
|
|
avformat_free_context(m_context->format);
|
|
|
|
|
|
|
|
if (m_context->sws)
|
|
|
|
sws_freeContext(m_context->sws);
|
|
|
|
|
|
|
|
m_context.reset();
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
2015-01-26 01:35:29 +00:00
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
void FFMpegFrameDump::DoState(PointerWrap& p)
|
2015-01-26 01:35:29 +00:00
|
|
|
{
|
2022-05-18 05:29:05 +00:00
|
|
|
if (p.IsReadMode())
|
2020-10-21 14:37:16 +00:00
|
|
|
++m_savestate_index;
|
2015-01-26 01:35:29 +00:00
|
|
|
}
|
2016-06-25 02:41:10 +00:00
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
void FFMpegFrameDump::CheckForConfigChange(const FrameData& frame)
|
2016-06-25 02:41:10 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
bool restart_dump = false;
|
|
|
|
|
2016-07-20 20:46:53 +00:00
|
|
|
// We check here to see if the requested width and height have changed since the last frame which
|
|
|
|
// was dumped, then create a new file accordingly. However, is it possible for the height
|
|
|
|
// (possibly width as well, but no examples known) to have a value of zero. This can occur as the
|
|
|
|
// VI is able to be set to a zero value for height/width to disable output. If this is the case,
|
|
|
|
// simply keep the last known resolution of the video for the added frame.
|
2020-10-21 14:37:16 +00:00
|
|
|
if ((frame.width != m_context->width || frame.height != m_context->height) &&
|
|
|
|
(frame.width > 0 && frame.height > 0))
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
INFO_LOG_FMT(FRAMEDUMP, "Starting new dump on resolution change.");
|
2020-10-21 14:37:16 +00:00
|
|
|
restart_dump = true;
|
|
|
|
}
|
|
|
|
else if (!IsFirstFrameInCurrentFile() &&
|
|
|
|
frame.state.savestate_index != m_context->savestate_index)
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
INFO_LOG_FMT(FRAMEDUMP, "Starting new dump on savestate load.");
|
2020-10-21 14:37:16 +00:00
|
|
|
restart_dump = true;
|
|
|
|
}
|
|
|
|
else if (frame.state.refresh_rate_den != m_context->codec->time_base.num ||
|
|
|
|
frame.state.refresh_rate_num != m_context->codec->time_base.den)
|
|
|
|
{
|
2020-11-09 18:45:04 +00:00
|
|
|
INFO_LOG_FMT(FRAMEDUMP, "Starting new dump on refresh rate change {}/{} vs {}/{}.",
|
2020-10-21 14:37:16 +00:00
|
|
|
m_context->codec->time_base.den, m_context->codec->time_base.num,
|
|
|
|
frame.state.refresh_rate_num, frame.state.refresh_rate_den);
|
|
|
|
restart_dump = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (restart_dump)
|
2016-06-25 02:41:10 +00:00
|
|
|
{
|
|
|
|
Stop();
|
2020-10-21 14:37:16 +00:00
|
|
|
++m_file_index;
|
2020-11-21 21:25:57 +00:00
|
|
|
PrepareEncoding(frame.width, frame.height, frame.state.ticks, frame.state.savestate_index);
|
2016-06-25 02:41:10 +00:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 17:19:35 +00:00
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
FrameState FFMpegFrameDump::FetchState(u64 ticks, int frame_number) const
|
2016-11-04 17:19:35 +00:00
|
|
|
{
|
2020-10-21 14:37:16 +00:00
|
|
|
FrameState state;
|
2016-11-04 17:19:35 +00:00
|
|
|
state.ticks = ticks;
|
2020-11-21 21:25:57 +00:00
|
|
|
state.frame_number = frame_number;
|
2020-10-21 14:37:16 +00:00
|
|
|
state.savestate_index = m_savestate_index;
|
|
|
|
|
|
|
|
const auto time_base = GetTimeBaseForCurrentRefreshRate();
|
|
|
|
state.refresh_rate_num = time_base.den;
|
|
|
|
state.refresh_rate_den = time_base.num;
|
2016-11-04 17:19:35 +00:00
|
|
|
return state;
|
|
|
|
}
|
2020-10-21 14:37:16 +00:00
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
FFMpegFrameDump::FFMpegFrameDump() = default;
|
2020-10-21 14:37:16 +00:00
|
|
|
|
2023-01-27 04:03:15 +00:00
|
|
|
FFMpegFrameDump::~FFMpegFrameDump()
|
2020-10-21 14:37:16 +00:00
|
|
|
{
|
|
|
|
Stop();
|
|
|
|
}
|