1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef COLOR_SPACE_INFO_PARSE_H
16 #define COLOR_SPACE_INFO_PARSE_H
17 
18 #include <queue>
19 #include <vector>
20 #include <iostream>
21 
22 namespace OHOS {
23 namespace CameraStandard {
24 using namespace std;
25 
26 typedef struct ColorSpaceStreamInfo {
27     int32_t streamType;
28     uint32_t colorSpaceCount;
29     std::vector<int32_t> colorSpaces;
30 } ColorSpaceStreamInfo;
31 
32 typedef struct ColorSpaceModeInfo {
33     int32_t modeType;
34     uint32_t streamTypeCount;
35     std::vector<ColorSpaceStreamInfo> streamInfo;
36 } ColorSpaceModeInfo;
37 
38 typedef struct ColorSpaceInfo {
39     uint32_t modeCount;
40     std::vector<ColorSpaceModeInfo> modeInfo;
41 } ColorSpaceInfo;
42 
43 constexpr static int32_t MODE_FINISH_IDENTIFIER = -1;
44 constexpr static int32_t STREAM_FINISH_IDENTIFIER = -1;
45 constexpr static int32_t LOOP_ONE_STEP = 1;
46 constexpr static int32_t COMMON_STREAM_WITHOUT_TYPE = -1;
47 
48 class ColorSpaceInfoParse {
49 public:
getColorSpaceInfo(int32_t * originInfo,uint32_t count,ColorSpaceInfo & transferedInfo)50     void getColorSpaceInfo(int32_t* originInfo, uint32_t count, ColorSpaceInfo& transferedInfo)
51     {
52         modeStartIndex_.push_back(0);
53         // 处理mode层级的信息,记录mode数量及每个mode开始和结束的下标
54         for (uint32_t i = LOOP_ONE_STEP; i < count; i++) {
55             // 连续两个-1代表当前模式的色彩空间信息上报结束
56             if (originInfo[i - LOOP_ONE_STEP] == STREAM_FINISH_IDENTIFIER && originInfo[i] == MODE_FINISH_IDENTIFIER) {
57                 modeEndIndex_.push_back(i);
58                 if (i + LOOP_ONE_STEP < count) {
59                     modeStartIndex_.push_back(i + LOOP_ONE_STEP);
60                 }
61                 transferedInfo.modeCount++;
62             }
63         }
64         transferedInfo.modeInfo.resize(transferedInfo.modeCount);
65 
66         getColorSpaceStreamCount(originInfo, transferedInfo);
67         for (uint32_t i = 0; i < transferedInfo.modeCount; i++) {
68             transferedInfo.modeInfo[i].modeType = originInfo[modeStartIndex_[i]];
69             getColorSpaceStreamInfo(originInfo, transferedInfo.modeInfo[i]);
70         }
71     }
72 private:
getColorSpaceStreamCount(int32_t * originInfo,ColorSpaceInfo & transferedInfo)73     void getColorSpaceStreamCount(int32_t* originInfo, ColorSpaceInfo& transferedInfo)
74     {
75         for (uint32_t i = 0; i < transferedInfo.modeCount; i++) {
76             for (uint32_t j = modeStartIndex_[i]; j < modeEndIndex_[i]; j++) {
77                 if (j == modeStartIndex_[i]) {
78                     streamStartIndex_.push(modeStartIndex_[i] + LOOP_ONE_STEP);
79                 }
80                 if (originInfo[j] == STREAM_FINISH_IDENTIFIER) {
81                     streamEndIndex_.push(j);
82                     transferedInfo.modeInfo[i].streamTypeCount++;
83                 }
84                 if ((originInfo[j] == STREAM_FINISH_IDENTIFIER) && ((j + LOOP_ONE_STEP) < modeEndIndex_[i])) {
85                     streamStartIndex_.push(j + LOOP_ONE_STEP);
86                 }
87             }
88         }
89         modeStartIndex_.clear();
90         modeEndIndex_.clear();
91     }
92 
getColorSpaceStreamInfo(int32_t * originInfo,ColorSpaceModeInfo & modeInfo)93     void getColorSpaceStreamInfo(int32_t* originInfo, ColorSpaceModeInfo& modeInfo)
94     {
95         modeInfo.streamInfo.resize(modeInfo.streamTypeCount);
96         for (uint32_t i = 0; i < modeInfo.streamTypeCount; i++) {
97             uint32_t loopStart;
98             // 第一套色彩空间能力集为common能力,不报streamType
99             if (i == 0) {
100                 modeInfo.streamInfo[i].streamType = COMMON_STREAM_WITHOUT_TYPE;
101                 loopStart = streamStartIndex_.front();
102             } else {
103                 // 除第一套外,其余色彩空间能力集的第一个int值表示streamType
104                 modeInfo.streamInfo[i].streamType = originInfo[streamStartIndex_.front()];
105                 loopStart = streamStartIndex_.front() + LOOP_ONE_STEP;
106             }
107 
108             modeInfo.streamInfo[i].colorSpaceCount = streamEndIndex_.front() - loopStart;
109             modeInfo.streamInfo[i].colorSpaces.resize(modeInfo.streamInfo[i].colorSpaceCount);
110             int j = 0;
111             for (uint32_t k = loopStart; k < streamEndIndex_.front(); k++) {
112                 modeInfo.streamInfo[i].colorSpaces[j] = originInfo[k];
113                 j++;
114             }
115 
116             streamStartIndex_.pop();
117             streamEndIndex_.pop();
118         }
119     }
120 
121     std::vector<uint32_t> modeStartIndex_ = {};
122     std::vector<uint32_t> modeEndIndex_ = {};
123     std::queue<uint32_t> streamStartIndex_;
124     std::queue<uint32_t> streamEndIndex_;
125 };
126 } // namespace CameraStandard
127 } // namespace OHOS
128 #endif // COLOR_SPACE_INFO_PARSE_H