summaryrefslogtreecommitdiff
path: root/src/plugins/multimedia/ffmpeg
diff options
context:
space:
mode:
authorArtem Dyomin <artem.dyomin@qt.io>2023-02-15 18:12:07 +0100
committerArtem Dyomin <artem.dyomin@qt.io>2023-02-17 10:05:51 +0100
commit5c7fe35edbca4f6eee5b4126727d3a9f95c7697f (patch)
tree8d3579ab0250ff2c7662753213624c3faaba0cd7 /src/plugins/multimedia/ffmpeg
parent68430756ac8f477bf91f9691d39f1605829f9500 (diff)
downloadqtmultimedia-5c7fe35edbca4f6eee5b4126727d3a9f95c7697f.tar.gz
Minor cleanup of duplicated code
Remove duplicated deleters; use a template instead Pick-to: 6.5 6.4 Change-Id: I79d4ae2866199583fec0d3dd357274a81f40b7fa Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org> Reviewed-by: Lars Knoll <lars@knoll.priv.no>
Diffstat (limited to 'src/plugins/multimedia/ffmpeg')
-rw-r--r--src/plugins/multimedia/ffmpeg/qffmpeg_p.h38
-rw-r--r--src/plugins/multimedia/ffmpeg/qffmpeghwaccel.cpp12
-rw-r--r--src/plugins/multimedia/ffmpeg/qffmpeghwaccel_p.h8
3 files changed, 21 insertions, 37 deletions
diff --git a/src/plugins/multimedia/ffmpeg/qffmpeg_p.h b/src/plugins/multimedia/ffmpeg/qffmpeg_p.h
index e9da8319d..2e01521fc 100644
--- a/src/plugins/multimedia/ffmpeg/qffmpeg_p.h
+++ b/src/plugins/multimedia/ffmpeg/qffmpeg_p.h
@@ -53,43 +53,33 @@ inline QString err2str(int errnum)
return QString::fromLocal8Bit(buffer);
}
-struct AVFrameDeleter
+template<typename FunctionType, FunctionType F>
+struct AVDeleter
{
- void operator()(AVFrame *frame) const
+ template<typename T>
+ void operator()(T *object) const
{
- if (frame)
- av_frame_free(&frame);
+ if (object)
+ F(&object);
}
};
-using AVFrameUPtr = std::unique_ptr<AVFrame, AVFrameDeleter>;
+using AVFrameUPtr = std::unique_ptr<AVFrame, AVDeleter<decltype(&av_frame_free), &av_frame_free>>;
inline AVFrameUPtr makeAVFrame()
{
return AVFrameUPtr(av_frame_alloc());
}
-struct AVPacketDeleter
-{
- void operator()(AVPacket *packet) const
- {
- if (packet)
- av_packet_free(&packet);
- }
-};
-
-using AVPacketUPtr = std::unique_ptr<AVPacket, AVPacketDeleter>;
+using AVPacketUPtr =
+ std::unique_ptr<AVPacket, AVDeleter<decltype(&av_packet_free), &av_packet_free>>;
-struct AVCodecContextDeleter
-{
- void operator()(AVCodecContext *context) const
- {
- if (context)
- avcodec_free_context(&context);
- }
-};
+using AVCodecContextUPtr =
+ std::unique_ptr<AVCodecContext,
+ AVDeleter<decltype(&avcodec_free_context), &avcodec_free_context>>;
-using AVCodecContextUPtr = std::unique_ptr<AVCodecContext, AVCodecContextDeleter>;
+using AVBufferUPtr =
+ std::unique_ptr<AVBufferRef, AVDeleter<decltype(&av_buffer_unref), &av_buffer_unref>>;
QT_END_NAMESPACE
diff --git a/src/plugins/multimedia/ffmpeg/qffmpeghwaccel.cpp b/src/plugins/multimedia/ffmpeg/qffmpeghwaccel.cpp
index affe8fd8a..5b3cf7474 100644
--- a/src/plugins/multimedia/ffmpeg/qffmpeghwaccel.cpp
+++ b/src/plugins/multimedia/ffmpeg/qffmpeghwaccel.cpp
@@ -135,13 +135,7 @@ TextureConverter::Data::~Data()
delete backend;
}
-HWAccel::~HWAccel()
-{
- if (m_hwDeviceContext)
- av_buffer_unref(&m_hwDeviceContext);
- if (m_hwFramesContext)
- av_buffer_unref(&m_hwFramesContext);
-}
+HWAccel::~HWAccel() = default;
std::unique_ptr<HWAccel> HWAccel::create(const AVCodec *codec)
{
@@ -309,14 +303,14 @@ void HWAccel::createFramesContext(AVPixelFormat swFormat, const QSize &size)
if (!m_hwDeviceContext)
return;
- m_hwFramesContext = av_hwframe_ctx_alloc(m_hwDeviceContext);
+ m_hwFramesContext.reset(av_hwframe_ctx_alloc(m_hwDeviceContext.get()));
auto *c = (AVHWFramesContext *)m_hwFramesContext->data;
c->format = hwFormat();
c->sw_format = swFormat;
c->width = size.width();
c->height = size.height();
qCDebug(qLHWAccel) << "init frames context";
- int err = av_hwframe_ctx_init(m_hwFramesContext);
+ int err = av_hwframe_ctx_init(m_hwFramesContext.get());
if (err < 0)
qWarning() << "failed to init HW frame context" << err << err2str(err);
else
diff --git a/src/plugins/multimedia/ffmpeg/qffmpeghwaccel_p.h b/src/plugins/multimedia/ffmpeg/qffmpeghwaccel_p.h
index 81bb163bb..b18883369 100644
--- a/src/plugins/multimedia/ffmpeg/qffmpeghwaccel_p.h
+++ b/src/plugins/multimedia/ffmpeg/qffmpeghwaccel_p.h
@@ -82,8 +82,8 @@ private:
class HWAccel
{
- AVBufferRef *m_hwDeviceContext = nullptr;
- AVBufferRef *m_hwFramesContext = nullptr;
+ AVBufferUPtr m_hwDeviceContext;
+ AVBufferUPtr m_hwFramesContext;
public:
~HWAccel();
@@ -97,12 +97,12 @@ public:
AVHWDeviceType deviceType() const;
- AVBufferRef *hwDeviceContextAsBuffer() const { return m_hwDeviceContext; }
+ AVBufferRef *hwDeviceContextAsBuffer() const { return m_hwDeviceContext.get(); }
AVHWDeviceContext *hwDeviceContext() const;
AVPixelFormat hwFormat() const;
void createFramesContext(AVPixelFormat swFormat, const QSize &size);
- AVBufferRef *hwFramesContextAsBuffer() const { return m_hwFramesContext; }
+ AVBufferRef *hwFramesContextAsBuffer() const { return m_hwFramesContext.get(); }
AVHWFramesContext *hwFramesContext() const;
static AVPixelFormat format(AVFrame *frame);