• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 
16 #include "avcodec_dump_utils.h"
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 
20 namespace {
21     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "AVCodecDumpUtils"};
22     constexpr uint32_t DUMP_LEVEL_4 = 4;
23     constexpr uint32_t DUMP_LEVEL_3 = 3;
24     constexpr uint32_t DUMP_LEVEL_2 = 2;
25     constexpr uint32_t DUMP_SPACE_LENGTH = 4;
26     constexpr uint32_t DUMP_OFFSET_24 = 24;
27     constexpr uint32_t DUMP_OFFSET_16 = 16;
28     constexpr uint32_t DUMP_OFFSET_8 = 8;
29 }
30 
31 namespace OHOS {
32 namespace MediaAVCodec {
AddInfo(const uint32_t dumpIdx,const std::string & name,const std::string & value)33 int32_t AVCodecDumpControler::AddInfo(const uint32_t dumpIdx, const std::string &name, const std::string &value)
34 {
35     CHECK_AND_RETURN_RET_LOG((dumpIdx >> DUMP_OFFSET_24) > 0, AVCS_ERR_INVALID_VAL,
36                              "Add dump info failed, get a invalid dump index.");
37     CHECK_AND_RETURN_RET_LOG(!name.empty(), AVCS_ERR_INVALID_VAL,
38                              "Add dump info failed, get a empty name.");
39     if (dumpInfoMap_.find(dumpIdx) != dumpInfoMap_.end()) {
40         AVCODEC_LOGW("Dump info index already exist, index: %{public}d, name: %{public}s.", dumpIdx, name.c_str());
41         return AVCS_ERR_OK;
42     }
43 
44     int32_t level = GetLevel(dumpIdx);
45     length_[level - 1] = length_[level - 1] > name.length() ? length_[level - 1] : name.length();
46     dumpInfoMap_.emplace(dumpIdx, make_pair(name, value));
47     return AVCS_ERR_OK;
48 }
49 
AddInfoFromFormat(const uint32_t dumpIdx,const Format & format,const std::string_view & key,const std::string & name)50 int32_t AVCodecDumpControler::AddInfoFromFormat(const uint32_t dumpIdx, const Format &format,
51                                                 const std::string_view &key, const std::string &name)
52 {
53     CHECK_AND_RETURN_RET_LOG(!key.empty(), AVCS_ERR_INVALID_VAL, "Add dump info failed, get a empty key.");
54 
55     std::string value;
56     bool ret = false;
57     switch (format.GetValueType(key)) {
58         case FORMAT_TYPE_INT32: {
59             int32_t valueTemp = 0;
60             ret = format.GetIntValue(key, valueTemp);
61             value = std::to_string(valueTemp);
62             break;
63         }
64         case FORMAT_TYPE_INT64: {
65             int64_t valueTemp = 0;
66             ret = format.GetLongValue(key, valueTemp);
67             value = std::to_string(valueTemp);
68             break;
69         }
70         case FORMAT_TYPE_FLOAT: {
71             float valueTemp = 0;
72             ret = format.GetFloatValue(key, valueTemp);
73             value = std::to_string(valueTemp);
74             break;
75         }
76         case FORMAT_TYPE_DOUBLE: {
77             double valueTemp = 0;
78             ret = format.GetDoubleValue(key, valueTemp);
79             value = std::to_string(valueTemp);
80             break;
81         }
82         case FORMAT_TYPE_STRING: {
83             ret = format.GetStringValue(key, value);
84             break;
85         }
86         case FORMAT_TYPE_ADDR:
87             break;
88         default:
89             AVCODEC_LOGE("Add info from format failed. Key: %{public}s", key.data());
90     }
91     if (ret != true) {
92         return AVCS_ERR_INVALID_VAL;
93     }
94 
95     this->AddInfo(dumpIdx, name, value);
96     return AVCS_ERR_OK;
97 }
98 
AddInfoFromFormatWithMapping(const uint32_t dumpIdx,const Format & format,const std::string_view & key,const std::string & name,std::map<int32_t,const std::string> mapping)99 int32_t AVCodecDumpControler::AddInfoFromFormatWithMapping(const uint32_t dumpIdx,
100                                                            const Format &format, const std::string_view &key,
101                                                            const std::string &name,
102                                                            std::map<int32_t, const std::string> mapping)
103 {
104     int32_t val;
105     if (format.GetIntValue(key, val) == true) {
106         auto itMappingString = mapping.find(val);
107         const std::string mappingString = itMappingString != mapping.end() ? itMappingString->second : "";
108         AddInfo(dumpIdx, name, mappingString);
109     } else {
110         return AVCS_ERR_INVALID_VAL;
111     }
112     return AVCS_ERR_OK;
113 }
114 
GetDumpString(std::string & dumpString)115 int32_t AVCodecDumpControler::GetDumpString(std::string &dumpString)
116 {
117     for (auto iter : dumpInfoMap_) {
118         int level = GetLevel(iter.first);
119         std::string name = iter.second.first;
120         std::string value = iter.second.second;
121         dumpString += std::string((level - 1) * DUMP_SPACE_LENGTH, ' ')
122             + name + std::string(length_[level - 1] - name.length(), ' ');
123         if (!value.empty()) {
124             dumpString +=  " - " + value;
125         }
126         dumpString += std::string("\n");
127     }
128     return AVCS_ERR_OK;
129 }
130 
GetLevel(const uint32_t dumpIdx)131 uint32_t AVCodecDumpControler::GetLevel(const uint32_t dumpIdx)
132 {
133     int level = 1;
134     if (dumpIdx & UINT8_MAX) {
135         level = DUMP_LEVEL_4;
136     } else if ((dumpIdx >> DUMP_OFFSET_8) & UINT8_MAX) {
137         level = DUMP_LEVEL_3;
138     } else if ((dumpIdx >> DUMP_OFFSET_16) & UINT8_MAX) {
139         level = DUMP_LEVEL_2;
140     }
141     return level;
142 }
143 } // namespace OHOS
144 } // namespace MediaAVCodec