1 /*
2 * Copyright (c) 2017, 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     codechal_mmc_decode_avc.cpp
24 //! \brief    Impelements the public interface for CodecHal Media Memory Compression
25 //!
26 
27 #include "codechal_mmc_decode_avc.h"
28 
CodechalMmcDecodeAvc(CodechalHwInterface * hwInterface,void * standardState)29 CodechalMmcDecodeAvc::CodechalMmcDecodeAvc(
30     CodechalHwInterface    *hwInterface,
31     void *standardState):
32     CodecHalMmcState(hwInterface)
33 {
34     CODECHAL_DECODE_FUNCTION_ENTER;
35 
36     m_avcState = (CodechalDecodeAvc*)standardState;
37     CODECHAL_HW_ASSERT(m_avcState);
38 
39     CODECHAL_HW_ASSERT(hwInterface);
40     CODECHAL_HW_ASSERT(hwInterface->GetSkuTable());
41     if (MEDIA_IS_SKU(hwInterface->GetSkuTable(), FtrMemoryCompression))
42     {
43         MOS_USER_FEATURE_VALUE_DATA userFeatureData;
44         MOS_ZeroMemory(&userFeatureData, sizeof(userFeatureData));
45         userFeatureData.i32Data = m_mmcEnabled;
46         userFeatureData.i32DataFlag = MOS_USER_FEATURE_VALUE_DATA_FLAG_CUSTOM_DEFAULT_VALUE_TYPE;
47 
48         MOS_UserFeature_ReadValue_ID(
49             nullptr,
50             __MEDIA_USER_FEATURE_VALUE_DECODE_MMC_ENABLE_ID,
51             &userFeatureData);
52         m_mmcEnabled = (userFeatureData.i32Data) ? true : false;
53 
54         MOS_USER_FEATURE_VALUE_WRITE_DATA userFeatureWriteData;
55         MOS_ZeroMemory(&userFeatureWriteData, sizeof(userFeatureWriteData));
56         userFeatureWriteData.Value.i32Data = m_mmcEnabled;
57         userFeatureWriteData.ValueID = __MEDIA_USER_FEATURE_VALUE_DECODE_MMC_IN_USE_ID;
58         MOS_UserFeature_WriteValues_ID(nullptr, &userFeatureWriteData, 1);
59     }
60 
61 #if (_DEBUG || _RELEASE_INTERNAL)
62     m_compressibleId  = __MEDIA_USER_FEATURE_VALUE_MMC_DEC_RT_COMPRESSIBLE_ID;
63     m_compressModeId  = __MEDIA_USER_FEATURE_VALUE_MMC_DEC_RT_COMPRESSMODE_ID;
64 #endif
65 }
66 
SetPipeBufAddr(PMHW_VDBOX_PIPE_BUF_ADDR_PARAMS pipeBufAddrParams,PMOS_COMMAND_BUFFER cmdBuffer)67 MOS_STATUS CodechalMmcDecodeAvc::SetPipeBufAddr(
68     PMHW_VDBOX_PIPE_BUF_ADDR_PARAMS pipeBufAddrParams,
69     PMOS_COMMAND_BUFFER cmdBuffer)
70 {
71     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
72 
73     CODECHAL_DECODE_FUNCTION_ENTER;
74 
75     CODECHAL_DECODE_CHK_NULL_RETURN(m_avcState->m_avcPicParams);
76 
77     if (m_mmcEnabled &&
78         m_avcState->m_destSurface.bCompressible &&
79         !m_avcState->m_avcPicParams->pic_fields.field_pic_flag &&
80         !m_avcState->m_avcPicParams->seq_fields.mb_adaptive_frame_field_flag &&
81         (m_avcState->m_avcPicParams->seq_fields.chroma_format_idc != avcChromaFormatMono))
82     {
83         if (m_avcState->m_deblockingEnabled)
84         {
85             pipeBufAddrParams->PostDeblockSurfMmcState = MOS_MEMCOMP_HORIZONTAL;
86         }
87         else
88         {
89             pipeBufAddrParams->PreDeblockSurfMmcState = MOS_MEMCOMP_VERTICAL;
90         }
91     }
92 
93     CODECHAL_DEBUG_TOOL(
94         m_avcState->m_destSurface.MmcState = m_avcState->m_deblockingEnabled ? pipeBufAddrParams->PostDeblockSurfMmcState : pipeBufAddrParams->PreDeblockSurfMmcState;)
95 
96     return eStatus;
97 }
98 
SetRefrenceSync(bool disableDecodeSyncLock,bool disableLockForTranscode)99 MOS_STATUS CodechalMmcDecodeAvc::SetRefrenceSync(
100     bool disableDecodeSyncLock,
101     bool disableLockForTranscode)
102 {
103     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
104 
105     CODECHAL_DECODE_FUNCTION_ENTER;
106 
107     CODECHAL_DECODE_CHK_NULL_RETURN(m_avcState->m_avcPicParams);
108 
109     if ((!CodecHal_PictureIsField(m_avcState->m_avcPicParams->CurrPic) ||
110             !m_avcState->m_isSecondField) &&
111         m_mmcEnabled)
112     {
113         MOS_SYNC_PARAMS syncParams = g_cInitSyncParams;
114         syncParams.GpuContext = m_avcState->GetVideoContext();
115         syncParams.bDisableDecodeSyncLock = disableDecodeSyncLock;
116         syncParams.bDisableLockForTranscode = disableLockForTranscode;
117 
118         for (int i = 0; i < CODEC_AVC_MAX_NUM_REF_FRAME; i++)
119         {
120             if (m_avcState->m_presReferences[i])
121             {
122                 syncParams.presSyncResource = m_avcState->m_presReferences[i];
123                 syncParams.bReadOnly = true;
124                 CODECHAL_DECODE_CHK_STATUS_RETURN(
125                     m_osInterface->pfnPerformOverlaySync(
126                         m_osInterface,
127                         &syncParams));
128                 CODECHAL_DECODE_CHK_STATUS_RETURN(
129                     m_osInterface->pfnResourceWait(
130                         m_osInterface,
131                         &syncParams));
132                 m_osInterface->pfnSetResourceSyncTag(
133                     m_osInterface,
134                     &syncParams);
135             }
136         }
137     }
138 
139     return eStatus;
140 }
141 
CheckReferenceList(PMHW_VDBOX_PIPE_BUF_ADDR_PARAMS pipeBufAddrParams)142 MOS_STATUS CodechalMmcDecodeAvc::CheckReferenceList(
143         PMHW_VDBOX_PIPE_BUF_ADDR_PARAMS pipeBufAddrParams)
144 {
145     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
146 
147     CODECHAL_DECODE_FUNCTION_ENTER;
148 
149     CODECHAL_DECODE_CHK_NULL_RETURN(pipeBufAddrParams);
150     CODECHAL_DECODE_CHK_NULL_RETURN(m_avcState->m_avcPicParams);
151 
152     if ((pipeBufAddrParams->PostDeblockSurfMmcState != MOS_MEMCOMP_DISABLED ||
153             pipeBufAddrParams->PreDeblockSurfMmcState != MOS_MEMCOMP_DISABLED) &&
154         !m_avcState->m_avcPicParams->pic_fields.IntraPicFlag)
155     {
156         uint8_t activeFrameCnt = 0;
157         bool selfReference = false;
158 
159         for (uint8_t i = 0; i < CODEC_AVC_MAX_NUM_REF_FRAME; i++)
160         {
161             if (m_avcState->m_avcPicIdx[i].bValid)
162             {
163                 activeFrameCnt++;
164 
165                 // Self reference by picture parameter
166                 if (m_avcState->m_avcPicParams->CurrPic.FrameIdx == m_avcState->m_avcPicParams->RefFrameList[i].FrameIdx)
167                 {
168                     selfReference = true;
169                     break;
170                 }
171             }
172         }
173 
174         // Error handling: application sent the wrong picture parameter
175         if (activeFrameCnt == 0)
176         {
177             selfReference = true;
178         }
179 
180         if (selfReference)
181         {
182             pipeBufAddrParams->PostDeblockSurfMmcState = MOS_MEMCOMP_DISABLED;
183             pipeBufAddrParams->PreDeblockSurfMmcState = MOS_MEMCOMP_DISABLED;
184             CODECHAL_DECODE_ASSERTMESSAGE("Self-reference is detected for P/B frames!");
185 
186             // Decompress current frame to avoid green corruptions in this error handling case
187             MOS_MEMCOMP_STATE mmcMode;
188             CODECHAL_DECODE_CHK_STATUS_RETURN(
189                 m_osInterface->pfnGetMemoryCompressionMode(
190                     m_osInterface,
191                     &m_avcState->m_destSurface.OsResource,
192                     &mmcMode));
193             if (mmcMode != MOS_MEMCOMP_DISABLED)
194             {
195                 m_osInterface->pfnDecompResource(
196                     m_osInterface,
197                     &m_avcState->m_destSurface.OsResource);
198             }
199         }
200     }
201 
202     return eStatus;
203 }
204