Skip to content

Commit

Permalink
Merge pull request #3946 from comex/avi-warning-fix
Browse files Browse the repository at this point in the history
Use newer ffmpeg APIs to avoid deprecation warnings.
  • Loading branch information
RisingFog committed Jul 11, 2016
2 parents f2474e0 + 4266029 commit 64a4e6b
Showing 1 changed file with 31 additions and 24 deletions.
55 changes: 31 additions & 24 deletions Source/Core/VideoCommon/AVIDump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ static AVStream* s_stream = nullptr;
static AVFrame* s_src_frame = nullptr;
static AVFrame* s_scaled_frame = nullptr;
static AVPixelFormat s_pix_fmt = AV_PIX_FMT_BGR24;
static uint8_t* s_yuv_buffer = nullptr;
static int s_bytes_per_pixel;
static SwsContext* s_sws_context = nullptr;
static int s_width;
static int s_height;
static int s_size;
static u64 s_last_frame;
static bool s_start_dumping = false;
static u64 s_last_pts;
Expand All @@ -63,9 +62,15 @@ static void InitAVCodec()
bool AVIDump::Start(int w, int h, DumpFormat format)
{
if (format == DumpFormat::FORMAT_BGR)
{
s_pix_fmt = AV_PIX_FMT_BGR24;
s_bytes_per_pixel = 3;
}
else
{
s_pix_fmt = AV_PIX_FMT_RGBA;
s_bytes_per_pixel = 4;
}

s_current_format = format;

Expand Down Expand Up @@ -140,21 +145,26 @@ bool AVIDump::CreateFile()
s_src_frame = av_frame_alloc();
s_scaled_frame = av_frame_alloc();

s_size = avpicture_get_size(s_stream->codec->pix_fmt, s_width, s_height);
s_scaled_frame->format = s_stream->codec->pix_fmt;
s_scaled_frame->width = s_width;
s_scaled_frame->height = s_height;

s_yuv_buffer = new uint8_t[s_size];
avpicture_fill((AVPicture*)s_scaled_frame, s_yuv_buffer, s_stream->codec->pix_fmt, s_width,
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_stream->codec, s_scaled_frame))
return false;
#endif

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)
if (avio_open(&s_format_context->pb, s_format_context->filename, AVIO_FLAG_WRITE) < 0 ||
avformat_write_header(s_format_context, nullptr))
{
WARN_LOG(VIDEO, "Could not open %s", s_format_context->filename);
return false;
}

avformat_write_header(s_format_context, nullptr);

return true;
}

Expand All @@ -168,7 +178,11 @@ static void PreparePacket(AVPacket* pkt)
void AVIDump::AddFrame(const u8* data, int width, int height)
{
CheckResolution(width, height);
avpicture_fill((AVPicture*)s_src_frame, const_cast<u8*>(data), s_pix_fmt, width, height);
s_src_frame->data[0] = const_cast<u8*>(data);
s_src_frame->linesize[0] = width * s_bytes_per_pixel;
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, and scale to initial
// width and height
Expand All @@ -180,10 +194,6 @@ void AVIDump::AddFrame(const u8* data, int width, int height)
s_scaled_frame->data, s_scaled_frame->linesize);
}

s_scaled_frame->format = s_stream->codec->pix_fmt;
s_scaled_frame->width = s_width;
s_scaled_frame->height = s_height;

// Encode and write the image.
AVPacket pkt;
PreparePacket(&pkt);
Expand Down Expand Up @@ -253,15 +263,13 @@ void AVIDump::CloseFile()
if (s_stream)
{
if (s_stream->codec)
{
#if LIBAVCODEC_VERSION_MAJOR < 55
avcodec_default_release_buffer(s_stream->codec, s_src_frame);
#endif
avcodec_close(s_stream->codec);
av_free(s_stream);
s_stream = nullptr;
}

if (s_yuv_buffer)
{
delete[] s_yuv_buffer;
s_yuv_buffer = nullptr;
}
av_freep(&s_stream);
}

av_frame_free(&s_src_frame);
Expand All @@ -271,8 +279,7 @@ void AVIDump::CloseFile()
{
if (s_format_context->pb)
avio_close(s_format_context->pb);
av_free(s_format_context);
s_format_context = nullptr;
av_freep(&s_format_context);
}

if (s_sws_context)
Expand Down

0 comments on commit 64a4e6b

Please sign in to comment.