File: gapi_streaming_sync_tests.cpp

package info (click to toggle)
opencv 4.10.0%2Bdfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 282,092 kB
  • sloc: cpp: 1,178,079; xml: 682,621; python: 49,092; lisp: 31,150; java: 25,469; ansic: 11,039; javascript: 6,085; sh: 1,214; cs: 601; perl: 494; objc: 210; makefile: 173
file content (220 lines) | stat: -rw-r--r-- 6,908 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// 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) 2021 Intel Corporation

#include "../test_precomp.hpp"

#include <opencv2/gapi/streaming/cap.hpp>
#include <opencv2/gapi/core.hpp>
#include <opencv2/gapi/fluid/imgproc.hpp>
#include <opencv2/gapi/streaming/cap.hpp>
#include <opencv2/gapi/streaming/sync.hpp>

namespace opencv_test {
namespace {

using ts_t = int64_t;
using ts_vec = std::vector<ts_t>;
using cv::gapi::streaming::sync_policy;

ts_t calcLeastCommonMultiple(const ts_vec& values) {
    ts_t res = *std::max_element(values.begin(), values.end());
    auto isDivisor = [&](ts_t v) { return res % v == 0; };
    while(!std::all_of(values.begin(), values.end(), isDivisor)) {
        res++;
    }
    return res;
}

struct TimestampGenerationParams {
    const ts_vec frame_times;
    sync_policy policy;
    ts_t end_time;
    TimestampGenerationParams(const ts_vec& ft, sync_policy sp, ts_t et = 25)
        : frame_times(ft), policy(sp), end_time(et) {
    }
};

class MultiFrameSource {
    class SingleSource : public cv::gapi::wip::IStreamSource {
        MultiFrameSource& m_source;
        std::size_t m_idx;
    public:
        SingleSource(MultiFrameSource& s, std::size_t idx)
            : m_source(s)
            , m_idx(idx)
        {}
        virtual bool pull(cv::gapi::wip::Data& data) {
            return m_source.pull(data, m_idx);
        }
        virtual GMetaArg descr_of() const { return GMetaArg{m_source.desc()}; }
    };

    TimestampGenerationParams p;
    ts_vec m_current_times;
    cv::Mat m_mat;

public:
    MultiFrameSource(const TimestampGenerationParams& params)
        : p(params)
        , m_current_times(p.frame_times.size(), 0u)
        , m_mat(8, 8, CV_8UC1) {
    }

    bool pull(cv::gapi::wip::Data& data, std::size_t idx) {
        cv::randn(m_mat, 127, 32);
        GAPI_Assert(idx < p.frame_times.size());
        m_current_times[idx] += p.frame_times[idx];
        if (m_current_times[idx] >= p.end_time) {
            return false;
        }
        data = m_mat.clone();
        data.meta[cv::gapi::streaming::meta_tag::timestamp] = m_current_times[idx];
        return true;
    }

    cv::gapi::wip::IStreamSource::Ptr getSource(std::size_t idx) {
        return cv::gapi::wip::IStreamSource::Ptr{new SingleSource(*this, idx)};
    }

    GMatDesc desc() const { return cv::descr_of(m_mat); }
};

class TimestampChecker {
    TimestampGenerationParams p;
    ts_t m_synced_time = 0u;
    ts_t m_synced_frame_time = 0u;
public:
    TimestampChecker(const TimestampGenerationParams& params)
        : p(params)
        , m_synced_frame_time(calcLeastCommonMultiple(p.frame_times)) {
    }

    void checkNext(const ts_vec& timestamps) {
        if (p.policy == sync_policy::dont_sync) {
            // don't check timestamps if the policy is dont_sync
            return;
        }
        m_synced_time += m_synced_frame_time;
        for (const auto& ts : timestamps) {
            EXPECT_EQ(m_synced_time, ts);
        }
    }

