2015-05-23 22:55:12 -06:00
|
|
|
// Copyright 2009 Dolphin Emulator Project
|
2015-05-17 17:08:10 -06:00
|
|
|
// Licensed under GPLv2+
|
2013-04-17 21:09:55 -06:00
|
|
|
// Refer to the license.txt file included.
|
2009-07-05 20:10:26 -06:00
|
|
|
|
2011-08-27 12:42:11 -06:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
2013-10-28 23:23:17 -06:00
|
|
|
#endif
|
2011-08-27 12:42:11 -06:00
|
|
|
|
2014-06-02 23:08:54 -06:00
|
|
|
#include <string>
|
|
|
|
|
2016-01-02 11:07:08 -07:00
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavutil/mathematics.h>
|
|
|
|
#include <libswscale/swscale.h>
|
|
|
|
}
|
|
|
|
|
2014-06-02 23:08:54 -06:00
|
|
|
#include "Common/FileUtil.h"
|
2014-06-05 17:29:54 -06:00
|
|
|
#include "Common/Logging/Log.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Common/MsgHandler.h"
|
2014-06-05 17:29:54 -06:00
|
|
|
|
2016-01-03 15:20:42 -07:00
|
|
|
#include "Core/ConfigManager.h"
|
2014-10-20 03:11:56 -06:00
|
|
|
#include "Core/HW/SystemTimers.h"
|
2016-06-24 02:43:46 -06:00
|
|
|
#include "Core/HW/VideoInterface.h" //for TargetRefreshRate
|
|
|
|
#include "Core/Movie.h"
|
2016-10-16 05:26:37 -06:00
|
|
|
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/AVIDump.h"
|
2016-10-16 05:26:37 -06:00
|
|
|
#include "VideoCommon/OnScreenDisplay.h"
|
2014-02-17 03:18:15 -07:00
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-11-14 14:14:26 -07:00
|
|
|
|
2014-07-13 15:06:20 -06:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(55, 28, 1)
|
|
|
|
#define av_frame_alloc avcodec_alloc_frame
|
|
|
|
#define av_frame_free avcodec_free_frame
|
|
|
|
#endif
|
|
|
|
|
2014-06-14 21:29:05 -06:00
|
|
|
static AVFormatContext* s_format_context = nullptr;
|
|
|
|
static AVStream* s_stream = nullptr;
|
2014-06-23 20:12:15 -06:00
|
|
|
static AVFrame* s_src_frame = nullptr;
|
|
|
|
static AVFrame* s_scaled_frame = nullptr;
|
2016-01-28 16:53:08 -07:00
|
|
|
static AVPixelFormat s_pix_fmt = AV_PIX_FMT_BGR24;
|
2014-06-14 21:16:58 -06:00
|
|
|
static SwsContext* s_sws_context = nullptr;
|
2014-06-14 21:29:05 -06:00
|
|
|
static int s_width;
|
|
|
|
static int s_height;
|
2014-10-04 01:28:01 -06:00
|
|
|
static u64 s_last_frame;
|
2016-10-08 04:56:28 -06:00
|
|
|
static bool s_last_frame_is_valid = false;
|
2014-11-01 21:51:44 -06:00
|
|
|
static bool s_start_dumping = false;
|
2014-10-20 03:11:56 -06:00
|
|
|
static u64 s_last_pts;
|
2016-06-24 20:41:10 -06:00
|
|
|
static int s_file_index = 0;
|
2016-11-04 11:24:03 -06:00
|
|
|
static int s_savestate_index = 0;
|
|
|
|
static int s_last_savestate_index = 0;
|
2010-11-14 14:14:26 -07:00
|
|
|
|
|
|
|
static void InitAVCodec()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
static bool first_run = true;
|
|
|
|
if (first_run)
|
|
|
|
{
|
|
|
|
av_register_all();
|
|
|
|
first_run = false;
|
|
|
|
}
|
2010-11-14 14:14:26 -07:00
|
|
|
}
|
|
|
|
|
2016-10-08 08:23:04 -06:00
|
|
|
bool AVIDump::Start(int w, int h)
|
2010-11-14 14:14:26 -07:00
|
|
|
{
|
2016-10-08 08:23:04 -06:00
|
|
|
s_pix_fmt = AV_PIX_FMT_RGBA;
|
2016-06-24 20:41:10 -06:00
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
s_width = w;
|
|
|
|
s_height = h;
|
|
|
|
|
2016-10-08 04:56:28 -06:00
|
|
|
s_last_frame_is_valid = false;
|
2016-06-24 02:43:46 -06:00
|
|
|
s_last_pts = 0;
|
|
|
|
|
|
|
|
InitAVCodec();
|
|
|
|
bool success = CreateFile();
|
|
|
|
if (!success)
|
2016-10-16 05:26:37 -06:00
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
CloseFile();
|
2016-10-16 05:26:37 -06:00
|
|
|
OSD::AddMessage("AVIDump Start failed");
|
|
|
|
}
|
2016-06-24 02:43:46 -06:00
|
|
|
return success;
|
2010-11-14 14:14:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AVIDump::CreateFile()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
AVCodec* codec = nullptr;
|
|
|
|
|
|
|
|
s_format_context = avformat_alloc_context();
|
2016-06-24 20:41:10 -06:00
|
|
|
std::stringstream s_file_index_str;
|
|
|
|
s_file_index_str << s_file_index;
|
2016-06-24 02:43:46 -06:00
|
|
|
snprintf(s_format_context->filename, sizeof(s_format_context->filename), "%s",
|
2016-06-24 20:41:10 -06:00
|
|
|
(File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump" + s_file_index_str.str() + ".avi")
|
|
|
|
.c_str());
|
2016-06-24 02:43:46 -06:00
|
|
|
File::CreateFullPath(s_format_context->filename);
|
|
|
|
|
|
|
|
// Ask to delete file
|
|
|
|
if (File::Exists(s_format_context->filename))
|
|
|
|
{
|
|
|
|
if (SConfig::GetInstance().m_DumpFramesSilent ||
|
|
|
|
AskYesNoT("Delete the existing file '%s'?", s_format_context->filename))
|
|
|
|
{
|
|
|
|
File::Delete(s_format_context->filename);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Stop and cancel dumping the video
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(s_format_context->oformat = av_guess_format("avi", nullptr, nullptr)) ||
|
|
|
|
!(s_stream = avformat_new_stream(s_format_context, codec)))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_stream->codec->codec_id =
|
|
|
|
g_Config.bUseFFV1 ? AV_CODEC_ID_FFV1 : s_format_context->oformat->video_codec;
|
|
|
|
if (!g_Config.bUseFFV1)
|
|
|
|
s_stream->codec->codec_tag =
|
|
|
|
MKTAG('X', 'V', 'I', 'D'); // Force XVID FourCC for better compatibility
|
|
|
|
s_stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
|
|
|
s_stream->codec->bit_rate = 400000;
|
|
|
|
s_stream->codec->width = s_width;
|
|
|
|
s_stream->codec->height = s_height;
|
|
|
|
s_stream->codec->time_base.num = 1;
|
|
|
|
s_stream->codec->time_base.den = VideoInterface::GetTargetRefreshRate();
|
|
|
|
s_stream->codec->gop_size = 12;
|
|
|
|
s_stream->codec->pix_fmt = g_Config.bUseFFV1 ? AV_PIX_FMT_BGRA : AV_PIX_FMT_YUV420P;
|
|
|
|
|
|
|
|
if (!(codec = avcodec_find_encoder(s_stream->codec->codec_id)) ||
|
|
|
|
(avcodec_open2(s_stream->codec, codec, nullptr) < 0))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
s_src_frame = av_frame_alloc();
|
|
|
|
s_scaled_frame = av_frame_alloc();
|
|
|
|
|
2016-07-10 19:36:18 -06:00
|
|
|
s_scaled_frame->format = s_stream->codec->pix_fmt;
|
|
|
|
s_scaled_frame->width = s_width;
|
|
|
|
s_scaled_frame->height = s_height;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-07-10 19:36:18 -06:00
|
|
|
#if LIBAVCODEC_VERSION_MAJOR >= 55
|
|
|
|
if (av_frame_get_buffer(s_scaled_frame, 1))
|
|
|
|
return false;
|
|
|
|
#else
|
|
|
|
if (avcodec_default_get_buffer(s_stream->codec, s_scaled_frame))
|
|
|
|
return false;
|
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
|
|
|
|
NOTICE_LOG(VIDEO, "Opening file %s for dumping", s_format_context->filename);
|
2016-07-10 19:36:18 -06:00
|
|
|
if (avio_open(&s_format_context->pb, s_format_context->filename, AVIO_FLAG_WRITE) < 0 ||
|
|
|
|
avformat_write_header(s_format_context, nullptr))
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
|
|
|
WARN_LOG(VIDEO, "Could not open %s", s_format_context->filename);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-10-16 05:26:37 -06:00
|
|
|
OSD::AddMessage(StringFromFormat("Dumping Frames to \"%s\" (%dx%d)", s_format_context->filename,
|
|
|
|
s_width, s_height));
|
|
|
|
|
2016-06-24 02:43:46 -06:00
|
|
|
return true;
|
2010-11-14 14:14:26 -07:00
|
|
|
}
|
|
|
|
|
2014-06-14 21:16:58 -06:00
|
|
|
static void PreparePacket(AVPacket* pkt)
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
av_init_packet(pkt);
|
|
|
|
pkt->data = nullptr;
|
|
|
|
pkt->size = 0;
|
2014-06-14 21:16:58 -06:00
|
|
|
}
|
|
|
|
|
2016-11-04 11:19:35 -06:00
|
|
|
void AVIDump::AddFrame(const u8* data, int width, int height, int stride, const Frame& state)
|
2010-11-14 14:14:26 -07:00
|
|
|
{
|
2016-11-04 11:24:03 -06:00
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-10-15 11:00:34 -06:00
|
|
|
CheckResolution(width, height);
|
2016-07-10 19:36:18 -06:00
|
|
|
s_src_frame->data[0] = const_cast<u8*>(data);
|
2016-10-08 07:28:12 -06:00
|
|
|
s_src_frame->linesize[0] = stride;
|
2016-07-10 19:36:18 -06:00
|
|
|
s_src_frame->format = s_pix_fmt;
|
|
|
|
s_src_frame->width = s_width;
|
|
|
|
s_src_frame->height = s_height;
|
2016-06-24 02:43:46 -06:00
|
|
|
|
2016-10-15 11:06:01 -06:00
|
|
|
// Convert image from {BGR24, RGBA} to desired pixel format
|
2016-06-24 02:43:46 -06:00
|
|
|
if ((s_sws_context =
|
|
|
|
sws_getCachedContext(s_sws_context, width, height, s_pix_fmt, s_width, s_height,
|
|
|
|
s_stream->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr)))
|
|
|
|
{
|
|
|
|
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.
|
2016-10-08 04:56:28 -06:00
|
|
|
if (!s_last_frame_is_valid)
|
|
|
|
{
|
2016-11-04 11:19:35 -06:00
|
|
|
s_last_frame = state.ticks;
|
2016-10-08 04:56:28 -06:00
|
|
|
s_last_frame_is_valid = true;
|
|
|
|
}
|
2016-11-04 11:19:35 -06:00
|
|
|
if (!s_start_dumping && state.first_frame)
|
2016-06-24 02:43:46 -06:00
|
|
|
{
|
2016-11-04 11:19:35 -06:00
|
|
|
delta = state.ticks;
|
2016-06-24 02:43:46 -06:00
|
|
|
last_pts = AV_NOPTS_VALUE;
|
|
|
|
s_start_dumping = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-11-04 11:19:35 -06:00
|
|
|
delta = state.ticks - s_last_frame;
|
|
|
|
last_pts = (s_last_pts * s_stream->codec->time_base.den) / state.ticks_per_second;
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
u64 pts_in_ticks = s_last_pts + delta;
|
2016-11-04 11:19:35 -06:00
|
|
|
s_scaled_frame->pts = (pts_in_ticks * s_stream->codec->time_base.den) / state.ticks_per_second;
|
2016-06-24 02:43:46 -06:00
|
|
|
if (s_scaled_frame->pts != last_pts)
|
|
|
|
{
|
2016-11-04 11:19:35 -06:00
|
|
|
s_last_frame = state.ticks;
|
2016-06-24 02:43:46 -06:00
|
|
|
s_last_pts = pts_in_ticks;
|
|
|
|
error = avcodec_encode_video2(s_stream->codec, &pkt, s_scaled_frame, &got_packet);
|
|
|
|
}
|
|
|
|
while (!error && got_packet)
|
|
|
|
{
|
|
|
|
// Write the compressed frame in the media file.
|
|
|
|
if (pkt.pts != (s64)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts, s_stream->codec->time_base, s_stream->time_base);
|
|
|
|
}
|
|
|
|
if (pkt.dts != (s64)AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts, s_stream->codec->time_base, s_stream->time_base);
|
|
|
|
}
|
2016-01-18 12:01:11 -07:00
|
|
|
#if LIBAVCODEC_VERSION_INT < AV_VERSION_INT(56, 60, 100)
|
2016-06-24 02:43:46 -06:00
|
|
|
if (s_stream->codec->coded_frame->key_frame)
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
2016-01-18 12:01:11 -07:00
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
pkt.stream_index = s_stream->index;
|
|
|
|
av_interleaved_write_frame(s_format_context, &pkt);
|
|
|
|
|
|
|
|
// Handle delayed frames.
|
|
|
|
PreparePacket(&pkt);
|
|
|
|
error = avcodec_encode_video2(s_stream->codec, &pkt, nullptr, &got_packet);
|
|
|
|
}
|
|
|
|
if (error)
|
|
|
|
ERROR_LOG(VIDEO, "Error while encoding video: %d", error);
|
2010-11-14 14:14:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::Stop()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
av_write_trailer(s_format_context);
|
|
|
|
CloseFile();
|
2016-06-24 20:41:10 -06:00
|
|
|
s_file_index = 0;
|
2016-06-24 02:43:46 -06:00
|
|
|
NOTICE_LOG(VIDEO, "Stopping frame dump");
|
2016-10-07 19:11:37 -06:00
|
|
|
OSD::AddMessage("Stopped dumping frames");
|
2010-11-14 14:14:26 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::CloseFile()
|
|
|
|
{
|
2016-06-24 02:43:46 -06:00
|
|
|
if (s_stream)
|
|
|
|
{
|
|
|
|
if (s_stream->codec)
|
2016-07-10 19:36:18 -06:00
|
|
|
{
|
|
|
|
#if LIBAVCODEC_VERSION_MAJOR < 55
|
|
|
|
avcodec_default_release_buffer(s_stream->codec, s_src_frame);
|
|
|
|
#endif
|
2016-06-24 02:43:46 -06:00
|
|
|
avcodec_close(s_stream->codec);
|
2016-07-10 19:36:18 -06:00
|
|
|
}
|
|
|
|
av_freep(&s_stream);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
av_frame_free(&s_src_frame);
|
|
|
|
av_frame_free(&s_scaled_frame);
|
|
|
|
|
|
|
|
if (s_format_context)
|
|
|
|
{
|
|
|
|
if (s_format_context->pb)
|
|
|
|
avio_close(s_format_context->pb);
|
2016-07-10 19:36:18 -06:00
|
|
|
av_freep(&s_format_context);
|
2016-06-24 02:43:46 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if (s_sws_context)
|
|
|
|
{
|
|
|
|
sws_freeContext(s_sws_context);
|
|
|
|
s_sws_context = nullptr;
|
|
|
|
}
|
2010-11-14 14:14:26 -07:00
|
|
|
}
|
2015-01-25 18:35:29 -07:00
|
|
|
|
|
|
|
void AVIDump::DoState()
|
|
|
|
{
|
2016-11-04 11:24:03 -06:00
|
|
|
s_savestate_index++;
|
2015-01-25 18:35:29 -07:00
|
|
|
}
|
2016-06-24 20:41:10 -06:00
|
|
|
|
|
|
|
void AVIDump::CheckResolution(int width, int height)
|
|
|
|
{
|
2016-07-20 14:46:53 -06: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.
|
2016-10-15 11:06:01 -06:00
|
|
|
if ((width != s_width || height != s_height) && (width > 0 && height > 0))
|
2016-06-24 20:41:10 -06:00
|
|
|
{
|
|
|
|
int temp_file_index = s_file_index;
|
|
|
|
Stop();
|
|
|
|
s_file_index = temp_file_index + 1;
|
2016-10-08 08:23:04 -06:00
|
|
|
Start(width, height);
|
2016-06-24 20:41:10 -06:00
|
|
|
}
|
|
|
|
}
|
2016-11-04 11:19:35 -06:00
|
|
|
|
|
|
|
AVIDump::Frame AVIDump::FetchState(u64 ticks)
|
|
|
|
{
|
|
|
|
Frame state;
|
|
|
|
state.ticks = ticks;
|
|
|
|
state.first_frame = Movie::GetCurrentFrame() < 1;
|
|
|
|
state.ticks_per_second = SystemTimers::GetTicksPerSecond();
|
2016-11-04 11:24:03 -06:00
|
|
|
state.savestate_index = s_savestate_index;
|
2016-11-04 11:19:35 -06:00
|
|
|
return state;
|
|
|
|
}
|