Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G-API: Introduce cv::MediaFrame, a host type for cv::GFrame #18415

Merged
merged 2 commits into from
Sep 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions modules/gapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ set(gapi_srcs
src/api/render_ocv.cpp
src/api/ginfer.cpp
src/api/ft_render.cpp
src/api/media.cpp

# Compiler part
src/compiler/gmodel.cpp
Expand Down
3 changes: 3 additions & 0 deletions modules/gapi/include/opencv2/gapi.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@

#include <opencv2/gapi/gmat.hpp>
#include <opencv2/gapi/garray.hpp>
#include <opencv2/gapi/gscalar.hpp>
#include <opencv2/gapi/gopaque.hpp>
#include <opencv2/gapi/gframe.hpp>
#include <opencv2/gapi/gcomputation.hpp>
#include <opencv2/gapi/gcompiled.hpp>
#include <opencv2/gapi/gtyped.hpp>
Expand Down
8 changes: 8 additions & 0 deletions modules/gapi/include/opencv2/gapi/gframe.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,20 @@ class GAPI_EXPORTS_W_SIMPLE GFrame
};
/** @} */

enum class MediaFormat
{
BGR = 0,
NV12,
};

/**
* \addtogroup gapi_meta_args
* @{
*/
struct GAPI_EXPORTS GFrameDesc
{
MediaFormat fmt;
cv::Size size;
};
static inline GFrameDesc empty_gframe_desc() { return GFrameDesc{}; }
/** @} */
Expand Down
72 changes: 72 additions & 0 deletions modules/gapi/include/opencv2/gapi/media.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation

#ifndef OPENCV_GAPI_MEDIA_HPP
#define OPENCV_GAPI_MEDIA_HPP

#include <memory> // unique_ptr<>, shared_ptr<>
#include <array> // array<>
#include <functional> // function<>
#include <utility> // forward<>()

#include <opencv2/gapi/gframe.hpp>

namespace cv {

class GAPI_EXPORTS MediaFrame {
public:
enum class Access { R, W };
class IAdapter;
class View;
using AdapterPtr = std::unique_ptr<IAdapter>;

MediaFrame();
explicit MediaFrame(AdapterPtr &&);
template<class T, class... Args> static cv::MediaFrame Create(Args&&...);

View access(Access);
cv::GFrameDesc desc() const;

private:
struct Priv;
std::shared_ptr<Priv> m;
};

template<class T, class... Args>
inline cv::MediaFrame cv::MediaFrame::Create(Args&&... args) {
std::unique_ptr<T> ptr(new T(std::forward<Args>(args)...));
return cv::MediaFrame(std::move(ptr));
}

class GAPI_EXPORTS MediaFrame::View final {
public:
static constexpr const size_t MAX_PLANES = 4;
using Ptrs = std::array<void*, MAX_PLANES>;
using Strides = std::array<std::size_t, MAX_PLANES>; // in bytes
using Callback = std::function<void()>;

View(Ptrs&& ptrs, Strides&& strs, Callback &&cb = [](){});
View(const View&) = delete;
View(View&&) = default;
~View();

Ptrs ptr;
Strides stride;

private:
Callback m_cb;
};

class GAPI_EXPORTS MediaFrame::IAdapter {
public:
virtual ~IAdapter() = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This = 0 looks misleading since ~IAdapter is implemented. So it simply can be = default

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is never misleading as it was quite common in pre-Cxx11 era.

My main concern about = default is that when a media.hpp is included in the user application, its destructor will be generated by the user's compiler (different from ours)? I don't trust much to this :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So my point here is that = 0 usually says that user must implement this method in a derived class, but actually it turns out that there is an implementation in a base class and user can utilize this base class implementation, that's what I meant by misleading. You can ask your compiler to generate a destructor by putting = default in a destructor definition at .cpp file

virtual cv::GFrameDesc meta() const = 0;
virtual MediaFrame::View access(MediaFrame::Access) = 0;
};

} //namespace cv

