xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/codec/hal/codechal_debug.h (revision ba62d9d3abf0e404f2022b4cd7a85e107f48596f)
1 /*
2 * Copyright (c) 2011-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     codechal_debug.h
24 //! \brief    Defines the debug interface shared by codec only.
25 //! \details  The debug interface dumps output from Media based on in input config file.
26 //!
27 #ifndef __CODEC_DEBUG_H__
28 #define __CODEC_DEBUG_H__
29 
30 #include "media_debug_interface.h"
31 #include "codec_hw_next.h"
32 #include "encode_status_report.h"
33 #include "codechal_event_debug.h"
34 #include "codec_def_decode_avc.h"
35 #include "codec_def_decode_av1.h"
36 #include "codec_def_decode_jpeg.h"
37 #include "codec_def_decode_mpeg2.h"
38 #include "codec_def_decode_vp8.h"
39 
40 static uint32_t DecodeFrameIndex = 0;
41 static uint32_t DecodeOutputIndex = 0;
42 
43 #if USE_MEDIA_DEBUG_TOOL
44 
45 #define USE_CODECHAL_DEBUG_TOOL 1
46 #define CODECHAL_DEBUG_TOOL(expr) expr;
47 
48 typedef struct _CODECHAL_DBG_CFG CODECHAL_DBG_CFG, *PCODECHAL_DBG_CFG;
49 
50 namespace CodechalDbgFieldType   = MediaDbgFieldType;
51 namespace CodechalDbgExtType     = MediaDbgExtType;
52 namespace CodechalDbgSurfaceType = MediaDbgSurfaceType;
53 namespace CodechalDbgBufferType  = MediaDbgBufferType;
54 namespace CodechalDbgAttr        = MediaDbgAttr;
55 
56 #define __MEDIA_USER_FEATURE_VALUE_DECODE_SFC_RGBFORMAT_OUTPUT_DEBUG "Decode SFC RGB Format Output"
57 #define __MEDIA_USER_FEATURE_VALUE_DECODE_SFC_LINEAR_OUTPUT_DEBUG "Decode SFC Linear Output Debug"
58 #define __MEDIA_USER_FEATURE_ENABLE_HW_DEBUG_HOOKS_DEBUG "Enable Media Debug Hooks"
59 #define __MEDIA_USER_FEATURE_VALUE_CODECHAL_FRAME_NUMBER_TO_STOP_DEBUG "Decode Stop To Frame"
60 #define __MEDIA_USER_FEATURE_VALUE_CODECHAL_ENABLE_SW_CRC_DEBUG "Enable SW CRC"
61 //------------------------------------------------------------------------------
62 // Macros specific to MOS_CODEC_SUBCOMP_DEBUG sub-comp
63 //------------------------------------------------------------------------------
64 #define CODECHAL_DEBUG_ASSERT(_expr) \
65     MOS_ASSERT(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _expr)
66 
67 #define CODECHAL_DEBUG_ASSERTMESSAGE(_message, ...) \
68     MOS_ASSERTMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _message, ##__VA_ARGS__)
69 
70 #define CODECHAL_DEBUG_NORMALMESSAGE(_message, ...) \
71     MOS_NORMALMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _message, ##__VA_ARGS__)
72 
73 #define CODECHAL_DEBUG_VERBOSEMESSAGE(_message, ...) \
74     MOS_VERBOSEMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _message, ##__VA_ARGS__)
75 
76 #define CODECHAL_DEBUG_FUNCTION_ENTER \
77     MOS_FUNCTION_ENTER(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG)
78 
79 #define CODECHAL_DEBUG_CHK_STATUS(_stmt) \
80     MOS_CHK_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _stmt)
81 
82 #define CODECHAL_DEBUG_CHK_STATUS_MESSAGE(_stmt, _message, ...) \
83     MOS_CHK_STATUS_MESSAGE_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _stmt, _message, ##__VA_ARGS__)
84 
85 #define CODECHAL_DEBUG_CHK_NULL(_ptr) \
86     MOS_CHK_NULL_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _ptr)
87 
88 #define CODECHAL_DEBUG_CHK_NULL_NO_STATUS(_ptr) \
89     MOS_CHK_NULL_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _ptr)
90 
91 enum CodechalHucRegionDumpType
92 {
93     hucRegionDumpDefault        = 0,
94     hucRegionDumpInit           = 1,
95     hucRegionDumpUpdate         = 2,
96     hucRegionDumpLAUpdate       = 3,
97     hucRegionDumpRegionLocked   = 4,
98     hucRegionDumpCmdInitializer = 5,
99     hucRegionDumpPakIntegrate   = 6,
100     hucRegionDumpHpu            = 7,
101     hucRegionDumpBackAnnotation = 8,
102     hucRegionDumpHpuSuperFrame  = 9
103 };
104 
105 typedef struct _CODECHAL_ME_OUTPUT_PARAMS
106 {
107     PMOS_SURFACE  psMeMvBuffer;
108     PMOS_SURFACE  psMeBrcDistortionBuffer;
109     PMOS_SURFACE  psMeDistortionBuffer;
110     PMOS_RESOURCE pResVdenStreamInBuffer;
111     bool          b16xMeInUse;
112     bool          b32xMeInUse;
113     bool          bVdencStreamInInUse;
114 } CODECHAL_ME_OUTPUT_PARAMS, *PCODECHAL_ME_OUTPUT_PARAMS;
115 
116 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_PIC_PARAMS &cr);
117 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_SF_SLICE_PARAMS &cr);
118 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_SLICE_PARAMS &cr);
119 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_IQ_MATRIX_PARAMS &cr);
120 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_PIC_PARAMS &cr);
121 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_EXT_PIC_PARAMS &cr);
122 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SCC_PIC_PARAMS &cr);
123 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SF_SLICE_PARAMS &cr);
124 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SLICE_PARAMS &cr);
125 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_EXT_SLICE_PARAMS &cr);
126 std::ostream &operator<<(std::ostream &oss, const CODECHAL_HEVC_IQ_MATRIX_PARAMS &cr);
127 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SUBSET_PARAMS &cr);
128 std::ostream &operator<<(std::ostream &oss, const CODEC_VP9_PIC_PARAMS &cr);
129 std::ostream &operator<<(std::ostream &oss, const CODEC_VP9_SLICE_PARAMS &cr);
130 std::ostream &operator<<(std::ostream &oss, const CODEC_VP9_SEGMENT_PARAMS &cr);
131 std::ostream &operator<<(std::ostream &oss, const CodecAv1PicParams &cr);
132 std::ostream &operator<<(std::ostream &oss, const CodecAv1TileParams &cr);
133 std::ostream &operator<<(std::ostream &oss, const CodecDecodeJpegPicParams &cr);
134 std::ostream &operator<<(std::ostream &oss, const CodecDecodeJpegScanParameter &cr);
135 std::ostream &operator<<(std::ostream &oss, const CODECHAL_DECODE_JPEG_HUFFMAN_TABLE &cr);
136 std::ostream &operator<<(std::ostream &oss, const CodecJpegQuantMatrix &cr);
137 std::ostream &operator<<(std::ostream &oss, const CodecDecodeMpeg2PicParams &cr);
138 std::ostream &operator<<(std::ostream &oss, const CodecDecodeMpeg2SliceParams &cr);
139 std::ostream &operator<<(std::ostream &oss, const CodecDecodeMpeg2MbParams &cr);
140 std::ostream &operator<<(std::ostream &oss, const CodecMpeg2IqMatrix &cr);
141 std::ostream &operator<<(std::ostream &oss, const CODEC_VP8_PIC_PARAMS &cr);
142 std::ostream &operator<<(std::ostream &oss, const CODEC_VP8_SLICE_PARAMS &cr);
143 std::ostream &operator<<(std::ostream &oss, const CODEC_VP8_IQ_MATRIX_PARAMS &cr);
144 
145 void DumpDecodeAvcPicParams(PCODEC_AVC_PIC_PARAMS picParams, std::string fileName);
146 void DumpDecodeAvcSliceParams(PCODEC_AVC_SLICE_PARAMS sliceParams, uint32_t numSlices, std::string fileName, bool shortFormatInUse);
147 void DumpDecodeAvcIQParams(PCODEC_AVC_IQ_MATRIX_PARAMS iqParams, std::string fileName);
148 void DumpDecodeHevcPicParams(PCODEC_HEVC_PIC_PARAMS picParams, std::string fileName);
149 void DumpDecodeHevcExtPicParams(PCODEC_HEVC_EXT_PIC_PARAMS extPicParams, std::string fileName);
150 void DumpDecodeHevcSccPicParams(PCODEC_HEVC_SCC_PIC_PARAMS sccPicParams, std::string fileName);
151 void DumpDecodeHevcSliceParams(PCODEC_HEVC_SLICE_PARAMS sliceParams, uint32_t numSlices, std::string fileName, bool shortFormatInUse);
152 void DumpDecodeHevcExtSliceParams(PCODEC_HEVC_EXT_SLICE_PARAMS extSliceParams, uint32_t numSlices, std::string fileName);
153 void DumpDecodeHevcIQParams(PCODECHAL_HEVC_IQ_MATRIX_PARAMS iqParams, std::string fileName);
154 void DumpDecodeHevcSubsetParams(PCODEC_HEVC_SUBSET_PARAMS subsetsParams, std::string fileName);
155 void DumpDecodeVp9PicParams(PCODEC_VP9_PIC_PARAMS picParams, std::string fileName);
156 void DumpDecodeVp9SliceParams(PCODEC_VP9_SLICE_PARAMS slcParams, std::string fileName);
157 void DumpDecodeVp9SegmentParams(PCODEC_VP9_SEGMENT_PARAMS segmentParams, std::string fileName);
158 void DumpDecodeAv1PicParams(CodecAv1PicParams *picParams, std::string fileName);
159 void DumpDecodeAv1TileParams(CodecAv1TileParams *tileParams, uint32_t tileNum, std::string fileName);
160 void DumpDecodeJpegPicParams(CodecDecodeJpegPicParams *picParams, std::string fileName);
161 void DumpDecodeJpegScanParams(CodecDecodeJpegScanParameter *scanParams, std::string fileName);
162 void DumpDecodeJpegHuffmanParams(PCODECHAL_DECODE_JPEG_HUFFMAN_TABLE huffmanTable, std::string fileName);
163 void DumpDecodeJpegIqParams(CodecJpegQuantMatrix *iqParams, std::string fileName);
164 void DumpDecodeMpeg2PicParams(CodecDecodeMpeg2PicParams *picParams, std::string fileName);
165 void DumpDecodeMpeg2SliceParams(CodecDecodeMpeg2SliceParams *sliceParams, uint32_t numSlices, std::string fileName);
166 void DumpDecodeMpeg2MbParams(CodecDecodeMpeg2MbParams *mbParams, uint32_t numMbs, std::string fileName);
167 void DumpDecodeMpeg2IqParams(CodecMpeg2IqMatrix *iqParams, std::string fileName);
168 void DumpDecodeVp8PicParams(PCODEC_VP8_PIC_PARAMS picParams, std::string fileName);
169 void DumpDecodeVp8SliceParams(PCODEC_VP8_SLICE_PARAMS sliceParams, std::string fileName);
170 void DumpDecodeVp8IqParams(PCODEC_VP8_IQ_MATRIX_PARAMS iqParams, std::string fileName);
171 
172 class MediaDebugInterface;
173 class CodechalHwInterface;
174 class CodechalDebugInterface : public MediaDebugInterface
175 {
176 public:
177     CodechalDebugInterface();
178     virtual ~CodechalDebugInterface();
179 
180     static CodechalDebugInterface* Create();
181 
182     virtual MOS_STATUS Initialize(
183         CodechalHwInterface *hwInterface,
184         CODECHAL_FUNCTION    codecFunction,
185         MediaCopyWrapper    *mediaCopyWrapper = nullptr);
186 
187     virtual MOS_STATUS Initialize(
188         CodechalHwInterfaceNext *hwInterface,
189         CODECHAL_FUNCTION        codecFunction,
190         MediaCopyWrapper        *mediaCopyWrapper = nullptr);
191 
192     virtual MOS_STATUS DumpHucDmem(
193         PMOS_RESOURCE             dmemResource,
194         uint32_t                  dmemSize,
195         uint32_t                  hucPassNum,
196         CodechalHucRegionDumpType dumpType);
197 
198     virtual MOS_STATUS DumpHucRegion(
199         PMOS_RESOURCE             region,
200         uint32_t                  regionOffset,
201         uint32_t                  regionSize,
202         uint32_t                  regionNum,
203         const char *              regionName,
204         bool                      inputBuffer,
205         uint32_t                  hucPassNum,
206         CodechalHucRegionDumpType dumpType);
207 
208     MOS_STATUS DetectCorruptionSw(std::vector<MOS_RESOURCE>& vResource, PMOS_RESOURCE frameCntRes, uint8_t *buf, uint32_t &size, uint32_t frameNum);
209 
210     MOS_STATUS DetectCorruptionHw(void *hwInterface, PMOS_RESOURCE frameCntRes, uint32_t curIdx, uint32_t frameCrcOffset, std::vector<MOS_RESOURCE> &vStatusBuffer, PMOS_COMMAND_BUFFER pCmdBuffer, uint32_t frameNum);
211 
212     MOS_STATUS StoreNumFrame(PMHW_MI_INTERFACE pMiInterface, PMOS_RESOURCE pResource, int32_t frameNum, PMOS_COMMAND_BUFFER pCmdBuffer);
213 
214     MOS_STATUS DumpEncodeStatusReport(
215         const struct EncodeStatusReport *report);
216 
217     MOS_STATUS DumpEncodeStatusReport(
218         const struct encode::EncodeStatusReportData *report);
219 
220     void CheckGoldenReferenceExist();
221 
222     MOS_STATUS DumpRgbDataOnYUVSurface(
223         PMOS_SURFACE               surface,
224         const char                *attrName,
225         const char                *surfName,
226         CODECHAL_MEDIA_STATE_TYPE  mediaState = CODECHAL_NUM_MEDIA_STATES,
227         uint32_t                   width_in   = 0,
228         uint32_t                   height_in  = 0);
229 
230     MOS_STATUS DumpBltOutput(
231         PMOS_SURFACE surface,
232         const char  *attrName) override;
233 
234     virtual MOS_STATUS InitializeUserSetting() override;
235 
236     bool DumpIsEnabled(
237         const char *              attr,
238         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES);
239 
240     MOS_STATUS SetFastDumpConfig(MediaCopyWrapper *mediaCopyWrapper);
241 
242     const char *CreateFileName(
243         const char *funcName,
244         const char *bufType,
245         const char *extType);
246 
247     MOS_STATUS DumpStringStream(
248         std::stringstream& ss,
249         const char*        bufferName,
250         const char*        attrName);
251 
252     virtual MOS_STATUS DumpCmdBuffer(
253         PMOS_COMMAND_BUFFER       cmdBuffer,
254         CODECHAL_MEDIA_STATE_TYPE mediaState,
255         const char *              cmdName = nullptr);
256 
257     MOS_STATUS Dump2ndLvlBatch(
258         PMHW_BATCH_BUFFER         batchBuffer,
259         CODECHAL_MEDIA_STATE_TYPE mediaState,
260         const char *              batchName = nullptr);
261 
262     MOS_STATUS DumpCurbe(
263         CODECHAL_MEDIA_STATE_TYPE mediaState,
264         PMHW_KERNEL_STATE         kernelState);
265 
266     MOS_STATUS DumpMDFCurbe(
267         CODECHAL_MEDIA_STATE_TYPE mediaState,
268         uint8_t *                 curbeBuffer,
269         uint32_t                  curbeSize);
270 
271     MOS_STATUS DumpKernelRegion(
272         CODECHAL_MEDIA_STATE_TYPE mediaState,
273         MHW_STATE_HEAP_TYPE       stateHeapType,
274         PMHW_KERNEL_STATE         kernelState);
275 
276     MOS_STATUS DumpYUVSurfaceToBuffer(PMOS_SURFACE surface, uint8_t *buffer, uint32_t &size);
277 
278     MOS_STATUS DumpYUVSurface(
279         PMOS_SURFACE              surface,
280         const char *              attrName,
281         const char *              surfName,
282         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES,
283         uint32_t                  width_in   = 0,
284         uint32_t                  height_in  = 0);
285 
286     virtual MOS_STATUS DumpBuffer(
287         PMOS_RESOURCE             resource,
288         const char *              attrName,
289         const char *              bufferName,
290         uint32_t                  size,
291         uint32_t                  offset = 0,
292         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES);
293 
294     MOS_STATUS DumpSurface(
295         PMOS_SURFACE              surface,
296         const char *              attrName,
297         const char *              surfaceName,
298         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES);
299 
300     MOS_STATUS DumpData(
301         void       *data,
302         uint32_t   size,
303         const char *attrName,
304         const char *bufferName);
305 
306     MOS_STATUS DumpSurfaceInfo(
307         PMOS_SURFACE surface,
308         const char*  surfaceName);
309 
310     CodechalHwInterface     *m_hwInterface      = nullptr;
311     CodechalHwInterfaceNext *m_hwInterfaceNext  = nullptr;
312     CODECHAL_FUNCTION        m_codecFunction    = CODECHAL_FUNCTION_INVALID;
313     PCODECHAL_DBG_CFG        m_dbgCfgHead       = nullptr;
314     CODEC_PICTURE            m_currPic          = {};
315 
316 protected:
317     std::string SetOutputPathKey() override;
318     std::string InitDefaultOutput() override;
319     uint8_t *m_decodeOutputBuf = nullptr;
320     PLATFORM    m_platform        = {};
321 
322     std::function<
323         MOS_STATUS(
324             PMOS_SURFACE              surface,
325             const char               *attrName,
326             const char               *surfName,
327             CODECHAL_MEDIA_STATE_TYPE mediaState,
328             uint32_t                  width_in,
329             uint32_t                  height_in)>
330         m_dumpYUVSurface;
331 
332     std::function<
333         MOS_STATUS(
334             PMOS_SURFACE              surface,
335             const char               *attrName,
336             const char               *surfName,
337             CODECHAL_MEDIA_STATE_TYPE mediaState,
338             uint32_t                  width_in,
339             uint32_t                  height_in)>
340         m_dumpYUVSurfaceLegacy;
341 
342     std::function<
343         MOS_STATUS(
344             PMOS_RESOURCE             resource,
345             const char               *attrName,
346             const char               *bufferName,
347             uint32_t                  size,
348             uint32_t                  offset,
349             CODECHAL_MEDIA_STATE_TYPE mediaState)>
350         m_dumpBuffer;
351 
352     std::function<
353         MOS_STATUS(
354             PMOS_RESOURCE             resource,
355             const char               *attrName,
356             const char               *bufferName,
357             uint32_t                  size,
358             uint32_t                  offset,
359             CODECHAL_MEDIA_STATE_TYPE mediaState)>
360         m_dumpBufferLegacy;
361 
362 private:
363     MOS_STATUS VDBypassCopyResource(PMOS_RESOURCE src, PMOS_RESOURCE dst);
364     MOS_STATUS AllocateLinearResource(
365         MOS_GFXRES_TYPE eType,
366         uint32_t        dwWidth,
367         uint32_t        dwHeight,
368         MOS_FORMAT      eFormat,
369         PMOS_RESOURCE   pResource,
370         bool            bSystemMem = false,
371         bool            bLockable = false) const;
372     MOS_STATUS FillResourceMemory(
373         MOS_RESOURCE &sResource,
374         uint32_t      uiSize,
375         uint8_t       ucValue);
376 
377 MEDIA_CLASS_DEFINE_END(CodechalDebugInterface)
378 };
379 #else
380 #define USE_CODECHAL_DEBUG_TOOL 0
381 #define CODECHAL_DEBUG_TOOL(expr) ;
382 
383 #endif  // USE_MEDIA_DEBUG_TOOL
384 #endif  /* __MEDIA_DEBUG_H__ */
385