blob: d733c5736754760980dec631bd557e361711a208 [file] [log] [blame]
Ray Essick1831f7b2021-03-15 16:10:51 -07001/*
2 * Copyright 2021, The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "CodecProperties"
19#include <utils/Log.h>
20
21#include <string>
22
23#include <media/formatshaper/CodecProperties.h>
24
25namespace android {
26namespace mediaformatshaper {
27
28CodecProperties::CodecProperties(std::string name, std::string mediaType) {
Ray Essick970f1c82021-03-25 13:37:45 -070029 ALOGV("CodecProperties(%s, %s)", name.c_str(), mediaType.c_str());
Ray Essick1831f7b2021-03-15 16:10:51 -070030 mName = name;
31 mMediaType = mediaType;
32}
33
34std::string CodecProperties::getName(){
35 return mName;
36}
37
38std::string CodecProperties::getMediaType(){
39 return mMediaType;
40}
41
42int CodecProperties::supportedMinimumQuality() {
43 return mMinimumQuality;
44}
45void CodecProperties::setSupportedMinimumQuality(int vmaf) {
46 mMinimumQuality = vmaf;
47}
48
49int CodecProperties::targetQpMax() {
50 return mTargetQpMax;
51}
52void CodecProperties::setTargetQpMax(int qpMax) {
53 mTargetQpMax = qpMax;
54}
55
56// what API is this codec set up for (e.g. API of the associated partition)
57// vendor-side (OEM) codecs may be older, due to 'vendor freeze' and treble
58int CodecProperties::supportedApi() {
59 return mApi;
60}
61
Ray Essick970f1c82021-03-25 13:37:45 -070062void CodecProperties::setFeatureValue(std::string key, int32_t value) {
63 ALOGD("setFeatureValue(%s,%d)", key.c_str(), value);
64 mFeatures.insert({key, value});
65
66 if (!strcmp(key.c_str(), "vq-minimum-quality")) {
67 setSupportedMinimumQuality(value);
68 } else if (!strcmp(key.c_str(), "vq-supports-qp")) { // key from prototyping
69 setSupportsQp(1);
70 } else if (!strcmp(key.c_str(), "qp-bounds")) { // official key
71 setSupportsQp(1);
72 } else if (!strcmp(key.c_str(), "vq-target-qpmax")) {
73 setTargetQpMax(value);
74 } else if (!strcmp(key.c_str(), "vq-target-bppx100")) {
75 double bpp = value / 100.0;
76 setBpp(bpp);
77 }
78}
79
80bool CodecProperties::getFeatureValue(std::string key, int32_t *valuep) {
81 ALOGV("getFeatureValue(%s)", key.c_str());
82 if (valuep == nullptr) {
83 return false;
84 }
85 auto mapped = mFeatures.find(key);
86 if (mapped != mFeatures.end()) {
87 *valuep = mapped->second;
88 return true;
89 }
90 return false;
91}
92
93
Ray Essick1831f7b2021-03-15 16:10:51 -070094std::string CodecProperties::getMapping(std::string key, std::string kind) {
95 ALOGV("getMapping(key %s, kind %s )", key.c_str(), kind.c_str());
96 //play with mMappings
97 auto mapped = mMappings.find(kind + "-" + key);
98 if (mapped != mMappings.end()) {
99 std::string result = mapped->second;
100 ALOGV("getMapping(%s, %s) -> %s", key.c_str(), kind.c_str(), result.c_str());
101 return result;
102 }
103 ALOGV("nope, return unchanged key");
104 return key;
105}
106
107
108// really a bit of debugging code here.
109void CodecProperties::showMappings() {
110 ALOGD("Mappings:");
111 int count = 0;
112 for (const auto& [key, value] : mMappings) {
113 count++;
114 ALOGD("'%s' -> '%s'", key.c_str(), value.c_str());
115 }
116 ALOGD("total %d mappings", count);
117}
118
119void CodecProperties::setMapping(std::string kind, std::string key, std::string value) {
120 ALOGV("setMapping(%s,%s,%s)", kind.c_str(), key.c_str(), value.c_str());
121 std::string metaKey = kind + "-" + key;
122 mMappings.insert({metaKey, value});
123}
124
125const char **CodecProperties::getMappings(std::string kind, bool reverse) {
126 ALOGV("getMappings(kind %s, reverse %d", kind.c_str(), reverse);
127 // how many do we need?
128 int count = mMappings.size();
129 if (count == 0) {
130 ALOGV("empty mappings");
131 return nullptr;
132 }
133 size_t size = sizeof(char *) * (2 * count + 2);
134 const char **result = (const char **)malloc(size);
135 if (result == nullptr) {
136 ALOGW("no memory to return mappings");
137 return nullptr;
138 }
139 memset(result, '\0', size);
140
141 const char **pp = result;
142 for (const auto& [key, value] : mMappings) {
143 // split out the kind/key
144 size_t pos = key.find('-');
145 if (pos == std::string::npos) {
146 ALOGD("ignoring malformed key: %s", key.c_str());
147 continue;
148 }
149 std::string actualKind = key.substr(0,pos);
150 if (kind.length() != 0 && kind != actualKind) {
151 ALOGD("kinds don't match: want '%s' got '%s'", kind.c_str(), actualKind.c_str());
152 continue;
153 }
154 if (reverse) {
155 // codec specific -> std aka 'unmapping'
156 pp[0] = strdup( value.c_str());
157 pp[1] = strdup( key.substr(pos+1).c_str());
158 } else {
159 // std -> codec specific
160 pp[0] = strdup( key.substr(pos+1).c_str());
161 pp[1] = strdup( value.c_str());
162 }
163 ALOGV(" %s -> %s", pp[0], pp[1]);
164 pp += 2;
165 }
166
167 pp[0] = nullptr;
168 pp[1] = nullptr;
169
170 return result;
171}
172
173
174} // namespace mediaformatshaper
175} // namespace android
176