From 1df655e376fe59291ee5c69755f2f2a70f82ee75 Mon Sep 17 00:00:00 2001 From: altimumdelta <48530820+altimumdelta@users.noreply.github.com> Date: Mon, 18 Mar 2019 05:50:53 +0100 Subject: VideoCommon: Rename AVIDump to FrameDump --- Source/Core/Core/State.cpp | 4 +- Source/Core/VideoCommon/AVIDump.cpp | 449 --------------------- Source/Core/VideoCommon/AVIDump.h | 35 -- Source/Core/VideoCommon/CMakeLists.txt | 4 +- Source/Core/VideoCommon/FrameDump.cpp | 449 +++++++++++++++++++++ Source/Core/VideoCommon/FrameDump.h | 35 ++ Source/Core/VideoCommon/RenderBase.cpp | 51 +-- Source/Core/VideoCommon/RenderBase.h | 14 +- Source/Core/VideoCommon/VideoCommon.vcxproj | 4 +- .../Core/VideoCommon/VideoCommon.vcxproj.filters | 4 +- 10 files changed, 525 insertions(+), 524 deletions(-) delete mode 100644 Source/Core/VideoCommon/AVIDump.cpp delete mode 100644 Source/Core/VideoCommon/AVIDump.h create mode 100644 Source/Core/VideoCommon/FrameDump.cpp create mode 100644 Source/Core/VideoCommon/FrameDump.h (limited to 'Source/Core') diff --git a/Source/Core/Core/State.cpp b/Source/Core/Core/State.cpp index 758774197a..0e393ee075 100644 --- a/Source/Core/Core/State.cpp +++ b/Source/Core/Core/State.cpp @@ -35,7 +35,7 @@ #include "Core/NetPlayClient.h" #include "Core/PowerPC/PowerPC.h" -#include "VideoCommon/AVIDump.h" +#include "VideoCommon/FrameDump.h" #include "VideoCommon/OnScreenDisplay.h" #include "VideoCommon/VideoBackendBase.h" @@ -192,7 +192,7 @@ static void DoState(PointerWrap& p) p.DoMarker("Gecko"); #if defined(HAVE_FFMPEG) - AVIDump::DoState(); + FrameDump::DoState(); #endif } diff --git a/Source/Core/VideoCommon/AVIDump.cpp b/Source/Core/VideoCommon/AVIDump.cpp deleted file mode 100644 index 5eaa31213c..0000000000 --- a/Source/Core/VideoCommon/AVIDump.cpp +++ /dev/null @@ -1,449 +0,0 @@ -// Copyright 2009 Dolphin Emulator Project -// Licensed under GPLv2+ -// Refer to the license.txt file included. - -#if defined(__FreeBSD__) -#define __STDC_CONSTANT_MACROS 1 -#endif - -#include -#include - -extern "C" { -#include -#include -#include -#include -} - -#include "Common/FileUtil.h" -#include "Common/Logging/Log.h" -#include "Common/MsgHandler.h" -#include "Common/StringUtil.h" - -#include "Core/ConfigManager.h" -#include "Core/HW/SystemTimers.h" -#include "Core/HW/VideoInterface.h" //for TargetRefreshRate -#include "Core/Movie.h" - -#include "VideoCommon/AVIDump.h" -#include "VideoCommon/OnScreenDisplay.h" -#include "VideoCommon/VideoConfig.h" - -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1) -#define AV_CODEC_FLAG_GLOBAL_HEADER CODEC_FLAG_GLOBAL_HEADER -#define av_frame_alloc avcodec_alloc_frame -#define av_frame_free avcodec_free_frame -#endif - -static AVFormatContext* s_format_context = nullptr; -static AVStream* s_stream = nullptr; -static AVCodecContext* s_codec_context = nullptr; -static AVFrame* s_src_frame = nullptr; -static AVFrame* s_scaled_frame = nullptr; -static AVPixelFormat s_pix_fmt = AV_PIX_FMT_BGR24; -static SwsContext* s_sws_context = nullptr; -static int s_width; -static int s_height; -static u64 s_last_frame; -static bool s_last_frame_is_valid = false; -static bool s_start_dumping = false; -static u64 s_last_pts; -static int s_file_index = 0; -static int s_savestate_index = 0; -static int s_last_savestate_index = 0; - -static void InitAVCodec() -{ - static bool first_run = true; - if (first_run) - { -#if LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100) - av_register_all(); -#endif - avformat_network_init(); - first_run = false; - } -} - -static bool AVStreamCopyContext(AVStream* stream, AVCodecContext* codec_context) -{ -#if (LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 33, 100)) || \ - (LIBAVCODEC_VERSION_MICRO < 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 5, 0)) - - stream->time_base = codec_context->time_base; - return avcodec_parameters_from_context(stream->codecpar, codec_context) >= 0; -#else - return avcodec_copy_context(stream->codec, codec_context) >= 0; -#endif -} - -bool AVIDump::Start(int w, int h) -{ - s_pix_fmt = AV_PIX_FMT_RGBA; - - s_width = w; - s_height = h; - - s_last_frame_is_valid = false; - s_last_pts = 0; - - InitAVCodec(); - bool success = CreateVideoFile(); - if (!success) - { - CloseVideoFile(); - OSD::AddMessage("AVIDump Start failed"); - } - return success; -} - -static std::string GetDumpPath(const std::string& format) -{ - if (!g_Config.sDumpPath.empty()) - return g_Config.sDumpPath; - - const std::string dump_path = File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump" + - std::to_string(s_file_index) + "." + format; - - // Ask to delete file - if (File::Exists(dump_path)) - { - if (SConfig::GetInstance().m_DumpFramesSilent || - AskYesNoT("Delete the existing file '%s'?", dump_path.c_str())) - { - File::Delete(dump_path); - } - else - { - // Stop and cancel dumping the video - return ""; - } - } - - return dump_path; -} - -bool AVIDump::CreateVideoFile() -{ - const std::string& format = g_Config.sDumpFormat; - - const std::string dump_path = GetDumpPath(format); - - if (dump_path.empty()) - return false; - - File::CreateFullPath(dump_path); - - AVOutputFormat* output_format = av_guess_format(format.c_str(), dump_path.c_str(), nullptr); - if (!output_format) - { - ERROR_LOG(VIDEO, "Invalid format %s", format.c_str()); - return false; - } - - if (avformat_alloc_output_context2(&s_format_context, output_format, nullptr, dump_path.c_str()) < - 0) - { - ERROR_LOG(VIDEO, "Could not allocate output context"); - return false; - } - - const std::string& codec_name = g_Config.bUseFFV1 ? "ffv1" : g_Config.sDumpCodec; - - AVCodecID codec_id = output_format->video_codec; - - if (!codec_name.empty()) - { - const AVCodecDescriptor* codec_desc = avcodec_descriptor_get_by_name(codec_name.c_str()); - if (codec_desc) - codec_id = codec_desc->id; - else - WARN_LOG(VIDEO, "Invalid codec %s", codec_name.c_str()); - } - - const AVCodec* codec = nullptr; - - if (!g_Config.sDumpEncoder.empty()) - { - codec = avcodec_find_encoder_by_name(g_Config.sDumpEncoder.c_str()); - if (!codec) - WARN_LOG(VIDEO, "Invalid encoder %s", g_Config.sDumpEncoder.c_str()); - } - if (!codec) - codec = avcodec_find_encoder(codec_id); - - s_codec_context = avcodec_alloc_context3(codec); - if (!codec || !s_codec_context) - { - ERROR_LOG(VIDEO, "Could not find encoder or allocate codec context"); - return false; - } - - // Force XVID FourCC for better compatibility - if (codec->id == AV_CODEC_ID_MPEG4) - s_codec_context->codec_tag = MKTAG('X', 'V', 'I', 'D'); - - s_codec_context->codec_type = AVMEDIA_TYPE_VIDEO; - s_codec_context->bit_rate = g_Config.iBitrateKbps * 1000; - s_codec_context->width = s_width; - s_codec_context->height = s_height; - s_codec_context->time_base.num = 1; - s_codec_context->time_base.den = VideoInterface::GetTargetRefreshRate(); - s_codec_context->gop_size = 1; - s_codec_context->pix_fmt = g_Config.bUseFFV1 ? AV_PIX_FMT_BGR0 : AV_PIX_FMT_YUV420P; - - if (output_format->flags & AVFMT_GLOBALHEADER) - s_codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; - - if (avcodec_open2(s_codec_context, codec, nullptr) < 0) - { - ERROR_LOG(VIDEO, "Could not open codec"); - return false; - } - - s_src_frame = av_frame_alloc(); - s_scaled_frame = av_frame_alloc(); - - s_scaled_frame->format = s_codec_context->pix_fmt; - s_scaled_frame->width = s_width; - s_scaled_frame->height = s_height; - -#if LIBAVCODEC_VERSION_MAJOR >= 55 - if (av_frame_get_buffer(s_scaled_frame, 1)) - return false; -#else - if (avcodec_default_get_buffer(s_codec_context, s_scaled_frame)) - return false; -#endif - - s_stream = avformat_new_stream(s_format_context, codec); - if (!s_stream || !AVStreamCopyContext(s_stream, s_codec_context)) - { - ERROR_LOG(VIDEO, "Could not create stream"); - return false; - } - - NOTICE_LOG(VIDEO, "Opening file %s for dumping", dump_path.c_str()); - if (avio_open(&s_format_context->pb, dump_path.c_str(), AVIO_FLAG_WRITE) < 0 || - avformat_write_header(s_format_context, nullptr)) - { - ERROR_LOG(VIDEO, "Could not open %s", dump_path.c_str()); - return false; - } - - OSD::AddMessage( - StringFromFormat("Dumping Frames to \"%s\" (%dx%d)", dump_path.c_str(), s_width, s_height)); - - return true; -} - -static void PreparePacket(AVPacket* pkt) -{ - av_init_packet(pkt); - pkt->data = nullptr; - pkt->size = 0; -} - -static int ReceivePacket(AVCodecContext* avctx, AVPacket* pkt, int* got_packet) -{ -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 37, 100) - return avcodec_encode_video2(avctx, pkt, nullptr, got_packet); -#else - *got_packet = 0; - int error = avcodec_receive_packet(avctx, pkt); - if (!error) - *got_packet = 1; - if (error == AVERROR(EAGAIN)) - return 0; - - return error; -#endif -} - -static int SendFrameAndReceivePacket(AVCodecContext* avctx, AVPacket* pkt, AVFrame* frame, - int* got_packet) -{ -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 37, 100) - return avcodec_encode_video2(avctx, pkt, frame, got_packet); -#else - *got_packet = 0; - int error = avcodec_send_frame(avctx, frame); - if (error) - return error; - - return ReceivePacket(avctx, pkt, got_packet); -#endif -} - -static void WritePacket(AVPacket& pkt) -{ - // Write the compressed frame in the media file. - if (pkt.pts != (s64)AV_NOPTS_VALUE) - { - pkt.pts = av_rescale_q(pkt.pts, s_codec_context->time_base, s_stream->time_base); - } - if (pkt.dts != (s64)AV_NOPTS_VALUE) - { - pkt.dts = av_rescale_q(pkt.dts, s_codec_context->time_base, s_stream->time_base); - } -#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 60, 100) - if (s_codec_context->coded_frame->key_frame) - pkt.flags |= AV_PKT_FLAG_KEY; -#endif - pkt.stream_index = s_stream->index; - av_interleaved_write_frame(s_format_context, &pkt); -} - -void AVIDump::AddFrame(const u8* data, int width, int height, int stride, const Frame& state) -{ - // Assume that the timing is valid, if the savestate id of the new frame - // doesn't match the last one. - if (state.savestate_index != s_last_savestate_index) - { - s_last_savestate_index = state.savestate_index; - s_last_frame_is_valid = false; - } - - CheckResolution(width, height); - s_src_frame->data[0] = const_cast(data); - s_src_frame->linesize[0] = stride; - s_src_frame->format = s_pix_fmt; - s_src_frame->width = s_width; - s_src_frame->height = s_height; - - // Convert image from {BGR24, RGBA} to desired pixel format - s_sws_context = - sws_getCachedContext(s_sws_context, width, height, s_pix_fmt, s_width, s_height, - s_codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr); - if (s_sws_context) - { - sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0, height, - s_scaled_frame->data, s_scaled_frame->linesize); - } - - // Encode and write the image. - AVPacket pkt; - PreparePacket(&pkt); - int got_packet = 0; - int error = 0; - u64 delta; - s64 last_pts; - // Check to see if the first frame being dumped is the first frame of output from the emulator. - // This prevents an issue with starting dumping later in emulation from placing the frames - // incorrectly. - if (!s_last_frame_is_valid) - { - s_last_frame = state.ticks; - s_last_frame_is_valid = true; - } - if (!s_start_dumping && state.first_frame) - { - delta = state.ticks; - last_pts = AV_NOPTS_VALUE; - s_start_dumping = true; - } - else - { - delta = state.ticks - s_last_frame; - last_pts = (s_last_pts * s_codec_context->time_base.den) / state.ticks_per_second; - } - u64 pts_in_ticks = s_last_pts + delta; - s_scaled_frame->pts = (pts_in_ticks * s_codec_context->time_base.den) / state.ticks_per_second; - if (s_scaled_frame->pts != last_pts) - { - s_last_frame = state.ticks; - s_last_pts = pts_in_ticks; - error = SendFrameAndReceivePacket(s_codec_context, &pkt, s_scaled_frame, &got_packet); - } - if (!error && got_packet) - { - WritePacket(pkt); - } - if (error) - ERROR_LOG(VIDEO, "Error while encoding video: %d", error); -} - -static void HandleDelayedPackets() -{ - AVPacket pkt; - - while (true) - { - PreparePacket(&pkt); - int got_packet; - int error = ReceivePacket(s_codec_context, &pkt, &got_packet); - if (error) - { - ERROR_LOG(VIDEO, "Error while stopping video: %d", error); - break; - } - - if (!got_packet) - break; - - WritePacket(pkt); - } -} - -void AVIDump::Stop() -{ - HandleDelayedPackets(); - av_write_trailer(s_format_context); - CloseVideoFile(); - s_file_index = 0; - NOTICE_LOG(VIDEO, "Stopping frame dump"); - OSD::AddMessage("Stopped dumping frames"); -} - -void AVIDump::CloseVideoFile() -{ - av_frame_free(&s_src_frame); - av_frame_free(&s_scaled_frame); - - avcodec_free_context(&s_codec_context); - - if (s_format_context) - { - avio_closep(&s_format_context->pb); - } - avformat_free_context(s_format_context); - s_format_context = nullptr; - - if (s_sws_context) - { - sws_freeContext(s_sws_context); - s_sws_context = nullptr; - } -} - -void AVIDump::DoState() -{ - s_savestate_index++; -} - -void AVIDump::CheckResolution(int width, int height) -{ - // 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. - if ((width != s_width || height != s_height) && (width > 0 && height > 0)) - { - int temp_file_index = s_file_index; - Stop(); - s_file_index = temp_file_index + 1; - Start(width, height); - } -} - -AVIDump::Frame AVIDump::FetchState(u64 ticks) -{ - Frame state; - state.ticks = ticks; - state.first_frame = Movie::GetCurrentFrame() < 1; - state.ticks_per_second = SystemTimers::GetTicksPerSecond(); - state.savestate_index = s_savestate_index; - return state; -} diff --git a/Source/Core/VideoCommon/AVIDump.h b/Source/Core/VideoCommon/AVIDump.h deleted file mode 100644 index 065a5d63d0..0000000000 --- a/Source/Core/VideoCommon/AVIDump.h +++ /dev/null @@ -1,35 +0,0 @@ -// Copyright 2008 Dolphin Emulator Project -// Licensed under GPLv2+ -// Refer to the license.txt file included. - -#pragma once - -#include "Common/CommonTypes.h" - -class AVIDump -{ -private: - static bool CreateVideoFile(); - static void CloseVideoFile(); - static void CheckResolution(int width, int height); - -public: - struct Frame - { - u64 ticks = 0; - u32 ticks_per_second = 0; - bool first_frame = false; - int savestate_index = 0; - }; - - static bool Start(int w, int h); - static void AddFrame(const u8* data, int width, int height, int stride, const Frame& state); - static void Stop(); - static void DoState(); - -#if defined(HAVE_FFMPEG) - static Frame FetchState(u64 ticks); -#else - static Frame FetchState(u64 ticks) { return {}; } -#endif -}; diff --git a/Source/Core/VideoCommon/CMakeLists.txt b/Source/Core/VideoCommon/CMakeLists.txt index 92665ca6b3..9b9e36dd79 100644 --- a/Source/Core/VideoCommon/CMakeLists.txt +++ b/Source/Core/VideoCommon/CMakeLists.txt @@ -157,8 +157,8 @@ endif() if(FFmpeg_FOUND) target_sources(videocommon PRIVATE - AVIDump.cpp - AVIDump.h + FrameDump.cpp + FrameDump.h ) target_link_libraries(videocommon PRIVATE FFmpeg::avcodec diff --git a/Source/Core/VideoCommon/FrameDump.cpp b/Source/Core/VideoCommon/FrameDump.cpp new file mode 100644 index 0000000000..6a00ce4e12 --- /dev/null +++ b/Source/Core/VideoCommon/FrameDump.cpp @@ -0,0 +1,449 @@ +// Copyright 2009 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#if defined(__FreeBSD__) +#define __STDC_CONSTANT_MACROS 1 +#endif + +#include +#include + +extern "C" { +#include +#include +#include +#include +} + +#include "Common/FileUtil.h" +#include "Common/Logging/Log.h" +#include "Common/MsgHandler.h" +#include "Common/StringUtil.h" + +#include "Core/ConfigManager.h" +#include "Core/HW/SystemTimers.h" +#include "Core/HW/VideoInterface.h" //for TargetRefreshRate +#include "Core/Movie.h" + +#include "VideoCommon/FrameDump.h" +#include "VideoCommon/OnScreenDisplay.h" +#include "VideoCommon/VideoConfig.h" + +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1) +#define AV_CODEC_FLAG_GLOBAL_HEADER CODEC_FLAG_GLOBAL_HEADER +#define av_frame_alloc avcodec_alloc_frame +#define av_frame_free avcodec_free_frame +#endif + +static AVFormatContext* s_format_context = nullptr; +static AVStream* s_stream = nullptr; +static AVCodecContext* s_codec_context = nullptr; +static AVFrame* s_src_frame = nullptr; +static AVFrame* s_scaled_frame = nullptr; +static AVPixelFormat s_pix_fmt = AV_PIX_FMT_BGR24; +static SwsContext* s_sws_context = nullptr; +static int s_width; +static int s_height; +static u64 s_last_frame; +static bool s_last_frame_is_valid = false; +static bool s_start_dumping = false; +static u64 s_last_pts; +static int s_file_index = 0; +static int s_savestate_index = 0; +static int s_last_savestate_index = 0; + +static void InitAVCodec() +{ + static bool first_run = true; + if (first_run) + { +#if LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT < AV_VERSION_INT(58, 9, 100) + av_register_all(); +#endif + avformat_network_init(); + first_run = false; + } +} + +static bool AVStreamCopyContext(AVStream* stream, AVCodecContext* codec_context) +{ +#if (LIBAVCODEC_VERSION_MICRO >= 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 33, 100)) || \ + (LIBAVCODEC_VERSION_MICRO < 100 && LIBAVCODEC_VERSION_INT >= AV_VERSION_INT(57, 5, 0)) + + stream->time_base = codec_context->time_base; + return avcodec_parameters_from_context(stream->codecpar, codec_context) >= 0; +#else + return avcodec_copy_context(stream->codec, codec_context) >= 0; +#endif +} + +bool FrameDump::Start(int w, int h) +{ + s_pix_fmt = AV_PIX_FMT_RGBA; + + s_width = w; + s_height = h; + + s_last_frame_is_valid = false; + s_last_pts = 0; + + InitAVCodec(); + bool success = CreateVideoFile(); + if (!success) + { + CloseVideoFile(); + OSD::AddMessage("FrameDump Start failed"); + } + return success; +} + +static std::string GetDumpPath(const std::string& format) +{ + if (!g_Config.sDumpPath.empty()) + return g_Config.sDumpPath; + + const std::string dump_path = File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump" + + std::to_string(s_file_index) + "." + format; + + // Ask to delete file + if (File::Exists(dump_path)) + { + if (SConfig::GetInstance().m_DumpFramesSilent || + AskYesNoT("Delete the existing file '%s'?", dump_path.c_str())) + { + File::Delete(dump_path); + } + else + { + // Stop and cancel dumping the video + return ""; + } + } + + return dump_path; +} + +bool FrameDump::CreateVideoFile() +{ + const std::string& format = g_Config.sDumpFormat; + + const std::string dump_path = GetDumpPath(format); + + if (dump_path.empty()) + return false; + + File::CreateFullPath(dump_path); + + AVOutputFormat* output_format = av_guess_format(format.c_str(), dump_path.c_str(), nullptr); + if (!output_format) + { + ERROR_LOG(VIDEO, "Invalid format %s", format.c_str()); + return false; + } + + if (avformat_alloc_output_context2(&s_format_context, output_format, nullptr, dump_path.c_str()) < + 0) + { + ERROR_LOG(VIDEO, "Could not allocate output context"); + return false; + } + + const std::string& codec_name = g_Config.bUseFFV1 ? "ffv1" : g_Config.sDumpCodec; + + AVCodecID codec_id = output_format->video_codec; + + if (!codec_name.empty()) + { + const AVCodecDescriptor* codec_desc = avcodec_descriptor_get_by_name(codec_name.c_str()); + if (codec_desc) + codec_id = codec_desc->id; + else + WARN_LOG(VIDEO, "Invalid codec %s", codec_name.c_str()); + } + + const AVCodec* codec = nullptr; + + if (!g_Config.sDumpEncoder.empty()) + { + codec = avcodec_find_encoder_by_name(g_Config.sDumpEncoder.c_str()); + if (!codec) + WARN_LOG(VIDEO, "Invalid encoder %s", g_Config.sDumpEncoder.c_str()); + } + if (!codec) + codec = avcodec_find_encoder(codec_id); + + s_codec_context = avcodec_alloc_context3(codec); + if (!codec || !s_codec_context) + { + ERROR_LOG(VIDEO, "Could not find encoder or allocate codec context"); + return false; + } + + // Force XVID FourCC for better compatibility when using H.263 + if (codec->id == AV_CODEC_ID_MPEG4) + s_codec_context->codec_tag = MKTAG('X', 'V', 'I', 'D'); + + s_codec_context->codec_type = AVMEDIA_TYPE_VIDEO; + s_codec_context->bit_rate = g_Config.iBitrateKbps * 1000; + s_codec_context->width = s_width; + s_codec_context->height = s_height; + s_codec_context->time_base.num = 1; + s_codec_context->time_base.den = VideoInterface::GetTargetRefreshRate(); + s_codec_context->gop_size = 1; + s_codec_context->pix_fmt = g_Config.bUseFFV1 ? AV_PIX_FMT_BGR0 : AV_PIX_FMT_YUV420P; + + if (output_format->flags & AVFMT_GLOBALHEADER) + s_codec_context->flags |= AV_CODEC_FLAG_GLOBAL_HEADER; + + if (avcodec_open2(s_codec_context, codec, nullptr) < 0) + { + ERROR_LOG(VIDEO, "Could not open codec"); + return false; + } + + s_src_frame = av_frame_alloc(); + s_scaled_frame = av_frame_alloc(); + + s_scaled_frame->format = s_codec_context->pix_fmt; + s_scaled_frame->width = s_width; + s_scaled_frame->height = s_height; + +#if LIBAVCODEC_VERSION_MAJOR >= 55 + if (av_frame_get_buffer(s_scaled_frame, 1)) + return false; +#else + if (avcodec_default_get_buffer(s_codec_context, s_scaled_frame)) + return false; +#endif + + s_stream = avformat_new_stream(s_format_context, codec); + if (!s_stream || !AVStreamCopyContext(s_stream, s_codec_context)) + { + ERROR_LOG(VIDEO, "Could not create stream"); + return false; + } + + NOTICE_LOG(VIDEO, "Opening file %s for dumping", dump_path.c_str()); + if (avio_open(&s_format_context->pb, dump_path.c_str(), AVIO_FLAG_WRITE) < 0 || + avformat_write_header(s_format_context, nullptr)) + { + ERROR_LOG(VIDEO, "Could not open %s", dump_path.c_str()); + return false; + } + + OSD::AddMessage( + StringFromFormat("Dumping Frames to \"%s\" (%dx%d)", dump_path.c_str(), s_width, s_height)); + + return true; +} + +static void PreparePacket(AVPacket* pkt) +{ + av_init_packet(pkt); + pkt->data = nullptr; + pkt->size = 0; +} + +static int ReceivePacket(AVCodecContext* avctx, AVPacket* pkt, int* got_packet) +{ +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 37, 100) + return avcodec_encode_video2(avctx, pkt, nullptr, got_packet); +#else + *got_packet = 0; + int error = avcodec_receive_packet(avctx, pkt); + if (!error) + *got_packet = 1; + if (error == AVERROR(EAGAIN)) + return 0; + + return error; +#endif +} + +static int SendFrameAndReceivePacket(AVCodecContext* avctx, AVPacket* pkt, AVFrame* frame, + int* got_packet) +{ +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(57, 37, 100) + return avcodec_encode_video2(avctx, pkt, frame, got_packet); +#else + *got_packet = 0; + int error = avcodec_send_frame(avctx, frame); + if (error) + return error; + + return ReceivePacket(avctx, pkt, got_packet); +#endif +} + +static void WritePacket(AVPacket& pkt) +{ + // Write the compressed frame in the media file. + if (pkt.pts != (s64)AV_NOPTS_VALUE) + { + pkt.pts = av_rescale_q(pkt.pts, s_codec_context->time_base, s_stream->time_base); + } + if (pkt.dts != (s64)AV_NOPTS_VALUE) + { + pkt.dts = av_rescale_q(pkt.dts, s_codec_context->time_base, s_stream->time_base); + } +#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 60, 100) + if (s_codec_context->coded_frame->key_frame) + pkt.flags |= AV_PKT_FLAG_KEY; +#endif + pkt.stream_index = s_stream->index; + av_interleaved_write_frame(s_format_context, &pkt); +} + +void FrameDump::AddFrame(const u8* data, int width, int height, int stride, const Frame& state) +{ + // Assume that the timing is valid, if the savestate id of the new frame + // doesn't match the last one. + if (state.savestate_index != s_last_savestate_index) + { + s_last_savestate_index = state.savestate_index; + s_last_frame_is_valid = false; + } + + CheckResolution(width, height); + s_src_frame->data[0] = const_cast(data); + s_src_frame->linesize[0] = stride; + s_src_frame->format = s_pix_fmt; + s_src_frame->width = s_width; + s_src_frame->height = s_height; + + // Convert image from {BGR24, RGBA} to desired pixel format + s_sws_context = + sws_getCachedContext(s_sws_context, width, height, s_pix_fmt, s_width, s_height, + s_codec_context->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr); + if (s_sws_context) + { + sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0, height, + s_scaled_frame->data, s_scaled_frame->linesize); + } + + // Encode and write the image. + AVPacket pkt; + PreparePacket(&pkt); + int got_packet = 0; + int error = 0; + u64 delta; + s64 last_pts; + // Check to see if the first frame being dumped is the first frame of output from the emulator. + // This prevents an issue with starting dumping later in emulation from placing the frames + // incorrectly. + if (!s_last_frame_is_valid) + { + s_last_frame = state.ticks; + s_last_frame_is_valid = true; + } + if (!s_start_dumping && state.first_frame) + { + delta = state.ticks; + last_pts = AV_NOPTS_VALUE; + s_start_dumping = true; + } + else + { + delta = state.ticks - s_last_frame; + last_pts = (s_last_pts * s_codec_context->time_base.den) / state.ticks_per_second; + } + u64 pts_in_ticks = s_last_pts + delta; + s_scaled_frame->pts = (pts_in_ticks * s_codec_context->time_base.den) / state.ticks_per_second; + if (s_scaled_frame->pts != last_pts) + { + s_last_frame = state.ticks; + s_last_pts = pts_in_ticks; + error = SendFrameAndReceivePacket(s_codec_context, &pkt, s_scaled_frame, &got_packet); + } + if (!error && got_packet) + { + WritePacket(pkt); + } + if (error) + ERROR_LOG(VIDEO, "Error while encoding video: %d", error); +} + +static void HandleDelayedPackets() +{ + AVPacket pkt; + + while (true) + { + PreparePacket(&pkt); + int got_packet; + int error = ReceivePacket(s_codec_context, &pkt, &got_packet); + if (error) + { + ERROR_LOG(VIDEO, "Error while stopping video: %d", error); + break; + } + + if (!got_packet) + break; + + WritePacket(pkt); + } +} + +void FrameDump::Stop() +{ + HandleDelayedPackets(); + av_write_trailer(s_format_context); + CloseVideoFile(); + s_file_index = 0; + NOTICE_LOG(VIDEO, "Stopping frame dump"); + OSD::AddMessage("Stopped dumping frames"); +} + +void FrameDump::CloseVideoFile() +{ + av_frame_free(&s_src_frame); + av_frame_free(&s_scaled_frame); + + avcodec_free_context(&s_codec_context); + + if (s_format_context) + { + avio_closep(&s_format_context->pb); + } + avformat_free_context(s_format_context); + s_format_context = nullptr; + + if (s_sws_context) + { + sws_freeContext(s_sws_context); + s_sws_context = nullptr; + } +} + +void FrameDump::DoState() +{ + s_savestate_index++; +} + +void FrameDump::CheckResolution(int width, int height) +{ + // 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. + if ((width != s_width || height != s_height) && (width > 0 && height > 0)) + { + int temp_file_index = s_file_index; + Stop(); + s_file_index = temp_file_index + 1; + Start(width, height); + } +} + +FrameDump::Frame FrameDump::FetchState(u64 ticks) +{ + Frame state; + state.ticks = ticks; + state.first_frame = Movie::GetCurrentFrame() < 1; + state.ticks_per_second = SystemTimers::GetTicksPerSecond(); + state.savestate_index = s_savestate_index; + return state; +} diff --git a/Source/Core/VideoCommon/FrameDump.h b/Source/Core/VideoCommon/FrameDump.h new file mode 100644 index 0000000000..e61a9054eb --- /dev/null +++ b/Source/Core/VideoCommon/FrameDump.h @@ -0,0 +1,35 @@ +// Copyright 2008 Dolphin Emulator Project +// Licensed under GPLv2+ +// Refer to the license.txt file included. + +#pragma once + +#include "Common/CommonTypes.h" + +class FrameDump +{ +private: + static bool CreateVideoFile(); + static void CloseVideoFile(); + static void CheckResolution(int width, int height); + +public: + struct Frame + { + u64 ticks = 0; + u32 ticks_per_second = 0; + bool first_frame = false; + int savestate_index = 0; + }; + + static bool Start(int w, int h); + static void AddFrame(const u8* data, int width, int height, int stride, const Frame& state); + static void Stop(); + static void DoState(); + +#if defined(HAVE_FFMPEG) + static Frame FetchState(u64 ticks); +#else + static Frame FetchState(u64 ticks) { return {}; } +#endif +}; diff --git a/Source/Core/VideoCommon/RenderBase.cpp b/Source/Core/VideoCommon/RenderBase.cpp index 8c3f89de57..190a827f1b 100644 --- a/Source/Core/VideoCommon/RenderBase.cpp +++ b/Source/Core/VideoCommon/RenderBase.cpp @@ -48,7 +48,6 @@ #include "Core/Host.h" #include "Core/Movie.h" -#include "VideoCommon/AVIDump.h" #include "VideoCommon/AbstractFramebuffer.h" #include "VideoCommon/AbstractStagingTexture.h" #include "VideoCommon/AbstractTexture.h" @@ -57,6 +56,7 @@ #include "VideoCommon/CPMemory.h" #include "VideoCommon/CommandProcessor.h" #include "VideoCommon/FPSCounter.h" +#include "VideoCommon/FrameDump.h" #include "VideoCommon/FramebufferManager.h" #include "VideoCommon/ImageWrite.h" #include "VideoCommon/NetPlayChatUI.h" @@ -1339,7 +1339,7 @@ void Renderer::DumpCurrentFrame(const AbstractTexture* src_texture, copy_rect = src_texture->GetRect(); } - // Index 0 was just sent to AVI dump. Swap with the second texture. + // Index 0 was just sent to FFMPEG dump. Swap with the second texture. if (m_frame_dump_readback_textures[0]) std::swap(m_frame_dump_readback_textures[0], m_frame_dump_readback_textures[1]); @@ -1348,7 +1348,7 @@ void Renderer::DumpCurrentFrame(const AbstractTexture* src_texture, m_frame_dump_readback_textures[0]->CopyFromTexture(src_texture, copy_rect, 0, 0, m_frame_dump_readback_textures[0]->GetRect()); - m_last_frame_state = AVIDump::FetchState(ticks); + m_last_frame_state = FrameDump::FetchState(ticks); m_last_frame_exported = true; } @@ -1442,7 +1442,8 @@ void Renderer::ShutdownFrameDumping() tex.reset(); } -void Renderer::DumpFrameData(const u8* data, int w, int h, int stride, const AVIDump::Frame& state) +void Renderer::DumpFrameData(const u8* data, int w, int h, int stride, + const FrameDump::Frame& state) { m_frame_dump_config = FrameDumpConfig{data, w, h, stride, state}; @@ -1471,16 +1472,16 @@ void Renderer::FinishFrameData() void Renderer::RunFrameDumps() { Common::SetCurrentThreadName("FrameDumping"); - bool dump_to_avi = !g_ActiveConfig.bDumpFramesAsImages; + bool dump_to_ffmpeg = !g_ActiveConfig.bDumpFramesAsImages; bool frame_dump_started = false; -// If Dolphin was compiled without libav, we only support dumping to images. +// If Dolphin was compiled without ffmpeg, we only support dumping to images. #if !defined(HAVE_FFMPEG) - if (dump_to_avi) + if (dump_to_ffmpeg) { - WARN_LOG(VIDEO, "AVI frame dump requested, but Dolphin was compiled without libav. " - "Frame dump will be saved as images instead."); - dump_to_avi = false; + WARN_LOG(VIDEO, "FrameDump: Dolphin was not compiled with FFmpeg, using fallback option. " + "Frames will be saved as PNG images instead."); + dump_to_ffmpeg = false; } #endif @@ -1510,8 +1511,8 @@ void Renderer::RunFrameDumps() { if (!frame_dump_started) { - if (dump_to_avi) - frame_dump_started = StartFrameDumpToAVI(config); + if (dump_to_ffmpeg) + frame_dump_started = StartFrameDumpToFFMPEG(config); else frame_dump_started = StartFrameDumpToImage(config); @@ -1523,8 +1524,8 @@ void Renderer::RunFrameDumps() // If we failed to start frame dumping, don't write a frame. if (frame_dump_started) { - if (dump_to_avi) - DumpFrameToAVI(config); + if (dump_to_ffmpeg) + DumpFrameToFFMPEG(config); else DumpFrameToImage(config); } @@ -1536,40 +1537,40 @@ void Renderer::RunFrameDumps() if (frame_dump_started) { // No additional cleanup is needed when dumping to images. - if (dump_to_avi) - StopFrameDumpToAVI(); + if (dump_to_ffmpeg) + StopFrameDumpToFFMPEG(); } } #if defined(HAVE_FFMPEG) -bool Renderer::StartFrameDumpToAVI(const FrameDumpConfig& config) +bool Renderer::StartFrameDumpToFFMPEG(const FrameDumpConfig& config) { - return AVIDump::Start(config.width, config.height); + return FrameDump::Start(config.width, config.height); } -void Renderer::DumpFrameToAVI(const FrameDumpConfig& config) +void Renderer::DumpFrameToFFMPEG(const FrameDumpConfig& config) { - AVIDump::AddFrame(config.data, config.width, config.height, config.stride, config.state); + FrameDump::AddFrame(config.data, config.width, config.height, config.stride, config.state); } -void Renderer::StopFrameDumpToAVI() +void Renderer::StopFrameDumpToFFMPEG() { - AVIDump::Stop(); + FrameDump::Stop(); } #else -bool Renderer::StartFrameDumpToAVI(const FrameDumpConfig& config) +bool Renderer::StartFrameDumpToFFMPEG(const FrameDumpConfig& config) { return false; } -void Renderer::DumpFrameToAVI(const FrameDumpConfig& config) +void Renderer::DumpFrameToFFMPEG(const FrameDumpConfig& config) { } -void Renderer::StopFrameDumpToAVI() +void Renderer::StopFrameDumpToFFMPEG() { } diff --git a/Source/Core/VideoCommon/RenderBase.h b/Source/Core/VideoCommon/RenderBase.h index b9eab7f331..c85dcb1645 100644 --- a/Source/Core/VideoCommon/RenderBase.h +++ b/Source/Core/VideoCommon/RenderBase.h @@ -27,10 +27,10 @@ #include "Common/Event.h" #include "Common/Flag.h" #include "Common/MathUtil.h" -#include "VideoCommon/AVIDump.h" #include "VideoCommon/AsyncShaderCompiler.h" #include "VideoCommon/BPMemory.h" #include "VideoCommon/FPSCounter.h" +#include "VideoCommon/FrameDump.h" #include "VideoCommon/RenderState.h" #include "VideoCommon/TextureConfig.h" #include "VideoCommon/VideoCommon.h" @@ -337,14 +337,14 @@ private: int width; int height; int stride; - AVIDump::Frame state; + FrameDump::Frame state; } m_frame_dump_config; // Texture used for screenshot/frame dumping std::unique_ptr m_frame_dump_render_texture; std::unique_ptr m_frame_dump_render_framebuffer; std::array, 2> m_frame_dump_readback_textures; - AVIDump::Frame m_last_frame_state; + FrameDump::Frame m_last_frame_state; bool m_last_frame_exported = false; // Tracking of XFB textures so we don't render duplicate frames. @@ -355,9 +355,9 @@ private: u32 m_last_xfb_height = MAX_XFB_HEIGHT; // NOTE: The methods below are called on the framedumping thread. - bool StartFrameDumpToAVI(const FrameDumpConfig& config); - void DumpFrameToAVI(const FrameDumpConfig& config); - void StopFrameDumpToAVI(); + bool StartFrameDumpToFFMPEG(const FrameDumpConfig& config); + void DumpFrameToFFMPEG(const FrameDumpConfig& config); + void StopFrameDumpToFFMPEG(); std::string GetFrameDumpNextImageFileName() const; bool StartFrameDumpToImage(const FrameDumpConfig& config); void DumpFrameToImage(const FrameDumpConfig& config); @@ -376,7 +376,7 @@ private: const MathUtil::Rectangle& src_rect, u64 ticks); // Asynchronously encodes the specified pointer of frame data to the frame dump. - void DumpFrameData(const u8* data, int w, int h, int stride, const AVIDump::Frame& state); + void DumpFrameData(const u8* data, int w, int h, int stride, const FrameDump::Frame& state); // Ensures all rendered frames are queued for encoding. void FlushFrameDump(); diff --git a/Source/Core/VideoCommon/VideoCommon.vcxproj b/Source/Core/VideoCommon/VideoCommon.vcxproj index 51f93d0457..467d1c29e7 100644 --- a/Source/Core/VideoCommon/VideoCommon.vcxproj +++ b/Source/Core/VideoCommon/VideoCommon.vcxproj @@ -41,7 +41,7 @@ - + @@ -108,7 +108,7 @@ - + diff --git a/Source/Core/VideoCommon/VideoCommon.vcxproj.filters b/Source/Core/VideoCommon/VideoCommon.vcxproj.filters index ea9e2d3346..f69a01a813 100644 --- a/Source/Core/VideoCommon/VideoCommon.vcxproj.filters +++ b/Source/Core/VideoCommon/VideoCommon.vcxproj.filters @@ -86,7 +86,7 @@ Shader Managers - + Util @@ -275,7 +275,7 @@ Shader Managers - + Util -- cgit v1.2.3