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 //! \file     decode_av1_pipeline.cpp
24 //! \brief    Defines the interface for av1 decode pipeline
25 //!
26 #include "decode_av1_pipeline.h"
27 #include "decode_utils.h"
28 #include "codechal_setting.h"
29 #include "decode_av1_feature_manager.h"
30 #include "decode_huc_packet_creator_base.h"
31 #include "media_debug_fast_dump.h"
32 
33 namespace decode {
34 
Av1Pipeline(CodechalHwInterfaceNext * hwInterface,CodechalDebugInterface * debugInterface)35 Av1Pipeline::Av1Pipeline(
36     CodechalHwInterfaceNext*   hwInterface,
37     CodechalDebugInterface *debugInterface)
38     : DecodePipeline(hwInterface, debugInterface)
39 {
40     MOS_STATUS m_status = InitUserSetting(m_userSettingPtr);
41 }
42 
Initialize(void * settings)43 MOS_STATUS Av1Pipeline::Initialize(void *settings)
44 {
45     DECODE_FUNC_CALL();
46     DECODE_CHK_STATUS(DecodePipeline::Initialize(settings));
47 
48     HucPacketCreatorBase *hucPktCreator = dynamic_cast<HucPacketCreatorBase *>(this);
49     DECODE_CHK_NULL(hucPktCreator);
50 
51     auto *codecSettings = (CodechalSetting*)settings;
52     DECODE_CHK_NULL(codecSettings);
53 
54   bool forceTileBasedDecodingRead = 0;
55 #if (_DEBUG || _RELEASE_INTERNAL)
56     forceTileBasedDecodingRead = ReadUserFeature(m_userSettingPtr, "Force Av1 Tile Based Decode", MediaUserSetting::Group::Sequence).Get<bool>();
57 #endif
58     m_forceTileBasedDecoding = forceTileBasedDecodingRead;
59 
60     return MOS_STATUS_SUCCESS;
61 }
62 
Prepare(void * params)63 MOS_STATUS Av1Pipeline::Prepare(void *params)
64 {
65     DECODE_FUNC_CALL();
66     DECODE_CHK_NULL(params);
67 
68     auto basicFeature = dynamic_cast<Av1BasicFeature*>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
69     DECODE_CHK_NULL(basicFeature);
70     DECODE_CHK_STATUS(DecodePipeline::Prepare(params));
71 
72     return MOS_STATUS_SUCCESS;
73 }
74 
Uninitialize()75 MOS_STATUS Av1Pipeline::Uninitialize()
76 {
77     DECODE_FUNC_CALL();
78 
79     return DecodePipeline::Uninitialize();
80 }
81 
UserFeatureReport()82 MOS_STATUS Av1Pipeline::UserFeatureReport()
83 {
84     DECODE_FUNC_CALL();
85     DECODE_CHK_STATUS(DecodePipeline::UserFeatureReport());
86 #if (_DEBUG || _RELEASE_INTERNAL)
87     WriteUserFeature(__MEDIA_USER_FEATURE_VALUE_APOGEIOS_AV1D_ENABLE_ID, 1, m_osInterface->pOsContext);
88 #endif
89     return MOS_STATUS_SUCCESS;
90 }
91 
ActivateDecodePackets()92 MOS_STATUS Av1Pipeline::ActivateDecodePackets()
93 {
94     DECODE_FUNC_CALL();
95 
96     bool immediateSubmit = true;
97 
98     if (m_isFirstTileInFrm)
99     {
100         m_isFirstTileInFrm = false;
101     }
102 
103     if (!m_forceTileBasedDecoding)
104     {
105         immediateSubmit = false;
106     }
107 
108     for (uint16_t curPass = 0; curPass < GetPassNum(); curPass++)
109     {
110         DECODE_CHK_STATUS(ActivatePacket(DecodePacketId(this, av1DecodePacketId), immediateSubmit, curPass, 0));
111     }
112 
113     return MOS_STATUS_SUCCESS;
114 }
115 
FrameBasedDecodingInUse()116 bool Av1Pipeline::FrameBasedDecodingInUse()
117 {
118     auto basicFeature = dynamic_cast<Av1BasicFeature*>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
119 
120     bool isframeBasedDecodingUsed = false;
121 
122     if (basicFeature != nullptr)
123     {
124         isframeBasedDecodingUsed = ((basicFeature->m_av1PicParams->m_loopRestorationFlags.m_fields.m_yframeRestorationType > 0) &
125                                    ((basicFeature->m_av1PicParams->m_loopRestorationFlags.m_fields.m_cbframeRestorationType |
126                                     basicFeature->m_av1PicParams->m_loopRestorationFlags.m_fields.m_crframeRestorationType) > 0) &&
127                                     basicFeature->m_av1PicParams->m_picInfoFlags.m_fields.m_useSuperres && MEDIA_IS_WA(GetWaTable(), Wa_1409820462)
128                                     || !m_forceTileBasedDecoding);
129     }
130     return isframeBasedDecodingUsed;
131 }
132 
CreateFeatureManager()133 MOS_STATUS Av1Pipeline::CreateFeatureManager()
134 {
135     DECODE_FUNC_CALL();
136     m_featureManager = MOS_New(DecodeAv1FeatureManager, m_allocator, m_hwInterface, m_osInterface);
137     DECODE_CHK_NULL(m_featureManager);
138     return MOS_STATUS_SUCCESS;
139 }
140 
CreateSubPackets(DecodeSubPacketManager & subPacketManager,CodechalSetting & codecSettings)141 MOS_STATUS Av1Pipeline::CreateSubPackets(DecodeSubPacketManager &subPacketManager, CodechalSetting &codecSettings)
142 {
143     DECODE_FUNC_CALL();
144 
145     DECODE_CHK_STATUS(DecodePipeline::CreateSubPackets(subPacketManager, codecSettings));
146 
147     return MOS_STATUS_SUCCESS;
148 }
149 
GetDecodeMode()150 Av1Pipeline::Av1DecodeMode Av1Pipeline::GetDecodeMode()
151 {
152     return m_decodeMode;
153 }
154 
155 #if USE_CODECHAL_DEBUG_TOOL
DumpParams(Av1BasicFeature & basicFeature)156 MOS_STATUS Av1Pipeline::DumpParams(Av1BasicFeature &basicFeature)
157 {
158     m_debugInterface->m_frameType = basicFeature.m_av1PicParams->m_picInfoFlags.m_fields.m_frameType ? P_TYPE : I_TYPE;
159     m_debugInterface->m_bufferDumpFrameNum = basicFeature.m_frameNum;
160 
161     DECODE_CHK_STATUS(DumpPicParams(basicFeature.m_av1PicParams));
162     DECODE_CHK_STATUS(DumpTileParams(basicFeature.m_av1TileParams, basicFeature.m_tileCoding.m_numTiles));
163     DECODE_CHK_STATUS(DumpBitstream(&basicFeature.m_resDataBuffer.OsResource, basicFeature.m_dataSize, 0));
164 
165     return MOS_STATUS_SUCCESS;
166 }
167 
DumpPicParams(CodecAv1PicParams * picParams)168 MOS_STATUS Av1Pipeline::DumpPicParams(CodecAv1PicParams *picParams)
169 {
170     CODECHAL_DEBUG_FUNCTION_ENTER;
171 
172     if (picParams == nullptr)
173     {
174         return MOS_STATUS_SUCCESS;
175     }
176 
177     if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrPicParams))
178     {
179         const char *fileName = m_debugInterface->CreateFileName(
180             "DEC",
181             CodechalDbgBufferType::bufPicParams,
182             CodechalDbgExtType::txt);
183 
184         if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrEnableFastDump))
185         {
186             MediaDebugFastDump::Dump(
187                 (uint8_t *)picParams,
188                 fileName,
189                 sizeof(CodecAv1PicParams),
190                 0,
191                 MediaDebugSerializer<CodecAv1PicParams>());
192         }
193         else
194         {
195             DumpDecodeAv1PicParams(picParams, fileName);
196         }
197     }
198 
199     return MOS_STATUS_SUCCESS;
200 }
201 
DumpTileParams(CodecAv1TileParams * tileParams,uint32_t tileNum)202 MOS_STATUS Av1Pipeline::DumpTileParams(CodecAv1TileParams *tileParams, uint32_t tileNum)
203 {
204     CODECHAL_DEBUG_FUNCTION_ENTER;
205 
206     if (tileParams == nullptr)
207     {
208         return MOS_STATUS_SUCCESS;
209     }
210 
211     if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrSlcParams))
212     {
213         const char *fileName = m_debugInterface->CreateFileName(
214             "DEC",
215             "TileParams",
216             CodechalDbgExtType::txt);
217 
218         if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrEnableFastDump))
219         {
220             MediaDebugFastDump::Dump(
221                 (uint8_t *)tileParams,
222                 fileName,
223                 sizeof(CodecAv1TileParams) * tileNum,
224                 0,
225                 MediaDebugSerializer<CodecAv1TileParams>());
226         }
227         else
228         {
229             DumpDecodeAv1TileParams(tileParams, tileNum, fileName);
230         }
231     }
232 
233     return MOS_STATUS_SUCCESS;
234 }
235 #endif
236 
237 }
238