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 
26 #include <media/stagefright/MediaCodecConstants.h>
27 
28 #include "CCodecConfig.h"
29 #include "Codec2Mapper.h"
30 
31 namespace android {
32 
33 // CCodecConfig
34 
35 namespace {
36 
37 /**
38  * mapping between SDK and Codec 2.0 configurations.
39  */
40 struct ConfigMapper {
41     /**
42      * Value mapper (C2Value => C2Value)
43      */
44     typedef std::function<C2Value(C2Value)> Mapper;
45 
46     /// shorthand
47     typedef CCodecConfig::Domain Domain;
48 
ConfigMapperandroid::__anon4a95bb5c0111::ConfigMapper49     ConfigMapper(std::string mediaKey, C2String c2struct, C2String c2field)
50         : mDomain(Domain::ALL), mMediaKey(mediaKey), mStruct(c2struct), mField(c2field) { }
51 
52     /// Limits this parameter to the given domain
limitToandroid::__anon4a95bb5c0111::ConfigMapper53     ConfigMapper &limitTo(uint32_t domain) {
54         C2_CHECK(domain & Domain::GUARD_BIT);
55         mDomain = Domain(mDomain & domain);
56         return *this;
57     }
58 
59     /// Adds SDK => Codec 2.0 mapper (should not be in the SDK format)
withMapperandroid::__anon4a95bb5c0111::ConfigMapper60     ConfigMapper &withMapper(Mapper mapper) {
61         C2_CHECK(!mMapper);
62         C2_CHECK(!mReverse);
63         mMapper = mapper;
64         return *this;
65     }
66 
67     /// Adds SDK <=> Codec 2.0 value mappers
withMappersandroid::__anon4a95bb5c0111::ConfigMapper68     ConfigMapper &withMappers(Mapper mapper, Mapper reverse) {
69         C2_CHECK(!mMapper);
70         C2_CHECK(!mReverse);
71         mMapper = mapper;
72         mReverse = reverse;
73         return *this;
74     }
75 
76     /// Maps from SDK values in an AMessage to a suitable C2Value.
mapFromMessageandroid::__anon4a95bb5c0111::ConfigMapper77     C2Value mapFromMessage(const AMessage::ItemData &item) const {
78         C2Value value;
79         int32_t int32Value;
80         int64_t int64Value;
81         float floatValue;
82         double doubleValue;
83         if (item.find(&int32Value)) {
84             value = int32Value;
85         } else if (item.find(&int64Value)) {
86             value = int64Value;
87         } else if (item.find(&floatValue)) {
88             value = floatValue;
89         } else if (item.find(&doubleValue)) {
90             value = (float)doubleValue;
91         }
92         if (value.type() != C2Value::NO_INIT && mMapper) {
93             value = mMapper(value);
94         }
95         return value;
96     }
97 
98     /// Maps from a C2Value to an SDK value in an AMessage.
mapToMessageandroid::__anon4a95bb5c0111::ConfigMapper99     AMessage::ItemData mapToMessage(C2Value value) const {
100         AMessage::ItemData item;
101         int32_t int32Value;
102         uint32_t uint32Value;
103         int64_t int64Value;
104         uint64_t uint64Value;
105         float floatValue;
106         if (value.type() != C2Value::NO_INIT && mReverse) {
107             value = mReverse(value);
108         }
109         if (value.get(&int32Value)) {
110             item.set(int32Value);
111         } else if (value.get(&uint32Value) && uint32Value <= uint32_t(INT32_MAX)) {
112             // SDK does not support unsigned values
113             item.set((int32_t)uint32Value);
114         } else if (value.get(&int64Value)) {
115             item.set(int64Value);
116         } else if (value.get(&uint64Value) && uint64Value <= uint64_t(INT64_MAX)) {
117             // SDK does not support unsigned values
118             item.set((int64_t)uint64Value);
119         } else if (value.get(&floatValue)) {
120             item.set(floatValue);
121         }
122         return item;
123     }
124 
domainandroid::__anon4a95bb5c0111::ConfigMapper125     Domain domain() const { return mDomain; }
mediaKeyandroid::__anon4a95bb5c0111::ConfigMapper126     std::string mediaKey() const { return mMediaKey; }
pathandroid::__anon4a95bb5c0111::ConfigMapper127     std::string path() const { return mStruct + '.' + mField; }
mapperandroid::__anon4a95bb5c0111::ConfigMapper128     Mapper mapper() const { return mMapper; }
reverseandroid::__anon4a95bb5c0111::ConfigMapper129     Mapper reverse() const { return mReverse; }
130 
131 private:
132     Domain mDomain;         ///< parameter domain
133     std::string mMediaKey;  ///< SDK key
134     C2String mStruct;       ///< Codec 2.0 struct name
135     C2String mField;        ///< Codec 2.0 field name
136     Mapper mMapper;         ///< optional SDK => Codec 2.0 value mapper
137     Mapper mReverse;        ///< optional Codec 2.0 => SDK value mapper
138 };
139 
140 }
141 
142 /**
143  * Set of standard parameters used by CCodec that are exposed to MediaCodec.
144  */
145 struct StandardParams {
146     typedef CCodecConfig::Domain Domain;
147 
148     // standard (MediaCodec) params are keyed by media format key
149     typedef std::string SdkKey;
150 
151     /// used to return reference to no config mappers in getConfigMappersForSdkKey
152     static const std::vector<ConfigMapper> NO_MAPPERS;
153 
154     /// Returns Codec 2.0 equivalent parameters for an SDK format key.
getConfigMappersForSdkKeyandroid::StandardParams155     const std::vector<ConfigMapper> &getConfigMappersForSdkKey(std::string key) const {
156         auto it = mConfigMappers.find(key);
157         if (it == mConfigMappers.end()) {
158             ALOGD("no c2 equivalents for %s", key.c_str());
159             return NO_MAPPERS;
160         }
161         ALOGV("found %zu eqs for %s", it->second.size(), key.c_str());
162         return it->second;
163     }
164 
165     /**
166      * Adds a SDK <=> Codec 2.0 parameter mapping. Multiple Codec 2.0 parameters may map to a
167      * single SDK key, in which case they shall be ordered from least authoritative to most
168      * authoritative. When constructing SDK formats, the last mapped Codec 2.0 parameter that
169      * is supported by the component will determine the exposed value. (TODO: perhaps restrict this
170      * by domain.)
171      */
addandroid::StandardParams172     void add(const ConfigMapper &cm) {
173         auto it = mConfigMappers.find(cm.mediaKey());
174         ALOGV("%c%c%c%c %04x %9s %s => %s",
175               ((cm.domain() & Domain::IS_INPUT) ? 'I' : ' '),
176               ((cm.domain() & Domain::IS_OUTPUT) ? 'O' : ' '),
177               ((cm.domain() & Domain::IS_CODED) ? 'C' : ' '),
178               ((cm.domain() & Domain::IS_RAW) ? 'R' : ' '),
179               cm.domain(),
180               it == mConfigMappers.end() ? "adding" : "extending",
181               cm.mediaKey().c_str(), cm.path().c_str());
182         if (it == mConfigMappers.end()) {
183             std::vector<ConfigMapper> eqs = { cm };
184             mConfigMappers.emplace(cm.mediaKey(), eqs);
185         } else {
186             it->second.push_back(cm);
187         }
188     }
189 
190     /**
191      * Returns all paths for a specific domain.
192      *
193      * \param any maximum domain mask. Returned parameters must match at least one of the domains
194      *            in the mask.
195      * \param all minimum domain mask. Returned parameters must match all of the domains in the
196      *            mask. This is restricted to the bits of the maximum mask.
197      */
getPathsForDomainandroid::StandardParams198     std::vector<std::string> getPathsForDomain(
199             Domain any, Domain all = Domain::ALL) const {
200         std::vector<std::string> res;
201         for (const std::pair<std::string, std::vector<ConfigMapper>> &el : mConfigMappers) {
202             for (const ConfigMapper &cm : el.second) {
203                 ALOGV("filtering %s %x %x %x %x", cm.path().c_str(), cm.domain(), any,
204                         (cm.domain() & any), (cm.domain() & any & all));
205                 if ((cm.domain() & any) && ((cm.domain() & any & all) == (any & all))) {
206                     res.push_back(cm.path());
207                 }
208             }
209         }
210         return res;
211     }
212 
213     /**
214      * Returns SDK <=> Codec 2.0 mappings.
215      *
216      * TODO: replace these with better methods as this exposes the inner structure.
217      */
getKeysandroid::StandardParams218     const std::map<SdkKey, std::vector<ConfigMapper>> getKeys() const {
219         return mConfigMappers;
220     }
221 
222 private:
223     std::map<SdkKey, std::vector<ConfigMapper>> mConfigMappers;
224 };
225 
226 const std::vector<ConfigMapper> StandardParams::NO_MAPPERS;
227 
228 
CCodecConfig()229 CCodecConfig::CCodecConfig()
230     : mInputFormat(new AMessage),
231       mOutputFormat(new AMessage) { }
232 
initializeStandardParams()233 void CCodecConfig::initializeStandardParams() {
234     typedef Domain D;
235     mStandardParams = std::make_shared<StandardParams>();
236     std::function<void(const ConfigMapper &)> add =
237         [params = mStandardParams](const ConfigMapper &cm) {
238             params->add(cm);
239     };
240     std::function<void(const ConfigMapper &)> deprecated = add;
241 
242     // allow int32 or float SDK values and represent them as float
243     ConfigMapper::Mapper makeFloat = [](C2Value v) -> C2Value {
244         // convert from i32 to float
245         int32_t i32Value;
246         float fpValue;
247         if (v.get(&i32Value)) {
248             return (float)i32Value;
249         } else if (v.get(&fpValue)) {
250             return fpValue;
251         }
252         return C2Value();
253     };
254 
255     ConfigMapper::Mapper negate = [](C2Value v) -> C2Value {
256         int32_t value;
257         if (v.get(&value)) {
258             return -value;
259         }
260         return C2Value();
261     };
262 
263     add(ConfigMapper(KEY_MIME,     C2_PARAMKEY_INPUT_MEDIA_TYPE,    "value")
264         .limitTo(D::INPUT & D::READ));
265     add(ConfigMapper(KEY_MIME,     C2_PARAMKEY_OUTPUT_MEDIA_TYPE,   "value")
266         .limitTo(D::OUTPUT & D::READ));
267 
268     add(ConfigMapper(KEY_BIT_RATE, C2_PARAMKEY_BITRATE, "value").limitTo(D::ENCODER));
269     add(ConfigMapper(PARAMETER_KEY_VIDEO_BITRATE, C2_PARAMKEY_BITRATE, "value")
270         .limitTo(D::ENCODER & D::VIDEO & D::PARAM));
271     add(ConfigMapper(KEY_BITRATE_MODE, C2_PARAMKEY_BITRATE_MODE, "value")
272         .limitTo(D::ENCODER & D::CODED)
273         .withMapper([](C2Value v) -> C2Value {
274             int32_t value;
275             C2Config::bitrate_mode_t mode;
276             if (v.get(&value) && C2Mapper::map(value, &mode)) {
277                 return mode;
278             }
279             return C2Value();
280         }));
281     // remove when codecs switch to PARAMKEY and new modes
282     deprecated(ConfigMapper(KEY_BITRATE_MODE, "coded.bitrate-mode", "value")
283                .limitTo(D::ENCODER));
284     add(ConfigMapper(KEY_FRAME_RATE, C2_PARAMKEY_FRAME_RATE, "value")
285         .limitTo(D::VIDEO)
286         .withMappers(makeFloat, [](C2Value v) -> C2Value {
287             // read back always as int
288             float value;
289             if (v.get(&value)) {
290                 return (int32_t)value;
291             }
292             return C2Value();
293         }));
294 
295     add(ConfigMapper(KEY_MAX_INPUT_SIZE, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE, "value")
296         .limitTo(D::INPUT));
297     // remove when codecs switch to PARAMKEY
298     deprecated(ConfigMapper(KEY_MAX_INPUT_SIZE, "coded.max-frame-size", "value")
299                .limitTo(D::INPUT));
300     // SDK rotation is clock-wise
301     add(ConfigMapper(KEY_ROTATION, C2_PARAMKEY_VUI_ROTATION, "value")
302         .limitTo(D::VIDEO & D::CODED)
303         .withMappers(negate, negate));
304     add(ConfigMapper(KEY_ROTATION, C2_PARAMKEY_ROTATION, "value")
305         .limitTo(D::VIDEO & D::RAW)
306         .withMappers(negate, negate));
307 
308     add(ConfigMapper(std::string(KEY_FEATURE_) + FEATURE_SecurePlayback,
309                      C2_PARAMKEY_SECURE_MODE, "value"));
310 
311     add(ConfigMapper("prepend-sps-pps-to-idr-frames",
312                      C2_PARAMKEY_PREPEND_HEADER_MODE, "value")
313         .limitTo(D::ENCODER & D::VIDEO)
314         .withMapper([](C2Value v) -> C2Value {
315             int32_t value;
316             if (v.get(&value) && value) {
317                 return C2Value(C2Config::PREPEND_HEADER_TO_ALL_SYNC);
318             } else {
319                 return C2Value(C2Config::PREPEND_HEADER_TO_NONE);
320             }
321         }));
322     // remove when codecs switch to PARAMKEY
323     deprecated(ConfigMapper("prepend-sps-pps-to-idr-frames",
324                             "coding.add-csd-to-sync-frames", "value")
325                .limitTo(D::ENCODER & D::VIDEO));
326     add(ConfigMapper(C2_PARAMKEY_SYNC_FRAME_PERIOD, C2_PARAMKEY_SYNC_FRAME_PERIOD, "value"));
327     // remove when codecs switch to PARAMKEY
328     deprecated(ConfigMapper(C2_PARAMKEY_SYNC_FRAME_PERIOD, "coding.gop", "intra-period")
329                .limitTo(D::ENCODER & D::VIDEO));
330     add(ConfigMapper(KEY_INTRA_REFRESH_PERIOD, C2_PARAMKEY_INTRA_REFRESH, "period")
331         .limitTo(D::VIDEO & D::ENCODER));
332     add(ConfigMapper(KEY_QUALITY, C2_PARAMKEY_QUALITY, "value"));
333     add(ConfigMapper(PARAMETER_KEY_REQUEST_SYNC_FRAME,
334                      "coding.request-sync", "value")
335         .limitTo(D::PARAM & D::ENCODER));
336     add(ConfigMapper(PARAMETER_KEY_REQUEST_SYNC_FRAME,
337                      C2_PARAMKEY_REQUEST_SYNC_FRAME, "value")
338         .limitTo(D::PARAM & D::ENCODER));
339 
340     add(ConfigMapper(KEY_OPERATING_RATE,   C2_PARAMKEY_OPERATING_RATE,     "value"));
341     // C2 priorities are inverted
342     add(ConfigMapper(KEY_PRIORITY,         C2_PARAMKEY_PRIORITY,           "value")
343         .withMappers(negate, negate));
344     // remove when codecs switch to PARAMKEY
345     deprecated(ConfigMapper(KEY_OPERATING_RATE,   "ctrl.operating-rate",     "value")
346                .withMapper(makeFloat));
347     deprecated(ConfigMapper(KEY_PRIORITY,         "ctrl.priority",           "value"));
348 
349     add(ConfigMapper(KEY_WIDTH,         C2_PARAMKEY_PICTURE_SIZE,       "width")
350         .limitTo(D::VIDEO | D::IMAGE));
351     add(ConfigMapper(KEY_HEIGHT,        C2_PARAMKEY_PICTURE_SIZE,       "height")
352         .limitTo(D::VIDEO | D::IMAGE));
353 
354     add(ConfigMapper("crop-left",       C2_PARAMKEY_CROP_RECT,       "left")
355         .limitTo(D::VIDEO | D::IMAGE));
356     add(ConfigMapper("crop-top",        C2_PARAMKEY_CROP_RECT,       "top")
357         .limitTo(D::VIDEO | D::IMAGE));
358     add(ConfigMapper("crop-width",      C2_PARAMKEY_CROP_RECT,       "width")
359         .limitTo(D::VIDEO | D::IMAGE));
360     add(ConfigMapper("crop-height",     C2_PARAMKEY_CROP_RECT,       "height")
361         .limitTo(D::VIDEO | D::IMAGE));
362 
363     add(ConfigMapper(KEY_MAX_WIDTH,     C2_PARAMKEY_MAX_PICTURE_SIZE,    "width")
364         .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
365     add(ConfigMapper(KEY_MAX_HEIGHT,    C2_PARAMKEY_MAX_PICTURE_SIZE,    "height")
366         .limitTo((D::VIDEO | D::IMAGE) & D::RAW));
367 
368     add(ConfigMapper("csd-0",           C2_PARAMKEY_INIT_DATA,       "value")
369         .limitTo(D::OUTPUT & D::READ));
370 
371     // Pixel Format (use local key for actual pixel format as we don't distinguish between
372     // SDK layouts for flexible format and we need the actual SDK color format in the media format)
373     add(ConfigMapper("android._color-format",  C2_PARAMKEY_PIXEL_FORMAT, "value")
374         .limitTo((D::VIDEO | D::IMAGE) & D::RAW)
375         .withMappers([](C2Value v) -> C2Value {
376             int32_t value;
377             if (v.get(&value)) {
378                 switch (value) {
379                     case COLOR_FormatSurface:
380                         return (uint32_t)HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED;
381                     case COLOR_FormatYUV420Flexible:
382                         return (uint32_t)HAL_PIXEL_FORMAT_YCBCR_420_888;
383                     case COLOR_FormatYUV420Planar:
384                     case COLOR_FormatYUV420SemiPlanar:
385                     case COLOR_FormatYUV420PackedPlanar:
386                     case COLOR_FormatYUV420PackedSemiPlanar:
387                         return (uint32_t)HAL_PIXEL_FORMAT_YV12;
388                     default:
389                         // TODO: support some sort of passthrough
390                         break;
391                 }
392             }
393             return C2Value();
394         }, [](C2Value v) -> C2Value {
395             uint32_t value;
396             if (v.get(&value)) {
397                 switch (value) {
398                     case HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED:
399                         return COLOR_FormatSurface;
400                     case HAL_PIXEL_FORMAT_YV12:
401                     case HAL_PIXEL_FORMAT_YCBCR_420_888:
402                         return COLOR_FormatYUV420Flexible;
403                     default:
404                         // TODO: support some sort of passthrough
405                         break;
406                 }
407             }
408             return C2Value();
409         }));
410 
411     add(ConfigMapper(KEY_CHANNEL_COUNT, C2_PARAMKEY_CHANNEL_COUNT,       "value")
412         .limitTo(D::AUDIO)); // read back to both formats
413     add(ConfigMapper(KEY_CHANNEL_COUNT, C2_PARAMKEY_CODED_CHANNEL_COUNT, "value")
414         .limitTo(D::AUDIO & D::CODED));
415 
416     add(ConfigMapper(KEY_SAMPLE_RATE,   C2_PARAMKEY_SAMPLE_RATE,        "value")
417         .limitTo(D::AUDIO)); // read back to both port formats
418     add(ConfigMapper(KEY_SAMPLE_RATE,   C2_PARAMKEY_CODED_SAMPLE_RATE,  "value")
419         .limitTo(D::AUDIO & D::CODED));
420 
421     add(ConfigMapper(KEY_PCM_ENCODING,  C2_PARAMKEY_PCM_ENCODING,       "value")
422         .limitTo(D::AUDIO));
423 
424     add(ConfigMapper(KEY_IS_ADTS, C2_PARAMKEY_AAC_PACKAGING, "value")
425         .limitTo(D::AUDIO & D::CODED)
426         .withMappers([](C2Value v) -> C2Value {
427             int32_t value;
428             if (v.get(&value) && value) {
429                 return C2Config::AAC_PACKAGING_ADTS;
430             }
431             return C2Value();
432         }, [](C2Value v) -> C2Value {
433             uint32_t value;
434             if (v.get(&value) && value == C2Config::AAC_PACKAGING_ADTS) {
435                 return (int32_t)1;
436             }
437             return C2Value();
438         }));
439 
440     /* still to do
441     constexpr char KEY_AAC_DRC_ATTENUATION_FACTOR[] = "aac-drc-cut-level";
442     constexpr char KEY_AAC_DRC_BOOST_FACTOR[] = "aac-drc-boost-level";
443     constexpr char KEY_AAC_DRC_EFFECT_TYPE[] = "aac-drc-effect-type";
444     constexpr char KEY_AAC_DRC_HEAVY_COMPRESSION[] = "aac-drc-heavy-compression";
445     constexpr char KEY_AAC_DRC_TARGET_REFERENCE_LEVEL[] = "aac-target-ref-level";
446     constexpr char KEY_AAC_ENCODED_TARGET_LEVEL[] = "aac-encoded-target-level";
447     constexpr char KEY_AAC_MAX_OUTPUT_CHANNEL_COUNT[] = "aac-max-output-channel_count";
448 
449     constexpr char KEY_AAC_SBR_MODE[] = "aac-sbr-mode";
450     constexpr char KEY_AUDIO_SESSION_ID[] = "audio-session-id";
451 
452     constexpr char KEY_CAPTURE_RATE[] = "capture-rate";
453     constexpr char KEY_CHANNEL_MASK[] = "channel-mask";
454     constexpr char KEY_COLOR_RANGE[] = "color-range";
455     constexpr char KEY_COLOR_STANDARD[] = "color-standard";
456     constexpr char KEY_COLOR_TRANSFER[] = "color-transfer";
457     constexpr char KEY_FLAC_COMPRESSION_LEVEL[] = "flac-compression-level";
458     constexpr char KEY_GRID_COLUMNS[] = "grid-cols";
459     constexpr char KEY_GRID_ROWS[] = "grid-rows";
460     constexpr char KEY_HDR_STATIC_INFO[] = "hdr-static-info";
461     constexpr char KEY_LATENCY[] = "latency";
462     constexpr char KEY_LEVEL[] = "level";
463     constexpr char KEY_MAX_BIT_RATE[] = "max-bitrate";
464     constexpr char KEY_OUTPUT_REORDER_DEPTH[] = "output-reorder-depth";
465     constexpr char KEY_PROFILE[] = "profile";
466     constexpr char KEY_PUSH_BLANK_BUFFERS_ON_STOP[] = "push-blank-buffers-on-shutdown";
467     constexpr char KEY_QUALITY[] = "quality";
468     constexpr char KEY_REPEAT_PREVIOUS_FRAME_AFTER[] = "repeat-previous-frame-after";
469     constexpr char KEY_SLICE_HEIGHT[] = "slice-height";
470     constexpr char KEY_STRIDE[] = "stride";
471     constexpr char KEY_TEMPORAL_LAYERING[] = "ts-schema";
472     constexpr char KEY_TILE_HEIGHT[] = "tile-height";
473     constexpr char KEY_TILE_WIDTH[] = "tile-width";
474     constexpr char KEY_TRACK_ID[] = "track-id";
475 
476     */
477 }
478 
initialize(const std::shared_ptr<Codec2Client> & client,const std::shared_ptr<Codec2Client::Component> & component)479 status_t CCodecConfig::initialize(
480         const std::shared_ptr<Codec2Client> &client,
481         const std::shared_ptr<Codec2Client::Component> &component) {
482     C2ComponentDomainSetting domain(C2Component::DOMAIN_OTHER);
483     C2ComponentKindSetting kind(C2Component::KIND_OTHER);
484 
485     std::vector<std::unique_ptr<C2Param>> queried;
486     c2_status_t c2err = component->query({ &domain, &kind }, {}, C2_DONT_BLOCK, &queried);
487     if (c2err != C2_OK) {
488         ALOGD("Query domain & kind failed => %s", asString(c2err));
489         // TEMP: determine kind from component name
490         if (kind.value == C2Component::KIND_OTHER) {
491             if (component->getName().find("encoder") != std::string::npos) {
492                 kind.value = C2Component::KIND_ENCODER;
493             } else if (component->getName().find("decoder") != std::string::npos) {
494                 kind.value = C2Component::KIND_DECODER;
495             }
496         }
497 
498         // TEMP: determine domain from media type (port (preferred) or stream #0)
499         if (domain.value == C2Component::DOMAIN_OTHER) {
500             c2err = component->query(
501                     {}, { C2PortMediaTypeSetting::input::PARAM_TYPE,
502                           C2StreamMediaTypeSetting::input::PARAM_TYPE }, C2_DONT_BLOCK, &queried);
503             if (c2err != C2_OK && queried.size() == 0) {
504                 ALOGD("Query media type failed => %s", asString(c2err));
505             } else {
506                 AString mediaType;
507                 C2PortMediaTypeSetting::input *portMediaType =
508                     C2PortMediaTypeSetting::input::From(queried[0].get());
509                 if (portMediaType) {
510                     mediaType = AString(portMediaType->m.value, portMediaType->flexCount());
511                 } else {
512                     C2StreamMediaTypeSetting::input *streamMediaType =
513                         C2StreamMediaTypeSetting::input::From(queried[0].get());
514                     if (streamMediaType) {
515                         mediaType = AString(streamMediaType->m.value, streamMediaType->flexCount());
516                     }
517                 }
518                 ALOGD("read media type: %s", mediaType.c_str());
519                 if (mediaType.startsWith("audio/")) {
520                     domain.value = C2Component::DOMAIN_AUDIO;
521                 } else if (mediaType.startsWith("video/")) {
522                     domain.value = C2Component::DOMAIN_VIDEO;
523                 } else if (mediaType.startsWith("image/")) {
524                     domain.value = C2Component::DOMAIN_IMAGE;
525                 }
526             }
527         }
528     }
529 
530     mDomain = (domain.value == C2Component::DOMAIN_VIDEO ? Domain::IS_VIDEO :
531                domain.value == C2Component::DOMAIN_IMAGE ? Domain::IS_IMAGE :
532                domain.value == C2Component::DOMAIN_AUDIO ? Domain::IS_AUDIO : Domain::OTHER_DOMAIN)
533             | (kind.value == C2Component::KIND_DECODER ? Domain::IS_DECODER :
534                kind.value == C2Component::KIND_ENCODER ? Domain::IS_ENCODER : Domain::OTHER_KIND);
535 
536     mInputDomain = Domain(((mDomain & IS_DECODER) ? IS_CODED : IS_RAW) | IS_INPUT);
537     mOutputDomain = Domain(((mDomain & IS_ENCODER) ? IS_CODED : IS_RAW) | IS_OUTPUT);
538 
539     ALOGV("domain is %#x (%u %u)", mDomain, domain.value, kind.value);
540 
541     c2err = component->querySupportedParams(&mParamDescs);
542     if (c2err != C2_OK) {
543         ALOGD("Query supported params failed after returning %zu values => %s",
544                 mParamDescs.size(), asString(c2err));
545         // TODO: return error once we complete implementation.
546         return UNKNOWN_ERROR;
547     }
548     for (const std::shared_ptr<C2ParamDescriptor> &desc : mParamDescs) {
549         mSupportedIndices.emplace(desc->index());
550     }
551 
552     mReflector = client->getParamReflector();
553     if (mReflector == nullptr) {
554         ALOGE("Failed to get param reflector");
555         // TODO: report error once we complete implementation.
556         return UNKNOWN_ERROR;
557     }
558 
559     // enumerate all fields
560     mParamUpdater = std::make_shared<ReflectedParamUpdater>();
561     mParamUpdater->clear();
562     mParamUpdater->addParamDesc(mReflector, mParamDescs);
563 
564     // TEMP: add some standard fields even if not reflected
565     if (kind.value == C2Component::KIND_ENCODER) {
566         mParamUpdater->addStandardParam<C2StreamInitDataInfo::output>(C2_PARAMKEY_INIT_DATA);
567     }
568     if (kind.value != C2Component::KIND_ENCODER
569             && (domain.value == C2Component::DOMAIN_IMAGE
570                     || domain.value == C2Component::DOMAIN_VIDEO)) {
571         addLocalParam<C2StreamPictureSizeInfo::output>(C2_PARAMKEY_PICTURE_SIZE);
572         addLocalParam<C2StreamCropRectInfo::output>(C2_PARAMKEY_CROP_RECT);
573         addLocalParam(
574                 new C2StreamPixelAspectRatioInfo::output(0u, 1u, 1u),
575                 C2_PARAMKEY_PIXEL_ASPECT_RATIO);
576         addLocalParam(new C2StreamRotationInfo::output(0u, 0), C2_PARAMKEY_ROTATION);
577         addLocalParam(new C2StreamColorAspectsInfo::output(0u), C2_PARAMKEY_COLOR_ASPECTS);
578         addLocalParam<C2StreamHdrStaticInfo::output>(C2_PARAMKEY_HDR_STATIC_INFO);
579         addLocalParam<C2StreamDataSpaceInfo::output>(C2_PARAMKEY_DATA_SPACE);
580         addLocalParam<C2StreamSurfaceScalingInfo::output>(C2_PARAMKEY_SURFACE_SCALING_MODE);
581     }
582 
583     initializeStandardParams();
584 
585     // subscribe to all supported standard (exposed) params
586     // TODO: limit this to params that are actually in the domain
587     std::vector<std::string> formatKeys = mStandardParams->getPathsForDomain(Domain(1 << 30));
588     std::vector<C2Param::Index> indices;
589     mParamUpdater->getParamIndicesForKeys(formatKeys, &indices);
590     mSubscribedIndices.insert(indices.begin(), indices.end());
591 
592     // also subscribe to some non-SDK standard parameters
593     // for number of input/output buffers
594     mSubscribedIndices.emplace(C2PortSuggestedBufferCountTuning::input::PARAM_TYPE);
595     mSubscribedIndices.emplace(C2PortSuggestedBufferCountTuning::output::PARAM_TYPE);
596     mSubscribedIndices.emplace(C2ActualPipelineDelayTuning::PARAM_TYPE);
597     mSubscribedIndices.emplace(C2PortActualDelayTuning::input::PARAM_TYPE);
598     mSubscribedIndices.emplace(C2PortActualDelayTuning::output::PARAM_TYPE);
599     // for output buffer array allocation
600     mSubscribedIndices.emplace(C2StreamMaxBufferSizeInfo::output::PARAM_TYPE);
601     // init data (CSD)
602     mSubscribedIndices.emplace(C2StreamInitDataInfo::output::PARAM_TYPE);
603 
604     return OK;
605 }
606 
subscribeToConfigUpdate(const std::shared_ptr<Codec2Client::Component> & component,const std::vector<C2Param::Index> & indices,c2_blocking_t blocking)607 status_t CCodecConfig::subscribeToConfigUpdate(
608         const std::shared_ptr<Codec2Client::Component> &component,
609         const std::vector<C2Param::Index> &indices,
610         c2_blocking_t blocking) {
611     mSubscribedIndices.insert(indices.begin(), indices.end());
612     // TODO: enable this when components no longer crash on this config
613     if (mSubscribedIndices.size() != mSubscribedIndicesSize && false) {
614         std::vector<uint32_t> indices;
615         for (C2Param::Index ix : mSubscribedIndices) {
616             indices.push_back(ix);
617         }
618         std::unique_ptr<C2SubscribedParamIndicesTuning> subscribeTuning =
619             C2SubscribedParamIndicesTuning::AllocUnique(indices);
620         std::vector<std::unique_ptr<C2SettingResult>> results;
621         c2_status_t c2Err = component->config({ subscribeTuning.get() }, blocking, &results);
622         if (c2Err != C2_OK && c2Err != C2_BAD_INDEX) {
623             ALOGD("Failed to subscribe to parameters => %s", asString(c2Err));
624             // TODO: error
625         }
626         ALOGV("Subscribed to %zu params", mSubscribedIndices.size());
627         mSubscribedIndicesSize = mSubscribedIndices.size();
628     }
629     return OK;
630 }
631 
queryConfiguration(const std::shared_ptr<Codec2Client::Component> & component)632 status_t CCodecConfig::queryConfiguration(
633         const std::shared_ptr<Codec2Client::Component> &component) {
634     // query all subscribed parameters
635     std::vector<C2Param::Index> indices(mSubscribedIndices.begin(), mSubscribedIndices.end());
636     std::vector<std::unique_ptr<C2Param>> queried;
637     c2_status_t c2Err = component->query({}, indices, C2_MAY_BLOCK, &queried);
638     if (c2Err != OK) {
639         ALOGI("query failed after returning %zu values (%s)", queried.size(), asString(c2Err));
640         // TODO: error
641     }
642 
643     updateConfiguration(queried, ALL);
644     return OK;
645 }
646 
updateConfiguration(std::vector<std::unique_ptr<C2Param>> & configUpdate,Domain domain)647 bool CCodecConfig::updateConfiguration(
648         std::vector<std::unique_ptr<C2Param>> &configUpdate, Domain domain) {
649     ALOGV("updating configuration with %zu params", configUpdate.size());
650     bool changed = false;
651     for (std::unique_ptr<C2Param> &p : configUpdate) {
652         if (p && *p) {
653             auto insertion = mCurrentConfig.emplace(p->index(), nullptr);
654             if (insertion.second || *insertion.first->second != *p) {
655                 changed = true;
656             }
657             insertion.first->second = std::move(p);
658         }
659     }
660 
661     ALOGV("updated configuration has %zu params (%s)", mCurrentConfig.size(),
662             changed ? "CHANGED" : "no change");
663     if (changed) {
664         return updateFormats(domain);
665     }
666     return false;
667 }
668 
updateFormats(Domain domain)669 bool CCodecConfig::updateFormats(Domain domain) {
670     // get addresses of params in the current config
671     std::vector<C2Param*> paramPointers;
672     for (const auto &it : mCurrentConfig) {
673         paramPointers.push_back(it.second.get());
674     }
675 
676     ReflectedParamUpdater::Dict reflected = mParamUpdater->getParams(paramPointers);
677     ALOGD("c2 config is %s", reflected.debugString().c_str());
678 
679     bool changed = false;
680     if (domain & mInputDomain) {
681         sp<AMessage> oldFormat = mInputFormat;
682         mInputFormat->extend(getSdkFormatForDomain(reflected, mInputDomain));
683         if (mInputFormat->countEntries() != oldFormat->countEntries()
684                 || mInputFormat->changesFrom(oldFormat)->countEntries() > 0) {
685             changed = true;
686         }
687     }
688     if (domain & mOutputDomain) {
689         sp<AMessage> oldFormat = mOutputFormat;
690         mOutputFormat->extend(getSdkFormatForDomain(reflected, mOutputDomain));
691         if (!changed &&
692                 (mOutputFormat->countEntries() != oldFormat->countEntries()
693                         || mOutputFormat->changesFrom(oldFormat)->countEntries() > 0)) {
694             changed = true;
695         }
696     }
697     ALOGV_IF(changed, "format(s) changed");
698     return changed;
699 }
700 
getSdkFormatForDomain(const ReflectedParamUpdater::Dict & reflected,Domain domain) const701 sp<AMessage> CCodecConfig::getSdkFormatForDomain(
702         const ReflectedParamUpdater::Dict &reflected, Domain domain) const {
703     sp<AMessage> msg = new AMessage;
704     for (const std::pair<std::string, std::vector<ConfigMapper>> &el : mStandardParams->getKeys()) {
705         for (const ConfigMapper &cm : el.second) {
706             if ((cm.domain() & domain) == 0 || (cm.domain() & mDomain) == 0) {
707                 continue;
708             }
709             auto it = reflected.find(cm.path());
710             if (it == reflected.end()) {
711                 continue;
712             }
713             C2Value c2Value;
714             sp<ABuffer> bufValue;
715             AString strValue;
716             AMessage::ItemData item;
717             if (it->second.find(&c2Value)) {
718                 item = cm.mapToMessage(c2Value);
719             } else if (it->second.find(&bufValue)) {
720                 item.set(bufValue);
721             } else if (it->second.find(&strValue)) {
722                 item.set(strValue);
723             } else {
724                 ALOGD("unexpected untyped query value for key: %s", cm.path().c_str());
725                 continue;
726             }
727             msg->setItem(el.first.c_str(), item);
728         }
729     }
730 
731     { // convert from Codec 2.0 rect to MediaFormat rect and add crop rect if not present
732         int32_t left, top, width, height;
733         if (msg->findInt32("crop-left", &left) && msg->findInt32("crop-width", &width)
734                 && msg->findInt32("crop-top", &top) && msg->findInt32("crop-height", &height)
735                 && left >= 0 && width >=0 && width <= INT32_MAX - left
736                 && top >= 0 && height >=0 && height <= INT32_MAX - top) {
737             msg->removeEntryAt(msg->findEntryByName("crop-left"));
738             msg->removeEntryAt(msg->findEntryByName("crop-top"));
739             msg->removeEntryAt(msg->findEntryByName("crop-width"));
740             msg->removeEntryAt(msg->findEntryByName("crop-height"));
741             msg->setRect("crop", left, top, left + width - 1, top + height - 1);
742         } else if (msg->findInt32("width", &width) && msg->findInt32("height", &height)) {
743             msg->setRect("crop", 0, 0, width - 1, height - 1);
744         }
745     }
746 
747     ALOGV("converted to SDK values as %s", msg->debugString().c_str());
748     return msg;
749 }
750 
751 /// converts an AMessage value to a ParamUpdater value
convert(const AMessage::ItemData & from,ReflectedParamUpdater::Value * to)752 static void convert(const AMessage::ItemData &from, ReflectedParamUpdater::Value *to) {
753     int32_t int32Value;
754     int64_t int64Value;
755     sp<ABuffer> bufValue;
756     AString strValue;
757     float floatValue;
758     double doubleValue;
759 
760     if (from.find(&int32Value)) {
761         to->set(int32Value);
762     } else if (from.find(&int64Value)) {
763         to->set(int64Value);
764     } else if (from.find(&bufValue)) {
765         to->set(bufValue);
766     } else if (from.find(&strValue)) {
767         to->set(strValue);
768     } else if (from.find(&floatValue)) {
769         to->set(C2Value(floatValue));
770     } else if (from.find(&doubleValue)) {
771         // convert double to float
772         to->set(C2Value((float)doubleValue));
773     }
774     // ignore all other AMessage types
775 }
776 
777 /// relaxes Codec 2.0 specific value types to SDK types (mainly removes signedness and counterness
778 /// from 32/64-bit values.)
relaxValues(ReflectedParamUpdater::Value & item)779 static void relaxValues(ReflectedParamUpdater::Value &item) {
780     C2Value c2Value;
781     int32_t int32Value;
782     int64_t int64Value;
783     (void)item.find(&c2Value);
784     if (c2Value.get(&int32Value) || c2Value.get((uint32_t*)&int32Value)
785             || c2Value.get((c2_cntr32_t*)&int32Value)) {
786         item.set(int32Value);
787     } else if (c2Value.get(&int64Value)
788             || c2Value.get((uint64_t*)&int64Value)
789             || c2Value.get((c2_cntr64_t*)&int64Value)) {
790         item.set(int64Value);
791     }
792 }
793 
getReflectedFormat(const sp<AMessage> & params_,Domain domain) const794 ReflectedParamUpdater::Dict CCodecConfig::getReflectedFormat(
795         const sp<AMessage> &params_, Domain domain) const {
796     // create a modifiable copy of params
797     sp<AMessage> params = params_->dup();
798     ALOGV("filtering with domain %x", domain);
799 
800     // convert some macro parameters to Codec 2.0 specific expressions
801 
802     { // make i-frame-interval time based
803         int32_t iFrameInterval;
804         if (params->findInt32("i-frame-interval", &iFrameInterval)) {
805             int32_t intFrameRate;
806             float frameRate;
807             if (params->findInt32("frame-rate", &intFrameRate)) {
808                 params->setInt32(C2_PARAMKEY_SYNC_FRAME_PERIOD, iFrameInterval * intFrameRate + 0.5);
809             } else if (params->findFloat("frame-rate", &frameRate)) {
810                 params->setInt32(C2_PARAMKEY_SYNC_FRAME_PERIOD, iFrameInterval * frameRate + 0.5);
811             }
812         }
813     }
814 
815     { // convert from MediaFormat rect to Codec 2.0 rect
816         int32_t offset;
817         int32_t end;
818         AMessage::ItemData item;
819         if (params->findInt32("crop-left", &offset) && params->findInt32("crop-right", &end)
820                 && offset >= 0 && end >= offset - 1) {
821             size_t ix = params->findEntryByName("crop-right");
822             params->setEntryNameAt(ix, "crop-width");
823             item.set(end - offset + 1);
824             params->setEntryAt(ix, item);
825         }
826         if (params->findInt32("crop-top", &offset) && params->findInt32("crop-bottom", &end)
827                 && offset >= 0 && end >= offset - 1) {
828             size_t ix = params->findEntryByName("crop-bottom");
829             params->setEntryNameAt(ix, "crop-height");
830             item.set(end - offset + 1);
831             params->setEntryAt(ix, item);
832         }
833     }
834 
835     // this is to verify that we set proper signedness for standard parameters
836     bool beVeryStrict = property_get_bool("debug.stagefright.ccodec_strict_type", false);
837     // this is to allow vendors to use the wrong signedness for standard parameters
838     bool beVeryLax = property_get_bool("debug.stagefright.ccodec_lax_type", false);
839 
840     ReflectedParamUpdater::Dict filtered;
841     for (size_t ix = 0; ix < params->countEntries(); ++ix) {
842         AMessage::Type type;
843         AString name = params->getEntryNameAt(ix, &type);
844         AMessage::ItemData msgItem = params->getEntryAt(ix);
845         ReflectedParamUpdater::Value item;
846         convert(msgItem, &item); // convert item to param updater item
847 
848         if (name.startsWith("vendor.")) {
849             // vendor params pass through as is
850             filtered.emplace(name.c_str(), item);
851             continue;
852         }
853         // standard parameters may get modified, filtered or duplicated
854         for (const ConfigMapper &cm : mStandardParams->getConfigMappersForSdkKey(name.c_str())) {
855             if (cm.domain() & domain & mDomain) {
856                 // map arithmetic values, pass through string or buffer
857                 switch (type) {
858                     case AMessage::kTypeBuffer:
859                     case AMessage::kTypeString:
860                         break;
861                     case AMessage::kTypeInt32:
862                     case AMessage::kTypeInt64:
863                     case AMessage::kTypeFloat:
864                     case AMessage::kTypeDouble:
865                         // for now only map settings with mappers as we are not creating
866                         // signed <=> unsigned mappers
867                         // TODO: be precise about signed unsigned
868                         if (beVeryStrict || cm.mapper()) {
869                             item.set(cm.mapFromMessage(params->getEntryAt(ix)));
870                             // also allow to relax type strictness
871                             if (beVeryLax) {
872                                 relaxValues(item);
873                             }
874                         }
875                         break;
876                     default:
877                         continue;
878                 }
879                 filtered.emplace(cm.path(), item);
880             }
881         }
882     }
883     ALOGV("filtered %s to %s", params->debugString(4).c_str(),
884             filtered.debugString(4).c_str());
885     return filtered;
886 }
887 
getConfigUpdateFromSdkParams(std::shared_ptr<Codec2Client::Component> component,const sp<AMessage> & sdkParams,Domain domain,c2_blocking_t blocking,std::vector<std::unique_ptr<C2Param>> * configUpdate) const888 status_t CCodecConfig::getConfigUpdateFromSdkParams(
889         std::shared_ptr<Codec2Client::Component> component,
890         const sp<AMessage> &sdkParams, Domain domain,
891         c2_blocking_t blocking,
892         std::vector<std::unique_ptr<C2Param>> *configUpdate) const {
893     ReflectedParamUpdater::Dict params = getReflectedFormat(sdkParams, domain);
894 
895     std::vector<C2Param::Index> indices;
896     mParamUpdater->getParamIndicesFromMessage(params, &indices);
897     if (indices.empty()) {
898         ALOGD("no recognized params in: %s", params.debugString().c_str());
899         return OK;
900     }
901 
902     configUpdate->clear();
903     std::vector<C2Param::Index> supportedIndices;
904     for (C2Param::Index ix : indices) {
905         if (mSupportedIndices.count(ix)) {
906             supportedIndices.push_back(ix);
907         } else if (mLocalParams.count(ix)) {
908             // query local parameter here
909             auto it = mCurrentConfig.find(ix);
910             if (it != mCurrentConfig.end()) {
911                 configUpdate->emplace_back(C2Param::Copy(*it->second));
912             }
913         }
914     }
915 
916     c2_status_t err = component->query({ }, supportedIndices, blocking, configUpdate);
917     if (err != C2_OK) {
918         ALOGD("query failed after returning %zu params => %s", configUpdate->size(), asString(err));
919     }
920 
921     if (configUpdate->size()) {
922         mParamUpdater->updateParamsFromMessage(params, configUpdate);
923     }
924     return OK;
925 }
926 
setParameters(std::shared_ptr<Codec2Client::Component> component,std::vector<std::unique_ptr<C2Param>> & configUpdate,c2_blocking_t blocking)927 status_t CCodecConfig::setParameters(
928         std::shared_ptr<Codec2Client::Component> component,
929         std::vector<std::unique_ptr<C2Param>> &configUpdate,
930         c2_blocking_t blocking) {
931     status_t result = OK;
932     if (configUpdate.empty()) {
933         return OK;
934     }
935 
936     std::vector<C2Param::Index> indices;
937     std::vector<C2Param *> paramVector;
938     for (const std::unique_ptr<C2Param> &param : configUpdate) {
939         if (mSupportedIndices.count(param->index())) {
940             // component parameter
941             paramVector.push_back(param.get());
942             indices.push_back(param->index());
943         } else if (mLocalParams.count(param->index())) {
944             // handle local parameter here
945             LocalParamValidator validator = mLocalParams.find(param->index())->second;
946             c2_status_t err = C2_OK;
947             std::unique_ptr<C2Param> copy = C2Param::Copy(*param);
948             if (validator) {
949                 err = validator(copy);
950             }
951             if (err == C2_OK) {
952                 ALOGV("updated local parameter value for %s",
953                         mParamUpdater->getParamName(param->index()).c_str());
954 
955                 mCurrentConfig[param->index()] = std::move(copy);
956             } else {
957                 ALOGD("failed to set parameter value for %s => %s",
958                         mParamUpdater->getParamName(param->index()).c_str(), asString(err));
959                 result = BAD_VALUE;
960             }
961         }
962     }
963     // update subscribed param indices
964     subscribeToConfigUpdate(component, indices, blocking);
965 
966     std::vector<std::unique_ptr<C2SettingResult>> failures;
967     c2_status_t err = component->config(paramVector, blocking, &failures);
968     if (err != C2_OK) {
969         ALOGD("config failed => %s", asString(err));
970         // This is non-fatal.
971     }
972     for (const std::unique_ptr<C2SettingResult> &failure : failures) {
973         switch (failure->failure) {
974             case C2SettingResult::BAD_VALUE:
975                 ALOGD("Bad parameter value");
976                 result = BAD_VALUE;
977                 break;
978             default:
979                 ALOGV("failure = %d", int(failure->failure));
980                 break;
981         }
982     }
983 
984     // Re-query parameter values in case config could not update them and update the current
985     // configuration.
986     configUpdate.clear();
987     err = component->query({}, indices, blocking, &configUpdate);
988     if (err != C2_OK) {
989         ALOGD("query failed after returning %zu params => %s", configUpdate.size(), asString(err));
990     }
991     (void)updateConfiguration(configUpdate, ALL);
992 
993     // TODO: error value
994     return result;
995 }
996 
getConfigParameterValue(C2Param::Index index) const997 const C2Param *CCodecConfig::getConfigParameterValue(C2Param::Index index) const {
998     auto it = mCurrentConfig.find(index);
999     if (it == mCurrentConfig.end()) {
1000         return nullptr;
1001     } else {
1002         return it->second.get();
1003     }
1004 }
1005 
1006 }  // namespace android
1007 
1008