blob: e3b4a0659630d601e74a328a3af6a3b4a50457de [file] [log] [blame]
andresp@webrtc.orgab654952013-09-19 12:14:03 +00001/*
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 06:47:31 +020010#include "test/frame_generator.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000011
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000012#include <string.h>
Jonas Olssona4d87372019-07-05 19:08:33 +020013
Yves Gerey3e707812018-11-28 16:47:49 +010014#include <cstdint>
15#include <cstdio>
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
17
Emircan Uysaler0823eec2018-07-13 17:10:00 -070018#include "api/video/i010_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010019#include "api/video/video_rotation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020020#include "common_video/include/video_frame_buffer.h"
21#include "common_video/libyuv/include/webrtc_libyuv.h"
Yves Gerey3e707812018-11-28 16:47:49 +010022#include "rtc_base/bind.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020023#include "rtc_base/checks.h"
24#include "rtc_base/keep_ref_until_done.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020025#include "test/frame_utils.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000026
27namespace webrtc {
28namespace test {
29namespace {
30
Emircan Uysaler03e6ec92018-03-09 15:03:26 -080031// Helper method for keeping a reference to passed pointers.
32void KeepBufferRefs(rtc::scoped_refptr<webrtc::VideoFrameBuffer>,
33 rtc::scoped_refptr<webrtc::VideoFrameBuffer>) {}
34
Artem Titov33f9d2b2019-12-05 15:59:00 +010035} // namespace
36
37SquareGenerator::SquareGenerator(int width,
38 int height,
39 OutputType type,
40 int num_squares)
41 : type_(type) {
42 ChangeResolution(width, height);
43 for (int i = 0; i < num_squares; ++i) {
44 squares_.emplace_back(new Square(width, height, i + 1));
45 }
46}
47
48void SquareGenerator::ChangeResolution(size_t width, size_t height) {
49 rtc::CritScope lock(&crit_);
50 width_ = static_cast<int>(width);
51 height_ = static_cast<int>(height);
52 RTC_CHECK(width_ > 0);
53 RTC_CHECK(height_ > 0);
54}
55
56rtc::scoped_refptr<I420Buffer> SquareGenerator::CreateI420Buffer(int width,
57 int height) {
58 rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height));
59 memset(buffer->MutableDataY(), 127, height * buffer->StrideY());
60 memset(buffer->MutableDataU(), 127,
61 buffer->ChromaHeight() * buffer->StrideU());
62 memset(buffer->MutableDataV(), 127,
63 buffer->ChromaHeight() * buffer->StrideV());
64 return buffer;
65}
66
67FrameGeneratorInterface::VideoFrameData SquareGenerator::NextFrame() {
68 rtc::CritScope lock(&crit_);
69
70 rtc::scoped_refptr<VideoFrameBuffer> buffer = nullptr;
71 switch (type_) {
72 case OutputType::kI420:
73 case OutputType::kI010: {
74 buffer = CreateI420Buffer(width_, height_);
75 break;
76 }
77 case OutputType::kI420A: {
78 rtc::scoped_refptr<I420Buffer> yuv_buffer =
79 CreateI420Buffer(width_, height_);
80 rtc::scoped_refptr<I420Buffer> axx_buffer =
81 CreateI420Buffer(width_, height_);
82 buffer = WrapI420ABuffer(
83 yuv_buffer->width(), yuv_buffer->height(), yuv_buffer->DataY(),
84 yuv_buffer->StrideY(), yuv_buffer->DataU(), yuv_buffer->StrideU(),
85 yuv_buffer->DataV(), yuv_buffer->StrideV(), axx_buffer->DataY(),
86 axx_buffer->StrideY(),
87 rtc::Bind(&KeepBufferRefs, yuv_buffer, axx_buffer));
88 break;
89 }
90 default:
91 RTC_NOTREACHED() << "The given output format is not supported.";
92 }
93
94 for (const auto& square : squares_)
95 square->Draw(buffer);
96
97 if (type_ == OutputType::kI010) {
98 buffer = I010Buffer::Copy(*buffer->ToI420());
99 }
100
101 return VideoFrameData(buffer, absl::nullopt);
102}
103
104SquareGenerator::Square::Square(int width, int height, int seed)
105 : random_generator_(seed),
106 x_(random_generator_.Rand(0, width)),
107 y_(random_generator_.Rand(0, height)),
108 length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)),
109 yuv_y_(random_generator_.Rand(0, 255)),
110 yuv_u_(random_generator_.Rand(0, 255)),
111 yuv_v_(random_generator_.Rand(0, 255)),
112 yuv_a_(random_generator_.Rand(0, 255)) {}
113
114void SquareGenerator::Square::Draw(
115 const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) {
116 RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 ||
117 frame_buffer->type() == VideoFrameBuffer::Type::kI420A);
118 rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420();
119 x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_);
120 y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_);
121 for (int y = y_; y < y_ + length_; ++y) {
122 uint8_t* pos_y =
123 (const_cast<uint8_t*>(buffer->DataY()) + x_ + y * buffer->StrideY());
124 memset(pos_y, yuv_y_, length_);
125 }
126
127 for (int y = y_; y < y_ + length_; y = y + 2) {
128 uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 +
129 y / 2 * buffer->StrideU());
130 memset(pos_u, yuv_u_, length_ / 2);
131 uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 +
132 y / 2 * buffer->StrideV());
133 memset(pos_v, yuv_v_, length_ / 2);
134 }
135
136 if (frame_buffer->type() == VideoFrameBuffer::Type::kI420)
137 return;
138
139 // Optionally draw on alpha plane if given.
140 const webrtc::I420ABufferInterface* yuva_buffer = frame_buffer->GetI420A();
141 for (int y = y_; y < y_ + length_; ++y) {
142 uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ +
143 y * yuva_buffer->StrideA());
144 memset(pos_y, yuv_a_, length_);
145 }
146}
147
148YuvFileGenerator::YuvFileGenerator(std::vector<FILE*> files,
149 size_t width,
150 size_t height,
151 int frame_repeat_count)
152 : file_index_(0),
153 frame_index_(std::numeric_limits<size_t>::max()),
154 files_(files),
155 width_(width),
156 height_(height),
157 frame_size_(CalcBufferSize(VideoType::kI420,
158 static_cast<int>(width_),
159 static_cast<int>(height_))),
160 frame_buffer_(new uint8_t[frame_size_]),
161 frame_display_count_(frame_repeat_count),
162 current_display_count_(0) {
163 RTC_DCHECK_GT(width, 0);
164 RTC_DCHECK_GT(height, 0);
165 RTC_DCHECK_GT(frame_repeat_count, 0);
166}
167
168YuvFileGenerator::~YuvFileGenerator() {
169 for (FILE* file : files_)
170 fclose(file);
171}
172
173FrameGeneratorInterface::VideoFrameData YuvFileGenerator::NextFrame() {
174 // Empty update by default.
175 VideoFrame::UpdateRect update_rect{0, 0, 0, 0};
176 if (current_display_count_ == 0) {
177 const bool got_new_frame = ReadNextFrame();
178 // Full update on a new frame from file.
179 if (got_new_frame) {
180 update_rect = VideoFrame::UpdateRect{0, 0, static_cast<int>(width_),
181 static_cast<int>(height_)};
perkja8ba1952017-02-27 06:52:10 -0800182 }
perkjfa10b552016-10-02 23:45:26 -0700183 }
Artem Titov33f9d2b2019-12-05 15:59:00 +0100184 if (++current_display_count_ >= frame_display_count_)
185 current_display_count_ = 0;
perkjfa10b552016-10-02 23:45:26 -0700186
Artem Titov33f9d2b2019-12-05 15:59:00 +0100187 return VideoFrameData(last_read_buffer_, update_rect);
188}
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000189
Artem Titov33f9d2b2019-12-05 15:59:00 +0100190bool YuvFileGenerator::ReadNextFrame() {
191 size_t prev_frame_index = frame_index_;
192 size_t prev_file_index = file_index_;
193 last_read_buffer_ = test::ReadI420Buffer(
194 static_cast<int>(width_), static_cast<int>(height_), files_[file_index_]);
195 ++frame_index_;
196 if (!last_read_buffer_) {
197 // No more frames to read in this file, rewind and move to next file.
198 rewind(files_[file_index_]);
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800199
Artem Titov33f9d2b2019-12-05 15:59:00 +0100200 frame_index_ = 0;
201 file_index_ = (file_index_ + 1) % files_.size();
nisse115bd152016-09-30 04:14:07 -0700202 last_read_buffer_ =
203 test::ReadI420Buffer(static_cast<int>(width_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200204 static_cast<int>(height_), files_[file_index_]);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100205 RTC_CHECK(last_read_buffer_);
206 }
207 return frame_index_ != prev_frame_index || file_index_ != prev_file_index;
208}
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100209
Artem Titov33f9d2b2019-12-05 15:59:00 +0100210SlideGenerator::SlideGenerator(int width, int height, int frame_repeat_count)
211 : width_(width),
212 height_(height),
213 frame_display_count_(frame_repeat_count),
214 current_display_count_(0),
215 random_generator_(1234) {
216 RTC_DCHECK_GT(width, 0);
217 RTC_DCHECK_GT(height, 0);
218 RTC_DCHECK_GT(frame_repeat_count, 0);
219}
220
221FrameGeneratorInterface::VideoFrameData SlideGenerator::NextFrame() {
222 if (current_display_count_ == 0)
223 GenerateNewFrame();
224 if (++current_display_count_ >= frame_display_count_)
225 current_display_count_ = 0;
226
227 return VideoFrameData(buffer_, absl::nullopt);
228}
229
230void SlideGenerator::GenerateNewFrame() {
231 // The squares should have a varying order of magnitude in order
232 // to simulate variation in the slides' complexity.
233 const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2));
234
235 buffer_ = I420Buffer::Create(width_, height_);
236 memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY());
237 memset(buffer_->MutableDataU(), 127,
238 buffer_->ChromaHeight() * buffer_->StrideU());
239 memset(buffer_->MutableDataV(), 127,
240 buffer_->ChromaHeight() * buffer_->StrideV());
241
242 for (int i = 0; i < kSquareNum; ++i) {
243 int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1);
244 // Limit the length of later squares so that they don't overwrite the
245 // previous ones too much.
246 length = (length * (kSquareNum - i)) / kSquareNum;
247
248 int x = random_generator_.Rand(0, width_ - length);
249 int y = random_generator_.Rand(0, height_ - length);
250 uint8_t yuv_y = random_generator_.Rand(0, 255);
251 uint8_t yuv_u = random_generator_.Rand(0, 255);
252 uint8_t yuv_v = random_generator_.Rand(0, 255);
253
254 for (int yy = y; yy < y + length; ++yy) {
255 uint8_t* pos_y = (buffer_->MutableDataY() + x + yy * buffer_->StrideY());
256 memset(pos_y, yuv_y, length);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000257 }
Artem Titov33f9d2b2019-12-05 15:59:00 +0100258 for (int yy = y; yy < y + length; yy += 2) {
259 uint8_t* pos_u =
260 (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU());
261 memset(pos_u, yuv_u, length / 2);
262 uint8_t* pos_v =
263 (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV());
264 memset(pos_v, yuv_v, length / 2);
erikvarga579de6f2017-08-29 09:12:57 -0700265 }
266 }
perkja49cbd32016-09-16 07:53:41 -0700267}
268
Artem Titov33f9d2b2019-12-05 15:59:00 +0100269ScrollingImageFrameGenerator::ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700270 Clock* clock,
Artem Titov33f9d2b2019-12-05 15:59:00 +0100271 const std::vector<FILE*>& files,
sprangd6358952015-07-29 07:58:13 -0700272 size_t source_width,
273 size_t source_height,
274 size_t target_width,
275 size_t target_height,
276 int64_t scroll_time_ms,
Artem Titov33f9d2b2019-12-05 15:59:00 +0100277 int64_t pause_time_ms)
278 : clock_(clock),
279 start_time_(clock->TimeInMilliseconds()),
280 scroll_time_(scroll_time_ms),
281 pause_time_(pause_time_ms),
282 num_frames_(files.size()),
283 target_width_(static_cast<int>(target_width)),
284 target_height_(static_cast<int>(target_height)),
285 current_frame_num_(num_frames_ - 1),
286 prev_frame_not_scrolled_(false),
287 current_source_frame_(nullptr, absl::nullopt),
288 current_frame_(nullptr, absl::nullopt),
289 file_generator_(files, source_width, source_height, 1) {
290 RTC_DCHECK(clock_ != nullptr);
291 RTC_DCHECK_GT(num_frames_, 0);
292 RTC_DCHECK_GE(source_height, target_height);
293 RTC_DCHECK_GE(source_width, target_width);
294 RTC_DCHECK_GE(scroll_time_ms, 0);
295 RTC_DCHECK_GE(pause_time_ms, 0);
296 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
297}
sprangd6358952015-07-29 07:58:13 -0700298
Artem Titov33f9d2b2019-12-05 15:59:00 +0100299FrameGeneratorInterface::VideoFrameData
300ScrollingImageFrameGenerator::NextFrame() {
301 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
302 const int64_t now = clock_->TimeInMilliseconds();
303 int64_t ms_since_start = now - start_time_;
304
305 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
306 UpdateSourceFrame(frame_num);
307
308 bool cur_frame_not_scrolled;
309
310 double scroll_factor;
311 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
312 if (time_into_frame < scroll_time_) {
313 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
314 cur_frame_not_scrolled = false;
315 } else {
316 scroll_factor = 1.0;
317 cur_frame_not_scrolled = true;
318 }
319 CropSourceToScrolledImage(scroll_factor);
320
321 bool same_scroll_position =
322 prev_frame_not_scrolled_ && cur_frame_not_scrolled;
323 if (!same_scroll_position) {
324 // If scrolling is not finished yet, force full frame update.
325 current_frame_.update_rect =
326 VideoFrame::UpdateRect{0, 0, target_width_, target_height_};
327 }
328 prev_frame_not_scrolled_ = cur_frame_not_scrolled;
329
330 return current_frame_;
331}
332
333void ScrollingImageFrameGenerator::UpdateSourceFrame(size_t frame_num) {
334 VideoFrame::UpdateRect acc_update{0, 0, 0, 0};
335 while (current_frame_num_ != frame_num) {
336 current_source_frame_ = file_generator_.NextFrame();
337 if (current_source_frame_.update_rect) {
338 acc_update.Union(*current_source_frame_.update_rect);
339 }
340 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
341 }
342 current_source_frame_.update_rect = acc_update;
343}
344
345void ScrollingImageFrameGenerator::CropSourceToScrolledImage(
346 double scroll_factor) {
347 int scroll_margin_x = current_source_frame_.buffer->width() - target_width_;
348 int pixels_scrolled_x =
349 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
350 int scroll_margin_y = current_source_frame_.buffer->height() - target_height_;
351 int pixels_scrolled_y =
352 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
353
354 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
355 current_source_frame_.buffer->ToI420();
356 int offset_y =
357 (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x;
358 int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) +
359 (pixels_scrolled_x / 2);
360 int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) +
361 (pixels_scrolled_x / 2);
362
363 VideoFrame::UpdateRect update_rect =
364 current_source_frame_.update_rect->IsEmpty()
365 ? VideoFrame::UpdateRect{0, 0, 0, 0}
366 : VideoFrame::UpdateRect{0, 0, target_width_, target_height_};
367 current_frame_ = VideoFrameData(
368 WrapI420Buffer(target_width_, target_height_,
369 &i420_buffer->DataY()[offset_y], i420_buffer->StrideY(),
370 &i420_buffer->DataU()[offset_u], i420_buffer->StrideU(),
371 &i420_buffer->DataV()[offset_v], i420_buffer->StrideV(),
372 KeepRefUntilDone(i420_buffer)),
373 update_rect);
sprangd6358952015-07-29 07:58:13 -0700374}
375
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000376} // namespace test
377} // namespace webrtc