xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_video_dec_hevc.h (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 
2 /*
3  * Copyright © Microsoft Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22  * IN THE SOFTWARE.
23  */
24 
25 #ifndef D3D12_VIDEO_DEC_HEVC_H
26 #define D3D12_VIDEO_DEC_HEVC_H
27 
28 #include "d3d12_video_types.h"
29 
30 // From DXVA spec regarding DXVA_PicEntry_HEVC entries:
31 // Entries that will not be used for decoding the current picture, or any subsequent pictures,
32 // are indicated by setting bPicEntry to 0xFF.
33 // If bPicEntry is not 0xFF, the entry may be used as a reference surface for decoding the current picture or
34 // a subsequent picture (in decoding order).
35 constexpr uint16_t DXVA_HEVC_INVALID_PICTURE_INDEX =
36    0x7F;   // This corresponds to DXVA_PicEntry_HEVC.Index7Bits ; Not to be confused with the invalid value for
37            // DXVA_PicEntry_HEVC.bPicEntry full uint8_t value
38 constexpr uint16_t DXVA_HEVC_INVALID_PICTURE_ENTRY_VALUE = 0xFF;   // This corresponds to DXVA_PicEntry_HEVC.bPicEntry
39 
40 constexpr unsigned int DXVA_HEVC_START_CODE          = 0x000001;   // 3 byte start code
41 constexpr unsigned int DXVA_HEVC_START_CODE_LEN_BITS = 24;         // 3 byte start code
42 #define DXVA_RPS_COUNT 8
43 
44 /* HEVC picture entry data structure */
45 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
46 #pragma pack(push, BeforeDXVApacking, 1)
47 typedef struct _DXVA_PicEntry_HEVC
48 {
49    union
50    {
51       struct
52       {
53          uint8_t Index7Bits : 7;
54          uint8_t AssociatedFlag : 1;
55       };
56       uint8_t bPicEntry;
57    };
58 } DXVA_PicEntry_HEVC, *LPDXVA_PicEntry_HEVC; /* 1 byte */
59 #pragma pack(pop, BeforeDXVApacking)
60 
61 /* HEVC picture parameters structure */
62 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
63 #pragma pack(push, BeforeDXVApacking, 1)
64 /* HEVC Picture Parameter structure */
65 typedef struct _DXVA_PicParams_HEVC {
66     uint16_t      PicWidthInMinCbsY;
67     uint16_t      PicHeightInMinCbsY;
68     union {
69         struct {
70             uint16_t  chroma_format_idc                       : 2;
71             uint16_t  separate_colour_plane_flag              : 1;
72             uint16_t  bit_depth_luma_minus8                   : 3;
73             uint16_t  bit_depth_chroma_minus8                 : 3;
74             uint16_t  log2_max_pic_order_cnt_lsb_minus4       : 4;
75             uint16_t  NoPicReorderingFlag                     : 1;
76             uint16_t  NoBiPredFlag                            : 1;
77             uint16_t  ReservedBits1                            : 1;
78         };
79         uint16_t wFormatAndSequenceInfoFlags;
80     };
81     DXVA_PicEntry_HEVC  CurrPic;
82     uint8_t   sps_max_dec_pic_buffering_minus1;
83     uint8_t   log2_min_luma_coding_block_size_minus3;
84     uint8_t   log2_diff_max_min_luma_coding_block_size;
85     uint8_t   log2_min_transform_block_size_minus2;
86     uint8_t   log2_diff_max_min_transform_block_size;
87     uint8_t   max_transform_hierarchy_depth_inter;
88     uint8_t   max_transform_hierarchy_depth_intra;
89     uint8_t   num_short_term_ref_pic_sets;
90     uint8_t   num_long_term_ref_pics_sps;
91     uint8_t   num_ref_idx_l0_default_active_minus1;
92     uint8_t   num_ref_idx_l1_default_active_minus1;
93     uint8_t    init_qp_minus26;
94     uint8_t   ucNumDeltaPocsOfRefRpsIdx;
95     uint16_t  wNumBitsForShortTermRPSInSlice;
96     uint16_t  ReservedBits2;
97 
98     union {
99         struct {
100             uint32_t  scaling_list_enabled_flag                    : 1;
101             uint32_t  amp_enabled_flag                            : 1;
102             uint32_t  sample_adaptive_offset_enabled_flag         : 1;
103             uint32_t  pcm_enabled_flag                            : 1;
104             uint32_t  pcm_sample_bit_depth_luma_minus1            : 4;
105             uint32_t  pcm_sample_bit_depth_chroma_minus1          : 4;
106             uint32_t  log2_min_pcm_luma_coding_block_size_minus3  : 2;
107             uint32_t  log2_diff_max_min_pcm_luma_coding_block_size : 2;
108             uint32_t  pcm_loop_filter_disabled_flag                : 1;
109             uint32_t  long_term_ref_pics_present_flag             : 1;
110             uint32_t  sps_temporal_mvp_enabled_flag               : 1;
111             uint32_t  strong_intra_smoothing_enabled_flag         : 1;
112             uint32_t  dependent_slice_segments_enabled_flag       : 1;
113             uint32_t  output_flag_present_flag                    : 1;
114             uint32_t  num_extra_slice_header_bits                 : 3;
115             uint32_t  sign_data_hiding_enabled_flag               : 1;
116             uint32_t  cabac_init_present_flag                     : 1;
117             uint32_t  ReservedBits3                               : 5;
118         };
119         uint32_t dwCodingParamToolFlags;
120     };
121 
122     union {
123         struct {
124             uint32_t  constrained_intra_pred_flag                 : 1;
125             uint32_t  transform_skip_enabled_flag                 : 1;
126             uint32_t  cu_qp_delta_enabled_flag                    : 1;
127             uint32_t  pps_slice_chroma_qp_offsets_present_flag    : 1;
128             uint32_t  weighted_pred_flag                          : 1;
129             uint32_t  weighted_bipred_flag                        : 1;
130             uint32_t  transquant_bypass_enabled_flag              : 1;
131             uint32_t  tiles_enabled_flag                          : 1;
132             uint32_t  entropy_coding_sync_enabled_flag            : 1;
133             uint32_t  uniform_spacing_flag                        : 1;
134             uint32_t  loop_filter_across_tiles_enabled_flag       : 1;
135             uint32_t  pps_loop_filter_across_slices_enabled_flag  : 1;
136             uint32_t  deblocking_filter_override_enabled_flag     : 1;
137             uint32_t  pps_deblocking_filter_disabled_flag         : 1;
138             uint32_t  lists_modification_present_flag             : 1;
139             uint32_t  slice_segment_header_extension_present_flag : 1;
140             uint32_t  IrapPicFlag                                 : 1;
141             uint32_t  IdrPicFlag                                  : 1;
142             uint32_t  IntraPicFlag                                : 1;
143             uint32_t  ReservedBits4                               : 13;
144         };
145         uint32_t dwCodingSettingPicturePropertyFlags;
146     };
147     uint8_t    pps_cb_qp_offset;
148     uint8_t    pps_cr_qp_offset;
149     uint8_t   num_tile_columns_minus1;
150     uint8_t   num_tile_rows_minus1;
151     uint16_t  column_width_minus1[19];
152     uint16_t  row_height_minus1[21];
153     uint8_t   diff_cu_qp_delta_depth;
154     uint8_t    pps_beta_offset_div2;
155     uint8_t    pps_tc_offset_div2;
156     uint8_t   log2_parallel_merge_level_minus2;
157     int32_t     CurrPicOrderCntVal;
158     DXVA_PicEntry_HEVC	RefPicList[15];
159     uint8_t   ReservedBits5;
160     int32_t     PicOrderCntValList[15];
161     uint8_t   RefPicSetStCurrBefore[8];
162     uint8_t   RefPicSetStCurrAfter[8];
163     uint8_t   RefPicSetLtCurr[8];
164     uint16_t  ReservedBits6;
165     uint16_t  ReservedBits7;
166     uint32_t    StatusReportFeedbackNumber;
167 } DXVA_PicParams_HEVC, *LPDXVA_PicParams_HEVC;
168 #pragma pack(pop, BeforeDXVApacking)
169 
170 static_assert(_countof(DXVA_PicParams_HEVC::RefPicSetStCurrBefore) == DXVA_RPS_COUNT,
171     "DXVA_PicParams_HEVC::RefPicSetStCurrBefore must have DXVA_RPS_COUNT elements");
172 static_assert(_countof(DXVA_PicParams_HEVC::RefPicSetStCurrAfter) == DXVA_RPS_COUNT,
173     "DXVA_PicParams_HEVC::RefPicSetStCurrAfter must have DXVA_RPS_COUNT elements");
174 static_assert(_countof(DXVA_PicParams_HEVC::RefPicSetLtCurr) == DXVA_RPS_COUNT,
175     "DXVA_PicParams_HEVC::RefPicSetLtCurr must have DXVA_RPS_COUNT elements");
176 
177 /* HEVC Quantization Matrix structure */
178 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
179 #pragma pack(push, BeforeDXVApacking, 1)
180 typedef struct _DXVA_Qmatrix_HEVC
181 {
182     uint8_t ucScalingLists0[6][16];
183     uint8_t ucScalingLists1[6][64];
184     uint8_t ucScalingLists2[6][64];
185     uint8_t ucScalingLists3[2][64];
186     uint8_t ucScalingListDCCoefSizeID2[6];
187     uint8_t ucScalingListDCCoefSizeID3[2];
188 } DXVA_Qmatrix_HEVC, *LPDXVA_Qmatrix_HEVC;
189 #pragma pack(pop, BeforeDXVApacking)
190 
191 /* HEVC slice control data structure - short form */
192 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
193 #pragma pack(push, BeforeDXVApacking, 1)
194 typedef struct _DXVA_Slice_HEVC_Short
195 {
196    uint32_t BSNALunitDataLocation; /* type 1..5 */
197    uint32_t SliceBytesInBuffer;    /* for off-host parse */
198    uint16_t wBadSliceChopping;     /* for off-host parse */
199 } DXVA_Slice_HEVC_Short, *LPDXVA_Slice_HEVC_Short;
200 #pragma pack(pop, BeforeDXVApacking)
201 
202 #pragma pack(push, BeforeDXVApacking, 1)
203 typedef struct _DXVA_Status_HEVC {
204     uint16_t StatusReportFeedbackNumber;
205     DXVA_PicEntry_HEVC CurrPic;
206     uint8_t  bBufType;
207     uint8_t  bStatus;
208     uint8_t  bReserved8Bits;
209     uint16_t wNumMbsAffected;
210 } DXVA_Status_HEVC, *LPDXVA_Status_HEVC;
211 #pragma pack(pop, BeforeDXVApacking)
212 
213 DXVA_PicParams_HEVC
214 d3d12_video_decoder_dxva_picparams_from_pipe_picparams_hevc(struct d3d12_video_decoder *pD3D12Dec,
215                                                             pipe_video_profile          profile,
216                                                             pipe_h265_picture_desc *    pipeDesc);
217 void
218 d3d12_video_decoder_get_frame_info_hevc(
219    struct d3d12_video_decoder *pD3D12Dec, uint32_t *pWidth, uint32_t *pHeight, uint16_t *pMaxDPB);
220 void
221 d3d12_video_decoder_prepare_current_frame_references_hevc(struct d3d12_video_decoder *pD3D12Dec,
222                                                           ID3D12Resource *            pTexture2D,
223                                                           uint32_t                    subresourceIndex);
224 void
225 d3d12_video_decoder_dxva_qmatrix_from_pipe_picparams_hevc(pipe_h265_picture_desc *pPipeDesc,
226                                                           DXVA_Qmatrix_HEVC &     outMatrixBuffer,
227                                                           bool &outScalingListEnabled);
228 void
229 d3d12_video_decoder_refresh_dpb_active_references_hevc(struct d3d12_video_decoder *pD3D12Dec);
230 bool
231 d3d12_video_decoder_get_next_slice_size_and_offset_hevc(std::vector<uint8_t> &buf,
232                                                    unsigned int          bufferOffset,
233                                                    uint32_t &            outSliceSize,
234                                                    uint32_t &            outSliceOffset);
235 
236 uint
237 d3d12_video_decoder_get_slice_count_hevc(std::vector<uint8_t> &buf);
238 
239 void
240 d3d12_video_decoder_prepare_dxva_slices_control_hevc(struct d3d12_video_decoder *        pD3D12Dec,
241                                                      std::vector<uint8_t> &vecOutSliceControlBuffers,
242                                                      struct pipe_h265_picture_desc* picture_hevc);
243 
244 void
245 d3d12_video_decoder_log_pic_params_hevc(DXVA_PicParams_HEVC * pPicParams);
246 
247 #endif
248