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 #ifndef C_CODEC_CONFIG_H_
18 #define C_CODEC_CONFIG_H_
19 
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <vector>
24 
25 #include <C2Component.h>
26 #include <codec2/hidl/client.h>
27 
28 #include <utils/RefBase.h>
29 
30 #include "InputSurfaceWrapper.h"
31 #include "ReflectedParamUpdater.h"
32 
33 namespace android {
34 
35 struct AMessage;
36 struct StandardParams;
37 
38 /**
39  * Struct managing the codec configuration for CCodec.
40  */
41 struct CCodecConfig {
42 
43     /**
44      * Domain consists of a bitmask divided into fields, and specifiers work by excluding other
45      * values in those domains.
46      *
47      * Component domains are composed by or-ing the individual IS_ constants, e.g.
48      * IS_DECODER | IS_AUDIO.
49      *
50      * Config specifiers are composed by or-ing the individual mask constants, and
51      * and-ing these groups: e.g. (DECODER | ENCODER) & AUDIO.
52      *
53      * The naming of these constants was to limit the length of mask names as these are used more
54      * commonly as masks.
55      */
56     enum Domain : uint32_t {
57         // component domain (domain & kind)
58         GUARD_BIT   = (1 << 1),   ///< this is to prevent against accidental && or || usage
59         IS_AUDIO    = (1 << 2),   ///< for audio codecs
60         IS_VIDEO    = (1 << 3),   ///< for video codecs
61         IS_IMAGE    = (1 << 4),   ///< for image codecs
62         OTHER_DOMAIN = (1 << 5),  ///< for other domains
63 
64         IS_ENCODER  = (1 << 6),   ///< for encoders
65         IS_DECODER  = (1 << 7),   ///< for decoders
66         OTHER_KIND  = (1 << 8),   ///< for other domains
67 
68         // config domain
69         IS_PARAM    = (1 << 9),   ///< for setParameter
70         IS_CONFIG   = (1 << 10),  ///< for configure
71         IS_READ     = (1 << 11),  ///< for getFormat
72 
73         // port domain
74         IS_INPUT    = (1 << 12),  ///< for input port (getFormat)
75         IS_OUTPUT   = (1 << 13),  ///< for output port (getFormat)
76         IS_RAW      = (1 << 14),  ///< for raw port (input-encoder, output-decoder)
77         IS_CODED    = (1 << 15),  ///< for coded port (input-decoder, output-encoder)
78 
79         ALL     = ~0U,
80         NONE    = 0,
81 
82         AUDIO   = ~(IS_IMAGE | IS_VIDEO | OTHER_DOMAIN),
83         VIDEO   = ~(IS_AUDIO | IS_IMAGE | OTHER_DOMAIN),
84         IMAGE   = ~(IS_AUDIO | IS_VIDEO | OTHER_DOMAIN),
85 
86         DECODER = ~(IS_ENCODER | OTHER_KIND),
87         ENCODER = ~(IS_DECODER | OTHER_KIND),
88 
89         PARAM   = ~(IS_CONFIG | IS_READ),
90         CONFIG  = ~(IS_PARAM  | IS_READ),
91         READ    = ~(IS_CONFIG | IS_PARAM),
92 
93         INPUT   = ~(IS_OUTPUT | IS_RAW    | IS_CODED),
94         OUTPUT  = ~(IS_INPUT  | IS_RAW    | IS_CODED),
95         RAW     = ~(IS_INPUT  | IS_OUTPUT | IS_CODED),
96         CODED   = ~(IS_INPUT  | IS_RAW    | IS_OUTPUT),
97     };
98 
99     // things required to manage formats
100     std::vector<std::shared_ptr<C2ParamDescriptor>> mParamDescs;
101     std::shared_ptr<C2ParamReflector> mReflector;
102 
103     std::shared_ptr<ReflectedParamUpdater> mParamUpdater;
104 
105     Domain mDomain; // component domain
106     Domain mInputDomain; // input port domain
107     Domain mOutputDomain; // output port domain
108     std::string mCodingMediaType;  // media type of the coded stream
109 
110     // standard MediaCodec to Codec 2.0 params mapping
111     std::shared_ptr<StandardParams> mStandardParams;
112 
113     std::set<C2Param::Index> mSupportedIndices; ///< indices supported by the component
114     std::set<C2Param::Index> mSubscribedIndices; ///< indices to subscribe to
115     size_t mSubscribedIndicesSize; ///< count of currently subscribed indices
116 
117     sp<AMessage> mInputFormat;
118     sp<AMessage> mOutputFormat;
119 
120     bool mUsingSurface; ///< using input or output surface
121 
122     std::shared_ptr<InputSurfaceWrapper> mInputSurface;
123     std::unique_ptr<InputSurfaceWrapper::Config> mISConfig;
124 
125     /// the current configuration. Updated after configure() and based on configUpdate in
126     /// onWorkDone
127     std::map<C2Param::Index, std::unique_ptr<C2Param>> mCurrentConfig;
128 
129     typedef std::function<c2_status_t(std::unique_ptr<C2Param>&)> LocalParamValidator;
130 
131     /// Parameter indices tracked in current config that are not supported by the component.
132     /// these are provided so that optional parameters can remain in the current configuration.
133     /// as such, these parameters have no dependencies. TODO: use C2InterfaceHelper for this.
134     /// For now support a validation function.
135     std::map<C2Param::Index, LocalParamValidator> mLocalParams;
136 
137     std::set<std::string> mLastConfig;
138 
139     CCodecConfig();
140 
141     /// initializes the members required to manage the format: descriptors, reflector,
142     /// reflected param helper, domain, standard params, and subscribes to standard
143     /// indices.
144     status_t initialize(
145             const std::shared_ptr<Codec2Client> &client,
146             const std::shared_ptr<Codec2Client::Component> &component);
147 
148 
149     /**
150      * Adds a locally maintained parameter. This is used for output configuration that can be
151      * appended to the output buffers in case it is not supported by the component.
152      */
153     template<typename T>
154     bool addLocalParam(
155             const std::string &name,
156             C2ParamDescriptor::attrib_t attrib = C2ParamDescriptor::IS_READ_ONLY,
157             std::function<c2_status_t(std::unique_ptr<T>&)> validator_ =
158                 std::function<c2_status_t(std::unique_ptr<T>&)>()) {
159         C2Param::Index index = T::PARAM_TYPE;
160         if (mSupportedIndices.count(index) || mLocalParams.count(index)) {
161             if (mSupportedIndices.count(index)) {
162                 mSubscribedIndices.emplace(index);
163             }
164             ALOGD("ignoring local param %s (%#x) as it is already %s",
165                     name.c_str(), (uint32_t)index, mSupportedIndices.count(index) ? "supported" : "local");
166             return false; // already supported by the component or already added
167         }
168 
169         // wrap typed validator into untyped validator
170         LocalParamValidator validator;
171         if (validator_) {
172             validator = [validator_](std::unique_ptr<C2Param>& p){
173                 c2_status_t res = C2_BAD_VALUE;
174                 std::unique_ptr<T> typed(static_cast<T*>(p.release()));
175                 // if parameter is correctly typed
176                 if (T::From(typed.get())) {
177                     res = validator_(typed);
178                     p.reset(typed.release());
179                 }
180                 return res;
181             };
182         }
183 
184         mLocalParams.emplace(index, validator);
185         mParamUpdater->addStandardParam<T>(name, attrib);
186         return true;
187     }
188 
189     /**
190      * Adds a locally maintained parameter with a default value.
191      */
192     template<typename T>
193     bool addLocalParam(
194             std::unique_ptr<T> default_,
195             const std::string &name,
196             C2ParamDescriptor::attrib_t attrib = C2ParamDescriptor::IS_READ_ONLY,
197             std::function<c2_status_t(std::unique_ptr<T>&)> validator_ =
198                 std::function<c2_status_t(std::unique_ptr<T>&)>()) {
199         if (addLocalParam<T>(name, attrib, validator_)) {
200             if (validator_) {
201                 c2_status_t err = validator_(default_);
202                 if (err != C2_OK) {
203                     ALOGD("default value for %s is invalid => %s", name.c_str(), asString(err));
204                     return false;
205                 }
206             }
207             mCurrentConfig[T::PARAM_TYPE] = std::move(default_);
208             return true;
209         }
210         return false;
211     }
212 
213     template<typename T>
214     bool addLocalParam(
215             T *default_, const std::string &name,
216             C2ParamDescriptor::attrib_t attrib = C2ParamDescriptor::IS_READ_ONLY,
217             std::function<c2_status_t(std::unique_ptr<T>&)> validator_ =
218                 std::function<c2_status_t(std::unique_ptr<T>&)>()) {
219         return addLocalParam(std::unique_ptr<T>(default_), name, attrib, validator_);
220     }
221 
222     /// Applies configuration updates, and updates format in the specific domain.
223     /// Returns true if formats were updated
224     /// \param domain input/output bitmask
225     bool updateConfiguration(
226             std::vector<std::unique_ptr<C2Param>> &configUpdate, Domain domain);
227 
228     /// Updates formats in the specific domain. Returns true if any of the formats have changed.
229     /// \param domain input/output bitmask
230     bool updateFormats(Domain domain);
231 
232     /**
233      * Applies SDK configurations in a specific configuration domain.
234      * Updates relevant input/output formats and subscribes to parameters specified in the
235      * configuration.
236      * \param domain config/setParam bitmask
237      * \param blocking blocking mode to use with the component
238      */
239     status_t getConfigUpdateFromSdkParams(
240             std::shared_ptr<Codec2Client::Component> component,
241             const sp<AMessage> &sdkParams, Domain domain,
242             c2_blocking_t blocking,
243             std::vector<std::unique_ptr<C2Param>> *configUpdate) const;
244 
245     /**
246      * Applies a configuration update to the component.
247      * Updates relevant input/output formats and subscribes to parameters specified in the
248      * configuration.
249      * \param blocking blocking mode to use with the component
250      */
251     status_t setParameters(
252             std::shared_ptr<Codec2Client::Component> component,
253             std::vector<std::unique_ptr<C2Param>> &configUpdate,
254             c2_blocking_t blocking);
255 
256     /// Queries subscribed indices (which contains all SDK-exposed values) and updates
257     /// input/output formats.
258     status_t queryConfiguration(
259             const std::shared_ptr<Codec2Client::Component> &component);
260 
261     /// Queries a configuration parameter value. Returns nullptr if the parameter is not
262     /// part of the current configuration
263     const C2Param *getConfigParameterValue(C2Param::Index index) const;
264 
265     /**
266      * Object that can be used to access configuration parameters and if they change.
267      */
268     template<typename T>
269     struct Watcher {
270         ~Watcher() = default;
271 
272         /// returns true if the value of this configuration has changed
hasChangedCCodecConfig::Watcher273         bool hasChanged() const {
274             const C2Param *value = mParent->getConfigParameterValue(mIndex);
275             if (value && mValue) {
276                 return *value != *mValue;
277             } else {
278                 return value != mValue.get();
279             }
280         }
281 
282         /// updates the current value and returns it
updateCCodecConfig::Watcher283         std::shared_ptr<const T> update() {
284             const C2Param *value = mParent->getConfigParameterValue(mIndex);
285             if (value) {
286                 mValue = std::shared_ptr<const T>(T::From(C2Param::Copy(*value).release()));
287             }
288             return mValue;
289         }
290 
291     private:
WatcherCCodecConfig::Watcher292         Watcher(C2Param::Index index, const CCodecConfig *parent)
293             : mParent(parent), mIndex(index) {
294             update();
295         }
296 
297         friend struct CCodecConfig;
298 
299         const CCodecConfig *mParent;
300         std::shared_ptr<const T> mValue;
301         C2Param::Index mIndex;
302     };
303 
304     /**
305      * Returns a watcher object for a parameter.
306      */
307     template<typename T>
308     Watcher<T> watch(C2Param::Index index = T::PARAM_TYPE) const {
309         if (index.type() != T::PARAM_TYPE) {
310             __builtin_trap();
311         }
312         return Watcher<T>(index, this);
313     }
314 
315 private:
316 
317     /// initializes the standard MediaCodec to Codec 2.0 params mapping
318     void initializeStandardParams();
319 
320     /// Adds indices to the subscribed indices, and updated subscription to component
321     /// \param blocking blocking mode to use with the component
322     status_t subscribeToConfigUpdate(
323             const std::shared_ptr<Codec2Client::Component> &component,
324             const std::vector<C2Param::Index> &indices,
325             c2_blocking_t blocking = C2_DONT_BLOCK);
326 
327     /// Gets SDK format from codec 2.0 reflected configuration
328     /// \param domain input/output bitmask
329     sp<AMessage> getSdkFormatForDomain(
330             const ReflectedParamUpdater::Dict &reflected, Domain domain) const;
331 
332     /**
333      * Converts a set of configuration parameters in an AMessage to a list of path-based Codec
334      * 2.0 configuration parameters.
335      *
336      * \param domain config/setParam bitmask
337      */
338     ReflectedParamUpdater::Dict getReflectedFormat(
339             const sp<AMessage> &config, Domain domain) const;
340 };
341 
342 DEFINE_ENUM_OPERATORS(CCodecConfig::Domain)
343 
344 }  // namespace android
345 
346 #endif  // C_CODEC_H_
347 
348