1 /*
2 * Copyright (c) 2011-2018, 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 all of CodecHal.
25 //! \details  The debug interface dumps output from CodecHal based on in input config file.
26 //!
27 #ifndef __CODECHAL_DEBUG_H__
28 #define __CODECHAL_DEBUG_H__
29 #if (_DEBUG || _RELEASE_INTERNAL)
30 
31 #define USE_CODECHAL_DEBUG_TOOL 1
32 #define CODECHAL_DEBUG_TOOL(expr)   expr;
33 
34 #include "BRCIF.h"
35 #include "mhw_utilities.h"
36 #include "mhw_state_heap.h"
37 #include "codec_def_common.h"
38 #include "codechal_debug_config_manager.h"
39 #include <sstream>
40 #include <fstream>
41 
42 #define CODECHAL_DEBUG_TOOL(expr)   expr;
43 
44 //------------------------------------------------------------------------------
45 // Macros specific to MOS_CODEC_SUBCOMP_DEBUG sub-comp
46 //------------------------------------------------------------------------------
47 #define CODECHAL_DEBUG_ASSERT(_expr) \
48     MOS_ASSERT(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _expr)
49 
50 #define CODECHAL_DEBUG_ASSERTMESSAGE(_message, ...) \
51     MOS_ASSERTMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _message, ##__VA_ARGS__)
52 
53 #define CODECHAL_DEBUG_NORMALMESSAGE(_message, ...) \
54     MOS_NORMALMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _message, ##__VA_ARGS__)
55 
56 #define CODECHAL_DEBUG_VERBOSEMESSAGE(_message, ...) \
57     MOS_VERBOSEMESSAGE(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _message, ##__VA_ARGS__)
58 
59 #define CODECHAL_DEBUG_FUNCTION_ENTER \
60     MOS_FUNCTION_ENTER(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG)
61 
62 #define CODECHAL_DEBUG_CHK_STATUS(_stmt) \
63     MOS_CHK_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _stmt)
64 
65 #define CODECHAL_DEBUG_CHK_STATUS_MESSAGE(_stmt, _message, ...) \
66     MOS_CHK_STATUS_MESSAGE_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _stmt, _message, ##__VA_ARGS__)
67 
68 #define CODECHAL_DEBUG_CHK_NULL(_ptr) \
69     MOS_CHK_NULL_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _ptr)
70 
71 #define CODECHAL_DEBUG_CHK_NULL_NO_STATUS(_ptr) \
72     MOS_CHK_NULL_NO_STATUS_RETURN(MOS_COMPONENT_CODEC, MOS_CODEC_SUBCOMP_DEBUG, _ptr)
73 
74 namespace CodechalDbgFieldType
75 {
76 static const char *topField = "_Top";
77 static const char *botField = "_Bot";
78 static const char *frame    = "_Frame";
79 };
80 
81 namespace CodechalDbgExtType
82 {
83 static const char *dat  = ".dat";
84 static const char *txt  = ".txt";
85 static const char *par  = ".par";
86 static const char *y    = ".Y";
87 static const char *uv   = ".UV";
88 static const char *yuv  = ".yuv";
89 static const char *yuy2 = ".yuy2";
90 static const char *uyvy = ".uyvy";
91 };
92 
93 namespace CodechalDbgSurfaceType
94 {
95 static const char *yuv444    = "_yuv444";
96 static const char *yuv411    = "_yuv411";
97 static const char *yuv420    = "_yuv420";
98 static const char *yuv400    = "_yuv400";
99 static const char *yuv422h2y = "_yuv422h_2y";
100 static const char *yuv422v2y = "_yuv422v_2y";
101 static const char *yuv422h4y = "_yuv422h_4y";
102 static const char *yuv422v4y = "_yuv422v_4y";
103 static const char *yuy2422   = "_yuy2422";
104 static const char *uyvy422   = "_uyvy422";
105 };
106 
107 namespace CodechalDbgBufferType
108 {
109 static const char *bufCmd           = "CmdBuf";
110 static const char *buf2ndLvl        = "2ndLvl";
111 static const char *bufCurbe         = "Curbe";
112 static const char *bufISH           = "ISH";
113 static const char *bufDSH           = "DSH";
114 static const char *bufSSH           = "SSH";
115 static const char *bufSeqParams     = "SeqParams";
116 static const char *bufPicParams     = "PicParams";
117 static const char *bufSlcParams     = "SlcParams";
118 static const char *bufSubsetsParams = "SubsetsParams";
119 static const char *bufVuiParams     = "VuiParams";
120 static const char *bufBitstream     = "Bitstream";
121 static const char *bufFeiPicParams  = "FeiPicParams";
122 static const char *bufMadRead       = "MADRead";
123 static const char *bufMadWrite      = "MADWrite";
124 static const char *bufSegmentParams = "SegmentParams";
125 static const char *bufIqParams      = "IqParams";
126 static const char *bufHuffmanTbl    = "HuffmanTbl";
127 static const char *bufScanParams    = "ScanParams";
128 static const char *bufMvcPicParams  = "MvcPicParams";
129 static const char *bufMbParams      = "MbParams";
130 static const char *bufDecProcParams = "DecProcParams";
131 static const char *bufHucRegion     = "HucRegion";
132 static const char *bufHucDmem       = "HucDmem";
133 static const char *bufEncodePar     = "EncodePar";
134 };
135 
136 namespace CodechalDbgAttr
137 {
138 static const char *attrInitSWScoreboard = "InitSWScoreboard";
139 static const char *attrSfcHistogram = "SfcHistogram";
140 };
141 
142 enum CodechalHucRegionDumpType
143 {
144     hucRegionDumpDefault        = 0,
145     hucRegionDumpInit           = 1,
146     hucRegionDumpUpdate         = 2,
147     hucRegionDumpRegionLocked   = 3,
148     hucRegionDumpCmdInitializer = 4,
149     hucRegionDumpPakIntegrate   = 5,
150     hucRegionDumpHpu            = 6,
151     hucRegionDumpBackAnnotation = 7
152 };
153 
154 struct _CODECHAL_DEBUG_INTERFACE;
155 typedef struct _CODECHAL_DBG_CFG    CODECHAL_DBG_CFG, *PCODECHAL_DBG_CFG;
156 
157 typedef struct _CODECHAL_ME_OUTPUT_PARAMS
158 {
159     PMOS_SURFACE                    psMeMvBuffer;
160     PMOS_SURFACE                    psMeBrcDistortionBuffer;
161     PMOS_SURFACE                    psMeDistortionBuffer;
162     PMOS_RESOURCE                   pResVdenStreamInBuffer;
163     bool                            b16xMeInUse;
164     bool                            b32xMeInUse;
165     bool                            bVdencStreamInInUse;
166 } CODECHAL_ME_OUTPUT_PARAMS, *PCODECHAL_ME_OUTPUT_PARAMS;
167 
168 class CodechalDebugInterface
169 {
170 public:
171     CodechalDebugInterface();
172     virtual ~CodechalDebugInterface();
173 
174     MOS_STATUS Initialize(
175         CodechalHwInterface *hwInterface,
176         CODECHAL_FUNCTION      codecFunction);
177 
178     bool DumpIsEnabled(
179         const char *              attr,
180         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES);
181 
182     const char *CreateFileName(
183         const char *funcName,
184         const char *bufType,
185         const char *extType);
186 
187     MOS_STATUS DumpCmdBuffer(
188         PMOS_COMMAND_BUFFER       cmdBuffer,
189         CODECHAL_MEDIA_STATE_TYPE mediaState,
190         const char *              cmdName = nullptr);
191 
192     MOS_STATUS Dump2ndLvlBatch(
193         PMHW_BATCH_BUFFER         batchBuffer,
194         CODECHAL_MEDIA_STATE_TYPE mediaState,
195         const char *              batchName = nullptr);
196 
197     MOS_STATUS DumpCurbe(
198         CODECHAL_MEDIA_STATE_TYPE mediaState,
199         PMHW_KERNEL_STATE         kernelState);
200 
201     MOS_STATUS DumpMDFCurbe(
202         CODECHAL_MEDIA_STATE_TYPE mediaState,
203         uint8_t *                 curbeBuffer,
204         uint32_t                  curbeSize);
205 
206     MOS_STATUS DumpKernelRegion(
207         CODECHAL_MEDIA_STATE_TYPE mediaState,
208         MHW_STATE_HEAP_TYPE       stateHeapType,
209         PMHW_KERNEL_STATE         kernelState);
210 
211     virtual MOS_STATUS DumpYUVSurface(
212         PMOS_SURFACE              surface,
213         const char *              attrName,
214         const char *              surfName,
215         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES,
216         uint32_t                  width_in = 0,
217         uint32_t                  height_in = 0);
218 
219     MOS_STATUS DumpBuffer(
220         PMOS_RESOURCE             resource,
221         const char *              attrName,
222         const char *              bufferName,
223         uint32_t                  size,
224         uint32_t                  offset     = 0,
225         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES);
226 
227     MOS_STATUS DumpSurface(
228         PMOS_SURFACE              surface,
229         const char *              attrName,
230         const char *              surfaceName,
231         CODECHAL_MEDIA_STATE_TYPE mediaState = CODECHAL_NUM_MEDIA_STATES);
232 
233     MOS_STATUS DumpData(
234         void *      data,
235         uint32_t    size,
236         const char *attrName,
237         const char *bufferName);
238 
239     MOS_STATUS DumpHucDmem(
240         PMOS_RESOURCE             dmemResource,
241         uint32_t                  dmemSize,
242         uint32_t                  hucPassNum,
243         CodechalHucRegionDumpType dumpType);
244 
245     MOS_STATUS DumpHucRegion(
246         PMOS_RESOURCE             region,
247         uint32_t                  regionOffset,
248         uint32_t                  regionSize,
249         uint32_t                  regionNum,
250         const char *              regionName,
251         bool                      inputBuffer,
252         uint32_t                  hucPassNum,
253         CodechalHucRegionDumpType dumpType);
254 
255     virtual MOS_STATUS DumpBltOutput(
256         PMOS_SURFACE              surface,
257         const char *              attrName);
258 
259     MOS_STATUS DeleteCfgLinkNode(uint32_t frameIdx);
260 
261     std::string             m_ddiFileName;
262     std::string             m_outputFileName;
263 
264     MOS_SURFACE          m_temp2DSurfForCopy      = {};
265     CodechalHwInterface *m_hwInterface            = nullptr;
266     PMOS_INTERFACE       m_osInterface            = nullptr;
267     MhwCpInterface      *m_cpInterface            = nullptr;
268     MhwMiInterface      *m_miInterface            = nullptr;
269     CODECHAL_FUNCTION    m_codecFunction          = CODECHAL_FUNCTION_INVALID;
270     bool                 m_enableBinaryDebugDumps = false;
271     bool                 m_enableEncodeDdiDump    = false;
272     PCODECHAL_DBG_CFG    m_dbgCfgHead             = nullptr;
273     CODEC_PICTURE        m_currPic;
274     uint32_t             m_scaledBottomFieldOffset = 0;
275     uint16_t             m_frameType               = 0;
276     uint32_t             m_sliceId                 = 0;  // used for constructing debug file name
277     char                 m_fileName[MOS_MAX_PATH_LENGTH + 1];
278     char                 m_path[MOS_MAX_PATH_LENGTH + 1];
279     bool                 m_secondField              = false;
280     bool                 m_hybridPakP1              = false;
281     bool                 m_hybridVp8EncodeBrcEnable = false;
282     bool                 m_hybridVp9EncodeEnable    = false;
283     bool                 m_vdboxContextCreated      = false;
284     uint16_t             m_preIndex                 = 0;
285     uint16_t             m_refIndex                 = 0;
286     uint32_t             m_bufferDumpFrameNum       = 0;
287     uint32_t             m_decodeSurfDumpFrameNum   = 0;
288 
289     uint32_t             m_streamId = 0;
290 
291     MOS_STATUS DumpBufferInHexDwords(
292         uint8_t *   data,
293         uint32_t    size);
294 
295 protected:
296     MOS_STATUS ReAllocateSurface(
297         PMOS_SURFACE    pSurface,
298         PMOS_SURFACE    pSrcSurf,
299         PCCHAR          pSurfaceName,
300         MOS_GFXRES_TYPE DefaultResType);
301 
302     MOS_STATUS CopySurfaceData_Vdbox(
303         uint32_t        dwDataSize,
304         PMOS_RESOURCE   presSourceSurface,
305         PMOS_RESOURCE   presCopiedSurface);
306 
307     MOS_STATUS DumpNotSwizzled(
308         std::string  surfName,
309         MOS_SURFACE& surf,
310         uint8_t*     lockedAddr,
311         int32_t      size);
312 
313     MOS_STATUS DumpBufferInBinary(
314         uint8_t *    data,
315         uint32_t     size);
316 
317     MOS_STATUS Dump2DBufferInBinary(
318         uint8_t *    data,
319         uint32_t     width,
320         uint32_t     height,
321         uint32_t     pitch);
322 
323     CodechalDebugConfigMgr *m_configMgr = nullptr;
324     std::string             m_outputFilePath;
325 };
326 #else
327 #define USE_CODECHAL_DEBUG_TOOL     0
328 #define CODECHAL_DEBUG_TOOL(expr)   ;
329 #endif  // (_DEBUG || _RELEASE_INTERNAL)
330 
331 #endif  /* __CODECHAL_DEBUG_H__ */
332