mirror of
https://github.com/dolphin-emu/dolphin.git
synced 2025-07-23 14:19:46 -06:00
AVIDump: cleanup
This commit is contained in:
@ -215,14 +215,15 @@ extern "C" {
|
|||||||
#include <libavutil/mathematics.h>
|
#include <libavutil/mathematics.h>
|
||||||
}
|
}
|
||||||
|
|
||||||
AVFormatContext *s_FormatContext = nullptr;
|
static AVFormatContext* s_format_context = nullptr;
|
||||||
AVStream *s_Stream = nullptr;
|
static AVStream* s_stream = nullptr;
|
||||||
AVFrame *s_BGRFrame = nullptr, *s_YUVFrame = nullptr;
|
static AVFrame* s_bgr_frame = nullptr;
|
||||||
uint8_t *s_YUVBuffer = nullptr;
|
static AVFrame* s_yuv_frame = nullptr;
|
||||||
uint8_t *s_OutBuffer = nullptr;
|
static uint8_t* s_yuv_buffer = nullptr;
|
||||||
int s_width;
|
static uint8_t* s_out_buffer = nullptr;
|
||||||
int s_height;
|
static int s_width;
|
||||||
int s_size;
|
static int s_height;
|
||||||
|
static int s_size;
|
||||||
|
|
||||||
static void InitAVCodec()
|
static void InitAVCodec()
|
||||||
{
|
{
|
||||||
@ -240,143 +241,144 @@ bool AVIDump::Start(int w, int h)
|
|||||||
s_height = h;
|
s_height = h;
|
||||||
|
|
||||||
InitAVCodec();
|
InitAVCodec();
|
||||||
return CreateFile();
|
bool success = CreateFile();
|
||||||
|
if (!success)
|
||||||
|
CloseFile();
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AVIDump::CreateFile()
|
bool AVIDump::CreateFile()
|
||||||
{
|
{
|
||||||
AVCodec* codec = nullptr;
|
AVCodec* codec = nullptr;
|
||||||
|
|
||||||
s_FormatContext = avformat_alloc_context();
|
s_format_context = avformat_alloc_context();
|
||||||
snprintf(s_FormatContext->filename, sizeof(s_FormatContext->filename), "%s",
|
snprintf(s_format_context->filename, sizeof(s_format_context->filename), "%s",
|
||||||
(File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump0.avi").c_str());
|
(File::GetUserPath(D_DUMPFRAMES_IDX) + "framedump0.avi").c_str());
|
||||||
File::CreateFullPath(s_FormatContext->filename);
|
File::CreateFullPath(s_format_context->filename);
|
||||||
|
|
||||||
if (!(s_FormatContext->oformat = av_guess_format("avi", nullptr, nullptr)) ||
|
if (!(s_format_context->oformat = av_guess_format("avi", nullptr, nullptr)) ||
|
||||||
!(s_Stream = avformat_new_stream(s_FormatContext, codec)))
|
!(s_stream = avformat_new_stream(s_format_context, codec)))
|
||||||
{
|
{
|
||||||
CloseFile();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
s_Stream->codec->codec_id =
|
s_stream->codec->codec_id = g_Config.bUseFFV1 ? CODEC_ID_FFV1 : s_format_context->oformat->video_codec;
|
||||||
g_Config.bUseFFV1 ? CODEC_ID_FFV1 : s_FormatContext->oformat->video_codec;
|
s_stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
||||||
s_Stream->codec->codec_type = AVMEDIA_TYPE_VIDEO;
|
s_stream->codec->bit_rate = 400000;
|
||||||
s_Stream->codec->bit_rate = 400000;
|
s_stream->codec->width = s_width;
|
||||||
s_Stream->codec->width = s_width;
|
s_stream->codec->height = s_height;
|
||||||
s_Stream->codec->height = s_height;
|
s_stream->codec->time_base = (AVRational){1, static_cast<int>(VideoInterface::TargetRefreshRate)};
|
||||||
s_Stream->codec->time_base = (AVRational){1, static_cast<int>(VideoInterface::TargetRefreshRate)};
|
s_stream->codec->gop_size = 12;
|
||||||
s_Stream->codec->gop_size = 12;
|
s_stream->codec->pix_fmt = g_Config.bUseFFV1 ? PIX_FMT_BGRA : PIX_FMT_YUV420P;
|
||||||
s_Stream->codec->pix_fmt = g_Config.bUseFFV1 ? PIX_FMT_BGRA : PIX_FMT_YUV420P;
|
|
||||||
|
|
||||||
if (!(codec = avcodec_find_encoder(s_Stream->codec->codec_id)) ||
|
if (!(codec = avcodec_find_encoder(s_stream->codec->codec_id)) ||
|
||||||
(avcodec_open2(s_Stream->codec, codec, nullptr) < 0))
|
(avcodec_open2(s_stream->codec, codec, nullptr) < 0))
|
||||||
{
|
{
|
||||||
CloseFile();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
s_BGRFrame = avcodec_alloc_frame();
|
s_bgr_frame = avcodec_alloc_frame();
|
||||||
s_YUVFrame = avcodec_alloc_frame();
|
s_yuv_frame = avcodec_alloc_frame();
|
||||||
|
|
||||||
s_size = avpicture_get_size(s_Stream->codec->pix_fmt, s_width, s_height);
|
s_size = avpicture_get_size(s_stream->codec->pix_fmt, s_width, s_height);
|
||||||
|
|
||||||
s_YUVBuffer = new uint8_t[s_size];
|
s_yuv_buffer = new uint8_t[s_size];
|
||||||
avpicture_fill((AVPicture *)s_YUVFrame, s_YUVBuffer, s_Stream->codec->pix_fmt, s_width, s_height);
|
avpicture_fill((AVPicture*)s_yuv_frame, s_yuv_buffer, s_stream->codec->pix_fmt, s_width, s_height);
|
||||||
|
|
||||||
s_OutBuffer = new uint8_t[s_size];
|
s_out_buffer = new uint8_t[s_size];
|
||||||
|
|
||||||
NOTICE_LOG(VIDEO, "Opening file %s for dumping", s_FormatContext->filename);
|
NOTICE_LOG(VIDEO, "Opening file %s for dumping", s_format_context->filename);
|
||||||
if (avio_open(&s_FormatContext->pb, s_FormatContext->filename, AVIO_FLAG_WRITE) < 0)
|
if (avio_open(&s_format_context->pb, s_format_context->filename, AVIO_FLAG_WRITE) < 0)
|
||||||
{
|
{
|
||||||
WARN_LOG(VIDEO, "Could not open %s", s_FormatContext->filename);
|
WARN_LOG(VIDEO, "Could not open %s", s_format_context->filename);
|
||||||
CloseFile();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
avformat_write_header(s_FormatContext, nullptr);
|
avformat_write_header(s_format_context, nullptr);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AVIDump::AddFrame(const u8* data, int width, int height)
|
void AVIDump::AddFrame(const u8* data, int width, int height)
|
||||||
{
|
{
|
||||||
avpicture_fill((AVPicture *)s_BGRFrame, const_cast<u8*>(data), PIX_FMT_BGR24, width, height);
|
avpicture_fill((AVPicture*)s_bgr_frame, const_cast<u8*>(data), PIX_FMT_BGR24, width, height);
|
||||||
|
|
||||||
// Convert image from BGR24 to desired pixel format, and scale to initial
|
// Convert image from BGR24 to desired pixel format, and scale to initial
|
||||||
// width and height
|
// width and height
|
||||||
struct SwsContext *s_SwsContext;
|
struct SwsContext* s_sws_context;
|
||||||
if ((s_SwsContext = sws_getContext(width, height, PIX_FMT_BGR24, s_width, s_height,
|
if ((s_sws_context = sws_getContext(width, height, PIX_FMT_BGR24, s_width, s_height,
|
||||||
s_Stream->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr)))
|
s_stream->codec->pix_fmt, SWS_BICUBIC, nullptr, nullptr, nullptr)))
|
||||||
{
|
{
|
||||||
sws_scale(s_SwsContext, s_BGRFrame->data, s_BGRFrame->linesize, 0,
|
sws_scale(s_sws_context, s_bgr_frame->data, s_bgr_frame->linesize, 0,
|
||||||
height, s_YUVFrame->data, s_YUVFrame->linesize);
|
height, s_yuv_frame->data, s_yuv_frame->linesize);
|
||||||
sws_freeContext(s_SwsContext);
|
sws_freeContext(s_sws_context);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Encode and write the image
|
// Encode and write the image
|
||||||
int outsize = avcodec_encode_video(s_Stream->codec, s_OutBuffer, s_size, s_YUVFrame);
|
int outsize = avcodec_encode_video(s_stream->codec, s_out_buffer, s_size, s_yuv_frame);
|
||||||
while (outsize > 0)
|
while (outsize > 0)
|
||||||
{
|
{
|
||||||
AVPacket pkt;
|
AVPacket pkt;
|
||||||
av_init_packet(&pkt);
|
av_init_packet(&pkt);
|
||||||
|
|
||||||
if (s_Stream->codec->coded_frame->pts != (unsigned int)AV_NOPTS_VALUE)
|
if (s_stream->codec->coded_frame->pts != (unsigned int)AV_NOPTS_VALUE)
|
||||||
pkt.pts = av_rescale_q(s_Stream->codec->coded_frame->pts,
|
{
|
||||||
s_Stream->codec->time_base, s_Stream->time_base);
|
pkt.pts = av_rescale_q(s_stream->codec->coded_frame->pts,
|
||||||
if (s_Stream->codec->coded_frame->key_frame)
|
s_stream->codec->time_base, s_stream->time_base);
|
||||||
|
}
|
||||||
|
if (s_stream->codec->coded_frame->key_frame)
|
||||||
pkt.flags |= AV_PKT_FLAG_KEY;
|
pkt.flags |= AV_PKT_FLAG_KEY;
|
||||||
pkt.stream_index = s_Stream->index;
|
pkt.stream_index = s_stream->index;
|
||||||
pkt.data = s_OutBuffer;
|
pkt.data = s_out_buffer;
|
||||||
pkt.size = outsize;
|
pkt.size = outsize;
|
||||||
|
|
||||||
// Write the compressed frame in the media file
|
// Write the compressed frame in the media file
|
||||||
av_interleaved_write_frame(s_FormatContext, &pkt);
|
av_interleaved_write_frame(s_format_context, &pkt);
|
||||||
|
|
||||||
// Encode delayed frames
|
// Encode delayed frames
|
||||||
outsize = avcodec_encode_video(s_Stream->codec, s_OutBuffer, s_size, nullptr);
|
outsize = avcodec_encode_video(s_stream->codec, s_out_buffer, s_size, nullptr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void AVIDump::Stop()
|
void AVIDump::Stop()
|
||||||
{
|
{
|
||||||
av_write_trailer(s_FormatContext);
|
av_write_trailer(s_format_context);
|
||||||
CloseFile();
|
CloseFile();
|
||||||
NOTICE_LOG(VIDEO, "Stopping frame dump");
|
NOTICE_LOG(VIDEO, "Stopping frame dump");
|
||||||
}
|
}
|
||||||
|
|
||||||
void AVIDump::CloseFile()
|
void AVIDump::CloseFile()
|
||||||
{
|
{
|
||||||
if (s_Stream)
|
if (s_stream)
|
||||||
{
|
{
|
||||||
if (s_Stream->codec)
|
if (s_stream->codec)
|
||||||
avcodec_close(s_Stream->codec);
|
avcodec_close(s_stream->codec);
|
||||||
av_free(s_Stream);
|
av_free(s_stream);
|
||||||
s_Stream = nullptr;
|
s_stream = nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (s_YUVBuffer)
|
if (s_yuv_buffer)
|
||||||
delete[] s_YUVBuffer;
|
delete[] s_yuv_buffer;
|
||||||
s_YUVBuffer = nullptr;
|
s_yuv_buffer = nullptr;
|
||||||
|
|
||||||
if (s_OutBuffer)
|
if (s_out_buffer)
|
||||||
delete[] s_OutBuffer;
|
delete[] s_out_buffer;
|
||||||
s_OutBuffer = nullptr;
|
s_out_buffer = nullptr;
|
||||||
|
|
||||||
if (s_BGRFrame)
|
if (s_bgr_frame)
|
||||||
av_free(s_BGRFrame);
|
av_free(s_bgr_frame);
|
||||||
s_BGRFrame = nullptr;
|
s_bgr_frame = nullptr;
|
||||||
|
|
||||||
if (s_YUVFrame)
|
if (s_yuv_frame)
|
||||||
av_free(s_YUVFrame);
|
av_free(s_yuv_frame);
|
||||||
s_YUVFrame = nullptr;
|
s_yuv_frame = nullptr;
|
||||||
|
|
||||||
if (s_FormatContext)
|
if (s_format_context)
|
||||||
{
|
{
|
||||||
if (s_FormatContext->pb)
|
if (s_format_context->pb)
|
||||||
avio_close(s_FormatContext->pb);
|
avio_close(s_format_context->pb);
|
||||||
av_free(s_FormatContext);
|
av_free(s_format_context);
|
||||||
s_FormatContext = nullptr;
|
s_format_context = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user