forked from Pissandshittium/pissandshittium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_resolution_chooser.cc
238 lines (210 loc) · 8.69 KB
/
capture_resolution_chooser.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "media/capture/capture_resolution_chooser.h"
#include <algorithm>
#include <limits>
#include "base/strings/string_util.h"
#include "media/base/limits.h"
#include "media/base/video_util.h"
namespace media {
namespace {
// Each snapped frame size is an integer multiple of this many lines apart.
// This is ideal for 16:9 content, but seems to also work well for many
// arbitrary aspect ratios.
const int kSnappedHeightStep = 90;
// The minimum amount of decrease in area between consecutive snapped frame
// sizes. This matters externally, where the end-to-end system is hunting for a
// capture size that works within all resource bottlenecks. If the snapped
// frame sizes are too-close together, the end-to-end system cannot stablize.
// If they are too-far apart, quality is being sacrificed.
const int kMinAreaDecreasePercent = 15;
// Compute the minimum frame size from the given |max_frame_size| and
// |resolution_change_policy|.
gfx::Size ComputeMinimumCaptureSize(
const gfx::Size& max_frame_size,
ResolutionChangePolicy resolution_change_policy) {
switch (resolution_change_policy) {
case RESOLUTION_POLICY_FIXED_RESOLUTION:
return max_frame_size;
case RESOLUTION_POLICY_FIXED_ASPECT_RATIO: {
// TODO(miu): This is a place-holder until "min constraints" are plumbed-
// in from the MediaStream framework. http://crbug.com/473336
const int kMinLines = 180;
if (max_frame_size.height() <= kMinLines)
return max_frame_size;
const gfx::Size result(
kMinLines * max_frame_size.width() / max_frame_size.height(),
kMinLines);
if (result.width() <= 0 || result.width() > limits::kMaxDimension)
return max_frame_size;
return result;
}
case RESOLUTION_POLICY_ANY_WITHIN_LIMIT:
return gfx::Size(1, 1);
}
NOTREACHED();
return gfx::Size(1, 1);
}
// Returns |size|, unless it exceeds |max_size| or is under |min_size|. When
// the bounds are exceeded, computes and returns an alternate size of similar
// aspect ratio that is within the bounds.
gfx::Size ComputeBoundedCaptureSize(const gfx::Size& size,
const gfx::Size& min_size,
const gfx::Size& max_size) {
if (size.width() > max_size.width() || size.height() > max_size.height()) {
gfx::Size result = ScaleSizeToFitWithinTarget(size, max_size);
result.SetToMax(min_size);
return result;
} else if (size.width() < min_size.width() ||
size.height() < min_size.height()) {
gfx::Size result = ScaleSizeToEncompassTarget(size, min_size);
result.SetToMin(max_size);
return result;
} else {
return size;
}
}
// Returns true if the area of |a| is less than that of |b|.
bool CompareByArea(const gfx::Size& a, const gfx::Size& b) {
return a.GetArea() < b.GetArea();
}
} // namespace
CaptureResolutionChooser::CaptureResolutionChooser(
const gfx::Size& max_frame_size,
ResolutionChangePolicy resolution_change_policy)
: max_frame_size_(max_frame_size),
min_frame_size_(ComputeMinimumCaptureSize(max_frame_size,
resolution_change_policy)),
resolution_change_policy_(resolution_change_policy),
target_area_(std::numeric_limits<decltype(target_area_)>::max()) {
DCHECK_LT(0, max_frame_size_.width());
DCHECK_LT(0, max_frame_size_.height());
DCHECK_LE(min_frame_size_.width(), max_frame_size_.width());
DCHECK_LE(min_frame_size_.height(), max_frame_size_.height());
DCHECK_LE(resolution_change_policy_, RESOLUTION_POLICY_LAST);
UpdateSnappedFrameSizes(max_frame_size_);
RecomputeCaptureSize();
}
CaptureResolutionChooser::~CaptureResolutionChooser() {}
void CaptureResolutionChooser::SetSourceSize(const gfx::Size& source_size) {
if (source_size.IsEmpty())
return;
switch (resolution_change_policy_) {
case RESOLUTION_POLICY_FIXED_RESOLUTION:
// Source size changes do not affect the frame resolution. Frame
// resolution is always fixed to |max_frame_size_|.
break;
case RESOLUTION_POLICY_FIXED_ASPECT_RATIO:
UpdateSnappedFrameSizes(ComputeBoundedCaptureSize(
PadToMatchAspectRatio(source_size, max_frame_size_),
min_frame_size_,
max_frame_size_));
RecomputeCaptureSize();
break;
case RESOLUTION_POLICY_ANY_WITHIN_LIMIT:
UpdateSnappedFrameSizes(ComputeBoundedCaptureSize(
source_size, min_frame_size_, max_frame_size_));
RecomputeCaptureSize();
break;
}
}
void CaptureResolutionChooser::SetTargetFrameArea(int area) {
DCHECK_GE(area, 0);
target_area_ = area;
RecomputeCaptureSize();
}
gfx::Size CaptureResolutionChooser::FindNearestFrameSize(int area) const {
const auto begin = snapped_sizes_.begin();
const auto end = snapped_sizes_.end();
DCHECK(begin != end);
const gfx::Size area_as_size(area, 1); // A facade for CompareByArea().
const auto p = std::lower_bound(begin, end, area_as_size, &CompareByArea);
if (p == end) {
// Boundary case: The target |area| is greater than or equal to the
// largest, so the largest size is closest.
return *(end - 1);
} else if (p == begin) {
// Boundary case: The target |area| is smaller than the smallest, so the
// smallest size is closest.
return *begin;
} else {
// |p| points to the smallest size whose area is greater than or equal to
// the target |area|. The next smaller size could be closer to the target
// |area|, so it must also be considered.
const auto q = p - 1;
return ((p->GetArea() - area) < (area - q->GetArea())) ? *p : *q;
}
}
gfx::Size CaptureResolutionChooser::FindLargerFrameSize(
int area, int num_steps_up) const {
DCHECK_GT(num_steps_up, 0);
const auto begin = snapped_sizes_.begin();
const auto end = snapped_sizes_.end();
DCHECK(begin != end);
const gfx::Size area_as_size(area, 1); // A facade for CompareByArea().
auto p = std::upper_bound(begin, end, area_as_size, &CompareByArea);
// |p| is already pointing one step up.
const int additional_steps_up = num_steps_up - 1;
if ((end - p) > additional_steps_up)
return *(p + additional_steps_up);
else
return *(end - 1);
}
gfx::Size CaptureResolutionChooser::FindSmallerFrameSize(
int area, int num_steps_down) const {
DCHECK_GT(num_steps_down, 0);
const auto begin = snapped_sizes_.begin();
const auto end = snapped_sizes_.end();
DCHECK(begin != end);
const gfx::Size area_as_size(area, 1); // A facade for CompareByArea().
const auto p = std::lower_bound(begin, end, area_as_size, &CompareByArea);
if ((p - begin) >= num_steps_down)
return *(p - num_steps_down);
else
return *begin;
}
void CaptureResolutionChooser::RecomputeCaptureSize() {
const gfx::Size old_capture_size = capture_size_;
capture_size_ = FindNearestFrameSize(target_area_);
VLOG_IF(1, capture_size_ != old_capture_size)
<< "Recomputed capture size from " << old_capture_size.ToString()
<< " to " << capture_size_.ToString() << " ("
<< (100.0 * capture_size_.height() / snapped_sizes_.back().height())
<< "% of ideal size)";
}
void CaptureResolutionChooser::UpdateSnappedFrameSizes(
const gfx::Size& constrained_size) {
// The |constrained_size| is always in the set of possible capture sizes and
// is the largest one.
snapped_sizes_.clear();
snapped_sizes_.push_back(constrained_size);
// Repeatedly decrease the size in steps, adding each to |snapped_sizes_|.
// However, skip the sizes that do not decrease in area by enough, relative to
// the prior size.
int last_area = constrained_size.GetArea();
for (int height = constrained_size.height() - kSnappedHeightStep;
height >= min_frame_size_.height();
height -= kSnappedHeightStep) {
const int width =
height * constrained_size.width() / constrained_size.height();
if (width < min_frame_size_.width())
break;
const int smaller_area = width * height;
const int percent_decrease = 100 * (last_area - smaller_area) / last_area;
if (percent_decrease >= kMinAreaDecreasePercent) {
snapped_sizes_.push_back(gfx::Size(width, height));
last_area = smaller_area;
}
}
// Reverse ordering, so that sizes are from smallest to largest.
std::reverse(snapped_sizes_.begin(), snapped_sizes_.end());
if (VLOG_IS_ON(1)) {
std::vector<std::string> stringified_sizes;
for (const gfx::Size& size : snapped_sizes_)
stringified_sizes.push_back(size.ToString());
VLOG_STREAM(1) << "Recomputed snapped frame sizes: "
<< JoinString(stringified_sizes, " <--> ");
}
}
} // namespace media