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