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_g12_base.cpp
24 //! \brief    Defines the interface for av1 decode pipeline
25 //!
26 #include "decode_av1_pipeline_g12_base.h"
27 #include "decode_utils.h"
28 #include "codechal_setting.h"
29 #include "decode_av1_feature_manager_g12_base.h"
30 #include "decode_huc_packet_creator_g12.h"
31 #include "decode_sfc_histogram_postsubpipeline_m12.h"
32 #include "decode_input_bitstream_m12.h"
33 #include "decode_cp_bitstream_m12.h"
34 #include "decode_marker_packet_g12.h"
35 #include "decode_predication_packet_g12.h"
36 #include "media_debug_fast_dump.h"
37 
38 namespace decode {
39 
Av1PipelineG12_Base(CodechalHwInterface * hwInterface,CodechalDebugInterface * debugInterface)40 Av1PipelineG12_Base::Av1PipelineG12_Base(
41     CodechalHwInterface *   hwInterface,
42     CodechalDebugInterface *debugInterface)
43     : DecodePipeline(hwInterface ? hwInterface->m_hwInterfaceNext : nullptr, debugInterface)
44 {
45     m_hwInterface = hwInterface;
46 }
47 
Initialize(void * settings)48 MOS_STATUS Av1PipelineG12_Base::Initialize(void *settings)
49 {
50     DECODE_FUNC_CALL();
51     DECODE_CHK_STATUS(MediaPipeline::InitPlatform());
52     DECODE_CHK_STATUS(MediaPipeline::CreateMediaCopyWrapper());
53     DECODE_CHK_NULL(m_mediaCopyWrapper);
54 
55     DECODE_CHK_NULL(m_waTable);
56 
57     auto *codecSettings = (CodechalSetting *)settings;
58     DECODE_CHK_NULL(m_hwInterface);
59     DECODE_CHK_STATUS(m_hwInterface->Initialize(codecSettings));
60 
61     if (m_mediaCopyWrapper->MediaCopyStateIsNull())
62     {
63         m_mediaCopyWrapper->SetMediaCopyState(m_hwInterface->CreateMediaCopy(m_osInterface));
64     }
65 
66     CODECHAL_DEBUG_TOOL(
67         m_debugInterface = MOS_New(CodechalDebugInterface);
68         DECODE_CHK_NULL(m_debugInterface);
69         DECODE_CHK_STATUS(
70             m_debugInterface->Initialize(m_hwInterface, codecSettings->codecFunction, m_mediaCopyWrapper)););
71 
72     if (m_hwInterface->m_hwInterfaceNext)
73     {
74         m_hwInterface->m_hwInterfaceNext->legacyHwInterface = m_hwInterface;
75     }
76     m_mediaContext = MOS_New(MediaContext, scalabilityDecoder, m_hwInterface->m_hwInterfaceNext, m_osInterface);
77     DECODE_CHK_NULL(m_mediaContext);
78 
79     m_task = CreateTask(MediaTask::TaskType::cmdTask);
80     DECODE_CHK_NULL(m_task);
81 
82     m_numVdbox = GetSystemVdboxNumber();
83 
84     bool limitedLMemBar = MEDIA_IS_SKU(m_skuTable, FtrLimitedLMemBar) ? true : false;
85     m_allocator         = MOS_New(DecodeAllocator, m_osInterface, limitedLMemBar);
86     DECODE_CHK_NULL(m_allocator);
87 
88     DECODE_CHK_STATUS(CreateStatusReport());
89 
90     m_decodecp = Create_DecodeCpInterface(codecSettings, m_hwInterface->GetCpInterface(), m_hwInterface->GetOsInterface());
91     if (m_decodecp)
92     {
93         DECODE_CHK_STATUS(m_decodecp->RegisterParams(codecSettings));
94     }
95     DECODE_CHK_STATUS(CreateFeatureManager());
96     DECODE_CHK_STATUS(m_featureManager->Init(codecSettings));
97 
98     DECODE_CHK_STATUS(CreateSubPipeLineManager(codecSettings));
99     DECODE_CHK_STATUS(CreateSubPacketManager(codecSettings));
100 
101     HucPacketCreatorG12 *hucPktCreator = dynamic_cast<HucPacketCreatorG12 *>(this);
102     DECODE_CHK_NULL(hucPktCreator);
103 
104   bool forceTileBasedDecodingRead = 0;
105 #if (_DEBUG || _RELEASE_INTERNAL)
106     forceTileBasedDecodingRead = ReadUserFeature(m_userSettingPtr, "Force Av1 Tile Based Decode", MediaUserSetting::Group::Sequence).Get<bool>();
107 #endif
108     m_forceTileBasedDecoding = forceTileBasedDecodingRead;
109 
110     return MOS_STATUS_SUCCESS;
111 }
112 
Prepare(void * params)113 MOS_STATUS Av1PipelineG12_Base::Prepare(void *params)
114 {
115     DECODE_FUNC_CALL();
116     DECODE_CHK_NULL(params);
117 
118     auto basicFeature = dynamic_cast<Av1BasicFeatureG12*>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
119     DECODE_CHK_NULL(basicFeature);
120     DECODE_CHK_STATUS(DecodePipeline::Prepare(params));
121 
122     return MOS_STATUS_SUCCESS;
123 }
124 
Uninitialize()125 MOS_STATUS Av1PipelineG12_Base::Uninitialize()
126 {
127     DECODE_FUNC_CALL();
128 
129     return DecodePipeline::Uninitialize();
130 }
131 
UserFeatureReport()132 MOS_STATUS Av1PipelineG12_Base::UserFeatureReport()
133 {
134     DECODE_FUNC_CALL();
135     DECODE_CHK_STATUS(DecodePipeline::UserFeatureReport());
136 #if (_DEBUG || _RELEASE_INTERNAL)
137     WriteUserFeature(__MEDIA_USER_FEATURE_VALUE_APOGEIOS_AV1D_ENABLE_ID, 1, m_osInterface->pOsContext);
138 #endif
139     return MOS_STATUS_SUCCESS;
140 }
141 
ActivateDecodePackets()142 MOS_STATUS Av1PipelineG12_Base::ActivateDecodePackets()
143 {
144     DECODE_FUNC_CALL();
145     auto basicFeature    = dynamic_cast<Av1BasicFeatureG12 *>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
146     DECODE_CHK_NULL(basicFeature);
147     bool immediateSubmit = true;
148 
149     if (m_isFirstTileInFrm)
150     {
151         m_isFirstTileInFrm = false;
152     }
153 
154     if (!m_forceTileBasedDecoding)
155     {
156         immediateSubmit = false;
157     }
158 
159     for (uint16_t curPass = 0; curPass < GetPassNum(); curPass++)
160     {
161         DECODE_CHK_STATUS(ActivatePacket(DecodePacketId(this, av1DecodePacketId), immediateSubmit, curPass, 0));
162         if (basicFeature->m_filmGrainEnabled)
163         {
164             m_activePacketList.back().frameTrackingRequested = false;
165         }
166     }
167 
168     return MOS_STATUS_SUCCESS;
169 }
170 
FrameBasedDecodingInUse()171 bool Av1PipelineG12_Base::FrameBasedDecodingInUse()
172 {
173     auto basicFeature = dynamic_cast<Av1BasicFeatureG12*>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
174 
175     bool isframeBasedDecodingUsed = false;
176 
177     if (basicFeature != nullptr)
178     {
179         isframeBasedDecodingUsed = ((basicFeature->m_av1PicParams->m_loopRestorationFlags.m_fields.m_yframeRestorationType > 0) &
180                                    ((basicFeature->m_av1PicParams->m_loopRestorationFlags.m_fields.m_cbframeRestorationType |
181                                     basicFeature->m_av1PicParams->m_loopRestorationFlags.m_fields.m_crframeRestorationType) > 0) &&
182                                     basicFeature->m_av1PicParams->m_picInfoFlags.m_fields.m_useSuperres && MEDIA_IS_WA(GetWaTable(), Wa_1409820462)
183                                     || !m_forceTileBasedDecoding);
184     }
185     return isframeBasedDecodingUsed;
186 }
187 
CreateFeatureManager()188 MOS_STATUS Av1PipelineG12_Base::CreateFeatureManager()
189 {
190     DECODE_FUNC_CALL();
191     m_featureManager = MOS_New(DecodeAv1FeatureManagerG12_Base, m_allocator, m_hwInterface, m_osInterface);
192     DECODE_CHK_NULL(m_featureManager);
193     return MOS_STATUS_SUCCESS;
194 }
195 
CreateSubPackets(DecodeSubPacketManager & subPacketManager,CodechalSetting & codecSettings)196 MOS_STATUS Av1PipelineG12_Base::CreateSubPackets(DecodeSubPacketManager &subPacketManager, CodechalSetting &codecSettings)
197 {
198     DECODE_FUNC_CALL();
199 
200     DecodePredicationPktG12 *predicationPkt = MOS_New(DecodePredicationPktG12, this, m_hwInterface);
201     DECODE_CHK_NULL(predicationPkt);
202     DECODE_CHK_STATUS(subPacketManager.Register(
203         DecodePacketId(this, predicationSubPacketId), *predicationPkt));
204 
205     DecodeMarkerPktG12 *markerPkt = MOS_New(DecodeMarkerPktG12, this, m_hwInterface);
206     DECODE_CHK_NULL(markerPkt);
207     DECODE_CHK_STATUS(subPacketManager.Register(
208         DecodePacketId(this, markerSubPacketId), *markerPkt));
209 
210     return MOS_STATUS_SUCCESS;
211 }
212 
GetDecodeMode()213 Av1PipelineG12_Base::Av1DecodeMode Av1PipelineG12_Base::GetDecodeMode()
214 {
215     return m_decodeMode;
216 }
217 
CreatePostSubPipeLines(DecodeSubPipelineManager & subPipelineManager)218 MOS_STATUS Av1PipelineG12_Base::CreatePostSubPipeLines(DecodeSubPipelineManager &subPipelineManager)
219 {
220     DECODE_FUNC_CALL();
221 
222 #ifdef _DECODE_PROCESSING_SUPPORTED
223     auto sfcHistogramPostSubPipeline = MOS_New(DecodeSfcHistogramSubPipelineM12, this, m_task, m_numVdbox, m_hwInterface);
224     DECODE_CHK_NULL(sfcHistogramPostSubPipeline);
225     DECODE_CHK_STATUS(m_postSubPipeline->Register(*sfcHistogramPostSubPipeline));
226 #endif
227 
228     return MOS_STATUS_SUCCESS;
229 }
230 
CreatePreSubPipeLines(DecodeSubPipelineManager & subPipelineManager)231 MOS_STATUS Av1PipelineG12_Base::CreatePreSubPipeLines(DecodeSubPipelineManager &subPipelineManager)
232 {
233     m_bitstream = MOS_New(DecodeInputBitstreamM12, this, m_task, m_numVdbox, m_hwInterface);
234     DECODE_CHK_NULL(m_bitstream);
235     DECODE_CHK_STATUS(subPipelineManager.Register(*m_bitstream));
236 
237     m_streamout = MOS_New(DecodeStreamOutM12, this, m_task, m_numVdbox, m_hwInterface);
238     DECODE_CHK_NULL(m_streamout);
239     DECODE_CHK_STATUS(subPipelineManager.Register(*m_streamout));
240     return MOS_STATUS_SUCCESS;
241 }
242 
243 #if USE_CODECHAL_DEBUG_TOOL
DumpParams(Av1BasicFeatureG12 & basicFeature)244 MOS_STATUS Av1PipelineG12_Base::DumpParams(Av1BasicFeatureG12 &basicFeature)
245 {
246     m_debugInterface->m_frameType          = basicFeature.m_av1PicParams->m_picInfoFlags.m_fields.m_frameType ? P_TYPE : I_TYPE;
247     m_debugInterface->m_bufferDumpFrameNum = basicFeature.m_frameNum;
248 
249     DECODE_CHK_STATUS(DumpPicParams(basicFeature.m_av1PicParams));
250     DECODE_CHK_STATUS(DumpTileParams(basicFeature.m_av1TileParams, basicFeature.m_tileCoding.m_numTiles));
251     DECODE_CHK_STATUS(DumpBitstream(&basicFeature.m_resDataBuffer.OsResource, basicFeature.m_dataSize, 0));
252 
253     return MOS_STATUS_SUCCESS;
254 }
255 
DumpPicParams(CodecAv1PicParams * picParams)256 MOS_STATUS Av1PipelineG12_Base::DumpPicParams(CodecAv1PicParams *picParams)
257 {
258     CODECHAL_DEBUG_FUNCTION_ENTER;
259 
260     if (picParams == nullptr)
261     {
262         return MOS_STATUS_SUCCESS;
263     }
264 
265     if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrPicParams))
266     {
267         const char *fileName = m_debugInterface->CreateFileName(
268             "DEC",
269             CodechalDbgBufferType::bufPicParams,
270             CodechalDbgExtType::txt);
271 
272         if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrEnableFastDump))
273         {
274             MediaDebugFastDump::Dump(
275                 (uint8_t *)picParams,
276                 fileName,
277                 sizeof(CodecAv1PicParams),
278                 0,
279                 MediaDebugSerializer<CodecAv1PicParams>());
280         }
281         else
282         {
283             DumpDecodeAv1PicParams(picParams, fileName);
284         }
285     }
286 
287     return MOS_STATUS_SUCCESS;
288 }
289 
DumpTileParams(CodecAv1TileParams * tileParams,uint32_t tileNum)290 MOS_STATUS Av1PipelineG12_Base::DumpTileParams(CodecAv1TileParams *tileParams, uint32_t tileNum)
291 {
292     CODECHAL_DEBUG_FUNCTION_ENTER;
293 
294     if (tileParams == nullptr)
295     {
296         return MOS_STATUS_SUCCESS;
297     }
298 
299     if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrSlcParams))
300     {
301         const char *fileName = m_debugInterface->CreateFileName(
302             "DEC",
303             "TileParams",
304             CodechalDbgExtType::txt);
305 
306         if (m_debugInterface->DumpIsEnabled(CodechalDbgAttr::attrEnableFastDump))
307         {
308             MediaDebugFastDump::Dump(
309                 (uint8_t *)tileParams,
310                 fileName,
311                 sizeof(CodecAv1TileParams) * tileNum,
312                 0,
313                 MediaDebugSerializer<CodecAv1TileParams>());
314         }
315         else
316         {
317             DumpDecodeAv1TileParams(tileParams, tileNum, fileName);
318         }
319     }
320 
321     return MOS_STATUS_SUCCESS;
322 }
323 #endif
324 
325 }
326