andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 1 | /* |
| 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 Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 10 | #include "test/frame_generator.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 11 | |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 12 | #include <string.h> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 13 | #include <cstdint> |
| 14 | #include <cstdio> |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 15 | #include <memory> |
| 16 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 17 | #include "absl/memory/memory.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 18 | #include "api/scoped_refptr.h" |
Emircan Uysaler | 0823eec | 2018-07-13 17:10:00 -0700 | [diff] [blame] | 19 | #include "api/video/i010_buffer.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 20 | #include "api/video/i420_buffer.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 21 | #include "api/video/video_frame_buffer.h" |
| 22 | #include "api/video/video_rotation.h" |
| 23 | #include "common_types.h" // NOLINT(build/include) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 24 | #include "common_video/include/video_frame_buffer.h" |
| 25 | #include "common_video/libyuv/include/webrtc_libyuv.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "rtc_base/bind.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 27 | #include "rtc_base/checks.h" |
| 28 | #include "rtc_base/keep_ref_until_done.h" |
| 29 | #include "rtc_base/random.h" |
| 30 | #include "system_wrappers/include/clock.h" |
| 31 | #include "test/frame_utils.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 32 | |
| 33 | namespace webrtc { |
| 34 | namespace test { |
| 35 | namespace { |
| 36 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 37 | // Helper method for keeping a reference to passed pointers. |
| 38 | void KeepBufferRefs(rtc::scoped_refptr<webrtc::VideoFrameBuffer>, |
| 39 | rtc::scoped_refptr<webrtc::VideoFrameBuffer>) {} |
| 40 | |
erikvarga@webrtc.org | c774d5d | 2017-10-10 14:34:38 +0200 | [diff] [blame] | 41 | // SquareGenerator is a FrameGenerator that draws a given amount of randomly |
| 42 | // sized and colored squares. Between each new generated frame, the squares |
| 43 | // are moved slightly towards the lower right corner. |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 44 | class SquareGenerator : public FrameGenerator { |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 45 | public: |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 46 | SquareGenerator(int width, int height, OutputType type, int num_squares) |
| 47 | : type_(type) { |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 48 | ChangeResolution(width, height); |
erikvarga@webrtc.org | c774d5d | 2017-10-10 14:34:38 +0200 | [diff] [blame] | 49 | for (int i = 0; i < num_squares; ++i) { |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 50 | squares_.emplace_back(new Square(width, height, i + 1)); |
| 51 | } |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 52 | } |
| 53 | |
| 54 | void ChangeResolution(size_t width, size_t height) override { |
| 55 | rtc::CritScope lock(&crit_); |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 56 | width_ = static_cast<int>(width); |
| 57 | height_ = static_cast<int>(height); |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 58 | RTC_CHECK(width_ > 0); |
| 59 | RTC_CHECK(height_ > 0); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 60 | } |
| 61 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 62 | rtc::scoped_refptr<I420Buffer> CreateI420Buffer(int width, int height) { |
| 63 | rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create(width, height)); |
| 64 | memset(buffer->MutableDataY(), 127, height * buffer->StrideY()); |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 65 | memset(buffer->MutableDataU(), 127, |
| 66 | buffer->ChromaHeight() * buffer->StrideU()); |
| 67 | memset(buffer->MutableDataV(), 127, |
| 68 | buffer->ChromaHeight() * buffer->StrideV()); |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 69 | return buffer; |
| 70 | } |
| 71 | |
| 72 | VideoFrame* NextFrame() override { |
| 73 | rtc::CritScope lock(&crit_); |
| 74 | |
| 75 | rtc::scoped_refptr<VideoFrameBuffer> buffer = nullptr; |
| 76 | switch (type_) { |
Emircan Uysaler | 0823eec | 2018-07-13 17:10:00 -0700 | [diff] [blame] | 77 | case OutputType::I420: |
| 78 | case OutputType::I010: { |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 79 | buffer = CreateI420Buffer(width_, height_); |
| 80 | break; |
| 81 | } |
| 82 | case OutputType::I420A: { |
| 83 | rtc::scoped_refptr<I420Buffer> yuv_buffer = |
| 84 | CreateI420Buffer(width_, height_); |
| 85 | rtc::scoped_refptr<I420Buffer> axx_buffer = |
| 86 | CreateI420Buffer(width_, height_); |
| 87 | buffer = WrapI420ABuffer( |
| 88 | yuv_buffer->width(), yuv_buffer->height(), yuv_buffer->DataY(), |
| 89 | yuv_buffer->StrideY(), yuv_buffer->DataU(), yuv_buffer->StrideU(), |
| 90 | yuv_buffer->DataV(), yuv_buffer->StrideV(), axx_buffer->DataY(), |
| 91 | axx_buffer->StrideY(), |
| 92 | rtc::Bind(&KeepBufferRefs, yuv_buffer, axx_buffer)); |
| 93 | break; |
| 94 | } |
| 95 | } |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 96 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 97 | for (const auto& square : squares_) |
| 98 | square->Draw(buffer); |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 99 | |
Emircan Uysaler | 0823eec | 2018-07-13 17:10:00 -0700 | [diff] [blame] | 100 | if (type_ == OutputType::I010) { |
| 101 | buffer = I010Buffer::Copy(*buffer->ToI420()); |
| 102 | } |
| 103 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 104 | frame_ = absl::make_unique<VideoFrame>( |
| 105 | VideoFrame::Builder() |
| 106 | .set_video_frame_buffer(buffer) |
| 107 | .set_rotation(webrtc::kVideoRotation_0) |
| 108 | .set_timestamp_us(0) |
| 109 | .build()); |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 110 | return frame_.get(); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | private: |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 114 | class Square { |
| 115 | public: |
| 116 | Square(int width, int height, int seed) |
| 117 | : random_generator_(seed), |
| 118 | x_(random_generator_.Rand(0, width)), |
| 119 | y_(random_generator_.Rand(0, height)), |
| 120 | length_(random_generator_.Rand(1, width > 4 ? width / 4 : 1)), |
| 121 | yuv_y_(random_generator_.Rand(0, 255)), |
| 122 | yuv_u_(random_generator_.Rand(0, 255)), |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 123 | yuv_v_(random_generator_.Rand(0, 255)), |
| 124 | yuv_a_(random_generator_.Rand(0, 255)) {} |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 125 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 126 | void Draw(const rtc::scoped_refptr<VideoFrameBuffer>& frame_buffer) { |
| 127 | RTC_DCHECK(frame_buffer->type() == VideoFrameBuffer::Type::kI420 || |
| 128 | frame_buffer->type() == VideoFrameBuffer::Type::kI420A); |
| 129 | rtc::scoped_refptr<I420BufferInterface> buffer = frame_buffer->ToI420(); |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 130 | x_ = (x_ + random_generator_.Rand(0, 4)) % (buffer->width() - length_); |
| 131 | y_ = (y_ + random_generator_.Rand(0, 4)) % (buffer->height() - length_); |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 132 | for (int y = y_; y < y_ + length_; ++y) { |
| 133 | uint8_t* pos_y = (const_cast<uint8_t*>(buffer->DataY()) + x_ + |
| 134 | y * buffer->StrideY()); |
| 135 | memset(pos_y, yuv_y_, length_); |
| 136 | } |
perkj | c8b0865 | 2017-03-22 04:57:53 -0700 | [diff] [blame] | 137 | |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 138 | for (int y = y_; y < y_ + length_; y = y + 2) { |
| 139 | uint8_t* pos_u = (const_cast<uint8_t*>(buffer->DataU()) + x_ / 2 + |
| 140 | y / 2 * buffer->StrideU()); |
| 141 | memset(pos_u, yuv_u_, length_ / 2); |
| 142 | uint8_t* pos_v = (const_cast<uint8_t*>(buffer->DataV()) + x_ / 2 + |
| 143 | y / 2 * buffer->StrideV()); |
| 144 | memset(pos_v, yuv_v_, length_ / 2); |
| 145 | } |
| 146 | |
| 147 | if (frame_buffer->type() == VideoFrameBuffer::Type::kI420) |
| 148 | return; |
| 149 | |
| 150 | // Optionally draw on alpha plane if given. |
| 151 | const webrtc::I420ABufferInterface* yuva_buffer = |
| 152 | frame_buffer->GetI420A(); |
| 153 | for (int y = y_; y < y_ + length_; ++y) { |
| 154 | uint8_t* pos_y = (const_cast<uint8_t*>(yuva_buffer->DataA()) + x_ + |
| 155 | y * yuva_buffer->StrideA()); |
| 156 | memset(pos_y, yuv_a_, length_); |
| 157 | } |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | private: |
| 161 | Random random_generator_; |
| 162 | int x_; |
| 163 | int y_; |
| 164 | const int length_; |
| 165 | const uint8_t yuv_y_; |
| 166 | const uint8_t yuv_u_; |
| 167 | const uint8_t yuv_v_; |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 168 | const uint8_t yuv_a_; |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 169 | }; |
| 170 | |
perkj | fa10b55 | 2016-10-02 23:45:26 -0700 | [diff] [blame] | 171 | rtc::CriticalSection crit_; |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 172 | const OutputType type_; |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 173 | int width_ RTC_GUARDED_BY(&crit_); |
| 174 | int height_ RTC_GUARDED_BY(&crit_); |
| 175 | std::vector<std::unique_ptr<Square>> squares_ RTC_GUARDED_BY(&crit_); |
| 176 | std::unique_ptr<VideoFrame> frame_ RTC_GUARDED_BY(&crit_); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 179 | class YuvFileGenerator : public FrameGenerator { |
| 180 | public: |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 181 | YuvFileGenerator(std::vector<FILE*> files, |
| 182 | size_t width, |
| 183 | size_t height, |
| 184 | int frame_repeat_count) |
| 185 | : file_index_(0), |
| 186 | files_(files), |
| 187 | width_(width), |
| 188 | height_(height), |
nisse | eb44b39 | 2017-04-28 07:18:05 -0700 | [diff] [blame] | 189 | frame_size_(CalcBufferSize(VideoType::kI420, |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 190 | static_cast<int>(width_), |
| 191 | static_cast<int>(height_))), |
| 192 | frame_buffer_(new uint8_t[frame_size_]), |
| 193 | frame_display_count_(frame_repeat_count), |
| 194 | current_display_count_(0) { |
kwiberg | b890c95c | 2016-11-29 05:30:40 -0800 | [diff] [blame] | 195 | RTC_DCHECK_GT(width, 0); |
| 196 | RTC_DCHECK_GT(height, 0); |
| 197 | RTC_DCHECK_GT(frame_repeat_count, 0); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 200 | ~YuvFileGenerator() override { |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 201 | for (FILE* file : files_) |
| 202 | fclose(file); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 205 | VideoFrame* NextFrame() override { |
sprang@webrtc.org | 25dd1db | 2015-03-02 11:55:45 +0000 | [diff] [blame] | 206 | if (current_display_count_ == 0) |
| 207 | ReadNextFrame(); |
| 208 | if (++current_display_count_ >= frame_display_count_) |
| 209 | current_display_count_ = 0; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 210 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 211 | temp_frame_ = absl::make_unique<VideoFrame>( |
| 212 | VideoFrame::Builder() |
| 213 | .set_video_frame_buffer(last_read_buffer_) |
| 214 | .set_rotation(webrtc::kVideoRotation_0) |
| 215 | .set_timestamp_us(0) |
| 216 | .build()); |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 217 | return temp_frame_.get(); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 218 | } |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 219 | |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 220 | void ReadNextFrame() { |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame] | 221 | last_read_buffer_ = |
| 222 | test::ReadI420Buffer(static_cast<int>(width_), |
| 223 | static_cast<int>(height_), |
| 224 | files_[file_index_]); |
| 225 | if (!last_read_buffer_) { |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 226 | // No more frames to read in this file, rewind and move to next file. |
| 227 | rewind(files_[file_index_]); |
| 228 | file_index_ = (file_index_ + 1) % files_.size(); |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame] | 229 | last_read_buffer_ = |
| 230 | test::ReadI420Buffer(static_cast<int>(width_), |
| 231 | static_cast<int>(height_), |
| 232 | files_[file_index_]); |
| 233 | RTC_CHECK(last_read_buffer_); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 234 | } |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | private: |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 238 | size_t file_index_; |
| 239 | const std::vector<FILE*> files_; |
| 240 | const size_t width_; |
| 241 | const size_t height_; |
| 242 | const size_t frame_size_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 243 | const std::unique_ptr<uint8_t[]> frame_buffer_; |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 244 | const int frame_display_count_; |
| 245 | int current_display_count_; |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 246 | rtc::scoped_refptr<I420Buffer> last_read_buffer_; |
| 247 | std::unique_ptr<VideoFrame> temp_frame_; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 248 | }; |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 249 | |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 250 | // SlideGenerator works similarly to YuvFileGenerator but it fills the frames |
| 251 | // with randomly sized and colored squares instead of reading their content |
| 252 | // from files. |
| 253 | class SlideGenerator : public FrameGenerator { |
| 254 | public: |
| 255 | SlideGenerator(int width, int height, int frame_repeat_count) |
| 256 | : width_(width), |
| 257 | height_(height), |
| 258 | frame_display_count_(frame_repeat_count), |
| 259 | current_display_count_(0), |
| 260 | random_generator_(1234) { |
| 261 | RTC_DCHECK_GT(width, 0); |
| 262 | RTC_DCHECK_GT(height, 0); |
| 263 | RTC_DCHECK_GT(frame_repeat_count, 0); |
| 264 | } |
| 265 | |
| 266 | VideoFrame* NextFrame() override { |
| 267 | if (current_display_count_ == 0) |
| 268 | GenerateNewFrame(); |
| 269 | if (++current_display_count_ >= frame_display_count_) |
| 270 | current_display_count_ = 0; |
| 271 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 272 | frame_ = absl::make_unique<VideoFrame>( |
| 273 | VideoFrame::Builder() |
| 274 | .set_video_frame_buffer(buffer_) |
| 275 | .set_rotation(webrtc::kVideoRotation_0) |
| 276 | .set_timestamp_us(0) |
| 277 | .build()); |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 278 | return frame_.get(); |
| 279 | } |
| 280 | |
| 281 | // Generates some randomly sized and colored squares scattered |
| 282 | // over the frame. |
| 283 | void GenerateNewFrame() { |
| 284 | // The squares should have a varying order of magnitude in order |
| 285 | // to simulate variation in the slides' complexity. |
Erik Språng | 3fed5db | 2017-11-16 10:39:25 +0100 | [diff] [blame] | 286 | const int kSquareNum = 1 << (4 + (random_generator_.Rand(0, 3) * 2)); |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 287 | |
| 288 | buffer_ = I420Buffer::Create(width_, height_); |
| 289 | memset(buffer_->MutableDataY(), 127, height_ * buffer_->StrideY()); |
| 290 | memset(buffer_->MutableDataU(), 127, |
| 291 | buffer_->ChromaHeight() * buffer_->StrideU()); |
| 292 | memset(buffer_->MutableDataV(), 127, |
| 293 | buffer_->ChromaHeight() * buffer_->StrideV()); |
| 294 | |
| 295 | for (int i = 0; i < kSquareNum; ++i) { |
| 296 | int length = random_generator_.Rand(1, width_ > 4 ? width_ / 4 : 1); |
| 297 | // Limit the length of later squares so that they don't overwrite the |
| 298 | // previous ones too much. |
| 299 | length = (length * (kSquareNum - i)) / kSquareNum; |
| 300 | |
| 301 | int x = random_generator_.Rand(0, width_ - length); |
| 302 | int y = random_generator_.Rand(0, height_ - length); |
| 303 | uint8_t yuv_y = random_generator_.Rand(0, 255); |
| 304 | uint8_t yuv_u = random_generator_.Rand(0, 255); |
| 305 | uint8_t yuv_v = random_generator_.Rand(0, 255); |
| 306 | |
| 307 | for (int yy = y; yy < y + length; ++yy) { |
| 308 | uint8_t* pos_y = |
| 309 | (buffer_->MutableDataY() + x + yy * buffer_->StrideY()); |
| 310 | memset(pos_y, yuv_y, length); |
| 311 | } |
| 312 | for (int yy = y; yy < y + length; yy += 2) { |
| 313 | uint8_t* pos_u = |
| 314 | (buffer_->MutableDataU() + x / 2 + yy / 2 * buffer_->StrideU()); |
| 315 | memset(pos_u, yuv_u, length / 2); |
| 316 | uint8_t* pos_v = |
| 317 | (buffer_->MutableDataV() + x / 2 + yy / 2 * buffer_->StrideV()); |
| 318 | memset(pos_v, yuv_v, length / 2); |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | private: |
| 324 | const int width_; |
| 325 | const int height_; |
| 326 | const int frame_display_count_; |
| 327 | int current_display_count_; |
| 328 | Random random_generator_; |
| 329 | rtc::scoped_refptr<I420Buffer> buffer_; |
| 330 | std::unique_ptr<VideoFrame> frame_; |
| 331 | }; |
| 332 | |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 333 | class ScrollingImageFrameGenerator : public FrameGenerator { |
| 334 | public: |
| 335 | ScrollingImageFrameGenerator(Clock* clock, |
| 336 | const std::vector<FILE*>& files, |
| 337 | size_t source_width, |
| 338 | size_t source_height, |
| 339 | size_t target_width, |
| 340 | size_t target_height, |
| 341 | int64_t scroll_time_ms, |
| 342 | int64_t pause_time_ms) |
| 343 | : clock_(clock), |
| 344 | start_time_(clock->TimeInMilliseconds()), |
| 345 | scroll_time_(scroll_time_ms), |
| 346 | pause_time_(pause_time_ms), |
| 347 | num_frames_(files.size()), |
nisse | f122a85 | 2016-10-04 23:27:30 -0700 | [diff] [blame] | 348 | target_width_(static_cast<int>(target_width)), |
| 349 | target_height_(static_cast<int>(target_height)), |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 350 | current_frame_num_(num_frames_ - 1), |
| 351 | current_source_frame_(nullptr), |
| 352 | file_generator_(files, source_width, source_height, 1) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 353 | RTC_DCHECK(clock_ != nullptr); |
kwiberg | af476c7 | 2016-11-28 15:21:39 -0800 | [diff] [blame] | 354 | RTC_DCHECK_GT(num_frames_, 0); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 355 | RTC_DCHECK_GE(source_height, target_height); |
| 356 | RTC_DCHECK_GE(source_width, target_width); |
| 357 | RTC_DCHECK_GE(scroll_time_ms, 0); |
| 358 | RTC_DCHECK_GE(pause_time_ms, 0); |
| 359 | RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 360 | } |
| 361 | |
Mirko Bonadei | fe055c1 | 2019-01-29 22:53:28 +0100 | [diff] [blame] | 362 | ~ScrollingImageFrameGenerator() override {} |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 363 | |
| 364 | VideoFrame* NextFrame() override { |
| 365 | const int64_t kFrameDisplayTime = scroll_time_ + pause_time_; |
| 366 | const int64_t now = clock_->TimeInMilliseconds(); |
| 367 | int64_t ms_since_start = now - start_time_; |
| 368 | |
| 369 | size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_; |
| 370 | UpdateSourceFrame(frame_num); |
| 371 | |
| 372 | double scroll_factor; |
| 373 | int64_t time_into_frame = ms_since_start % kFrameDisplayTime; |
| 374 | if (time_into_frame < scroll_time_) { |
| 375 | scroll_factor = static_cast<double>(time_into_frame) / scroll_time_; |
| 376 | } else { |
| 377 | scroll_factor = 1.0; |
| 378 | } |
| 379 | CropSourceToScrolledImage(scroll_factor); |
| 380 | |
nisse | df2ceb8 | 2016-12-15 06:29:53 -0800 | [diff] [blame] | 381 | return current_frame_ ? &*current_frame_ : nullptr; |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | void UpdateSourceFrame(size_t frame_num) { |
Ilya Nikolaevskiy | 6cfb403 | 2019-02-06 10:56:39 +0100 | [diff] [blame] | 385 | while (current_frame_num_ != frame_num || |
| 386 | current_source_frame_ == nullptr) { |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 387 | current_source_frame_ = file_generator_.NextFrame(); |
| 388 | current_frame_num_ = (current_frame_num_ + 1) % num_frames_; |
| 389 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 390 | RTC_DCHECK(current_source_frame_ != nullptr); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | void CropSourceToScrolledImage(double scroll_factor) { |
nisse | f122a85 | 2016-10-04 23:27:30 -0700 | [diff] [blame] | 394 | int scroll_margin_x = current_source_frame_->width() - target_width_; |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 395 | int pixels_scrolled_x = |
| 396 | static_cast<int>(scroll_margin_x * scroll_factor + 0.5); |
nisse | f122a85 | 2016-10-04 23:27:30 -0700 | [diff] [blame] | 397 | int scroll_margin_y = current_source_frame_->height() - target_height_; |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 398 | int pixels_scrolled_y = |
| 399 | static_cast<int>(scroll_margin_y * scroll_factor + 0.5); |
| 400 | |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 401 | rtc::scoped_refptr<I420BufferInterface> i420_buffer = |
| 402 | current_source_frame_->video_frame_buffer()->ToI420(); |
| 403 | int offset_y = |
| 404 | (i420_buffer->StrideY() * pixels_scrolled_y) + pixels_scrolled_x; |
| 405 | int offset_u = (i420_buffer->StrideU() * (pixels_scrolled_y / 2)) + |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 406 | (pixels_scrolled_x / 2); |
Magnus Jedvert | 90e3190 | 2017-06-07 11:32:50 +0200 | [diff] [blame] | 407 | int offset_v = (i420_buffer->StrideV() * (pixels_scrolled_y / 2)) + |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 408 | (pixels_scrolled_x / 2); |
| 409 | |
Artem Titov | 1ebfb6a | 2019-01-03 23:49:37 +0100 | [diff] [blame] | 410 | current_frame_ = |
| 411 | VideoFrame::Builder() |
| 412 | .set_video_frame_buffer(WrapI420Buffer( |
| 413 | target_width_, target_height_, &i420_buffer->DataY()[offset_y], |
| 414 | i420_buffer->StrideY(), &i420_buffer->DataU()[offset_u], |
| 415 | i420_buffer->StrideU(), &i420_buffer->DataV()[offset_v], |
| 416 | i420_buffer->StrideV(), KeepRefUntilDone(i420_buffer))) |
| 417 | .set_rotation(kVideoRotation_0) |
| 418 | .set_timestamp_us(0) |
| 419 | .build(); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 420 | } |
| 421 | |
| 422 | Clock* const clock_; |
| 423 | const int64_t start_time_; |
| 424 | const int64_t scroll_time_; |
| 425 | const int64_t pause_time_; |
| 426 | const size_t num_frames_; |
nisse | f122a85 | 2016-10-04 23:27:30 -0700 | [diff] [blame] | 427 | const int target_width_; |
| 428 | const int target_height_; |
| 429 | |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 430 | size_t current_frame_num_; |
| 431 | VideoFrame* current_source_frame_; |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 432 | absl::optional<VideoFrame> current_frame_; |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 433 | YuvFileGenerator file_generator_; |
| 434 | }; |
| 435 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 436 | } // namespace |
| 437 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 438 | FrameForwarder::FrameForwarder() : sink_(nullptr) {} |
sprang | b1ca073 | 2017-02-01 08:38:12 -0800 | [diff] [blame] | 439 | FrameForwarder::~FrameForwarder() {} |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 440 | |
| 441 | void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) { |
| 442 | rtc::CritScope lock(&crit_); |
| 443 | if (sink_) |
| 444 | sink_->OnFrame(video_frame); |
| 445 | } |
| 446 | |
| 447 | void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 448 | const rtc::VideoSinkWants& wants) { |
| 449 | rtc::CritScope lock(&crit_); |
| 450 | RTC_DCHECK(!sink_ || sink_ == sink); |
| 451 | sink_ = sink; |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 452 | sink_wants_ = wants; |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| 455 | void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 456 | rtc::CritScope lock(&crit_); |
| 457 | RTC_DCHECK_EQ(sink, sink_); |
| 458 | sink_ = nullptr; |
| 459 | } |
| 460 | |
perkj | 803d97f | 2016-11-01 11:45:46 -0700 | [diff] [blame] | 461 | rtc::VideoSinkWants FrameForwarder::sink_wants() const { |
| 462 | rtc::CritScope lock(&crit_); |
| 463 | return sink_wants_; |
| 464 | } |
| 465 | |
| 466 | bool FrameForwarder::has_sinks() const { |
| 467 | rtc::CritScope lock(&crit_); |
| 468 | return sink_ != nullptr; |
| 469 | } |
| 470 | |
Artem Titov | f50c6c2 | 2019-01-24 16:54:03 +0100 | [diff] [blame] | 471 | void FrameGenerator::ChangeResolution(size_t width, size_t height) { |
| 472 | RTC_NOTREACHED(); |
| 473 | } |
| 474 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 475 | std::unique_ptr<FrameGenerator> FrameGenerator::CreateSquareGenerator( |
| 476 | int width, |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 477 | int height, |
Danil Chapovalov | 431abd9 | 2018-06-18 12:54:17 +0200 | [diff] [blame] | 478 | absl::optional<OutputType> type, |
| 479 | absl::optional<int> num_squares) { |
erikvarga@webrtc.org | c774d5d | 2017-10-10 14:34:38 +0200 | [diff] [blame] | 480 | return std::unique_ptr<FrameGenerator>( |
Emircan Uysaler | 03e6ec9 | 2018-03-09 15:03:26 -0800 | [diff] [blame] | 481 | new SquareGenerator(width, height, type.value_or(OutputType::I420), |
| 482 | num_squares.value_or(10))); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 483 | } |
| 484 | |
erikvarga | 579de6f | 2017-08-29 09:12:57 -0700 | [diff] [blame] | 485 | std::unique_ptr<FrameGenerator> FrameGenerator::CreateSlideGenerator( |
| 486 | int width, int height, int frame_repeat_count) { |
| 487 | return std::unique_ptr<FrameGenerator>(new SlideGenerator( |
| 488 | width, height, frame_repeat_count)); |
| 489 | } |
| 490 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 491 | std::unique_ptr<FrameGenerator> FrameGenerator::CreateFromYuvFile( |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 492 | std::vector<std::string> filenames, |
| 493 | size_t width, |
| 494 | size_t height, |
| 495 | int frame_repeat_count) { |
kwiberg | b890c95c | 2016-11-29 05:30:40 -0800 | [diff] [blame] | 496 | RTC_DCHECK(!filenames.empty()); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 497 | std::vector<FILE*> files; |
| 498 | for (const std::string& filename : filenames) { |
| 499 | FILE* file = fopen(filename.c_str(), "rb"); |
Sebastian Jansson | 9eda327 | 2018-09-13 16:23:03 +0200 | [diff] [blame] | 500 | RTC_DCHECK(file != nullptr) << "Failed to open: '" << filename << "'\n"; |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 501 | files.push_back(file); |
| 502 | } |
| 503 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 504 | return std::unique_ptr<FrameGenerator>( |
| 505 | new YuvFileGenerator(files, width, height, frame_repeat_count)); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 506 | } |
| 507 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 508 | std::unique_ptr<FrameGenerator> |
| 509 | FrameGenerator::CreateScrollingInputFromYuvFiles( |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 510 | Clock* clock, |
| 511 | std::vector<std::string> filenames, |
| 512 | size_t source_width, |
| 513 | size_t source_height, |
| 514 | size_t target_width, |
| 515 | size_t target_height, |
| 516 | int64_t scroll_time_ms, |
| 517 | int64_t pause_time_ms) { |
kwiberg | b890c95c | 2016-11-29 05:30:40 -0800 | [diff] [blame] | 518 | RTC_DCHECK(!filenames.empty()); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 519 | std::vector<FILE*> files; |
| 520 | for (const std::string& filename : filenames) { |
| 521 | FILE* file = fopen(filename.c_str(), "rb"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 522 | RTC_DCHECK(file != nullptr); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 523 | files.push_back(file); |
| 524 | } |
| 525 | |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 526 | return std::unique_ptr<FrameGenerator>(new ScrollingImageFrameGenerator( |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 527 | clock, files, source_width, source_height, target_width, target_height, |
perkj | a8ba195 | 2017-02-27 06:52:10 -0800 | [diff] [blame] | 528 | scroll_time_ms, pause_time_ms)); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 529 | } |
| 530 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 531 | } // namespace test |
| 532 | } // namespace webrtc |