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