OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #include "media/cast/sender/performance_metrics_overlay.h" | |
6 | |
7 #include <algorithm> | |
8 #include <string> | |
9 | |
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" | |
14 | |
15 namespace media { | |
16 namespace cast { | |
17 | |
18 namespace { | |
19 | |
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. | |
26 | |
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) { | |
33 DCHECK(p_ul); | |
34 DCHECK_GT(stride, 0); | |
35 | |
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: | |
41 // | |
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; | |
49 | |
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); | |
60 else | |
61 intensity += kDivergeUpAmount; | |
62 p_l[x] = static_cast<uint8>(intensity); | |
63 } | |
64 } | |
65 } | |
66 | |
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 | |
69 // will be rendered. | |
70 void RenderLineOfText(const std::string& line, int top, VideoFrame* frame) { | |
71 // Compute number of physical pixels wide the rendered |line| would be, | |
72 // including padding. | |
73 const int line_width = | |
74 (((kCharacterWidth + kCharacterSpacing) * static_cast<int>(line.size())) + | |
75 kCharacterSpacing) * kScale; | |
76 | |
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 : | |
81 static_cast<size_t>( | |
82 ((line_width - frame->visible_rect().width()) / pixels_per_char) + 1); | |
83 | |
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); | |
87 uint8* p_ul = | |
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... | |
93 - line_width | |
94 // ...now move right to where line[first_idx] would be rendered. | |
95 + first_idx * pixels_per_char; | |
96 | |
97 // Render each character. | |
98 for (size_t i = first_idx; i < line.size(); ++i, p_ul += pixels_per_char) { | |
99 switch (line[i]) { | |
100 case '0': | |
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); | |
105 break; | |
106 case '1': | |
107 DivergePixels(gfx::Rect(1, 0, 1, 5), p_ul, stride); | |
108 break; | |
109 case '2': | |
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); | |
115 break; | |
116 case '3': | |
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); | |
122 break; | |
123 case '4': | |
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); | |
127 break; | |
128 case '5': | |
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); | |
134 break; | |
135 case '6': | |
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); | |
142 break; | |
143 case '7': | |
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); | |
147 break; | |
148 case '8': | |
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); | |
156 break; | |
157 case '9': | |
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); | |
164 break; | |
165 case 'e': | |
166 case 'E': | |
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); | |
172 break; | |
173 case '.': | |
174 DivergePixels(gfx::Rect(1, 4, 1, 1), p_ul, stride); | |
175 break; | |
176 case '+': | |
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... | |
180 case '-': | |
181 DivergePixels(gfx::Rect(0, 2, 3, 1), p_ul, stride); | |
182 break; | |
183 case 'x': | |
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); | |
189 break; | |
190 case ':': | |
191 DivergePixels(gfx::Rect(1, 1, 1, 1), p_ul, stride); | |
192 DivergePixels(gfx::Rect(1, 3, 1, 1), p_ul, stride); | |
193 break; | |
194 case '%': | |
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); | |
200 break; | |
201 case ' ': | |
202 default: | |
203 break; | |
204 } | |
205 } | |
206 } | |
207 | |
208 } // namespace | |
209 | |
210 void MaybeRenderPerformanceMetricsOverlay(int target_bitrate, | |
211 int frames_ago, | |
212 double deadline_utilization, | |
213 double lossy_utilization, | |
214 VideoFrame* frame) { | |
215 if (VideoFrame::PlaneHorizontalBitsPerPixel(frame->format(), kPlane) != 8) { | |
216 DLOG(WARNING) << "Cannot render overlay: Plane " << kPlane << " not 8bpp."; | |
217 return; | |
218 } | |
219 | |
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)) | |
224 return; | |
225 | |
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(), | |
236 minutes, | |
237 seconds, | |
238 hundredth_seconds), | |
239 top, | |
240 frame); | |
241 | |
242 // Move up one line's worth of pixels. | |
243 top -= line_height; | |
244 if (top < 0 || !VLOG_IS_ON(2)) | |
245 return; | |
246 | |
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); | |
256 } | |
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, | |
261 &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; | |
266 } | |
267 const int target_kbits = target_bitrate / 1000; | |
268 RenderLineOfText(base::StringPrintf("%3.1d %3.1d.%01d %4.1d", | |
269 capture_duration_ms, | |
270 frame_duration_ms, | |
271 frame_duration_ms_frac, | |
272 target_kbits), | |
273 top, | |
274 frame); | |
275 | |
276 // Move up one line's worth of pixels. | |
277 top -= line_height; | |
278 if (top < 0 || !VLOG_IS_ON(3)) | |
279 return; | |
280 | |
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%%", | |
287 frames_ago, | |
288 deadline_pct, | |
289 lossy_pct), | |
290 top, | |
291 frame); | |
292 } | |
293 | |
294 } // namespace cast | |
295 } // namespace media | |
OLD | NEW |