Debian Codegraph @

media_softlet/agnostic/common/codec/hal/dec/avc/packet/decode_avc_packet.cpp

raw
1 /*
2 * Copyright (c) 2018-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 decode_avc_packet.cpp
24 //! \brief Defines the interface for avc decode packet.
25 //!
26
27 #include "decode_avc_packet.h"
28 #include "decode_utils.h"
29 #include "decode_marker_packet.h"
30 #include "decode_status_report_defs.h"
31 #include "decode_predication_packet.h"
32 #include "codechal_debug.h"
33 #if (_DEBUG || _RELEASE_INTERNAL)
34 #include "decode_avc_debug_packet.h"
35 #endif
36
37 namespace decode
38 {
39 MOS_STATUS AvcDecodePkt::Init()
40 {
41 DECODE_FUNC_CALL();
42 DECODE_CHK_NULL(m_miItf);
43 DECODE_CHK_NULL(m_statusReport);
44 DECODE_CHK_NULL(m_featureManager);
45 DECODE_CHK_NULL(m_avcPipeline);
46 DECODE_CHK_NULL(m_osInterface);
47
48 DECODE_CHK_STATUS(CmdPacket::Init());
49
50 m_avcBasicFeature = dynamic_cast<AvcBasicFeature *>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
51 DECODE_CHK_NULL(m_avcBasicFeature);
52
53 m_allocator = m_avcPipeline->GetDecodeAllocator();
54 DECODE_CHK_NULL(m_allocator);
55
56 DECODE_CHK_STATUS(m_statusReport->RegistObserver(this));
57
58 DecodeSubPacket *subPacket = m_avcPipeline->GetSubPacket(DecodePacketId(m_avcPipeline, avcPictureSubPacketId));
59
60 m_picturePkt = dynamic_cast<AvcDecodePicPkt *>(subPacket);
61 DECODE_CHK_NULL(m_picturePkt);
62 DECODE_CHK_STATUS(m_picturePkt->CalculateCommandSize(m_pictureStatesSize, m_picturePatchListSize));
63
64 subPacket = m_avcPipeline->GetSubPacket(DecodePacketId(m_avcPipeline, avcSliceSubPacketId));
65 m_slicePkt = dynamic_cast<AvcDecodeSlcPkt *>(subPacket);
66 DECODE_CHK_NULL(m_slicePkt);
67 DECODE_CHK_STATUS(m_slicePkt->CalculateCommandSize(m_sliceStatesSize, m_slicePatchListSize));
68
69 #if (_DEBUG || _RELEASE_INTERNAL)
70 // Initialize debug packet
71 DecodeSubPacket* debugSubPacket = m_avcPipeline->GetSubPacket(
72 DecodePacketId(m_avcPipeline, avcDebugSubPacketId));
73 m_debugPkt = dynamic_cast<AvcDecodeDebugPkt*>(debugSubPacket);
74 if (m_debugPkt != nullptr)
75 {
76 DECODE_CHK_STATUS(m_debugPkt->Init());
77 }
78 #endif
79
80 return MOS_STATUS_SUCCESS;
81 }
82
83 MOS_STATUS AvcDecodePkt::Prepare()
84 {
85 DECODE_FUNC_CALL();
86
87 DECODE_CHK_NULL(m_avcBasicFeature->m_avcPicParams);
88 m_avcPicParams = m_avcBasicFeature->m_avcPicParams;
89
90 return MOS_STATUS_SUCCESS;
91 }
92
93 MOS_STATUS AvcDecodePkt::Destroy()
94 {
95 m_statusReport->UnregistObserver(this);
96
97 #if (_DEBUG || _RELEASE_INTERNAL)
98 // Destroy debug packet
99 if (m_debugPkt != nullptr)
100 {
101 DECODE_CHK_STATUS(m_debugPkt->Destroy());
102 }
103 #endif
104
105 return MOS_STATUS_SUCCESS;
106 }
107
108 void AvcDecodePkt::SetPerfTag(CODECHAL_MODE mode, uint16_t picCodingType)
109 {
110 DECODE_FUNC_CALL();
111 uint16_t perfTag = ((mode << 4) & 0xF0) | (picCodingType & 0xF);
112
113 m_osInterface->pfnIncPerfFrameID(m_osInterface);
114 m_osInterface->pfnSetPerfTag(m_osInterface, perfTag);
115 m_osInterface->pfnResetPerfBufferID(m_osInterface);
116 }
117
118 bool AvcDecodePkt::IsPrologRequired()
119 {
120 return true;
121 }
122
123 MOS_STATUS AvcDecodePkt::AddForceWakeup(MOS_COMMAND_BUFFER &cmdBuffer)
124 {
125 DECODE_FUNC_CALL();
126 // NativeFence sync BB may cause preemption and ring idle, so force wakeup after it
127 DECODE_CHK_STATUS(AddNativeFenceSyncBBStartCmd(cmdBuffer));
128
129 auto &par = m_miItf->GETPAR_MI_FORCE_WAKEUP();
130 MOS_ZeroMemory(&par, sizeof(mhw::mi::MI_FORCE_WAKEUP_PAR));
131 par.bMFXPowerWellControl = true;
132 par.bMFXPowerWellControlMask = true;
133 par.bHEVCPowerWellControl = false;
134 par.bHEVCPowerWellControlMask = true;
135
136 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_FORCE_WAKEUP(&cmdBuffer));
137
138 return MOS_STATUS_SUCCESS;
139 }
140
141 MOS_STATUS AvcDecodePkt::SendPrologWithFrameTracking(MOS_COMMAND_BUFFER &cmdBuffer, bool frameTrackingRequested)
142 {
143 DecodeSubPacket *subPacket = m_avcPipeline->GetSubPacket(DecodePacketId(m_avcPipeline, markerSubPacketId));
144 DecodeMarkerPkt *makerPacket = dynamic_cast<DecodeMarkerPkt *>(subPacket);
145 DECODE_CHK_NULL(makerPacket);
146 DECODE_CHK_STATUS(makerPacket->Execute(cmdBuffer));
147
148 m_mmcState = m_avcPipeline->GetMmcState();
149 bool isMmcEnabled = (m_mmcState != nullptr && m_mmcState->IsMmcEnabled());
150 if (isMmcEnabled)
151 {
152 DECODE_CHK_STATUS(m_mmcState->SendPrologCmd(&cmdBuffer, false));
153 }
154
155 MHW_GENERIC_PROLOG_PARAMS genericPrologParams;
156 MOS_ZeroMemory(&genericPrologParams, sizeof(genericPrologParams));
157 genericPrologParams.pOsInterface = m_osInterface;
158 genericPrologParams.pvMiInterface = nullptr;
159
160 genericPrologParams.bMmcEnabled = isMmcEnabled;
161
162 DECODE_CHK_STATUS(Mhw_SendGenericPrologCmdNext(&cmdBuffer, &genericPrologParams, m_miItf));
163
164 subPacket = m_avcPipeline->GetSubPacket(DecodePacketId(m_avcPipeline, predicationSubPacketId));
165 DecodePredicationPkt *predicationPacket = dynamic_cast<DecodePredicationPkt *>(subPacket);
166 DECODE_CHK_NULL(predicationPacket);
167 DECODE_CHK_STATUS(predicationPacket->Execute(cmdBuffer));
168
169 return MOS_STATUS_SUCCESS;
170 }
171
172 MOS_STATUS AvcDecodePkt::MiFlush(MOS_COMMAND_BUFFER &cmdBuffer)
173 {
174 DECODE_FUNC_CALL();
175
176 auto &par = m_miItf->GETPAR_MI_FLUSH_DW();
177 MOS_ZeroMemory(&par, sizeof(mhw::mi::MI_FLUSH_DW_PAR));
178
179 // Add Video Pipeline Cache Invalidate when CRC output is enabled
180 if (m_avcPipeline && m_avcPipeline->GetMemDataAccessCrcOutputEnable())
181 {
182 par.bVideoPipelineCacheInvalidate = true;
183 }
184
185 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_FLUSH_DW(&cmdBuffer));
186
187 return MOS_STATUS_SUCCESS;
188 }
189
190 MOS_STATUS AvcDecodePkt::Completed(void *mfxStatus, void *rcsStatus, void *statusReport)
191 {
192 DECODE_FUNC_CALL();
193
194 DECODE_CHK_NULL(mfxStatus);
195 DECODE_CHK_NULL(statusReport);
196 DECODE_CHK_NULL(m_avcBasicFeature);
197
198 DecodeStatusMfx * decodeStatusMfx = (DecodeStatusMfx *)mfxStatus;
199 DecodeStatusReportData *statusReportData = (DecodeStatusReportData *)statusReport;
200 auto mfxItf = std::static_pointer_cast<mhw::vdbox::mfx::Itf>(m_hwInterface->GetMfxInterfaceNext());
201
202 if (mfxItf)
203 {
204 if ((decodeStatusMfx->m_mmioErrorStatusReg & mfxItf->GetMfxErrorFlagsMask()) != 0)
205 {
206 statusReportData->codecStatus = CODECHAL_STATUS_ERROR;
207 statusReportData->numMbsAffected = decodeStatusMfx->m_mmioMBCountReg & 0xFFFF;
208 }
209 statusReportData->frameCrc = decodeStatusMfx->m_mmioFrameCrcReg;
210 }
211
212 DECODE_VERBOSEMESSAGE("Current Frame Index = %d", statusReportData->currDecodedPic.FrameIdx);
213 DECODE_VERBOSEMESSAGE("FrameCrc = 0x%x", statusReportData->frameCrc);
214
215 #if (_DEBUG || _RELEASE_INTERNAL)
216 // Call debug packet completion handling for MFX debug functionality
217 if (m_debugPkt != nullptr)
218 {
219 DECODE_CHK_STATUS(m_debugPkt->Completed(mfxStatus));
220 }
221 #endif
222
223 return MOS_STATUS_SUCCESS;
224 }
225
226 MOS_STATUS AvcDecodePkt::CalculateCommandSize(uint32_t &commandBufferSize, uint32_t &requestedPatchListSize)
227 {
228 commandBufferSize = CalculateCommandBufferSize();
229 requestedPatchListSize = CalculatePatchListSize();
230
231 return MOS_STATUS_SUCCESS;
232 }
233
234 uint32_t AvcDecodePkt::CalculateCommandBufferSize()
235 {
236 uint32_t commandBufferSize = 0;
237
238 // plus 1 is for phantom slice command
239 commandBufferSize = m_pictureStatesSize + m_sliceStatesSize * (m_avcBasicFeature->m_numSlices + 1);
240
241 return (commandBufferSize + COMMAND_BUFFER_RESERVED_SPACE);
242 }
243
244 uint32_t AvcDecodePkt::CalculatePatchListSize()
245 {
246 if (!m_osInterface->bUsesPatchList)
247 {
248 return 0;
249 }
250
251 uint32_t requestedPatchListSize = 0;
252 requestedPatchListSize = m_picturePatchListSize + m_slicePatchListSize * (m_avcBasicFeature->m_numSlices + 1);
253
254 return requestedPatchListSize;
255 }
256
257 MOS_STATUS AvcDecodePkt::ReadMfxStatus(MediaStatusReport *statusReport, MOS_COMMAND_BUFFER &cmdBuffer)
258 {
259 DECODE_FUNC_CALL();
260
261 DECODE_CHK_NULL(statusReport);
262
263 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
264 MOS_RESOURCE *osResource = nullptr;
265 uint32_t offset = 0;
266
267 auto &par = m_miItf->GETPAR_MI_STORE_REGISTER_MEM();
268 par = {};
269
270 DECODE_CHK_NULL(m_hwInterface->GetVdencInterfaceNext());
271 auto mmioRegisters = m_hwInterface->GetVdencInterfaceNext()->GetMmioRegisters(MHW_VDBOX_NODE_1);
272
273 DECODE_CHK_STATUS(statusReport->GetAddress(decode::DecodeStatusReportType::DecErrorStatusOffset, osResource, offset));
274 par.presStoreBuffer = osResource;
275 par.dwOffset = offset;
276 par.dwRegister = mmioRegisters->mfxErrorFlagsRegOffset;
277
278 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer));
279
280 // Skip CRC MMIO register read when CRC output is enabled
281 // When CRC debug mode is enabled, debug packet Execute() handles CRC data collection via MFX_MEM_DATA_ACCESS
282 if (!m_avcPipeline->GetMemDataAccessCrcOutputEnable())
283 {
284 DECODE_CHK_STATUS(statusReport->GetAddress(decode::DecodeStatusReportType::DecFrameCrcOffset, osResource, offset));
285 par.presStoreBuffer = osResource;
286 par.dwOffset = offset;
287 par.dwRegister = mmioRegisters->mfxFrameCrcRegOffset;
288
289 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer));
290 }
291
292 DECODE_CHK_STATUS(statusReport->GetAddress(decode::DecodeStatusReportType::DecMBCountOffset, osResource, offset));
293 par.presStoreBuffer = osResource;
294 par.dwOffset = offset;
295 par.dwRegister = mmioRegisters->mfxMBCountRegOffset;
296
297 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer));
298
299 return eStatus;
300 }
301
302 MOS_STATUS AvcDecodePkt::StartStatusReport(uint32_t srType, MOS_COMMAND_BUFFER *cmdBuffer)
303 {
304 DECODE_FUNC_CALL();
305
306 DECODE_CHK_STATUS(MediaPacket::StartStatusReportNext(srType, cmdBuffer));
307
308 SetPerfTag(CODECHAL_DECODE_MODE_AVCVLD, m_avcBasicFeature->m_pictureCodingType);
309
310 MediaPerfProfiler *perfProfiler = MediaPerfProfiler::Instance();
311 DECODE_CHK_NULL(perfProfiler);
312 DECODE_CHK_STATUS(perfProfiler->AddPerfCollectStartCmd(
313 (void *)m_avcPipeline, m_osInterface, m_miItf, cmdBuffer));
314 #if (_DEBUG || _RELEASE_INTERNAL)
315 if (m_statusReport && m_statusReport->IsVdboxIdReportEnabled())
316 {
317 StoreEngineId(cmdBuffer, decode::DecodeStatusReportType::CsEngineIdOffset_0);
318 }
319 // Add command counter commands for debug
320 if (m_debugPkt != nullptr)
321 {
322 DECODE_CHK_STATUS(m_debugPkt->AddCommandCounterCmds(*cmdBuffer, m_statusReport));
323 }
324 #endif
325 return MOS_STATUS_SUCCESS;
326 }
327
328 MOS_STATUS AvcDecodePkt::EndStatusReport(uint32_t srType, MOS_COMMAND_BUFFER *cmdBuffer)
329 {
330 DECODE_FUNC_CALL();
331 DECODE_CHK_NULL(cmdBuffer);
332 #if (_DEBUG || _RELEASE_INTERNAL)
333 // Execute debug packet BEFORE ReadMfxStatus when CRC output is enabled
334 // This ensures CRC data and MI_FLUSH commands from debug packet are inserted before status reads
335 if (m_debugPkt != nullptr)
336 {
337 DECODE_CHK_STATUS(m_debugPkt->Execute(*cmdBuffer, m_statusReport));
338 }
339 #endif
340 DECODE_CHK_STATUS(ReadMfxStatus(m_statusReport, *cmdBuffer));
341 DECODE_CHK_STATUS(MediaPacket::EndStatusReportNext(srType, cmdBuffer));
342
343 MediaPerfProfiler *perfProfiler = MediaPerfProfiler::Instance();
344 DECODE_CHK_NULL(perfProfiler);
345 DECODE_CHK_STATUS(perfProfiler->AddPerfCollectEndCmd(
346 (void *)m_avcPipeline, m_osInterface, m_miItf, cmdBuffer));
347
348 // Add Mi flush here to ensure end status tag flushed to memory earlier than completed count
349 DECODE_CHK_STATUS(MiFlush(*cmdBuffer));
350
351 return MOS_STATUS_SUCCESS;
352 }
353
354 MOS_STATUS AvcDecodePkt::SetCencBatchBuffer(
355 PMOS_COMMAND_BUFFER cmdBuffer)
356 {
357 DECODE_FUNC_CALL();
358 DECODE_CHK_NULL(cmdBuffer);
359
360 auto cencBuf = m_avcBasicFeature->m_cencBuf;
361 DECODE_CHK_NULL(cencBuf);
362 MHW_BATCH_BUFFER batchBuffer;
363 MOS_ZeroMemory(&batchBuffer, sizeof(MHW_BATCH_BUFFER));
364 MOS_RESOURCE *resHeap = nullptr;
365 DECODE_CHK_NULL(resHeap = cencBuf->secondLvlBbBlock->GetResource());
366 batchBuffer.OsResource = *resHeap;
367 batchBuffer.dwOffset = cencBuf->secondLvlBbBlock->GetOffset();
368 batchBuffer.iSize = cencBuf->secondLvlBbBlock->GetSize();
369 batchBuffer.bSecondLevel = true;
370 #if (_DEBUG || _RELEASE_INTERNAL)
371 batchBuffer.iLastCurrent = batchBuffer.iSize;
372 #endif // (_DEBUG || _RELEASE_INTERNAL)
373
374 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_BATCH_BUFFER_START(cmdBuffer, &batchBuffer));
375
376 #if USE_CODECHAL_DEBUG_TOOL
377 CODECHAL_DEBUG_TOOL(
378 CodechalDebugInterface *debugInterface = m_avcPipeline->GetDebugInterface();
379 DECODE_CHK_STATUS(debugInterface->Dump2ndLvlBatch(
380 &batchBuffer,
381 CODECHAL_NUM_MEDIA_STATES,
382 "_2ndLvlBatch"));)
383 #endif
384 // Update GlobalCmdBufId
385 auto &par = m_miItf->GETPAR_MI_STORE_DATA_IMM();
386 par = {};
387 par.pOsResource = cencBuf->resTracker;
388 par.dwValue = cencBuf->trackerId;
389 DECODE_NORMALMESSAGE("dwCmdBufId = %d", par.dwValue);
390 DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_DATA_IMM(cmdBuffer));
391
392 return MOS_STATUS_SUCCESS;
393 }
394
395 } // namespace decode