From 38a0844cdf8a876d50c017f36aa64be2cf3ff60a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Tue, 12 Mar 2019 23:51:46 +0100 Subject: [PATCH] decoder/ffmpeg: add AVFrame wrapper class --- src/decoder/plugins/FfmpegDecoderPlugin.cxx | 11 +---- src/lib/ffmpeg/Frame.hxx | 54 +++++++++++++++++++++ 2 files changed, 56 insertions(+), 9 deletions(-) create mode 100644 src/lib/ffmpeg/Frame.hxx diff --git a/src/decoder/plugins/FfmpegDecoderPlugin.cxx b/src/decoder/plugins/FfmpegDecoderPlugin.cxx index 3bd0ecad5..0d96e119d 100644 --- a/src/decoder/plugins/FfmpegDecoderPlugin.cxx +++ b/src/decoder/plugins/FfmpegDecoderPlugin.cxx @@ -27,6 +27,7 @@ #include "lib/ffmpeg/LogError.hxx" #include "lib/ffmpeg/Init.hxx" #include "lib/ffmpeg/Buffer.hxx" +#include "lib/ffmpeg/Frame.hxx" #include "../DecoderAPI.hxx" #include "FfmpegMetaData.hxx" #include "FfmpegIo.hxx" @@ -574,15 +575,7 @@ FfmpegDecode(DecoderClient &client, InputStream &input, FfmpegParseMetaData(client, format_context, audio_stream); - AVFrame *frame = av_frame_alloc(); - if (!frame) { - LogError(ffmpeg_domain, "Could not allocate frame"); - return; - } - - AtScopeExit(&frame) { - av_frame_free(&frame); - }; + Ffmpeg::Frame frame; FfmpegBuffer interleaved_buffer; diff --git a/src/lib/ffmpeg/Frame.hxx b/src/lib/ffmpeg/Frame.hxx new file mode 100644 index 000000000..c5fb32f86 --- /dev/null +++ b/src/lib/ffmpeg/Frame.hxx @@ -0,0 +1,54 @@ +/* + * Copyright 2003-2019 The Music Player Daemon Project + * http://www.musicpd.org + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef MPD_FFMPEG_FRAME_HXX +#define MPD_FFMPEG_FRAME_HXX + +extern "C" { +#include +} + +#include + +namespace Ffmpeg { + +class Frame { + AVFrame *frame; + +public: + Frame():frame(av_frame_alloc()) { + if (frame == nullptr) + throw std::bad_alloc(); + } + + ~Frame() noexcept { + av_frame_free(&frame); + } + + Frame(const Frame &) = delete; + Frame &operator=(const Frame &) = delete; + + AVFrame &operator*() noexcept { + return *frame; + } +}; + +} // namespace Ffmpeg + +#endif