#endif // OPENCV_GAPI_MEDIA_HPP
42 changes: 42 additions & 0 deletions modules/gapi/src/api/media.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// This file is part of OpenCV project.
// It is subject to the license terms in the LICENSE file found in the top-level directory
// of this distribution and at http://opencv.org/license.html.
//
// Copyright (C) 2020 Intel Corporation

#include "precomp.hpp"
Copy link
Member

@alalek alalek Sep 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

relative paths should be used (opencv_world build with PCH is broken - precomp.hpp from another module may be used)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will fix in a follow-up MR, thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait.. We've been never using relative paths for precomp.hpp:

grep -Rn "precomp.h" .
./api/ft_render.cpp:7:#include "precomp.hpp"
./api/garray.cpp:8:#include "precomp.hpp"
./api/gcall.cpp:8:#include "precomp.hpp"
./api/gcomputation.cpp:8:#include "precomp.hpp"
./api/gframe.cpp:8:#include "precomp.hpp"
./api/gframe.cpp~:8:#include "precomp.hpp"
./api/ginfer.cpp:8:#include "precomp.hpp"
./api/gkernel.cpp:8:#include "precomp.hpp"
./api/gmat.cpp:8:#include "precomp.hpp"
./api/gnode.cpp:8:#include "precomp.hpp"
./api/gopaque.cpp:8:#include "precomp.hpp"
./api/gorigin.cpp:8:#include "precomp.hpp"
./api/gproto.cpp:8:#include "precomp.hpp"
./api/gscalar.cpp:8:#include "precomp.hpp"
./api/kernels_core.cpp:8:#include "precomp.hpp"
./api/kernels_imgproc.cpp:8:#include "precomp.hpp"
./api/kernels_nnparsers.cpp:8:#include "precomp.hpp"
./api/kernels_video.cpp:8:#include "precomp.hpp"
./api/operators.cpp:8:#include "precomp.hpp"
./api/render.cpp:1:#include "precomp.hpp"
./backends/common/gcompoundbackend.cpp:8:#include "precomp.hpp"
./backends/common/gcompoundkernel.cpp:8:#include "precomp.hpp"
./backends/cpu/gcpubackend.cpp:8:#include "precomp.hpp"
./backends/cpu/gcpucore.cpp:8:#include "precomp.hpp"
./backends/cpu/gcpuimgproc.cpp:8:#include "precomp.hpp"
./backends/cpu/gcpukernel.cpp:8:#include "precomp.hpp"
./backends/cpu/gcpuvideo.cpp:8:#include "precomp.hpp"
./backends/fluid/gfluidbackend.cpp:8:#include "precomp.hpp"
./backends/fluid/gfluidbuffer.cpp:8:#include "precomp.hpp"
./backends/fluid/gfluidcore.cpp:9:#include "precomp.hpp"
./backends/fluid/gfluidimgproc.cpp:9:#include "precomp.hpp"
./backends/ie/giebackend.cpp:7:#include "precomp.hpp"
./backends/ocl/goclbackend.cpp:8:#include "precomp.hpp"
./backends/ocl/goclcore.cpp:8:#include "precomp.hpp"
./backends/ocl/goclimgproc.cpp:8:#include "precomp.hpp"
./backends/openvx/govxbackend.cpp~:9:#include "precomp.hpp"
./backends/plaidml/gplaidmlbackend.cpp:10:#include "precomp.hpp"
./backends/plaidml/gplaidmlcore.cpp:8:#include "precomp.hpp"
./backends/render/grenderocvbackend.cpp:7:#include "precomp.hpp"
./compiler/gcompiled.cpp:8:#include "precomp.hpp"
./compiler/gcompiler.cpp:8:#include "precomp.hpp"
./compiler/gislandmodel.cpp:8:#include "precomp.hpp"
./compiler/gmodel.cpp:8:#include "precomp.hpp"
./compiler/gmodelbuilder.cpp:14:#include "precomp.hpp"
./compiler/gstreaming.cpp:8:#include "precomp.hpp"
./compiler/passes/dump_dot.cpp:8:#include "precomp.hpp"
./compiler/passes/exec.cpp:8:#include "precomp.hpp"
./compiler/passes/helpers.cpp:8:#include "precomp.hpp"
./compiler/passes/intrin.cpp~:8:#include "precomp.hpp"
./compiler/passes/islands.cpp:8:#include "precomp.hpp"
./compiler/passes/kernels.cpp:8:#include "precomp.hpp"
./compiler/passes/meta.cpp:8:#include "precomp.hpp"
./compiler/passes/streaming.cpp:7:#include "precomp.hpp"
./compiler/passes/transformations.cpp:7:#include "precomp.hpp"
./executor/gexecutor.cpp:8:#include "precomp.hpp"
./executor/gstreamingexecutor.cpp:7:#include "precomp.hpp"

