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) : angle_(0.0) { |
29 : angle_(0.0), width_(width), height_(height) { | |
30 assert(width > 0); | 30 assert(width > 0); |
31 assert(height > 0); | 31 assert(height > 0); |
32 size_t half_width = (width + 1) / 2; | |
33 y_size_ = width * height; | |
34 uv_size_ = half_width * ((height + 1) / 2); | |
35 | |
36 // Ensure stride == width. | |
37 buffer_ = I420Buffer::Create( | |
38 static_cast<int>(width), static_cast<int>(height), | |
39 static_cast<int>(width), static_cast<int>(half_width), | |
40 static_cast<int>(half_width)); | |
nisse-webrtc
2016/09/16 05:58:33
This always keeps the same buffer. That doesn't se
| |
32 } | 41 } |
33 | 42 |
34 VideoFrame* NextFrame() override { | 43 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; | 44 angle_ += 30.0; |
41 uint8_t u = fabs(sin(angle_)) * 0xFF; | 45 uint8_t u = fabs(sin(angle_)) * 0xFF; |
42 uint8_t v = fabs(cos(angle_)) * 0xFF; | 46 uint8_t v = fabs(cos(angle_)) * 0xFF; |
43 | 47 |
44 memset(frame_.video_frame_buffer()->MutableDataY(), 0x80, | 48 memset(buffer_->MutableDataY(), 0x80, y_size_); |
45 frame_.allocated_size(kYPlane)); | 49 memset(buffer_->MutableDataU(), u, uv_size_); |
46 memset(frame_.video_frame_buffer()->MutableDataU(), u, | 50 memset(buffer_->MutableDataV(), v, uv_size_); |
47 frame_.allocated_size(kUPlane)); | 51 |
48 memset(frame_.video_frame_buffer()->MutableDataV(), v, | 52 frame_.reset(new VideoFrame(buffer_, 0, 0, webrtc::kVideoRotation_0)); |
49 frame_.allocated_size(kVPlane)); | 53 return frame_.get(); |
50 return &frame_; | |
51 } | 54 } |
52 | 55 |
53 private: | 56 private: |
54 double angle_; | 57 double angle_; |
55 size_t width_; | 58 size_t y_size_; |
56 size_t height_; | 59 size_t uv_size_; |
57 VideoFrame frame_; | 60 rtc::scoped_refptr<I420Buffer> buffer_; |
61 std::unique_ptr<VideoFrame> frame_; | |
58 }; | 62 }; |
59 | 63 |
60 class YuvFileGenerator : public FrameGenerator { | 64 class YuvFileGenerator : public FrameGenerator { |
61 public: | 65 public: |
62 YuvFileGenerator(std::vector<FILE*> files, | 66 YuvFileGenerator(std::vector<FILE*> files, |
63 size_t width, | 67 size_t width, |
64 size_t height, | 68 size_t height, |
65 int frame_repeat_count) | 69 int frame_repeat_count) |
66 : file_index_(0), | 70 : file_index_(0), |
67 files_(files), | 71 files_(files), |
(...skipping 14 matching lines...) Expand all Loading... | |
82 for (FILE* file : files_) | 86 for (FILE* file : files_) |
83 fclose(file); | 87 fclose(file); |
84 } | 88 } |
85 | 89 |
86 VideoFrame* NextFrame() override { | 90 VideoFrame* NextFrame() override { |
87 if (current_display_count_ == 0) | 91 if (current_display_count_ == 0) |
88 ReadNextFrame(); | 92 ReadNextFrame(); |
89 if (++current_display_count_ >= frame_display_count_) | 93 if (++current_display_count_ >= frame_display_count_) |
90 current_display_count_ = 0; | 94 current_display_count_ = 0; |
91 | 95 |
92 // If this is the last repeatition of this frame, it's OK to use the | 96 temp_frame_.reset( |
93 // original instance, otherwise use a copy. | 97 new VideoFrame(last_read_buffer_, 0, 0, webrtc::kVideoRotation_0)); |
94 if (current_display_count_ == frame_display_count_) | 98 return temp_frame_.get(); |
95 return &last_read_frame_; | |
96 | |
97 temp_frame_copy_.CopyFrame(last_read_frame_); | |
98 return &temp_frame_copy_; | |
99 } | 99 } |
100 | 100 |
101 // TODO(nisse): Have a frame reader in one place. And read directly | |
102 // into the planes of an I420Buffer, the extra copying below is silly. | |
101 void ReadNextFrame() { | 103 void ReadNextFrame() { |
102 size_t bytes_read = | 104 size_t bytes_read = |
103 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]); | 105 fread(frame_buffer_.get(), 1, frame_size_, files_[file_index_]); |
104 if (bytes_read < frame_size_) { | 106 if (bytes_read < frame_size_) { |
105 // No more frames to read in this file, rewind and move to next file. | 107 // No more frames to read in this file, rewind and move to next file. |
106 rewind(files_[file_index_]); | 108 rewind(files_[file_index_]); |
107 file_index_ = (file_index_ + 1) % files_.size(); | 109 file_index_ = (file_index_ + 1) % files_.size(); |
108 bytes_read = fread(frame_buffer_.get(), 1, frame_size_, | 110 bytes_read = fread(frame_buffer_.get(), 1, frame_size_, |
109 files_[file_index_]); | 111 files_[file_index_]); |
110 assert(bytes_read >= frame_size_); | 112 assert(bytes_read >= frame_size_); |
111 } | 113 } |
112 | 114 |
113 last_read_frame_.CreateEmptyFrame( | 115 size_t half_width = (width_ + 1) / 2; |
116 size_t size_y = width_ * height_; | |
117 size_t size_uv = half_width * ((height_ + 1) / 2); | |
118 last_read_buffer_ = I420Buffer::Create( | |
114 static_cast<int>(width_), static_cast<int>(height_), | 119 static_cast<int>(width_), static_cast<int>(height_), |
115 static_cast<int>(width_), static_cast<int>((width_ + 1) / 2), | 120 static_cast<int>(width_), static_cast<int>(half_width), |
116 static_cast<int>((width_ + 1) / 2)); | 121 static_cast<int>(half_width)); |
117 | 122 libyuv::I420Copy( |
118 ConvertToI420(kI420, frame_buffer_.get(), 0, 0, static_cast<int>(width_), | 123 frame_buffer_.get(), static_cast<int>(width_), |
119 static_cast<int>(height_), 0, kVideoRotation_0, | 124 frame_buffer_.get() + size_y , static_cast<int>(half_width), |
120 &last_read_frame_); | 125 frame_buffer_.get() + size_y + size_uv, static_cast<int>(half_width), |
126 last_read_buffer_->MutableDataY(), last_read_buffer_->StrideY(), | |
127 last_read_buffer_->MutableDataU(), last_read_buffer_->StrideU(), | |
128 last_read_buffer_->MutableDataV(), last_read_buffer_->StrideV(), | |
129 static_cast<int>(width_), static_cast<int>(height_)); | |
121 } | 130 } |
122 | 131 |
123 private: | 132 private: |
124 size_t file_index_; | 133 size_t file_index_; |
125 const std::vector<FILE*> files_; | 134 const std::vector<FILE*> files_; |
126 const size_t width_; | 135 const size_t width_; |
127 const size_t height_; | 136 const size_t height_; |
128 const size_t frame_size_; | 137 const size_t frame_size_; |
129 const std::unique_ptr<uint8_t[]> frame_buffer_; | 138 const std::unique_ptr<uint8_t[]> frame_buffer_; |
130 const int frame_display_count_; | 139 const int frame_display_count_; |
131 int current_display_count_; | 140 int current_display_count_; |
132 VideoFrame last_read_frame_; | 141 rtc::scoped_refptr<I420Buffer> last_read_buffer_; |
133 VideoFrame temp_frame_copy_; | 142 std::unique_ptr<VideoFrame> temp_frame_; |
134 }; | 143 }; |
135 | 144 |
136 class ScrollingImageFrameGenerator : public FrameGenerator { | 145 class ScrollingImageFrameGenerator : public FrameGenerator { |
137 public: | 146 public: |
138 ScrollingImageFrameGenerator(Clock* clock, | 147 ScrollingImageFrameGenerator(Clock* clock, |
139 const std::vector<FILE*>& files, | 148 const std::vector<FILE*>& files, |
140 size_t source_width, | 149 size_t source_width, |
141 size_t source_height, | 150 size_t source_height, |
142 size_t target_width, | 151 size_t target_width, |
143 size_t target_height, | 152 size_t target_height, |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
277 files.push_back(file); | 286 files.push_back(file); |
278 } | 287 } |
279 | 288 |
280 return new ScrollingImageFrameGenerator( | 289 return new ScrollingImageFrameGenerator( |
281 clock, files, source_width, source_height, target_width, target_height, | 290 clock, files, source_width, source_height, target_width, target_height, |
282 scroll_time_ms, pause_time_ms); | 291 scroll_time_ms, pause_time_ms); |
283 } | 292 } |
284 | 293 |
285 } // namespace test | 294 } // namespace test |
286 } // namespace webrtc | 295 } // namespace webrtc |
OLD | NEW |