Debian Codegraph @

media_softlet/agnostic/common/codec/hal/dec/shared/features/decode_downsampling_feature.cpp

raw
1 /*
2 * Copyright (c) 2020-2025, 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 //!
24 //! \file decode_downsampling_feature.cpp
25 //! \brief Defines the common interface for decode downsampling features
26 //! \details The decode downsampling feature interface is further sub-divided by codec standard,
27 //! this file is for the base interface which is shared by all codecs.
28 //!
29 #include "decode_downsampling_feature.h"
30 #include "decode_utils.h"
31 #include "codechal_debug.h"
32
33 #ifdef _DECODE_PROCESSING_SUPPORTED
34
35 namespace decode
36 {
37 DecodeDownSamplingFeature::DecodeDownSamplingFeature(
38 MediaFeatureManager *featureManager, DecodeAllocator *allocator, PMOS_INTERFACE osInterface) :
39 m_osInterface(osInterface), m_allocator(allocator)
40 {
41 m_featureManager = featureManager;
42 }
43
44 DecodeDownSamplingFeature::~DecodeDownSamplingFeature()
45 {
46 FreeHistogramBuffer();
47 }
48
49 MOS_STATUS DecodeDownSamplingFeature::Init(void *setting)
50 {
51 DECODE_FUNC_CALL();
52 DECODE_CHK_NULL(m_featureManager);
53 DECODE_CHK_NULL(m_allocator);
54
55 DECODE_CHK_STATUS(m_internalTargets.Init(*m_allocator));
56
57 m_basicFeature = dynamic_cast<DecodeBasicFeature*>(m_featureManager->GetFeature(FeatureIDs::basicFeature));
58 DECODE_CHK_NULL(m_basicFeature);
59
60 MOS_ZeroMemory(&m_outputSurface, sizeof(m_outputSurface));
61
62 DECODE_CHK_NULL(m_osInterface);
63 m_userSettingPtr = m_osInterface->pfnGetUserSettingInstance(m_osInterface);
64
65 #if (_DEBUG || _RELEASE_INTERNAL)
66 m_histogramDebug = ReadUserFeature(m_userSettingPtr, "Decode Histogram Debug", MediaUserSetting::Group::Sequence).Get<bool>();
67 #endif
68
69 return MOS_STATUS_SUCCESS;
70 }
71
72 MOS_STATUS DecodeDownSamplingFeature::Update(void *params)
73 {
74 DECODE_FUNC_CALL();
75 DECODE_CHK_NULL(params);
76
77 CodechalDecodeParams *decodeParams = (CodechalDecodeParams *)params;
78
79 if (decodeParams->m_procParams == nullptr)
80 {
81 m_inputSurface = nullptr;
82 m_histogramDestSurf = nullptr;
83 m_histogramBuffer = nullptr;
84 m_histogramDestSurfU = nullptr;
85 m_histogramBufferU = nullptr;
86 m_histogramDestSurfV = nullptr;
87 m_histogramBufferV = nullptr;
88 m_histogramStatisticsSummary = nullptr;
89 m_histogramMetaDataStreamOut = nullptr;
90 m_histogramMetaDataStreamIn = nullptr;
91 m_enabled = false;
92
93 return MOS_STATUS_SUCCESS;
94 }
95 else
96 {
97 m_enabled = true;
98 }
99
100 DecodeProcessingParams *procParams = (DecodeProcessingParams *)decodeParams->m_procParams;
101
102 m_chromaSitingType = procParams->m_chromaSitingType;
103 m_rotationState = procParams->m_rotationState;
104 m_blendState = procParams->m_blendState;
105 m_mirrorState = procParams->m_mirrorState;
106 m_scalingMode = procParams->m_scalingMode;
107 m_isReferenceOnlyPattern = procParams->m_isReferenceOnlyPattern;
108
109 if (m_isReferenceOnlyPattern)
110 {
111 m_enabled = false;
112 m_inputSurface = procParams->m_inputSurface;
113 return MOS_STATUS_SUCCESS;
114 }
115
116 DECODE_CHK_NULL(procParams->m_outputSurface);
117 m_outputSurface = *(procParams->m_outputSurface);
118 DECODE_CHK_STATUS(m_allocator->GetSurfaceInfo(&m_outputSurface));
119
120 m_outputSurfaceRegion.m_x = procParams->m_outputSurfaceRegion.m_x;
121 m_outputSurfaceRegion.m_y = procParams->m_outputSurfaceRegion.m_y;
122 m_outputSurfaceRegion.m_width = (procParams->m_outputSurfaceRegion.m_width == 0) ?
123 m_outputSurface.dwWidth : procParams->m_outputSurfaceRegion.m_width;
124 m_outputSurfaceRegion.m_height = (procParams->m_outputSurfaceRegion.m_height == 0) ?
125 m_outputSurface.dwHeight : procParams->m_outputSurfaceRegion.m_height;
126
127 if (procParams->m_inputSurface != nullptr)
128 {
129 m_inputSurface = procParams->m_inputSurface;
130 DECODE_CHK_STATUS(m_allocator->GetSurfaceInfo(m_inputSurface));
131
132 m_inputSurfaceRegion.m_x = procParams->m_inputSurfaceRegion.m_x;
133 m_inputSurfaceRegion.m_y = procParams->m_inputSurfaceRegion.m_y;
134 m_inputSurfaceRegion.m_width = (procParams->m_inputSurfaceRegion.m_width == 0) ?
135 m_inputSurface->dwWidth : procParams->m_inputSurfaceRegion.m_width;
136 m_inputSurfaceRegion.m_height = (procParams->m_inputSurfaceRegion.m_height == 0) ?
137 m_inputSurface->dwHeight : procParams->m_inputSurfaceRegion.m_height;
138 }
139 else
140 {
141 if (m_basicFeature->m_curRenderPic.FrameIdx >= decodeParams->m_refFrameCnt)
142 {
143 DECODE_ASSERTMESSAGE("Invalid Downsampling Reference Frame Index !");
144 return MOS_STATUS_INVALID_PARAMETER;
145 }
146
147 DECODE_CHK_STATUS(UpdateInternalTargets(*m_basicFeature));
148
149 m_inputSurface = m_internalTargets.GetCurSurf();
150 DECODE_CHK_NULL(m_inputSurface);
151
152 m_inputSurfaceRegion.m_x = 0;
153 m_inputSurfaceRegion.m_y = 0;
154 m_inputSurfaceRegion.m_width = (procParams->m_inputSurfaceRegion.m_width == 0 || procParams->m_inputSurfaceRegion.m_width > m_basicFeature->m_width) ?
155 m_basicFeature->m_width : procParams->m_inputSurfaceRegion.m_width;
156 m_inputSurfaceRegion.m_height = (procParams->m_inputSurfaceRegion.m_height == 0 || procParams->m_inputSurfaceRegion.m_height > m_basicFeature->m_height) ?
157 m_basicFeature->m_height : procParams->m_inputSurfaceRegion.m_height;
158 }
159
160 // Histogram
161 if (!m_allocator->ResourceIsNull(&decodeParams->m_histogramSurface.OsResource) || m_histogramDebug)
162 {
163 m_histogramDestSurf = &decodeParams->m_histogramSurface;
164 m_histogramBuffer = AllocateHistogramBuffer(m_basicFeature->m_curRenderPic.FrameIdx, HistogramY);
165 DECODE_CHK_NULL(m_histogramBuffer);
166 }
167 else
168 {
169 m_histogramDestSurf = nullptr;
170 m_histogramBuffer = nullptr;
171 }
172
173 // Histogram U plane
174 if (!m_allocator->ResourceIsNull(&decodeParams->m_histogramSurfaceU.OsResource) || m_histogramDebug)
175 {
176 m_histogramDestSurfU = &decodeParams->m_histogramSurfaceU;
177 m_histogramBufferU = AllocateHistogramBuffer(m_basicFeature->m_curRenderPic.FrameIdx, HistogramU);
178 DECODE_CHK_NULL(m_histogramBufferU);
179 }
180 else
181 {
182 m_histogramDestSurfU = nullptr;
183 m_histogramBufferU = nullptr;
184 }
185
186 // Histogram V plane
187 if (!m_allocator->ResourceIsNull(&decodeParams->m_histogramSurfaceV.OsResource) || m_histogramDebug)
188 {
189 m_histogramDestSurfV = &decodeParams->m_histogramSurfaceV;
190 m_histogramBufferV = AllocateHistogramBuffer(m_basicFeature->m_curRenderPic.FrameIdx, HistogramV);
191 DECODE_CHK_NULL(m_histogramBufferV);
192 }
193 else
194 {
195 m_histogramDestSurfV = nullptr;
196 m_histogramBufferV = nullptr;
197 }
198
199 if (m_osInterface && MEDIA_IS_SKU(m_osInterface->pfnGetSkuTable(m_osInterface), FtrVDAQMHistogram) &&
200 (m_histogramBuffer != nullptr || m_histogramBufferU != nullptr || m_histogramBufferV != nullptr))
201 {
202 m_aqmHistogramEnable = true;
203 }
204 else
205 {
206 m_aqmHistogramEnable = false;
207 }
208
209 if (m_aqmHistogramEnable)
210 {
211 m_histogramStatisticsSummary = AllocateHistogramBuffer(m_basicFeature->m_curRenderPic.FrameIdx, HistogramStatistics);
212 DECODE_CHK_NULL(m_histogramStatisticsSummary);
213 m_histogramMetaDataStreamIn = AllocateHistogramBuffer(m_basicFeature->m_curRenderPic.FrameIdx, HistogramStreamIn);
214 DECODE_CHK_NULL(m_histogramMetaDataStreamIn);
215 m_histogramMetaDataStreamOut = AllocateHistogramBuffer(m_basicFeature->m_curRenderPic.FrameIdx, HistogramStreamOut);
216 DECODE_CHK_NULL(m_histogramMetaDataStreamOut);
217 }
218
219 // Update decode output in basic feature
220 if (!m_aqmHistogramEnable)
221 {
222 DECODE_CHK_STATUS(UpdateDecodeTarget(*m_inputSurface));
223 }
224
225 #if (_DEBUG || _RELEASE_INTERNAL)
226 m_outputSurfaceList[m_basicFeature->m_curRenderPic.FrameIdx] = m_outputSurface;
227 #endif
228
229 return MOS_STATUS_SUCCESS;
230 }
231
232 MOS_STATUS DecodeDownSamplingFeature::UpdateInternalTargets(DecodeBasicFeature &basicFeature)
233 {
234 DECODE_FUNC_CALL();
235
236 uint32_t curFrameIdx = basicFeature.m_curRenderPic.FrameIdx;
237
238 std::vector<uint32_t> refFrameList;
239 DECODE_CHK_STATUS(GetRefFrameList(refFrameList));
240 DECODE_CHK_STATUS(m_internalTargets.UpdateRefList(curFrameIdx, refFrameList));
241
242 MOS_SURFACE surface;
243 MOS_ZeroMemory(&surface, sizeof(surface));
244 DECODE_CHK_STATUS(GetDecodeTargetSize(surface.dwWidth, surface.dwHeight));
245 DECODE_CHK_STATUS(GetDecodeTargetFormat(surface.Format));
246 DECODE_CHK_STATUS(m_internalTargets.ActiveCurSurf(
247 curFrameIdx, &surface, basicFeature.IsMmcEnabled(), resourceOutputPicture, notLockableVideoMem));
248
249 return MOS_STATUS_SUCCESS;
250 }
251
252 PMOS_BUFFER DecodeDownSamplingFeature::AllocateHistogramBuffer(uint8_t frameIndex, HistogramBufferType bufferType)
253 {
254 DECODE_FUNC_CALL();
255
256 if (frameIndex >= DecodeBasicFeature::m_maxFrameIndex)
257 {
258 return nullptr;
259 }
260
261 switch (bufferType)
262 {
263 case HistogramY:
264 if (m_histogramBufferList[frameIndex] == nullptr)
265 {
266 auto histogramBuffer = m_allocator->AllocateBuffer(HISTOGRAM_BINCOUNT * m_histogramBinWidth,
267 "Histogram internal buffer",
268 resourceInternalReadWriteCache,
269 lockableVideoMem,
270 true,
271 0,
272 false);
273
274 if (histogramBuffer == nullptr ||
275 m_allocator->ResourceIsNull(&histogramBuffer->OsResource))
276 {
277 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram internal buffer!");
278 }
279
280 m_histogramBufferList[frameIndex] = histogramBuffer;
281 }
282 return m_histogramBufferList[frameIndex];
283 case HistogramU:
284 if (m_histogramBufferUList[frameIndex] == nullptr)
285 {
286 auto histogramBufferU = m_allocator->AllocateBuffer(HISTOGRAM_BINCOUNT * m_histogramBinWidth,
287 "Histogram U-Plane buffer",
288 resourceInternalReadWriteCache,
289 lockableVideoMem,
290 true,
291 0,
292 false);
293
294 if (histogramBufferU == nullptr ||
295 m_allocator->ResourceIsNull(&histogramBufferU->OsResource))
296 {
297 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram U-Plane internal buffer!");
298 }
299
300 m_histogramBufferUList[frameIndex] = histogramBufferU;
301 }
302 return m_histogramBufferUList[frameIndex];
303 case HistogramV:
304 if (m_histogramBufferVList[frameIndex] == nullptr)
305 {
306 auto histogramBufferV = m_allocator->AllocateBuffer(HISTOGRAM_BINCOUNT * m_histogramBinWidth,
307 "Histogram V-Plane buffer",
308 resourceInternalReadWriteCache,
309 lockableVideoMem,
310 true,
311 0,
312 false);
313
314 if (histogramBufferV == nullptr ||
315 m_allocator->ResourceIsNull(&histogramBufferV->OsResource))
316 {
317 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram V-Plane internal buffer!");
318 }
319
320 m_histogramBufferVList[frameIndex] = histogramBufferV;
321 }
322 return m_histogramBufferVList[frameIndex];
323 case HistogramStatistics:
324 if (m_histogramBufferStatisticsSummaryList[frameIndex] == nullptr)
325 {
326 auto histogramBufferStatistics = m_allocator->AllocateBuffer(HISTOGRAM_BINCOUNT * m_histogramBinWidth,
327 "Histogram statistics summary buffer",
328 resourceInternalReadWriteCache,
329 lockableVideoMem,
330 true,
331 0,
332 false);
333
334 if (histogramBufferStatistics == nullptr ||
335 m_allocator->ResourceIsNull(&histogramBufferStatistics->OsResource))
336 {
337 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram statistics internal buffer!");
338 }
339
340 m_histogramBufferStatisticsSummaryList[frameIndex] = histogramBufferStatistics;
341 }
342 return m_histogramBufferStatisticsSummaryList[frameIndex];
343 case HistogramStreamIn:
344 if (m_histogramBufferMetaDataStreamInList[frameIndex] == nullptr)
345 {
346 auto histogramBufferStreamIn = m_allocator->AllocateBuffer(HISTOGRAM_BINCOUNT * m_histogramBinWidth,
347 "Histogram stream in buffer",
348 resourceInternalReadWriteCache,
349 lockableVideoMem,
350 true,
351 0,
352 false);
353
354 if (histogramBufferStreamIn == nullptr ||
355 m_allocator->ResourceIsNull(&histogramBufferStreamIn->OsResource))
356 {
357 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram StreamIn internal buffer!");
358 }
359
360 m_histogramBufferMetaDataStreamInList[frameIndex] = histogramBufferStreamIn;
361 }
362 return m_histogramBufferMetaDataStreamInList[frameIndex];
363 case HistogramStreamOut:
364 if (m_histogramBufferMetaDataStreamOutList[frameIndex] == nullptr)
365 {
366 auto histogramBufferStreamOut = m_allocator->AllocateBuffer(HISTOGRAM_BINCOUNT * m_histogramBinWidth,
367 "Histogram stream out buffer",
368 resourceInternalReadWriteCache,
369 lockableVideoMem,
370 true,
371 0,
372 false);
373
374 if (histogramBufferStreamOut == nullptr ||
375 m_allocator->ResourceIsNull(&histogramBufferStreamOut->OsResource))
376 {
377 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram StreamOut internal buffer!");
378 }
379
380 m_histogramBufferMetaDataStreamOutList[frameIndex] = histogramBufferStreamOut;
381 }
382 return m_histogramBufferMetaDataStreamOutList[frameIndex];
383 default:
384 DECODE_ASSERTMESSAGE("Failed to allocate hsitogram buffer due to invalid buffer type %d !", bufferType);
385 break;
386 }
387
388 return nullptr;
389 }
390
391 void DecodeDownSamplingFeature::FreeHistogramBuffer()
392 {
393 DECODE_FUNC_CALL();
394
395 PMOS_BUFFER *histogramBufferList = nullptr;
396
397 for (int k = HistogramY; k < HistogramStreamMax; k++)
398 {
399 switch (k)
400 {
401 case HistogramY:
402 histogramBufferList = m_histogramBufferList;
403 break;
404 case HistogramU:
405 histogramBufferList = m_histogramBufferUList;
406 break;
407 case HistogramV:
408 histogramBufferList = m_histogramBufferVList;
409 break;
410 case HistogramStatistics:
411 histogramBufferList = m_histogramBufferStatisticsSummaryList;
412 break;
413 case HistogramStreamIn:
414 histogramBufferList = m_histogramBufferMetaDataStreamInList;
415 break;
416 case HistogramStreamOut:
417 histogramBufferList = m_histogramBufferMetaDataStreamOutList;
418 break;
419 default:
420 histogramBufferList = nullptr;
421 break;
422 }
423
424 if (histogramBufferList != nullptr && m_allocator != nullptr)
425 {
426 for (auto i = 0; i < DecodeBasicFeature::m_maxFrameIndex; i++)
427 {
428 MOS_BUFFER *histogramBuffer = histogramBufferList[i];
429 if (histogramBuffer == nullptr ||
430 m_allocator->ResourceIsNull(&histogramBuffer->OsResource))
431 {
432 continue;
433 }
434 MOS_STATUS eStatus = m_allocator->Destroy(histogramBuffer);
435 if (eStatus != MOS_STATUS_SUCCESS)
436 {
437 DECODE_ASSERTMESSAGE("Failed to free histogram internal buffer!");
438 }
439 }
440 }
441 }
442 }
443
444 MOS_STATUS DecodeDownSamplingFeature::DumpSfcOutputs(CodechalDebugInterface* debugInterface)
445 {
446 DECODE_FUNC_CALL();
447 DECODE_CHK_NULL(debugInterface);
448 DECODE_CHK_NULL(m_basicFeature);
449
450 // Dump histogram
451 if ((m_histogramDestSurf != nullptr || m_histogramDebug) &&
452 m_histogramBuffer != nullptr &&
453 !m_allocator->ResourceIsNull(&m_histogramBuffer->OsResource))
454 {
455 CODECHAL_DEBUG_TOOL(
456 debugInterface->m_bufferDumpFrameNum = m_basicFeature->m_frameNum;
457 DECODE_CHK_STATUS(debugInterface->DumpBuffer(
458 &m_histogramBuffer->OsResource,
459 m_aqmHistogramEnable ? CodechalDbgAttr::attrAqmHistogram : CodechalDbgAttr::attrSfcHistogram,
460 "_DEC_Hist",
461 HISTOGRAM_BINCOUNT * m_histogramBinWidth));)
462 }
463
464 // Dump histogram U & V
465 if ((m_histogramDestSurfU != nullptr || m_histogramDebug) &&
466 m_histogramBufferU != nullptr &&
467 !m_allocator->ResourceIsNull(&m_histogramBufferU->OsResource))
468 {
469 CODECHAL_DEBUG_TOOL(
470 debugInterface->m_bufferDumpFrameNum = m_basicFeature->m_frameNum;
471 DECODE_CHK_STATUS(debugInterface->DumpBuffer(
472 &m_histogramBufferU->OsResource,
473 m_aqmHistogramEnable ? CodechalDbgAttr::attrAqmHistogram : CodechalDbgAttr::attrSfcHistogram,
474 "_DEC_Hist_U",
475 HISTOGRAM_BINCOUNT * m_histogramBinWidth));)
476 }
477
478 if ((m_histogramDestSurfV != nullptr || m_histogramDebug) &&
479 m_histogramBufferV != nullptr &&
480 !m_allocator->ResourceIsNull(&m_histogramBufferV->OsResource))
481 {
482 CODECHAL_DEBUG_TOOL(
483 debugInterface->m_bufferDumpFrameNum = m_basicFeature->m_frameNum;
484 DECODE_CHK_STATUS(debugInterface->DumpBuffer(
485 &m_histogramBufferV->OsResource,
486 m_aqmHistogramEnable ? CodechalDbgAttr::attrAqmHistogram : CodechalDbgAttr::attrSfcHistogram,
487 "_DEC_Hist_V",
488 HISTOGRAM_BINCOUNT * m_histogramBinWidth));)
489 }
490
491 // Dump SFC
492 if (!m_allocator->ResourceIsNull(&m_outputSurface.OsResource) &&
493 m_inputSurface != nullptr &&
494 !m_aqmHistogramEnable)
495 {
496 CODECHAL_DEBUG_TOOL(
497 debugInterface->m_bufferDumpFrameNum = m_basicFeature->m_frameNum;
498 DECODE_CHK_STATUS(debugInterface->DumpYUVSurface(
499 &m_outputSurface,
500 CodechalDbgAttr::attrSfcOutputSurface,
501 "_SFCSurf"));)
502 }
503
504 return MOS_STATUS_SUCCESS;
505 }
506 }
507
508 #endif