1 /*
2 * Copyright (c) 2020-2021, 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 //! \file     encode_avc_rounding.cpp
24 //! \brief    Defines the common interface for avc encode rounding feature
25 //!
26 
27 #include "encode_avc_rounding.h"
28 #include "encode_avc_brc.h"
29 #include "encode_avc_vdenc_feature_manager.h"
30 #include "encode_avc_vdenc_const_settings.h"
31 #include "media_avc_feature_defs.h"
32 
33 namespace encode
34 {
35 
AvcEncodeRounding(MediaFeatureManager * featureManager,EncodeAllocator * allocator,CodechalHwInterfaceNext * hwInterface,void * constSettings)36 AvcEncodeRounding::AvcEncodeRounding(
37     MediaFeatureManager *featureManager,
38     EncodeAllocator *allocator,
39     CodechalHwInterfaceNext *hwInterface,
40     void *constSettings) :
41     MediaFeature(constSettings)
42 {
43     m_featureManager = featureManager;
44     auto encFeatureManager = dynamic_cast<EncodeAvcVdencFeatureManager *>(featureManager);
45     ENCODE_CHK_NULL_NO_STATUS_RETURN(encFeatureManager);
46 
47     m_basicFeature = dynamic_cast<AvcBasicFeature *>(encFeatureManager->GetFeature(FeatureIDs::basicFeature));
48     ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature);
49 }
50 
Update(void * params)51 MOS_STATUS AvcEncodeRounding::Update(void *params)
52 {
53     ENCODE_FUNC_CALL();
54 
55     ENCODE_CHK_NULL_RETURN(params);
56     EncoderParams* encodeParams = (EncoderParams*)params;
57     m_roundingParams = (PCODECHAL_ENCODE_AVC_ROUNDING_PARAMS)encodeParams->pAVCRoundingParams;
58 
59     return MOS_STATUS_SUCCESS;
60 }
61 
GetRounding(MHW_VDBOX_AVC_SLICE_STATE & sliceState) const62 MOS_STATUS AvcEncodeRounding::GetRounding(MHW_VDBOX_AVC_SLICE_STATE &sliceState) const
63 {
64     ENCODE_FUNC_CALL();
65 
66     auto    seqParams   = m_basicFeature->m_seqParam;
67     auto    picParams   = m_basicFeature->m_picParam;
68     auto    sliceParams = &m_basicFeature->m_sliceParams[m_basicFeature->m_curNumSlices];
69     uint8_t sliceQP     = picParams->pic_init_qp_minus26 + 26 + sliceParams->slice_qp_delta;
70 
71     auto settings = static_cast<AvcVdencFeatureSettings *>(m_constSettings);
72     ENCODE_CHK_NULL_RETURN(settings);
73 
74     auto brcFeature = dynamic_cast<AvcEncodeBRC*>(m_featureManager->GetFeature(AvcFeatureIDs::avcBrcFeature));
75     ENCODE_CHK_NULL_RETURN(brcFeature);
76 
77     sliceState.dwRoundingIntraValue = settings->DefaultIntraRounding;
78 
79     if (Slice_Type[sliceParams->slice_type] != SLICE_I)
80     {
81         bool isRef   = m_basicFeature->m_ref->GetRefList()[m_basicFeature->m_currReconstructedPic.FrameIdx]->bUsedAsRef;
82         bool isB     = (Slice_Type[sliceParams->slice_type] == SLICE_B);
83         bool isIPGOP = (seqParams->GopRefDist == 1);
84 
85         auto index   = (isB) ? (isRef ? 1 : 0) : (isIPGOP ? 3 : 2);
86 
87         if (brcFeature->IsVdencBrcEnabled() || !m_basicFeature->m_adaptiveRoundingInterEnable)
88         {
89             sliceState.dwRoundingValue      = settings->StaticInterRounding[index];
90             sliceState.dwRoundingIntraValue = settings->StaticIntraRounding[index];
91         }
92         else
93         {
94             sliceState.dwRoundingValue      = settings->AdaptiveInterRounding[index][sliceQP];
95             sliceState.dwRoundingIntraValue = settings->AdaptiveIntraRounding[index][sliceQP];
96         }
97     }
98 
99     return MOS_STATUS_SUCCESS;
100 }
101 
SetRoundingParams(MHW_VDBOX_AVC_SLICE_STATE & sliceState) const102 MOS_STATUS AvcEncodeRounding::SetRoundingParams(MHW_VDBOX_AVC_SLICE_STATE &sliceState) const
103 {
104     ENCODE_FUNC_CALL();
105 
106     auto settings = static_cast<AvcVdencFeatureSettings *>(m_constSettings);
107     ENCODE_CHK_NULL_RETURN(settings);
108 
109     sliceState.bRoundingInterEnable = true;
110 
111     if(m_roundingParams != nullptr && m_roundingParams->bEnableCustomRoudingIntra)
112         sliceState.dwRoundingIntraValue = m_roundingParams->dwRoundingIntra;
113     else
114         sliceState.dwRoundingIntraValue = settings->DefaultIntraRounding;
115 
116     if (m_roundingParams != nullptr && m_roundingParams->bEnableCustomRoudingInter)
117         sliceState.dwRoundingValue = m_roundingParams->dwRoundingInter;
118     else
119         ENCODE_CHK_STATUS_RETURN(GetRounding(sliceState));
120 
121     return MOS_STATUS_SUCCESS;
122 }
123 
MHW_SETPAR_DECL_SRC(VDENC_AVC_SLICE_STATE,AvcEncodeRounding)124 MHW_SETPAR_DECL_SRC(VDENC_AVC_SLICE_STATE, AvcEncodeRounding)
125 {
126     auto sliceParams = &m_basicFeature->m_sliceParams[m_basicFeature->m_curNumSlices];
127 
128     MHW_VDBOX_AVC_SLICE_STATE sliceState;
129     MOS_ZeroMemory(&sliceState, sizeof(sliceState));
130     ENCODE_CHK_STATUS_RETURN(SetRoundingParams(sliceState));
131 
132     params.roundIntra = (uint8_t)sliceState.dwRoundingIntraValue;
133     params.roundInter = 2;
134 
135     if (m_basicFeature->IsAvcPSlice(sliceParams->slice_type) ||
136         m_basicFeature->IsAvcBSlice(sliceParams->slice_type))
137     {
138         params.roundInterEnable = sliceState.bRoundingInterEnable;
139         params.roundInter       = (uint8_t)sliceState.dwRoundingValue;
140     }
141 
142     return MOS_STATUS_SUCCESS;
143 }
144 
MHW_SETPAR_DECL_SRC(MFX_AVC_SLICE_STATE,AvcEncodeRounding)145 MHW_SETPAR_DECL_SRC(MFX_AVC_SLICE_STATE, AvcEncodeRounding)
146 {
147     auto sliceParams = &m_basicFeature->m_sliceParams[m_basicFeature->m_curNumSlices];
148 
149     MHW_VDBOX_AVC_SLICE_STATE sliceState;
150     MOS_ZeroMemory(&sliceState, sizeof(sliceState));
151     ENCODE_CHK_STATUS_RETURN(SetRoundingParams(sliceState));
152 
153     params.roundintra = (uint8_t)sliceState.dwRoundingIntraValue;
154     params.roundinter = 2;
155 
156     if (m_basicFeature->IsAvcPSlice(sliceParams->slice_type) ||
157         m_basicFeature->IsAvcBSlice(sliceParams->slice_type))
158     {
159         params.roundinterenable = sliceState.bRoundingInterEnable;
160         params.roundinter = (uint8_t)sliceState.dwRoundingValue;
161     }
162 
163     return MOS_STATUS_SUCCESS;
164 }
165 
166 }  // namespace encode
167