1 /*
2 * Copyright (c) 2021-2024, 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,Av1EncodeTile
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_aqm.cpp
24 //! \brief    Defines the common interface for avc aqm
25 //!
26 
27 #include "encode_avc_aqm.h"
28 #include "encode_avc_vdenc_feature_manager.h"
29 #include "encode_avc_basic_feature.h"
30 
31 namespace encode
32 {
AvcEncodeAqm(MediaFeatureManager * featureManager,EncodeAllocator * allocator,CodechalHwInterfaceNext * hwInterface,void * constSettings)33 AvcEncodeAqm::AvcEncodeAqm(MediaFeatureManager *featureManager,
34     EncodeAllocator *                           allocator,
35     CodechalHwInterfaceNext *                       hwInterface,
36     void *                                      constSettings) : EncodeAqmFeature(featureManager, allocator, hwInterface, constSettings)
37 {
38     auto encFeatureManager = dynamic_cast<EncodeAvcVdencFeatureManager *>(featureManager);
39     ENCODE_CHK_NULL_NO_STATUS_RETURN(encFeatureManager);
40 
41     m_basicFeature = dynamic_cast<EncodeBasicFeature *>(encFeatureManager->GetFeature(FeatureIDs::basicFeature));
42     ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature);
43 
44     m_numTiles = 1;
45 };
46 
Update(void * params)47 MOS_STATUS AvcEncodeAqm::Update(void* params)
48 {
49     auto basicFeature = dynamic_cast<AvcBasicFeature*>(m_basicFeature);
50     ENCODE_CHK_NULL_RETURN(basicFeature);
51     if (basicFeature->m_picParam->QualityInfoSupportFlags.fields.enable_frame)
52     {
53         m_enabled = true;
54         basicFeature->m_suppressReconPicSupported = false;
55     }
56     m_numTiles = 1;
57     m_tile_width[0]     = (uint16_t)m_basicFeature->m_oriFrameWidth;
58     m_tile_height[0]    = (uint16_t)m_basicFeature->m_oriFrameHeight;
59 #if USE_CODECHAL_DEBUG_TOOL
60     UpdateFrameDisplayOrder(basicFeature->m_pictureCodingType, basicFeature->m_picParam->CurrFieldOrderCnt[0] / 2, basicFeature->m_seqParam->GopPicSize);
61 #endif
62     ENCODE_CHK_STATUS_RETURN(EncodeAqmFeature::Update(params));
63     return MOS_STATUS_SUCCESS;
64 }
65 
Init(void * setting)66 MOS_STATUS AvcEncodeAqm::Init(void *setting)
67 {
68     ENCODE_FUNC_CALL();
69     ENCODE_CHK_NULL_RETURN(setting);
70 
71     ENCODE_CHK_STATUS_RETURN(EncodeAqmFeature::Init(setting));
72 
73     auto basicFeature = dynamic_cast<AvcBasicFeature *>(m_basicFeature);
74     ENCODE_CHK_NULL_RETURN(basicFeature);
75 
76     if (m_enabled)
77     {
78         // Assuming AvcBasicFeature is already initialized, if VDAQM is in use, enable deblocking/recon generation for all frames
79         basicFeature->m_suppressReconPicSupported = false;
80     }
81 
82     return MOS_STATUS_SUCCESS;
83 }
84 
MHW_SETPAR_DECL_SRC(AQM_PIC_STATE,AvcEncodeAqm)85 MHW_SETPAR_DECL_SRC(AQM_PIC_STATE, AvcEncodeAqm)
86 {
87     ENCODE_CHK_STATUS_RETURN(EncodeAqmFeature::MHW_SETPAR_F(AQM_PIC_STATE)(params));
88     if (m_enabled)
89     {
90         params.frameWidthInPixelMinus1  = MOS_ALIGN_CEIL(m_basicFeature->m_oriFrameWidth, 16) - 1;
91         params.FrameHeightInPixelMinus1 = MOS_ALIGN_CEIL(m_basicFeature->m_oriFrameHeight, 16) - 1;
92         params.lcuSize                  = LCU_SIZE_16X16;
93         params.codectype                = CODECTYPE_AVC;
94     }
95 
96     return MOS_STATUS_SUCCESS;
97 }
98 
MHW_SETPAR_DECL_SRC(AQM_SLICE_STATE,AvcEncodeAqm)99 MHW_SETPAR_DECL_SRC(AQM_SLICE_STATE, AvcEncodeAqm)
100 {
101     auto basicFeature = dynamic_cast<AvcBasicFeature *>(m_basicFeature);
102     ENCODE_CHK_NULL_RETURN(basicFeature);
103 
104     auto sliceParams = &basicFeature->m_sliceParams[basicFeature->m_curNumSlices];
105     auto frameHeight = static_cast<uint32_t>(CODECHAL_GET_HEIGHT_IN_MACROBLOCKS(basicFeature->m_seqParam->FrameHeight));
106     auto frameWidth  = static_cast<uint32_t>(CODECHAL_GET_WIDTH_IN_MACROBLOCKS(basicFeature->m_seqParam->FrameWidth));
107     auto nextsliceMbStartYPosition = (sliceParams->first_mb_in_slice + sliceParams->NumMbsForSlice) / frameWidth;
108 
109     params.tileSliceStartLcuMbX     = 0;
110     params.tileSliceStartLcuMbY     = sliceParams->first_mb_in_slice / frameWidth;
111     params.nextTileSliceStartLcuMbX = 0;
112     params.nextTileSliceStartLcuMbY = nextsliceMbStartYPosition > frameHeight ? frameHeight : nextsliceMbStartYPosition;
113 
114     return MOS_STATUS_SUCCESS;
115 }
116 
MHW_SETPAR_DECL_SRC(MFX_AVC_IMG_STATE,AvcEncodeAqm)117 MHW_SETPAR_DECL_SRC(MFX_AVC_IMG_STATE, AvcEncodeAqm)
118 {
119     params.vdaqmEnable = m_enabled;
120 
121     return MOS_STATUS_SUCCESS;
122 }
123 
124 #if USE_CODECHAL_DEBUG_TOOL
UpdateFrameDisplayOrder(const uint16_t pictureCodingType,const uint32_t framePOC,const uint32_t gopPicSize)125 MOS_STATUS AvcEncodeAqm::UpdateFrameDisplayOrder(const uint16_t pictureCodingType, const uint32_t framePOC, const uint32_t gopPicSize)
126 {
127     auto basicFeature = dynamic_cast<AvcBasicFeature *>(m_basicFeature);
128     ENCODE_CHK_NULL_RETURN(basicFeature);
129     if (basicFeature->m_picParam->bIdrPic == 1)
130     {
131         m_frameNumPrevious += m_gopSizePrevious;
132     }
133     uint32_t    displayOrderInGOP   = framePOC;
134     uint32_t    displayOrderInSeq   = displayOrderInGOP + m_frameNumPrevious;
135     m_gopSizePrevious               = gopPicSize;
136     m_frameIdxQueue.push(displayOrderInSeq);
137     return MOS_STATUS_SUCCESS;
138 }
139 #endif
140 
141 }  // namespace encode