blob: 27935e4b26be0a1575547012e6955e3c1d52fe11 [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 */
pbos@webrtc.org724947b2013-12-11 16:26:16 +000010#include "webrtc/test/frame_generator.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000011
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000012#include <math.h>
andresp@webrtc.orgab654952013-09-19 12:14:03 +000013#include <stdio.h>
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000014#include <string.h>
andresp@webrtc.orgab654952013-09-19 12:14:03 +000015
kwibergbfefb032016-05-01 14:53:46 -070016#include <memory>
17
sprang@webrtc.org131bea82015-02-18 12:46:06 +000018#include "webrtc/base/checks.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000019#include "webrtc/common_video/libyuv/include/webrtc_libyuv.h"
Henrik Kjellander98f53512015-10-28 18:17:40 +010020#include "webrtc/system_wrappers/include/clock.h"
nisse1996e3f2016-09-19 00:34:46 -070021#include "libyuv/convert.h"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000022
23namespace webrtc {
24namespace test {
25namespace {
26
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000027class ChromaGenerator : public FrameGenerator {
28 public:
pbos@webrtc.org724947b2013-12-11 16:26:16 +000029 ChromaGenerator(size_t width, size_t height)
nisse30116272016-09-16 05:45:08 -070030 : angle_(0.0), width_(width), height_(height) {
nisse1996e3f2016-09-19 00:34:46 -070031 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.org266c7b32013-10-15 09:15:47 +000036 }
37
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070038 VideoFrame* NextFrame() override {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000039 angle_ += 30.0;
40 uint8_t u = fabs(sin(angle_)) * 0xFF;
41 uint8_t v = fabs(cos(angle_)) * 0xFF;
42
nisse1996e3f2016-09-19 00:34:46 -070043 // 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.org266c7b32013-10-15 09:15:47 +000055 }
56
57 private:
58 double angle_;
nissefbf14602016-09-16 01:32:02 -070059 size_t width_;
60 size_t height_;
nisse1996e3f2016-09-19 00:34:46 -070061 size_t half_width_;
62 size_t y_size_;
63 size_t uv_size_;
64 std::unique_ptr<VideoFrame> frame_;
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000065};
66
andresp@webrtc.orgab654952013-09-19 12:14:03 +000067class YuvFileGenerator : public FrameGenerator {
68 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +000069 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.orgab654952013-09-19 12:14:03 +000083 assert(width > 0);
84 assert(height > 0);
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000085 assert(frame_repeat_count > 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000086 }
87
88 virtual ~YuvFileGenerator() {
sprang@webrtc.org131bea82015-02-18 12:46:06 +000089 for (FILE* file : files_)
90 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000091 }
92
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070093 VideoFrame* NextFrame() override {
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000094 if (current_display_count_ == 0)
95 ReadNextFrame();
96 if (++current_display_count_ >= frame_display_count_)
97 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +000098
nisse1996e3f2016-09-19 00:34:46 -070099 temp_frame_.reset(
100 new VideoFrame(last_read_buffer_, 0, 0, webrtc::kVideoRotation_0));
101 return temp_frame_.get();
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000102 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000103
nisse1996e3f2016-09-19 00:34:46 -0700104 // TODO(nisse): Have a frame reader in one place. And read directly
105 // into the planes of an I420Buffer, the extra copying below is silly.
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000106 void ReadNextFrame() {
107 size_t bytes_read =
108 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]);
109 if (bytes_read < frame_size_) {
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();
113 bytes_read = fread(frame_buffer_.get(), 1, frame_size_,
114 files_[file_index_]);
115 assert(bytes_read >= frame_size_);
116 }
117
nisse1996e3f2016-09-19 00:34:46 -0700118 size_t half_width = (width_ + 1) / 2;
119 size_t size_y = width_ * height_;
120 size_t size_uv = half_width * ((height_ + 1) / 2);
121 last_read_buffer_ = I420Buffer::Create(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000122 static_cast<int>(width_), static_cast<int>(height_),
nisse1996e3f2016-09-19 00:34:46 -0700123 static_cast<int>(width_), static_cast<int>(half_width),
124 static_cast<int>(half_width));
125 libyuv::I420Copy(
126 frame_buffer_.get(), static_cast<int>(width_),
127 frame_buffer_.get() + size_y, static_cast<int>(half_width),
128 frame_buffer_.get() + size_y + size_uv, static_cast<int>(half_width),
129 last_read_buffer_->MutableDataY(), last_read_buffer_->StrideY(),
130 last_read_buffer_->MutableDataU(), last_read_buffer_->StrideU(),
131 last_read_buffer_->MutableDataV(), last_read_buffer_->StrideV(),
132 static_cast<int>(width_), static_cast<int>(height_));
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000133 }
134
135 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000136 size_t file_index_;
137 const std::vector<FILE*> files_;
138 const size_t width_;
139 const size_t height_;
140 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700141 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000142 const int frame_display_count_;
143 int current_display_count_;
nisse1996e3f2016-09-19 00:34:46 -0700144 rtc::scoped_refptr<I420Buffer> last_read_buffer_;
145 std::unique_ptr<VideoFrame> temp_frame_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000146};
sprangd6358952015-07-29 07:58:13 -0700147
148class ScrollingImageFrameGenerator : public FrameGenerator {
149 public:
150 ScrollingImageFrameGenerator(Clock* clock,
151 const std::vector<FILE*>& files,
152 size_t source_width,
153 size_t source_height,
154 size_t target_width,
155 size_t target_height,
156 int64_t scroll_time_ms,
157 int64_t pause_time_ms)
158 : clock_(clock),
159 start_time_(clock->TimeInMilliseconds()),
160 scroll_time_(scroll_time_ms),
161 pause_time_(pause_time_ms),
162 num_frames_(files.size()),
163 current_frame_num_(num_frames_ - 1),
164 current_source_frame_(nullptr),
165 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700166 RTC_DCHECK(clock_ != nullptr);
167 RTC_DCHECK_GT(num_frames_, 0u);
168 RTC_DCHECK_GE(source_height, target_height);
169 RTC_DCHECK_GE(source_width, target_width);
170 RTC_DCHECK_GE(scroll_time_ms, 0);
171 RTC_DCHECK_GE(pause_time_ms, 0);
172 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700173 current_frame_.CreateEmptyFrame(static_cast<int>(target_width),
174 static_cast<int>(target_height),
175 static_cast<int>(target_width),
176 static_cast<int>((target_width + 1) / 2),
177 static_cast<int>((target_width + 1) / 2));
178 }
179
180 virtual ~ScrollingImageFrameGenerator() {}
181
182 VideoFrame* NextFrame() override {
183 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
184 const int64_t now = clock_->TimeInMilliseconds();
185 int64_t ms_since_start = now - start_time_;
186
187 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
188 UpdateSourceFrame(frame_num);
189
190 double scroll_factor;
191 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
192 if (time_into_frame < scroll_time_) {
193 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
194 } else {
195 scroll_factor = 1.0;
196 }
197 CropSourceToScrolledImage(scroll_factor);
198
199 return &current_frame_;
200 }
201
202 void UpdateSourceFrame(size_t frame_num) {
203 while (current_frame_num_ != frame_num) {
204 current_source_frame_ = file_generator_.NextFrame();
205 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
206 }
henrikg91d6ede2015-09-17 00:24:34 -0700207 RTC_DCHECK(current_source_frame_ != nullptr);
sprangd6358952015-07-29 07:58:13 -0700208 }
209
210 void CropSourceToScrolledImage(double scroll_factor) {
211 const int kTargetWidth = current_frame_.width();
212 const int kTargetHeight = current_frame_.height();
213 int scroll_margin_x = current_source_frame_->width() - kTargetWidth;
214 int pixels_scrolled_x =
215 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
216 int scroll_margin_y = current_source_frame_->height() - kTargetHeight;
217 int pixels_scrolled_y =
218 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
219
nissec9c142f2016-05-17 04:05:47 -0700220 int offset_y = (current_source_frame_->video_frame_buffer()->StrideY() *
sprangd6358952015-07-29 07:58:13 -0700221 pixels_scrolled_y) +
222 pixels_scrolled_x;
nissec9c142f2016-05-17 04:05:47 -0700223 int offset_u = (current_source_frame_->video_frame_buffer()->StrideU() *
sprangd6358952015-07-29 07:58:13 -0700224 (pixels_scrolled_y / 2)) +
225 (pixels_scrolled_x / 2);
nissec9c142f2016-05-17 04:05:47 -0700226 int offset_v = (current_source_frame_->video_frame_buffer()->StrideV() *
sprangd6358952015-07-29 07:58:13 -0700227 (pixels_scrolled_y / 2)) +
228 (pixels_scrolled_x / 2);
229
230 current_frame_.CreateFrame(
nissec9c142f2016-05-17 04:05:47 -0700231 &current_source_frame_->video_frame_buffer()->DataY()[offset_y],
232 &current_source_frame_->video_frame_buffer()->DataU()[offset_u],
233 &current_source_frame_->video_frame_buffer()->DataV()[offset_v],
sprangd6358952015-07-29 07:58:13 -0700234 kTargetWidth, kTargetHeight,
nissec9c142f2016-05-17 04:05:47 -0700235 current_source_frame_->video_frame_buffer()->StrideY(),
236 current_source_frame_->video_frame_buffer()->StrideU(),
237 current_source_frame_->video_frame_buffer()->StrideV(),
Niels Möller739fcb92016-02-29 13:11:45 +0100238 kVideoRotation_0);
sprangd6358952015-07-29 07:58:13 -0700239 }
240
241 Clock* const clock_;
242 const int64_t start_time_;
243 const int64_t scroll_time_;
244 const int64_t pause_time_;
245 const size_t num_frames_;
246 size_t current_frame_num_;
247 VideoFrame* current_source_frame_;
248 VideoFrame current_frame_;
249 YuvFileGenerator file_generator_;
250};
251
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000252} // namespace
253
perkja49cbd32016-09-16 07:53:41 -0700254FrameForwarder::FrameForwarder() : sink_(nullptr) {}
255
256void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
257 rtc::CritScope lock(&crit_);
258 if (sink_)
259 sink_->OnFrame(video_frame);
260}
261
262void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
263 const rtc::VideoSinkWants& wants) {
264 rtc::CritScope lock(&crit_);
265 RTC_DCHECK(!sink_ || sink_ == sink);
266 sink_ = sink;
267}
268
269void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
270 rtc::CritScope lock(&crit_);
271 RTC_DCHECK_EQ(sink, sink_);
272 sink_ = nullptr;
273}
274
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000275FrameGenerator* FrameGenerator::CreateChromaGenerator(size_t width,
276 size_t height) {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000277 return new ChromaGenerator(width, height);
278}
279
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000280FrameGenerator* FrameGenerator::CreateFromYuvFile(
281 std::vector<std::string> filenames,
282 size_t width,
283 size_t height,
284 int frame_repeat_count) {
285 assert(!filenames.empty());
286 std::vector<FILE*> files;
287 for (const std::string& filename : filenames) {
288 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700289 RTC_DCHECK(file != nullptr);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000290 files.push_back(file);
291 }
292
293 return new YuvFileGenerator(files, width, height, frame_repeat_count);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000294}
295
sprangd6358952015-07-29 07:58:13 -0700296FrameGenerator* FrameGenerator::CreateScrollingInputFromYuvFiles(
297 Clock* clock,
298 std::vector<std::string> filenames,
299 size_t source_width,
300 size_t source_height,
301 size_t target_width,
302 size_t target_height,
303 int64_t scroll_time_ms,
304 int64_t pause_time_ms) {
305 assert(!filenames.empty());
306 std::vector<FILE*> files;
307 for (const std::string& filename : filenames) {
308 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700309 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700310 files.push_back(file);
311 }
312
313 return new ScrollingImageFrameGenerator(
314 clock, files, source_width, source_height, target_width, target_height,
315 scroll_time_ms, pause_time_ms);
316}
317
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000318} // namespace test
319} // namespace webrtc