1 /*
2 * Copyright (c) 2019-2022, 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     encode_vp9_vdenc_feature_manager.cpp
25 //! \brief    Defines the common interface for vp9 vdenc feature manager
26 //! \details  The encode 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 #include "encode_vp9_vdenc_feature_manager.h"
31 #include "encode_vp9_vdenc_const_settings.h"
32 #include "codec_def_encode.h"
33 #include "encode_vp9_hpu.h"
34 #include "encode_vp9_brc.h"
35 #include "encode_vp9_cqp.h"
36 #include "encode_vp9_segmentation.h"
37 #include "encode_vp9_pak.h"
38 
39 namespace encode
40 {
CheckFeatures(void * params)41 MOS_STATUS EncodeVp9VdencFeatureManager::CheckFeatures(void *params)
42 {
43     ENCODE_FUNC_CALL();
44     EncoderParams *encodeParams = (EncoderParams *)params;
45 
46     auto seqParams =
47         static_cast<PCODEC_VP9_ENCODE_SEQUENCE_PARAMS>(encodeParams->pSeqParams);
48     ENCODE_CHK_NULL_RETURN(seqParams);
49     auto picParams =
50         static_cast<PCODEC_VP9_ENCODE_PIC_PARAMS>(encodeParams->pPicParams);
51     ENCODE_CHK_NULL_RETURN(picParams);
52     auto segmentParams =
53         static_cast<PCODEC_VP9_ENCODE_SEGMENT_PARAMS>(encodeParams->pSegmentParams);
54 
55     auto settings = static_cast<EncodeVp9VdencConstSettings *>(m_featureConstSettings);
56     ENCODE_CHK_NULL_RETURN(settings);
57     ENCODE_CHK_STATUS_RETURN(settings->Update(params));
58 
59     if (encodeParams->bNewSeq)
60     {
61         // Store original target usage pass from app before it got modify.
62         // CalculateRePakThresholds() is use original target usage to calculate
63         // rePAK thresholds [See VP9 Codechal]
64         auto basicFeature = static_cast<Vp9BasicFeature *>(GetFeature(Vp9FeatureIDs::basicFeature));
65         ENCODE_CHK_NULL_RETURN(basicFeature);
66         basicFeature->m_oriTargetUsage = seqParams->TargetUsage;
67 
68         m_ddiTargetUsage = seqParams->TargetUsage;
69         ENCODE_CHK_STATUS_RETURN(MapTargetUsage(seqParams->TargetUsage));
70         m_targetUsage = seqParams->TargetUsage;
71     }
72 
73     ENCODE_CHK_STATUS_RETURN(ValidatePassNum(seqParams, picParams));
74 
75     return MOS_STATUS_SUCCESS;
76 }
77 
CreateConstSettings()78 MOS_STATUS EncodeVp9VdencFeatureManager::CreateConstSettings()
79 {
80     ENCODE_FUNC_CALL();
81     m_featureConstSettings = MOS_New(EncodeVp9VdencConstSettings);
82     return MOS_STATUS_SUCCESS;
83 }
84 
CreateFeatures(void * constSettings)85 MOS_STATUS EncodeVp9VdencFeatureManager::CreateFeatures(void *constSettings)
86 {
87     ENCODE_FUNC_CALL();
88 
89     EncodeBasicFeature *basicFeature = MOS_New(Vp9BasicFeature, m_allocator, m_hwInterface, m_trackedBuf, m_recycleResource, constSettings);
90     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::basicFeature, basicFeature));
91 
92     Vp9EncodeHpu *hpuFeature = MOS_New(Vp9EncodeHpu, this, m_allocator, m_hwInterface, constSettings);
93     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::vp9HpuFeature, hpuFeature));
94 
95     Vp9EncodeCqp *cqpFeature = MOS_New(Vp9EncodeCqp, this, m_allocator, m_hwInterface, constSettings);
96     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::vp9CqpFeature, cqpFeature));
97 
98     Vp9EncodeTile *tileFeature = MOS_New(Vp9EncodeTile, this, m_allocator, m_hwInterface, constSettings);
99     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::encodeTile, tileFeature));
100 
101     Vp9EncodeBrc *brcFeature = MOS_New(Vp9EncodeBrc, this, m_allocator, m_hwInterface, constSettings);
102     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::vp9BrcFeature, brcFeature));
103 
104     Vp9Segmentation *segmentFeature = MOS_New(Vp9Segmentation, this, m_allocator, m_hwInterface, constSettings);
105     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::vp9Segmentation, segmentFeature));
106 
107     Vp9EncodePak *pakFeature = MOS_New(Vp9EncodePak, this, m_allocator, m_hwInterface, constSettings);
108     ENCODE_CHK_STATUS_RETURN(RegisterFeatures(Vp9FeatureIDs::vp9PakFeature, pakFeature));
109 
110     return MOS_STATUS_SUCCESS;
111 }
112 
MapTargetUsage(uint8_t & targetUsage)113 MOS_STATUS EncodeVp9VdencFeatureManager::MapTargetUsage(uint8_t &targetUsage)
114 {
115     ENCODE_FUNC_CALL();
116 
117     switch (targetUsage)
118     {
119     case 1:
120     case 2:
121         targetUsage = TargetUsage::QUALITY_2;
122         break;
123     case 3:
124     case 4:
125     case 5:
126         targetUsage = TargetUsage::NORMAL;
127         break;
128     case 6:
129     case 7:
130         targetUsage = TargetUsage::PERFORMANCE;
131         break;
132     default:
133         targetUsage = TargetUsage::NORMAL;
134         break;
135     }
136     return MOS_STATUS_SUCCESS;
137 }
138 
ValidatePassNum(PCODEC_VP9_ENCODE_SEQUENCE_PARAMS vp9SeqParams,PCODEC_VP9_ENCODE_PIC_PARAMS vp9PicParams)139 MOS_STATUS EncodeVp9VdencFeatureManager::ValidatePassNum(PCODEC_VP9_ENCODE_SEQUENCE_PARAMS vp9SeqParams, PCODEC_VP9_ENCODE_PIC_PARAMS vp9PicParams)
140 {
141     ENCODE_FUNC_CALL();
142 
143     auto basicFeature = static_cast<Vp9BasicFeature *>(GetFeature(Vp9FeatureIDs::basicFeature));
144     ENCODE_CHK_NULL_RETURN(basicFeature);
145     auto brcFeature = static_cast<Vp9EncodeBrc *>(GetFeature(Vp9FeatureIDs::vp9BrcFeature));
146     ENCODE_CHK_NULL_RETURN(brcFeature);
147 
148     bool brcEnabled = CodecHalIsRateControlBrc(vp9SeqParams->RateControlMethod, CODECHAL_VP9);
149 
150     m_passNum = basicFeature->m_hucEnabled ? CODECHAL_ENCODE_VP9_CQP_NUM_OF_PASSES : 1;
151     if (brcEnabled)
152     {
153         m_passNum = brcFeature->IsMultipassBrcSupported()
154                         ? (CODECHAL_ENCODE_VP9_BRC_DEFAULT_NUM_OF_PASSES + 1)
155                         : CODECHAL_ENCODE_VP9_BRC_DEFAULT_NUM_OF_PASSES;
156     }
157 
158     return MOS_STATUS_SUCCESS;
159 }
160 
161 }  // namespace encode
162