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
24 #include "vk_video.h"
25 #include "vk_util.h"
26 #include "vk_log.h"
27 #include "vk_alloc.h"
28 #include "vk_device.h"
29 #include "util/vl_rbsp.h"
30 #include "util/vl_bitstream.h"
31
32 VkResult
vk_video_session_init(struct vk_device * device,struct vk_video_session * vid,const VkVideoSessionCreateInfoKHR * create_info)33 vk_video_session_init(struct vk_device *device,
34 struct vk_video_session *vid,
35 const VkVideoSessionCreateInfoKHR *create_info)
36 {
37 vk_object_base_init(device, &vid->base, VK_OBJECT_TYPE_VIDEO_SESSION_KHR);
38
39 vid->flags = create_info->flags;
40 vid->op = create_info->pVideoProfile->videoCodecOperation;
41 vid->max_coded = create_info->maxCodedExtent;
42 vid->picture_format = create_info->pictureFormat;
43 vid->ref_format = create_info->referencePictureFormat;
44 vid->max_dpb_slots = create_info->maxDpbSlots;
45 vid->max_active_ref_pics = create_info->maxActiveReferencePictures;
46
47 switch (vid->op) {
48 case VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR: {
49 const struct VkVideoDecodeH264ProfileInfoKHR *h264_profile =
50 vk_find_struct_const(create_info->pVideoProfile->pNext,
51 VIDEO_DECODE_H264_PROFILE_INFO_KHR);
52 vid->h264.profile_idc = h264_profile->stdProfileIdc;
53 break;
54 }
55 case VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR: {
56 const struct VkVideoDecodeH265ProfileInfoKHR *h265_profile =
57 vk_find_struct_const(create_info->pVideoProfile->pNext,
58 VIDEO_DECODE_H265_PROFILE_INFO_KHR);
59 vid->h265.profile_idc = h265_profile->stdProfileIdc;
60 break;
61 }
62 case VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR: {
63 const struct VkVideoDecodeAV1ProfileInfoKHR *av1_profile =
64 vk_find_struct_const(create_info->pVideoProfile->pNext,
65 VIDEO_DECODE_AV1_PROFILE_INFO_KHR);
66 vid->av1.profile = av1_profile->stdProfile;
67 vid->av1.film_grain_support = av1_profile->filmGrainSupport;
68 break;
69 };
70 case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
71 const struct VkVideoEncodeH264ProfileInfoKHR *h264_profile =
72 vk_find_struct_const(create_info->pVideoProfile->pNext, VIDEO_ENCODE_H264_PROFILE_INFO_KHR);
73 vid->h264.profile_idc = h264_profile->stdProfileIdc;
74 break;
75 }
76 case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
77 const struct VkVideoEncodeH265ProfileInfoKHR *h265_profile =
78 vk_find_struct_const(create_info->pVideoProfile->pNext, VIDEO_ENCODE_H265_PROFILE_INFO_KHR);
79 vid->h265.profile_idc = h265_profile->stdProfileIdc;
80 break;
81 }
82 default:
83 return VK_ERROR_FEATURE_NOT_PRESENT;
84 }
85
86 if (vid->op == VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR ||
87 vid->op == VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR) {
88 const struct VkVideoEncodeUsageInfoKHR *encode_usage_profile =
89 vk_find_struct_const(create_info->pVideoProfile->pNext, VIDEO_ENCODE_USAGE_INFO_KHR);
90 if (encode_usage_profile) {
91 vid->enc_usage.video_usage_hints = encode_usage_profile->videoUsageHints;
92 vid->enc_usage.video_content_hints = encode_usage_profile->videoContentHints;
93 vid->enc_usage.tuning_mode = encode_usage_profile->tuningMode;
94 } else {
95 vid->enc_usage.video_usage_hints = VK_VIDEO_ENCODE_USAGE_DEFAULT_KHR;
96 vid->enc_usage.video_content_hints = VK_VIDEO_ENCODE_CONTENT_DEFAULT_KHR;
97 vid->enc_usage.tuning_mode = VK_VIDEO_ENCODE_TUNING_MODE_DEFAULT_KHR;
98 }
99 }
100
101 return VK_SUCCESS;
102 }
103
104 static void
vk_video_deep_copy_h264_sps(struct vk_video_h264_sps * dst,const StdVideoH264SequenceParameterSet * src)105 vk_video_deep_copy_h264_sps(struct vk_video_h264_sps *dst,
106 const StdVideoH264SequenceParameterSet *src)
107 {
108 memcpy(&dst->base, src, sizeof(StdVideoH264SequenceParameterSet));
109 if (src->num_ref_frames_in_pic_order_cnt_cycle && src->pOffsetForRefFrame) {
110 memcpy(dst->offsets_for_ref_frame, src->pOffsetForRefFrame, sizeof(int32_t) * src->num_ref_frames_in_pic_order_cnt_cycle);
111 dst->base.pOffsetForRefFrame = dst->offsets_for_ref_frame;
112 }
113 if (src->flags.seq_scaling_matrix_present_flag && src->pScalingLists) {
114 memcpy(&dst->scaling_lists, src->pScalingLists, sizeof(StdVideoH264ScalingLists));
115 dst->base.pScalingLists = &dst->scaling_lists;
116 }
117 if (src->flags.vui_parameters_present_flag && src->pSequenceParameterSetVui) {
118 memcpy(&dst->vui, src->pSequenceParameterSetVui, sizeof(StdVideoH264SequenceParameterSetVui));
119 dst->base.pSequenceParameterSetVui = &dst->vui;
120
121 if (src->pSequenceParameterSetVui->pHrdParameters) {
122 memcpy(&dst->vui_hrd_parameters, src->pSequenceParameterSetVui->pHrdParameters,
123 sizeof(StdVideoH264HrdParameters));
124 dst->vui.pHrdParameters = &dst->vui_hrd_parameters;
125 }
126 }
127 }
128
129 static void
vk_video_deep_copy_h264_pps(struct vk_video_h264_pps * dst,const StdVideoH264PictureParameterSet * src)130 vk_video_deep_copy_h264_pps(struct vk_video_h264_pps *dst,
131 const StdVideoH264PictureParameterSet *src)
132 {
133 memcpy(&dst->base, src, sizeof(StdVideoH264PictureParameterSet));
134 if (src->flags.pic_scaling_matrix_present_flag && src->pScalingLists) {
135 memcpy(&dst->scaling_lists, src->pScalingLists, sizeof(StdVideoH264ScalingLists));
136 dst->base.pScalingLists = &dst->scaling_lists;
137 }
138 }
139
140 static void
vk_video_deep_copy_h265_vps(struct vk_video_h265_vps * dst,const StdVideoH265VideoParameterSet * src)141 vk_video_deep_copy_h265_vps(struct vk_video_h265_vps *dst,
142 const StdVideoH265VideoParameterSet *src)
143 {
144 memcpy(&dst->base, src, sizeof(StdVideoH265VideoParameterSet));
145 if (src->pDecPicBufMgr) {
146 memcpy(&dst->dec_pic_buf_mgr, src->pDecPicBufMgr, sizeof(StdVideoH265DecPicBufMgr));
147 dst->base.pDecPicBufMgr = &dst->dec_pic_buf_mgr;
148 }
149 if (src->pHrdParameters) {
150 memcpy(&dst->hrd_parameters, src->pHrdParameters, sizeof(StdVideoH265HrdParameters));
151 dst->base.pHrdParameters = &dst->hrd_parameters;
152 if (src->pHrdParameters->pSubLayerHrdParametersNal) {
153 memcpy(&dst->hrd_parameters_nal, src->pHrdParameters->pSubLayerHrdParametersNal,
154 sizeof(StdVideoH265SubLayerHrdParameters));
155 dst->hrd_parameters.pSubLayerHrdParametersNal = &dst->hrd_parameters_nal;
156 }
157 if (src->pHrdParameters->pSubLayerHrdParametersVcl) {
158 memcpy(&dst->hrd_parameters_vcl, src->pHrdParameters->pSubLayerHrdParametersVcl,
159 sizeof(StdVideoH265SubLayerHrdParameters));
160 dst->hrd_parameters.pSubLayerHrdParametersVcl = &dst->hrd_parameters_vcl;
161 }
162 }
163
164 if (src->pProfileTierLevel) {
165 memcpy(&dst->tier_level, src->pProfileTierLevel, sizeof(StdVideoH265ProfileTierLevel));
166 dst->base.pProfileTierLevel = &dst->tier_level;
167 }
168 }
169
170 static void
vk_video_deep_copy_h265_sps(struct vk_video_h265_sps * dst,const StdVideoH265SequenceParameterSet * src)171 vk_video_deep_copy_h265_sps(struct vk_video_h265_sps *dst,
172 const StdVideoH265SequenceParameterSet *src)
173 {
174 memcpy(&dst->base, src, sizeof(StdVideoH265SequenceParameterSet));
175 if (src->pProfileTierLevel) {
176 memcpy(&dst->tier_level, src->pProfileTierLevel, sizeof(StdVideoH265ProfileTierLevel));
177 dst->base.pProfileTierLevel = &dst->tier_level;
178 }
179 if (src->pDecPicBufMgr) {
180 memcpy(&dst->dec_pic_buf_mgr, src->pDecPicBufMgr, sizeof(StdVideoH265DecPicBufMgr));
181 dst->base.pDecPicBufMgr = &dst->dec_pic_buf_mgr;
182 }
183 if (src->flags.sps_scaling_list_data_present_flag && src->pScalingLists) {
184 memcpy(&dst->scaling_lists, src->pScalingLists, sizeof(StdVideoH265ScalingLists));
185 dst->base.pScalingLists = &dst->scaling_lists;
186 }
187
188 if (src->pShortTermRefPicSet) {
189 memcpy(&dst->short_term_ref_pic_set, src->pShortTermRefPicSet, sizeof(StdVideoH265ShortTermRefPicSet));
190 dst->base.pShortTermRefPicSet = &dst->short_term_ref_pic_set;
191 }
192
193 if (src->pLongTermRefPicsSps) {
194 memcpy(&dst->long_term_ref_pics_sps, src->pLongTermRefPicsSps, sizeof(StdVideoH265LongTermRefPicsSps));
195 dst->base.pLongTermRefPicsSps = &dst->long_term_ref_pics_sps;
196 }
197
198 if (src->pSequenceParameterSetVui) {
199 memcpy(&dst->vui, src->pSequenceParameterSetVui, sizeof(StdVideoH265SequenceParameterSetVui));
200 dst->base.pSequenceParameterSetVui = &dst->vui;
201
202 if (src->pSequenceParameterSetVui->pHrdParameters) {
203 memcpy(&dst->hrd_parameters, src->pSequenceParameterSetVui->pHrdParameters, sizeof(StdVideoH265HrdParameters));
204 dst->vui.pHrdParameters = &dst->hrd_parameters;
205 if (src->pSequenceParameterSetVui->pHrdParameters->pSubLayerHrdParametersNal) {
206 memcpy(&dst->hrd_parameters_nal, src->pSequenceParameterSetVui->pHrdParameters->pSubLayerHrdParametersNal,
207 sizeof(StdVideoH265SubLayerHrdParameters));
208 dst->hrd_parameters.pSubLayerHrdParametersNal = &dst->hrd_parameters_nal;
209 }
210 if (src->pSequenceParameterSetVui->pHrdParameters->pSubLayerHrdParametersVcl) {
211 memcpy(&dst->hrd_parameters_vcl, src->pSequenceParameterSetVui->pHrdParameters->pSubLayerHrdParametersVcl,
212 sizeof(StdVideoH265SubLayerHrdParameters));
213 dst->hrd_parameters.pSubLayerHrdParametersVcl = &dst->hrd_parameters_vcl;
214 }
215 }
216 }
217 if (src->flags.sps_palette_predictor_initializers_present_flag && src->pPredictorPaletteEntries) {
218 memcpy(&dst->palette_entries, src->pPredictorPaletteEntries, sizeof(StdVideoH265PredictorPaletteEntries));
219 dst->base.pPredictorPaletteEntries = &dst->palette_entries;
220 }
221 }
222
223 static void
vk_video_deep_copy_h265_pps(struct vk_video_h265_pps * dst,const StdVideoH265PictureParameterSet * src)224 vk_video_deep_copy_h265_pps(struct vk_video_h265_pps *dst,
225 const StdVideoH265PictureParameterSet *src)
226 {
227 memcpy(&dst->base, src, sizeof(StdVideoH265PictureParameterSet));
228 if (src->flags.pps_scaling_list_data_present_flag && src->pScalingLists) {
229 memcpy(&dst->scaling_lists, src->pScalingLists, sizeof(StdVideoH265ScalingLists));
230 dst->base.pScalingLists = &dst->scaling_lists;
231 }
232
233 if (src->flags.pps_palette_predictor_initializers_present_flag && src->pPredictorPaletteEntries) {
234 memcpy(&dst->palette_entries, src->pPredictorPaletteEntries, sizeof(StdVideoH265PredictorPaletteEntries));
235 dst->base.pPredictorPaletteEntries = &dst->palette_entries;
236 }
237 }
238
239
240 #define FIND(PARAMSET, SS, SET, ID) \
241 static struct vk_video_##SET *find_##SS##_##SET(const struct vk_video_session_parameters *params, uint32_t id) { \
242 for (unsigned i = 0; i < params->SS.SET##_count; i++) { \
243 if (params->SS.SET[i].base.ID == id) \
244 return ¶ms->SS.SET[i]; \
245 } \
246 return NULL; \
247 } \
248 \
249 static void add_##SS##_##SET(struct vk_video_session_parameters *params, \
250 const PARAMSET *new_set, bool noreplace) { \
251 struct vk_video_##SET *set = find_##SS##_##SET(params, new_set->ID); \
252 if (set) { \
253 if (noreplace) \
254 return; \
255 vk_video_deep_copy_##SET(set, new_set); \
256 } else \
257 vk_video_deep_copy_##SET(¶ms->SS.SET[params->SS.SET##_count++], new_set); \
258 } \
259 \
260 static VkResult update_##SS##_##SET(struct vk_video_session_parameters *params, \
261 uint32_t count, const PARAMSET *updates) { \
262 if (params->SS.SET##_count + count >= params->SS.max_##SET##_count) \
263 return VK_ERROR_TOO_MANY_OBJECTS; \
264 for (unsigned _c = 0; _c < count; _c++) \
265 vk_video_deep_copy_##SET(¶ms->SS.SET[params->SS.SET##_count + _c], &updates[_c]); \
266 params->SS.SET##_count += count; \
267 return VK_SUCCESS; \
268 }
269
FIND(StdVideoH264SequenceParameterSet,h264_dec,h264_sps,seq_parameter_set_id)270 FIND(StdVideoH264SequenceParameterSet, h264_dec, h264_sps, seq_parameter_set_id)
271 FIND(StdVideoH264PictureParameterSet, h264_dec, h264_pps, pic_parameter_set_id)
272 FIND(StdVideoH265VideoParameterSet, h265_dec, h265_vps, vps_video_parameter_set_id)
273 FIND(StdVideoH265SequenceParameterSet, h265_dec, h265_sps, sps_seq_parameter_set_id)
274 FIND(StdVideoH265PictureParameterSet, h265_dec, h265_pps, pps_pic_parameter_set_id)
275
276 FIND(StdVideoH264SequenceParameterSet, h264_enc, h264_sps, seq_parameter_set_id)
277 FIND(StdVideoH264PictureParameterSet, h264_enc, h264_pps, pic_parameter_set_id)
278
279 FIND(StdVideoH265VideoParameterSet, h265_enc, h265_vps, vps_video_parameter_set_id)
280 FIND(StdVideoH265SequenceParameterSet, h265_enc, h265_sps, sps_seq_parameter_set_id)
281 FIND(StdVideoH265PictureParameterSet, h265_enc, h265_pps, pps_pic_parameter_set_id)
282
283 static void
284 init_add_h264_dec_session_parameters(struct vk_video_session_parameters *params,
285 const struct VkVideoDecodeH264SessionParametersAddInfoKHR *h264_add,
286 const struct vk_video_session_parameters *templ)
287 {
288 unsigned i;
289
290 if (h264_add) {
291 for (i = 0; i < h264_add->stdSPSCount; i++) {
292 add_h264_dec_h264_sps(params, &h264_add->pStdSPSs[i], false);
293 }
294 }
295 if (templ) {
296 for (i = 0; i < templ->h264_dec.h264_sps_count; i++) {
297 add_h264_dec_h264_sps(params, &templ->h264_dec.h264_sps[i].base, true);
298 }
299 }
300
301 if (h264_add) {
302 for (i = 0; i < h264_add->stdPPSCount; i++) {
303 add_h264_dec_h264_pps(params, &h264_add->pStdPPSs[i], false);
304 }
305 }
306 if (templ) {
307 for (i = 0; i < templ->h264_dec.h264_pps_count; i++) {
308 add_h264_dec_h264_pps(params, &templ->h264_dec.h264_pps[i].base, true);
309 }
310 }
311 }
312
313 static void
init_add_h264_enc_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoEncodeH264SessionParametersAddInfoKHR * h264_add,const struct vk_video_session_parameters * templ)314 init_add_h264_enc_session_parameters(struct vk_video_session_parameters *params,
315 const struct VkVideoEncodeH264SessionParametersAddInfoKHR *h264_add,
316 const struct vk_video_session_parameters *templ)
317 {
318 unsigned i;
319 if (h264_add) {
320 for (i = 0; i < h264_add->stdSPSCount; i++) {
321 add_h264_enc_h264_sps(params, &h264_add->pStdSPSs[i], false);
322 }
323 }
324 if (templ) {
325 for (i = 0; i < templ->h264_dec.h264_sps_count; i++) {
326 add_h264_enc_h264_sps(params, &templ->h264_enc.h264_sps[i].base, true);
327 }
328 }
329
330 if (h264_add) {
331 for (i = 0; i < h264_add->stdPPSCount; i++) {
332 add_h264_enc_h264_pps(params, &h264_add->pStdPPSs[i], false);
333 }
334 }
335 if (templ) {
336 for (i = 0; i < templ->h264_enc.h264_pps_count; i++) {
337 add_h264_enc_h264_pps(params, &templ->h264_enc.h264_pps[i].base, true);
338 }
339 }
340 }
341
342 static void
init_add_h265_dec_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoDecodeH265SessionParametersAddInfoKHR * h265_add,const struct vk_video_session_parameters * templ)343 init_add_h265_dec_session_parameters(struct vk_video_session_parameters *params,
344 const struct VkVideoDecodeH265SessionParametersAddInfoKHR *h265_add,
345 const struct vk_video_session_parameters *templ)
346 {
347 unsigned i;
348
349 if (h265_add) {
350 for (i = 0; i < h265_add->stdVPSCount; i++) {
351 add_h265_dec_h265_vps(params, &h265_add->pStdVPSs[i], false);
352 }
353 }
354 if (templ) {
355 for (i = 0; i < templ->h265_dec.h265_vps_count; i++) {
356 add_h265_dec_h265_vps(params, &templ->h265_dec.h265_vps[i].base, true);
357 }
358 }
359 if (h265_add) {
360 for (i = 0; i < h265_add->stdSPSCount; i++) {
361 add_h265_dec_h265_sps(params, &h265_add->pStdSPSs[i], false);
362 }
363 }
364 if (templ) {
365 for (i = 0; i < templ->h265_dec.h265_sps_count; i++) {
366 add_h265_dec_h265_sps(params, &templ->h265_dec.h265_sps[i].base, true);
367 }
368 }
369
370 if (h265_add) {
371 for (i = 0; i < h265_add->stdPPSCount; i++) {
372 add_h265_dec_h265_pps(params, &h265_add->pStdPPSs[i], false);
373 }
374 }
375 if (templ) {
376 for (i = 0; i < templ->h265_dec.h265_pps_count; i++) {
377 add_h265_dec_h265_pps(params, &templ->h265_dec.h265_pps[i].base, true);
378 }
379 }
380 }
381
382 static void
init_add_h265_enc_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoEncodeH265SessionParametersAddInfoKHR * h265_add,const struct vk_video_session_parameters * templ)383 init_add_h265_enc_session_parameters(struct vk_video_session_parameters *params,
384 const struct VkVideoEncodeH265SessionParametersAddInfoKHR *h265_add,
385 const struct vk_video_session_parameters *templ)
386 {
387 unsigned i;
388
389 if (h265_add) {
390 for (i = 0; i < h265_add->stdVPSCount; i++) {
391 add_h265_enc_h265_vps(params, &h265_add->pStdVPSs[i], false);
392 }
393 }
394 if (templ) {
395 for (i = 0; i < templ->h265_enc.h265_vps_count; i++) {
396 add_h265_enc_h265_vps(params, &templ->h265_enc.h265_vps[i].base, true);
397 }
398 }
399 if (h265_add) {
400 for (i = 0; i < h265_add->stdSPSCount; i++) {
401 add_h265_enc_h265_sps(params, &h265_add->pStdSPSs[i], false);
402 }
403 }
404 if (templ) {
405 for (i = 0; i < templ->h265_enc.h265_sps_count; i++) {
406 add_h265_enc_h265_sps(params, &templ->h265_enc.h265_sps[i].base, true);
407 }
408 }
409
410 if (h265_add) {
411 for (i = 0; i < h265_add->stdPPSCount; i++) {
412 add_h265_enc_h265_pps(params, &h265_add->pStdPPSs[i], false);
413 }
414 }
415 if (templ) {
416 for (i = 0; i < templ->h265_enc.h265_pps_count; i++) {
417 add_h265_enc_h265_pps(params, &templ->h265_enc.h265_pps[i].base, true);
418 }
419 }
420 }
421
422 static void
vk_video_deep_copy_av1_seq_hdr(struct vk_video_av1_seq_hdr * dst,const StdVideoAV1SequenceHeader * src)423 vk_video_deep_copy_av1_seq_hdr(struct vk_video_av1_seq_hdr *dst,
424 const StdVideoAV1SequenceHeader *src)
425 {
426 memcpy(&dst->base, src, sizeof(StdVideoAV1SequenceHeader));
427 if (src->pColorConfig) {
428 memcpy(&dst->color_config, src->pColorConfig, sizeof(StdVideoAV1ColorConfig));
429 dst->base.pColorConfig = &dst->color_config;
430 }
431 if (src->pTimingInfo) {
432 memcpy(&dst->timing_info, src->pTimingInfo, sizeof(StdVideoAV1TimingInfo));
433 dst->base.pTimingInfo = &dst->timing_info;
434 }
435 }
436
437 VkResult
vk_video_session_parameters_init(struct vk_device * device,struct vk_video_session_parameters * params,const struct vk_video_session * vid,const struct vk_video_session_parameters * templ,const VkVideoSessionParametersCreateInfoKHR * create_info)438 vk_video_session_parameters_init(struct vk_device *device,
439 struct vk_video_session_parameters *params,
440 const struct vk_video_session *vid,
441 const struct vk_video_session_parameters *templ,
442 const VkVideoSessionParametersCreateInfoKHR *create_info)
443 {
444 memset(params, 0, sizeof(*params));
445 vk_object_base_init(device, ¶ms->base, VK_OBJECT_TYPE_VIDEO_SESSION_PARAMETERS_KHR);
446
447 params->op = vid->op;
448
449 switch (vid->op) {
450 case VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR: {
451 const struct VkVideoDecodeH264SessionParametersCreateInfoKHR *h264_create =
452 vk_find_struct_const(create_info->pNext, VIDEO_DECODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR);
453
454 params->h264_dec.max_h264_sps_count = h264_create->maxStdSPSCount;
455 params->h264_dec.max_h264_pps_count = h264_create->maxStdPPSCount;
456
457 uint32_t sps_size = params->h264_dec.max_h264_sps_count * sizeof(struct vk_video_h264_sps);
458 uint32_t pps_size = params->h264_dec.max_h264_pps_count * sizeof(struct vk_video_h264_pps);
459
460 params->h264_dec.h264_sps = vk_alloc(&device->alloc, sps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
461 params->h264_dec.h264_pps = vk_alloc(&device->alloc, pps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
462 if (!params->h264_dec.h264_sps || !params->h264_dec.h264_pps) {
463 vk_free(&device->alloc, params->h264_dec.h264_sps);
464 vk_free(&device->alloc, params->h264_dec.h264_pps);
465 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
466 }
467
468 init_add_h264_dec_session_parameters(params, h264_create->pParametersAddInfo, templ);
469 break;
470 }
471 case VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR: {
472 const struct VkVideoDecodeH265SessionParametersCreateInfoKHR *h265_create =
473 vk_find_struct_const(create_info->pNext, VIDEO_DECODE_H265_SESSION_PARAMETERS_CREATE_INFO_KHR);
474
475 params->h265_dec.max_h265_vps_count = h265_create->maxStdVPSCount;
476 params->h265_dec.max_h265_sps_count = h265_create->maxStdSPSCount;
477 params->h265_dec.max_h265_pps_count = h265_create->maxStdPPSCount;
478
479 uint32_t vps_size = params->h265_dec.max_h265_vps_count * sizeof(struct vk_video_h265_vps);
480 uint32_t sps_size = params->h265_dec.max_h265_sps_count * sizeof(struct vk_video_h265_sps);
481 uint32_t pps_size = params->h265_dec.max_h265_pps_count * sizeof(struct vk_video_h265_pps);
482
483 params->h265_dec.h265_vps = vk_alloc(&device->alloc, vps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
484 params->h265_dec.h265_sps = vk_alloc(&device->alloc, sps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
485 params->h265_dec.h265_pps = vk_alloc(&device->alloc, pps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
486 if (!params->h265_dec.h265_sps || !params->h265_dec.h265_pps || !params->h265_dec.h265_vps) {
487 vk_free(&device->alloc, params->h265_dec.h265_vps);
488 vk_free(&device->alloc, params->h265_dec.h265_sps);
489 vk_free(&device->alloc, params->h265_dec.h265_pps);
490 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
491 }
492
493 init_add_h265_dec_session_parameters(params, h265_create->pParametersAddInfo, templ);
494 break;
495 }
496 case VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR: {
497 const struct VkVideoDecodeAV1SessionParametersCreateInfoKHR *av1_create =
498 vk_find_struct_const(create_info->pNext, VIDEO_DECODE_AV1_SESSION_PARAMETERS_CREATE_INFO_KHR);
499 if (av1_create && av1_create->pStdSequenceHeader) {
500 vk_video_deep_copy_av1_seq_hdr(¶ms->av1_dec.seq_hdr,
501 av1_create->pStdSequenceHeader);
502 }
503 break;
504 }
505 case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
506 const struct VkVideoEncodeH264SessionParametersCreateInfoKHR *h264_create =
507 vk_find_struct_const(create_info->pNext, VIDEO_ENCODE_H264_SESSION_PARAMETERS_CREATE_INFO_KHR);
508
509 params->h264_enc.max_h264_sps_count = h264_create->maxStdSPSCount;
510 params->h264_enc.max_h264_pps_count = h264_create->maxStdPPSCount;
511
512 uint32_t sps_size = params->h264_enc.max_h264_sps_count * sizeof(struct vk_video_h264_sps);
513 uint32_t pps_size = params->h264_enc.max_h264_pps_count * sizeof(struct vk_video_h264_pps);
514
515 params->h264_enc.h264_sps = vk_alloc(&device->alloc, sps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
516 params->h264_enc.h264_pps = vk_alloc(&device->alloc, pps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
517 if (!params->h264_enc.h264_sps || !params->h264_enc.h264_pps) {
518 vk_free(&device->alloc, params->h264_enc.h264_sps);
519 vk_free(&device->alloc, params->h264_enc.h264_pps);
520 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
521 }
522
523 params->h264_enc.profile_idc = vid->h264.profile_idc;
524 init_add_h264_enc_session_parameters(params, h264_create->pParametersAddInfo, templ);
525 break;
526 }
527 case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
528 const struct VkVideoEncodeH265SessionParametersCreateInfoKHR *h265_create =
529 vk_find_struct_const(create_info->pNext, VIDEO_ENCODE_H265_SESSION_PARAMETERS_CREATE_INFO_KHR);
530
531 params->h265_enc.max_h265_vps_count = h265_create->maxStdVPSCount;
532 params->h265_enc.max_h265_sps_count = h265_create->maxStdSPSCount;
533 params->h265_enc.max_h265_pps_count = h265_create->maxStdPPSCount;
534
535 uint32_t vps_size = params->h265_enc.max_h265_vps_count * sizeof(struct vk_video_h265_vps);
536 uint32_t sps_size = params->h265_enc.max_h265_sps_count * sizeof(struct vk_video_h265_sps);
537 uint32_t pps_size = params->h265_enc.max_h265_pps_count * sizeof(struct vk_video_h265_pps);
538
539 params->h265_enc.h265_vps = vk_alloc(&device->alloc, vps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
540 params->h265_enc.h265_sps = vk_alloc(&device->alloc, sps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
541 params->h265_enc.h265_pps = vk_alloc(&device->alloc, pps_size, 8, VK_SYSTEM_ALLOCATION_SCOPE_OBJECT);
542 if (!params->h265_enc.h265_sps || !params->h265_enc.h265_pps || !params->h265_enc.h265_vps) {
543 vk_free(&device->alloc, params->h265_enc.h265_vps);
544 vk_free(&device->alloc, params->h265_enc.h265_sps);
545 vk_free(&device->alloc, params->h265_enc.h265_pps);
546 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
547 }
548
549 init_add_h265_enc_session_parameters(params, h265_create->pParametersAddInfo, templ);
550 break;
551 }
552 default:
553 unreachable("Unsupported video codec operation");
554 break;
555 }
556 return VK_SUCCESS;
557 }
558
559 void
vk_video_session_parameters_finish(struct vk_device * device,struct vk_video_session_parameters * params)560 vk_video_session_parameters_finish(struct vk_device *device,
561 struct vk_video_session_parameters *params)
562 {
563 switch (params->op) {
564 case VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR:
565 vk_free(&device->alloc, params->h264_dec.h264_sps);
566 vk_free(&device->alloc, params->h264_dec.h264_pps);
567 break;
568 case VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR:
569 vk_free(&device->alloc, params->h265_dec.h265_vps);
570 vk_free(&device->alloc, params->h265_dec.h265_sps);
571 vk_free(&device->alloc, params->h265_dec.h265_pps);
572 break;
573 case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR:
574 vk_free(&device->alloc, params->h264_enc.h264_sps);
575 vk_free(&device->alloc, params->h264_enc.h264_pps);
576 break;
577 case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR:
578 vk_free(&device->alloc, params->h265_enc.h265_vps);
579 vk_free(&device->alloc, params->h265_enc.h265_sps);
580 vk_free(&device->alloc, params->h265_enc.h265_pps);
581 break;
582 default:
583 break;
584 }
585 vk_object_base_finish(¶ms->base);
586 }
587
588 static VkResult
update_h264_dec_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoDecodeH264SessionParametersAddInfoKHR * h264_add)589 update_h264_dec_session_parameters(struct vk_video_session_parameters *params,
590 const struct VkVideoDecodeH264SessionParametersAddInfoKHR *h264_add)
591 {
592 VkResult result = VK_SUCCESS;
593
594 result = update_h264_dec_h264_sps(params, h264_add->stdSPSCount, h264_add->pStdSPSs);
595 if (result != VK_SUCCESS)
596 return result;
597
598 result = update_h264_dec_h264_pps(params, h264_add->stdPPSCount, h264_add->pStdPPSs);
599 return result;
600 }
601
602 static VkResult
update_h264_enc_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoEncodeH264SessionParametersAddInfoKHR * h264_add)603 update_h264_enc_session_parameters(struct vk_video_session_parameters *params,
604 const struct VkVideoEncodeH264SessionParametersAddInfoKHR *h264_add)
605 {
606 VkResult result = VK_SUCCESS;
607 result = update_h264_enc_h264_sps(params, h264_add->stdSPSCount, h264_add->pStdSPSs);
608 if (result != VK_SUCCESS)
609 return result;
610
611 result = update_h264_enc_h264_pps(params, h264_add->stdPPSCount, h264_add->pStdPPSs);
612 return result;
613 }
614
615 static VkResult
update_h265_enc_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoEncodeH265SessionParametersAddInfoKHR * h265_add)616 update_h265_enc_session_parameters(struct vk_video_session_parameters *params,
617 const struct VkVideoEncodeH265SessionParametersAddInfoKHR *h265_add)
618 {
619 VkResult result = VK_SUCCESS;
620
621 result = update_h265_enc_h265_vps(params, h265_add->stdVPSCount, h265_add->pStdVPSs);
622 if (result != VK_SUCCESS)
623 return result;
624
625 result = update_h265_enc_h265_sps(params, h265_add->stdSPSCount, h265_add->pStdSPSs);
626 if (result != VK_SUCCESS)
627 return result;
628
629 result = update_h265_enc_h265_pps(params, h265_add->stdPPSCount, h265_add->pStdPPSs);
630 return result;
631 }
632
633 static VkResult
update_h265_session_parameters(struct vk_video_session_parameters * params,const struct VkVideoDecodeH265SessionParametersAddInfoKHR * h265_add)634 update_h265_session_parameters(struct vk_video_session_parameters *params,
635 const struct VkVideoDecodeH265SessionParametersAddInfoKHR *h265_add)
636 {
637 VkResult result = VK_SUCCESS;
638 result = update_h265_dec_h265_vps(params, h265_add->stdVPSCount, h265_add->pStdVPSs);
639 if (result != VK_SUCCESS)
640 return result;
641
642 result = update_h265_dec_h265_sps(params, h265_add->stdSPSCount, h265_add->pStdSPSs);
643 if (result != VK_SUCCESS)
644 return result;
645
646 result = update_h265_dec_h265_pps(params, h265_add->stdPPSCount, h265_add->pStdPPSs);
647 return result;
648 }
649
650 VkResult
vk_video_session_parameters_update(struct vk_video_session_parameters * params,const VkVideoSessionParametersUpdateInfoKHR * update)651 vk_video_session_parameters_update(struct vk_video_session_parameters *params,
652 const VkVideoSessionParametersUpdateInfoKHR *update)
653 {
654 /* 39.6.5. Decoder Parameter Sets -
655 * "The provided H.264 SPS/PPS parameters must be within the limits specified during decoder
656 * creation for the decoder specified in VkVideoSessionParametersCreateInfoKHR."
657 */
658
659 /*
660 * There is no need to deduplicate here.
661 * videoSessionParameters must not already contain a StdVideoH264PictureParameterSet entry with
662 * both seq_parameter_set_id and pic_parameter_set_id matching any of the elements of
663 * VkVideoDecodeH264SessionParametersAddInfoKHR::pStdPPS
664 */
665 VkResult result = VK_SUCCESS;
666
667 switch (params->op) {
668 case VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR: {
669 const struct VkVideoDecodeH264SessionParametersAddInfoKHR *h264_add =
670 vk_find_struct_const(update->pNext, VIDEO_DECODE_H264_SESSION_PARAMETERS_ADD_INFO_KHR);
671 return update_h264_dec_session_parameters(params, h264_add);
672 }
673 case VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR: {
674 const struct VkVideoDecodeH265SessionParametersAddInfoKHR *h265_add =
675 vk_find_struct_const(update->pNext, VIDEO_DECODE_H265_SESSION_PARAMETERS_ADD_INFO_KHR);
676
677 return update_h265_session_parameters(params, h265_add);
678 }
679 case VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR: {
680 const struct VkVideoEncodeH264SessionParametersAddInfoKHR *h264_add =
681 vk_find_struct_const(update->pNext, VIDEO_ENCODE_H264_SESSION_PARAMETERS_ADD_INFO_KHR);
682 return update_h264_enc_session_parameters(params, h264_add);
683 }
684 case VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR: {
685 const struct VkVideoEncodeH265SessionParametersAddInfoKHR *h265_add =
686 vk_find_struct_const(update->pNext, VIDEO_ENCODE_H265_SESSION_PARAMETERS_ADD_INFO_KHR);
687 return update_h265_enc_session_parameters(params, h265_add);
688 }
689 default:
690 unreachable("Unknown codec\n");
691 }
692 return result;
693 }
694
695 const uint8_t h264_scaling_list_default_4x4_intra[] =
696 {
697 /* Table 7-3 - Default_4x4_Intra */
698 6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32, 32, 37, 37, 42
699 };
700
701 const uint8_t h264_scaling_list_default_4x4_inter[] =
702 {
703 /* Table 7-3 - Default_4x4_Inter */
704 10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27, 27, 30, 30, 34
705 };
706
707 const uint8_t h264_scaling_list_default_8x8_intra[] =
708 {
709 /* Table 7-4 - Default_8x8_Intra */
710 6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18, 18, 18, 18, 23,
711 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27,
712 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31,
713 31, 33, 33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42,
714 };
715
716 const uint8_t h264_scaling_list_default_8x8_inter[] =
717 {
718 /* Table 7-4 - Default_8x8_Inter */
719 9 , 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19, 19, 19, 19, 21,
720 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24, 24,
721 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27,
722 27, 28, 28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35,
723 };
724
725 void
vk_video_derive_h264_scaling_list(const StdVideoH264SequenceParameterSet * sps,const StdVideoH264PictureParameterSet * pps,StdVideoH264ScalingLists * list)726 vk_video_derive_h264_scaling_list(const StdVideoH264SequenceParameterSet *sps,
727 const StdVideoH264PictureParameterSet *pps,
728 StdVideoH264ScalingLists *list)
729 {
730 StdVideoH264ScalingLists temp;
731
732 /* derive SPS scaling list first, because PPS may depend on it in fall-back
733 * rule B */
734 if (sps->flags.seq_scaling_matrix_present_flag)
735 {
736 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS; i++)
737 {
738 if (sps->pScalingLists->scaling_list_present_mask & (1 << i))
739 memcpy(temp.ScalingList4x4[i],
740 sps->pScalingLists->ScalingList4x4[i],
741 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
742 else /* fall-back rule A */
743 {
744 if (i == 0)
745 memcpy(temp.ScalingList4x4[i],
746 h264_scaling_list_default_4x4_intra,
747 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
748 else if (i == 3)
749 memcpy(temp.ScalingList4x4[i],
750 h264_scaling_list_default_4x4_inter,
751 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
752 else
753 memcpy(temp.ScalingList4x4[i],
754 temp.ScalingList4x4[i - 1],
755 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
756 }
757 }
758
759 for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS; j++)
760 {
761 int i = j + STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS;
762 if (sps->pScalingLists->scaling_list_present_mask & (1 << i))
763 memcpy(temp.ScalingList8x8[j], sps->pScalingLists->ScalingList8x8[j],
764 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
765 else /* fall-back rule A */
766 {
767 if (i == 6)
768 memcpy(temp.ScalingList8x8[j],
769 h264_scaling_list_default_8x8_intra,
770 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
771 else if (i == 7)
772 memcpy(temp.ScalingList8x8[j],
773 h264_scaling_list_default_8x8_inter,
774 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
775 else
776 memcpy(temp.ScalingList8x8[j], temp.ScalingList8x8[j - 2],
777 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
778 }
779 }
780 }
781 else
782 {
783 memset(temp.ScalingList4x4, 0x10,
784 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS *
785 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
786 memset(temp.ScalingList8x8, 0x10,
787 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS *
788 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
789 }
790
791 if (pps->flags.pic_scaling_matrix_present_flag)
792 {
793 for (int i = 0; i < STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS; i++)
794 {
795 if (pps->pScalingLists->scaling_list_present_mask & (1 << i))
796 memcpy(list->ScalingList4x4[i], pps->pScalingLists->ScalingList4x4[i],
797 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
798 else if (sps->flags.seq_scaling_matrix_present_flag) /* fall-back rule B */
799 {
800 if (i == 0 || i == 3)
801 memcpy(list->ScalingList4x4[i], temp.ScalingList4x4[i],
802 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
803 else
804 memcpy(list->ScalingList4x4[i], list->ScalingList4x4[i - 1],
805 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
806 }
807 else /* fall-back rule A */
808 {
809 if (i == 0)
810 memcpy(list->ScalingList4x4[i],
811 h264_scaling_list_default_4x4_intra,
812 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
813 else if (i == 3)
814 memcpy(list->ScalingList4x4[i],
815 h264_scaling_list_default_4x4_inter,
816 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
817 else
818 memcpy(list->ScalingList4x4[i],
819 list->ScalingList4x4[i - 1],
820 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
821 }
822 }
823
824 for (int j = 0; j < STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS; j++)
825 {
826 int i = j + STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS;
827 if (pps->pScalingLists->scaling_list_present_mask & (1 << i))
828 memcpy(list->ScalingList8x8[j], pps->pScalingLists->ScalingList8x8[j],
829 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
830 else if (sps->flags.seq_scaling_matrix_present_flag) /* fall-back rule B */
831 {
832 if (i == 6 || i == 7)
833 memcpy(list->ScalingList8x8[j], temp.ScalingList8x8[j],
834 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
835 else
836 memcpy(list->ScalingList8x8[j], list->ScalingList8x8[j - 2],
837 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
838 }
839 else /* fall-back rule A */
840 {
841 if (i == 6)
842 memcpy(list->ScalingList8x8[j],
843 h264_scaling_list_default_8x8_intra,
844 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
845 else if (i == 7)
846 memcpy(list->ScalingList8x8[j],
847 h264_scaling_list_default_8x8_inter,
848 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
849 else
850 memcpy(list->ScalingList8x8[j], list->ScalingList8x8[j - 2],
851 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
852 }
853 }
854 }
855 else
856 {
857 memcpy(list->ScalingList4x4, temp.ScalingList4x4,
858 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_LISTS *
859 STD_VIDEO_H264_SCALING_LIST_4X4_NUM_ELEMENTS);
860 memcpy(list->ScalingList8x8, temp.ScalingList8x8,
861 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_LISTS *
862 STD_VIDEO_H264_SCALING_LIST_8X8_NUM_ELEMENTS);
863 }
864 }
865
866 const StdVideoH264SequenceParameterSet *
vk_video_find_h264_dec_std_sps(const struct vk_video_session_parameters * params,uint32_t id)867 vk_video_find_h264_dec_std_sps(const struct vk_video_session_parameters *params,
868 uint32_t id)
869 {
870 return &find_h264_dec_h264_sps(params, id)->base;
871 }
872
873 const StdVideoH264PictureParameterSet *
vk_video_find_h264_dec_std_pps(const struct vk_video_session_parameters * params,uint32_t id)874 vk_video_find_h264_dec_std_pps(const struct vk_video_session_parameters *params,
875 uint32_t id)
876 {
877 return &find_h264_dec_h264_pps(params, id)->base;
878 }
879
880 const StdVideoH265VideoParameterSet *
vk_video_find_h265_dec_std_vps(const struct vk_video_session_parameters * params,uint32_t id)881 vk_video_find_h265_dec_std_vps(const struct vk_video_session_parameters *params,
882 uint32_t id)
883 {
884 return &find_h265_dec_h265_vps(params, id)->base;
885 }
886
887 const StdVideoH265SequenceParameterSet *
vk_video_find_h265_dec_std_sps(const struct vk_video_session_parameters * params,uint32_t id)888 vk_video_find_h265_dec_std_sps(const struct vk_video_session_parameters *params,
889 uint32_t id)
890 {
891 return &find_h265_dec_h265_sps(params, id)->base;
892 }
893
894 const StdVideoH265PictureParameterSet *
vk_video_find_h265_dec_std_pps(const struct vk_video_session_parameters * params,uint32_t id)895 vk_video_find_h265_dec_std_pps(const struct vk_video_session_parameters *params,
896 uint32_t id)
897 {
898 return &find_h265_dec_h265_pps(params, id)->base;
899 }
900
901 int
vk_video_h265_poc_by_slot(const struct VkVideoDecodeInfoKHR * frame_info,int slot)902 vk_video_h265_poc_by_slot(const struct VkVideoDecodeInfoKHR *frame_info, int slot)
903 {
904 for (unsigned i = 0; i < frame_info->referenceSlotCount; i++) {
905 const VkVideoDecodeH265DpbSlotInfoKHR *dpb_slot_info =
906 vk_find_struct_const(frame_info->pReferenceSlots[i].pNext, VIDEO_DECODE_H265_DPB_SLOT_INFO_KHR);
907 if (frame_info->pReferenceSlots[i].slotIndex == slot)
908 return dpb_slot_info->pStdReferenceInfo->PicOrderCntVal;
909 }
910
911 assert(0);
912
913 return 0;
914 }
915
916 void
vk_fill_video_h265_reference_info(const VkVideoDecodeInfoKHR * frame_info,const struct VkVideoDecodeH265PictureInfoKHR * pic,const struct vk_video_h265_slice_params * slice_params,struct vk_video_h265_reference ref_slots[][8])917 vk_fill_video_h265_reference_info(const VkVideoDecodeInfoKHR *frame_info,
918 const struct VkVideoDecodeH265PictureInfoKHR *pic,
919 const struct vk_video_h265_slice_params *slice_params,
920 struct vk_video_h265_reference ref_slots[][8])
921 {
922 uint8_t list_cnt = slice_params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B ? 2 : 1;
923 uint8_t list_idx;
924 int i, j;
925
926 for (list_idx = 0; list_idx < list_cnt; list_idx++) {
927 /* The order is
928 * L0: Short term current before set - Short term current after set - long term current
929 * L1: Short term current after set - short term current before set - long term current
930 */
931 const uint8_t *rps[3] = {
932 list_idx ? pic->pStdPictureInfo->RefPicSetStCurrAfter : pic->pStdPictureInfo->RefPicSetStCurrBefore,
933 list_idx ? pic->pStdPictureInfo->RefPicSetStCurrBefore : pic->pStdPictureInfo->RefPicSetStCurrAfter,
934 pic->pStdPictureInfo->RefPicSetLtCurr
935 };
936
937 uint8_t ref_idx = 0;
938 for (i = 0; i < 3; i++) {
939 const uint8_t *cur_rps = rps[i];
940
941 for (j = 0; (cur_rps[j] != 0xff) && ((j + ref_idx) < 8); j++) {
942 ref_slots[list_idx][j + ref_idx].slot_index = cur_rps[j];
943 ref_slots[list_idx][j + ref_idx].pic_order_cnt = vk_video_h265_poc_by_slot(frame_info, cur_rps[j]);
944 }
945 ref_idx += j;
946 }
947
948 /* TODO: should handle cases where rpl_modification_flag is true. */
949 assert(!slice_params->rpl_modification_flag[0] && !slice_params->rpl_modification_flag[1]);
950 }
951 }
952
953 static void
h265_pred_weight_table(struct vk_video_h265_slice_params * params,struct vl_rbsp * rbsp,const StdVideoH265SequenceParameterSet * sps,StdVideoH265SliceType slice_type)954 h265_pred_weight_table(struct vk_video_h265_slice_params *params,
955 struct vl_rbsp *rbsp,
956 const StdVideoH265SequenceParameterSet *sps,
957 StdVideoH265SliceType slice_type)
958 {
959 unsigned chroma_array_type = sps->flags.separate_colour_plane_flag ? 0 : sps->chroma_format_idc;
960 unsigned i, j;
961
962 params->luma_log2_weight_denom = vl_rbsp_ue(rbsp);
963
964 assert(params->luma_log2_weight_denom >= 0 && params->luma_log2_weight_denom < 8);
965
966 if (chroma_array_type != 0) {
967 params->chroma_log2_weight_denom = params->luma_log2_weight_denom + vl_rbsp_se(rbsp);
968 assert(params->chroma_log2_weight_denom >= 0 && params->chroma_log2_weight_denom < 8);
969 }
970
971 for (i = 0; i < params->num_ref_idx_l0_active; ++i) {
972 params->luma_weight_l0_flag[i] = vl_rbsp_u(rbsp, 1);
973 if (!params->luma_weight_l0_flag[i]) {
974 params->luma_weight_l0[i] = 1 << params->luma_log2_weight_denom;
975 params->luma_offset_l0[i] = 0;
976 }
977 }
978
979 for (i = 0; i < params->num_ref_idx_l0_active; ++i) {
980 if (chroma_array_type == 0) {
981 params->chroma_weight_l0_flag[i] = 0;
982 } else {
983 params->chroma_weight_l0_flag[i] = vl_rbsp_u(rbsp, 1);
984 }
985 }
986
987 for (i = 0; i < params->num_ref_idx_l0_active; ++i) {
988 if (params->luma_weight_l0_flag[i]) {
989 params->delta_luma_weight_l0[i] = vl_rbsp_se(rbsp);
990 params->luma_weight_l0[i] = (1 << params->luma_log2_weight_denom) + params->delta_luma_weight_l0[i];
991 params->luma_offset_l0[i] = vl_rbsp_se(rbsp);
992 }
993
994 if (params->chroma_weight_l0_flag[i]) {
995 for (j = 0; j < 2; j++) {
996 params->delta_chroma_weight_l0[i][j] = vl_rbsp_se(rbsp);
997 params->delta_chroma_offset_l0[i][j] = vl_rbsp_se(rbsp);
998
999 params->chroma_weight_l0[i][j] =
1000 (1 << params->chroma_log2_weight_denom) + params->delta_chroma_weight_l0[i][j];
1001 params->chroma_offset_l0[i][j] = CLAMP(params->delta_chroma_offset_l0[i][j] -
1002 ((128 * params->chroma_weight_l0[i][j]) >> params->chroma_log2_weight_denom) + 128, -128, 127);
1003 }
1004 } else {
1005 for (j = 0; j < 2; j++) {
1006 params->chroma_weight_l0[i][j] = 1 << params->chroma_log2_weight_denom;
1007 params->chroma_offset_l0[i][j] = 0;
1008 }
1009 }
1010 }
1011
1012 if (slice_type == STD_VIDEO_H265_SLICE_TYPE_B) {
1013 for (i = 0; i < params->num_ref_idx_l1_active; ++i) {
1014 params->luma_weight_l1_flag[i] = vl_rbsp_u(rbsp, 1);
1015 if (!params->luma_weight_l1_flag[i]) {
1016 params->luma_weight_l1[i] = 1 << params->luma_log2_weight_denom;
1017 params->luma_offset_l1[i] = 0;
1018 }
1019 }
1020
1021 for (i = 0; i < params->num_ref_idx_l1_active; ++i) {
1022 if (chroma_array_type == 0) {
1023 params->chroma_weight_l1_flag[i] = 0;
1024 } else {
1025 params->chroma_weight_l1_flag[i] = vl_rbsp_u(rbsp, 1);
1026 }
1027 }
1028
1029 for (i = 0; i < params->num_ref_idx_l1_active; ++i) {
1030 if (params->luma_weight_l1_flag[i]) {
1031 params->delta_luma_weight_l1[i] = vl_rbsp_se(rbsp);
1032 params->luma_weight_l1[i] =
1033 (1 << params->luma_log2_weight_denom) + params->delta_luma_weight_l1[i];
1034 params->luma_offset_l1[i] = vl_rbsp_se(rbsp);
1035 }
1036
1037 if (params->chroma_weight_l1_flag[i]) {
1038 for (j = 0; j < 2; j++) {
1039 params->delta_chroma_weight_l1[i][j] = vl_rbsp_se(rbsp);
1040 params->delta_chroma_offset_l1[i][j] = vl_rbsp_se(rbsp);
1041
1042 params->chroma_weight_l1[i][j] =
1043 (1 << params->chroma_log2_weight_denom) + params->delta_chroma_weight_l1[i][j];
1044 params->chroma_offset_l1[i][j] = CLAMP(params->delta_chroma_offset_l1[i][j] -
1045 ((128 * params->chroma_weight_l1[i][j]) >> params->chroma_log2_weight_denom) + 128, -128, 127);
1046 }
1047 } else {
1048 for (j = 0; j < 2; j++) {
1049 params->chroma_weight_l1[i][j] = 1 << params->chroma_log2_weight_denom;
1050 params->chroma_offset_l1[i][j] = 0;
1051 }
1052 }
1053 }
1054 }
1055 }
1056
1057 void
vk_video_parse_h265_slice_header(const struct VkVideoDecodeInfoKHR * frame_info,const VkVideoDecodeH265PictureInfoKHR * pic_info,const StdVideoH265SequenceParameterSet * sps,const StdVideoH265PictureParameterSet * pps,void * slice_data,uint32_t slice_size,struct vk_video_h265_slice_params * params)1058 vk_video_parse_h265_slice_header(const struct VkVideoDecodeInfoKHR *frame_info,
1059 const VkVideoDecodeH265PictureInfoKHR *pic_info,
1060 const StdVideoH265SequenceParameterSet *sps,
1061 const StdVideoH265PictureParameterSet *pps,
1062 void *slice_data,
1063 uint32_t slice_size,
1064 struct vk_video_h265_slice_params *params)
1065 {
1066 struct vl_vlc vlc;
1067 const void *slice_headers[1] = { slice_data };
1068 vl_vlc_init(&vlc, 1, slice_headers, &slice_size);
1069
1070 assert(vl_vlc_peekbits(&vlc, 24) == 0x000001);
1071
1072 vl_vlc_eatbits(&vlc, 24);
1073
1074 /* forbidden_zero_bit */
1075 vl_vlc_eatbits(&vlc, 1);
1076
1077 if (vl_vlc_valid_bits(&vlc) < 15)
1078 vl_vlc_fillbits(&vlc);
1079
1080 vl_vlc_get_uimsbf(&vlc, 6); /* nal_unit_type */
1081 vl_vlc_get_uimsbf(&vlc, 6); /* nuh_layer_id */
1082 vl_vlc_get_uimsbf(&vlc, 3); /* nuh_temporal_id_plus1 */
1083
1084 struct vl_rbsp rbsp;
1085 vl_rbsp_init(&rbsp, &vlc, 128, /* emulation_bytes */ true);
1086
1087 memset(params, 0, sizeof(*params));
1088
1089 params->slice_size = slice_size;
1090 params->first_slice_segment_in_pic_flag = vl_rbsp_u(&rbsp, 1);
1091
1092 /* no_output_of_prior_pics_flag */
1093 if (pic_info->pStdPictureInfo->flags.IrapPicFlag)
1094 vl_rbsp_u(&rbsp, 1);
1095
1096 /* pps id */
1097 vl_rbsp_ue(&rbsp);
1098
1099 if (!params->first_slice_segment_in_pic_flag) {
1100 int size, num;
1101 int bits_slice_segment_address = 0;
1102
1103 if (pps->flags.dependent_slice_segments_enabled_flag)
1104 params->dependent_slice_segment = vl_rbsp_u(&rbsp, 1);
1105
1106 size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
1107 sps->log2_diff_max_min_luma_coding_block_size);
1108
1109 num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
1110 ((sps->pic_height_in_luma_samples + size - 1) / size);
1111
1112 while (num > (1 << bits_slice_segment_address))
1113 bits_slice_segment_address++;
1114
1115 /* slice_segment_address */
1116 params->slice_segment_address = vl_rbsp_u(&rbsp, bits_slice_segment_address);
1117 }
1118
1119 if (params->dependent_slice_segment)
1120 return;
1121
1122 for (unsigned i = 0; i < pps->num_extra_slice_header_bits; ++i)
1123 /* slice_reserved_flag */
1124 vl_rbsp_u(&rbsp, 1);
1125
1126 /* slice_type */
1127 params->slice_type = vl_rbsp_ue(&rbsp);
1128
1129 if (pps->flags.output_flag_present_flag)
1130 /* pic output flag */
1131 vl_rbsp_u(&rbsp, 1);
1132
1133 if (sps->flags.separate_colour_plane_flag)
1134 /* colour_plane_id */
1135 vl_rbsp_u(&rbsp, 2);
1136
1137 if (!pic_info->pStdPictureInfo->flags.IdrPicFlag) {
1138 /* slice_pic_order_cnt_lsb */
1139 params->pic_order_cnt_lsb =
1140 vl_rbsp_u(&rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1141
1142 /* short_term_ref_pic_set_sps_flag */
1143 if (!vl_rbsp_u(&rbsp, 1)) {
1144 uint8_t rps_predict = 0;
1145
1146 if (sps->num_short_term_ref_pic_sets)
1147 rps_predict = vl_rbsp_u(&rbsp, 1);
1148
1149 if (rps_predict) {
1150 /* delta_idx */
1151 vl_rbsp_ue(&rbsp);
1152 /* delta_rps_sign */
1153 vl_rbsp_u(&rbsp, 1);
1154 /* abs_delta_rps */
1155 vl_rbsp_ue(&rbsp);
1156
1157 for (int i = 0 ; i <= pic_info->pStdPictureInfo->NumDeltaPocsOfRefRpsIdx; i++) {
1158 uint8_t used = vl_rbsp_u(&rbsp, 1);
1159 if (!used)
1160 vl_rbsp_u(&rbsp, 1);
1161 }
1162 } else {
1163 /* num_negative_pics */
1164 unsigned num_neg_pics = vl_rbsp_ue(&rbsp);
1165 /* num_positive_pics */
1166 unsigned num_pos_pics = vl_rbsp_ue(&rbsp);
1167
1168 for(unsigned i = 0 ; i < num_neg_pics; ++i) {
1169 /* delta_poc_s0_minus1 */
1170 vl_rbsp_ue(&rbsp);
1171 /* used_by_curr_pic_s0_flag */
1172 vl_rbsp_u(&rbsp, 1);
1173 }
1174
1175 for(unsigned i = 0; i < num_pos_pics; ++i) {
1176 /* delta_poc_s1_minus1 */
1177 vl_rbsp_ue(&rbsp);
1178 /* used_by_curr_pic_s0_flag */
1179 vl_rbsp_u(&rbsp, 1);
1180 }
1181 }
1182
1183 } else {
1184 unsigned num_st_rps = sps->num_short_term_ref_pic_sets;
1185
1186 int numbits = util_logbase2_ceil(num_st_rps);
1187 if (numbits > 0)
1188 /* short_term_ref_pic_set_idx */
1189 vl_rbsp_u(&rbsp, numbits);
1190 }
1191
1192 if (sps->flags.long_term_ref_pics_present_flag) {
1193 unsigned num_lt_sps = 0;
1194
1195 if (sps->num_long_term_ref_pics_sps > 0)
1196 num_lt_sps = vl_rbsp_ue(&rbsp);
1197
1198 unsigned num_lt_pics = vl_rbsp_ue(&rbsp);
1199 unsigned num_refs = num_lt_pics + num_lt_sps;
1200
1201 for (unsigned i = 0; i < num_refs; i++) {
1202 if (i < num_lt_sps) {
1203 if (sps->num_long_term_ref_pics_sps > 1)
1204 /* lt_idx_sps */
1205 vl_rbsp_u(&rbsp,
1206 util_logbase2_ceil(sps->num_long_term_ref_pics_sps));
1207 } else {
1208 /* poc_lsb_lt */
1209 vl_rbsp_u(&rbsp, sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
1210 /* used_by_curr_pic_lt_flag */
1211 vl_rbsp_u(&rbsp, 1);
1212 }
1213
1214 /* poc_msb_present */
1215 if (vl_rbsp_u(&rbsp, 1)) {
1216 /* delta_poc_msb_cycle_lt */
1217 vl_rbsp_ue(&rbsp);
1218 }
1219 }
1220 }
1221
1222 if (sps->flags.sps_temporal_mvp_enabled_flag)
1223 params->temporal_mvp_enable = vl_rbsp_u(&rbsp, 1);
1224 }
1225
1226 if (sps->flags.sample_adaptive_offset_enabled_flag) {
1227 params->sao_luma_flag = vl_rbsp_u(&rbsp, 1);
1228 if (sps->chroma_format_idc)
1229 params->sao_chroma_flag = vl_rbsp_u(&rbsp, 1);
1230 }
1231
1232 params->max_num_merge_cand = 5;
1233
1234 if (params->slice_type != STD_VIDEO_H265_SLICE_TYPE_I) {
1235
1236 params->num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active_minus1 + 1;
1237
1238 if (params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)
1239 params->num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active_minus1 + 1;
1240 else
1241 params->num_ref_idx_l1_active = 0;
1242
1243 /* num_ref_idx_active_override_flag */
1244 if (vl_rbsp_u(&rbsp, 1)) {
1245 params->num_ref_idx_l0_active = vl_rbsp_ue(&rbsp) + 1;
1246 if (params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)
1247 params->num_ref_idx_l1_active = vl_rbsp_ue(&rbsp) + 1;
1248 }
1249
1250 if (pps->flags.lists_modification_present_flag) {
1251 params->rpl_modification_flag[0] = vl_rbsp_u(&rbsp, 1);
1252 if (params->rpl_modification_flag[0]) {
1253 for (int i = 0; i < params->num_ref_idx_l0_active; i++) {
1254 /* list_entry_l0 */
1255 vl_rbsp_u(&rbsp,
1256 util_logbase2_ceil(params->num_ref_idx_l0_active + params->num_ref_idx_l1_active));
1257 }
1258 }
1259
1260 if (params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B) {
1261 params->rpl_modification_flag[1] = vl_rbsp_u(&rbsp, 1);
1262 if (params->rpl_modification_flag[1]) {
1263 for (int i = 0; i < params->num_ref_idx_l1_active; i++) {
1264 /* list_entry_l1 */
1265 vl_rbsp_u(&rbsp,
1266 util_logbase2_ceil(params->num_ref_idx_l0_active + params->num_ref_idx_l1_active));
1267 }
1268 }
1269 }
1270 }
1271
1272 if (params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)
1273 params->mvd_l1_zero_flag = vl_rbsp_u(&rbsp, 1);
1274
1275 if (pps->flags.cabac_init_present_flag)
1276 /* cabac_init_flag */
1277 params->cabac_init_idc = vl_rbsp_u(&rbsp, 1);
1278
1279 if (params->temporal_mvp_enable) {
1280 if (params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)
1281 params->collocated_list = !vl_rbsp_u(&rbsp, 1);
1282
1283 if (params->collocated_list == 0) {
1284 if (params->num_ref_idx_l0_active > 1)
1285 params->collocated_ref_idx = vl_rbsp_ue(&rbsp);
1286 } else if (params->collocated_list == 1) {
1287 if (params->num_ref_idx_l1_active > 1)
1288 params->collocated_ref_idx = vl_rbsp_ue(&rbsp);
1289 }
1290 }
1291
1292 if ((pps->flags.weighted_pred_flag && params->slice_type == STD_VIDEO_H265_SLICE_TYPE_P) ||
1293 (pps->flags.weighted_bipred_flag && params->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)) {
1294 h265_pred_weight_table(params, &rbsp, sps, params->slice_type);
1295 }
1296
1297 params->max_num_merge_cand -= vl_rbsp_ue(&rbsp);
1298 }
1299
1300 params->slice_qp_delta = vl_rbsp_se(&rbsp);
1301
1302 if (pps->flags.pps_slice_chroma_qp_offsets_present_flag) {
1303 params->slice_cb_qp_offset = vl_rbsp_se(&rbsp);
1304 params->slice_cr_qp_offset = vl_rbsp_se(&rbsp);
1305 }
1306
1307 if (pps->flags.chroma_qp_offset_list_enabled_flag)
1308 /* cu_chroma_qp_offset_enabled_flag */
1309 vl_rbsp_u(&rbsp, 1);
1310
1311 if (pps->flags.deblocking_filter_control_present_flag) {
1312 if (pps->flags.deblocking_filter_override_enabled_flag) {
1313 /* deblocking_filter_override_flag */
1314 if (vl_rbsp_u(&rbsp, 1)) {
1315 params->disable_deblocking_filter_idc = vl_rbsp_u(&rbsp, 1);
1316
1317 if (!params->disable_deblocking_filter_idc) {
1318 params->beta_offset_div2 = vl_rbsp_se(&rbsp);
1319 params->tc_offset_div2 = vl_rbsp_se(&rbsp);
1320 }
1321 } else {
1322 params->disable_deblocking_filter_idc =
1323 pps->flags.pps_deblocking_filter_disabled_flag;
1324 }
1325 }
1326 }
1327
1328 if (pps->flags.pps_loop_filter_across_slices_enabled_flag &&
1329 (params->sao_luma_flag || params->sao_chroma_flag ||
1330 !params->disable_deblocking_filter_idc))
1331 params->loop_filter_across_slices_enable = vl_rbsp_u(&rbsp, 1);
1332
1333 if (pps->flags.tiles_enabled_flag || pps->flags.entropy_coding_sync_enabled_flag) {
1334 unsigned num_entry_points_offsets = vl_rbsp_ue(&rbsp);
1335
1336 if (num_entry_points_offsets > 0) {
1337 unsigned offset_len = vl_rbsp_ue(&rbsp) + 1;
1338 for (unsigned i = 0; i < num_entry_points_offsets; i++) {
1339 /* entry_point_offset_minus1 */
1340 vl_rbsp_u(&rbsp, offset_len);
1341 }
1342 }
1343 }
1344
1345 if (pps->flags.pps_extension_present_flag) {
1346 unsigned length = vl_rbsp_ue(&rbsp);
1347 for (unsigned i = 0; i < length; i++)
1348 /* slice_reserved_undetermined_flag */
1349 vl_rbsp_u(&rbsp, 1);
1350 }
1351
1352 unsigned header_bits =
1353 (slice_size * 8 - 24 /* start code */) - vl_vlc_bits_left(&rbsp.nal) - rbsp.removed;
1354 params->slice_data_bytes_offset = (header_bits + 8) / 8;
1355 }
1356
1357 void
vk_video_get_profile_alignments(const VkVideoProfileListInfoKHR * profile_list,uint32_t * width_align_out,uint32_t * height_align_out)1358 vk_video_get_profile_alignments(const VkVideoProfileListInfoKHR *profile_list,
1359 uint32_t *width_align_out, uint32_t *height_align_out)
1360 {
1361 uint32_t width_align = 1, height_align = 1;
1362
1363 if (!profile_list) {
1364 width_align = MAX2(width_align, VK_VIDEO_H264_MACROBLOCK_WIDTH);
1365 height_align = MAX2(height_align, VK_VIDEO_H264_MACROBLOCK_HEIGHT);
1366 width_align = MAX2(width_align, VK_VIDEO_H265_CTU_MAX_WIDTH);
1367 height_align = MAX2(height_align, VK_VIDEO_H265_CTU_MAX_HEIGHT);
1368 width_align = MAX2(width_align, VK_VIDEO_AV1_BLOCK_WIDTH);
1369 height_align = MAX2(height_align, VK_VIDEO_AV1_BLOCK_HEIGHT);
1370 } else {
1371 for (unsigned i = 0; i < profile_list->profileCount; i++) {
1372 if (profile_list->pProfiles[i].videoCodecOperation == VK_VIDEO_CODEC_OPERATION_DECODE_H264_BIT_KHR ||
1373 profile_list->pProfiles[i].videoCodecOperation == VK_VIDEO_CODEC_OPERATION_ENCODE_H264_BIT_KHR
1374 ) {
1375 width_align = MAX2(width_align, VK_VIDEO_H264_MACROBLOCK_WIDTH);
1376 height_align = MAX2(height_align, VK_VIDEO_H264_MACROBLOCK_HEIGHT);
1377 }
1378 if (profile_list->pProfiles[i].videoCodecOperation == VK_VIDEO_CODEC_OPERATION_DECODE_H265_BIT_KHR ||
1379 profile_list->pProfiles[i].videoCodecOperation == VK_VIDEO_CODEC_OPERATION_ENCODE_H265_BIT_KHR
1380 ) {
1381 width_align = MAX2(width_align, VK_VIDEO_H265_CTU_MAX_WIDTH);
1382 height_align = MAX2(height_align, VK_VIDEO_H265_CTU_MAX_HEIGHT);
1383 }
1384 if (profile_list->pProfiles[i].videoCodecOperation == VK_VIDEO_CODEC_OPERATION_DECODE_AV1_BIT_KHR) {
1385 width_align = MAX2(width_align, VK_VIDEO_AV1_BLOCK_WIDTH);
1386 height_align = MAX2(height_align, VK_VIDEO_AV1_BLOCK_HEIGHT);
1387 }
1388 }
1389 }
1390 *width_align_out = width_align;
1391 *height_align_out = height_align;
1392 }
1393
1394 static const uint8_t vk_video_h264_levels[] = {10, 11, 12, 13, 20, 21, 22, 30, 31, 32, 40, 41, 42, 50, 51, 52, 60, 61, 62};
1395 uint8_t
vk_video_get_h264_level(StdVideoH264LevelIdc level)1396 vk_video_get_h264_level(StdVideoH264LevelIdc level)
1397 {
1398 assert(level <= STD_VIDEO_H264_LEVEL_IDC_6_2);
1399 return vk_video_h264_levels[level];
1400 }
1401
1402 const StdVideoH264SequenceParameterSet *
vk_video_find_h264_enc_std_sps(const struct vk_video_session_parameters * params,uint32_t id)1403 vk_video_find_h264_enc_std_sps(const struct vk_video_session_parameters *params,
1404 uint32_t id)
1405 {
1406 return &find_h264_enc_h264_sps(params, id)->base;
1407 }
1408
1409 const StdVideoH264PictureParameterSet *
vk_video_find_h264_enc_std_pps(const struct vk_video_session_parameters * params,uint32_t id)1410 vk_video_find_h264_enc_std_pps(const struct vk_video_session_parameters *params,
1411 uint32_t id)
1412 {
1413 return &find_h264_enc_h264_pps(params, id)->base;
1414 }
1415
1416 const StdVideoH265VideoParameterSet *
vk_video_find_h265_enc_std_vps(const struct vk_video_session_parameters * params,uint32_t id)1417 vk_video_find_h265_enc_std_vps(const struct vk_video_session_parameters *params,
1418 uint32_t id)
1419 {
1420 return &find_h265_enc_h265_vps(params, id)->base;
1421 }
1422
1423 const StdVideoH265SequenceParameterSet *
vk_video_find_h265_enc_std_sps(const struct vk_video_session_parameters * params,uint32_t id)1424 vk_video_find_h265_enc_std_sps(const struct vk_video_session_parameters *params,
1425 uint32_t id)
1426 {
1427 return &find_h265_enc_h265_sps(params, id)->base;
1428 }
1429
1430 const StdVideoH265PictureParameterSet *
vk_video_find_h265_enc_std_pps(const struct vk_video_session_parameters * params,uint32_t id)1431 vk_video_find_h265_enc_std_pps(const struct vk_video_session_parameters *params,
1432 uint32_t id)
1433 {
1434 return &find_h265_enc_h265_pps(params, id)->base;
1435 }
1436
1437 enum H264NALUType
1438 {
1439 H264_NAL_UNSPECIFIED = 0,
1440 H264_NAL_SLICE = 1,
1441 H264_NAL_SLICEDATA_A = 2,
1442 H264_NAL_SLICEDATA_B = 3,
1443 H264_NAL_SLICEDATA_C = 4,
1444 H264_NAL_IDR = 5,
1445 H264_NAL_SEI = 6,
1446 H264_NAL_SPS = 7,
1447 H264_NAL_PPS = 8,
1448 H264_NAL_ACCESS_UNIT_DEMILITER = 9,
1449 H264_NAL_END_OF_SEQUENCE = 10,
1450 H264_NAL_END_OF_STREAM = 11,
1451 H264_NAL_FILLER_DATA = 12,
1452 H264_NAL_SPS_EXTENSION = 13,
1453 H264_NAL_PREFIX = 14,
1454 /* 15...18 RESERVED */
1455 H264_NAL_AUXILIARY_SLICE = 19,
1456 /* 20...23 RESERVED */
1457 /* 24...31 UNSPECIFIED */
1458 };
1459
1460 enum HEVCNALUnitType {
1461 HEVC_NAL_TRAIL_N = 0,
1462 HEVC_NAL_TRAIL_R = 1,
1463 HEVC_NAL_TSA_N = 2,
1464 HEVC_NAL_TSA_R = 3,
1465 HEVC_NAL_STSA_N = 4,
1466 HEVC_NAL_STSA_R = 5,
1467 HEVC_NAL_RADL_N = 6,
1468 HEVC_NAL_RADL_R = 7,
1469 HEVC_NAL_RASL_N = 8,
1470 HEVC_NAL_RASL_R = 9,
1471 HEVC_NAL_VCL_N10 = 10,
1472 HEVC_NAL_VCL_R11 = 11,
1473 HEVC_NAL_VCL_N12 = 12,
1474 HEVC_NAL_VCL_R13 = 13,
1475 HEVC_NAL_VCL_N14 = 14,
1476 HEVC_NAL_VCL_R15 = 15,
1477 HEVC_NAL_BLA_W_LP = 16,
1478 HEVC_NAL_BLA_W_RADL = 17,
1479 HEVC_NAL_BLA_N_LP = 18,
1480 HEVC_NAL_IDR_W_RADL = 19,
1481 HEVC_NAL_IDR_N_LP = 20,
1482 HEVC_NAL_CRA_NUT = 21,
1483 HEVC_NAL_VPS_NUT = 32,
1484 HEVC_NAL_SPS_NUT = 33,
1485 HEVC_NAL_PPS_NUT = 34,
1486 };
1487
1488 unsigned
vk_video_get_h265_nal_unit(const StdVideoEncodeH265PictureInfo * pic_info)1489 vk_video_get_h265_nal_unit(const StdVideoEncodeH265PictureInfo *pic_info)
1490 {
1491 switch (pic_info->pic_type) {
1492 case STD_VIDEO_H265_PICTURE_TYPE_IDR:
1493 return HEVC_NAL_IDR_W_RADL;
1494 case STD_VIDEO_H265_PICTURE_TYPE_I:
1495 return HEVC_NAL_CRA_NUT;
1496 case STD_VIDEO_H265_PICTURE_TYPE_P:
1497 if (pic_info->TemporalId > 0)
1498 if (pic_info->flags.is_reference)
1499 return HEVC_NAL_TSA_R;
1500 else
1501 return HEVC_NAL_TSA_N;
1502 else
1503 if (pic_info->flags.is_reference)
1504 return HEVC_NAL_TRAIL_R;
1505 else
1506 return HEVC_NAL_TRAIL_N;
1507 case STD_VIDEO_H265_PICTURE_TYPE_B:
1508 if (pic_info->flags.IrapPicFlag)
1509 if (pic_info->flags.is_reference)
1510 return HEVC_NAL_RASL_R;
1511 else
1512 return HEVC_NAL_RASL_N;
1513 else if (pic_info->TemporalId > 0)
1514 if (pic_info->flags.is_reference)
1515 return HEVC_NAL_TSA_R;
1516 else
1517 return HEVC_NAL_TSA_N;
1518 else
1519 if (pic_info->flags.is_reference)
1520 return HEVC_NAL_TRAIL_R;
1521 else
1522 return HEVC_NAL_TRAIL_N;
1523 break;
1524 default:
1525 assert(0);
1526 break;
1527 }
1528 return 0;
1529 }
1530
1531 static const uint8_t vk_video_h265_levels[] = {10, 20, 21, 30, 31, 40, 41, 50, 51, 52, 60, 61, 62};
1532
1533 static uint8_t
vk_video_get_h265_level(StdVideoH265LevelIdc level)1534 vk_video_get_h265_level(StdVideoH265LevelIdc level)
1535 {
1536 assert(level <= STD_VIDEO_H265_LEVEL_IDC_6_2);
1537 return vk_video_h265_levels[level];
1538 }
1539
1540 static void
emit_nalu_header(struct vl_bitstream_encoder * enc,int nal_ref,int nal_unit)1541 emit_nalu_header(struct vl_bitstream_encoder *enc,
1542 int nal_ref, int nal_unit)
1543 {
1544 enc->prevent_start_code = false;
1545
1546 vl_bitstream_put_bits(enc, 24, 0);
1547 vl_bitstream_put_bits(enc, 8, 1);
1548 vl_bitstream_put_bits(enc, 1, 0);
1549 vl_bitstream_put_bits(enc, 2, nal_ref); /* SPS NAL REF */
1550 vl_bitstream_put_bits(enc, 5, nal_unit); /* SPS NAL UNIT */
1551 vl_bitstream_flush(enc);
1552
1553 enc->prevent_start_code = true;
1554 }
1555
1556 static void
encode_hrd_params(struct vl_bitstream_encoder * enc,const StdVideoH264HrdParameters * hrd)1557 encode_hrd_params(struct vl_bitstream_encoder *enc,
1558 const StdVideoH264HrdParameters *hrd)
1559 {
1560 vl_bitstream_exp_golomb_ue(enc, hrd->cpb_cnt_minus1);
1561 vl_bitstream_put_bits(enc, 4, hrd->bit_rate_scale);
1562 vl_bitstream_put_bits(enc, 4, hrd->cpb_size_scale);
1563 for (int sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
1564 vl_bitstream_exp_golomb_ue(enc, hrd->bit_rate_value_minus1[sched_sel_idx]);
1565 vl_bitstream_exp_golomb_ue(enc, hrd->cpb_size_value_minus1[sched_sel_idx]);
1566 vl_bitstream_put_bits(enc, 1, hrd->cbr_flag[sched_sel_idx]);
1567 }
1568 vl_bitstream_put_bits(enc, 5, hrd->initial_cpb_removal_delay_length_minus1);
1569 vl_bitstream_put_bits(enc, 5, hrd->cpb_removal_delay_length_minus1);
1570 vl_bitstream_put_bits(enc, 5, hrd->dpb_output_delay_length_minus1);
1571 vl_bitstream_put_bits(enc, 5, hrd->time_offset_length);
1572 }
1573
1574 void
vk_video_encode_h264_sps(const StdVideoH264SequenceParameterSet * sps,size_t size_limit,size_t * data_size_ptr,void * data_ptr)1575 vk_video_encode_h264_sps(const StdVideoH264SequenceParameterSet *sps,
1576 size_t size_limit,
1577 size_t *data_size_ptr,
1578 void *data_ptr)
1579 {
1580 struct vl_bitstream_encoder enc;
1581 uint32_t data_size = *data_size_ptr;
1582
1583 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, size_limit);
1584
1585 emit_nalu_header(&enc, 3, H264_NAL_SPS);
1586
1587 vl_bitstream_put_bits(&enc, 8, sps->profile_idc);
1588 vl_bitstream_put_bits(&enc, 1, sps->flags.constraint_set0_flag);
1589 vl_bitstream_put_bits(&enc, 1, sps->flags.constraint_set1_flag);
1590 vl_bitstream_put_bits(&enc, 1, sps->flags.constraint_set2_flag);
1591 vl_bitstream_put_bits(&enc, 1, sps->flags.constraint_set3_flag);
1592 vl_bitstream_put_bits(&enc, 1, sps->flags.constraint_set4_flag);
1593 vl_bitstream_put_bits(&enc, 1, sps->flags.constraint_set5_flag);
1594 vl_bitstream_put_bits(&enc, 2, 0);
1595 vl_bitstream_put_bits(&enc, 8, vk_video_get_h264_level(sps->level_idc));
1596 vl_bitstream_exp_golomb_ue(&enc, sps->seq_parameter_set_id);
1597
1598 if (sps->profile_idc == STD_VIDEO_H264_PROFILE_IDC_HIGH /* high10 as well */) {
1599 vl_bitstream_exp_golomb_ue(&enc, sps->chroma_format_idc);
1600 vl_bitstream_exp_golomb_ue(&enc, sps->bit_depth_luma_minus8);
1601 vl_bitstream_exp_golomb_ue(&enc, sps->bit_depth_chroma_minus8);
1602 vl_bitstream_put_bits(&enc, 1, sps->flags.qpprime_y_zero_transform_bypass_flag);
1603 vl_bitstream_put_bits(&enc, 1, sps->flags.seq_scaling_matrix_present_flag);
1604 }
1605
1606 vl_bitstream_exp_golomb_ue(&enc, sps->log2_max_frame_num_minus4);
1607
1608 vl_bitstream_exp_golomb_ue(&enc, sps->pic_order_cnt_type);
1609 if (sps->pic_order_cnt_type == 0)
1610 vl_bitstream_exp_golomb_ue(&enc, sps->log2_max_pic_order_cnt_lsb_minus4);
1611
1612 vl_bitstream_exp_golomb_ue(&enc, sps->max_num_ref_frames);
1613 vl_bitstream_put_bits(&enc, 1, sps->flags.gaps_in_frame_num_value_allowed_flag);
1614 vl_bitstream_exp_golomb_ue(&enc, sps->pic_width_in_mbs_minus1);
1615 vl_bitstream_exp_golomb_ue(&enc, sps->pic_height_in_map_units_minus1);
1616
1617 vl_bitstream_put_bits(&enc, 1, sps->flags.frame_mbs_only_flag);
1618 vl_bitstream_put_bits(&enc, 1, sps->flags.direct_8x8_inference_flag);
1619
1620 vl_bitstream_put_bits(&enc, 1, sps->flags.frame_cropping_flag);
1621 if (sps->flags.frame_cropping_flag) {
1622 vl_bitstream_exp_golomb_ue(&enc, sps->frame_crop_left_offset);
1623 vl_bitstream_exp_golomb_ue(&enc, sps->frame_crop_right_offset);
1624 vl_bitstream_exp_golomb_ue(&enc, sps->frame_crop_top_offset);
1625 vl_bitstream_exp_golomb_ue(&enc, sps->frame_crop_bottom_offset);
1626 }
1627
1628 vl_bitstream_put_bits(&enc, 1, sps->flags.vui_parameters_present_flag); /* vui parameters present flag */
1629 if (sps->flags.vui_parameters_present_flag) {
1630 const StdVideoH264SequenceParameterSetVui *vui = sps->pSequenceParameterSetVui;
1631 vl_bitstream_put_bits(&enc, 1, vui->flags.aspect_ratio_info_present_flag);
1632
1633 if (vui->flags.aspect_ratio_info_present_flag) {
1634 vl_bitstream_put_bits(&enc, 8, vui->aspect_ratio_idc);
1635 if (vui->aspect_ratio_idc == STD_VIDEO_H264_ASPECT_RATIO_IDC_EXTENDED_SAR) {
1636 vl_bitstream_put_bits(&enc, 16, vui->sar_width);
1637 vl_bitstream_put_bits(&enc, 16, vui->sar_height);
1638 }
1639 }
1640
1641 vl_bitstream_put_bits(&enc, 1, vui->flags.overscan_info_present_flag);
1642 if (vui->flags.overscan_info_present_flag)
1643 vl_bitstream_put_bits(&enc, 1, vui->flags.overscan_appropriate_flag);
1644 vl_bitstream_put_bits(&enc, 1, vui->flags.video_signal_type_present_flag);
1645 if (vui->flags.video_signal_type_present_flag) {
1646 vl_bitstream_put_bits(&enc, 3, vui->video_format);
1647 vl_bitstream_put_bits(&enc, 1, vui->flags.video_full_range_flag);
1648 vl_bitstream_put_bits(&enc, 1, vui->flags.color_description_present_flag);
1649 if (vui->flags.color_description_present_flag) {
1650 vl_bitstream_put_bits(&enc, 8, vui->colour_primaries);
1651 vl_bitstream_put_bits(&enc, 8, vui->transfer_characteristics);
1652 vl_bitstream_put_bits(&enc, 8, vui->matrix_coefficients);
1653 }
1654 }
1655
1656 vl_bitstream_put_bits(&enc, 1, vui->flags.chroma_loc_info_present_flag);
1657 if (vui->flags.chroma_loc_info_present_flag) {
1658 vl_bitstream_exp_golomb_ue(&enc, vui->chroma_sample_loc_type_top_field);
1659 vl_bitstream_exp_golomb_ue(&enc, vui->chroma_sample_loc_type_bottom_field);
1660 }
1661 vl_bitstream_put_bits(&enc, 1, vui->flags.timing_info_present_flag);
1662 if (vui->flags.timing_info_present_flag) {
1663 vl_bitstream_put_bits(&enc, 32, vui->num_units_in_tick);
1664 vl_bitstream_put_bits(&enc, 32, vui->time_scale);
1665 vl_bitstream_put_bits(&enc, 1, vui->flags.fixed_frame_rate_flag);
1666 }
1667 vl_bitstream_put_bits(&enc, 1, vui->flags.nal_hrd_parameters_present_flag);
1668 if (vui->flags.nal_hrd_parameters_present_flag)
1669 encode_hrd_params(&enc, vui->pHrdParameters);
1670 vl_bitstream_put_bits(&enc, 1, vui->flags.vcl_hrd_parameters_present_flag);
1671 if (vui->flags.vcl_hrd_parameters_present_flag)
1672 encode_hrd_params(&enc, vui->pHrdParameters);
1673 if (vui->flags.nal_hrd_parameters_present_flag || vui->flags.vcl_hrd_parameters_present_flag)
1674 vl_bitstream_put_bits(&enc, 1, 0);
1675 vl_bitstream_put_bits(&enc, 1, 0);
1676 vl_bitstream_put_bits(&enc, 1, vui->flags.bitstream_restriction_flag);
1677 if (vui->flags.bitstream_restriction_flag) {
1678 vl_bitstream_put_bits(&enc, 1, 0);
1679 vl_bitstream_exp_golomb_ue(&enc, 0);
1680 vl_bitstream_exp_golomb_ue(&enc, 0);
1681 vl_bitstream_exp_golomb_ue(&enc, 0);
1682 vl_bitstream_exp_golomb_ue(&enc, 0);
1683 vl_bitstream_exp_golomb_ue(&enc, vui->max_num_reorder_frames);
1684 vl_bitstream_exp_golomb_ue(&enc, vui->max_dec_frame_buffering);
1685 }
1686 }
1687
1688 vl_bitstream_rbsp_trailing(&enc);
1689
1690 vl_bitstream_flush(&enc);
1691 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
1692 vl_bitstream_encoder_free(&enc);
1693 }
1694
1695 void
vk_video_encode_h264_pps(const StdVideoH264PictureParameterSet * pps,bool high_profile,size_t size_limit,size_t * data_size_ptr,void * data_ptr)1696 vk_video_encode_h264_pps(const StdVideoH264PictureParameterSet *pps,
1697 bool high_profile,
1698 size_t size_limit,
1699 size_t *data_size_ptr,
1700 void *data_ptr)
1701 {
1702 struct vl_bitstream_encoder enc;
1703 uint32_t data_size = *data_size_ptr;
1704
1705 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, size_limit);
1706
1707 emit_nalu_header(&enc, 3, H264_NAL_PPS);
1708
1709 vl_bitstream_exp_golomb_ue(&enc, pps->pic_parameter_set_id);
1710 vl_bitstream_exp_golomb_ue(&enc, pps->seq_parameter_set_id);
1711 vl_bitstream_put_bits(&enc, 1, pps->flags.entropy_coding_mode_flag);
1712 vl_bitstream_put_bits(&enc, 1, pps->flags.bottom_field_pic_order_in_frame_present_flag);
1713 vl_bitstream_exp_golomb_ue(&enc, 0); /* num_slice_groups_minus1 */
1714
1715 vl_bitstream_exp_golomb_ue(&enc, pps->num_ref_idx_l0_default_active_minus1);
1716 vl_bitstream_exp_golomb_ue(&enc, pps->num_ref_idx_l1_default_active_minus1);
1717 vl_bitstream_put_bits(&enc, 1, pps->flags.weighted_pred_flag);
1718 vl_bitstream_put_bits(&enc, 2, pps->weighted_bipred_idc);
1719 vl_bitstream_exp_golomb_se(&enc, pps->pic_init_qp_minus26);
1720 vl_bitstream_exp_golomb_se(&enc, pps->pic_init_qs_minus26);
1721 vl_bitstream_exp_golomb_se(&enc, pps->chroma_qp_index_offset);
1722 vl_bitstream_put_bits(&enc, 1, pps->flags.deblocking_filter_control_present_flag);
1723 vl_bitstream_put_bits(&enc, 1, pps->flags.constrained_intra_pred_flag);
1724 vl_bitstream_put_bits(&enc, 1, pps->flags.redundant_pic_cnt_present_flag);
1725
1726 /* high profile */
1727 if (high_profile) {
1728 vl_bitstream_put_bits(&enc, 1, pps->flags.transform_8x8_mode_flag);
1729 vl_bitstream_put_bits(&enc, 1, pps->flags.pic_scaling_matrix_present_flag);
1730 vl_bitstream_exp_golomb_se(&enc, pps->second_chroma_qp_index_offset);
1731 }
1732 vl_bitstream_rbsp_trailing(&enc);
1733
1734 vl_bitstream_flush(&enc);
1735 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
1736 vl_bitstream_encoder_free(&enc);
1737 }
1738
1739 static void
emit_nalu_h265_header(struct vl_bitstream_encoder * enc,int nal_unit_type,int temporal_id)1740 emit_nalu_h265_header(struct vl_bitstream_encoder *enc,
1741 int nal_unit_type, int temporal_id)
1742 {
1743 enc->prevent_start_code = false;
1744
1745 vl_bitstream_put_bits(enc, 24, 0);
1746 vl_bitstream_put_bits(enc, 8, 1);
1747 vl_bitstream_put_bits(enc, 1, 0);
1748 vl_bitstream_put_bits(enc, 6, nal_unit_type); /* SPS NAL REF */
1749 vl_bitstream_put_bits(enc, 6, 0); /* nuh_layer_id */
1750 vl_bitstream_put_bits(enc, 3, temporal_id + 1); /* nuh_temporal_id_plus1 */
1751 vl_bitstream_flush(enc);
1752
1753 enc->prevent_start_code = true;
1754 }
1755
1756 static void
encode_h265_profile_tier_level(struct vl_bitstream_encoder * enc,const StdVideoH265ProfileTierLevel * ptl,unsigned int max_sub_layers_minus1)1757 encode_h265_profile_tier_level(struct vl_bitstream_encoder *enc,
1758 const StdVideoH265ProfileTierLevel *ptl,
1759 unsigned int max_sub_layers_minus1)
1760 {
1761 vl_bitstream_put_bits(enc, 2, 0);
1762 vl_bitstream_put_bits(enc, 1, ptl->flags.general_tier_flag);
1763 vl_bitstream_put_bits(enc, 5, ptl->general_profile_idc);
1764
1765 for (int j = 0; j < 32; j++)
1766 vl_bitstream_put_bits(enc, 1, j == ptl->general_profile_idc);
1767
1768 vl_bitstream_put_bits(enc, 1, ptl->flags.general_progressive_source_flag);
1769 vl_bitstream_put_bits(enc, 1, ptl->flags.general_interlaced_source_flag);
1770 vl_bitstream_put_bits(enc, 1, ptl->flags.general_non_packed_constraint_flag);
1771 vl_bitstream_put_bits(enc, 1, ptl->flags.general_frame_only_constraint_flag);
1772 vl_bitstream_put_bits(enc, 31, 0);
1773 vl_bitstream_put_bits(enc, 13, 0);
1774 vl_bitstream_put_bits(enc, 8, vk_video_get_h265_level(ptl->general_level_idc));
1775
1776 if (max_sub_layers_minus1 > 0) {
1777 /* sub_layer_(profile|level)_present_flag, plus padding */
1778 vl_bitstream_put_bits(enc, 16, 0);
1779 }
1780 }
1781
1782 void
vk_video_encode_h265_vps(const StdVideoH265VideoParameterSet * vps,size_t size_limit,size_t * data_size_ptr,void * data_ptr)1783 vk_video_encode_h265_vps(const StdVideoH265VideoParameterSet *vps,
1784 size_t size_limit,
1785 size_t *data_size_ptr,
1786 void *data_ptr)
1787 {
1788 struct vl_bitstream_encoder enc;
1789 uint32_t data_size = *data_size_ptr;
1790
1791 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, size_limit);
1792
1793 emit_nalu_h265_header(&enc, HEVC_NAL_VPS_NUT, 0);
1794
1795 vl_bitstream_put_bits(&enc, 4, vps->vps_video_parameter_set_id);
1796 vl_bitstream_put_bits(&enc, 2, 3);
1797 vl_bitstream_put_bits(&enc, 6, 0);//vps->vps_max_layers_minus1);
1798 vl_bitstream_put_bits(&enc, 3, vps->vps_max_sub_layers_minus1);
1799 vl_bitstream_put_bits(&enc, 1, vps->flags.vps_temporal_id_nesting_flag);
1800 vl_bitstream_put_bits(&enc, 16, 0xffff);
1801
1802 encode_h265_profile_tier_level(&enc, vps->pProfileTierLevel, vps->vps_max_sub_layers_minus1);
1803
1804 vl_bitstream_put_bits(&enc, 1, vps->flags.vps_sub_layer_ordering_info_present_flag);
1805
1806 int i = vps->flags.vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers_minus1;
1807 for (; i <= vps->vps_max_sub_layers_minus1; i++) {
1808 vl_bitstream_exp_golomb_ue(&enc, vps->pDecPicBufMgr->max_dec_pic_buffering_minus1[i]);
1809 vl_bitstream_exp_golomb_ue(&enc, vps->pDecPicBufMgr->max_num_reorder_pics[i]);
1810 vl_bitstream_exp_golomb_ue(&enc, vps->pDecPicBufMgr->max_latency_increase_plus1[i]);
1811 }
1812
1813 vl_bitstream_put_bits(&enc, 6, 0);//vps->vps_max_layer_id);
1814 vl_bitstream_exp_golomb_ue(&enc, 0);//vps->vps_num_layer_sets_minus1);
1815 vl_bitstream_put_bits(&enc, 1, vps->flags.vps_timing_info_present_flag);
1816
1817 if (vps->flags.vps_timing_info_present_flag) {
1818 vl_bitstream_put_bits(&enc, 32, vps->vps_num_units_in_tick);
1819 vl_bitstream_put_bits(&enc, 32, vps->vps_time_scale);
1820 vl_bitstream_put_bits(&enc, 1, vps->flags.vps_poc_proportional_to_timing_flag);
1821 if (vps->flags.vps_poc_proportional_to_timing_flag)
1822 vl_bitstream_exp_golomb_ue(&enc, vps->vps_num_ticks_poc_diff_one_minus1);
1823 vl_bitstream_exp_golomb_ue(&enc, 0);
1824 }
1825
1826 vl_bitstream_put_bits(&enc, 1, 0); /* vps extension flag */
1827 vl_bitstream_rbsp_trailing(&enc);
1828
1829 vl_bitstream_flush(&enc);
1830 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
1831 vl_bitstream_encoder_free(&enc);
1832 }
1833
1834 static void
encode_rps(struct vl_bitstream_encoder * enc,const StdVideoH265SequenceParameterSet * sps,int st_rps_idx)1835 encode_rps(struct vl_bitstream_encoder *enc,
1836 const StdVideoH265SequenceParameterSet *sps,
1837 int st_rps_idx)
1838 {
1839 const StdVideoH265ShortTermRefPicSet *rps = &sps->pShortTermRefPicSet[st_rps_idx];
1840 if (st_rps_idx != 0)
1841 vl_bitstream_put_bits(enc, 1, rps->flags.inter_ref_pic_set_prediction_flag);
1842
1843 if (rps->flags.inter_ref_pic_set_prediction_flag) {
1844 int ref_rps_idx = st_rps_idx - (rps->delta_idx_minus1 + 1);
1845 vl_bitstream_put_bits(enc, 1, rps->flags.delta_rps_sign);
1846 vl_bitstream_exp_golomb_ue(enc, rps->abs_delta_rps_minus1);
1847
1848 const StdVideoH265ShortTermRefPicSet *rps_ref = &sps->pShortTermRefPicSet[ref_rps_idx];
1849 int num_delta_pocs = rps_ref->num_negative_pics + rps_ref->num_positive_pics;
1850
1851 for (int j = 0; j < num_delta_pocs; j++) {
1852 vl_bitstream_put_bits(enc, 1, !!(rps->used_by_curr_pic_flag & (1 << j)));
1853 if (!(rps->used_by_curr_pic_flag & (1 << j))) {
1854 vl_bitstream_put_bits(enc, 1, !!(rps->use_delta_flag & (1 << j)));
1855 }
1856 }
1857 } else {
1858 vl_bitstream_exp_golomb_ue(enc, rps->num_negative_pics);
1859 vl_bitstream_exp_golomb_ue(enc, rps->num_positive_pics);
1860
1861 for (int i = 0; i < rps->num_negative_pics; i++) {
1862 vl_bitstream_exp_golomb_ue(enc, rps->delta_poc_s0_minus1[i]);
1863 vl_bitstream_put_bits(enc, 1, !!(rps->used_by_curr_pic_s0_flag & (1 << i)));
1864 }
1865 for (int i = 0; i < rps->num_positive_pics; i++) {
1866 vl_bitstream_exp_golomb_ue(enc, rps->delta_poc_s1_minus1[i]);
1867 vl_bitstream_put_bits(enc, 1, !!(rps->used_by_curr_pic_s1_flag & (1 << i)));
1868 }
1869 }
1870 }
1871
1872 void
vk_video_encode_h265_sps(const StdVideoH265SequenceParameterSet * sps,size_t size_limit,size_t * data_size_ptr,void * data_ptr)1873 vk_video_encode_h265_sps(const StdVideoH265SequenceParameterSet *sps,
1874 size_t size_limit,
1875 size_t *data_size_ptr,
1876 void *data_ptr)
1877 {
1878 struct vl_bitstream_encoder enc;
1879 uint32_t data_size = *data_size_ptr;
1880
1881 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, size_limit);
1882
1883 emit_nalu_h265_header(&enc, HEVC_NAL_SPS_NUT, 0);
1884
1885 vl_bitstream_put_bits(&enc, 4, sps->sps_video_parameter_set_id);
1886 vl_bitstream_put_bits(&enc, 3, sps->sps_max_sub_layers_minus1);
1887 vl_bitstream_put_bits(&enc, 1, sps->flags.sps_temporal_id_nesting_flag);
1888
1889 encode_h265_profile_tier_level(&enc, sps->pProfileTierLevel, sps->sps_max_sub_layers_minus1);
1890
1891 vl_bitstream_exp_golomb_ue(&enc, sps->sps_seq_parameter_set_id);
1892 vl_bitstream_exp_golomb_ue(&enc, sps->chroma_format_idc);
1893
1894 vl_bitstream_exp_golomb_ue(&enc, sps->pic_width_in_luma_samples);
1895 vl_bitstream_exp_golomb_ue(&enc, sps->pic_height_in_luma_samples);
1896
1897 vl_bitstream_put_bits(&enc, 1, sps->flags.conformance_window_flag);
1898
1899 if (sps->flags.conformance_window_flag) {
1900 vl_bitstream_exp_golomb_ue(&enc, sps->conf_win_left_offset);
1901 vl_bitstream_exp_golomb_ue(&enc, sps->conf_win_right_offset);
1902 vl_bitstream_exp_golomb_ue(&enc, sps->conf_win_top_offset);
1903 vl_bitstream_exp_golomb_ue(&enc, sps->conf_win_bottom_offset);
1904 }
1905
1906 vl_bitstream_exp_golomb_ue(&enc, sps->bit_depth_luma_minus8);
1907 vl_bitstream_exp_golomb_ue(&enc, sps->bit_depth_chroma_minus8);
1908
1909 vl_bitstream_exp_golomb_ue(&enc, sps->log2_max_pic_order_cnt_lsb_minus4);
1910 vl_bitstream_put_bits(&enc, 1, sps->flags.sps_sub_layer_ordering_info_present_flag);
1911
1912 int i = sps->flags.sps_sub_layer_ordering_info_present_flag ? 0 : sps->sps_max_sub_layers_minus1;
1913 for (; i <= sps->sps_max_sub_layers_minus1; i++) {
1914 vl_bitstream_exp_golomb_ue(&enc, sps->pDecPicBufMgr->max_dec_pic_buffering_minus1[i]);
1915 vl_bitstream_exp_golomb_ue(&enc, sps->pDecPicBufMgr->max_num_reorder_pics[i]);
1916 vl_bitstream_exp_golomb_ue(&enc, sps->pDecPicBufMgr->max_latency_increase_plus1[i]);
1917 }
1918
1919 vl_bitstream_exp_golomb_ue(&enc, sps->log2_min_luma_coding_block_size_minus3);
1920 vl_bitstream_exp_golomb_ue(&enc, sps->log2_diff_max_min_luma_coding_block_size);
1921 vl_bitstream_exp_golomb_ue(&enc, sps->log2_min_luma_transform_block_size_minus2);
1922 vl_bitstream_exp_golomb_ue(&enc, sps->log2_diff_max_min_luma_transform_block_size);
1923
1924 vl_bitstream_exp_golomb_ue(&enc, sps->max_transform_hierarchy_depth_inter);
1925 vl_bitstream_exp_golomb_ue(&enc, sps->max_transform_hierarchy_depth_intra);
1926
1927 vl_bitstream_put_bits(&enc, 1, sps->flags.scaling_list_enabled_flag);
1928
1929 vl_bitstream_put_bits(&enc, 1, sps->flags.amp_enabled_flag);
1930 vl_bitstream_put_bits(&enc, 1, sps->flags.sample_adaptive_offset_enabled_flag);
1931
1932 vl_bitstream_put_bits(&enc, 1, sps->flags.pcm_enabled_flag);
1933
1934 if (sps->flags.pcm_enabled_flag) {
1935 vl_bitstream_put_bits(&enc, 4, sps->bit_depth_luma_minus8 + 7);
1936 vl_bitstream_put_bits(&enc, 4, sps->bit_depth_chroma_minus8 + 7);
1937 vl_bitstream_exp_golomb_ue(&enc, sps->log2_min_luma_coding_block_size_minus3);
1938 vl_bitstream_exp_golomb_ue(&enc, sps->log2_diff_max_min_luma_coding_block_size);
1939 vl_bitstream_put_bits(&enc, 1, sps->flags.pcm_loop_filter_disabled_flag);
1940 }
1941
1942 vl_bitstream_exp_golomb_ue(&enc, sps->num_short_term_ref_pic_sets);
1943 for (int i = 0; i < sps->num_short_term_ref_pic_sets; i++)
1944 encode_rps(&enc, sps, i);
1945
1946 vl_bitstream_put_bits(&enc, 1, sps->flags.long_term_ref_pics_present_flag);
1947 if (sps->flags.long_term_ref_pics_present_flag) {
1948 vl_bitstream_exp_golomb_ue(&enc, sps->num_long_term_ref_pics_sps);
1949 for (int i = 0; i < sps->num_long_term_ref_pics_sps; i++) {
1950 vl_bitstream_put_bits(&enc, sps->log2_max_pic_order_cnt_lsb_minus4 + 4, sps->pLongTermRefPicsSps->lt_ref_pic_poc_lsb_sps[i]);
1951 vl_bitstream_put_bits(&enc, 1, sps->pLongTermRefPicsSps->used_by_curr_pic_lt_sps_flag);
1952 }
1953 }
1954
1955 vl_bitstream_put_bits(&enc, 1, sps->flags.sps_temporal_mvp_enabled_flag);
1956 vl_bitstream_put_bits(&enc, 1, sps->flags.strong_intra_smoothing_enabled_flag);
1957 vl_bitstream_put_bits(&enc, 1, sps->flags.vui_parameters_present_flag);
1958
1959 if (sps->flags.vui_parameters_present_flag) {
1960 const StdVideoH265SequenceParameterSetVui *vui = sps->pSequenceParameterSetVui;
1961 vl_bitstream_put_bits(&enc, 1, vui->flags.aspect_ratio_info_present_flag);
1962 if (vui->flags.aspect_ratio_info_present_flag) {
1963 vl_bitstream_put_bits(&enc, 8, vui->aspect_ratio_idc);
1964 if (vui->aspect_ratio_idc == STD_VIDEO_H265_ASPECT_RATIO_IDC_EXTENDED_SAR) {
1965 vl_bitstream_put_bits(&enc, 16, vui->sar_width);
1966 vl_bitstream_put_bits(&enc, 16, vui->sar_height);
1967 }
1968 }
1969 vl_bitstream_put_bits(&enc, 1, vui->flags.overscan_info_present_flag);
1970 if (vui->flags.overscan_info_present_flag)
1971 vl_bitstream_put_bits(&enc, 1, vui->flags.overscan_appropriate_flag);
1972 vl_bitstream_put_bits(&enc, 1, vui->flags.video_signal_type_present_flag);
1973 if (vui->flags.video_signal_type_present_flag) {
1974 vl_bitstream_put_bits(&enc, 3, vui->video_format);
1975 vl_bitstream_put_bits(&enc, 1, vui->flags.video_full_range_flag);
1976 vl_bitstream_put_bits(&enc, 1, vui->flags.colour_description_present_flag);
1977 if (vui->flags.colour_description_present_flag) {
1978 vl_bitstream_put_bits(&enc, 8, vui->colour_primaries);
1979 vl_bitstream_put_bits(&enc, 8, vui->transfer_characteristics);
1980 vl_bitstream_put_bits(&enc, 8, vui->matrix_coeffs);
1981 }
1982 }
1983 vl_bitstream_put_bits(&enc, 1, vui->flags.chroma_loc_info_present_flag);
1984 if (vui->flags.chroma_loc_info_present_flag) {
1985 vl_bitstream_exp_golomb_ue(&enc, vui->chroma_sample_loc_type_top_field);
1986 vl_bitstream_exp_golomb_ue(&enc, vui->chroma_sample_loc_type_bottom_field);
1987 }
1988 vl_bitstream_put_bits(&enc, 1, vui->flags.neutral_chroma_indication_flag);
1989 vl_bitstream_put_bits(&enc, 1, vui->flags.field_seq_flag);
1990 vl_bitstream_put_bits(&enc, 1, vui->flags.frame_field_info_present_flag);
1991 vl_bitstream_put_bits(&enc, 1, vui->flags.default_display_window_flag);
1992 if (vui->flags.default_display_window_flag) {
1993 vl_bitstream_exp_golomb_ue(&enc, vui->def_disp_win_left_offset);
1994 vl_bitstream_exp_golomb_ue(&enc, vui->def_disp_win_right_offset);
1995 vl_bitstream_exp_golomb_ue(&enc, vui->def_disp_win_top_offset);
1996 vl_bitstream_exp_golomb_ue(&enc, vui->def_disp_win_bottom_offset);
1997 }
1998 vl_bitstream_put_bits(&enc, 1, vui->flags.vui_timing_info_present_flag);
1999 if (vui->flags.vui_timing_info_present_flag) {
2000 vl_bitstream_put_bits(&enc, 32, vui->vui_num_units_in_tick);
2001 vl_bitstream_put_bits(&enc, 32, vui->vui_time_scale);
2002 vl_bitstream_put_bits(&enc, 1, vui->flags.vui_poc_proportional_to_timing_flag);
2003 if (vui->flags.vui_poc_proportional_to_timing_flag)
2004 vl_bitstream_exp_golomb_ue(&enc, vui->vui_num_ticks_poc_diff_one_minus1);
2005 vl_bitstream_put_bits(&enc, 1, 0);//vui->flags.vui_hrd_parameters_present_flag);
2006 // HRD
2007 }
2008
2009 vl_bitstream_put_bits(&enc, 1, vui->flags.bitstream_restriction_flag);
2010 if (vui->flags.bitstream_restriction_flag) {
2011 vl_bitstream_put_bits(&enc, 1, vui->flags.tiles_fixed_structure_flag);
2012 vl_bitstream_put_bits(&enc, 1, vui->flags.motion_vectors_over_pic_boundaries_flag);
2013 vl_bitstream_put_bits(&enc, 1, vui->flags.restricted_ref_pic_lists_flag);
2014 vl_bitstream_exp_golomb_ue(&enc, vui->min_spatial_segmentation_idc);
2015 vl_bitstream_exp_golomb_ue(&enc, vui->max_bytes_per_pic_denom);
2016 vl_bitstream_exp_golomb_ue(&enc, vui->max_bits_per_min_cu_denom);
2017 vl_bitstream_exp_golomb_ue(&enc, vui->log2_max_mv_length_horizontal);
2018 vl_bitstream_exp_golomb_ue(&enc, vui->log2_max_mv_length_vertical);
2019 }
2020 }
2021
2022 vl_bitstream_put_bits(&enc, 1, 0); /* sps extension flg */
2023 vl_bitstream_rbsp_trailing(&enc);
2024
2025 vl_bitstream_flush(&enc);
2026 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
2027 vl_bitstream_encoder_free(&enc);
2028 }
2029
2030 void
vk_video_encode_h265_pps(const StdVideoH265PictureParameterSet * pps,size_t size_limit,size_t * data_size_ptr,void * data_ptr)2031 vk_video_encode_h265_pps(const StdVideoH265PictureParameterSet *pps,
2032 size_t size_limit,
2033 size_t *data_size_ptr,
2034 void *data_ptr)
2035 {
2036 struct vl_bitstream_encoder enc;
2037 uint32_t data_size = *data_size_ptr;
2038
2039 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, size_limit);
2040
2041 emit_nalu_h265_header(&enc, HEVC_NAL_PPS_NUT, 0);
2042 vl_bitstream_exp_golomb_ue(&enc, pps->pps_pic_parameter_set_id);
2043 vl_bitstream_exp_golomb_ue(&enc, pps->pps_seq_parameter_set_id);
2044
2045 vl_bitstream_put_bits(&enc, 1, pps->flags.dependent_slice_segments_enabled_flag);
2046
2047 vl_bitstream_put_bits(&enc, 1, pps->flags.output_flag_present_flag);
2048 vl_bitstream_put_bits(&enc, 3, pps->num_extra_slice_header_bits);
2049
2050 vl_bitstream_put_bits(&enc, 1, pps->flags.sign_data_hiding_enabled_flag);
2051 vl_bitstream_put_bits(&enc, 1, pps->flags.cabac_init_present_flag);
2052
2053 vl_bitstream_exp_golomb_ue(&enc, pps->num_ref_idx_l0_default_active_minus1);
2054 vl_bitstream_exp_golomb_ue(&enc, pps->num_ref_idx_l1_default_active_minus1);
2055
2056 vl_bitstream_exp_golomb_se(&enc, pps->init_qp_minus26);
2057
2058 vl_bitstream_put_bits(&enc, 1, pps->flags.constrained_intra_pred_flag);
2059 vl_bitstream_put_bits(&enc, 1, pps->flags.transform_skip_enabled_flag);
2060 vl_bitstream_put_bits(&enc, 1, pps->flags.cu_qp_delta_enabled_flag);
2061
2062 if (pps->flags.cu_qp_delta_enabled_flag)
2063 vl_bitstream_exp_golomb_ue(&enc, pps->diff_cu_qp_delta_depth);
2064
2065 vl_bitstream_exp_golomb_se(&enc, pps->pps_cb_qp_offset);
2066 vl_bitstream_exp_golomb_se(&enc, pps->pps_cr_qp_offset);
2067
2068 vl_bitstream_put_bits(&enc, 1, pps->flags.pps_slice_chroma_qp_offsets_present_flag);
2069 vl_bitstream_put_bits(&enc, 1, pps->flags.weighted_pred_flag);
2070 vl_bitstream_put_bits(&enc, 1, pps->flags.weighted_bipred_flag);
2071 vl_bitstream_put_bits(&enc, 1, pps->flags.transquant_bypass_enabled_flag);
2072
2073 vl_bitstream_put_bits(&enc, 1, pps->flags.tiles_enabled_flag);
2074 vl_bitstream_put_bits(&enc, 1, pps->flags.entropy_coding_sync_enabled_flag);
2075
2076 assert (!pps->flags.tiles_enabled_flag);
2077
2078 vl_bitstream_put_bits(&enc, 1, pps->flags.pps_loop_filter_across_slices_enabled_flag);
2079 vl_bitstream_put_bits(&enc, 1, pps->flags.deblocking_filter_control_present_flag);
2080
2081 if (pps->flags.deblocking_filter_control_present_flag) {
2082 vl_bitstream_put_bits(&enc, 1, pps->flags.deblocking_filter_override_enabled_flag);
2083 vl_bitstream_put_bits(&enc, 1, pps->flags.pps_deblocking_filter_disabled_flag);
2084 if (!pps->flags.pps_deblocking_filter_disabled_flag) {
2085 vl_bitstream_exp_golomb_se(&enc, pps->pps_beta_offset_div2);
2086 vl_bitstream_exp_golomb_se(&enc, pps->pps_tc_offset_div2);
2087 }
2088 }
2089
2090 vl_bitstream_put_bits(&enc, 1, pps->flags.pps_scaling_list_data_present_flag);
2091 assert (!pps->flags.pps_scaling_list_data_present_flag);
2092
2093 vl_bitstream_put_bits(&enc, 1, pps->flags.lists_modification_present_flag);
2094 vl_bitstream_exp_golomb_ue(&enc, pps->log2_parallel_merge_level_minus2);
2095 vl_bitstream_put_bits(&enc, 1, pps->flags.slice_segment_header_extension_present_flag);
2096
2097 vl_bitstream_put_bits(&enc, 1, 0); /* pps extension flag */
2098 vl_bitstream_rbsp_trailing(&enc);
2099
2100 vl_bitstream_flush(&enc);
2101 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
2102 vl_bitstream_encoder_free(&enc);
2103 }
2104
2105 void
vk_video_encode_h264_slice_header(const StdVideoEncodeH264PictureInfo * pic_info,const StdVideoH264SequenceParameterSet * sps,const StdVideoH264PictureParameterSet * pps,const StdVideoEncodeH264SliceHeader * slice_header,const int8_t slice_qp_delta,size_t * data_size_ptr,void * data_ptr)2106 vk_video_encode_h264_slice_header(const StdVideoEncodeH264PictureInfo *pic_info,
2107 const StdVideoH264SequenceParameterSet *sps,
2108 const StdVideoH264PictureParameterSet *pps,
2109 const StdVideoEncodeH264SliceHeader *slice_header,
2110 const int8_t slice_qp_delta,
2111 size_t *data_size_ptr,
2112 void *data_ptr)
2113 {
2114 struct vl_bitstream_encoder enc;
2115 uint32_t data_size = *data_size_ptr;
2116
2117 int is_idr = !!pic_info->flags.IdrPicFlag;
2118 int is_ref = !!pic_info->flags.is_reference;
2119 uint32_t slice_type = slice_header->slice_type % 5;
2120
2121 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, VL_BITSTREAM_MAX_BUFFER);
2122
2123 if (slice_type == STD_VIDEO_H264_SLICE_TYPE_I) {
2124 emit_nalu_header(&enc, 3, is_idr ? H264_NAL_IDR : H264_NAL_SLICE);
2125 } else if (slice_type == STD_VIDEO_H264_SLICE_TYPE_P) {
2126 assert(!is_idr);
2127 emit_nalu_header(&enc, 2, H264_NAL_SLICE);
2128 } else {
2129 assert(slice_type == STD_VIDEO_H264_SLICE_TYPE_B);
2130 assert(!is_idr);
2131 emit_nalu_header(&enc, is_ref ? 1 : 0, H264_NAL_SLICE);
2132 }
2133
2134 vl_bitstream_put_bits(&enc, 1, slice_header->first_mb_in_slice);
2135 vl_bitstream_exp_golomb_ue(&enc, slice_header->slice_type);
2136 vl_bitstream_exp_golomb_ue(&enc, pic_info->pic_parameter_set_id);
2137
2138 if (sps->flags.separate_colour_plane_flag)
2139 /* colour plane id */
2140 vl_bitstream_put_bits(&enc, 2, 0);
2141
2142 vl_bitstream_put_bits(&enc, sps->log2_max_frame_num_minus4 + 4, pic_info->frame_num);
2143
2144 /* frame_mbs_only_flag == 1 */
2145 if (!sps->flags.frame_mbs_only_flag) {
2146 /* FIXME: */
2147 assert(0);
2148 }
2149
2150 if (pic_info->flags.IdrPicFlag)
2151 vl_bitstream_exp_golomb_ue(&enc, pic_info->idr_pic_id);
2152
2153 if (sps->pic_order_cnt_type == STD_VIDEO_H264_POC_TYPE_0) {
2154 vl_bitstream_put_bits(&enc, sps->log2_max_pic_order_cnt_lsb_minus4 + 4, pic_info->PicOrderCnt);
2155 /* pic_order_present_flag == 0 */
2156 if (pps->flags.bottom_field_pic_order_in_frame_present_flag) {
2157 assert(0);
2158 vl_bitstream_exp_golomb_se(&enc, 0);
2159 }
2160 } else if (sps->pic_order_cnt_type == STD_VIDEO_H264_POC_TYPE_1) {
2161 assert(0);
2162
2163 if (!sps->flags.delta_pic_order_always_zero_flag) {
2164 }
2165 } else if (sps->pic_order_cnt_type == STD_VIDEO_H264_POC_TYPE_2) {
2166 } else {
2167 assert(0);
2168 }
2169
2170 /* redundant_pic_cnt_present_flag == 0 */
2171 if (pps->flags.redundant_pic_cnt_present_flag) {
2172 vl_bitstream_exp_golomb_ue(&enc, 0);
2173 }
2174
2175 /* slice type */
2176 if (slice_type == STD_VIDEO_H264_SLICE_TYPE_P) {
2177 vl_bitstream_put_bits(&enc, 1, slice_header->flags.num_ref_idx_active_override_flag);
2178
2179 if (slice_header->flags.num_ref_idx_active_override_flag) {
2180 vl_bitstream_exp_golomb_ue(&enc, pic_info->pRefLists->num_ref_idx_l0_active_minus1);
2181 }
2182
2183 vl_bitstream_put_bits(&enc, 1, pic_info->pRefLists->flags.ref_pic_list_modification_flag_l0);
2184
2185 if (pic_info->pRefLists->flags.ref_pic_list_modification_flag_l0) {
2186 assert(0);
2187 /* TODO */
2188 for (unsigned i = 0; i < pic_info->pRefLists->refList0ModOpCount; i++) {
2189 }
2190 }
2191 } else if (slice_type == STD_VIDEO_H264_SLICE_TYPE_B) {
2192 vl_bitstream_put_bits(&enc, 1, slice_header->flags.direct_spatial_mv_pred_flag);
2193 vl_bitstream_put_bits(&enc, 1, slice_header->flags.num_ref_idx_active_override_flag);
2194
2195 if (slice_header->flags.num_ref_idx_active_override_flag) {
2196 vl_bitstream_exp_golomb_ue(&enc, pic_info->pRefLists->num_ref_idx_l0_active_minus1);
2197 vl_bitstream_exp_golomb_ue(&enc, pic_info->pRefLists->num_ref_idx_l1_active_minus1);
2198 }
2199
2200 vl_bitstream_put_bits(&enc, 1, pic_info->pRefLists->flags.ref_pic_list_modification_flag_l0);
2201 vl_bitstream_put_bits(&enc, 1, pic_info->pRefLists->flags.ref_pic_list_modification_flag_l1);
2202
2203 if (pic_info->pRefLists->flags.ref_pic_list_modification_flag_l0) {
2204 assert(0);
2205 for (unsigned i = 0; i < pic_info->pRefLists->refList0ModOpCount; i++) {
2206 }
2207 }
2208 if (pic_info->pRefLists->flags.ref_pic_list_modification_flag_l1) {
2209 assert(0);
2210 for (unsigned i = 0; i < pic_info->pRefLists->refList1ModOpCount; i++) {
2211 }
2212 }
2213 }
2214
2215 if ((pps->flags.weighted_pred_flag && (slice_type == STD_VIDEO_H264_SLICE_TYPE_P)) ||
2216 ((pps->weighted_bipred_idc == 1) && (slice_type == STD_VIDEO_H264_SLICE_TYPE_B))) {
2217 /* FIXME: fill weight/offset table */
2218 assert(0);
2219 }
2220
2221 /* dec_ref_pic_marking */
2222 /* nal_ref_idc != 0 */
2223 if (slice_type != STD_VIDEO_H264_SLICE_TYPE_B || pic_info->flags.is_reference) {
2224 unsigned char no_output_of_prior_pics_flag = 0;
2225 unsigned char long_term_reference_flag = 0;
2226 unsigned char adaptive_ref_pic_marking_mode_flag = 0;
2227
2228 if (pic_info->flags.IdrPicFlag) {
2229 vl_bitstream_put_bits(&enc, 1, no_output_of_prior_pics_flag);
2230 vl_bitstream_put_bits(&enc, 1, long_term_reference_flag);
2231 } else {
2232 vl_bitstream_put_bits(&enc, 1, adaptive_ref_pic_marking_mode_flag);
2233 }
2234 }
2235
2236 if (pps->flags.entropy_coding_mode_flag && (slice_type != STD_VIDEO_H264_SLICE_TYPE_I))
2237 vl_bitstream_exp_golomb_ue(&enc, slice_header->cabac_init_idc);
2238
2239 vl_bitstream_exp_golomb_se(&enc, slice_qp_delta);
2240
2241 if (pps->flags.deblocking_filter_control_present_flag) {
2242 vl_bitstream_exp_golomb_ue(&enc, slice_header->disable_deblocking_filter_idc);
2243
2244 if (slice_header->disable_deblocking_filter_idc != 1) {
2245 vl_bitstream_exp_golomb_se(&enc, slice_header->slice_alpha_c0_offset_div2);
2246 vl_bitstream_exp_golomb_se(&enc, slice_header->slice_beta_offset_div2);
2247 }
2248 }
2249
2250 if (pps->flags.entropy_coding_mode_flag) {
2251 int left = vl_bitstream_get_num_bits_for_byte_align(&enc);
2252 int val = (1 << left) - 1;
2253
2254 if (left)
2255 vl_bitstream_put_bits(&enc, left, val);
2256
2257 ASSERTED bool is_aligned = vl_bitstream_is_byte_aligned(&enc);
2258 assert(is_aligned);
2259 }
2260
2261 vl_bitstream_flush(&enc);
2262 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
2263 vl_bitstream_encoder_free(&enc);
2264
2265 return;
2266 }
2267
2268 void
vk_video_encode_h265_slice_header(const StdVideoEncodeH265PictureInfo * pic_info,const StdVideoH265VideoParameterSet * vps,const StdVideoH265SequenceParameterSet * sps,const StdVideoH265PictureParameterSet * pps,const StdVideoEncodeH265SliceSegmentHeader * slice_header,const int8_t slice_qp_delta,size_t * data_size_ptr,void * data_ptr)2269 vk_video_encode_h265_slice_header(const StdVideoEncodeH265PictureInfo *pic_info,
2270 const StdVideoH265VideoParameterSet *vps,
2271 const StdVideoH265SequenceParameterSet *sps,
2272 const StdVideoH265PictureParameterSet *pps,
2273 const StdVideoEncodeH265SliceSegmentHeader *slice_header,
2274 const int8_t slice_qp_delta,
2275 size_t *data_size_ptr,
2276 void *data_ptr)
2277 {
2278 struct vl_bitstream_encoder enc;
2279 uint32_t data_size = *data_size_ptr;
2280
2281 vl_bitstream_encoder_clear(&enc, data_ptr, data_size, VL_BITSTREAM_MAX_BUFFER);
2282 emit_nalu_h265_header(&enc, vk_video_get_h265_nal_unit(pic_info), pic_info->TemporalId);
2283
2284 vl_bitstream_put_bits(&enc, 1, slice_header->flags.first_slice_segment_in_pic_flag);
2285 if (pic_info->flags.IrapPicFlag) {
2286 vl_bitstream_put_bits(&enc, 1, pic_info->flags.no_output_of_prior_pics_flag);
2287 }
2288
2289 vl_bitstream_exp_golomb_ue(&enc, pic_info->pps_pic_parameter_set_id);
2290
2291 if (!slice_header->flags.first_slice_segment_in_pic_flag) {
2292 unsigned size, num;
2293 unsigned bits_slice_segment_address = 0;
2294
2295 if (pps->flags.dependent_slice_segments_enabled_flag)
2296 vl_bitstream_put_bits(&enc, 1, slice_header->flags.dependent_slice_segment_flag);
2297
2298 size = 1 << (sps->log2_min_luma_coding_block_size_minus3 + 3 +
2299 sps->log2_diff_max_min_luma_coding_block_size);
2300
2301 num = ((sps->pic_width_in_luma_samples + size - 1) / size) *
2302 ((sps->pic_height_in_luma_samples + size - 1) / size);
2303
2304 while (num > (1 << bits_slice_segment_address))
2305 bits_slice_segment_address++;
2306
2307 vl_bitstream_put_bits(&enc, bits_slice_segment_address, slice_header->slice_segment_address);
2308 }
2309
2310 if (slice_header->flags.dependent_slice_segment_flag)
2311 goto finish;
2312
2313 for (unsigned i = 0; i < pps->num_extra_slice_header_bits; ++i)
2314 /* slice_reserved_flag */
2315 vl_bitstream_put_bits(&enc, 1, 0);
2316
2317 vl_bitstream_exp_golomb_ue(&enc, slice_header->slice_type);
2318
2319 if (pps->flags.output_flag_present_flag)
2320 vl_bitstream_put_bits(&enc, 1, pic_info->flags.pic_output_flag);
2321
2322 if (sps->flags.separate_colour_plane_flag)
2323 /* colour_plane_id */
2324 vl_bitstream_put_bits(&enc, 2, 0);
2325
2326 if (pic_info->pic_type != STD_VIDEO_H265_PICTURE_TYPE_IDR) {
2327 /* slice_pic_order_cnt_lsb */
2328 uint32_t slice_pic_order_cnt_lsb =
2329 pic_info->PicOrderCntVal & ((1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4)) - 1);
2330
2331 vl_bitstream_put_bits(&enc, sps->log2_max_pic_order_cnt_lsb_minus4 + 4, slice_pic_order_cnt_lsb);
2332 vl_bitstream_put_bits(&enc, 1, pic_info->flags.short_term_ref_pic_set_sps_flag);
2333
2334 if (!pic_info->flags.short_term_ref_pic_set_sps_flag) {
2335 const StdVideoH265ShortTermRefPicSet* st_rps = pic_info->pShortTermRefPicSet;
2336 unsigned num_st_rps = sps->num_short_term_ref_pic_sets;
2337 bool rps_predict = false;
2338
2339 if (num_st_rps) {
2340 rps_predict = st_rps->flags.inter_ref_pic_set_prediction_flag;
2341 vl_bitstream_put_bits(&enc, 1, st_rps->flags.inter_ref_pic_set_prediction_flag);
2342 }
2343
2344 if (rps_predict) {
2345 vl_bitstream_exp_golomb_ue(&enc, st_rps->delta_idx_minus1);
2346 vl_bitstream_put_bits(&enc, 1, st_rps->flags.delta_rps_sign);
2347 vl_bitstream_exp_golomb_ue(&enc, st_rps->abs_delta_rps_minus1);
2348
2349 for (unsigned i = 0; i <= st_rps->num_negative_pics + st_rps->num_positive_pics; i++) {
2350 vl_bitstream_put_bits(&enc, 1, st_rps->used_by_curr_pic_flag);
2351 if (!st_rps->used_by_curr_pic_flag) {
2352 vl_bitstream_put_bits(&enc, 1, st_rps->use_delta_flag);
2353 }
2354 }
2355 } else {
2356 vl_bitstream_exp_golomb_ue(&enc, st_rps->num_negative_pics);
2357 vl_bitstream_exp_golomb_ue(&enc, st_rps->num_positive_pics);
2358
2359 for (unsigned i = 0; i < st_rps->num_negative_pics; i++) {
2360 vl_bitstream_exp_golomb_ue(&enc, st_rps->delta_poc_s0_minus1[i]);
2361 vl_bitstream_put_bits(&enc, 1, st_rps->used_by_curr_pic_s0_flag);
2362 }
2363 for (unsigned i = 0; i < st_rps->num_positive_pics; i++) {
2364 vl_bitstream_exp_golomb_ue(&enc, st_rps->delta_poc_s1_minus1[i]);
2365 vl_bitstream_put_bits(&enc, 1, st_rps->used_by_curr_pic_s1_flag);
2366 }
2367 }
2368 } else {
2369 unsigned num_st_rps = sps->num_short_term_ref_pic_sets;
2370
2371 int numbits = util_logbase2_ceil(num_st_rps);
2372 vl_bitstream_put_bits(&enc, numbits, pic_info->short_term_ref_pic_set_idx);
2373 }
2374
2375 if (sps->flags.long_term_ref_pics_present_flag) {
2376 const StdVideoEncodeH265LongTermRefPics* lt_pics = pic_info->pLongTermRefPics;
2377 unsigned num_lt_sps = 0;
2378 unsigned num_lt_pics = lt_pics->num_long_term_pics;
2379
2380 if (sps->num_long_term_ref_pics_sps > 0) {
2381 num_lt_sps = lt_pics->num_long_term_sps;
2382 vl_bitstream_exp_golomb_ue(&enc, num_lt_sps);
2383 }
2384
2385 vl_bitstream_exp_golomb_ue(&enc, num_lt_pics);
2386
2387 unsigned num_refs = num_lt_sps + num_lt_pics;
2388
2389 for (unsigned i = 0; i < num_refs; i++) {
2390 if (i < num_lt_sps) {
2391 if (sps->num_long_term_ref_pics_sps > 1) {
2392 vl_bitstream_put_bits(&enc, util_logbase2_ceil(sps->num_long_term_ref_pics_sps),
2393 lt_pics->lt_idx_sps[i]);
2394 }
2395 } else {
2396 vl_bitstream_put_bits(&enc, sps->log2_max_pic_order_cnt_lsb_minus4 + 4,
2397 lt_pics->poc_lsb_lt[i]),
2398 vl_bitstream_put_bits(&enc, 1, lt_pics->used_by_curr_pic_lt_flag);
2399 }
2400
2401 vl_bitstream_put_bits(&enc, 1, lt_pics->delta_poc_msb_present_flag[i]);
2402 if (lt_pics->delta_poc_msb_present_flag[i]) {
2403 vl_bitstream_exp_golomb_ue(&enc, lt_pics->delta_poc_msb_cycle_lt[i]);
2404 }
2405 }
2406 }
2407
2408 if (sps->flags.sps_temporal_mvp_enabled_flag)
2409 vl_bitstream_put_bits(&enc, 1, pic_info->flags.slice_temporal_mvp_enabled_flag);
2410 }
2411
2412 if (sps->flags.sample_adaptive_offset_enabled_flag) {
2413 vl_bitstream_put_bits(&enc, 1, slice_header->flags.slice_sao_luma_flag);
2414 if (sps->chroma_format_idc)
2415 vl_bitstream_put_bits(&enc, 1, slice_header->flags.slice_sao_chroma_flag);
2416 }
2417
2418 if (slice_header->slice_type != STD_VIDEO_H265_SLICE_TYPE_I) {
2419 unsigned num_ref_idx_l0_active = pps->num_ref_idx_l0_default_active_minus1 + 1;
2420 unsigned num_ref_idx_l1_active = pps->num_ref_idx_l1_default_active_minus1 + 1;
2421
2422 vl_bitstream_put_bits(&enc, 1, slice_header->flags.num_ref_idx_active_override_flag);
2423 if (slice_header->flags.num_ref_idx_active_override_flag) {
2424 vl_bitstream_exp_golomb_ue(&enc, pic_info->pRefLists->num_ref_idx_l0_active_minus1);
2425 num_ref_idx_l0_active = pic_info->pRefLists->num_ref_idx_l0_active_minus1 + 1;
2426
2427 if (slice_header->slice_type == STD_VIDEO_H265_SLICE_TYPE_B) {
2428 vl_bitstream_exp_golomb_ue(&enc, pic_info->pRefLists->num_ref_idx_l1_active_minus1);
2429 num_ref_idx_l1_active = pic_info->pRefLists->num_ref_idx_l1_active_minus1 + 1;
2430 }
2431 }
2432
2433 if (pps->flags.lists_modification_present_flag) {
2434 vl_bitstream_put_bits(&enc, 1, pic_info->pRefLists->flags.ref_pic_list_modification_flag_l0);
2435 if (pic_info->pRefLists->flags.ref_pic_list_modification_flag_l0) {
2436
2437 for (int i = 0; i < num_ref_idx_l0_active; i++) {
2438 vl_bitstream_put_bits(&enc, util_logbase2_ceil(num_ref_idx_l0_active + num_ref_idx_l1_active),
2439 pic_info->pRefLists->list_entry_l0[i]);
2440 }
2441 }
2442
2443 if (slice_header->slice_type == STD_VIDEO_H265_SLICE_TYPE_B) {
2444 vl_bitstream_put_bits(&enc, 1, pic_info->pRefLists->flags.ref_pic_list_modification_flag_l1);
2445
2446 if (pic_info->pRefLists->flags.ref_pic_list_modification_flag_l1) {
2447 for (int i = 0; i < num_ref_idx_l1_active; i++) {
2448 vl_bitstream_put_bits(&enc, util_logbase2_ceil(num_ref_idx_l0_active + num_ref_idx_l1_active),
2449 pic_info->pRefLists->list_entry_l1[i]);
2450 }
2451 }
2452 }
2453 }
2454
2455 if (slice_header->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)
2456 vl_bitstream_put_bits(&enc, 1, slice_header->flags.mvd_l1_zero_flag);
2457
2458 if (pps->flags.cabac_init_present_flag)
2459 /* cabac_init_flag */
2460 vl_bitstream_put_bits(&enc, 1, slice_header->flags.cabac_init_flag);
2461
2462 if (pic_info->flags.slice_temporal_mvp_enabled_flag) {
2463 unsigned collocated_list = 0;
2464 if (slice_header->slice_type == STD_VIDEO_H265_SLICE_TYPE_B) {
2465 collocated_list = 1;
2466 vl_bitstream_put_bits(&enc, 1, collocated_list);
2467 }
2468
2469 if (collocated_list == 0) {
2470 if (num_ref_idx_l0_active > 1)
2471 vl_bitstream_exp_golomb_ue(&enc, slice_header->collocated_ref_idx);
2472 } else if (collocated_list == 1) {
2473 if (num_ref_idx_l1_active > 1)
2474 vl_bitstream_exp_golomb_ue(&enc, slice_header->collocated_ref_idx);
2475 }
2476 }
2477
2478 if ((pps->flags.weighted_pred_flag && slice_header->slice_type == STD_VIDEO_H265_SLICE_TYPE_P) ||
2479 (pps->flags.weighted_bipred_flag && slice_header->slice_type == STD_VIDEO_H265_SLICE_TYPE_B)) {
2480 /* FIXME : h265_pred_weight_table */
2481 assert(0);
2482 }
2483
2484 vl_bitstream_exp_golomb_ue(&enc, 5 - slice_header->MaxNumMergeCand);
2485 }
2486
2487 vl_bitstream_exp_golomb_se(&enc, slice_qp_delta);
2488
2489 if (pps->flags.pps_slice_chroma_qp_offsets_present_flag) {
2490 vl_bitstream_exp_golomb_se(&enc, slice_header->slice_cb_qp_offset);
2491 vl_bitstream_exp_golomb_se(&enc, slice_header->slice_cr_qp_offset);
2492 }
2493
2494 if (pps->flags.chroma_qp_offset_list_enabled_flag)
2495 vl_bitstream_put_bits(&enc, 1, slice_header->flags.cu_chroma_qp_offset_enabled_flag);
2496
2497 if (pps->flags.deblocking_filter_control_present_flag) {
2498 if (pps->flags.deblocking_filter_override_enabled_flag) {
2499 vl_bitstream_put_bits(&enc, 1, slice_header->flags.deblocking_filter_override_flag);
2500
2501 if (slice_header->flags.deblocking_filter_override_flag) {
2502 vl_bitstream_put_bits(&enc, 1, slice_header->flags.slice_deblocking_filter_disabled_flag);
2503
2504 if (!slice_header->flags.slice_deblocking_filter_disabled_flag) {
2505 vl_bitstream_exp_golomb_se(&enc, slice_header->slice_beta_offset_div2);
2506 vl_bitstream_exp_golomb_se(&enc, slice_header->slice_tc_offset_div2);
2507 }
2508 }
2509 }
2510 }
2511
2512 if (pps->flags.pps_loop_filter_across_slices_enabled_flag &&
2513 (slice_header->flags.slice_sao_luma_flag || slice_header->flags.slice_sao_chroma_flag ||
2514 !slice_header->flags.slice_deblocking_filter_disabled_flag))
2515 vl_bitstream_put_bits(&enc, 1, slice_header->flags.slice_loop_filter_across_slices_enabled_flag);
2516
2517 if (pps->flags.tiles_enabled_flag || pps->flags.entropy_coding_sync_enabled_flag) {
2518 assert(0);
2519 }
2520
2521 if (pps->flags.pps_extension_present_flag) {
2522 assert(0);
2523 }
2524
2525 finish:
2526 vl_bitstream_rbsp_trailing(&enc);
2527 vl_bitstream_flush(&enc);
2528 *data_size_ptr += vl_bitstream_get_byte_count(&enc);
2529 vl_bitstream_encoder_free(&enc);
2530
2531 return;
2532 }
2533