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"
16 using media::VideoFrame
;
20 enum { kBytesPerPixelRGB32
= 4 };
22 // Do not write LOG messages in this routine since it is called from within
23 // our LOG message handler. Bad things will happen.
24 std::string
GetTimestampString() {
25 base::Time t
= base::Time::NowFromSystemTime();
26 base::Time::Exploded tex
;
28 return StringPrintf("%02d%02d/%02d%02d%02d:",
29 tex
.month
, tex
.day_of_month
,
30 tex
.hour
, tex
.minute
, tex
.second
);
33 int CalculateRGBOffset(int x
, int y
, int stride
) {
34 return stride
* y
+ kBytesPerPixelRGB32
* x
;
37 int CalculateYOffset(int x
, int y
, int stride
) {
38 DCHECK(((x
& 1) == 0) && ((y
& 1) == 0));
39 return stride
* y
+ x
;
42 int CalculateUVOffset(int x
, int y
, int stride
) {
43 DCHECK(((x
& 1) == 0) && ((y
& 1) == 0));
44 return stride
* y
/ 2 + x
/ 2;
47 void ConvertRGB32ToYUVWithRect(const uint8
* rgb_plane
,
58 int rgb_offset
= CalculateRGBOffset(x
, y
, rgb_stride
);
59 int y_offset
= CalculateYOffset(x
, y
, y_stride
);
60 int uv_offset
= CalculateUVOffset(x
, y
, uv_stride
);;
62 media::ConvertRGB32ToYUV(rgb_plane
+ rgb_offset
,
73 void ConvertAndScaleYUVToRGB32Rect(const uint8
* source_yplane
,
74 const uint8
* source_uplane
,
75 const uint8
* source_vplane
,
78 const SkISize
& source_size
,
79 const SkIRect
& source_buffer_rect
,
82 const SkISize
& dest_size
,
83 const SkIRect
& dest_buffer_rect
,
84 const SkIRect
& dest_rect
) {
85 // N.B. It is caller's responsibility to check if strides are large enough. We
86 // cannot do it here anyway.
87 DCHECK(SkIRect::MakeSize(source_size
).contains(source_buffer_rect
));
88 DCHECK(SkIRect::MakeSize(dest_size
).contains(dest_buffer_rect
));
89 DCHECK(dest_buffer_rect
.contains(dest_rect
));
90 DCHECK(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
.x(),
96 source_buffer_rect
.y(),
98 int uv_offset
= - CalculateUVOffset(source_buffer_rect
.x(),
99 source_buffer_rect
.y(),
101 int rgb_offset
= - CalculateRGBOffset(dest_buffer_rect
.x(),
102 dest_buffer_rect
.y(),
105 // See if scaling is needed.
106 if (source_size
== dest_size
) {
107 // Calculate the inner rectangle that can be copied by the optimized
108 // ConvertYUVToRGB32().
110 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect
.left() + 1),
111 RoundToTwosMultiple(dest_rect
.top() + 1),
115 // Offset pointers to point to the top left corner of the inner rectangle.
116 y_offset
+= CalculateYOffset(inner_rect
.x(), inner_rect
.y(),
118 uv_offset
+= CalculateUVOffset(inner_rect
.x(), inner_rect
.y(),
120 rgb_offset
+= CalculateRGBOffset(inner_rect
.x(), inner_rect
.y(),
123 media::ConvertYUVToRGB32(source_yplane
+ y_offset
,
124 source_uplane
+ uv_offset
,
125 source_vplane
+ uv_offset
,
126 dest_buffer
+ rgb_offset
,
134 // Now see if some pixels weren't copied due to alignment.
135 if (dest_rect
!= inner_rect
) {
137 SkIRect::MakeLTRB(RoundToTwosMultiple(dest_rect
.left()),
138 RoundToTwosMultiple(dest_rect
.top()),
142 SkIPoint offset
= SkIPoint::Make(outer_rect
.x() - inner_rect
.x(),
143 outer_rect
.y() - inner_rect
.y());
145 // Offset the pointers to point to the top left corner of the outer
147 y_offset
+= CalculateYOffset(offset
.x(), offset
.y(), source_ystride
);
148 uv_offset
+= CalculateUVOffset(offset
.x(), offset
.y(), source_uvstride
);
149 rgb_offset
+= CalculateRGBOffset(offset
.x(), offset
.y(), dest_stride
);
151 // Draw unaligned edges.
152 SkRegion
edges(dest_rect
);
153 edges
.op(inner_rect
, SkRegion::kDifference_Op
);
154 for (SkRegion::Iterator
i(edges
); !i
.done(); i
.next()) {
155 SkIRect
rect(i
.rect());
156 rect
.offset(- outer_rect
.left(), - outer_rect
.top());
157 media::ScaleYUVToRGB32WithRect(source_yplane
+ y_offset
,
158 source_uplane
+ uv_offset
,
159 source_vplane
+ uv_offset
,
160 dest_buffer
+ rgb_offset
,
162 source_size
.height(),
175 media::ScaleYUVToRGB32WithRect(source_yplane
+ y_offset
,
176 source_uplane
+ uv_offset
,
177 source_vplane
+ uv_offset
,
178 dest_buffer
+ rgb_offset
,
180 source_size
.height(),
193 int RoundToTwosMultiple(int x
) {
197 SkIRect
AlignRect(const SkIRect
& rect
) {
198 int x
= RoundToTwosMultiple(rect
.left());
199 int y
= RoundToTwosMultiple(rect
.top());
200 int right
= RoundToTwosMultiple(rect
.right() + 1);
201 int bottom
= RoundToTwosMultiple(rect
.bottom() + 1);
202 return SkIRect::MakeLTRB(x
, y
, right
, bottom
);
205 SkIRect
ScaleRect(const SkIRect
& rect
,
206 const SkISize
& in_size
,
207 const SkISize
& out_size
) {
208 int left
= (rect
.left() * out_size
.width()) / in_size
.width();
209 int top
= (rect
.top() * out_size
.height()) / in_size
.height();
210 int right
= (rect
.right() * out_size
.width() + in_size
.width() - 1) /
212 int bottom
= (rect
.bottom() * out_size
.height() + in_size
.height() - 1) /
214 return SkIRect::MakeLTRB(left
, top
, right
, bottom
);
217 void CopyRGB32Rect(const uint8
* source_buffer
,
219 const SkIRect
& source_buffer_rect
,
222 const SkIRect
& dest_buffer_rect
,
223 const SkIRect
& dest_rect
) {
224 DCHECK(dest_buffer_rect
.contains(dest_rect
));
225 DCHECK(source_buffer_rect
.contains(dest_rect
));
227 // Get the address of the starting point.
228 source_buffer
+= CalculateRGBOffset(dest_rect
.x() - source_buffer_rect
.x(),
229 dest_rect
.y() - source_buffer_rect
.y(),
231 dest_buffer
+= CalculateRGBOffset(dest_rect
.x() - dest_buffer_rect
.x(),
232 dest_rect
.y() - dest_buffer_rect
.y(),
235 // Copy pixels in the rectangle line by line.
236 const int bytes_per_line
= kBytesPerPixelRGB32
* dest_rect
.width();
237 for (int i
= 0 ; i
< dest_rect
.height(); ++i
) {
238 memcpy(dest_buffer
, source_buffer
, bytes_per_line
);
239 source_buffer
+= source_stride
;
240 dest_buffer
+= dest_stride
;
244 std::string
ReplaceLfByCrLf(const std::string
& in
) {
246 out
.resize(2 * in
.size());
247 char* out_p_begin
= &out
[0];
248 char* out_p
= out_p_begin
;
249 const char* in_p_begin
= &in
[0];
250 const char* in_p_end
= &in
[in
.size()];
251 for (const char* in_p
= in_p_begin
; in_p
< in_p_end
; ++in_p
) {
258 out
.resize(out_p
- out_p_begin
);
262 std::string
ReplaceCrLfByLf(const std::string
& in
) {
264 out
.resize(in
.size());
265 char* out_p_begin
= &out
[0];
266 char* out_p
= out_p_begin
;
267 const char* in_p_begin
= &in
[0];
268 const char* in_p_end
= &in
[in
.size()];
269 for (const char* in_p
= in_p_begin
; in_p
< in_p_end
; ++in_p
) {
271 if ((c
== '\r') && (in_p
+ 1 < in_p_end
) && (*(in_p
+ 1) == '\n')) {
278 out
.resize(out_p
- out_p_begin
);
282 bool StringIsUtf8(const char* data
, size_t length
) {
283 const char* ptr
= data
;
284 const char* ptr_end
= data
+ length
;
285 while (ptr
!= ptr_end
) {
286 if ((*ptr
& 0x80) == 0) {
287 // Single-byte symbol.
289 } else if ((*ptr
& 0xc0) == 0x80 || (*ptr
& 0xfe) == 0xfe) {
290 // Invalid first byte.
293 // First byte of a multi-byte symbol. The bits from 2 to 6 are the count
294 // of continuation bytes (up to 5 of them).
295 for (char first
= *ptr
<< 1; first
& 0x80; first
<<= 1) {
298 // Missing continuation byte.
302 // Invalid continuation byte.
303 if ((*ptr
& 0xc0) != 0x80)
314 } // namespace remoting