• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 #include "remoting/codec/video_encoder_vpx.h"
6 
7 #include "base/bind.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"
17 
18 extern "C" {
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"
22 }
23 
24 namespace remoting {
25 
26 namespace {
27 
28 // Name of command-line flag to enable VP9 to use I444 by default.
29 const char kEnableI444SwitchName[] = "enable-i444";
30 
31 // Number of bytes in an RGBx pixel.
32 const int kBytesPerRgbPixel = 4;
33 
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;
37 
38 // Magic encoder profile numbers for I420 and I444 input formats.
39 const int kVp9I420ProfileNumber = 0;
40 const int kVp9I444ProfileNumber = 1;
41 
SetCommonCodecParameters(const webrtc::DesktopSize & size,vpx_codec_enc_cfg_t * config)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;
47 
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;
51 
52   config->g_w = size.width();
53   config->g_h = size.height();
54   config->g_pass = VPX_RC_ONE_PASS;
55 
56   // Start emitting packets immediately.
57   config->g_lag_in_frames = 0;
58 
59   // Using 2 threads gives a great boost in performance for most systems with
60   // adequate processing power. NB: Going to multiple threads on low end
61   // windows systems can really hurt performance.
62   // http://crbug.com/99179
63   config->g_threads = (base::SysInfo::NumberOfProcessors() > 2) ? 2 : 1;
64 }
65 
CreateVP8Codec(const webrtc::DesktopSize & size)66 ScopedVpxCodec CreateVP8Codec(const webrtc::DesktopSize& size) {
67   ScopedVpxCodec codec(new vpx_codec_ctx_t);
68 
69   // Configure the encoder.
70   vpx_codec_enc_cfg_t config;
71   const vpx_codec_iface_t* algo = vpx_codec_vp8_cx();
72   CHECK(algo);
73   vpx_codec_err_t ret = vpx_codec_enc_config_default(algo, &config, 0);
74   if (ret != VPX_CODEC_OK)
75     return ScopedVpxCodec();
76 
77   SetCommonCodecParameters(size, &config);
78 
79   // Value of 2 means using the real time profile. This is basically a
80   // redundant option since we explicitly select real time mode when doing
81   // encoding.
82   config.g_profile = 2;
83 
84   // Clamping the quantizer constrains the worst-case quality and CPU usage.
85   config.rc_min_quantizer = 20;
86   config.rc_max_quantizer = 30;
87 
88   if (vpx_codec_enc_init(codec.get(), algo, &config, 0))
89     return ScopedVpxCodec();
90 
91   // Value of 16 will have the smallest CPU load. This turns off subpixel
92   // motion search.
93   if (vpx_codec_control(codec.get(), VP8E_SET_CPUUSED, 16))
94     return ScopedVpxCodec();
95 
96   // Use the lowest level of noise sensitivity so as to spend less time
97   // on motion estimation and inter-prediction mode.
98   if (vpx_codec_control(codec.get(), VP8E_SET_NOISE_SENSITIVITY, 0))
99     return ScopedVpxCodec();
100 
101   return codec.Pass();
102 }
103 
CreateVP9Codec(const webrtc::DesktopSize & size,bool lossless_color,bool lossless_encode)104 ScopedVpxCodec CreateVP9Codec(const webrtc::DesktopSize& size,
105                               bool lossless_color,
106                               bool lossless_encode) {
107   ScopedVpxCodec codec(new vpx_codec_ctx_t);
108 
109   // Configure the encoder.
110   vpx_codec_enc_cfg_t config;
111   const vpx_codec_iface_t* algo = vpx_codec_vp9_cx();
112   CHECK(algo);
113   vpx_codec_err_t ret = vpx_codec_enc_config_default(algo, &config, 0);
114   if (ret != VPX_CODEC_OK)
115     return ScopedVpxCodec();
116 
117   SetCommonCodecParameters(size, &config);
118 
119   // Configure VP9 for I420 or I444 source frames.
120   config.g_profile =
121       lossless_color ? kVp9I444ProfileNumber : kVp9I420ProfileNumber;
122 
123   if (lossless_encode) {
124     // Disable quantization entirely, putting the encoder in "lossless" mode.
125     config.rc_min_quantizer = 0;
126     config.rc_max_quantizer = 0;
127   } else {
128     // Lossy encode using the same settings as for VP8.
129     config.rc_min_quantizer = 20;
130     config.rc_max_quantizer = 30;
131   }
132 
133   if (vpx_codec_enc_init(codec.get(), algo, &config, 0))
134     return ScopedVpxCodec();
135 
136   // VP9 encode doesn't yet support Realtime, so falls back to Good quality,
137   // for which 4 is the lowest CPU usage.
138   // Note that this is configured via the same parameter as for VP8.
139   if (vpx_codec_control(codec.get(), VP8E_SET_CPUUSED, 4))
140     return ScopedVpxCodec();
141 
142   // Use the lowest level of noise sensitivity so as to spend less time
143   // on motion estimation and inter-prediction mode.
144   // Note that this is configured via the same parameter as for VP8.
145   if (vpx_codec_control(codec.get(), VP8E_SET_NOISE_SENSITIVITY, 0))
146     return ScopedVpxCodec();
147 
148   return codec.Pass();
149 }
150 
CreateImage(bool use_i444,const webrtc::DesktopSize & size,scoped_ptr<vpx_image_t> * out_image,scoped_ptr<uint8[]> * out_image_buffer)151 void CreateImage(bool use_i444,
152                  const webrtc::DesktopSize& size,
153                  scoped_ptr<vpx_image_t>* out_image,
154                  scoped_ptr<uint8[]>* out_image_buffer) {
155   DCHECK(!size.is_empty());
156 
157   scoped_ptr<vpx_image_t> image(new vpx_image_t());
158   memset(image.get(), 0, sizeof(vpx_image_t));
159 
160   // libvpx seems to require both to be assigned.
161   image->d_w = size.width();
162   image->w = size.width();
163   image->d_h = size.height();
164   image->h = size.height();
165 
166   // libvpx should derive chroma shifts from|fmt| but currently has a bug:
167   // https://code.google.com/p/webm/issues/detail?id=627
168   if (use_i444) {
169     image->fmt = VPX_IMG_FMT_I444;
170     image->x_chroma_shift = 0;
171     image->y_chroma_shift = 0;
172   } else { // I420
173     image->fmt = VPX_IMG_FMT_YV12;
174     image->x_chroma_shift = 1;
175     image->y_chroma_shift = 1;
176   }
177 
178   // libyuv's fast-path requires 16-byte aligned pointers and strides, so pad
179   // the Y, U and V planes' strides to multiples of 16 bytes.
180   const int y_stride = ((image->w - 1) & ~15) + 16;
181   const int uv_unaligned_stride = y_stride >> image->x_chroma_shift;
182   const int uv_stride = ((uv_unaligned_stride - 1) & ~15) + 16;
183 
184   // libvpx accesses the source image in macro blocks, and will over-read
185   // if the image is not padded out to the next macroblock: crbug.com/119633.
186   // Pad the Y, U and V planes' height out to compensate.
187   // Assuming macroblocks are 16x16, aligning the planes' strides above also
188   // macroblock aligned them.
189   DCHECK_EQ(16, kMacroBlockSize);
190   const int y_rows = ((image->h - 1) & ~(kMacroBlockSize-1)) + kMacroBlockSize;
191   const int uv_rows = y_rows >> image->y_chroma_shift;
192 
193   // Allocate a YUV buffer large enough for the aligned data & padding.
194   const int buffer_size = y_stride * y_rows + 2*uv_stride * uv_rows;
195   scoped_ptr<uint8[]> image_buffer(new uint8[buffer_size]);
196 
197   // Reset image value to 128 so we just need to fill in the y plane.
198   memset(image_buffer.get(), 128, buffer_size);
199 
200   // Fill in the information for |image_|.
201   unsigned char* uchar_buffer =
202       reinterpret_cast<unsigned char*>(image_buffer.get());
203   image->planes[0] = uchar_buffer;
204   image->planes[1] = image->planes[0] + y_stride * y_rows;
205   image->planes[2] = image->planes[1] + uv_stride * uv_rows;
206   image->stride[0] = y_stride;
207   image->stride[1] = uv_stride;
208   image->stride[2] = uv_stride;
209 
210   *out_image = image.Pass();
211   *out_image_buffer = image_buffer.Pass();
212 }
213 
214 } // namespace
215 
216 // static
CreateForVP8()217 scoped_ptr<VideoEncoderVpx> VideoEncoderVpx::CreateForVP8() {
218   return scoped_ptr<VideoEncoderVpx>(new VideoEncoderVpx(false));
219 }
220 
221 // static
CreateForVP9()222 scoped_ptr<VideoEncoderVpx> VideoEncoderVpx::CreateForVP9() {
223   return scoped_ptr<VideoEncoderVpx>(new VideoEncoderVpx(true));
224 }
225 
~VideoEncoderVpx()226 VideoEncoderVpx::~VideoEncoderVpx() {}
227 
SetLosslessEncode(bool want_lossless)228 void VideoEncoderVpx::SetLosslessEncode(bool want_lossless) {
229   if (use_vp9_ && (want_lossless != lossless_encode_)) {
230     lossless_encode_ = want_lossless;
231     codec_.reset(); // Force encoder re-initialization.
232   }
233 }
234 
SetLosslessColor(bool want_lossless)235 void VideoEncoderVpx::SetLosslessColor(bool want_lossless) {
236   if (use_vp9_ && (want_lossless != lossless_color_)) {
237     lossless_color_ = want_lossless;
238     codec_.reset(); // Force encoder re-initialization.
239   }
240 }
241 
Encode(const webrtc::DesktopFrame & frame)242 scoped_ptr<VideoPacket> VideoEncoderVpx::Encode(
243     const webrtc::DesktopFrame& frame) {
244   DCHECK_LE(32, frame.size().width());
245   DCHECK_LE(32, frame.size().height());
246 
247   base::TimeTicks encode_start_time = base::TimeTicks::Now();
248 
249   if (!codec_ ||
250       !frame.size().equals(webrtc::DesktopSize(image_->w, image_->h))) {
251     bool ret = Initialize(frame.size());
252     // TODO(hclam): Handle error better.
253     CHECK(ret) << "Initialization of encoder failed";
254 
255     // Set now as the base for timestamp calculation.
256     timestamp_base_ = encode_start_time;
257   }
258 
259   // Convert the updated capture data ready for encode.
260   webrtc::DesktopRegion updated_region;
261   PrepareImage(frame, &updated_region);
262 
263   // Update active map based on updated region.
264   PrepareActiveMap(updated_region);
265 
266   // Apply active map to the encoder.
267   vpx_active_map_t act_map;
268   act_map.rows = active_map_height_;
269   act_map.cols = active_map_width_;
270   act_map.active_map = active_map_.get();
271   if (vpx_codec_control(codec_.get(), VP8E_SET_ACTIVEMAP, &act_map)) {
272     LOG(ERROR) << "Unable to apply active map";
273   }
274 
275   // Do the actual encoding.
276   int timestamp = (encode_start_time - timestamp_base_).InMilliseconds();
277   vpx_codec_err_t ret = vpx_codec_encode(
278       codec_.get(), image_.get(), timestamp, 1, 0, VPX_DL_REALTIME);
279   DCHECK_EQ(ret, VPX_CODEC_OK)
280       << "Encoding error: " << vpx_codec_err_to_string(ret) << "\n"
281       << "Details: " << vpx_codec_error(codec_.get()) << "\n"
282       << vpx_codec_error_detail(codec_.get());
283 
284   // Read the encoded data.
285   vpx_codec_iter_t iter = NULL;
286   bool got_data = false;
287 
288   // TODO(hclam): Make sure we get exactly one frame from the packet.
289   // TODO(hclam): We should provide the output buffer to avoid one copy.
290   scoped_ptr<VideoPacket> packet(new VideoPacket());
291 
292   while (!got_data) {
293     const vpx_codec_cx_pkt_t* vpx_packet =
294         vpx_codec_get_cx_data(codec_.get(), &iter);
295     if (!vpx_packet)
296       continue;
297 
298     switch (vpx_packet->kind) {
299       case VPX_CODEC_CX_FRAME_PKT:
300         got_data = true;
301         packet->set_data(vpx_packet->data.frame.buf, vpx_packet->data.frame.sz);
302         break;
303       default:
304         break;
305     }
306   }
307 
308   // Construct the VideoPacket message.
309   packet->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VP8);
310   packet->mutable_format()->set_screen_width(frame.size().width());
311   packet->mutable_format()->set_screen_height(frame.size().height());
312   packet->set_capture_time_ms(frame.capture_time_ms());
313   packet->set_encode_time_ms(
314       (base::TimeTicks::Now() - encode_start_time).InMillisecondsRoundedUp());
315   if (!frame.dpi().is_zero()) {
316     packet->mutable_format()->set_x_dpi(frame.dpi().x());
317     packet->mutable_format()->set_y_dpi(frame.dpi().y());
318   }
319   for (webrtc::DesktopRegion::Iterator r(updated_region); !r.IsAtEnd();
320        r.Advance()) {
321     Rect* rect = packet->add_dirty_rects();
322     rect->set_x(r.rect().left());
323     rect->set_y(r.rect().top());
324     rect->set_width(r.rect().width());
325     rect->set_height(r.rect().height());
326   }
327 
328   return packet.Pass();
329 }
330 
VideoEncoderVpx(bool use_vp9)331 VideoEncoderVpx::VideoEncoderVpx(bool use_vp9)
332     : use_vp9_(use_vp9),
333       lossless_encode_(false),
334       lossless_color_(false),
335       active_map_width_(0),
336       active_map_height_(0) {
337   if (use_vp9_) {
338     // Use lossless encoding mode by default.
339     SetLosslessEncode(true);
340 
341     // Use I444 colour space, by default, if specified on the command-line.
342     if (CommandLine::ForCurrentProcess()->HasSwitch(kEnableI444SwitchName)) {
343       SetLosslessColor(true);
344     }
345   }
346 }
347 
Initialize(const webrtc::DesktopSize & size)348 bool VideoEncoderVpx::Initialize(const webrtc::DesktopSize& size) {
349   DCHECK(use_vp9_ || !lossless_color_);
350   DCHECK(use_vp9_ || !lossless_encode_);
351 
352   codec_.reset();
353 
354   // (Re)Create the VPX image structure and pixel buffer.
355   CreateImage(lossless_color_, size, &image_, &image_buffer_);
356 
357   // Initialize active map.
358   active_map_width_ = (image_->w + kMacroBlockSize - 1) / kMacroBlockSize;
359   active_map_height_ = (image_->h + kMacroBlockSize - 1) / kMacroBlockSize;
360   active_map_.reset(new uint8[active_map_width_ * active_map_height_]);
361 
362   // (Re)Initialize the codec.
363   if (use_vp9_) {
364     codec_ = CreateVP9Codec(size, lossless_color_, lossless_encode_);
365   } else {
366     codec_ = CreateVP8Codec(size);
367   }
368 
369   return codec_;
370 }
371 
PrepareImage(const webrtc::DesktopFrame & frame,webrtc::DesktopRegion * updated_region)372 void VideoEncoderVpx::PrepareImage(const webrtc::DesktopFrame& frame,
373                                    webrtc::DesktopRegion* updated_region) {
374   if (frame.updated_region().is_empty()) {
375     updated_region->Clear();
376     return;
377   }
378 
379   // Align the region to macroblocks, to avoid encoding artefacts.
380   // This also ensures that all rectangles have even-aligned top-left, which
381   // is required for ConvertRGBToYUVWithRect() to work.
382   std::vector<webrtc::DesktopRect> aligned_rects;
383   for (webrtc::DesktopRegion::Iterator r(frame.updated_region());
384        !r.IsAtEnd(); r.Advance()) {
385     const webrtc::DesktopRect& rect = r.rect();
386     aligned_rects.push_back(AlignRect(webrtc::DesktopRect::MakeLTRB(
387         rect.left(), rect.top(), rect.right(), rect.bottom())));
388   }
389   DCHECK(!aligned_rects.empty());
390   updated_region->Clear();
391   updated_region->AddRects(&aligned_rects[0], aligned_rects.size());
392 
393   // Clip back to the screen dimensions, in case they're not macroblock aligned.
394   // The conversion routines don't require even width & height, so this is safe
395   // even if the source dimensions are not even.
396   updated_region->IntersectWith(
397       webrtc::DesktopRect::MakeWH(image_->w, image_->h));
398 
399   // Convert the updated region to YUV ready for encoding.
400   const uint8* rgb_data = frame.data();
401   const int rgb_stride = frame.stride();
402   const int y_stride = image_->stride[0];
403   DCHECK_EQ(image_->stride[1], image_->stride[2]);
404   const int uv_stride = image_->stride[1];
405   uint8* y_data = image_->planes[0];
406   uint8* u_data = image_->planes[1];
407   uint8* v_data = image_->planes[2];
408 
409   switch (image_->fmt) {
410     case VPX_IMG_FMT_I444:
411       for (webrtc::DesktopRegion::Iterator r(*updated_region); !r.IsAtEnd();
412            r.Advance()) {
413         const webrtc::DesktopRect& rect = r.rect();
414         int rgb_offset = rgb_stride * rect.top() +
415                          rect.left() * kBytesPerRgbPixel;
416         int yuv_offset = uv_stride * rect.top() + rect.left();
417         libyuv::ARGBToI444(rgb_data + rgb_offset, rgb_stride,
418                            y_data + yuv_offset, y_stride,
419                            u_data + yuv_offset, uv_stride,
420                            v_data + yuv_offset, uv_stride,
421                            rect.width(), rect.height());
422       }
423       break;
424     case VPX_IMG_FMT_YV12:
425       for (webrtc::DesktopRegion::Iterator r(*updated_region); !r.IsAtEnd();
426            r.Advance()) {
427         const webrtc::DesktopRect& rect = r.rect();
428         int rgb_offset = rgb_stride * rect.top() +
429                          rect.left() * kBytesPerRgbPixel;
430         int y_offset = y_stride * rect.top() + rect.left();
431         int uv_offset = uv_stride * rect.top() / 2 + rect.left() / 2;
432         libyuv::ARGBToI420(rgb_data + rgb_offset, rgb_stride,
433                            y_data + y_offset, y_stride,
434                            u_data + uv_offset, uv_stride,
435                            v_data + uv_offset, uv_stride,
436                            rect.width(), rect.height());
437       }
438       break;
439     default:
440       NOTREACHED();
441       break;
442   }
443 }
444 
PrepareActiveMap(const webrtc::DesktopRegion & updated_region)445 void VideoEncoderVpx::PrepareActiveMap(
446     const webrtc::DesktopRegion& updated_region) {
447   // Clear active map first.
448   memset(active_map_.get(), 0, active_map_width_ * active_map_height_);
449 
450   // Mark updated areas active.
451   for (webrtc::DesktopRegion::Iterator r(updated_region); !r.IsAtEnd();
452        r.Advance()) {
453     const webrtc::DesktopRect& rect = r.rect();
454     int left = rect.left() / kMacroBlockSize;
455     int right = (rect.right() - 1) / kMacroBlockSize;
456     int top = rect.top() / kMacroBlockSize;
457     int bottom = (rect.bottom() - 1) / kMacroBlockSize;
458     DCHECK_LT(right, active_map_width_);
459     DCHECK_LT(bottom, active_map_height_);
460 
461     uint8* map = active_map_.get() + top * active_map_width_;
462     for (int y = top; y <= bottom; ++y) {
463       for (int x = left; x <= right; ++x)
464         map[x] = 1;
465       map += active_map_width_;
466     }
467   }
468 }
469 
470 }  // namespace remoting
471