blob: dbf39c4e096f8c64a9b1b7ab4e2bf65ea5816c97 [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"
andresp@webrtc.orgab654952013-09-19 12:14:03 +000021
22namespace webrtc {
23namespace test {
24namespace {
25
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000026class ChromaGenerator : public FrameGenerator {
27 public:
pbos@webrtc.org724947b2013-12-11 16:26:16 +000028 ChromaGenerator(size_t width, size_t height)
nisse30116272016-09-16 05:45:08 -070029 : angle_(0.0), width_(width), height_(height) {
30 assert(width > 0);
31 assert(height > 0);
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000032 }
33
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070034 VideoFrame* NextFrame() override {
nisse30116272016-09-16 05:45:08 -070035 frame_.CreateEmptyFrame(static_cast<int>(width_),
36 static_cast<int>(height_),
37 static_cast<int>(width_),
38 static_cast<int>((width_ + 1) / 2),
39 static_cast<int>((width_ + 1) / 2));
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000040 angle_ += 30.0;
41 uint8_t u = fabs(sin(angle_)) * 0xFF;
42 uint8_t v = fabs(cos(angle_)) * 0xFF;
43
nisse30116272016-09-16 05:45:08 -070044 memset(frame_.video_frame_buffer()->MutableDataY(), 0x80,
45 frame_.allocated_size(kYPlane));
46 memset(frame_.video_frame_buffer()->MutableDataU(), u,
47 frame_.allocated_size(kUPlane));
48 memset(frame_.video_frame_buffer()->MutableDataV(), v,
49 frame_.allocated_size(kVPlane));
50 return &frame_;
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000051 }
52
53 private:
54 double angle_;
nissefbf14602016-09-16 01:32:02 -070055 size_t width_;
56 size_t height_;
nisse30116272016-09-16 05:45:08 -070057 VideoFrame frame_;
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000058};
59
andresp@webrtc.orgab654952013-09-19 12:14:03 +000060class YuvFileGenerator : public FrameGenerator {
61 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +000062 YuvFileGenerator(std::vector<FILE*> files,
63 size_t width,
64 size_t height,
65 int frame_repeat_count)
66 : file_index_(0),
67 files_(files),
68 width_(width),
69 height_(height),
70 frame_size_(CalcBufferSize(kI420,
71 static_cast<int>(width_),
72 static_cast<int>(height_))),
73 frame_buffer_(new uint8_t[frame_size_]),
74 frame_display_count_(frame_repeat_count),
75 current_display_count_(0) {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000076 assert(width > 0);
77 assert(height > 0);
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000078 assert(frame_repeat_count > 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000079 }
80
81 virtual ~YuvFileGenerator() {
sprang@webrtc.org131bea82015-02-18 12:46:06 +000082 for (FILE* file : files_)
83 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000084 }
85
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070086 VideoFrame* NextFrame() override {
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000087 if (current_display_count_ == 0)
88 ReadNextFrame();
89 if (++current_display_count_ >= frame_display_count_)
90 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +000091
nisse30116272016-09-16 05:45:08 -070092 // If this is the last repeatition of this frame, it's OK to use the
93 // original instance, otherwise use a copy.
94 if (current_display_count_ == frame_display_count_)
95 return &last_read_frame_;
96
97 temp_frame_copy_.CopyFrame(last_read_frame_);
98 return &temp_frame_copy_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +000099 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +0000100
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000101 void ReadNextFrame() {
102 size_t bytes_read =
103 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]);
104 if (bytes_read < frame_size_) {
105 // No more frames to read in this file, rewind and move to next file.
106 rewind(files_[file_index_]);
107 file_index_ = (file_index_ + 1) % files_.size();
108 bytes_read = fread(frame_buffer_.get(), 1, frame_size_,
109 files_[file_index_]);
110 assert(bytes_read >= frame_size_);
111 }
112
nisse30116272016-09-16 05:45:08 -0700113 last_read_frame_.CreateEmptyFrame(
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000114 static_cast<int>(width_), static_cast<int>(height_),
nisse30116272016-09-16 05:45:08 -0700115 static_cast<int>(width_), static_cast<int>((width_ + 1) / 2),
116 static_cast<int>((width_ + 1) / 2));
117
118 ConvertToI420(kI420, frame_buffer_.get(), 0, 0, static_cast<int>(width_),
119 static_cast<int>(height_), 0, kVideoRotation_0,
120 &last_read_frame_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000121 }
122
123 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000124 size_t file_index_;
125 const std::vector<FILE*> files_;
126 const size_t width_;
127 const size_t height_;
128 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700129 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000130 const int frame_display_count_;
131 int current_display_count_;
nisse30116272016-09-16 05:45:08 -0700132 VideoFrame last_read_frame_;
133 VideoFrame temp_frame_copy_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000134};
sprangd6358952015-07-29 07:58:13 -0700135
136class ScrollingImageFrameGenerator : public FrameGenerator {
137 public:
138 ScrollingImageFrameGenerator(Clock* clock,
139 const std::vector<FILE*>& files,
140 size_t source_width,
141 size_t source_height,
142 size_t target_width,
143 size_t target_height,
144 int64_t scroll_time_ms,
145 int64_t pause_time_ms)
146 : clock_(clock),
147 start_time_(clock->TimeInMilliseconds()),
148 scroll_time_(scroll_time_ms),
149 pause_time_(pause_time_ms),
150 num_frames_(files.size()),
151 current_frame_num_(num_frames_ - 1),
152 current_source_frame_(nullptr),
153 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700154 RTC_DCHECK(clock_ != nullptr);
155 RTC_DCHECK_GT(num_frames_, 0u);
156 RTC_DCHECK_GE(source_height, target_height);
157 RTC_DCHECK_GE(source_width, target_width);
158 RTC_DCHECK_GE(scroll_time_ms, 0);
159 RTC_DCHECK_GE(pause_time_ms, 0);
160 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700161 current_frame_.CreateEmptyFrame(static_cast<int>(target_width),
162 static_cast<int>(target_height),
163 static_cast<int>(target_width),
164 static_cast<int>((target_width + 1) / 2),
165 static_cast<int>((target_width + 1) / 2));
166 }
167
168 virtual ~ScrollingImageFrameGenerator() {}
169
170 VideoFrame* NextFrame() override {
171 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
172 const int64_t now = clock_->TimeInMilliseconds();
173 int64_t ms_since_start = now - start_time_;
174
175 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
176 UpdateSourceFrame(frame_num);
177
178 double scroll_factor;
179 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
180 if (time_into_frame < scroll_time_) {
181 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
182 } else {
183 scroll_factor = 1.0;
184 }
185 CropSourceToScrolledImage(scroll_factor);
186
187 return &current_frame_;
188 }
189
190 void UpdateSourceFrame(size_t frame_num) {
191 while (current_frame_num_ != frame_num) {
192 current_source_frame_ = file_generator_.NextFrame();
193 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
194 }
henrikg91d6ede2015-09-17 00:24:34 -0700195 RTC_DCHECK(current_source_frame_ != nullptr);
sprangd6358952015-07-29 07:58:13 -0700196 }
197
198 void CropSourceToScrolledImage(double scroll_factor) {
199 const int kTargetWidth = current_frame_.width();
200 const int kTargetHeight = current_frame_.height();
201 int scroll_margin_x = current_source_frame_->width() - kTargetWidth;
202 int pixels_scrolled_x =
203 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
204 int scroll_margin_y = current_source_frame_->height() - kTargetHeight;
205 int pixels_scrolled_y =
206 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
207
nissec9c142f2016-05-17 04:05:47 -0700208 int offset_y = (current_source_frame_->video_frame_buffer()->StrideY() *
sprangd6358952015-07-29 07:58:13 -0700209 pixels_scrolled_y) +
210 pixels_scrolled_x;
nissec9c142f2016-05-17 04:05:47 -0700211 int offset_u = (current_source_frame_->video_frame_buffer()->StrideU() *
sprangd6358952015-07-29 07:58:13 -0700212 (pixels_scrolled_y / 2)) +
213 (pixels_scrolled_x / 2);
nissec9c142f2016-05-17 04:05:47 -0700214 int offset_v = (current_source_frame_->video_frame_buffer()->StrideV() *
sprangd6358952015-07-29 07:58:13 -0700215 (pixels_scrolled_y / 2)) +
216 (pixels_scrolled_x / 2);
217
218 current_frame_.CreateFrame(
nissec9c142f2016-05-17 04:05:47 -0700219 &current_source_frame_->video_frame_buffer()->DataY()[offset_y],
220 &current_source_frame_->video_frame_buffer()->DataU()[offset_u],
221 &current_source_frame_->video_frame_buffer()->DataV()[offset_v],
sprangd6358952015-07-29 07:58:13 -0700222 kTargetWidth, kTargetHeight,
nissec9c142f2016-05-17 04:05:47 -0700223 current_source_frame_->video_frame_buffer()->StrideY(),
224 current_source_frame_->video_frame_buffer()->StrideU(),
225 current_source_frame_->video_frame_buffer()->StrideV(),
Niels Möller739fcb92016-02-29 13:11:45 +0100226 kVideoRotation_0);
sprangd6358952015-07-29 07:58:13 -0700227 }
228
229 Clock* const clock_;
230 const int64_t start_time_;
231 const int64_t scroll_time_;
232 const int64_t pause_time_;
233 const size_t num_frames_;
234 size_t current_frame_num_;
235 VideoFrame* current_source_frame_;
236 VideoFrame current_frame_;
237 YuvFileGenerator file_generator_;
238};
239
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000240} // namespace
241
perkja49cbd32016-09-16 07:53:41 -0700242FrameForwarder::FrameForwarder() : sink_(nullptr) {}
243
244void FrameForwarder::IncomingCapturedFrame(const VideoFrame& video_frame) {
245 rtc::CritScope lock(&crit_);
246 if (sink_)
247 sink_->OnFrame(video_frame);
248}
249
250void FrameForwarder::AddOrUpdateSink(rtc::VideoSinkInterface<VideoFrame>* sink,
251 const rtc::VideoSinkWants& wants) {
252 rtc::CritScope lock(&crit_);
253 RTC_DCHECK(!sink_ || sink_ == sink);
254 sink_ = sink;
255}
256
257void FrameForwarder::RemoveSink(rtc::VideoSinkInterface<VideoFrame>* sink) {
258 rtc::CritScope lock(&crit_);
259 RTC_DCHECK_EQ(sink, sink_);
260 sink_ = nullptr;
261}
262
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000263FrameGenerator* FrameGenerator::CreateChromaGenerator(size_t width,
264 size_t height) {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000265 return new ChromaGenerator(width, height);
266}
267
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000268FrameGenerator* FrameGenerator::CreateFromYuvFile(
269 std::vector<std::string> filenames,
270 size_t width,
271 size_t height,
272 int frame_repeat_count) {
273 assert(!filenames.empty());
274 std::vector<FILE*> files;
275 for (const std::string& filename : filenames) {
276 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700277 RTC_DCHECK(file != nullptr);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000278 files.push_back(file);
279 }
280
281 return new YuvFileGenerator(files, width, height, frame_repeat_count);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000282}
283
sprangd6358952015-07-29 07:58:13 -0700284FrameGenerator* FrameGenerator::CreateScrollingInputFromYuvFiles(
285 Clock* clock,
286 std::vector<std::string> filenames,
287 size_t source_width,
288 size_t source_height,
289 size_t target_width,
290 size_t target_height,
291 int64_t scroll_time_ms,
292 int64_t pause_time_ms) {
293 assert(!filenames.empty());
294 std::vector<FILE*> files;
295 for (const std::string& filename : filenames) {
296 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700297 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700298 files.push_back(file);
299 }
300
301 return new ScrollingImageFrameGenerator(
302 clock, files, source_width, source_height, target_width, target_height,
303 scroll_time_ms, pause_time_ms);
304}
305
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000306} // namespace test
307} // namespace webrtc