• 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 #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     CCodecConfig();
138 
139     /// initializes the members required to manage the format: descriptors, reflector,
140     /// reflected param helper, domain, standard params, and subscribes to standard
141     /// indices.
142     status_t initialize(
143             const std::shared_ptr<Codec2Client> &client,
144             const std::shared_ptr<Codec2Client::Component> &component);
145 
146 
147     /**
148      * Adds a locally maintained parameter. This is used for output configuration that can be
149      * appended to the output buffers in case it is not supported by the component.
150      */
151     template<typename T>
152     bool addLocalParam(
153             const std::string &name,
154             C2ParamDescriptor::attrib_t attrib = C2ParamDescriptor::IS_READ_ONLY,
155             std::function<c2_status_t(std::unique_ptr<T>&)> validator_ =
156                 std::function<c2_status_t(std::unique_ptr<T>&)>()) {
157         C2Param::Index index = T::PARAM_TYPE;
158         if (mSupportedIndices.count(index) || mLocalParams.count(index)) {
159             if (mSupportedIndices.count(index)) {
160                 mSubscribedIndices.emplace(index);
161             }
162             ALOGD("ignoring local param %s (%#x) as it is already %s",
163                     name.c_str(), (uint32_t)index, mSupportedIndices.count(index) ? "supported" : "local");
164             return false; // already supported by the component or already added
165         }
166 
167         // wrap typed validator into untyped validator
168         LocalParamValidator validator;
169         if (validator_) {
170             validator = [validator_](std::unique_ptr<C2Param>& p){
171                 c2_status_t res = C2_BAD_VALUE;
172                 std::unique_ptr<T> typed(static_cast<T*>(p.release()));
173                 // if parameter is correctly typed
174                 if (T::From(typed.get())) {
175                     res = validator_(typed);
176                     p.reset(typed.release());
177                 }
178                 return res;
179             };
180         }
181 
182         mLocalParams.emplace(index, validator);
183         mParamUpdater->addStandardParam<T>(name, attrib);
184         return true;
185     }
186 
187     /**
188      * Adds a locally maintained parameter with a default value.
189      */
190     template<typename T>
191     bool addLocalParam(
192             std::unique_ptr<T> default_,
193             const std::string &name,
194             C2ParamDescriptor::attrib_t attrib = C2ParamDescriptor::IS_READ_ONLY,
195             std::function<c2_status_t(std::unique_ptr<T>&)> validator_ =
196                 std::function<c2_status_t(std::unique_ptr<T>&)>()) {
197         if (addLocalParam<T>(name, attrib, validator_)) {
198             if (validator_) {
199                 c2_status_t err = validator_(default_);
200                 if (err != C2_OK) {
201                     ALOGD("default value for %s is invalid => %s", name.c_str(), asString(err));
202                     return false;
203                 }
204             }
205             mCurrentConfig[T::PARAM_TYPE] = std::move(default_);
206             return true;
207         }
208         return false;
209     }
210 
211     template<typename T>
212     bool addLocalParam(
213             T *default_, const std::string &name,
214             C2ParamDescriptor::attrib_t attrib = C2ParamDescriptor::IS_READ_ONLY,
215             std::function<c2_status_t(std::unique_ptr<T>&)> validator_ =
216                 std::function<c2_status_t(std::unique_ptr<T>&)>()) {
217         return addLocalParam(std::unique_ptr<T>(default_), name, attrib, validator_);
218     }
219 
220     /// Applies configuration updates, and updates format in the specific domain.
221     /// Returns true if formats were updated
222     /// \param domain input/output bitmask
223     bool updateConfiguration(
224             std::vector<std::unique_ptr<C2Param>> &configUpdate, Domain domain);
225 
226     /// Updates formats in the specific domain. Returns true if any of the formats have changed.
227     /// \param domain input/output bitmask
228     bool updateFormats(Domain domain);
229 
230     /**
231      * Applies SDK configurations in a specific configuration domain.
232      * Updates relevant input/output formats and subscribes to parameters specified in the
233      * configuration.
234      * \param domain config/setParam bitmask
235      * \param blocking blocking mode to use with the component
236      */
237     status_t getConfigUpdateFromSdkParams(
238             std::shared_ptr<Codec2Client::Component> component,
239             const sp<AMessage> &sdkParams, Domain domain,
240             c2_blocking_t blocking,
241             std::vector<std::unique_ptr<C2Param>> *configUpdate) const;
242 
243     /**
244      * Applies a configuration update to the component.
245      * Updates relevant input/output formats and subscribes to parameters specified in the
246      * configuration.
247      * \param blocking blocking mode to use with the component
248      */
249     status_t setParameters(
250             std::shared_ptr<Codec2Client::Component> component,
251             std::vector<std::unique_ptr<C2Param>> &configUpdate,
252             c2_blocking_t blocking);
253 
254     /// Queries subscribed indices (which contains all SDK-exposed values) and updates
255     /// input/output formats.
256     status_t queryConfiguration(
257             const std::shared_ptr<Codec2Client::Component> &component);
258 
259     /// Queries a configuration parameter value. Returns nullptr if the parameter is not
260     /// part of the current configuration
261     const C2Param *getConfigParameterValue(C2Param::Index index) const;
262 
263     /**
264      * Object that can be used to access configuration parameters and if they change.
265      */
266     template<typename T>
267     struct Watcher {
268         ~Watcher() = default;
269 
270         /// returns true if the value of this configuration has changed
hasChangedCCodecConfig::Watcher271         bool hasChanged() const {
272             const C2Param *value = mParent->getConfigParameterValue(mIndex);
273             if (value && mValue) {
274                 return *value != *mValue;
275             } else {
276                 return value != mValue.get();
277             }
278         }
279 
280         /// updates the current value and returns it
updateCCodecConfig::Watcher281         std::shared_ptr<const T> update() {
282             const C2Param *value = mParent->getConfigParameterValue(mIndex);
283             if (value) {
284                 mValue = std::shared_ptr<const T>(T::From(C2Param::Copy(*value).release()));
285             }
286             return mValue;
287         }
288 
289     private:
WatcherCCodecConfig::Watcher290         Watcher(C2Param::Index index, const CCodecConfig *parent)
291             : mParent(parent), mIndex(index) {
292             update();
293         }
294 
295         friend struct CCodecConfig;
296 
297         const CCodecConfig *mParent;
298         std::shared_ptr<const T> mValue;
299         C2Param::Index mIndex;
300     };
301 
302     /**
303      * Returns a watcher object for a parameter.
304      */
305     template<typename T>
306     Watcher<T> watch(C2Param::Index index = T::PARAM_TYPE) const {
307         if (index.type() != T::PARAM_TYPE) {
308             __builtin_trap();
309         }
310         return Watcher<T>(index, this);
311     }
312 
313 private:
314 
315     /// initializes the standard MediaCodec to Codec 2.0 params mapping
316     void initializeStandardParams();
317 
318     /// Adds indices to the subscribed indices, and updated subscription to component
319     /// \param blocking blocking mode to use with the component
320     status_t subscribeToConfigUpdate(
321             const std::shared_ptr<Codec2Client::Component> &component,
322             const std::vector<C2Param::Index> &indices,
323             c2_blocking_t blocking = C2_DONT_BLOCK);
324 
325     /// Gets SDK format from codec 2.0 reflected configuration
326     /// \param domain input/output bitmask
327     sp<AMessage> getSdkFormatForDomain(
328             const ReflectedParamUpdater::Dict &reflected, Domain domain) const;
329 
330     /**
331      * Converts a set of configuration parameters in an AMessage to a list of path-based Codec
332      * 2.0 configuration parameters.
333      *
334      * \param domain config/setParam bitmask
335      */
336     ReflectedParamUpdater::Dict getReflectedFormat(
337             const sp<AMessage> &config, Domain domain) const;
338 };
339 
340 DEFINE_ENUM_OPERATORS(CCodecConfig::Domain)
341 
342 }  // namespace android
343 
344 #endif  // C_CODEC_H_
345 
346