1 /* 2 * Copyright 2021, 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 _LIBMEDIAFORMATSHAPER_CODECPROPERTIES_H_ 18 #define _LIBMEDIAFORMATSHAPER_CODECPROPERTIES_H_ 19 20 #include <map> 21 #include <mutex> 22 #include <string> 23 24 #include <inttypes.h> 25 26 #include <utils/RefBase.h> 27 28 namespace android { 29 namespace mediaformatshaper { 30 31 class CodecProperties { 32 33 public: 34 CodecProperties(std::string name, std::string mediaType); 35 36 // seed the codec with some preconfigured values 37 // (e.g. mediaType-granularity defaults) 38 // runs from the constructor 39 void Seed(); 40 void Finish(); 41 42 std::string getName(); 43 std::string getMediaType(); 44 45 // establish a mapping from standard 'key' to non-standard 'value' in the namespace 'kind' 46 void setMapping(std::string kind, std::string key, std::string value); 47 48 // translate from from standard key to non-standard key 49 // return original standard key if there is no mapping 50 std::string getMapping(std::string key, std::string kind); 51 52 // returns an array of char *, which are paired "from" and "to" values 53 // for mapping (or unmapping). it's always expressed as from->to 54 // and 'reverse' describes which strings are to be on which side. 55 const char **getMappings(std::string kind, bool reverse); 56 57 // keep a map of all features and their parameters 58 void setFeatureValue(std::string key, int32_t value); 59 bool getFeatureValue(std::string key, int32_t *valuep); 60 61 // keep a map of all tunings and their parameters 62 void setTuningValue(std::string key, std::string value); 63 bool getTuningValue(std::string key, std::string &value); 64 65 // does the codec support the Android S minimum quality rules 66 void setSupportedMinimumQuality(int vmaf); 67 int supportedMinimumQuality(); 68 69 // qp max bound used to compensate when SupportedMinimumQuality == 0 70 // 0 == let a system default handle it 71 void setTargetQpMax(int qpmax); 72 int targetQpMax(int32_t width, int32_t height); 73 74 // target bits-per-pixel (per second) for encoding operations. 75 // This is used to calculate a minimum bitrate for any particular resolution. 76 // A 1080p (1920*1080 = 2073600 pixels) to be encoded at 5Mbps has a bpp == 2.41 setBpp(double bpp)77 void setBpp(double bpp) { mBpp = bpp;} 78 double getBpp(int32_t width, int32_t height); 79 80 // Does this codec support QP bounding 81 // The getMapping() methods provide any needed mapping to non-standard keys. setSupportsQp(bool supported)82 void setSupportsQp(bool supported) { mSupportsQp = supported;} supportsQp()83 bool supportsQp() { return mSupportsQp;} 84 85 // defines our range of operation -- multiplier on the floor bitrate getPhaseOut()86 double getPhaseOut() { return mPhaseOut; } 87 void setPhaseOut(double overageMultiplier); 88 89 // how much (0.20 = +20%) do we add if Qp is requested but unsupported getMissingQpBoost()90 double getMissingQpBoost() {return mMissingQpBoost; } 91 void setMissingQpBoost(double boost); 92 93 int supportedApi(); 94 95 // a codec is not usable until it has been registered with its 96 // name/mediaType. isRegistered()97 bool isRegistered() { return mIsRegistered;} setRegistered(bool registered)98 void setRegistered(bool registered) { mIsRegistered = registered;} 99 100 private: 101 std::string mName; 102 std::string mMediaType; 103 int mApi = 0; 104 int mMinimumQuality = 0; 105 int mTargetQpMax = INT32_MAX; 106 bool mSupportsQp = false; 107 double mBpp = 0.0; 108 109 // target bitrates above floor * mPhaseOut are left untouched 110 double mPhaseOut = 1.75; 111 // 20% bump if QP is configured but it is unavailable 112 double mMissingQpBoost = 0.20; 113 114 // allow different target bits-per-pixel based on resolution 115 // similar to codec 'performance points' 116 // uses 'next largest' (by pixel count) point as minimum bpp 117 struct bpp_point { 118 struct bpp_point *next; 119 int32_t pixels; 120 int32_t width, height; 121 double bpp; 122 }; 123 struct bpp_point *mBppPoints = nullptr; 124 bool bppPoint(std::string resolution, std::string value); 125 126 // same thing for qpmax -- allow different ones based on resolution 127 // allow different target bits-per-pixel based on resolution 128 // similar to codec 'performance points' 129 // uses 'next largest' (by pixel count) point as minimum bpp 130 struct qpmax_point { 131 struct qpmax_point *next; 132 int32_t pixels; 133 int32_t width, height; 134 int qpMax; 135 }; 136 struct qpmax_point *mQpMaxPoints = nullptr; 137 bool qpMaxPoint(std::string resolution, std::string value); 138 139 std::mutex mMappingLock; 140 // XXX figure out why I'm having problems getting compiler to like GUARDED_BY 141 std::map<std::string, std::string> mMappings /*GUARDED_BY(mMappingLock)*/ ; 142 143 std::map<std::string, int32_t> mFeatures /*GUARDED_BY(mMappingLock)*/ ; 144 std::map<std::string, std::string> mTunings /*GUARDED_BY(mMappingLock)*/ ; 145 146 // Seed() and Finish() use this as the underlying implementation 147 void addMediaDefaults(bool overrideable); 148 149 bool mIsRegistered = false; 150 151 // debugging of what's in the mapping dictionary 152 void showMappings(); 153 154 // DISALLOW_EVIL_CONSTRUCTORS(CodecProperties); 155 }; 156 157 extern CodecProperties *findCodec(const char *codecName, const char *mediaType); 158 extern CodecProperties *registerCodec(CodecProperties *codec, const char *codecName, 159 const char *mediaType); 160 161 162 } // namespace mediaformatshaper 163 } // namespace android 164 165 #endif // _LIBMEDIAFORMATSHAPER_CODECPROPERTIES_H_ 166