1 /*-------------------------------------------------------------------------- 2 Copyright (c) 2017, The Linux Foundation. All rights reserved. 3 4 Redistribution and use in source and binary forms, with or without 5 modification, are permitted provided that the following conditions are 6 met: 7 * Redistributions of source code must retain the above copyright 8 notice, this list of conditions and the following disclaimer. 9 * Redistributions in binary form must reproduce the above 10 copyright notice, this list of conditions and the following 11 disclaimer in the documentation and/or other materials provided 12 with the distribution. 13 * Neither the name of The Linux Foundation nor the names of its 14 contributors may be used to endorse or promote products derived 15 from this software without specific prior written permission. 16 17 THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED 18 WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT 20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 21 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 24 BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 25 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 26 OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 27 IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 --------------------------------------------------------------------------*/ 29 30 #ifndef _VIDC_VENDOR_ENXTENSIONS_H_ 31 #define _VIDC_VENDOR_ENXTENSIONS_H_ 32 33 #include <inttypes.h> 34 #include <string.h> 35 #include <string> 36 #include <vector> 37 38 /* 39 * This class represents a Vendor-Extension (except for the data). 40 * A Vendor extension is identified by a unique extension-name and 41 * is mapped to a specific OMX-extension. it contains params that 42 * signify individual parameter-field 43 * VendorExtension::mName => similar to OMX extension string. 44 * (Name must be unique) 45 * VendorExtension::mId => similar to OMX extension ID 46 * VendorExtension::mParam[0,1..] => similar to an individual field 47 * in OMX extension struct 48 * VendorExtension::mIsSet => flag that indicates whether this 49 * extension was set by the client. 50 * This also provides utility methods to: 51 * - copy info(keys/types..) to client's extension strcuture 52 * including copying of param-key and type of each param 53 * - copy data from/to the client's extension structure, given the 54 * param-key (this is type-aware copy) 55 * - sanity checks 56 * 57 * Extension name - naming convention 58 * - name must be unique 59 * - must be prefixed with "ext-" followed by component-type 60 * Eg: "enc" "dec" "vpp" 61 * - SHOULD NOT contain "." 62 * - keywords SHOULD be separated by "-" 63 * - name may contain feature-name and/or parameter-name 64 * Eg: "ext-enc-preprocess-rotate" 65 * "ext-dec-picture-order" 66 * 67 * Overall paramter-key => vendor (dot) extension-name (dot) param-key 68 */ 69 struct VendorExtension { 70 71 /* 72 * Param represents an individual parameter (field) of a VendorExtension. 73 * This is a variant holding values of type [int32, int64 or String]. 74 * Each Param has a name (unique within the extension) that is appended 75 * to the 'extension-name' and prefixed with "vendor." to generate the 76 * key that will be exposed to the client. 77 * 78 * Param name(key) - naming convention 79 * - key must be unique (within the extension) 80 * - SHOULD not contain "." 81 * - Keywords seperated by "-" ONLY if required 82 * Eg: "angle" 83 * "n-idr-period" 84 * 85 */ 86 struct Param { 87 Param (const std::string &name, OMX_ANDROID_VENDOR_VALUETYPE type) 88 : mName(name), mType(type) {} 89 90 const char *name() const { 91 return mName.c_str(); 92 } 93 OMX_ANDROID_VENDOR_VALUETYPE type() const { 94 return mType; 95 } 96 private: 97 std::string mName; 98 OMX_ANDROID_VENDOR_VALUETYPE mType; 99 }; 100 101 // helper to build a list of variable number or params 102 struct ParamListBuilder { 103 ParamListBuilder (std::initializer_list<Param> l) 104 : mParams(l) {} 105 private: 106 friend struct VendorExtension; 107 std::vector<Param> mParams; 108 }; 109 110 VendorExtension(OMX_INDEXTYPE id, const char *name, OMX_DIRTYPE dir, 111 const ParamListBuilder& p); 112 113 // getters 114 OMX_INDEXTYPE extensionIndex() const { 115 return (OMX_INDEXTYPE)mId; 116 } 117 const char *name() const { 118 return mName.c_str(); 119 } 120 OMX_U32 paramCount() const { 121 return (OMX_U32)mParams.size(); 122 } 123 bool isSet() const { 124 return mIsSet; 125 } 126 127 // (the only) setter 128 void set() const { 129 mIsSet = true; 130 } 131 132 // copy extension Info to OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE* struct passed (except data) 133 OMX_ERRORTYPE copyInfoTo(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext) const; 134 135 // Type-aware data copy methods 136 // (NOTE: data here is passed explicitly to avoid this class having to know all types) 137 // returns true if value was written 138 bool setParamInt32(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey, 139 OMX_S32 setInt32) const; 140 bool setParamInt64(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey, 141 OMX_S32 setInt64) const; 142 bool setParamString(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey, 143 const char *setStr) const; 144 145 // read-values are updated ONLY IF the param[paramIndex] is set by client 146 // returns true if value was read 147 bool readParamInt32(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey, 148 OMX_S32 *readInt32) const; 149 bool readParamInt64(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey, 150 OMX_S32 *readInt64) const; 151 bool readParamInt64(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, const char *paramKey, 152 char *readStr) const; 153 154 // Sanity checkers 155 // Check if the extension-name, port-dir, allotted params match 156 // for each param, check if key and type both match 157 // Must be called to check whether config data provided with setConfig is valid 158 OMX_ERRORTYPE isConfigValid(OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext) const; 159 160 // utils 161 static const char* typeString(OMX_ANDROID_VENDOR_VALUETYPE type); 162 std::string debugString() const; 163 164 private: 165 // Id assigned to the extension 166 OMX_INDEXTYPE mId; 167 // Name of the extension 168 std::string mName; 169 // Port that this setting applies to 170 OMX_DIRTYPE mPortDir; 171 // parameters required for this extension 172 std::vector<Param> mParams; 173 // Flag that indicates client has set this extension. 174 mutable bool mIsSet; 175 176 // check if the index is valid, name matches, type matches and is set 177 // This must be called to verify config-data passed with setConfig() 178 bool _isParamAccessOK( 179 OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, int paramIndex) const; 180 181 // check if the index is valid, check against explicit type 182 bool _isParamAccessTypeOK( 183 OMX_CONFIG_ANDROID_VENDOR_EXTENSIONTYPE *ext, int paramIndex, 184 OMX_ANDROID_VENDOR_VALUETYPE type) const; 185 186 int indexOfParam(const char *key) const; 187 }; 188 189 /* 190 * Store(List) of all vendor extensions *that are supported* by a component. 191 * The list is populated (per-component) at init, based on the capabilities. 192 * The store is immutable once created, except for setting the flag to indicate 193 * -whether the extension was set by the Client 194 */ 195 struct VendorExtensionStore { 196 VendorExtensionStore() 197 : mInvalid(VendorExtension((OMX_INDEXTYPE)-1, "invalid", OMX_DirMax, {{}})) { 198 } 199 200 VendorExtensionStore(const VendorExtensionStore&) = delete; 201 VendorExtensionStore& operator= (const VendorExtensionStore&) = delete; 202 203 void add(const VendorExtension& _e) { 204 mExt.push_back(_e); 205 } 206 const VendorExtension& operator[] (OMX_U32 index) const { 207 return index < mExt.size() ? mExt[index] : mInvalid; 208 } 209 OMX_U32 size() const { 210 return mExt.size(); 211 } 212 void dumpExtensions(const char *prefix) const; 213 214 private: 215 std::vector<VendorExtension> mExt; 216 VendorExtension mInvalid; 217 }; 218 219 // Macros to help add extensions 220 #define ADD_EXTENSION(_name, _extIndex, _dir) \ 221 store.add(VendorExtension((OMX_INDEXTYPE)_extIndex, _name, _dir, { \ 222 223 #define ADD_PARAM(_key, _type) \ 224 {_key, _type}, 225 226 #define ADD_PARAM_END(_key, _type) \ 227 {_key, _type} })); 228 229 #endif // _VIDC_VENDOR_ENXTENSIONS_H_ 230