blob: 4da909d669ba899120d34a977a81249baefa472e [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)
29 : angle_(0.0), width_(width), height_(height) {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000030 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 {
pbos@webrtc.org724947b2013-12-11 16:26:16 +000035 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
nisse5b3c4432016-04-29 02:39:24 -070044 memset(frame_.buffer(kYPlane), 0x80, frame_.allocated_size(kYPlane));
45 memset(frame_.buffer(kUPlane), u, frame_.allocated_size(kUPlane));
46 memset(frame_.buffer(kVPlane), v, frame_.allocated_size(kVPlane));
pbos@webrtc.org724947b2013-12-11 16:26:16 +000047 return &frame_;
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000048 }
49
50 private:
51 double angle_;
pbos@webrtc.org724947b2013-12-11 16:26:16 +000052 size_t width_;
53 size_t height_;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070054 VideoFrame frame_;
pbos@webrtc.org266c7b32013-10-15 09:15:47 +000055};
56
andresp@webrtc.orgab654952013-09-19 12:14:03 +000057class YuvFileGenerator : public FrameGenerator {
58 public:
sprang@webrtc.org131bea82015-02-18 12:46:06 +000059 YuvFileGenerator(std::vector<FILE*> files,
60 size_t width,
61 size_t height,
62 int frame_repeat_count)
63 : file_index_(0),
64 files_(files),
65 width_(width),
66 height_(height),
67 frame_size_(CalcBufferSize(kI420,
68 static_cast<int>(width_),
69 static_cast<int>(height_))),
70 frame_buffer_(new uint8_t[frame_size_]),
71 frame_display_count_(frame_repeat_count),
72 current_display_count_(0) {
andresp@webrtc.orgab654952013-09-19 12:14:03 +000073 assert(width > 0);
74 assert(height > 0);
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000075 assert(frame_repeat_count > 0);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000076 }
77
78 virtual ~YuvFileGenerator() {
sprang@webrtc.org131bea82015-02-18 12:46:06 +000079 for (FILE* file : files_)
80 fclose(file);
andresp@webrtc.orgab654952013-09-19 12:14:03 +000081 }
82
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -070083 VideoFrame* NextFrame() override {
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000084 if (current_display_count_ == 0)
85 ReadNextFrame();
86 if (++current_display_count_ >= frame_display_count_)
87 current_display_count_ = 0;
andresp@webrtc.orgab654952013-09-19 12:14:03 +000088
sprang@webrtc.org25dd1db2015-03-02 11:55:45 +000089 // If this is the last repeatition of this frame, it's OK to use the
90 // original instance, otherwise use a copy.
91 if (current_display_count_ == frame_display_count_)
92 return &last_read_frame_;
93
94 temp_frame_copy_.CopyFrame(last_read_frame_);
95 return &temp_frame_copy_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +000096 }
pbos@webrtc.org724947b2013-12-11 16:26:16 +000097
sprang@webrtc.org131bea82015-02-18 12:46:06 +000098 void ReadNextFrame() {
99 size_t bytes_read =
100 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]);
101 if (bytes_read < frame_size_) {
102 // No more frames to read in this file, rewind and move to next file.
103 rewind(files_[file_index_]);
104 file_index_ = (file_index_ + 1) % files_.size();
105 bytes_read = fread(frame_buffer_.get(), 1, frame_size_,
106 files_[file_index_]);
107 assert(bytes_read >= frame_size_);
108 }
109
110 last_read_frame_.CreateEmptyFrame(
111 static_cast<int>(width_), static_cast<int>(height_),
112 static_cast<int>(width_), static_cast<int>((width_ + 1) / 2),
113 static_cast<int>((width_ + 1) / 2));
114
115 ConvertToI420(kI420, frame_buffer_.get(), 0, 0, static_cast<int>(width_),
guoweis@webrtc.org59140d62015-03-09 17:07:31 +0000116 static_cast<int>(height_), 0, kVideoRotation_0,
117 &last_read_frame_);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000118 }
119
120 private:
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000121 size_t file_index_;
122 const std::vector<FILE*> files_;
123 const size_t width_;
124 const size_t height_;
125 const size_t frame_size_;
kwibergbfefb032016-05-01 14:53:46 -0700126 const std::unique_ptr<uint8_t[]> frame_buffer_;
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000127 const int frame_display_count_;
128 int current_display_count_;
Miguel Casas-Sanchez47650702015-05-29 17:21:40 -0700129 VideoFrame last_read_frame_;
130 VideoFrame temp_frame_copy_;
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000131};
sprangd6358952015-07-29 07:58:13 -0700132
133class ScrollingImageFrameGenerator : public FrameGenerator {
134 public:
135 ScrollingImageFrameGenerator(Clock* clock,
136 const std::vector<FILE*>& files,
137 size_t source_width,
138 size_t source_height,
139 size_t target_width,
140 size_t target_height,
141 int64_t scroll_time_ms,
142 int64_t pause_time_ms)
143 : clock_(clock),
144 start_time_(clock->TimeInMilliseconds()),
145 scroll_time_(scroll_time_ms),
146 pause_time_(pause_time_ms),
147 num_frames_(files.size()),
148 current_frame_num_(num_frames_ - 1),
149 current_source_frame_(nullptr),
150 file_generator_(files, source_width, source_height, 1) {
henrikg91d6ede2015-09-17 00:24:34 -0700151 RTC_DCHECK(clock_ != nullptr);
152 RTC_DCHECK_GT(num_frames_, 0u);
153 RTC_DCHECK_GE(source_height, target_height);
154 RTC_DCHECK_GE(source_width, target_width);
155 RTC_DCHECK_GE(scroll_time_ms, 0);
156 RTC_DCHECK_GE(pause_time_ms, 0);
157 RTC_DCHECK_GT(scroll_time_ms + pause_time_ms, 0);
sprangd6358952015-07-29 07:58:13 -0700158 current_frame_.CreateEmptyFrame(static_cast<int>(target_width),
159 static_cast<int>(target_height),
160 static_cast<int>(target_width),
161 static_cast<int>((target_width + 1) / 2),
162 static_cast<int>((target_width + 1) / 2));
163 }
164
165 virtual ~ScrollingImageFrameGenerator() {}
166
167 VideoFrame* NextFrame() override {
168 const int64_t kFrameDisplayTime = scroll_time_ + pause_time_;
169 const int64_t now = clock_->TimeInMilliseconds();
170 int64_t ms_since_start = now - start_time_;
171
172 size_t frame_num = (ms_since_start / kFrameDisplayTime) % num_frames_;
173 UpdateSourceFrame(frame_num);
174
175 double scroll_factor;
176 int64_t time_into_frame = ms_since_start % kFrameDisplayTime;
177 if (time_into_frame < scroll_time_) {
178 scroll_factor = static_cast<double>(time_into_frame) / scroll_time_;
179 } else {
180 scroll_factor = 1.0;
181 }
182 CropSourceToScrolledImage(scroll_factor);
183
184 return &current_frame_;
185 }
186
187 void UpdateSourceFrame(size_t frame_num) {
188 while (current_frame_num_ != frame_num) {
189 current_source_frame_ = file_generator_.NextFrame();
190 current_frame_num_ = (current_frame_num_ + 1) % num_frames_;
191 }
henrikg91d6ede2015-09-17 00:24:34 -0700192 RTC_DCHECK(current_source_frame_ != nullptr);
sprangd6358952015-07-29 07:58:13 -0700193 }
194
195 void CropSourceToScrolledImage(double scroll_factor) {
196 const int kTargetWidth = current_frame_.width();
197 const int kTargetHeight = current_frame_.height();
198 int scroll_margin_x = current_source_frame_->width() - kTargetWidth;
199 int pixels_scrolled_x =
200 static_cast<int>(scroll_margin_x * scroll_factor + 0.5);
201 int scroll_margin_y = current_source_frame_->height() - kTargetHeight;
202 int pixels_scrolled_y =
203 static_cast<int>(scroll_margin_y * scroll_factor + 0.5);
204
nisse5b3c4432016-04-29 02:39:24 -0700205 int offset_y = (current_source_frame_->stride(PlaneType::kYPlane) *
sprangd6358952015-07-29 07:58:13 -0700206 pixels_scrolled_y) +
207 pixels_scrolled_x;
nisse5b3c4432016-04-29 02:39:24 -0700208 int offset_u = (current_source_frame_->stride(PlaneType::kUPlane) *
sprangd6358952015-07-29 07:58:13 -0700209 (pixels_scrolled_y / 2)) +
210 (pixels_scrolled_x / 2);
nisse5b3c4432016-04-29 02:39:24 -0700211 int offset_v = (current_source_frame_->stride(PlaneType::kVPlane) *
sprangd6358952015-07-29 07:58:13 -0700212 (pixels_scrolled_y / 2)) +
213 (pixels_scrolled_x / 2);
214
215 current_frame_.CreateFrame(
nisse5b3c4432016-04-29 02:39:24 -0700216 &current_source_frame_->buffer(PlaneType::kYPlane)[offset_y],
217 &current_source_frame_->buffer(PlaneType::kUPlane)[offset_u],
218 &current_source_frame_->buffer(PlaneType::kVPlane)[offset_v],
sprangd6358952015-07-29 07:58:13 -0700219 kTargetWidth, kTargetHeight,
nisse5b3c4432016-04-29 02:39:24 -0700220 current_source_frame_->stride(PlaneType::kYPlane),
221 current_source_frame_->stride(PlaneType::kUPlane),
222 current_source_frame_->stride(PlaneType::kVPlane),
Niels Möller739fcb92016-02-29 13:11:45 +0100223 kVideoRotation_0);
sprangd6358952015-07-29 07:58:13 -0700224 }
225
226 Clock* const clock_;
227 const int64_t start_time_;
228 const int64_t scroll_time_;
229 const int64_t pause_time_;
230 const size_t num_frames_;
231 size_t current_frame_num_;
232 VideoFrame* current_source_frame_;
233 VideoFrame current_frame_;
234 YuvFileGenerator file_generator_;
235};
236
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000237} // namespace
238
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000239FrameGenerator* FrameGenerator::CreateChromaGenerator(size_t width,
240 size_t height) {
pbos@webrtc.org266c7b32013-10-15 09:15:47 +0000241 return new ChromaGenerator(width, height);
242}
243
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000244FrameGenerator* FrameGenerator::CreateFromYuvFile(
245 std::vector<std::string> filenames,
246 size_t width,
247 size_t height,
248 int frame_repeat_count) {
249 assert(!filenames.empty());
250 std::vector<FILE*> files;
251 for (const std::string& filename : filenames) {
252 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700253 RTC_DCHECK(file != nullptr);
sprang@webrtc.org131bea82015-02-18 12:46:06 +0000254 files.push_back(file);
255 }
256
257 return new YuvFileGenerator(files, width, height, frame_repeat_count);
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000258}
259
sprangd6358952015-07-29 07:58:13 -0700260FrameGenerator* FrameGenerator::CreateScrollingInputFromYuvFiles(
261 Clock* clock,
262 std::vector<std::string> filenames,
263 size_t source_width,
264 size_t source_height,
265 size_t target_width,
266 size_t target_height,
267 int64_t scroll_time_ms,
268 int64_t pause_time_ms) {
269 assert(!filenames.empty());
270 std::vector<FILE*> files;
271 for (const std::string& filename : filenames) {
272 FILE* file = fopen(filename.c_str(), "rb");
henrikg91d6ede2015-09-17 00:24:34 -0700273 RTC_DCHECK(file != nullptr);
sprangd6358952015-07-29 07:58:13 -0700274 files.push_back(file);
275 }
276
277 return new ScrollingImageFrameGenerator(
278 clock, files, source_width, source_height, target_width, target_height,
279 scroll_time_ms, pause_time_ms);
280}
281
andresp@webrtc.orgab654952013-09-19 12:14:03 +0000282} // namespace test
283} // namespace webrtc