Is this something new we should follow now?

#include <opencv2/gapi/media.hpp>

struct cv::MediaFrame::Priv {
std::unique_ptr<IAdapter> adapter;
};

cv::MediaFrame::MediaFrame() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can simply write = default here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same concern as above

Copy link
Contributor

@rgarnov rgarnov Sep 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's a .cpp, you can ask your compiler to generate this constructor here by

cv::MediaFrame::MediaFrame() = default;

}

cv::MediaFrame::MediaFrame(AdapterPtr &&ptr)
: m(new Priv{std::move(ptr)}) {
}

cv::GFrameDesc cv::MediaFrame::desc() const {
return m->adapter->meta();
}

cv::MediaFrame::View cv::MediaFrame::access(Access code) {
return m->adapter->access(code);
}

cv::MediaFrame::View::View(Ptrs&& ptrs, Strides&& strs, Callback &&cb)
: ptr (std::move(ptrs))
, stride(std::move(strs))
, m_cb (std::move(cb)) {
}

cv::MediaFrame::View::~View() {
if (m_cb) {
m_cb();
}
}

cv::MediaFrame::IAdapter::~IAdapter() {
}
135 changes: 132 additions & 3 deletions modules/gapi/test/gapi_frame_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@

#include "test_precomp.hpp"

#include <opencv2/gapi/cpu/gcpukernel.hpp>
#include <opencv2/gapi/media.hpp>

