Debian Codegraph @

tests/videowriter_test.cc

raw symbols SCIP
1 /*
2 This file is part of the CVD Library.
3
4 Copyright (C) 2022 The Authors
5
6 This library is free software, see LICENSE file for details
7 */
8 #include <cvd/videowriter.h>
9
10 #include "test_utility.h"
11
12 #include <cvd/image.h>
13 #include <cvd/rgba.h>
14 #include <cvd/videoreader.h>
15
16 #include <algorithm>
17 #include <cstdint>
18 #include <cstdlib>
19 #include <string>
20
21 namespace
22 {
23 using CVD::Image;
24 using CVD::ImageRef;
25 using CVD::Rgba;
26 using CVD::VideoReader;
27 using CVD::VideoWriter;
28 using CVD::Testing::assert_equal;
29 using CVD::Testing::assert_near;
30 using std::uint8_t;
31 }
32
33 int main(int argc, char* argv[])
34 {
35 if(argc < 2)
36 {
37 std::cerr << "usage: " << argv[0] << " videowriter.mp4\n";
38 exit(EXIT_FAILURE);
39 }
40 {
41 VideoWriter writer(argv[1], "mpeg4", "yuv420p", { 1, 10240 }, { 10, 1 }, 0, 4);
42
43 Image<Rgba<uint8_t>> image(ImageRef { 128, 128 });
44 for(int j = 0; j < image.size().y; ++j)
45 {
46 for(int i = 0; i < image.size().x; ++i)
47 {
48 if(i >= 64)
49 {
50 if(j >= 64)
51 image[j][i] = Rgba<uint8_t>(0, 0, 255, 255);
52 else
53 image[j][i] = Rgba<uint8_t>(255, 0, 0, 255);
54 }
55 else
56 {
57 if(j >= 64)
58 image[j][i] = Rgba<uint8_t>(0, 255, 0, 255);
59 else
60 image[j][i] = Rgba<uint8_t>(0, 0, 0, 255);
61 }
62 }
63 }
64 for(int i = 0; i < 10; ++i)
65 {
66 writer.put_frame(image, i * 1024);
67 }
68 for(int j = 0; j < image.size().y; ++j)
69 {
70 for(int i = 0; i < image.size().x; ++i)
71 {
72 if(i >= 64)
73 {
74 if(j >= 64)
75 image[j][i] = Rgba<uint8_t>(255, 255, 0, 255);
76 else
77 image[j][i] = Rgba<uint8_t>(0, 255, 255, 255);
78 }
79 else
80 {
81 if(j >= 64)
82 image[j][i] = Rgba<uint8_t>(255, 0, 255, 255);
83 else
84 image[j][i] = Rgba<uint8_t>(0, 0, 0, 255);
85 }
86 }
87 }
88 for(int i = 10; i < 20; ++i)
89 {
90 writer.put_frame(image, i * 1024);
91 }
92 }
93
94 VideoReader reader(argv[1], 4);
95 assert_equal(1, reader.timebase().num, "Incorrect timebase numerator");
96 assert_equal(10240, reader.timebase().den, "Incorrect timebase denominator");
97 for(int i = 0; i < 20; ++i)
98 {
99 auto [frame, timestamp] = reader.get_frame();
100
101 int64_t expected_timestamp = i * 1024;
102 assert_equal(expected_timestamp, timestamp, "Incorrect timestamp for frame " + std::to_string(i));
103
104 if(i < 10)
105 {
106 assert_near(Rgba<uint8_t>(0, 0, 0, 255), frame[32][32], "Incorrect top left square on frame " + std::to_string(i));
107 assert_near(Rgba<uint8_t>(255, 0, 0, 255), frame[32][96], "Incorrect top right square on frame " + std::to_string(i));
108 assert_near(Rgba<uint8_t>(0, 255, 0, 255), frame[96][32], "Incorrect bottom left square on frame " + std::to_string(i));
109 assert_near(Rgba<uint8_t>(0, 0, 255, 255), frame[96][96], "Incorrect bottom right square on frame " + std::to_string(i));
110 }
111 else
112 {
113 assert_near(Rgba<uint8_t>(0, 0, 0, 255), frame[32][32], "Incorrect top left square on frame " + std::to_string(i));
114 assert_near(Rgba<uint8_t>(0, 255, 255, 255), frame[32][96], "Incorrect top right square on frame " + std::to_string(i));
115 assert_near(Rgba<uint8_t>(255, 0, 255, 255), frame[96][32], "Incorrect bottom left square on frame " + std::to_string(i));
116 assert_near(Rgba<uint8_t>(255, 255, 0, 255), frame[96][96], "Incorrect bottom right square on frame " + std::to_string(i));
117 }
118 }
119
120 auto [frame, timestamp] = reader.get_frame();
121 if(frame.size().x != 0)
122 {
123 std::cerr << "Expected end of stream, received frame\n";
124 exit(EXIT_FAILURE);
125 }
126 }