blob: 92e95f68a0d254db8a5474e77772a70579ef4bfb [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"
Evan Shrubsole55c17862020-09-28 10:16:00 +020019#include "api/video/nv12_buffer.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/video/video_rotation.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020021#include "common_video/include/video_frame_buffer.h"
22#include "common_video/libyuv/include/webrtc_libyuv.h"
23#include "rtc_base/checks.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "test/frame_utils.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000025
26namespace webrtc {
27namespace test {
Artem Titov33f9d2b2019-12-05 15:59:00 +010028
29SquareGenerator::SquareGenerator(int width,
30 int height,
31 OutputType type,
32 int num_squares)
33 : type_(type) {
34 ChangeResolution(width, height);
35 for (int i = 0; i < num_squares; ++i) {
36 squares_.emplace_back(new Square(width, height, i + 1));
37 }
38}
39
40void SquareGenerator::ChangeResolution(size_t width, size_t height) {
Markus Handella5a4be12020-07-08 16:09:21 +020041 MutexLock lock(&mutex_);
Artem Titov33f9d2b2019-12-05 15:59:00 +010042 width_ = static_cast<int>(width);
43 height_ = static_cast<int>(height);
44 RTC_CHECK(width_ > 0);
45 RTC_CHECK(height_ > 0);
46}
47
48rtc::scoped_refptr<I420Buffer> SquareGenerator::CreateI420Buffer(int width,
49 int height) {
50 rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height));
51 memset(buffer->MutableDataY(), 127, height * buffer->StrideY());
52 memset(buffer->MutableDataU(), 127,
53 buffer->ChromaHeight() * buffer->StrideU());
54 memset(buffer->MutableDataV(), 127,
55 buffer->ChromaHeight() * buffer->StrideV());
56 return buffer;
57}
58
59FrameGeneratorInterface::VideoFrameData SquareGenerator::NextFrame() {
Markus Handella5a4be12020-07-08 16:09:21 +020060 MutexLock lock(&mutex_);
Artem Titov33f9d2b2019-12-05 15:59:00 +010061
62 rtc::scoped_refptr<VideoFrameBuffer> buffer = nullptr;
63 switch (type_) {
64 case OutputType::kI420:
Evan Shrubsole55c17862020-09-28 10:16:00 +020065 case OutputType::kI010:
66 case OutputType::kNV12: {
Artem Titov33f9d2b2019-12-05 15:59:00 +010067 buffer = CreateI420Buffer(width_, height_);
68 break;
69 }
70 case OutputType::kI420A: {
71 rtc::scoped_refptr<I420Buffer> yuv_buffer =
72 CreateI420Buffer(width_, height_);
73 rtc::scoped_refptr<I420Buffer> axx_buffer =
74 CreateI420Buffer(width_, height_);
Niels Möllera68bfc52021-01-11 13:26:35 +010075 buffer = WrapI420ABuffer(yuv_buffer->width(), yuv_buffer->height(),
76 yuv_buffer->DataY(), yuv_buffer->StrideY(),
77 yuv_buffer->DataU(), yuv_buffer->StrideU(),
78 yuv_buffer->DataV(), yuv_buffer->StrideV(),
79 axx_buffer->DataY(), axx_buffer->StrideY(),
80 // To keep references alive.
81 [yuv_buffer, axx_buffer] {});
Artem Titov33f9d2b2019-12-05 15:59:00 +010082 break;
83 }
84 default:
Artem Titovd3251962021-11-15 16:57:07 +010085 RTC_DCHECK_NOTREACHED() << "The given output format is not supported.";
Artem Titov33f9d2b2019-12-05 15:59:00 +010086 }
87
88 for (const auto& square : squares_)
89 square->Draw(buffer);
90
91 if (type_ == OutputType::kI010) {
92 buffer = I010Buffer::Copy(*buffer->ToI420());
Evan Shrubsole55c17862020-09-28 10:16:00 +020093 } else if (type_ == OutputType::kNV12) {
94 buffer = NV12Buffer::Copy(*buffer->ToI420());
Artem Titov33f9d2b2019-12-05 15:59:00 +010095 }
96
97 return VideoFrameData(buffer, absl::nullopt);
98}
99
100SquareGenerator::Square::Square(int width, int height, int seed)
101 : random_generator_(seed),
102 x_(random_generator_.Rand(0, width)),
103 y_(random_generator_.Rand(0, height)),
104 length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)),
105 yuv_y_(random_generator_.Rand(0, 255)),
106 yuv_u_(random_generator_.Rand(0, 255)),
107 yuv_v_(random_generator_.Rand(0, 255)),
108 yuv_a_(random_generator_.Rand(0, 255)) {}
109
110void SquareGenerator::Square::Draw(
111 const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) {
112 RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 ||
113 frame_buffer->type() == VideoFrameBuffer::Type::kI420A);
114 rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420();
Sebastian Janssond35a6862020-03-09 19:18:14 +0100115 int length_cap = std::min(buffer->height(), buffer->width()) / 4;
116 int length = std::min(length_, length_cap);
117 x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length);
118 y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length);
119 for (int y = y_; y < y_ + length; ++y) {
Artem Titov33f9d2b2019-12-05 15:59:00 +0100120 uint8_t* pos_y =
121 (const_cast<uint8_t*>(buffer->DataY()) + x_ + y * buffer->StrideY());
Sebastian Janssond35a6862020-03-09 19:18:14 +0100122 memset(pos_y, yuv_y_, length);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100123 }
124
Sebastian Janssond35a6862020-03-09 19:18:14 +0100125 for (int y = y_; y < y_ + length; y = y + 2) {
Artem Titov33f9d2b2019-12-05 15:59:00 +0100126 uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 +
127 y / 2 * buffer->StrideU());
Sebastian Janssond35a6862020-03-09 19:18:14 +0100128 memset(pos_u, yuv_u_, length / 2);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100129 uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 +
130 y / 2 * buffer->StrideV());
Sebastian Janssond35a6862020-03-09 19:18:14 +0100131 memset(pos_v, yuv_v_, length / 2);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100132 }
133
134 if (frame_buffer->type() == VideoFrameBuffer::Type::kI420)
135 return;
136
137 // Optionally draw on alpha plane if given.
138 const webrtc::I420ABufferInterface* yuva_buffer = frame_buffer->GetI420A();
Sebastian Janssond35a6862020-03-09 19:18:14 +0100139 for (int y = y_; y < y_ + length; ++y) {
Artem Titov33f9d2b2019-12-05 15:59:00 +0100140 uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ +
141 y * yuva_buffer->StrideA());
Sebastian Janssond35a6862020-03-09 19:18:14 +0100142 memset(pos_y, yuv_a_, length);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100143 }
144}
145
146YuvFileGenerator::YuvFileGenerator(std::vector<FILE*> files,
147 size_t width,
148 size_t height,
149 int frame_repeat_count)
150 : file_index_(0),
151 frame_index_(std::numeric_limits<size_t>::max()),
152 files_(files),
153 width_(width),
154 height_(height),
155 frame_size_(CalcBufferSize(VideoType::kI420,
156 static_cast<int>(width_),
157 static_cast<int>(height_))),
158 frame_buffer_(new uint8_t[frame_size_]),
159 frame_display_count_(frame_repeat_count),
160 current_display_count_(0) {
161 RTC_DCHECK_GT(width, 0);
162 RTC_DCHECK_GT(height, 0);
163 RTC_DCHECK_GT(frame_repeat_count, 0);
164}
165
166YuvFileGenerator::~YuvFileGenerator() {
167 for (FILE* file : files_)
168 fclose(file);
169}
170
171FrameGeneratorInterface::VideoFrameData YuvFileGenerator::NextFrame() {
172 // Empty update by default.
173 VideoFrame::UpdateRect update_rect{0, 0, 0, 0};
174 if (current_display_count_ == 0) {
175 const bool got_new_frame = ReadNextFrame();
176 // Full update on a new frame from file.
177 if (got_new_frame) {
178 update_rect = VideoFrame::UpdateRect{0, 0, static_cast<int>(width_),
179 static_cast<int>(height_)};
perkja8ba1952017-02-27 06:52:10 -0800180 }
perkjfa10b552016-10-02 23:45:26 -0700181 }
Artem Titov33f9d2b2019-12-05 15:59:00 +0100182 if (++current_display_count_ >= frame_display_count_)
183 current_display_count_ = 0;
perkjfa10b552016-10-02 23:45:26 -0700184
Artem Titov33f9d2b2019-12-05 15:59:00 +0100185 return VideoFrameData(last_read_buffer_, update_rect);
186}
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000187
Artem Titov33f9d2b2019-12-05 15:59:00 +0100188bool YuvFileGenerator::ReadNextFrame() {
189 size_t prev_frame_index = frame_index_;
190 size_t prev_file_index = file_index_;
191 last_read_buffer_ = test::ReadI420Buffer(
192 static_cast<int>(width_), static_cast<int>(height_), files_[file_index_]);
193 ++frame_index_;
194 if (!last_read_buffer_) {
195 // No more frames to read in this file, rewind and move to next file.
196 rewind(files_[file_index_]);
Emircan Uysaler03e6ec92018-03-09 15:03:26 -0800197
Artem Titov33f9d2b2019-12-05 15:59:00 +0100198 frame_index_ = 0;
199 file_index_ = (file_index_ + 1) % files_.size();
nisse115bd152016-09-30 04:14:07 -0700200 last_read_buffer_ =
201 test::ReadI420Buffer(static_cast<int>(width_),
Jonas Olssona4d87372019-07-05 19:08:33 +0200202 static_cast<int>(height_), files_[file_index_]);
Artem Titov33f9d2b2019-12-05 15:59:00 +0100203 RTC_CHECK(last_read_buffer_);
204 }
205 return frame_index_ != prev_frame_index || file_index_ != prev_file_index;
206}
Ilya Nikolaevskiyd0f3d842019-03-04 15:10:44 +0100207
Artem Titov33f9d2b2019-12-05 15:59:00 +0100208SlideGenerator::SlideGenerator(int width, int height, int frame_repeat_count)
209 : width_(width),
210 height_(height),
211 frame_display_count_(frame_repeat_count),
212 current_display_count_(0),
213 random_generator_(1234) {
214 RTC_DCHECK_GT(width, 0);
215 RTC_DCHECK_GT(height, 0);
216 RTC_DCHECK_GT(frame_repeat_count, 0);
217}
218
219FrameGeneratorInterface::VideoFrameData SlideGenerator::NextFrame() {
220 if (current_display_count_ == 0)
221 GenerateNewFrame();
222 if (++current_display_count_ >= frame_display_count_)
223 current_display_count_ = 0;
224
225 return VideoFrameData(buffer_, absl::nullopt);
226}
227
228void SlideGenerator::GenerateNewFrame() {
229 // The squares should have a varying order of magnitude in order
230 // to simulate variation in the slides' complexity.
231 const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2));
232
233 buffer_ = I420Buffer::Create(width_, height_);
234 memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY());
235 memset(buffer_->MutableDataU(), 127,
236 buffer_->ChromaHeight() * buffer_->StrideU());
237 memset(buffer_->MutableDataV(), 127,
238 buffer_->ChromaHeight() * buffer_->StrideV());
239
240 for (int i = 0; i < kSquareNum; ++i) {
241 int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1);
242 // Limit the length of later squares so that they don't overwrite the
243 // previous ones too much.
244 length = (length * (kSquareNum - i)) / kSquareNum;
245
246 int x = random_generator_.Rand(0, width_ - length);
247 int y = random_generator_.Rand(0, height_ - length);
248 uint8_t yuv_y = random_generator_.Rand(0, 255);
249 uint8_t yuv_u = random_generator_.Rand(0, 255);
250 uint8_t yuv_v = random_generator_.Rand(0, 255);
251
252 for (int yy = y; yy < y + length; ++yy) {
253 uint8_t* pos_y = (buffer_->MutableDataY() + x + yy * buffer_->StrideY());
254 memset(pos_y, yuv_y, length);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000255 }
Artem Titov33f9d2b2019-12-05 15:59:00 +0100256 for (int yy = y; yy < y + length; yy += 2) {
257 uint8_t* pos_u =
258 (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU());
259 memset(pos_u, yuv_u, length / 2);
260 uint8_t* pos_v =
261 (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV());
262 memset(pos_v, yuv_v, length / 2);
erikvarga579de6f2017-08-29 09:12:57 -0700263 }
264 }
perkja49cbd32016-09-16 07:53:41 -0700265}
266
Artem Titov33f9d2b2019-12-05 15:59:00 +0100267ScrollingImageFrameGenerator::ScrollingImageFrameGenerator(
sprangd6358952015-07-29 07:58:13 -0700268 Clock* clock,
Artem Titov33f9d2b2019-12-05 15:59:00 +0100269 const std::vector<FILE*>& files,
sprangd6358952015-07-29 07:58:13 -0700270 size_t source_width,
271 size_t source_height,
272 size_t target_width,
273 size_t target_height,
274 int64_t scroll_time_ms,
Artem Titov33f9d2b2019-12-05 15:59:00 +0100275 int64_t pause_time_ms)
276 : clock_(clock),
277 start_time_(clock->TimeInMilliseconds()),
278 scroll_time_(scroll_time_ms),
279 pause_time_(pause_time_ms),
280 num_frames_(files.size()),
281 target_width_(static_cast<int>(target_width)),
282 target_height_(static_cast<int>(target_height)),
283 current_frame_num_(num_frames_ - 1),
284 prev_frame_not_scrolled_(false),
285 current_source_frame_(nullptr, absl::nullopt),
286 current_frame_(nullptr, absl::nullopt),
287 file_generator_(files, source_width, source_height, 1) {
288 RTC_DCHECK(clock_ != nullptr);
289 RTC_DCHECK_GT(num_frames_, 0);
290 RTC_DCHECK_GE(source_height, target_height);
291 RTC_DCHECK_GE(source_width, target_width);
292 RTC_DCHECK_GE(scroll_time_ms, 0);
293 RTC_DCHECK_GE(pause_time_ms, 0);
294 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
295}
sprangd6358952015-07-29 07:58:13 -0700296
Artem Titov33f9d2b2019-12-05 15:59:00 +0100297FrameGeneratorInterface::VideoFrameData
298ScrollingImageFrameGenerator::NextFrame() {
299 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
300 const int64_t now = clock_->TimeInMilliseconds();
301 int64_t ms_since_start = now - start_time_;
302
303 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
304 UpdateSourceFrame(frame_num);
305
306 bool cur_frame_not_scrolled;
307
308 double scroll_factor;
309 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
310 if (time_into_frame < scroll_time_) {
311 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
312 cur_frame_not_scrolled = false;
313 } else {
314 scroll_factor = 1.0;
315 cur_frame_not_scrolled = true;
316 }
317 CropSourceToScrolledImage(scroll_factor);
318
319 bool same_scroll_position =
320 prev_frame_not_scrolled_ && cur_frame_not_scrolled;
321 if (!same_scroll_position) {
322 // If scrolling is not finished yet, force full frame update.
323 current_frame_.update_rect =
324 VideoFrame::UpdateRect{0, 0, target_width_, target_height_};
325 }
326 prev_frame_not_scrolled_ = cur_frame_not_scrolled;
327
328 return current_frame_;
329}
330
331void ScrollingImageFrameGenerator::UpdateSourceFrame(size_t frame_num) {
332 VideoFrame::UpdateRect acc_update{0, 0, 0, 0};
333 while (current_frame_num_ != frame_num) {
334 current_source_frame_ = file_generator_.NextFrame();
335 if (current_source_frame_.update_rect) {
336 acc_update.Union(*current_source_frame_.update_rect);
337 }
338 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
339 }
340 current_source_frame_.update_rect = acc_update;
341}
342
343void ScrollingImageFrameGenerator::CropSourceToScrolledImage(
344 double scroll_factor) {
345 int scroll_margin_x = current_source_frame_.buffer->width() - target_width_;
346 int pixels_scrolled_x =
347 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
348 int scroll_margin_y = current_source_frame_.buffer->height() - target_height_;
349 int pixels_scrolled_y =
350 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
351
352 rtc::scoped_refptr<I420BufferInterface> i420_buffer =
353 current_source_frame_.buffer->ToI420();
354 int offset_y =
355 (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x;
356 int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) +
357 (pixels_scrolled_x / 2);
358 int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) +
359 (pixels_scrolled_x / 2);
360
361 VideoFrame::UpdateRect update_rect =
362 current_source_frame_.update_rect->IsEmpty()
363 ? VideoFrame::UpdateRect{0, 0, 0, 0}
364 : VideoFrame::UpdateRect{0, 0, target_width_, target_height_};
365 current_frame_ = VideoFrameData(
366 WrapI420Buffer(target_width_, target_height_,
367 &i420_buffer->DataY()[offset_y], i420_buffer->StrideY(),
368 &i420_buffer->DataU()[offset_u], i420_buffer->StrideU(),
369 &i420_buffer->DataV()[offset_v], i420_buffer->StrideV(),
Niels Möllerf4e3e2b2021-02-02 11:37:39 +0100370 // To keep reference alive.
371 [i420_buffer] {}),
Artem Titov33f9d2b2019-12-05 15:59:00 +0100372 update_rect);
sprangd6358952015-07-29 07:58:13 -0700373}
374
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000375} // namespace test
376} // namespace webrtc