Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(414)

Side by Side Diff: media/video/capture/win/capability_list_win.cc

Issue 558623002: Video capture: Refactor GetBestMatchedFormat from Win to OS independent (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: replace std::min_element and std::bind with loop Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "media/video/capture/win/capability_list_win.h" 5 #include "media/video/capture/win/capability_list_win.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional>
8 9
9 #include "base/logging.h" 10 #include "base/logging.h"
10 11
11 namespace media { 12 namespace media {
12 namespace {
13 13
14 // Help structure used for comparing video capture capabilities. 14 static bool CompareCapability(const VideoCaptureFormat& requested,
15 struct ResolutionDiff { 15 const CapabilityWin& capability_lhs,
16 const VideoCaptureCapabilityWin* capability; 16 const CapabilityWin& capability_rhs) {
17 int diff_height; 17 const VideoCaptureFormat& lhs = capability_lhs.supported_format;
18 int diff_width; 18 const VideoCaptureFormat& rhs = capability_rhs.supported_format;
19 int diff_frame_rate;
20 };
21 19
22 bool CompareHeight(const ResolutionDiff& item1, const ResolutionDiff& item2) { 20 const int diff_height_lhs =
23 return abs(item1.diff_height) < abs(item2.diff_height); 21 std::abs(lhs.frame_size.height() - requested.frame_size.height());
22 const int diff_height_rhs =
23 std::abs(rhs.frame_size.height() - requested.frame_size.height());
24 if (diff_height_lhs != diff_height_rhs)
25 return diff_height_lhs < diff_height_rhs;
26
27 const int diff_width_lhs =
28 std::abs(lhs.frame_size.width() - requested.frame_size.width());
29 const int diff_width_rhs =
30 std::abs(rhs.frame_size.width() - requested.frame_size.width());
31 if (diff_width_lhs != diff_width_rhs)
32 return diff_width_lhs < diff_width_rhs;
33
34 const float diff_fps_lhs = std::fabs(lhs.frame_rate - requested.frame_rate);
35 const float diff_fps_rhs = std::fabs(rhs.frame_rate - requested.frame_rate);
36 if (diff_fps_lhs != diff_fps_rhs)
37 return diff_fps_lhs < diff_fps_rhs;
38
39 return lhs.pixel_format < rhs.pixel_format;
24 } 40 }
25 41
26 bool CompareWidth(const ResolutionDiff& item1, const ResolutionDiff& item2) { 42 CapabilityWin GetBestMatchedCapability(const VideoCaptureFormat& requested,
27 return abs(item1.diff_width) < abs(item2.diff_width); 43 const CapabilityList& capabilities) {
28 } 44 DCHECK(!capabilities.empty());
29 45 CapabilityWin best_match = capabilities.front();
mcasas 2014/10/14 15:17:35 const CapabilityWin&
mcasas 2014/10/14 15:17:35 What if the first capability is bad and none impro
magjed_chromium 2014/10/14 15:27:47 Not possible because I can't reassign a reference.
magjed_chromium 2014/10/14 15:27:48 I know the list is non-empty, because I do DCHECK(
30 bool CompareFrameRate(const ResolutionDiff& item1, 46 for (const CapabilityWin& capability : capabilities) {
31 const ResolutionDiff& item2) { 47 if (CompareCapability(requested, capability, best_match)) {
mcasas 2014/10/14 15:17:35 No need for {} in the if.
magjed_chromium 2014/10/14 15:27:48 Done.
32 return abs(item1.diff_frame_rate) < abs(item2.diff_frame_rate); 48 best_match = capability;
33 }
34
35 bool CompareColor(const ResolutionDiff& item1, const ResolutionDiff& item2) {
36 return item1.capability->supported_format.pixel_format <
37 item2.capability->supported_format.pixel_format;
38 }
39
40 } // namespace.
41
42 CapabilityList::CapabilityList() {
43 DetachFromThread();
44 }
45
46 CapabilityList::~CapabilityList() {}
47
48 // Appends an entry to the list.
49 void CapabilityList::Add(const VideoCaptureCapabilityWin& capability) {
50 DCHECK(CalledOnValidThread());
51 capabilities_.push_back(capability);
52 }
53
54 const VideoCaptureCapabilityWin& CapabilityList::GetBestMatchedFormat(
55 int requested_width,
56 int requested_height,
57 float requested_frame_rate) const {
58 DCHECK(CalledOnValidThread());
59 DCHECK(!capabilities_.empty());
60
61 std::list<ResolutionDiff> diff_list;
62
63 // Loop through the candidates to create a list of differentials between the
64 // requested resolution and the camera capability.
65 for (Capabilities::const_iterator it = capabilities_.begin();
66 it != capabilities_.end(); ++it) {
67 ResolutionDiff diff;
68 diff.capability = &(*it);
69 diff.diff_width = it->supported_format.frame_size.width() - requested_width;
70 diff.diff_height =
71 it->supported_format.frame_size.height() - requested_height;
72 // The 1000 allows using integer arithmetic for f.i. 29.971 fps.
73 diff.diff_frame_rate =
74 1000 * ((static_cast<float>(it->frame_rate_numerator) /
75 it->frame_rate_denominator) -
76 requested_frame_rate);
77 diff_list.push_back(diff);
78 }
79
80 // Sort the best height candidates.
81 diff_list.sort(&CompareHeight);
82 int best_diff = diff_list.front().diff_height;
83 for (std::list<ResolutionDiff>::iterator it = diff_list.begin();
84 it != diff_list.end(); ++it) {
85 if (it->diff_height != best_diff) {
86 // Remove all candidates but the best.
87 diff_list.erase(it, diff_list.end());
88 break;
89 } 49 }
90 } 50 }
91 51 return best_match;
92 // Sort the best width candidates.
93 diff_list.sort(&CompareWidth);
94 best_diff = diff_list.front().diff_width;
95 for (std::list<ResolutionDiff>::iterator it = diff_list.begin();
96 it != diff_list.end(); ++it) {
97 if (it->diff_width != best_diff) {
98 // Remove all candidates but the best.
99 diff_list.erase(it, diff_list.end());
100 break;
101 }
102 }
103
104 // Sort the best frame rate candidates.
105 diff_list.sort(&CompareFrameRate);
106 best_diff = diff_list.front().diff_frame_rate;
107 for (std::list<ResolutionDiff>::iterator it = diff_list.begin();
108 it != diff_list.end(); ++it) {
109 if (it->diff_frame_rate != best_diff) {
110 diff_list.erase(it, diff_list.end());
111 break;
112 }
113 }
114
115 // Decide the best color format.
116 diff_list.sort(&CompareColor);
117 return *diff_list.front().capability;
118 } 52 }
119 53
120 } // namespace media 54 } // namespace media
OLDNEW
« no previous file with comments | « media/video/capture/win/capability_list_win.h ('k') | media/video/capture/win/video_capture_device_mf_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698