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 | */ |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 10 | #include "webrtc/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 <math.h> |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 13 | #include <stdio.h> |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 14 | #include <string.h> |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 15 | |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 16 | #include <memory> |
| 17 | |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 18 | #include "webrtc/base/checks.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 19 | #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
Henrik Kjellander | 98f5351 | 2015-10-28 18:17:40 +0100 | [diff] [blame] | 20 | #include "webrtc/system_wrappers/include/clock.h" |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 21 | #include "webrtc/test/frame_utils.h" |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 22 | |
| 23 | namespace webrtc { |
| 24 | namespace test { |
| 25 | namespace { |
| 26 | |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 27 | class ChromaGenerator : public FrameGenerator { |
| 28 | public: |
perkj | 3b703ed | 2016-09-29 23:25:40 -0700 | [diff] [blame] | 29 | ChromaGenerator(size_t width, size_t height) |
| 30 | : angle_(0.0), width_(width), height_(height) { |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 31 | RTC_CHECK(width_ > 0); |
| 32 | RTC_CHECK(height_ > 0); |
| 33 | half_width_ = (width_ + 1) / 2; |
| 34 | y_size_ = width_ * height_; |
| 35 | uv_size_ = half_width_ * ((height_ + 1) / 2); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 38 | VideoFrame* NextFrame() override { |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 39 | angle_ += 30.0; |
| 40 | uint8_t u = fabs(sin(angle_)) * 0xFF; |
| 41 | uint8_t v = fabs(cos(angle_)) * 0xFF; |
| 42 | |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 43 | // Ensure stride == width. |
| 44 | rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create( |
| 45 | static_cast<int>(width_), static_cast<int>(height_), |
| 46 | static_cast<int>(width_), static_cast<int>(half_width_), |
| 47 | static_cast<int>(half_width_))); |
| 48 | |
| 49 | memset(buffer->MutableDataY(), 0x80, y_size_); |
| 50 | memset(buffer->MutableDataU(), u, uv_size_); |
| 51 | memset(buffer->MutableDataV(), v, uv_size_); |
| 52 | |
| 53 | frame_.reset(new VideoFrame(buffer, 0, 0, webrtc::kVideoRotation_0)); |
| 54 | return frame_.get(); |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | private: |
perkj | 3b703ed | 2016-09-29 23:25:40 -0700 | [diff] [blame] | 58 | double angle_; |
| 59 | size_t width_; |
| 60 | size_t height_; |
| 61 | size_t half_width_; |
| 62 | size_t y_size_; |
| 63 | size_t uv_size_; |
| 64 | std::unique_ptr<VideoFrame> frame_; |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 65 | }; |
| 66 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 67 | class YuvFileGenerator : public FrameGenerator { |
| 68 | public: |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 69 | YuvFileGenerator(std::vector<FILE*> files, |
| 70 | size_t width, |
| 71 | size_t height, |
| 72 | int frame_repeat_count) |
| 73 | : file_index_(0), |
| 74 | files_(files), |
| 75 | width_(width), |
| 76 | height_(height), |
| 77 | frame_size_(CalcBufferSize(kI420, |
| 78 | static_cast<int>(width_), |
| 79 | static_cast<int>(height_))), |
| 80 | frame_buffer_(new uint8_t[frame_size_]), |
| 81 | frame_display_count_(frame_repeat_count), |
| 82 | current_display_count_(0) { |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 83 | assert(width > 0); |
| 84 | assert(height > 0); |
sprang@webrtc.org | 25dd1db | 2015-03-02 11:55:45 +0000 | [diff] [blame] | 85 | assert(frame_repeat_count > 0); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | virtual ~YuvFileGenerator() { |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 89 | for (FILE* file : files_) |
| 90 | fclose(file); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Miguel Casas-Sanchez | 4765070 | 2015-05-29 17:21:40 -0700 | [diff] [blame] | 93 | VideoFrame* NextFrame() override { |
sprang@webrtc.org | 25dd1db | 2015-03-02 11:55:45 +0000 | [diff] [blame] | 94 | if (current_display_count_ == 0) |
| 95 | ReadNextFrame(); |
| 96 | if (++current_display_count_ >= frame_display_count_) |
| 97 | current_display_count_ = 0; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 98 | |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 99 | temp_frame_.reset( |
| 100 | new VideoFrame(last_read_buffer_, 0, 0, webrtc::kVideoRotation_0)); |
| 101 | return temp_frame_.get(); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 102 | } |
pbos@webrtc.org | 724947b | 2013-12-11 16:26:16 +0000 | [diff] [blame] | 103 | |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 104 | void ReadNextFrame() { |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 105 | last_read_buffer_ = |
| 106 | test::ReadI420Buffer(static_cast<int>(width_), |
| 107 | static_cast<int>(height_), |
| 108 | files_[file_index_]); |
| 109 | if (!last_read_buffer_) { |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 110 | // No more frames to read in this file, rewind and move to next file. |
| 111 | rewind(files_[file_index_]); |
| 112 | file_index_ = (file_index_ + 1) % files_.size(); |
nisse | 115bd15 | 2016-09-30 04:14:07 -0700 | [diff] [blame^] | 113 | last_read_buffer_ = |
| 114 | test::ReadI420Buffer(static_cast<int>(width_), |
| 115 | static_cast<int>(height_), |
| 116 | files_[file_index_]); |
| 117 | RTC_CHECK(last_read_buffer_); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 118 | } |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | private: |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 122 | size_t file_index_; |
| 123 | const std::vector<FILE*> files_; |
| 124 | const size_t width_; |
| 125 | const size_t height_; |
| 126 | const size_t frame_size_; |
kwiberg | bfefb03 | 2016-05-01 14:53:46 -0700 | [diff] [blame] | 127 | const std::unique_ptr<uint8_t[]> frame_buffer_; |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 128 | const int frame_display_count_; |
| 129 | int current_display_count_; |
nisse | 1996e3f | 2016-09-19 00:34:46 -0700 | [diff] [blame] | 130 | rtc::scoped_refptr<I420Buffer> last_read_buffer_; |
| 131 | std::unique_ptr<VideoFrame> temp_frame_; |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 132 | }; |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 133 | |
| 134 | class ScrollingImageFrameGenerator : public FrameGenerator { |
| 135 | public: |
| 136 | ScrollingImageFrameGenerator(Clock* clock, |
| 137 | const std::vector<FILE*>& files, |
| 138 | size_t source_width, |
| 139 | size_t source_height, |
| 140 | size_t target_width, |
| 141 | size_t target_height, |
| 142 | int64_t scroll_time_ms, |
| 143 | int64_t pause_time_ms) |
| 144 | : clock_(clock), |
| 145 | start_time_(clock->TimeInMilliseconds()), |
| 146 | scroll_time_(scroll_time_ms), |
| 147 | pause_time_(pause_time_ms), |
| 148 | num_frames_(files.size()), |
| 149 | current_frame_num_(num_frames_ - 1), |
| 150 | current_source_frame_(nullptr), |
| 151 | file_generator_(files, source_width, source_height, 1) { |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 152 | RTC_DCHECK(clock_ != nullptr); |
| 153 | RTC_DCHECK_GT(num_frames_, 0u); |
| 154 | RTC_DCHECK_GE(source_height, target_height); |
| 155 | RTC_DCHECK_GE(source_width, target_width); |
| 156 | RTC_DCHECK_GE(scroll_time_ms, 0); |
| 157 | RTC_DCHECK_GE(pause_time_ms, 0); |
| 158 | RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 159 | current_frame_.CreateEmptyFrame(static_cast<int>(target_width), |
| 160 | static_cast<int>(target_height), |
| 161 | static_cast<int>(target_width), |
| 162 | static_cast<int>((target_width + 1) / 2), |
| 163 | static_cast<int>((target_width + 1) / 2)); |
| 164 | } |
| 165 | |
| 166 | virtual ~ScrollingImageFrameGenerator() {} |
| 167 | |
| 168 | VideoFrame* NextFrame() override { |
| 169 | const int64_t kFrameDisplayTime = scroll_time_ + pause_time_; |
| 170 | const int64_t now = clock_->TimeInMilliseconds(); |
| 171 | int64_t ms_since_start = now - start_time_; |
| 172 | |
| 173 | size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_; |
| 174 | UpdateSourceFrame(frame_num); |
| 175 | |
| 176 | double scroll_factor; |
| 177 | int64_t time_into_frame = ms_since_start % kFrameDisplayTime; |
| 178 | if (time_into_frame < scroll_time_) { |
| 179 | scroll_factor = static_cast<double>(time_into_frame) / scroll_time_; |
| 180 | } else { |
| 181 | scroll_factor = 1.0; |
| 182 | } |
| 183 | CropSourceToScrolledImage(scroll_factor); |
| 184 | |
| 185 | return ¤t_frame_; |
| 186 | } |
| 187 | |
| 188 | void UpdateSourceFrame(size_t frame_num) { |
| 189 | while (current_frame_num_ != frame_num) { |
| 190 | current_source_frame_ = file_generator_.NextFrame(); |
| 191 | current_frame_num_ = (current_frame_num_ + 1) % num_frames_; |
| 192 | } |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 193 | RTC_DCHECK(current_source_frame_ != nullptr); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | void CropSourceToScrolledImage(double scroll_factor) { |
| 197 | const int kTargetWidth = current_frame_.width(); |
| 198 | const int kTargetHeight = current_frame_.height(); |
| 199 | int scroll_margin_x = current_source_frame_->width() - kTargetWidth; |
| 200 | int pixels_scrolled_x = |
| 201 | static_cast<int>(scroll_margin_x * scroll_factor + 0.5); |
| 202 | int scroll_margin_y = current_source_frame_->height() - kTargetHeight; |
| 203 | int pixels_scrolled_y = |
| 204 | static_cast<int>(scroll_margin_y * scroll_factor + 0.5); |
| 205 | |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 206 | int offset_y = (current_source_frame_->video_frame_buffer()->StrideY() * |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 207 | pixels_scrolled_y) + |
| 208 | pixels_scrolled_x; |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 209 | int offset_u = (current_source_frame_->video_frame_buffer()->StrideU() * |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 210 | (pixels_scrolled_y / 2)) + |
| 211 | (pixels_scrolled_x / 2); |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 212 | int offset_v = (current_source_frame_->video_frame_buffer()->StrideV() * |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 213 | (pixels_scrolled_y / 2)) + |
| 214 | (pixels_scrolled_x / 2); |
| 215 | |
| 216 | current_frame_.CreateFrame( |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 217 | ¤t_source_frame_->video_frame_buffer()->DataY()[offset_y], |
| 218 | ¤t_source_frame_->video_frame_buffer()->DataU()[offset_u], |
| 219 | ¤t_source_frame_->video_frame_buffer()->DataV()[offset_v], |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 220 | kTargetWidth, kTargetHeight, |
nisse | c9c142f | 2016-05-17 04:05:47 -0700 | [diff] [blame] | 221 | current_source_frame_->video_frame_buffer()->StrideY(), |
| 222 | current_source_frame_->video_frame_buffer()->StrideU(), |
| 223 | current_source_frame_->video_frame_buffer()->StrideV(), |
Niels Möller | 739fcb9 | 2016-02-29 13:11:45 +0100 | [diff] [blame] | 224 | kVideoRotation_0); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | Clock* const clock_; |
| 228 | const int64_t start_time_; |
| 229 | const int64_t scroll_time_; |
| 230 | const int64_t pause_time_; |
| 231 | const size_t num_frames_; |
| 232 | size_t current_frame_num_; |
| 233 | VideoFrame* current_source_frame_; |
| 234 | VideoFrame current_frame_; |
| 235 | YuvFileGenerator file_generator_; |
| 236 | }; |
| 237 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 238 | } // namespace |
| 239 | |
perkj | a49cbd3 | 2016-09-16 07:53:41 -0700 | [diff] [blame] | 240 | FrameForwarder::FrameForwarder() : sink_(nullptr) {} |
| 241 | |
| 242 | void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) { |
| 243 | rtc::CritScope lock(&crit_); |
| 244 | if (sink_) |
| 245 | sink_->OnFrame(video_frame); |
| 246 | } |
| 247 | |
| 248 | void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink, |
| 249 | const rtc::VideoSinkWants& wants) { |
| 250 | rtc::CritScope lock(&crit_); |
| 251 | RTC_DCHECK(!sink_ || sink_ == sink); |
| 252 | sink_ = sink; |
| 253 | } |
| 254 | |
| 255 | void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) { |
| 256 | rtc::CritScope lock(&crit_); |
| 257 | RTC_DCHECK_EQ(sink, sink_); |
| 258 | sink_ = nullptr; |
| 259 | } |
| 260 | |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 261 | FrameGenerator* FrameGenerator::CreateChromaGenerator(size_t width, |
| 262 | size_t height) { |
pbos@webrtc.org | 266c7b3 | 2013-10-15 09:15:47 +0000 | [diff] [blame] | 263 | return new ChromaGenerator(width, height); |
| 264 | } |
| 265 | |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 266 | FrameGenerator* FrameGenerator::CreateFromYuvFile( |
| 267 | std::vector<std::string> filenames, |
| 268 | size_t width, |
| 269 | size_t height, |
| 270 | int frame_repeat_count) { |
| 271 | assert(!filenames.empty()); |
| 272 | std::vector<FILE*> files; |
| 273 | for (const std::string& filename : filenames) { |
| 274 | FILE* file = fopen(filename.c_str(), "rb"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 275 | RTC_DCHECK(file != nullptr); |
sprang@webrtc.org | 131bea8 | 2015-02-18 12:46:06 +0000 | [diff] [blame] | 276 | files.push_back(file); |
| 277 | } |
| 278 | |
| 279 | return new YuvFileGenerator(files, width, height, frame_repeat_count); |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 280 | } |
| 281 | |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 282 | FrameGenerator* FrameGenerator::CreateScrollingInputFromYuvFiles( |
| 283 | Clock* clock, |
| 284 | std::vector<std::string> filenames, |
| 285 | size_t source_width, |
| 286 | size_t source_height, |
| 287 | size_t target_width, |
| 288 | size_t target_height, |
| 289 | int64_t scroll_time_ms, |
| 290 | int64_t pause_time_ms) { |
| 291 | assert(!filenames.empty()); |
| 292 | std::vector<FILE*> files; |
| 293 | for (const std::string& filename : filenames) { |
| 294 | FILE* file = fopen(filename.c_str(), "rb"); |
henrikg | 91d6ede | 2015-09-17 00:24:34 -0700 | [diff] [blame] | 295 | RTC_DCHECK(file != nullptr); |
sprang | d635895 | 2015-07-29 07:58:13 -0700 | [diff] [blame] | 296 | files.push_back(file); |
| 297 | } |
| 298 | |
| 299 | return new ScrollingImageFrameGenerator( |
| 300 | clock, files, source_width, source_height, target_width, target_height, |
| 301 | scroll_time_ms, pause_time_ms); |
| 302 | } |
| 303 | |
andresp@webrtc.org | ab65495 | 2013-09-19 12:14:03 +0000 | [diff] [blame] | 304 | } // namespace test |
| 305 | } // namespace webrtc |