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 #include <cutils/properties.h>
20 #include <log/log.h>
21
22 #include <C2Component.h>
23 #include <C2Debug.h>
24 #include <C2Param.h>
25 #include <util/C2InterfaceHelper.h>
26
27 #include <media/stagefright/MediaCodecConstants.h>
28
29 #include "CCodecConfig.h"
30 #include "Codec2Mapper.h"
31
32 #define DRC_DEFAULT_MOBILE_REF_LEVEL 64 /* 64*-0.25dB = -16 dB below full scale for mobile conf */
33 #define DRC_DEFAULT_MOBILE_DRC_CUT 127 /* maximum compression of dynamic range for mobile conf */
34 #define DRC_DEFAULT_MOBILE_DRC_BOOST 127 /* maximum compression of dynamic range for mobile conf */
35 #define DRC_DEFAULT_MOBILE_DRC_HEAVY 1 /* switch for heavy compression for mobile conf */
36 #define DRC_DEFAULT_MOBILE_DRC_EFFECT 3 /* MPEG-D DRC effect type; 3 => Limited playback range */
37 #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) */
38 // names of properties that can be used to override the default DRC settings
39 #define PROP_DRC_OVERRIDE_REF_LEVEL "aac_drc_reference_level"
40 #define PROP_DRC_OVERRIDE_CUT "aac_drc_cut"
41 #define PROP_DRC_OVERRIDE_BOOST "aac_drc_boost"
42 #define PROP_DRC_OVERRIDE_HEAVY "aac_drc_heavy"
43 #define PROP_DRC_OVERRIDE_ENC_LEVEL "aac_drc_enc_target_level"
44 #define PROP_DRC_OVERRIDE_EFFECT "ro.aac_drc_effect_type"
45
46 namespace android {
47
48 // CCodecConfig
49
50 namespace {
51
52 /**
53 * mapping between SDK and Codec 2.0 configurations.
54 */
55 struct ConfigMapper {
56 /**
57 * Value mapper (C2Value => C2Value)
58 */
59 typedef std::function<C2Value(C2Value)> Mapper;
60
61 /// shorthand
62 typedef CCodecConfig::Domain Domain;
63
ConfigMapperandroid::__anon458f9ee50111::ConfigMapper64 ConfigMapper(std::string mediaKey, C2String c2struct, C2String c2field)
65 : mDomain(Domain::ALL), mMediaKey(mediaKey), mStruct(c2struct), mField(c2field) { }
66
67 /// Limits this parameter to the given domain
limitToandroid::__anon458f9ee50111::ConfigMapper68 ConfigMapper &limitTo(uint32_t domain) {
69 C2_CHECK(domain & Domain::GUARD_BIT);
70 mDomain = Domain(mDomain & domain);
71 return *this;
72 }
73
74 /// Adds SDK => Codec 2.0 mapper (should not be in the SDK format)
withMapperandroid::__anon458f9ee50111::ConfigMapper75 ConfigMapper &withMapper(Mapper mapper) {
76 C2_CHECK(!mMapper);
77 C2_CHECK(!mReverse);
78 mMapper = mapper;
79 return *this;
80 }
81
82 /// Adds SDK <=> Codec 2.0 value mappers
withMappersandroid::__anon458f9ee50111::ConfigMapper83 ConfigMapper &withMappers(Mapper mapper, Mapper reverse) {
84 C2_CHECK(!mMapper);
85 C2_CHECK(!mReverse);
86 mMapper = mapper;
87 mReverse = reverse;
88 return *this;
89 }
90
91 /// Adds SDK <=> Codec 2.0 value mappers based on C2Mapper
92 template<typename C2Type, typename SdkType=int32_t>
withC2Mappersandroid::__anon458f9ee50111::ConfigMapper93 ConfigMapper &withC2Mappers() {
94 C2_CHECK(!mMapper);
95 C2_CHECK(!mReverse);
96 mMapper = [](C2Value v) -> C2Value {
97 SdkType sdkValue;
98 C2Type c2Value;
99 if (v.get(&sdkValue) && C2Mapper::map(sdkValue, &c2Value)) {
100 return c2Value;
101 }
102 return C2Value();
103 };
104 mReverse = [](C2Value v) -> C2Value {
105 SdkType sdkValue;
106 C2Type c2Value;
107 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<C2Type>::type;
108 if (v.get((C2ValueType*)&c2Value) && C2Mapper::map(c2Value, &sdkValue)) {
109 return sdkValue;
110 }
111 return C2Value();
112 };
113 return *this;
114 }
115
116 /// Maps from SDK values in an AMessage to a suitable C2Value.
mapFromMessageandroid::__anon458f9ee50111::ConfigMapper117 C2Value mapFromMessage(const AMessage::ItemData &item) const {
118 C2Value value;
119 int32_t int32Value;
120 int64_t int64Value;
121 float floatValue;
122 double doubleValue;
123 if (item.find(&int32Value)) {
124 value = int32Value;
125 } else if (item.find(&int64Value)) {
126 value = int64Value;
127 } else if (item.find(&floatValue)) {
128 value = floatValue;
129 } else if (item.find(&doubleValue)) {
130 value = (float)doubleValue;
131 }
132 if (value.type() != C2Value::NO_INIT && mMapper) {
133 value = mMapper(value);
134 }
135 return value;
136 }
137
138 /// Maps from a C2Value to an SDK value in an AMessage.
mapToMessageandroid::__anon458f9ee50111::ConfigMapper139 AMessage::ItemData mapToMessage(C2Value value) const {
140 AMessage::ItemData item;
141 int32_t int32Value;
142 uint32_t uint32Value;
143 int64_t int64Value;
144 uint64_t uint64Value;
145 float floatValue;
146 if (value.type() != C2Value::NO_INIT && mReverse) {
147 value = mReverse(value);
148 }
149 if (value.get(&int32Value)) {
150 item.set(int32Value);
151 } else if (value.get(&uint32Value) && uint32Value <= uint32_t(INT32_MAX)) {
152 // SDK does not support unsigned values
153 item.set((int32_t)uint32Value);
154 } else if (value.get(&int64Value)) {
155 item.set(int64Value);
156 } else if (value.get(&uint64Value) && uint64Value <= uint64_t(INT64_MAX)) {
157 // SDK does not support unsigned values
158 item.set((int64_t)uint64Value);
159 } else if (value.get(&floatValue)) {
160 item.set(floatValue);
161 }
162 return item;
163 }
164
domainandroid::__anon458f9ee50111::ConfigMapper165 Domain domain() const { return mDomain; }
mediaKeyandroid::__anon458f9ee50111::ConfigMapper166 std::string mediaKey() const { return mMediaKey; }
pathandroid::__anon458f9ee50111::ConfigMapper167 std::string path() const { return mField.size() ? mStruct + '.' + mField : mStruct; }
mapperandroid::__anon458f9ee50111::ConfigMapper168 Mapper mapper() const { return mMapper; }
reverseandroid::__anon458f9ee50111::ConfigMapper169 Mapper reverse() const { return mReverse; }
170
171 private:
172 Domain mDomain; ///< parameter domain (mask) containing port, kind and config domains
173 std::string mMediaKey; ///< SDK key
174 C2String mStruct; ///< Codec 2.0 struct name
175 C2String mField; ///< Codec 2.0 field name
176 Mapper mMapper; ///< optional SDK => Codec 2.0 value mapper
177 Mapper mReverse; ///< optional Codec 2.0 => SDK value mapper
178 };
179
180 template <typename PORT, typename STREAM>
QueryMediaTypeImpl(const std::shared_ptr<Codec2Client::Component> & component)181 AString QueryMediaTypeImpl(
182 const std::shared_ptr<Codec2Client::Component> &component) {
183 AString mediaType;
184 std::vector<std::unique_ptr<C2Param>> queried;
185 c2_status_t c2err = component->query(
186 {}, { PORT::PARAM_TYPE, STREAM::PARAM_TYPE }, C2_DONT_BLOCK, &queried);
187 if (c2err != C2_OK && queried.size() == 0) {
188 ALOGD("Query media type failed => %s", asString(c2err));
189 } else {
190 PORT *portMediaType =
191 PORT::From(queried[0].get());
192 if (portMediaType) {
193 mediaType = AString(
194 portMediaType->m.value,
195 strnlen(portMediaType->m.value, portMediaType->flexCount()));
196 } else {
197 STREAM *streamMediaType = STREAM::From(queried[0].get());
198 if (streamMediaType) {
199 mediaType = AString(
200 streamMediaType->m.value,
201 strnlen(streamMediaType->m.value, streamMediaType->flexCount()));
202 }
203 }
204 ALOGD("read media type: %s", mediaType.c_str());
205 }
206 return mediaType;
207 }
208
QueryMediaType(bool input,const std::shared_ptr<Codec2Client::Component> & component)209 AString QueryMediaType(
210 bool input, const std::shared_ptr<Codec2Client::Component> &component) {
211 typedef C2PortMediaTypeSetting P;
212 typedef C2StreamMediaTypeSetting S;
213 if (input) {
214 return QueryMediaTypeImpl<P::input, S::input>(component);
215 } else {
216 return QueryMediaTypeImpl<P::output, S::output>(component);
217 }
218 }
219
220 } // namespace
221
222 /**
223 * Set of standard parameters used by CCodec that are exposed to MediaCodec.
224 */
225 struct StandardParams {
226 typedef CCodecConfig::Domain Domain;
227
228 // standard (MediaCodec) params are keyed by media format key
229 typedef std::string SdkKey;
230
231 /// used to return reference to no config mappers in getConfigMappersForSdkKey
232 static const std::vector<ConfigMapper> NO_MAPPERS;
233
234 /// Returns Codec 2.0 equivalent parameters for an SDK format key.
getConfigMappersForSdkKeyandroid::StandardParams235 const std::vector<ConfigMapper> &getConfigMappersForSdkKey(std::string key) const {
236 auto it = mConfigMappers.find(key);
237 if (it == mConfigMappers.end()) {
238 ALOGD("no c2 equivalents for %s", key.c_str());
239 return NO_MAPPERS;
240 }
241 ALOGV("found %zu eqs for %s", it->second.size(), key.c_str());
242 return it->second;
243 }
244
245 /**
246 * Adds a SDK <=> Codec 2.0 parameter mapping. Multiple Codec 2.0 parameters may map to a
247 * single SDK key, in which case they shall be ordered from least authoritative to most
248 * authoritative. When constructing SDK formats, the last mapped Codec 2.0 parameter that
249 * is supported by the component will determine the exposed value. (TODO: perhaps restrict this
250 * by domain.)
251 */
addandroid::StandardParams252 void add(const ConfigMapper &cm) {
253 auto it = mConfigMappers.find(cm.mediaKey());
254 ALOGV("%c%c%c%c %c%c%c %04x %9s %s => %s",
255 ((cm.domain() & Domain::IS_INPUT) ? 'I' : ' '),
256 ((cm.domain() & Domain::IS_OUTPUT) ? 'O' : ' '),
257 ((cm.domain() & Domain::IS_CODED) ? 'C' : ' '),
258 ((cm.domain() & Domain::IS_RAW) ? 'R' : ' '),
259 ((cm.domain() & Domain::IS_CONFIG) ? 'c' : ' '),
260 ((cm.domain() & Domain::IS_PARAM) ? 'p' : ' '),
261 ((cm.domain() & Domain::IS_READ) ? 'r' : ' '),
262 cm.domain(),
263 it == mConfigMappers.end() ? "adding" : "extending",
264 cm.mediaKey().c_str(), cm.path().c_str());
265 if (it == mConfigMappers.end()) {
266 std::vector<ConfigMapper> eqs = { cm };
267 mConfigMappers.emplace(cm.mediaKey(), eqs);
268 } else {
269 it->second.push_back(cm);
270 }
271 }
272
273 /**
274 * Returns all paths for a specific domain.
275 *
276 * \param any maximum domain mask. Returned parameters must match at least one of the domains
277 * in the mask.
278 * \param all minimum domain mask. Returned parameters must match all of the domains in the
279 * mask. This is restricted to the bits of the maximum mask.
280 */
getPathsForDomainandroid::StandardParams281 std::vector<std::string> getPathsForDomain(
282 Domain any, Domain all = Domain::ALL) const {
283 std::vector<std::string> res;
284 for (const std::pair<std::string, std::vector<ConfigMapper>> &el : mConfigMappers) {
285 for (const ConfigMapper &cm : el.second) {
286 ALOGV("filtering %s %x %x %x %x", cm.path().c_str(), cm.domain(), any,
287 (cm.domain() & any), (cm.domain() & any & all));
288 if ((cm.domain() & any) && ((cm.domain() & any & all) == (any & all))) {
289 res.push_back(cm.path());
290 }
291 }
292 }
293 return res;
294 }
295
296 /**
297 * Returns SDK <=> Codec 2.0 mappings.
298 *
299 * TODO: replace these with better methods as this exposes the inner structure.
300 */
getKeysandroid::StandardParams301 const std::map<SdkKey, std::vector<ConfigMapper>> getKeys() const {
302 return mConfigMappers;
303 }
304
305 private:
306 std::map<SdkKey, std::vector<ConfigMapper>> mConfigMappers;
307 };
308
309 const std::vector<ConfigMapper> StandardParams::NO_MAPPERS;
310
311
CCodecConfig()312 CCodecConfig::CCodecConfig()
313 : mInputFormat(new AMessage),
314 mOutputFormat(new AMessage),
315 mUsingSurface(false) { }
316
initializeStandardParams()317 void CCodecConfig::initializeStandardParams() {
318 typedef Domain D;
319 mStandardParams = std::make_shared<StandardParams>();
320 std::function<void(const ConfigMapper &)> add =
321 [params = mStandardParams](const ConfigMapper &cm) {
322 params->add(cm);
323 };
324 std::function<void(const ConfigMapper &)> deprecated = add;
325
326 // allow int32 or float SDK values and represent them as float
327 ConfigMapper::Mapper makeFloat = [](C2Value v) -> C2Value {
328 // convert from i32 to float
329 int32_t i32Value;
330 float fpValue;
331 if (v.get(&i32Value)) {
332 return (float)i32Value;
333 } else if (v.get(&fpValue)) {
334 return fpValue;
335 }
336 return C2Value();
337 };
338
339 ConfigMapper::Mapper negate = [](C2Value v) -> C2Value {
340 int32_t value;
341 if (v.get(&value)) {
342 return -value;
343 }
344 return C2Value();
345 };
346
347 add(ConfigMapper(KEY_MIME, C2_PARAMKEY_INPUT_MEDIA_TYPE, "value")
348 .limitTo(D::INPUT & D::READ));
349 add(ConfigMapper(KEY_MIME, C2_PARAMKEY_OUTPUT_MEDIA_TYPE, "value")
350 .limitTo(D::OUTPUT & D::READ));
351
352 add(ConfigMapper(KEY_BIT_RATE, C2_PARAMKEY_BITRATE, "value")
353 .limitTo(D::ENCODER & D::OUTPUT));
354 // we also need to put the bitrate in the max bitrate field
355 add(ConfigMapper(KEY_MAX_BIT_RATE, C2_PARAMKEY_BITRATE, "value")
356 .limitTo(D::ENCODER & D::READ & D::OUTPUT));
357 add(ConfigMapper(PARAMETER_KEY_VIDEO_BITRATE, C2_PARAMKEY_BITRATE, "value")
358 .limitTo(D::ENCODER & D::VIDEO & D::PARAM));
359 add(ConfigMapper(KEY_BITRATE_MODE, C2_PARAMKEY_BITRATE_MODE, "value")
360 .limitTo(D::ENCODER & D::CODED)
361 .withMapper([](C2Value v) -> C2Value {
362 int32_t value;
363 C2Config::bitrate_mode_t mode;
364 if (v.get(&value) && C2Mapper::map(value, &mode)) {
365 return mode;
366 }
367 return C2Value();
368 }));
369 // remove when codecs switch to PARAMKEY and new modes
370 deprecated(ConfigMapper(KEY_BITRATE_MODE, "coded.bitrate-mode", "value")
371 .limitTo(D::ENCODER));
372 add(ConfigMapper(KEY_FRAME_RATE, C2_PARAMKEY_FRAME_RATE, "value")
373 .limitTo(D::VIDEO)
374 .withMappers(makeFloat, [](C2Value v) -> C2Value {
375 // read back always as int
376 float value;
377 if (v.get(&value)) {
378 return (int32_t)value;
379 }
380 return C2Value();
381 }));
382
383 add(ConfigMapper(KEY_MAX_INPUT_SIZE, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE, "value")
384 .limitTo(D::INPUT));
385 // remove when codecs switch to PARAMKEY
386 deprecated(ConfigMapper(KEY_MAX_INPUT_SIZE, "coded.max-frame-size", "value")
387 .limitTo(D::INPUT));
388
389 // Rotation
390 // Note: SDK rotation is clock-wise, while C2 rotation is counter-clock-wise
391 add(ConfigMapper(KEY_ROTATION, C2_PARAMKEY_VUI_ROTATION, "value")
392 .limitTo(D::VIDEO & D::CODED)
393 .withMappers(negate, negate));
394 add(ConfigMapper(KEY_ROTATION, C2_PARAMKEY_ROTATION, "value")
395 .limitTo(D::VIDEO & D::RAW)
396 .withMappers(negate, negate));
397
398 // android 'video-scaling'
399 add(ConfigMapper("android._video-scaling", C2_PARAMKEY_SURFACE_SCALING_MODE, "value")
400 .limitTo(D::VIDEO & D::DECODER & D::RAW));
401
402 // Color Aspects
403 //
404 // configure default for decoders
405 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "range")
406 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM))
407 .withC2Mappers<C2Color::range_t>());
408 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "transfer")
409 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM))
410 .withC2Mappers<C2Color::transfer_t>());
411 add(ConfigMapper("color-primaries", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "primaries")
412 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM)));
413 add(ConfigMapper("color-matrix", C2_PARAMKEY_DEFAULT_COLOR_ASPECTS, "matrix")
414 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::CODED & (D::CONFIG | D::PARAM)));
415
416 // read back final for decoder output (also, configure final aspects as well. This should be
417 // overwritten based on coded/default values if component supports color aspects, but is used
418 // as final values if component does not support aspects at all)
419 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_COLOR_ASPECTS, "range")
420 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW)
421 .withC2Mappers<C2Color::range_t>());
422 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_COLOR_ASPECTS, "transfer")
423 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW)
424 .withC2Mappers<C2Color::transfer_t>());
425 add(ConfigMapper("color-primaries", C2_PARAMKEY_COLOR_ASPECTS, "primaries")
426 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW));
427 add(ConfigMapper("color-matrix", C2_PARAMKEY_COLOR_ASPECTS, "matrix")
428 .limitTo((D::VIDEO | D::IMAGE) & D::DECODER & D::RAW));
429
430 // configure source aspects for encoders and read them back on the coded(!) port.
431 // This is to ensure muxing the desired aspects into the container.
432 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_COLOR_ASPECTS, "range")
433 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED)
434 .withC2Mappers<C2Color::range_t>());
435 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_COLOR_ASPECTS, "transfer")
436 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED)
437 .withC2Mappers<C2Color::transfer_t>());
438 add(ConfigMapper("color-primaries", C2_PARAMKEY_COLOR_ASPECTS, "primaries")
439 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED));
440 add(ConfigMapper("color-matrix", C2_PARAMKEY_COLOR_ASPECTS, "matrix")
441 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::CODED));
442
443 // read back coded aspects for encoders (on the raw port), but also configure
444 // desired aspects here.
445 add(ConfigMapper(KEY_COLOR_RANGE, C2_PARAMKEY_VUI_COLOR_ASPECTS, "range")
446 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW)
447 .withC2Mappers<C2Color::range_t>());
448 add(ConfigMapper(KEY_COLOR_TRANSFER, C2_PARAMKEY_VUI_COLOR_ASPECTS, "transfer")
449 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW)
450 .withC2Mappers<C2Color::transfer_t>());
451 add(ConfigMapper("color-primaries", C2_PARAMKEY_VUI_COLOR_ASPECTS, "primaries")
452 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW));
453 add(ConfigMapper("color-matrix", C2_PARAMKEY_VUI_COLOR_ASPECTS, "matrix")
454 .limitTo((D::VIDEO | D::IMAGE) & D::ENCODER & D::RAW));
455
456 // Dataspace
457 add(ConfigMapper("android._dataspace", C2_PARAMKEY_DATA_SPACE, "value")
458 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
459
460 // HDR
461 add(ConfigMapper("smpte2086.red.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.red.x")
462 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
463 add(ConfigMapper("smpte2086.red.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.red.y")
464 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
465 add(ConfigMapper("smpte2086.green.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.green.x")
466 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
467 add(ConfigMapper("smpte2086.green.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.green.y")
468 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
469 add(ConfigMapper("smpte2086.blue.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.blue.x")
470 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
471 add(ConfigMapper("smpte2086.blue.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.blue.y")
472 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
473 add(ConfigMapper("smpte2086.white.x", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.white.x")
474 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
475 add(ConfigMapper("smpte2086.white.y", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.white.y")
476 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
477 add(ConfigMapper("smpte2086.max-luminance", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.max-luminance")
478 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
479 add(ConfigMapper("smpte2086.min-luminance", C2_PARAMKEY_HDR_STATIC_INFO, "mastering.min-luminance")
480 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
481 add(ConfigMapper("cta861.max-cll", C2_PARAMKEY_HDR_STATIC_INFO, "max-cll")
482 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
483 add(ConfigMapper("cta861.max-fall", C2_PARAMKEY_HDR_STATIC_INFO, "max-fall")
484 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
485
486 add(ConfigMapper(std::string(KEY_FEATURE_) + FEATURE_SecurePlayback,
487 C2_PARAMKEY_SECURE_MODE, "value"));
488
489 add(ConfigMapper("prepend-sps-pps-to-idr-frames",
490 C2_PARAMKEY_PREPEND_HEADER_MODE, "value")
491 .limitTo(D::ENCODER & D::VIDEO)
492 .withMapper([](C2Value v) -> C2Value {
493 int32_t value;
494 if (v.get(&value) && value) {
495 return C2Value(C2Config::PREPEND_HEADER_TO_ALL_SYNC);
496 } else {
497 return C2Value(C2Config::PREPEND_HEADER_TO_NONE);
498 }
499 }));
500 // remove when codecs switch to PARAMKEY
501 deprecated(ConfigMapper("prepend-sps-pps-to-idr-frames",
502 "coding.add-csd-to-sync-frames", "value")
503 .limitTo(D::ENCODER & D::VIDEO));
504 // convert to timestamp base
505 add(ConfigMapper(KEY_I_FRAME_INTERVAL, C2_PARAMKEY_SYNC_FRAME_INTERVAL, "value")
506 .withMapper([](C2Value v) -> C2Value {
507 // convert from i32 to float
508 int32_t i32Value;
509 float fpValue;
510 if (v.get(&i32Value)) {
511 return int64_t(1000000) * i32Value;
512 } else if (v.get(&fpValue)) {
513 return int64_t(c2_min(1000000 * fpValue + 0.5, (double)INT64_MAX));
514 }
515 return C2Value();
516 }));
517 // remove when codecs switch to proper coding.gop (add support for calculating gop)
518 deprecated(ConfigMapper("i-frame-period", "coding.gop", "intra-period")
519 .limitTo(D::ENCODER & D::VIDEO));
520 add(ConfigMapper(KEY_INTRA_REFRESH_PERIOD, C2_PARAMKEY_INTRA_REFRESH, "period")
521 .limitTo(D::VIDEO & D::ENCODER)
522 .withMappers(makeFloat, [](C2Value v) -> C2Value {
523 // read back always as int
524 float value;
525 if (v.get(&value)) {
526 return (int32_t)value;
527 }
528 return C2Value();
529 }));
530 add(ConfigMapper(KEY_QUALITY, C2_PARAMKEY_QUALITY, "value"));
531 deprecated(ConfigMapper(PARAMETER_KEY_REQUEST_SYNC_FRAME,
532 "coding.request-sync", "value")
533 .limitTo(D::PARAM & D::ENCODER)
534 .withMapper([](C2Value) -> C2Value { return uint32_t(1); }));
535 add(ConfigMapper(PARAMETER_KEY_REQUEST_SYNC_FRAME,
536 C2_PARAMKEY_REQUEST_SYNC_FRAME, "value")
537 .limitTo(D::PARAM & D::ENCODER)
538 .withMapper([](C2Value) -> C2Value { return uint32_t(1); }));
539
540 add(ConfigMapper(KEY_OPERATING_RATE, C2_PARAMKEY_OPERATING_RATE, "value")
541 .limitTo(D::PARAM | D::CONFIG) // write-only
542 .withMapper(makeFloat));
543 // C2 priorities are inverted
544 add(ConfigMapper(KEY_PRIORITY, C2_PARAMKEY_PRIORITY, "value")
545 .withMappers(negate, negate));
546 // remove when codecs switch to PARAMKEY
547 deprecated(ConfigMapper(KEY_OPERATING_RATE, "ctrl.operating-rate", "value")
548 .withMapper(makeFloat));
549 deprecated(ConfigMapper(KEY_PRIORITY, "ctrl.priority", "value"));
550
551 add(ConfigMapper(KEY_WIDTH, C2_PARAMKEY_PICTURE_SIZE, "width")
552 .limitTo(D::VIDEO | D::IMAGE));
553 add(ConfigMapper(KEY_HEIGHT, C2_PARAMKEY_PICTURE_SIZE, "height")
554 .limitTo(D::VIDEO | D::IMAGE));
555
556 add(ConfigMapper("crop-left", C2_PARAMKEY_CROP_RECT, "left")
557 .limitTo(D::VIDEO | D::IMAGE));
558 add(ConfigMapper("crop-top", C2_PARAMKEY_CROP_RECT, "top")
559 .limitTo(D::VIDEO | D::IMAGE));
560 add(ConfigMapper("crop-width", C2_PARAMKEY_CROP_RECT, "width")
561 .limitTo(D::VIDEO | D::IMAGE));
562 add(ConfigMapper("crop-height", C2_PARAMKEY_CROP_RECT, "height")
563 .limitTo(D::VIDEO | D::IMAGE));
564
565 add(ConfigMapper(KEY_MAX_WIDTH, C2_PARAMKEY_MAX_PICTURE_SIZE, "width")
566 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
567 add(ConfigMapper(KEY_MAX_HEIGHT, C2_PARAMKEY_MAX_PICTURE_SIZE, "height")
568 .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
569
570 add(ConfigMapper("csd-0", C2_PARAMKEY_INIT_DATA, "value")
571 .limitTo(D::OUTPUT & D::READ));
572
573 add(ConfigMapper(KEY_HDR10_PLUS_INFO, C2_PARAMKEY_INPUT_HDR10_PLUS_INFO, "value")
574 .limitTo(D::VIDEO & D::PARAM & D::INPUT));
575
576 add(ConfigMapper(KEY_HDR10_PLUS_INFO, C2_PARAMKEY_OUTPUT_HDR10_PLUS_INFO, "value")
577 .limitTo(D::VIDEO & D::OUTPUT));
578
579 add(ConfigMapper(C2_PARAMKEY_TEMPORAL_LAYERING, C2_PARAMKEY_TEMPORAL_LAYERING, "")
580 .limitTo(D::ENCODER & D::VIDEO & D::OUTPUT));
581
582 // Pixel Format (use local key for actual pixel format as we don't distinguish between
583 // SDK layouts for flexible format and we need the actual SDK color format in the media format)
584 add(ConfigMapper("android._color-format", C2_PARAMKEY_PIXEL_FORMAT, "value")
585 .limitTo((D::VIDEO | D::IMAGE) & D::RAW)
586 .withMappers([](C2Value v) -> C2Value {
587 int32_t value;
588 if (v.get(&value)) {
589 switch (value) {
590 case COLOR_FormatSurface:
591 return (uint32_t)HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
592 case COLOR_FormatYUV420Flexible:
593 return (uint32_t)HAL_PIXEL_FORMAT_YCBCR_420_888;
594 case COLOR_FormatYUV420Planar:
595 case COLOR_FormatYUV420SemiPlanar:
596 case COLOR_FormatYUV420PackedPlanar:
597 case COLOR_FormatYUV420PackedSemiPlanar:
598 return (uint32_t)HAL_PIXEL_FORMAT_YV12;
599 default:
600 // TODO: support some sort of passthrough
601 break;
602 }
603 }
604 return C2Value();
605 }, [](C2Value v) -> C2Value {
606 uint32_t value;
607 if (v.get(&value)) {
608 switch (value) {
609 case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
610 return COLOR_FormatSurface;
611 case HAL_PIXEL_FORMAT_YV12:
612 case HAL_PIXEL_FORMAT_YCBCR_420_888:
613 return COLOR_FormatYUV420Flexible;
614 default:
615 // TODO: support some sort of passthrough
616 break;
617 }
618 }
619 return C2Value();
620 }));
621
622 add(ConfigMapper(KEY_CHANNEL_COUNT, C2_PARAMKEY_CHANNEL_COUNT, "value")
623 .limitTo(D::AUDIO)); // read back to both formats
624 add(ConfigMapper(KEY_CHANNEL_COUNT, C2_PARAMKEY_CODED_CHANNEL_COUNT, "value")
625 .limitTo(D::AUDIO & D::CODED));
626
627 add(ConfigMapper(KEY_SAMPLE_RATE, C2_PARAMKEY_SAMPLE_RATE, "value")
628 .limitTo(D::AUDIO)); // read back to both port formats
629 add(ConfigMapper(KEY_SAMPLE_RATE, C2_PARAMKEY_CODED_SAMPLE_RATE, "value")
630 .limitTo(D::AUDIO & D::CODED));
631
632 add(ConfigMapper(KEY_PCM_ENCODING, C2_PARAMKEY_PCM_ENCODING, "value")
633 .limitTo(D::AUDIO)
634 .withMappers([](C2Value v) -> C2Value {
635 int32_t value;
636 C2Config::pcm_encoding_t to;
637 if (v.get(&value) && C2Mapper::map(value, &to)) {
638 return to;
639 }
640 return C2Value();
641 }, [](C2Value v) -> C2Value {
642 C2Config::pcm_encoding_t value;
643 int32_t to;
644 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(value)>::type;
645 if (v.get((C2ValueType*)&value) && C2Mapper::map(value, &to)) {
646 return to;
647 }
648 return C2Value();
649 }));
650
651 add(ConfigMapper(KEY_IS_ADTS, C2_PARAMKEY_AAC_PACKAGING, "value")
652 .limitTo(D::AUDIO & D::CODED)
653 .withMappers([](C2Value v) -> C2Value {
654 int32_t value;
655 if (v.get(&value) && value) {
656 return C2Config::AAC_PACKAGING_ADTS;
657 }
658 return C2Value();
659 }, [](C2Value v) -> C2Value {
660 uint32_t value;
661 if (v.get(&value) && value == C2Config::AAC_PACKAGING_ADTS) {
662 return (int32_t)1;
663 }
664 return C2Value();
665 }));
666
667 std::shared_ptr<C2Mapper::ProfileLevelMapper> mapper =
668 C2Mapper::GetProfileLevelMapper(mCodingMediaType);
669
670 add(ConfigMapper(KEY_PROFILE, C2_PARAMKEY_PROFILE_LEVEL, "profile")
671 .limitTo(D::CODED)
672 .withMappers([mapper](C2Value v) -> C2Value {
673 C2Config::profile_t c2 = PROFILE_UNUSED;
674 int32_t sdk;
675 if (mapper && v.get(&sdk) && mapper->mapProfile(sdk, &c2)) {
676 return c2;
677 }
678 return PROFILE_UNUSED;
679 }, [mapper](C2Value v) -> C2Value {
680 C2Config::profile_t c2;
681 int32_t sdk;
682 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
683 if (mapper && v.get((C2ValueType*)&c2) && mapper->mapProfile(c2, &sdk)) {
684 return sdk;
685 }
686 return C2Value();
687 }));
688
689 add(ConfigMapper(KEY_LEVEL, C2_PARAMKEY_PROFILE_LEVEL, "level")
690 .limitTo(D::CODED)
691 .withMappers([mapper](C2Value v) -> C2Value {
692 C2Config::level_t c2 = LEVEL_UNUSED;
693 int32_t sdk;
694 if (mapper && v.get(&sdk) && mapper->mapLevel(sdk, &c2)) {
695 return c2;
696 }
697 return LEVEL_UNUSED;
698 }, [mapper](C2Value v) -> C2Value {
699 C2Config::level_t c2;
700 int32_t sdk;
701 using C2ValueType=typename _c2_reduce_enum_to_underlying_type<decltype(c2)>::type;
702 if (mapper && v.get((C2ValueType*)&c2) && mapper->mapLevel(c2, &sdk)) {
703 return sdk;
704 }
705 return C2Value();
706 }));
707
708 // convert to dBFS and add default
709 add(ConfigMapper(KEY_AAC_DRC_TARGET_REFERENCE_LEVEL, C2_PARAMKEY_DRC_TARGET_REFERENCE_LEVEL, "value")
710 .limitTo(D::AUDIO & D::DECODER)
711 .withMapper([](C2Value v) -> C2Value {
712 int32_t value;
713 if (!v.get(&value) || value < 0) {
714 value = property_get_int32(PROP_DRC_OVERRIDE_REF_LEVEL, DRC_DEFAULT_MOBILE_REF_LEVEL);
715 }
716 return float(-0.25 * c2_min(value, 127));
717 }));
718
719 // convert to 0-1 (%) and add default
720 add(ConfigMapper(KEY_AAC_DRC_ATTENUATION_FACTOR, C2_PARAMKEY_DRC_ATTENUATION_FACTOR, "value")
721 .limitTo(D::AUDIO & D::DECODER)
722 .withMapper([](C2Value v) -> C2Value {
723 int32_t value;
724 if (!v.get(&value) || value < 0) {
725 value = property_get_int32(PROP_DRC_OVERRIDE_CUT, DRC_DEFAULT_MOBILE_DRC_CUT);
726 }
727 return float(c2_min(value, 127) / 127.);
728 }));
729
730 // convert to 0-1 (%) and add default
731 add(ConfigMapper(KEY_AAC_DRC_BOOST_FACTOR, C2_PARAMKEY_DRC_BOOST_FACTOR, "value")
732 .limitTo(D::AUDIO & D::DECODER)
733 .withMapper([](C2Value v) -> C2Value {
734 int32_t value;
735 if (!v.get(&value) || value < 0) {
736 value = property_get_int32(PROP_DRC_OVERRIDE_BOOST, DRC_DEFAULT_MOBILE_DRC_BOOST);
737 }
738 return float(c2_min(value, 127) / 127.);
739 }));
740
741 // convert to compression type and add default
742 add(ConfigMapper(KEY_AAC_DRC_HEAVY_COMPRESSION, C2_PARAMKEY_DRC_COMPRESSION_MODE, "value")
743 .limitTo(D::AUDIO & D::DECODER)
744 .withMapper([](C2Value v) -> C2Value {
745 int32_t value;
746 if (!v.get(&value) || value < 0) {
747 value = property_get_int32(PROP_DRC_OVERRIDE_HEAVY, DRC_DEFAULT_MOBILE_DRC_HEAVY);
748 }
749 return value == 1 ? C2Config::DRC_COMPRESSION_HEAVY : C2Config::DRC_COMPRESSION_LIGHT;
750 }));
751
752 // convert to dBFS and add default
753 add(ConfigMapper(KEY_AAC_ENCODED_TARGET_LEVEL, C2_PARAMKEY_DRC_ENCODED_TARGET_LEVEL, "value")
754 .limitTo(D::AUDIO & D::DECODER)
755 .withMapper([](C2Value v) -> C2Value {
756 int32_t value;
757 if (!v.get(&value) || value < 0) {
758 value = property_get_int32(PROP_DRC_OVERRIDE_ENC_LEVEL, DRC_DEFAULT_MOBILE_ENC_LEVEL);
759 }
760 return float(-0.25 * c2_min(value, 127));
761 }));
762
763 // convert to effect type (these map to SDK values) and add default
764 add(ConfigMapper(KEY_AAC_DRC_EFFECT_TYPE, C2_PARAMKEY_DRC_EFFECT_TYPE, "value")
765 .limitTo(D::AUDIO & D::DECODER)
766 .withMapper([](C2Value v) -> C2Value {
767 int32_t value;
768 if (!v.get(&value) || value < -1 || value > 8) {
769 value = property_get_int32(PROP_DRC_OVERRIDE_EFFECT, DRC_DEFAULT_MOBILE_DRC_EFFECT);
770 // ensure value is within range
771 if (value < -1 || value > 8) {
772 value = DRC_DEFAULT_MOBILE_DRC_EFFECT;
773 }
774 }
775 return value;
776 }));
777
778 add(ConfigMapper(KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT, C2_PARAMKEY_MAX_CHANNEL_COUNT, "value")
779 .limitTo(D::AUDIO));
780
781 add(ConfigMapper(KEY_AAC_SBR_MODE, C2_PARAMKEY_AAC_SBR_MODE, "value")
782 .limitTo(D::AUDIO & D::ENCODER & D::CONFIG)
783 .withMapper([](C2Value v) -> C2Value {
784 int32_t value;
785 if (!v.get(&value) || value < 0) {
786 return C2Config::AAC_SBR_AUTO;
787 }
788 switch (value) {
789 case 0: return C2Config::AAC_SBR_OFF;
790 case 1: return C2Config::AAC_SBR_SINGLE_RATE;
791 case 2: return C2Config::AAC_SBR_DUAL_RATE;
792 default: return C2Config::AAC_SBR_AUTO + 1; // invalid value
793 }
794 }));
795
796 add(ConfigMapper(KEY_QUALITY, C2_PARAMKEY_QUALITY, "value"));
797 add(ConfigMapper(KEY_FLAC_COMPRESSION_LEVEL, C2_PARAMKEY_COMPLEXITY, "value")
798 .limitTo(D::AUDIO & D::ENCODER));
799 add(ConfigMapper("complexity", C2_PARAMKEY_COMPLEXITY, "value")
800 .limitTo(D::ENCODER));
801
802 add(ConfigMapper(KEY_GRID_COLUMNS, C2_PARAMKEY_TILE_LAYOUT, "columns")
803 .limitTo(D::IMAGE));
804 add(ConfigMapper(KEY_GRID_ROWS, C2_PARAMKEY_TILE_LAYOUT, "rows")
805 .limitTo(D::IMAGE));
806 add(ConfigMapper(KEY_TILE_WIDTH, C2_PARAMKEY_TILE_LAYOUT, "tile.width")
807 .limitTo(D::IMAGE));
808 add(ConfigMapper(KEY_TILE_HEIGHT, C2_PARAMKEY_TILE_LAYOUT, "tile.height")
809 .limitTo(D::IMAGE));
810
811 add(ConfigMapper(KEY_LATENCY, C2_PARAMKEY_PIPELINE_DELAY_REQUEST, "value")
812 .limitTo(D::VIDEO & D::ENCODER));
813
814 add(ConfigMapper(C2_PARAMKEY_INPUT_TIME_STRETCH, C2_PARAMKEY_INPUT_TIME_STRETCH, "value"));
815
816 /* still to do
817 constexpr char KEY_PUSH_BLANK_BUFFERS_ON_STOP[] = "push-blank-buffers-on-shutdown";
818
819 not yet used by MediaCodec, but defined as MediaFormat
820 KEY_AUDIO_SESSION_ID // we use "audio-hw-sync"
821 KEY_OUTPUT_REORDER_DEPTH
822 */
823 }
824
initialize(const std::shared_ptr<Codec2Client> & client,const std::shared_ptr<Codec2Client::Component> & component)825 status_t CCodecConfig::initialize(
826 const std::shared_ptr<Codec2Client> &client,
827 const std::shared_ptr<Codec2Client::Component> &component) {
828 C2ComponentDomainSetting domain(C2Component::DOMAIN_OTHER);
829 C2ComponentKindSetting kind(C2Component::KIND_OTHER);
830
831 std::vector<std::unique_ptr<C2Param>> queried;
832 c2_status_t c2err = component->query({ &domain, &kind }, {}, C2_DONT_BLOCK, &queried);
833 if (c2err != C2_OK) {
834 ALOGD("Query domain & kind failed => %s", asString(c2err));
835 // TEMP: determine kind from component name
836 if (kind.value == C2Component::KIND_OTHER) {
837 if (component->getName().find("encoder") != std::string::npos) {
838 kind.value = C2Component::KIND_ENCODER;
839 } else if (component->getName().find("decoder") != std::string::npos) {
840 kind.value = C2Component::KIND_DECODER;
841 }
842 }
843
844 // TEMP: determine domain from media type (port (preferred) or stream #0)
845 if (domain.value == C2Component::DOMAIN_OTHER) {
846 AString mediaType = QueryMediaType(true /* input */, component);
847 if (mediaType.startsWith("audio/")) {
848 domain.value = C2Component::DOMAIN_AUDIO;
849 } else if (mediaType.startsWith("video/")) {
850 domain.value = C2Component::DOMAIN_VIDEO;
851 } else if (mediaType.startsWith("image/")) {
852 domain.value = C2Component::DOMAIN_IMAGE;
853 }
854 }
855 }
856
857 mDomain = (domain.value == C2Component::DOMAIN_VIDEO ? Domain::IS_VIDEO :
858 domain.value == C2Component::DOMAIN_IMAGE ? Domain::IS_IMAGE :
859 domain.value == C2Component::DOMAIN_AUDIO ? Domain::IS_AUDIO : Domain::OTHER_DOMAIN)
860 | (kind.value == C2Component::KIND_DECODER ? Domain::IS_DECODER :
861 kind.value == C2Component::KIND_ENCODER ? Domain::IS_ENCODER : Domain::OTHER_KIND);
862
863 mInputDomain = Domain(((mDomain & IS_DECODER) ? IS_CODED : IS_RAW) | IS_INPUT);
864 mOutputDomain = Domain(((mDomain & IS_ENCODER) ? IS_CODED : IS_RAW) | IS_OUTPUT);
865
866 ALOGV("domain is %#x (%u %u)", mDomain, domain.value, kind.value);
867
868 std::vector<C2Param::Index> paramIndices;
869 switch (kind.value) {
870 case C2Component::KIND_DECODER:
871 mCodingMediaType = QueryMediaType(true /* input */, component).c_str();
872 break;
873 case C2Component::KIND_ENCODER:
874 mCodingMediaType = QueryMediaType(false /* input */, component).c_str();
875 break;
876 default:
877 mCodingMediaType = "";
878 }
879
880 c2err = component->querySupportedParams(&mParamDescs);
881 if (c2err != C2_OK) {
882 ALOGD("Query supported params failed after returning %zu values => %s",
883 mParamDescs.size(), asString(c2err));
884 return UNKNOWN_ERROR;
885 }
886 for (const std::shared_ptr<C2ParamDescriptor> &desc : mParamDescs) {
887 mSupportedIndices.emplace(desc->index());
888 }
889
890 mReflector = client->getParamReflector();
891 if (mReflector == nullptr) {
892 ALOGE("Failed to get param reflector");
893 return UNKNOWN_ERROR;
894 }
895
896 // enumerate all fields
897 mParamUpdater = std::make_shared<ReflectedParamUpdater>();
898 mParamUpdater->clear();
899 mParamUpdater->supportWholeParam(
900 C2_PARAMKEY_TEMPORAL_LAYERING, C2StreamTemporalLayeringTuning::CORE_INDEX);
901 mParamUpdater->addParamDesc(mReflector, mParamDescs);
902
903 // TEMP: add some standard fields even if not reflected
904 if (kind.value == C2Component::KIND_ENCODER) {
905 mParamUpdater->addStandardParam<C2StreamInitDataInfo::output>(C2_PARAMKEY_INIT_DATA);
906 }
907 if (domain.value == C2Component::DOMAIN_IMAGE || domain.value == C2Component::DOMAIN_VIDEO) {
908 if (kind.value != C2Component::KIND_ENCODER) {
909 addLocalParam<C2StreamPictureSizeInfo::output>(C2_PARAMKEY_PICTURE_SIZE);
910 addLocalParam<C2StreamCropRectInfo::output>(C2_PARAMKEY_CROP_RECT);
911 addLocalParam(
912 new C2StreamPixelAspectRatioInfo::output(0u, 1u, 1u),
913 C2_PARAMKEY_PIXEL_ASPECT_RATIO);
914 addLocalParam(new C2StreamRotationInfo::output(0u, 0), C2_PARAMKEY_ROTATION);
915 addLocalParam(new C2StreamColorAspectsInfo::output(0u), C2_PARAMKEY_COLOR_ASPECTS);
916 addLocalParam<C2StreamDataSpaceInfo::output>(C2_PARAMKEY_DATA_SPACE);
917 addLocalParam<C2StreamHdrStaticInfo::output>(C2_PARAMKEY_HDR_STATIC_INFO);
918 addLocalParam(new C2StreamSurfaceScalingInfo::output(0u, VIDEO_SCALING_MODE_SCALE_TO_FIT),
919 C2_PARAMKEY_SURFACE_SCALING_MODE);
920 } else {
921 addLocalParam(new C2StreamColorAspectsInfo::input(0u), C2_PARAMKEY_COLOR_ASPECTS);
922 }
923 }
924
925 initializeStandardParams();
926
927 // subscribe to all supported standard (exposed) params
928 // TODO: limit this to params that are actually in the domain
929 std::vector<std::string> formatKeys = mStandardParams->getPathsForDomain(Domain(1 << 30));
930 std::vector<C2Param::Index> indices;
931 mParamUpdater->getParamIndicesForKeys(formatKeys, &indices);
932 mSubscribedIndices.insert(indices.begin(), indices.end());
933
934 // also subscribe to some non-SDK standard parameters
935 // for number of input/output buffers
936 mSubscribedIndices.emplace(C2PortSuggestedBufferCountTuning::input::PARAM_TYPE);
937 mSubscribedIndices.emplace(C2PortSuggestedBufferCountTuning::output::PARAM_TYPE);
938 mSubscribedIndices.emplace(C2ActualPipelineDelayTuning::PARAM_TYPE);
939 mSubscribedIndices.emplace(C2PortActualDelayTuning::input::PARAM_TYPE);
940 mSubscribedIndices.emplace(C2PortActualDelayTuning::output::PARAM_TYPE);
941 // for output buffer array allocation
942 mSubscribedIndices.emplace(C2StreamMaxBufferSizeInfo::output::PARAM_TYPE);
943 // init data (CSD)
944 mSubscribedIndices.emplace(C2StreamInitDataInfo::output::PARAM_TYPE);
945
946 return OK;
947 }
948
subscribeToConfigUpdate(const std::shared_ptr<Codec2Client::Component> & component,const std::vector<C2Param::Index> & indices,c2_blocking_t blocking)949 status_t CCodecConfig::subscribeToConfigUpdate(
950 const std::shared_ptr<Codec2Client::Component> &component,
951 const std::vector<C2Param::Index> &indices,
952 c2_blocking_t blocking) {
953 mSubscribedIndices.insert(indices.begin(), indices.end());
954 // TODO: enable this when components no longer crash on this config
955 if (mSubscribedIndices.size() != mSubscribedIndicesSize && false) {
956 std::vector<uint32_t> indices;
957 for (C2Param::Index ix : mSubscribedIndices) {
958 indices.push_back(ix);
959 }
960 std::unique_ptr<C2SubscribedParamIndicesTuning> subscribeTuning =
961 C2SubscribedParamIndicesTuning::AllocUnique(indices);
962 std::vector<std::unique_ptr<C2SettingResult>> results;
963 c2_status_t c2Err = component->config({ subscribeTuning.get() }, blocking, &results);
964 if (c2Err != C2_OK && c2Err != C2_BAD_INDEX) {
965 ALOGD("Failed to subscribe to parameters => %s", asString(c2Err));
966 // TODO: error
967 }
968 ALOGV("Subscribed to %zu params", mSubscribedIndices.size());
969 mSubscribedIndicesSize = mSubscribedIndices.size();
970 }
971 return OK;
972 }
973
queryConfiguration(const std::shared_ptr<Codec2Client::Component> & component)974 status_t CCodecConfig::queryConfiguration(
975 const std::shared_ptr<Codec2Client::Component> &component) {
976 // query all subscribed parameters
977 std::vector<C2Param::Index> indices(mSubscribedIndices.begin(), mSubscribedIndices.end());
978 std::vector<std::unique_ptr<C2Param>> queried;
979 c2_status_t c2Err = component->query({}, indices, C2_MAY_BLOCK, &queried);
980 if (c2Err != OK) {
981 ALOGI("query failed after returning %zu values (%s)", queried.size(), asString(c2Err));
982 // TODO: error
983 }
984
985 updateConfiguration(queried, ALL);
986 return OK;
987 }
988
updateConfiguration(std::vector<std::unique_ptr<C2Param>> & configUpdate,Domain domain)989 bool CCodecConfig::updateConfiguration(
990 std::vector<std::unique_ptr<C2Param>> &configUpdate, Domain domain) {
991 ALOGV("updating configuration with %zu params", configUpdate.size());
992 bool changed = false;
993 for (std::unique_ptr<C2Param> &p : configUpdate) {
994 if (p && *p) {
995 auto insertion = mCurrentConfig.emplace(p->index(), nullptr);
996 if (insertion.second || *insertion.first->second != *p) {
997 if (mSupportedIndices.count(p->index()) || mLocalParams.count(p->index())) {
998 // only track changes in supported (reflected or local) indices
999 changed = true;
1000 } else {
1001 ALOGV("an unlisted config was %s: %#x",
1002 insertion.second ? "added" : "updated", p->index());
1003 }
1004 }
1005 insertion.first->second = std::move(p);
1006 }
1007 }
1008
1009 ALOGV("updated configuration has %zu params (%s)", mCurrentConfig.size(),
1010 changed ? "CHANGED" : "no change");
1011 if (changed) {
1012 return updateFormats(domain);
1013 }
1014 return false;
1015 }
1016
updateFormats(Domain domain)1017 bool CCodecConfig::updateFormats(Domain domain) {
1018 // get addresses of params in the current config
1019 std::vector<C2Param*> paramPointers;
1020 for (const auto &it : mCurrentConfig) {
1021 paramPointers.push_back(it.second.get());
1022 }
1023
1024 ReflectedParamUpdater::Dict reflected = mParamUpdater->getParams(paramPointers);
1025 ALOGD("c2 config is %s", reflected.debugString().c_str());
1026
1027 bool changed = false;
1028 if (domain & mInputDomain) {
1029 sp<AMessage> oldFormat = mInputFormat;
1030 mInputFormat = mInputFormat->dup(); // trigger format changed
1031 mInputFormat->extend(getSdkFormatForDomain(reflected, mInputDomain));
1032 if (mInputFormat->countEntries() != oldFormat->countEntries()
1033 || mInputFormat->changesFrom(oldFormat)->countEntries() > 0) {
1034 changed = true;
1035 } else {
1036 mInputFormat = oldFormat; // no change
1037 }
1038 }
1039 if (domain & mOutputDomain) {
1040 sp<AMessage> oldFormat = mOutputFormat;
1041 mOutputFormat = mOutputFormat->dup(); // trigger output format changed
1042 mOutputFormat->extend(getSdkFormatForDomain(reflected, mOutputDomain));
1043 if (mOutputFormat->countEntries() != oldFormat->countEntries()
1044 || mOutputFormat->changesFrom(oldFormat)->countEntries() > 0) {
1045 changed = true;
1046 } else {
1047 mOutputFormat = oldFormat; // no change
1048 }
1049 }
1050 ALOGV_IF(changed, "format(s) changed");
1051 return changed;
1052 }
1053
getSdkFormatForDomain(const ReflectedParamUpdater::Dict & reflected,Domain portDomain) const1054 sp<AMessage> CCodecConfig::getSdkFormatForDomain(
1055 const ReflectedParamUpdater::Dict &reflected, Domain portDomain) const {
1056 sp<AMessage> msg = new AMessage;
1057 for (const std::pair<std::string, std::vector<ConfigMapper>> &el : mStandardParams->getKeys()) {
1058 for (const ConfigMapper &cm : el.second) {
1059 if ((cm.domain() & portDomain) == 0 // input-output-coded-raw
1060 || (cm.domain() & mDomain) != mDomain // component domain + kind (these must match)
1061 || (cm.domain() & IS_READ) == 0) {
1062 continue;
1063 }
1064 auto it = reflected.find(cm.path());
1065 if (it == reflected.end()) {
1066 continue;
1067 }
1068 C2Value c2Value;
1069 sp<ABuffer> bufValue;
1070 AString strValue;
1071 AMessage::ItemData item;
1072 if (it->second.find(&c2Value)) {
1073 item = cm.mapToMessage(c2Value);
1074 } else if (it->second.find(&bufValue)) {
1075 item.set(bufValue);
1076 } else if (it->second.find(&strValue)) {
1077 item.set(strValue);
1078 } else {
1079 ALOGD("unexpected untyped query value for key: %s", cm.path().c_str());
1080 continue;
1081 }
1082 msg->setItem(el.first.c_str(), item);
1083 }
1084 }
1085
1086 { // convert from Codec 2.0 rect to MediaFormat rect and add crop rect if not present
1087 int32_t left, top, width, height;
1088 if (msg->findInt32("crop-left", &left) && msg->findInt32("crop-width", &width)
1089 && msg->findInt32("crop-top", &top) && msg->findInt32("crop-height", &height)
1090 && left >= 0 && width >=0 && width <= INT32_MAX - left
1091 && top >= 0 && height >=0 && height <= INT32_MAX - top) {
1092 msg->removeEntryAt(msg->findEntryByName("crop-left"));
1093 msg->removeEntryAt(msg->findEntryByName("crop-top"));
1094 msg->removeEntryAt(msg->findEntryByName("crop-width"));
1095 msg->removeEntryAt(msg->findEntryByName("crop-height"));
1096 msg->setRect("crop", left, top, left + width - 1, top + height - 1);
1097 } else if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) {
1098 msg->setRect("crop", 0, 0, width - 1, height - 1);
1099 }
1100 }
1101
1102 { // convert temporal layering to schema
1103 sp<ABuffer> tmp;
1104 if (msg->findBuffer(C2_PARAMKEY_TEMPORAL_LAYERING, &tmp) && tmp != nullptr) {
1105 C2StreamTemporalLayeringTuning *layering =
1106 C2StreamTemporalLayeringTuning::From(C2Param::From(tmp->data(), tmp->size()));
1107 if (layering && layering->m.layerCount > 0
1108 && layering->m.bLayerCount < layering->m.layerCount) {
1109 // check if this is webrtc compatible
1110 AString mime;
1111 if (msg->findString(KEY_MIME, &mime) &&
1112 mime.equalsIgnoreCase(MIMETYPE_VIDEO_VP8) &&
1113 layering->m.bLayerCount == 0 &&
1114 (layering->m.layerCount == 1
1115 || (layering->m.layerCount == 2
1116 && layering->flexCount() >= 1
1117 && layering->m.bitrateRatios[0] == .6f)
1118 || (layering->m.layerCount == 3
1119 && layering->flexCount() >= 2
1120 && layering->m.bitrateRatios[0] == .4f
1121 && layering->m.bitrateRatios[1] == .6f)
1122 || (layering->m.layerCount == 4
1123 && layering->flexCount() >= 3
1124 && layering->m.bitrateRatios[0] == .25f
1125 && layering->m.bitrateRatios[1] == .4f
1126 && layering->m.bitrateRatios[2] == .6f))) {
1127 msg->setString(KEY_TEMPORAL_LAYERING, AStringPrintf(
1128 "webrtc.vp8.%u-layer", layering->m.layerCount));
1129 } else if (layering->m.bLayerCount) {
1130 msg->setString(KEY_TEMPORAL_LAYERING, AStringPrintf(
1131 "android.generic.%u+%u",
1132 layering->m.layerCount - layering->m.bLayerCount,
1133 layering->m.bLayerCount));
1134 } else if (layering->m.bLayerCount) {
1135 msg->setString(KEY_TEMPORAL_LAYERING, AStringPrintf(
1136 "android.generic.%u", layering->m.layerCount));
1137 }
1138 }
1139 msg->removeEntryAt(msg->findEntryByName(C2_PARAMKEY_TEMPORAL_LAYERING));
1140 }
1141 }
1142
1143 { // convert color info
1144 C2Color::primaries_t primaries;
1145 C2Color::matrix_t matrix;
1146 if (msg->findInt32("color-primaries", (int32_t*)&primaries)
1147 && msg->findInt32("color-matrix", (int32_t*)&matrix)) {
1148 int32_t standard;
1149
1150 if (C2Mapper::map(primaries, matrix, &standard)) {
1151 msg->setInt32(KEY_COLOR_STANDARD, standard);
1152 }
1153
1154 msg->removeEntryAt(msg->findEntryByName("color-primaries"));
1155 msg->removeEntryAt(msg->findEntryByName("color-matrix"));
1156 }
1157
1158
1159 // calculate dataspace for raw graphic buffers if not specified by component, or if
1160 // using surface with unspecified aspects (as those must be defaulted which may change
1161 // the dataspace)
1162 if ((portDomain & IS_RAW) && (mDomain & (IS_IMAGE | IS_VIDEO))) {
1163 android_dataspace dataspace;
1164 ColorAspects aspects = {
1165 ColorAspects::RangeUnspecified, ColorAspects::PrimariesUnspecified,
1166 ColorAspects::TransferUnspecified, ColorAspects::MatrixUnspecified
1167 };
1168 ColorUtils::getColorAspectsFromFormat(msg, aspects);
1169 ColorAspects origAspects = aspects;
1170 if (mUsingSurface) {
1171 // get image size (default to HD)
1172 int32_t width = 1280;
1173 int32_t height = 720;
1174 int32_t left, top, right, bottom;
1175 if (msg->findRect("crop", &left, &top, &right, &bottom)) {
1176 width = right - left + 1;
1177 height = bottom - top + 1;
1178 } else {
1179 (void)msg->findInt32(KEY_WIDTH, &width);
1180 (void)msg->findInt32(KEY_HEIGHT, &height);
1181 }
1182 ColorUtils::setDefaultCodecColorAspectsIfNeeded(aspects, width, height);
1183 ColorUtils::setColorAspectsIntoFormat(aspects, msg);
1184 }
1185
1186 if (!msg->findInt32("android._dataspace", (int32_t*)&dataspace)
1187 || aspects.mRange != origAspects.mRange
1188 || aspects.mPrimaries != origAspects.mPrimaries
1189 || aspects.mTransfer != origAspects.mTransfer
1190 || aspects.mMatrixCoeffs != origAspects.mMatrixCoeffs) {
1191 dataspace = ColorUtils::getDataSpaceForColorAspects(aspects, true /* mayExpand */);
1192 msg->setInt32("android._dataspace", dataspace);
1193 }
1194 }
1195
1196 // HDR static info
1197
1198 C2HdrStaticMetadataStruct hdr;
1199 if (msg->findFloat("smpte2086.red.x", &hdr.mastering.red.x)
1200 && msg->findFloat("smpte2086.red.y", &hdr.mastering.red.y)
1201 && msg->findFloat("smpte2086.green.x", &hdr.mastering.green.x)
1202 && msg->findFloat("smpte2086.green.y", &hdr.mastering.green.y)
1203 && msg->findFloat("smpte2086.blue.x", &hdr.mastering.blue.x)
1204 && msg->findFloat("smpte2086.blue.y", &hdr.mastering.blue.y)
1205 && msg->findFloat("smpte2086.white.x", &hdr.mastering.white.x)
1206 && msg->findFloat("smpte2086.white.y", &hdr.mastering.white.y)
1207 && msg->findFloat("smpte2086.max-luminance", &hdr.mastering.maxLuminance)
1208 && msg->findFloat("smpte2086.min-luminance", &hdr.mastering.minLuminance)
1209 && msg->findFloat("cta861.max-cll", &hdr.maxCll)
1210 && msg->findFloat("cta861.max-fall", &hdr.maxFall)) {
1211 if (hdr.mastering.red.x >= 0 && hdr.mastering.red.x <= 1
1212 && hdr.mastering.red.y >= 0 && hdr.mastering.red.y <= 1
1213 && hdr.mastering.green.x >= 0 && hdr.mastering.green.x <= 1
1214 && hdr.mastering.green.y >= 0 && hdr.mastering.green.y <= 1
1215 && hdr.mastering.blue.x >= 0 && hdr.mastering.blue.x <= 1
1216 && hdr.mastering.blue.y >= 0 && hdr.mastering.blue.y <= 1
1217 && hdr.mastering.white.x >= 0 && hdr.mastering.white.x <= 1
1218 && hdr.mastering.white.y >= 0 && hdr.mastering.white.y <= 1
1219 && hdr.mastering.maxLuminance >= 0 && hdr.mastering.maxLuminance <= 65535
1220 && hdr.mastering.minLuminance >= 0 && hdr.mastering.minLuminance <= 6.5535
1221 && hdr.maxCll >= 0 && hdr.maxCll <= 65535
1222 && hdr.maxFall >= 0 && hdr.maxFall <= 65535) {
1223 HDRStaticInfo meta;
1224 meta.mID = meta.kType1;
1225 meta.sType1.mR.x = hdr.mastering.red.x / 0.00002 + 0.5;
1226 meta.sType1.mR.y = hdr.mastering.red.y / 0.00002 + 0.5;
1227 meta.sType1.mG.x = hdr.mastering.green.x / 0.00002 + 0.5;
1228 meta.sType1.mG.y = hdr.mastering.green.y / 0.00002 + 0.5;
1229 meta.sType1.mB.x = hdr.mastering.blue.x / 0.00002 + 0.5;
1230 meta.sType1.mB.y = hdr.mastering.blue.y / 0.00002 + 0.5;
1231 meta.sType1.mW.x = hdr.mastering.white.x / 0.00002 + 0.5;
1232 meta.sType1.mW.y = hdr.mastering.white.y / 0.00002 + 0.5;
1233 meta.sType1.mMaxDisplayLuminance = hdr.mastering.maxLuminance + 0.5;
1234 meta.sType1.mMinDisplayLuminance = hdr.mastering.minLuminance / 0.0001 + 0.5;
1235 meta.sType1.mMaxContentLightLevel = hdr.maxCll + 0.5;
1236 meta.sType1.mMaxFrameAverageLightLevel = hdr.maxFall + 0.5;
1237 msg->removeEntryAt(msg->findEntryByName("smpte2086.red.x"));
1238 msg->removeEntryAt(msg->findEntryByName("smpte2086.red.y"));
1239 msg->removeEntryAt(msg->findEntryByName("smpte2086.green.x"));
1240 msg->removeEntryAt(msg->findEntryByName("smpte2086.green.y"));
1241 msg->removeEntryAt(msg->findEntryByName("smpte2086.blue.x"));
1242 msg->removeEntryAt(msg->findEntryByName("smpte2086.blue.y"));
1243 msg->removeEntryAt(msg->findEntryByName("smpte2086.white.x"));
1244 msg->removeEntryAt(msg->findEntryByName("smpte2086.white.y"));
1245 msg->removeEntryAt(msg->findEntryByName("smpte2086.max-luminance"));
1246 msg->removeEntryAt(msg->findEntryByName("smpte2086.min-luminance"));
1247 msg->removeEntryAt(msg->findEntryByName("cta861.max-cll"));
1248 msg->removeEntryAt(msg->findEntryByName("cta861.max-fall"));
1249 msg->setBuffer(KEY_HDR_STATIC_INFO, ABuffer::CreateAsCopy(&meta, sizeof(meta)));
1250 } else {
1251 ALOGD("found invalid HDR static metadata %s", msg->debugString(8).c_str());
1252 }
1253 }
1254 }
1255
1256 ALOGV("converted to SDK values as %s", msg->debugString().c_str());
1257 return msg;
1258 }
1259
1260 /// converts an AMessage value to a ParamUpdater value
convert(const AMessage::ItemData & from,ReflectedParamUpdater::Value * to)1261 static void convert(const AMessage::ItemData &from, ReflectedParamUpdater::Value *to) {
1262 int32_t int32Value;
1263 int64_t int64Value;
1264 sp<ABuffer> bufValue;
1265 AString strValue;
1266 float floatValue;
1267 double doubleValue;
1268
1269 if (from.find(&int32Value)) {
1270 to->set(int32Value);
1271 } else if (from.find(&int64Value)) {
1272 to->set(int64Value);
1273 } else if (from.find(&bufValue)) {
1274 to->set(bufValue);
1275 } else if (from.find(&strValue)) {
1276 to->set(strValue);
1277 } else if (from.find(&floatValue)) {
1278 to->set(C2Value(floatValue));
1279 } else if (from.find(&doubleValue)) {
1280 // convert double to float
1281 to->set(C2Value((float)doubleValue));
1282 }
1283 // ignore all other AMessage types
1284 }
1285
1286 /// relaxes Codec 2.0 specific value types to SDK types (mainly removes signedness and counterness
1287 /// from 32/64-bit values.)
relaxValues(ReflectedParamUpdater::Value & item)1288 static void relaxValues(ReflectedParamUpdater::Value &item) {
1289 C2Value c2Value;
1290 int32_t int32Value;
1291 int64_t int64Value;
1292 (void)item.find(&c2Value);
1293 if (c2Value.get(&int32Value) || c2Value.get((uint32_t*)&int32Value)
1294 || c2Value.get((c2_cntr32_t*)&int32Value)) {
1295 item.set(int32Value);
1296 } else if (c2Value.get(&int64Value)
1297 || c2Value.get((uint64_t*)&int64Value)
1298 || c2Value.get((c2_cntr64_t*)&int64Value)) {
1299 item.set(int64Value);
1300 }
1301 }
1302
getReflectedFormat(const sp<AMessage> & params_,Domain configDomain) const1303 ReflectedParamUpdater::Dict CCodecConfig::getReflectedFormat(
1304 const sp<AMessage> ¶ms_, Domain configDomain) const {
1305 // create a modifiable copy of params
1306 sp<AMessage> params = params_->dup();
1307 ALOGV("filtering with config domain %x", configDomain);
1308
1309 // convert some macro parameters to Codec 2.0 specific expressions
1310
1311 { // make i-frame-interval frame based
1312 float iFrameInterval;
1313 if (params->findAsFloat(KEY_I_FRAME_INTERVAL, &iFrameInterval)) {
1314 float frameRate;
1315 if (params->findAsFloat(KEY_FRAME_RATE, &frameRate)) {
1316 params->setInt32("i-frame-period",
1317 (frameRate <= 0 || iFrameInterval < 0)
1318 ? -1 /* no sync frames */
1319 : (int32_t)c2_min(iFrameInterval * frameRate + 0.5,
1320 (float)INT32_MAX));
1321 }
1322 }
1323 }
1324
1325 if (mDomain == (IS_VIDEO | IS_ENCODER)) {
1326 // convert capture-rate into input-time-stretch
1327 float frameRate, captureRate;
1328 if (params->findAsFloat(KEY_FRAME_RATE, &frameRate)) {
1329 if (!params->findAsFloat("time-lapse-fps", &captureRate)
1330 && !params->findAsFloat(KEY_CAPTURE_RATE, &captureRate)) {
1331 captureRate = frameRate;
1332 }
1333 if (captureRate > 0 && frameRate > 0) {
1334 params->setFloat(C2_PARAMKEY_INPUT_TIME_STRETCH, captureRate / frameRate);
1335 }
1336 }
1337 }
1338
1339 { // reflect temporal layering into a binary blob
1340 AString schema;
1341 if (params->findString(KEY_TEMPORAL_LAYERING, &schema)) {
1342 unsigned int numLayers = 0;
1343 unsigned int numBLayers = 0;
1344 int tags;
1345 char dummy;
1346 std::unique_ptr<C2StreamTemporalLayeringTuning::output> layering;
1347 if (sscanf(schema.c_str(), "webrtc.vp8.%u-layer%c", &numLayers, &dummy) == 1
1348 && numLayers > 0) {
1349 switch (numLayers) {
1350 case 1:
1351 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1352 {}, 0u, 1u, 0u);
1353 break;
1354 case 2:
1355 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1356 { .6f }, 0u, 2u, 0u);
1357 break;
1358 case 3:
1359 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1360 { .4f, .6f }, 0u, 3u, 0u);
1361 break;
1362 default:
1363 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1364 { .25f, .4f, .6f }, 0u, 4u, 0u);
1365 break;
1366 }
1367 } else if ((tags = sscanf(schema.c_str(), "android.generic.%u%c%u%c",
1368 &numLayers, &dummy, &numBLayers, &dummy))
1369 && (tags == 1 || (tags == 3 && dummy == '+'))
1370 && numLayers > 0 && numLayers < UINT32_MAX - numBLayers) {
1371 layering = C2StreamTemporalLayeringTuning::output::AllocUnique(
1372 {}, 0u, numLayers + numBLayers, numBLayers);
1373 } else {
1374 ALOGD("Ignoring unsupported ts-schema [%s]", schema.c_str());
1375 }
1376 if (layering) {
1377 params->setBuffer(C2_PARAMKEY_TEMPORAL_LAYERING,
1378 ABuffer::CreateAsCopy(layering.get(), layering->size()));
1379 }
1380 }
1381 }
1382
1383 { // convert from MediaFormat rect to Codec 2.0 rect
1384 int32_t offset;
1385 int32_t end;
1386 AMessage::ItemData item;
1387 if (params->findInt32("crop-left", &offset) && params->findInt32("crop-right", &end)
1388 && offset >= 0 && end >= offset - 1) {
1389 size_t ix = params->findEntryByName("crop-right");
1390 params->setEntryNameAt(ix, "crop-width");
1391 item.set(end - offset + 1);
1392 params->setEntryAt(ix, item);
1393 }
1394 if (params->findInt32("crop-top", &offset) && params->findInt32("crop-bottom", &end)
1395 && offset >= 0 && end >= offset - 1) {
1396 size_t ix = params->findEntryByName("crop-bottom");
1397 params->setEntryNameAt(ix, "crop-height");
1398 item.set(end - offset + 1);
1399 params->setEntryAt(ix, item);
1400 }
1401 }
1402
1403 { // convert color info
1404 int32_t standard;
1405 if (params->findInt32(KEY_COLOR_STANDARD, &standard)) {
1406 C2Color::primaries_t primaries;
1407 C2Color::matrix_t matrix;
1408
1409 if (C2Mapper::map(standard, &primaries, &matrix)) {
1410 params->setInt32("color-primaries", primaries);
1411 params->setInt32("color-matrix", matrix);
1412 }
1413 }
1414
1415 sp<ABuffer> hdrMeta;
1416 if (params->findBuffer(KEY_HDR_STATIC_INFO, &hdrMeta)
1417 && hdrMeta->size() == sizeof(HDRStaticInfo)) {
1418 HDRStaticInfo *meta = (HDRStaticInfo*)hdrMeta->data();
1419 if (meta->mID == meta->kType1) {
1420 params->setFloat("smpte2086.red.x", meta->sType1.mR.x * 0.00002);
1421 params->setFloat("smpte2086.red.y", meta->sType1.mR.y * 0.00002);
1422 params->setFloat("smpte2086.green.x", meta->sType1.mG.x * 0.00002);
1423 params->setFloat("smpte2086.green.y", meta->sType1.mG.y * 0.00002);
1424 params->setFloat("smpte2086.blue.x", meta->sType1.mB.x * 0.00002);
1425 params->setFloat("smpte2086.blue.y", meta->sType1.mB.y * 0.00002);
1426 params->setFloat("smpte2086.white.x", meta->sType1.mW.x * 0.00002);
1427 params->setFloat("smpte2086.white.y", meta->sType1.mW.y * 0.00002);
1428 params->setFloat("smpte2086.max-luminance", meta->sType1.mMaxDisplayLuminance);
1429 params->setFloat("smpte2086.min-luminance", meta->sType1.mMinDisplayLuminance * 0.0001);
1430 params->setFloat("cta861.max-cll", meta->sType1.mMaxContentLightLevel);
1431 params->setFloat("cta861.max-fall", meta->sType1.mMaxFrameAverageLightLevel);
1432 }
1433 }
1434 }
1435
1436 // this is to verify that we set proper signedness for standard parameters
1437 bool beVeryStrict = property_get_bool("debug.stagefright.ccodec_strict_type", false);
1438 // this is to allow vendors to use the wrong signedness for standard parameters
1439 bool beVeryLax = property_get_bool("debug.stagefright.ccodec_lax_type", false);
1440
1441 ReflectedParamUpdater::Dict filtered;
1442 for (size_t ix = 0; ix < params->countEntries(); ++ix) {
1443 AMessage::Type type;
1444 AString name = params->getEntryNameAt(ix, &type);
1445 AMessage::ItemData msgItem = params->getEntryAt(ix);
1446 ReflectedParamUpdater::Value item;
1447 convert(msgItem, &item); // convert item to param updater item
1448
1449 if (name.startsWith("vendor.")) {
1450 // vendor params pass through as is
1451 filtered.emplace(name.c_str(), item);
1452 continue;
1453 }
1454 // standard parameters may get modified, filtered or duplicated
1455 for (const ConfigMapper &cm : mStandardParams->getConfigMappersForSdkKey(name.c_str())) {
1456 // note: we ignore port domain for configuration
1457 if ((cm.domain() & configDomain)
1458 // component domain + kind (these must match)
1459 && (cm.domain() & mDomain) == mDomain) {
1460 // map arithmetic values, pass through string or buffer
1461 switch (type) {
1462 case AMessage::kTypeBuffer:
1463 case AMessage::kTypeString:
1464 break;
1465 case AMessage::kTypeInt32:
1466 case AMessage::kTypeInt64:
1467 case AMessage::kTypeFloat:
1468 case AMessage::kTypeDouble:
1469 // for now only map settings with mappers as we are not creating
1470 // signed <=> unsigned mappers
1471 // TODO: be precise about signed unsigned
1472 if (beVeryStrict || cm.mapper()) {
1473 item.set(cm.mapFromMessage(params->getEntryAt(ix)));
1474 // also allow to relax type strictness
1475 if (beVeryLax) {
1476 relaxValues(item);
1477 }
1478 }
1479 break;
1480 default:
1481 continue;
1482 }
1483 filtered.emplace(cm.path(), item);
1484 }
1485 }
1486 }
1487 ALOGV("filtered %s to %s", params->debugString(4).c_str(),
1488 filtered.debugString(4).c_str());
1489 return filtered;
1490 }
1491
getConfigUpdateFromSdkParams(std::shared_ptr<Codec2Client::Component> component,const sp<AMessage> & sdkParams,Domain configDomain,c2_blocking_t blocking,std::vector<std::unique_ptr<C2Param>> * configUpdate) const1492 status_t CCodecConfig::getConfigUpdateFromSdkParams(
1493 std::shared_ptr<Codec2Client::Component> component,
1494 const sp<AMessage> &sdkParams, Domain configDomain,
1495 c2_blocking_t blocking,
1496 std::vector<std::unique_ptr<C2Param>> *configUpdate) const {
1497 ReflectedParamUpdater::Dict params = getReflectedFormat(sdkParams, configDomain);
1498
1499 std::vector<C2Param::Index> indices;
1500 mParamUpdater->getParamIndicesFromMessage(params, &indices);
1501 if (indices.empty()) {
1502 ALOGD("no recognized params in: %s", params.debugString().c_str());
1503 return OK;
1504 }
1505
1506 configUpdate->clear();
1507 std::vector<C2Param::Index> supportedIndices;
1508 for (C2Param::Index ix : indices) {
1509 if (mSupportedIndices.count(ix)) {
1510 supportedIndices.push_back(ix);
1511 } else if (mLocalParams.count(ix)) {
1512 // query local parameter here
1513 auto it = mCurrentConfig.find(ix);
1514 if (it != mCurrentConfig.end()) {
1515 configUpdate->emplace_back(C2Param::Copy(*it->second));
1516 }
1517 }
1518 }
1519
1520 c2_status_t err = component->query({ }, supportedIndices, blocking, configUpdate);
1521 if (err != C2_OK) {
1522 ALOGD("query failed after returning %zu params => %s", configUpdate->size(), asString(err));
1523 }
1524
1525 if (configUpdate->size()) {
1526 mParamUpdater->updateParamsFromMessage(params, configUpdate);
1527 }
1528 return OK;
1529 }
1530
setParameters(std::shared_ptr<Codec2Client::Component> component,std::vector<std::unique_ptr<C2Param>> & configUpdate,c2_blocking_t blocking)1531 status_t CCodecConfig::setParameters(
1532 std::shared_ptr<Codec2Client::Component> component,
1533 std::vector<std::unique_ptr<C2Param>> &configUpdate,
1534 c2_blocking_t blocking) {
1535 status_t result = OK;
1536 if (configUpdate.empty()) {
1537 return OK;
1538 }
1539
1540 std::vector<C2Param::Index> indices;
1541 std::vector<C2Param *> paramVector;
1542 for (const std::unique_ptr<C2Param> ¶m : configUpdate) {
1543 if (mSupportedIndices.count(param->index())) {
1544 // component parameter
1545 paramVector.push_back(param.get());
1546 indices.push_back(param->index());
1547 } else if (mLocalParams.count(param->index())) {
1548 // handle local parameter here
1549 LocalParamValidator validator = mLocalParams.find(param->index())->second;
1550 c2_status_t err = C2_OK;
1551 std::unique_ptr<C2Param> copy = C2Param::Copy(*param);
1552 if (validator) {
1553 err = validator(copy);
1554 }
1555 if (err == C2_OK) {
1556 ALOGV("updated local parameter value for %s",
1557 mParamUpdater->getParamName(param->index()).c_str());
1558
1559 mCurrentConfig[param->index()] = std::move(copy);
1560 } else {
1561 ALOGD("failed to set parameter value for %s => %s",
1562 mParamUpdater->getParamName(param->index()).c_str(), asString(err));
1563 result = BAD_VALUE;
1564 }
1565 }
1566 }
1567 // update subscribed param indices
1568 subscribeToConfigUpdate(component, indices, blocking);
1569
1570 std::vector<std::unique_ptr<C2SettingResult>> failures;
1571 c2_status_t err = component->config(paramVector, blocking, &failures);
1572 if (err != C2_OK) {
1573 ALOGD("config failed => %s", asString(err));
1574 // This is non-fatal.
1575 }
1576 for (const std::unique_ptr<C2SettingResult> &failure : failures) {
1577 switch (failure->failure) {
1578 case C2SettingResult::BAD_VALUE:
1579 ALOGD("Bad parameter value");
1580 result = BAD_VALUE;
1581 break;
1582 default:
1583 ALOGV("failure = %d", int(failure->failure));
1584 break;
1585 }
1586 }
1587
1588 // Re-query parameter values in case config could not update them and update the current
1589 // configuration.
1590 configUpdate.clear();
1591 err = component->query({}, indices, blocking, &configUpdate);
1592 if (err != C2_OK) {
1593 ALOGD("query failed after returning %zu params => %s", configUpdate.size(), asString(err));
1594 }
1595 (void)updateConfiguration(configUpdate, ALL);
1596
1597 // TODO: error value
1598 return result;
1599 }
1600
getConfigParameterValue(C2Param::Index index) const1601 const C2Param *CCodecConfig::getConfigParameterValue(C2Param::Index index) const {
1602 auto it = mCurrentConfig.find(index);
1603 if (it == mCurrentConfig.end()) {
1604 return nullptr;
1605 } else {
1606 return it->second.get();
1607 }
1608 }
1609
1610 } // namespace android
1611