1 // Copyright 2015 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 "media/cast/sender/performance_metrics_overlay.h"
10 #include "base/logging.h"
11 #include "base/numerics/safe_conversions.h"
12 #include "base/strings/stringprintf.h"
13 #include "media/base/video_frame.h"
20 const int kScale
= 4; // Physical pixels per one logical pixel.
21 const int kCharacterWidth
= 3; // Logical pixel width of one character.
22 const int kCharacterHeight
= 5; // Logical pixel height of one character.
23 const int kCharacterSpacing
= 1; // Logical pixels between each character.
24 const int kLineSpacing
= 2; // Logical pixels between each line of characters.
25 const int kPlane
= 0; // Y-plane in YUV formats.
27 // For each pixel in the |rect| (logical coordinates), either decrease the
28 // intensity or increase it so that the resulting pixel has a perceivably
29 // different value than it did before. |p_ul| is a pointer to the pixel at
30 // coordinate (0,0) in a single-channel 8bpp bitmap. |stride| is the number of
31 // bytes per row in the output bitmap.
32 void DivergePixels(const gfx::Rect
& rect
, uint8
* p_ul
, int stride
) {
36 // These constants and heuristics were chosen based on experimenting with a
37 // wide variety of content, and converging on a readable result. The amount
38 // by which the darker pixels are changed is less because each unit of change
39 // has a larger visual impact on the darker end of the spectrum. Each pixel's
40 // intensity value is changed as follows:
42 // [16,31] --> [32,63] (always a difference of +16)
43 // [32,64] --> 16 (a difference between -16 and -48)
44 // [65,235] --> [17,187] (always a difference of -48)
45 const int kDivergeDownThreshold
= 32;
46 const int kDivergeDownAmount
= 48;
47 const int kDivergeUpAmount
= 32;
48 const int kMinIntensity
= 16;
50 const int top
= rect
.y() * kScale
;
51 const int bottom
= rect
.bottom() * kScale
;
52 const int left
= rect
.x() * kScale
;
53 const int right
= rect
.right() * kScale
;
54 for (int y
= top
; y
< bottom
; ++y
) {
55 uint8
* const p_l
= p_ul
+ y
* stride
;
56 for (int x
= left
; x
< right
; ++x
) {
57 int intensity
= p_l
[x
];
58 if (intensity
>= kDivergeDownThreshold
)
59 intensity
= std::max(kMinIntensity
, intensity
- kDivergeDownAmount
);
61 intensity
+= kDivergeUpAmount
;
62 p_l
[x
] = static_cast<uint8
>(intensity
);
67 // Render |line| into |frame| at physical pixel row |top| and aligned to the
68 // right edge. Only number digits and a smattering of punctuation characters
70 void RenderLineOfText(const std::string
& line
, int top
, VideoFrame
* frame
) {
71 // Compute number of physical pixels wide the rendered |line| would be,
73 const int line_width
=
74 (((kCharacterWidth
+ kCharacterSpacing
) * static_cast<int>(line
.size())) +
75 kCharacterSpacing
) * kScale
;
77 // Determine if any characters would render past the left edge of the frame,
78 // and compute the index of the first character to be rendered.
79 const int pixels_per_char
= (kCharacterWidth
+ kCharacterSpacing
) * kScale
;
80 const size_t first_idx
= (line_width
< frame
->visible_rect().width()) ? 0u :
82 ((line_width
- frame
->visible_rect().width()) / pixels_per_char
) + 1);
84 // Compute the pointer to the pixel at the upper-left corner of the first
85 // character to be rendered.
86 const int stride
= frame
->stride(kPlane
);
88 // Start at the first pixel in the first row...
89 frame
->visible_data(kPlane
) + (stride
* top
)
90 // ...now move to the right edge of the visible part of the frame...
91 + frame
->visible_rect().width()
92 // ...now move left to where line[0] would be rendered...
94 // ...now move right to where line[first_idx] would be rendered.
95 + first_idx
* pixels_per_char
;
97 // Render each character.
98 for (size_t i
= first_idx
; i
< line
.size(); ++i
, p_ul
+= pixels_per_char
) {
101 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
102 DivergePixels(gfx::Rect(0, 1, 1, 3), p_ul
, stride
);
103 DivergePixels(gfx::Rect(2, 1, 1, 3), p_ul
, stride
);
104 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
107 DivergePixels(gfx::Rect(1, 0, 1, 5), p_ul
, stride
);
110 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
111 DivergePixels(gfx::Rect(2, 1, 1, 1), p_ul
, stride
);
112 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
113 DivergePixels(gfx::Rect(0, 3, 1, 1), p_ul
, stride
);
114 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
117 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
118 DivergePixels(gfx::Rect(2, 1, 1, 1), p_ul
, stride
);
119 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
120 DivergePixels(gfx::Rect(2, 3, 1, 1), p_ul
, stride
);
121 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
124 DivergePixels(gfx::Rect(0, 0, 1, 2), p_ul
, stride
);
125 DivergePixels(gfx::Rect(2, 0, 1, 5), p_ul
, stride
);
126 DivergePixels(gfx::Rect(0, 2, 2, 1), p_ul
, stride
);
129 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
130 DivergePixels(gfx::Rect(0, 1, 1, 1), p_ul
, stride
);
131 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
132 DivergePixels(gfx::Rect(2, 3, 1, 1), p_ul
, stride
);
133 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
136 DivergePixels(gfx::Rect(1, 0, 2, 1), p_ul
, stride
);
137 DivergePixels(gfx::Rect(0, 1, 1, 1), p_ul
, stride
);
138 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
139 DivergePixels(gfx::Rect(0, 3, 1, 1), p_ul
, stride
);
140 DivergePixels(gfx::Rect(2, 3, 1, 1), p_ul
, stride
);
141 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
144 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
145 DivergePixels(gfx::Rect(2, 1, 1, 2), p_ul
, stride
);
146 DivergePixels(gfx::Rect(1, 3, 1, 2), p_ul
, stride
);
149 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
150 DivergePixels(gfx::Rect(0, 1, 1, 1), p_ul
, stride
);
151 DivergePixels(gfx::Rect(2, 1, 1, 1), p_ul
, stride
);
152 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
153 DivergePixels(gfx::Rect(0, 3, 1, 1), p_ul
, stride
);
154 DivergePixels(gfx::Rect(2, 3, 1, 1), p_ul
, stride
);
155 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
158 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
159 DivergePixels(gfx::Rect(0, 1, 1, 1), p_ul
, stride
);
160 DivergePixels(gfx::Rect(2, 1, 1, 1), p_ul
, stride
);
161 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
162 DivergePixels(gfx::Rect(2, 3, 1, 1), p_ul
, stride
);
163 DivergePixels(gfx::Rect(0, 4, 2, 1), p_ul
, stride
);
167 DivergePixels(gfx::Rect(0, 0, 3, 1), p_ul
, stride
);
168 DivergePixels(gfx::Rect(0, 1, 1, 1), p_ul
, stride
);
169 DivergePixels(gfx::Rect(0, 2, 2, 1), p_ul
, stride
);
170 DivergePixels(gfx::Rect(0, 3, 1, 1), p_ul
, stride
);
171 DivergePixels(gfx::Rect(0, 4, 3, 1), p_ul
, stride
);
174 DivergePixels(gfx::Rect(1, 4, 1, 1), p_ul
, stride
);
177 DivergePixels(gfx::Rect(1, 1, 1, 1), p_ul
, stride
);
178 DivergePixels(gfx::Rect(1, 3, 1, 1), p_ul
, stride
);
179 // ...fall through...
181 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul
, stride
);
184 DivergePixels(gfx::Rect(0, 1, 1, 1), p_ul
, stride
);
185 DivergePixels(gfx::Rect(2, 1, 1, 1), p_ul
, stride
);
186 DivergePixels(gfx::Rect(1, 2, 1, 1), p_ul
, stride
);
187 DivergePixels(gfx::Rect(0, 3, 1, 1), p_ul
, stride
);
188 DivergePixels(gfx::Rect(2, 3, 1, 1), p_ul
, stride
);
191 DivergePixels(gfx::Rect(1, 1, 1, 1), p_ul
, stride
);
192 DivergePixels(gfx::Rect(1, 3, 1, 1), p_ul
, stride
);
195 DivergePixels(gfx::Rect(0, 0, 1, 1), p_ul
, stride
);
196 DivergePixels(gfx::Rect(2, 1, 1, 1), p_ul
, stride
);
197 DivergePixels(gfx::Rect(1, 2, 1, 1), p_ul
, stride
);
198 DivergePixels(gfx::Rect(0, 3, 1, 1), p_ul
, stride
);
199 DivergePixels(gfx::Rect(2, 4, 1, 1), p_ul
, stride
);
210 void MaybeRenderPerformanceMetricsOverlay(int target_bitrate
,
212 double deadline_utilization
,
213 double lossy_utilization
,
215 if (VideoFrame::PlaneHorizontalBitsPerPixel(frame
->format(), kPlane
) != 8) {
216 DLOG(WARNING
) << "Cannot render overlay: Plane " << kPlane
<< " not 8bpp.";
220 // Compute the physical pixel top row for the bottom-most line of text.
221 const int line_height
= (kCharacterHeight
+ kLineSpacing
) * kScale
;
222 int top
= frame
->visible_rect().height() - line_height
;
223 if (top
< 0 || !VLOG_IS_ON(1))
226 // Line 3: Frame resolution and timestamp.
227 base::TimeDelta rem
= frame
->timestamp();
228 const int minutes
= rem
.InMinutes();
229 rem
-= base::TimeDelta::FromMinutes(minutes
);
230 const int seconds
= static_cast<int>(rem
.InSeconds());
231 rem
-= base::TimeDelta::FromSeconds(seconds
);
232 const int hundredth_seconds
= static_cast<int>(rem
.InMilliseconds() / 10);
233 RenderLineOfText(base::StringPrintf("%dx%d %d:%02d.%02d",
234 frame
->visible_rect().width(),
235 frame
->visible_rect().height(),
242 // Move up one line's worth of pixels.
244 if (top
< 0 || !VLOG_IS_ON(2))
247 // Line 2: Capture/frame duration and target bitrate.
248 int capture_duration_ms
= 0;
249 base::TimeTicks capture_begin_time
, capture_end_time
;
250 if (frame
->metadata()->GetTimeTicks(VideoFrameMetadata::CAPTURE_BEGIN_TIME
,
251 &capture_begin_time
) &&
252 frame
->metadata()->GetTimeTicks(VideoFrameMetadata::CAPTURE_END_TIME
,
253 &capture_end_time
)) {
254 capture_duration_ms
= base::saturated_cast
<int>(
255 (capture_end_time
- capture_begin_time
).InMillisecondsF() + 0.5);
257 int frame_duration_ms
= 0;
258 int frame_duration_ms_frac
= 0;
259 base::TimeDelta frame_duration
;
260 if (frame
->metadata()->GetTimeDelta(VideoFrameMetadata::FRAME_DURATION
,
262 const int decimilliseconds
= base::saturated_cast
<int>(
263 frame_duration
.InMicroseconds() / 100.0 + 0.5);
264 frame_duration_ms
= decimilliseconds
/ 10;
265 frame_duration_ms_frac
= decimilliseconds
% 10;
267 const int target_kbits
= target_bitrate
/ 1000;
268 RenderLineOfText(base::StringPrintf("%3.1d %3.1d.%01d %4.1d",
271 frame_duration_ms_frac
,
276 // Move up one line's worth of pixels.
278 if (top
< 0 || !VLOG_IS_ON(3))
281 // Line 1: Recent utilization metrics.
282 const int deadline_pct
=
283 base::saturated_cast
<int>(deadline_utilization
* 100.0 + 0.5);
284 const int lossy_pct
=
285 base::saturated_cast
<int>(lossy_utilization
* 100.0 + 0.5);
286 RenderLineOfText(base::StringPrintf("%d %3.1d%% %3.1d%%",