• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 // For translating the QP matrices from VA to DXVA
188 const uint8_t d3d12_video_zigzag_direct[64] = {
189    0,   1,  8, 16,  9,  2,  3, 10,
190    17, 24, 32, 25, 18, 11,  4,  5,
191    12, 19, 26, 33, 40, 48, 41, 34,
192    27, 20, 13,  6,  7, 14, 21, 28,
193    35, 42, 49, 56, 57, 50, 43, 36,
194    29, 22, 15, 23, 30, 37, 44, 51,
195    58, 59, 52, 45, 38, 31, 39, 46,
196    53, 60, 61, 54, 47, 55, 62, 63
197 };
198 
199 // For translating the QP matrices from VA to DXVA
200 const uint8_t d3d12_video_zigzag_scan[16+1] = {
201    0 + 0 * 4, 1 + 0 * 4, 0 + 1 * 4, 0 + 2 * 4,
202    1 + 1 * 4, 2 + 0 * 4, 3 + 0 * 4, 2 + 1 * 4,
203    1 + 2 * 4, 0 + 3 * 4, 1 + 3 * 4, 2 + 2 * 4,
204    3 + 1 * 4, 3 + 2 * 4, 2 + 3 * 4, 3 + 3 * 4,
205 };
206 
207 /* H.264/AVC slice control data structure - short form */
208 /* If including new DXVA structs in this header, check the byte-alignment packing pragma declarations that need to be included with them */
209 #pragma pack(push, BeforeDXVApacking, 1)
210 typedef struct _DXVA_Slice_H264_Short
211 {
212    uint32_t BSNALunitDataLocation; /* type 1..5 */
213    uint32_t SliceBytesInBuffer;    /* for off-host parse */
214    uint16_t wBadSliceChopping;     /* for off-host parse */
215 } DXVA_Slice_H264_Short, *LPDXVA_Slice_H264_Short;
216 #pragma pack(pop, BeforeDXVApacking)
217 
218 DXVA_PicParams_H264
219 d3d12_video_decoder_dxva_picparams_from_pipe_picparams_h264(uint32_t                frameNum,
220                                                             pipe_video_profile      profile,
221                                                             uint32_t                frameWidth,
222                                                             uint32_t                frameHeight,
223                                                             pipe_h264_picture_desc *pipeDesc);
224 void
225 d3d12_video_decoder_get_frame_info_h264(
226    struct d3d12_video_decoder *pD3D12Dec, uint32_t *pWidth, uint32_t *pHeight, uint16_t *pMaxDPB, bool &isInterlaced);
227 void
228 d3d12_video_decoder_prepare_current_frame_references_h264(struct d3d12_video_decoder *pD3D12Dec,
229                                                           ID3D12Resource *            pTexture2D,
230                                                           uint32_t                    subresourceIndex);
231 void
232 d3d12_video_decoder_dxva_qmatrix_from_pipe_picparams_h264(pipe_h264_picture_desc *pPipeDesc,
233                                                           DXVA_Qmatrix_H264 &     outMatrixBuffer);
234 void
235 d3d12_video_decoder_refresh_dpb_active_references_h264(struct d3d12_video_decoder *pD3D12Dec);
236 bool
237 d3d12_video_decoder_get_next_slice_size_and_offset_h264(std::vector<uint8_t> &buf,
238                                                    unsigned int          bufferOffset,
239                                                    uint32_t &            outSliceSize,
240                                                    uint32_t &            outSliceOffset);
241 
242 uint
243 d3d12_video_decoder_get_slice_count_h264(std::vector<uint8_t> &buf);
244 
245 void
246 d3d12_video_decoder_prepare_dxva_slices_control_h264(struct d3d12_video_decoder *        pD3D12Dec,
247                                                      std::vector<DXVA_Slice_H264_Short> &pOutSliceControlBuffers,
248                                                      struct pipe_h264_picture_desc* picture_h264);
249 
250 void
251 d3d12_video_decoder_log_pic_params_h264(DXVA_PicParams_H264 * pPicParams);
252 
253 #endif
254