xref: /aosp_15_r20/external/v4l2_codec2/tests/c2_e2e_test/jni/common.h (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
1 // Copyright 2018 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef C2_E2E_TEST_COMMON_H_
6 #define C2_E2E_TEST_COMMON_H_
7 
8 #include <fstream>
9 #include <ios>
10 #include <memory>
11 #include <string>
12 #include <vector>
13 
14 namespace android {
15 
16 // The enumeration of video codec profile. This would be better to align with
17 // VideoCodecProfile enum in Chromium so we could use the identical test stream
18 // data arguments for both ARC end-to-end and Chromium tests.
19 enum VideoCodecProfile {
20     VIDEO_CODEC_PROFILE_UNKNOWN = -1,
21     VIDEO_CODEC_PROFILE_MIN = VIDEO_CODEC_PROFILE_UNKNOWN,
22     H264PROFILE_MIN = 0,
23     H264PROFILE_BASELINE = H264PROFILE_MIN,
24     H264PROFILE_MAIN = 1,
25     H264PROFILE_EXTENDED = 2,
26     H264PROFILE_HIGH = 3,
27     H264PROFILE_HIGH10PROFILE = 4,
28     H264PROFILE_HIGH422PROFILE = 5,
29     H264PROFILE_HIGH444PREDICTIVEPROFILE = 6,
30     H264PROFILE_SCALABLEBASELINE = 7,
31     H264PROFILE_SCALABLEHIGH = 8,
32     H264PROFILE_STEREOHIGH = 9,
33     H264PROFILE_MULTIVIEWHIGH = 10,
34     H264PROFILE_MAX = H264PROFILE_MULTIVIEWHIGH,
35     VP8PROFILE_MIN = 11,
36     VP8PROFILE_ANY = VP8PROFILE_MIN,
37     VP8PROFILE_MAX = VP8PROFILE_ANY,
38     VP9PROFILE_MIN = 12,
39     VP9PROFILE_PROFILE0 = VP9PROFILE_MIN,
40     VP9PROFILE_PROFILE1 = 13,
41     VP9PROFILE_PROFILE2 = 14,
42     VP9PROFILE_PROFILE3 = 15,
43     VP9PROFILE_MAX = VP9PROFILE_PROFILE3,
44     HEVCPROFILE_MIN = 16,
45     HEVCPROFILE_MAIN = HEVCPROFILE_MIN,
46     HEVCPROFILE_MAIN10 = 17,
47     HEVCPROFILE_MAIN_STILL_PICTURE = 18,
48     HEVCPROFILE_MAX = HEVCPROFILE_MAIN_STILL_PICTURE,
49 };
50 
51 // The enum class of video codec type.
52 enum class VideoCodecType {
53     UNKNOWN,
54     H264,
55     VP8,
56     VP9,
57     HEVC,
58 };
59 
60 // Structure to store resolution.
61 struct Size {
SizeSize62     Size() : width(0), height(0) {}
SizeSize63     Size(int w, int h) : width(w), height(h) {}
IsEmptySize64     bool IsEmpty() const { return width <= 0 || height <= 0; }
65 
66     int width;
67     int height;
68 };
69 
70 class InputFile {
71 public:
72     explicit InputFile(std::string file_path);
73     InputFile(std::string file_path, std::ios_base::openmode openmode);
74 
75     // Check if the file is valid.
76     bool IsValid() const;
77     // Get the size of the file.
78     size_t GetLength();
79     // Set position to the beginning of the file.
80     virtual void Rewind();
81 
82 protected:
83     std::ifstream file_;
84 };
85 
86 // Wrapper of std::ifstream for reading binary file.
87 class CachedInputFileStream : public InputFile {
88 public:
89     explicit CachedInputFileStream(std::string file_path);
90 
91     // Read the given number of bytes to the buffer. Return the number of bytes
92     // read or -1 on error.
93     size_t Read(char* buffer, size_t size);
94 
95     void Rewind() override;
96 
97 private:
98     std::vector<char> data_;
99     size_t position_ = 0;
100 };
101 
102 // Wrapper of std::ifstream for reading ASCII file.
103 class InputFileASCII : public InputFile {
104 public:
105     explicit InputFileASCII(std::string file_path);
106 
107     // Read one line from the file. Return false if EOF.
108     bool ReadLine(std::string* line);
109 };
110 
111 // IVF file writer, can be used to write an encoded VP8/9 video to disk.
112 class IVFWriter {
113 public:
114     IVFWriter(std::ofstream* output_file, VideoCodecType codec);
115 
116     // Write the IVF file header.
117     bool WriteHeader(const Size& resolution, uint32_t frame_rate, uint32_t num_frames);
118     // Append the specified frame data to the IVF file.
119     bool WriteFrame(const uint8_t* data, uint32_t data_size, uint64_t timestamp);
120     // Set the number of video frames in the IVF file header.
121     bool SetNumFrames(uint32_t num_frames);
122 
123 private:
124     std::ofstream* output_file_;
125     VideoCodecType codec_ = VideoCodecType::UNKNOWN;
126 };
127 
128 class OutputFile {
129 public:
130     bool Open(const std::string& file_path, VideoCodecType codec);
131     void Close();
132     bool IsOpen();
133 
134     // Write the video file header.
135     bool WriteHeader(const Size& resolution, uint32_t frame_rate, uint32_t num_frames);
136     // Append the specified frame data to the video file.
137     bool WriteFrame(uint32_t data_size, const uint8_t* data);
138 
139 private:
140     std::ofstream output_file_;
141     std::unique_ptr<IVFWriter> ivf_writer_;
142     uint64_t frame_index_ = 0;
143 };
144 
145 struct PerformanceTimeStats {
PerformanceTimeStatsPerformanceTimeStats146     PerformanceTimeStats() {}
147     explicit PerformanceTimeStats(const std::vector<double>& times);
148     double avg_us_ = 0.0;
149     double percentile_25_us_ = 0.0;
150     double percentile_50_us_ = 0.0;
151     double percentile_75_us_ = 0.0;
152 };
153 
154 // The helper class to calculate FPS.
155 class FPSCalculator {
156 public:
157     // Record the time interval of output buffers. Return false if is invalid.
158     // This should be called per output buffer ready callback.
159     bool RecordFrameTimeDiff();
160 
161     // Calucalate FPS value.
162     double CalculateFPS() const;
163 
164     // Calucalate delivery time stats.
165     PerformanceTimeStats CalucalateDeliveryTimeStats() const;
166 
167 private:
168     static constexpr double kMovingAvgWindowUs = 1000000;
169     static constexpr double kRegardedPercentile = 95;
170 
171     // Return the statistics for the moving average over a window over the
172     // cumulative sum. Basically, moves a window from: [0, window] to
173     // [sum - window, sum] over the cumulative sum, over ((sum - window)/average)
174     // steps, and returns the average value over each window.
175     // This method is used to average time-diff data over a window of a constant
176     // time.
177     std::vector<double> MovingAvgOverSum() const;
178 
179     std::vector<double> frame_time_diffs_us_;
180     int64_t last_frame_time_us_ = 0;
181 };
182 
183 // Helper function to get VideoCodecType from |profile|.
184 VideoCodecType VideoCodecProfileToType(VideoCodecProfile profile);
185 
186 // Split the string |src| by the delimiter |delim|.
187 std::vector<std::string> SplitString(const std::string& src, char delim);
188 
189 // Get monotonic timestamp for now in microseconds.
190 int64_t GetNowUs();
191 
192 // Get Mime type name from video codec type.
193 const char* GetMimeType(VideoCodecType type);
194 
195 }  // namespace android
196 #endif  // C2_E2E_TEST_COMMON_H_
197