-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathFormatCustomIO_test.cpp
More file actions
336 lines (277 loc) · 11 KB
/
Copy pathFormatCustomIO_test.cpp
File metadata and controls
336 lines (277 loc) · 11 KB
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
#include <catch2/catch_test_macros.hpp>
#include "catch2/catch_message.hpp"
#include "avcpp/format.h"
#include "avcpp/formatcontext.h"
#if AVCPP_HAS_AVFORMAT && (AVCPP_CXX_STANDARD >= 20) && defined(__cpp_lib_format)
#include <format>
static constexpr std::size_t ImageW = 640;
static constexpr std::size_t ImageH = 480;
static constexpr std::size_t ImageCount = 11;
static constexpr av::PixelFormat ImagePixFmt = AV_PIX_FMT_GRAY8;
static const std::size_t FrameSizeBytes = ImageW * ImageH * ImagePixFmt.bitsPerPixel() / 8;
struct TestBufferIo : public av::CustomIO
{
public:
std::vector<uint8_t> _buffer{};
std::vector<uint8_t>::iterator _pos{};
TestBufferIo()
{
_buffer.resize(FrameSizeBytes * ImageCount);
_pos = _buffer.begin();
}
std::size_t remain() const noexcept
{
return std::ranges::distance(_pos, _buffer.end());
}
bool eof() const noexcept
{
return _pos == _buffer.end();
}
void fillPattern(uint8_t patternOffset)
{
std::span out = _buffer;
for (auto i = 0u; i < ImageCount; ++i) {
auto buf = out.subspan(i * FrameSizeBytes, FrameSizeBytes);
std::ranges::fill(buf, uint8_t(i + patternOffset));
}
}
void fill(uint8_t val = 0xFF)
{
std::ranges::fill(_buffer, val);
}
//
// Iface
//
int write(const uint8_t *data, size_t size) final
{
UNSCOPED_INFO("IO::write: size=" << size);
// ENOSPC
// EWOULDBLOCK
// EMSGSIZE
// Do not write
if (size > remain()) {
UNSCOPED_INFO("IO::write too big: " << size);
return AVERROR(EMSGSIZE);
}
std::ranges::copy_n(data, size, _pos);
std::advance(_pos, size);
return 0;
}
int read(uint8_t *data, size_t size) final
{
UNSCOPED_INFO("IO::read: size=" << size);
if (eof()) {
INFO("IO::read: EOF reached");
return AVERROR_EOF;
}
auto readed = std::min(size, remain());
std::ranges::copy_n(_pos, readed, data);
std::advance(_pos, readed);
return readed;
}
int64_t seek(int64_t offset, int whence) final
{
UNSCOPED_INFO("IO::seek: offset=" << offset << ", whence=0x" << std::hex << whence << std::dec);
if (whence & AVSEEK_SIZE) {
return _buffer.size();
}
long cur = -1;
if (whence == SEEK_CUR) {
cur = std::distance(_buffer.begin(), _pos);
} else if (whence == SEEK_SET) {
cur = 0;
} else if (whence == SEEK_END) {
cur = _buffer.size() - 1;
} else {
AVERROR(EINVAL);
}
assert(cur >= 0 && cur <= _buffer.size() - 1);
auto next = cur + offset;
if (next >= _buffer.size() || next < 0)
return AVERROR(EINVAL);
std::advance(_pos, offset);
return next;
}
int seekable() const final
{
return AVIO_SEEKABLE_NORMAL;
}
const char *name() const final
{
return "TestBufferIo";
}
};
TEST_CASE("Format Custom IO checks", "[FormatCustomIo]")
{
SECTION("CustomIo :: Basic")
{
TestBufferIo customIoIn;
TestBufferIo customIoOut;
static auto const VideoSize = std::format("{}x{}", ImageW, ImageH);
static auto const FrameSizeBytes = ImageW * ImageH * ImagePixFmt.bitsPerPixel() / 8;
static auto const PatternOffset = 100u;
// Fill In buffer with pattern
customIoIn.fillPattern(PatternOffset);
// Fill Out buffer with FF
customIoOut.fill();
{
av::FormatContext ictx;
ictx.openInput(&customIoIn,
av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
},
av::InputFormat("rawvideo"));
ictx.findStreamInfo();
std::size_t count = 0;
while (auto pkt = ictx.readPacket()) {
INFO("Pkt counter: " << count << ", pattern value " << (count + PatternOffset));
REQUIRE(std::ranges::find_if_not(pkt.span(), [&](auto val) { return val == count + PatternOffset; }) == pkt.span().end());
++count;
}
REQUIRE(ictx.streamsCount() == 1);
REQUIRE(ImageCount == count);
}
{
av::FormatContext octx;
octx.setFormat(av::OutputFormat("rawvideo"));
octx.addStream();
octx.openOutput(&customIoOut);
octx.writeHeader(av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
});
std::vector<uint8_t> packetData(FrameSizeBytes);
for (auto i = 0u; i < ImageCount; ++i) {
std::ranges::fill(packetData, uint8_t(i + PatternOffset));
av::Packet pkt{packetData, av::Packet::wrap_data_static{}};
pkt.setPts(av::Timestamp{std::chrono::seconds(i)});
pkt.setStreamIndex(0);
octx.writePacket(pkt);
}
octx.flush();
// Check Output data
std::span in = customIoOut._buffer;
for (auto i = 0u; i < ImageCount; ++i) {
auto buf = in.subspan(i * FrameSizeBytes, FrameSizeBytes);
INFO("Pkt counter: " << i << ", pattern value " << (i + PatternOffset));
REQUIRE(std::ranges::find_if_not(buf, [&](auto val) { return val == i + PatternOffset; }) == buf.end());
}
}
}
SECTION("Output Open")
{
TestBufferIo customIo;
static auto const VideoSize = std::format("{}x{}", ImageW, ImageH);
static auto const FrameSizeBytes = ImageW * ImageH * ImagePixFmt.bitsPerPixel() / 8;
static auto const PatternOffset = 100u;
{
av::FormatContext ctx;
CHECK_THROWS(ctx.openOutput(&customIo,
av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
}));
REQUIRE(ctx.isOpened() == false);
}
{
av::FormatContext ctx;
ctx.setFormat(av::OutputFormat{"rawvideo"});
// Yep, format pointed, but there is no any streams for muxing and initOuput called.
REQUIRE_THROWS(ctx.openOutput(&customIo,
av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
}));
REQUIRE(ctx.isOpened() == false);
// set format again
ctx.setFormat(av::OutputFormat{"rawvideo"});
// add stream for muxing, it must be valid now
REQUIRE_NOTHROW(ctx.addStream());
REQUIRE_NOTHROW(ctx.openOutput(&customIo,
av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
}));
}
{
av::FormatContext ctx;
// no streams, should throws
CHECK_THROWS(ctx.openOutput(&customIo,
av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
},
av::OutputFormat{"rawvideo"}));
REQUIRE(ctx.isOpened() == false);
ctx.addStream();
CHECK_NOTHROW(ctx.openOutput(&customIo,
av::Dictionary {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()},
},
av::OutputFormat{"rawvideo"}));
REQUIRE(ctx.isOpened() == true);
}
// non-move stor for options
{
av::FormatContext ctx;
av::Dictionary options {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()}
};
// no streams, should throws
CHECK_THROWS(ctx.openOutput(&customIo,
options,
av::OutputFormat{"rawvideo"}));
REQUIRE(ctx.isOpened() == false);
// Options should kept:
REQUIRE(options.count() == 2);
ctx.addStream();
CHECK_NOTHROW(ctx.openOutput(&customIo,
options,
av::OutputFormat{"rawvideo"}));
REQUIRE(ctx.isOpened() == true);
// Options should be empty
// REQUIRE(options.count() == 0);
}
// Check write
{
av::FormatContext ctx;
av::Dictionary options {
{"pixel_format", ImagePixFmt.name()},
{"video_size", VideoSize.c_str()}
};
// Options should kept:
REQUIRE(options.count() == 2);
ctx.addStream();
bool openOutputFlag{};
CHECK_NOTHROW(openOutputFlag = ctx.openOutput(&customIo,
options,
av::OutputFormat{"rawvideo"}));
REQUIRE(ctx.isOpened() == true);
// for the rawvideo openOutputFlag should be false
REQUIRE(openOutputFlag == false);
ctx.writeHeader();
// fill with FF
customIo.fill();
std::vector<uint8_t> packetData(FrameSizeBytes);
for (auto i = 0u; i < ImageCount; ++i) {
std::ranges::fill(packetData, uint8_t(i + PatternOffset));
av::Packet pkt{packetData, av::Packet::wrap_data_static{}};
pkt.setPts(av::Timestamp{std::chrono::seconds(i)});
pkt.setStreamIndex(0);
ctx.writePacket(pkt);
}
ctx.flush();
// Check Output data
std::span in = customIo._buffer;
for (auto i = 0u; i < ImageCount; ++i) {
auto buf = in.subspan(i * FrameSizeBytes, FrameSizeBytes);
INFO("Pkt counter: " << i << ", pattern value " << (i + PatternOffset));
REQUIRE(std::ranges::find_if_not(buf, [&](auto val) { return val == i + PatternOffset; }) == buf.end());
}
}
}
}
#endif