xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/d3d12/d3d12_video_dec_h264.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_H264_H
26 #define D3D12_VIDEO_DEC_H264_H
27 
28 #include "d3d12_video_types.h"
29 
30 // From DXVA spec regarding DXVA_PicEntry_H264 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_H264_INVALID_PICTURE_INDEX =
36    0x7F;   // This corresponds to DXVA_PicEntry_H264.Index7Bits ; Not to be confused with the invalid value for
37            // DXVA_PicEntry_H264.bPicEntry full char value
38 constexpr uint16_t DXVA_H264_INVALID_PICTURE_ENTRY_VALUE = 0xFF;   // This corresponds to DXVA_PicEntry_H264.bPicEntry
39 
40 constexpr unsigned int DXVA_H264_START_CODE          = 0x000001;   // 3 byte start code
41 constexpr unsigned int DXVA_H264_START_CODE_LEN_BITS = 24;         // 3 byte start code
42 
43 enum d3d12_video_decoder_nal_unit_type_h264
44 {
45     type_unspecified = 0,
46     type_slice = 1,
47     type_slice_part_A = 2,
48     type_slice_part_B = 3,
49     type_slice_part_C = 4,
50     type_slice_IDR = 5,
51     type_SEI = 6,
52     type_SPS = 7,
53     type_PPS = 8,
54     type_acces_delimiter = 9,
55     type_EOSeq = 10,
56     type_EOStr = 11,
57     type_EOData = 12,
58     type_SPSxt = 13,
59     type_prefix_nal_unit = 14,
60     type_subset_SPS = 15,
61     type_reserved16 = 16,
62     type_reserved17 = 17,
63     type_reserved18 = 18,
64     type_slice_aux = 19,
65     type_slice_layer_ext = 20,
66     type_reserved21 = 21,
67     type_reserved22 = 22,
68     type_reserved23 = 23,
69     type_unspecified24 = 24,
70     type_unspecified25 = 25,
71     type_unspecified26 = 26,
72     type_unspecified27 = 27,
73     type_unspecified28 = 28,
74     type_unspecified29 = 29,
75     type_unspecified30 = 30,
76     type_unspecified31 = 31
77 };
78 
79 /* H.264/AVC picture entry data structure */
80 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
81 #pragma pack(push, BeforeDXVApacking, 1)
82 typedef struct _DXVA_PicEntry_H264
83 {
84    union
85    {
86       struct
87       {
88          uint8_t Index7Bits : 7;
89          uint8_t AssociatedFlag : 1;
90       };
91       uint8_t bPicEntry;
92    };
93 } DXVA_PicEntry_H264, *LPDXVA_PicEntry_H264; /* 1 byte */
94 #pragma pack(pop, BeforeDXVApacking)
95 
96 /* H.264/AVC picture parameters structure */
97 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
98 #pragma pack(push, BeforeDXVApacking, 1)
99 typedef struct _DXVA_PicParams_H264
100 {
101    uint16_t           wFrameWidthInMbsMinus1;
102    uint16_t           wFrameHeightInMbsMinus1;
103    DXVA_PicEntry_H264 CurrPic; /* flag is bot field flag */
104    uint8_t            num_ref_frames;
105 
106    union
107    {
108       struct
109       {
110          uint16_t field_pic_flag : 1;
111          uint16_t MbaffFrameFlag : 1;
112          uint16_t residual_colour_transform_flag : 1;
113          uint16_t sp_for_switch_flag : 1;
114          uint16_t chroma_format_idc : 2;
115          uint16_t RefPicFlag : 1;
116          uint16_t constrained_intra_pred_flag : 1;
117 
118          uint16_t weighted_pred_flag : 1;
119          uint16_t weighted_bipred_idc : 2;
120          uint16_t MbsConsecutiveFlag : 1;
121          uint16_t frame_mbs_only_flag : 1;
122          uint16_t transform_8x8_mode_flag : 1;
123          uint16_t MinLumaBipredSize8x8Flag : 1;
124          uint16_t IntraPicFlag : 1;
125       };
126       uint16_t wBitFields;
127    };
128    uint8_t bit_depth_luma_minus8;
129    uint8_t bit_depth_chroma_minus8;
130 
131    uint16_t Reserved16Bits;
132    uint32_t StatusReportFeedbackNumber;
133 
134    DXVA_PicEntry_H264 RefFrameList[16]; /* flag LT */
135    int32_t            CurrFieldOrderCnt[2];
136    int32_t            FieldOrderCntList[16][2];
137 
138    char    pic_init_qs_minus26;
139    char    chroma_qp_index_offset;        /* also used for QScb */
140    char    second_chroma_qp_index_offset; /* also for QScr */
141    uint8_t ContinuationFlag;
142 
143    /* remainder for parsing */
144    char    pic_init_qp_minus26;
145    uint8_t num_ref_idx_l0_active_minus1;
146    uint8_t num_ref_idx_l1_active_minus1;
147    uint8_t Reserved8BitsA;
148 
149    uint16_t FrameNumList[16];
150    uint32_t UsedForReferenceFlags;
151    uint16_t NonExistingFrameFlags;
152    uint16_t frame_num;
153 
154    uint8_t log2_max_frame_num_minus4;
155    uint8_t pic_order_cnt_type;
156    uint8_t log2_max_pic_order_cnt_lsb_minus4;
157    uint8_t delta_pic_order_always_zero_flag;
158 
159    uint8_t direct_8x8_inference_flag;
160    uint8_t entropy_coding_mode_flag;
161    uint8_t pic_order_present_flag;
162    uint8_t num_slice_groups_minus1;
163 
164    uint8_t slice_group_map_type;
165    uint8_t deblocking_filter_control_present_flag;
166    uint8_t redundant_pic_cnt_present_flag;
167    uint8_t Reserved8BitsB;
168 
169    uint16_t slice_group_change_rate_minus1;
170 
171    uint8_t SliceGroupMap[810]; /* 4b/sgmu, Size BT.601 */
172 
173 } DXVA_PicParams_H264, *LPDXVA_PicParams_H264;
174 #pragma pack(pop, BeforeDXVApacking)
175 
176 /* H.264/AVC quantization weighting matrix data structure */
177 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
178 #pragma pack(push, BeforeDXVApacking, 1)
179 typedef struct _DXVA_Qmatrix_H264
180 {
181    uint8_t bScalingLists4x4[6][16];
182    uint8_t bScalingLists8x8[2][64];
183 
184 } DXVA_Qmatrix_H264, *LPDXVA_Qmatrix_H264;
185 #pragma pack(pop, BeforeDXVApacking)
186 
187 /* H.264/AVC slice control data structure - short form */
188 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
189 #pragma pack(push, BeforeDXVApacking, 1)
190 typedef struct _DXVA_Slice_H264_Short
191 {
192    uint32_t BSNALunitDataLocation; /* type 1..5 */
193    uint32_t SliceBytesInBuffer;    /* for off-host parse */
194    uint16_t wBadSliceChopping;     /* for off-host parse */
195 } DXVA_Slice_H264_Short, *LPDXVA_Slice_H264_Short;
196 #pragma pack(pop, BeforeDXVApacking)
197 
198 DXVA_PicParams_H264
199 d3d12_video_decoder_dxva_picparams_from_pipe_picparams_h264(uint32_t                frameNum,
200                                                             pipe_video_profile      profile,
201                                                             uint32_t                frameWidth,
202                                                             uint32_t                frameHeight,
203                                                             pipe_h264_picture_desc *pipeDesc);
204 void
205 d3d12_video_decoder_get_frame_info_h264(
206    struct d3d12_video_decoder *pD3D12Dec, uint32_t *pWidth, uint32_t *pHeight, uint16_t *pMaxDPB);
207 void
208 d3d12_video_decoder_prepare_current_frame_references_h264(struct d3d12_video_decoder *pD3D12Dec,
209                                                           ID3D12Resource *            pTexture2D,
210                                                           uint32_t                    subresourceIndex);
211 void
212 d3d12_video_decoder_dxva_qmatrix_from_pipe_picparams_h264(pipe_h264_picture_desc *pPipeDesc,
213                                                           DXVA_Qmatrix_H264 &     outMatrixBuffer);
214 void
215 d3d12_video_decoder_refresh_dpb_active_references_h264(struct d3d12_video_decoder *pD3D12Dec);
216 
217 uint
218 d3d12_video_decoder_get_slice_count_h264(std::vector<uint8_t> &buf);
219 
220 void
221 d3d12_video_decoder_prepare_dxva_slices_control_h264(struct d3d12_video_decoder *        pD3D12Dec,
222                                                      std::vector<uint8_t> &vecOutSliceControlBuffers,
223                                                      struct pipe_h264_picture_desc* picture_h264);
224 
225 void
226 d3d12_video_decoder_log_pic_params_h264(DXVA_PicParams_H264 * pPicParams);
227 
228 #endif
229