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_trellis.cpp
24 //! \brief    Defines the common interface for avc encode trellis feature
25 //!
26 
27 #include "encode_avc_trellis.h"
28 #include "encode_avc_vdenc_feature_manager.h"
29 #include "encode_avc_vdenc_const_settings.h"
30 
31 namespace encode
32 {
33 
34 #define ENCODE_AVC_DEFAULT_TRELLIS_QUANT_ROUNDING 3
35 
AvcEncodeTrellis(MediaFeatureManager * featureManager,EncodeAllocator * allocator,CodechalHwInterfaceNext * hwInterface,void * constSettings)36 AvcEncodeTrellis::AvcEncodeTrellis(
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 AvcEncodeTrellis::Update(void *params)
52 {
53     ENCODE_FUNC_CALL();
54 
55     auto seqParams = m_basicFeature->m_seqParam;
56 
57     m_trellis = seqParams->Trellis;
58 
59     ENCODE_CHK_STATUS_RETURN(UpdateTrellisParameters());
60 
61     return MOS_STATUS_SUCCESS;
62 }
63 
GetTrellisQuantization(PCODECHAL_ENCODE_AVC_TQ_INPUT_PARAMS params,PCODECHAL_ENCODE_AVC_TQ_PARAMS trellisQuantParams)64 MOS_STATUS AvcEncodeTrellis::GetTrellisQuantization(
65     PCODECHAL_ENCODE_AVC_TQ_INPUT_PARAMS params,
66     PCODECHAL_ENCODE_AVC_TQ_PARAMS trellisQuantParams)
67 {
68     ENCODE_FUNC_CALL();
69 
70     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
71 
72     ENCODE_CHK_NULL_RETURN(params);
73     ENCODE_CHK_NULL_RETURN(trellisQuantParams);
74 
75     auto settings = static_cast<AvcVdencFeatureSettings *>(m_constSettings);
76     ENCODE_CHK_NULL_RETURN(settings);
77 
78     trellisQuantParams->dwTqEnabled  = settings->TrellisQuantizationEnable[params->ucTargetUsage];
79     trellisQuantParams->dwTqRounding = trellisQuantParams->dwTqEnabled ?
80                                        settings->TrellisQuantizationRounding[params->ucTargetUsage] : 0;
81 
82     return eStatus;
83 }
84 
UpdateTrellisParameters()85 MOS_STATUS AvcEncodeTrellis::UpdateTrellisParameters()
86 {
87     ENCODE_FUNC_CALL();
88 
89     uint8_t sliceQP = m_basicFeature->m_picParam->pic_init_qp_minus26 + 26 + m_basicFeature->m_sliceParams->slice_qp_delta;
90 
91     // Determine if Trellis Quantization should be enabled
92     MOS_ZeroMemory(&m_trellisQuantParams, sizeof(m_trellisQuantParams));
93 
94     // Trellis must remain switched off if it is explicitly disabled or picture is encoded with CAVLC
95     if (!(m_trellis & trellisDisabled) && m_basicFeature->m_picParam->entropy_coding_mode_flag)
96     {
97         if (m_trellis == trellisInternal)
98         {
99             CODECHAL_ENCODE_AVC_TQ_INPUT_PARAMS tqInputParams;
100             tqInputParams.ucQP               = sliceQP;
101             tqInputParams.ucTargetUsage      = m_basicFeature->m_seqParam->TargetUsage;
102             tqInputParams.wPictureCodingType = m_basicFeature->m_pictureCodingType;
103             tqInputParams.bBrcEnabled        = false;
104             tqInputParams.bVdEncEnabled      = true;
105 
106             ENCODE_CHK_STATUS_RETURN(GetTrellisQuantization(
107                 &tqInputParams,
108                 &m_trellisQuantParams));
109         }
110         else if ((m_basicFeature->m_pictureCodingType == I_TYPE && (m_trellis & trellisEnabledI)) ||
111                  (m_basicFeature->m_pictureCodingType == P_TYPE && (m_trellis & trellisEnabledP)) ||
112                  (m_basicFeature->m_pictureCodingType == B_TYPE && (m_trellis & trellisEnabledB)))
113         {
114             m_trellisQuantParams.dwTqEnabled  = true;
115             m_trellisQuantParams.dwTqRounding = ENCODE_AVC_DEFAULT_TRELLIS_QUANT_ROUNDING;
116         }
117     }
118 
119     return MOS_STATUS_SUCCESS;
120 }
121 
MHW_SETPAR_DECL_SRC(VDENC_AVC_IMG_STATE,AvcEncodeTrellis)122 MHW_SETPAR_DECL_SRC(VDENC_AVC_IMG_STATE, AvcEncodeTrellis)
123 {
124     params.trellisQuantEn = (uint8_t)m_trellisQuantParams.dwTqEnabled;
125 
126     return MOS_STATUS_SUCCESS;
127 }
128 
MHW_SETPAR_DECL_SRC(MFX_AVC_IMG_STATE,AvcEncodeTrellis)129 MHW_SETPAR_DECL_SRC(MFX_AVC_IMG_STATE, AvcEncodeTrellis)
130 {
131     if (m_trellisQuantParams.dwTqEnabled && m_basicFeature->m_picParam->entropy_coding_mode_flag)
132     {
133         params.trellisQuantizationEnabledTqenb = m_trellisQuantParams.dwTqEnabled;
134         params.trellisQuantizationRoundingTqr = m_trellisQuantParams.dwTqRounding;
135     }
136 
137     return MOS_STATUS_SUCCESS;
138 }
139 
140 }  // namespace encode
141