Chromium Code Reviews

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: fix mac syntax error Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff |
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 8
9 #include "base/logging.h"
10
11 namespace media { 9 namespace media {
12 namespace { 10 namespace {
13 11
14 // Help structure used for comparing video capture capabilities. 12 // Comparison function object for VideoCaptureCapabilityWin such that
15 struct ResolutionDiff { 13 // std::min_element can be used. It is a wrapper around CompareVideoFormat.
16 const VideoCaptureCapabilityWin* capability; 14 class CompareVideoFormatWin {
17 int diff_height; 15 public:
18 int diff_width; 16 CompareVideoFormatWin(const VideoCaptureFormat& requested)
19 int diff_frame_rate; 17 : compare_video_format_(requested) {}
18
19 bool operator()(const VideoCaptureCapabilityWin& lhs,
20 const VideoCaptureCapabilityWin& rhs) const {
21 return compare_video_format_(lhs.second, rhs.second);
tommi (sloooow) - chröme 2014/09/09 20:11:11 is there a way to avoid using operator overloading
22 }
23
24 private:
25 const CompareVideoFormat compare_video_format_;
20 }; 26 };
21 27
22 bool CompareHeight(const ResolutionDiff& item1, const ResolutionDiff& item2) {
23 return abs(item1.diff_height) < abs(item2.diff_height);
24 }
25
26 bool CompareWidth(const ResolutionDiff& item1, const ResolutionDiff& item2) {
27 return abs(item1.diff_width) < abs(item2.diff_width);
28 }
29
30 bool CompareFrameRate(const ResolutionDiff& item1,
31 const ResolutionDiff& item2) {
32 return abs(item1.diff_frame_rate) < abs(item2.diff_frame_rate);
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. 28 } // namespace.
41 29
42 CapabilityList::CapabilityList() { 30 CapabilityList::const_iterator GetBestMatchedFormat(
43 DetachFromThread(); 31 const VideoCaptureFormat& requested,
44 } 32 const CapabilityList& capabilities) {
45 33 // Find the format with the minimum difference to the requested format.
46 CapabilityList::~CapabilityList() {} 34 return std::min_element(capabilities.begin(),
47 35 capabilities.end(),
48 // Appends an entry to the list. 36 CompareVideoFormatWin(requested));
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 }
90 }
91
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 } 37 }
119 38
120 } // namespace media 39 } // namespace media
OLDNEW

Powered by Google App Engine