1 /*
2 * Copyright (C) 2018 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 "CCodecConfig"
19
20 #include <initializer_list>
21
22 #include <android_media_codec.h>
23
24 #include <cutils/properties.h>
25 #include <log/log.h>
26 #include <utils/NativeHandle.h>
27
28 #include <android-base/properties.h>
29
30 #include <C2Component.h>
31 #include <C2Param.h>
32 #include <util/C2InterfaceHelper.h>
33
34 #include <media/stagefright/CodecBase.h>
35 #include <media/stagefright/MediaCodecConstants.h>
36
37 #include "CCodecConfig.h"
38 #include "Codec2Mapper.h"
39
40 #define DRC_DEFAULT_MOBILE_REF_LEVEL 64 /* 64*-0.25dB = -16 dB below full scale for mobile conf */
41 #define DRC_DEFAULT_MOBILE_DRC_CUT 127 /* maximum compression of dynamic range for mobile conf */
42 #define DRC_DEFAULT_MOBILE_DRC_BOOST 127 /* maximum compression of dynamic range for mobile conf */
43 #define DRC_DEFAULT_MOBILE_DRC_HEAVY 1 /* switch for heavy compression for mobile conf */
44 #define DRC_DEFAULT_MOBILE_DRC_EFFECT 3 /* MPEG-D DRC effect type; 3 => Limited playback range */
45 #define DRC_DEFAULT_MOBILE_DRC_ALBUM 0 /* MPEG-D DRC album mode; 0 => album mode is disabled, 1 => album mode is enabled */
46 #define DRC_DEFAULT_MOBILE_OUTPUT_LOUDNESS -1 /* decoder output loudness; -1 => the value is unknown, otherwise dB step value (e.g. 64 for -16 dB) */
47 #define DRC_DEFAULT_MOBILE_ENC_LEVEL (-1) /* encoder target level; -1 => the value is unknown, otherwise dB step value (e.g. 64 for -16 dB) */
48 // names of properties that can be used to override the default DRC settings
49 #define PROP_DRC_OVERRIDE_REF_LEVEL "aac_drc_reference_level"
50 #define PROP_DRC_OVERRIDE_CUT "aac_drc_cut"
51 #define PROP_DRC_OVERRIDE_BOOST "aac_drc_boost"
52 #define PROP_DRC_OVERRIDE_HEAVY "aac_drc_heavy"
53 #define PROP_DRC_OVERRIDE_ENC_LEVEL "aac_drc_enc_target_level"
54 #define PROP_DRC_OVERRIDE_EFFECT "ro.aac_drc_effect_type"
55
56 namespace android {
57
58 // CCodecConfig
59
60 namespace {
61
C2ValueToMessageItem(const C2Value & value,AMessage::ItemData & item)62 void C2ValueToMessageItem(const C2Value &value, AMessage::ItemData &item) {
63 int32_t int32Value;
64 uint32_t uint32Value;
65 int64_t int64Value;
66 uint64_t uint64Value;
67 float floatValue;
68 if (value.get(&int32Value)) {
69 item.set(int32Value);
70 } else if (value.get(&uint32Value) && uint32Value <= uint32_t(INT32_MAX)) {
71 // SDK does not support unsigned values
72 item.set((int32_t)uint32Value);
73 } else if (value.get(&int64Value)) {
74 item.set(int64Value);
75 } else if (value.get(&uint64Value) && uint64Value <= uint64_t(INT64_MAX)) {
76 // SDK does not support unsigned values
77 item.set((int64_t)uint64Value);
78 } else if (value.get(&floatValue)) {
79 item.set(floatValue);
80 }
81 }
82
83 /**
84 * mapping between SDK and Codec 2.0 configurations.
85 */
86 struct ConfigMapper {
87 /**
88 * Value mapper (C2Value => C2Value)
89 */
90 typedef std::function<C2Value(C2Value)> Mapper;
91
92 /// shorthand
93 typedef CCodecConfig::Domain Domain;
94
ConfigMapperandroid::__anon5f9fd7420111::ConfigMapper95 ConfigMapper(std::string mediaKey, C2String c2struct, C2String c2field)
96 : mDomain(Domain::ALL), mMediaKey(mediaKey), mStruct(c2struct), mField(c2field) { }
97
98 /// Limits this parameter to the given domain
limitToandroid::__anon5f9fd7420111::ConfigMapper99 ConfigMapper &limitTo(uint32_t domain) {
100 C2_CHECK(domain & Domain::GUARD_BIT);
101 mDomain = Domain(mDomain & domain);
102 return *this;
103 }
104
105 /// Adds SDK => Codec 2.0 mapper (should not be in the SDK format)
withMapperandroid::__anon5f9fd7420111::ConfigMapper106 ConfigMapper &withMapper(Mapper mapper) {
107 C2_CHECK(!mMapper);
108 C2_CHECK(!mReverse);
109 mMapper = mapper;
110 return *this;
111 }
112
113 /// Adds SDK <=> Codec 2.0 value mappers
withMappersandroid::__anon5f9fd7420111::ConfigMapper114 ConfigMapper &withMappers(Mapper mapper, Mapper reverse) {
115 C2_CHECK(!mMapper);
116 C2_CHECK(!mReverse);
117 mMapper = mapper;
118 mReverse = reverse;
119 return *this;
120 }
121
122 /// Adds SDK <=> Codec 2.0 value mappers based on C2Mapper
123 template<typename C2Type, typename SdkType=int32_t>
withC2Mappersandroid::__anon5f9fd7420111::ConfigMapper124 ConfigMapper &withC2Mappers() {
125 C2_CHECK(!mMapper);
126 C2_CHECK(!mReverse);
127 mMapper = [](C2Value v) -> C2Value {
128 SdkType sdkValue;
129 C2Type c2Value;
130 if (v.get(&sdkValue) && C2Mapper::map(sdkValue, &c2Value)) {
131 return c2Value;
132 }
133 return C2Value();
134 };
135 mReverse = [](C2Value v) -> C2Value {
136 SdkType sdkValue;
137 C2Type c2Value;
138 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<C2Type>::type;
139 if (v.get((C2ValueType*)&c2Value) && C2Mapper::map(c2Value, &sdkValue)) {
140 return sdkValue;
141 }
142 return C2Value();
143 };
144 return *this;
145 }
146
147 /// Maps from SDK values in an AMessage to a suitable C2Value.
mapFromMessageandroid::__anon5f9fd7420111::ConfigMapper148 C2Value mapFromMessage(const AMessage::ItemData &item) const {
149 C2Value value;
150 int32_t int32Value;
151 int64_t int64Value;
152 float floatValue;
153 double doubleValue;
154 if (item.find(&int32Value)) {
155 value = int32Value;
156 } else if (item.find(&int64Value)) {
157 value = int64Value;
158 } else if (item.find(&floatValue)) {
159 value = floatValue;
160 } else if (item.find(&doubleValue)) {
161 value = (float)doubleValue;
162 }
163 if (value.type() != C2Value::NO_INIT && mMapper) {
164 value = mMapper(value);
165 }
166 return value;
167 }
168
169 /// Maps from a C2Value to an SDK value in an AMessage.
mapToMessageandroid::__anon5f9fd7420111::ConfigMapper170 AMessage::ItemData mapToMessage(C2Value value) const {
171 AMessage::ItemData item;
172 if (value.type() != C2Value::NO_INIT && mReverse) {
173 value = mReverse(value);
174 }
175 C2ValueToMessageItem(value, item);
176 return item;
177 }
178
domainandroid::__anon5f9fd7420111::ConfigMapper179 Domain domain() const { return mDomain; }
mediaKeyandroid::__anon5f9fd7420111::ConfigMapper180 std::string mediaKey() const { return mMediaKey; }
pathandroid::__anon5f9fd7420111::ConfigMapper181 std::string path() const { return mField.size() ? mStruct + '.' + mField : mStruct; }
mapperandroid::__anon5f9fd7420111::ConfigMapper182 Mapper mapper() const { return mMapper; }
reverseandroid::__anon5f9fd7420111::ConfigMapper183 Mapper reverse() const { return mReverse; }
184
185 private:
186 Domain mDomain; ///< parameter domain (mask) containing port, kind and config domains
187 std::string mMediaKey; ///< SDK key
188 C2String mStruct; ///< Codec 2.0 struct name
189 C2String mField; ///< Codec 2.0 field name
190 Mapper mMapper; ///< optional SDK => Codec 2.0 value mapper
191 Mapper mReverse; ///< optional Codec 2.0 => SDK value mapper
192 };
193
194 template <typename PORT, typename STREAM>
QueryMediaTypeImpl(const std::shared_ptr<Codec2Client::Configurable> & configurable)195 AString QueryMediaTypeImpl(
196 const std::shared_ptr<Codec2Client::Configurable> &configurable) {
197 AString mediaType;
198 std::vector<std::unique_ptr<C2Param>> queried;
199 c2_status_t c2err = configurable->query(
200 {}, { PORT::PARAM_TYPE, STREAM::PARAM_TYPE }, C2_DONT_BLOCK, &queried);
201 if (c2err != C2_OK && queried.size() == 0) {
202 ALOGD("Query media type failed => %s", asString(c2err));
203 } else {
204 PORT *portMediaType =
205 PORT::From(queried[0].get());
206 if (portMediaType) {
207 mediaType = AString(
208 portMediaType->m.value,
209 strnlen(portMediaType->m.value, portMediaType->flexCount()));
210 } else {
211 STREAM *streamMediaType = STREAM::From(queried[0].get());
212 if (streamMediaType) {
213 mediaType = AString(
214 streamMediaType->m.value,
215 strnlen(streamMediaType->m.value, streamMediaType->flexCount()));
216 }
217 }
218 ALOGD("read media type: %s", mediaType.c_str());
219 }
220 return mediaType;
221 }
222
QueryMediaType(bool input,const std::shared_ptr<Codec2Client::Configurable> & configurable)223 AString QueryMediaType(
224 bool input, const std::shared_ptr<Codec2Client::Configurable> &configurable) {
225 typedef C2PortMediaTypeSetting P;
226 typedef C2StreamMediaTypeSetting S;
227 if (input) {
228 return QueryMediaTypeImpl<P::input, S::input>(configurable);
229 } else {
230 return QueryMediaTypeImpl<P::output, S::output>(configurable);
231 }
232 }
233
234 } // namespace
235
236 /**
237 * Set of standard parameters used by CCodec that are exposed to MediaCodec.
238 */
239 struct StandardParams {
240 typedef CCodecConfig::Domain Domain;
241
242 // standard (MediaCodec) params are keyed by media format key
243 typedef std::string SdkKey;
244
245 /// used to return reference to no config mappers in getConfigMappersForSdkKey
246 static const std::vector<ConfigMapper> NO_MAPPERS;
247
248 /// Returns Codec 2.0 equivalent parameters for an SDK format key.
getConfigMappersForSdkKeyandroid::StandardParams249 const std::vector<ConfigMapper> &getConfigMappersForSdkKey(std::string key) const {
250 auto it = mConfigMappers.find(key);
251 if (it == mConfigMappers.end()) {
252 if (mComplained.count(key) == 0) {
253 ALOGD("no c2 equivalents for %s", key.c_str());
254 mComplained.insert(key);
255 }
256 return NO_MAPPERS;
257 }
258 ALOGV("found %zu eqs for %s", it->second.size(), key.c_str());
259 return it->second;
260 }
261
262 /**
263 * Adds a SDK <=> Codec 2.0 parameter mapping. Multiple Codec 2.0 parameters may map to a
264 * single SDK key, in which case they shall be ordered from least authoritative to most
265 * authoritative. When constructing SDK formats, the last mapped Codec 2.0 parameter that
266 * is supported by the component will determine the exposed value. (TODO: perhaps restrict this
267 * by domain.)
268 */
addandroid::StandardParams269 void add(const ConfigMapper &cm) {
270 auto it = mConfigMappers.find(cm.mediaKey());
271 ALOGV("%c%c%c%c %c%c%c %04x %9s %s => %s",
272 ((cm.domain() & Domain::IS_INPUT) ? 'I' : ' '),
273 ((cm.domain() & Domain::IS_OUTPUT) ? 'O' : ' '),
274 ((cm.domain() & Domain::IS_CODED) ? 'C' : ' '),
275 ((cm.domain() & Domain::IS_RAW) ? 'R' : ' '),
276 ((cm.domain() & Domain::IS_CONFIG) ? 'c' : ' '),
277 ((cm.domain() & Domain::IS_PARAM) ? 'p' : ' '),
278 ((cm.domain() & Domain::IS_READ) ? 'r' : ' '),
279 cm.domain(),
280 it == mConfigMappers.end() ? "adding" : "extending",
281 cm.mediaKey().c_str(), cm.path().c_str());
282 if (it == mConfigMappers.end()) {
283 std::vector<ConfigMapper> eqs = { cm };
284 mConfigMappers.emplace(cm.mediaKey(), eqs);
285 } else {
286 it->second.push_back(cm);
287 }
288 }
289
290 // Updates or adds a mapper for a "sdkkey"
updateConfigMappersForKeyandroid::StandardParams291 void updateConfigMappersForKey(const SdkKey& key,
292 const std::vector<ConfigMapper>& vec_cm) {
293 mConfigMappers.insert_or_assign(key, vec_cm);
294 }
295
296 /**
297 * Returns all paths for a specific domain.
298 *
299 * \param any maximum domain mask. Returned parameters must match at least one of the domains
300 * in the mask.
301 * \param all minimum domain mask. Returned parameters must match all of the domains in the
302 * mask. This is restricted to the bits of the maximum mask.
303 */
getPathsForDomainandroid::StandardParams304 std::vector<std::string> getPathsForDomain(
305 Domain any, Domain all = Domain::ALL) const {
306 std::vector<std::string> res;
307 for (const auto &[key, mappers] : mConfigMappers) {
308 for (const ConfigMapper &cm : mappers) {
309 ALOGV("filtering %s %x %x %x %x", cm.path().c_str(), cm.domain(), any,
310 (cm.domain() & any), (cm.domain() & any & all));
311 if ((cm.domain() & any) && ((cm.domain() & any & all) == (any & all))) {
312 res.push_back(cm.path());
313 }
314 }
315 }
316 return res;
317 }
318
319 /**
320 * Returns SDK <=> Codec 2.0 mappings.
321 *
322 * TODO: replace these with better methods as this exposes the inner structure.
323 */
getKeysandroid::StandardParams324 const std::map<SdkKey, std::vector<ConfigMapper>> getKeys() const {
325 return mConfigMappers;
326 }
327
328 private:
329 std::map<SdkKey, std::vector<ConfigMapper>> mConfigMappers;
330 mutable std::set<std::string> mComplained;
331 };
332
333 const std::vector<ConfigMapper> StandardParams::NO_MAPPERS;
334
335
CCodecConfig()336 CCodecConfig::CCodecConfig()
337 : mInputFormat(new AMessage),
338 mOutputFormat(new AMessage),
339 mUsingSurface(false),
340 mTunneled(false),
341 mPushBlankBuffersOnStop(false) { }
342
initializeStandardParams()343 void CCodecConfig::initializeStandardParams() {
344 typedef Domain D;
345 mStandardParams = std::make_shared<StandardParams>();
346 std::function<void(const ConfigMapper &)> add =
347 [params = mStandardParams](const ConfigMapper &cm) {
348 params->add(cm);
349 };
350 std::function<void(const ConfigMapper &)> deprecated = add;
351
352 // allow int32 or float SDK values and represent them as float
353 ConfigMapper::Mapper makeFloat = [](C2Value v) -> C2Value {
354 // convert from i32 to float
355 int32_t i32Value;
356 float fpValue;
357 if (v.get(&i32Value)) {
358 return (float)i32Value;
359 } else if (v.get(&fpValue)) {
360 return fpValue;
361 }
362 return C2Value();
363 };
364
365 ConfigMapper::Mapper negate = [](C2Value v) -> C2Value {
366 int32_t value;
367 if (v.get(&value)) {
368 return -value;
369 }
370 return C2Value();
371 };
372
373 add(ConfigMapper(KEY_MIME, C2_PARAMKEY_INPUT_MEDIA_TYPE, "value")
374 .limitTo(D::INPUT & D::READ));
375 add(ConfigMapper(KEY_MIME, C2_PARAMKEY_OUTPUT_MEDIA_TYPE, "value")
376 .limitTo(D::OUTPUT & D::READ));
377
378 add(ConfigMapper(KEY_BIT_RATE, C2_PARAMKEY_BITRATE, "value")
379 .limitTo(D::ENCODER & D::CODED));
380 // Some audio decoders require bitrate information to be set
381 add(ConfigMapper(KEY_BIT_RATE, C2_PARAMKEY_BITRATE, "value")
382 .limitTo(D::AUDIO & D::DECODER & D::CODED));
383 // we also need to put the bitrate in the max bitrate field
384 add(ConfigMapper(KEY_MAX_BIT_RATE, C2_PARAMKEY_BITRATE, "value")
385 .limitTo(D::ENCODER & D::READ & D::OUTPUT));
386 add(ConfigMapper(PARAMETER_KEY_VIDEO_BITRATE, C2_PARAMKEY_BITRATE, "value")
387 .limitTo(D::ENCODER & D::VIDEO & D::PARAM));
388 add(ConfigMapper(KEY_BITRATE_MODE, C2_PARAMKEY_BITRATE_MODE, "value")
389 .limitTo(D::ENCODER & D::CODED)
390 .withC2Mappers<C2Config::bitrate_mode_t>());
391 // remove when codecs switch to PARAMKEY and new modes
392 deprecated(ConfigMapper(KEY_BITRATE_MODE, "coded.bitrate-mode", "value")
393 .limitTo(D::ENCODER));
394 add(ConfigMapper(KEY_FRAME_RATE, C2_PARAMKEY_FRAME_RATE, "value")
395 .limitTo(D::VIDEO)
396 .withMappers(makeFloat, [](C2Value v) -> C2Value {
397 // read back always as int
398 float value;
399 if (v.get(&value)) {
400 return (int32_t) (value + 0.5);
401 }
402 return C2Value();
403 }));
404
405 add(ConfigMapper(KEY_MAX_INPUT_SIZE, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE, "value")
406 .limitTo(D::INPUT));
407
408 // remove when codecs switch to PARAMKEY
409 deprecated(ConfigMapper(KEY_MAX_INPUT_SIZE, "coded.max-frame-size", "value")
410 .limitTo(D::INPUT));
411
412 // large frame params
413 add(ConfigMapper(KEY_BUFFER_BATCH_MAX_OUTPUT_SIZE,
414 C2_PARAMKEY_OUTPUT_LARGE_FRAME, "max-size")
415 .limitTo(D::AUDIO & D::OUTPUT));
416 add(ConfigMapper(KEY_BUFFER_BATCH_THRESHOLD_OUTPUT_SIZE,
417 C2_PARAMKEY_OUTPUT_LARGE_FRAME, "threshold-size")
418 .limitTo(D::AUDIO & D::OUTPUT));
419
420 // Rotation
421 // Note: SDK rotation is clock-wise, while C2 rotation is counter-clock-wise
422 add(ConfigMapper(KEY_ROTATION, C2_PARAMKEY_VUI_ROTATION, "value")
423 .limitTo((D::VIDEO | D::IMAGE) & D::CODED)
424 .withMappers(negate, negate));
425 add(ConfigMapper(KEY_ROTATION, C2_PARAMKEY_ROTATION, "value")
426 .limitTo((D::VIDEO | D::IMAGE) & D::RAW)
427 .withMappers(negate, negate));
428
429 // android 'video-scaling'
430 add(ConfigMapper("android._video-scaling", C2_PARAMKEY_SURFACE_SCALING_MODE, "value")
431 .limitTo(D::VIDEO & D::DECODER & D::RAW));
432
433 // Color Aspects
434 //
435 // configure default for decoders
436 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "range")
437 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM))
438 .withC2Mappers<C2Color::range_t>());
439 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "transfer")
440 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM))
441 .withC2Mappers<C2Color::transfer_t>());
442 add(ConfigMapper("color-primaries", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "primaries")
443 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM)));
444 add(ConfigMapper("color-matrix", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "matrix")
445 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM)));
446
447 // read back default for decoders. This is needed in case the component does not support
448 // color aspects. In that case, these values get copied to color-* keys.
449 // TRICKY: We read these values at raw port, since that's where we want to read these.
450 add(ConfigMapper("default-color-range", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "range")
451 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ)
452 .withC2Mappers<C2Color::range_t>());
453 add(ConfigMapper("default-color-transfer", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "transfer")
454 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ)
455 .withC2Mappers<C2Color::transfer_t>());
456 add(ConfigMapper("default-color-primaries", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "primaries")
457 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ));
458 add(ConfigMapper("default-color-matrix", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "matrix")
459 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ));
460
461 // read back final for decoder output (also, configure final aspects as well. This should be
462 // overwritten based on coded/default values if component supports color aspects, but is used
463 // as final values if component does not support aspects at all)
464 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_COLOR_ASPECTS, "range")
465 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ)
466 .withC2Mappers<C2Color::range_t>());
467 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_COLOR_ASPECTS, "transfer")
468 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ)
469 .withC2Mappers<C2Color::transfer_t>());
470 add(ConfigMapper("color-primaries", C2_PARAMKEY_COLOR_ASPECTS, "primaries")
471 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ));
472 add(ConfigMapper("color-matrix", C2_PARAMKEY_COLOR_ASPECTS, "matrix")
473 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::READ));
474
475 // configure transfer request
476 add(ConfigMapper("color-transfer-request", C2_PARAMKEY_COLOR_ASPECTS, "transfer")
477 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW & D::CONFIG)
478 .withC2Mappers<C2Color::transfer_t>());
479
480 // configure source aspects for encoders and read them back on the coded(!) port.
481 // This is to ensure muxing the desired aspects into the container.
482 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_COLOR_ASPECTS, "range")
483 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED)
484 .withC2Mappers<C2Color::range_t>());
485 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_COLOR_ASPECTS, "transfer")
486 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED)
487 .withC2Mappers<C2Color::transfer_t>());
488 add(ConfigMapper("color-primaries", C2_PARAMKEY_COLOR_ASPECTS, "primaries")
489 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED));
490 add(ConfigMapper("color-matrix", C2_PARAMKEY_COLOR_ASPECTS, "matrix")
491 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED));
492
493 // read back coded aspects for encoders (on the raw port), but also configure
494 // desired aspects here.
495 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_VUI_COLOR_ASPECTS, "range")
496 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW)
497 .withC2Mappers<C2Color::range_t>());
498 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_VUI_COLOR_ASPECTS, "transfer")
499 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW)
500 .withC2Mappers<C2Color::transfer_t>());
501 add(ConfigMapper("color-primaries", C2_PARAMKEY_VUI_COLOR_ASPECTS, "primaries")
502 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW));
503 add(ConfigMapper("color-matrix", C2_PARAMKEY_VUI_COLOR_ASPECTS, "matrix")
504 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW));
505
506 // Dataspace
507 add(ConfigMapper("android._dataspace", C2_PARAMKEY_DATA_SPACE, "value")
508 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
509
510 // HDR
511 add(ConfigMapper("smpte2086.red.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.red.x")
512 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
513 add(ConfigMapper("smpte2086.red.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.red.y")
514 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
515 add(ConfigMapper("smpte2086.green.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.green.x")
516 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
517 add(ConfigMapper("smpte2086.green.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.green.y")
518 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
519 add(ConfigMapper("smpte2086.blue.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.blue.x")
520 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
521 add(ConfigMapper("smpte2086.blue.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.blue.y")
522 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
523 add(ConfigMapper("smpte2086.white.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.white.x")
524 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
525 add(ConfigMapper("smpte2086.white.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.white.y")
526 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
527 add(ConfigMapper("smpte2086.max-luminance", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.max-luminance")
528 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
529 add(ConfigMapper("smpte2086.min-luminance", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.min-luminance")
530 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
531 add(ConfigMapper("cta861.max-cll", C2_PARAMKEY_HDR_STATIC_INFO, "max-cll")
532 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
533 add(ConfigMapper("cta861.max-fall", C2_PARAMKEY_HDR_STATIC_INFO, "max-fall")
534 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
535
536 add(ConfigMapper(C2_PARAMKEY_HDR_FORMAT, C2_PARAMKEY_HDR_FORMAT, "value")
537 .limitTo((D::VIDEO | D::IMAGE) & D::CODED & D::CONFIG));
538
539 add(ConfigMapper(std::string(KEY_FEATURE_) + FEATURE_SecurePlayback,
540 C2_PARAMKEY_SECURE_MODE, "value"));
541
542 add(ConfigMapper(KEY_PREPEND_HEADER_TO_SYNC_FRAMES,
543 C2_PARAMKEY_PREPEND_HEADER_MODE, "value")
544 .limitTo(D::ENCODER & D::VIDEO)
545 .withMappers([](C2Value v) -> C2Value {
546 int32_t value;
547 if (v.get(&value)) {
548 return value ? C2Value(C2Config::PREPEND_HEADER_TO_ALL_SYNC)
549 : C2Value(C2Config::PREPEND_HEADER_TO_NONE);
550 }
551 return C2Value();
552 }, [](C2Value v) -> C2Value {
553 C2Config::prepend_header_mode_t value;
554 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(value)>::type;
555 if (v.get((C2ValueType *)&value)) {
556 switch (value) {
557 case C2Config::PREPEND_HEADER_TO_NONE: return 0;
558 case C2Config::PREPEND_HEADER_TO_ALL_SYNC: return 1;
559 case C2Config::PREPEND_HEADER_ON_CHANGE: [[fallthrough]];
560 default: return C2Value();
561 }
562 }
563 return C2Value();
564 }));
565 // remove when codecs switch to PARAMKEY
566 deprecated(ConfigMapper(KEY_PREPEND_HEADER_TO_SYNC_FRAMES,
567 "coding.add-csd-to-sync-frames", "value")
568 .limitTo(D::ENCODER & D::VIDEO));
569 // convert to timestamp base
570 add(ConfigMapper(KEY_I_FRAME_INTERVAL, C2_PARAMKEY_SYNC_FRAME_INTERVAL, "value")
571 .limitTo(D::VIDEO & D::ENCODER & D::CONFIG)
572 .withMapper([](C2Value v) -> C2Value {
573 // convert from i32 to float
574 int32_t i32Value;
575 float fpValue;
576 if (v.get(&i32Value)) {
577 return int64_t(1000000) * i32Value;
578 } else if (v.get(&fpValue)) {
579 return int64_t(c2_min(1000000 * fpValue + 0.5, (double)INT64_MAX));
580 }
581 return C2Value();
582 }));
583 // remove when codecs switch to proper coding.gop (add support for calculating gop)
584 deprecated(ConfigMapper("i-frame-period", "coding.gop", "intra-period")
585 .limitTo(D::ENCODER & D::VIDEO));
586 add(ConfigMapper(KEY_INTRA_REFRESH_PERIOD, C2_PARAMKEY_INTRA_REFRESH, "period")
587 .limitTo(D::VIDEO & D::ENCODER)
588 .withMappers(makeFloat, [](C2Value v) -> C2Value {
589 // read back always as int
590 float value;
591 if (v.get(&value)) {
592 return (int32_t)value;
593 }
594 return C2Value();
595 }));
596
597 if (android::media::codec::provider_->region_of_interest()
598 && android::media::codec::provider_->region_of_interest_support()) {
599 add(ConfigMapper(C2_PARAMKEY_QP_OFFSET_RECTS, C2_PARAMKEY_QP_OFFSET_RECTS, "")
600 .limitTo(D::VIDEO & (D::CONFIG | D::PARAM) & D::ENCODER & D::INPUT));
601 }
602
603 deprecated(ConfigMapper(PARAMETER_KEY_REQUEST_SYNC_FRAME,
604 "coding.request-sync", "value")
605 .limitTo(D::PARAM & D::ENCODER)
606 .withMapper([](C2Value) -> C2Value { return uint32_t(1); }));
607 add(ConfigMapper(PARAMETER_KEY_REQUEST_SYNC_FRAME,
608 C2_PARAMKEY_REQUEST_SYNC_FRAME, "value")
609 .limitTo(D::PARAM & D::ENCODER)
610 .withMapper([](C2Value) -> C2Value { return uint32_t(1); }));
611
612 add(ConfigMapper(KEY_OPERATING_RATE, C2_PARAMKEY_OPERATING_RATE, "value")
613 .limitTo(D::PARAM | D::CONFIG) // write-only
614 .withMapper(makeFloat));
615 // C2 priorities are inverted
616 add(ConfigMapper(KEY_PRIORITY, C2_PARAMKEY_PRIORITY, "value")
617 .withMappers(negate, negate));
618 // remove when codecs switch to PARAMKEY
619 deprecated(ConfigMapper(KEY_OPERATING_RATE, "ctrl.operating-rate", "value")
620 .withMapper(makeFloat));
621 deprecated(ConfigMapper(KEY_PRIORITY, "ctrl.priority", "value"));
622
623 add(ConfigMapper(KEY_WIDTH, C2_PARAMKEY_PICTURE_SIZE, "width")
624 .limitTo(D::VIDEO | D::IMAGE));
625 add(ConfigMapper(KEY_HEIGHT, C2_PARAMKEY_PICTURE_SIZE, "height")
626 .limitTo(D::VIDEO | D::IMAGE));
627
628 add(ConfigMapper("crop-left", C2_PARAMKEY_CROP_RECT, "left")
629 .limitTo(D::VIDEO | D::IMAGE));
630 add(ConfigMapper("crop-top", C2_PARAMKEY_CROP_RECT, "top")
631 .limitTo(D::VIDEO | D::IMAGE));
632 add(ConfigMapper("crop-width", C2_PARAMKEY_CROP_RECT, "width")
633 .limitTo(D::VIDEO | D::IMAGE));
634 add(ConfigMapper("crop-height", C2_PARAMKEY_CROP_RECT, "height")
635 .limitTo(D::VIDEO | D::IMAGE));
636
637 add(ConfigMapper(KEY_MAX_WIDTH, C2_PARAMKEY_MAX_PICTURE_SIZE, "width")
638 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
639 add(ConfigMapper(KEY_MAX_HEIGHT, C2_PARAMKEY_MAX_PICTURE_SIZE, "height")
640 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
641
642 add(ConfigMapper("csd-0", C2_PARAMKEY_INIT_DATA, "value")
643 .limitTo(D::OUTPUT & D::READ));
644
645 deprecated(ConfigMapper(KEY_HDR10_PLUS_INFO, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO, "value")
646 .limitTo(D::VIDEO & D::PARAM & D::INPUT & (D::CONFIG | D::PARAM)));
647
648 deprecated(ConfigMapper(KEY_HDR10_PLUS_INFO, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO, "value")
649 .limitTo(D::VIDEO & D::OUTPUT & D::READ));
650
651 add(ConfigMapper(
652 std::string(C2_PARAMKEY_INPUT_HDR_DYNAMIC_INFO) + ".type",
653 C2_PARAMKEY_INPUT_HDR_DYNAMIC_INFO, "type")
654 .limitTo(D::VIDEO & D::PARAM & D::INPUT & (D::CONFIG | D::PARAM)));
655
656 add(ConfigMapper(
657 std::string(C2_PARAMKEY_INPUT_HDR_DYNAMIC_INFO) + ".data",
658 C2_PARAMKEY_INPUT_HDR_DYNAMIC_INFO, "data")
659 .limitTo(D::VIDEO & D::PARAM & D::INPUT & (D::CONFIG | D::PARAM)));
660
661 add(ConfigMapper(
662 std::string(C2_PARAMKEY_OUTPUT_HDR_DYNAMIC_INFO) + ".type",
663 C2_PARAMKEY_OUTPUT_HDR_DYNAMIC_INFO, "type")
664 .limitTo(D::VIDEO & D::OUTPUT & D::READ));
665
666 add(ConfigMapper(
667 std::string(C2_PARAMKEY_OUTPUT_HDR_DYNAMIC_INFO) + ".data",
668 C2_PARAMKEY_OUTPUT_HDR_DYNAMIC_INFO, "data")
669 .limitTo(D::VIDEO & D::OUTPUT & D::READ));
670
671 add(ConfigMapper(C2_PARAMKEY_TEMPORAL_LAYERING, C2_PARAMKEY_TEMPORAL_LAYERING, "")
672 .limitTo(D::ENCODER & D::VIDEO & D::OUTPUT));
673
674 // Pixel Format (use local key for actual pixel format as we don't distinguish between
675 // SDK layouts for flexible format and we need the actual SDK color format in the media format)
676 add(ConfigMapper("android._color-format", C2_PARAMKEY_PIXEL_FORMAT, "value")
677 .limitTo((D::VIDEO | D::IMAGE) & D::RAW)
678 .withMappers([](C2Value v) -> C2Value {
679 int32_t value;
680 if (v.get(&value)) {
681 uint32_t result;
682 if (C2Mapper::mapPixelFormatFrameworkToCodec(value, &result)) {
683 return result;
684 }
685 }
686 return C2Value();
687 }, [](C2Value v) -> C2Value {
688 uint32_t value;
689 if (v.get(&value)) {
690 int32_t result;
691 if (C2Mapper::mapPixelFormatCodecToFramework(value, &result)) {
692 return result;
693 }
694 }
695 return C2Value();
696 }));
697
698 add(ConfigMapper(KEY_PIXEL_ASPECT_RATIO_WIDTH, C2_PARAMKEY_PIXEL_ASPECT_RATIO, "width")
699 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
700 add(ConfigMapper(KEY_PIXEL_ASPECT_RATIO_HEIGHT, C2_PARAMKEY_PIXEL_ASPECT_RATIO, "height")
701 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
702
703 add(ConfigMapper(KEY_CHANNEL_COUNT, C2_PARAMKEY_CHANNEL_COUNT, "value")
704 .limitTo(D::AUDIO)); // read back to both formats
705 add(ConfigMapper(KEY_CHANNEL_COUNT, C2_PARAMKEY_CODED_CHANNEL_COUNT, "value")
706 .limitTo(D::AUDIO & D::CODED));
707
708 add(ConfigMapper(KEY_SAMPLE_RATE, C2_PARAMKEY_SAMPLE_RATE, "value")
709 .limitTo(D::AUDIO)); // read back to both port formats
710 add(ConfigMapper(KEY_SAMPLE_RATE, C2_PARAMKEY_CODED_SAMPLE_RATE, "value")
711 .limitTo(D::AUDIO & D::CODED));
712
713 auto pcmEncodingMapper = [](C2Value v) -> C2Value {
714 int32_t value;
715 C2Config::pcm_encoding_t to;
716 if (v.get(&value) && C2Mapper::map(value, &to)) {
717 return to;
718 }
719 return C2Value();
720 };
721 auto pcmEncodingReverse = [](C2Value v) -> C2Value {
722 C2Config::pcm_encoding_t value;
723 int32_t to;
724 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(value)>::type;
725 if (v.get((C2ValueType*)&value) && C2Mapper::map(value, &to)) {
726 return to;
727 }
728 return C2Value();
729 };
730 add(ConfigMapper(KEY_PCM_ENCODING, C2_PARAMKEY_PCM_ENCODING, "value")
731 .limitTo(D::AUDIO)
732 .withMappers(pcmEncodingMapper, pcmEncodingReverse));
733 add(ConfigMapper("android._codec-pcm-encoding", C2_PARAMKEY_PCM_ENCODING, "value")
734 .limitTo(D::AUDIO & D::READ)
735 .withMappers(pcmEncodingMapper, pcmEncodingReverse));
736
737 add(ConfigMapper(KEY_IS_ADTS, C2_PARAMKEY_AAC_PACKAGING, "value")
738 .limitTo(D::AUDIO & D::CODED)
739 .withMappers([](C2Value v) -> C2Value {
740 int32_t value;
741 if (v.get(&value) && value) {
742 return C2Config::AAC_PACKAGING_ADTS;
743 }
744 return C2Value();
745 }, [](C2Value v) -> C2Value {
746 uint32_t value;
747 if (v.get(&value) && value == C2Config::AAC_PACKAGING_ADTS) {
748 return (int32_t)1;
749 }
750 return C2Value();
751 }));
752
753 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
754 C2Mapper::GetProfileLevelMapper(mCodingMediaType);
755
756 add(ConfigMapper(KEY_PROFILE, C2_PARAMKEY_PROFILE_LEVEL, "profile")
757 .limitTo(D::CODED)
758 .withMappers([mapper](C2Value v) -> C2Value {
759 C2Config::profile_t c2 = PROFILE_UNUSED;
760 int32_t sdk;
761 if (mapper && v.get(&sdk) && mapper->mapProfile(sdk, &c2)) {
762 return c2;
763 }
764 return PROFILE_UNUSED;
765 }, [mapper](C2Value v) -> C2Value {
766 C2Config::profile_t c2;
767 int32_t sdk;
768 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
769 if (mapper && v.get((C2ValueType*)&c2) && mapper->mapProfile(c2, &sdk)) {
770 return sdk;
771 }
772 return C2Value();
773 }));
774
775 add(ConfigMapper(KEY_LEVEL, C2_PARAMKEY_PROFILE_LEVEL, "level")
776 .limitTo(D::CODED)
777 .withMappers([mapper](C2Value v) -> C2Value {
778 C2Config::level_t c2 = LEVEL_UNUSED;
779 int32_t sdk;
780 if (mapper && v.get(&sdk) && mapper->mapLevel(sdk, &c2)) {
781 return c2;
782 }
783 return LEVEL_UNUSED;
784 }, [mapper](C2Value v) -> C2Value {
785 C2Config::level_t c2;
786 int32_t sdk;
787 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
788 if (mapper && v.get((C2ValueType*)&c2) && mapper->mapLevel(c2, &sdk)) {
789 return sdk;
790 }
791 return C2Value();
792 }));
793
794 add(ConfigMapper(KEY_AAC_PROFILE, C2_PARAMKEY_PROFILE_LEVEL, "profile")
795 .limitTo(D::AUDIO & D::ENCODER & (D::CONFIG | D::PARAM))
796 .withMapper([mapper](C2Value v) -> C2Value {
797 C2Config::profile_t c2 = PROFILE_UNUSED;
798 int32_t sdk;
799 if (mapper && v.get(&sdk) && mapper->mapProfile(sdk, &c2)) {
800 return c2;
801 }
802 return PROFILE_UNUSED;
803 }));
804
805 // convert to dBFS and add default
806 add(ConfigMapper(KEY_AAC_DRC_TARGET_REFERENCE_LEVEL, C2_PARAMKEY_DRC_TARGET_REFERENCE_LEVEL, "value")
807 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM | D::READ))
808 .withMappers([](C2Value v) -> C2Value {
809 int32_t value;
810 if (!v.get(&value) || value < -1) {
811 value = property_get_int32(PROP_DRC_OVERRIDE_REF_LEVEL, DRC_DEFAULT_MOBILE_REF_LEVEL);
812 }
813 return float(-0.25 * c2_min(value, 127));
814 },[](C2Value v) -> C2Value {
815 float value;
816 if (v.get(&value)) {
817 return (int32_t) (-4. * value);
818 }
819 return C2Value();
820 }));
821
822 // convert to 0-1 (%) and add default
823 add(ConfigMapper(KEY_AAC_DRC_ATTENUATION_FACTOR, C2_PARAMKEY_DRC_ATTENUATION_FACTOR, "value")
824 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM | D::READ))
825 .withMappers([](C2Value v) -> C2Value {
826 int32_t value;
827 if (!v.get(&value) || value < 0) {
828 value = property_get_int32(PROP_DRC_OVERRIDE_CUT, DRC_DEFAULT_MOBILE_DRC_CUT);
829 }
830 return float(c2_min(value, 127) / 127.);
831 },[](C2Value v) -> C2Value {
832 float value;
833 if (v.get(&value)) {
834 return (int32_t) (value * 127. + 0.5);
835 }
836 else {
837 return C2Value();
838 }
839 }));
840
841 // convert to 0-1 (%) and add default
842 add(ConfigMapper(KEY_AAC_DRC_BOOST_FACTOR, C2_PARAMKEY_DRC_BOOST_FACTOR, "value")
843 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM | D::READ))
844 .withMappers([](C2Value v) -> C2Value {
845 int32_t value;
846 if (!v.get(&value) || value < 0) {
847 value = property_get_int32(PROP_DRC_OVERRIDE_BOOST, DRC_DEFAULT_MOBILE_DRC_BOOST);
848 }
849 return float(c2_min(value, 127) / 127.);
850 },[](C2Value v) -> C2Value {
851 float value;
852 if (v.get(&value)) {
853 return (int32_t) (value * 127. + 0.5);
854 }
855 else {
856 return C2Value();
857 }
858 }));
859
860 // convert to compression type and add default
861 add(ConfigMapper(KEY_AAC_DRC_HEAVY_COMPRESSION, C2_PARAMKEY_DRC_COMPRESSION_MODE, "value")
862 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM))
863 .withMapper([](C2Value v) -> C2Value {
864 int32_t value;
865 if (!v.get(&value) || value < 0) {
866 value = property_get_int32(PROP_DRC_OVERRIDE_HEAVY, DRC_DEFAULT_MOBILE_DRC_HEAVY);
867 }
868 return value == 1 ? C2Config::DRC_COMPRESSION_HEAVY : C2Config::DRC_COMPRESSION_LIGHT;
869 }));
870
871 // convert to dBFS and add default
872 add(ConfigMapper(KEY_AAC_ENCODED_TARGET_LEVEL, C2_PARAMKEY_DRC_ENCODED_TARGET_LEVEL, "value")
873 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM | D::READ))
874 .withMappers([](C2Value v) -> C2Value {
875 int32_t value;
876 if (!v.get(&value) || value < 0) {
877 value = property_get_int32(PROP_DRC_OVERRIDE_ENC_LEVEL, DRC_DEFAULT_MOBILE_ENC_LEVEL);
878 }
879 return float(-0.25 * c2_min(value, 127));
880 },[](C2Value v) -> C2Value {
881 float value;
882 if (v.get(&value)) {
883 return (int32_t) (-4. * value);
884 }
885 else {
886 return C2Value();
887 }
888 }));
889
890 // convert to effect type (these map to SDK values) and add default
891 add(ConfigMapper(KEY_AAC_DRC_EFFECT_TYPE, C2_PARAMKEY_DRC_EFFECT_TYPE, "value")
892 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM | D::READ))
893 .withMappers([](C2Value v) -> C2Value {
894 int32_t value;
895 if (!v.get(&value) || value < -1 || value > 8) {
896 value = property_get_int32(PROP_DRC_OVERRIDE_EFFECT, DRC_DEFAULT_MOBILE_DRC_EFFECT);
897 // ensure value is within range
898 if (value < -1 || value > 8) {
899 value = DRC_DEFAULT_MOBILE_DRC_EFFECT;
900 }
901 }
902 return value;
903 },[](C2Value v) -> C2Value {
904 int32_t value;
905 if (v.get(&value)) {
906 return value;
907 }
908 else {
909 return C2Value();
910 }
911 }));
912
913 // convert to album mode and add default
914 add(ConfigMapper(KEY_AAC_DRC_ALBUM_MODE, C2_PARAMKEY_DRC_ALBUM_MODE, "value")
915 .limitTo(D::AUDIO & D::DECODER & (D::CONFIG | D::PARAM | D::READ))
916 .withMappers([](C2Value v) -> C2Value {
917 int32_t value;
918 if (!v.get(&value) || value < 0 || value > 1) {
919 value = DRC_DEFAULT_MOBILE_DRC_ALBUM;
920 // ensure value is within range
921 if (value < 0 || value > 1) {
922 value = DRC_DEFAULT_MOBILE_DRC_ALBUM;
923 }
924 }
925 return value;
926 },[](C2Value v) -> C2Value {
927 int32_t value;
928 if (v.get(&value)) {
929 return value;
930 }
931 else {
932 return C2Value();
933 }
934 }));
935
936 add(ConfigMapper(KEY_AAC_DRC_OUTPUT_LOUDNESS, C2_PARAMKEY_DRC_OUTPUT_LOUDNESS, "value")
937 .limitTo(D::OUTPUT & D::DECODER & D::READ)
938 .withMappers([](C2Value v) -> C2Value {
939 int32_t value;
940 if (!v.get(&value) || value < -1) {
941 value = DRC_DEFAULT_MOBILE_OUTPUT_LOUDNESS;
942 }
943 return float(-0.25 * c2_min(value, 127));
944 },[](C2Value v) -> C2Value {
945 float value;
946 if (v.get(&value)) {
947 return (int32_t) (-4. * value);
948 }
949 return C2Value();
950 }));
951
952 add(ConfigMapper(KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT, C2_PARAMKEY_MAX_CHANNEL_COUNT, "value")
953 .limitTo(D::AUDIO & (D::CONFIG | D::PARAM | D::READ)));
954
955 add(ConfigMapper(KEY_MAX_OUTPUT_CHANNEL_COUNT, C2_PARAMKEY_MAX_CHANNEL_COUNT, "value")
956 .limitTo(D::AUDIO & (D::CONFIG | D::PARAM | D::READ)));
957
958 add(ConfigMapper(KEY_CHANNEL_MASK, C2_PARAMKEY_CHANNEL_MASK, "value")
959 .limitTo(D::AUDIO & D::DECODER & D::READ));
960
961 add(ConfigMapper(KEY_CHANNEL_MASK, C2_PARAMKEY_CHANNEL_MASK, "value")
962 .limitTo(D::AUDIO & D::ENCODER & D::CONFIG));
963
964 add(ConfigMapper(KEY_AAC_SBR_MODE, C2_PARAMKEY_AAC_SBR_MODE, "value")
965 .limitTo(D::AUDIO & D::ENCODER & (D::CONFIG | D::PARAM | D::READ))
966 .withMapper([](C2Value v) -> C2Value {
967 int32_t value;
968 if (!v.get(&value) || value < 0) {
969 return C2Config::AAC_SBR_AUTO;
970 }
971 switch (value) {
972 case 0: return C2Config::AAC_SBR_OFF;
973 case 1: return C2Config::AAC_SBR_SINGLE_RATE;
974 case 2: return C2Config::AAC_SBR_DUAL_RATE;
975 default: return C2Config::AAC_SBR_AUTO + 1; // invalid value
976 }
977 }));
978
979 add(ConfigMapper("android._encoding-quality-level", C2_PARAMKEY_ENCODING_QUALITY_LEVEL, "value")
980 .limitTo(D::ENCODER & (D::CONFIG | D::PARAM)));
981 add(ConfigMapper(KEY_QUALITY, C2_PARAMKEY_QUALITY, "value")
982 .limitTo(D::ENCODER & (D::CONFIG | D::PARAM)));
983 add(ConfigMapper(KEY_FLAC_COMPRESSION_LEVEL, C2_PARAMKEY_COMPLEXITY, "value")
984 .limitTo(D::AUDIO & D::ENCODER));
985 add(ConfigMapper(KEY_COMPLEXITY, C2_PARAMKEY_COMPLEXITY, "value")
986 .limitTo(D::ENCODER & (D::CONFIG | D::PARAM)));
987
988 add(ConfigMapper(KEY_GRID_COLUMNS, C2_PARAMKEY_TILE_LAYOUT, "columns")
989 .limitTo(D::IMAGE));
990 add(ConfigMapper(KEY_GRID_ROWS, C2_PARAMKEY_TILE_LAYOUT, "rows")
991 .limitTo(D::IMAGE));
992 add(ConfigMapper(KEY_TILE_WIDTH, C2_PARAMKEY_TILE_LAYOUT, "tile.width")
993 .limitTo(D::IMAGE));
994 add(ConfigMapper(KEY_TILE_HEIGHT, C2_PARAMKEY_TILE_LAYOUT, "tile.height")
995 .limitTo(D::IMAGE));
996
997 add(ConfigMapper(KEY_LATENCY, C2_PARAMKEY_PIPELINE_DELAY_REQUEST, "value")
998 .limitTo(D::VIDEO & D::ENCODER));
999
1000 add(ConfigMapper(C2_PARAMKEY_INPUT_TIME_STRETCH, C2_PARAMKEY_INPUT_TIME_STRETCH, "value"));
1001
1002 add(ConfigMapper(KEY_LOW_LATENCY, C2_PARAMKEY_LOW_LATENCY_MODE, "value")
1003 .limitTo(D::DECODER & (D::CONFIG | D::PARAM))
1004 .withMapper([](C2Value v) -> C2Value {
1005 int32_t value = 0;
1006 (void)v.get(&value);
1007 return value == 0 ? C2_FALSE : C2_TRUE;
1008 }));
1009
1010 add(ConfigMapper("android._trigger-tunnel-peek", C2_PARAMKEY_TUNNEL_START_RENDER, "value")
1011 .limitTo(D::PARAM & D::VIDEO & D::DECODER)
1012 .withMapper([](C2Value v) -> C2Value {
1013 int32_t value = 0;
1014 (void)v.get(&value);
1015 return value == 0 ? C2_FALSE : C2_TRUE;
1016 }));
1017
1018 add(ConfigMapper("android._tunnel-peek-set-legacy", C2_PARAMKEY_TUNNEL_PEEK_MODE, "value")
1019 .limitTo(D::PARAM & D::VIDEO & D::DECODER)
1020 .withMapper([](C2Value v) -> C2Value {
1021 int32_t value = 0;
1022 (void)v.get(&value);
1023 return value == 0
1024 ? C2Value(C2PlatformConfig::SPECIFIED_PEEK)
1025 : C2Value(C2PlatformConfig::UNSPECIFIED_PEEK);
1026 }));
1027
1028 add(ConfigMapper(KEY_VIDEO_QP_AVERAGE, C2_PARAMKEY_AVERAGE_QP, "value")
1029 .limitTo(D::ENCODER & D::VIDEO & D::READ));
1030
1031 add(ConfigMapper(KEY_PICTURE_TYPE, C2_PARAMKEY_PICTURE_TYPE, "value")
1032 .limitTo(D::ENCODER & D::VIDEO & D::READ)
1033 .withMappers([](C2Value v) -> C2Value {
1034 int32_t sdk;
1035 C2Config::picture_type_t c2;
1036 if (v.get(&sdk) && C2Mapper::map(sdk, &c2)) {
1037 return C2Value(c2);
1038 }
1039 return C2Value();
1040 }, [](C2Value v) -> C2Value {
1041 C2Config::picture_type_t c2;
1042 int32_t sdk = PICTURE_TYPE_UNKNOWN;
1043 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
1044 if (v.get((C2ValueType*)&c2) && C2Mapper::map(c2, &sdk)) {
1045 return sdk;
1046 }
1047 return C2Value();
1048 }));
1049
1050 /* still to do
1051 not yet used by MediaCodec, but defined as MediaFormat
1052 KEY_AUDIO_SESSION_ID // we use "audio-hw-sync"
1053 KEY_OUTPUT_REORDER_DEPTH
1054 */
1055 }
1056
initialize(const std::shared_ptr<C2ParamReflector> & reflector,const std::shared_ptr<Codec2Client::Configurable> & configurable)1057 status_t CCodecConfig::initialize(
1058 const std::shared_ptr<C2ParamReflector> &reflector,
1059 const std::shared_ptr<Codec2Client::Configurable> &configurable) {
1060 C2ComponentDomainSetting domain(C2Component::DOMAIN_OTHER);
1061 C2ComponentKindSetting kind(C2Component::KIND_OTHER);
1062
1063 std::vector<std::unique_ptr<C2Param>> queried;
1064 c2_status_t c2err = configurable->query({ &domain, &kind }, {}, C2_DONT_BLOCK, &queried);
1065 if (c2err != C2_OK) {
1066 ALOGD("Query domain & kind failed => %s", asString(c2err));
1067 // TEMP: determine kind from component name
1068 if (kind.value == C2Component::KIND_OTHER) {
1069 if (configurable->getName().find("encoder") != std::string::npos) {
1070 kind.value = C2Component::KIND_ENCODER;
1071 } else if (configurable->getName().find("decoder") != std::string::npos) {
1072 kind.value = C2Component::KIND_DECODER;
1073 }
1074 }
1075
1076 // TEMP: determine domain from media type (port (preferred) or stream #0)
1077 if (domain.value == C2Component::DOMAIN_OTHER) {
1078 AString mediaType = QueryMediaType(true /* input */, configurable);
1079 if (mediaType.startsWith("audio/")) {
1080 domain.value = C2Component::DOMAIN_AUDIO;
1081 } else if (mediaType.startsWith("video/")) {
1082 domain.value = C2Component::DOMAIN_VIDEO;
1083 } else if (mediaType.startsWith("image/")) {
1084 domain.value = C2Component::DOMAIN_IMAGE;
1085 }
1086 }
1087 }
1088
1089 mDomain = (domain.value == C2Component::DOMAIN_VIDEO ? Domain::IS_VIDEO :
1090 domain.value == C2Component::DOMAIN_IMAGE ? Domain::IS_IMAGE :
1091 domain.value == C2Component::DOMAIN_AUDIO ? Domain::IS_AUDIO : Domain::OTHER_DOMAIN)
1092 | (kind.value == C2Component::KIND_DECODER ? Domain::IS_DECODER :
1093 kind.value == C2Component::KIND_ENCODER ? Domain::IS_ENCODER : Domain::OTHER_KIND);
1094
1095 mInputDomain = Domain(((mDomain & IS_DECODER) ? IS_CODED : IS_RAW) | IS_INPUT);
1096 mOutputDomain = Domain(((mDomain & IS_ENCODER) ? IS_CODED : IS_RAW) | IS_OUTPUT);
1097
1098 ALOGV("domain is %#x (%u %u)", mDomain, domain.value, kind.value);
1099
1100 std::vector<C2Param::Index> paramIndices;
1101 switch (kind.value) {
1102 case C2Component::KIND_DECODER:
1103 mCodingMediaType = QueryMediaType(true /* input */, configurable).c_str();
1104 break;
1105 case C2Component::KIND_ENCODER:
1106 mCodingMediaType = QueryMediaType(false /* input */, configurable).c_str();
1107 break;
1108 default:
1109 mCodingMediaType = "";
1110 }
1111
1112 c2err = configurable->querySupportedParams(&mParamDescs);
1113 if (c2err != C2_OK) {
1114 ALOGD("Query supported params failed after returning %zu values => %s",
1115 mParamDescs.size(), asString(c2err));
1116 return UNKNOWN_ERROR;
1117 }
1118 for (const std::shared_ptr<C2ParamDescriptor> &desc : mParamDescs) {
1119 mSupportedIndices.emplace(desc->index());
1120 }
1121
1122 mReflector = reflector;
1123 if (mReflector == nullptr) {
1124 ALOGE("Null param reflector");
1125 return UNKNOWN_ERROR;
1126 }
1127
1128 // enumerate all fields
1129 mParamUpdater = std::make_shared<ReflectedParamUpdater>();
1130 mParamUpdater->clear();
1131 mParamUpdater->supportWholeParam(
1132 C2_PARAMKEY_TEMPORAL_LAYERING, C2StreamTemporalLayeringTuning::CORE_INDEX);
1133 if (android::media::codec::provider_->region_of_interest()
1134 && android::media::codec::provider_->region_of_interest_support()) {
1135 mParamUpdater->supportWholeParam(
1136 C2_PARAMKEY_QP_OFFSET_RECTS, C2StreamQpOffsetRects::CORE_INDEX);
1137 }
1138 mParamUpdater->addParamDesc(mReflector, mParamDescs);
1139
1140 // TEMP: add some standard fields even if not reflected
1141 if (kind.value == C2Component::KIND_ENCODER) {
1142 mParamUpdater->addStandardParam<C2StreamInitDataInfo::output>(C2_PARAMKEY_INIT_DATA);
1143 }
1144 if (domain.value == C2Component::DOMAIN_IMAGE || domain.value == C2Component::DOMAIN_VIDEO) {
1145 if (kind.value != C2Component::KIND_ENCODER) {
1146 addLocalParam<C2StreamPictureSizeInfo::output>(C2_PARAMKEY_PICTURE_SIZE);
1147 addLocalParam<C2StreamCropRectInfo::output>(C2_PARAMKEY_CROP_RECT);
1148 addLocalParam(
1149 new C2StreamPixelAspectRatioInfo::output(0u, 1u, 1u),
1150 C2_PARAMKEY_PIXEL_ASPECT_RATIO);
1151 addLocalParam(new C2StreamRotationInfo::output(0u, 0), C2_PARAMKEY_ROTATION);
1152 addLocalParam(
1153 new C2StreamColorAspectsTuning::output(0u),
1154 C2_PARAMKEY_DEFAULT_COLOR_ASPECTS);
1155 addLocalParam<C2StreamDataSpaceInfo::output>(C2_PARAMKEY_DATA_SPACE);
1156 addLocalParam<C2StreamHdrStaticInfo::output>(C2_PARAMKEY_HDR_STATIC_INFO);
1157 addLocalParam(
1158 new C2StreamSurfaceScalingInfo::output(0u, VIDEO_SCALING_MODE_SCALE_TO_FIT),
1159 C2_PARAMKEY_SURFACE_SCALING_MODE);
1160 } else {
1161 addLocalParam(new C2StreamColorAspectsInfo::input(0u), C2_PARAMKEY_COLOR_ASPECTS);
1162
1163 if (domain.value == C2Component::DOMAIN_VIDEO) {
1164 addLocalParam(new C2AndroidStreamAverageBlockQuantizationInfo::output(0u, 0),
1165 C2_PARAMKEY_AVERAGE_QP);
1166 addLocalParam(new C2StreamPictureTypeInfo::output(0u, 0),
1167 C2_PARAMKEY_PICTURE_TYPE);
1168 }
1169 }
1170 }
1171
1172 initializeStandardParams();
1173
1174 // subscribe to all supported standard (exposed) params
1175 // TODO: limit this to params that are actually in the domain
1176 std::vector<std::string> formatKeys = mStandardParams->getPathsForDomain(Domain(1 << 30));
1177 std::vector<C2Param::Index> indices;
1178 mParamUpdater->getParamIndicesForKeys(formatKeys, &indices);
1179 mSubscribedIndices.insert(indices.begin(), indices.end());
1180
1181 // also subscribe to some non-SDK standard parameters
1182 // for number of input/output buffers
1183 mSubscribedIndices.emplace(C2PortSuggestedBufferCountTuning::input::PARAM_TYPE);
1184 mSubscribedIndices.emplace(C2PortSuggestedBufferCountTuning::output::PARAM_TYPE);
1185 mSubscribedIndices.emplace(C2ActualPipelineDelayTuning::PARAM_TYPE);
1186 mSubscribedIndices.emplace(C2PortActualDelayTuning::input::PARAM_TYPE);
1187 mSubscribedIndices.emplace(C2PortActualDelayTuning::output::PARAM_TYPE);
1188 // for output buffer array allocation
1189 mSubscribedIndices.emplace(C2StreamMaxBufferSizeInfo::output::PARAM_TYPE);
1190 // init data (CSD)
1191 mSubscribedIndices.emplace(C2StreamInitDataInfo::output::PARAM_TYPE);
1192
1193 for (const std::shared_ptr<C2ParamDescriptor> &desc : mParamDescs) {
1194 if (desc->index().isVendor()) {
1195 std::vector<std::string> keys;
1196 mParamUpdater->getKeysForParamIndex(desc->index(), &keys);
1197 for (const std::string &key : keys) {
1198 mVendorParams.insert_or_assign(key, desc);
1199 }
1200 }
1201 }
1202
1203 // Parameters that are not subscribed initially, but can be subscribed
1204 // upon explicit request.
1205 static const std::initializer_list<C2Param::Index> kOptionalParams = {
1206 C2AndroidStreamAverageBlockQuantizationInfo::output::PARAM_TYPE,
1207 C2StreamPictureTypeInfo::output::PARAM_TYPE,
1208 };
1209 for (const C2Param::Index &index : kOptionalParams) {
1210 mSubscribedIndices.erase(index);
1211 }
1212 subscribeToConfigUpdate(configurable, {}, C2_MAY_BLOCK);
1213
1214 return OK;
1215 }
1216
subscribeToConfigUpdate(const std::shared_ptr<Codec2Client::Configurable> & configurable,const std::vector<C2Param::Index> & indices,c2_blocking_t blocking)1217 status_t CCodecConfig::subscribeToConfigUpdate(
1218 const std::shared_ptr<Codec2Client::Configurable> &configurable,
1219 const std::vector<C2Param::Index> &indices,
1220 c2_blocking_t blocking) {
1221 static const int32_t kProductFirstApiLevel =
1222 base::GetIntProperty<int32_t>("ro.product.first_api_level", 0);
1223 static const int32_t kBoardApiLevel =
1224 base::GetIntProperty<int32_t>("ro.board.first_api_level", 0);
1225 static const int32_t kFirstApiLevel =
1226 (kBoardApiLevel != 0) ? kBoardApiLevel : kProductFirstApiLevel;
1227 mSubscribedIndices.insert(indices.begin(), indices.end());
1228 if (mSubscribedIndices.size() != mSubscribedIndicesSize
1229 && kFirstApiLevel >= __ANDROID_API_T__) {
1230 std::vector<uint32_t> indicesVector;
1231 for (C2Param::Index ix : mSubscribedIndices) {
1232 indicesVector.push_back(ix);
1233 }
1234 std::unique_ptr<C2SubscribedParamIndicesTuning> subscribeTuning =
1235 C2SubscribedParamIndicesTuning::AllocUnique(indicesVector);
1236 std::vector<std::unique_ptr<C2SettingResult>> results;
1237 c2_status_t c2Err = configurable->config({ subscribeTuning.get() }, blocking, &results);
1238 if (c2Err != C2_OK && c2Err != C2_BAD_INDEX) {
1239 ALOGD("Failed to subscribe to parameters => %s", asString(c2Err));
1240 // TODO: error
1241 }
1242 ALOGV("Subscribed to %zu params", mSubscribedIndices.size());
1243 mSubscribedIndicesSize = mSubscribedIndices.size();
1244 }
1245 #if defined(LOG_NDEBUG) && !LOG_NDEBUG
1246 ALOGV("subscribed to %zu params:", mSubscribedIndices.size());
1247 std::stringstream ss;
1248 for (const C2Param::Index &index : mSubscribedIndices) {
1249 ss << index << " ";
1250 if (ss.str().length() > 70) {
1251 ALOGV("%s", ss.str().c_str());
1252 std::stringstream().swap(ss);
1253 }
1254 }
1255 if (!ss.str().empty()) {
1256 ALOGV("%s", ss.str().c_str());
1257 }
1258 #endif
1259 return OK;
1260 }
1261
queryConfiguration(const std::shared_ptr<Codec2Client::Configurable> & configurable)1262 status_t CCodecConfig::queryConfiguration(
1263 const std::shared_ptr<Codec2Client::Configurable> &configurable) {
1264 // query all subscribed parameters
1265 std::vector<C2Param::Index> indices(mSubscribedIndices.begin(), mSubscribedIndices.end());
1266 std::vector<std::unique_ptr<C2Param>> queried;
1267 c2_status_t c2Err = configurable->query({}, indices, C2_MAY_BLOCK, &queried);
1268 if (c2Err != OK) {
1269 ALOGI("query failed after returning %zu values (%s)", queried.size(), asString(c2Err));
1270 // TODO: error
1271 }
1272
1273 updateConfiguration(queried, ALL);
1274 return OK;
1275 }
1276
updateConfiguration(std::vector<std::unique_ptr<C2Param>> & configUpdate,Domain domain)1277 bool CCodecConfig::updateConfiguration(
1278 std::vector<std::unique_ptr<C2Param>> &configUpdate, Domain domain) {
1279 ALOGV("updating configuration with %zu params", configUpdate.size());
1280 bool changed = false;
1281 for (std::unique_ptr<C2Param> &p : configUpdate) {
1282 if (p && *p) {
1283 // Allow unsubscribed vendor parameters to go through --- it may be
1284 // later handled by the format shaper.
1285 if (!p->isVendor() && mSubscribedIndices.count(p->index()) == 0) {
1286 ALOGV("updateConfiguration: skipped unsubscribed param %08x", p->index());
1287 continue;
1288 }
1289 auto insertion = mCurrentConfig.emplace(p->index(), nullptr);
1290 if (insertion.second || *insertion.first->second != *p) {
1291 if (mSupportedIndices.count(p->index()) || mLocalParams.count(p->index())) {
1292 // only track changes in supported (reflected or local) indices
1293 changed = true;
1294 } else {
1295 ALOGV("an unlisted config was %s: %#x",
1296 insertion.second ? "added" : "updated", p->index());
1297 }
1298 }
1299 insertion.first->second = std::move(p);
1300 }
1301 }
1302 if (mInputSurface
1303 && (domain & mOutputDomain)
1304 && mInputSurfaceDataspace != mInputSurface->getDataspace()) {
1305 changed = true;
1306 mInputSurfaceDataspace = mInputSurface->getDataspace();
1307 }
1308
1309 ALOGV("updated configuration has %zu params (%s)", mCurrentConfig.size(),
1310 changed ? "CHANGED" : "no change");
1311 if (changed) {
1312 return updateFormats(domain);
1313 }
1314 return false;
1315 }
1316
updateFormats(Domain domain)1317 bool CCodecConfig::updateFormats(Domain domain) {
1318 // get addresses of params in the current config
1319 std::vector<C2Param*> paramPointers;
1320 for (const auto &it : mCurrentConfig) {
1321 paramPointers.push_back(it.second.get());
1322 }
1323
1324 ReflectedParamUpdater::Dict reflected = mParamUpdater->getParams(paramPointers);
1325 std::string config = reflected.debugString();
1326 std::set<std::string> configLines;
1327 std::string diff;
1328 for (size_t start = 0; start != std::string::npos; ) {
1329 size_t end = config.find('\n', start);
1330 size_t count = (end == std::string::npos)
1331 ? std::string::npos
1332 : end - start + 1;
1333 std::string line = config.substr(start, count);
1334 configLines.insert(line);
1335 if (mLastConfig.count(line) == 0) {
1336 diff.append(line);
1337 }
1338 start = (end == std::string::npos) ? std::string::npos : end + 1;
1339 }
1340 if (!diff.empty()) {
1341 ALOGD("c2 config diff is %s", diff.c_str());
1342 }
1343 mLastConfig.swap(configLines);
1344
1345 bool changed = false;
1346 if (domain & mInputDomain) {
1347 sp<AMessage> oldFormat = mInputFormat;
1348 mInputFormat = mInputFormat->dup(); // trigger format changed
1349 mInputFormat->extend(getFormatForDomain(reflected, mInputDomain));
1350 if (mInputFormat->countEntries() != oldFormat->countEntries()
1351 || mInputFormat->changesFrom(oldFormat)->countEntries() > 0) {
1352 changed = true;
1353 } else {
1354 mInputFormat = oldFormat; // no change
1355 }
1356 }
1357 if (domain & mOutputDomain) {
1358 sp<AMessage> oldFormat = mOutputFormat;
1359 mOutputFormat = mOutputFormat->dup(); // trigger output format changed
1360 mOutputFormat->extend(getFormatForDomain(reflected, mOutputDomain));
1361 if (mOutputFormat->countEntries() != oldFormat->countEntries()
1362 || mOutputFormat->changesFrom(oldFormat)->countEntries() > 0) {
1363 changed = true;
1364 } else {
1365 mOutputFormat = oldFormat; // no change
1366 }
1367 }
1368 ALOGV_IF(changed, "format(s) changed");
1369 return changed;
1370 }
1371
getFormatForDomain(const ReflectedParamUpdater::Dict & reflected,Domain portDomain) const1372 sp<AMessage> CCodecConfig::getFormatForDomain(
1373 const ReflectedParamUpdater::Dict &reflected,
1374 Domain portDomain) const {
1375 sp<AMessage> msg = new AMessage;
1376 for (const auto &[key, mappers] : mStandardParams->getKeys()) {
1377 for (const ConfigMapper &cm : mappers) {
1378 if ((cm.domain() & portDomain) == 0 // input-output-coded-raw
1379 || (cm.domain() & mDomain) != mDomain // component domain + kind (these must match)
1380 || (cm.domain() & IS_READ) == 0) {
1381 continue;
1382 }
1383 auto it = reflected.find(cm.path());
1384 if (it == reflected.end()) {
1385 continue;
1386 }
1387 C2Value c2Value;
1388 sp<ABuffer> bufValue;
1389 AString strValue;
1390 AMessage::ItemData item;
1391 if (it->second.find(&c2Value)) {
1392 item = cm.mapToMessage(c2Value);
1393 } else if (it->second.find(&bufValue)) {
1394 item.set(bufValue);
1395 } else if (it->second.find(&strValue)) {
1396 item.set(strValue);
1397 } else {
1398 ALOGD("unexpected untyped query value for key: %s", cm.path().c_str());
1399 continue;
1400 }
1401 msg->setItem(key.c_str(), item);
1402 }
1403 }
1404
1405 bool input = (portDomain & Domain::IS_INPUT);
1406 std::vector<std::string> vendorKeys;
1407 for (const auto &[key, value] : reflected) {
1408 auto it = mVendorParams.find(key);
1409 if (it == mVendorParams.end()) {
1410 continue;
1411 }
1412 C2Param::Index index = it->second->index();
1413 if (mSubscribedIndices.count(index) == 0) {
1414 continue;
1415 }
1416 // For vendor parameters, we only care about direction
1417 if ((input && !index.forInput())
1418 || (!input && !index.forOutput())) {
1419 continue;
1420 }
1421 C2Value c2Value;
1422 sp<ABuffer> bufValue;
1423 AString strValue;
1424 AMessage::ItemData item;
1425 if (value.find(&c2Value)) {
1426 C2ValueToMessageItem(c2Value, item);
1427 } else if (value.find(&bufValue)) {
1428 item.set(bufValue);
1429 } else if (value.find(&strValue)) {
1430 item.set(strValue);
1431 } else {
1432 ALOGD("unexpected untyped query value for key: %s", key.c_str());
1433 continue;
1434 }
1435 msg->setItem(key.c_str(), item);
1436 }
1437
1438 { // convert from Codec 2.0 rect to MediaFormat rect and add crop rect if not present
1439 int32_t left, top, width, height;
1440 if (msg->findInt32("crop-left", &left) && msg->findInt32("crop-width", &width)
1441 && msg->findInt32("crop-top", &top) && msg->findInt32("crop-height", &height)
1442 && left >= 0 && width >=0 && width <= INT32_MAX - left
1443 && top >= 0 && height >=0 && height <= INT32_MAX - top) {
1444 msg->removeEntryAt(msg->findEntryByName("crop-left"));
1445 msg->removeEntryAt(msg->findEntryByName("crop-top"));
1446 msg->removeEntryAt(msg->findEntryByName("crop-width"));
1447 msg->removeEntryAt(msg->findEntryByName("crop-height"));
1448 msg->setRect("crop", left, top, left + width - 1, top + height - 1);
1449 } else if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) {
1450 msg->setRect("crop", 0, 0, width - 1, height - 1);
1451 }
1452 }
1453
1454 { // convert temporal layering to schema
1455 sp<ABuffer> tmp;
1456 if (msg->findBuffer(C2_PARAMKEY_TEMPORAL_LAYERING, &tmp) && tmp != nullptr) {
1457 C2StreamTemporalLayeringTuning *layering =
1458 C2StreamTemporalLayeringTuning::From(C2Param::From(tmp->data(), tmp->size()));
1459 if (layering && layering->m.layerCount > 0
1460 && layering->m.bLayerCount < layering->m.layerCount) {
1461 // check if this is webrtc compatible
1462 AString mime;
1463 if (msg->findString(KEY_MIME, &mime) &&
1464 mime.equalsIgnoreCase(MIMETYPE_VIDEO_VP8) &&
1465 layering->m.bLayerCount == 0 &&
1466 (layering->m.layerCount == 1
1467 || (layering->m.layerCount == 2
1468 && layering->flexCount() >= 1
1469 && layering->m.bitrateRatios[0] == .6f)
1470 || (layering->m.layerCount == 3
1471 && layering->flexCount() >= 2
1472 && layering->m.bitrateRatios[0] == .4f
1473 && layering->m.bitrateRatios[1] == .6f)
1474 || (layering->m.layerCount == 4
1475 && layering->flexCount() >= 3
1476 && layering->m.bitrateRatios[0] == .25f
1477 && layering->m.bitrateRatios[1] == .4f
1478 && layering->m.bitrateRatios[2] == .6f))) {
1479 msg->setString(KEY_TEMPORAL_LAYERING, AStringPrintf(
1480 "webrtc.vp8.%u-layer", layering->m.layerCount));
1481 } else if (layering->m.bLayerCount) {
1482 msg->setString(KEY_TEMPORAL_LAYERING, AStringPrintf(
1483 "android.generic.%u+%u",
1484 layering->m.layerCount - layering->m.bLayerCount,
1485 layering->m.bLayerCount));
1486 } else if (layering->m.bLayerCount) {
1487 msg->setString(KEY_TEMPORAL_LAYERING, AStringPrintf(
1488 "android.generic.%u", layering->m.layerCount));
1489 }
1490 }
1491 msg->removeEntryAt(msg->findEntryByName(C2_PARAMKEY_TEMPORAL_LAYERING));
1492 }
1493 }
1494
1495 // Remove KEY_AAC_SBR_MODE from SDK message if it is outside supported range
1496 // as SDK doesn't have a way to signal default sbr mode based on profile and
1497 // requires that the key isn't present in format to signal that
1498 int sbrMode;
1499 if (msg->findInt32(KEY_AAC_SBR_MODE, &sbrMode) && (sbrMode < 0 || sbrMode > 2)) {
1500 msg->removeEntryAt(msg->findEntryByName(KEY_AAC_SBR_MODE));
1501 }
1502
1503 { // convert color info
1504 // move default color to color aspect if not read from the component
1505 int32_t tmp;
1506 int32_t range;
1507 if (msg->findInt32("default-color-range", &range)) {
1508 if (!msg->findInt32(KEY_COLOR_RANGE, &tmp)) {
1509 msg->setInt32(KEY_COLOR_RANGE, range);
1510 }
1511 msg->removeEntryAt(msg->findEntryByName("default-color-range"));
1512 }
1513 int32_t transfer;
1514 if (msg->findInt32("default-color-transfer", &transfer)) {
1515 if (!msg->findInt32(KEY_COLOR_TRANSFER, &tmp)) {
1516 msg->setInt32(KEY_COLOR_TRANSFER, transfer);
1517 }
1518 msg->removeEntryAt(msg->findEntryByName("default-color-transfer"));
1519 }
1520 C2Color::primaries_t primaries;
1521 if (msg->findInt32("default-color-primaries", (int32_t*)&primaries)) {
1522 if (!msg->findInt32("color-primaries", &tmp)) {
1523 msg->setInt32("color-primaries", primaries);
1524 }
1525 msg->removeEntryAt(msg->findEntryByName("default-color-primaries"));
1526 }
1527 C2Color::matrix_t matrix;
1528 if (msg->findInt32("default-color-matrix", (int32_t*)&matrix)) {
1529 if (!msg->findInt32("color-matrix", &tmp)) {
1530 msg->setInt32("color-matrix", matrix);
1531 }
1532 msg->removeEntryAt(msg->findEntryByName("default-color-matrix"));
1533 }
1534
1535 if (msg->findInt32("color-primaries", (int32_t*)&primaries)
1536 && msg->findInt32("color-matrix", (int32_t*)&matrix)) {
1537 int32_t standard;
1538
1539 if (C2Mapper::map(primaries, matrix, &standard)) {
1540 msg->setInt32(KEY_COLOR_STANDARD, standard);
1541 }
1542
1543 msg->removeEntryAt(msg->findEntryByName("color-primaries"));
1544 msg->removeEntryAt(msg->findEntryByName("color-matrix"));
1545 }
1546
1547 // calculate dataspace for raw graphic buffers if not specified by component, or if
1548 // using surface with unspecified aspects (as those must be defaulted which may change
1549 // the dataspace)
1550 if ((portDomain & IS_RAW) && (mDomain & (IS_IMAGE | IS_VIDEO))) {
1551 android_dataspace dataspace;
1552 ColorAspects aspects = {
1553 ColorAspects::RangeUnspecified, ColorAspects::PrimariesUnspecified,
1554 ColorAspects::TransferUnspecified, ColorAspects::MatrixUnspecified
1555 };
1556 ColorUtils::getColorAspectsFromFormat(msg, aspects);
1557 ColorAspects origAspects = aspects;
1558 if (mUsingSurface) {
1559 // get image size (default to HD)
1560 int32_t width = 1280;
1561 int32_t height = 720;
1562 int32_t left, top, right, bottom;
1563 if (msg->findRect("crop", &left, &top, &right, &bottom)) {
1564 width = right - left + 1;
1565 height = bottom - top + 1;
1566 } else {
1567 (void)msg->findInt32(KEY_WIDTH, &width);
1568 (void)msg->findInt32(KEY_HEIGHT, &height);
1569 }
1570 ColorUtils::setDefaultCodecColorAspectsIfNeeded(aspects, width, height);
1571 ColorUtils::setColorAspectsIntoFormat(aspects, msg);
1572 }
1573
1574 if (!msg->findInt32("android._dataspace", (int32_t*)&dataspace)
1575 || aspects.mRange != origAspects.mRange
1576 || aspects.mPrimaries != origAspects.mPrimaries
1577 || aspects.mTransfer != origAspects.mTransfer
1578 || aspects.mMatrixCoeffs != origAspects.mMatrixCoeffs) {
1579 dataspace = ColorUtils::getDataSpaceForColorAspects(aspects, true /* mayExpand */);
1580 msg->setInt32("android._dataspace", dataspace);
1581 }
1582 }
1583
1584 if (mInputSurface) {
1585 android_dataspace dataspace = mInputSurface->getDataspace();
1586 ColorUtils::convertDataSpaceToV0(dataspace);
1587 int32_t standard;
1588 ColorUtils::getColorConfigFromDataSpace(dataspace, &range, &standard, &transfer);
1589 if (range != 0) {
1590 msg->setInt32(KEY_COLOR_RANGE, range);
1591 }
1592 if (standard != 0) {
1593 msg->setInt32(KEY_COLOR_STANDARD, standard);
1594 }
1595 if (transfer != 0) {
1596 msg->setInt32(KEY_COLOR_TRANSFER, transfer);
1597 }
1598 msg->setInt32("android._dataspace", dataspace);
1599 }
1600
1601 // HDR static info
1602
1603 C2HdrStaticMetadataStruct hdr;
1604 if (msg->findFloat("smpte2086.red.x", &hdr.mastering.red.x)
1605 && msg->findFloat("smpte2086.red.y", &hdr.mastering.red.y)
1606 && msg->findFloat("smpte2086.green.x", &hdr.mastering.green.x)
1607 && msg->findFloat("smpte2086.green.y", &hdr.mastering.green.y)
1608 && msg->findFloat("smpte2086.blue.x", &hdr.mastering.blue.x)
1609 && msg->findFloat("smpte2086.blue.y", &hdr.mastering.blue.y)
1610 && msg->findFloat("smpte2086.white.x", &hdr.mastering.white.x)
1611 && msg->findFloat("smpte2086.white.y", &hdr.mastering.white.y)
1612 && msg->findFloat("smpte2086.max-luminance", &hdr.mastering.maxLuminance)
1613 && msg->findFloat("smpte2086.min-luminance", &hdr.mastering.minLuminance)
1614 && msg->findFloat("cta861.max-cll", &hdr.maxCll)
1615 && msg->findFloat("cta861.max-fall", &hdr.maxFall)) {
1616 if (hdr.mastering.red.x >= 0 && hdr.mastering.red.x <= 1
1617 && hdr.mastering.red.y >= 0 && hdr.mastering.red.y <= 1
1618 && hdr.mastering.green.x >= 0 && hdr.mastering.green.x <= 1
1619 && hdr.mastering.green.y >= 0 && hdr.mastering.green.y <= 1
1620 && hdr.mastering.blue.x >= 0 && hdr.mastering.blue.x <= 1
1621 && hdr.mastering.blue.y >= 0 && hdr.mastering.blue.y <= 1
1622 && hdr.mastering.white.x >= 0 && hdr.mastering.white.x <= 1
1623 && hdr.mastering.white.y >= 0 && hdr.mastering.white.y <= 1
1624 && hdr.mastering.maxLuminance >= 0 && hdr.mastering.maxLuminance <= 65535
1625 && hdr.mastering.minLuminance >= 0 && hdr.mastering.minLuminance <= 6.5535
1626 && hdr.maxCll >= 0 && hdr.maxCll <= 65535
1627 && hdr.maxFall >= 0 && hdr.maxFall <= 65535) {
1628 HDRStaticInfo meta;
1629 meta.mID = meta.kType1;
1630 meta.sType1.mR.x = hdr.mastering.red.x / 0.00002 + 0.5;
1631 meta.sType1.mR.y = hdr.mastering.red.y / 0.00002 + 0.5;
1632 meta.sType1.mG.x = hdr.mastering.green.x / 0.00002 + 0.5;
1633 meta.sType1.mG.y = hdr.mastering.green.y / 0.00002 + 0.5;
1634 meta.sType1.mB.x = hdr.mastering.blue.x / 0.00002 + 0.5;
1635 meta.sType1.mB.y = hdr.mastering.blue.y / 0.00002 + 0.5;
1636 meta.sType1.mW.x = hdr.mastering.white.x / 0.00002 + 0.5;
1637 meta.sType1.mW.y = hdr.mastering.white.y / 0.00002 + 0.5;
1638 meta.sType1.mMaxDisplayLuminance = hdr.mastering.maxLuminance + 0.5;
1639 meta.sType1.mMinDisplayLuminance = hdr.mastering.minLuminance / 0.0001 + 0.5;
1640 meta.sType1.mMaxContentLightLevel = hdr.maxCll + 0.5;
1641 meta.sType1.mMaxFrameAverageLightLevel = hdr.maxFall + 0.5;
1642 msg->setBuffer(KEY_HDR_STATIC_INFO, ABuffer::CreateAsCopy(&meta, sizeof(meta)));
1643 } else {
1644 ALOGD("found invalid HDR static metadata %s", msg->debugString(8).c_str());
1645 }
1646 msg->removeEntryAt(msg->findEntryByName("smpte2086.red.x"));
1647 msg->removeEntryAt(msg->findEntryByName("smpte2086.red.y"));
1648 msg->removeEntryAt(msg->findEntryByName("smpte2086.green.x"));
1649 msg->removeEntryAt(msg->findEntryByName("smpte2086.green.y"));
1650 msg->removeEntryAt(msg->findEntryByName("smpte2086.blue.x"));
1651 msg->removeEntryAt(msg->findEntryByName("smpte2086.blue.y"));
1652 msg->removeEntryAt(msg->findEntryByName("smpte2086.white.x"));
1653 msg->removeEntryAt(msg->findEntryByName("smpte2086.white.y"));
1654 msg->removeEntryAt(msg->findEntryByName("smpte2086.max-luminance"));
1655 msg->removeEntryAt(msg->findEntryByName("smpte2086.min-luminance"));
1656 msg->removeEntryAt(msg->findEntryByName("cta861.max-cll"));
1657 msg->removeEntryAt(msg->findEntryByName("cta861.max-fall"));
1658 }
1659
1660 // HDR dynamic info
1661 std::string keyPrefix = input ? C2_PARAMKEY_INPUT_HDR_DYNAMIC_INFO
1662 : C2_PARAMKEY_OUTPUT_HDR_DYNAMIC_INFO;
1663 std::string typeKey = keyPrefix + ".type";
1664 std::string dataKey = keyPrefix + ".data";
1665 int32_t type;
1666 sp<ABuffer> data;
1667 if (msg->findInt32(typeKey.c_str(), &type)
1668 && msg->findBuffer(dataKey.c_str(), &data)) {
1669 if (type == HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40) {
1670 msg->setBuffer(KEY_HDR10_PLUS_INFO, data);
1671 msg->removeEntryAt(msg->findEntryByName(typeKey.c_str()));
1672 msg->removeEntryAt(msg->findEntryByName(dataKey.c_str()));
1673 }
1674 }
1675 }
1676
1677 ALOGV("converted to SDK values as %s", msg->debugString().c_str());
1678 return msg;
1679 }
1680
1681 /// converts an AMessage value to a ParamUpdater value
convert(const AMessage::ItemData & from,ReflectedParamUpdater::Value * to)1682 static void convert(const AMessage::ItemData &from, ReflectedParamUpdater::Value *to) {
1683 int32_t int32Value;
1684 int64_t int64Value;
1685 sp<ABuffer> bufValue;
1686 AString strValue;
1687 float floatValue;
1688 double doubleValue;
1689
1690 if (from.find(&int32Value)) {
1691 to->set(int32Value);
1692 } else if (from.find(&int64Value)) {
1693 to->set(int64Value);
1694 } else if (from.find(&bufValue)) {
1695 to->set(bufValue);
1696 } else if (from.find(&strValue)) {
1697 to->set(strValue);
1698 } else if (from.find(&floatValue)) {
1699 to->set(C2Value(floatValue));
1700 } else if (from.find(&doubleValue)) {
1701 // convert double to float
1702 to->set(C2Value((float)doubleValue));
1703 }
1704 // ignore all other AMessage types
1705 }
1706
1707 /// relaxes Codec 2.0 specific value types to SDK types (mainly removes signedness and counterness
1708 /// from 32/64-bit values.)
relaxValues(ReflectedParamUpdater::Value & item)1709 static void relaxValues(ReflectedParamUpdater::Value &item) {
1710 C2Value c2Value;
1711 int32_t int32Value;
1712 int64_t int64Value;
1713 (void)item.find(&c2Value);
1714 if (c2Value.get(&int32Value) || c2Value.get((uint32_t*)&int32Value)
1715 || c2Value.get((c2_cntr32_t*)&int32Value)) {
1716 item.set(int32Value);
1717 } else if (c2Value.get(&int64Value)
1718 || c2Value.get((uint64_t*)&int64Value)
1719 || c2Value.get((c2_cntr64_t*)&int64Value)) {
1720 item.set(int64Value);
1721 }
1722 }
1723
getReflectedFormat(const sp<AMessage> & params_,Domain configDomain) const1724 ReflectedParamUpdater::Dict CCodecConfig::getReflectedFormat(
1725 const sp<AMessage> ¶ms_, Domain configDomain) const {
1726 // create a modifiable copy of params
1727 sp<AMessage> params = params_->dup();
1728 ALOGV("filtering with config domain %x", configDomain);
1729
1730 // convert some macro parameters to Codec 2.0 specific expressions
1731
1732 { // make i-frame-interval frame based
1733 float iFrameInterval;
1734 if (params->findAsFloat(KEY_I_FRAME_INTERVAL, &iFrameInterval)) {
1735 float frameRate;
1736 if (params->findAsFloat(KEY_FRAME_RATE, &frameRate)) {
1737 params->setInt32("i-frame-period",
1738 (frameRate <= 0 || iFrameInterval < 0)
1739 ? -1 /* no sync frames */
1740 : (int32_t)c2_min(iFrameInterval * frameRate + 0.5,
1741 (float)INT32_MAX));
1742 }
1743 }
1744 }
1745
1746 if (mDomain == (IS_VIDEO | IS_ENCODER)) {
1747 // convert capture-rate into input-time-stretch
1748 float frameRate, captureRate;
1749 if (params->findAsFloat(KEY_FRAME_RATE, &frameRate)) {
1750 if (!params->findAsFloat("time-lapse-fps", &captureRate)
1751 && !params->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) {
1752 captureRate = frameRate;
1753 }
1754 if (captureRate > 0 && frameRate > 0) {
1755 params->setFloat(C2_PARAMKEY_INPUT_TIME_STRETCH, captureRate / frameRate);
1756 }
1757 }
1758
1759 // add HDR format for video encoding
1760 if (configDomain == IS_CONFIG) {
1761 // don't assume here that transfer is set for HDR, only require it for HLG
1762 int transfer = 0;
1763 params->findInt32(KEY_COLOR_TRANSFER, &transfer);
1764
1765 int profile;
1766 if (params->findInt32(KEY_PROFILE, &profile)) {
1767 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
1768 C2Mapper::GetProfileLevelMapper(mCodingMediaType);
1769 C2Config::hdr_format_t c2 = C2Config::hdr_format_t::UNKNOWN;
1770 if (mapper && mapper->mapHdrFormat(profile, &c2)) {
1771 if (c2 == C2Config::hdr_format_t::HLG &&
1772 transfer != COLOR_TRANSFER_HLG) {
1773 c2 = C2Config::hdr_format_t::UNKNOWN;
1774 }
1775 params->setInt32(C2_PARAMKEY_HDR_FORMAT, c2);
1776 }
1777 }
1778 }
1779 }
1780
1781 { // reflect temporal layering into a binary blob
1782 AString schema;
1783 if (params->findString(KEY_TEMPORAL_LAYERING, &schema)) {
1784 unsigned int numLayers = 0;
1785 unsigned int numBLayers = 0;
1786 int tags;
1787 char dummy;
1788 std::unique_ptr<C2StreamTemporalLayeringTuning::output> layering;
1789 if (sscanf(schema.c_str(), "webrtc.vp8.%u-layer%c", &numLayers, &dummy) == 1
1790 && numLayers > 0) {
1791 switch (numLayers) {
1792 case 1:
1793 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1794 {}, 0u, 1u, 0u);
1795 break;
1796 case 2:
1797 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1798 { .6f }, 0u, 2u, 0u);
1799 break;
1800 case 3:
1801 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1802 { .4f, .6f }, 0u, 3u, 0u);
1803 break;
1804 default:
1805 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1806 { .25f, .4f, .6f }, 0u, 4u, 0u);
1807 break;
1808 }
1809 } else if ((tags = sscanf(schema.c_str(), "android.generic.%u%c%u%c",
1810 &numLayers, &dummy, &numBLayers, &dummy))
1811 && (tags == 1 || (tags == 3 && dummy == '+'))
1812 && numLayers > 0 && numLayers < UINT32_MAX - numBLayers) {
1813 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1814 {}, 0u, numLayers + numBLayers, numBLayers);
1815 } else {
1816 ALOGD("Ignoring unsupported ts-schema [%s]", schema.c_str());
1817 }
1818 if (layering) {
1819 params->setBuffer(C2_PARAMKEY_TEMPORAL_LAYERING,
1820 ABuffer::CreateAsCopy(layering.get(), layering->size()));
1821 }
1822 }
1823 }
1824
1825 { // convert from MediaFormat rect to Codec 2.0 rect
1826 int32_t offset;
1827 int32_t end;
1828 AMessage::ItemData item;
1829 if (params->findInt32("crop-left", &offset) && params->findInt32("crop-right", &end)
1830 && offset >= 0 && end >= offset - 1) {
1831 size_t ix = params->findEntryByName("crop-right");
1832 params->setEntryNameAt(ix, "crop-width");
1833 item.set(end - offset + 1);
1834 params->setEntryAt(ix, item);
1835 }
1836 if (params->findInt32("crop-top", &offset) && params->findInt32("crop-bottom", &end)
1837 && offset >= 0 && end >= offset - 1) {
1838 size_t ix = params->findEntryByName("crop-bottom");
1839 params->setEntryNameAt(ix, "crop-height");
1840 item.set(end - offset + 1);
1841 params->setEntryAt(ix, item);
1842 }
1843 }
1844
1845 { // convert color info
1846 int32_t standard;
1847 if (params->findInt32(KEY_COLOR_STANDARD, &standard)) {
1848 C2Color::primaries_t primaries;
1849 C2Color::matrix_t matrix;
1850
1851 if (C2Mapper::map(standard, &primaries, &matrix)) {
1852 params->setInt32("color-primaries", primaries);
1853 params->setInt32("color-matrix", matrix);
1854 }
1855 }
1856
1857 sp<ABuffer> hdrMeta;
1858 if (params->findBuffer(KEY_HDR_STATIC_INFO, &hdrMeta)
1859 && hdrMeta->size() == sizeof(HDRStaticInfo)) {
1860 HDRStaticInfo *meta = (HDRStaticInfo*)hdrMeta->data();
1861 if (meta->mID == meta->kType1) {
1862 params->setFloat("smpte2086.red.x", meta->sType1.mR.x * 0.00002);
1863 params->setFloat("smpte2086.red.y", meta->sType1.mR.y * 0.00002);
1864 params->setFloat("smpte2086.green.x", meta->sType1.mG.x * 0.00002);
1865 params->setFloat("smpte2086.green.y", meta->sType1.mG.y * 0.00002);
1866 params->setFloat("smpte2086.blue.x", meta->sType1.mB.x * 0.00002);
1867 params->setFloat("smpte2086.blue.y", meta->sType1.mB.y * 0.00002);
1868 params->setFloat("smpte2086.white.x", meta->sType1.mW.x * 0.00002);
1869 params->setFloat("smpte2086.white.y", meta->sType1.mW.y * 0.00002);
1870 params->setFloat("smpte2086.max-luminance", meta->sType1.mMaxDisplayLuminance);
1871 params->setFloat("smpte2086.min-luminance", meta->sType1.mMinDisplayLuminance * 0.0001);
1872 params->setFloat("cta861.max-cll", meta->sType1.mMaxContentLightLevel);
1873 params->setFloat("cta861.max-fall", meta->sType1.mMaxFrameAverageLightLevel);
1874 }
1875 }
1876
1877 sp<ABuffer> hdrDynamicInfo;
1878 if (params->findBuffer(KEY_HDR10_PLUS_INFO, &hdrDynamicInfo)) {
1879 for (const std::string &prefix : { C2_PARAMKEY_INPUT_HDR_DYNAMIC_INFO,
1880 C2_PARAMKEY_OUTPUT_HDR_DYNAMIC_INFO }) {
1881 params->setInt32((prefix + ".type").c_str(),
1882 HDR_DYNAMIC_METADATA_TYPE_SMPTE_2094_40);
1883 params->setBuffer((prefix + ".data").c_str(), hdrDynamicInfo);
1884 }
1885 }
1886 }
1887
1888 if (android::media::codec::provider_->region_of_interest()
1889 && android::media::codec::provider_->region_of_interest_support()) {
1890 if (mDomain == (IS_VIDEO | IS_ENCODER)) {
1891 AString qpOffsetRects;
1892 if (params->findString(PARAMETER_KEY_QP_OFFSET_RECTS, &qpOffsetRects)) {
1893 std::vector<C2QpOffsetRectStruct> c2QpOffsetRects;
1894 char mutableStrQpOffsetRects[strlen(qpOffsetRects.c_str()) + 1];
1895 strcpy(mutableStrQpOffsetRects, qpOffsetRects.c_str());
1896 char* box = strtok(mutableStrQpOffsetRects, ";");
1897 while (box != nullptr) {
1898 int top, left, bottom, right, offset;
1899 if (sscanf(box, "%d,%d-%d,%d=%d", &top, &left, &bottom, &right, &offset) == 5) {
1900 left = c2_max(0, left);
1901 top = c2_max(0, top);
1902 if (right > left && bottom > top) {
1903 C2Rect rect(right - left, bottom - top);
1904 rect.at(left, top);
1905 c2QpOffsetRects.push_back(C2QpOffsetRectStruct(rect, offset));
1906 }
1907 }
1908 box = strtok(nullptr, ";");
1909 }
1910 if (c2QpOffsetRects.size() != 0) {
1911 const std::unique_ptr<C2StreamQpOffsetRects::output> regions =
1912 C2StreamQpOffsetRects::output::AllocUnique(
1913 c2QpOffsetRects.size(), 0u, c2QpOffsetRects);
1914 params->setBuffer(C2_PARAMKEY_QP_OFFSET_RECTS,
1915 ABuffer::CreateAsCopy(regions.get(), regions->size()));
1916 }
1917 }
1918 }
1919 }
1920
1921 // this is to verify that we set proper signedness for standard parameters
1922 bool beVeryStrict = property_get_bool("debug.stagefright.ccodec_strict_type", false);
1923 // this is to allow vendors to use the wrong signedness for standard parameters
1924 bool beVeryLax = property_get_bool("debug.stagefright.ccodec_lax_type", false);
1925
1926 ReflectedParamUpdater::Dict filtered;
1927 for (size_t ix = 0; ix < params->countEntries(); ++ix) {
1928 AMessage::Type type;
1929 AString name = params->getEntryNameAt(ix, &type);
1930 AMessage::ItemData msgItem = params->getEntryAt(ix);
1931 ReflectedParamUpdater::Value item;
1932 convert(msgItem, &item); // convert item to param updater item
1933
1934 if (name.startsWith("vendor.")) {
1935 // vendor params pass through as is
1936 filtered.emplace(name.c_str(), item);
1937 continue;
1938 }
1939 // standard parameters may get modified, filtered or duplicated
1940 for (const ConfigMapper &cm : mStandardParams->getConfigMappersForSdkKey(name.c_str())) {
1941 // note: we ignore port domain for configuration
1942 if ((cm.domain() & configDomain)
1943 // component domain + kind (these must match)
1944 && (cm.domain() & mDomain) == mDomain) {
1945 // map arithmetic values, pass through string or buffer
1946 switch (type) {
1947 case AMessage::kTypeBuffer:
1948 case AMessage::kTypeString:
1949 break;
1950 case AMessage::kTypeInt32:
1951 case AMessage::kTypeInt64:
1952 case AMessage::kTypeFloat:
1953 case AMessage::kTypeDouble:
1954 // for now only map settings with mappers as we are not creating
1955 // signed <=> unsigned mappers
1956 // TODO: be precise about signed unsigned
1957 if (beVeryStrict || cm.mapper()) {
1958 item.set(cm.mapFromMessage(params->getEntryAt(ix)));
1959 // also allow to relax type strictness
1960 if (beVeryLax) {
1961 relaxValues(item);
1962 }
1963 }
1964 break;
1965 default:
1966 continue;
1967 }
1968 filtered.emplace(cm.path(), item);
1969 }
1970 }
1971 }
1972 ALOGV("filter src msg %s", params->debugString(4).c_str());
1973 ALOGV("filter dst params %s", filtered.debugString(4).c_str());
1974 return filtered;
1975 }
1976
getConfigUpdateFromSdkParams(std::shared_ptr<Codec2Client::Configurable> configurable,const sp<AMessage> & sdkParams,Domain configDomain,c2_blocking_t blocking,std::vector<std::unique_ptr<C2Param>> * configUpdate) const1977 status_t CCodecConfig::getConfigUpdateFromSdkParams(
1978 std::shared_ptr<Codec2Client::Configurable> configurable,
1979 const sp<AMessage> &sdkParams, Domain configDomain,
1980 c2_blocking_t blocking,
1981 std::vector<std::unique_ptr<C2Param>> *configUpdate) const {
1982 // update the mappers if we know something more of this format.
1983 // AV1 10b or 8b encoding request.
1984 AString mime;
1985 int32_t requestedSdkProfile = -1;
1986 if ((mDomain == (IS_VIDEO | IS_ENCODER)) &&
1987 sdkParams->findString(KEY_MIME, &mime) &&
1988 mime == MIMETYPE_VIDEO_AV1) {
1989
1990 sdkParams->findInt32(KEY_PROFILE, &requestedSdkProfile);
1991 bool is10bAv1EncodeRequested = (requestedSdkProfile == AV1ProfileMain10);
1992
1993 int32_t bitDepth = (is10bAv1EncodeRequested) ? 10 : 8;
1994 // we always initilze with an 8b mapper. Update this only if needed.
1995 if (bitDepth != 8) {
1996 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
1997 C2Mapper::GetBitDepthProfileLevelMapper(mCodingMediaType, bitDepth);
1998 mStandardParams->updateConfigMappersForKey(StandardParams::SdkKey(KEY_PROFILE),
1999 {
2000 ConfigMapper(KEY_PROFILE, C2_PARAMKEY_PROFILE_LEVEL, "profile")
2001 .limitTo(Domain::CODED)
2002 .withMappers([mapper](C2Value v) -> C2Value {
2003 C2Config::profile_t c2 = PROFILE_UNUSED;
2004 int32_t sdk;
2005 if (mapper && v.get(&sdk) && mapper->mapProfile(sdk, &c2)) {
2006 return c2;
2007 }
2008 return PROFILE_UNUSED;
2009 }, [mapper](C2Value v) -> C2Value {
2010 C2Config::profile_t c2;
2011 int32_t sdk;
2012 using C2ValueType =
2013 typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
2014 if (mapper && v.get((C2ValueType*)&c2) && mapper->mapProfile(c2, &sdk)) {
2015 return sdk;
2016 }
2017 return C2Value();
2018 })});
2019 mStandardParams->updateConfigMappersForKey(StandardParams::SdkKey(KEY_LEVEL),
2020 {
2021 ConfigMapper(KEY_LEVEL, C2_PARAMKEY_PROFILE_LEVEL, "level")
2022 .limitTo(Domain::CODED)
2023 .withMappers([mapper](C2Value v) -> C2Value {
2024 C2Config::level_t c2 = LEVEL_UNUSED;
2025 int32_t sdk;
2026 if (mapper && v.get(&sdk) && mapper->mapLevel(sdk, &c2)) {
2027 return c2;
2028 }
2029 return LEVEL_UNUSED;
2030 }, [mapper](C2Value v) -> C2Value {
2031 C2Config::level_t c2;
2032 int32_t sdk;
2033 using C2ValueType =
2034 typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
2035 if (mapper && v.get((C2ValueType*)&c2) && mapper->mapLevel(c2, &sdk)) {
2036 return sdk;
2037 }
2038 return C2Value();
2039 })});
2040 }
2041 }
2042
2043 ReflectedParamUpdater::Dict params = getReflectedFormat(sdkParams, configDomain);
2044
2045 std::vector<C2Param::Index> indices;
2046 mParamUpdater->getParamIndicesFromMessage(params, &indices);
2047 if (indices.empty()) {
2048 ALOGD("no recognized params in: %s", params.debugString().c_str());
2049 return OK;
2050 }
2051
2052 configUpdate->clear();
2053 std::vector<C2Param::Index> supportedIndices;
2054 for (C2Param::Index ix : indices) {
2055 if (mSupportedIndices.count(ix)) {
2056 supportedIndices.push_back(ix);
2057 } else if (mLocalParams.count(ix)) {
2058 // query local parameter here
2059 auto it = mCurrentConfig.find(ix);
2060 if (it != mCurrentConfig.end()) {
2061 configUpdate->emplace_back(C2Param::Copy(*it->second));
2062 }
2063 }
2064 }
2065
2066 c2_status_t err = configurable->query({ }, supportedIndices, blocking, configUpdate);
2067 if (err != C2_OK) {
2068 ALOGD("query failed after returning %zu params => %s", configUpdate->size(), asString(err));
2069 }
2070
2071 if (configUpdate->size()) {
2072 mParamUpdater->updateParamsFromMessage(params, configUpdate);
2073 }
2074 return OK;
2075 }
2076
setParameters(std::shared_ptr<Codec2Client::Configurable> configurable,std::vector<std::unique_ptr<C2Param>> & configUpdate,c2_blocking_t blocking)2077 status_t CCodecConfig::setParameters(
2078 std::shared_ptr<Codec2Client::Configurable> configurable,
2079 std::vector<std::unique_ptr<C2Param>> &configUpdate,
2080 c2_blocking_t blocking) {
2081 status_t result = OK;
2082 if (configUpdate.empty()) {
2083 return OK;
2084 }
2085
2086 std::vector<C2Param::Index> indices;
2087 std::vector<C2Param *> paramVector;
2088 for (const std::unique_ptr<C2Param> ¶m : configUpdate) {
2089 if (mSupportedIndices.count(param->index())) {
2090 // component parameter
2091 paramVector.push_back(param.get());
2092 indices.push_back(param->index());
2093 } else if (mLocalParams.count(param->index())) {
2094 // handle local parameter here
2095 LocalParamValidator validator = mLocalParams.find(param->index())->second;
2096 c2_status_t err = C2_OK;
2097 std::unique_ptr<C2Param> copy = C2Param::Copy(*param);
2098 if (validator) {
2099 err = validator(copy);
2100 }
2101 if (err == C2_OK) {
2102 ALOGV("updated local parameter value for %s",
2103 mParamUpdater->getParamName(param->index()).c_str());
2104
2105 mCurrentConfig[param->index()] = std::move(copy);
2106 } else {
2107 ALOGD("failed to set parameter value for %s => %s",
2108 mParamUpdater->getParamName(param->index()).c_str(), asString(err));
2109 result = BAD_VALUE;
2110 }
2111 }
2112 }
2113 // update subscribed param indices
2114 subscribeToConfigUpdate(configurable, indices, blocking);
2115
2116 std::vector<std::unique_ptr<C2SettingResult>> failures;
2117 c2_status_t err = configurable->config(paramVector, blocking, &failures);
2118 if (err != C2_OK) {
2119 ALOGD("config failed => %s", asString(err));
2120 // This is non-fatal.
2121 }
2122 for (const std::unique_ptr<C2SettingResult> &failure : failures) {
2123 switch (failure->failure) {
2124 case C2SettingResult::BAD_VALUE:
2125 ALOGD("Bad parameter value");
2126 result = BAD_VALUE;
2127 break;
2128 default:
2129 ALOGV("failure = %d", int(failure->failure));
2130 break;
2131 }
2132 }
2133
2134 // Re-query parameter values in case config could not update them and update the current
2135 // configuration.
2136 configUpdate.clear();
2137 err = configurable->query({}, indices, blocking, &configUpdate);
2138 if (err != C2_OK) {
2139 ALOGD("query failed after returning %zu params => %s", configUpdate.size(), asString(err));
2140 }
2141 (void)updateConfiguration(configUpdate, ALL);
2142
2143 // TODO: error value
2144 return result;
2145 }
2146
getConfigParameterValue(C2Param::Index index) const2147 const C2Param *CCodecConfig::getConfigParameterValue(C2Param::Index index) const {
2148 auto it = mCurrentConfig.find(index);
2149 if (it == mCurrentConfig.end()) {
2150 return nullptr;
2151 } else {
2152 return it->second.get();
2153 }
2154 }
2155
subscribeToAllVendorParams(const std::shared_ptr<Codec2Client::Configurable> & configurable,c2_blocking_t blocking)2156 status_t CCodecConfig::subscribeToAllVendorParams(
2157 const std::shared_ptr<Codec2Client::Configurable> &configurable,
2158 c2_blocking_t blocking) {
2159 for (const auto &[path, desc] : mVendorParams) {
2160 mSubscribedIndices.insert(desc->index());
2161 }
2162 return subscribeToConfigUpdate(configurable, {}, blocking);
2163 }
2164
querySupportedParameters(std::vector<std::string> * names)2165 status_t CCodecConfig::querySupportedParameters(std::vector<std::string> *names) {
2166 if (!names) {
2167 return BAD_VALUE;
2168 }
2169 names->clear();
2170 // TODO: expand to standard params
2171 for (const auto &[key, desc] : mVendorParams) {
2172 if (desc->isVisible()) {
2173 names->push_back(key);
2174 }
2175 }
2176 return OK;
2177 }
2178
describe(const std::string & name,CodecParameterDescriptor * desc)2179 status_t CCodecConfig::describe(const std::string &name, CodecParameterDescriptor *desc) {
2180 if (!desc) {
2181 return BAD_VALUE;
2182 }
2183 // TODO: expand to standard params
2184 desc->name = name;
2185 switch (mParamUpdater->getTypeForKey(name)) {
2186 case C2FieldDescriptor::INT32:
2187 case C2FieldDescriptor::UINT32:
2188 case C2FieldDescriptor::CNTR32:
2189 desc->type = AMessage::kTypeInt32;
2190 return OK;
2191 case C2FieldDescriptor::INT64:
2192 case C2FieldDescriptor::UINT64:
2193 case C2FieldDescriptor::CNTR64:
2194 desc->type = AMessage::kTypeInt64;
2195 return OK;
2196 case C2FieldDescriptor::FLOAT:
2197 desc->type = AMessage::kTypeFloat;
2198 return OK;
2199 case C2FieldDescriptor::STRING:
2200 desc->type = AMessage::kTypeString;
2201 return OK;
2202 case C2FieldDescriptor::BLOB:
2203 desc->type = AMessage::kTypeBuffer;
2204 return OK;
2205 default:
2206 return NAME_NOT_FOUND;
2207 }
2208 }
2209
subscribeToVendorConfigUpdate(const std::shared_ptr<Codec2Client::Configurable> & configurable,const std::vector<std::string> & names,c2_blocking_t blocking)2210 status_t CCodecConfig::subscribeToVendorConfigUpdate(
2211 const std::shared_ptr<Codec2Client::Configurable> &configurable,
2212 const std::vector<std::string> &names,
2213 c2_blocking_t blocking) {
2214 for (const std::string &name : names) {
2215 auto it = mVendorParams.find(name);
2216 if (it == mVendorParams.end()) {
2217 ALOGD("%s is not a recognized vendor parameter; ignored.", name.c_str());
2218 continue;
2219 }
2220 mSubscribedIndices.insert(it->second->index());
2221 }
2222 return subscribeToConfigUpdate(configurable, {}, blocking);
2223 }
2224
unsubscribeFromVendorConfigUpdate(const std::shared_ptr<Codec2Client::Configurable> & configurable,const std::vector<std::string> & names,c2_blocking_t blocking)2225 status_t CCodecConfig::unsubscribeFromVendorConfigUpdate(
2226 const std::shared_ptr<Codec2Client::Configurable> &configurable,
2227 const std::vector<std::string> &names,
2228 c2_blocking_t blocking) {
2229 for (const std::string &name : names) {
2230 auto it = mVendorParams.find(name);
2231 if (it == mVendorParams.end()) {
2232 ALOGD("%s is not a recognized vendor parameter; ignored.", name.c_str());
2233 continue;
2234 }
2235 mSubscribedIndices.erase(it->second->index());
2236 }
2237 return subscribeToConfigUpdate(configurable, {}, blocking);
2238 }
2239
2240 } // namespace android
2241