namespace opencv_test
{
////////////////////////////////////////////////////////////////////////////////
// cv::GFrame tests

namespace opencv_test {

G_API_OP(GBlurFrame, <GMat(GFrame)>, "test.blur_frame") {
static GMatDesc outMeta(GMatDesc in) {
Expand Down Expand Up @@ -53,4 +55,131 @@ TEST_F(GFrameTest, Input) {
check();
}

////////////////////////////////////////////////////////////////////////////////
// cv::MediaFrame tests
namespace {
class TestMediaBGR final: public cv::MediaFrame::IAdapter {
cv::Mat m_mat;
using Cb = cv::MediaFrame::View::Callback;
Cb m_cb;

public:
explicit TestMediaBGR(cv::Mat m, Cb cb = [](){})
: m_mat(m), m_cb(cb) {
}
cv::GFrameDesc meta() const override {
return cv::GFrameDesc{cv::MediaFormat::BGR, cv::Size(m_mat.cols, m_mat.rows)};
}
cv::MediaFrame::View access(cv::MediaFrame::Access) override {
cv::MediaFrame::View::Ptrs pp = { m_mat.ptr(), nullptr, nullptr, nullptr };
cv::MediaFrame::View::Strides ss = { m_mat.step, 0u, 0u, 0u };
return cv::MediaFrame::View(std::move(pp), std::move(ss), Cb{m_cb});
}
};

class TestMediaNV12 final: public cv::MediaFrame::IAdapter {
cv::Mat m_y;
cv::Mat m_uv;
public:
TestMediaNV12(cv::Mat y, cv::Mat uv) : m_y(y), m_uv(uv) {
}
cv::GFrameDesc meta() const override {
return cv::GFrameDesc{cv::MediaFormat::NV12, cv::Size(m_y.cols, m_y.rows)};
}
cv::MediaFrame::View access(cv::MediaFrame::Access) override {
cv::MediaFrame::View::Ptrs pp = {
m_y.ptr(), m_uv.ptr(), nullptr, nullptr
};
cv::MediaFrame::View::Strides ss = {
m_y.step, m_uv.step, 0u, 0u
};
return cv::MediaFrame::View(std::move(pp), std::move(ss));
}
};
} // anonymous namespace

struct MediaFrame_Test: public ::testing::Test {
using M = cv::Mat;
using MF = cv::MediaFrame;
MF frame;
};

struct MediaFrame_BGR: public MediaFrame_Test {
M bgr;
MediaFrame_BGR()
: bgr(M::eye(240, 320, CV_8UC3)) {
frame = MF::Create<TestMediaBGR>(bgr);
}
};

TEST_F(MediaFrame_BGR, Meta) {
auto meta = frame.desc();
EXPECT_EQ(cv::MediaFormat::BGR, meta.fmt);
EXPECT_EQ(cv::Size(320,240), meta.size);
}

TEST_F(MediaFrame_BGR, Access) {
cv::MediaFrame::View view1 = frame.access(cv::MediaFrame::Access::R);
EXPECT_EQ(bgr.ptr(), view1.ptr[0]);
EXPECT_EQ(bgr.step, view1.stride[0]);

cv::MediaFrame::View view2 = frame.access(cv::MediaFrame::Access::R);
EXPECT_EQ(bgr.ptr(), view2.ptr[0]);
EXPECT_EQ(bgr.step, view2.stride[0]);
}

struct MediaFrame_NV12: public MediaFrame_Test {
cv::Size sz;
cv::Mat buf, y, uv;
MediaFrame_NV12()
: sz {320, 240}
, buf(M::eye(sz.height*3/2, sz.width, CV_8UC1))
, y (buf.rowRange(0, sz.height))
, uv (buf.rowRange(sz.height, sz.height*3/2)) {
frame = MF::Create<TestMediaNV12>(y, uv);
}
};

TEST_F(MediaFrame_NV12, Meta) {
auto meta = frame.desc();
EXPECT_EQ(cv::MediaFormat::NV12, meta.fmt);
EXPECT_EQ(cv::Size(320,240), meta.size);
}

TEST_F(MediaFrame_NV12, Access) {
cv::MediaFrame::View view1 = frame.access(cv::MediaFrame::Access::R);
EXPECT_EQ(y. ptr(), view1.ptr [0]);
EXPECT_EQ(y. step, view1.stride[0]);
EXPECT_EQ(uv.ptr(), view1.ptr [1]);
EXPECT_EQ(uv.step, view1.stride[1]);

cv::MediaFrame::View view2 = frame.access(cv::MediaFrame::Access::R);
EXPECT_EQ(y. ptr(), view2.ptr [0]);
EXPECT_EQ(y. step, view2.stride[0]);
EXPECT_EQ(uv.ptr(), view2.ptr [1]);
EXPECT_EQ(uv.step, view2.stride[1]);
}

TEST(MediaFrame, Callback) {
int counter = 0;
cv::Mat bgr = cv::Mat::eye(240, 320, CV_8UC3);
cv::MediaFrame frame = cv::MediaFrame::Create<TestMediaBGR>(bgr, [&counter](){counter++;});

// Test that the callback (in this case, incrementing the counter)
// is called only on View destruction.
EXPECT_EQ(0, counter);
{
cv::MediaFrame::View v1 = frame.access(cv::MediaFrame::Access::R);
EXPECT_EQ(0, counter);
}
EXPECT_EQ(1, counter);
{
cv::MediaFrame::View v1 = frame.access(cv::MediaFrame::Access::R);
EXPECT_EQ(1, counter);
cv::MediaFrame::View v2 = frame.access(cv::MediaFrame::Access::W);
EXPECT_EQ(1, counter);
}
EXPECT_EQ(3, counter);
}

} // namespace opencv_test