    std::size_t nFrames() const {
        auto frame_time = p.policy == sync_policy::dont_sync
                          ? *std::max_element(p.frame_times.begin(), p.frame_times.end())
                          : m_synced_frame_time;
        auto n_frames = p.end_time / frame_time;
        GAPI_Assert(n_frames > 0u);
        return (std::size_t)n_frames;
    }
};

struct TimestampSyncTest : public ::testing::TestWithParam<sync_policy> {
    void run(cv::GProtoInputArgs&& ins, cv::GProtoOutputArgs&& outs,
             const ts_vec& frame_times) {
        auto video_in_n = frame_times.size();
        GAPI_Assert(video_in_n <= ins.m_args.size());
        // Assume that all remaining inputs are const
        auto const_in_n = ins.m_args.size() - video_in_n;
        auto out_n = outs.m_args.size();
        auto policy = GetParam();
        TimestampGenerationParams ts_params(frame_times, policy);
        MultiFrameSource source(ts_params);

        GRunArgs gins;
        for (std::size_t i = 0; i < video_in_n; i++) {
            gins += cv::gin(source.getSource(i));
        }
        auto desc = source.desc();
        cv::Mat const_mat = cv::Mat::eye(desc.size.height,
                                         desc.size.width,
                                         CV_MAKE_TYPE(desc.depth, desc.chan));
        for (std::size_t i = 0; i < const_in_n; i++) {
            gins += cv::gin(const_mat);
        }
        ts_vec out_timestamps(out_n);
        cv::GRunArgsP gouts{};
        for (auto& t : out_timestamps) {
            gouts += cv::gout(t);
        }

        auto pipe = cv::GComputation(std::move(ins), std::move(outs))
                    .compileStreaming(cv::compile_args(policy));

        pipe.setSource(std::move(gins));
        pipe.start();

        std::size_t frames = 0u;
        TimestampChecker checker(ts_params);
        while(pipe.pull(std::move(gouts))) {
            checker.checkNext(out_timestamps);
            frames++;
        }

        EXPECT_EQ(checker.nFrames(), frames);
    }
};

} // anonymous namespace

TEST_P(TimestampSyncTest, Basic)
{
    cv::GMat in1, in2;
    auto out = cv::gapi::add(in1, in2);
    auto ts = cv::gapi::streaming::timestamp(out);

    run(cv::GIn(in1, in2), cv::GOut(ts), {1,2});
}

TEST_P(TimestampSyncTest, ThreeInputs)
{
    cv::GMat in1, in2, in3;
    auto tmp = cv::gapi::add(in1, in2);
    auto out = cv::gapi::add(tmp, in3);
    auto ts = cv::gapi::streaming::timestamp(out);

    run(cv::GIn(in1, in2, in3), cv::GOut(ts), {2,4,3});
}

TEST_P(TimestampSyncTest, TwoOutputs)
{
    cv::GMat in1, in2, in3;
    auto out1 = cv::gapi::add(in1, in3);
    auto out2 = cv::gapi::add(in2, in3);
    auto ts1 = cv::gapi::streaming::timestamp(out1);
    auto ts2 = cv::gapi::streaming::timestamp(out2);

    run(cv::GIn(in1, in2, in3), cv::GOut(ts1, ts2), {1,4,2});
}

TEST_P(TimestampSyncTest, ConstInput)
{
    cv::GMat in1, in2, in3;
    auto out1 = cv::gapi::add(in1, in3);
    auto out2 = cv::gapi::add(in2, in3);
    auto ts1 = cv::gapi::streaming::timestamp(out1);
    auto ts2 = cv::gapi::streaming::timestamp(out2);

    run(cv::GIn(in1, in2, in3), cv::GOut(ts1, ts2), {1,2});
}

TEST_P(TimestampSyncTest, ChangeSource)
{
    cv::GMat in1, in2, in3;
    auto out1 = cv::gapi::add(in1, in3);
    auto out2 = cv::gapi::add(in2, in3);
    auto ts1 = cv::gapi::streaming::timestamp(out1);
    auto ts2 = cv::gapi::streaming::timestamp(out2);

    run(cv::GIn(in1, in2, in3), cv::GOut(ts1, ts2), {1,2});
    run(cv::GIn(in1, in2, in3), cv::GOut(ts1, ts2), {1,2});
}

INSTANTIATE_TEST_CASE_P(InputSynchronization, TimestampSyncTest,
                        Values(sync_policy::dont_sync,
                               sync_policy::drop));
} // namespace opencv_test