1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "remoting/codec/video_encoder_vpx.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/sys_info.h"
11 #include "remoting/base/util.h"
12 #include "remoting/proto/video.pb.h"
13 #include "third_party/libyuv/include/libyuv/convert_from_argb.h"
14 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h"
15 #include "third_party/webrtc/modules/desktop_capture/desktop_geometry.h"
16 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h"
19 #define VPX_CODEC_DISABLE_COMPAT 1
20 #include "third_party/libvpx/source/libvpx/vpx/vpx_encoder.h"
21 #include "third_party/libvpx/source/libvpx/vpx/vp8cx.h"
28 // Name of command-line flag to enable VP9 to use I444 by default.
29 const char kEnableI444SwitchName[] = "enable-i444";
31 // Number of bytes in an RGBx pixel.
32 const int kBytesPerRgbPixel = 4;
34 // Defines the dimension of a macro block. This is used to compute the active
35 // map for the encoder.
36 const int kMacroBlockSize = 16;
38 // Magic encoder profile numbers for I420 and I444 input formats.
39 const int kVp9I420ProfileNumber = 0;
40 const int kVp9I444ProfileNumber = 1;
42 void SetCommonCodecParameters(const webrtc::DesktopSize& size,
43 vpx_codec_enc_cfg_t* config) {
44 // Use millisecond granularity time base.
45 config->g_timebase.num = 1;
46 config->g_timebase.den = 1000;
48 // Adjust default target bit-rate to account for actual desktop size.
49 config->rc_target_bitrate = size.width() * size.height() *
50 config->rc_target_bitrate / config->g_w / config->g_h;
52 config->g_w = size.width();
53 config->g_h = size.height();
54 config->g_pass = VPX_RC_ONE_PASS;
56 // Start emitting packets immediately.
57 config->g_lag_in_frames = 0;
59 // Since the transport layer is reliable, keyframes aren't necessary.
60 config->kf_mode = VPX_KF_DISABLED;
62 // Using 2 threads gives a great boost in performance for most systems with
63 // adequate processing power. NB: Going to multiple threads on low end
64 // windows systems can really hurt performance.
65 // http://crbug.com/99179
66 config->g_threads = (base::SysInfo::NumberOfProcessors() > 2) ? 2 : 1;
69 ScopedVpxCodec CreateVP8Codec(const webrtc::DesktopSize& size) {
70 ScopedVpxCodec codec(new vpx_codec_ctx_t);
72 // Configure the encoder.
73 vpx_codec_enc_cfg_t config;
74 const vpx_codec_iface_t* algo = vpx_codec_vp8_cx();
76 vpx_codec_err_t ret = vpx_codec_enc_config_default(algo, &config, 0);
77 if (ret != VPX_CODEC_OK)
78 return ScopedVpxCodec();
80 SetCommonCodecParameters(size, &config);
82 // Value of 2 means using the real time profile. This is basically a
83 // redundant option since we explicitly select real time mode when doing
87 // Clamping the quantizer constrains the worst-case quality and CPU usage.
88 config.rc_min_quantizer = 20;
89 config.rc_max_quantizer = 30;
91 if (vpx_codec_enc_init(codec.get(), algo, &config, 0))
92 return ScopedVpxCodec();
94 // Value of 16 will have the smallest CPU load. This turns off subpixel
96 if (vpx_codec_control(codec.get(), VP8E_SET_CPUUSED, 16))
97 return ScopedVpxCodec();
99 // Use the lowest level of noise sensitivity so as to spend less time
100 // on motion estimation and inter-prediction mode.
101 if (vpx_codec_control(codec.get(), VP8E_SET_NOISE_SENSITIVITY, 0))
102 return ScopedVpxCodec();
107 ScopedVpxCodec CreateVP9Codec(const webrtc::DesktopSize& size,
109 bool lossless_encode) {
110 ScopedVpxCodec codec(new vpx_codec_ctx_t);
112 // Configure the encoder.
113 vpx_codec_enc_cfg_t config;
114 const vpx_codec_iface_t* algo = vpx_codec_vp9_cx();
116 vpx_codec_err_t ret = vpx_codec_enc_config_default(algo, &config, 0);
117 if (ret != VPX_CODEC_OK)
118 return ScopedVpxCodec();
120 SetCommonCodecParameters(size, &config);
122 // Configure VP9 for I420 or I444 source frames.
124 lossless_color ? kVp9I444ProfileNumber : kVp9I420ProfileNumber;
126 if (lossless_encode) {
127 // Disable quantization entirely, putting the encoder in "lossless" mode.
128 config.rc_min_quantizer = 0;
129 config.rc_max_quantizer = 0;
131 // Lossy encode using the same settings as for VP8.
132 config.rc_min_quantizer = 20;
133 config.rc_max_quantizer = 30;
136 if (vpx_codec_enc_init(codec.get(), algo, &config, 0))
137 return ScopedVpxCodec();
139 // Request the lowest-CPU usage that VP9 supports, which depends on whether
140 // we are encoding lossy or lossless.
141 // Note that this is configured via the same parameter as for VP8.
142 int cpu_used = lossless_encode ? 5 : 6;
143 if (vpx_codec_control(codec.get(), VP8E_SET_CPUUSED, cpu_used))
144 return ScopedVpxCodec();
146 // Use the lowest level of noise sensitivity so as to spend less time
147 // on motion estimation and inter-prediction mode.
148 if (vpx_codec_control(codec.get(), VP9E_SET_NOISE_SENSITIVITY, 0))
149 return ScopedVpxCodec();
154 void CreateImage(bool use_i444,
155 const webrtc::DesktopSize& size,
156 scoped_ptr<vpx_image_t>* out_image,
157 scoped_ptr<uint8[]>* out_image_buffer) {
158 DCHECK(!size.is_empty());
160 scoped_ptr<vpx_image_t> image(new vpx_image_t());
161 memset(image.get(), 0, sizeof(vpx_image_t));
163 // libvpx seems to require both to be assigned.
164 image->d_w = size.width();
165 image->w = size.width();
166 image->d_h = size.height();
167 image->h = size.height();
169 // libvpx should derive chroma shifts from|fmt| but currently has a bug:
170 // https://code.google.com/p/webm/issues/detail?id=627
172 image->fmt = VPX_IMG_FMT_I444;
173 image->x_chroma_shift = 0;
174 image->y_chroma_shift = 0;
176 image->fmt = VPX_IMG_FMT_YV12;
177 image->x_chroma_shift = 1;
178 image->y_chroma_shift = 1;
181 // libyuv's fast-path requires 16-byte aligned pointers and strides, so pad
182 // the Y, U and V planes' strides to multiples of 16 bytes.
183 const int y_stride = ((image->w - 1) & ~15) + 16;
184 const int uv_unaligned_stride = y_stride >> image->x_chroma_shift;
185 const int uv_stride = ((uv_unaligned_stride - 1) & ~15) + 16;
187 // libvpx accesses the source image in macro blocks, and will over-read
188 // if the image is not padded out to the next macroblock: crbug.com/119633.
189 // Pad the Y, U and V planes' height out to compensate.
190 // Assuming macroblocks are 16x16, aligning the planes' strides above also
191 // macroblock aligned them.
192 DCHECK_EQ(16, kMacroBlockSize);
193 const int y_rows = ((image->h - 1) & ~(kMacroBlockSize-1)) + kMacroBlockSize;
194 const int uv_rows = y_rows >> image->y_chroma_shift;
196 // Allocate a YUV buffer large enough for the aligned data & padding.
197 const int buffer_size = y_stride * y_rows + 2*uv_stride * uv_rows;
198 scoped_ptr<uint8[]> image_buffer(new uint8[buffer_size]);
200 // Reset image value to 128 so we just need to fill in the y plane.
201 memset(image_buffer.get(), 128, buffer_size);
203 // Fill in the information for |image_|.
204 unsigned char* uchar_buffer =
205 reinterpret_cast<unsigned char*>(image_buffer.get());
206 image->planes[0] = uchar_buffer;
207 image->planes[1] = image->planes[0] + y_stride * y_rows;
208 image->planes[2] = image->planes[1] + uv_stride * uv_rows;
209 image->stride[0] = y_stride;
210 image->stride[1] = uv_stride;
211 image->stride[2] = uv_stride;
213 *out_image = image.Pass();
214 *out_image_buffer = image_buffer.Pass();
220 scoped_ptr<VideoEncoderVpx> VideoEncoderVpx::CreateForVP8() {
221 return make_scoped_ptr(new VideoEncoderVpx(false));
225 scoped_ptr<VideoEncoderVpx> VideoEncoderVpx::CreateForVP9() {
226 return make_scoped_ptr(new VideoEncoderVpx(true));
229 VideoEncoderVpx::~VideoEncoderVpx() {}
231 void VideoEncoderVpx::SetLosslessEncode(bool want_lossless) {
232 if (use_vp9_ && (want_lossless != lossless_encode_)) {
233 lossless_encode_ = want_lossless;
234 codec_.reset(); // Force encoder re-initialization.
238 void VideoEncoderVpx::SetLosslessColor(bool want_lossless) {
239 if (use_vp9_ && (want_lossless != lossless_color_)) {
240 lossless_color_ = want_lossless;
241 codec_.reset(); // Force encoder re-initialization.
245 scoped_ptr<VideoPacket> VideoEncoderVpx::Encode(
246 const webrtc::DesktopFrame& frame) {
247 DCHECK_LE(32, frame.size().width());
248 DCHECK_LE(32, frame.size().height());
250 base::TimeTicks encode_start_time = base::TimeTicks::Now();
253 !frame.size().equals(webrtc::DesktopSize(image_->w, image_->h))) {
254 bool ret = Initialize(frame.size());
255 // TODO(hclam): Handle error better.
256 CHECK(ret) << "Initialization of encoder failed";
258 // Set now as the base for timestamp calculation.
259 timestamp_base_ = encode_start_time;
262 // Convert the updated capture data ready for encode.
263 webrtc::DesktopRegion updated_region;
264 PrepareImage(frame, &updated_region);
266 // Update active map based on updated region.
267 PrepareActiveMap(updated_region);
269 // Apply active map to the encoder.
270 vpx_active_map_t act_map;
271 act_map.rows = active_map_height_;
272 act_map.cols = active_map_width_;
273 act_map.active_map = active_map_.get();
274 if (vpx_codec_control(codec_.get(), VP8E_SET_ACTIVEMAP, &act_map)) {
275 LOG(ERROR) << "Unable to apply active map";
278 // Do the actual encoding.
279 int timestamp = (encode_start_time - timestamp_base_).InMilliseconds();
280 vpx_codec_err_t ret = vpx_codec_encode(
281 codec_.get(), image_.get(), timestamp, 1, 0, VPX_DL_REALTIME);
282 DCHECK_EQ(ret, VPX_CODEC_OK)
283 << "Encoding error: " << vpx_codec_err_to_string(ret) << "\n"
284 << "Details: " << vpx_codec_error(codec_.get()) << "\n"
285 << vpx_codec_error_detail(codec_.get());
287 // Read the encoded data.
288 vpx_codec_iter_t iter = NULL;
289 bool got_data = false;
291 // TODO(hclam): Make sure we get exactly one frame from the packet.
292 // TODO(hclam): We should provide the output buffer to avoid one copy.
293 scoped_ptr<VideoPacket> packet(
294 helper_.CreateVideoPacketWithUpdatedRegion(frame, updated_region));
295 packet->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VP8);
298 const vpx_codec_cx_pkt_t* vpx_packet =
299 vpx_codec_get_cx_data(codec_.get(), &iter);
303 switch (vpx_packet->kind) {
304 case VPX_CODEC_CX_FRAME_PKT:
306 packet->set_data(vpx_packet->data.frame.buf, vpx_packet->data.frame.sz);
313 // Note the time taken to encode the pixel data.
314 packet->set_encode_time_ms(
315 (base::TimeTicks::Now() - encode_start_time).InMillisecondsRoundedUp());
317 return packet.Pass();
320 VideoEncoderVpx::VideoEncoderVpx(bool use_vp9)
322 lossless_encode_(false),
323 lossless_color_(false),
324 active_map_width_(0),
325 active_map_height_(0) {
327 // Use I444 colour space, by default, if specified on the command-line.
328 if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableI444SwitchName)) {
329 SetLosslessColor(true);
334 bool VideoEncoderVpx::Initialize(const webrtc::DesktopSize& size) {
335 DCHECK(use_vp9_ || !lossless_color_);
336 DCHECK(use_vp9_ || !lossless_encode_);
340 // (Re)Create the VPX image structure and pixel buffer.
341 CreateImage(lossless_color_, size, &image_, &image_buffer_);
343 // Initialize active map.
344 active_map_width_ = (image_->w + kMacroBlockSize - 1) / kMacroBlockSize;
345 active_map_height_ = (image_->h + kMacroBlockSize - 1) / kMacroBlockSize;
346 active_map_.reset(new uint8[active_map_width_ * active_map_height_]);
348 // (Re)Initialize the codec.
350 codec_ = CreateVP9Codec(size, lossless_color_, lossless_encode_);
352 codec_ = CreateVP8Codec(size);
358 void VideoEncoderVpx::PrepareImage(const webrtc::DesktopFrame& frame,
359 webrtc::DesktopRegion* updated_region) {
360 if (frame.updated_region().is_empty()) {
361 updated_region->Clear();
365 // Align the region to macroblocks, to avoid encoding artefacts.
366 // This also ensures that all rectangles have even-aligned top-left, which
367 // is required for ConvertRGBToYUVWithRect() to work.
368 std::vector<webrtc::DesktopRect> aligned_rects;
369 for (webrtc::DesktopRegion::Iterator r(frame.updated_region());
370 !r.IsAtEnd(); r.Advance()) {
371 const webrtc::DesktopRect& rect = r.rect();
372 aligned_rects.push_back(AlignRect(webrtc::DesktopRect::MakeLTRB(
373 rect.left(), rect.top(), rect.right(), rect.bottom())));
375 DCHECK(!aligned_rects.empty());
376 updated_region->Clear();
377 updated_region->AddRects(&aligned_rects[0], aligned_rects.size());
379 // Clip back to the screen dimensions, in case they're not macroblock aligned.
380 // The conversion routines don't require even width & height, so this is safe
381 // even if the source dimensions are not even.
382 updated_region->IntersectWith(
383 webrtc::DesktopRect::MakeWH(image_->w, image_->h));
385 // Convert the updated region to YUV ready for encoding.
386 const uint8* rgb_data = frame.data();
387 const int rgb_stride = frame.stride();
388 const int y_stride = image_->stride[0];
389 DCHECK_EQ(image_->stride[1], image_->stride[2]);
390 const int uv_stride = image_->stride[1];
391 uint8* y_data = image_->planes[0];
392 uint8* u_data = image_->planes[1];
393 uint8* v_data = image_->planes[2];
395 switch (image_->fmt) {
396 case VPX_IMG_FMT_I444:
397 for (webrtc::DesktopRegion::Iterator r(*updated_region); !r.IsAtEnd();
399 const webrtc::DesktopRect& rect = r.rect();
400 int rgb_offset = rgb_stride * rect.top() +
401 rect.left() * kBytesPerRgbPixel;
402 int yuv_offset = uv_stride * rect.top() + rect.left();
403 libyuv::ARGBToI444(rgb_data + rgb_offset, rgb_stride,
404 y_data + yuv_offset, y_stride,
405 u_data + yuv_offset, uv_stride,
406 v_data + yuv_offset, uv_stride,
407 rect.width(), rect.height());
410 case VPX_IMG_FMT_YV12:
411 for (webrtc::DesktopRegion::Iterator r(*updated_region); !r.IsAtEnd();
413 const webrtc::DesktopRect& rect = r.rect();
414 int rgb_offset = rgb_stride * rect.top() +
415 rect.left() * kBytesPerRgbPixel;
416 int y_offset = y_stride * rect.top() + rect.left();
417 int uv_offset = uv_stride * rect.top() / 2 + rect.left() / 2;
418 libyuv::ARGBToI420(rgb_data + rgb_offset, rgb_stride,
419 y_data + y_offset, y_stride,
420 u_data + uv_offset, uv_stride,
421 v_data + uv_offset, uv_stride,
422 rect.width(), rect.height());
431 void VideoEncoderVpx::PrepareActiveMap(
432 const webrtc::DesktopRegion& updated_region) {
433 // Clear active map first.
434 memset(active_map_.get(), 0, active_map_width_ * active_map_height_);
436 // Mark updated areas active.
437 for (webrtc::DesktopRegion::Iterator r(updated_region); !r.IsAtEnd();
439 const webrtc::DesktopRect& rect = r.rect();
440 int left = rect.left() / kMacroBlockSize;
441 int right = (rect.right() - 1) / kMacroBlockSize;
442 int top = rect.top() / kMacroBlockSize;
443 int bottom = (rect.bottom() - 1) / kMacroBlockSize;
444 DCHECK_LT(right, active_map_width_);
445 DCHECK_LT(bottom, active_map_height_);
447 uint8* map = active_map_.get() + top * active_map_width_;
448 for (int y = top; y <= bottom; ++y) {
449 for (int x = left; x <= right; ++x)
451 map += active_map_width_;
456 } // namespace remoting