blob: b118af6c9deaf8d140dba121db7f7066eaaa2d27 [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>
Ray Essicka727ef92021-03-29 13:35:02 -070022#include <stdlib.h>
Ray Essick1831f7b2021-03-15 16:10:51 -070023
Ray Essick7b4e9d92021-04-20 16:48:00 -070024#include "CodecProperties.h"
25
26#include <media/stagefright/MediaCodecConstants.h>
Ray Essick1831f7b2021-03-15 16:10:51 -070027
Ray Essick244aaa52021-04-05 14:47:02 -070028
29// we aren't going to mess with shaping points dimensions beyond this
30static const int32_t DIMENSION_LIMIT = 16384;
31
Ray Essick1831f7b2021-03-15 16:10:51 -070032namespace android {
33namespace mediaformatshaper {
34
35CodecProperties::CodecProperties(std::string name, std::string mediaType) {
Ray Essick970f1c82021-03-25 13:37:45 -070036 ALOGV("CodecProperties(%s, %s)", name.c_str(), mediaType.c_str());
Ray Essick1831f7b2021-03-15 16:10:51 -070037 mName = name;
38 mMediaType = mediaType;
39}
40
41std::string CodecProperties::getName(){
42 return mName;
43}
44
45std::string CodecProperties::getMediaType(){
46 return mMediaType;
47}
48
49int CodecProperties::supportedMinimumQuality() {
50 return mMinimumQuality;
51}
52void CodecProperties::setSupportedMinimumQuality(int vmaf) {
53 mMinimumQuality = vmaf;
54}
55
56int CodecProperties::targetQpMax() {
57 return mTargetQpMax;
58}
59void CodecProperties::setTargetQpMax(int qpMax) {
60 mTargetQpMax = qpMax;
61}
62
Ray Essick614d8da2021-04-27 11:35:00 -070063void CodecProperties::setMissingQpBoost(double boost) {
64 mMissingQpBoost = boost;
65}
66void CodecProperties::setPhaseOut(double phaseout) {
67 mPhaseOut = phaseout;
68}
69
Ray Essick1831f7b2021-03-15 16:10:51 -070070// what API is this codec set up for (e.g. API of the associated partition)
71// vendor-side (OEM) codecs may be older, due to 'vendor freeze' and treble
72int CodecProperties::supportedApi() {
73 return mApi;
74}
75
Ray Essick970f1c82021-03-25 13:37:45 -070076void CodecProperties::setFeatureValue(std::string key, int32_t value) {
77 ALOGD("setFeatureValue(%s,%d)", key.c_str(), value);
78 mFeatures.insert({key, value});
79
Ray Essick7b4e9d92021-04-20 16:48:00 -070080 if (!strcmp(key.c_str(), FEATURE_QpBounds)) {
Ray Essick970f1c82021-03-25 13:37:45 -070081 setSupportsQp(1);
Ray Essick7b4e9d92021-04-20 16:48:00 -070082 } else if (!strcmp(key.c_str(), "video-minimum-quality")) {
83 setSupportedMinimumQuality(1);
84 } else if (!strcmp(key.c_str(), "vq-minimum-quality")) { // from prototyping
Ray Essicka727ef92021-03-29 13:35:02 -070085 setSupportedMinimumQuality(1);
Ray Essick970f1c82021-03-25 13:37:45 -070086 }
87}
88
89bool CodecProperties::getFeatureValue(std::string key, int32_t *valuep) {
90 ALOGV("getFeatureValue(%s)", key.c_str());
91 if (valuep == nullptr) {
92 return false;
93 }
94 auto mapped = mFeatures.find(key);
95 if (mapped != mFeatures.end()) {
96 *valuep = mapped->second;
97 return true;
98 }
99 return false;
100}
101
Ray Essicka727ef92021-03-29 13:35:02 -0700102// Tuning values (which differ from Features)
103// this is where we set up things like target bitrates and QP ranges
104// NB the tuning values arrive as a string, allowing us to convert it into an appropriate
105// format (int, float, ranges, other combinations)
106//
107void CodecProperties::setTuningValue(std::string key, std::string value) {
108 ALOGD("setTuningValue(%s,%s)", key.c_str(), value.c_str());
109 mTunings.insert({key, value});
110
111 bool legal = false;
112 // NB: old school strtol() because std::stoi() throws exceptions
113 if (!strcmp(key.c_str(), "vq-target-qpmax")) {
114 const char *p = value.c_str();
115 char *q;
116 int32_t iValue = strtol(p, &q, 0);
117 if (q != p) {
118 setTargetQpMax(iValue);
119 legal = true;
120 }
121 } else if (!strcmp(key.c_str(), "vq-target-bpp")) {
122 const char *p = value.c_str();
123 char *q;
124 double bpp = strtod(p, &q);
125 if (q != p) {
126 setBpp(bpp);
127 legal = true;
128 }
Ray Essick244aaa52021-04-05 14:47:02 -0700129 } else if (!strncmp(key.c_str(), "vq-target-bpp-", strlen("vq-target-bpp-"))) {
130 std::string resolution = key.substr(strlen("vq-target-bpp-"));
131 if (bppPoint(resolution, value)) {
132 legal = true;
133 }
Ray Essicka727ef92021-03-29 13:35:02 -0700134 } else if (!strcmp(key.c_str(), "vq-target-bppx100")) {
Ray Essick244aaa52021-04-05 14:47:02 -0700135 // legacy, prototyping
Ray Essicka727ef92021-03-29 13:35:02 -0700136 const char *p = value.c_str();
137 char *q;
138 int32_t iValue = strtol(p, &q, 0);
139 if (q != p) {
140 double bpp = iValue / 100.0;
141 setBpp(bpp);
142 legal = true;
143 }
Ray Essick614d8da2021-04-27 11:35:00 -0700144 } else if (!strcmp(key.c_str(), "vq-bitrate-phaseout")) {
145 const char *p = value.c_str();
146 char *q;
147 double phaseout = strtod(p, &q);
148 if (q != p) {
149 setPhaseOut(phaseout);
150 legal = true;
151 }
152 } else if (!strcmp(key.c_str(), "vq-boost-missing-qp")) {
153 const char *p = value.c_str();
154 char *q;
155 double boost = strtod(p, &q);
156 if (q != p) {
157 setMissingQpBoost(boost);
158 legal = true;
159 }
Ray Essicka727ef92021-03-29 13:35:02 -0700160 } else {
161 legal = true;
162 }
163
164 if (!legal) {
165 ALOGW("setTuningValue() unable to apply tuning '%s' with value '%s'",
166 key.c_str(), value.c_str());
167 }
168 return;
169}
170
171bool CodecProperties::getTuningValue(std::string key, std::string &value) {
172 ALOGV("getTuningValue(%s)", key.c_str());
173 auto mapped = mFeatures.find(key);
174 if (mapped != mFeatures.end()) {
175 value = mapped->second;
176 return true;
177 }
178 return false;
179}
180
Ray Essick244aaa52021-04-05 14:47:02 -0700181bool CodecProperties::bppPoint(std::string resolution, std::string value) {
182
183 int32_t width = 0;
184 int32_t height = 0;
185 double bpp = -1;
186
187 // resolution is "WxH", "W*H" or a standard name like "720p"
188 if (resolution == "1080p") {
189 width = 1080; height = 1920;
190 } else if (resolution == "720p") {
191 width = 720; height = 1280;
192 } else if (resolution == "540p") {
193 width = 540; height = 960;
194 } else if (resolution == "480p") {
195 width = 480; height = 854;
196 } else {
197 size_t sep = resolution.find('x');
198 if (sep == std::string::npos) {
199 sep = resolution.find('*');
200 }
201 if (sep == std::string::npos) {
202 ALOGW("unable to parse resolution: '%s'", resolution.c_str());
203 return false;
204 }
205 std::string w = resolution.substr(0, sep);
206 std::string h = resolution.substr(sep+1);
207
208 char *q;
209 const char *p = w.c_str();
210 width = strtol(p, &q, 0);
211 if (q == p) {
212 width = -1;
213 }
214 p = h.c_str();
215 height = strtol(p, &q, 0);
216 if (q == p) {
217 height = -1;
218 }
219 if (width <= 0 || height <= 0 || width > DIMENSION_LIMIT || height > DIMENSION_LIMIT) {
220 ALOGW("unparseable: width, height '%s'", resolution.c_str());
221 return false;
222 }
223 }
224
225 const char *p = value.c_str();
226 char *q;
227 bpp = strtod(p, &q);
228 if (q == p) {
229 ALOGW("unparseable bpp '%s'", value.c_str());
230 return false;
231 }
232
233 struct bpp_point *point = (struct bpp_point*) malloc(sizeof(*point));
234 if (point == nullptr) {
235 ALOGW("unable to allocate memory for bpp point");
236 return false;
237 }
238
239 point->pixels = width * height;
240 point->width = width;
241 point->height = height;
242 point->bpp = bpp;
243
244 if (mBppPoints == nullptr) {
245 point->next = nullptr;
246 mBppPoints = point;
247 } else if (point->pixels < mBppPoints->pixels) {
248 // at the front
249 point->next = mBppPoints;
250 mBppPoints = point;
251 } else {
252 struct bpp_point *after = mBppPoints;
253 while (after->next) {
254 if (point->pixels > after->next->pixels) {
255 after = after->next;
256 continue;
257 }
258
259 // insert before after->next
260 point->next = after->next;
261 after->next = point;
262 break;
263 }
264 if (after->next == nullptr) {
265 // hasn't gone in yet
266 point->next = nullptr;
267 after->next = point;
268 }
269 }
270
271 return true;
272}
273
274double CodecProperties::getBpp(int32_t width, int32_t height) {
275 // look in the per-resolution list
276
277 int32_t pixels = width * height;
278
279 if (mBppPoints) {
280 struct bpp_point *point = mBppPoints;
281 while (point && point->pixels < pixels) {
282 point = point->next;
283 }
284 if (point) {
285 ALOGV("getBpp(w=%d,h=%d) returns %f from bpppoint w=%d h=%d",
286 width, height, point->bpp, point->width, point->height);
287 return point->bpp;
288 }
289 }
290
291 ALOGV("defaulting to %f bpp", mBpp);
292 return mBpp;
293}
Ray Essick970f1c82021-03-25 13:37:45 -0700294
Ray Essick1831f7b2021-03-15 16:10:51 -0700295std::string CodecProperties::getMapping(std::string key, std::string kind) {
296 ALOGV("getMapping(key %s, kind %s )", key.c_str(), kind.c_str());
297 //play with mMappings
298 auto mapped = mMappings.find(kind + "-" + key);
299 if (mapped != mMappings.end()) {
300 std::string result = mapped->second;
301 ALOGV("getMapping(%s, %s) -> %s", key.c_str(), kind.c_str(), result.c_str());
302 return result;
303 }
304 ALOGV("nope, return unchanged key");
305 return key;
306}
307
308
309// really a bit of debugging code here.
310void CodecProperties::showMappings() {
311 ALOGD("Mappings:");
312 int count = 0;
313 for (const auto& [key, value] : mMappings) {
314 count++;
315 ALOGD("'%s' -> '%s'", key.c_str(), value.c_str());
316 }
317 ALOGD("total %d mappings", count);
318}
319
320void CodecProperties::setMapping(std::string kind, std::string key, std::string value) {
321 ALOGV("setMapping(%s,%s,%s)", kind.c_str(), key.c_str(), value.c_str());
322 std::string metaKey = kind + "-" + key;
323 mMappings.insert({metaKey, value});
324}
325
326const char **CodecProperties::getMappings(std::string kind, bool reverse) {
327 ALOGV("getMappings(kind %s, reverse %d", kind.c_str(), reverse);
328 // how many do we need?
329 int count = mMappings.size();
330 if (count == 0) {
331 ALOGV("empty mappings");
332 return nullptr;
333 }
334 size_t size = sizeof(char *) * (2 * count + 2);
335 const char **result = (const char **)malloc(size);
336 if (result == nullptr) {
337 ALOGW("no memory to return mappings");
338 return nullptr;
339 }
340 memset(result, '\0', size);
341
342 const char **pp = result;
343 for (const auto& [key, value] : mMappings) {
344 // split out the kind/key
345 size_t pos = key.find('-');
346 if (pos == std::string::npos) {
347 ALOGD("ignoring malformed key: %s", key.c_str());
348 continue;
349 }
350 std::string actualKind = key.substr(0,pos);
351 if (kind.length() != 0 && kind != actualKind) {
352 ALOGD("kinds don't match: want '%s' got '%s'", kind.c_str(), actualKind.c_str());
353 continue;
354 }
355 if (reverse) {
356 // codec specific -> std aka 'unmapping'
357 pp[0] = strdup( value.c_str());
358 pp[1] = strdup( key.substr(pos+1).c_str());
359 } else {
360 // std -> codec specific
361 pp[0] = strdup( key.substr(pos+1).c_str());
362 pp[1] = strdup( value.c_str());
363 }
364 ALOGV(" %s -> %s", pp[0], pp[1]);
365 pp += 2;
366 }
367
368 pp[0] = nullptr;
369 pp[1] = nullptr;
370
371 return result;
372}
373
374
375} // namespace mediaformatshaper
376} // namespace android
377