1 /*
2 * Copyright (c) 2020-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_vp8_basic_feature.cpp
24 //! \brief Defines the common interface for decode vp8 parameter
25 //!
26
27 #include "decode_vp8_basic_feature.h"
28 #include "decode_utils.h"
29 #include "decode_allocator.h"
30 #include "decode_resource_auto_lock.h"
31 #include <iostream>
32
33 namespace decode
34 {
Vp8BasicFeature(DecodeAllocator * allocator,void * hwInterface,PMOS_INTERFACE osInterface)35 Vp8BasicFeature::Vp8BasicFeature(DecodeAllocator *allocator, void *hwInterface, PMOS_INTERFACE osInterface) : DecodeBasicFeature(allocator, hwInterface, osInterface)
36 {
37 if (osInterface != nullptr)
38 {
39 m_osInterface = osInterface;
40 }
41
42 MOS_ZeroMemory(&m_destSurface, sizeof(m_destSurface));
43 MOS_ZeroMemory(&m_vp8FrameHead, sizeof(m_vp8FrameHead));
44 MOS_ZeroMemory(&m_resCoefProbBufferExternal, sizeof(m_resCoefProbBufferExternal));
45 }
46
~Vp8BasicFeature()47 Vp8BasicFeature::~Vp8BasicFeature()
48 {
49 if (m_allocator != nullptr)
50 {
51 m_allocator->Destroy(m_resCoefProbBufferInternal);
52 }
53 }
54
Init(void * setting)55 MOS_STATUS Vp8BasicFeature::Init(void *setting)
56 {
57 DECODE_FUNC_CALL();
58
59 PERF_UTILITY_AUTO(__FUNCTION__, PERF_DECODE, PERF_LEVEL_HAL);
60
61 DECODE_CHK_NULL(setting);
62
63 DECODE_CHK_STATUS(DecodeBasicFeature::Init(setting));
64
65 DECODE_CHK_STATUS(m_refFrames.Init(this, *m_allocator));
66
67 CodechalSetting *codecSettings = (CodechalSetting *)setting;
68 m_shortFormatInUse = codecSettings->shortFormatInUse ? true : false;
69
70 return MOS_STATUS_SUCCESS;
71 }
72
Update(void * params)73 MOS_STATUS Vp8BasicFeature::Update(void *params)
74 {
75 DECODE_FUNC_CALL();
76
77 PERF_UTILITY_AUTO(__FUNCTION__, PERF_DECODE, PERF_LEVEL_HAL);
78
79 DECODE_CHK_NULL(params);
80 DECODE_CHK_STATUS(DecodeBasicFeature::Update(params));
81
82 CodechalDecodeParams *decodeParams = (CodechalDecodeParams *)params;
83 m_coefProbSize = decodeParams->m_coefProbSize;
84
85 m_vp8PicParams = static_cast<PCODEC_VP8_PIC_PARAMS>(decodeParams->m_picParams);
86 m_vp8SliceParams = static_cast<PCODEC_VP8_SLICE_PARAMS>(decodeParams->m_sliceParams);
87 m_vp8IqMatrixParams = static_cast<PCODEC_VP8_IQ_MATRIX_PARAMS>(decodeParams->m_iqMatrixBuffer);
88 DECODE_CHK_NULL(m_vp8IqMatrixParams);
89
90 DECODE_CHK_STATUS(SetPictureStructs(decodeParams));
91
92 DECODE_CHK_STATUS(m_refFrames.UpdatePicture(*m_vp8PicParams));
93
94 return MOS_STATUS_SUCCESS;
95 }
96
SetPictureStructs(CodechalDecodeParams * decodeParams)97 MOS_STATUS Vp8BasicFeature::SetPictureStructs(CodechalDecodeParams *decodeParams)
98 {
99 DECODE_FUNC_CALL();
100
101 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
102
103 DECODE_CHK_NULL(decodeParams->m_destSurface);
104 DECODE_CHK_NULL(decodeParams->m_dataBuffer);
105
106 m_coefProbSize = decodeParams->m_coefProbSize;
107 m_LastRefSurface = decodeParams->m_presNoneRegLastRefFrame;
108 m_AltRefSurface = decodeParams->m_presNoneRegAltRefFrame;
109 m_GoldenRefSurface = decodeParams->m_presNoneRegGoldenRefFrame;
110 m_bitstreamLockingInUse = decodeParams->m_bitstreamLockingInUse;
111
112 DECODE_CHK_NULL(m_vp8PicParams);
113
114 m_deblockingEnabled = !m_vp8PicParams->LoopFilterDisable ? true : false;
115
116 if (decodeParams->m_bitstreamLockingInUse)
117 {
118 DECODE_CHK_STATUS(AllocateCoefProbBuffer());
119 if (decodeParams->m_bitstreamLockable)
120 {
121 ResourceAutoLock resLock(m_allocator, &m_resDataBuffer.OsResource);
122 auto bitstreamBuffer = (uint8_t *)resLock.LockResourceForRead();
123
124 DECODE_CHK_NULL(bitstreamBuffer);
125
126 DECODE_CHK_STATUS(ParseFrameHead(bitstreamBuffer + m_dataOffset, m_dataSize));
127 }
128 else
129 {
130 // in some case, need to do bitstream copy, skipped for no usage now.
131 DECODE_ASSERTMESSAGE("Not support VP8 bitstream without Lockable!")
132 }
133
134 decodeParams->m_coefProbSize = sizeof(m_vp8FrameHead.FrameContext.CoefProbs);
135 }
136 else
137 {
138 m_resCoefProbBufferExternal = *(decodeParams->m_coefProbBuffer);
139 }
140
141 return eStatus;
142 }
143
AllocateCoefProbBuffer()144 MOS_STATUS Vp8BasicFeature::AllocateCoefProbBuffer()
145 {
146 DECODE_FUNC_CALL();
147
148 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
149
150 // Coef Prob
151 if (m_resCoefProbBufferInternal == nullptr)
152 {
153 m_resCoefProbBufferInternal = m_allocator->AllocateBuffer(
154 m_coefProbSize, "VP8_Coef_Prob", resourceInternalReadWriteCache, lockableVideoMem);
155 }
156 else
157 {
158 DECODE_CHK_STATUS(m_allocator->Resize(
159 m_resCoefProbBufferInternal, m_coefProbSize, lockableVideoMem));
160 }
161
162 return eStatus;
163 }
164
ParseFrameHead(uint8_t * bitstreamBuffer,uint32_t bitstreamBufferSize)165 MOS_STATUS Vp8BasicFeature::ParseFrameHead(uint8_t *bitstreamBuffer, uint32_t bitstreamBufferSize)
166 {
167 MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
168
169 DECODE_FUNC_CALL();
170
171 DECODE_CHK_NULL(bitstreamBuffer);
172
173 m_vp8EntropyState.Initialize(&m_vp8FrameHead, bitstreamBuffer, bitstreamBufferSize);
174
175 eStatus = m_vp8EntropyState.ParseFrameHead(m_vp8PicParams);
176
177 if (eStatus != MOS_STATUS_SUCCESS)
178 {
179 DECODE_ASSERTMESSAGE("Fail to parse VP8 Frame Head");
180 return eStatus;
181 }
182
183 // Loop Filter
184 for (int32_t i = 0; i < VP8_MAX_MB_SEGMENTS; i++)
185 {
186 int32_t segmentLvl = m_vp8PicParams->ucFilterLevel;
187
188 if (m_vp8PicParams->segmentation_enabled)
189 {
190 if (m_vp8PicParams->mb_segement_abs_delta == 1)
191 {
192 m_vp8PicParams->ucLoopFilterLevel[i] = segmentLvl = m_vp8PicParams->cSegmentFeatureData[VP8_MB_LVL_ALT_LF][i];
193 }
194 else
195 {
196 segmentLvl += m_vp8PicParams->cSegmentFeatureData[VP8_MB_LVL_ALT_LF][i];
197 m_vp8PicParams->ucLoopFilterLevel[i] = segmentLvl = (segmentLvl > 0) ? ((segmentLvl > 63) ? 63 : segmentLvl) : 0;
198 }
199 }
200 else
201 {
202 m_vp8PicParams->ucLoopFilterLevel[i] = m_vp8PicParams->ucFilterLevel;
203 }
204 }
205
206 // Quant Matrix
207 int32_t vp8QIndex[VP8_MAX_MB_SEGMENTS];
208 if (m_vp8PicParams->segmentation_enabled)
209 {
210 for (int32_t i = 0; i < 4; i++)
211 {
212 if (m_vp8PicParams->mb_segement_abs_delta == 1)
213 {
214 vp8QIndex[i] = (int32_t)m_vp8PicParams->cSegmentFeatureData[VP8_MB_LVL_ALT_Q][i];
215 }
216 else
217 {
218 vp8QIndex[i] = (int32_t)m_vp8PicParams->ucBaseQIndex + (int32_t)m_vp8PicParams->cSegmentFeatureData[VP8_MB_LVL_ALT_Q][i];
219 vp8QIndex[i] = (vp8QIndex[i] >= 0) ? ((vp8QIndex[i] <= VP8_MAX_Q) ? vp8QIndex[i] : VP8_MAX_Q) : 0; // Clamp to valid range
220 }
221 }
222 }
223 else
224 {
225 vp8QIndex[0] = (int32_t)m_vp8PicParams->ucBaseQIndex;
226 vp8QIndex[1] = 0;
227 vp8QIndex[2] = 0;
228 vp8QIndex[3] = 0;
229 }
230
231 m_vp8EntropyState.FrameHeadQuantUpdate(m_vp8PicParams);
232
233 m_vp8IqMatrixParams->quantization_values[0][0] = m_vp8FrameHead.Y1DeQuant[vp8QIndex[0]][0];
234 m_vp8IqMatrixParams->quantization_values[0][1] = m_vp8FrameHead.Y1DeQuant[vp8QIndex[0]][1];
235 m_vp8IqMatrixParams->quantization_values[0][2] = m_vp8FrameHead.UVDeQuant[vp8QIndex[0]][0];
236 m_vp8IqMatrixParams->quantization_values[0][3] = m_vp8FrameHead.UVDeQuant[vp8QIndex[0]][1];
237 m_vp8IqMatrixParams->quantization_values[0][4] = m_vp8FrameHead.Y2DeQuant[vp8QIndex[0]][0];
238 m_vp8IqMatrixParams->quantization_values[0][5] = m_vp8FrameHead.Y2DeQuant[vp8QIndex[0]][1];
239
240 if (m_vp8FrameHead.u8SegmentationEnabled)
241 {
242 for (int32_t i = 1; i < 4; i++)
243 {
244 m_vp8IqMatrixParams->quantization_values[i][0] = m_vp8FrameHead.Y1DeQuant[vp8QIndex[i]][0];
245 m_vp8IqMatrixParams->quantization_values[i][1] = m_vp8FrameHead.Y1DeQuant[vp8QIndex[i]][1];
246 m_vp8IqMatrixParams->quantization_values[i][2] = m_vp8FrameHead.UVDeQuant[vp8QIndex[i]][0];
247 m_vp8IqMatrixParams->quantization_values[i][3] = m_vp8FrameHead.UVDeQuant[vp8QIndex[i]][1];
248 m_vp8IqMatrixParams->quantization_values[i][4] = m_vp8FrameHead.Y2DeQuant[vp8QIndex[i]][0];
249 m_vp8IqMatrixParams->quantization_values[i][5] = m_vp8FrameHead.Y2DeQuant[vp8QIndex[i]][1];
250 }
251 }
252 else
253 {
254 for (int32_t i = 1; i < 4; i++)
255 {
256 for (int32_t j = 0; j < 6; j++)
257 {
258 m_vp8IqMatrixParams->quantization_values[i][j] = 0;
259 }
260 }
261 }
262
263 ResourceAutoLock resLock(m_allocator, &m_resCoefProbBufferInternal->OsResource);
264 auto data = (uint8_t *)resLock.LockResourceForWrite();
265
266 DECODE_CHK_NULL(data);
267
268 MOS_SecureMemcpy(
269 data,
270 m_resCoefProbBufferInternal->size,
271 (void *)&(m_vp8FrameHead.FrameContext.CoefProbs),
272 sizeof(m_vp8FrameHead.FrameContext.CoefProbs));
273
274 m_vp8FrameHead.bNotFirstCall = true;
275
276 return eStatus;
277 }
278
SetRequiredBitstreamSize(uint32_t requiredSize)279 MOS_STATUS Vp8BasicFeature::SetRequiredBitstreamSize(uint32_t requiredSize)
280 {
281 DECODE_FUNC_CALL();
282
283 if (requiredSize > m_dataSize)
284 {
285 m_dataOffset = 0;
286 m_dataSize = MOS_ALIGN_CEIL(requiredSize, MHW_CACHELINE_SIZE);
287 }
288
289 DECODE_NORMALMESSAGE("Estimate bitstream size in this Frame: %u", requiredSize);
290 return MOS_STATUS_SUCCESS;
291 }
292
293 } // namespace decode
294