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/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"
17 using media::VideoFrame
;
21 enum { kBytesPerPixelRGB32
= 4 };
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
;
29 return base::StringPrintf("%02d%02d/%02d%02d%02d:",
30 tex
.month
, tex
.day_of_month
,
31 tex
.hour
, tex
.minute
, tex
.second
);
34 int CalculateRGBOffset(int x
, int y
, int stride
) {
35 return stride
* y
+ kBytesPerPixelRGB32
* x
;
38 int CalculateYOffset(int x
, int y
, int stride
) {
39 DCHECK(((x
& 1) == 0) && ((y
& 1) == 0));
40 return stride
* y
+ x
;
43 int CalculateUVOffset(int x
, int y
, int stride
) {
44 DCHECK(((x
& 1) == 0) && ((y
& 1) == 0));
45 return stride
* y
/ 2 + x
/ 2;
48 void ConvertRGB32ToYUVWithRect(const uint8
* rgb_plane
,
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
);;
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
,
70 void ConvertAndScaleYUVToRGB32Rect(
71 const uint8
* source_yplane
,
72 const uint8
* source_uplane
,
73 const uint8
* source_vplane
,
76 const webrtc::DesktopSize
& source_size
,
77 const webrtc::DesktopRect
& source_buffer_rect
,
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
),
87 DCHECK(DoesRectContain(webrtc::DesktopRect::MakeSize(dest_size
),
89 DCHECK(DoesRectContain(dest_buffer_rect
, dest_rect
));
90 DCHECK(DoesRectContain(ScaleRect(source_buffer_rect
, source_size
, dest_size
),
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(),
98 int uv_offset
= - CalculateUVOffset(source_buffer_rect
.left(),
99 source_buffer_rect
.top(),
101 int rgb_offset
= - CalculateRGBOffset(dest_buffer_rect
.left(),
102 dest_buffer_rect
.top(),
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());
114 // Offset pointers to point to the top left corner of the inner rectangle.
115 y_offset
+= CalculateYOffset(inner_rect
.left(), inner_rect
.top(),
117 uv_offset
+= CalculateUVOffset(inner_rect
.left(), inner_rect
.top(),
119 rgb_offset
+= CalculateRGBOffset(inner_rect
.left(), inner_rect
.top(),
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());
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());
135 webrtc::DesktopVector
offset(outer_rect
.left() - inner_rect
.left(),
136 outer_rect
.top() - inner_rect
.top());
138 // Offset the pointers to point to the top left corner of the outer
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
);
144 // Draw unaligned edges.
145 webrtc::DesktopRegion
edges(dest_rect
);
146 edges
.Subtract(inner_rect
);
147 for (webrtc::DesktopRegion::Iterator
i(edges
); !i
.IsAtEnd();
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
,
156 source_size
.height(),
169 media::ScaleYUVToRGB32WithRect(source_yplane
+ y_offset
,
170 source_uplane
+ uv_offset
,
171 source_vplane
+ uv_offset
,
172 dest_buffer
+ rgb_offset
,
174 source_size
.height(),
187 int RoundToTwosMultiple(int x
) {
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
);
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) /
206 int bottom
= (rect
.bottom() * out_size
.height() + in_size
.height() - 1) /
208 return webrtc::DesktopRect::MakeLTRB(left
, top
, right
, bottom
);
211 void CopyRGB32Rect(const uint8
* source_buffer
,
213 const webrtc::DesktopRect
& source_buffer_rect
,
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
));
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(),
226 dest_buffer
+= CalculateRGBOffset(
227 dest_rect
.left() - dest_buffer_rect
.left(),
228 dest_rect
.top() - dest_buffer_rect
.top(),
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
;
240 std::string
ReplaceLfByCrLf(const std::string
& in
) {
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
) {
254 out
.resize(out_p
- out_p_begin
);
258 std::string
ReplaceCrLfByLf(const std::string
& in
) {
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
) {
267 if ((c
== '\r') && (in_p
+ 1 < in_p_end
) && (*(in_p
+ 1) == '\n')) {
274 out
.resize(out_p
- out_p_begin
);
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.
285 } else if ((*ptr
& 0xc0) == 0x80 || (*ptr
& 0xfe) == 0xfe) {
286 // Invalid first byte.
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) {
294 // Missing continuation byte.
298 // Invalid continuation byte.
299 if ((*ptr
& 0xc0) != 0x80)
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
);
317 } // namespace remoting