OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 #include "webrtc/test/frame_generator.h" | 10 #include "webrtc/test/frame_generator.h" |
11 | 11 |
12 #include <math.h> | 12 #include <math.h> |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <string.h> | 14 #include <string.h> |
15 | 15 |
16 #include <memory> | 16 #include <memory> |
17 | 17 |
18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
19 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" | 19 #include "webrtc/common_video/libyuv/include/webrtc_libyuv.h" |
20 #include "webrtc/system_wrappers/include/clock.h" | 20 #include "webrtc/system_wrappers/include/clock.h" |
| 21 #include "libyuv/convert.h" |
21 | 22 |
22 namespace webrtc { | 23 namespace webrtc { |
23 namespace test { | 24 namespace test { |
24 namespace { | 25 namespace { |
25 | 26 |
26 class ChromaGenerator : public FrameGenerator { | 27 class ChromaGenerator : public FrameGenerator { |
27 public: | 28 public: |
28 ChromaGenerator(size_t width, size_t height) | 29 ChromaGenerator(size_t width, size_t height) |
29 : angle_(0.0), width_(width), height_(height) { | 30 : angle_(0.0), width_(width), height_(height) { |
30 assert(width > 0); | 31 RTC_CHECK(width_ > 0); |
31 assert(height > 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); |
32 } | 36 } |
33 | 37 |
34 VideoFrame* NextFrame() override { | 38 VideoFrame* NextFrame() override { |
35 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)); | |
40 angle_ += 30.0; | 39 angle_ += 30.0; |
41 uint8_t u = fabs(sin(angle_)) * 0xFF; | 40 uint8_t u = fabs(sin(angle_)) * 0xFF; |
42 uint8_t v = fabs(cos(angle_)) * 0xFF; | 41 uint8_t v = fabs(cos(angle_)) * 0xFF; |
43 | 42 |
44 memset(frame_.video_frame_buffer()->MutableDataY(), 0x80, | 43 // Ensure stride == width. |
45 frame_.allocated_size(kYPlane)); | 44 rtc::scoped_refptr<I420Buffer> buffer(I420Buffer::Create( |
46 memset(frame_.video_frame_buffer()->MutableDataU(), u, | 45 static_cast<int>(width_), static_cast<int>(height_), |
47 frame_.allocated_size(kUPlane)); | 46 static_cast<int>(width_), static_cast<int>(half_width_), |
48 memset(frame_.video_frame_buffer()->MutableDataV(), v, | 47 static_cast<int>(half_width_))); |
49 frame_.allocated_size(kVPlane)); | 48 |
50 return &frame_; | 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(); |
51 } | 55 } |
52 | 56 |
53 private: | 57 private: |
54 double angle_; | 58 double angle_; |
55 size_t width_; | 59 size_t width_; |
56 size_t height_; | 60 size_t height_; |
57 VideoFrame frame_; | 61 size_t half_width_; |
| 62 size_t y_size_; |
| 63 size_t uv_size_; |
| 64 std::unique_ptr<VideoFrame> frame_; |
58 }; | 65 }; |
59 | 66 |
60 class YuvFileGenerator : public FrameGenerator { | 67 class YuvFileGenerator : public FrameGenerator { |
61 public: | 68 public: |
62 YuvFileGenerator(std::vector<FILE*> files, | 69 YuvFileGenerator(std::vector<FILE*> files, |
63 size_t width, | 70 size_t width, |
64 size_t height, | 71 size_t height, |
65 int frame_repeat_count) | 72 int frame_repeat_count) |
66 : file_index_(0), | 73 : file_index_(0), |
67 files_(files), | 74 files_(files), |
(...skipping 14 matching lines...) Expand all Loading... |
82 for (FILE* file : files_) | 89 for (FILE* file : files_) |
83 fclose(file); | 90 fclose(file); |
84 } | 91 } |
85 | 92 |
86 VideoFrame* NextFrame() override { | 93 VideoFrame* NextFrame() override { |
87 if (current_display_count_ == 0) | 94 if (current_display_count_ == 0) |
88 ReadNextFrame(); | 95 ReadNextFrame(); |
89 if (++current_display_count_ >= frame_display_count_) | 96 if (++current_display_count_ >= frame_display_count_) |
90 current_display_count_ = 0; | 97 current_display_count_ = 0; |
91 | 98 |
92 // If this is the last repeatition of this frame, it's OK to use the | 99 temp_frame_.reset( |
93 // original instance, otherwise use a copy. | 100 new VideoFrame(last_read_buffer_, 0, 0, webrtc::kVideoRotation_0)); |
94 if (current_display_count_ == frame_display_count_) | 101 return temp_frame_.get(); |
95 return &last_read_frame_; | |
96 | |
97 temp_frame_copy_.CopyFrame(last_read_frame_); | |
98 return &temp_frame_copy_; | |
99 } | 102 } |
100 | 103 |
| 104 // 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. |
101 void ReadNextFrame() { | 106 void ReadNextFrame() { |
102 size_t bytes_read = | 107 size_t bytes_read = |
103 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]); | 108 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]); |
104 if (bytes_read < frame_size_) { | 109 if (bytes_read < frame_size_) { |
105 // No more frames to read in this file, rewind and move to next file. | 110 // No more frames to read in this file, rewind and move to next file. |
106 rewind(files_[file_index_]); | 111 rewind(files_[file_index_]); |
107 file_index_ = (file_index_ + 1) % files_.size(); | 112 file_index_ = (file_index_ + 1) % files_.size(); |
108 bytes_read = fread(frame_buffer_.get(), 1, frame_size_, | 113 bytes_read = fread(frame_buffer_.get(), 1, frame_size_, |
109 files_[file_index_]); | 114 files_[file_index_]); |
110 assert(bytes_read >= frame_size_); | 115 assert(bytes_read >= frame_size_); |
111 } | 116 } |
112 | 117 |
113 last_read_frame_.CreateEmptyFrame( | 118 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( |
114 static_cast<int>(width_), static_cast<int>(height_), | 122 static_cast<int>(width_), static_cast<int>(height_), |
115 static_cast<int>(width_), static_cast<int>((width_ + 1) / 2), | 123 static_cast<int>(width_), static_cast<int>(half_width), |
116 static_cast<int>((width_ + 1) / 2)); | 124 static_cast<int>(half_width)); |
117 | 125 libyuv::I420Copy( |
118 ConvertToI420(kI420, frame_buffer_.get(), 0, 0, static_cast<int>(width_), | 126 frame_buffer_.get(), static_cast<int>(width_), |
119 static_cast<int>(height_), 0, kVideoRotation_0, | 127 frame_buffer_.get() + size_y, static_cast<int>(half_width), |
120 &last_read_frame_); | 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_)); |
121 } | 133 } |
122 | 134 |
123 private: | 135 private: |
124 size_t file_index_; | 136 size_t file_index_; |
125 const std::vector<FILE*> files_; | 137 const std::vector<FILE*> files_; |
126 const size_t width_; | 138 const size_t width_; |
127 const size_t height_; | 139 const size_t height_; |
128 const size_t frame_size_; | 140 const size_t frame_size_; |
129 const std::unique_ptr<uint8_t[]> frame_buffer_; | 141 const std::unique_ptr<uint8_t[]> frame_buffer_; |
130 const int frame_display_count_; | 142 const int frame_display_count_; |
131 int current_display_count_; | 143 int current_display_count_; |
132 VideoFrame last_read_frame_; | 144 rtc::scoped_refptr<I420Buffer> last_read_buffer_; |
133 VideoFrame temp_frame_copy_; | 145 std::unique_ptr<VideoFrame> temp_frame_; |
134 }; | 146 }; |
135 | 147 |
136 class ScrollingImageFrameGenerator : public FrameGenerator { | 148 class ScrollingImageFrameGenerator : public FrameGenerator { |
137 public: | 149 public: |
138 ScrollingImageFrameGenerator(Clock* clock, | 150 ScrollingImageFrameGenerator(Clock* clock, |
139 const std::vector<FILE*>& files, | 151 const std::vector<FILE*>& files, |
140 size_t source_width, | 152 size_t source_width, |
141 size_t source_height, | 153 size_t source_height, |
142 size_t target_width, | 154 size_t target_width, |
143 size_t target_height, | 155 size_t target_height, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 files.push_back(file); | 289 files.push_back(file); |
278 } | 290 } |
279 | 291 |
280 return new ScrollingImageFrameGenerator( | 292 return new ScrollingImageFrameGenerator( |
281 clock, files, source_width, source_height, target_width, target_height, | 293 clock, files, source_width, source_height, target_width, target_height, |
282 scroll_time_ms, pause_time_ms); | 294 scroll_time_ms, pause_time_ms); |
283 } | 295 } |
284 | 296 |
285 } // namespace test | 297 } // namespace test |
286 } // namespace webrtc | 298 } // namespace webrtc |
OLD | NEW |