blob: 196a40f37bc1d29f424366d5dd68e78f646251ce [file] [log] [blame]
Ray Essick1831f7b2021-03-15 16:10:51 -07001/*
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
Ray Essick244aaa52021-04-05 14:47:02 -070024#include <inttypes.h>
25
Ray Essick1831f7b2021-03-15 16:10:51 -070026#include <utils/RefBase.h>
27
28namespace android {
29namespace mediaformatshaper {
30
31class CodecProperties {
32
33 public:
34 CodecProperties(std::string name, std::string mediaType);
35
Ray Essick970f1c82021-03-25 13:37:45 -070036 // 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
Ray Essick1831f7b2021-03-15 16:10:51 -070042 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
Ray Essick970f1c82021-03-25 13:37:45 -070057 // 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);
Ray Essick1831f7b2021-03-15 16:10:51 -070060
Ray Essicka727ef92021-03-29 13:35:02 -070061 // 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
Ray Essick1831f7b2021-03-15 16:10:51 -070065 // 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);
Ray Essick981ddf82021-05-03 13:33:09 -070072 int targetQpMax(int32_t width, int32_t height);
Ray Essick1831f7b2021-03-15 16:10:51 -070073
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
77 void setBpp(double bpp) { mBpp = bpp;}
Ray Essick244aaa52021-04-05 14:47:02 -070078 double getBpp(int32_t width, int32_t height);
Ray Essick1831f7b2021-03-15 16:10:51 -070079
80 // Does this codec support QP bounding
81 // The getMapping() methods provide any needed mapping to non-standard keys.
82 void setSupportsQp(bool supported) { mSupportsQp = supported;}
83 bool supportsQp() { return mSupportsQp;}
84
Ray Essick614d8da2021-04-27 11:35:00 -070085 // defines our range of operation -- multiplier on the floor bitrate
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
90 double getMissingQpBoost() {return mMissingQpBoost; }
91 void setMissingQpBoost(double boost);
92
Ray Essick1831f7b2021-03-15 16:10:51 -070093 int supportedApi();
94
95 // a codec is not usable until it has been registered with its
96 // name/mediaType.
97 bool isRegistered() { return mIsRegistered;}
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;
Ray Essick244aaa52021-04-05 14:47:02 -0700105 int mTargetQpMax = INT32_MAX;
Ray Essick1831f7b2021-03-15 16:10:51 -0700106 bool mSupportsQp = false;
107 double mBpp = 0.0;
108
Ray Essick614d8da2021-04-27 11:35:00 -0700109 // 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
Ray Essick244aaa52021-04-05 14:47:02 -0700114 // 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
Ray Essick981ddf82021-05-03 13:33:09 -0700126 // 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
Ray Essick1831f7b2021-03-15 16:10:51 -0700139 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)*/ ;
Ray Essick970f1c82021-03-25 13:37:45 -0700142
143 std::map<std::string, int32_t> mFeatures /*GUARDED_BY(mMappingLock)*/ ;
Ray Essicka727ef92021-03-29 13:35:02 -0700144 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);
Ray Essick1831f7b2021-03-15 16:10:51 -0700148
149 bool mIsRegistered = false;
150
Ray Essick970f1c82021-03-25 13:37:45 -0700151 // debugging of what's in the mapping dictionary
152 void showMappings();
153
Ray Essick1831f7b2021-03-15 16:10:51 -0700154 // DISALLOW_EVIL_CONSTRUCTORS(CodecProperties);
155};
156
157extern CodecProperties *findCodec(const char *codecName, const char *mediaType);
158extern 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_