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