1 /*
2  * Copyright © 2021 Red Hat
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 (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21  * IN THE SOFTWARE.
22  */
23 #ifndef VK_VIDEO_H
24 #define VK_VIDEO_H
25 
26 #include "vk_object.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31 
32 struct vk_video_session {
33    struct vk_object_base base;
34    VkVideoCodecOperationFlagsKHR op;
35    VkExtent2D max_coded;
36    VkFormat picture_format;
37    VkFormat ref_format;
38    uint32_t max_dpb_slots;
39    uint32_t max_active_ref_pics;
40 
41    union {
42       struct {
43          StdVideoH264ProfileIdc profile_idc;
44       } h264;
45       struct {
46          StdVideoH265ProfileIdc profile_idc;
47       } h265;
48    };
49 };
50 
51 struct vk_video_session_parameters {
52    struct vk_object_base base;
53    VkVideoCodecOperationFlagsKHR op;
54    union {
55       struct {
56          uint32_t max_std_sps_count;
57          uint32_t max_std_pps_count;
58 
59          uint32_t std_sps_count;
60          StdVideoH264SequenceParameterSet *std_sps;
61          uint32_t std_pps_count;
62          StdVideoH264PictureParameterSet *std_pps;
63       } h264_dec;
64 
65       struct {
66          uint32_t max_std_vps_count;
67          uint32_t max_std_sps_count;
68          uint32_t max_std_pps_count;
69 
70          uint32_t std_vps_count;
71          StdVideoH265VideoParameterSet *std_vps;
72          uint32_t std_sps_count;
73          StdVideoH265SequenceParameterSet *std_sps;
74          uint32_t std_pps_count;
75          StdVideoH265PictureParameterSet *std_pps;
76       } h265_dec;
77    };
78 };
79 
80 VkResult vk_video_session_init(struct vk_device *device,
81                                struct vk_video_session *vid,
82                                const VkVideoSessionCreateInfoKHR *create_info);
83 
84 VkResult vk_video_session_parameters_init(struct vk_device *device,
85                                           struct vk_video_session_parameters *params,
86                                           const struct vk_video_session *vid,
87                                           const struct vk_video_session_parameters *templ,
88                                           const VkVideoSessionParametersCreateInfoKHR *create_info);
89 
90 VkResult vk_video_session_parameters_update(struct vk_video_session_parameters *params,
91                                             const VkVideoSessionParametersUpdateInfoKHR *update);
92 
93 void vk_video_session_parameters_finish(struct vk_device *device,
94                                         struct vk_video_session_parameters *params);
95 
96 void vk_video_derive_h264_scaling_list(const StdVideoH264SequenceParameterSet *sps,
97                                        const StdVideoH264PictureParameterSet *pps,
98                                        StdVideoH264ScalingLists *list);
99 
100 const StdVideoH264SequenceParameterSet *
101 vk_video_find_h264_dec_std_sps(const struct vk_video_session_parameters *params,
102                                uint32_t id);
103 const StdVideoH264PictureParameterSet *
104 vk_video_find_h264_dec_std_pps(const struct vk_video_session_parameters *params,
105                                uint32_t id);
106 const StdVideoH265VideoParameterSet *
107 vk_video_find_h265_dec_std_vps(const struct vk_video_session_parameters *params,
108                                uint32_t id);
109 const StdVideoH265SequenceParameterSet *
110 vk_video_find_h265_dec_std_sps(const struct vk_video_session_parameters *params,
111                                uint32_t id);
112 const StdVideoH265PictureParameterSet *
113 vk_video_find_h265_dec_std_pps(const struct vk_video_session_parameters *params,
114                                uint32_t id);
115 
116 struct vk_video_h265_slice_params {
117    uint32_t slice_size;
118 
119    uint8_t first_slice_segment_in_pic_flag;
120    StdVideoH265SliceType slice_type;
121    uint8_t dependent_slice_segment;
122    uint8_t temporal_mvp_enable;
123    uint8_t loop_filter_across_slices_enable;
124    int32_t pic_order_cnt_lsb;
125    uint8_t sao_luma_flag;
126    uint8_t sao_chroma_flag;
127    uint8_t collocated_list;
128    uint32_t collocated_ref_idx;
129    uint8_t mvd_l1_zero_flag;
130 
131    uint8_t num_ref_idx_l0_active;
132    uint8_t num_ref_idx_l1_active;
133    uint8_t rpl_modification_flag[2];
134    uint8_t cabac_init_idc;
135    int8_t slice_qp_delta;
136    int8_t slice_cb_qp_offset;
137    int8_t slice_cr_qp_offset;
138    int8_t max_num_merge_cand;
139    uint32_t slice_data_bytes_offset;
140    uint8_t disable_deblocking_filter_idc;
141    int8_t tc_offset_div2;
142    int8_t beta_offset_div2;
143    uint32_t slice_segment_address;
144 
145    uint8_t luma_log2_weight_denom;
146    uint8_t chroma_log2_weight_denom;
147    uint8_t luma_weight_l0_flag[16];
148    int16_t luma_weight_l0[16];
149    int16_t luma_offset_l0[16];
150    uint8_t chroma_weight_l0_flag[16];
151    int16_t chroma_weight_l0[16][2];
152    int16_t chroma_offset_l0[16][2];
153    uint8_t luma_weight_l1_flag[16];
154    int16_t luma_weight_l1[16];
155    int16_t luma_offset_l1[16];
156    uint8_t chroma_weight_l1_flag[16];
157    int16_t chroma_weight_l1[16][2];
158    int16_t chroma_offset_l1[16][2];
159 
160    int8_t delta_luma_weight_l0[16];
161    int8_t delta_luma_weight_l1[16];
162    int8_t delta_chroma_weight_l0[16][2];
163    int8_t delta_chroma_weight_l1[16][2];
164    int16_t delta_chroma_offset_l0[16][2];
165    int16_t delta_chroma_offset_l1[16][2];
166 };
167 
168 void
169 vk_video_parse_h265_slice_header(const struct VkVideoDecodeInfoKHR *frame_info,
170                                  const VkVideoDecodeH265PictureInfoKHR *pic_info,
171                                  const StdVideoH265SequenceParameterSet *sps,
172                                  const StdVideoH265PictureParameterSet *pps,
173                                  void *slice_data,
174                                  uint32_t slice_size,
175                                  struct vk_video_h265_slice_params *params);
176 
177 
178 struct vk_video_h265_reference {
179    const VkVideoPictureResourceInfoKHR *pPictureResource;
180    StdVideoDecodeH265ReferenceInfoFlags flags;
181    uint32_t slot_index;
182    int32_t pic_order_cnt;
183 };
184 
185 int vk_video_h265_poc_by_slot(const struct VkVideoDecodeInfoKHR *frame_info, int slot);
186 
187 void vk_fill_video_h265_reference_info(const VkVideoDecodeInfoKHR *frame_info,
188                                        const struct VkVideoDecodeH265PictureInfoKHR *pic,
189                                        const struct vk_video_h265_slice_params *slice_params,
190                                        struct vk_video_h265_reference ref_slots[][8]);
191 
192 #define VK_VIDEO_H264_MACROBLOCK_WIDTH 16
193 #define VK_VIDEO_H264_MACROBLOCK_HEIGHT 16
194 
195 #define VK_VIDEO_H265_CTU_MAX_WIDTH 64
196 #define VK_VIDEO_H265_CTU_MAX_HEIGHT 64
197 
198 void
199 vk_video_get_profile_alignments(const VkVideoProfileListInfoKHR *profile_list,
200                                 uint32_t *width_align_out, uint32_t *height_align_out);
201 #ifdef __cplusplus
202 }
203 #endif
204 
205 #endif
206