OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/codec/video_encoder_vp8.h" | 5 #include "remoting/codec/video_encoder_vp8.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/sys_info.h" | 8 #include "base/sys_info.h" |
9 #include "base/time/time.h" | 9 #include "base/time/time.h" |
10 #include "media/base/yuv_convert.h" | 10 #include "media/base/yuv_convert.h" |
11 #include "remoting/base/util.h" | 11 #include "remoting/base/util.h" |
12 #include "remoting/proto/video.pb.h" | 12 #include "remoting/proto/video.pb.h" |
13 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 13 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
14 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" | 14 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h" |
| 15 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" |
15 | 16 |
16 extern "C" { | 17 extern "C" { |
17 #define VPX_CODEC_DISABLE_COMPAT 1 | 18 #define VPX_CODEC_DISABLE_COMPAT 1 |
18 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h" | 19 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h" |
19 #include "third_party/libvpx/source/libvpx/vpx/vp8cx.h" | 20 #include "third_party/libvpx/source/libvpx/vpx/vp8cx.h" |
20 } | 21 } |
21 | 22 |
22 namespace { | 23 namespace { |
23 | 24 |
24 // Defines the dimension of a macro block. This is used to compute the active | 25 // Defines the dimension of a macro block. This is used to compute the active |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 return false; | 139 return false; |
139 | 140 |
140 // Use the lowest level of noise sensitivity so as to spend less time | 141 // Use the lowest level of noise sensitivity so as to spend less time |
141 // on motion estimation and inter-prediction mode. | 142 // on motion estimation and inter-prediction mode. |
142 if (vpx_codec_control(codec_.get(), VP8E_SET_NOISE_SENSITIVITY, 0)) | 143 if (vpx_codec_control(codec_.get(), VP8E_SET_NOISE_SENSITIVITY, 0)) |
143 return false; | 144 return false; |
144 return true; | 145 return true; |
145 } | 146 } |
146 | 147 |
147 void VideoEncoderVp8::PrepareImage(const webrtc::DesktopFrame& frame, | 148 void VideoEncoderVp8::PrepareImage(const webrtc::DesktopFrame& frame, |
148 SkRegion* updated_region) { | 149 webrtc::DesktopRegion* updated_region) { |
149 if (frame.updated_region().is_empty()) { | 150 if (frame.updated_region().is_empty()) { |
150 updated_region->setEmpty(); | 151 updated_region->Clear(); |
151 return; | 152 return; |
152 } | 153 } |
153 | 154 |
154 // Align the region to macroblocks, to avoid encoding artefacts. | 155 // Align the region to macroblocks, to avoid encoding artefacts. |
155 // This also ensures that all rectangles have even-aligned top-left, which | 156 // This also ensures that all rectangles have even-aligned top-left, which |
156 // is required for ConvertRGBToYUVWithRect() to work. | 157 // is required for ConvertRGBToYUVWithRect() to work. |
157 std::vector<SkIRect> aligned_rects; | 158 std::vector<webrtc::DesktopRect> aligned_rects; |
158 for (webrtc::DesktopRegion::Iterator r(frame.updated_region()); | 159 for (webrtc::DesktopRegion::Iterator r(frame.updated_region()); |
159 !r.IsAtEnd(); r.Advance()) { | 160 !r.IsAtEnd(); r.Advance()) { |
160 const webrtc::DesktopRect& rect = r.rect(); | 161 const webrtc::DesktopRect& rect = r.rect(); |
161 aligned_rects.push_back(AlignRect( | 162 aligned_rects.push_back(AlignRect(webrtc::DesktopRect::MakeLTRB( |
162 SkIRect::MakeLTRB(rect.left(), rect.top(), rect.right(), rect.bottom()))); | 163 rect.left(), rect.top(), rect.right(), rect.bottom()))); |
163 } | 164 } |
164 DCHECK(!aligned_rects.empty()); | 165 DCHECK(!aligned_rects.empty()); |
165 updated_region->setRects(&aligned_rects[0], aligned_rects.size()); | 166 updated_region->Clear(); |
| 167 updated_region->AddRects(&aligned_rects[0], aligned_rects.size()); |
166 | 168 |
167 // Clip back to the screen dimensions, in case they're not macroblock aligned. | 169 // Clip back to the screen dimensions, in case they're not macroblock aligned. |
168 // The conversion routines don't require even width & height, so this is safe | 170 // The conversion routines don't require even width & height, so this is safe |
169 // even if the source dimensions are not even. | 171 // even if the source dimensions are not even. |
170 updated_region->op(SkIRect::MakeWH(image_->w, image_->h), | 172 updated_region->IntersectWith( |
171 SkRegion::kIntersect_Op); | 173 webrtc::DesktopRect::MakeWH(image_->w, image_->h)); |
172 | 174 |
173 // Convert the updated region to YUV ready for encoding. | 175 // Convert the updated region to YUV ready for encoding. |
174 const uint8* rgb_data = frame.data(); | 176 const uint8* rgb_data = frame.data(); |
175 const int rgb_stride = frame.stride(); | 177 const int rgb_stride = frame.stride(); |
176 const int y_stride = image_->stride[0]; | 178 const int y_stride = image_->stride[0]; |
177 DCHECK_EQ(image_->stride[1], image_->stride[2]); | 179 DCHECK_EQ(image_->stride[1], image_->stride[2]); |
178 const int uv_stride = image_->stride[1]; | 180 const int uv_stride = image_->stride[1]; |
179 uint8* y_data = image_->planes[0]; | 181 uint8* y_data = image_->planes[0]; |
180 uint8* u_data = image_->planes[1]; | 182 uint8* u_data = image_->planes[1]; |
181 uint8* v_data = image_->planes[2]; | 183 uint8* v_data = image_->planes[2]; |
182 for (SkRegion::Iterator r(*updated_region); !r.done(); r.next()) { | 184 for (webrtc::DesktopRegion::Iterator r(*updated_region); !r.IsAtEnd(); |
183 const SkIRect& rect = r.rect(); | 185 r.Advance()) { |
| 186 const webrtc::DesktopRect& rect = r.rect(); |
184 ConvertRGB32ToYUVWithRect( | 187 ConvertRGB32ToYUVWithRect( |
185 rgb_data, y_data, u_data, v_data, | 188 rgb_data, y_data, u_data, v_data, |
186 rect.x(), rect.y(), rect.width(), rect.height(), | 189 rect.left(), rect.top(), rect.width(), rect.height(), |
187 rgb_stride, y_stride, uv_stride); | 190 rgb_stride, y_stride, uv_stride); |
188 } | 191 } |
189 } | 192 } |
190 | 193 |
191 void VideoEncoderVp8::PrepareActiveMap(const SkRegion& updated_region) { | 194 void VideoEncoderVp8::PrepareActiveMap( |
| 195 const webrtc::DesktopRegion& updated_region) { |
192 // Clear active map first. | 196 // Clear active map first. |
193 memset(active_map_.get(), 0, active_map_width_ * active_map_height_); | 197 memset(active_map_.get(), 0, active_map_width_ * active_map_height_); |
194 | 198 |
195 // Mark updated areas active. | 199 // Mark updated areas active. |
196 for (SkRegion::Iterator r(updated_region); !r.done(); r.next()) { | 200 for (webrtc::DesktopRegion::Iterator r(updated_region); !r.IsAtEnd(); |
197 const SkIRect& rect = r.rect(); | 201 r.Advance()) { |
| 202 const webrtc::DesktopRect& rect = r.rect(); |
198 int left = rect.left() / kMacroBlockSize; | 203 int left = rect.left() / kMacroBlockSize; |
199 int right = (rect.right() - 1) / kMacroBlockSize; | 204 int right = (rect.right() - 1) / kMacroBlockSize; |
200 int top = rect.top() / kMacroBlockSize; | 205 int top = rect.top() / kMacroBlockSize; |
201 int bottom = (rect.bottom() - 1) / kMacroBlockSize; | 206 int bottom = (rect.bottom() - 1) / kMacroBlockSize; |
202 DCHECK_LT(right, active_map_width_); | 207 DCHECK_LT(right, active_map_width_); |
203 DCHECK_LT(bottom, active_map_height_); | 208 DCHECK_LT(bottom, active_map_height_); |
204 | 209 |
205 uint8* map = active_map_.get() + top * active_map_width_; | 210 uint8* map = active_map_.get() + top * active_map_width_; |
206 for (int y = top; y <= bottom; ++y) { | 211 for (int y = top; y <= bottom; ++y) { |
207 for (int x = left; x <= right; ++x) | 212 for (int x = left; x <= right; ++x) |
(...skipping 12 matching lines...) Expand all Loading... |
220 | 225 |
221 if (!initialized_ || | 226 if (!initialized_ || |
222 !frame.size().equals(webrtc::DesktopSize(image_->w, image_->h))) { | 227 !frame.size().equals(webrtc::DesktopSize(image_->w, image_->h))) { |
223 bool ret = Init(frame.size()); | 228 bool ret = Init(frame.size()); |
224 // TODO(hclam): Handle error better. | 229 // TODO(hclam): Handle error better. |
225 CHECK(ret) << "Initialization of encoder failed"; | 230 CHECK(ret) << "Initialization of encoder failed"; |
226 initialized_ = ret; | 231 initialized_ = ret; |
227 } | 232 } |
228 | 233 |
229 // Convert the updated capture data ready for encode. | 234 // Convert the updated capture data ready for encode. |
230 SkRegion updated_region; | 235 webrtc::DesktopRegion updated_region; |
231 PrepareImage(frame, &updated_region); | 236 PrepareImage(frame, &updated_region); |
232 | 237 |
233 // Update active map based on updated region. | 238 // Update active map based on updated region. |
234 PrepareActiveMap(updated_region); | 239 PrepareActiveMap(updated_region); |
235 | 240 |
236 // Apply active map to the encoder. | 241 // Apply active map to the encoder. |
237 vpx_active_map_t act_map; | 242 vpx_active_map_t act_map; |
238 act_map.rows = active_map_height_; | 243 act_map.rows = active_map_height_; |
239 act_map.cols = active_map_width_; | 244 act_map.cols = active_map_width_; |
240 act_map.active_map = active_map_.get(); | 245 act_map.active_map = active_map_.get(); |
(...skipping 15 matching lines...) Expand all Loading... |
256 | 261 |
257 // Read the encoded data. | 262 // Read the encoded data. |
258 vpx_codec_iter_t iter = NULL; | 263 vpx_codec_iter_t iter = NULL; |
259 bool got_data = false; | 264 bool got_data = false; |
260 | 265 |
261 // TODO(hclam): Make sure we get exactly one frame from the packet. | 266 // TODO(hclam): Make sure we get exactly one frame from the packet. |
262 // TODO(hclam): We should provide the output buffer to avoid one copy. | 267 // TODO(hclam): We should provide the output buffer to avoid one copy. |
263 scoped_ptr<VideoPacket> packet(new VideoPacket()); | 268 scoped_ptr<VideoPacket> packet(new VideoPacket()); |
264 | 269 |
265 while (!got_data) { | 270 while (!got_data) { |
266 const vpx_codec_cx_pkt_t* vpx_packet = vpx_codec_get_cx_data(codec_.get(), | 271 const vpx_codec_cx_pkt_t* vpx_packet = |
267 &iter); | 272 vpx_codec_get_cx_data(codec_.get(), &iter); |
268 if (!vpx_packet) | 273 if (!vpx_packet) |
269 continue; | 274 continue; |
270 | 275 |
271 switch (vpx_packet->kind) { | 276 switch (vpx_packet->kind) { |
272 case VPX_CODEC_CX_FRAME_PKT: | 277 case VPX_CODEC_CX_FRAME_PKT: |
273 got_data = true; | 278 got_data = true; |
274 // TODO(sergeyu): Split each frame into multiple partitions. | 279 // TODO(sergeyu): Split each frame into multiple partitions. |
275 packet->set_data(vpx_packet->data.frame.buf, vpx_packet->data.frame.sz); | 280 packet->set_data(vpx_packet->data.frame.buf, vpx_packet->data.frame.sz); |
276 break; | 281 break; |
277 default: | 282 default: |
278 break; | 283 break; |
279 } | 284 } |
280 } | 285 } |
281 | 286 |
282 // Construct the VideoPacket message. | 287 // Construct the VideoPacket message. |
283 packet->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VP8); | 288 packet->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VP8); |
284 packet->mutable_format()->set_screen_width(frame.size().width()); | 289 packet->mutable_format()->set_screen_width(frame.size().width()); |
285 packet->mutable_format()->set_screen_height(frame.size().height()); | 290 packet->mutable_format()->set_screen_height(frame.size().height()); |
286 packet->set_capture_time_ms(frame.capture_time_ms()); | 291 packet->set_capture_time_ms(frame.capture_time_ms()); |
287 packet->set_encode_time_ms( | 292 packet->set_encode_time_ms( |
288 (base::Time::Now() - encode_start_time).InMillisecondsRoundedUp()); | 293 (base::Time::Now() - encode_start_time).InMillisecondsRoundedUp()); |
289 if (!frame.dpi().is_zero()) { | 294 if (!frame.dpi().is_zero()) { |
290 packet->mutable_format()->set_x_dpi(frame.dpi().x()); | 295 packet->mutable_format()->set_x_dpi(frame.dpi().x()); |
291 packet->mutable_format()->set_y_dpi(frame.dpi().y()); | 296 packet->mutable_format()->set_y_dpi(frame.dpi().y()); |
292 } | 297 } |
293 for (SkRegion::Iterator r(updated_region); !r.done(); r.next()) { | 298 for (webrtc::DesktopRegion::Iterator r(updated_region); !r.IsAtEnd(); |
| 299 r.Advance()) { |
294 Rect* rect = packet->add_dirty_rects(); | 300 Rect* rect = packet->add_dirty_rects(); |
295 rect->set_x(r.rect().x()); | 301 rect->set_x(r.rect().left()); |
296 rect->set_y(r.rect().y()); | 302 rect->set_y(r.rect().top()); |
297 rect->set_width(r.rect().width()); | 303 rect->set_width(r.rect().width()); |
298 rect->set_height(r.rect().height()); | 304 rect->set_height(r.rect().height()); |
299 } | 305 } |
300 | 306 |
301 return packet.Pass(); | 307 return packet.Pass(); |
302 } | 308 } |
303 | 309 |
304 } // namespace remoting | 310 } // namespace remoting |
OLD | NEW |