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