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