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 3 // found in the LICENSE file. 4 5 #include "remoting/base/util.h" 6 7 #include <math.h> 8 9 #include "base/logging.h" 10 #include "base/strings/stringprintf.h" 11 #include "base/time/time.h" 12 #include "media/base/video_frame.h" 13 #include "media/base/yuv_convert.h" 14 #include "third_party/libyuv/include/libyuv/convert.h" 15 #include "third_party/webrtc/modules/desktop_capture/desktop_region.h" 16 17 using media::VideoFrame; 18 19 namespace remoting { 20 21 enum { kBytesPerPixelRGB32 = 4 }; 22 23 // Do not write LOG messages in this routine since it is called from within 24 // our LOG message handler. Bad things will happen. 25 std::string GetTimestampString() { 26 base::Time t = base::Time::NowFromSystemTime(); 27 base::Time::Exploded tex; 28 t.LocalExplode(&tex); 29 return base::StringPrintf("%02d%02d/%02d%02d%02d:", 30 tex.month, tex.day_of_month, 31 tex.hour, tex.minute, tex.second); 32 } 33 34 int CalculateRGBOffset(int x, int y, int stride) { 35 return stride * y + kBytesPerPixelRGB32 * x; 36 } 37 38 int CalculateYOffset(int x, int y, int stride) { 39 DCHECK(((x & 1) == 0) && ((y & 1) == 0)); 40 return stride * y + x; 41 } 42 43 int CalculateUVOffset(int x, int y, int stride) { 44 DCHECK(((x & 1) == 0) && ((y & 1) == 0)); 45 return stride * y / 2 + x / 2; 46 } 47 48 void ConvertRGB32ToYUVWithRect(const uint8* rgb_plane, 49 uint8* y_plane, 50 uint8* u_plane, 51 uint8* v_plane, 52 int x, 53 int y, 54 int width, 55 int height, 56 int rgb_stride, 57 int y_stride, 58 int uv_stride) { 59 int rgb_offset = CalculateRGBOffset(x, y, rgb_stride); 60 int y_offset = CalculateYOffset(x, y, y_stride); 61 int uv_offset = CalculateUVOffset(x, y, uv_stride);; 62 63 libyuv::ARGBToI420(rgb_plane + rgb_offset, rgb_stride, 64 y_plane + y_offset, y_stride, 65 u_plane + uv_offset, uv_stride, 66 v_plane + uv_offset, uv_stride, 67 width, height); 68 } 69 70 void ConvertAndScaleYUVToRGB32Rect( 71 const uint8* source_yplane, 72 const uint8* source_uplane, 73 const uint8* source_vplane, 74 int source_ystride, 75 int source_uvstride, 76 const webrtc::DesktopSize& source_size, 77 const webrtc::DesktopRect& source_buffer_rect, 78 uint8* dest_buffer, 79 int dest_stride, 80 const webrtc::DesktopSize& dest_size, 81 const webrtc::DesktopRect& dest_buffer_rect, 82 const webrtc::DesktopRect& dest_rect) { 83 // N.B. It is caller's responsibility to check if strides are large enough. We 84 // cannot do it here anyway. 85 DCHECK(DoesRectContain(webrtc::DesktopRect::MakeSize(source_size), 86 source_buffer_rect)); 87 DCHECK(DoesRectContain(webrtc::DesktopRect::MakeSize(dest_size), 88 dest_buffer_rect)); 89 DCHECK(DoesRectContain(dest_buffer_rect, dest_rect)); 90 DCHECK(DoesRectContain(ScaleRect(source_buffer_rect, source_size, dest_size), 91 dest_rect)); 92 93 // If the source and/or destination buffers don't start at (0, 0) 94 // offset the pointers to pretend we have complete buffers. 95 int y_offset = - CalculateYOffset(source_buffer_rect.left(), 96 source_buffer_rect.top(), 97 source_ystride); 98 int uv_offset = - CalculateUVOffset(source_buffer_rect.left(), 99 source_buffer_rect.top(), 100 source_uvstride); 101 int rgb_offset = - CalculateRGBOffset(dest_buffer_rect.left(), 102 dest_buffer_rect.top(), 103 dest_stride); 104 105 // See if scaling is needed. 106 if (source_size.equals(dest_size)) { 107 // Calculate the inner rectangle that can be copied by the optimized 108 // libyuv::I420ToARGB(). 109 webrtc::DesktopRect inner_rect = 110 webrtc::DesktopRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left() + 1), 111 RoundToTwosMultiple(dest_rect.top() + 1), 112 dest_rect.right(), dest_rect.bottom()); 113 114 // Offset pointers to point to the top left corner of the inner rectangle. 115 y_offset += CalculateYOffset(inner_rect.left(), inner_rect.top(), 116 source_ystride); 117 uv_offset += CalculateUVOffset(inner_rect.left(), inner_rect.top(), 118 source_uvstride); 119 rgb_offset += CalculateRGBOffset(inner_rect.left(), inner_rect.top(), 120 dest_stride); 121 122 libyuv::I420ToARGB(source_yplane + y_offset, source_ystride, 123 source_uplane + uv_offset, source_uvstride, 124 source_vplane + uv_offset, source_uvstride, 125 dest_buffer + rgb_offset, dest_stride, 126 inner_rect.width(), inner_rect.height()); 127 128 // Now see if some pixels weren't copied due to alignment. 129 if (!dest_rect.equals(inner_rect)) { 130 webrtc::DesktopRect outer_rect = 131 webrtc::DesktopRect::MakeLTRB(RoundToTwosMultiple(dest_rect.left()), 132 RoundToTwosMultiple(dest_rect.top()), 133 dest_rect.right(), dest_rect.bottom()); 134 135 webrtc::DesktopVector offset(outer_rect.left() - inner_rect.left(), 136 outer_rect.top() - inner_rect.top()); 137 138 // Offset the pointers to point to the top left corner of the outer 139 // rectangle. 140 y_offset += CalculateYOffset(offset.x(), offset.y(), source_ystride); 141 uv_offset += CalculateUVOffset(offset.x(), offset.y(), source_uvstride); 142 rgb_offset += CalculateRGBOffset(offset.x(), offset.y(), dest_stride); 143 144 // Draw unaligned edges. 145 webrtc::DesktopRegion edges(dest_rect); 146 edges.Subtract(inner_rect); 147 for (webrtc::DesktopRegion::Iterator i(edges); !i.IsAtEnd(); 148 i.Advance()) { 149 webrtc::DesktopRect rect = i.rect(); 150 rect.Translate(-outer_rect.left(), -outer_rect.top()); 151 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset, 152 source_uplane + uv_offset, 153 source_vplane + uv_offset, 154 dest_buffer + rgb_offset, 155 source_size.width(), 156 source_size.height(), 157 dest_size.width(), 158 dest_size.height(), 159 rect.left(), 160 rect.top(), 161 rect.right(), 162 rect.bottom(), 163 source_ystride, 164 source_uvstride, 165 dest_stride); 166 } 167 } 168 } else { 169 media::ScaleYUVToRGB32WithRect(source_yplane + y_offset, 170 source_uplane + uv_offset, 171 source_vplane + uv_offset, 172 dest_buffer + rgb_offset, 173 source_size.width(), 174 source_size.height(), 175 dest_size.width(), 176 dest_size.height(), 177 dest_rect.left(), 178 dest_rect.top(), 179 dest_rect.right(), 180 dest_rect.bottom(), 181 source_ystride, 182 source_uvstride, 183 dest_stride); 184 } 185 } 186 187 int RoundToTwosMultiple(int x) { 188 return x & (~1); 189 } 190 191 webrtc::DesktopRect AlignRect(const webrtc::DesktopRect& rect) { 192 int x = RoundToTwosMultiple(rect.left()); 193 int y = RoundToTwosMultiple(rect.top()); 194 int right = RoundToTwosMultiple(rect.right() + 1); 195 int bottom = RoundToTwosMultiple(rect.bottom() + 1); 196 return webrtc::DesktopRect::MakeLTRB(x, y, right, bottom); 197 } 198 199 webrtc::DesktopRect ScaleRect(const webrtc::DesktopRect& rect, 200 const webrtc::DesktopSize& in_size, 201 const webrtc::DesktopSize& out_size) { 202 int left = (rect.left() * out_size.width()) / in_size.width(); 203 int top = (rect.top() * out_size.height()) / in_size.height(); 204 int right = (rect.right() * out_size.width() + in_size.width() - 1) / 205 in_size.width(); 206 int bottom = (rect.bottom() * out_size.height() + in_size.height() - 1) / 207 in_size.height(); 208 return webrtc::DesktopRect::MakeLTRB(left, top, right, bottom); 209 } 210 211 void CopyRGB32Rect(const uint8* source_buffer, 212 int source_stride, 213 const webrtc::DesktopRect& source_buffer_rect, 214 uint8* dest_buffer, 215 int dest_stride, 216 const webrtc::DesktopRect& dest_buffer_rect, 217 const webrtc::DesktopRect& dest_rect) { 218 DCHECK(DoesRectContain(dest_buffer_rect, dest_rect)); 219 DCHECK(DoesRectContain(source_buffer_rect, dest_rect)); 220 221 // Get the address of the starting point. 222 source_buffer += CalculateRGBOffset( 223 dest_rect.left() - source_buffer_rect.left(), 224 dest_rect.top() - source_buffer_rect.top(), 225 source_stride); 226 dest_buffer += CalculateRGBOffset( 227 dest_rect.left() - dest_buffer_rect.left(), 228 dest_rect.top() - dest_buffer_rect.top(), 229 source_stride); 230 231 // Copy pixels in the rectangle line by line. 232 const int bytes_per_line = kBytesPerPixelRGB32 * dest_rect.width(); 233 for (int i = 0 ; i < dest_rect.height(); ++i) { 234 memcpy(dest_buffer, source_buffer, bytes_per_line); 235 source_buffer += source_stride; 236 dest_buffer += dest_stride; 237 } 238 } 239 240 std::string ReplaceLfByCrLf(const std::string& in) { 241 std::string out; 242 out.resize(2 * in.size()); 243 char* out_p_begin = &out[0]; 244 char* out_p = out_p_begin; 245 const char* in_p_begin = &in[0]; 246 const char* in_p_end = &in[in.size()]; 247 for (const char* in_p = in_p_begin; in_p < in_p_end; ++in_p) { 248 char c = *in_p; 249 if (c == '\n') { 250 *out_p++ = '\r'; 251 } 252 *out_p++ = c; 253 } 254 out.resize(out_p - out_p_begin); 255 return out; 256 } 257 258 std::string ReplaceCrLfByLf(const std::string& in) { 259 std::string out; 260 out.resize(in.size()); 261 char* out_p_begin = &out[0]; 262 char* out_p = out_p_begin; 263 const char* in_p_begin = &in[0]; 264 const char* in_p_end = &in[in.size()]; 265 for (const char* in_p = in_p_begin; in_p < in_p_end; ++in_p) { 266 char c = *in_p; 267 if ((c == '\r') && (in_p + 1 < in_p_end) && (*(in_p + 1) == '\n')) { 268 *out_p++ = '\n'; 269 ++in_p; 270 } else { 271 *out_p++ = c; 272 } 273 } 274 out.resize(out_p - out_p_begin); 275 return out; 276 } 277 278 bool StringIsUtf8(const char* data, size_t length) { 279 const char* ptr = data; 280 const char* ptr_end = data + length; 281 while (ptr != ptr_end) { 282 if ((*ptr & 0x80) == 0) { 283 // Single-byte symbol. 284 ++ptr; 285 } else if ((*ptr & 0xc0) == 0x80 || (*ptr & 0xfe) == 0xfe) { 286 // Invalid first byte. 287 return false; 288 } else { 289 // First byte of a multi-byte symbol. The bits from 2 to 6 are the count 290 // of continuation bytes (up to 5 of them). 291 for (char first = *ptr << 1; first & 0x80; first <<= 1) { 292 ++ptr; 293 294 // Missing continuation byte. 295 if (ptr == ptr_end) 296 return false; 297 298 // Invalid continuation byte. 299 if ((*ptr & 0xc0) != 0x80) 300 return false; 301 } 302 303 ++ptr; 304 } 305 } 306 307 return true; 308 } 309 310 bool DoesRectContain(const webrtc::DesktopRect& a, 311 const webrtc::DesktopRect& b) { 312 webrtc::DesktopRect intersection(a); 313 intersection.IntersectWith(b); 314 return intersection.equals(b); 315 } 316 317 } // namespace remoting 318