xref: /aosp_15_r20/external/v4l2_codec2/common/Common.cpp (revision 0ec5a0ec62797f775085659156625e7f1bdb369f)
1 // Copyright 2021 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 <v4l2_codec2/common/Common.h>
6 
7 #include <base/numerics/safe_math.h>
8 
9 namespace android {
10 
contains(const Rect & rect1,const Rect & rect2)11 bool contains(const Rect& rect1, const Rect& rect2) {
12     return (rect2.left >= rect1.left && rect2.right <= rect1.right && rect2.top >= rect1.top &&
13             rect2.bottom <= rect1.bottom);
14 }
15 
toString(const Rect & rect)16 std::string toString(const Rect& rect) {
17     return std::string("(") + std::to_string(rect.left) + "," + std::to_string(rect.top) + ") " +
18            std::to_string(rect.width()) + "x" + std::to_string(rect.height());
19 }
20 
getArea(const ui::Size & size)21 std::optional<int> getArea(const ui::Size& size) {
22     base::CheckedNumeric<int> checked_area = size.width;
23     checked_area *= size.height;
24     return checked_area.IsValid() ? std::optional<int>(checked_area.ValueOrDie()) : std::nullopt;
25 }
26 
isEmpty(const ui::Size & size)27 bool isEmpty(const ui::Size& size) {
28     return !size.width || !size.height;
29 }
30 
toString(const ui::Size & size)31 std::string toString(const ui::Size& size) {
32     return std::to_string(size.width) + "x" + std::to_string(size.height);
33 }
34 
35 // Check whether the specified profile is a valid profile for the specified codec.
isValidProfileForCodec(VideoCodec codec,C2Config::profile_t profile)36 bool isValidProfileForCodec(VideoCodec codec, C2Config::profile_t profile) {
37     switch (codec) {
38     case VideoCodec::H264:
39         return ((profile >= C2Config::PROFILE_AVC_BASELINE) &&
40                 (profile <= C2Config::PROFILE_AVC_ENHANCED_MULTIVIEW_DEPTH_HIGH));
41     case VideoCodec::VP8:
42         return ((profile >= C2Config::PROFILE_VP8_0) && (profile <= C2Config::PROFILE_VP8_3));
43     case VideoCodec::VP9:
44         return ((profile >= C2Config::PROFILE_VP9_0) && (profile <= C2Config::PROFILE_VP9_3));
45     case VideoCodec::HEVC:
46         return ((profile >= C2Config::PROFILE_HEVC_MAIN) &&
47                 (profile <= C2Config::PROFILE_HEVC_3D_MAIN));
48     default:
49         return false;
50     }
51 }
52 
53 }  // namespace android
54