| 1 | /* |
| 2 | * Copyright (c) 2022-2023, 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_vp9_packet.cpp |
| 24 | //! \brief Defines the interface for vp9 decode packet |
| 25 | //! |
| 26 | #include "decode_vp9_packet.h" |
| 27 | #include "decode_status_report_defs.h" |
| 28 | #include "decode_predication_packet.h" |
| 29 | #include "decode_marker_packet.h" |
| 30 | |
| 31 | namespace decode { |
| 32 | |
| 33 | Vp9DecodePkt::Vp9DecodePkt(MediaPipeline *pipeline, MediaTask *task, CodechalHwInterfaceNext *hwInterface) |
| 34 | : CmdPacket(task) |
| 35 | { |
| 36 | if (pipeline != nullptr) |
| 37 | { |
| 38 | m_statusReport = pipeline->GetStatusReportInstance(); |
| 39 | m_featureManager = pipeline->GetFeatureManager(); |
| 40 | m_vp9Pipeline = dynamic_cast<Vp9Pipeline *>(pipeline); |
| 41 | } |
| 42 | if (hwInterface != nullptr) |
| 43 | { |
| 44 | m_hwInterface = hwInterface; |
| 45 | m_miItf = std::static_pointer_cast<mhw::mi::Itf>(hwInterface->GetMiInterfaceNext()); |
| 46 | m_osInterface = hwInterface->GetOsInterface(); |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | MOS_STATUS Vp9DecodePkt::Init() |
| 51 | { |
| 52 | DECODE_FUNC_CALL(); |
| 53 | DECODE_CHK_NULL(m_miItf); |
| 54 | DECODE_CHK_NULL(m_statusReport); |
| 55 | DECODE_CHK_NULL(m_featureManager); |
| 56 | DECODE_CHK_NULL(m_vp9Pipeline); |
| 57 | DECODE_CHK_NULL(m_osInterface); |
| 58 | |
| 59 | DECODE_CHK_STATUS(CmdPacket::Init()); |
| 60 | |
| 61 | m_vp9BasicFeature = dynamic_cast<Vp9BasicFeature*>(m_featureManager->GetFeature(FeatureIDs::basicFeature)); |
| 62 | DECODE_CHK_NULL(m_vp9BasicFeature); |
| 63 | |
| 64 | m_allocator = m_vp9Pipeline->GetDecodeAllocator(); |
| 65 | DECODE_CHK_NULL(m_allocator); |
| 66 | |
| 67 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 68 | // Initialize debug packet |
| 69 | DecodeSubPacket* subPacket = m_vp9Pipeline->GetSubPacket( |
| 70 | DecodePacketId(m_vp9Pipeline, vp9DebugSubPacketId)); |
| 71 | m_debugPkt = dynamic_cast<Vp9DecodeDebugPkt*>(subPacket); |
| 72 | if (m_debugPkt != nullptr) |
| 73 | { |
| 74 | DECODE_CHK_STATUS(m_debugPkt->Init()); |
| 75 | } |
| 76 | #endif |
| 77 | |
| 78 | return MOS_STATUS_SUCCESS; |
| 79 | } |
| 80 | |
| 81 | MOS_STATUS Vp9DecodePkt::Prepare() |
| 82 | { |
| 83 | DECODE_FUNC_CALL(); |
| 84 | |
| 85 | m_phase = static_cast<DecodePhase *>(m_vp9Pipeline->GetComponentState()); |
| 86 | DECODE_CHK_NULL(m_phase); |
| 87 | |
| 88 | DECODE_CHK_NULL(m_vp9BasicFeature); |
| 89 | DECODE_CHK_NULL(m_vp9BasicFeature->m_vp9PicParams); |
| 90 | m_vp9PicParams = m_vp9BasicFeature->m_vp9PicParams; |
| 91 | |
| 92 | return MOS_STATUS_SUCCESS; |
| 93 | } |
| 94 | |
| 95 | MOS_STATUS Vp9DecodePkt::Destroy() |
| 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 | m_statusReport->UnregistObserver(this); |
| 106 | return MOS_STATUS_SUCCESS; |
| 107 | } |
| 108 | |
| 109 | void Vp9DecodePkt::SetPerfTag(CODECHAL_MODE mode, uint16_t picCodingType) |
| 110 | { |
| 111 | DECODE_FUNC_CALL(); |
| 112 | |
| 113 | uint16_t perfTag = ((mode << 4) & 0xF0) | (picCodingType & 0xF); |
| 114 | m_osInterface->pfnIncPerfFrameID(m_osInterface); |
| 115 | m_osInterface->pfnSetPerfTag(m_osInterface, perfTag); |
| 116 | m_osInterface->pfnResetPerfBufferID(m_osInterface); |
| 117 | } |
| 118 | |
| 119 | MOS_STATUS Vp9DecodePkt::AddForceWakeup(MOS_COMMAND_BUFFER& cmdBuffer) |
| 120 | { |
| 121 | DECODE_FUNC_CALL(); |
| 122 | // NativeFence sync BB may cause preemption and ring idle, so force wakeup after it |
| 123 | DECODE_CHK_STATUS(AddNativeFenceSyncBBStartCmd(cmdBuffer)); |
| 124 | |
| 125 | auto &par = m_miItf->GETPAR_MI_FORCE_WAKEUP(); |
| 126 | par = {}; |
| 127 | par.bMFXPowerWellControl = false; |
| 128 | par.bMFXPowerWellControlMask = true; |
| 129 | par.bHEVCPowerWellControl = true; |
| 130 | par.bHEVCPowerWellControlMask = true; |
| 131 | |
| 132 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_FORCE_WAKEUP(&cmdBuffer)); |
| 133 | |
| 134 | return MOS_STATUS_SUCCESS; |
| 135 | } |
| 136 | |
| 137 | MOS_STATUS Vp9DecodePkt::SendPrologWithFrameTracking(MOS_COMMAND_BUFFER& cmdBuffer, bool frameTrackingRequested) |
| 138 | { |
| 139 | DecodeSubPacket* subPacket = m_vp9Pipeline->GetSubPacket(DecodePacketId(m_vp9Pipeline, markerSubPacketId)); |
| 140 | DecodeMarkerPkt *makerPacket = dynamic_cast<DecodeMarkerPkt*>(subPacket); |
| 141 | DECODE_CHK_NULL(makerPacket); |
| 142 | DECODE_CHK_STATUS(makerPacket->Execute(cmdBuffer)); |
| 143 | |
| 144 | DecodeMemComp *mmcState = m_vp9Pipeline->GetMmcState(); |
| 145 | bool isMmcEnabled = (mmcState != nullptr && mmcState->IsMmcEnabled()); |
| 146 | if (isMmcEnabled) |
| 147 | { |
| 148 | DECODE_CHK_STATUS(mmcState->SendPrologCmd(&cmdBuffer, false)); |
| 149 | } |
| 150 | |
| 151 | MHW_GENERIC_PROLOG_PARAMS genericPrologParams; |
| 152 | MOS_ZeroMemory(&genericPrologParams, sizeof(genericPrologParams)); |
| 153 | genericPrologParams.pOsInterface = m_osInterface; |
| 154 | genericPrologParams.pvMiInterface = nullptr; |
| 155 | |
| 156 | genericPrologParams.bMmcEnabled = isMmcEnabled; |
| 157 | |
| 158 | DECODE_CHK_STATUS(Mhw_SendGenericPrologCmdNext(&cmdBuffer, &genericPrologParams, m_miItf)); |
| 159 | |
| 160 | subPacket = m_vp9Pipeline->GetSubPacket(DecodePacketId(m_vp9Pipeline, predicationSubPacketId)); |
| 161 | DecodePredicationPkt *predicationPacket = dynamic_cast<DecodePredicationPkt*>(subPacket); |
| 162 | DECODE_CHK_NULL(predicationPacket); |
| 163 | DECODE_CHK_STATUS(predicationPacket->Execute(cmdBuffer)); |
| 164 | |
| 165 | return MOS_STATUS_SUCCESS; |
| 166 | } |
| 167 | |
| 168 | MOS_STATUS Vp9DecodePkt::MiFlush(MOS_COMMAND_BUFFER &cmdBuffer) |
| 169 | { |
| 170 | DECODE_FUNC_CALL(); |
| 171 | |
| 172 | auto &par = m_miItf->GETPAR_MI_FLUSH_DW(); |
| 173 | par = {}; |
| 174 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_FLUSH_DW(&cmdBuffer)); |
| 175 | |
| 176 | return MOS_STATUS_SUCCESS; |
| 177 | } |
| 178 | |
| 179 | MOS_STATUS Vp9DecodePkt::Completed(void *mfxStatus, void *rcsStatus, void *statusReport) |
| 180 | { |
| 181 | DECODE_FUNC_CALL(); |
| 182 | |
| 183 | DECODE_CHK_NULL(mfxStatus); |
| 184 | DECODE_CHK_NULL(statusReport); |
| 185 | |
| 186 | DecodeStatusMfx * decodeStatusMfx = (DecodeStatusMfx *)mfxStatus; |
| 187 | DecodeStatusReportData *statusReportData = (DecodeStatusReportData *)statusReport; |
| 188 | |
| 189 | std::shared_ptr<mhw::vdbox::hcp::Itf> hcpItf = std::static_pointer_cast<mhw::vdbox::hcp::Itf>(m_hwInterface->GetHcpInterfaceNext()); |
| 190 | if (hcpItf != nullptr) |
| 191 | { |
| 192 | if ((decodeStatusMfx->m_mmioErrorStatusReg & hcpItf->GetHcpCabacErrorFlagsMask()) != 0) |
| 193 | { |
| 194 | statusReportData->codecStatus = CODECHAL_STATUS_ERROR; |
| 195 | statusReportData->numMbsAffected = (decodeStatusMfx->m_mmioMBCountReg & 0xFFFC0000) >> 18; |
| 196 | } |
| 197 | |
| 198 | statusReportData->frameCrc = decodeStatusMfx->m_mmioFrameCrcReg; |
| 199 | } |
| 200 | |
| 201 | DECODE_VERBOSEMESSAGE("Index = %d", statusReportData->currDecodedPic.FrameIdx); |
| 202 | DECODE_VERBOSEMESSAGE("FrameCrc = 0x%x", statusReportData->frameCrc); |
| 203 | |
| 204 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 205 | // Call debug packet completion handling for HCP debug functionality |
| 206 | if (m_debugPkt != nullptr) |
| 207 | { |
| 208 | DECODE_CHK_STATUS(m_debugPkt->Completed(mfxStatus)); |
| 209 | } |
| 210 | #endif |
| 211 | |
| 212 | return MOS_STATUS_SUCCESS; |
| 213 | } |
| 214 | |
| 215 | MOS_STATUS Vp9DecodePkt::ReadVdboxId(MOS_COMMAND_BUFFER &cmdBuffer) |
| 216 | { |
| 217 | DECODE_FUNC_CALL(); |
| 218 | DECODE_CHK_NULL(m_phase); |
| 219 | DECODE_CHK_NULL(m_statusReport); |
| 220 | |
| 221 | uint8_t curPipe = m_phase->GetPipe(); |
| 222 | DECODE_CHK_COND(curPipe >= csInstanceIdMax, "Invalid pipe index."); |
| 223 | uint32_t csEngineIdOffsetIdx = decode::DecodeStatusReportType::CsEngineIdOffset_0 + curPipe; |
| 224 | |
| 225 | auto &par = m_miItf->GETPAR_MI_STORE_REGISTER_MEM(); |
| 226 | par = {}; |
| 227 | |
| 228 | auto mmioRegistersHcp = m_hwInterface->GetHcpInterfaceNext()->GetMmioRegisters(MHW_VDBOX_NODE_1); |
| 229 | |
| 230 | MOS_RESOURCE* osResource = nullptr; |
| 231 | uint32_t offset = 0; |
| 232 | DECODE_CHK_STATUS(m_statusReport->GetAddress(csEngineIdOffsetIdx, osResource, offset)); |
| 233 | par.presStoreBuffer = osResource; |
| 234 | par.dwOffset = offset; |
| 235 | par.dwRegister = mmioRegistersHcp->csEngineIdOffset; |
| 236 | |
| 237 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer)); |
| 238 | |
| 239 | return MOS_STATUS_SUCCESS; |
| 240 | } |
| 241 | |
| 242 | MOS_STATUS Vp9DecodePkt::ReadHcpStatus(MediaStatusReport *statusReport, MOS_COMMAND_BUFFER &cmdBuffer) |
| 243 | { |
| 244 | DECODE_FUNC_CALL(); |
| 245 | |
| 246 | MOS_STATUS eStatus = MOS_STATUS_SUCCESS; |
| 247 | |
| 248 | DECODE_CHK_NULL(statusReport); |
| 249 | |
| 250 | MOS_RESOURCE *osResource; |
| 251 | uint32_t offset; |
| 252 | |
| 253 | auto &par = m_miItf->GETPAR_MI_STORE_REGISTER_MEM(); |
| 254 | par = {}; |
| 255 | |
| 256 | auto mmioRegistersHcp = m_hwInterface->GetHcpInterfaceNext()->GetMmioRegisters(MHW_VDBOX_NODE_1); |
| 257 | |
| 258 | DECODE_CHK_STATUS(statusReport->GetAddress(decode::DecodeStatusReportType::DecErrorStatusOffset, osResource, offset)); |
| 259 | par.presStoreBuffer = osResource; |
| 260 | par.dwOffset = offset; |
| 261 | par.dwRegister = mmioRegistersHcp->hcpCabacStatusRegOffset; |
| 262 | |
| 263 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer)); |
| 264 | |
| 265 | // Skip CRC MMIO register read when CRC output is enabled |
| 266 | // When CRC debug mode is enabled, debug packet Execute() handles CRC data collection via HCP_MEM_DATA_ACCESS |
| 267 | if (!m_vp9Pipeline->GetMemDataAccessCrcOutputEnable()) |
| 268 | { |
| 269 | DECODE_CHK_STATUS(statusReport->GetAddress(decode::DecodeStatusReportType::DecFrameCrcOffset, osResource, offset)); |
| 270 | par.presStoreBuffer = osResource; |
| 271 | par.dwOffset = offset; |
| 272 | par.dwRegister = mmioRegistersHcp->hcpFrameCrcRegOffset; |
| 273 | |
| 274 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer)); |
| 275 | } |
| 276 | |
| 277 | DECODE_CHK_STATUS(statusReport->GetAddress(decode::DecodeStatusReportType::DecMBCountOffset, osResource, offset)); |
| 278 | par.presStoreBuffer = osResource; |
| 279 | par.dwOffset = offset; |
| 280 | par.dwRegister = mmioRegistersHcp->hcpDecStatusRegOffset; |
| 281 | |
| 282 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_STORE_REGISTER_MEM(&cmdBuffer)); |
| 283 | |
| 284 | return MOS_STATUS_SUCCESS; |
| 285 | } |
| 286 | |
| 287 | MOS_STATUS Vp9DecodePkt::StartStatusReport(uint32_t srType, MOS_COMMAND_BUFFER* cmdBuffer) |
| 288 | { |
| 289 | DECODE_FUNC_CALL(); |
| 290 | DECODE_CHK_NULL(cmdBuffer); |
| 291 | DECODE_CHK_STATUS(MediaPacket::StartStatusReportNext(srType, cmdBuffer)); |
| 292 | |
| 293 | SetPerfTag(CODECHAL_DECODE_MODE_VP9VLD, m_vp9BasicFeature->m_pictureCodingType); |
| 294 | |
| 295 | MediaPerfProfiler *perfProfiler = MediaPerfProfiler::Instance(); |
| 296 | DECODE_CHK_NULL(perfProfiler); |
| 297 | DECODE_CHK_STATUS(perfProfiler->AddPerfCollectStartCmd( |
| 298 | (void*)m_vp9Pipeline, m_osInterface, m_miItf, cmdBuffer)); |
| 299 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 300 | if (m_statusReport && m_statusReport->IsVdboxIdReportEnabled()) |
| 301 | { |
| 302 | DECODE_CHK_NULL(m_phase); |
| 303 | StoreEngineId(cmdBuffer, decode::DecodeStatusReportType::CsEngineIdOffset_0, m_phase->GetPipe()); |
| 304 | } |
| 305 | // Add command counter commands for debug |
| 306 | if (m_debugPkt != nullptr) |
| 307 | { |
| 308 | DECODE_CHK_STATUS(m_debugPkt->AddCommandCounterCmds(*cmdBuffer, m_statusReport)); |
| 309 | } |
| 310 | #endif |
| 311 | return MOS_STATUS_SUCCESS; |
| 312 | } |
| 313 | |
| 314 | MOS_STATUS Vp9DecodePkt::EndStatusReport(uint32_t srType, MOS_COMMAND_BUFFER* cmdBuffer) |
| 315 | { |
| 316 | DECODE_FUNC_CALL(); |
| 317 | DECODE_CHK_NULL(cmdBuffer); |
| 318 | #if (_DEBUG || _RELEASE_INTERNAL) |
| 319 | // Execute debug packet BEFORE ReadHcpStatus when CRC output is enabled |
| 320 | // This ensures CRC data and MI_FLUSH commands from debug packet are inserted before status reads |
| 321 | if (m_debugPkt != nullptr) |
| 322 | { |
| 323 | DECODE_CHK_STATUS(m_debugPkt->Execute(*cmdBuffer, m_statusReport)); |
| 324 | } |
| 325 | #endif |
| 326 | DECODE_CHK_STATUS(ReadHcpStatus(m_statusReport, *cmdBuffer)); |
| 327 | DECODE_CHK_STATUS(MediaPacket::EndStatusReportNext(srType, cmdBuffer)); |
| 328 | |
| 329 | MediaPerfProfiler *perfProfiler = MediaPerfProfiler::Instance(); |
| 330 | DECODE_CHK_NULL(perfProfiler); |
| 331 | DECODE_CHK_STATUS(perfProfiler->AddPerfCollectEndCmd( |
| 332 | (void*)m_vp9Pipeline, m_osInterface, m_miItf, cmdBuffer)); |
| 333 | |
| 334 | // Add Mi flush here to ensure end status tag flushed to memory earlier than completed count |
| 335 | DECODE_CHK_STATUS(MiFlush(*cmdBuffer)); |
| 336 | |
| 337 | return MOS_STATUS_SUCCESS; |
| 338 | } |
| 339 | |
| 340 | MOS_STATUS Vp9DecodePkt::EnsureAllCommandsExecuted(MOS_COMMAND_BUFFER &cmdBuffer) |
| 341 | { |
| 342 | DECODE_FUNC_CALL(); |
| 343 | |
| 344 | // Send MI_FLUSH command |
| 345 | auto &par = m_miItf->GETPAR_MI_FLUSH_DW(); |
| 346 | par = {}; |
| 347 | |
| 348 | // Add Video Pipeline Cache Invalidate when CRC output is enabled |
| 349 | if (m_vp9Pipeline && m_vp9Pipeline->GetMemDataAccessCrcOutputEnable()) |
| 350 | { |
| 351 | par.bVideoPipelineCacheInvalidate = true; |
| 352 | } |
| 353 | |
| 354 | DECODE_CHK_NULL(m_vp9Pipeline); |
| 355 | auto *skuTable = m_vp9Pipeline->GetSkuTable(); |
| 356 | if (skuTable && MEDIA_IS_SKU(skuTable, FtrEnablePPCFlush)) |
| 357 | { |
| 358 | // Add PPC fulsh |
| 359 | par.bEnablePPCFlush = true; |
| 360 | } |
| 361 | DECODE_CHK_STATUS(m_miItf->ADDCMD_MI_FLUSH_DW(&cmdBuffer)); |
| 362 | |
| 363 | return MOS_STATUS_SUCCESS; |
| 364 | } |
| 365 | |
| 366 | #if USE_CODECHAL_DEBUG_TOOL |
| 367 | MOS_STATUS Vp9DecodePkt::DumpSecondaryCommandBuffer(MOS_COMMAND_BUFFER &cmdBuffer) |
| 368 | { |
| 369 | DECODE_ASSERT(m_phase != nullptr); |
| 370 | CodechalDebugInterface *debugInterface = m_vp9Pipeline->GetDebugInterface(); |
| 371 | DECODE_CHK_NULL(debugInterface); |
| 372 | std::string cmdName = "DEC_secondary_" + std::to_string(m_phase->GetCmdBufIndex()); |
| 373 | DECODE_CHK_STATUS(debugInterface->DumpCmdBuffer( |
| 374 | &cmdBuffer, CODECHAL_NUM_MEDIA_STATES, cmdName.c_str())); |
| 375 | |
| 376 | return MOS_STATUS_SUCCESS; |
| 377 | } |
| 378 | #endif |
| 379 | |
| 380 | } |