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_vp9_cqp.cpp
24 //! \brief    Defines the common interface for vp9 encode cqp features
25 //!
26 
27 #include "encode_vp9_cqp.h"
28 #include "encode_vp9_vdenc_feature_manager.h"
29 #include "encode_vp9_vdenc_const_settings.h"
30 
31 namespace encode
32 {
Vp9EncodeCqp(MediaFeatureManager * featureManager,EncodeAllocator * allocator,CodechalHwInterfaceNext * hwInterface,void * constSettings)33 Vp9EncodeCqp::Vp9EncodeCqp(
34     MediaFeatureManager *featureManager,
35     EncodeAllocator *    allocator,
36     CodechalHwInterfaceNext *hwInterface,
37     void *               constSettings) : MediaFeature(constSettings),
38                            m_allocator(allocator)
39 {
40     ENCODE_FUNC_CALL();
41     ENCODE_CHK_NULL_NO_STATUS_RETURN(featureManager);
42 
43     m_featureManager = featureManager;
44     auto encFeatureManager = dynamic_cast<EncodeVp9VdencFeatureManager *>(featureManager);
45     ENCODE_CHK_NULL_NO_STATUS_RETURN(encFeatureManager);
46 
47     m_basicFeature = dynamic_cast<Vp9BasicFeature *>(encFeatureManager->GetFeature(FeatureIDs::basicFeature));
48     ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature);
49 }
50 
Init(void * settings)51 MOS_STATUS Vp9EncodeCqp::Init(void *settings)
52 {
53     ENCODE_FUNC_CALL();
54 
55     m_enabled = true;
56 
57     ENCODE_CHK_STATUS_RETURN(AllocateResources());
58 
59     return MOS_STATUS_SUCCESS;
60 }
61 
Update(void * params)62 MOS_STATUS Vp9EncodeCqp::Update(void *params)
63 {
64     ENCODE_FUNC_CALL();
65     ENCODE_CHK_NULL_RETURN(params);
66 
67     EncoderParams *encodeParams = (EncoderParams *)params;
68 
69     PCODEC_VP9_ENCODE_SEQUENCE_PARAMS vp9SeqParams = static_cast<PCODEC_VP9_ENCODE_SEQUENCE_PARAMS>(encodeParams->pSeqParams);
70     ENCODE_CHK_NULL_RETURN(vp9SeqParams);
71     PCODEC_VP9_ENCODE_PIC_PARAMS vp9PicParams = static_cast<PCODEC_VP9_ENCODE_PIC_PARAMS>(encodeParams->pPicParams);
72     ENCODE_CHK_NULL_RETURN(vp9PicParams);
73 
74     if (m_basicFeature->m_newSeq)
75     {
76         ENCODE_CHK_STATUS_RETURN(SetConstSettings());
77     }
78 
79     return MOS_STATUS_SUCCESS;
80 }
81 
AllocateResources()82 MOS_STATUS Vp9EncodeCqp::AllocateResources()
83 {
84     ENCODE_FUNC_CALL();
85     ENCODE_CHK_NULL_RETURN(m_basicFeature);
86 
87     MOS_RESOURCE *allocatedBuffer = nullptr;
88 
89     // Initiate allocation parameters and lock flags
90     MOS_ALLOC_GFXRES_PARAMS allocParamsForBufferLinear;
91     MOS_ZeroMemory(&allocParamsForBufferLinear, sizeof(MOS_ALLOC_GFXRES_PARAMS));
92     allocParamsForBufferLinear.Type     = MOS_GFXRES_BUFFER;
93     allocParamsForBufferLinear.TileType = MOS_TILE_LINEAR;
94     allocParamsForBufferLinear.Format   = Format_Buffer;
95 
96     uint32_t formatMultiFactor = (m_basicFeature->m_chromaFormat == HCP_CHROMA_FORMAT_YUV444) ? 3 : 2;
97     formatMultiFactor *= (m_basicFeature->m_bitDepth == 8) ? 1 : 2;
98     uint32_t size = (18 * formatMultiFactor) / 2;
99     // Deblocking filter line buffer
100     size                                = m_basicFeature->m_maxPicWidthInSb * size * CODECHAL_CACHELINE_SIZE;
101     allocParamsForBufferLinear.dwBytes  = size;
102     allocParamsForBufferLinear.pBufName = "DeblockingFilterLineBuffer";
103     allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ;
104     allocatedBuffer                     = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
105     ENCODE_CHK_NULL_RETURN(allocatedBuffer);
106     m_resDeblockingFilterLineBuffer = *allocatedBuffer;
107 
108     // Deblocking filter tile line buffer
109     allocParamsForBufferLinear.dwBytes  = size;
110     allocParamsForBufferLinear.pBufName = "DeblockingFilterTileLineBuffer";
111     allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ;
112     allocatedBuffer                     = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
113     ENCODE_CHK_NULL_RETURN(allocatedBuffer);
114     m_resDeblockingFilterTileLineBuffer = *allocatedBuffer;
115 
116     formatMultiFactor = (m_basicFeature->m_chromaFormat == HCP_CHROMA_FORMAT_YUV444) ? 25 : 17;
117     size              = formatMultiFactor * ((m_basicFeature->m_bitDepth == 8) ? 1 : 2);
118     // Deblocking filter tile column buffer
119     size                                = m_basicFeature->m_maxPicHeightInSb * size * CODECHAL_CACHELINE_SIZE;
120     allocParamsForBufferLinear.dwBytes  = size;
121     allocParamsForBufferLinear.pBufName = "DeblockingFilterTileColumnBuffer";
122     allocParamsForBufferLinear.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ;
123     allocatedBuffer                     = m_allocator->AllocateResource(allocParamsForBufferLinear, true);
124     ENCODE_CHK_NULL_RETURN(allocatedBuffer);
125     m_resDeblockingFilterTileColumnBuffer = *allocatedBuffer;
126 
127     return MOS_STATUS_SUCCESS;
128 }
129 
MHW_SETPAR_DECL_SRC(HCP_PIPE_MODE_SELECT,Vp9EncodeCqp)130 MHW_SETPAR_DECL_SRC(HCP_PIPE_MODE_SELECT, Vp9EncodeCqp)
131 {
132     ENCODE_FUNC_CALL();
133 
134     params.bRdoqEnable = false;
135 
136     return MOS_STATUS_SUCCESS;
137 }
138 
MHW_SETPAR_DECL_SRC(HCP_PIPE_BUF_ADDR_STATE,Vp9EncodeCqp)139 MHW_SETPAR_DECL_SRC(HCP_PIPE_BUF_ADDR_STATE, Vp9EncodeCqp)
140 {
141     ENCODE_FUNC_CALL();
142 
143     params.presMfdDeblockingFilterRowStoreScratchBuffer    = const_cast<PMOS_RESOURCE>(&m_resDeblockingFilterLineBuffer);
144     params.presDeblockingFilterTileRowStoreScratchBuffer   = const_cast<PMOS_RESOURCE>(&m_resDeblockingFilterTileLineBuffer);
145     params.presDeblockingFilterColumnRowStoreScratchBuffer = const_cast<PMOS_RESOURCE>(&m_resDeblockingFilterTileColumnBuffer);
146 
147     return MOS_STATUS_SUCCESS;
148 }
149 
150 }  // namespace encode
151