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