xref: /aosp_15_r20/external/intel-media-driver/media_softlet/agnostic/common/codec/hal/codechal_debug.cpp (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.cpp
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 #include "codechal_debug.h"
28 #if USE_CODECHAL_DEBUG_TOOL
29 #include "codechal_debug_config_manager.h"
30 #include "media_debug_fast_dump.h"
31 #include <iomanip>
32 
33 #define QUERY_STATUS_BUFFER_INITIAL_VALUE 0xf
34 #define STORE_DATA_DWORD_DATA 0x01010102
35 
36 // AVC Decode
operator <<(std::ostream & oss,const CODEC_AVC_PIC_PARAMS & cr)37 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_PIC_PARAMS &cr)
38 {
39     const CODEC_AVC_PIC_PARAMS *picParams = &cr;
40 
41     oss << "CurrPic FrameIdx: " << +picParams->CurrPic.FrameIdx << std::endl;
42     oss << "CurrPic PicFlags: " << +picParams->CurrPic.PicFlags << std::endl;
43 
44     // Dump RefFrameList[15]
45     for (uint8_t i = 0; i < 15; ++i)
46     {
47         oss << "RefFrameList[" << +i << "] FrameIdx:" << +picParams->RefFrameList[i].FrameIdx << std::endl;
48         oss << "RefFrameList[" << +i << "] PicFlags:" << +picParams->RefFrameList[i].PicFlags << std::endl;
49     }
50 
51     oss << "pic_width_in_mbs_minus1: " << +picParams->pic_width_in_mbs_minus1 << std::endl;
52     oss << "pic_height_in_mbs_minus1: " << +picParams->pic_height_in_mbs_minus1 << std::endl;
53     oss << "bit_depth_luma_minus8: " << +picParams->bit_depth_luma_minus8 << std::endl;
54     oss << "bit_depth_chroma_minus8: " << +picParams->bit_depth_chroma_minus8 << std::endl;
55     oss << "num_ref_frames: " << +picParams->num_ref_frames << std::endl;
56     oss << "CurrFieldOrderCnt: " << +picParams->CurrFieldOrderCnt[0] << std::endl;
57     oss << "CurrFieldOrderCnt: " << +picParams->CurrFieldOrderCnt[1] << std::endl;
58 
59     // Dump FieldOrderCntList (16x2)
60     for (uint8_t i = 0; i < 2; ++i)
61     {
62         oss << "FieldOrderCntList[" << +i << "]:";
63         for (uint8_t j = 0; j < 16; j++)
64             oss << +picParams->FieldOrderCntList[j][i] << " ";
65         oss << std::endl;
66     }
67 
68     // Dump seq_fields
69     oss << "seq_fields value: " << +picParams->seq_fields.value << std::endl;
70     oss << "chroma_format_idc: " << +picParams->seq_fields.chroma_format_idc << std::endl;
71     oss << "residual_colour_transform_flag: " << +picParams->seq_fields.residual_colour_transform_flag << std::endl;
72     oss << "frame_mbs_only_flag: " << +picParams->seq_fields.frame_mbs_only_flag << std::endl;
73     oss << "mb_adaptive_frame_field_flag: " << +picParams->seq_fields.mb_adaptive_frame_field_flag << std::endl;
74     oss << "direct_8x8_inference_flag: " << +picParams->seq_fields.direct_8x8_inference_flag << std::endl;
75     oss << "log2_max_frame_num_minus4: " << +picParams->seq_fields.log2_max_frame_num_minus4 << std::endl;
76     oss << "pic_order_cnt_type: " << +picParams->seq_fields.pic_order_cnt_type << std::endl;
77     oss << "log2_max_pic_order_cnt_lsb_minus4: " << +picParams->seq_fields.log2_max_pic_order_cnt_lsb_minus4 << std::endl;
78     oss << "delta_pic_order_always_zero_flag: " << +picParams->seq_fields.delta_pic_order_always_zero_flag << std::endl;
79     oss << "num_slice_groups_minus1:" << +picParams->num_slice_groups_minus1 << std::endl;
80     oss << "slice_group_map_type:" << +picParams->slice_group_map_type << std::endl;
81     oss << "slice_group_change_rate_minus1:" << +picParams->slice_group_change_rate_minus1 << std::endl;
82     oss << "pic_init_qp_minus26:" << +picParams->pic_init_qp_minus26 << std::endl;
83     oss << "chroma_qp_index_offset:" << +picParams->chroma_qp_index_offset << std::endl;
84     oss << "second_chroma_qp_index_offset:" << +picParams->second_chroma_qp_index_offset << std::endl;
85 
86     // Dump pic_fields
87     oss << "pic_fields value: " << +picParams->pic_fields.value << std::endl;
88     oss << "entropy_coding_mode_flag: " << +picParams->pic_fields.entropy_coding_mode_flag << std::endl;
89     oss << "weighted_pred_flag: " << +picParams->pic_fields.weighted_pred_flag << std::endl;
90     oss << "weighted_bipred_idc: " << +picParams->pic_fields.weighted_bipred_idc << std::endl;
91     oss << "transform_8x8_mode_flag: " << +picParams->pic_fields.transform_8x8_mode_flag << std::endl;
92     oss << "field_pic_flag: " << +picParams->pic_fields.field_pic_flag << std::endl;
93     oss << "constrained_intra_pred_flag: " << +picParams->pic_fields.constrained_intra_pred_flag << std::endl;
94     oss << "pic_order_present_flag: " << +picParams->pic_fields.pic_order_present_flag << std::endl;
95     oss << "deblocking_filter_control_present_flag: " << +picParams->pic_fields.deblocking_filter_control_present_flag << std::endl;
96     oss << "redundant_pic_cnt_present_flag: " << +picParams->pic_fields.redundant_pic_cnt_present_flag << std::endl;
97     oss << "reference_pic_flag: " << +picParams->pic_fields.reference_pic_flag << std::endl;
98     oss << "IntraPicFlag: " << +picParams->pic_fields.IntraPicFlag << std::endl;
99 
100     // Dump Short format specific
101     oss << "num_ref_idx_l0_active_minus1: " << +picParams->num_ref_idx_l0_active_minus1 << std::endl;
102     oss << "num_ref_idx_l1_active_minus1: " << +picParams->num_ref_idx_l1_active_minus1 << std::endl;
103     oss << "NonExistingFrameFlags: " << +picParams->NonExistingFrameFlags << std::endl;
104     oss << "UsedForReferenceFlags: " << +picParams->UsedForReferenceFlags << std::endl;
105     oss << "frame_num: " << +picParams->frame_num << std::endl;
106     oss << "StatusReportFeedbackNumber: " << +picParams->StatusReportFeedbackNumber << std::endl;
107 
108     // Dump FrameNumList[16]
109     oss << "scaling_list_present_flag_buffer:";
110     for (uint8_t i = 0; i < 16; i++)
111         oss << picParams->FrameNumList[i];
112     oss << std::endl;
113 
114     return oss;
115 }
116 
operator <<(std::ostream & oss,const CODEC_AVC_SF_SLICE_PARAMS & cr)117 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_SF_SLICE_PARAMS &cr)
118 {
119     const CODEC_AVC_SF_SLICE_PARAMS *sliceControl = &cr;
120 
121     oss << "slice_data_size: " << +sliceControl->slice_data_size << std::endl;
122     oss << "slice_data_offset: " << +sliceControl->slice_data_offset << std::endl;
123     oss << std::endl;
124 
125     return oss;
126 }
127 
operator <<(std::ostream & oss,const CODEC_AVC_SLICE_PARAMS & cr)128 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_SLICE_PARAMS &cr)
129 {
130     const CODEC_AVC_SLICE_PARAMS *sliceControl = &cr;
131 
132     oss << "slice_data_size: " << +sliceControl->slice_data_size << std::endl;
133     oss << "slice_data_offset: " << +sliceControl->slice_data_offset << std::endl;
134 
135     // Dump Long format Params
136     oss << std::endl;
137     oss << "// Long Format Specific" << std::endl;
138     oss << "slice_data_bit_offset: " << +sliceControl->slice_data_bit_offset << std::endl;
139     oss << "first_mb_in_slice: " << +sliceControl->first_mb_in_slice << std::endl;
140     oss << "NumMbsForSlice: " << +sliceControl->NumMbsForSlice << std::endl;
141     oss << "slice_type: " << +sliceControl->slice_type << std::endl;
142     oss << "direct_spatial_mv_pred_flag: " << +sliceControl->direct_spatial_mv_pred_flag << std::endl;
143     oss << "num_ref_idx_l0_active_minus1: " << +sliceControl->num_ref_idx_l0_active_minus1 << std::endl;
144     oss << "num_ref_idx_l1_active_minus1: " << +sliceControl->num_ref_idx_l1_active_minus1 << std::endl;
145     oss << "cabac_init_idc: " << +sliceControl->cabac_init_idc << std::endl;
146     oss << "slice_qp_delta: " << +sliceControl->slice_qp_delta << std::endl;
147     oss << "disable_deblocking_filter_idc: " << +sliceControl->disable_deblocking_filter_idc << std::endl;
148     oss << "slice_alpha_c0_offset_div2: " << +sliceControl->slice_alpha_c0_offset_div2 << std::endl;
149     oss << "slice_beta_offset_div2: " << +sliceControl->slice_beta_offset_div2 << std::endl;
150 
151     // Dump RefPicList[2][32]
152     for (uint8_t i = 0; i < 32; ++i)
153     {
154         oss << "RefPicList[0][" << +i << "] FrameIdx: " << +sliceControl->RefPicList[0][i].FrameIdx << std::endl;
155         oss << "RefPicList[0][" << +i << "] PicFlags: " << +sliceControl->RefPicList[0][i].PicFlags << std::endl;
156         oss << "RefPicList[1][" << +i << "] FrameIdx: " << +sliceControl->RefPicList[1][i].FrameIdx << std::endl;
157         oss << "RefPicList[1][" << +i << "] PicFlags: " << +sliceControl->RefPicList[1][i].PicFlags << std::endl;
158     }
159 
160     oss << "luma_log2_weight_denom: " << +sliceControl->luma_log2_weight_denom << std::endl;
161     oss << "chroma_log2_weight_denom: " << +sliceControl->chroma_log2_weight_denom << std::endl;
162     oss << "slice_id: " << +sliceControl->slice_id << std::endl;
163 
164     // Dump Weights[2][32][3][2]
165     for (uint8_t i = 0; i < 32; ++i)
166     {
167         oss << "Weights[0][" << +i << "][0][0]: " << +sliceControl->Weights[0][i][0][0] << std::endl;
168         oss << "Weights[0][" << +i << "][0][1]: " << +sliceControl->Weights[0][i][0][1] << std::endl;
169         oss << "Weights[0][" << +i << "][1][0]: " << +sliceControl->Weights[0][i][1][0] << std::endl;
170         oss << "Weights[0][" << +i << "][1][1]: " << +sliceControl->Weights[0][i][1][1] << std::endl;
171         oss << "Weights[1][" << +i << "][0][0]: " << +sliceControl->Weights[1][i][0][0] << std::endl;
172         oss << "Weights[1][" << +i << "][0][1]: " << +sliceControl->Weights[1][i][0][1] << std::endl;
173         oss << "Weights[1][" << +i << "][1][0]: " << +sliceControl->Weights[1][i][1][0] << std::endl;
174         oss << "Weights[1][" << +i << "][1][1]: " << +sliceControl->Weights[1][i][1][1] << std::endl;
175         oss << "Weights[0][" << +i << "][2][0]: " << +sliceControl->Weights[0][i][2][0] << std::endl;
176         oss << "Weights[0][" << +i << "][2][1]: " << +sliceControl->Weights[0][i][2][1] << std::endl;
177         oss << "Weights[1][" << +i << "][2][0]: " << +sliceControl->Weights[1][i][2][0] << std::endl;
178         oss << "Weights[1][" << +i << "][2][1]: " << +sliceControl->Weights[1][i][2][1] << std::endl;
179     }
180 
181     oss << std::endl;
182 
183     return oss;
184 }
185 
operator <<(std::ostream & oss,const CODEC_AVC_IQ_MATRIX_PARAMS & cr)186 std::ostream &operator<<(std::ostream &oss, const CODEC_AVC_IQ_MATRIX_PARAMS &cr)
187 {
188     const CODEC_AVC_IQ_MATRIX_PARAMS *iqParams = &cr;
189 
190     uint32_t idx, idx2;
191     // 4x4 block
192     for (idx2 = 0; idx2 < 6; idx2++)
193     {
194         oss << "Qmatrix_H264_ScalingLists4x4[" << +idx2 << "]:" << std::endl;
195         for (idx = 0; idx < 12; idx += 4)
196         {
197             oss << "ScalingList4x4[" << +idx / 4 << "]:";
198             oss << +iqParams->ScalingList4x4[idx2][idx] << " ";
199             oss << +iqParams->ScalingList4x4[idx2][idx + 1] << " ";
200             oss << +iqParams->ScalingList4x4[idx2][idx + 2] << " ";
201             oss << +iqParams->ScalingList4x4[idx2][idx + 3] << " ";
202             oss << std::endl;
203         }
204         oss << std::endl;
205     }
206     // 8x8 block
207     for (idx2 = 0; idx2 < 2; idx2++)
208     {
209         oss << "Qmatrix_H264_ScalingLists8x8[" << +idx2 << "]:" << std::endl;
210         for (idx = 0; idx < 56; idx += 8)
211         {
212             oss << "ScalingList8x8[" << +idx / 8 << "]:";
213             oss << +iqParams->ScalingList8x8[idx2][idx] << " ";
214             oss << +iqParams->ScalingList8x8[idx2][idx + 1] << " ";
215             oss << +iqParams->ScalingList8x8[idx2][idx + 2] << " ";
216             oss << +iqParams->ScalingList8x8[idx2][idx + 3] << " ";
217             oss << +iqParams->ScalingList8x8[idx2][idx + 4] << " ";
218             oss << +iqParams->ScalingList8x8[idx2][idx + 5] << " ";
219             oss << +iqParams->ScalingList8x8[idx2][idx + 6] << " ";
220             oss << +iqParams->ScalingList8x8[idx2][idx + 7] << " ";
221             oss << std::endl;
222         }
223         oss << std::endl;
224     }
225 
226     return oss;
227 }
228 
DumpDecodeAvcPicParams(PCODEC_AVC_PIC_PARAMS picParams,std::string fileName)229 void DumpDecodeAvcPicParams(PCODEC_AVC_PIC_PARAMS picParams, std::string fileName)
230 {
231     std::ostringstream oss;
232     oss.setf(std::ios::showbase | std::ios::uppercase);
233 
234     oss << *picParams << std::endl;
235 
236     std::ofstream ofs(fileName, std::ios::out);
237     ofs << oss.str();
238     ofs.close();
239 }
240 
DumpDecodeAvcSliceParams(PCODEC_AVC_SLICE_PARAMS sliceParams,uint32_t numSlices,std::string fileName,bool shortFormatInUse)241 void DumpDecodeAvcSliceParams(PCODEC_AVC_SLICE_PARAMS sliceParams, uint32_t numSlices, std::string fileName, bool shortFormatInUse)
242 {
243     std::ostringstream oss;
244     oss.setf(std::ios::showbase | std::ios::uppercase);
245 
246     if (numSlices > 1)
247     {
248         oss << "Total Num: " << numSlices << std::endl;
249         oss << std::endl;
250     }
251 
252     for (uint16_t i = 0; i < numSlices; i++)
253     {
254         if (numSlices > 1)
255         {
256             oss << "---------Index = " << i << "---------" << std::endl;
257         }
258 
259         if (shortFormatInUse)
260         {
261             oss << reinterpret_cast<CODEC_AVC_SF_SLICE_PARAMS &>(sliceParams[i]) << std::endl;
262         }
263         else
264         {
265             oss << sliceParams[i] << std::endl;
266         }
267     }
268 
269     std::ofstream ofs(fileName, std::ios::out);
270     ofs << oss.str();
271     ofs.close();
272 }
273 
DumpDecodeAvcIQParams(PCODEC_AVC_IQ_MATRIX_PARAMS iqParams,std::string fileName)274 void DumpDecodeAvcIQParams(PCODEC_AVC_IQ_MATRIX_PARAMS iqParams, std::string fileName)
275 {
276     std::ostringstream oss;
277     oss.setf(std::ios::showbase | std::ios::uppercase);
278 
279     oss << *iqParams << std::endl;
280 
281     std::ofstream ofs(fileName, std::ios::out);
282     ofs << oss.str();
283     ofs.close();
284 }
285 
286 // HEVC Decode
operator <<(std::ostream & oss,const CODEC_HEVC_PIC_PARAMS & cr)287 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_PIC_PARAMS &cr)
288 {
289     const CODEC_HEVC_PIC_PARAMS *picParams = &cr;
290 
291     oss << "PicWidthInMinCbsY: " << +picParams->PicWidthInMinCbsY << std::endl;
292     oss << "PicHeightInMinCbsY: " << +picParams->PicHeightInMinCbsY << std::endl;
293 
294     // FormatAndSequenceInfoFlags
295     oss << "chroma_format_idc: " << +picParams->chroma_format_idc << std::endl;
296     oss << "separate_colour_plane_flag: " << +picParams->separate_colour_plane_flag << std::endl;
297     oss << "bit_depth_luma_minus8: " << +picParams->bit_depth_luma_minus8 << std::endl;
298     oss << "bit_depth_chroma_minus8: " << +picParams->bit_depth_chroma_minus8 << std::endl;
299     oss << "log2_max_pic_order_cnt_lsb_minus4: " << +picParams->log2_max_pic_order_cnt_lsb_minus4 << std::endl;
300     oss << "NoPicReorderingFlag: " << +picParams->NoPicReorderingFlag << std::endl;
301     oss << "ReservedBits1: " << +picParams->ReservedBits1 << std::endl;
302     oss << "wFormatAndSequenceInfoFlags: " << +picParams->wFormatAndSequenceInfoFlags << std::endl;
303     oss << "CurrPic FrameIdx: " << +picParams->CurrPic.FrameIdx << std::endl;
304     oss << "CurrPic PicFlags: " << +picParams->CurrPic.PicFlags << std::endl;
305     oss << "sps_max_dec_pic_buffering_minus1: " << +picParams->sps_max_dec_pic_buffering_minus1 << std::endl;
306     oss << "log2_min_luma_coding_block_size_minus3: " << +picParams->log2_min_luma_coding_block_size_minus3 << std::endl;
307     oss << "log2_diff_max_min_luma_coding_block_size: " << +picParams->log2_diff_max_min_luma_coding_block_size << std::endl;
308     oss << "log2_min_transform_block_size_minus2: " << +picParams->log2_min_transform_block_size_minus2 << std::endl;
309     oss << "log2_diff_max_min_transform_block_size: " << +picParams->log2_diff_max_min_transform_block_size << std::endl;
310     oss << "max_transform_hierarchy_depth_intra: " << +picParams->max_transform_hierarchy_depth_intra << std::endl;
311     oss << "max_transform_hierarchy_depth_inter: " << +picParams->max_transform_hierarchy_depth_inter << std::endl;
312     oss << "num_short_term_ref_pic_sets: " << +picParams->num_short_term_ref_pic_sets << std::endl;
313     oss << "num_long_term_ref_pic_sps: " << +picParams->num_long_term_ref_pic_sps << std::endl;
314     oss << "num_ref_idx_l0_default_active_minus1: " << +picParams->num_ref_idx_l0_default_active_minus1 << std::endl;
315     oss << "num_ref_idx_l1_default_active_minus1: " << +picParams->num_ref_idx_l1_default_active_minus1 << std::endl;
316     oss << "init_qp_minus26: " << +picParams->init_qp_minus26 << std::endl;
317     oss << "ucNumDeltaPocsOfRefRpsIdx: " << +picParams->ucNumDeltaPocsOfRefRpsIdx << std::endl;
318     oss << "wNumBitsForShortTermRPSInSlice: " << +picParams->wNumBitsForShortTermRPSInSlice << std::endl;
319     oss << "ReservedBits2: " << +picParams->ReservedBits2 << std::endl;
320 
321     // CodingParamToolFlags
322     oss << "scaling_list_enabled_flag: " << +picParams->scaling_list_enabled_flag << std::endl;
323     oss << "amp_enabled_flag: " << +picParams->amp_enabled_flag << std::endl;
324     oss << "sample_adaptive_offset_enabled_flag: " << +picParams->sample_adaptive_offset_enabled_flag << std::endl;
325     oss << "pcm_enabled_flag: " << +picParams->pcm_enabled_flag << std::endl;
326     oss << "pcm_sample_bit_depth_luma_minus1: " << +picParams->pcm_sample_bit_depth_luma_minus1 << std::endl;
327     oss << "pcm_sample_bit_depth_chroma_minus1: " << +picParams->pcm_sample_bit_depth_chroma_minus1 << std::endl;
328     oss << "log2_min_pcm_luma_coding_block_size_minus3: " << +picParams->log2_min_pcm_luma_coding_block_size_minus3 << std::endl;
329     oss << "log2_diff_max_min_pcm_luma_coding_block_size: " << +picParams->log2_diff_max_min_pcm_luma_coding_block_size << std::endl;
330     oss << "pcm_loop_filter_disabled_flag: " << +picParams->pcm_loop_filter_disabled_flag << std::endl;
331     oss << "long_term_ref_pics_present_flag: " << +picParams->long_term_ref_pics_present_flag << std::endl;
332     oss << "sps_temporal_mvp_enabled_flag: " << +picParams->sps_temporal_mvp_enabled_flag << std::endl;
333     oss << "strong_intra_smoothing_enabled_flag: " << +picParams->strong_intra_smoothing_enabled_flag << std::endl;
334     oss << "dependent_slice_segments_enabled_flag: " << +picParams->dependent_slice_segments_enabled_flag << std::endl;
335     oss << "output_flag_present_flag: " << +picParams->output_flag_present_flag << std::endl;
336     oss << "num_extra_slice_header_bits: " << +picParams->num_extra_slice_header_bits << std::endl;
337     oss << "sign_data_hiding_enabled_flag: " << +picParams->sign_data_hiding_enabled_flag << std::endl;
338     oss << "cabac_init_present_flag: " << +picParams->cabac_init_present_flag << std::endl;
339     oss << "ReservedBits3: " << +picParams->ReservedBits3 << std::endl;
340     oss << "dwCodingParamToolFlags: " << +picParams->dwCodingParamToolFlags << std::endl;
341 
342     // CodingSettingPicturePropertyFlags
343     oss << "constrained_intra_pred_flag: " << +picParams->constrained_intra_pred_flag << std::endl;
344     oss << "transform_skip_enabled_flag: " << +picParams->transform_skip_enabled_flag << std::endl;
345     oss << "cu_qp_delta_enabled_flag: " << +picParams ->cu_qp_delta_enabled_flag << std::endl;
346     oss << "diff_cu_qp_delta_depth: " << +picParams->diff_cu_qp_delta_depth << std::endl;
347     oss << "pps_slice_chroma_qp_offsets_present_flag: " << +picParams->pps_slice_chroma_qp_offsets_present_flag << std::endl;
348     oss << "weighted_pred_flag: " << +picParams->weighted_pred_flag << std::endl;
349     oss << "weighted_bipred_flag: " << +picParams->weighted_bipred_flag << std::endl;
350     oss << "transquant_bypass_enabled_flag: " << +picParams->transquant_bypass_enabled_flag << std::endl;
351     oss << "tiles_enabled_flag: " << +picParams->tiles_enabled_flag << std::endl;
352     oss << "entropy_coding_sync_enabled_flag: " << +picParams->entropy_coding_sync_enabled_flag << std::endl;
353     oss << "uniform_spacing_flag: " << +picParams->uniform_spacing_flag << std::endl;
354     oss << "loop_filter_across_tiles_enabled_flag: " << +picParams->loop_filter_across_tiles_enabled_flag << std::endl;
355     oss << "pps_loop_filter_across_slices_enabled_flag: " << +picParams->pps_loop_filter_across_slices_enabled_flag << std::endl;
356     oss << "deblocking_filter_override_enabled_flag: " << +picParams->deblocking_filter_override_enabled_flag << std::endl;
357     oss << "pps_deblocking_filter_disabled_flag: " << +picParams->pps_deblocking_filter_disabled_flag << std::endl;
358     oss << "lists_modification_present_flag: " << +picParams->lists_modification_present_flag << std::endl;
359     oss << "slice_segment_header_extension_present_flag: " << +picParams->slice_segment_header_extension_present_flag << std::endl;
360     oss << "IrapPicFlag: " << +picParams->IrapPicFlag << std::endl;
361     oss << "IdrPicFlag: " << +picParams->IdrPicFlag << std::endl;
362     oss << "IntraPicFlag: " << +picParams->IntraPicFlag << std::endl;
363     oss << "ReservedBits4: " << +picParams->ReservedBits4 << std::endl;
364     oss << "dwCodingSettingPicturePropertyFlags: " << +picParams->dwCodingSettingPicturePropertyFlags << std::endl;
365     oss << "pps_cb_qp_offset: " << +picParams->pps_cb_qp_offset << std::endl;
366     oss << "pps_cr_qp_offset: " << +picParams->pps_cr_qp_offset << std::endl;
367     oss << "num_tile_columns_minus1: " << +picParams->num_tile_columns_minus1 << std::endl;
368     oss << "num_tile_rows_minus1: " << +picParams->num_tile_rows_minus1 << std::endl;
369 
370     // column width
371     oss << "column_width_minus1[19]:";
372     for (uint8_t i = 0; i < 19; i++)
373         oss << picParams->column_width_minus1[i] << " ";
374     oss << std::endl;
375 
376     // row height
377     oss << "row_height_minus1[21]:";
378     for (uint8_t i = 0; i < 21; i++)
379         oss << picParams->row_height_minus1[i] << " ";
380     oss << std::endl;
381 
382     oss << "pps_beta_offset_div2: " << +picParams->pps_beta_offset_div2 << std::endl;
383     oss << "pps_tc_offset_div2: " << +picParams->pps_tc_offset_div2 << std::endl;
384     oss << "log2_parallel_merge_level_minus2: " << +picParams->log2_parallel_merge_level_minus2 << std::endl;
385     oss << "CurrPicOrderCntVal: " << +picParams->CurrPicOrderCntVal << std::endl;
386 
387     oss.setf(std::ios::dec, std::ios::basefield);
388 
389     // RefFrameList[15]
390     for (uint8_t i = 0; i < 15; ++i)
391     {
392         oss << "RefFrameList[" << +i << "].FrameIdx:" << +picParams->RefFrameList[i].FrameIdx << std::endl;
393         oss << "RefFrameList[" << +i << "].PicFlags:" << +picParams->RefFrameList[i].PicFlags << std::endl;
394     }
395 
396     // POC List
397     oss << "PicOrderCntValList[15]:";
398     for (uint8_t i = 0; i < 15; i++)
399         oss << picParams->PicOrderCntValList[i] << " ";
400     oss << std::endl;
401 
402     // Ref RefPicSetStCurrBefore List
403     oss << "RefPicSetStCurrBefore[8]:";
404     for (uint8_t i = 0; i < 8; i++)
405         oss << +picParams->RefPicSetStCurrBefore[i] << " ";
406     oss << std::endl;
407 
408     // Ref RefPicSetStCurrAfter List
409     oss << "RefPicSetStCurrAfter[16]:";
410     for (uint8_t i = 0; i < 8; i++)
411         oss << +picParams->RefPicSetStCurrAfter[i] << " ";
412     oss << std::endl;
413 
414     // Ref PicSetStCurr List
415     oss << "RefPicSetLtCurr[16]:";
416     for (uint8_t i = 0; i < 8; i++)
417         oss << +picParams->RefPicSetLtCurr[i] << " ";
418     oss << std::endl;
419 
420     // Ref RefPicSetStCurrBefore List with POC
421     oss << "POC of RefPicSetStCurrBefore[8]: ";
422     for (uint8_t i = 0; i < 8; i++)
423         oss << +picParams->PicOrderCntValList[picParams->RefPicSetStCurrBefore[i] % 15] << " ";
424     oss << std::endl;
425 
426     // Ref RefPicSetStCurrAfter List with POC
427     oss << "POC of RefPicSetStCurrAfter[16]:";
428     for (uint8_t i = 0; i < 8; i++)
429         oss << +picParams->PicOrderCntValList[picParams->RefPicSetStCurrAfter[i] % 15] << " ";
430     oss << std::endl;
431 
432     // Ref PicSetStCurr List with POC
433     oss << "POC of RefPicSetLtCurr[16]: ";
434     for (uint8_t i = 0; i < 8; i++)
435         oss << +picParams->PicOrderCntValList[picParams->RefPicSetLtCurr[i] % 15] << " ";
436     oss << std::endl;
437 
438     oss << "RefFieldPicFlag: " << +picParams->RefFieldPicFlag << std::endl;
439     oss << "RefBottomFieldFlag: " << +picParams->RefBottomFieldFlag << std::endl;
440     oss << "StatusReportFeedbackNumber: " << +picParams->StatusReportFeedbackNumber << std::endl;
441 
442     return oss;
443 }
444 
operator <<(std::ostream & oss,const CODEC_HEVC_EXT_PIC_PARAMS & cr)445 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_EXT_PIC_PARAMS &cr)
446 {
447     const CODEC_HEVC_EXT_PIC_PARAMS *extPicParams = &cr;
448 
449     // PicRangeExtensionFlags
450     oss << "transform_skip_rotation_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.transform_skip_rotation_enabled_flag << std::endl;
451     oss << "transform_skip_context_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.transform_skip_context_enabled_flag << std::endl;
452     oss << "implicit_rdpcm_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.implicit_rdpcm_enabled_flag << std::endl;
453     oss << "explicit_rdpcm_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.explicit_rdpcm_enabled_flag << std::endl;
454     oss << "extended_precision_processing_flag: " << +extPicParams->PicRangeExtensionFlags.fields.extended_precision_processing_flag << std::endl;
455     oss << "intra_smoothing_disabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.intra_smoothing_disabled_flag << std::endl;
456     oss << "high_precision_offsets_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.high_precision_offsets_enabled_flag << std::endl;
457     oss << "persistent_rice_adaptation_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.persistent_rice_adaptation_enabled_flag << std::endl;
458     oss << "cabac_bypass_alignment_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.cabac_bypass_alignment_enabled_flag << std::endl;
459     oss << "cross_component_prediction_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.cross_component_prediction_enabled_flag << std::endl;
460     oss << "chroma_qp_offset_list_enabled_flag: " << +extPicParams->PicRangeExtensionFlags.fields.chroma_qp_offset_list_enabled_flag << std::endl;
461     oss << "BitDepthLuma16: " << +extPicParams->PicRangeExtensionFlags.fields.BitDepthLuma16 << std::endl;
462     oss << "BitDepthChroma16: " << +extPicParams->PicRangeExtensionFlags.fields.BitDepthChroma16 << std::endl;
463     oss << "diff_cu_chroma_qp_offset_depth: " << +extPicParams->diff_cu_chroma_qp_offset_depth << std::endl;
464     oss << "chroma_qp_offset_list_len_minus1: " << +extPicParams->chroma_qp_offset_list_len_minus1 << std::endl;
465     oss << "log2_sao_offset_scale_luma: " << +extPicParams->log2_sao_offset_scale_luma << std::endl;
466     oss << "log2_sao_offset_scale_chroma: " << +extPicParams->log2_sao_offset_scale_chroma << std::endl;
467     oss << "log2_max_transform_skip_block_size_minus2: " << +extPicParams->log2_max_transform_skip_block_size_minus2 << std::endl;
468 
469     // cb_qp_offset_list[6]
470     oss << "cb_qp_offset_list[6]: ";
471     for (uint8_t i = 0; i < 6; i++)
472         oss << +extPicParams->cb_qp_offset_list[i] << " ";
473     oss << std::endl;
474 
475     // cr_qp_offset_list[6]
476     oss << "cr_qp_offset_list[6]: ";
477     for (uint8_t i = 0; i < 6; i++)
478         oss << +extPicParams->cr_qp_offset_list[i] << " ";
479     oss << std::endl;
480 
481     return oss;
482 }
483 
operator <<(std::ostream & oss,const CODEC_HEVC_SCC_PIC_PARAMS & cr)484 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SCC_PIC_PARAMS &cr)
485 {
486     const CODEC_HEVC_SCC_PIC_PARAMS *sccPicParams = &cr;
487 
488     oss << "pps_curr_pic_ref_enabled_flag: " << +sccPicParams->PicSCCExtensionFlags.fields.pps_curr_pic_ref_enabled_flag << std::endl;
489     oss << "palette_mode_enabled_flag: " << +sccPicParams->PicSCCExtensionFlags.fields.palette_mode_enabled_flag << std::endl;
490     oss << "motion_vector_resolution_control_idc: " << +sccPicParams->PicSCCExtensionFlags.fields.motion_vector_resolution_control_idc << std::endl;
491     oss << "intra_boundary_filtering_disabled_flag: " << +sccPicParams->PicSCCExtensionFlags.fields.intra_boundary_filtering_disabled_flag << std::endl;
492     oss << "residual_adaptive_colour_transform_enabled_flag: " << +sccPicParams->PicSCCExtensionFlags.fields.residual_adaptive_colour_transform_enabled_flag << std::endl;
493     oss << "pps_slice_act_qp_offsets_present_flag: " << +sccPicParams->PicSCCExtensionFlags.fields.pps_slice_act_qp_offsets_present_flag << std::endl;
494     oss << "palette_max_size: " << +sccPicParams->palette_max_size << std::endl;
495     oss << "delta_palette_max_predictor_size: " << +sccPicParams->delta_palette_max_predictor_size << std::endl;
496     oss << "PredictorPaletteSize: " << +sccPicParams->PredictorPaletteSize << std::endl;
497 
498     for (uint8_t i = 0; i < 128; i++)
499     {
500         oss << "PredictorPaletteEntries[0][" << +i << "]: " << +sccPicParams->PredictorPaletteEntries[0][i] << std::endl;
501         oss << "PredictorPaletteEntries[1][" << +i << "]: " << +sccPicParams->PredictorPaletteEntries[1][i] << std::endl;
502         oss << "PredictorPaletteEntries[2][" << +i << "]: " << +sccPicParams->PredictorPaletteEntries[2][i] << std::endl;
503     }
504 
505     oss << "pps_act_y_qp_offset_plus5: " << +sccPicParams->pps_act_y_qp_offset_plus5 << std::endl;
506     oss << "pps_act_cb_qp_offset_plus5: " << +sccPicParams->pps_act_y_qp_offset_plus5 << std::endl;
507     oss << "pps_act_cr_qp_offset_plus3: " << +sccPicParams->pps_act_y_qp_offset_plus5 << std::endl;
508 
509     return oss;
510 }
511 
operator <<(std::ostream & oss,const CODEC_HEVC_SF_SLICE_PARAMS & cr)512 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SF_SLICE_PARAMS &cr)
513 {
514     const CODEC_HEVC_SF_SLICE_PARAMS *sliceControl = &cr;
515 
516     oss << "slice_data_size: " << +sliceControl->slice_data_size << std::endl;
517     oss << "slice_data_offset: " << +sliceControl->slice_data_offset << std::endl;
518     oss << "slice_chopping: " << +sliceControl->slice_chopping << std::endl;
519     oss << std::endl;
520 
521     return oss;
522 }
523 
operator <<(std::ostream & oss,const CODEC_HEVC_SLICE_PARAMS & cr)524 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SLICE_PARAMS &cr)
525 {
526     const CODEC_HEVC_SLICE_PARAMS *hevcSliceControl = &cr;
527 
528     oss << "slice_data_size: " << +hevcSliceControl->slice_data_size << std::endl;
529     oss << "slice_data_offset: " << +hevcSliceControl->slice_data_offset << std::endl;
530     oss << "slice_chopping: " << +hevcSliceControl->slice_chopping << std::endl;
531 
532     // Dump Long format Params
533     oss << std::endl;
534     oss << "ByteOffsetToSliceData: " << +hevcSliceControl->ByteOffsetToSliceData << std::endl;
535     oss << "slice_segment_address: " << +hevcSliceControl->slice_segment_address << std::endl;
536 
537     // RefPicList[2][15]
538     for (uint8_t i = 0; i < 15; ++i)
539     {
540         oss << "RefPicList[0][" << +i << "]";
541         oss << ".FrameIdx: " << +hevcSliceControl->RefPicList[0][i].FrameIdx << std::endl;
542         oss << "RefPicList[0][" << +i << "]";
543         oss << ".PicFlags: " << +hevcSliceControl->RefPicList[0][i].PicFlags << std::endl;
544     }
545     for (uint8_t i = 0; i < 15; ++i)
546     {
547         oss << "RefPicList[1][" << +i << "]";
548         oss << ".FrameIdx: " << +hevcSliceControl->RefPicList[1][i].FrameIdx << std::endl;
549         oss << "RefPicList[1][" << +i << "]";
550         oss << ".PicFlags: " << +hevcSliceControl->RefPicList[1][i].PicFlags << std::endl;
551     }
552 
553     oss << "last_slice_of_pic: " << +hevcSliceControl->LongSliceFlags.fields.LastSliceOfPic << std::endl;
554     oss << "dependent_slice_segment_flag: " << +hevcSliceControl->LongSliceFlags.fields.dependent_slice_segment_flag << std::endl;
555     oss << "slice_type: " << +hevcSliceControl->LongSliceFlags.fields.slice_type << std::endl;
556     oss << "color_plane_id: " << +hevcSliceControl->LongSliceFlags.fields.color_plane_id << std::endl;
557     oss << "slice_sao_luma_flag: " << +hevcSliceControl->LongSliceFlags.fields.slice_sao_luma_flag << std::endl;
558     oss << "slice_sao_chroma_flag: " << +hevcSliceControl->LongSliceFlags.fields.slice_sao_chroma_flag << std::endl;
559     oss << "mvd_l1_zero_flag: " << +hevcSliceControl->LongSliceFlags.fields.mvd_l1_zero_flag << std::endl;
560     oss << "cabac_init_flag: " << +hevcSliceControl->LongSliceFlags.fields.cabac_init_flag << std::endl;
561     oss << "slice_temporal_mvp_enabled_flag: " << +hevcSliceControl->LongSliceFlags.fields.slice_temporal_mvp_enabled_flag << std::endl;
562     oss << "slice_deblocking_filter_disabled_flag: " << +hevcSliceControl->LongSliceFlags.fields.slice_deblocking_filter_disabled_flag << std::endl;
563     oss << "collocated_from_l0_flag: " << +hevcSliceControl->LongSliceFlags.fields.collocated_from_l0_flag << std::endl;
564     oss << "slice_loop_filter_across_slices_enabled_flag: " << +hevcSliceControl->LongSliceFlags.fields.slice_loop_filter_across_slices_enabled_flag << std::endl;
565     oss << "reserved: " << +hevcSliceControl->LongSliceFlags.fields.reserved << std::endl;
566     oss << "collocated_ref_idx: " << +hevcSliceControl->collocated_ref_idx << std::endl;
567     oss << "num_ref_idx_l0_active_minus1: " << +hevcSliceControl->num_ref_idx_l0_active_minus1 << std::endl;
568     oss << "num_ref_idx_l1_active_minus1: " << +hevcSliceControl->num_ref_idx_l1_active_minus1 << std::endl;
569     oss << "slice_qp_delta: " << +hevcSliceControl->slice_qp_delta << std::endl;
570     oss << "slice_cb_qp_offset: " << +hevcSliceControl->slice_cb_qp_offset << std::endl;
571     oss << "slice_cr_qp_offset: " << +hevcSliceControl->slice_cr_qp_offset << std::endl;
572     oss << "slice_beta_offset_div2: " << +hevcSliceControl->slice_beta_offset_div2 << std::endl;
573     oss << "slice_tc_offset_div2: " << +hevcSliceControl->slice_tc_offset_div2 << std::endl;
574     oss << "luma_log2_weight_denom: " << +hevcSliceControl->luma_log2_weight_denom << std::endl;
575     oss << "delta_chroma_log2_weight_denom: " << +hevcSliceControl->delta_chroma_log2_weight_denom << std::endl;
576 
577     // luma_offset[2][15]
578     for (uint8_t i = 0; i < 15; i++)
579     {
580         oss << "luma_offset_l0[" << +i << "]: " << +hevcSliceControl->luma_offset_l0[i] << std::endl;
581         oss << "luma_offset_l1[" << +i << "]: " << +hevcSliceControl->luma_offset_l1[i] << std::endl;
582     }
583     // delta_luma_weight[2][15]
584     for (uint8_t i = 0; i < 15; i++)
585     {
586         oss << "delta_luma_weight_l0[" << +i << "]: " << +hevcSliceControl->delta_luma_weight_l0[i] << std::endl;
587         oss << "delta_luma_weight_l1[" << +i << "]: " << +hevcSliceControl->delta_luma_weight_l1[i] << std::endl;
588     }
589     // chroma_offset[2][15][2]
590     for (uint8_t i = 0; i < 15; i++)
591     {
592         oss << "ChromaOffsetL0[" << +i << "][0]: " << +hevcSliceControl->ChromaOffsetL0[i][0] << std::endl;
593         oss << "ChromaOffsetL0[" << +i << "][1]: " << +hevcSliceControl->ChromaOffsetL0[i][1] << std::endl;
594         oss << "ChromaOffsetL1[" << +i << "][0]: " << +hevcSliceControl->ChromaOffsetL1[i][0] << std::endl;
595         oss << "ChromaOffsetL1[" << +i << "][1]: " << +hevcSliceControl->ChromaOffsetL1[i][1] << std::endl;
596     }
597     // delta_chroma_weight[2][15][2]
598     for (uint8_t i = 0; i < 15; i++)
599     {
600         oss << "delta_chroma_weight_l0[" << +i << "][0]: " << +hevcSliceControl->delta_chroma_weight_l0[i][0] << std::endl;
601         oss << "delta_chroma_weight_l0[" << +i << "][1]: " << +hevcSliceControl->delta_chroma_weight_l0[i][1] << std::endl;
602         oss << "delta_chroma_weight_l1[" << +i << "][0]: " << +hevcSliceControl->delta_chroma_weight_l1[i][0] << std::endl;
603         oss << "delta_chroma_weight_l1[" << +i << "][1]: " << +hevcSliceControl->delta_chroma_weight_l1[i][1] << std::endl;
604     }
605 
606     oss << "five_minus_max_num_merge_cand: " << +hevcSliceControl->five_minus_max_num_merge_cand << std::endl;
607     oss << "num_entry_point_offsets: " << +hevcSliceControl->num_entry_point_offsets << std::endl;
608     oss << "EntryOffsetToSubsetArray: " << +hevcSliceControl->EntryOffsetToSubsetArray << std::endl;
609 
610     oss << std::endl;
611 
612     return oss;
613 }
614 
operator <<(std::ostream & oss,const CODEC_HEVC_EXT_SLICE_PARAMS & cr)615 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_EXT_SLICE_PARAMS &cr)
616 {
617     const CODEC_HEVC_EXT_SLICE_PARAMS *hevcExtSliceControl = &cr;
618 
619     // luma_offset[2][15]
620     for (uint8_t i = 0; i < 15; i++)
621     {
622         oss << "luma_offset_l0[" << +i << "]: " << +hevcExtSliceControl->luma_offset_l0[i] << std::endl;
623         oss << "luma_offset_l1[" << +i << "]: " << +hevcExtSliceControl->luma_offset_l1[i] << std::endl;
624     }
625 
626     // chroma_offset[2][15][2]
627     for (uint8_t i = 0; i < 15; i++)
628     {
629         oss << "ChromaOffsetL0[" << +i << "][0]: " << +hevcExtSliceControl->ChromaOffsetL0[i][0] << std::endl;
630         oss << "ChromaOffsetL0[" << +i << "][1]: " << +hevcExtSliceControl->ChromaOffsetL0[i][1] << std::endl;
631         oss << "ChromaOffsetL1[" << +i << "][0]: " << +hevcExtSliceControl->ChromaOffsetL1[i][0] << std::endl;
632         oss << "ChromaOffsetL1[" << +i << "][1]: " << +hevcExtSliceControl->ChromaOffsetL1[i][1] << std::endl;
633     }
634 
635     oss << "cu_chroma_qp_offset_enabled_flag: " << +hevcExtSliceControl->cu_chroma_qp_offset_enabled_flag << std::endl;
636 
637     // scc slice parameters
638     oss << "slice_act_y_qp_offset: " << +hevcExtSliceControl->slice_act_y_qp_offset << std::endl;
639     oss << "slice_act_cb_qp_offset: " << +hevcExtSliceControl->slice_act_cb_qp_offset << std::endl;
640     oss << "slice_act_cr_qp_offset: " << +hevcExtSliceControl->slice_act_cr_qp_offset << std::endl;
641     oss << "use_integer_mv_flag: " << +hevcExtSliceControl->use_integer_mv_flag << std::endl;
642 
643     oss << std::endl;
644 
645     return oss;
646 }
647 
operator <<(std::ostream & oss,const CODECHAL_HEVC_IQ_MATRIX_PARAMS & cr)648 std::ostream &operator<<(std::ostream &oss, const CODECHAL_HEVC_IQ_MATRIX_PARAMS &cr)
649 {
650     const CODECHAL_HEVC_IQ_MATRIX_PARAMS *iqParams = &cr;
651 
652     uint32_t idx;
653     uint32_t idx2;
654 
655     // 4x4 block
656     for (idx2 = 0; idx2 < 6; idx2++)
657     {
658         oss << "Qmatrix_HEVC_ucScalingLists0[" << +idx2 << "]:" << std::endl;
659 
660         oss << "ucScalingLists0[" << +idx2 << "]:";
661         for (uint8_t i = 0; i < 16; i++)
662             oss << +iqParams->ucScalingLists0[idx2][i] << " ";
663         oss << std::endl;
664     }
665 
666     // 8x8 block
667     for (idx2 = 0; idx2 < 6; idx2++)
668     {
669         oss << "ucScalingLists1[" << +idx2 << "]:" << std::endl;
670 
671         for (idx = 0; idx < 64; idx += 8)
672         {
673             oss << "ucScalingLists1[" << +idx / 8 << "]:" << std::endl;
674             for (uint8_t i = 0; i < 8; i++)
675                 oss << +iqParams->ucScalingLists1[idx2][idx + i] << " ";
676             oss << std::endl;
677         }
678     }
679 
680     // 16x16 block
681     for (idx2 = 0; idx2 < 6; idx2++)
682     {
683         oss << "ucScalingLists2[" << +idx2 << "]:" << std::endl;
684 
685         for (idx = 0; idx < 64; idx += 8)
686         {
687             oss << "ucScalingLists2[" << +idx / 8 << "]:" << std::endl;
688             for (uint8_t i = 0; i < 8; i++)
689                 oss << +iqParams->ucScalingLists2[idx2][idx + i] << " ";
690             oss << std::endl;
691         }
692     }
693 
694     // 32x32 block
695     for (idx2 = 0; idx2 < 2; idx2++)
696     {
697         oss << "ucScalingLists3[" << +idx2 << "]:" << std::endl;
698 
699         for (idx = 0; idx < 64; idx += 8)
700         {
701             oss << "ucScalingLists3[" << +idx / 8 << "]:" << std::endl;
702             for (uint8_t i = 0; i < 8; i++)
703                 oss << +iqParams->ucScalingLists3[idx2][idx + i] << " ";
704             oss << std::endl;
705         }
706     }
707 
708     // DC16x16 block
709     oss << "ucScalingListDCCoefSizeID2: ";
710     for (uint8_t i = 0; i < 6; i++)
711         oss << +iqParams->ucScalingListDCCoefSizeID2[i] << " ";
712 
713     oss << std::endl;
714 
715     // DC32x32 block
716     oss << "ucScalingListDCCoefSizeID3: ";
717     oss << +iqParams->ucScalingListDCCoefSizeID3[0] << " " << +iqParams->ucScalingListDCCoefSizeID3[1] << std::endl;
718 
719     return oss;
720 }
721 
operator <<(std::ostream & oss,const CODEC_HEVC_SUBSET_PARAMS & cr)722 std::ostream &operator<<(std::ostream &oss, const CODEC_HEVC_SUBSET_PARAMS &cr)
723 {
724     const CODEC_HEVC_SUBSET_PARAMS *subsetsParams = &cr;
725 
726     for (uint16_t i = 0; i < 440; i++)
727     {
728         oss << "entry_point_offset_minus1[" << +i << "]: " << +subsetsParams->entry_point_offset_minus1[i] << std::endl;
729     }
730 
731     return oss;
732 }
733 
DumpDecodeHevcPicParams(PCODEC_HEVC_PIC_PARAMS picParams,std::string fileName)734 void DumpDecodeHevcPicParams(PCODEC_HEVC_PIC_PARAMS picParams, std::string fileName)
735 {
736     std::ostringstream oss;
737     oss.setf(std::ios::showbase | std::ios::uppercase);
738 
739     oss << *picParams << std::endl;
740 
741     std::ofstream ofs(fileName, std::ios::out);
742     ofs << oss.str();
743     ofs.close();
744 }
745 
DumpDecodeHevcExtPicParams(PCODEC_HEVC_EXT_PIC_PARAMS extPicParams,std::string fileName)746 void DumpDecodeHevcExtPicParams(PCODEC_HEVC_EXT_PIC_PARAMS extPicParams, std::string fileName)
747 {
748     std::ostringstream oss;
749     oss.setf(std::ios::showbase | std::ios::uppercase);
750 
751     oss << *extPicParams << std::endl;
752 
753     std::ofstream ofs(fileName, std::ios::out);
754     ofs << oss.str();
755     ofs.close();
756 }
757 
DumpDecodeHevcSccPicParams(PCODEC_HEVC_SCC_PIC_PARAMS sccPicParams,std::string fileName)758 void DumpDecodeHevcSccPicParams(PCODEC_HEVC_SCC_PIC_PARAMS sccPicParams, std::string fileName)
759 {
760     std::ostringstream oss;
761     oss.setf(std::ios::showbase | std::ios::uppercase);
762 
763     oss << *sccPicParams << std::endl;
764 
765     std::ofstream ofs(fileName, std::ios::out);
766     ofs << oss.str();
767     ofs.close();
768 }
769 
DumpDecodeHevcSliceParams(PCODEC_HEVC_SLICE_PARAMS sliceParams,uint32_t numSlices,std::string fileName,bool shortFormatInUse)770 void DumpDecodeHevcSliceParams(PCODEC_HEVC_SLICE_PARAMS sliceParams, uint32_t numSlices, std::string fileName, bool shortFormatInUse)
771 {
772     std::ostringstream oss;
773     oss.setf(std::ios::showbase | std::ios::uppercase);
774 
775     if (numSlices > 1)
776     {
777         oss << "Total Num: " << numSlices << std::endl;
778         oss << std::endl;
779     }
780 
781     for (uint16_t i = 0; i < numSlices; i++)
782     {
783         if (numSlices > 1)
784         {
785             oss << "---------Index = " << i << "---------" << std::endl;
786         }
787 
788         if (shortFormatInUse)
789         {
790             oss << reinterpret_cast<CODEC_HEVC_SF_SLICE_PARAMS &>(sliceParams[i]) << std::endl;
791         }
792         else
793         {
794             oss << sliceParams[i] << std::endl;
795         }
796     }
797 
798     std::ofstream ofs(fileName, std::ios::out);
799     ofs << oss.str();
800     ofs.close();
801 }
802 
DumpDecodeHevcExtSliceParams(PCODEC_HEVC_EXT_SLICE_PARAMS extSliceParams,uint32_t numSlices,std::string fileName)803 void DumpDecodeHevcExtSliceParams(PCODEC_HEVC_EXT_SLICE_PARAMS extSliceParams, uint32_t numSlices, std::string fileName)
804 {
805     std::ostringstream oss;
806     oss.setf(std::ios::showbase | std::ios::uppercase);
807 
808     if (numSlices > 1)
809     {
810         oss << "Total Num: " << numSlices << std::endl;
811         oss << std::endl;
812     }
813 
814     for (uint16_t i = 0; i < numSlices; i++)
815     {
816         if (numSlices > 1)
817         {
818             oss << "---------Index = " << i << "---------" << std::endl;
819         }
820 
821         oss << extSliceParams[i] << std::endl;
822     }
823 
824     std::ofstream ofs(fileName, std::ios::out);
825     ofs << oss.str();
826     ofs.close();
827 }
828 
DumpDecodeHevcIQParams(PCODECHAL_HEVC_IQ_MATRIX_PARAMS iqParams,std::string fileName)829 void DumpDecodeHevcIQParams(PCODECHAL_HEVC_IQ_MATRIX_PARAMS iqParams, std::string fileName)
830 {
831     std::ostringstream oss;
832     oss.setf(std::ios::showbase | std::ios::uppercase);
833 
834     oss << *iqParams << std::endl;
835 
836     std::ofstream ofs(fileName, std::ios::out);
837     ofs << oss.str();
838     ofs.close();
839 }
840 
DumpDecodeHevcSubsetParams(PCODEC_HEVC_SUBSET_PARAMS subsetsParams,std::string fileName)841 void DumpDecodeHevcSubsetParams(PCODEC_HEVC_SUBSET_PARAMS subsetsParams, std::string fileName)
842 {
843     std::ostringstream oss;
844     oss.setf(std::ios::showbase | std::ios::uppercase);
845 
846     oss << *subsetsParams << std::endl;
847 
848     std::ofstream ofs(fileName, std::ios::out);
849     ofs << oss.str();
850     ofs.close();
851 }
852 
853 // VP9 Decode
operator <<(std::ostream & oss,const CODEC_VP9_PIC_PARAMS & cr)854 std::ostream &operator<<(std::ostream &oss, const CODEC_VP9_PIC_PARAMS &cr)
855 {
856     const CODEC_VP9_PIC_PARAMS *picParams = &cr;
857 
858     oss << "CurrPic FrameIdx: " << +picParams->CurrPic.FrameIdx << std::endl;
859     oss << "CurrPic PicFlags: " << +picParams->CurrPic.PicFlags << std::endl;
860 
861     for (uint8_t i = 0; i < 8; ++i)
862     {
863         oss << "RefFrameList[" << +i << "] FrameIdx:" << +picParams->RefFrameList[i].FrameIdx << std::endl;
864         oss << "RefFrameList[" << +i << "] PicFlags:" << +picParams->RefFrameList[i].PicFlags << std::endl;
865     }
866     oss << "FrameWidthMinus1: " << +picParams->FrameWidthMinus1 << std::endl;
867     oss << "FrameHeightMinus1: " << +picParams->FrameHeightMinus1 << std::endl;
868     oss << "PicFlags value: " << +picParams->PicFlags.value << std::endl;
869     oss << "frame_type: " << +picParams->PicFlags.fields.frame_type << std::endl;
870     oss << "show_frame: " << +picParams->PicFlags.fields.show_frame << std::endl;
871     oss << "error_resilient_mode: " << +picParams->PicFlags.fields.error_resilient_mode << std::endl;
872     oss << "intra_only: " << +picParams->PicFlags.fields.intra_only << std::endl;
873     oss << "LastRefIdx: " << +picParams->PicFlags.fields.LastRefIdx << std::endl;
874     oss << "LastRefSignBias: " << +picParams->PicFlags.fields.LastRefSignBias << std::endl;
875     oss << "GoldenRefIdx: " << +picParams->PicFlags.fields.GoldenRefIdx << std::endl;
876     oss << "GoldenRefSignBias: " << +picParams->PicFlags.fields.GoldenRefSignBias << std::endl;
877     oss << "AltRefIdx: " << +picParams->PicFlags.fields.AltRefIdx << std::endl;
878     oss << "AltRefSignBias: " << +picParams->PicFlags.fields.AltRefSignBias << std::endl;
879     oss << "allow_high_precision_mv: " << +picParams->PicFlags.fields.allow_high_precision_mv << std::endl;
880     oss << "mcomp_filter_type: " << +picParams->PicFlags.fields.mcomp_filter_type << std::endl;
881     oss << "frame_parallel_decoding_mode: " << +picParams->PicFlags.fields.frame_parallel_decoding_mode << std::endl;
882     oss << "segmentation_enabled: " << +picParams->PicFlags.fields.segmentation_enabled << std::endl;
883     oss << "segmentation_temporal_update: " << +picParams->PicFlags.fields.segmentation_temporal_update << std::endl;
884     oss << "segmentation_update_map: " << +picParams->PicFlags.fields.segmentation_update_map << std::endl;
885     oss << "reset_frame_context: " << +picParams->PicFlags.fields.reset_frame_context << std::endl;
886     oss << "refresh_frame_context: " << +picParams->PicFlags.fields.refresh_frame_context << std::endl;
887     oss << "frame_context_idx: " << +picParams->PicFlags.fields.frame_context_idx << std::endl;
888     oss << "LosslessFlag: " << +picParams->PicFlags.fields.LosslessFlag << std::endl;
889     oss << "ReservedField: " << +picParams->PicFlags.fields.ReservedField << std::endl;
890     oss << "filter_level: " << +picParams->filter_level << std::endl;
891     oss << "sharpness_level: " << +picParams->sharpness_level << std::endl;
892     oss << "log2_tile_rows: " << +picParams->log2_tile_rows << std::endl;
893     oss << "log2_tile_columns: " << +picParams->log2_tile_columns << std::endl;
894     oss << "UncompressedHeaderLengthInBytes: " << +picParams->UncompressedHeaderLengthInBytes << std::endl;
895     oss << "FirstPartitionSize: " << +picParams->FirstPartitionSize << std::endl;
896     oss << "profile: " << +picParams->profile << std::endl;
897     oss << "BitDepthMinus8: " << +picParams->BitDepthMinus8 << std::endl;
898     oss << "subsampling_x: " << +picParams->subsampling_x << std::endl;
899     oss << "subsampling_y: " << +picParams->subsampling_y << std::endl;
900 
901     for (uint8_t i = 0; i < 7; ++i)
902     {
903         oss << "SegTreeProbs[" << +i << "]: " << +picParams->SegTreeProbs[i] << std::endl;
904     }
905     for (uint8_t i = 0; i < 3; ++i)
906     {
907         oss << "SegPredProbs[" << +i << "]: " << +picParams->SegPredProbs[i] << std::endl;
908     }
909     oss << "BSBytesInBuffer: " << +picParams->BSBytesInBuffer << std::endl;
910     oss << "StatusReportFeedbackNumber: " << +picParams->StatusReportFeedbackNumber << std::endl;
911 
912     return oss;
913 }
914 
operator <<(std::ostream & oss,const CODEC_VP9_SLICE_PARAMS & cr)915 std::ostream &operator<<(std::ostream &oss, const CODEC_VP9_SLICE_PARAMS &cr)
916 {
917     const CODEC_VP9_SLICE_PARAMS *slcParams = &cr;
918 
919     oss << "BSNALunitDataLocation: " << +slcParams->BSNALunitDataLocation << std::endl;
920     oss << "SliceBytesInBuffer: " << +slcParams->SliceBytesInBuffer << std::endl;
921     oss << "wBadSliceChopping: " << +slcParams->wBadSliceChopping << std::endl;
922     oss << std::endl;
923 
924     return oss;
925 }
926 
operator <<(std::ostream & oss,const CODEC_VP9_SEGMENT_PARAMS & cr)927 std::ostream &operator<<(std::ostream &oss, const CODEC_VP9_SEGMENT_PARAMS &cr)
928 {
929     const CODEC_VP9_SEGMENT_PARAMS *segmentParams = &cr;
930 
931     for (uint8_t i = 0; i < 8; ++i)
932     {
933         oss << "SegData[" << +i << "] SegmentFlags value: " << +segmentParams->SegData[i].SegmentFlags.value << std::endl;
934         oss << "SegData[" << +i << "] SegmentReferenceEnabled: " << +segmentParams->SegData[i].SegmentFlags.fields.SegmentReferenceEnabled << std::endl;
935         oss << "SegData[" << +i << "] SegmentReference: " << +segmentParams->SegData[i].SegmentFlags.fields.SegmentReference << std::endl;
936         oss << "SegData[" << +i << "] SegmentReferenceSkipped: " << +segmentParams->SegData[i].SegmentFlags.fields.SegmentReferenceSkipped << std::endl;
937         oss << "SegData[" << +i << "] ReservedField3: " << +segmentParams->SegData[i].SegmentFlags.fields.ReservedField3 << std::endl;
938 
939         for (uint8_t j = 0; j < 4; ++j)
940         {
941             oss << "SegData[" << +i << "] FilterLevel[" << +j << "]:";
942             oss << +segmentParams->SegData[i].FilterLevel[j][0] << " ";
943             oss << +segmentParams->SegData[i].FilterLevel[j][1] << std::endl;
944         }
945 
946         oss << "SegData[" << +i << "] LumaACQuantScale: " << +segmentParams->SegData[i].LumaACQuantScale << std::endl;
947         oss << "SegData[" << +i << "] LumaDCQuantScale: " << +segmentParams->SegData[i].LumaDCQuantScale << std::endl;
948         oss << "SegData[" << +i << "] ChromaACQuantScale: " << +segmentParams->SegData[i].ChromaACQuantScale << std::endl;
949         oss << "SegData[" << +i << "] ChromaDCQuantScale: " << +segmentParams->SegData[i].ChromaDCQuantScale << std::endl;
950     }
951 
952     return oss;
953 }
954 
DumpDecodeVp9PicParams(PCODEC_VP9_PIC_PARAMS picParams,std::string fileName)955 void DumpDecodeVp9PicParams(PCODEC_VP9_PIC_PARAMS picParams, std::string fileName)
956 {
957     std::ostringstream oss;
958     oss.setf(std::ios::showbase | std::ios::uppercase);
959 
960     oss << *picParams << std::endl;
961 
962     std::ofstream ofs(fileName, std::ios::out);
963     ofs << oss.str();
964     ofs.close();
965 }
966 
DumpDecodeVp9SliceParams(PCODEC_VP9_SLICE_PARAMS slcParams,std::string fileName)967 void DumpDecodeVp9SliceParams(PCODEC_VP9_SLICE_PARAMS slcParams, std::string fileName)
968 {
969     std::ostringstream oss;
970     oss.setf(std::ios::showbase | std::ios::uppercase);
971 
972     oss << *slcParams << std::endl;
973 
974     std::ofstream ofs(fileName, std::ios::out);
975     ofs << oss.str();
976     ofs.close();
977 }
978 
DumpDecodeVp9SegmentParams(PCODEC_VP9_SEGMENT_PARAMS segmentParams,std::string fileName)979 void DumpDecodeVp9SegmentParams(PCODEC_VP9_SEGMENT_PARAMS segmentParams, std::string fileName)
980 {
981     std::ostringstream oss;
982     oss.setf(std::ios::showbase | std::ios::uppercase);
983 
984     oss << *segmentParams << std::endl;
985 
986     std::ofstream ofs(fileName, std::ios::out);
987     ofs << oss.str();
988     ofs.close();
989 }
990 
991 // AV1 Decode
operator <<(std::ostream & oss,const CodecAv1PicParams & cr)992 std::ostream &operator<<(std::ostream &oss, const CodecAv1PicParams &cr)
993 {
994     const CodecAv1PicParams *picParams = &cr;
995 
996     oss << "CurrPic FrameIdx: " << +picParams->m_currPic.FrameIdx << std::endl;
997     oss << "CurrDisplayPic FrameIdx: " << +picParams->m_currDisplayPic.FrameIdx << std::endl;
998     oss << "Profile: " << +picParams->m_profile << std::endl;
999     oss << "AnchorFrameInsertion: " << +picParams->m_anchorFrameInsertion << std::endl;
1000     oss << "order_hint_bits_minus_1: " << +picParams->m_orderHintBitsMinus1 << std::endl;
1001     oss << "BitDepthIdx: " << +picParams->m_bitDepthIdx << std::endl;
1002 
1003     //Sequence Info Flags
1004     oss << "dwSeqInfoFlags: " << +picParams->m_seqInfoFlags.m_value << std::endl;
1005     oss << "still_picture: " << +picParams->m_seqInfoFlags.m_fields.m_stillPicture << std::endl;
1006     oss << "use_128x128_superblock: " << +picParams->m_seqInfoFlags.m_fields.m_use128x128Superblock << std::endl;
1007     oss << "enable_filter_intra: " << +picParams->m_seqInfoFlags.m_fields.m_enableFilterIntra << std::endl;
1008     oss << "enable_intra_edge_filter: " << +picParams->m_seqInfoFlags.m_fields.m_enableIntraEdgeFilter << std::endl;
1009     oss << "enable_interintra_compound: " << +picParams->m_seqInfoFlags.m_fields.m_enableInterintraCompound << std::endl;
1010     oss << "enable_masked_compound: " << +picParams->m_seqInfoFlags.m_fields.m_enableMaskedCompound << std::endl;
1011     oss << "enable_dual_filter: " << +picParams->m_seqInfoFlags.m_fields.m_enableDualFilter << std::endl;
1012     oss << "enable_order_hint: " << +picParams->m_seqInfoFlags.m_fields.m_enableOrderHint << std::endl;
1013     oss << "enable_jnt_comp: " << +picParams->m_seqInfoFlags.m_fields.m_enableJntComp << std::endl;
1014     oss << "enable_cdef: " << +picParams->m_seqInfoFlags.m_fields.m_enableCdef << std::endl;
1015     oss << "mono_chrome: " << +picParams->m_seqInfoFlags.m_fields.m_monoChrome << std::endl;
1016     oss << "color_range: " << +picParams->m_seqInfoFlags.m_fields.m_colorRange << std::endl;
1017     oss << "subsampling_x: " << +picParams->m_seqInfoFlags.m_fields.m_subsamplingX << std::endl;
1018     oss << "subsampling_y: " << +picParams->m_seqInfoFlags.m_fields.m_subsamplingY << std::endl;
1019     oss << "chroma_sample_position: " << +picParams->m_seqInfoFlags.m_fields.m_chromaSamplePosition << std::endl;
1020     oss << "film_grain_params_present: " << +picParams->m_seqInfoFlags.m_fields.m_filmGrainParamsPresent << std::endl;
1021 
1022     //frame info
1023     oss << "dwPicInfoFlags: " << +picParams->m_picInfoFlags.m_value << std::endl;
1024     oss << "frame_type: " << +picParams->m_picInfoFlags.m_fields.m_frameType << std::endl;
1025     oss << "show_frame: " << +picParams->m_picInfoFlags.m_fields.m_showFrame << std::endl;
1026     oss << "showable_frame: " << +picParams->m_picInfoFlags.m_fields.m_showableFrame << std::endl;
1027     oss << "error_resilient_mode: " << +picParams->m_picInfoFlags.m_fields.m_errorResilientMode << std::endl;
1028     oss << "disable_cdf_update: " << +picParams->m_picInfoFlags.m_fields.m_disableCdfUpdate << std::endl;
1029     oss << "allow_screen_content_tools: " << +picParams->m_picInfoFlags.m_fields.m_allowScreenContentTools << std::endl;
1030     oss << "force_integer_mv: " << +picParams->m_picInfoFlags.m_fields.m_forceIntegerMv << std::endl;
1031     oss << "allow_intrabc: " << +picParams->m_picInfoFlags.m_fields.m_allowIntrabc << std::endl;
1032     oss << "use_superres: " << +picParams->m_picInfoFlags.m_fields.m_useSuperres << std::endl;
1033     oss << "allow_high_precision_mv: " << +picParams->m_picInfoFlags.m_fields.m_allowHighPrecisionMv << std::endl;
1034     oss << "is_motion_mode_switchable: " << +picParams->m_picInfoFlags.m_fields.m_isMotionModeSwitchable << std::endl;
1035     oss << "use_ref_frame_mvs: " << +picParams->m_picInfoFlags.m_fields.m_useRefFrameMvs << std::endl;
1036     oss << "disable_frame_end_update_cdf: " << +picParams->m_picInfoFlags.m_fields.m_disableFrameEndUpdateCdf << std::endl;
1037     oss << "uniform_tile_spacing_flag: " << +picParams->m_picInfoFlags.m_fields.m_uniformTileSpacingFlag << std::endl;
1038     oss << "allow_warped_motion: " << +picParams->m_picInfoFlags.m_fields.m_allowWarpedMotion << std::endl;
1039     oss << "large_scale_tile: " << +picParams->m_picInfoFlags.m_fields.m_largeScaleTile << std::endl;
1040 
1041     oss << "frame_width_minus1: " << +picParams->m_frameWidthMinus1 << std::endl;
1042     oss << "frame_height_minus1: " << +picParams->m_frameHeightMinus1 << std::endl;
1043 
1044     for (auto i = 0; i < 8; ++i)
1045     {
1046         oss << "ref_frame_map[" << +i << "] FrameIdx:" << +picParams->m_refFrameMap[i].FrameIdx << std::endl;
1047         oss << "ref_frame_map[" << +i << "] PicFlags:" << +picParams->m_refFrameMap[i].PicFlags << std::endl;
1048     }
1049 
1050     for (auto i = 0; i < 7; i++)
1051     {
1052         oss << "ref_frame_idx[" << +i << "]: " << +picParams->m_refFrameIdx[i] << std::endl;
1053     }
1054 
1055     oss << "primary_ref_frame: " << +picParams->m_primaryRefFrame << std::endl;
1056     oss << "output_frame_width_in_tiles_minus_1: " << +picParams->m_outputFrameWidthInTilesMinus1 << std::endl;
1057     oss << "output_frame_height_in_tiles_minus_1: " << +picParams->m_outputFrameHeightInTilesMinus1 << std::endl;
1058 
1059     for (auto i = 0; i < 2; i++)
1060     {
1061         oss << "filter_level[" << +i << "]: " << +picParams->m_filterLevel[i] << std::endl;
1062     }
1063     oss << "filter_level_u: " << +picParams->m_filterLevelU << std::endl;
1064     oss << "filter_level_v: " << +picParams->m_filterLevelV << std::endl;
1065 
1066     //Loop Filter Info Flags
1067     oss << "cLoopFilterInfoFlags value: " << +picParams->m_loopFilterInfoFlags.m_value << std::endl;
1068     oss << "sharpness_level: " << +picParams->m_loopFilterInfoFlags.m_fields.m_sharpnessLevel << std::endl;
1069     oss << "mode_ref_delta_enabled: " << +picParams->m_loopFilterInfoFlags.m_fields.m_modeRefDeltaEnabled << std::endl;
1070     oss << "mode_ref_delta_update: " << +picParams->m_loopFilterInfoFlags.m_fields.m_modeRefDeltaUpdate << std::endl;
1071 
1072     oss << "order_hint: " << +picParams->m_orderHint << std::endl;
1073     oss << "superres_scale_denominator: " << +picParams->m_superresScaleDenominator << std::endl;
1074     oss << "interp_filter: " << +picParams->m_interpFilter << std::endl;
1075 
1076     for (auto i = 0; i < 8; i++)
1077     {
1078         oss << "ref_deltas[" << +i << "]: " << +picParams->m_refDeltas[i] << std::endl;
1079     }
1080 
1081     for (auto i = 0; i < 2; i++)
1082     {
1083         oss << "mode_deltas[" << +i << "]: " << +picParams->m_modeDeltas[i] << std::endl;
1084     }
1085 
1086     oss << "base_qindex: " << +picParams->m_baseQindex << std::endl;
1087     oss << "y_dc_delta_q: " << +picParams->m_yDcDeltaQ << std::endl;
1088     oss << "u_dc_delta_q: " << +picParams->m_uDcDeltaQ << std::endl;
1089     oss << "u_ac_delta_q: " << +picParams->m_uAcDeltaQ << std::endl;
1090     oss << "v_dc_delta_q: " << +picParams->m_vDcDeltaQ << std::endl;
1091     oss << "v_ac_delta_q: " << +picParams->m_vAcDeltaQ << std::endl;
1092 
1093     // quantization_matrix
1094     oss << "wQMatrixFlags value: " << +picParams->m_qMatrixFlags.m_value << std::endl;
1095     oss << "using_qmatrix: " << +picParams->m_qMatrixFlags.m_fields.m_usingQmatrix << std::endl;
1096     oss << "qm_y: " << +picParams->m_qMatrixFlags.m_fields.m_qmY << std::endl;
1097     oss << "qm_u: " << +picParams->m_qMatrixFlags.m_fields.m_qmU << std::endl;
1098     oss << "qm_v: " << +picParams->m_qMatrixFlags.m_fields.m_qmV << std::endl;
1099 
1100     // Mode control flags
1101     oss << "dwModeControlFlags value: " << +picParams->m_modeControlFlags.m_value << std::endl;
1102     oss << "delta_q_present_flag: " << +picParams->m_modeControlFlags.m_fields.m_deltaQPresentFlag << std::endl;
1103     oss << "log2_delta_q_res: " << +picParams->m_modeControlFlags.m_fields.m_log2DeltaQRes << std::endl;
1104     oss << "delta_lf_present_flag: " << +picParams->m_modeControlFlags.m_fields.m_deltaLfPresentFlag << std::endl;
1105     oss << "log2_delta_lf_res: " << +picParams->m_modeControlFlags.m_fields.m_log2DeltaLfRes << std::endl;
1106     oss << "delta_lf_multi: " << +picParams->m_modeControlFlags.m_fields.m_deltaLfMulti << std::endl;
1107     oss << "tx_mode: " << +picParams->m_modeControlFlags.m_fields.m_txMode << std::endl;
1108     oss << "reference_mode: " << +picParams->m_modeControlFlags.m_fields.m_referenceMode << std::endl;
1109     oss << "reduced_tx_set_used: " << +picParams->m_modeControlFlags.m_fields.m_reducedTxSetUsed << std::endl;
1110     oss << "skip_mode_present: " << +picParams->m_modeControlFlags.m_fields.m_skipModePresent << std::endl;
1111 
1112     // Segmentation
1113     oss << "enabled: " << +picParams->m_av1SegData.m_enabled << std::endl;
1114     oss << "update_map: " << +picParams->m_av1SegData.m_updateMap << std::endl;
1115     oss << "temporal_update: " << +picParams->m_av1SegData.m_temporalUpdate << std::endl;
1116     oss << "update_data: " << +picParams->m_av1SegData.m_updateData << std::endl;
1117 
1118     for (auto i = 0; i < 8; i++)
1119     {
1120         for (auto j = 0; j < 8; j++)
1121         {
1122             oss << "feature_data[" << +i << "][" << +j << "]: " << +picParams->m_av1SegData.m_featureData[i][j] << std::endl;
1123         }
1124     }
1125     for (auto i = 0; i < 8; i++)
1126     {
1127         oss << "feature_mask[" << +i << "]: " << +picParams->m_av1SegData.m_featureMask[i] << std::endl;
1128     }
1129 
1130     oss << "tile_cols: " << +picParams->m_tileCols << std::endl;
1131     for (auto i = 0; i < 63; i++)
1132     {
1133         oss << "width_in_sbs_minus_1[" << +i << "]: " << +picParams->m_widthInSbsMinus1[i] << std::endl;
1134     }
1135     oss << "tile_rows: " << +picParams->m_tileRows << std::endl;
1136     for (auto i = 0; i < 63; i++)
1137     {
1138         oss << "height_in_sbs_minus_1[" << +i << "]: " << +picParams->m_heightInSbsMinus1[i] << std::endl;
1139     }
1140 
1141     oss << "tile_count_minus_1: " << +picParams->m_tileCountMinus1 << std::endl;
1142     oss << "context_update_tile_id: " << +picParams->m_contextUpdateTileId << std::endl;
1143 
1144     oss << "cdef_damping_minus_3: " << +picParams->m_cdefDampingMinus3 << std::endl;
1145     oss << "cdef_bits: " << +picParams->m_cdefBits << std::endl;
1146     for (auto i = 0; i < 8; i++)
1147     {
1148         oss << "cdef_y_strengths[" << +i << "]: " << +picParams->m_cdefYStrengths[i] << std::endl;
1149     }
1150     for (auto i = 0; i < 8; i++)
1151     {
1152         oss << "cdef_uv_strengths[" << +i << "]: " << +picParams->m_cdefUvStrengths[i] << std::endl;
1153     }
1154 
1155     // Loop Restoration Flags
1156     oss << "LoopRestorationFlags value: " << +picParams->m_loopRestorationFlags.m_value << std::endl;
1157     oss << "yframe_restoration_type: " << +picParams->m_loopRestorationFlags.m_fields.m_yframeRestorationType << std::endl;
1158     oss << "cbframe_restoration_type: " << +picParams->m_loopRestorationFlags.m_fields.m_cbframeRestorationType << std::endl;
1159     oss << "crframe_restoration_type: " << +picParams->m_loopRestorationFlags.m_fields.m_crframeRestorationType << std::endl;
1160     oss << "lr_unit_shift: " << +picParams->m_loopRestorationFlags.m_fields.m_lrUnitShift << std::endl;
1161     oss << "lr_uv_shift: " << +picParams->m_loopRestorationFlags.m_fields.m_lrUvShift << std::endl;
1162 
1163     for (auto i = 0; i < 7; i++)
1164     {
1165         oss << "wm[" << +i << "].wmtype: " << +picParams->m_wm[i].m_wmtype << std::endl;
1166         for (auto j = 0; j < 8; j++)
1167         {
1168             oss << "wm[" << +i << "].wmmat[" << +j << "]: " << +picParams->m_wm[i].m_wmmat[j] << std::endl;
1169         }
1170     }
1171 
1172     //Film Grain params
1173     oss << "apply_grain: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_applyGrain << std::endl;
1174     oss << "chroma_scaling_from_luma: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_chromaScalingFromLuma << std::endl;
1175     oss << "grain_scaling_minus_8: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_grainScalingMinus8 << std::endl;
1176     oss << "ar_coeff_lag: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_arCoeffLag << std::endl;
1177     oss << "ar_coeff_shift_minus_6: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_arCoeffShiftMinus6 << std::endl;
1178     oss << "grain_scale_shift: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_grainScaleShift << std::endl;
1179     oss << "overlap_flag: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_overlapFlag << std::endl;
1180     oss << "clip_to_restricted_range: " << +picParams->m_filmGrainParams.m_filmGrainInfoFlags.m_fields.m_clipToRestrictedRange << std::endl;
1181 
1182     oss << "random_seed: " << +picParams->m_filmGrainParams.m_randomSeed << std::endl;
1183     oss << "num_y_points: " << +picParams->m_filmGrainParams.m_numYPoints << std::endl;
1184     for (auto i = 0; i < 14; i++)
1185     {
1186         oss << "point_y_value[" << +i << "]: " << +picParams->m_filmGrainParams.m_pointYValue[i] << std::endl;
1187     }
1188     for (auto i = 0; i < 14; i++)
1189     {
1190         oss << "point_y_scaling[" << +i << "]: " << +picParams->m_filmGrainParams.m_pointYScaling[i] << std::endl;
1191     }
1192 
1193     oss << "num_cb_points: " << +picParams->m_filmGrainParams.m_numCbPoints << std::endl;
1194     for (auto i = 0; i < 10; i++)
1195     {
1196         oss << "point_cb_value[" << +i << "]: " << +picParams->m_filmGrainParams.m_pointCbValue[i] << std::endl;
1197     }
1198     for (auto i = 0; i < 10; i++)
1199     {
1200         oss << "point_cb_scaling[" << +i << "]: " << +picParams->m_filmGrainParams.m_pointCbScaling[i] << std::endl;
1201     }
1202 
1203     oss << "num_cr_points: " << +picParams->m_filmGrainParams.m_numCrPoints << std::endl;
1204     for (auto i = 0; i < 10; i++)
1205     {
1206         oss << "point_cr_value[" << +i << "]: " << +picParams->m_filmGrainParams.m_pointCrValue[i] << std::endl;
1207     }
1208     for (auto i = 0; i < 10; i++)
1209     {
1210         oss << "point_cr_scaling[" << +i << "]: " << +picParams->m_filmGrainParams.m_pointCrScaling[i] << std::endl;
1211     }
1212 
1213     for (auto i = 0; i < 24; i++)
1214     {
1215         oss << "ar_coeffs_y[" << +i << "]: " << +picParams->m_filmGrainParams.m_arCoeffsY[i] << std::endl;
1216     }
1217     for (auto i = 0; i < 25; i++)
1218     {
1219         oss << "ar_coeffs_cb[" << +i << "]: " << +picParams->m_filmGrainParams.m_arCoeffsCb[i] << std::endl;
1220     }
1221     for (auto i = 0; i < 25; i++)
1222     {
1223         oss << "ar_coeffs_cr[" << +i << "]: " << +picParams->m_filmGrainParams.m_arCoeffsCr[i] << std::endl;
1224     }
1225 
1226     oss << "cb_mult: " << +picParams->m_filmGrainParams.m_cbMult << std::endl;
1227     oss << "cb_luma_mult: " << +picParams->m_filmGrainParams.m_cbLumaMult << std::endl;
1228     oss << "cb_offset: " << +picParams->m_filmGrainParams.m_cbOffset << std::endl;
1229     oss << "cr_mult: " << +picParams->m_filmGrainParams.m_crMult << std::endl;
1230     oss << "cr_luma_mult: " << +picParams->m_filmGrainParams.m_crLumaMult << std::endl;
1231     oss << "cr_offset: " << +picParams->m_filmGrainParams.m_crOffset << std::endl;
1232     oss << "StatusReportFeedbackNumber: " << +picParams->m_statusReportFeedbackNumber << std::endl;
1233 
1234     //Driver internal
1235     oss << "losslessMode: " << +picParams->m_losslessMode << std::endl;
1236     oss << "superResUpscaledWidthMinus1: " << +picParams->m_superResUpscaledWidthMinus1 << std::endl;
1237     oss << "superResUpscaledHeightMinus1: " << +picParams->m_superResUpscaledHeightMinus1 << std::endl;
1238     for (auto i = 0; i < 8; i++)
1239     {
1240         oss << "activeRefBitMaskMfmv[" << +i << "]: " << +picParams->m_activeRefBitMaskMfmv[i] << std::endl;
1241     }
1242 
1243     return oss;
1244 }
1245 
operator <<(std::ostream & oss,const CodecAv1TileParams & cr)1246 std::ostream &operator<<(std::ostream &oss, const CodecAv1TileParams &cr)
1247 {
1248     const CodecAv1TileParams *tileParams = &cr;
1249 
1250     oss << "BSTileDataLocation" << +tileParams->m_bsTileDataLocation << std::endl;
1251     oss << "BSTileBytesInBuffer" << +tileParams->m_bsTileBytesInBuffer << std::endl;
1252     oss << "wBadBSBufferChopping" << +tileParams->m_badBSBufferChopping << std::endl;
1253     oss << "tile_row" << +tileParams->m_tileRow << std::endl;
1254     oss << "tile_column" << +tileParams->m_tileColumn << std::endl;
1255     oss << "anchor_frame_idx" << +tileParams->m_anchorFrameIdx.FrameIdx << std::endl;
1256     oss << "BSTilePayloadSizeInBytes" << +tileParams->m_bsTilePayloadSizeInBytes << std::endl;
1257 
1258     oss << "--------- Intel Profile Specific ---------" << std::endl;
1259     oss << "tile_index" << +tileParams->m_tileIndex << std::endl;
1260     oss << "StartTileIdx" << +tileParams->m_startTileIdx << std::endl;
1261     oss << "EndTileIdx" << +tileParams->m_endTileIdx << std::endl;
1262 
1263     return oss;
1264 }
1265 
DumpDecodeAv1PicParams(CodecAv1PicParams * picParams,std::string fileName)1266 void DumpDecodeAv1PicParams(CodecAv1PicParams *picParams, std::string fileName)
1267 {
1268     std::ostringstream oss;
1269     oss.setf(std::ios::showbase | std::ios::uppercase);
1270 
1271     oss << *picParams << std::endl;
1272 
1273     std::ofstream ofs(fileName, std::ios::out);
1274     ofs << oss.str();
1275     ofs.close();
1276 }
1277 
DumpDecodeAv1TileParams(CodecAv1TileParams * tileParams,uint32_t tileNum,std::string fileName)1278 void DumpDecodeAv1TileParams(CodecAv1TileParams *tileParams, uint32_t tileNum, std::string fileName)
1279 {
1280     std::ostringstream oss;
1281     oss.setf(std::ios::showbase | std::ios::uppercase);
1282 
1283     if (tileNum > 1)
1284     {
1285         oss << "Total Num: " << tileNum << std::endl;
1286         oss << std::endl;
1287     }
1288 
1289     for (uint16_t i = 0; i < tileNum; i++)
1290     {
1291         if (tileNum > 1)
1292         {
1293             oss << "---------Index = " << i << "---------" << std::endl;
1294         }
1295 
1296         oss << tileParams[i] << std::endl;
1297     }
1298 
1299     std::ofstream ofs(fileName, std::ios::out);
1300     ofs << oss.str();
1301     ofs.close();
1302 }
1303 
1304 // JPEG Decode
operator <<(std::ostream & oss,const CodecDecodeJpegPicParams & cr)1305 std::ostream &operator<<(std::ostream &oss, const CodecDecodeJpegPicParams &cr)
1306 {
1307     const CodecDecodeJpegPicParams *picParams = &cr;
1308 
1309     oss << "destPic.FrameIdx: " << +picParams->m_destPic.FrameIdx << std::endl;
1310     oss << "destPic.PicFlags: " << +picParams->m_destPic.PicFlags << std::endl;
1311     oss << "frameWidth: " << +picParams->m_frameWidth << std::endl;
1312     oss << "frameHeight: " << +picParams->m_frameHeight << std::endl;
1313     oss << "numCompInFrame: " << +picParams->m_numCompInFrame << std::endl;
1314 
1315     // componentIdentifier[jpegNumComponent]
1316     for (uint32_t i = 0; i < jpegNumComponent; ++i)
1317     {
1318         oss << "componentIdentifier[" << +i << "]: " << +picParams->m_componentIdentifier[i] << std::endl;
1319     }
1320 
1321     // quantTableSelector[jpegNumComponent]
1322     for (uint32_t i = 0; i < jpegNumComponent; ++i)
1323     {
1324         oss << "quantTableSelector[" << +i << "]: " << +picParams->m_quantTableSelector[i] << std::endl;
1325     }
1326 
1327     oss << "chromaType: " << +picParams->m_chromaType << std::endl;
1328     oss << "rotation: " << +picParams->m_rotation << std::endl;
1329     oss << "totalScans: " << +picParams->m_totalScans << std::endl;
1330     oss << "interleavedData: " << +picParams->m_interleavedData << std::endl;
1331     oss << "reserved: " << +picParams->m_reserved << std::endl;
1332     oss << "statusReportFeedbackNumber: " << +picParams->m_statusReportFeedbackNumber << std::endl;
1333 
1334     return oss;
1335 }
1336 
operator <<(std::ostream & oss,const CodecDecodeJpegScanParameter & cr)1337 std::ostream &operator<<(std::ostream &oss, const CodecDecodeJpegScanParameter &cr)
1338 {
1339     const CodecDecodeJpegScanParameter *scanParams = &cr;
1340 
1341     oss << "NumScans: " << +scanParams->NumScans << std::endl;
1342 
1343     // ScanHeader[jpegNumComponent]
1344     for (uint32_t i = 0; i < jpegNumComponent; ++i)
1345     {
1346         oss << "ScanHeader[" << +i << "].NumComponents: " << +scanParams->ScanHeader[i].NumComponents << std::endl;
1347 
1348         // ComponentSelector[jpegNumComponent]
1349         for (uint32_t j = 0; j < jpegNumComponent; ++j)
1350         {
1351             oss << "ScanHeader[" << +i << "].ComponentSelector[" << +j << "]: " << +scanParams->ScanHeader[i].ComponentSelector[j] << std::endl;
1352         }
1353 
1354         // DcHuffTblSelector[jpegNumComponent]
1355         for (uint32_t j = 0; j < jpegNumComponent; ++j)
1356         {
1357             oss << "ScanHeader[" << +i << "].DcHuffTblSelector[" << +j << "]: " << +scanParams->ScanHeader[i].DcHuffTblSelector[j] << std::endl;
1358         }
1359 
1360         // AcHuffTblSelector[jpegNumComponent]
1361         for (uint32_t j = 0; j < jpegNumComponent; ++j)
1362         {
1363             oss << "ScanHeader[" << +i << "].AcHuffTblSelector[" << +j << "]: " << +scanParams->ScanHeader[i].AcHuffTblSelector[j] << std::endl;
1364         }
1365 
1366         oss << "ScanHeader[" << +i << "].RestartInterval: " << +scanParams->ScanHeader[i].RestartInterval << std::endl;
1367         oss << "ScanHeader[" << +i << "].MCUCount: " << +scanParams->ScanHeader[i].MCUCount << std::endl;
1368         oss << "ScanHeader[" << +i << "].ScanHoriPosition: " << +scanParams->ScanHeader[i].ScanHoriPosition << std::endl;
1369         oss << "ScanHeader[" << +i << "].ScanVertPosition: " << +scanParams->ScanHeader[i].ScanVertPosition << std::endl;
1370         oss << "ScanHeader[" << +i << "].DataOffset: " << +scanParams->ScanHeader[i].DataOffset << std::endl;
1371         oss << "ScanHeader[" << +i << "].DataLength: " << +scanParams->ScanHeader[i].DataLength << std::endl;
1372     }
1373 
1374     return oss;
1375 }
1376 
operator <<(std::ostream & oss,const CODECHAL_DECODE_JPEG_HUFFMAN_TABLE & cr)1377 std::ostream &operator<<(std::ostream &oss, const CODECHAL_DECODE_JPEG_HUFFMAN_TABLE &cr)
1378 {
1379     const CODECHAL_DECODE_JPEG_HUFFMAN_TABLE *huffmanTable = &cr;
1380 
1381     // HuffTable[JPEG_MAX_NUM_HUFF_TABLE_INDEX]
1382     for (uint32_t i = 0; i < JPEG_MAX_NUM_HUFF_TABLE_INDEX; ++i)
1383     {
1384         // DC_BITS[JPEG_NUM_HUFF_TABLE_DC_BITS]
1385         oss << "HuffTable[" << +i << "].DC_BITS[0-" << (JPEG_NUM_HUFF_TABLE_DC_BITS - 1) << "]: " << std::endl;
1386 
1387         for (uint32_t j = 0; j < JPEG_NUM_HUFF_TABLE_DC_BITS; ++j)
1388         {
1389             oss << +huffmanTable->HuffTable[i].DC_BITS[j] << " ";
1390             if (j % 6 == 5 || j == JPEG_NUM_HUFF_TABLE_DC_BITS - 1)
1391             {
1392                 oss << std::endl;
1393             }
1394         }
1395         // DC_HUFFVAL[JPEG_NUM_HUFF_TABLE_DC_HUFFVAL]
1396         oss << "HuffTable[" << +i << "].DC_HUFFVAL[0-" << (JPEG_NUM_HUFF_TABLE_DC_HUFFVAL - 1) << "]: " << std::endl;
1397         for (uint32_t j = 0; j < JPEG_NUM_HUFF_TABLE_DC_HUFFVAL; ++j)
1398         {
1399             oss << +huffmanTable->HuffTable[i].DC_HUFFVAL[j] << ' ';
1400             if (j % 6 == 5 || j == JPEG_NUM_HUFF_TABLE_DC_HUFFVAL - 1)
1401             {
1402                 oss << std::endl;
1403             }
1404         }
1405         // AC_BITS[JPEG_NUM_HUFF_TABLE_AC_BITS]
1406         oss << "HuffTable[" << +i << "].AC_BITS[0-" << (JPEG_NUM_HUFF_TABLE_AC_BITS - 1) << "]: " << std::endl;
1407 
1408         for (uint32_t j = 0; j < JPEG_NUM_HUFF_TABLE_AC_BITS; ++j)
1409         {
1410             oss << +huffmanTable->HuffTable[i].AC_BITS[j] << ' ';
1411             if (j % 8 == 7 || j == JPEG_NUM_HUFF_TABLE_AC_BITS - 1)
1412             {
1413                 oss << std::endl;
1414             }
1415         }
1416 
1417         // AC_HUFFVAL[JPEG_NUM_HUFF_TABLE_AC_HUFFVAL]
1418         oss << "HuffTable[" << +i << "].AC_HUFFVAL[0-" << (JPEG_NUM_HUFF_TABLE_AC_HUFFVAL - 1) << "]: " << std::endl;
1419 
1420         for (uint32_t j = 0; j < JPEG_NUM_HUFF_TABLE_AC_HUFFVAL; ++j)
1421         {
1422             oss << +huffmanTable->HuffTable[i].AC_HUFFVAL[j] << ' ';
1423             if (j % 9 == 8 || j == JPEG_NUM_HUFF_TABLE_AC_HUFFVAL - 1)
1424             {
1425                 oss << std::endl;
1426             }
1427         }
1428     }
1429 
1430     return oss;
1431 }
1432 
operator <<(std::ostream & oss,const CodecJpegQuantMatrix & cr)1433 std::ostream &operator<<(std::ostream &oss, const CodecJpegQuantMatrix &cr)
1434 {
1435     const CodecJpegQuantMatrix *iqParams = &cr;
1436 
1437     for (uint32_t j = 0; j < jpegNumComponent; j++)
1438     {
1439         oss << "Qmatrix " << +j << ": " << std::endl;
1440 
1441         for (int8_t i = 0; i < 56; i += 8)
1442         {
1443             oss << "Qmatrix[" << +i / 8 << "]:";
1444             for (uint8_t k = 0; k < 8; k++)
1445                 oss << +iqParams->m_quantMatrix[j][i + k] << " ";
1446             oss << std::endl;
1447         }
1448     }
1449 
1450     return oss;
1451 }
1452 
DumpDecodeJpegPicParams(CodecDecodeJpegPicParams * picParams,std::string fileName)1453 void DumpDecodeJpegPicParams(CodecDecodeJpegPicParams *picParams, std::string fileName)
1454 {
1455     std::ostringstream oss;
1456     oss.setf(std::ios::showbase | std::ios::uppercase);
1457 
1458     oss << *picParams << std::endl;
1459 
1460     std::ofstream ofs(fileName, std::ios::out);
1461     ofs << oss.str();
1462     ofs.close();
1463 }
1464 
DumpDecodeJpegScanParams(CodecDecodeJpegScanParameter * scanParams,std::string fileName)1465 void DumpDecodeJpegScanParams(CodecDecodeJpegScanParameter *scanParams, std::string fileName)
1466 {
1467     std::ostringstream oss;
1468     oss.setf(std::ios::showbase | std::ios::uppercase);
1469 
1470     oss << *scanParams << std::endl;
1471 
1472     std::ofstream ofs(fileName, std::ios::out);
1473     ofs << oss.str();
1474     ofs.close();
1475 }
1476 
DumpDecodeJpegHuffmanParams(PCODECHAL_DECODE_JPEG_HUFFMAN_TABLE huffmanTable,std::string fileName)1477 void DumpDecodeJpegHuffmanParams(PCODECHAL_DECODE_JPEG_HUFFMAN_TABLE huffmanTable, std::string fileName)
1478 {
1479     std::ostringstream oss;
1480     oss.setf(std::ios::showbase | std::ios::uppercase);
1481 
1482     oss << *huffmanTable << std::endl;
1483 
1484     std::ofstream ofs(fileName, std::ios::out);
1485     ofs << oss.str();
1486     ofs.close();
1487 }
1488 
DumpDecodeJpegIqParams(CodecJpegQuantMatrix * iqParams,std::string fileName)1489 void DumpDecodeJpegIqParams(CodecJpegQuantMatrix *iqParams, std::string fileName)
1490 {
1491     std::ostringstream oss;
1492     oss.setf(std::ios::showbase | std::ios::uppercase);
1493 
1494     oss << *iqParams << std::endl;
1495 
1496     std::ofstream ofs(fileName, std::ios::out);
1497     ofs << oss.str();
1498     ofs.close();
1499 }
1500 
1501 // MPEG2 Decode
operator <<(std::ostream & oss,const CodecDecodeMpeg2PicParams & cr)1502 std::ostream &operator<<(std::ostream &oss, const CodecDecodeMpeg2PicParams &cr)
1503 {
1504     const CodecDecodeMpeg2PicParams *picParams = &cr;
1505 
1506     oss << "m_currPic FrameIdx: " << +picParams->m_currPic.FrameIdx << std::endl;
1507     oss << "m_currPic PicFlags: " << +picParams->m_currPic.PicFlags << std::endl;
1508     oss << "m_forwardRefIdx: " << +picParams->m_forwardRefIdx << std::endl;
1509     oss << "m_backwardRefIdx: " << +picParams->m_backwardRefIdx << std::endl;
1510     oss << "m_topFieldFirst: " << +picParams->m_topFieldFirst << std::endl;
1511     oss << "m_secondField: " << +picParams->m_secondField << std::endl;
1512     oss << "m_statusReportFeedbackNumber: " << +picParams->m_statusReportFeedbackNumber << std::endl;
1513     // union w0
1514     oss << "w0 m_value: " << +picParams->W0.m_value << std::endl;
1515     oss << "m_scanOrder: " << +picParams->W0.m_scanOrder << std::endl;
1516     oss << "m_intraVlcFormat: " << +picParams->W0.m_intraVlcFormat << std::endl;
1517     oss << "m_quantizerScaleType: " << +picParams->W0.m_quantizerScaleType << std::endl;
1518     oss << "m_concealmentMVFlag: " << +picParams->W0.m_concealmentMVFlag << std::endl;
1519     oss << "m_frameDctPrediction: " << +picParams->W0.m_frameDctPrediction << std::endl;
1520     oss << "m_topFieldFirst: " << +picParams->W0.m_topFieldFirst << std::endl;
1521     oss << "m_intraDCPrecision: " << +picParams->W0.m_intraDCPrecision << std::endl;
1522     // union w1
1523     oss << "w1 m_value: " << +picParams->W1.m_value << std::endl;
1524     oss << "m_fcode11: " << +picParams->W1.m_fcode11 << std::endl;
1525     oss << "m_fcode10: " << +picParams->W1.m_fcode10 << std::endl;
1526     oss << "m_fcode01: " << +picParams->W1.m_fcode01 << std::endl;
1527     oss << "m_fcode00: " << +picParams->W1.m_fcode00 << std::endl;
1528     oss << "m_horizontalSize: " << +picParams->m_horizontalSize << std::endl;
1529     oss << "m_verticalSize: " << +picParams->m_verticalSize << std::endl;
1530     oss << "m_pictureCodingType: " << +picParams->m_pictureCodingType << std::endl;
1531 
1532     return oss;
1533 }
1534 
operator <<(std::ostream & oss,const CodecDecodeMpeg2SliceParams & cr)1535 std::ostream &operator<<(std::ostream &oss, const CodecDecodeMpeg2SliceParams &cr)
1536 {
1537     const CodecDecodeMpeg2SliceParams *sliceControl = &cr;
1538 
1539     oss << "m_sliceDataSize: " << +sliceControl->m_sliceDataSize << std::endl;
1540     oss << "m_sliceDataOffset: " << +sliceControl->m_sliceDataOffset << std::endl;
1541     oss << "m_macroblockOffset: " << +sliceControl->m_macroblockOffset << std::endl;
1542     oss << "m_sliceHorizontalPosition: " << +sliceControl->m_sliceHorizontalPosition << std::endl;
1543     oss << "m_sliceVerticalPosition: " << +sliceControl->m_sliceVerticalPosition << std::endl;
1544     oss << "m_quantiserScaleCode: " << +sliceControl->m_quantiserScaleCode << std::endl;
1545     oss << "m_numMbsForSlice: " << +sliceControl->m_numMbsForSlice << std::endl;
1546     oss << "m_numMbsForSliceOverflow: " << +sliceControl->m_numMbsForSliceOverflow << std::endl;
1547     oss << "m_reservedBits: " << +sliceControl->m_reservedBits << std::endl;
1548     oss << "m_startCodeBitOffset: " << +sliceControl->m_startCodeBitOffset << std::endl;
1549     oss << std::endl;
1550 
1551     return oss;
1552 }
1553 
operator <<(std::ostream & oss,const CodecDecodeMpeg2MbParams & cr)1554 std::ostream &operator<<(std::ostream &oss, const CodecDecodeMpeg2MbParams &cr)
1555 {
1556     const CodecDecodeMpeg2MbParams *mbParams = &cr;
1557 
1558     oss << "m_mbAddr: " << +mbParams->m_mbAddr << std::endl;
1559     oss << "MBType.m_intraMb: " << +mbParams->MBType.m_intraMb << std::endl;
1560     oss << "MBType.m_motionFwd: " << +mbParams->MBType.m_motionFwd << std::endl;
1561     oss << "MBType.m_motionBwd: " << +mbParams->MBType.m_motionBwd << std::endl;
1562     oss << "MBType.m_motion4mv: " << +mbParams->MBType.m_motion4mv << std::endl;
1563     oss << "MBType.m_h261Lpfilter: " << +mbParams->MBType.m_h261Lpfilter << std::endl;
1564     oss << "MBType.m_fieldResidual: " << +mbParams->MBType.m_fieldResidual << std::endl;
1565     oss << "MBType.m_mbScanMethod: " << +mbParams->MBType.m_mbScanMethod << std::endl;
1566     oss << "MBType.m_motionType: " << +mbParams->MBType.m_motionType << std::endl;
1567     oss << "MBType.m_hostResidualDiff: " << +mbParams->MBType.m_hostResidualDiff << std::endl;
1568     oss << "MBType.m_mvertFieldSel: " << +mbParams->MBType.m_mvertFieldSel << std::endl;
1569     oss << "m_mbSkipFollowing: " << +mbParams->m_mbSkipFollowing << std::endl;
1570     oss << "m_mbDataLoc: " << +mbParams->m_mbDataLoc << std::endl;
1571     oss << "m_codedBlockPattern: " << +mbParams->m_codedBlockPattern << std::endl;
1572 
1573     // NumCoeff[CODEC_NUM_BLOCK_PER_MB]
1574     for (uint16_t i = 0; i < CODEC_NUM_BLOCK_PER_MB; ++i)
1575     {
1576         oss << "m_numCoeff[" << +i << "]: " << +mbParams->m_numCoeff[i] << std::endl;
1577     }
1578 
1579     // motion_vectors[8],printing them in 4 value chunks per line
1580     for (uint8_t i = 0; i < 2; ++i)
1581     {
1582         oss << "m_motionVectors[" << +i * 4 << "-" << (+i * 4) + 3 << "]: ";
1583         for (uint8_t j = 0; j < 4; j++)
1584             oss << +mbParams->m_motionVectors[i * 4 + j] << " ";
1585         oss << std::endl;
1586     }
1587 
1588     oss << std::endl;
1589 
1590     return oss;
1591 }
1592 
operator <<(std::ostream & oss,const CodecMpeg2IqMatrix & cr)1593 std::ostream &operator<<(std::ostream &oss, const CodecMpeg2IqMatrix &cr)
1594 {
1595     const CodecMpeg2IqMatrix *iqParams = &cr;
1596 
1597     if (iqParams->m_loadIntraQuantiserMatrix)
1598     {
1599         oss << "intra_QmatrixData:" << std::endl;
1600 
1601         for (uint8_t i = 0; i < 56; i += 8)
1602         {
1603             oss << "Qmatrix[" << +i / 8 << "]: ";
1604             for (uint8_t j = 0; j < 8; j++)
1605                 oss << +iqParams->m_intraQuantiserMatrix[i + j] << " ";
1606             oss << std::endl;
1607         }
1608     }
1609 
1610     if (iqParams->m_loadNonIntraQuantiserMatrix)
1611     {
1612         oss << "non_intra_QmatrixData:" << std::endl;
1613 
1614         for (uint8_t i = 0; i < 56; i += 8)
1615         {
1616             oss << "Qmatrix[" << +i / 8 << "]: ";
1617             for (uint8_t j = 0; j < 8; j++)
1618                 oss << +iqParams->m_nonIntraQuantiserMatrix[i + j] << " ";
1619             oss << std::endl;
1620         }
1621     }
1622 
1623     if (iqParams->m_loadChromaIntraQuantiserMatrix)
1624     {
1625         oss << "chroma_intra_QmatrixData:" << std::endl;
1626 
1627         for (uint8_t i = 0; i < 56; i += 8)
1628         {
1629             oss << "Qmatrix[" << +i / 8 << "]: ";
1630             for (uint8_t j = 0; j < 8; j++)
1631                 oss << +iqParams->m_chromaIntraQuantiserMatrix[i + j] << " ";
1632             oss << std::endl;
1633         }
1634     }
1635 
1636     if (iqParams->m_loadChromaNonIntraQuantiserMatrix)
1637     {
1638         oss << "chroma_non_intra_QmatrixData:" << std::endl;
1639 
1640         for (uint8_t i = 0; i < 56; i += 8)
1641         {
1642             oss << "Qmatrix[" << +i / 8 << "]: ";
1643             for (uint8_t j = 0; j < 8; j++)
1644                 oss << +iqParams->m_chromaNonIntraQuantiserMatrix[i + j] << " ";
1645             oss << std::endl;
1646         }
1647     }
1648 
1649     return oss;
1650 }
1651 
DumpDecodeMpeg2PicParams(CodecDecodeMpeg2PicParams * picParams,std::string fileName)1652 void DumpDecodeMpeg2PicParams(CodecDecodeMpeg2PicParams *picParams, std::string fileName)
1653 {
1654     std::ostringstream oss;
1655     oss.setf(std::ios::showbase | std::ios::uppercase);
1656 
1657     oss << *picParams << std::endl;
1658 
1659     std::ofstream ofs(fileName, std::ios::out);
1660     ofs << oss.str();
1661     ofs.close();
1662 }
1663 
DumpDecodeMpeg2SliceParams(CodecDecodeMpeg2SliceParams * sliceParams,uint32_t numSlices,std::string fileName)1664 void DumpDecodeMpeg2SliceParams(CodecDecodeMpeg2SliceParams *sliceParams, uint32_t numSlices, std::string fileName)
1665 {
1666     std::ostringstream oss;
1667     oss.setf(std::ios::showbase | std::ios::uppercase);
1668 
1669     if (numSlices > 1)
1670     {
1671         oss << "Total Num: " << numSlices << std::endl;
1672         oss << std::endl;
1673     }
1674 
1675     for (uint32_t i = 0; i < numSlices; i++)
1676     {
1677         if (numSlices > 1)
1678         {
1679             oss << "---------Index = " << i << "---------" << std::endl;
1680         }
1681 
1682         oss << sliceParams[i] << std::endl;
1683     }
1684 
1685     std::ofstream ofs(fileName, std::ios::out);
1686     ofs << oss.str();
1687     ofs.close();
1688 }
1689 
DumpDecodeMpeg2MbParams(CodecDecodeMpeg2MbParams * mbParams,uint32_t numMbs,std::string fileName)1690 void DumpDecodeMpeg2MbParams(CodecDecodeMpeg2MbParams *mbParams, uint32_t numMbs, std::string fileName)
1691 {
1692     std::ostringstream oss;
1693     oss.setf(std::ios::showbase | std::ios::uppercase);
1694 
1695     if (numMbs > 1)
1696     {
1697         oss << "Total Num: " << numMbs << std::endl;
1698         oss << std::endl;
1699     }
1700 
1701     for (uint32_t i = 0; i < numMbs; i++)
1702     {
1703         if (numMbs > 1)
1704         {
1705             oss << "---------Index = " << i << "---------" << std::endl;
1706         }
1707 
1708         oss << mbParams[i] << std::endl;
1709     }
1710 
1711     std::ofstream ofs(fileName, std::ios::out);
1712     ofs << oss.str();
1713     ofs.close();
1714 }
1715 
DumpDecodeMpeg2IqParams(CodecMpeg2IqMatrix * iqParams,std::string fileName)1716 void DumpDecodeMpeg2IqParams(CodecMpeg2IqMatrix *iqParams, std::string fileName)
1717 {
1718     std::ostringstream oss;
1719     oss.setf(std::ios::showbase | std::ios::uppercase);
1720 
1721     oss << *iqParams << std::endl;
1722 
1723     std::ofstream ofs(fileName, std::ios::out);
1724     ofs << oss.str();
1725     ofs.close();
1726 }
1727 
1728 // VP8 Decode
operator <<(std::ostream & oss,const CODEC_VP8_PIC_PARAMS & cr)1729 std::ostream &operator<<(std::ostream &oss, const CODEC_VP8_PIC_PARAMS &cr)
1730 {
1731     const CODEC_VP8_PIC_PARAMS *picParams = &cr;
1732 
1733     oss << "CurrPic FrameIdx: " << +picParams->CurrPic.FrameIdx << std::endl;
1734     oss << "CurrPic PicFlags: " << +picParams->CurrPic.PicFlags << std::endl;
1735     oss << "wFrameWidthInMbsMinus1: " << +picParams->wFrameWidthInMbsMinus1 << std::endl;
1736     oss << "wFrameHeightInMbsMinus1: " << +picParams->wFrameHeightInMbsMinus1 << std::endl;
1737     oss << "ucCurrPicIndex: " << +picParams->ucCurrPicIndex << std::endl;
1738     oss << "ucLastRefPicIndex: " << +picParams->ucLastRefPicIndex << std::endl;
1739     oss << "ucGoldenRefPicIndex: " << +picParams->ucGoldenRefPicIndex << std::endl;
1740     oss << "ucAltRefPicIndex: " << +picParams->ucAltRefPicIndex << std::endl;
1741     oss << "ucDeblockedPicIndex: " << +picParams->ucDeblockedPicIndex << std::endl;
1742     oss << "ucReserved8Bits: " << +picParams->ucReserved8Bits << std::endl;
1743     oss << "wPicFlags: " << +picParams->wPicFlags << std::endl;
1744     oss << "key_frame: " << +picParams->key_frame << std::endl;
1745     oss << "version: " << +picParams->version << std::endl;
1746     oss << "segmentation_enabled: " << +picParams->segmentation_enabled << std::endl;
1747     oss << "update_mb_segmentation_map: " << +picParams->update_mb_segmentation_map << std::endl;
1748     oss << "update_segment_feature_data: " << +picParams->update_segment_feature_data << std::endl;
1749     oss << "filter_type: " << +picParams->filter_type << std::endl;
1750     oss << "sign_bias_golden: " << +picParams->sign_bias_golden << std::endl;
1751     oss << "sign_bias_alternate: " << +picParams->sign_bias_alternate << std::endl;
1752     oss << "mb_no_coeff_skip: " << +picParams->mb_no_coeff_skip << std::endl;
1753     oss << "mode_ref_lf_delta_update: " << +picParams->mode_ref_lf_delta_update << std::endl;
1754     oss << "CodedCoeffTokenPartition: " << +picParams->CodedCoeffTokenPartition << std::endl;
1755     oss << "LoopFilterDisable: " << +picParams->LoopFilterDisable << std::endl;
1756     oss << "loop_filter_adj_enable: " << +picParams->loop_filter_adj_enable << std::endl;
1757 
1758     for (uint8_t i = 0; i < 4; ++i)
1759     {
1760         oss << "ucLoopFilterLevel[" << +i << "]: " << +picParams->ucLoopFilterLevel[i] << std::endl;
1761     }
1762 
1763     for (uint8_t i = 0; i < 4; ++i)
1764     {
1765         oss << "cRefLfDelta[" << +i << "]: " << +picParams->cRefLfDelta[i] << std::endl;
1766     }
1767 
1768     for (uint8_t i = 0; i < 4; ++i)
1769     {
1770         oss << "cModeLfDelta[" << +i << "]: " << +picParams->cModeLfDelta[i] << std::endl;
1771     }
1772     oss << "ucSharpnessLevel: " << +picParams->ucSharpnessLevel << std::endl;
1773 
1774     for (uint8_t i = 0; i < 3; ++i)
1775     {
1776         oss << "cMbSegmentTreeProbs[" << +i << "]: " << +picParams->cMbSegmentTreeProbs[i] << std::endl;
1777     }
1778     oss << "ucProbSkipFalse: " << +picParams->ucProbSkipFalse << std::endl;
1779     oss << "ucProbIntra: " << +picParams->ucProbIntra << std::endl;
1780     oss << "ucProbLast: " << +picParams->ucProbLast << std::endl;
1781     oss << "ucProbGolden: " << +picParams->ucProbGolden << std::endl;
1782 
1783     for (uint8_t i = 0; i < 4; ++i)
1784     {
1785         oss << "ucYModeProbs[" << +i << "]: " << +picParams->ucYModeProbs[i] << std::endl;
1786     }
1787 
1788     for (uint8_t i = 0; i < 3; ++i)
1789     {
1790         oss << "ucUvModeProbs[" << +i << "]: " << +picParams->ucUvModeProbs[i] << std::endl;
1791     }
1792     oss << "ucReserved8Bits1: " << +picParams->ucReserved8Bits1 << std::endl;
1793     oss << "ucP0EntropyCount: " << +picParams->ucP0EntropyCount << std::endl;
1794     oss << "ucP0EntropyValue: " << +picParams->ucP0EntropyValue << std::endl;
1795     oss << "uiP0EntropyRange: " << +picParams->uiP0EntropyRange << std::endl;
1796     oss << "uiFirstMbByteOffset: " << +picParams->uiFirstMbByteOffset << std::endl;
1797 
1798     for (uint8_t i = 0; i < 2; ++i)
1799     {
1800         for (uint8_t j = 0; j < CODEC_VP8_MVP_COUNT; ++j)
1801         {
1802             oss << "ucMvUpdateProb[" << +i << "][" << +j << "]: " << +picParams->ucMvUpdateProb[i][j] << std::endl;
1803         }
1804     }
1805     for (uint8_t i = 0; i < CODEC_VP8_MAX_PARTITION_NUMBER; ++i)
1806     {
1807         oss << "uiPartitionSize[" << +i << "]: " << +picParams->uiPartitionSize[i] << std::endl;
1808     }
1809     oss << "uiStatusReportFeedbackNumber: " << +picParams->uiStatusReportFeedbackNumber << std::endl;
1810 
1811     return oss;
1812 }
1813 
operator <<(std::ostream & oss,const CODEC_VP8_SLICE_PARAMS & cr)1814 std::ostream &operator<<(std::ostream &oss, const CODEC_VP8_SLICE_PARAMS &cr)
1815 {
1816     const CODEC_VP8_SLICE_PARAMS *sliceParams = &cr;
1817 
1818     oss << "BSNALunitDataLocation: " << +sliceParams->BSNALunitDataLocation << std::endl;
1819     oss << "SliceBytesInBuffer: " << +sliceParams->SliceBytesInBuffer << std::endl;
1820     oss << std::endl;
1821 
1822     return oss;
1823 }
1824 
operator <<(std::ostream & oss,const CODEC_VP8_IQ_MATRIX_PARAMS & cr)1825 std::ostream &operator<<(std::ostream &oss, const CODEC_VP8_IQ_MATRIX_PARAMS &cr)
1826 {
1827     const CODEC_VP8_IQ_MATRIX_PARAMS *iqParams = &cr;
1828 
1829     for (uint8_t i = 0; i < 4; ++i)
1830     {
1831         for (uint8_t j = 0; j < 6; ++j)
1832         {
1833             oss << "quantization_values[" << +i << "][" << +j << "]: " << +iqParams->quantization_values[i][j] << std::endl;
1834         }
1835     }
1836 
1837     return oss;
1838 }
1839 
DumpDecodeVp8PicParams(PCODEC_VP8_PIC_PARAMS picParams,std::string fileName)1840 void DumpDecodeVp8PicParams(PCODEC_VP8_PIC_PARAMS picParams, std::string fileName)
1841 {
1842     std::ostringstream oss;
1843     oss.setf(std::ios::showbase | std::ios::uppercase);
1844 
1845     oss << *picParams << std::endl;
1846 
1847     std::ofstream ofs(fileName, std::ios::out);
1848     ofs << oss.str();
1849     ofs.close();
1850 }
1851 
DumpDecodeVp8SliceParams(PCODEC_VP8_SLICE_PARAMS sliceParams,std::string fileName)1852 void DumpDecodeVp8SliceParams(PCODEC_VP8_SLICE_PARAMS sliceParams, std::string fileName)
1853 {
1854     std::ostringstream oss;
1855     oss.setf(std::ios::showbase | std::ios::uppercase);
1856 
1857     oss << *sliceParams << std::endl;
1858 
1859     std::ofstream ofs(fileName, std::ios::out);
1860     ofs << oss.str();
1861     ofs.close();
1862 }
1863 
DumpDecodeVp8IqParams(PCODEC_VP8_IQ_MATRIX_PARAMS iqParams,std::string fileName)1864 void DumpDecodeVp8IqParams(PCODEC_VP8_IQ_MATRIX_PARAMS iqParams, std::string fileName)
1865 {
1866     std::ostringstream oss;
1867     oss.setf(std::ios::showbase | std::ios::uppercase);
1868 
1869     oss << *iqParams << std::endl;
1870 
1871     std::ofstream ofs(fileName, std::ios::out);
1872     ofs << oss.str();
1873     ofs.close();
1874 }
1875 
CodechalDebugInterface()1876 CodechalDebugInterface::CodechalDebugInterface()
1877 {
1878     MOS_ZeroMemory(m_fileName, sizeof(m_fileName));
1879     MOS_ZeroMemory(m_path, sizeof(m_path));
1880 
1881     m_dumpYUVSurfaceLegacy = [this](
1882                            PMOS_SURFACE              surface,
1883                            const char               *attrName,
1884                            const char               *surfName,
1885                            CODECHAL_MEDIA_STATE_TYPE mediaState,
1886                            uint32_t                  width_in,
1887                            uint32_t                  height_in) {
1888         bool     hasAuxSurf   = false;
1889         bool     isPlanar     = true;
1890         bool     hasRefSurf   = false;
1891         uint8_t *surfBaseAddr = nullptr;
1892         uint8_t *lockedAddr   = nullptr;
1893         if (!DumpIsEnabled(attrName, mediaState))
1894         {
1895             return MOS_STATUS_SUCCESS;
1896         }
1897 
1898         const char *funcName = (m_codecFunction == CODECHAL_FUNCTION_DECODE) ? "_DEC" : (m_codecFunction == CODECHAL_FUNCTION_CENC_DECODE ? "_DEC" : "_ENC");
1899         std::string bufName  = std::string(surfName) + "_w[" + std::to_string(surface->dwWidth) + "]_h[" + std::to_string(surface->dwHeight) + "]_p[" + std::to_string(surface->dwPitch) + "]";
1900 
1901         const char *filePath = CreateFileName(funcName, bufName.c_str(), hasAuxSurf ? ".Y" : ".yuv");
1902 
1903         MOS_LOCK_PARAMS   lockFlags;
1904         GMM_RESOURCE_FLAG gmmFlags;
1905 
1906         MOS_ZeroMemory(&gmmFlags, sizeof(gmmFlags));
1907         CODECHAL_DEBUG_CHK_NULL(surface);
1908         gmmFlags   = surface->OsResource.pGmmResInfo->GetResFlags();
1909         hasAuxSurf = (gmmFlags.Gpu.MMC || gmmFlags.Info.MediaCompressed) && gmmFlags.Gpu.UnifiedAuxSurface;
1910 
1911         if (!m_configMgr->AttrIsEnabled(CodechalDbgAttr::attrDecodeAuxSurface))
1912         {
1913             hasAuxSurf = false;
1914         }
1915 
1916         isPlanar = !!(m_osInterface->pfnGetGmmClientContext(m_osInterface)->IsPlanar(surface->OsResource.pGmmResInfo->GetResourceFormat()));
1917 
1918         MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
1919         lockFlags.ReadOnly     = 1;
1920         lockFlags.TiledAsTiled = 1;  // Bypass GMM CPU blit due to some issues in GMM CpuBlt function
1921         if (hasAuxSurf)
1922         {
1923             // Dump MMC surface as raw layout
1924             lockFlags.NoDecompress = 1;
1925         }
1926 
1927         // when not setting dump compressed surface cfg, use ve copy for dump
1928         if (!m_configMgr->AttrIsEnabled(CodechalDbgAttr::attrDecodeCompSurface))
1929         {
1930             DumpUncompressedYUVSurface(surface);
1931             lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &m_temp2DSurfForCopy.OsResource, &lockFlags);
1932             CODECHAL_DEBUG_CHK_NULL(lockedAddr);
1933             surface = &m_temp2DSurfForCopy;
1934         }
1935         else
1936         {
1937             // when setting dump compressed surface cfg, try directly lock surface
1938             lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &surface->OsResource, &lockFlags);
1939             // if failed to lock, try to submit copy task and reallocate temp surface for dump
1940             if (lockedAddr == nullptr)
1941             {
1942                 uint32_t        sizeToBeCopied = 0;
1943                 MOS_GFXRES_TYPE ResType;
1944 
1945 #if !defined(LINUX)
1946                 ResType = surface->OsResource.ResType;
1947 
1948                 CODECHAL_DEBUG_CHK_STATUS(ReAllocateSurface(
1949                     &m_temp2DSurfForCopy,
1950                     surface,
1951                     "Temp2DSurfForSurfDumper",
1952                     ResType));
1953 #endif
1954 
1955                 uint32_t arraySize = surface->OsResource.pGmmResInfo->GetArraySize();
1956 
1957                 if (arraySize == 0)
1958                 {
1959                     return MOS_STATUS_UNKNOWN;
1960                 }
1961 
1962                 uint32_t sizeSrcSurface = (uint32_t)(surface->OsResource.pGmmResInfo->GetSizeMainSurface()) / arraySize;
1963                 // Ensure allocated buffer size contains the source surface size
1964                 if (m_temp2DSurfForCopy.OsResource.pGmmResInfo->GetSizeMainSurface() >= sizeSrcSurface)
1965                 {
1966                     sizeToBeCopied = sizeSrcSurface;
1967                 }
1968 
1969                 if (sizeToBeCopied == 0)
1970                 {
1971                     CODECHAL_DEBUG_ASSERTMESSAGE("Cannot allocate correct size, failed to copy nonlockable resource");
1972                     return MOS_STATUS_INVALID_PARAMETER;
1973                 }
1974 
1975                 CODECHAL_DEBUG_VERBOSEMESSAGE("Temp2DSurfaceForCopy width %d, height %d, pitch %d, TileType %d, bIsCompressed %d, CompressionMode %d",
1976                     m_temp2DSurfForCopy.dwWidth,
1977                     m_temp2DSurfForCopy.dwHeight,
1978                     m_temp2DSurfForCopy.dwPitch,
1979                     m_temp2DSurfForCopy.TileType,
1980                     m_temp2DSurfForCopy.bIsCompressed,
1981                     m_temp2DSurfForCopy.CompressionMode);
1982 
1983                 uint32_t bpp = surface->OsResource.pGmmResInfo->GetBitsPerPixel();
1984 
1985                 CODECHAL_DEBUG_CHK_STATUS(m_osInterface->pfnMediaCopyResource2D(m_osInterface, &surface->OsResource, &m_temp2DSurfForCopy.OsResource, surface->dwPitch, sizeSrcSurface / (surface->dwPitch), bpp, false));
1986 
1987                 lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &m_temp2DSurfForCopy.OsResource, &lockFlags);
1988                 CODECHAL_DEBUG_CHK_NULL(lockedAddr);
1989                 surface = &m_temp2DSurfForCopy;
1990             }
1991         }
1992         gmmFlags     = surface->OsResource.pGmmResInfo->GetResFlags();
1993         surfBaseAddr = lockedAddr;
1994 
1995         // Use MOS swizzle for non-vecopy dump
1996         if (m_configMgr->AttrIsEnabled(CodechalDbgAttr::attrDecodeCompSurface))
1997         {
1998             uint32_t sizeMain = (uint32_t)(surface->OsResource.pGmmResInfo->GetSizeMainSurface());
1999             surfBaseAddr      = (uint8_t *)MOS_AllocMemory(sizeMain);
2000             CODECHAL_DEBUG_CHK_NULL(surfBaseAddr);
2001             Mos_SwizzleData(lockedAddr, surfBaseAddr, surface->TileType, MOS_TILE_LINEAR, sizeMain / surface->dwPitch, surface->dwPitch, !MEDIA_IS_SKU(m_osInterface->pfnGetSkuTable(m_osInterface), FtrTileY) || gmmFlags.Info.Tile4);
2002         }
2003 
2004         uint8_t *data = surfBaseAddr;
2005         data += surface->dwOffset + surface->YPlaneOffset.iYOffset * surface->dwPitch;
2006 
2007         uint32_t width      = width_in ? width_in : surface->dwWidth;
2008         uint32_t height     = height_in ? height_in : surface->dwHeight;
2009         uint32_t lumaheight = 0;
2010 
2011         switch (surface->Format)
2012         {
2013         case Format_YUY2:
2014         case Format_P010:
2015         case Format_P016:
2016             width = width << 1;
2017             break;
2018         case Format_Y216:
2019         case Format_Y210:  //422 10bit -- Y0[15:0]:U[15:0]:Y1[15:0]:V[15:0] = 32bits per pixel = 4Bytes per pixel
2020         case Format_Y410:  //444 10bit -- A[31:30]:V[29:20]:Y[19:10]:U[9:0] = 32bits per pixel = 4Bytes per pixel
2021         case Format_R10G10B10A2:
2022         case Format_AYUV:  //444 8bit  -- A[31:24]:Y[23:16]:U[15:8]:V[7:0] = 32bits per pixel = 4Bytes per pixel
2023         case Format_A8R8G8B8:
2024             width = width << 2;
2025             break;
2026         case Format_Y416:
2027             width = width << 3;
2028             break;
2029         case Format_R8G8B8:
2030             width = width * 3;
2031             break;
2032         default:
2033             break;
2034         }
2035 
2036         uint32_t pitch = surface->dwPitch;
2037         if (surface->Format == Format_UYVY)
2038             pitch = width;
2039 
2040         if (CodecHal_PictureIsBottomField(m_currPic))
2041         {
2042             data += pitch;
2043         }
2044 
2045         if (CodecHal_PictureIsField(m_currPic))
2046         {
2047             pitch *= 2;
2048             height /= 2;
2049         }
2050 
2051         lumaheight = hasAuxSurf ? GFX_ALIGN(height, 32) : height;
2052 
2053         std::ofstream ofs(filePath, std::ios_base::out | std::ios_base::binary);
2054         if (ofs.fail())
2055         {
2056             return MOS_STATUS_UNKNOWN;
2057         }
2058 
2059         // write luma data to file
2060         for (uint32_t h = 0; h < lumaheight; h++)
2061         {
2062             ofs.write((char *)data, hasAuxSurf ? pitch : width);
2063             data += pitch;
2064         }
2065 
2066         switch (surface->Format)
2067         {
2068         case Format_NV12:
2069         case Format_P010:
2070         case Format_P016:
2071             height = height >> 1;
2072             break;
2073         case Format_Y416:  //444 16bit
2074         case Format_AYUV:  //444 8bit
2075         case Format_AUYV:
2076         case Format_Y410:  //444 10bit
2077         case Format_R10G10B10A2:
2078             height = height << 1;
2079             break;
2080         case Format_YUY2:
2081         case Format_YUYV:
2082         case Format_YVYU:
2083         case Format_UYVY:
2084         case Format_VYUY:
2085         case Format_Y216:  //422 16bit
2086         case Format_Y210:  //422 10bit
2087         case Format_P208:  //422 8bit
2088         case Format_RGBP:
2089         case Format_BGRP:
2090             break;
2091         case Format_422V:
2092         case Format_IMC3:
2093             height = height / 2;
2094             break;
2095         default:
2096             height = 0;
2097             break;
2098         }
2099 
2100         uint8_t *vPlaneData = surfBaseAddr;
2101         if (isPlanar)
2102         {
2103             if (hasAuxSurf)
2104             {
2105                 data = surfBaseAddr + surface->UPlaneOffset.iSurfaceOffset;
2106             }
2107             else
2108             {
2109 // LibVA uses iSurfaceOffset instead of iLockSurfaceOffset.To use this dump in Linux,
2110 // changing the variable to iSurfaceOffset may be necessary for the chroma dump to work properly.
2111 #if defined(LINUX)
2112                 data = surfBaseAddr + surface->UPlaneOffset.iSurfaceOffset;
2113                 if (surface->Format == Format_422V || surface->Format == Format_IMC3)
2114                 {
2115                     vPlaneData = surfBaseAddr + surface->VPlaneOffset.iSurfaceOffset;
2116                 }
2117 #else
2118                 data = surfBaseAddr + surface->UPlaneOffset.iLockSurfaceOffset;
2119                 if (surface->Format == Format_422V ||
2120                     surface->Format == Format_IMC3 ||
2121                     surface->Format == Format_RGBP ||
2122                     surface->Format == Format_BGRP)
2123                 {
2124                     vPlaneData = surfBaseAddr + surface->VPlaneOffset.iLockSurfaceOffset;
2125                 }
2126 #endif
2127             }
2128 
2129             //No seperate chroma for linear surfaces
2130             // Seperate Y/UV if MMC is enabled
2131             if (hasAuxSurf)
2132             {
2133                 const char   *uvfilePath = CreateFileName(funcName, bufName.c_str(), ".UV");
2134                 std::ofstream ofs1(uvfilePath, std::ios_base::out | std::ios_base::binary);
2135                 if (ofs1.fail())
2136                 {
2137                     return MOS_STATUS_UNKNOWN;
2138                 }
2139                 // write chroma data to file
2140                 for (uint32_t h = 0; h < GFX_ALIGN(height, 32); h++)
2141                 {
2142                     ofs1.write((char *)data, pitch);
2143                     data += pitch;
2144                 }
2145                 ofs1.close();
2146             }
2147             else
2148             {
2149                 // write chroma data to file
2150                 for (uint32_t h = 0; h < height; h++)
2151                 {
2152                     ofs.write((char *)data, hasAuxSurf ? pitch : width);
2153                     data += pitch;
2154                 }
2155 
2156                 // write v planar data to file
2157                 if (surface->Format == Format_422V ||
2158                     surface->Format == Format_IMC3 ||
2159                     surface->Format == Format_RGBP ||
2160                     surface->Format == Format_BGRP)
2161                 {
2162                     for (uint32_t h = 0; h < height; h++)
2163                     {
2164                         ofs.write((char *)vPlaneData, hasAuxSurf ? pitch : width);
2165                         vPlaneData += pitch;
2166                     }
2167                 }
2168             }
2169             ofs.close();
2170         }
2171 
2172         if (hasAuxSurf)
2173         {
2174             uint32_t resourceIndex = m_osInterface->pfnGetResourceIndex(&surface->OsResource);
2175             uint8_t *yAuxData      = (uint8_t *)lockedAddr + surface->OsResource.pGmmResInfo->GetPlanarAuxOffset(resourceIndex, GMM_AUX_Y_CCS);
2176             uint32_t yAuxSize      = isPlanar ? ((uint32_t)(surface->OsResource.pGmmResInfo->GetPlanarAuxOffset(resourceIndex, GMM_AUX_UV_CCS) -
2177                                                        surface->OsResource.pGmmResInfo->GetPlanarAuxOffset(resourceIndex, GMM_AUX_Y_CCS)))
2178                                               : (uint32_t)surface->OsResource.pGmmResInfo->GetAuxQPitch();
2179 
2180             // Y Aux data
2181             const char   *yAuxfilePath = CreateFileName(funcName, bufName.c_str(), ".Yaux");
2182             std::ofstream ofs2(yAuxfilePath, std::ios_base::out | std::ios_base::binary);
2183             if (ofs2.fail())
2184             {
2185                 return MOS_STATUS_UNKNOWN;
2186             }
2187             ofs2.write((char *)yAuxData, yAuxSize);
2188             ofs2.close();
2189 
2190             if (isPlanar)
2191             {
2192                 uint8_t *uvAuxData = (uint8_t *)lockedAddr + surface->OsResource.pGmmResInfo->GetPlanarAuxOffset(resourceIndex, GMM_AUX_UV_CCS);
2193                 uint32_t uvAuxSize = (uint32_t)surface->OsResource.pGmmResInfo->GetAuxQPitch() - yAuxSize;
2194 
2195                 // UV Aux data
2196                 const char   *uvAuxfilePath = CreateFileName(funcName, bufName.c_str(), ".UVaux");
2197                 std::ofstream ofs3(uvAuxfilePath, std::ios_base::out | std::ios_base::binary);
2198                 if (ofs3.fail())
2199                 {
2200                     return MOS_STATUS_UNKNOWN;
2201                 }
2202                 ofs3.write((char *)uvAuxData, uvAuxSize);
2203 
2204                 ofs3.close();
2205             }
2206         }
2207 
2208         if (surfBaseAddr && m_configMgr->AttrIsEnabled(CodechalDbgAttr::attrDecodeCompSurface))
2209         {
2210             MOS_FreeMemory(surfBaseAddr);
2211         }
2212         if (lockedAddr)
2213         {
2214             m_osInterface->pfnUnlockResource(m_osInterface, &surface->OsResource);
2215         }
2216 
2217         return MOS_STATUS_SUCCESS;
2218     };
2219 
2220     m_dumpYUVSurface = [this](
2221                                      PMOS_SURFACE              surface,
2222                                      const char               *attrName,
2223                                      const char               *surfName,
2224                                      CODECHAL_MEDIA_STATE_TYPE mediaState,
2225                                      uint32_t                  width_in,
2226                                      uint32_t                  height_in) {
2227         if (!DumpIsEnabled(attrName, mediaState))
2228         {
2229             return MOS_STATUS_SUCCESS;
2230         }
2231 
2232         const char *funcName = (m_codecFunction == CODECHAL_FUNCTION_DECODE) ? "_DEC" : (m_codecFunction == CODECHAL_FUNCTION_CENC_DECODE ? "_DEC" : "_ENC");
2233         std::string bufName  = std::string(surfName) + "_w[" + std::to_string(surface->dwWidth) + "]_h[" + std::to_string(surface->dwHeight) + "]_p[" + std::to_string(surface->dwPitch) + "]";
2234 
2235         const char *filePath = CreateFileName(funcName, bufName.c_str(), ".yuv");
2236 
2237         MediaDebugFastDump::Dump(surface->OsResource, filePath);
2238 
2239         if (!MediaDebugFastDump::IsGood())
2240         {
2241             return m_dumpYUVSurfaceLegacy(
2242                 surface,
2243                 attrName,
2244                 surfName,
2245                 mediaState,
2246                 width_in,
2247                 height_in);
2248         }
2249 
2250         return MOS_STATUS_SUCCESS;
2251     };
2252 
2253     m_dumpBufferLegacy = [this](
2254                        PMOS_RESOURCE             resource,
2255                        const char               *attrName,
2256                        const char               *bufferName,
2257                        uint32_t                  size,
2258                        uint32_t                  offset,
2259                        CODECHAL_MEDIA_STATE_TYPE mediaState) {
2260         MEDIA_DEBUG_FUNCTION_ENTER;
2261 
2262         MEDIA_DEBUG_CHK_NULL(resource);
2263         MEDIA_DEBUG_CHK_NULL(bufferName);
2264 
2265         if (size == 0)
2266         {
2267             return MOS_STATUS_SUCCESS;
2268         }
2269 
2270         if (attrName)
2271         {
2272             bool attrEnabled = false;
2273 
2274             if (mediaState == CODECHAL_NUM_MEDIA_STATES)
2275             {
2276                 attrEnabled = m_configMgr->AttrIsEnabled(attrName);
2277             }
2278             else
2279             {
2280                 attrEnabled = static_cast<CodecDebugConfigMgr *>(m_configMgr)->AttrIsEnabled(mediaState, attrName);
2281             }
2282 
2283             if (!attrEnabled)
2284             {
2285                 return MOS_STATUS_SUCCESS;
2286             }
2287         }
2288 
2289         const char *fileName;
2290         bool        binaryDump = false;
2291         if ((!strcmp(attrName, MediaDbgAttr::attrDecodeBitstream)) ||
2292             (!strcmp(attrName, MediaDbgAttr::attrStreamIn)) ||
2293             (!strcmp(attrName, MediaDbgAttr::attrStreamOut)) ||
2294             (!strcmp(attrName, MediaDbgAttr::attrMvData)) ||
2295             (!strcmp(attrName, MediaDbgAttr::attrSegId)) ||
2296             (!strcmp(attrName, MediaDbgAttr::attrCoefProb)) ||
2297             m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary))
2298         {
2299             binaryDump = true;
2300         }
2301         const char *extType = binaryDump ? MediaDbgExtType::dat : MediaDbgExtType::txt;
2302 
2303         if (mediaState == CODECHAL_NUM_MEDIA_STATES)
2304         {
2305             fileName = CreateFileName(bufferName, attrName, extType);
2306         }
2307         else
2308         {
2309             std::string kernelName = static_cast<CodecDebugConfigMgr *>(m_configMgr)->GetMediaStateStr(mediaState);
2310             fileName               = CreateFileName(kernelName.c_str(), bufferName, extType);
2311         }
2312 
2313         MOS_STATUS status = MOS_STATUS_SUCCESS;
2314 
2315         if ((resource->pGmmResInfo != nullptr) &&
2316             (resource->pGmmResInfo->GetResFlags().Info.NotLockable))
2317         {
2318             uint8_t      *data            = nullptr;
2319             PMOS_RESOURCE pReadbackBuffer = (PMOS_RESOURCE)MOS_AllocAndZeroMemory(sizeof(MOS_RESOURCE));
2320             CODECHAL_DEBUG_CHK_NULL(pReadbackBuffer);
2321             status                        = AllocateLinearResource(MOS_GFXRES_BUFFER, (uint32_t)resource->pGmmResInfo->GetBaseWidth(), (uint32_t)resource->pGmmResInfo->GetBaseHeight(), Format_Any, pReadbackBuffer);
2322             if (MOS_FAILED(status))
2323             {
2324                 MOS_FreeMemAndSetNull(pReadbackBuffer);
2325             }
2326             CODECHAL_DEBUG_CHK_STATUS(status);
2327 
2328             status = VDBypassCopyResource(resource, pReadbackBuffer);
2329             if (MOS_FAILED(status))
2330             {
2331                 m_osInterface->pfnFreeResource(m_osInterface, pReadbackBuffer);
2332                 MOS_FreeMemAndSetNull(pReadbackBuffer);
2333             }
2334             CODECHAL_DEBUG_CHK_STATUS(status);
2335 
2336             MOS_LOCK_PARAMS lockFlags;
2337             MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
2338             lockFlags.ReadOnly = 1;
2339             data               = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, pReadbackBuffer, &lockFlags);
2340             if(data == nullptr)
2341             {
2342                 m_osInterface->pfnFreeResource(m_osInterface, pReadbackBuffer);
2343                 MOS_FreeMemAndSetNull(pReadbackBuffer);
2344             }
2345             CODECHAL_DEBUG_CHK_NULL(data);
2346 
2347             data += offset;
2348 
2349             if (binaryDump)
2350             {
2351                 status = DumpBufferInBinary(data, size);
2352             }
2353             else
2354             {
2355                 status = DumpBufferInHexDwords(data, size);
2356             }
2357 
2358             m_osInterface->pfnUnlockResource(m_osInterface, pReadbackBuffer);
2359             m_osInterface->pfnFreeResource(m_osInterface, pReadbackBuffer);
2360             MOS_FreeMemAndSetNull(pReadbackBuffer);
2361             CODECHAL_DEBUG_CHK_STATUS(status);
2362         }
2363         else
2364         {
2365             MOS_LOCK_PARAMS lockFlags;
2366             MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
2367             lockFlags.ReadOnly = 1;
2368             uint8_t *data      = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, resource, &lockFlags);
2369             MEDIA_DEBUG_CHK_NULL(data);
2370             data += offset;
2371 
2372             if (binaryDump)
2373             {
2374                 status = DumpBufferInBinary(data, size);
2375             }
2376             else
2377             {
2378                 status = DumpBufferInHexDwords(data, size);
2379             }
2380 
2381             if (data)
2382             {
2383                 m_osInterface->pfnUnlockResource(m_osInterface, resource);
2384             }
2385         }
2386 
2387         return status;
2388     };
2389 
2390     m_dumpBuffer = [this](
2391                                  PMOS_RESOURCE             resource,
2392                                  const char               *attrName,
2393                                  const char               *bufferName,
2394                                  uint32_t                  size,
2395                                  uint32_t                  offset,
2396                                  CODECHAL_MEDIA_STATE_TYPE mediaState) {
2397         MEDIA_DEBUG_FUNCTION_ENTER;
2398 
2399         MEDIA_DEBUG_CHK_NULL(resource);
2400         MEDIA_DEBUG_CHK_NULL(bufferName);
2401 
2402         if (size == 0)
2403         {
2404             return MOS_STATUS_SUCCESS;
2405         }
2406 
2407         bool attrEnabled = false;
2408         if (mediaState == CODECHAL_NUM_MEDIA_STATES)
2409         {
2410             attrEnabled = m_configMgr->AttrIsEnabled(attrName);
2411         }
2412         else
2413         {
2414             attrEnabled = static_cast<CodecDebugConfigMgr *>(m_configMgr)->AttrIsEnabled(mediaState, attrName);
2415         }
2416         if (!attrEnabled)
2417         {
2418             return MOS_STATUS_SUCCESS;
2419         }
2420 
2421         const char *fileName;
2422         bool        binaryDump = false;
2423         if ((!strcmp(attrName, MediaDbgAttr::attrDecodeBitstream)) ||
2424             (!strcmp(attrName, MediaDbgAttr::attrStreamIn)) ||
2425             (!strcmp(attrName, MediaDbgAttr::attrStreamOut)) ||
2426             (!strcmp(attrName, MediaDbgAttr::attrMvData)) ||
2427             (!strcmp(attrName, MediaDbgAttr::attrSegId)) ||
2428             (!strcmp(attrName, MediaDbgAttr::attrCoefProb)) ||
2429             m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary))
2430         {
2431             binaryDump = true;
2432         }
2433         const char *extType = binaryDump ? MediaDbgExtType::dat : MediaDbgExtType::txt;
2434 
2435         if (mediaState == CODECHAL_NUM_MEDIA_STATES)
2436         {
2437             fileName = CreateFileName(bufferName, attrName, extType);
2438         }
2439         else
2440         {
2441             std::string kernelName = static_cast<CodecDebugConfigMgr *>(m_configMgr)->GetMediaStateStr(mediaState);
2442             fileName               = CreateFileName(kernelName.c_str(), bufferName, extType);
2443         }
2444 
2445         MediaDebugFastDump::Dump(*resource, fileName, size, offset);
2446 
2447         if (!MediaDebugFastDump::IsGood())
2448         {
2449             return m_dumpBufferLegacy(
2450                 resource,
2451                 attrName,
2452                 bufferName,
2453                 size,
2454                 offset,
2455                 mediaState);
2456         }
2457 
2458         return MOS_STATUS_SUCCESS;
2459     };
2460 }
2461 
~CodechalDebugInterface()2462 CodechalDebugInterface::~CodechalDebugInterface()
2463 {
2464     if (nullptr != m_configMgr)
2465     {
2466         MOS_Delete(m_configMgr);
2467     }
2468 
2469     MediaDebugFastDump::DestroyInstance();
2470 }
2471 
Create()2472 CodechalDebugInterface* CodechalDebugInterface::Create()
2473 {
2474     return MOS_New(CodechalDebugInterface);
2475 }
2476 
CheckGoldenReferenceExist()2477 void CodechalDebugInterface::CheckGoldenReferenceExist()
2478 {
2479     std::ifstream crcGoldenRefStream(m_crcGoldenRefFileName);
2480     m_goldenReferenceExist = crcGoldenRefStream.good() ? true : false;
2481 }
2482 
DumpRgbDataOnYUVSurface(PMOS_SURFACE surface,const char * attrName,const char * surfName,CODECHAL_MEDIA_STATE_TYPE mediaState,uint32_t width_in,uint32_t height_in)2483 MOS_STATUS CodechalDebugInterface::DumpRgbDataOnYUVSurface(
2484     PMOS_SURFACE              surface,
2485     const char               *attrName,
2486     const char               *surfName,
2487     CODECHAL_MEDIA_STATE_TYPE mediaState,
2488     uint32_t                  width_in,
2489     uint32_t                  height_in)
2490 {
2491     bool     hasAuxSurf   = false;
2492     bool     isPlanar     = true;
2493     bool     hasRefSurf   = false;
2494     uint8_t *surfBaseAddr = nullptr;
2495     uint8_t *lockedAddr   = nullptr;
2496 
2497     if (!DumpIsEnabled(attrName, mediaState))
2498     {
2499         return MOS_STATUS_SUCCESS;
2500     }
2501 
2502     //To write RGB data on AYUV surface for validation purpose due to similiar layout
2503     int32_t sfcOutputRgbFormatFlag = 0;
2504     int32_t bIsSfcOutputLinearFlag = 0;
2505     //tile type read from reg key
2506     {
2507         MediaUserSetting::Value outValue;
2508         ReadUserSettingForDebug(
2509             m_userSettingPtr,
2510             outValue,
2511             __MEDIA_USER_FEATURE_VALUE_DECODE_SFC_RGBFORMAT_OUTPUT_DEBUG,
2512             MediaUserSetting::Group::Sequence);
2513         sfcOutputRgbFormatFlag = outValue.Get<int32_t>();
2514     }
2515 
2516     //rgb format output read from reg key
2517     {
2518         MediaUserSetting::Value outValue;
2519         ReadUserSettingForDebug(
2520             m_userSettingPtr,
2521             outValue,
2522             __MEDIA_USER_FEATURE_VALUE_DECODE_SFC_LINEAR_OUTPUT_DEBUG,
2523             MediaUserSetting::Group::Sequence);
2524         bIsSfcOutputLinearFlag = outValue.Get<int32_t>();
2525     }
2526 
2527     if (!sfcOutputRgbFormatFlag)
2528     {
2529         return MOS_STATUS_SUCCESS;
2530     }
2531 
2532     MOS_FORMAT  outputFormat = surface->Format;
2533     const char *filePostfix  = "unsupport";
2534 
2535     switch (sfcOutputRgbFormatFlag)
2536     {
2537     case 1:  //RGBP
2538         outputFormat = Format_RGBP;
2539         filePostfix  = "rgbp";
2540         break;
2541     case 2:  //BGRP
2542         outputFormat = Format_BGRP;
2543         filePostfix  = "bgrp";
2544         break;
2545     case 3:  //RGB24 only support Tile Linear
2546         if (bIsSfcOutputLinearFlag)
2547         {
2548             outputFormat = Format_R8G8B8;
2549             filePostfix  = "rgb24";
2550             break;
2551         }
2552     default:
2553         CODECHAL_DEBUG_ASSERTMESSAGE("unsupport rgb format");
2554         return MOS_STATUS_SUCCESS;
2555     }
2556 
2557     MOS_TILE_TYPE outputTileType = MOS_TILE_Y;
2558     if (bIsSfcOutputLinearFlag == 1)
2559     {
2560         outputTileType = MOS_TILE_LINEAR;
2561     }
2562 
2563     MOS_LOCK_PARAMS   lockFlags;
2564     GMM_RESOURCE_FLAG gmmFlags;
2565 
2566     MOS_ZeroMemory(&gmmFlags, sizeof(gmmFlags));
2567     CODECHAL_DEBUG_CHK_NULL(surface);
2568     gmmFlags   = surface->OsResource.pGmmResInfo->GetResFlags();
2569     hasAuxSurf = (gmmFlags.Gpu.MMC || gmmFlags.Info.MediaCompressed) && gmmFlags.Gpu.UnifiedAuxSurface;
2570 
2571     if (!m_configMgr->AttrIsEnabled(CodechalDbgAttr::attrDecodeAuxSurface))
2572     {
2573         hasAuxSurf = false;
2574     }
2575 
2576     if (strcmp(attrName, CodechalDbgAttr::attrDecodeReferenceSurfaces) == 0)
2577     {
2578         hasRefSurf = true;
2579     }
2580 
2581     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
2582     lockFlags.ReadOnly     = 1;
2583     lockFlags.TiledAsTiled = 1;  // Bypass GMM CPU blit due to some issues in GMM CpuBlt function
2584     if (hasAuxSurf)
2585     {
2586         // Dump MMC surface as raw layout
2587         lockFlags.NoDecompress = 1;
2588     }
2589 
2590     lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &surface->OsResource, &lockFlags);
2591 
2592     if (lockedAddr == nullptr)  // Failed to lock. Try to submit copy task and dump another surface
2593     {
2594         uint32_t        sizeToBeCopied = 0;
2595         MOS_GFXRES_TYPE ResType;
2596 
2597 #if LINUX
2598         // Linux does not have OsResource->ResType
2599         ResType = surface->Type;
2600 #else
2601         ResType = surface->OsResource.ResType;
2602 #endif
2603 
2604         CODECHAL_DEBUG_CHK_STATUS(ReAllocateSurface(
2605             &m_temp2DSurfForCopy,
2606             surface,
2607             "Temp2DSurfForSurfDumper",
2608             ResType));
2609 
2610         if (!hasRefSurf)
2611         {
2612             uint32_t arraySize = surface->OsResource.pGmmResInfo->GetArraySize();
2613 
2614             if (arraySize == 0)
2615             {
2616                 return MOS_STATUS_UNKNOWN;
2617             }
2618 
2619             uint32_t sizeSrcSurface = (uint32_t)(surface->OsResource.pGmmResInfo->GetSizeMainSurface()) / arraySize;
2620             // Ensure allocated buffer size contains the source surface size
2621             if (m_temp2DSurfForCopy.OsResource.pGmmResInfo->GetSizeMainSurface() >= sizeSrcSurface)
2622             {
2623                 sizeToBeCopied = sizeSrcSurface;
2624             }
2625 
2626             if (sizeToBeCopied == 0)
2627             {
2628                 // Currently, MOS's pfnAllocateResource does not support allocate a surface reference to another surface.
2629                 // When the source surface is not created from Media, it is possible that we cannot allocate the same size as source.
2630                 // For example, on Gen9, Render target might have GMM set CCS=1 MMC=0, but MOS cannot allocate surface with such combination.
2631                 // When Gmm allocation parameter is different, the resulting surface size/padding/pitch will be differnt.
2632                 // Once if MOS can support allocate a surface by reference another surface, we can do a bit to bit copy without problem.
2633                 CODECHAL_DEBUG_ASSERTMESSAGE("Cannot allocate correct size, failed to copy nonlockable resource");
2634                 return MOS_STATUS_NULL_POINTER;
2635             }
2636 
2637             CODECHAL_DEBUG_VERBOSEMESSAGE("Temp2DSurfaceForCopy width %d, height %d, pitch %d, TileType %d, bIsCompressed %d, CompressionMode %d",
2638                 m_temp2DSurfForCopy.dwWidth,
2639                 m_temp2DSurfForCopy.dwHeight,
2640                 m_temp2DSurfForCopy.dwPitch,
2641                 m_temp2DSurfForCopy.TileType,
2642                 m_temp2DSurfForCopy.bIsCompressed,
2643                 m_temp2DSurfForCopy.CompressionMode);
2644 
2645             uint32_t bpp = surface->OsResource.pGmmResInfo->GetBitsPerPixel();
2646 
2647             CODECHAL_DEBUG_CHK_STATUS(m_osInterface->pfnMediaCopyResource2D(m_osInterface, &surface->OsResource, &m_temp2DSurfForCopy.OsResource, surface->dwPitch, sizeSrcSurface / (surface->dwPitch), bpp, false));
2648         }
2649         else
2650         {
2651             CODECHAL_DEBUG_CHK_STATUS(m_osInterface->pfnDoubleBufferCopyResource(m_osInterface, &surface->OsResource, &m_temp2DSurfForCopy.OsResource, false));
2652         }
2653 
2654         lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &m_temp2DSurfForCopy.OsResource, &lockFlags);
2655         CODECHAL_DEBUG_CHK_NULL(lockedAddr);
2656 
2657         surface  = &m_temp2DSurfForCopy;
2658         gmmFlags = surface->OsResource.pGmmResInfo->GetResFlags();
2659     }
2660 
2661     if (hasAuxSurf)
2662     {
2663         uint32_t sizeMain = (uint32_t)(surface->OsResource.pGmmResInfo->GetSizeMainSurface());
2664         surfBaseAddr      = (uint8_t *)MOS_AllocMemory(sizeMain);
2665         CODECHAL_DEBUG_CHK_NULL(surfBaseAddr);
2666 
2667         Mos_SwizzleData(lockedAddr, surfBaseAddr, surface->TileType, MOS_TILE_LINEAR, sizeMain / surface->dwPitch, surface->dwPitch, true);
2668     }
2669     else
2670     {
2671         surfBaseAddr = lockedAddr;
2672     }
2673 
2674     // Allocate RGB surface internally to get surface info
2675     MOS_SURFACE *tmpRgbSurface = MOS_New(MOS_SURFACE);
2676     CODECHAL_DEBUG_CHK_NULL(tmpRgbSurface);
2677     MOS_ALLOC_GFXRES_PARAMS allocParams;
2678     MOS_ZeroMemory(&allocParams, sizeof(MOS_ALLOC_GFXRES_PARAMS));
2679     allocParams.Type              = MOS_GFXRES_2D;
2680     allocParams.TileType          = outputTileType;
2681     allocParams.Format            = outputFormat;
2682     allocParams.dwWidth           = surface->dwWidth;
2683     allocParams.dwHeight          = surface->dwHeight;
2684     allocParams.dwArraySize       = 1;
2685     allocParams.pBufName          = "dump rgb surface";
2686     allocParams.bIsCompressible   = false;
2687     allocParams.ResUsageType      = MOS_HW_RESOURCE_DEF_MAX;
2688     allocParams.m_tileModeByForce = MOS_TILE_UNSET_GMM;
2689 
2690     CODECHAL_DEBUG_CHK_STATUS(m_osInterface->pfnAllocateResource(
2691         m_osInterface,
2692         &allocParams,
2693         &tmpRgbSurface->OsResource));
2694 
2695     tmpRgbSurface->Format = Format_Invalid;
2696     CODECHAL_DEBUG_CHK_STATUS(m_osInterface->pfnGetResourceInfo(m_osInterface, &tmpRgbSurface->OsResource, tmpRgbSurface));
2697 
2698     uint32_t rgbPitch      = tmpRgbSurface->dwPitch;
2699     uint32_t alignedHeight = surface->dwHeight;
2700     uint32_t alignedWidth  = surface->dwWidth;
2701 
2702     if (outputFormat == Format_RGBP || outputFormat == Format_BGRP && rgbPitch)
2703     {
2704         alignedHeight = tmpRgbSurface->RenderOffset.YUV.U.BaseOffset / rgbPitch;
2705         alignedWidth  = rgbPitch;
2706     }
2707 
2708     // Always use MOS swizzle instead of GMM Cpu blit
2709     uint32_t sizeMain = (uint32_t)(surface->OsResource.pGmmResInfo->GetSizeMainSurface());
2710     surfBaseAddr      = (uint8_t *)MOS_AllocMemory(sizeMain);
2711     CODECHAL_DEBUG_CHK_NULL(surfBaseAddr);
2712 
2713     int32_t  swizzleHeight = sizeMain / surface->dwPitch;
2714     int32_t  swizzlePitch  = surface->dwPitch;
2715     uint8_t *data          = lockedAddr;
2716 
2717     CODECHAL_DEBUG_VERBOSEMESSAGE("RGB Surface info: format %d, tiletype %d, pitch %d,UOffset %d, dwSize %d, sizemain %d",
2718         outputFormat,
2719         tmpRgbSurface->TileType,
2720         tmpRgbSurface->dwPitch,
2721         tmpRgbSurface->RenderOffset.YUV.U.BaseOffset,
2722         tmpRgbSurface->dwSize,
2723         sizeMain);
2724 
2725     if (outputTileType == MOS_TILE_Y)
2726     {
2727         swizzlePitch = rgbPitch;
2728         Mos_SwizzleData(lockedAddr, surfBaseAddr, outputTileType, MOS_TILE_LINEAR, sizeMain / swizzlePitch, swizzlePitch, !MEDIA_IS_SKU(m_osInterface->pfnGetSkuTable(m_osInterface), FtrTileY) || gmmFlags.Info.Tile4);
2729         data = surfBaseAddr;
2730     }
2731 
2732     data += surface->dwOffset + surface->YPlaneOffset.iYOffset * surface->dwPitch;
2733 
2734     uint32_t width      = (width_in ? width_in : surface->dwWidth) << 2;
2735     uint32_t height     = height_in ? height_in : surface->dwHeight;
2736     uint32_t lumaheight = 0;
2737     uint32_t pitch      = surface->dwPitch;
2738 
2739     lumaheight = hasAuxSurf ? GFX_ALIGN(height, 32) : height;
2740 
2741     const char *funcName = (m_codecFunction == CODECHAL_FUNCTION_DECODE) ? "_DEC" : (m_codecFunction == CODECHAL_FUNCTION_CENC_DECODE ? "_DEC" : "_ENC");
2742     std::string bufName  = std::string(surfName) + "_w[" + std::to_string(alignedWidth) + "]_h[" + std::to_string(alignedHeight) + "]_p[" + std::to_string(rgbPitch) + "]";
2743 
2744     const char *filePath = CreateFileName(funcName, bufName.c_str(), filePostfix);
2745 
2746     std::ofstream ofs(filePath, std::ios_base::out | std::ios_base::binary);
2747     if (ofs.fail())
2748     {
2749         return MOS_STATUS_UNKNOWN;
2750     }
2751 
2752     // RGB data has one zero byte in every 64 bytes, need to remove this byte
2753     if (IS_RGB24_FORMAT(outputFormat))
2754     {
2755         for (uint32_t h = 0; h < lumaheight; h++)
2756         {
2757             width                 = surface->dwWidth * 3;
2758             int dummyBytesPerLine = width / 63;
2759             int resPixel          = width - 63 * dummyBytesPerLine;
2760             int resBytesPerLine   = rgbPitch;
2761             for (int p = 0; p < dummyBytesPerLine; p++)
2762             {
2763                 ofs.write((char *)data, 63);
2764                 data += 64;
2765                 resBytesPerLine -= 64;
2766             }
2767 
2768             if (resPixel > 0)
2769             {
2770                 ofs.write((char *)data, resPixel);
2771                 data += resPixel;
2772                 resBytesPerLine -= resPixel;
2773             }
2774 
2775             if (resBytesPerLine)
2776             {
2777                 data += resBytesPerLine;
2778             }
2779         }
2780     }
2781     else
2782     {
2783         for (uint32_t h = 0; h < lumaheight; h++)
2784         {
2785             ofs.write((char *)data, hasAuxSurf ? pitch : width);
2786             data += pitch;
2787         }
2788     }
2789 
2790     if (surfBaseAddr)
2791     {
2792         MOS_FreeMemory(surfBaseAddr);
2793     }
2794     if (lockedAddr)
2795     {
2796         m_osInterface->pfnUnlockResource(m_osInterface, &surface->OsResource);
2797     }
2798 
2799     m_osInterface->pfnFreeResource(m_osInterface, &(tmpRgbSurface->OsResource));
2800     MOS_Delete(tmpRgbSurface);
2801 
2802     return MOS_STATUS_SUCCESS;
2803 }
2804 
DumpBltOutput(PMOS_SURFACE surface,const char * attrName)2805 MOS_STATUS CodechalDebugInterface::DumpBltOutput(
2806     PMOS_SURFACE surface,
2807     const char  *attrName)
2808 {
2809     if (!DumpIsEnabled(attrName))
2810     {
2811         return MOS_STATUS_SUCCESS;
2812     }
2813     else
2814     {
2815         CODECHAL_DEBUG_NORMALMESSAGE("Don't support BltOutput dump, pls not use BltState directly!");
2816         return MOS_STATUS_SUCCESS;
2817     }
2818 
2819 }
2820 
InitializeUserSetting()2821 MOS_STATUS CodechalDebugInterface::InitializeUserSetting()
2822 {
2823     CODECHAL_DEBUG_FUNCTION_ENTER;
2824     CODECHAL_DEBUG_CHK_STATUS(MediaDebugInterface::InitializeUserSetting());
2825 
2826     DeclareUserSettingKeyForDebug(
2827         m_userSettingPtr,
2828         __MEDIA_USER_FEATURE_ENABLE_HW_DEBUG_HOOKS_DEBUG,
2829         MediaUserSetting::Group::Device,
2830         int32_t(0),
2831         false);
2832 
2833     DeclareUserSettingKeyForDebug(
2834         m_userSettingPtr,
2835         __MEDIA_USER_FEATURE_VALUE_CODECHAL_FRAME_NUMBER_TO_STOP_DEBUG,
2836         MediaUserSetting::Group::Device,
2837         int32_t(-1),
2838         false);
2839 
2840     DeclareUserSettingKeyForDebug(
2841         m_userSettingPtr,
2842         __MEDIA_USER_FEATURE_VALUE_CODECHAL_ENABLE_SW_CRC_DEBUG,
2843         MediaUserSetting::Group::Device,
2844         uint32_t(0),
2845         false);
2846 
2847     return MOS_STATUS_SUCCESS;
2848 }
2849 
Initialize(CodechalHwInterfaceNext * hwInterface,CODECHAL_FUNCTION codecFunction,MediaCopyWrapper * mediaCopyWrapper)2850 MOS_STATUS CodechalDebugInterface::Initialize(
2851     CodechalHwInterfaceNext *hwInterface,
2852     CODECHAL_FUNCTION        codecFunction,
2853     MediaCopyWrapper        *mediaCopyWrapper)
2854 {
2855     CODECHAL_DEBUG_FUNCTION_ENTER;
2856 
2857     CODECHAL_DEBUG_CHK_NULL(hwInterface);
2858     m_hwInterfaceNext  = hwInterface;
2859     m_codecFunction    = codecFunction;
2860     m_osInterface      = m_hwInterfaceNext->GetOsInterface();
2861     m_cpInterface      = m_hwInterfaceNext->GetCpInterface();
2862 
2863     CODECHAL_DEBUG_CHK_NULL(m_osInterface);
2864     m_userSettingPtr = m_osInterface->pfnGetUserSettingInstance(m_osInterface);
2865     CODECHAL_DEBUG_CHK_STATUS(InitializeUserSetting());
2866 
2867     //dump loctaion is codechaldump
2868     MediaDebugInterface::SetOutputFilePath();
2869 
2870     m_configMgr = MOS_New(CodecDebugConfigMgr, this, m_codecFunction, m_outputFilePath);
2871     CODECHAL_DEBUG_CHK_NULL(m_configMgr);
2872     CODECHAL_DEBUG_CHK_STATUS(m_configMgr->ParseConfig(m_osInterface->pOsContext));
2873 
2874     MediaDebugInterface::InitDumpLocation();
2875 
2876     m_osInterface->pfnGetPlatform(m_osInterface, &m_platform);
2877 
2878 #if (_DEBUG || _RELEASE_INTERNAL)
2879     {
2880         MediaUserSetting::Value outValue;
2881         ReadUserSettingForDebug(
2882             m_userSettingPtr,
2883             outValue,
2884             __MEDIA_USER_FEATURE_ENABLE_HW_DEBUG_HOOKS_DEBUG,
2885             MediaUserSetting::Group::Device,
2886             0,
2887             true);
2888         m_enableHwDebugHooks = outValue.Get<bool>();
2889     }
2890     CheckGoldenReferenceExist();
2891     if (m_enableHwDebugHooks && m_goldenReferenceExist)
2892     {
2893         LoadGoldenReference();
2894     }
2895 
2896     {
2897         MediaUserSetting::Value outValue;
2898         ReadUserSettingForDebug(
2899             m_userSettingPtr,
2900             outValue,
2901             __MEDIA_USER_FEATURE_VALUE_CODECHAL_FRAME_NUMBER_TO_STOP_DEBUG,
2902             MediaUserSetting::Group::Device,
2903             -1,
2904             true);
2905         m_stopFrameNumber = outValue.Get<int32_t>();
2906     }
2907 
2908     {
2909         MediaUserSetting::Value outValue;
2910         ReadUserSettingForDebug(
2911             m_userSettingPtr,
2912             outValue,
2913             __MEDIA_USER_FEATURE_VALUE_CODECHAL_ENABLE_SW_CRC_DEBUG,
2914             MediaUserSetting::Group::Device,
2915             0,
2916             true);
2917         m_swCRC = outValue.Get<bool>();
2918     }
2919 #endif
2920 
2921     SetFastDumpConfig(mediaCopyWrapper);
2922 
2923     return MOS_STATUS_SUCCESS;
2924 }
2925 
DumpHucDmem(PMOS_RESOURCE dmemResource,uint32_t dmemSize,uint32_t hucPassNum,CodechalHucRegionDumpType dumpType)2926 MOS_STATUS CodechalDebugInterface::DumpHucDmem(
2927     PMOS_RESOURCE             dmemResource,
2928     uint32_t                  dmemSize,
2929     uint32_t                  hucPassNum,
2930     CodechalHucRegionDumpType dumpType)
2931 {
2932     CODECHAL_DEBUG_FUNCTION_ENTER;
2933 
2934     if (!m_configMgr->AttrIsEnabled(MediaDbgAttr::attrHuCDmem) && !MosUtilities::GetTraceSetting())
2935     {
2936         return MOS_STATUS_SUCCESS;
2937     }
2938 
2939     CODECHAL_DEBUG_CHK_NULL(dmemResource);
2940     if (Mos_ResourceIsNull(dmemResource))
2941     {
2942         return MOS_STATUS_NULL_POINTER;
2943     }
2944 
2945     std::string funcName = "";
2946     if (m_codecFunction == CODECHAL_FUNCTION_DECODE)
2947     {
2948         funcName = "DEC_";
2949     }
2950     else if (m_codecFunction == CODECHAL_FUNCTION_CENC_DECODE)
2951     {
2952         funcName = "DEC_Cenc_";
2953     }
2954     else
2955     {
2956         funcName = "ENC_";
2957     }
2958 
2959     std::string dmemName = MediaDbgBufferType::bufHucDmem;
2960     std::string passName = std::to_string(hucPassNum);
2961     switch (dumpType)
2962     {
2963     case hucRegionDumpInit:
2964         funcName = funcName + dmemName + "_InitPass" + passName;
2965         break;
2966     case hucRegionDumpUpdate:
2967         funcName = funcName + dmemName + "_UpdatePass" + passName;
2968         break;
2969     case hucRegionDumpLAUpdate:
2970         funcName = funcName + dmemName + "_LookaheadUpdatePass" + passName;
2971         break;
2972     case hucRegionDumpRegionLocked:
2973         funcName = funcName + dmemName + "_RegionLocked" + passName;
2974         break;
2975     case hucRegionDumpCmdInitializer:
2976         funcName = funcName + dmemName + "_CmdInitializerPass" + passName;
2977         break;
2978     case hucRegionDumpPakIntegrate:
2979         funcName = funcName + dmemName + "_PakIntPass" + passName;
2980         break;
2981     case hucRegionDumpHpu:
2982         funcName = funcName + dmemName + "_HpuPass" + passName;
2983         break;
2984     case hucRegionDumpHpuSuperFrame:
2985         funcName = funcName + dmemName + "_HpuPass" + passName + "_SuperFramePass";
2986         break;
2987     case hucRegionDumpBackAnnotation:
2988         funcName = funcName + dmemName + "_BackAnnotationPass" + passName;
2989         break;
2990     default:
2991         funcName = funcName + dmemName + "_Pass" + passName;
2992         break;
2993     }
2994 
2995     return DumpBuffer(dmemResource, MediaDbgAttr::attrHuCDmem, funcName.c_str(), dmemSize);
2996 }
2997 
SetOutputPathKey()2998 std::string CodechalDebugInterface::SetOutputPathKey()
2999 {
3000     return __MEDIA_USER_FEATURE_VALUE_CODECHAL_DEBUG_OUTPUT_DIRECTORY;
3001 }
3002 
InitDefaultOutput()3003 std::string CodechalDebugInterface::InitDefaultOutput()
3004 {
3005     m_outputFilePath.append(MEDIA_DEBUG_CODECHAL_DUMP_OUTPUT_FOLDER);
3006     return SetOutputPathKey();
3007 }
3008 
VDBypassCopyResource(PMOS_RESOURCE src,PMOS_RESOURCE dst)3009 MOS_STATUS CodechalDebugInterface::VDBypassCopyResource(PMOS_RESOURCE src, PMOS_RESOURCE dst)
3010 {
3011     CODECHAL_DEBUG_FUNCTION_ENTER;
3012 
3013     CODECHAL_DEBUG_CHK_NULL(m_osInterface);
3014     CODECHAL_DEBUG_CHK_NULL(m_hwInterfaceNext)
3015     CODECHAL_DEBUG_CHK_NULL(src);
3016     CODECHAL_DEBUG_CHK_NULL(dst);
3017 
3018     MOS_STATUS                       eStatus = MOS_STATUS_SUCCESS;
3019     MOS_GPUCTX_CREATOPTIONS_ENHANCED createOption;
3020     MOS_COMMAND_BUFFER               cmdBuffer;
3021     std::shared_ptr<mhw::mi::Itf>    miInterface    = m_hwInterfaceNext->GetMiInterfaceNext();
3022     PMHW_MI_MMIOREGISTERS            pMmioRegisters = miInterface->GetMmioRegisters();
3023     MOS_CONTEXT                     *pOsContext     = m_osInterface->pOsContext;
3024     MHW_ADD_CP_COPY_PARAMS           cpCopyParams;
3025     MHW_MI_FLUSH_DW_PARAMS           FlushDwParams;
3026     MhwCpInterface                  *mhwCpInterface = m_hwInterfaceNext->GetCpInterface();
3027 
3028     CODECHAL_DEBUG_CHK_NULL(pMmioRegisters);
3029     CODECHAL_DEBUG_CHK_NULL(pOsContext);
3030     CODECHAL_DEBUG_CHK_NULL(mhwCpInterface);
3031 
3032     CODECHAL_DEBUG_CHK_STATUS(m_osInterface->pfnGetCommandBuffer(m_osInterface, &cmdBuffer, 0));
3033 
3034     CODECHAL_DEBUG_CHK_STATUS(miInterface->AddProtectedProlog(&cmdBuffer));
3035 
3036     MOS_ZeroMemory(&cpCopyParams, sizeof(cpCopyParams));
3037     uint32_t dwWidth     = (uint32_t)src->pGmmResInfo->GetBaseWidth();
3038     uint32_t dwHeight    = (uint32_t)src->pGmmResInfo->GetBaseHeight();
3039     cpCopyParams.size    = dwWidth * dwHeight;
3040     cpCopyParams.presSrc = src;
3041     cpCopyParams.presDst = dst;
3042     cpCopyParams.offset  = 0;
3043     cpCopyParams.bypass  = true;
3044 
3045     eStatus = mhwCpInterface->AddCpCopy(m_osInterface, &cmdBuffer, &cpCopyParams);
3046 
3047     auto &flushDwParams = miInterface->MHW_GETPAR_F(MI_FLUSH_DW)();
3048     flushDwParams       = {};
3049     CODECHAL_DEBUG_CHK_STATUS(miInterface->MHW_ADDCMD_F(MI_FLUSH_DW)(&cmdBuffer));
3050     PMOS_RESOURCE pStoreResource = (PMOS_RESOURCE)MOS_AllocAndZeroMemory(sizeof(MOS_RESOURCE));
3051     CODECHAL_DEBUG_CHK_NULL(pStoreResource);
3052     // Create store resource
3053     {
3054         //Insert MFX_WAIT command.
3055         auto &mfxWaitParams               = miInterface->MHW_GETPAR_F(MFX_WAIT)();
3056         mfxWaitParams                     = {};
3057         mfxWaitParams.iStallVdboxPipeline = true;
3058         CODECHAL_DEBUG_CHK_STATUS(miInterface->MHW_ADDCMD_F(MFX_WAIT)(&cmdBuffer, nullptr));
3059 
3060         eStatus = AllocateLinearResource(MOS_GFXRES_BUFFER, sizeof(uint32_t), 1, Format_Buffer, pStoreResource, true);
3061         if (MOS_FAILED(eStatus))
3062         {
3063             MOS_FreeMemAndSetNull(pStoreResource);
3064         }
3065         CODECHAL_DEBUG_CHK_STATUS(eStatus);
3066 
3067         eStatus = FillResourceMemory(*pStoreResource, sizeof(uint32_t), QUERY_STATUS_BUFFER_INITIAL_VALUE);
3068         if (MOS_FAILED(eStatus))
3069         {
3070             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed FillResourceMemory to pStoreResource.");
3071             m_osInterface->pfnFreeResource(m_osInterface, pStoreResource);
3072             MOS_FreeMemAndSetNull(pStoreResource);
3073         }
3074         CODECHAL_DEBUG_CHK_STATUS(eStatus);
3075 
3076         auto &flushDwParams             = miInterface->MHW_GETPAR_F(MI_FLUSH_DW)();
3077         flushDwParams                   = {};
3078         flushDwParams.postSyncOperation = MHW_FLUSH_WRITE_IMMEDIATE_DATA;
3079         flushDwParams.pOsResource       = pStoreResource;
3080         flushDwParams.dwResourceOffset  = 0;
3081         flushDwParams.dwDataDW1         = STORE_DATA_DWORD_DATA;
3082         eStatus = miInterface->MHW_ADDCMD_F(MI_FLUSH_DW)(&cmdBuffer);
3083         if (MOS_FAILED(eStatus))
3084         {
3085             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed MHW_ADDCMD_F(MI_FLUSH_DW).");
3086             m_osInterface->pfnFreeResource(m_osInterface, pStoreResource);
3087             MOS_FreeMemAndSetNull(pStoreResource);
3088         }
3089     }
3090 
3091     do
3092     {
3093         auto &batchBufferEndParams = miInterface->MHW_GETPAR_F(MI_BATCH_BUFFER_END)();
3094         batchBufferEndParams       = {};
3095         eStatus = miInterface->MHW_ADDCMD_F(MI_BATCH_BUFFER_END)(&cmdBuffer);
3096         if (MOS_FAILED(eStatus))
3097         {
3098             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed MHW_ADDCMD_F(MI_BATCH_BUFFER_END).");
3099             break;
3100         }
3101         m_osInterface->pfnReturnCommandBuffer(m_osInterface, &cmdBuffer, 0);
3102         eStatus                           = m_osInterface->pfnSubmitCommandBuffer(m_osInterface, &cmdBuffer, false);
3103         if (MOS_FAILED(eStatus))
3104         {
3105             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed MHW_ADDCMD_F(MI_BATCH_BUFFER_END).");
3106             break;
3107         }
3108 
3109         volatile uint32_t dwValueInMemory = 0;
3110         MOS_LOCK_PARAMS sMosLockParams{};
3111         sMosLockParams.ReadOnly = 1;
3112         uint8_t *pStoreResBytes = reinterpret_cast<uint8_t*>(m_osInterface->pfnLockResource(m_osInterface, pStoreResource, &sMosLockParams));
3113         if (pStoreResBytes == nullptr)
3114         {
3115             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed locking pStoreResource.");
3116             eStatus = MOS_STATUS_UNINITIALIZED;
3117             break;
3118         }
3119 
3120         dwValueInMemory     = *(reinterpret_cast<uint32_t *>(pStoreResBytes));
3121         if (dwValueInMemory != STORE_DATA_DWORD_DATA)
3122         {
3123             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed to read Store Data output.");
3124             m_osInterface->pfnUnlockResource(m_osInterface, pStoreResource);
3125             eStatus = MOS_STATUS_SUCCESS;
3126             break;
3127         }
3128         m_osInterface->pfnUnlockResource(m_osInterface, pStoreResource);
3129 
3130     } while (false);
3131     m_osInterface->pfnFreeResource(m_osInterface, pStoreResource);
3132     MOS_FreeMemAndSetNull(pStoreResource);
3133 
3134     return eStatus;
3135 }
3136 
AllocateLinearResource(MOS_GFXRES_TYPE eType,uint32_t dwWidth,uint32_t dwHeight,MOS_FORMAT eFormat,PMOS_RESOURCE pResource,bool bSystemMem,bool bLockable) const3137 MOS_STATUS CodechalDebugInterface::AllocateLinearResource(MOS_GFXRES_TYPE eType, uint32_t dwWidth, uint32_t dwHeight, MOS_FORMAT eFormat, PMOS_RESOURCE pResource, bool bSystemMem, bool bLockable) const
3138 {
3139     MOS_ALLOC_GFXRES_PARAMS sMosAllocParams;
3140     MOS_STATUS              eStatus;
3141 
3142     CODECHAL_DEBUG_FUNCTION_ENTER;
3143 
3144     CODECHAL_DEBUG_CHK_NULL(m_osInterface);
3145     CODECHAL_DEBUG_CHK_NULL(m_osInterface->pfnAllocateResource);
3146 
3147     do
3148     {
3149         if (eType != MOS_GFXRES_BUFFER &&
3150             eType != MOS_GFXRES_2D)
3151         {
3152             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Only 'BUFFER' and '2D' types are supported in CP.");
3153             eStatus = MOS_STATUS_INVALID_PARAMETER;
3154             break;
3155         }
3156 
3157         if (eType == MOS_GFXRES_BUFFER &&
3158             dwHeight != 1)
3159         {
3160             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Resources of type 'BUFFER' must have height = 1.");
3161             eStatus = MOS_STATUS_INVALID_PARAMETER;
3162             break;
3163         }
3164 
3165         if (pResource == nullptr)
3166         {
3167             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed to allocate memory for MOS_RESOURCE.");
3168             eStatus = MOS_STATUS_NO_SPACE;
3169             break;
3170         }
3171         MOS_ZeroMemory(pResource, sizeof(*pResource));
3172 
3173         MOS_ZeroMemory(&sMosAllocParams, sizeof(sMosAllocParams));
3174         sMosAllocParams.Type     = eType;
3175         sMosAllocParams.dwWidth  = dwWidth;
3176         sMosAllocParams.dwHeight = dwHeight;
3177         sMosAllocParams.TileType = MOS_TILE_LINEAR;
3178         sMosAllocParams.Format   = eFormat;
3179 
3180         if (MEDIA_IS_SKU(m_osInterface->pfnGetSkuTable(m_osInterface), FtrLimitedLMemBar))
3181         {
3182             if (bSystemMem)
3183             {
3184                 sMosAllocParams.dwMemType = MOS_MEMPOOL_SYSTEMMEMORY;
3185             }
3186             else if (!bLockable)
3187             {
3188                 sMosAllocParams.Flags.bNotLockable = 1;
3189                 sMosAllocParams.dwMemType          = MOS_MEMPOOL_DEVICEMEMORY;
3190             }
3191             else if (bLockable)
3192             {
3193                 sMosAllocParams.Flags.bNotLockable = 0;
3194                 sMosAllocParams.dwMemType          = MOS_MEMPOOL_VIDEOMEMORY;
3195             }
3196         }
3197 
3198         if (MEDIA_IS_SKU(m_osInterface->pfnGetSkuTable(m_osInterface), FtrSAMediaCachePolicy))
3199         {
3200             sMosAllocParams.ResUsageType = MOS_HW_RESOURCE_USAGE_CP_INTERNAL_WRITE;
3201         }
3202 
3203         eStatus = m_osInterface->pfnAllocateResource(
3204             m_osInterface,
3205             &sMosAllocParams,
3206             pResource);
3207         if (MOS_FAILED(eStatus))
3208         {
3209             CODECHAL_DEBUG_ASSERTMESSAGE("Failed to allocate resource. Error = 0x%x.", eStatus);
3210             eStatus = MOS_STATUS_UNKNOWN;
3211         }
3212     } while (false);
3213 
3214     return eStatus;
3215 }
3216 
FillResourceMemory(MOS_RESOURCE & sResource,uint32_t uiSize,uint8_t ucValue)3217 MOS_STATUS CodechalDebugInterface::FillResourceMemory(MOS_RESOURCE &sResource, uint32_t uiSize, uint8_t ucValue)
3218 {
3219     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
3220     void      *pSurface = nullptr;
3221 
3222     CODECHAL_DEBUG_FUNCTION_ENTER;
3223 
3224     if (uiSize == 0)
3225     {
3226         // Nothing to do.
3227         CODECHAL_DEBUG_ASSERTMESSAGE("Received uiSize = 0. This should never happen!");
3228         return MOS_STATUS_INVALID_PARAMETER;
3229     }
3230 
3231     do
3232     {
3233         MOS_LOCK_PARAMS sMosLockParams{};
3234         sMosLockParams.WriteOnly = true;
3235 
3236         // Lock the surface for writing.
3237         pSurface = m_osInterface->pfnLockResource(m_osInterface, &sResource, &sMosLockParams);
3238 
3239         if (pSurface == nullptr)
3240         {
3241             CODECHAL_DEBUG_ASSERTMESSAGE("Error: Failed locking the resource.");
3242             eStatus = MOS_STATUS_UNKNOWN;
3243             break;
3244         }
3245 
3246         // Fill the surface with the wanted value.
3247         MOS_FillMemory(pSurface, uiSize, ucValue);
3248 
3249     } while (false);
3250 
3251     // Unlock the resource if needed.
3252     m_osInterface->pfnUnlockResource(m_osInterface, &sResource);
3253 
3254     return eStatus;
3255 }
3256 
DumpHucRegion(PMOS_RESOURCE region,uint32_t regionOffset,uint32_t regionSize,uint32_t regionNum,const char * regionName,bool inputBuffer,uint32_t hucPassNum,CodechalHucRegionDumpType dumpType)3257 MOS_STATUS CodechalDebugInterface::DumpHucRegion(
3258     PMOS_RESOURCE             region,
3259     uint32_t                  regionOffset,
3260     uint32_t                  regionSize,
3261     uint32_t                  regionNum,
3262     const char *              regionName,
3263     bool                      inputBuffer,
3264     uint32_t                  hucPassNum,
3265     CodechalHucRegionDumpType dumpType)
3266 {
3267     CODECHAL_DEBUG_FUNCTION_ENTER;
3268 
3269     if (!m_configMgr->AttrIsEnabled(MediaDbgAttr::attrHucRegions) && !MosUtilities::GetTraceSetting())
3270     {
3271         return MOS_STATUS_SUCCESS;
3272     }
3273     CODECHAL_DEBUG_ASSERT(regionNum < 16);
3274     CODECHAL_DEBUG_CHK_NULL(region);
3275 
3276     if (Mos_ResourceIsNull(region))
3277     {
3278         return MOS_STATUS_NULL_POINTER;
3279     }
3280 
3281     std::string funcName = "";
3282     if (m_codecFunction == CODECHAL_FUNCTION_DECODE)
3283     {
3284         funcName = "DEC_";
3285     }
3286     else if (m_codecFunction == CODECHAL_FUNCTION_CENC_DECODE)
3287     {
3288         funcName = "DEC_CENC_";
3289     }
3290     else
3291     {
3292         funcName = "ENC_";
3293     }
3294 
3295     std::string bufName       = MediaDbgBufferType::bufHucRegion;
3296     std::string inputName     = (inputBuffer) ? "Input_" : "Output_";
3297     std::string regionNumName = std::to_string(regionNum);
3298     std::string passName      = std::to_string(hucPassNum);
3299     switch (dumpType)
3300     {
3301     case hucRegionDumpInit:
3302         funcName = funcName + inputName + bufName + regionNumName + regionName + "_InitPass" + passName;
3303         break;
3304     case hucRegionDumpUpdate:
3305         funcName = funcName + inputName + bufName + regionNumName + regionName + "_UpdatePass" + passName;
3306         break;
3307     case hucRegionDumpLAUpdate:
3308         funcName = funcName + inputName + bufName + regionNumName + regionName + "_LookaheadUpdatePass" + passName;
3309         break;
3310     case hucRegionDumpRegionLocked:
3311         funcName = funcName + inputName + bufName + regionNumName + regionName + "_RegionLockedPass" + passName;
3312         break;
3313     case hucRegionDumpCmdInitializer:
3314         funcName = funcName + inputName + bufName + regionNumName + regionName + "_CmdInitializerPass" + passName;
3315         break;
3316     case hucRegionDumpPakIntegrate:
3317         funcName = funcName + inputName + bufName + regionNumName + regionName + "_PakIntPass" + passName;
3318         break;
3319     case hucRegionDumpHpu:
3320         funcName = funcName + inputName + bufName + regionNumName + regionName + "_HpuPass" + passName;
3321         break;
3322     case hucRegionDumpBackAnnotation:
3323         funcName = funcName + inputName + bufName + regionNumName + regionName + "_BackAnnotationPass" + passName;
3324         break;
3325     default:
3326         funcName = funcName + inputName + bufName + regionNumName + regionName + "_Pass" + passName;
3327         break;
3328     }
3329 
3330     return DumpBuffer(region, MediaDbgAttr::attrHucRegions, funcName.c_str(), regionSize, regionOffset);
3331 }
3332 
3333 #define FIELD_TO_OFS(field_name) ofs << print_shift << std::setfill(' ') << std::setw(25) << std::left << std::string(#field_name) + ": " << (int64_t)report->field_name << std::endl;
3334 #define PTR_TO_OFS(ptr_name) ofs << print_shift << std::setfill(' ') << std::setw(25) << std::left << std::string(#ptr_name) + ": " << report->ptr_name << std::endl;
DumpEncodeStatusReport(const encode::EncodeStatusReportData * report)3335 MOS_STATUS CodechalDebugInterface::DumpEncodeStatusReport(const encode::EncodeStatusReportData *report)
3336 {
3337     CODECHAL_DEBUG_FUNCTION_ENTER;
3338 
3339     CODECHAL_DEBUG_CHK_NULL(report);
3340 
3341     const char *bufferName = "EncodeStatusReport_Parsed";
3342     const char *attrName   = MediaDbgAttr::attrStatusReport;
3343     if (!m_configMgr->AttrIsEnabled(attrName))
3344     {
3345         return MOS_STATUS_SUCCESS;
3346     }
3347 
3348     const char *  filePath = CreateFileName(bufferName, attrName, MediaDbgExtType::txt);
3349     std::ofstream ofs(filePath);
3350 
3351     if (ofs.fail())
3352     {
3353         return MOS_STATUS_UNKNOWN;
3354     }
3355     std::string print_shift = "";
3356     sizeof(report->codecStatus);
3357     FIELD_TO_OFS(codecStatus);
3358     FIELD_TO_OFS(statusReportNumber);
3359     FIELD_TO_OFS(currOriginalPic.FrameIdx);
3360     FIELD_TO_OFS(currOriginalPic.PicFlags);
3361     FIELD_TO_OFS(currOriginalPic.PicEntry);
3362     FIELD_TO_OFS(func);
3363     PTR_TO_OFS(  currRefList);
3364     ofs << std::endl;
3365 
3366     FIELD_TO_OFS(sequential);
3367     FIELD_TO_OFS(bitstreamSize);
3368     FIELD_TO_OFS(qpY);
3369     FIELD_TO_OFS(suggestedQPYDelta);
3370     FIELD_TO_OFS(numberPasses);
3371     FIELD_TO_OFS(averageQP);
3372     FIELD_TO_OFS(hwCounterValue.IV);
3373     FIELD_TO_OFS(hwCounterValue.Count);
3374     PTR_TO_OFS(hwCtr);
3375     FIELD_TO_OFS(queryStatusFlags);
3376 
3377     print_shift = "    ";
3378     FIELD_TO_OFS(panicMode);
3379     FIELD_TO_OFS(sliceSizeOverflow);
3380     FIELD_TO_OFS(numSlicesNonCompliant);
3381     FIELD_TO_OFS(longTermReference);
3382     FIELD_TO_OFS(frameSkipped);
3383     FIELD_TO_OFS(sceneChangeDetected);
3384     print_shift = "";
3385     ofs << std::endl;
3386 
3387     FIELD_TO_OFS(mad);
3388     FIELD_TO_OFS(loopFilterLevel);
3389     FIELD_TO_OFS(longTermIndication);
3390     FIELD_TO_OFS(nextFrameWidthMinus1);
3391     FIELD_TO_OFS(nextFrameHeightMinus1);
3392     FIELD_TO_OFS(numberSlices);
3393 
3394     FIELD_TO_OFS(psnrX100[0]);
3395     FIELD_TO_OFS(psnrX100[1]);
3396     FIELD_TO_OFS(psnrX100[2]);
3397 
3398     FIELD_TO_OFS(numberTilesInFrame);
3399     FIELD_TO_OFS(usedVdBoxNumber);
3400     FIELD_TO_OFS(sizeOfSliceSizesBuffer);
3401     PTR_TO_OFS(  sliceSizes);
3402     FIELD_TO_OFS(sizeOfTileInfoBuffer);
3403     PTR_TO_OFS(  hevcTileinfo);
3404     FIELD_TO_OFS(numTileReported);
3405     ofs << std::endl;
3406 
3407     FIELD_TO_OFS(streamId);
3408     PTR_TO_OFS(  pLookaheadStatus);
3409     ofs.close();
3410 
3411     return MOS_STATUS_SUCCESS;
3412 }
3413 
3414 #undef FIELD_TO_OFS
3415 #undef PTR_TO_OFS
3416 
DumpIsEnabled(const char * attr,CODECHAL_MEDIA_STATE_TYPE mediaState)3417 bool CodechalDebugInterface::DumpIsEnabled(
3418     const char *              attr,
3419     CODECHAL_MEDIA_STATE_TYPE mediaState)
3420 {
3421     if (nullptr == m_configMgr)
3422     {
3423         return false;
3424     }
3425 
3426     if (mediaState != CODECHAL_NUM_MEDIA_STATES)
3427     {
3428         return static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, attr);
3429     }
3430     else
3431     {
3432         return m_configMgr->AttrIsEnabled(attr);
3433     }
3434 }
3435 
SetFastDumpConfig(MediaCopyWrapper * mediaCopyWrapper)3436 MOS_STATUS CodechalDebugInterface::SetFastDumpConfig(MediaCopyWrapper *mediaCopyWrapper)
3437 {
3438     auto traceSetting = MosUtilities::GetTraceSetting();
3439     if (!mediaCopyWrapper || !(DumpIsEnabled(MediaDbgAttr::attrEnableFastDump) || traceSetting))
3440     {
3441         return MOS_STATUS_SUCCESS;
3442     }
3443 
3444     MediaDebugFastDump::Config cfg{};
3445     if (traceSetting)
3446     {
3447         const auto &c           = traceSetting->fastDump;
3448         cfg.allowDataLoss       = c.allowDataLoss;
3449         cfg.frameIdx            = c.frameIdxBasedSampling ? &m_bufferDumpFrameNum : nullptr;
3450         cfg.samplingTime        = static_cast<size_t>(c.samplingTime);
3451         cfg.samplingInterval    = static_cast<size_t>(c.samplingInterval);
3452         cfg.memUsagePolicy      = c.memUsagePolicy;
3453         cfg.maxPrioritizedMem   = c.maxPrioritizedMem;
3454         cfg.maxDeprioritizedMem = c.maxDeprioritizedMem;
3455         cfg.weightRenderCopy    = c.weightRenderCopy;
3456         cfg.weightVECopy        = c.weightVECopy;
3457         cfg.weightBLTCopy       = c.weightBLTCopy;
3458         cfg.writeMode           = c.writeMode;
3459         cfg.bufferSize          = static_cast<size_t>(c.bufferSize);
3460         cfg.informOnError       = c.informOnError;
3461 
3462         auto suffix = cfg.writeMode == 0 ? ".bin" : cfg.writeMode == 1 ? ".txt"
3463                                                                       : "";
3464 
3465         class DumpEnabled
3466         {
3467         public:
3468             bool operator()(const char *attrName)
3469             {
3470                 decltype(m_filter)::const_iterator it;
3471                 return (it = m_filter.find(attrName)) != m_filter.end() &&
3472                        MOS_TraceKeyEnabled(it->second);
3473             }
3474 
3475         private:
3476             const std::map<std::string, MEDIA_EVENT_FILTER_KEYID> m_filter = {
3477                 {MediaDbgAttr::attrDecodeOutputSurface, TR_KEY_DECODE_DSTYUV},
3478                 {MediaDbgAttr::attrDecodeReferenceSurfaces, TR_KEY_DECODE_REFYUV},
3479                 {MediaDbgAttr::attrDecodeBitstream, TR_KEY_DECODE_BITSTREAM},
3480                 {MediaDbgAttr::attrEncodeRawInputSurface, TR_KEY_ENCODE_DATA_INPUT_SURFACE},
3481                 {MediaDbgAttr::attrReferenceSurfaces, TR_KEY_ENCODE_DATA_REF_SURFACE},
3482                 {MediaDbgAttr::attrReconstructedSurface, TR_KEY_ENCODE_DATA_RECON_SURFACE},
3483                 {MediaDbgAttr::attrBitstream, TR_KEY_ENCODE_DATA_BITSTREAM},
3484                 {MediaDbgAttr::attrHuCDmem, TR_KEY_ENCODE_DATA_HUC_DMEM},
3485                 {MediaDbgAttr::attrHucRegions, TR_KEY_ENCODE_DATA_HUC_REGION},
3486             };
3487         };
3488 
3489         auto dumpEnabled = std::make_shared<DumpEnabled>();
3490 
3491         m_dumpYUVSurface = [this, dumpEnabled, traceSetting, suffix](
3492                                PMOS_SURFACE              surface,
3493                                const char               *attrName,
3494                                const char               *surfName,
3495                                CODECHAL_MEDIA_STATE_TYPE mediaState,
3496                                uint32_t,
3497                                uint32_t) {
3498             if ((*dumpEnabled)(attrName))
3499             {
3500                 MediaDebugFastDump::Dump(
3501                     surface->OsResource,
3502                     std::string(traceSetting->fastDump.filePath) +
3503                         std::to_string(m_bufferDumpFrameNum) +
3504                         '-' +
3505                         surfName +
3506                         "w[0]_h[0]_p[0]" +
3507                         suffix);
3508             }
3509             return MOS_STATUS_SUCCESS;
3510         };
3511 
3512         m_dumpBuffer = [this, dumpEnabled, traceSetting, suffix](
3513                            PMOS_RESOURCE             resource,
3514                            const char               *attrName,
3515                            const char               *compName,
3516                            uint32_t                  size,
3517                            uint32_t                  offset,
3518                            CODECHAL_MEDIA_STATE_TYPE mediaState) {
3519             if ((*dumpEnabled)(attrName))
3520             {
3521                 std::string bufferName = "";
3522 
3523                 if (!strcmp(attrName, "DecodeBitstream") || !strcmp(attrName, "Bitstream"))
3524                 {
3525                     bufferName = "_Bitstream";
3526                 }
3527 
3528                 MediaDebugFastDump::Dump(
3529                     *resource,
3530                     std::string(traceSetting->fastDump.filePath) +
3531                         std::to_string(m_bufferDumpFrameNum) +
3532                         '-' +
3533                         compName + bufferName +
3534                         suffix,
3535                     size,
3536                     offset,
3537                     MediaDebugSerializer<uint32_t>());
3538             }
3539             return MOS_STATUS_SUCCESS;
3540         };
3541     }
3542     else
3543     {
3544         cfg.allowDataLoss = false;
3545         cfg.informOnError = false;
3546 
3547         MediaUserSetting::Value outValue{};
3548         ReadUserSettingForDebug(
3549             m_userSettingPtr,
3550             outValue,
3551             "Enable VECopy For Surface Dump",
3552             MediaUserSetting::Group::Sequence);
3553 
3554         if (outValue.Get<bool>())
3555         {
3556             // use VE copy
3557             cfg.weightRenderCopy = 0;
3558             cfg.weightVECopy     = 100;
3559             cfg.weightBLTCopy    = 0;
3560         }
3561     }
3562 
3563     MediaDebugFastDump::CreateInstance(*m_osInterface, *mediaCopyWrapper, &cfg);
3564 
3565     return MOS_STATUS_SUCCESS;
3566 }
3567 
CreateFileName(const char * funcName,const char * bufType,const char * extType)3568 const char *CodechalDebugInterface::CreateFileName(
3569     const char *funcName,
3570     const char *bufType,
3571     const char *extType)
3572 {
3573     if (nullptr == funcName || nullptr == extType)
3574     {
3575         return nullptr;
3576     }
3577 
3578     char frameType = 'X';
3579     // Sets the frameType label
3580     if (m_frameType == I_TYPE)
3581     {
3582         frameType = 'I';
3583     }
3584     else if (m_frameType == P_TYPE)
3585     {
3586         frameType = 'P';
3587     }
3588     else if (m_frameType == B_TYPE)
3589     {
3590         frameType = 'B';
3591     }
3592     else if (m_frameType == MIXED_TYPE)
3593     {
3594         frameType = 'M';
3595     }
3596 
3597     const char *fieldOrder;
3598     // Sets the Field Order label
3599     if (CodecHal_PictureIsTopField(m_currPic))
3600     {
3601         fieldOrder = MediaDbgFieldType::topField;
3602     }
3603     else if (CodecHal_PictureIsBottomField(m_currPic))
3604     {
3605         fieldOrder = MediaDbgFieldType::botField;
3606     }
3607     else
3608     {
3609         fieldOrder = MediaDbgFieldType::frame;
3610     }
3611 
3612     // Sets the Postfix label
3613     if (m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary) &&
3614         strcmp(extType, MediaDbgExtType::txt) == 0)
3615     {
3616         extType = MediaDbgExtType::dat;
3617     }
3618 
3619     if (bufType != nullptr &&
3620         !strncmp(bufType, MediaDbgBufferType::bufSlcParams, sizeof(MediaDbgBufferType::bufSlcParams) - 1) && !strncmp(funcName, "_DDIEnc", sizeof("_DDIEnc") - 1))
3621     {
3622         m_outputFileName = m_outputFilePath +
3623                            std::to_string(m_bufferDumpFrameNum) + '-' +
3624                            std::to_string(m_streamId) + '_' +
3625                            std::to_string(m_sliceId + 1) +
3626                            funcName + '_' + bufType + '_' + frameType + fieldOrder + extType;
3627     }
3628     else if (bufType != nullptr &&
3629              !strncmp(bufType, MediaDbgBufferType::bufEncodePar, sizeof(MediaDbgBufferType::bufEncodePar) - 1))
3630     {
3631         if (!strncmp(funcName, "EncodeSequence", sizeof("EncodeSequence") - 1))
3632         {
3633             m_outputFileName = m_outputFilePath +
3634                                std::to_string(m_streamId) + '_' +
3635                                funcName + extType;
3636         }
3637         else
3638         {
3639             m_outputFileName = m_outputFilePath +
3640                                std::to_string(m_bufferDumpFrameNum) + '-' +
3641                                std::to_string(m_streamId) + '_' +
3642                                funcName + frameType + fieldOrder + extType;
3643         }
3644     }
3645     else
3646     {
3647         if (funcName[0] == '_')
3648             funcName += 1;
3649 
3650         if (bufType != nullptr)
3651         {
3652             m_outputFileName = m_outputFilePath +
3653                                std::to_string(m_bufferDumpFrameNum) + '-' +
3654                                std::to_string(m_streamId) + '_' +
3655                                funcName + '_' + bufType + '_' + frameType + fieldOrder + extType;
3656         }
3657         else
3658         {
3659             m_outputFileName = m_outputFilePath +
3660                                std::to_string(m_bufferDumpFrameNum) + '-' +
3661                                std::to_string(m_streamId) + '_' +
3662                                funcName + '_' + frameType + fieldOrder + extType;
3663         }
3664     }
3665 
3666     return m_outputFileName.c_str();
3667 }
3668 
DumpStringStream(std::stringstream & ss,const char * bufferName,const char * attrName)3669 MOS_STATUS CodechalDebugInterface::DumpStringStream(std::stringstream& ss, const char* bufferName, const char* attrName)
3670 {
3671     MEDIA_DEBUG_FUNCTION_ENTER;
3672 
3673     MEDIA_DEBUG_CHK_NULL(bufferName);
3674     MEDIA_DEBUG_CHK_NULL(attrName);
3675 
3676     if (!m_configMgr->AttrIsEnabled(attrName))
3677     {
3678         return MOS_STATUS_SUCCESS;
3679     }
3680 
3681     const char* filePath = CreateFileName(bufferName, nullptr, MediaDbgExtType::txt);
3682     std::ofstream ofs(filePath);
3683     ofs << ss.str();
3684     ofs.close();
3685 
3686     return MOS_STATUS_SUCCESS;
3687 }
3688 
DumpCmdBuffer(PMOS_COMMAND_BUFFER cmdBuffer,CODECHAL_MEDIA_STATE_TYPE mediaState,const char * cmdName)3689 MOS_STATUS CodechalDebugInterface::DumpCmdBuffer(
3690     PMOS_COMMAND_BUFFER       cmdBuffer,
3691     CODECHAL_MEDIA_STATE_TYPE mediaState,
3692     const char *              cmdName)
3693 {
3694     MEDIA_DEBUG_FUNCTION_ENTER;
3695 
3696     bool attrEnabled = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrCmdBufferMfx);
3697 
3698     if (!attrEnabled && mediaState != CODECHAL_NUM_MEDIA_STATES)
3699     {
3700         attrEnabled = static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attrCmdBuffer);
3701     }
3702 
3703     if (!attrEnabled)
3704     {
3705         return MOS_STATUS_SUCCESS;
3706     }
3707 
3708     bool binaryDumpEnabled = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpCmdBufInBinary);
3709 
3710     std::string funcName = cmdName ? cmdName : static_cast<CodecDebugConfigMgr*>(m_configMgr)->GetMediaStateStr(mediaState);
3711     const char *fileName = CreateFileName(
3712         funcName.c_str(),
3713         MediaDbgBufferType::bufCmd,
3714         binaryDumpEnabled ? MediaDbgExtType::dat : MediaDbgExtType::txt);
3715 
3716     if (m_configMgr->AttrIsEnabled(CodechalDbgAttr::attrEnableFastDump) && MediaDebugFastDump::IsGood())
3717     {
3718         MediaDebugFastDump::Dump(
3719             (uint8_t *)cmdBuffer->pCmdBase,
3720             fileName,
3721             (uint32_t)cmdBuffer->iOffset,
3722             0,
3723             MediaDebugSerializer<uint32_t>());
3724     }
3725     else
3726     {
3727         if (binaryDumpEnabled)
3728         {
3729             DumpBufferInBinary((uint8_t *)cmdBuffer->pCmdBase, (uint32_t)cmdBuffer->iOffset);
3730         }
3731         else
3732         {
3733             DumpBufferInHexDwords((uint8_t *)cmdBuffer->pCmdBase, (uint32_t)cmdBuffer->iOffset);
3734         }
3735     }
3736 
3737     return MOS_STATUS_SUCCESS;
3738 }
3739 
Dump2ndLvlBatch(PMHW_BATCH_BUFFER batchBuffer,CODECHAL_MEDIA_STATE_TYPE mediaState,const char * batchName)3740 MOS_STATUS CodechalDebugInterface::Dump2ndLvlBatch(
3741     PMHW_BATCH_BUFFER         batchBuffer,
3742     CODECHAL_MEDIA_STATE_TYPE mediaState,
3743     const char *              batchName)
3744 {
3745     MEDIA_DEBUG_FUNCTION_ENTER;
3746 
3747     MEDIA_DEBUG_CHK_NULL(batchBuffer);
3748 
3749     bool attrEnabled = m_configMgr->AttrIsEnabled(MediaDbgAttr::attr2ndLvlBatchMfx);
3750 
3751     if (!attrEnabled && mediaState != CODECHAL_NUM_MEDIA_STATES)
3752     {
3753         attrEnabled = static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attr2ndLvlBatch);
3754     }
3755 
3756     if (!attrEnabled)
3757     {
3758         return MOS_STATUS_SUCCESS;
3759     }
3760 
3761     std::string funcName = batchName ? batchName : static_cast<CodecDebugConfigMgr*>(m_configMgr)->GetMediaStateStr(mediaState);
3762     const char *fileName = CreateFileName(
3763         funcName.c_str(),
3764         MediaDbgBufferType::buf2ndLvl,
3765         MediaDbgExtType::txt);
3766 
3767     MOS_STATUS status = MOS_STATUS_SUCCESS;
3768     if ((batchBuffer->OsResource.pGmmResInfo != nullptr) &&
3769         (batchBuffer->OsResource.pGmmResInfo->GetResFlags().Info.NotLockable))
3770     {
3771         uint8_t      *data            = nullptr;
3772         PMOS_RESOURCE pReadbackBuffer = (PMOS_RESOURCE)MOS_AllocAndZeroMemory(sizeof(MOS_RESOURCE));
3773         CODECHAL_DEBUG_CHK_NULL(pReadbackBuffer);
3774         status = AllocateLinearResource(MOS_GFXRES_BUFFER, (uint32_t)batchBuffer->OsResource.pGmmResInfo->GetBaseWidth(), (uint32_t)batchBuffer->OsResource.pGmmResInfo->GetBaseHeight(), Format_Any, pReadbackBuffer);
3775         if (MOS_FAILED(status))
3776         {
3777             MOS_FreeMemAndSetNull(pReadbackBuffer);
3778         }
3779         CODECHAL_DEBUG_CHK_STATUS(status);
3780         status = VDBypassCopyResource(&batchBuffer->OsResource, pReadbackBuffer);
3781         if (MOS_FAILED(status))
3782         {
3783             m_osInterface->pfnFreeResource(m_osInterface, pReadbackBuffer);
3784             MOS_FreeMemAndSetNull(pReadbackBuffer);
3785         }
3786         CODECHAL_DEBUG_CHK_STATUS(status);
3787 
3788         MOS_LOCK_PARAMS lockFlags;
3789         MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
3790         lockFlags.ReadOnly = 1;
3791         data               = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, pReadbackBuffer, &lockFlags);
3792         if (data == nullptr)
3793         {
3794             m_osInterface->pfnFreeResource(m_osInterface, pReadbackBuffer);
3795             MOS_FreeMemAndSetNull(pReadbackBuffer);
3796         }
3797         CODECHAL_DEBUG_CHK_NULL(data);
3798 
3799         if (DumpIsEnabled(MediaDbgAttr::attrEnableFastDump) && MediaDebugFastDump::IsGood())
3800         {
3801             MediaDebugFastDump::Dump(
3802                 data + batchBuffer->dwOffset,
3803                 fileName,
3804                 (uint32_t)batchBuffer->iLastCurrent,
3805                 0,
3806                 MediaDebugSerializer<uint32_t>());
3807         }
3808         else
3809         {
3810             DumpBufferInHexDwords(data + batchBuffer->dwOffset,
3811                 (uint32_t)batchBuffer->iLastCurrent);
3812         }
3813 
3814         m_osInterface->pfnUnlockResource(m_osInterface, pReadbackBuffer);
3815         m_osInterface->pfnFreeResource(m_osInterface, pReadbackBuffer);
3816         MOS_FreeMemAndSetNull(pReadbackBuffer);
3817         CODECHAL_DEBUG_CHK_STATUS(status);
3818     }
3819     else
3820     {
3821         bool batchLockedForDebug = !batchBuffer->bLocked;
3822 
3823         if (batchLockedForDebug)
3824         {
3825             (Mhw_LockBb(m_osInterface, batchBuffer));
3826         }
3827 
3828         batchBuffer->pData += batchBuffer->dwOffset;
3829 
3830         if (DumpIsEnabled(MediaDbgAttr::attrEnableFastDump) && MediaDebugFastDump::IsGood())
3831         {
3832             MediaDebugFastDump::Dump(
3833                 batchBuffer->pData,
3834                 fileName,
3835                 (uint32_t)batchBuffer->iLastCurrent,
3836                 0,
3837                 MediaDebugSerializer<uint32_t>());
3838         }
3839         else
3840         {
3841             DumpBufferInHexDwords(batchBuffer->pData,
3842                 (uint32_t)batchBuffer->iLastCurrent);
3843         }
3844 
3845         if (batchLockedForDebug)
3846         {
3847             (Mhw_UnlockBb(m_osInterface, batchBuffer, false));
3848         }
3849     }
3850 
3851     return MOS_STATUS_SUCCESS;
3852 }
3853 
DumpCurbe(CODECHAL_MEDIA_STATE_TYPE mediaState,PMHW_KERNEL_STATE kernelState)3854 MOS_STATUS CodechalDebugInterface::DumpCurbe(
3855     CODECHAL_MEDIA_STATE_TYPE mediaState,
3856     PMHW_KERNEL_STATE         kernelState)
3857 {
3858     MEDIA_DEBUG_FUNCTION_ENTER;
3859 
3860     if (mediaState >= CODECHAL_NUM_MEDIA_STATES ||
3861         !static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attrCurbe))
3862     {
3863         return MOS_STATUS_SUCCESS;
3864     }
3865 
3866     std::string funcName   = static_cast<CodecDebugConfigMgr*>(m_configMgr)->GetMediaStateStr(mediaState);
3867     bool        binaryDump = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary);
3868 
3869     const char *fileName = CreateFileName(
3870         funcName.c_str(),
3871         MediaDbgBufferType::bufCurbe,
3872         MediaDbgExtType::txt);
3873 
3874     return kernelState->m_dshRegion.Dump(
3875         fileName,
3876         kernelState->dwCurbeOffset,
3877         kernelState->KernelParams.iCurbeLength,
3878         binaryDump);
3879 }
3880 
DumpMDFCurbe(CODECHAL_MEDIA_STATE_TYPE mediaState,uint8_t * curbeBuffer,uint32_t curbeSize)3881 MOS_STATUS CodechalDebugInterface::DumpMDFCurbe(
3882     CODECHAL_MEDIA_STATE_TYPE mediaState,
3883     uint8_t *                 curbeBuffer,
3884     uint32_t                  curbeSize)
3885 {
3886     MEDIA_DEBUG_FUNCTION_ENTER;
3887 
3888     uint8_t *  curbeAlignedData = nullptr;
3889     uint32_t   curbeAlignedSize = 0;
3890     MOS_STATUS eStatus          = MOS_STATUS_SUCCESS;
3891 
3892     if (mediaState >= CODECHAL_NUM_MEDIA_STATES ||
3893         !static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attrCurbe))
3894     {
3895         return eStatus;
3896     }
3897 
3898     std::string funcName   = static_cast<CodecDebugConfigMgr*>(m_configMgr)->GetMediaStateStr(mediaState);
3899     bool        binaryDump = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary);
3900     const char *extType    = binaryDump ? MediaDbgExtType::dat : MediaDbgExtType::txt;
3901 
3902     const char *fileName = CreateFileName(
3903         funcName.c_str(),
3904         MediaDbgBufferType::bufCurbe,
3905         extType);
3906 
3907     curbeAlignedSize = MOS_ALIGN_CEIL(curbeSize, 64);
3908     curbeAlignedData = (uint8_t *)malloc(curbeAlignedSize * sizeof(uint8_t));
3909     if (curbeAlignedData == nullptr)
3910     {
3911         eStatus = MOS_STATUS_NULL_POINTER;
3912         return eStatus;
3913     }
3914 
3915     MOS_ZeroMemory(curbeAlignedData, curbeAlignedSize);
3916     MOS_SecureMemcpy(curbeAlignedData, curbeSize, curbeBuffer, curbeSize);
3917 
3918     if (binaryDump)
3919     {
3920         eStatus = DumpBufferInBinary(curbeAlignedData, curbeAlignedSize);
3921     }
3922     else
3923     {
3924         eStatus = DumpBufferInHexDwords(curbeAlignedData, curbeAlignedSize);
3925     }
3926 
3927     free(curbeAlignedData);
3928 
3929     return eStatus;
3930 }
3931 
DumpKernelRegion(CODECHAL_MEDIA_STATE_TYPE mediaState,MHW_STATE_HEAP_TYPE stateHeap,PMHW_KERNEL_STATE kernelState)3932 MOS_STATUS CodechalDebugInterface::DumpKernelRegion(
3933     CODECHAL_MEDIA_STATE_TYPE mediaState,
3934     MHW_STATE_HEAP_TYPE       stateHeap,
3935     PMHW_KERNEL_STATE         kernelState)
3936 {
3937     MEDIA_DEBUG_FUNCTION_ENTER;
3938 
3939     uint8_t *sshData = nullptr;
3940     uint32_t sshSize = 0;
3941 
3942     MemoryBlock *regionBlock = nullptr;
3943     bool         attrEnabled = false;
3944     const char * bufferType;
3945     if (stateHeap == MHW_ISH_TYPE)
3946     {
3947         regionBlock = &kernelState->m_ishRegion;
3948         attrEnabled = static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attrIsh);
3949         bufferType  = MediaDbgBufferType::bufISH;
3950     }
3951     else if (stateHeap == MHW_DSH_TYPE)
3952     {
3953         regionBlock = &kernelState->m_dshRegion;
3954         attrEnabled = static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attrDsh);
3955         bufferType  = MediaDbgBufferType::bufDSH;
3956     }
3957     else
3958     {
3959         attrEnabled = static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, MediaDbgAttr::attrSsh);
3960         bufferType  = MediaDbgBufferType::bufSSH;
3961 
3962         MEDIA_DEBUG_CHK_NULL(m_osInterface);
3963         MEDIA_DEBUG_CHK_STATUS(m_osInterface->pfnGetIndirectStatePointer(
3964             m_osInterface,
3965             &sshData));
3966         sshData += kernelState->dwSshOffset;
3967         sshSize = kernelState->dwSshSize;
3968     }
3969 
3970     if (!attrEnabled)
3971     {
3972         return MOS_STATUS_SUCCESS;
3973     }
3974 
3975     std::string funcName = static_cast<CodecDebugConfigMgr*>(m_configMgr)->GetMediaStateStr(mediaState);
3976 
3977     const char *fileName = CreateFileName(
3978         funcName.c_str(),
3979         bufferType,
3980         MediaDbgExtType::txt);
3981 
3982     bool binaryDump = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary);
3983 
3984     if (regionBlock)
3985     {
3986         return regionBlock->Dump(fileName, 0, 0, binaryDump);
3987     }
3988     else
3989     {
3990         return DumpBufferInHexDwords(sshData, sshSize);
3991     }
3992 }
3993 
DumpYUVSurfaceToBuffer(PMOS_SURFACE surface,uint8_t * buffer,uint32_t & size)3994 MOS_STATUS CodechalDebugInterface::DumpYUVSurfaceToBuffer(PMOS_SURFACE surface,
3995     uint8_t *                                                          buffer,
3996     uint32_t &                                                         size)
3997 {
3998     MOS_STATUS eStatus = MOS_STATUS_SUCCESS;
3999 
4000     MOS_LOCK_PARAMS lockFlags;
4001     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
4002     lockFlags.ReadOnly     = 1;
4003     lockFlags.TiledAsTiled = 1;  // Bypass GMM CPU blit due to some issues in GMM CpuBlt function
4004 
4005     uint8_t *lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &surface->OsResource, &lockFlags);
4006     if (lockedAddr == nullptr)  // Failed to lock. Try to submit copy task and dump another surface
4007     {
4008         uint32_t        sizeToBeCopied = 0;
4009         MOS_GFXRES_TYPE ResType;
4010 
4011 #if LINUX
4012         // Linux does not have OsResource->ResType
4013         ResType = surface->Type;
4014 #else
4015         ResType = surface->OsResource.ResType;
4016 #endif
4017 
4018         GMM_RESOURCE_FLAG gmmFlags  = surface->OsResource.pGmmResInfo->GetResFlags();
4019         bool              allocated = false;
4020 
4021         MEDIA_DEBUG_CHK_STATUS(ReAllocateSurface(
4022             &m_temp2DSurfForCopy,
4023             surface,
4024             "Temp2DSurfForSurfDumper",
4025             ResType));
4026 
4027         // Ensure allocated buffer size contains the source surface size
4028         if (m_temp2DSurfForCopy.OsResource.pGmmResInfo->GetSizeMainSurface() >= surface->OsResource.pGmmResInfo->GetSizeMainSurface())
4029         {
4030             sizeToBeCopied = (uint32_t)surface->OsResource.pGmmResInfo->GetSizeMainSurface();
4031         }
4032 
4033         if (sizeToBeCopied == 0)
4034         {
4035             // Currently, MOS's pfnAllocateResource does not support allocate a surface reference to another surface.
4036             // When the source surface is not created from Media, it is possible that we cannot allocate the same size as source.
4037             // For example, on Gen9, Render target might have GMM set CCS=1 MMC=0, but MOS cannot allocate surface with such combination.
4038             // When Gmm allocation parameter is different, the resulting surface size/padding/pitch will be differnt.
4039             // Once if MOS can support allocate a surface by reference another surface, we can do a bit to bit copy without problem.
4040             MEDIA_DEBUG_ASSERTMESSAGE("Cannot allocate correct size, failed to copy nonlockable resource");
4041             return MOS_STATUS_NULL_POINTER;
4042         }
4043 
4044         MEDIA_DEBUG_VERBOSEMESSAGE("Temp2DSurfaceForCopy width %d, height %d, pitch %d, TileType %d, bIsCompressed %d, CompressionMode %d",
4045             m_temp2DSurfForCopy.dwWidth,
4046             m_temp2DSurfForCopy.dwHeight,
4047             m_temp2DSurfForCopy.dwPitch,
4048             m_temp2DSurfForCopy.TileType,
4049             m_temp2DSurfForCopy.bIsCompressed,
4050             m_temp2DSurfForCopy.CompressionMode);
4051 
4052         if (CopySurfaceData_Vdbox(sizeToBeCopied, &surface->OsResource, &m_temp2DSurfForCopy.OsResource) != MOS_STATUS_SUCCESS)
4053         {
4054             MEDIA_DEBUG_ASSERTMESSAGE("CopyDataSurface_Vdbox failed");
4055             m_osInterface->pfnFreeResource(m_osInterface, &m_temp2DSurfForCopy.OsResource);
4056             return MOS_STATUS_NULL_POINTER;
4057         }
4058         lockedAddr = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &m_temp2DSurfForCopy.OsResource, &lockFlags);
4059         MEDIA_DEBUG_CHK_NULL(lockedAddr);
4060     }
4061 
4062     uint32_t sizeMain     = (uint32_t)(surface->OsResource.pGmmResInfo->GetSizeMainSurface());
4063     uint8_t *surfBaseAddr = (uint8_t *)MOS_AllocMemory(sizeMain);
4064     MEDIA_DEBUG_CHK_NULL(surfBaseAddr);
4065 
4066     Mos_SwizzleData(lockedAddr, surfBaseAddr, surface->TileType, MOS_TILE_LINEAR, sizeMain / surface->dwPitch, surface->dwPitch, 0);
4067 
4068     uint8_t *data = surfBaseAddr;
4069     data += surface->dwOffset + surface->YPlaneOffset.iYOffset * surface->dwPitch;
4070 
4071     uint32_t width  = surface->dwWidth;
4072     uint32_t height = surface->dwHeight;
4073 
4074     switch (surface->Format)
4075     {
4076     case Format_YUY2:
4077     case Format_Y216V:
4078     case Format_P010:
4079     case Format_P016:
4080         width = width << 1;
4081         break;
4082     case Format_Y216:
4083     case Format_Y210:  //422 10bit -- Y0[15:0]:U[15:0]:Y1[15:0]:V[15:0] = 32bits per pixel = 4Bytes per pixel
4084     case Format_Y410:  //444 10bit -- A[31:30]:V[29:20]:Y[19:10]:U[9:0] = 32bits per pixel = 4Bytes per pixel
4085     case Format_R10G10B10A2:
4086     case Format_AYUV:  //444 8bit  -- A[31:24]:Y[23:16]:U[15:8]:V[7:0] = 32bits per pixel = 4Bytes per pixel
4087     case Format_A8R8G8B8:
4088         width = width << 2;
4089         break;
4090     default:
4091         break;
4092     }
4093 
4094     uint32_t pitch = surface->dwPitch;
4095     if (surface->Format == Format_UYVY)
4096         pitch = width;
4097 
4098     if (CodecHal_PictureIsBottomField(m_currPic))
4099     {
4100         data += pitch;
4101     }
4102 
4103     if (CodecHal_PictureIsField(m_currPic))
4104     {
4105         pitch *= 2;
4106         height /= 2;
4107     }
4108 
4109     // write luma data to file
4110     for (uint32_t h = 0; h < height; h++)
4111     {
4112         MOS_SecureMemcpy(buffer, width, data, width);
4113         buffer += width;
4114         size += width;
4115         data += pitch;
4116     }
4117 
4118     if (surface->Format != Format_A8B8G8R8)
4119     {
4120         switch (surface->Format)
4121         {
4122         case Format_NV12:
4123         case Format_P010:
4124         case Format_P016:
4125             height >>= 1;
4126             break;
4127         case Format_Y416:
4128         case Format_AUYV:
4129         case Format_R10G10B10A2:
4130             height *= 2;
4131             break;
4132         case Format_YUY2:
4133         case Format_YUYV:
4134         case Format_YUY2V:
4135         case Format_Y216V:
4136         case Format_YVYU:
4137         case Format_UYVY:
4138         case Format_VYUY:
4139         case Format_Y216:  //422 16bit
4140         case Format_Y210:  //422 10bit
4141         case Format_P208:  //422 8bit
4142             break;
4143         case Format_422V:
4144         case Format_IMC3:
4145             height = height / 2;
4146             break;
4147         case Format_AYUV:
4148         default:
4149             height = 0;
4150             break;
4151         }
4152 
4153         uint8_t *vPlaneData = surfBaseAddr;
4154 #ifdef LINUX
4155         data = surfBaseAddr + surface->UPlaneOffset.iSurfaceOffset;
4156         if (surface->Format == Format_422V || surface->Format == Format_IMC3)
4157         {
4158             vPlaneData = surfBaseAddr + surface->VPlaneOffset.iSurfaceOffset;
4159         }
4160 #else
4161         data = surfBaseAddr + surface->UPlaneOffset.iLockSurfaceOffset;
4162         if (surface->Format == Format_422V || surface->Format == Format_IMC3)
4163         {
4164             vPlaneData = surfBaseAddr + surface->VPlaneOffset.iLockSurfaceOffset;
4165         }
4166 
4167 #endif
4168 
4169         // write chroma data to file
4170         for (uint32_t h = 0; h < height; h++)
4171         {
4172             MOS_SecureMemcpy(buffer, width, data, width);
4173             buffer += width;
4174             size += width;
4175             data += pitch;
4176         }
4177 
4178         // write v planar data to file
4179         if (surface->Format == Format_422V || surface->Format == Format_IMC3)
4180         {
4181             for (uint32_t h = 0; h < height; h++)
4182             {
4183                 MOS_SecureMemcpy(buffer, width, vPlaneData, width);
4184                 buffer += width;
4185                 size += width;
4186                 vPlaneData += pitch;
4187             }
4188         }
4189     }
4190 
4191     m_osInterface->pfnUnlockResource(m_osInterface, &surface->OsResource);
4192 
4193     MOS_FreeMemory(surfBaseAddr);
4194 
4195     return MOS_STATUS_SUCCESS;
4196 }
4197 
DumpYUVSurface(PMOS_SURFACE surface,const char * attrName,const char * surfName,CODECHAL_MEDIA_STATE_TYPE mediaState,uint32_t width_in,uint32_t height_in)4198 MOS_STATUS CodechalDebugInterface::DumpYUVSurface(
4199     PMOS_SURFACE              surface,
4200     const char *              attrName,
4201     const char *              surfName,
4202     CODECHAL_MEDIA_STATE_TYPE mediaState,
4203     uint32_t                  width_in,
4204     uint32_t                  height_in)
4205 {
4206     return m_dumpYUVSurface(
4207         surface,
4208         attrName,
4209         surfName,
4210         mediaState,
4211         width_in,
4212         height_in);
4213 }
4214 
DumpBuffer(PMOS_RESOURCE resource,const char * attrName,const char * bufferName,uint32_t size,uint32_t offset,CODECHAL_MEDIA_STATE_TYPE mediaState)4215 MOS_STATUS CodechalDebugInterface::DumpBuffer(
4216     PMOS_RESOURCE             resource,
4217     const char *              attrName,
4218     const char *              bufferName,
4219     uint32_t                  size,
4220     uint32_t                  offset,
4221     CODECHAL_MEDIA_STATE_TYPE mediaState)
4222 {
4223     return m_dumpBuffer(
4224         resource,
4225         attrName,
4226         bufferName,
4227         size,
4228         offset,
4229         mediaState);
4230 }
4231 
DumpSurface(PMOS_SURFACE surface,const char * attrName,const char * surfaceName,CODECHAL_MEDIA_STATE_TYPE mediaState)4232 MOS_STATUS CodechalDebugInterface::DumpSurface(
4233     PMOS_SURFACE              surface,
4234     const char *              attrName,
4235     const char *              surfaceName,
4236     CODECHAL_MEDIA_STATE_TYPE mediaState)
4237 {
4238     MEDIA_DEBUG_FUNCTION_ENTER;
4239 
4240     MEDIA_DEBUG_CHK_NULL(surface);
4241     MEDIA_DEBUG_CHK_NULL(attrName);
4242     MEDIA_DEBUG_CHK_NULL(surfaceName);
4243 
4244     bool attrEnabled = false;
4245 
4246     if (mediaState == CODECHAL_NUM_MEDIA_STATES)
4247     {
4248         attrEnabled = m_configMgr->AttrIsEnabled(attrName);
4249     }
4250     else
4251     {
4252         attrEnabled = static_cast<CodecDebugConfigMgr*>(m_configMgr)->AttrIsEnabled(mediaState, attrName);
4253     }
4254 
4255     if (!attrEnabled)
4256     {
4257         return MOS_STATUS_SUCCESS;
4258     }
4259 
4260     bool        binaryDump = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary);
4261     const char *extType    = binaryDump ? MediaDbgExtType::dat : MediaDbgExtType::txt;
4262 
4263     MOS_LOCK_PARAMS lockFlags;
4264     MOS_ZeroMemory(&lockFlags, sizeof(MOS_LOCK_PARAMS));
4265     lockFlags.ReadOnly = 1;
4266     uint8_t *data      = (uint8_t *)m_osInterface->pfnLockResource(m_osInterface, &surface->OsResource, &lockFlags);
4267     MEDIA_DEBUG_CHK_NULL(data);
4268 
4269     std::string bufName = std::string(surfaceName) + "_w[" + std::to_string(surface->dwWidth) + "]_h[" + std::to_string(surface->dwHeight) + "]_p[" + std::to_string(surface->dwPitch) + "]";
4270     const char *fileName;
4271     if (mediaState == CODECHAL_NUM_MEDIA_STATES)
4272     {
4273         fileName = CreateFileName(bufName.c_str(), nullptr, extType);
4274     }
4275     else
4276     {
4277         std::string kernelName = static_cast<CodecDebugConfigMgr*>(m_configMgr)->GetMediaStateStr(mediaState);
4278         fileName               = CreateFileName(kernelName.c_str(), bufName.c_str(), extType);
4279     }
4280 
4281     MOS_STATUS status;
4282     if (binaryDump)
4283     {
4284         status = Dump2DBufferInBinary(data, surface->dwWidth, surface->dwHeight, surface->dwPitch);
4285     }
4286     else
4287     {
4288         status = DumpBufferInHexDwords(data, surface->dwHeight * surface->dwPitch);
4289     }
4290 
4291     if (data)
4292     {
4293         m_osInterface->pfnUnlockResource(m_osInterface, &surface->OsResource);
4294     }
4295 
4296     return status;
4297 }
4298 
DumpData(void * data,uint32_t size,const char * attrName,const char * bufferName)4299 MOS_STATUS CodechalDebugInterface::DumpData(
4300     void *      data,
4301     uint32_t    size,
4302     const char *attrName,
4303     const char *bufferName)
4304 {
4305     MEDIA_DEBUG_FUNCTION_ENTER;
4306 
4307     MEDIA_DEBUG_CHK_NULL(data);
4308     MEDIA_DEBUG_CHK_NULL(attrName);
4309     MEDIA_DEBUG_CHK_NULL(bufferName);
4310 
4311     if (!m_configMgr->AttrIsEnabled(attrName))
4312     {
4313         return MOS_STATUS_SUCCESS;
4314     }
4315 
4316     bool        binaryDump = m_configMgr->AttrIsEnabled(MediaDbgAttr::attrDumpBufferInBinary);
4317     const char *fileName   = CreateFileName(bufferName, nullptr, binaryDump ? MediaDbgExtType::dat : MediaDbgExtType::txt);
4318 
4319     if (binaryDump)
4320     {
4321         DumpBufferInBinary((uint8_t *)data, size);
4322     }
4323     else
4324     {
4325         DumpBufferInHexDwords((uint8_t *)data, size);
4326     }
4327 
4328     return MOS_STATUS_SUCCESS;
4329 }
4330 
4331 #define FIELD_TO_OFS(name, shift) ofs << shift #name << ": " << (int64_t)ptr->name << std::endl;
4332 #define EMPTY_TO_OFS()
4333 #define UNION_STRUCT_START_TO_OFS()     ofs << "union"      << std::endl \
4334                                             << "{"          << std::endl \
4335                                             << "    struct" << std::endl \
4336                                             << "    {"      << std::endl;
4337 
4338 #define UNION_STRUCT_FIELD_TO_OFS(name) ofs << "        "#name << ": " << ptr->name << std::endl;
4339 #define UNION_END_TO_OFS(name)          ofs << "    }"      << std::endl \
4340                                             << "    "#name << ": " << ptr->name << std::endl \
4341                                             << "}" << std::endl;
4342 #define OFFSET_FIELD_TO_OFS(class_name, f_name, shift) << shift "                 "#f_name": " << ptr->class_name.f_name << std::endl
4343 #define PLANE_OFFSET_TO_OFS(name) ofs << "MOS_PLANE_OFFSET "#name << std::endl \
4344                                                             OFFSET_FIELD_TO_OFS(name, iSurfaceOffset,)    \
4345                                                             OFFSET_FIELD_TO_OFS(name, iXOffset,)          \
4346                                                             OFFSET_FIELD_TO_OFS(name, iYOffset,)          \
4347                                                             OFFSET_FIELD_TO_OFS(name, iLockSurfaceOffset,);
4348 #define RESOURCE_OFFSET_TO_OFS(name, shift) ofs << shift "MOS_RESOURCE_OFFSETS "#name << std::endl \
4349                                                                                 OFFSET_FIELD_TO_OFS(name, BaseOffset, shift) \
4350                                                                                 OFFSET_FIELD_TO_OFS(name, XOffset, shift)    \
4351                                                                                 OFFSET_FIELD_TO_OFS(name, YOffset, shift);
4352 
4353 #define FIELD_TO_OFS_8SHIFT(name) FIELD_TO_OFS(name, "        ")
4354 
DumpSurfaceInfo(PMOS_SURFACE surface,const char * surfaceName)4355 MOS_STATUS CodechalDebugInterface::DumpSurfaceInfo(
4356     PMOS_SURFACE surface,
4357     const char* surfaceName)
4358 {
4359     MEDIA_DEBUG_FUNCTION_ENTER;
4360 
4361     MEDIA_DEBUG_CHK_NULL(surface);
4362     MEDIA_DEBUG_CHK_NULL(surfaceName);
4363 
4364     if (!m_configMgr->AttrIsEnabled(MediaDbgAttr::attrSurfaceInfo))
4365     {
4366         return MOS_STATUS_SUCCESS;
4367     }
4368 
4369     const char* funcName = (m_mediafunction == MEDIA_FUNCTION_VP) ? "_VP" : ((m_mediafunction == MEDIA_FUNCTION_ENCODE) ? "_ENC" : "_DEC");
4370     const char* filePath = CreateFileName(funcName, surfaceName, MediaDbgExtType::txt);
4371     std::ofstream ofs(filePath);
4372     PMOS_SURFACE ptr = surface;
4373 
4374     if (ofs.fail())
4375     {
4376         return MOS_STATUS_UNKNOWN;
4377     }
4378 
4379     ofs << "Surface name: " << surfaceName << std::endl;
4380 
4381     EMPTY_TO_OFS();
4382     ofs << "MOS_SURFACE:" << std::endl;
4383     FIELD_TO_OFS(dwArraySlice, );
4384     FIELD_TO_OFS(dwMipSlice, );
4385     FIELD_TO_OFS(S3dChannel, );
4386 
4387     EMPTY_TO_OFS();
4388     FIELD_TO_OFS(Type, );
4389     FIELD_TO_OFS(bOverlay, );
4390     FIELD_TO_OFS(bFlipChain, );
4391 
4392 #if !defined(LINUX)
4393     EMPTY_TO_OFS();
4394     UNION_STRUCT_START_TO_OFS();
4395     UNION_STRUCT_FIELD_TO_OFS(dwFirstArraySlice);
4396     UNION_STRUCT_FIELD_TO_OFS(dwFirstMipSlice);
4397     UNION_END_TO_OFS(dwSubResourceIndex);
4398 #endif
4399 
4400     EMPTY_TO_OFS();
4401     FIELD_TO_OFS(dwWidth, );
4402     FIELD_TO_OFS(dwHeight, );
4403     FIELD_TO_OFS(dwSize, );
4404     FIELD_TO_OFS(dwDepth, );
4405     FIELD_TO_OFS(dwArraySize, );
4406     FIELD_TO_OFS(dwLockPitch, );
4407     FIELD_TO_OFS(dwPitch, );
4408     FIELD_TO_OFS(dwSlicePitch, );
4409     FIELD_TO_OFS(dwQPitch, );
4410     FIELD_TO_OFS(TileType, );
4411     FIELD_TO_OFS(TileModeGMM, );
4412     FIELD_TO_OFS(bGMMTileEnabled, );
4413     FIELD_TO_OFS(Format, );
4414     FIELD_TO_OFS(bArraySpacing, );
4415     FIELD_TO_OFS(bCompressible, );
4416 
4417     EMPTY_TO_OFS();
4418     FIELD_TO_OFS(dwOffset, );
4419     PLANE_OFFSET_TO_OFS(YPlaneOffset);
4420     PLANE_OFFSET_TO_OFS(UPlaneOffset);
4421     PLANE_OFFSET_TO_OFS(VPlaneOffset);
4422 
4423     EMPTY_TO_OFS();
4424     UNION_STRUCT_START_TO_OFS();
4425     RESOURCE_OFFSET_TO_OFS(RenderOffset.YUV.Y, "    ");
4426     RESOURCE_OFFSET_TO_OFS(RenderOffset.YUV.U, "    ");
4427     RESOURCE_OFFSET_TO_OFS(RenderOffset.YUV.V, "    ");
4428     ofs << "    } YUV;" << std::endl;
4429     RESOURCE_OFFSET_TO_OFS(RenderOffset.RGB, );
4430     ofs << "}" << std::endl;
4431 
4432     EMPTY_TO_OFS();
4433     UNION_STRUCT_START_TO_OFS();
4434     UNION_STRUCT_FIELD_TO_OFS(LockOffset.YUV.Y);
4435     UNION_STRUCT_FIELD_TO_OFS(LockOffset.YUV.U);
4436     UNION_STRUCT_FIELD_TO_OFS(LockOffset.YUV.V);
4437     UNION_END_TO_OFS(LockOffset.RGB);
4438 
4439     EMPTY_TO_OFS();
4440     FIELD_TO_OFS(bIsCompressed, );
4441     FIELD_TO_OFS(CompressionMode, );
4442     FIELD_TO_OFS(CompressionFormat, );
4443     FIELD_TO_OFS(YoffsetForUplane, );
4444     FIELD_TO_OFS(YoffsetForVplane, );
4445 
4446     EMPTY_TO_OFS();
4447     EMPTY_TO_OFS();
4448     MOS_STATUS sts = DumpMosSpecificResourceInfoToOfs(&surface->OsResource, ofs);
4449     ofs.close();
4450 
4451     return sts;
4452 }
4453 
4454 #endif // USE_CODECHAL_DEBUG_TOOL
4455