2013-04-18 03:09:55 +00:00
|
|
|
// Copyright 2013 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2
|
|
|
|
// Refer to the license.txt file included.
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2011-08-27 18:42:11 +00:00
|
|
|
#if defined(__FreeBSD__)
|
|
|
|
#define __STDC_CONSTANT_MACROS 1
|
2013-10-29 05:23:17 +00:00
|
|
|
#endif
|
2011-08-27 18:42:11 +00:00
|
|
|
|
2014-06-03 05:08:54 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
2014-06-05 23:29:54 +00:00
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
|
2014-10-12 03:12:17 +00:00
|
|
|
#include "Core/CoreTiming.h"
|
2014-10-20 09:11:56 +00:00
|
|
|
#include "Core/HW/SystemTimers.h"
|
2014-02-17 10:18:15 +00:00
|
|
|
#include "Core/HW/VideoInterface.h" //for TargetRefreshRate
|
|
|
|
#include "VideoCommon/AVIDump.h"
|
|
|
|
#include "VideoCommon/VideoConfig.h"
|
2010-11-14 21:14:26 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
#include "tchar.h"
|
|
|
|
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <vfw.h>
|
|
|
|
#include <winerror.h>
|
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
#include "Core/ConfigManager.h" // for EuRGB60
|
|
|
|
#include "Core/CoreTiming.h"
|
|
|
|
|
|
|
|
static HWND s_emu_wnd;
|
|
|
|
static LONG s_byte_buffer;
|
|
|
|
static LONG s_frame_count;
|
|
|
|
static LONG s_total_bytes;
|
|
|
|
static PAVIFILE s_file;
|
|
|
|
static int s_width;
|
|
|
|
static int s_height;
|
|
|
|
static int s_file_count;
|
|
|
|
static u64 s_last_frame;
|
|
|
|
static PAVISTREAM s_stream;
|
|
|
|
static PAVISTREAM s_stream_compressed;
|
|
|
|
static int s_frame_rate;
|
|
|
|
static AVISTREAMINFO s_header;
|
|
|
|
static AVICOMPRESSOPTIONS s_options;
|
|
|
|
static AVICOMPRESSOPTIONS* s_array_options[1];
|
|
|
|
static BITMAPINFOHEADER s_bitmap;
|
|
|
|
// the CFR dump design doesn't let you dump until you know the NEXT timecode.
|
|
|
|
// so we have to save a frame and always be behind
|
|
|
|
static void* s_stored_frame = nullptr;
|
|
|
|
static u64 s_stored_frame_size = 0;
|
|
|
|
bool b_start_dumping = false;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
bool AVIDump::Start(HWND hWnd, int w, int h)
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
s_emu_wnd = hWnd;
|
|
|
|
s_file_count = 0;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
s_width = w;
|
|
|
|
s_height = h;
|
|
|
|
|
|
|
|
s_last_frame = CoreTiming::GetTicks();
|
|
|
|
|
|
|
|
if (SConfig::GetInstance().m_SYSCONF->GetData<u8>("IPL.E60"))
|
|
|
|
s_frame_rate = 60; // always 60, for either pal60 or ntsc
|
|
|
|
else
|
|
|
|
s_frame_rate = VideoInterface::TargetRefreshRate; // 50 or 60, depending on region
|
|
|
|
|
|
|
|
// clear CFR frame cache on start, not on file create (which is also segment switch)
|
|
|
|
SetBitmapFormat();
|
|
|
|
StoreFrame(nullptr);
|
2009-07-06 02:10:26 +00:00
|
|
|
|
|
|
|
return CreateFile();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AVIDump::CreateFile()
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
s_total_bytes = 0;
|
|
|
|
s_frame_count = 0;
|
2014-06-03 05:08:54 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
std::string movie_file_name = StringFromFormat("%sframedump%d.avi", File::GetUserPath(D_DUMPFRAMES_IDX).c_str(), s_file_count);
|
2014-06-03 05:08:54 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
// Create path
|
|
|
|
File::CreateFullPath(movie_file_name);
|
|
|
|
|
|
|
|
// Ask to delete file
|
|
|
|
if (File::Exists(movie_file_name))
|
|
|
|
{
|
2014-06-03 05:08:54 +00:00
|
|
|
if (AskYesNoT("Delete the existing file '%s'?", movie_file_name.c_str()))
|
2009-07-06 02:10:26 +00:00
|
|
|
File::Delete(movie_file_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
AVIFileInit();
|
2014-06-03 05:08:54 +00:00
|
|
|
NOTICE_LOG(VIDEO, "Opening AVI file (%s) for dumping", movie_file_name.c_str());
|
2009-07-06 02:10:26 +00:00
|
|
|
// TODO: Make this work with AVIFileOpenW without it throwing REGDB_E_CLASSNOTREG
|
2014-10-04 07:28:01 +00:00
|
|
|
HRESULT hr = AVIFileOpenA(&s_file, movie_file_name.c_str(), OF_WRITE | OF_CREATE, nullptr);
|
2009-07-26 09:52:35 +00:00
|
|
|
if (FAILED(hr))
|
|
|
|
{
|
2013-10-29 05:23:17 +00:00
|
|
|
if (hr == AVIERR_BADFORMAT) NOTICE_LOG(VIDEO, "The file couldn't be read, indicating a corrupt file or an unrecognized format.");
|
|
|
|
if (hr == AVIERR_MEMORY) NOTICE_LOG(VIDEO, "The file could not be opened because of insufficient memory.");
|
|
|
|
if (hr == AVIERR_FILEREAD) NOTICE_LOG(VIDEO, "A disk error occurred while reading the file.");
|
2009-07-06 02:10:26 +00:00
|
|
|
if (hr == AVIERR_FILEOPEN) NOTICE_LOG(VIDEO, "A disk error occurred while opening the file.");
|
|
|
|
if (hr == REGDB_E_CLASSNOTREG) NOTICE_LOG(VIDEO, "AVI class not registered");
|
|
|
|
Stop();
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
SetBitmapFormat();
|
|
|
|
NOTICE_LOG(VIDEO, "Setting video format...");
|
2009-07-26 09:52:35 +00:00
|
|
|
if (!SetVideoFormat())
|
|
|
|
{
|
2009-07-06 02:10:26 +00:00
|
|
|
NOTICE_LOG(VIDEO, "Setting video format failed");
|
|
|
|
Stop();
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
if (!s_file_count)
|
2013-04-24 13:21:54 +00:00
|
|
|
{
|
|
|
|
if (!SetCompressionOptions())
|
|
|
|
{
|
2009-07-06 02:10:26 +00:00
|
|
|
NOTICE_LOG(VIDEO, "SetCompressionOptions failed");
|
|
|
|
Stop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
if (FAILED(AVIMakeCompressedStream(&s_stream_compressed, s_stream, &s_options, nullptr)))
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2009-07-06 02:10:26 +00:00
|
|
|
NOTICE_LOG(VIDEO, "AVIMakeCompressedStream failed");
|
|
|
|
Stop();
|
|
|
|
return false;
|
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
if (FAILED(AVIStreamSetFormat(s_stream_compressed, 0, &s_bitmap, s_bitmap.biSize)))
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2009-07-06 02:10:26 +00:00
|
|
|
NOTICE_LOG(VIDEO, "AVIStreamSetFormat failed");
|
|
|
|
Stop();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::CloseFile()
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
if (s_stream_compressed)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
AVIStreamClose(s_stream_compressed);
|
|
|
|
s_stream_compressed = nullptr;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
if (s_stream)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
AVIStreamClose(s_stream);
|
|
|
|
s_stream = nullptr;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
if (s_file)
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
AVIFileRelease(s_file);
|
|
|
|
s_file = nullptr;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2009-07-26 09:52:35 +00:00
|
|
|
|
2009-07-06 02:10:26 +00:00
|
|
|
AVIFileExit();
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::Stop()
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
// store one copy of the last video frame, CFR case
|
|
|
|
if (s_stream_compressed)
|
|
|
|
AVIStreamWrite(s_stream_compressed, s_frame_count++, 1, GetFrame(), s_bitmap.biSizeImage, AVIIF_KEYFRAME, nullptr, &s_byte_buffer);
|
|
|
|
b_start_dumping = false;
|
2009-07-06 02:10:26 +00:00
|
|
|
CloseFile();
|
2014-10-04 07:28:01 +00:00
|
|
|
s_file_count = 0;
|
2009-07-06 02:10:26 +00:00
|
|
|
NOTICE_LOG(VIDEO, "Stop");
|
|
|
|
}
|
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
void AVIDump::StoreFrame(const void* data)
|
|
|
|
{
|
|
|
|
if (s_bitmap.biSizeImage > s_stored_frame_size)
|
|
|
|
{
|
|
|
|
void* temp_stored_frame = realloc(s_stored_frame, s_bitmap.biSizeImage);
|
|
|
|
if (temp_stored_frame)
|
|
|
|
{
|
|
|
|
s_stored_frame = temp_stored_frame;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
free(s_stored_frame);
|
|
|
|
PanicAlert("Something has gone seriously wrong.\n"
|
|
|
|
"Stopping video recording.\n"
|
|
|
|
"Your video will likely be broken.");
|
|
|
|
Stop();
|
|
|
|
}
|
|
|
|
s_stored_frame_size = s_bitmap.biSizeImage;
|
|
|
|
memset(s_stored_frame, 0, s_bitmap.biSizeImage);
|
|
|
|
}
|
|
|
|
if (s_stored_frame)
|
|
|
|
{
|
|
|
|
if (data)
|
|
|
|
memcpy(s_stored_frame, data, s_bitmap.biSizeImage);
|
|
|
|
else // pitch black frame
|
|
|
|
memset(s_stored_frame, 0, s_bitmap.biSizeImage);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void* AVIDump::GetFrame()
|
|
|
|
{
|
|
|
|
return s_stored_frame;
|
|
|
|
}
|
|
|
|
|
2013-02-27 01:47:48 +00:00
|
|
|
void AVIDump::AddFrame(const u8* data, int w, int h)
|
2009-07-06 02:10:26 +00:00
|
|
|
{
|
2013-02-27 01:59:33 +00:00
|
|
|
static bool shown_error = false;
|
2014-10-04 07:28:01 +00:00
|
|
|
if ((w != s_bitmap.biWidth || h != s_bitmap.biHeight) && !shown_error)
|
2013-02-27 01:47:48 +00:00
|
|
|
{
|
|
|
|
PanicAlert("You have resized the window while dumping frames.\n"
|
|
|
|
"Nothing sane can be done to handle this.\n"
|
|
|
|
"Your video will likely be broken.");
|
2013-02-27 01:59:33 +00:00
|
|
|
shown_error = true;
|
2013-02-27 01:47:48 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
s_bitmap.biWidth = w;
|
|
|
|
s_bitmap.biHeight = h;
|
2013-02-27 01:47:48 +00:00
|
|
|
}
|
2014-10-04 07:28:01 +00:00
|
|
|
// no timecodes, instead dump each frame as many/few times as needed to keep sync
|
|
|
|
u64 one_cfr = SystemTimers::GetTicksPerSecond() / VideoInterface::TargetRefreshRate;
|
|
|
|
int nplay = 0;
|
|
|
|
s64 delta;
|
|
|
|
if (!b_start_dumping && s_last_frame <= SystemTimers::GetTicksPerSecond())
|
2009-07-26 09:52:35 +00:00
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
delta = CoreTiming::GetTicks();
|
|
|
|
b_start_dumping = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delta = CoreTiming::GetTicks() - s_last_frame;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2014-10-04 07:28:01 +00:00
|
|
|
bool b_frame_dumped = false;
|
|
|
|
// try really hard to place one copy of frame in stream (otherwise it's dropped)
|
|
|
|
if (delta > (s64)one_cfr * 3 / 10) // place if 3/10th of a frame space
|
|
|
|
{
|
|
|
|
delta -= one_cfr;
|
|
|
|
nplay++;
|
|
|
|
}
|
|
|
|
// try not nearly so hard to place additional copies of the frame
|
|
|
|
while (delta > (s64)one_cfr * 8 / 10) // place if 8/10th of a frame space
|
|
|
|
{
|
|
|
|
delta -= one_cfr;
|
|
|
|
nplay++;
|
|
|
|
}
|
|
|
|
while (nplay--)
|
|
|
|
{
|
|
|
|
if (!b_frame_dumped)
|
|
|
|
{
|
|
|
|
AVIStreamWrite(s_stream_compressed, s_frame_count++, 1, GetFrame(), s_bitmap.biSizeImage, AVIIF_KEYFRAME, nullptr, &s_byte_buffer);
|
|
|
|
b_frame_dumped = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AVIStreamWrite(s_stream, s_frame_count++, 1, nullptr, 0, 0, nullptr, nullptr);
|
|
|
|
}
|
|
|
|
s_total_bytes += s_byte_buffer;
|
|
|
|
// Close the recording if the file is larger than 2gb
|
|
|
|
// VfW can't properly save files over 2gb in size, but can keep writing to them up to 4gb.
|
|
|
|
if (s_total_bytes >= 2000000000)
|
|
|
|
{
|
|
|
|
CloseFile();
|
|
|
|
s_file_count++;
|
|
|
|
CreateFile();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
StoreFrame(data);
|
|
|
|
s_last_frame = CoreTiming::GetTicks();
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::SetBitmapFormat()
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
memset(&s_bitmap, 0, sizeof(s_bitmap));
|
|
|
|
s_bitmap.biSize = 0x28;
|
|
|
|
s_bitmap.biPlanes = 1;
|
|
|
|
s_bitmap.biBitCount = 24;
|
|
|
|
s_bitmap.biWidth = s_width;
|
|
|
|
s_bitmap.biHeight = s_height;
|
|
|
|
s_bitmap.biSizeImage = 3 * s_width * s_height;
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AVIDump::SetCompressionOptions()
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
memset(&s_options, 0, sizeof(s_options));
|
|
|
|
s_array_options[0] = &s_options;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
return (AVISaveOptions(s_emu_wnd, 0, 1, &s_stream, s_array_options) != 0);
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AVIDump::SetVideoFormat()
|
|
|
|
{
|
2014-10-04 07:28:01 +00:00
|
|
|
memset(&s_header, 0, sizeof(s_header));
|
|
|
|
s_header.fccType = streamtypeVIDEO;
|
|
|
|
s_header.dwScale = 1;
|
|
|
|
s_header.dwRate = s_frame_rate;
|
|
|
|
s_header.dwSuggestedBufferSize = s_bitmap.biSizeImage;
|
2009-07-06 02:10:26 +00:00
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
return SUCCEEDED(AVIFileCreateStream(s_file, &s_stream, &s_header));
|
2009-07-06 02:10:26 +00:00
|
|
|
}
|
2010-11-14 21:14:26 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
#include "Common/CommonPaths.h"
|
|
|
|
#include "Common/FileUtil.h"
|
|
|
|
#include "Common/StringUtil.h"
|
|
|
|
#include "Common/Logging/Log.h"
|
|
|
|
|
2010-11-14 21:14:26 +00:00
|
|
|
extern "C" {
|
|
|
|
#include <libavcodec/avcodec.h>
|
2010-11-18 16:46:17 +00:00
|
|
|
#include <libavformat/avformat.h>
|
2010-11-14 21:14:26 +00:00
|
|
|
#include <libswscale/swscale.h>
|
2012-05-02 05:29:15 +00:00
|
|
|
#include <libavutil/mathematics.h>
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
2014-07-13 21:06:20 +00: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-15 03:29:05 +00:00
|
|
|
static AVFormatContext* s_format_context = nullptr;
|
|
|
|
static AVStream* s_stream = nullptr;
|
2014-06-24 02:12:15 +00:00
|
|
|
static AVFrame* s_src_frame = nullptr;
|
|
|
|
static AVFrame* s_scaled_frame = nullptr;
|
2014-06-15 03:29:05 +00:00
|
|
|
static uint8_t* s_yuv_buffer = nullptr;
|
2014-06-15 03:16:58 +00:00
|
|
|
static SwsContext* s_sws_context = nullptr;
|
2014-06-15 03:29:05 +00:00
|
|
|
static int s_width;
|
|
|
|
static int s_height;
|
|
|
|
static int s_size;
|
2014-10-04 07:28:01 +00:00
|
|
|
static u64 s_last_frame;
|
|
|
|
bool b_start_dumping = false;
|
2014-10-20 09:11:56 +00:00
|
|
|
static u64 s_last_pts;
|
2010-11-14 21:14:26 +00:00
|
|
|
|
|
|
|
static void InitAVCodec()
|
|
|
|
{
|
|
|
|
static bool first_run = true;
|
|
|
|
if (first_run)
|
|
|
|
{
|
2010-11-18 16:46:17 +00:00
|
|
|
av_register_all();
|
2010-11-14 21:14:26 +00:00
|
|
|
first_run = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool AVIDump::Start(int w, int h)
|
|
|
|
{
|
|
|
|
s_width = w;
|
|
|
|
s_height = h;
|
|
|
|
|
2014-10-04 07:28:01 +00:00
|
|
|
s_last_frame = CoreTiming::GetTicks();
|
2014-10-20 09:11:56 +00:00
|
|
|
s_last_pts = 0;
|
2014-10-04 07:28:01 +00:00
|
|
|
|
2010-11-14 21:14:26 +00:00
|
|
|
InitAVCodec();
|
2014-06-15 03:29:05 +00:00
|
|
|
bool success = CreateFile();
|
|
|
|
if (!success)
|
|
|
|
CloseFile();
|
|
|
|
return success;
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool AVIDump::CreateFile()
|
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
AVCodec* codec = nullptr;
|
2010-11-18 16:46:17 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
s_format_context = avformat_alloc_context();
|
|
|
|
snprintf(s_format_context->filename, sizeof(s_format_context->filename), "%s",
|
|
|
|
(File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump0.avi").c_str());
|
|
|
|
File::CreateFullPath(s_format_context->filename);
|
2010-11-18 16:46:17 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
if (!(s_format_context->oformat = av_guess_format("avi", nullptr, nullptr)) ||
|
|
|
|
!(s_stream = avformat_new_stream(s_format_context, codec)))
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
2010-11-14 21:14:26 +00:00
|
|
|
return false;
|
2010-11-18 16:46:17 +00:00
|
|
|
}
|
|
|
|
|
2014-06-15 03:16:58 +00:00
|
|
|
s_stream->codec->codec_id = g_Config.bUseFFV1 ? AV_CODEC_ID_FFV1
|
|
|
|
: s_format_context->oformat->video_codec;
|
2014-06-15 03:29:05 +00:00
|
|
|
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 = (AVRational){1, static_cast<int>(VideoInterface::TargetRefreshRate)};
|
|
|
|
s_stream->codec->gop_size = 12;
|
2014-06-15 03:16:58 +00:00
|
|
|
s_stream->codec->pix_fmt = g_Config.bUseFFV1 ? AV_PIX_FMT_BGRA : AV_PIX_FMT_YUV420P;
|
2014-06-15 03:29:05 +00:00
|
|
|
|
|
|
|
if (!(codec = avcodec_find_encoder(s_stream->codec->codec_id)) ||
|
|
|
|
(avcodec_open2(s_stream->codec, codec, nullptr) < 0))
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-24 02:12:15 +00:00
|
|
|
s_src_frame = av_frame_alloc();
|
|
|
|
s_scaled_frame = av_frame_alloc();
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
s_size = avpicture_get_size(s_stream->codec->pix_fmt, s_width, s_height);
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
s_yuv_buffer = new uint8_t[s_size];
|
2014-06-24 02:12:15 +00:00
|
|
|
avpicture_fill((AVPicture*)s_scaled_frame, s_yuv_buffer, s_stream->codec->pix_fmt, s_width, s_height);
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
NOTICE_LOG(VIDEO, "Opening file %s for dumping", s_format_context->filename);
|
|
|
|
if (avio_open(&s_format_context->pb, s_format_context->filename, AVIO_FLAG_WRITE) < 0)
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
WARN_LOG(VIDEO, "Could not open %s", s_format_context->filename);
|
2010-11-18 16:46:17 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
avformat_write_header(s_format_context, nullptr);
|
2010-11-18 16:46:17 +00:00
|
|
|
|
2010-11-14 21:14:26 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-06-15 03:16:58 +00:00
|
|
|
static void PreparePacket(AVPacket* pkt)
|
|
|
|
{
|
|
|
|
av_init_packet(pkt);
|
|
|
|
pkt->data = nullptr;
|
|
|
|
pkt->size = 0;
|
|
|
|
}
|
|
|
|
|
2013-02-27 01:47:48 +00:00
|
|
|
void AVIDump::AddFrame(const u8* data, int width, int height)
|
2010-11-14 21:14:26 +00:00
|
|
|
{
|
2014-06-24 02:12:15 +00:00
|
|
|
avpicture_fill((AVPicture*)s_src_frame, const_cast<u8*>(data), AV_PIX_FMT_BGR24, width, height);
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2011-03-25 18:12:40 +00:00
|
|
|
// Convert image from BGR24 to desired pixel format, and scale to initial
|
|
|
|
// width and height
|
2014-06-15 03:16:58 +00:00
|
|
|
if ((s_sws_context = sws_getCachedContext(s_sws_context,
|
|
|
|
width, height, AV_PIX_FMT_BGR24,
|
|
|
|
s_width, s_height, s_stream->codec->pix_fmt,
|
|
|
|
SWS_BICUBIC, nullptr, nullptr, nullptr)))
|
2011-03-25 18:12:40 +00:00
|
|
|
{
|
2014-06-24 02:12:15 +00:00
|
|
|
sws_scale(s_sws_context, s_src_frame->data, s_src_frame->linesize, 0,
|
|
|
|
height, s_scaled_frame->data, s_scaled_frame->linesize);
|
2011-03-25 18:12:40 +00:00
|
|
|
}
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-07-10 00:53:12 +00:00
|
|
|
s_scaled_frame->format = s_stream->codec->pix_fmt;
|
|
|
|
s_scaled_frame->width = s_width;
|
|
|
|
s_scaled_frame->height = s_height;
|
|
|
|
|
2014-06-15 03:16:58 +00:00
|
|
|
// Encode and write the image.
|
|
|
|
AVPacket pkt;
|
|
|
|
PreparePacket(&pkt);
|
2014-10-20 09:11:56 +00:00
|
|
|
int got_packet = 0;
|
|
|
|
int error = 0;
|
|
|
|
u64 delta;
|
|
|
|
s64 last_pts;
|
|
|
|
if (!b_start_dumping && s_last_frame <= SystemTimers::GetTicksPerSecond())
|
|
|
|
{
|
|
|
|
delta = CoreTiming::GetTicks();
|
|
|
|
last_pts = AV_NOPTS_VALUE;
|
|
|
|
b_start_dumping = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
delta = CoreTiming::GetTicks() - s_last_frame;
|
|
|
|
last_pts = (s_last_pts * s_stream->codec->time_base.den) / SystemTimers::GetTicksPerSecond();
|
|
|
|
}
|
|
|
|
u64 pts_in_ticks = s_last_pts + delta;
|
|
|
|
s_scaled_frame->pts = (pts_in_ticks * s_stream->codec->time_base.den) / SystemTimers::GetTicksPerSecond();
|
|
|
|
if (s_scaled_frame->pts != last_pts)
|
|
|
|
{
|
|
|
|
s_last_frame = CoreTiming::GetTicks();
|
|
|
|
s_last_pts = pts_in_ticks;
|
|
|
|
error = avcodec_encode_video2(s_stream->codec, &pkt, s_scaled_frame, &got_packet);
|
|
|
|
}
|
2014-06-15 03:16:58 +00:00
|
|
|
while (!error && got_packet)
|
2010-11-14 21:14:26 +00:00
|
|
|
{
|
2014-06-15 03:16:58 +00:00
|
|
|
// Write the compressed frame in the media file.
|
2014-10-20 09:11:56 +00:00
|
|
|
if (pkt.pts != AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt.pts = av_rescale_q(pkt.pts,
|
|
|
|
s_stream->codec->time_base, s_stream->time_base);
|
|
|
|
}
|
|
|
|
if (pkt.dts != AV_NOPTS_VALUE)
|
|
|
|
{
|
|
|
|
pkt.dts = av_rescale_q(pkt.dts,
|
|
|
|
s_stream->codec->time_base, s_stream->time_base);
|
|
|
|
}
|
|
|
|
if (s_stream->codec->coded_frame->key_frame)
|
|
|
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
|
|
|
pkt.stream_index = s_stream->index;
|
2014-06-15 03:29:05 +00:00
|
|
|
av_interleaved_write_frame(s_format_context, &pkt);
|
2010-11-18 16:46:17 +00:00
|
|
|
|
2014-06-15 03:16:58 +00:00
|
|
|
// Handle delayed frames.
|
|
|
|
PreparePacket(&pkt);
|
|
|
|
error = avcodec_encode_video2(s_stream->codec, &pkt, nullptr, &got_packet);
|
2010-11-18 16:46:17 +00:00
|
|
|
}
|
2014-06-15 03:16:58 +00:00
|
|
|
if (error)
|
|
|
|
ERROR_LOG(VIDEO, "Error while encoding video: %d", error);
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::Stop()
|
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
av_write_trailer(s_format_context);
|
2010-11-14 21:14:26 +00:00
|
|
|
CloseFile();
|
|
|
|
NOTICE_LOG(VIDEO, "Stopping frame dump");
|
|
|
|
}
|
|
|
|
|
|
|
|
void AVIDump::CloseFile()
|
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
if (s_stream)
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
if (s_stream->codec)
|
|
|
|
avcodec_close(s_stream->codec);
|
|
|
|
av_free(s_stream);
|
|
|
|
s_stream = nullptr;
|
2010-11-18 16:46:17 +00:00
|
|
|
}
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
if (s_yuv_buffer)
|
2014-06-15 03:16:58 +00:00
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
delete[] s_yuv_buffer;
|
2014-06-15 03:16:58 +00:00
|
|
|
s_yuv_buffer = nullptr;
|
|
|
|
}
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-06-24 02:12:15 +00:00
|
|
|
av_frame_free(&s_src_frame);
|
|
|
|
av_frame_free(&s_scaled_frame);
|
2010-11-14 21:14:26 +00:00
|
|
|
|
2014-06-15 03:29:05 +00:00
|
|
|
if (s_format_context)
|
2010-11-18 16:46:17 +00:00
|
|
|
{
|
2014-06-15 03:29:05 +00:00
|
|
|
if (s_format_context->pb)
|
|
|
|
avio_close(s_format_context->pb);
|
|
|
|
av_free(s_format_context);
|
|
|
|
s_format_context = nullptr;
|
2010-11-18 16:46:17 +00:00
|
|
|
}
|
2014-06-15 03:16:58 +00:00
|
|
|
|
|
|
|
if (s_sws_context)
|
|
|
|
{
|
|
|
|
sws_freeContext(s_sws_context);
|
|
|
|
s_sws_context = nullptr;
|
|
|
|
}
|
2010-11-14 21:14:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2014-10-04 07:28:01 +00:00
|
|
|
|
|
|
|
void AVIDump::DoState()
|
|
|
|
{
|
|
|
|
s_last_frame = CoreTiming::GetTicks();
|
|
|
|
}
|