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.
5 #include "remoting/base/util.h"
9 #include "base/logging.h"
10 #include "base/stringprintf.h"
11 #include "base/time.h"
12 #include "media/base/video_frame.h"
13 #include "media/base/yuv_convert.h"
14 #include "third_party/skia/include/core/SkRegion.h"
18 #include <sys/types.h>
20 #endif // defined(OS_POSIX)
22 using media::VideoFrame
;
26 enum { kBytesPerPixelRGB32
= 4 };
28 // Do not write LOG messages in this routine since it is called from within
29 // our LOG message handler. Bad things will happen.
30 std::string
GetTimestampString() {
31 base::Time t
= base::Time::NowFromSystemTime();
32 base::Time::Exploded tex
;
34 return base::StringPrintf("%02d%02d/%02d%02d%02d:",
35 tex
.month
, tex
.day_of_month
,
36 tex
.hour
, tex
.minute
, tex
.second
);
39 int CalculateRGBOffset(int x
, int y
, int stride
) {
40 return stride
* y
+ kBytesPerPixelRGB32
* x
;
43 int CalculateYOffset(int x
, int y
, int stride
) {
44 DCHECK(((x
& 1) == 0) && ((y
& 1) == 0));
45 return stride
* y
+ x
;
48 int CalculateUVOffset(int x
, int y
, int stride
) {
49 DCHECK(((x
& 1) == 0) && ((y
& 1) == 0));
50 return stride
* y
/ 2 + x
/ 2;
53 void ConvertRGB32ToYUVWithRect(const uint8
* rgb_plane
,
64 int rgb_offset
= CalculateRGBOffset(x
, y
, rgb_stride
);
65 int y_offset
= CalculateYOffset(x
, y
, y_stride
);
66 int uv_offset
= CalculateUVOffset(x
, y
, uv_stride
);;
68 media::ConvertRGB32ToYUV(rgb_plane
+ rgb_offset
,
79 void ConvertAndScaleYUVToRGB32Rect(const uint8
* source_yplane
,
80 const uint8
* source_uplane
,
81 const uint8
* source_vplane
,
84 const SkISize
& source_size
,
85 const SkIRect
& source_buffer_rect
,
88 const SkISize
& dest_size
,
89 const SkIRect
& dest_buffer_rect
,
90 const SkIRect
& dest_rect
) {
91 // N.B. It is caller's responsibility to check if strides are large enough. We
92 // cannot do it here anyway.
93 DCHECK(SkIRect::MakeSize(source_size
).contains(source_buffer_rect
));
94 DCHECK(SkIRect::MakeSize(dest_size
).contains(dest_buffer_rect
));
95 DCHECK(dest_buffer_rect
.contains(dest_rect
));
96 DCHECK(ScaleRect(source_buffer_rect
, source_size
, dest_size
).
99 // If the source and/or destination buffers don't start at (0, 0)
100 // offset the pointers to pretend we have complete buffers.
101 int y_offset
= - CalculateYOffset(source_buffer_rect
.x(),
102 source_buffer_rect
.y(),
104 int uv_offset
= - CalculateUVOffset(source_buffer_rect
.x(),
105 source_buffer_rect
.y(),
107 int rgb_offset
= - CalculateRGBOffset(dest_buffer_rect
.x(),
108 dest_buffer_rect
.y(),
111 // See if scaling is needed.
112 if (source_size
== dest_size
) {
113 // Calculate the inner rectangle that can be copied by the optimized
114 // ConvertYUVToRGB32().
116 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect
.left() + 1),
117 RoundToTwosMultiple(dest_rect
.top() + 1),
121 // Offset pointers to point to the top left corner of the inner rectangle.
122 y_offset
+= CalculateYOffset(inner_rect
.x(), inner_rect
.y(),
124 uv_offset
+= CalculateUVOffset(inner_rect
.x(), inner_rect
.y(),
126 rgb_offset
+= CalculateRGBOffset(inner_rect
.x(), inner_rect
.y(),
129 media::ConvertYUVToRGB32(source_yplane
+ y_offset
,
130 source_uplane
+ uv_offset
,
131 source_vplane
+ uv_offset
,
132 dest_buffer
+ rgb_offset
,
140 // Now see if some pixels weren't copied due to alignment.
141 if (dest_rect
!= inner_rect
) {
143 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect
.left()),
144 RoundToTwosMultiple(dest_rect
.top()),
148 SkIPoint offset
= SkIPoint::Make(outer_rect
.x() - inner_rect
.x(),
149 outer_rect
.y() - inner_rect
.y());
151 // Offset the pointers to point to the top left corner of the outer
153 y_offset
+= CalculateYOffset(offset
.x(), offset
.y(), source_ystride
);
154 uv_offset
+= CalculateUVOffset(offset
.x(), offset
.y(), source_uvstride
);
155 rgb_offset
+= CalculateRGBOffset(offset
.x(), offset
.y(), dest_stride
);
157 // Draw unaligned edges.
158 SkRegion
edges(dest_rect
);
159 edges
.op(inner_rect
, SkRegion::kDifference_Op
);
160 for (SkRegion::Iterator
i(edges
); !i
.done(); i
.next()) {
161 SkIRect
rect(i
.rect());
162 rect
.offset(- outer_rect
.left(), - outer_rect
.top());
163 media::ScaleYUVToRGB32WithRect(source_yplane
+ y_offset
,
164 source_uplane
+ uv_offset
,
165 source_vplane
+ uv_offset
,
166 dest_buffer
+ rgb_offset
,
168 source_size
.height(),
181 media::ScaleYUVToRGB32WithRect(source_yplane
+ y_offset
,
182 source_uplane
+ uv_offset
,
183 source_vplane
+ uv_offset
,
184 dest_buffer
+ rgb_offset
,
186 source_size
.height(),
199 int RoundToTwosMultiple(int x
) {
203 SkIRect
AlignRect(const SkIRect
& rect
) {
204 int x
= RoundToTwosMultiple(rect
.left());
205 int y
= RoundToTwosMultiple(rect
.top());
206 int right
= RoundToTwosMultiple(rect
.right() + 1);
207 int bottom
= RoundToTwosMultiple(rect
.bottom() + 1);
208 return SkIRect::MakeLTRB(x
, y
, right
, bottom
);
211 SkIRect
ScaleRect(const SkIRect
& rect
,
212 const SkISize
& in_size
,
213 const SkISize
& out_size
) {
214 int left
= (rect
.left() * out_size
.width()) / in_size
.width();
215 int top
= (rect
.top() * out_size
.height()) / in_size
.height();
216 int right
= (rect
.right() * out_size
.width() + in_size
.width() - 1) /
218 int bottom
= (rect
.bottom() * out_size
.height() + in_size
.height() - 1) /
220 return SkIRect::MakeLTRB(left
, top
, right
, bottom
);
223 void CopyRGB32Rect(const uint8
* source_buffer
,
225 const SkIRect
& source_buffer_rect
,
228 const SkIRect
& dest_buffer_rect
,
229 const SkIRect
& dest_rect
) {
230 DCHECK(dest_buffer_rect
.contains(dest_rect
));
231 DCHECK(source_buffer_rect
.contains(dest_rect
));
233 // Get the address of the starting point.
234 source_buffer
+= CalculateRGBOffset(dest_rect
.x() - source_buffer_rect
.x(),
235 dest_rect
.y() - source_buffer_rect
.y(),
237 dest_buffer
+= CalculateRGBOffset(dest_rect
.x() - dest_buffer_rect
.x(),
238 dest_rect
.y() - dest_buffer_rect
.y(),
241 // Copy pixels in the rectangle line by line.
242 const int bytes_per_line
= kBytesPerPixelRGB32
* dest_rect
.width();
243 for (int i
= 0 ; i
< dest_rect
.height(); ++i
) {
244 memcpy(dest_buffer
, source_buffer
, bytes_per_line
);
245 source_buffer
+= source_stride
;
246 dest_buffer
+= dest_stride
;
250 std::string
ReplaceLfByCrLf(const std::string
& in
) {
252 out
.resize(2 * in
.size());
253 char* out_p_begin
= &out
[0];
254 char* out_p
= out_p_begin
;
255 const char* in_p_begin
= &in
[0];
256 const char* in_p_end
= &in
[in
.size()];
257 for (const char* in_p
= in_p_begin
; in_p
< in_p_end
; ++in_p
) {
264 out
.resize(out_p
- out_p_begin
);
268 std::string
ReplaceCrLfByLf(const std::string
& in
) {
270 out
.resize(in
.size());
271 char* out_p_begin
= &out
[0];
272 char* out_p
= out_p_begin
;
273 const char* in_p_begin
= &in
[0];
274 const char* in_p_end
= &in
[in
.size()];
275 for (const char* in_p
= in_p_begin
; in_p
< in_p_end
; ++in_p
) {
277 if ((c
== '\r') && (in_p
+ 1 < in_p_end
) && (*(in_p
+ 1) == '\n')) {
284 out
.resize(out_p
- out_p_begin
);
288 bool StringIsUtf8(const char* data
, size_t length
) {
289 const char* ptr
= data
;
290 const char* ptr_end
= data
+ length
;
291 while (ptr
!= ptr_end
) {
292 if ((*ptr
& 0x80) == 0) {
293 // Single-byte symbol.
295 } else if ((*ptr
& 0xc0) == 0x80 || (*ptr
& 0xfe) == 0xfe) {
296 // Invalid first byte.
299 // First byte of a multi-byte symbol. The bits from 2 to 6 are the count
300 // of continuation bytes (up to 5 of them).
301 for (char first
= *ptr
<< 1; first
& 0x80; first
<<= 1) {
304 // Missing continuation byte.
308 // Invalid continuation byte.
309 if ((*ptr
& 0xc0) != 0x80)
320 std::string
GetUsername() {
321 #if defined(OS_POSIX)
322 long buf_size
= sysconf(_SC_GETPW_R_SIZE_MAX
);
324 return std::string();
325 scoped_ptr
<char[]> buf(new char[buf_size
]);
326 struct passwd passwd
;
327 struct passwd
* passwd_result
= NULL
;
328 getpwuid_r(getuid(), &passwd
, buf
.get(), buf_size
, &passwd_result
);
330 return std::string();
331 return std::string(passwd_result
->pw_name
);
332 #else // !defined(OS_POSIX)
333 return std::string();
334 #endif // defined(OS_POSIX)
337 } // namespace remoting