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 "media/base/yuv_convert.h" | 9 #include "media/base/yuv_convert.h" |
10 #include "remoting/base/capture_data.h" | 10 #include "remoting/base/capture_data.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 | 13 |
14 extern "C" { | 14 extern "C" { |
15 #define VPX_CODEC_DISABLE_COMPAT 1 | 15 #define VPX_CODEC_DISABLE_COMPAT 1 |
16 #include "third_party/libvpx/libvpx.h" | 16 #include "third_party/libvpx/libvpx.h" |
17 } | 17 } |
18 | 18 |
19 namespace { | 19 namespace { |
20 | 20 |
21 // Defines the dimension of a macro block. This is used to compute the active | 21 // Defines the dimension of a macro block. This is used to compute the active |
22 // map for the encoder. | 22 // map for the encoder. |
23 const int kMacroBlockSize = 16; | 23 const int kMacroBlockSize = 16; |
24 | 24 |
25 } // namespace remoting | 25 } // namespace remoting |
26 | 26 |
27 namespace remoting { | 27 namespace remoting { |
28 | 28 |
29 EncoderVp8::EncoderVp8() | 29 VideoEncoderVp8::VideoEncoderVp8() |
30 : initialized_(false), | 30 : initialized_(false), |
31 codec_(NULL), | 31 codec_(NULL), |
32 image_(NULL), | 32 image_(NULL), |
33 active_map_width_(0), | 33 active_map_width_(0), |
34 active_map_height_(0), | 34 active_map_height_(0), |
35 last_timestamp_(0) { | 35 last_timestamp_(0) { |
36 } | 36 } |
37 | 37 |
38 EncoderVp8::~EncoderVp8() { | 38 VideoEncoderVp8::~VideoEncoderVp8() { |
39 Destroy(); | 39 Destroy(); |
40 } | 40 } |
41 | 41 |
42 void EncoderVp8::Destroy() { | 42 void VideoEncoderVp8::Destroy() { |
43 if (initialized_) { | 43 if (initialized_) { |
44 vpx_codec_err_t ret = vpx_codec_destroy(codec_.get()); | 44 vpx_codec_err_t ret = vpx_codec_destroy(codec_.get()); |
45 DCHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec"; | 45 DCHECK(ret == VPX_CODEC_OK) << "Failed to destroy codec"; |
46 initialized_ = false; | 46 initialized_ = false; |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 bool EncoderVp8::Init(const SkISize& size) { | 50 bool VideoEncoderVp8::Init(const SkISize& size) { |
51 Destroy(); | 51 Destroy(); |
52 codec_.reset(new vpx_codec_ctx_t()); | 52 codec_.reset(new vpx_codec_ctx_t()); |
53 image_.reset(new vpx_image_t()); | 53 image_.reset(new vpx_image_t()); |
54 memset(image_.get(), 0, sizeof(vpx_image_t)); | 54 memset(image_.get(), 0, sizeof(vpx_image_t)); |
55 | 55 |
56 image_->fmt = VPX_IMG_FMT_YV12; | 56 image_->fmt = VPX_IMG_FMT_YV12; |
57 | 57 |
58 // libvpx seems to require both to be assigned. | 58 // libvpx seems to require both to be assigned. |
59 image_->d_w = size.width(); | 59 image_->d_w = size.width(); |
60 image_->w = size.width(); | 60 image_->w = size.width(); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 if (vpx_codec_control(codec_.get(), VP8E_SET_CPUUSED, 16)) | 137 if (vpx_codec_control(codec_.get(), VP8E_SET_CPUUSED, 16)) |
138 return false; | 138 return false; |
139 | 139 |
140 // 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 |
141 // on motion estimation and inter-prediction mode. | 141 // on motion estimation and inter-prediction mode. |
142 if (vpx_codec_control(codec_.get(), VP8E_SET_NOISE_SENSITIVITY, 0)) | 142 if (vpx_codec_control(codec_.get(), VP8E_SET_NOISE_SENSITIVITY, 0)) |
143 return false; | 143 return false; |
144 return true; | 144 return true; |
145 } | 145 } |
146 | 146 |
147 void EncoderVp8::PrepareImage(scoped_refptr<CaptureData> capture_data, | 147 void VideoEncoderVp8::PrepareImage(scoped_refptr<CaptureData> capture_data, |
148 SkRegion* updated_region) { | 148 SkRegion* updated_region) { |
149 // Perform RGB->YUV conversion. | 149 // Perform RGB->YUV conversion. |
150 CHECK_EQ(capture_data->pixel_format(), media::VideoFrame::RGB32) | 150 CHECK_EQ(capture_data->pixel_format(), media::VideoFrame::RGB32) |
151 << "Only RGB32 is supported"; | 151 << "Only RGB32 is supported"; |
152 | 152 |
153 const SkRegion& region = capture_data->dirty_region(); | 153 const SkRegion& region = capture_data->dirty_region(); |
154 if (region.isEmpty()) { | 154 if (region.isEmpty()) { |
155 updated_region->setEmpty(); | 155 updated_region->setEmpty(); |
156 return; | 156 return; |
157 } | 157 } |
158 | 158 |
(...skipping 24 matching lines...) Expand all Loading... |
183 uint8* v_data = image_->planes[2]; | 183 uint8* v_data = image_->planes[2]; |
184 for (SkRegion::Iterator r(*updated_region); !r.done(); r.next()) { | 184 for (SkRegion::Iterator r(*updated_region); !r.done(); r.next()) { |
185 const SkIRect& rect = r.rect(); | 185 const SkIRect& rect = r.rect(); |
186 ConvertRGB32ToYUVWithRect( | 186 ConvertRGB32ToYUVWithRect( |
187 rgb_data, y_data, u_data, v_data, | 187 rgb_data, y_data, u_data, v_data, |
188 rect.x(), rect.y(), rect.width(), rect.height(), | 188 rect.x(), rect.y(), rect.width(), rect.height(), |
189 rgb_stride, y_stride, uv_stride); | 189 rgb_stride, y_stride, uv_stride); |
190 } | 190 } |
191 } | 191 } |
192 | 192 |
193 void EncoderVp8::PrepareActiveMap(const SkRegion& updated_region) { | 193 void VideoEncoderVp8::PrepareActiveMap(const SkRegion& updated_region) { |
194 // Clear active map first. | 194 // Clear active map first. |
195 memset(active_map_.get(), 0, active_map_width_ * active_map_height_); | 195 memset(active_map_.get(), 0, active_map_width_ * active_map_height_); |
196 | 196 |
197 // Mark updated areas active. | 197 // Mark updated areas active. |
198 for (SkRegion::Iterator r(updated_region); !r.done(); r.next()) { | 198 for (SkRegion::Iterator r(updated_region); !r.done(); r.next()) { |
199 const SkIRect& rect = r.rect(); | 199 const SkIRect& rect = r.rect(); |
200 int left = rect.left() / kMacroBlockSize; | 200 int left = rect.left() / kMacroBlockSize; |
201 int right = (rect.right() - 1) / kMacroBlockSize; | 201 int right = (rect.right() - 1) / kMacroBlockSize; |
202 int top = rect.top() / kMacroBlockSize; | 202 int top = rect.top() / kMacroBlockSize; |
203 int bottom = (rect.bottom() - 1) / kMacroBlockSize; | 203 int bottom = (rect.bottom() - 1) / kMacroBlockSize; |
204 CHECK(right < active_map_width_); | 204 CHECK(right < active_map_width_); |
205 CHECK(bottom < active_map_height_); | 205 CHECK(bottom < active_map_height_); |
206 | 206 |
207 uint8* map = active_map_.get() + top * active_map_width_; | 207 uint8* map = active_map_.get() + top * active_map_width_; |
208 for (int y = top; y <= bottom; ++y) { | 208 for (int y = top; y <= bottom; ++y) { |
209 for (int x = left; x <= right; ++x) | 209 for (int x = left; x <= right; ++x) |
210 map[x] = 1; | 210 map[x] = 1; |
211 map += active_map_width_; | 211 map += active_map_width_; |
212 } | 212 } |
213 } | 213 } |
214 } | 214 } |
215 | 215 |
216 void EncoderVp8::Encode(scoped_refptr<CaptureData> capture_data, | 216 void VideoEncoderVp8::Encode( |
217 bool key_frame, | 217 scoped_refptr<CaptureData> capture_data, |
218 const DataAvailableCallback& data_available_callback) { | 218 bool key_frame, |
| 219 const DataAvailableCallback& data_available_callback) { |
219 DCHECK_LE(32, capture_data->size().width()); | 220 DCHECK_LE(32, capture_data->size().width()); |
220 DCHECK_LE(32, capture_data->size().height()); | 221 DCHECK_LE(32, capture_data->size().height()); |
221 | 222 |
222 if (!initialized_ || | 223 if (!initialized_ || |
223 (capture_data->size() != SkISize::Make(image_->w, image_->h))) { | 224 (capture_data->size() != SkISize::Make(image_->w, image_->h))) { |
224 bool ret = Init(capture_data->size()); | 225 bool ret = Init(capture_data->size()); |
225 // TODO(hclam): Handle error better. | 226 // TODO(hclam): Handle error better. |
226 CHECK(ret) << "Initialization of encoder failed"; | 227 CHECK(ret) << "Initialization of encoder failed"; |
227 initialized_ = ret; | 228 initialized_ = ret; |
228 } | 229 } |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
298 rect->set_x(r.rect().x()); | 299 rect->set_x(r.rect().x()); |
299 rect->set_y(r.rect().y()); | 300 rect->set_y(r.rect().y()); |
300 rect->set_width(r.rect().width()); | 301 rect->set_width(r.rect().width()); |
301 rect->set_height(r.rect().height()); | 302 rect->set_height(r.rect().height()); |
302 } | 303 } |
303 | 304 |
304 data_available_callback.Run(packet.Pass()); | 305 data_available_callback.Run(packet.Pass()); |
305 } | 306 } |
306 | 307 |
307 } // namespace remoting | 308 } // namespace remoting |
OLD | NEW |