1 /*
2 * Copyright (c) 2018 - 2020, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 //!
24 //! \file     media_feature_manager.h
25 //! \brief    Defines the common interface for encode feature manager
26 //! \details  The media feature manager is further sub-divided by codec type
27 //!           this file is for the base interface which is shared by all components.
28 //!
29 
30 #ifndef __MEDIA_FEATURE_MANAGER_H__
31 #define __MEDIA_FEATURE_MANAGER_H__
32 #include <vector>
33 #include <stdint.h>
34 #include <map>
35 #include <memory>
36 #include <utility>
37 #include "media_user_setting.h"
38 #include "media_utils.h"
39 #include "mos_defs.h"
40 #include "media_feature_const_settings.h"
41 
42 #define CONSTRUCTFEATUREID(_componentID, _subComponentID, _featureID) \
43     (_componentID << 24 | _subComponentID << 16 | _featureID)
44 
45 enum ComponentIDs
46 {
47     FEATURE_COMPONENT_COMMON = 0,
48     FEATURE_COMPONENT_ENCODE,
49     FEATURE_COMPONENT_DECODE,
50     FEATURE_COMPONENT_VP,
51     FEATURE_COMPONENT_OTHER
52 };
53 
54 enum SubComponentIDs
55 {
56     FEATURE_SUBCOMPONENT_COMMON = 0,
57     FEATURE_SUBCOMPONENT_HEVC,
58     FEATURE_SUBCOMPONENT_VP9,
59     FEATURE_SUBCOMPONENT_AVC,
60     FEATURE_SUBCOMPONENT_AV1,
61     FEATURE_SUBCOMPONENT_JPEG
62 };
63 
64 struct FeatureIDs
65 {
66     enum CommonFeatureIDs
67     {
68         basicFeature = CONSTRUCTFEATUREID(FEATURE_COMPONENT_COMMON, FEATURE_SUBCOMPONENT_COMMON, 0),
69         encodeTile,
70         preEncFeature,
71     };
72 };
73 //!
74 //! \def RUN_FEATURE_INTERFACE_RETURN(_featureClassName, _featureID, _featureInterface, ...)
75 //!  Run _featureInterface if it exit
76 //!
77 #define RUN_FEATURE_INTERFACE_RETURN(_featureClassName, _featureID, _featureInterface, ...)                \
78 {                                                                                                   \
79     if(m_featureManager){                                                                           \
80         auto feature = static_cast<_featureClassName*>(m_featureManager->GetFeature(_featureID));   \
81         if (feature)                                                                                \
82         {                                                                                           \
83             MEDIA_CHK_STATUS_RETURN(feature->_featureInterface(__VA_ARGS__));                       \
84         }                                                                                           \
85     }                                                                                               \
86 }
87 
88 //!
89 //! \def RUN_FEATURE_INTERFACE_NO_RETURN(_featureClassName, _featureID, _featureInterface, ...)
90 //!  Run _featureInterface if it exit
91 //!
92 #define RUN_FEATURE_INTERFACE_NO_RETURN(_featureClassName, _featureID, _featureInterface, ...)                \
93 {                                                                                                   \
94     if(m_featureManager){                                                                           \
95         auto feature = static_cast<_featureClassName*>(m_featureManager->GetFeature(_featureID));   \
96         if (feature)                                                                                \
97         {                                                                                           \
98             feature->_featureInterface(__VA_ARGS__);                              \
99         }                                                                                           \
100     }                                                                                               \
101 }
102 
103 class MediaFeature;
104 
105 enum class LIST_TYPE
106 {
107     BLOCK_LIST,
108     ALLOW_LIST,
109 };
110 
111 class MediaFeatureManager  // for pipe line use
112 {
113 protected:
114     using container_t = std::map<int, MediaFeature *>;
115 
116 public:
117     class ManagerLite final  // for packet use
118     {
119         friend class MediaFeatureManager;
120 
121     public:
122         class iterator : public container_t::iterator
123         {
124         public:
iterator(container_t::iterator it)125             explicit iterator(container_t::iterator it) : container_t::iterator(it) {}
126 
127             container_t::mapped_type operator*() { return (*this)->second; }
128         };
129 
130         ManagerLite() = default;
131 
begin()132         iterator begin() { return iterator(m_features.begin()); }
133 
end()134         iterator end() { return iterator(m_features.end()); }
135 
GetFeature(int featureID)136         MediaFeature *GetFeature(int featureID)
137         {
138             auto iter = m_features.find(featureID);
139             if (iter == m_features.end())
140             {
141                 return nullptr;
142             }
143             return iter->second;
144         }
145 
146     private:
147         container_t m_features;
148     };
149 
150 public:
151     class iterator : public container_t::iterator
152     {
153     public:
iterator(container_t::iterator it)154         explicit iterator(container_t::iterator it) : container_t::iterator(it) {}
155 
156         container_t::mapped_type operator*() { return (*this)->second; }
157     };
158 
159     //!
160     //! \brief  MediaFeatureManager constructor
161     //!
MediaFeatureManager()162     MediaFeatureManager(){};
163 
164     //!
165     //! \brief  MediaFeatureManager deconstructor
166     //!
~MediaFeatureManager()167     virtual ~MediaFeatureManager() { Destroy(); }
168 
begin()169     iterator begin() { return iterator(m_features.begin()); }
170 
end()171     iterator end() { return iterator(m_features.end()); }
172 
173     //!
174     //! \brief  Initialize all features
175     //! \param  [in] settings
176     //!         Pointer to the initialize settings
177     //! \return MOS_STATUS
178     //!         MOS_STATUS_SUCCESS if success, else fail reason
179     //!
Init(void * settings)180     virtual MOS_STATUS Init(void *settings) { return MOS_STATUS_UNIMPLEMENTED; }
181 
182     //!
183     //! \brief  Register features, if last two parameters use
184     //!         default values, feature to be registered will
185     //!         not be blocked by any packet
186     //! \param  [in] featureID
187     //!         ID of the feature to be reigstered
188     //! \param  [in] feature
189     //!         Pointer to the feature to be registered
190     //! \param  [in] packetIds
191     //!         Packet ID list, if it is an allow list, feature will
192     //!         be only added to packets in the list, otherwise feature
193     //!         will be added to packets not in the list, by default it
194     //!         is an empty list
195     //! \param  [in] packetIdListType
196     //!         Indicate whether packet ID list is a block list
197     //!         or an allow list, by default it is a block list.
198     //! \return MOS_STATUS
199     //!         MOS_STATUS_SUCCESS if success, else fail reason
200     //!
201     MOS_STATUS RegisterFeatures(
202         int                featureID,
203         MediaFeature *     feature,
204         std::vector<int> &&packetIds        = {},
205         LIST_TYPE          packetIdListType = LIST_TYPE::BLOCK_LIST);
206 
207     //!
208     //! \brief  Get packet level feature manager
209     //! \param  [in] packetId
210     //!         ID of packet
211     //! \return std::shared_ptr<ManagerLite>
212     //!         A pointer to packet level feature manager
213     //!
214     std::shared_ptr<ManagerLite> GetPacketLevelFeatureManager(int packetId);
215 
216     //!
217     //! \brief  Update all features
218     //! \param  [in] params
219     //!         Pointer to the params
220     //! \return MOS_STATUS
221     //!         MOS_STATUS_SUCCESS if success, else fail reason
222     //!
223     MOS_STATUS Update(void *params);
224 
225     //!
226     //! \brief  Destroy all features
227     //! \return MOS_STATUS
228     //!         MOS_STATUS_SUCCESS if success, else fail reason
229     //!
230     MOS_STATUS Destroy();
231 
232     //!
233     //! \brief  Get feature
234     //! \param  [in] featureID
235     //!         feature ID to get the feature
236     //! \return MediaFeature*
237     //!         Pointer of the feature
238     //!
GetFeature(int featureID)239     virtual MediaFeature *GetFeature(int featureID)
240     {
241         auto iter = m_features.find(featureID);
242         if (iter == m_features.end())
243         {
244             return nullptr;
245         }
246         return iter->second;
247     }
248     //!
249     //! \brief  Get Pass Number
250     //! \return uint8_t
251     //!         actual pass number after feature check
252     //!
GetNumPass()253     uint8_t GetNumPass() { return m_passNum; };
GetFeatureSettings()254     MediaFeatureConstSettings *GetFeatureSettings() { return m_featureConstSettings; };
255     //!
256     //! \brief  Check the conflict between features
257     //! \param  [in] params
258     //!         encode parameters
259     //! \return MOS_STATUS
260     //!         MOS_STATUS_SUCCESS if success, else fail reason
261     //!
CheckFeatures(void * params)262     virtual MOS_STATUS CheckFeatures(void *params) { return MOS_STATUS_SUCCESS; };
263 
264     //!
265     //! \brief  Get DDI Target Usage
266     //! \return uint8_t
267     //!         return the DDI target usage.
268     //!
GetDDITargetUsage()269     uint8_t GetDDITargetUsage(){return m_ddiTargetUsage;}
270 
271 protected:
272 
273     //!
274     //! \brief  Create feature const settings
275     //! \return MOS_STATUS
276     //!         MOS_STATUS_SUCCESS if success, else fail reason
277     //!
CreateConstSettings()278     virtual MOS_STATUS CreateConstSettings() { return MOS_STATUS_SUCCESS; };
279 
280     //!
281     //! \brief  Create features
282     //! \param  [in] constsettings
283     //!         feature const settings
284     //! \return MOS_STATUS
285     //!         MOS_STATUS_SUCCESS if success, else fail reason
286     //!
CreateFeatures(void * constSettings)287     virtual MOS_STATUS CreateFeatures(void *constSettings) { return MOS_STATUS_SUCCESS; };
288 
289     //!
290     //! \brief  Get Target Usage
291     //! \return uint8_t
292     //!         return the target usage.
293     //!
GetTargetUsage()294     uint8_t GetTargetUsage(){return m_targetUsage;}
295 
296     container_t m_features;
297     std::map<int, std::vector<int>> m_packetIdList;  // map feature ID to a vector of packet ID
298     std::map<int, LIST_TYPE> m_packetIdListTypes;  // map feature ID to a flag, indicates whether packet ID vector is a block list or an allow list
299     MediaFeatureConstSettings *m_featureConstSettings = nullptr;
300     uint8_t m_ddiTargetUsage = 0; // for user input setting report
301     uint8_t m_targetUsage = 0;
302     uint8_t m_passNum = 1;
303     // Media user setting instance
304     MediaUserSettingSharedPtr m_userSettingPtr = nullptr;
305 MEDIA_CLASS_DEFINE_END(MediaFeatureManager)
306 };
307 
308 #endif  // !__MEDIA_FEATURE_MANAGER_H__
309