Debian Codegraph @

media_softlet/agnostic/common/codec/hal/enc/av1/features/encode_av1_scc.cpp

raw
1 /*
2 * Copyright (c) 2021-2024, 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 encode_av1_scc.cpp
24 //! \brief SCC feature
25 //!
26
27 #include "encode_av1_scc.h"
28 #include "encode_av1_vdenc_const_settings.h"
29 #include "encode_av1_tile.h"
30 #include "encode_av1_segmentation.h"
31
32 namespace encode
33 {
34
35 Av1Scc::Av1Scc(EncodeAllocator *allocator,
36 CodechalHwInterfaceNext *hwInterface,
37 void *constSettings,
38 MediaFeatureManager *featureManager)
39 : MediaFeature(constSettings, hwInterface ? hwInterface->GetOsInterface() : nullptr)
40 {
41 ENCODE_FUNC_CALL();
42
43 ENCODE_CHK_NULL_NO_STATUS_RETURN(featureManager);
44 m_featureManager = featureManager;
45
46 m_basicFeature = dynamic_cast<Av1BasicFeature *>(featureManager->GetFeature(Av1FeatureIDs::basicFeature));
47 ENCODE_CHK_NULL_NO_STATUS_RETURN(m_basicFeature);
48
49 m_segmentation = dynamic_cast<Av1Segmentation *>(featureManager->GetFeature(Av1FeatureIDs::av1Segmentation));
50 ENCODE_CHK_NULL_NO_STATUS_RETURN(m_segmentation);
51
52 m_allocator = allocator;
53
54 if (hwInterface)
55 {
56 m_osInterface = hwInterface->GetOsInterface();
57 }
58 }
59
60 Av1Scc::~Av1Scc()
61 {
62 ENCODE_FUNC_CALL();
63 }
64
65 inline MOS_STATUS CheckIBCParams(const CODEC_AV1_ENCODE_PICTURE_PARAMS &picParams)
66 {
67 if (picParams.PicFlags.fields.allow_intrabc)
68 {
69 const auto &lr = picParams.LoopRestorationFlags.fields;
70 if (!AV1_KEY_OR_INRA_FRAME(picParams.PicFlags.fields.frame_type)
71 || picParams.filter_level[0] || picParams.filter_level[1]
72 || picParams.cdef_bits
73 || picParams.PicFlags.fields.use_superres
74 || lr.yframe_restoration_type || lr.cbframe_restoration_type || lr.crframe_restoration_type)
75 {
76 ENCODE_ASSERTMESSAGE("All loop filters MUST be disabled when IBC is enabled");
77 return MOS_STATUS_INVALID_PARAMETER;
78 }
79 }
80
81 return MOS_STATUS_SUCCESS;
82 }
83
84 MOS_STATUS Av1Scc::MakeCdfTrackedBufferLockable()
85 {
86 ENCODE_FUNC_CALL();
87 MOS_ALLOC_GFXRES_PARAMS allocParams;
88 MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
89 allocParams.Type = MOS_GFXRES_BUFFER;
90 allocParams.TileType = MOS_TILE_LINEAR;
91 allocParams.Format = Format_Buffer;
92 allocParams.Flags.bNotLockable = 0;
93
94 allocParams.dwBytes = MOS_ALIGN_CEIL(m_basicFeature->m_cdfMaxNumBytes, CODECHAL_PAGE_SIZE);
95 allocParams.pBufName = "bwdAdaptCdfBuffer";
96 allocParams.ResUsageType = MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE;
97 ENCODE_CHK_STATUS_RETURN(m_basicFeature->m_trackedBuf->RegisterParam(encode::BufferType::bwdAdaptCdfBuffer, allocParams));
98
99 return MOS_STATUS_SUCCESS;
100 }
101
102 MOS_STATUS Av1Scc::ResetMvProbsToDefault()
103 {
104 ENCODE_FUNC_CALL();
105 PMOS_RESOURCE cdfTrackedBuf = m_basicFeature->m_trackedBuf->GetBuffer(BufferType::bwdAdaptCdfBuffer, 0);
106 ENCODE_CHK_NULL_RETURN(cdfTrackedBuf);
107
108 auto data = (uint16_t *)m_allocator->LockResourceForWrite(cdfTrackedBuf);
109 ENCODE_CHK_NULL_RETURN(data);
110 ENCODE_CHK_STATUS_RETURN(InitDefaultFrameContextBuffer(data, 0, mvJointType, interintra));
111 ENCODE_CHK_STATUS_RETURN(m_allocator->UnLock(cdfTrackedBuf));
112
113 m_resetMvProbs = false;
114
115 return MOS_STATUS_SUCCESS;
116 }
117
118 MOS_STATUS Av1Scc::Update(void *params)
119 {
120 ENCODE_FUNC_CALL();
121
122 EncoderParams *encodeParams = (EncoderParams *)params;
123 ENCODE_CHK_NULL_RETURN(encodeParams);
124
125 auto av1PicParams = static_cast<PCODEC_AV1_ENCODE_PICTURE_PARAMS>(encodeParams->pPicParams);
126 ENCODE_CHK_NULL_RETURN(av1PicParams);
127
128 m_enablePalette = av1PicParams->PicFlags.fields.PaletteModeEnable;
129
130 ENCODE_CHK_STATUS_RETURN(CheckIBCParams(*av1PicParams));
131
132 m_enableIBC = av1PicParams->PicFlags.fields.allow_intrabc;
133 m_IBCPossible = m_IBCPossible || m_enableIBC;
134
135 if (m_resetMvProbs)
136 {
137 ResetMvProbsToDefault();
138 }
139
140 if (m_IBCPossible && m_basicFeature->m_resolutionChanged)
141 {
142 // Defines 2 sets of MV probabilities: set 0 for regular Inter blocks, set 1 for IBC blocks
143 // HW CDF table contains single set of MV probabilities instead of 2
144 // when IBC is enabled PAK will update MV probabilities during encoding of KEY frame
145 // Inter frame which follows KEY frame requires default (not updated) MV probabilities
146 // CPU-lockable CDF table tracked buffer is needed to reset MV probs back to defaults
147 // tracked buffer is forced to be CPU-lockable
148 ENCODE_CHK_STATUS_RETURN(MakeCdfTrackedBufferLockable());
149 }
150
151 if (m_enableIBC)
152 {
153 // need to reset MV probs to default values after each KEY frame with IBC enabled
154 m_resetMvProbs = true;
155 if (m_intrabcReconSurface == nullptr)
156 {
157 MOS_ALLOC_GFXRES_PARAMS allocParams;
158 MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
159 allocParams.Type = MOS_GFXRES_2D;
160 allocParams.TileType = MOS_TILE_Y;
161 allocParams.Format = (10 == m_basicFeature->m_bitDepth) ? Format_P010 : Format_NV12;
162 allocParams.dwWidth = MOS_ALIGN_CEIL(m_basicFeature->m_frameWidth, av1SuperBlockWidth);
163 allocParams.dwHeight = MOS_ALIGN_CEIL(m_basicFeature->m_frameHeight, av1SuperBlockHeight);
164 allocParams.pBufName = "Intra Block Copy output buffer";
165 allocParams.bIsCompressible = false;
166 allocParams.CompressionMode = MOS_MMC_DISABLED;
167
168 if (m_basicFeature->m_outputChromaFormat == AVP_CHROMA_FORMAT_YUV444)
169 {
170 if (!m_basicFeature->m_is10Bit)
171 {
172 allocParams.Format = Format_AYUV;
173 allocParams.dwWidth = MOS_ALIGN_CEIL(allocParams.dwWidth, 512 / 4);
174 allocParams.dwHeight = MOS_ALIGN_CEIL(allocParams.dwHeight * 3 / 4, 8);
175 }
176 else
177 {
178 allocParams.Format = Format_Y410;
179 //Y410Viriant surface is same as Y416Viriant
180 allocParams.dwWidth = MOS_ALIGN_CEIL(allocParams.dwWidth, 256 / 4);
181 allocParams.dwHeight = MOS_ALIGN_CEIL(allocParams.dwHeight * 3 / 2, 8);
182 }
183 }
184 else if (m_basicFeature->m_outputChromaFormat == AVP_CHROMA_FORMAT_YUV422)
185 {
186 if (!m_basicFeature->m_is10Bit)
187 {
188 // allocated as YUY2, but used by HW as YUY2 Variant, which is
189 // twice the height and half the width of the YUY2 source surface
190 allocParams.Format = Format_YUY2;
191 allocParams.dwWidth = MOS_ALIGN_CEIL(allocParams.dwWidth >> 1, 128);
192 allocParams.dwHeight = MOS_ALIGN_CEIL(allocParams.dwHeight << 1, 8);
193 }
194 else
195 {
196 // allocated as Y210, but used by HW as Y210 Variant, which is
197 // twice the height and half the width of the Y210 source surface
198 allocParams.Format = Format_Y210;
199 allocParams.dwWidth = MOS_ALIGN_CEIL(allocParams.dwWidth >> 1, 128 >> 1); // HW requires 128 byte aligned width, 2 bytes per pixel
200 allocParams.dwHeight = MOS_ALIGN_CEIL(allocParams.dwHeight << 1, 8);
201 }
202 }
203
204 m_intrabcReconSurface = m_allocator->AllocateSurface(allocParams, false, MOS_HW_RESOURCE_USAGE_ENCODE_INTERNAL_READ_WRITE_CACHE);
205 m_allocator->GetSurfaceInfo(m_intrabcReconSurface);
206 }
207 }
208
209 ENCODE_CHK_STATUS_RETURN(UpdateIBCStatusForCurrentTile());
210
211 return MOS_STATUS_SUCCESS;
212 }
213
214 const uint32_t INTRABC_DELAY_SB64 = 4;
215
216 MOS_STATUS Av1Scc::UpdateIBCStatusForCurrentTile()
217 {
218 ENCODE_FUNC_CALL();
219 uint32_t wSB64 = MOS_ROUNDUP_DIVIDE(m_basicFeature->m_frameWidth, 64);
220
221 auto tileFeature = dynamic_cast<Av1EncodeTile *>(m_featureManager->GetFeature(Av1FeatureIDs::encodeTile));
222
223 if (tileFeature)
224 {
225 Av1TileInfo tileInfo = {};
226 tileFeature->GetTileInfo(&tileInfo);
227
228 wSB64 = tileInfo.tileWidthInSbMinus1 + 1;
229 }
230
231 m_IBCEnabledForCurrentTile = m_enableIBC && (wSB64 > (INTRABC_DELAY_SB64 + 1));
232 return MOS_STATUS_SUCCESS;
233 }
234
235 MHW_SETPAR_DECL_SRC(VDENC_HEVC_VP9_TILE_SLICE_STATE, Av1Scc)
236 {
237 ENCODE_FUNC_CALL();
238 bool m_is10Bit = m_basicFeature->m_is10Bit;
239 uint16_t qp = (m_basicFeature->m_av1PicParams->base_qindex) / 4;
240
241 uint8_t tableIdx = 0;
242 if (qp <= 12)
243 tableIdx = 0;
244 else if (qp > 12 && qp <= 17)
245 tableIdx = 1;
246 else if (qp > 17 && qp <= 22)
247 tableIdx = 2;
248 else if (qp > 22 && qp <= 27)
249 tableIdx = 3;
250 else if (qp > 27 && qp <= 32)
251 tableIdx = 4;
252 else if (qp > 32 && qp <= 37)
253 tableIdx = 5;
254 else if (qp > 37 && qp <= 42)
255 tableIdx = 6;
256 else if (qp > 42 && qp <= 47)
257 tableIdx = 7;
258 else if (qp > 47 && qp <= 49)
259 tableIdx = 8;
260 else
261 tableIdx = 9;
262
263 if (m_enablePalette)
264 {
265 params.VdencHEVCVP9TileSlicePar8 = AV1table[tableIdx][0];
266 params.VdencHEVCVP9TileSlicePar9 = AV1table[tableIdx][1];
267 params.VdencHEVCVP9TileSlicePar5 = AV1table[tableIdx][2];
268 params.VdencHEVCVP9TileSlicePar22 = 64;
269 params.paletteModeEnable = 1;
270
271 if (m_is10Bit)
272 {
273 params.VdencHEVCVP9TileSlicePar5 = AV1table[tableIdx][2] + 2;
274 }
275 }
276
277 if (m_IBCEnabledForCurrentTile)
278 {
279 params.ibcControl = IBC_ENABLED;
280 params.VdencHEVCVP9TileSlicePar0 = 0;
281 }
282
283 auto numTiles = m_basicFeature->m_av1PicParams->tile_rows * m_basicFeature->m_av1PicParams->tile_cols;
284 if (numTiles > 1)
285 {
286 auto waTable = m_osInterface->pfnGetWaTable(m_osInterface);
287 if (MEDIA_IS_WA(waTable, Wa_15014143531))
288 {
289 params.ibcControl = IBC_DISABLED;
290 }
291 }
292
293 return MOS_STATUS_SUCCESS;
294 }
295
296 MHW_SETPAR_DECL_SRC(VDENC_CMD2, Av1Scc)
297 {
298 ENCODE_FUNC_CALL();
299 if (m_IBCEnabledForCurrentTile)
300 {
301 params.frameIdxL0Ref0 = 0;
302 }
303
304 #if _MEDIA_RESERVED
305 if (m_IBCEnabledForCurrentTile)
306 {
307 params.vdencCmd2Par3 = vdencCmd2Par3Value2;
308 params.vdencCmd2Par136 = vdencCmd2Par136Value1;
309 params.vdencCmd2Par102 = false;
310 params.vdencCmd2Par101 = false;
311
312 if (params.vdencCmd2Par135[1] == vdencCmd2Par135Value2)
313 {
314 params.vdencCmd2Par135[1] = vdencCmd2Par135Value1;
315 }
316 }
317
318 if (m_segmentation->HasZeroSegmentQIndex())
319 {
320 params.vdencCmd2Par133 = false;
321 }
322
323 if (m_enablePalette && (m_basicFeature->m_targetUsage == 7))
324 {
325 params.vdencCmd2Par134[1] = 1;
326 }
327 #else
328 params.extSettings.emplace_back(
329 [this](uint32_t *data) {
330 if (m_IBCEnabledForCurrentTile)
331 {
332 data[2] = data[2] & 0xFFFFFFFC | 0x2;
333 data[37] = data[37] & 0xFFFFFF9F | 0x40;
334 data[54] &= 0xFFFFFF3F;
335
336 if (((data[51] >> 10) & 0b11) == vdencCmd2Par135Value1)
337 {
338 data[51] = data[51] & 0xFFFFF3FF | 0x400;
339 }
340 }
341
342 if (m_segmentation->HasZeroSegmentQIndex())
343 {
344 data[2] &= 0xFFFFFFBF;
345 }
346
347 if (m_enablePalette && (m_basicFeature->m_targetUsage == 7))
348 {
349 data[51] = data[51] & 0xFFFFFF3F | 0x40;
350 }
351
352 return MOS_STATUS_SUCCESS;
353 });
354 #endif // _MEDIA_RESERVED
355
356 return MOS_STATUS_SUCCESS;
357 }
358
359 MHW_SETPAR_DECL_SRC(VDENC_CMD1, Av1Scc)
360 {
361 ENCODE_FUNC_CALL();
362 auto setting = static_cast<Av1VdencFeatureSettings*>(m_constSettings);
363 ENCODE_CHK_NULL_RETURN(setting);
364
365 if (m_IBCEnabledForCurrentTile)
366 {
367
368 std::fill_n(&params.vdencCmd1Par15[0], 4, (uint8_t)158);
369 std::fill_n(&params.vdencCmd1Par14[0], 4, (uint8_t)146);
370 std::fill_n(&params.vdencCmd1Par13[0], 4, (uint8_t)146);
371 std::fill_n(&params.vdencCmd1Par12[0], 4, (uint8_t)143);
372 std::fill_n(&params.vdencCmd1Par11[0], 4, (uint8_t)140);
373 std::fill_n(&params.vdencCmd1Par10[0], 4, (uint8_t)134);
374 std::fill_n(&params.vdencCmd1Par9[0], 4, (uint8_t)134);
375 std::fill_n(&params.vdencCmd1Par8[0], 4, (uint8_t)131);
376
377 params.vdencCmd1Par5 = 8;
378 params.vdencCmd1Par6 = 4;
379 params.vdencCmd1Par7 = 12;
380
381 params.vdencCmd1Par16 = 229;
382 params.vdencCmd1Par17 = 146;
383 params.vdencCmd1Par18 = 230;
384 params.vdencCmd1Par19 = 144;
385 params.vdencCmd1Par20 = 145;
386
387 params.vdencCmd1Par90 = 0;
388 params.vdencCmd1Par91 = 0;
389 params.vdencCmd1Par92 = 0;
390 params.vdencCmd1Par94 = 24;
391 }
392
393 return MOS_STATUS_SUCCESS;
394 }
395
396 MHW_SETPAR_DECL_SRC(VDENC_PIPE_BUF_ADDR_STATE, Av1Scc)
397 {
398 ENCODE_FUNC_CALL();
399 if (m_enableIBC)
400 {
401 params.refs[0] = &m_intrabcReconSurface->OsResource;
402 params.refsDsStage2[0] = nullptr;
403 params.refsDsStage1[0] = nullptr;
404
405 params.numActiveRefL0 = 1;
406 }
407
408 return MOS_STATUS_SUCCESS;
409 }
410
411 MHW_SETPAR_DECL_SRC(AVP_PIC_STATE, Av1Scc)
412 {
413 ENCODE_FUNC_CALL();
414 if (m_enableIBC || m_enablePalette)
415 {
416 params.allowScreenContentTools = true;
417 params.allowIntraBC = m_enableIBC;
418
419 if (m_enableIBC)
420 {
421 params.enableCDEF = false;
422 params.enableRestoration = false;
423 }
424 }
425
426 return MOS_STATUS_SUCCESS;
427 }
428
429 MHW_SETPAR_DECL_SRC(AVP_PIPE_BUF_ADDR_STATE, Av1Scc)
430 {
431 ENCODE_FUNC_CALL();
432 if (m_enableIBC)
433 {
434 params.intrabcDecodedOutputFrameBuffer = &m_intrabcReconSurface->OsResource;
435 }
436
437 return MOS_STATUS_SUCCESS;
438 }
439
440 MHW_SETPAR_DECL_SRC(AVP_SURFACE_STATE, Av1Scc)
441 {
442 ENCODE_FUNC_CALL();
443 MOS_MEMCOMP_STATE mmcState = {};
444 if (m_enableIBC && params.surfaceStateId == intrabcDecodedFrame)
445 {
446 if (m_basicFeature->m_outputChromaFormat == AVP_CHROMA_FORMAT_YUV420)
447 {
448 params.pitch = m_basicFeature->m_reconSurface.dwPitch;
449 params.uOffset = m_basicFeature->m_reconSurface.YoffsetForUplane;
450 params.vOffset = m_basicFeature->m_reconSurface.YoffsetForVplane;
451 }
452 else if (m_basicFeature->m_outputChromaFormat == AVP_CHROMA_FORMAT_YUV444)
453 {
454 if (!m_basicFeature->m_is10Bit)
455 {
456 params.pitch = m_basicFeature->m_reconSurface.dwPitch / 4;
457 params.uOffset = (uint16_t)m_basicFeature->m_rawSurface.dwHeight;
458 params.vOffset = (uint16_t)m_basicFeature->m_rawSurface.dwHeight << 1;
459 }
460 else
461 {
462 params.pitch = m_basicFeature->m_reconSurface.dwPitch / 2;
463 params.uOffset = (uint16_t)m_basicFeature->m_rawSurface.dwHeight;
464 params.vOffset = (uint16_t)m_basicFeature->m_rawSurface.dwHeight << 1;
465 }
466 }
467 else if (m_basicFeature->m_outputChromaFormat == AVP_CHROMA_FORMAT_YUV422)
468 {
469 params.pitch = m_basicFeature->m_reconSurface.dwPitch;
470 params.uOffset = params.vOffset = m_basicFeature->m_rawSurface.dwHeight;
471 }
472
473 m_basicFeature->GetSurfaceMmcInfo(m_intrabcReconSurface, mmcState, params.compressionFormat);
474 std::fill(std::begin(params.mmcState), std::end(params.mmcState), mmcState);
475 }
476 return MOS_STATUS_SUCCESS;
477 }
478
479 } // namespace encode