FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/vvc.c
Date: 2024-05-03 15:42:48
Exec Total Coverage
Lines: 0 446 0.0%
Functions: 0 14 0.0%
Branches: 0 244 0.0%

Line Branch Exec Source
1 /*
2 * H.266/VVC helper functions for muxers
3 *
4 * Copyright (C) 2022, Thomas Siedel
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "libavcodec/get_bits.h"
24 #include "libavcodec/golomb.h"
25 #include "libavcodec/vvc.h"
26 #include "libavutil/intreadwrite.h"
27 #include "libavutil/mem.h"
28 #include "avc.h"
29 #include "avio.h"
30 #include "avio_internal.h"
31 #include "vvc.h"
32
33 typedef struct VVCCNALUnitArray {
34 uint8_t array_completeness;
35 uint8_t NAL_unit_type;
36 uint16_t num_nalus;
37 uint16_t *nal_unit_length;
38 uint8_t **nal_unit;
39 } VVCCNALUnitArray;
40
41 typedef struct VVCPTLRecord {
42 uint8_t num_bytes_constraint_info;
43 uint8_t general_profile_idc;
44 uint8_t general_tier_flag;
45 uint8_t general_level_idc;
46 uint8_t ptl_frame_only_constraint_flag;
47 uint8_t ptl_multilayer_enabled_flag;
48 uint8_t general_constraint_info[9];
49 uint8_t ptl_sublayer_level_present_flag[VVC_MAX_SUBLAYERS - 1];
50 uint8_t sublayer_level_idc[VVC_MAX_SUBLAYERS - 1];
51 uint8_t ptl_num_sub_profiles;
52 uint32_t general_sub_profile_idc[VVC_MAX_SUB_PROFILES];
53 } VVCPTLRecord;
54
55 typedef struct VVCDecoderConfigurationRecord {
56 uint8_t lengthSizeMinusOne;
57 uint8_t ptl_present_flag;
58 uint16_t ols_idx;
59 uint8_t num_sublayers;
60 uint8_t constant_frame_rate;
61 uint8_t chroma_format_idc;
62 uint8_t bit_depth_minus8;
63 VVCPTLRecord ptl;
64 uint16_t max_picture_width;
65 uint16_t max_picture_height;
66 uint16_t avg_frame_rate;
67 uint8_t num_of_arrays;
68 VVCCNALUnitArray *array;
69 } VVCDecoderConfigurationRecord;
70
71 typedef struct VVCCProfileTierLevel {
72 uint8_t profile_idc;
73 uint8_t tier_flag;
74 uint8_t general_level_idc;
75 uint8_t ptl_frame_only_constraint_flag;
76 uint8_t ptl_multilayer_enabled_flag;
77 // general_constraint_info
78 uint8_t gci_present_flag;
79 uint8_t gci_general_constraints[9];
80 uint8_t gci_num_reserved_bits;
81 // end general_constraint_info
82 uint8_t ptl_sublayer_level_present_flag[VVC_MAX_SUBLAYERS - 1];
83 uint8_t sublayer_level_idc[VVC_MAX_SUBLAYERS - 1];
84 uint8_t ptl_num_sub_profiles;
85 uint32_t general_sub_profile_idc[VVC_MAX_SUB_PROFILES];
86 } VVCCProfileTierLevel;
87
88 static void vvcc_update_ptl(VVCDecoderConfigurationRecord *vvcc,
89 VVCCProfileTierLevel *ptl)
90 {
91 /*
92 * The level indication general_level_idc must indicate a level of
93 * capability equal to or greater than the highest level indicated for the
94 * highest tier in all the parameter sets.
95 */
96 if (vvcc->ptl.general_tier_flag < ptl->tier_flag)
97 vvcc->ptl.general_level_idc = ptl->general_level_idc;
98 else
99 vvcc->ptl.general_level_idc =
100 FFMAX(vvcc->ptl.general_level_idc, ptl->general_level_idc);
101
102 /*
103 * The tier indication general_tier_flag must indicate a tier equal to or
104 * greater than the highest tier indicated in all the parameter sets.
105 */
106 vvcc->ptl.general_tier_flag =
107 FFMAX(vvcc->ptl.general_tier_flag, ptl->tier_flag);
108
109 /*
110 * The profile indication general_profile_idc must indicate a profile to
111 * which the stream associated with this configuration record conforms.
112 *
113 * If the sequence parameter sets are marked with different profiles, then
114 * the stream may need examination to determine which profile, if any, the
115 * entire stream conforms to. If the entire stream is not examined, or the
116 * examination reveals that there is no profile to which the entire stream
117 * conforms, then the entire stream must be split into two or more
118 * sub-streams with separate configuration records in which these rules can
119 * be met.
120 *
121 * Note: set the profile to the highest value for the sake of simplicity.
122 */
123 vvcc->ptl.general_profile_idc =
124 FFMAX(vvcc->ptl.general_profile_idc, ptl->profile_idc);
125
126 /*
127 * Each bit in flags may only be set if all
128 * the parameter sets set that bit.
129 */
130 vvcc->ptl.ptl_frame_only_constraint_flag &=
131 ptl->ptl_frame_only_constraint_flag;
132 vvcc->ptl.ptl_multilayer_enabled_flag &= ptl->ptl_multilayer_enabled_flag;
133
134 /*
135 * Constraints Info
136 */
137 if (ptl->gci_present_flag) {
138 vvcc->ptl.num_bytes_constraint_info = 9;
139 memcpy(&vvcc->ptl.general_constraint_info[0],
140 &ptl->gci_general_constraints[0], sizeof(uint8_t) * 9);
141
142 } else {
143 vvcc->ptl.num_bytes_constraint_info = 1;
144 memset(&vvcc->ptl.general_constraint_info[0], 0, sizeof(uint8_t) * 9);
145 }
146
147 /*
148 * Each bit in flags may only be set if one of
149 * the parameter sets set that bit.
150 */
151 memset(vvcc->ptl.ptl_sublayer_level_present_flag, 0,
152 sizeof(uint8_t) * vvcc->num_sublayers - 1);
153 memset(vvcc->ptl.sublayer_level_idc, 0,
154 sizeof(uint8_t) * vvcc->num_sublayers - 1);
155
156 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
157 vvcc->ptl.ptl_sublayer_level_present_flag[i] |=
158 ptl->ptl_sublayer_level_present_flag[i];
159 if (vvcc->ptl.ptl_sublayer_level_present_flag[i]) {
160 vvcc->ptl.sublayer_level_idc[i] =
161 FFMAX(vvcc->ptl.sublayer_level_idc[i],
162 ptl->sublayer_level_idc[i]);
163 } else {
164 if (i == vvcc->num_sublayers - 1) {
165 vvcc->ptl.sublayer_level_idc[i] = vvcc->ptl.general_level_idc;
166 } else {
167 vvcc->ptl.sublayer_level_idc[i] =
168 vvcc->ptl.sublayer_level_idc[i + 1];
169 }
170 }
171 }
172
173 vvcc->ptl.ptl_num_sub_profiles =
174 FFMAX(vvcc->ptl.ptl_num_sub_profiles, ptl->ptl_num_sub_profiles);
175 if (vvcc->ptl.ptl_num_sub_profiles) {
176 for (int i = 0; i < vvcc->ptl.ptl_num_sub_profiles; i++) {
177 vvcc->ptl.general_sub_profile_idc[i] =
178 ptl->general_sub_profile_idc[i];
179 }
180 }
181 }
182
183 static void vvcc_parse_ptl(GetBitContext *gb,
184 VVCDecoderConfigurationRecord *vvcc,
185 unsigned int profileTierPresentFlag,
186 unsigned int max_sub_layers_minus1)
187 {
188 VVCCProfileTierLevel general_ptl;
189 int j;
190
191 if (profileTierPresentFlag) {
192 general_ptl.profile_idc = get_bits(gb, 7);
193 general_ptl.tier_flag = get_bits1(gb);
194 }
195 general_ptl.general_level_idc = get_bits(gb, 8);
196
197 general_ptl.ptl_frame_only_constraint_flag = get_bits1(gb);
198 general_ptl.ptl_multilayer_enabled_flag = get_bits1(gb);
199 if (profileTierPresentFlag) { // parse constraint info
200 general_ptl.gci_present_flag = get_bits1(gb);
201 if (general_ptl.gci_present_flag) {
202 for (j = 0; j < 8; j++)
203 general_ptl.gci_general_constraints[j] = get_bits(gb, 8);
204 general_ptl.gci_general_constraints[8] = get_bits(gb, 7);
205
206 general_ptl.gci_num_reserved_bits = get_bits(gb, 8);
207 skip_bits(gb, general_ptl.gci_num_reserved_bits);
208 }
209 while (gb->index % 8 != 0)
210 skip_bits1(gb);
211 }
212
213 for (int i = max_sub_layers_minus1 - 1; i >= 0; i--)
214 general_ptl.ptl_sublayer_level_present_flag[i] = get_bits1(gb);
215
216 while (gb->index % 8 != 0)
217 skip_bits1(gb);
218
219 for (int i = max_sub_layers_minus1 - 1; i >= 0; i--) {
220 if (general_ptl.ptl_sublayer_level_present_flag[i])
221 general_ptl.sublayer_level_idc[i] = get_bits(gb, 8);
222 }
223
224 if (profileTierPresentFlag) {
225 general_ptl.ptl_num_sub_profiles = get_bits(gb, 8);
226 if (general_ptl.ptl_num_sub_profiles) {
227 for (int i = 0; i < general_ptl.ptl_num_sub_profiles; i++)
228 general_ptl.general_sub_profile_idc[i] = get_bits_long(gb, 32);
229 }
230 }
231
232 vvcc_update_ptl(vvcc, &general_ptl);
233 }
234
235 static int vvcc_parse_vps(GetBitContext *gb,
236 VVCDecoderConfigurationRecord *vvcc)
237 {
238 unsigned int vps_max_layers_minus1;
239 unsigned int vps_max_sublayers_minus1;
240 unsigned int vps_default_ptl_dpb_hrd_max_tid_flag;
241 unsigned int vps_all_independent_layers_flag;
242 unsigned int vps_each_layer_is_an_ols_flag;
243 unsigned int vps_ols_mode_idc;
244
245 unsigned int vps_pt_present_flag[VVC_MAX_PTLS];
246 unsigned int vps_ptl_max_tid[VVC_MAX_PTLS];
247 unsigned int vps_num_ptls_minus1 = 0;
248
249 /*
250 * vps_video_parameter_set_id u(4)
251 */
252 skip_bits(gb, 4);
253
254 vps_max_layers_minus1 = get_bits(gb, 6);
255 vps_max_sublayers_minus1 = get_bits(gb, 3);
256
257 /*
258 * numTemporalLayers greater than 1 indicates that the stream to which this
259 * configuration record applies is temporally scalable and the contained
260 * number of temporal layers (also referred to as temporal sub-layer or
261 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
262 * indicates that the stream is not temporally scalable. Value 0 indicates
263 * that it is unknown whether the stream is temporally scalable.
264 */
265 vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
266 vps_max_sublayers_minus1 + 1);
267
268 if (vps_max_layers_minus1 > 0 && vps_max_sublayers_minus1 > 0)
269 vps_default_ptl_dpb_hrd_max_tid_flag = get_bits1(gb);
270 if (vps_max_layers_minus1 > 0)
271 vps_all_independent_layers_flag = get_bits1(gb);
272 else
273 vps_all_independent_layers_flag = 1;
274
275 for (int i = 0; i <= vps_max_layers_minus1; i++) {
276 skip_bits(gb, 6); //vps_layer_id[i]
277 if (i > 0 && !vps_all_independent_layers_flag) {
278 if (get_bits1(gb)) { // vps_independent_layer_flag[i]
279 unsigned int vps_max_tid_ref_present_flag = get_bits1(gb);
280 for (int j = 0; j < i; j++) {
281 if (vps_max_tid_ref_present_flag && get_bits1(gb)) // vps_direct_ref_layer_flag[i][j]
282 skip_bits(gb, 3); // vps_max_tid_il_ref_pics_plus1
283 }
284 }
285 }
286 }
287
288 if (vps_max_layers_minus1 > 0) {
289 if (vps_all_independent_layers_flag)
290 vps_each_layer_is_an_ols_flag = get_bits1(gb);
291 else
292 vps_each_layer_is_an_ols_flag = 0;
293 if (!vps_each_layer_is_an_ols_flag) {
294 if (!vps_all_independent_layers_flag)
295 vps_ols_mode_idc = get_bits(gb, 2);
296 else
297 vps_ols_mode_idc = 2;
298 if (vps_ols_mode_idc == 2) {
299 unsigned int vps_num_output_layer_sets_minus2 = get_bits(gb, 8);
300 for (int i = 1; i <= vps_num_output_layer_sets_minus2 + 1; i++) {
301 for (int j = 0; j <= vps_max_layers_minus1; j++) {
302 skip_bits1(gb); // vps_ols_output_layer_flag[i][j]
303 }
304 }
305 }
306 }
307 vps_num_ptls_minus1 = get_bits(gb, 8);
308 } else {
309 vps_each_layer_is_an_ols_flag = 0;
310 }
311
312 for (int i = 0; i <= vps_num_ptls_minus1; i++) {
313 if (i > 0)
314 vps_pt_present_flag[i] = get_bits1(gb);
315 else
316 vps_pt_present_flag[i] = 1;
317
318 if (!vps_default_ptl_dpb_hrd_max_tid_flag)
319 vps_ptl_max_tid[i] = get_bits(gb, 3);
320 else
321 vps_ptl_max_tid[i] = vps_max_sublayers_minus1;
322 }
323
324 while (gb->index % 8 != 0)
325 skip_bits1(gb);
326
327 for (int i = 0; i <= vps_num_ptls_minus1; i++)
328 vvcc_parse_ptl(gb, vvcc, vps_pt_present_flag[i], vps_ptl_max_tid[i]);
329
330 /* nothing useful for vvcc past this point */
331 return 0;
332 }
333
334 static int vvcc_parse_sps(GetBitContext *gb,
335 VVCDecoderConfigurationRecord *vvcc)
336 {
337 unsigned int sps_max_sublayers_minus1, sps_log2_ctu_size_minus5;
338 unsigned int sps_subpic_same_size_flag, sps_pic_height_max_in_luma_samples,
339 sps_pic_width_max_in_luma_samples;
340 unsigned int sps_independent_subpics_flag;
341
342 skip_bits(gb, 8); // sps_seq_parameter_set_id && sps_video_parameter_set_id
343 sps_max_sublayers_minus1 = get_bits(gb, 3);
344
345 /*
346 * numTemporalLayers greater than 1 indicates that the stream to which this
347 * configuration record applies is temporally scalable and the contained
348 * number of temporal layers (also referred to as temporal sub-layer or
349 * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
350 * indicates that the stream is not temporally scalable. Value 0 indicates
351 * that it is unknown whether the stream is temporally scalable.
352 */
353 vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
354 sps_max_sublayers_minus1 + 1);
355
356 vvcc->chroma_format_idc = get_bits(gb, 2);
357 sps_log2_ctu_size_minus5 = get_bits(gb, 2);
358
359 if (get_bits1(gb)) // sps_ptl_dpb_hrd_params_present_flag
360 vvcc_parse_ptl(gb, vvcc, 1, sps_max_sublayers_minus1);
361
362 skip_bits1(gb); // sps_gdr_enabled_flag
363 if (get_bits(gb, 1)) // sps_ref_pic_resampling_enabled_flag
364 skip_bits1(gb); // sps_res_change_in_clvs_allowed_flag
365
366 sps_pic_width_max_in_luma_samples = get_ue_golomb_long(gb);
367 vvcc->max_picture_width =
368 FFMAX(vvcc->max_picture_width, sps_pic_width_max_in_luma_samples);
369 sps_pic_height_max_in_luma_samples = get_ue_golomb_long(gb);
370 vvcc->max_picture_height =
371 FFMAX(vvcc->max_picture_height, sps_pic_height_max_in_luma_samples);
372
373 if (get_bits1(gb)) {
374 get_ue_golomb_long(gb); // sps_conf_win_left_offset
375 get_ue_golomb_long(gb); // sps_conf_win_right_offset
376 get_ue_golomb_long(gb); // sps_conf_win_top_offset
377 get_ue_golomb_long(gb); // sps_conf_win_bottom_offset
378 }
379
380 if (get_bits1(gb)) { // sps_subpic_info_present_flag
381 const unsigned int sps_num_subpics_minus1 = get_ue_golomb_long(gb);
382 const int ctb_log2_size_y = sps_log2_ctu_size_minus5 + 5;
383 const int ctb_size_y = 1 << ctb_log2_size_y;
384 const int tmp_width_val = AV_CEIL_RSHIFT(sps_pic_width_max_in_luma_samples, ctb_log2_size_y);
385 const int tmp_height_val = AV_CEIL_RSHIFT(sps_pic_height_max_in_luma_samples, ctb_log2_size_y);
386 const int wlen = av_ceil_log2(tmp_width_val);
387 const int hlen = av_ceil_log2(tmp_height_val);
388 if (sps_num_subpics_minus1 > 0) { // sps_num_subpics_minus1
389 sps_independent_subpics_flag = get_bits1(gb);
390 sps_subpic_same_size_flag = get_bits1(gb);
391 }
392 for (int i = 0; sps_num_subpics_minus1 > 0 && i <= sps_num_subpics_minus1; i++) {
393 if (!sps_subpic_same_size_flag || i == 0) {
394 if (i > 0 && sps_pic_width_max_in_luma_samples > ctb_size_y)
395 skip_bits(gb, wlen);
396 if (i > 0 && sps_pic_height_max_in_luma_samples > ctb_size_y)
397 skip_bits(gb, hlen);
398 if (i < sps_num_subpics_minus1 && sps_pic_width_max_in_luma_samples > ctb_size_y)
399 skip_bits(gb, wlen);
400 if (i < sps_num_subpics_minus1 && sps_pic_height_max_in_luma_samples > ctb_size_y)
401 skip_bits(gb, hlen);
402 }
403 if (!sps_independent_subpics_flag) {
404 skip_bits(gb, 2); // sps_subpic_treated_as_pic_flag && sps_loop_filter_across_subpic_enabled_flag
405 }
406 }
407 get_ue_golomb_long(gb); // sps_subpic_id_len_minus1
408 if (get_bits1(gb)) { // sps_subpic_id_mapping_explicitly_signalled_flag
409 if (get_bits1(gb)) // sps_subpic_id_mapping_present_flag
410 for (int i = 0; i <= sps_num_subpics_minus1; i++) {
411 skip_bits1(gb); // sps_subpic_id[i]
412 }
413 }
414 }
415 vvcc->bit_depth_minus8 = get_ue_golomb_long(gb);
416
417 /* nothing useful for vvcc past this point */
418 return 0;
419 }
420
421 static int vvcc_parse_pps(GetBitContext *gb,
422 VVCDecoderConfigurationRecord *vvcc)
423 {
424
425 // Nothing of importance to parse in PPS
426 /* nothing useful for vvcc past this point */
427 return 0;
428 }
429
430 static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
431 {
432 /*
433 * forbidden_zero_bit u(1)
434 * nuh_reserved_zero_bit u(1)
435 * nuh_layer_id u(6)
436 */
437 skip_bits(gb, 8);
438 *nal_type = get_bits(gb, 5);
439
440 /*
441 * nuh_temporal_id_plus1 u(3)
442 */
443 skip_bits(gb, 3);
444 }
445
446 static int vvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
447 uint8_t nal_type, int ps_array_completeness,
448 VVCDecoderConfigurationRecord *vvcc)
449 {
450 int ret;
451 uint8_t index;
452 uint16_t num_nalus;
453 VVCCNALUnitArray *array;
454
455 for (index = 0; index < vvcc->num_of_arrays; index++)
456 if (vvcc->array[index].NAL_unit_type == nal_type)
457 break;
458
459 if (index >= vvcc->num_of_arrays) {
460 uint8_t i;
461
462 ret =
463 av_reallocp_array(&vvcc->array, index + 1,
464 sizeof(VVCCNALUnitArray));
465 if (ret < 0)
466 return ret;
467
468 for (i = vvcc->num_of_arrays; i <= index; i++)
469 memset(&vvcc->array[i], 0, sizeof(VVCCNALUnitArray));
470 vvcc->num_of_arrays = index + 1;
471 }
472
473 array = &vvcc->array[index];
474 num_nalus = array->num_nalus;
475
476 ret = av_reallocp_array(&array->nal_unit, num_nalus + 1, sizeof(uint8_t *));
477 if (ret < 0)
478 return ret;
479
480 ret =
481 av_reallocp_array(&array->nal_unit_length, num_nalus + 1,
482 sizeof(uint16_t));
483 if (ret < 0)
484 return ret;
485
486 array->nal_unit[num_nalus] = nal_buf;
487 array->nal_unit_length[num_nalus] = nal_size;
488 array->NAL_unit_type = nal_type;
489 array->num_nalus++;
490
491 /*
492 * When the sample entry name is 'vvc1', the following applies:
493 * • The value of array_completeness shall be equal to 1 for arrays of SPS,
494 * and PPS NAL units.
495 * • If a VVC bitstream includes DCI NAL unit(s), the value of
496 * array_completeness shall be equal to 1 for the array of DCI units.
497 * Otherwise, NAL_unit_type shall not indicate DCI NAL units.
498 * • If a VVC bitstream includes VPS NAL unit(s), the value of
499 * array_completeness shall be equal to 1 for the array of VPS NAL units.
500 * Otherwise, NAL_unit_type shall not indicate VPS NAL units.
501 * When the value of array_completeness is equal to 1 for an array of a
502 * particular NAL_unit_type value, NAL units of that NAL_unit_type value
503 * cannot be updated without causing a different sample entry to be used.
504 * When the sample entry name is 'vvi1', the value of array_completeness
505 * of at least one of the following arrays shall be equal to 0:
506 • The array of DCI NAL units, if present.
507 • The array of VPS NAL units, if present.
508 • The array of SPS NAL units
509 • The array of PPS NAL units.
510 */
511 if (nal_type == VVC_VPS_NUT || nal_type == VVC_SPS_NUT ||
512 nal_type == VVC_PPS_NUT || nal_type == VVC_DCI_NUT )
513 array->array_completeness = ps_array_completeness;
514
515 return 0;
516 }
517
518 static int vvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
519 int ps_array_completeness,
520 VVCDecoderConfigurationRecord *vvcc)
521 {
522 int ret = 0;
523 GetBitContext gbc;
524 uint8_t nal_type;
525 uint8_t *rbsp_buf;
526 uint32_t rbsp_size;
527
528 rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
529 if (!rbsp_buf) {
530 ret = AVERROR(ENOMEM);
531 goto end;
532 }
533
534 ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
535 if (ret < 0)
536 goto end;
537
538 nal_unit_parse_header(&gbc, &nal_type);
539
540 /*
541 * Note: only 'declarative' SEI messages are allowed in
542 * vvcc. Perhaps the SEI playload type should be checked
543 * and non-declarative SEI messages discarded?
544 */
545 switch (nal_type) {
546 case VVC_OPI_NUT:
547 case VVC_VPS_NUT:
548 case VVC_SPS_NUT:
549 case VVC_PPS_NUT:
550 case VVC_PREFIX_SEI_NUT:
551 case VVC_SUFFIX_SEI_NUT:
552 ret = vvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
553 ps_array_completeness, vvcc);
554 if (ret < 0)
555 goto end;
556 else if (nal_type == VVC_VPS_NUT)
557 ret = vvcc_parse_vps(&gbc, vvcc);
558 else if (nal_type == VVC_SPS_NUT)
559 ret = vvcc_parse_sps(&gbc, vvcc);
560 else if (nal_type == VVC_PPS_NUT)
561 ret = vvcc_parse_pps(&gbc, vvcc);
562 else if (nal_type == VVC_OPI_NUT) {
563 // not yet supported
564 }
565 if (ret < 0)
566 goto end;
567 break;
568 default:
569 ret = AVERROR_INVALIDDATA;
570 goto end;
571 }
572
573 end:
574 av_free(rbsp_buf);
575 return ret;
576 }
577
578 static void vvcc_init(VVCDecoderConfigurationRecord *vvcc)
579 {
580 memset(vvcc, 0, sizeof(VVCDecoderConfigurationRecord));
581 vvcc->lengthSizeMinusOne = 3; // 4 bytes
582
583 vvcc->ptl.num_bytes_constraint_info = 1;
584
585 vvcc->ptl_present_flag = 1;
586 }
587
588 static void vvcc_close(VVCDecoderConfigurationRecord *vvcc)
589 {
590 uint8_t i;
591
592 for (i = 0; i < vvcc->num_of_arrays; i++) {
593 vvcc->array[i].num_nalus = 0;
594 av_freep(&vvcc->array[i].nal_unit);
595 av_freep(&vvcc->array[i].nal_unit_length);
596 }
597
598 vvcc->num_of_arrays = 0;
599 av_freep(&vvcc->array);
600 }
601
602 static int vvcc_write(AVIOContext *pb, VVCDecoderConfigurationRecord *vvcc)
603 {
604 uint8_t i;
605 uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
606 unsigned char *buf = NULL;
607 /*
608 * It's unclear how to properly compute these fields, so
609 * let's always set them to values meaning 'unspecified'.
610 */
611 vvcc->avg_frame_rate = 0;
612 vvcc->constant_frame_rate = 1;
613
614 av_log(NULL, AV_LOG_TRACE,
615 "lengthSizeMinusOne: %" PRIu8 "\n",
616 vvcc->lengthSizeMinusOne);
617 av_log(NULL, AV_LOG_TRACE,
618 "ptl_present_flag: %" PRIu8 "\n",
619 vvcc->ptl_present_flag);
620 av_log(NULL, AV_LOG_TRACE,
621 "ols_idx: %" PRIu16 "\n", vvcc->ols_idx);
622 av_log(NULL, AV_LOG_TRACE,
623 "num_sublayers: %" PRIu8 "\n",
624 vvcc->num_sublayers);
625 av_log(NULL, AV_LOG_TRACE,
626 "constant_frame_rate: %" PRIu8 "\n",
627 vvcc->constant_frame_rate);
628 av_log(NULL, AV_LOG_TRACE,
629 "chroma_format_idc: %" PRIu8 "\n",
630 vvcc->chroma_format_idc);
631
632 av_log(NULL, AV_LOG_TRACE,
633 "bit_depth_minus8: %" PRIu8 "\n",
634 vvcc->bit_depth_minus8);
635 av_log(NULL, AV_LOG_TRACE,
636 "num_bytes_constraint_info: %" PRIu8 "\n",
637 vvcc->ptl.num_bytes_constraint_info);
638 av_log(NULL, AV_LOG_TRACE,
639 "general_profile_idc: %" PRIu8 "\n",
640 vvcc->ptl.general_profile_idc);
641 av_log(NULL, AV_LOG_TRACE,
642 "general_tier_flag: %" PRIu8 "\n",
643 vvcc->ptl.general_tier_flag);
644 av_log(NULL, AV_LOG_TRACE,
645 "general_level_idc: %" PRIu8 "\n",
646 vvcc->ptl.general_level_idc);
647 av_log(NULL, AV_LOG_TRACE,
648 "ptl_frame_only_constraint_flag: %" PRIu8 "\n",
649 vvcc->ptl.ptl_frame_only_constraint_flag);
650 av_log(NULL, AV_LOG_TRACE,
651 "ptl_multilayer_enabled_flag: %" PRIu8 "\n",
652 vvcc->ptl.ptl_multilayer_enabled_flag);
653 for (i = 0; i < vvcc->ptl.num_bytes_constraint_info; i++) {
654 av_log(NULL, AV_LOG_TRACE,
655 "general_constraint_info[%d]: %" PRIu8 "\n", i,
656 vvcc->ptl.general_constraint_info[i]);
657 }
658
659 for (i = 0; i < vvcc->num_sublayers - 1; i++) {
660 av_log(NULL, AV_LOG_TRACE,
661 "ptl_sublayer_level_present_flag[%" PRIu8 "]: %" PRIu8 "\n", i,
662 vvcc->ptl.ptl_sublayer_level_present_flag[i]);
663 av_log(NULL, AV_LOG_TRACE,
664 "sublayer_level_idc[%" PRIu8 "]: %" PRIu8 "\n", i,
665 vvcc->ptl.sublayer_level_idc[i]);
666 }
667
668 av_log(NULL, AV_LOG_TRACE,
669 "num_sub_profiles: %" PRIu8 "\n",
670 vvcc->ptl.ptl_num_sub_profiles);
671
672 for (i = 0; i < vvcc->ptl.ptl_num_sub_profiles; i++) {
673 av_log(NULL, AV_LOG_TRACE,
674 "general_sub_profile_idc[%" PRIu8 "]: %" PRIx32 "\n", i,
675 vvcc->ptl.general_sub_profile_idc[i]);
676 }
677
678 av_log(NULL, AV_LOG_TRACE,
679 "max_picture_width: %" PRIu16 "\n",
680 vvcc->max_picture_width);
681 av_log(NULL, AV_LOG_TRACE,
682 "max_picture_height: %" PRIu16 "\n",
683 vvcc->max_picture_height);
684 av_log(NULL, AV_LOG_TRACE,
685 "avg_frame_rate: %" PRIu16 "\n",
686 vvcc->avg_frame_rate);
687
688 av_log(NULL, AV_LOG_TRACE,
689 "num_of_arrays: %" PRIu8 "\n",
690 vvcc->num_of_arrays);
691 for (i = 0; i < vvcc->num_of_arrays; i++) {
692 av_log(NULL, AV_LOG_TRACE,
693 "array_completeness[%" PRIu8 "]: %" PRIu8 "\n", i,
694 vvcc->array[i].array_completeness);
695 av_log(NULL, AV_LOG_TRACE,
696 "NAL_unit_type[%" PRIu8 "]: %" PRIu8 "\n", i,
697 vvcc->array[i].NAL_unit_type);
698 av_log(NULL, AV_LOG_TRACE,
699 "num_nalus[%" PRIu8 "]: %" PRIu16 "\n", i,
700 vvcc->array[i].num_nalus);
701 for (j = 0; j < vvcc->array[i].num_nalus; j++)
702 av_log(NULL, AV_LOG_TRACE,
703 "nal_unit_length[%" PRIu8 "][%" PRIu16 "]: %"
704 PRIu16 "\n", i, j, vvcc->array[i].nal_unit_length[j]);
705 }
706
707 /*
708 * We need at least one of each: VPS and SPS.
709 */
710 for (i = 0; i < vvcc->num_of_arrays; i++)
711 switch (vvcc->array[i].NAL_unit_type) {
712 case VVC_VPS_NUT:
713 vps_count += vvcc->array[i].num_nalus;
714 break;
715 case VVC_SPS_NUT:
716 sps_count += vvcc->array[i].num_nalus;
717 break;
718 case VVC_PPS_NUT:
719 pps_count += vvcc->array[i].num_nalus;
720 break;
721 default:
722 break;
723 }
724
725 if (vps_count > VVC_MAX_VPS_COUNT)
726 return AVERROR_INVALIDDATA;
727 if (!sps_count || sps_count > VVC_MAX_SPS_COUNT)
728 return AVERROR_INVALIDDATA;
729 if (!pps_count || pps_count > VVC_MAX_PPS_COUNT)
730 return AVERROR_INVALIDDATA;
731
732 /* bit(5) reserved = ‘11111’b;
733 unsigned int (2) LengthSizeMinusOne
734 unsigned int (1) ptl_present_flag */
735 avio_w8(pb, vvcc->lengthSizeMinusOne << 1 | vvcc->ptl_present_flag | 0xf8);
736
737 if (vvcc->ptl_present_flag) {
738 /*
739 * unsigned int(9) ols_idx;
740 * unsigned int(3) num_sublayers;
741 * unsigned int(2) constant_frame_rate;
742 * unsigned int(2) chroma_format_idc; */
743 avio_wb16(pb,
744 vvcc->ols_idx << 7 | vvcc->num_sublayers << 4 | vvcc->
745 constant_frame_rate << 2 | vvcc->chroma_format_idc);
746
747 /* unsigned int(3) bit_depth_minus8;
748 bit(5) reserved = ‘11111’b; */
749 avio_w8(pb, vvcc->bit_depth_minus8 << 5 | 0x1f);
750
751 //VVCPTLRecord
752
753 /* bit(2) reserved = ‘00’b;
754 unsigned int (6) num_bytes_constraint_info */
755 avio_w8(pb, vvcc->ptl.num_bytes_constraint_info & 0x3f);
756
757 /* unsigned int (7) general_profile_idc
758 unsigned int (1) general_tier_flag */
759 avio_w8(pb,
760 vvcc->ptl.general_profile_idc << 1 | vvcc->ptl.general_tier_flag);
761
762 /* unsigned int (8) general_level_idc */
763 avio_w8(pb, vvcc->ptl.general_level_idc);
764
765 /*
766 * unsigned int (1) ptl_frame_only_constraint_flag
767 * unsigned int (1) ptl_multilayer_enabled_flag
768 * unsigned int (8*num_bytes_constraint_info -2) general_constraint_info */
769 buf =
770 (unsigned char *) malloc(sizeof(unsigned char) *
771 vvcc->ptl.num_bytes_constraint_info);
772 *buf = vvcc->ptl.ptl_frame_only_constraint_flag << vvcc->ptl.
773 num_bytes_constraint_info * 8 - 1 | vvcc->ptl.
774 ptl_multilayer_enabled_flag << vvcc->ptl.num_bytes_constraint_info *
775 8 - 2 | *vvcc->ptl.general_constraint_info >> 2;
776 avio_write(pb, buf, vvcc->ptl.num_bytes_constraint_info);
777 free(buf);
778
779 if (vvcc->num_sublayers > 1) {
780 uint8_t ptl_sublayer_level_present_flags = 0;
781 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
782 ptl_sublayer_level_present_flags =
783 (ptl_sublayer_level_present_flags << 1 | vvcc->ptl.
784 ptl_sublayer_level_present_flag[i]);
785 }
786 avio_w8(pb, ptl_sublayer_level_present_flags);
787 }
788
789 for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
790 if (vvcc->ptl.ptl_sublayer_level_present_flag[i])
791 avio_w8(pb, vvcc->ptl.sublayer_level_idc[i]);
792 }
793
794 /* unsigned int(8) num_sub_profiles; */
795 avio_w8(pb, vvcc->ptl.ptl_num_sub_profiles);
796
797 for (int j = 0; j < vvcc->ptl.ptl_num_sub_profiles; j++) {
798 /* unsigned int(32) general_sub_profile_idc[j]; */
799 avio_wb32(pb, vvcc->ptl.general_sub_profile_idc[j]);
800 }
801
802 //End of VvcPTLRecord
803
804 /*
805 * unsigned int(16) max_picture_width;*/
806 avio_wb16(pb, vvcc->max_picture_width);
807
808 /*
809 * unsigned int(16) max_picture_height;*/
810 avio_wb16(pb, vvcc->max_picture_height);
811
812 /*
813 * unsigned int(16) avg_frame_rate; */
814 avio_wb16(pb, vvcc->avg_frame_rate);
815 }
816
817 /* unsigned int(8) num_of_arrays; */
818 avio_w8(pb, vvcc->num_of_arrays);
819
820 for (i = 0; i < vvcc->num_of_arrays; i++) {
821 /*
822 * bit(1) array_completeness;
823 * unsigned int(2) reserved = 0;
824 * unsigned int(5) NAL_unit_type;
825 */
826 avio_w8(pb, vvcc->array[i].array_completeness << 7 |
827 vvcc->array[i].NAL_unit_type & 0x1f);
828 /* unsigned int(16) num_nalus; */
829 if (vvcc->array[i].NAL_unit_type != VVC_DCI_NUT &&
830 vvcc->array[i].NAL_unit_type != VVC_OPI_NUT)
831 avio_wb16(pb, vvcc->array[i].num_nalus);
832 for (j = 0; j < vvcc->array[i].num_nalus; j++) {
833 /* unsigned int(16) nal_unit_length; */
834 avio_wb16(pb, vvcc->array[i].nal_unit_length[j]);
835
836 /* bit(8*nal_unit_length) nal_unit; */
837 avio_write(pb, vvcc->array[i].nal_unit[j],
838 vvcc->array[i].nal_unit_length[j]);
839 }
840 }
841
842 return 0;
843 }
844
845 int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
846 int size, int filter_ps, int *ps_count)
847 {
848 int num_ps = 0, ret = 0;
849 uint8_t *buf, *end, *start = NULL;
850
851 if (!filter_ps) {
852 ret = ff_avc_parse_nal_units(pb, buf_in, size);
853 goto end;
854 }
855
856 ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
857 if (ret < 0)
858 goto end;
859
860 ret = 0;
861 buf = start;
862 end = start + size;
863
864 while (end - buf > 4) {
865 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
866 uint8_t type = (buf[5] >> 3);
867
868 buf += 4;
869
870 switch (type) {
871 case VVC_VPS_NUT:
872 case VVC_SPS_NUT:
873 case VVC_PPS_NUT:
874 num_ps++;
875 break;
876 default:
877 ret += 4 + len;
878 avio_wb32(pb, len);
879 avio_write(pb, buf, len);
880 break;
881 }
882
883 buf += len;
884 }
885
886 end:
887 av_free(start);
888 if (ps_count)
889 *ps_count = num_ps;
890 return ret;
891 }
892
893 int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
894 int *size, int filter_ps, int *ps_count)
895 {
896 AVIOContext *pb;
897 int ret;
898
899 ret = avio_open_dyn_buf(&pb);
900 if (ret < 0)
901 return ret;
902
903 ret = ff_vvc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
904 if (ret < 0) {
905 ffio_free_dyn_buf(&pb);
906 return ret;
907 }
908
909 *size = avio_close_dyn_buf(pb, buf_out);
910
911 return 0;
912 }
913
914 int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data,
915 int size, int ps_array_completeness)
916 {
917 VVCDecoderConfigurationRecord vvcc;
918 uint8_t *buf, *end, *start;
919 int ret;
920
921 if (size < 6) {
922 /* We can't write a valid vvcc from the provided data */
923 return AVERROR_INVALIDDATA;
924 } else if ((*data & 0xf8) == 0xf8) {
925 /* Data is already vvcc-formatted */
926 avio_write(pb, data, size);
927 return 0;
928 } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
929 /* Not a valid Annex B start code prefix */
930 return AVERROR_INVALIDDATA;
931 }
932
933 ret = ff_avc_parse_nal_units_buf(data, &start, &size);
934 if (ret < 0)
935 return ret;
936
937 vvcc_init(&vvcc);
938
939 buf = start;
940 end = start + size;
941
942 while (end - buf > 4) {
943 uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
944 uint8_t type = (buf[5] >> 3);
945
946 buf += 4;
947
948 switch (type) {
949 case VVC_OPI_NUT:
950 case VVC_VPS_NUT:
951 case VVC_SPS_NUT:
952 case VVC_PPS_NUT:
953 case VVC_PREFIX_SEI_NUT:
954 case VVC_SUFFIX_SEI_NUT:
955 ret = vvcc_add_nal_unit(buf, len, ps_array_completeness, &vvcc);
956 if (ret < 0)
957 goto end;
958 break;
959 default:
960 break;
961 }
962
963 buf += len;
964 }
965
966 ret = vvcc_write(pb, &vvcc);
967
968 end:
969 vvcc_close(&vvcc);
970 av_free(start);
971 return ret;
972 }
973