blob: 76f195d3264261c921064cedca19674c172255e4 [file] [log] [blame]
andresp@webrtc.orgab654952013-09-19 12:14:031/*
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
Mirko Bonadei92ea95e2017-09-15 04:47:3110#ifndef TEST_FRAME_GENERATOR_H_
11#define TEST_FRAME_GENERATOR_H_
andresp@webrtc.orgab654952013-09-19 12:14:0312
perkja8ba1952017-02-27 14:52:1013#include <memory>
sprang@webrtc.org131bea82015-02-18 12:46:0614#include <string>
15#include <vector>
16
Artem Titov33f9d2b2019-12-05 14:59:0017#include "api/scoped_refptr.h"
Artem Titov503d7232019-12-04 11:37:1318#include "api/test/frame_generator_interface.h"
Artem Titov33f9d2b2019-12-05 14:59:0019#include "api/video/i420_buffer.h"
Artem Titov9b731592022-10-07 13:01:3220#include "api/video/nv12_buffer.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3121#include "api/video/video_frame.h"
Artem Titov33f9d2b2019-12-05 14:59:0022#include "api/video/video_frame_buffer.h"
Niels Möller0327c2d2018-05-21 12:09:3123#include "api/video/video_source_interface.h"
Per Kjellanderf5770a02022-01-12 12:43:3124#include "rtc_base/logging.h"
Artem Titov33f9d2b2019-12-05 14:59:0025#include "rtc_base/random.h"
Markus Handella5a4be12020-07-08 14:09:2126#include "rtc_base/synchronization/mutex.h"
Artem Titov33f9d2b2019-12-05 14:59:0027#include "system_wrappers/include/clock.h"
andresp@webrtc.orgab654952013-09-19 12:14:0328
29namespace webrtc {
30namespace test {
31
Artem Titov33f9d2b2019-12-05 14:59:0032// SquareGenerator is a FrameGenerator that draws a given amount of randomly
33// sized and colored squares. Between each new generated frame, the squares
34// are moved slightly towards the lower right corner.
35class SquareGenerator : public FrameGeneratorInterface {
perkja49cbd32016-09-16 14:53:4136 public:
Artem Titov33f9d2b2019-12-05 14:59:0037 SquareGenerator(int width, int height, OutputType type, int num_squares);
perkja49cbd32016-09-16 14:53:4138
Artem Titov33f9d2b2019-12-05 14:59:0039 void ChangeResolution(size_t width, size_t height) override;
40 VideoFrameData NextFrame() override;
Mirko Bonadeif1e39222023-02-17 12:49:5841 Resolution GetResolution() const override;
Artem Titov33f9d2b2019-12-05 14:59:0042
Mirko Bonadeib5f2c7e2023-02-27 10:13:1343 absl::optional<int> fps() const override { return absl::nullopt; }
44
Artem Titov33f9d2b2019-12-05 14:59:0045 private:
46 rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height);
47
48 class Square {
49 public:
50 Square(int width, int height, int seed);
51
52 void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer);
53
54 private:
55 Random random_generator_;
56 int x_;
57 int y_;
58 const int length_;
59 const uint8_t yuv_y_;
60 const uint8_t yuv_u_;
61 const uint8_t yuv_v_;
62 const uint8_t yuv_a_;
63 };
perkja49cbd32016-09-16 14:53:4164
Mirko Bonadeif1e39222023-02-17 12:49:5865 mutable Mutex mutex_;
Artem Titov33f9d2b2019-12-05 14:59:0066 const OutputType type_;
Markus Handella5a4be12020-07-08 14:09:2167 int width_ RTC_GUARDED_BY(&mutex_);
68 int height_ RTC_GUARDED_BY(&mutex_);
69 std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&mutex_);
perkja49cbd32016-09-16 14:53:4170};
71
Artem Titov33f9d2b2019-12-05 14:59:0072class YuvFileGenerator : public FrameGeneratorInterface {
andresp@webrtc.orgab654952013-09-19 12:14:0373 public:
Artem Titov33f9d2b2019-12-05 14:59:0074 YuvFileGenerator(std::vector<FILE*> files,
75 size_t width,
76 size_t height,
77 int frame_repeat_count);
andresp@webrtc.orgab654952013-09-19 12:14:0378
Artem Titov33f9d2b2019-12-05 14:59:0079 ~YuvFileGenerator();
Emircan Uysaler207a75d2018-03-12 21:12:1480
Artem Titov33f9d2b2019-12-05 14:59:0081 VideoFrameData NextFrame() override;
82 void ChangeResolution(size_t width, size_t height) override {
Artem Titov9b731592022-10-07 13:01:3283 RTC_LOG(LS_WARNING) << "YuvFileGenerator::ChangeResolution not implemented";
Artem Titov33f9d2b2019-12-05 14:59:0084 }
Mirko Bonadeif1e39222023-02-17 12:49:5885 Resolution GetResolution() const override;
sprang@webrtc.org131bea82015-02-18 12:46:0686
Mirko Bonadeib5f2c7e2023-02-27 10:13:1387 absl::optional<int> fps() const override { return absl::nullopt; }
88
Artem Titov33f9d2b2019-12-05 14:59:0089 private:
90 // Returns true if the new frame was loaded.
91 // False only in case of a single file with a single frame in it.
92 bool ReadNextFrame();
sprangd6358952015-07-29 14:58:1393
Artem Titov33f9d2b2019-12-05 14:59:0094 size_t file_index_;
95 size_t frame_index_;
96 const std::vector<FILE*> files_;
97 const size_t width_;
98 const size_t height_;
99 const size_t frame_size_;
100 const std::unique_ptr<uint8_t[]> frame_buffer_;
101 const int frame_display_count_;
102 int current_display_count_;
103 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
andresp@webrtc.orgab654952013-09-19 12:14:03104};
Artem Titov33f9d2b2019-12-05 14:59:00105
Artem Titov9b731592022-10-07 13:01:32106class NV12FileGenerator : public FrameGeneratorInterface {
107 public:
108 NV12FileGenerator(std::vector<FILE*> files,
109 size_t width,
110 size_t height,
111 int frame_repeat_count);
112
113 ~NV12FileGenerator();
114
115 VideoFrameData NextFrame() override;
116 void ChangeResolution(size_t width, size_t height) override {
117 RTC_LOG(LS_WARNING)
118 << "NV12FileGenerator::ChangeResolution not implemented";
119 }
Mirko Bonadeif1e39222023-02-17 12:49:58120 Resolution GetResolution() const override;
Artem Titov9b731592022-10-07 13:01:32121
Mirko Bonadeib5f2c7e2023-02-27 10:13:13122 absl::optional<int> fps() const override { return absl::nullopt; }
123
Artem Titov9b731592022-10-07 13:01:32124 private:
125 // Returns true if the new frame was loaded.
126 // False only in case of a single file with a single frame in it.
127 bool ReadNextFrame();
128
129 size_t file_index_;
130 size_t frame_index_;
131 const std::vector<FILE*> files_;
132 const size_t width_;
133 const size_t height_;
134 const size_t frame_size_;
135 const std::unique_ptr<uint8_t[]> frame_buffer_;
136 const int frame_display_count_;
137 int current_display_count_;
138 rtc::scoped_refptr<NV12Buffer> last_read_buffer_;
139};
140
Artem Titov33f9d2b2019-12-05 14:59:00141// SlideGenerator works similarly to YuvFileGenerator but it fills the frames
142// with randomly sized and colored squares instead of reading their content
143// from files.
144class SlideGenerator : public FrameGeneratorInterface {
145 public:
146 SlideGenerator(int width, int height, int frame_repeat_count);
147
148 VideoFrameData NextFrame() override;
149 void ChangeResolution(size_t width, size_t height) override {
Per Kjellanderf5770a02022-01-12 12:43:31150 RTC_LOG(LS_WARNING) << "SlideGenerator::ChangeResolution not implemented";
Artem Titov33f9d2b2019-12-05 14:59:00151 }
Mirko Bonadeif1e39222023-02-17 12:49:58152 Resolution GetResolution() const override;
Artem Titov33f9d2b2019-12-05 14:59:00153
Mirko Bonadeib5f2c7e2023-02-27 10:13:13154 absl::optional<int> fps() const override { return absl::nullopt; }
155
Artem Titov33f9d2b2019-12-05 14:59:00156 private:
157 // Generates some randomly sized and colored squares scattered
158 // over the frame.
159 void GenerateNewFrame();
160
161 const int width_;
162 const int height_;
163 const int frame_display_count_;
164 int current_display_count_;
165 Random random_generator_;
166 rtc::scoped_refptr<I420Buffer> buffer_;
167};
168
169class ScrollingImageFrameGenerator : public FrameGeneratorInterface {
170 public:
171 ScrollingImageFrameGenerator(Clock* clock,
172 const std::vector<FILE*>& files,
173 size_t source_width,
174 size_t source_height,
175 size_t target_width,
176 size_t target_height,
177 int64_t scroll_time_ms,
178 int64_t pause_time_ms);
179 ~ScrollingImageFrameGenerator() override = default;
180
181 VideoFrameData NextFrame() override;
182 void ChangeResolution(size_t width, size_t height) override {
Per Kjellanderf5770a02022-01-12 12:43:31183 RTC_LOG(LS_WARNING)
184 << "ScrollingImageFrameGenerator::ChangeResolution not implemented";
Artem Titov33f9d2b2019-12-05 14:59:00185 }
Mirko Bonadeif1e39222023-02-17 12:49:58186 Resolution GetResolution() const override;
Artem Titov33f9d2b2019-12-05 14:59:00187
Mirko Bonadeib5f2c7e2023-02-27 10:13:13188 absl::optional<int> fps() const override { return absl::nullopt; }
189
Artem Titov33f9d2b2019-12-05 14:59:00190 private:
191 void UpdateSourceFrame(size_t frame_num);
192 void CropSourceToScrolledImage(double scroll_factor);
193
194 Clock* const clock_;
195 const int64_t start_time_;
196 const int64_t scroll_time_;
197 const int64_t pause_time_;
198 const size_t num_frames_;
199 const int target_width_;
200 const int target_height_;
201
202 size_t current_frame_num_;
203 bool prev_frame_not_scrolled_;
204 VideoFrameData current_source_frame_;
205 VideoFrameData current_frame_;
206 YuvFileGenerator file_generator_;
207};
208
andresp@webrtc.orgab654952013-09-19 12:14:03209} // namespace test
210} // namespace webrtc
211
Mirko Bonadei92ea95e2017-09-15 04:47:31212#endif // TEST_FRAME_GENERATOR_H_